cgen.nim 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the C code generator.
  10. import
  11. ast, astalgo, trees, platform, magicsys, extccomp, options,
  12. nversion, nimsets, msgs, bitsets, idents, types,
  13. ccgutils, ropes, wordrecg, treetab, cgmeth,
  14. rodutils, renderer, cgendata, aliases,
  15. lowerings, ndi, lineinfos, pathutils, transf,
  16. injectdestructors, astmsgs, modulepaths, backendpragmas
  17. from expanddefaults import caseObjDefaultBranch
  18. import pipelineutils
  19. when defined(nimPreviewSlimSystem):
  20. import std/assertions
  21. when not defined(leanCompiler):
  22. import spawn, semparallel
  23. import std/strutils except `%`, addf # collides with ropes.`%`
  24. from ic / ic import ModuleBackendFlag
  25. import std/[dynlib, math, tables, sets, os, intsets, hashes]
  26. when not declared(dynlib.libCandidates):
  27. proc libCandidates(s: string, dest: var seq[string]) =
  28. ## given a library name pattern `s` write possible library names to `dest`.
  29. var le = strutils.find(s, '(')
  30. var ri = strutils.find(s, ')', le+1)
  31. if le >= 0 and ri > le:
  32. var prefix = substr(s, 0, le - 1)
  33. var suffix = substr(s, ri + 1)
  34. for middle in split(substr(s, le + 1, ri - 1), '|'):
  35. libCandidates(prefix & middle & suffix, dest)
  36. else:
  37. dest.add(s)
  38. when options.hasTinyCBackend:
  39. import tccgen
  40. proc hcrOn(m: BModule): bool = m.config.hcrOn
  41. proc hcrOn(p: BProc): bool = p.module.config.hcrOn
  42. proc addForwardedProc(m: BModule, prc: PSym) =
  43. m.g.forwardedProcs.add(prc)
  44. proc findPendingModule(m: BModule, s: PSym): BModule =
  45. # TODO fixme
  46. if m.config.symbolFiles == v2Sf:
  47. let ms = s.itemId.module #getModule(s)
  48. result = m.g.modules[ms]
  49. else:
  50. var ms = getModule(s)
  51. result = m.g.modules[ms.position]
  52. proc initLoc(k: TLocKind, lode: PNode, s: TStorageLoc, flags: TLocFlags = {}): TLoc =
  53. result = TLoc(k: k, storage: s, lode: lode,
  54. r: "", flags: flags
  55. )
  56. proc fillLoc(a: var TLoc, k: TLocKind, lode: PNode, r: Rope, s: TStorageLoc) {.inline.} =
  57. # fills the loc if it is not already initialized
  58. if a.k == locNone:
  59. a.k = k
  60. a.lode = lode
  61. a.storage = s
  62. if a.r == "": a.r = r
  63. proc fillLoc(a: var TLoc, k: TLocKind, lode: PNode, s: TStorageLoc) {.inline.} =
  64. # fills the loc if it is not already initialized
  65. if a.k == locNone:
  66. a.k = k
  67. a.lode = lode
  68. a.storage = s
  69. proc t(a: TLoc): PType {.inline.} =
  70. if a.lode.kind == nkSym:
  71. result = a.lode.sym.typ
  72. else:
  73. result = a.lode.typ
  74. proc lodeTyp(t: PType): PNode =
  75. result = newNode(nkEmpty)
  76. result.typ = t
  77. proc isSimpleConst(typ: PType): bool =
  78. let t = skipTypes(typ, abstractVar)
  79. result = t.kind notin
  80. {tyTuple, tyObject, tyArray, tySet, tySequence} and not
  81. (t.kind == tyProc and t.callConv == ccClosure)
  82. proc useHeader(m: BModule, sym: PSym) =
  83. if lfHeader in sym.loc.flags:
  84. assert(sym.annex != nil)
  85. let str = getStr(sym.annex.path)
  86. m.includeHeader(str)
  87. proc cgsym(m: BModule, name: string)
  88. proc cgsymValue(m: BModule, name: string): Rope
  89. proc getCFile(m: BModule): AbsoluteFile
  90. proc getModuleDllPath(m: BModule): Rope =
  91. let (dir, name, ext) = splitFile(getCFile(m))
  92. let filename = strutils.`%`(platform.OS[m.g.config.target.targetOS].dllFrmt, [name & ext])
  93. result = makeCString(dir.string & "/" & filename)
  94. proc getModuleDllPath(m: BModule, module: int): Rope =
  95. result = getModuleDllPath(m.g.modules[module])
  96. proc getModuleDllPath(m: BModule, s: PSym): Rope =
  97. result = getModuleDllPath(m.g.modules[s.itemId.module])
  98. import std/macros
  99. proc cgFormatValue(result: var string; value: string) =
  100. result.add value
  101. proc cgFormatValue(result: var string; value: BiggestInt) =
  102. result.addInt value
  103. proc cgFormatValue(result: var string; value: Int128) =
  104. result.addInt128 value
  105. # TODO: please document
  106. macro ropecg(m: BModule, frmt: static[FormatStr], args: untyped): Rope =
  107. args.expectKind nnkBracket
  108. # echo "ropecg ", newLit(frmt).repr, ", ", args.repr
  109. var i = 0
  110. result = nnkStmtListExpr.newTree()
  111. result.add quote do:
  112. assert `m` != nil
  113. let resVar = genSym(nskVar, "res")
  114. # during `koch boot` the median of all generates strings from this
  115. # macro is around 40 bytes in length.
  116. result.add newVarStmt(resVar, newCall(bindSym"newStringOfCap", newLit(80)))
  117. let formatValue = bindSym"cgFormatValue"
  118. var num = 0
  119. var strLit = ""
  120. template flushStrLit() =
  121. if strLit != "":
  122. result.add newCall(ident "add", resVar, newLit(strLit))
  123. strLit.setLen 0
  124. while i < frmt.len:
  125. if frmt[i] == '$':
  126. inc(i) # skip '$'
  127. case frmt[i]
  128. of '$':
  129. strLit.add '$'
  130. inc(i)
  131. of '#':
  132. flushStrLit()
  133. inc(i)
  134. result.add newCall(formatValue, resVar, args[num])
  135. inc(num)
  136. of '^':
  137. flushStrLit()
  138. inc(i)
  139. result.add newCall(formatValue, resVar, args[^1])
  140. inc(num)
  141. of '0'..'9':
  142. var j = 0
  143. while true:
  144. j = (j * 10) + ord(frmt[i]) - ord('0')
  145. inc(i)
  146. if i >= frmt.len or not (frmt[i] in {'0'..'9'}): break
  147. num = j
  148. if j > args.len:
  149. error("ropes: invalid format string " & newLit(frmt).repr & " args.len: " & $args.len)
  150. flushStrLit()
  151. result.add newCall(formatValue, resVar, args[j-1])
  152. of 'n':
  153. flushStrLit()
  154. result.add quote do:
  155. if optLineDir notin `m`.config.options:
  156. `resVar`.add("\L")
  157. inc(i)
  158. of 'N':
  159. strLit.add "\L"
  160. inc(i)
  161. else:
  162. error("ropes: invalid format string $" & frmt[i])
  163. elif frmt[i] == '#' and frmt[i+1] in IdentStartChars:
  164. inc(i)
  165. var j = i
  166. while frmt[j] in IdentChars: inc(j)
  167. var ident = newLit(substr(frmt, i, j-1))
  168. i = j
  169. flushStrLit()
  170. result.add newCall(formatValue, resVar, newCall(ident"cgsymValue", m, ident))
  171. elif frmt[i] == '#' and frmt[i+1] == '$':
  172. inc(i, 2)
  173. var j = 0
  174. while frmt[i] in Digits:
  175. j = (j * 10) + ord(frmt[i]) - ord('0')
  176. inc(i)
  177. let ident = args[j-1]
  178. flushStrLit()
  179. result.add newCall(formatValue, resVar, newCall(ident"cgsymValue", m, ident))
  180. elif frmt[i] == '#' and frmt[i+1] == '#':
  181. inc(i, 2)
  182. strLit.add("#")
  183. else:
  184. strLit.add(frmt[i])
  185. inc(i)
  186. flushStrLit()
  187. result.add newCall(ident"rope", resVar)
  188. proc addIndent(p: BProc; result: var Rope) =
  189. var i = result.len
  190. let newLen = i + p.blocks.len
  191. result.setLen newLen
  192. while i < newLen:
  193. result[i] = '\t'
  194. inc i
  195. template appcg(m: BModule, c: var Rope, frmt: FormatStr,
  196. args: untyped) =
  197. c.add(ropecg(m, frmt, args))
  198. template appcg(m: BModule, sec: TCFileSection, frmt: FormatStr,
  199. args: untyped) =
  200. m.s[sec].add(ropecg(m, frmt, args))
  201. template appcg(p: BProc, sec: TCProcSection, frmt: FormatStr,
  202. args: untyped) =
  203. p.s(sec).add(ropecg(p.module, frmt, args))
  204. template line(p: BProc, sec: TCProcSection, r: string) =
  205. addIndent p, p.s(sec)
  206. p.s(sec).add(r)
  207. template lineF(p: BProc, sec: TCProcSection, frmt: FormatStr,
  208. args: untyped) =
  209. addIndent p, p.s(sec)
  210. p.s(sec).add(frmt % args)
  211. template lineCg(p: BProc, sec: TCProcSection, frmt: FormatStr,
  212. args: untyped) =
  213. addIndent p, p.s(sec)
  214. p.s(sec).add(ropecg(p.module, frmt, args))
  215. template linefmt(p: BProc, sec: TCProcSection, frmt: FormatStr,
  216. args: untyped) =
  217. addIndent p, p.s(sec)
  218. p.s(sec).add(ropecg(p.module, frmt, args))
  219. proc safeLineNm(info: TLineInfo): int =
  220. result = toLinenumber(info)
  221. if result < 0: result = 0 # negative numbers are not allowed in #line
  222. proc genCLineDir(r: var Rope, filename: string, line: int; conf: ConfigRef) =
  223. assert line >= 0
  224. if optLineDir in conf.options and line > 0:
  225. r.addf("\n#line $2 $1\n",
  226. [rope(makeSingleLineCString(filename)), rope(line)])
  227. proc genCLineDir(r: var Rope, filename: string, line: int; p: BProc; info: TLineInfo; lastFileIndex: FileIndex) =
  228. assert line >= 0
  229. if optLineDir in p.config.options and line > 0:
  230. if lastFileIndex == info.fileIndex:
  231. r.addf("\n#line $1\n", [rope(line)])
  232. else:
  233. r.addf("\n#line $2 $1\n",
  234. [rope(makeSingleLineCString(filename)), rope(line)])
  235. proc genCLineDir(r: var Rope, info: TLineInfo; conf: ConfigRef) =
  236. if optLineDir in conf.options:
  237. genCLineDir(r, toFullPath(conf, info), info.safeLineNm, conf)
  238. proc freshLineInfo(p: BProc; info: TLineInfo): bool =
  239. if p.lastLineInfo.line != info.line or
  240. p.lastLineInfo.fileIndex != info.fileIndex:
  241. p.lastLineInfo.line = info.line
  242. p.lastLineInfo.fileIndex = info.fileIndex
  243. result = true
  244. else:
  245. result = false
  246. proc genCLineDir(r: var Rope, p: BProc, info: TLineInfo; conf: ConfigRef) =
  247. if optLineDir in conf.options:
  248. let lastFileIndex = p.lastLineInfo.fileIndex
  249. if freshLineInfo(p, info):
  250. genCLineDir(r, toFullPath(conf, info), info.safeLineNm, p, info, lastFileIndex)
  251. proc genLineDir(p: BProc, t: PNode) =
  252. if p == p.module.preInitProc: return
  253. let line = t.info.safeLineNm
  254. if optEmbedOrigSrc in p.config.globalOptions:
  255. p.s(cpsStmts).add("//" & sourceLine(p.config, t.info) & "\L")
  256. let lastFileIndex = p.lastLineInfo.fileIndex
  257. let freshLine = freshLineInfo(p, t.info)
  258. if freshLine:
  259. genCLineDir(p.s(cpsStmts), toFullPath(p.config, t.info), line, p, t.info, lastFileIndex)
  260. if ({optLineTrace, optStackTrace} * p.options == {optLineTrace, optStackTrace}) and
  261. (p.prc == nil or sfPure notin p.prc.flags) and t.info.fileIndex != InvalidFileIdx:
  262. if freshLine:
  263. if lastFileIndex == t.info.fileIndex:
  264. linefmt(p, cpsStmts, "nimln_($1);",
  265. [line])
  266. else:
  267. linefmt(p, cpsStmts, "nimlf_($1, $2);",
  268. [line, quotedFilename(p.config, t.info)])
  269. proc accessThreadLocalVar(p: BProc, s: PSym)
  270. proc emulatedThreadVars(conf: ConfigRef): bool {.inline.}
  271. proc genProc(m: BModule, prc: PSym)
  272. proc raiseInstr(p: BProc; result: var Rope)
  273. template compileToCpp(m: BModule): untyped =
  274. m.config.backend == backendCpp or sfCompileToCpp in m.module.flags
  275. proc getTempName(m: BModule): Rope =
  276. result = m.tmpBase & rope(m.labels)
  277. inc m.labels
  278. proc rdLoc(a: TLoc): Rope =
  279. # 'read' location (deref if indirect)
  280. if lfIndirect in a.flags:
  281. result = "(*" & a.r & ")"
  282. else:
  283. result = a.r
  284. proc addRdLoc(a: TLoc; result: var Rope) =
  285. if lfIndirect in a.flags:
  286. result.add "(*" & a.r & ")"
  287. else:
  288. result.add a.r
  289. proc lenField(p: BProc): Rope {.inline.} =
  290. result = rope(if p.module.compileToCpp: "len" else: "Sup.len")
  291. proc lenExpr(p: BProc; a: TLoc): Rope =
  292. if optSeqDestructors in p.config.globalOptions:
  293. result = rdLoc(a) & ".len"
  294. else:
  295. result = "($1 ? $1->$2 : 0)" % [rdLoc(a), lenField(p)]
  296. proc dataFieldAccessor(p: BProc, sym: Rope): Rope =
  297. if optSeqDestructors in p.config.globalOptions:
  298. result = "(" & sym & ").p"
  299. else:
  300. result = sym
  301. proc dataField(p: BProc): Rope =
  302. if optSeqDestructors in p.config.globalOptions:
  303. result = rope".p->data"
  304. else:
  305. result = rope"->data"
  306. proc genProcPrototype(m: BModule, sym: PSym)
  307. include ccgliterals
  308. include ccgtypes
  309. # ------------------------------ Manager of temporaries ------------------
  310. template mapTypeChooser(n: PNode): TSymKind =
  311. (if n.kind == nkSym: n.sym.kind else: skVar)
  312. template mapTypeChooser(a: TLoc): TSymKind = mapTypeChooser(a.lode)
  313. proc addAddrLoc(conf: ConfigRef; a: TLoc; result: var Rope) =
  314. if lfIndirect notin a.flags and mapType(conf, a.t, mapTypeChooser(a) == skParam) != ctArray:
  315. result.add "(&" & a.r & ")"
  316. else:
  317. result.add a.r
  318. proc addrLoc(conf: ConfigRef; a: TLoc): Rope =
  319. if lfIndirect notin a.flags and mapType(conf, a.t, mapTypeChooser(a) == skParam) != ctArray:
  320. result = "(&" & a.r & ")"
  321. else:
  322. result = a.r
  323. proc byRefLoc(p: BProc; a: TLoc): Rope =
  324. if lfIndirect notin a.flags and mapType(p.config, a.t, mapTypeChooser(a) == skParam) != ctArray and not
  325. p.module.compileToCpp:
  326. result = "(&" & a.r & ")"
  327. else:
  328. result = a.r
  329. proc rdCharLoc(a: TLoc): Rope =
  330. # read a location that may need a char-cast:
  331. result = rdLoc(a)
  332. if skipTypes(a.t, abstractRange).kind == tyChar:
  333. result = "((NU8)($1))" % [result]
  334. type
  335. TAssignmentFlag = enum
  336. needToCopy
  337. needTempForOpenArray
  338. TAssignmentFlags = set[TAssignmentFlag]
  339. proc genObjConstr(p: BProc, e: PNode, d: var TLoc)
  340. proc rawConstExpr(p: BProc, n: PNode; d: var TLoc)
  341. proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags)
  342. type
  343. ObjConstrMode = enum
  344. constructObj,
  345. constructRefObj
  346. proc genObjectInit(p: BProc, section: TCProcSection, t: PType, a: var TLoc,
  347. mode: ObjConstrMode) =
  348. #if optNimV2 in p.config.globalOptions: return
  349. case analyseObjectWithTypeField(t)
  350. of frNone:
  351. discard
  352. of frHeader:
  353. var r = rdLoc(a)
  354. if mode == constructRefObj: r = "(*$1)" % [r]
  355. var s = skipTypes(t, abstractInst)
  356. if not p.module.compileToCpp:
  357. while s.kind == tyObject and s[0] != nil:
  358. r.add(".Sup")
  359. s = skipTypes(s[0], skipPtrs)
  360. if optTinyRtti in p.config.globalOptions:
  361. linefmt(p, section, "$1.m_type = $2;$n", [r, genTypeInfoV2(p.module, t, a.lode.info)])
  362. else:
  363. linefmt(p, section, "$1.m_type = $2;$n", [r, genTypeInfoV1(p.module, t, a.lode.info)])
  364. of frEmbedded:
  365. if optTinyRtti in p.config.globalOptions:
  366. var tmp: TLoc = default(TLoc)
  367. if mode == constructRefObj:
  368. let objType = t.skipTypes(abstractInst+{tyRef})
  369. rawConstExpr(p, newNodeIT(nkType, a.lode.info, objType), tmp)
  370. linefmt(p, cpsStmts,
  371. "#nimCopyMem((void*)$1, (NIM_CONST void*)&$2, sizeof($3));$n",
  372. [rdLoc(a), rdLoc(tmp), getTypeDesc(p.module, objType, descKindFromSymKind mapTypeChooser(a))])
  373. else:
  374. rawConstExpr(p, newNodeIT(nkType, a.lode.info, t), tmp)
  375. genAssignment(p, a, tmp, {})
  376. else:
  377. # worst case for performance:
  378. var r = if mode == constructObj: addrLoc(p.config, a) else: rdLoc(a)
  379. linefmt(p, section, "#objectInit($1, $2);$n", [r, genTypeInfoV1(p.module, t, a.lode.info)])
  380. if isException(t):
  381. var r = rdLoc(a)
  382. if mode == constructRefObj: r = "(*$1)" % [r]
  383. var s = skipTypes(t, abstractInst)
  384. if not p.module.compileToCpp:
  385. while s.kind == tyObject and s[0] != nil and s.sym.magic != mException:
  386. r.add(".Sup")
  387. s = skipTypes(s[0], skipPtrs)
  388. linefmt(p, section, "$1.name = $2;$n", [r, makeCString(t.skipTypes(abstractInst).sym.name.s)])
  389. proc genRefAssign(p: BProc, dest, src: TLoc)
  390. proc isComplexValueType(t: PType): bool {.inline.} =
  391. let t = t.skipTypes(abstractInst + tyUserTypeClasses)
  392. result = t.kind in {tyArray, tySet, tyTuple, tyObject, tyOpenArray} or
  393. (t.kind == tyProc and t.callConv == ccClosure)
  394. include ccgreset
  395. proc resetLoc(p: BProc, loc: var TLoc) =
  396. let containsGcRef = optSeqDestructors notin p.config.globalOptions and containsGarbageCollectedRef(loc.t)
  397. let typ = skipTypes(loc.t, abstractVarRange)
  398. if isImportedCppType(typ): return
  399. if optSeqDestructors in p.config.globalOptions and typ.kind in {tyString, tySequence}:
  400. assert loc.r != ""
  401. let atyp = skipTypes(loc.t, abstractInst)
  402. if atyp.kind in {tyVar, tyLent}:
  403. linefmt(p, cpsStmts, "$1->len = 0; $1->p = NIM_NIL;$n", [rdLoc(loc)])
  404. else:
  405. linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)])
  406. elif not isComplexValueType(typ):
  407. if containsGcRef:
  408. var nilLoc: TLoc = initLoc(locTemp, loc.lode, OnStack)
  409. nilLoc.r = rope("NIM_NIL")
  410. genRefAssign(p, loc, nilLoc)
  411. else:
  412. linefmt(p, cpsStmts, "$1 = 0;$n", [rdLoc(loc)])
  413. else:
  414. if loc.storage != OnStack and containsGcRef:
  415. specializeReset(p, loc)
  416. when false:
  417. linefmt(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
  418. [addrLoc(p.config, loc), genTypeInfoV1(p.module, loc.t, loc.lode.info)])
  419. # XXX: generated reset procs should not touch the m_type
  420. # field, so disabling this should be safe:
  421. genObjectInit(p, cpsStmts, loc.t, loc, constructObj)
  422. else:
  423. # array passed as argument decayed into pointer, bug #7332
  424. # so we use getTypeDesc here rather than rdLoc(loc)
  425. let tyDesc = getTypeDesc(p.module, loc.t, descKindFromSymKind mapTypeChooser(loc))
  426. if p.module.compileToCpp and isOrHasImportedCppType(typ):
  427. if lfIndirect in loc.flags:
  428. #C++ cant be just zeroed. We need to call the ctors
  429. var tmp = getTemp(p, loc.t)
  430. linefmt(p, cpsStmts,"#nimCopyMem((void*)$1, (NIM_CONST void*)$2, sizeof($3));$n",
  431. [addrLoc(p.config, loc), addrLoc(p.config, tmp), tyDesc])
  432. else:
  433. linefmt(p, cpsStmts, "#nimZeroMem((void*)$1, sizeof($2));$n",
  434. [addrLoc(p.config, loc), tyDesc])
  435. # XXX: We can be extra clever here and call memset only
  436. # on the bytes following the m_type field?
  437. genObjectInit(p, cpsStmts, loc.t, loc, constructObj)
  438. proc constructLoc(p: BProc, loc: var TLoc, isTemp = false) =
  439. let typ = loc.t
  440. if optSeqDestructors in p.config.globalOptions and skipTypes(typ, abstractInst + {tyStatic}).kind in {tyString, tySequence}:
  441. linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)])
  442. elif not isComplexValueType(typ):
  443. if containsGarbageCollectedRef(loc.t):
  444. var nilLoc: TLoc = initLoc(locTemp, loc.lode, OnStack)
  445. nilLoc.r = rope("NIM_NIL")
  446. genRefAssign(p, loc, nilLoc)
  447. else:
  448. linefmt(p, cpsStmts, "$1 = ($2)0;$n", [rdLoc(loc),
  449. getTypeDesc(p.module, typ, descKindFromSymKind mapTypeChooser(loc))])
  450. else:
  451. if not isTemp or containsGarbageCollectedRef(loc.t):
  452. # don't use nimZeroMem for temporary values for performance if we can
  453. # avoid it:
  454. if not isOrHasImportedCppType(typ):
  455. linefmt(p, cpsStmts, "#nimZeroMem((void*)$1, sizeof($2));$n",
  456. [addrLoc(p.config, loc), getTypeDesc(p.module, typ, descKindFromSymKind mapTypeChooser(loc))])
  457. genObjectInit(p, cpsStmts, loc.t, loc, constructObj)
  458. proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) =
  459. if sfNoInit notin v.flags:
  460. # we know it is a local variable and thus on the stack!
  461. # If ``not immediateAsgn`` it is not initialized in a binding like
  462. # ``var v = X`` and thus we need to init it.
  463. # If ``v`` contains a GC-ref we may pass it to ``unsureAsgnRef`` somehow
  464. # which requires initialization. However this can really only happen if
  465. # ``var v = X()`` gets transformed into ``X(&v)``.
  466. # Nowadays the logic in ccgcalls deals with this case however.
  467. if not immediateAsgn:
  468. constructLoc(p, v.loc)
  469. proc getTemp(p: BProc, t: PType, needsInit=false): TLoc =
  470. inc(p.labels)
  471. result = TLoc(r: "T" & rope(p.labels) & "_", k: locTemp, lode: lodeTyp t,
  472. storage: OnStack, flags: {})
  473. if p.module.compileToCpp and isOrHasImportedCppType(t):
  474. linefmt(p, cpsLocals, "$1 $2$3;$n", [getTypeDesc(p.module, t, dkVar), result.r,
  475. genCppInitializer(p.module, p, t)])
  476. else:
  477. linefmt(p, cpsLocals, "$1 $2;$n", [getTypeDesc(p.module, t, dkVar), result.r])
  478. constructLoc(p, result, not needsInit)
  479. when false:
  480. # XXX Introduce a compiler switch in order to detect these easily.
  481. if getSize(p.config, t) > 1024 * 1024:
  482. if p.prc != nil:
  483. echo "ENORMOUS TEMPORARY! ", p.config $ p.prc.info
  484. else:
  485. echo "ENORMOUS TEMPORARY! ", p.config $ p.lastLineInfo
  486. writeStackTrace()
  487. proc getTempCpp(p: BProc, t: PType, value: Rope): TLoc =
  488. inc(p.labels)
  489. result = TLoc(r: "T" & rope(p.labels) & "_", k: locTemp, lode: lodeTyp t,
  490. storage: OnStack, flags: {})
  491. linefmt(p, cpsStmts, "auto $1 = $2;$n", [result.r, value])
  492. proc getIntTemp(p: BProc): TLoc =
  493. inc(p.labels)
  494. result = TLoc(r: "T" & rope(p.labels) & "_", k: locTemp,
  495. storage: OnStack, lode: lodeTyp getSysType(p.module.g.graph, unknownLineInfo, tyInt),
  496. flags: {})
  497. linefmt(p, cpsLocals, "NI $1;$n", [result.r])
  498. proc localVarDecl(p: BProc; n: PNode): Rope =
  499. result = ""
  500. let s = n.sym
  501. if s.loc.k == locNone:
  502. fillLocalName(p, s)
  503. fillLoc(s.loc, locLocalVar, n, OnStack)
  504. if s.kind == skLet: incl(s.loc.flags, lfNoDeepCopy)
  505. if s.kind in {skLet, skVar, skField, skForVar} and s.alignment > 0:
  506. result.addf("NIM_ALIGN($1) ", [rope(s.alignment)])
  507. genCLineDir(result, p, n.info, p.config)
  508. result.add getTypeDesc(p.module, s.typ, dkVar)
  509. if sfCodegenDecl notin s.flags:
  510. if sfRegister in s.flags: result.add(" register")
  511. #elif skipTypes(s.typ, abstractInst).kind in GcTypeKinds:
  512. # decl.add(" GC_GUARD")
  513. if sfVolatile in s.flags: result.add(" volatile")
  514. if sfNoalias in s.flags: result.add(" NIM_NOALIAS")
  515. result.add(" ")
  516. result.add(s.loc.r)
  517. else:
  518. result = runtimeFormat(s.cgDeclFrmt, [result, s.loc.r])
  519. proc assignLocalVar(p: BProc, n: PNode) =
  520. #assert(s.loc.k == locNone) # not yet assigned
  521. # this need not be fulfilled for inline procs; they are regenerated
  522. # for each module that uses them!
  523. let nl = if optLineDir in p.config.options: "" else: "\n"
  524. var decl = localVarDecl(p, n)
  525. if p.module.compileToCpp and isOrHasImportedCppType(n.typ):
  526. decl.add genCppInitializer(p.module, p, n.typ)
  527. decl.add ";" & nl
  528. line(p, cpsLocals, decl)
  529. include ccgthreadvars
  530. proc varInDynamicLib(m: BModule, sym: PSym)
  531. proc treatGlobalDifferentlyForHCR(m: BModule, s: PSym): bool =
  532. return m.hcrOn and {sfThread, sfGlobal} * s.flags == {sfGlobal} and
  533. ({lfNoDecl, lfHeader} * s.loc.flags == {})
  534. # and s.owner.kind == skModule # owner isn't always a module (global pragma on local var)
  535. # and s.loc.k == locGlobalVar # loc isn't always initialized when this proc is used
  536. proc genGlobalVarDecl(p: BProc, n: PNode; td, value: Rope; decl: var Rope) =
  537. let s = n.sym
  538. if sfCodegenDecl notin s.flags:
  539. if s.kind in {skLet, skVar, skField, skForVar} and s.alignment > 0:
  540. decl.addf "NIM_ALIGN($1) ", [rope(s.alignment)]
  541. if p.hcrOn: decl.add("static ")
  542. elif sfImportc in s.flags: decl.add("extern ")
  543. elif lfExportLib in s.loc.flags: decl.add("N_LIB_EXPORT_VAR ")
  544. else: decl.add("N_LIB_PRIVATE ")
  545. if s.kind == skLet and value != "": decl.add("NIM_CONST ")
  546. decl.add(td)
  547. if p.hcrOn: decl.add("*")
  548. if sfRegister in s.flags: decl.add(" register")
  549. if sfVolatile in s.flags: decl.add(" volatile")
  550. if sfNoalias in s.flags: decl.add(" NIM_NOALIAS")
  551. else:
  552. if value != "":
  553. decl = runtimeFormat(s.cgDeclFrmt & " = $#;$n", [td, s.loc.r, value])
  554. else:
  555. decl = runtimeFormat(s.cgDeclFrmt & ";$n", [td, s.loc.r])
  556. proc genCppVarForCtor(p: BProc; call: PNode; decl: var Rope)
  557. proc callGlobalVarCppCtor(p: BProc; v: PSym; vn, value: PNode) =
  558. let s = vn.sym
  559. fillBackendName(p.module, s)
  560. fillLoc(s.loc, locGlobalVar, vn, OnHeap)
  561. var decl: Rope = ""
  562. let td = getTypeDesc(p.module, vn.sym.typ, dkVar)
  563. genGlobalVarDecl(p, vn, td, "", decl)
  564. decl.add " " & $s.loc.r
  565. genCppVarForCtor(p, value, decl)
  566. p.module.s[cfsVars].add decl
  567. proc assignGlobalVar(p: BProc, n: PNode; value: Rope) =
  568. let s = n.sym
  569. if s.loc.k == locNone:
  570. fillBackendName(p.module, s)
  571. fillLoc(s.loc, locGlobalVar, n, OnHeap)
  572. if treatGlobalDifferentlyForHCR(p.module, s): incl(s.loc.flags, lfIndirect)
  573. if lfDynamicLib in s.loc.flags:
  574. var q = findPendingModule(p.module, s)
  575. if q != nil and not containsOrIncl(q.declaredThings, s.id):
  576. varInDynamicLib(q, s)
  577. else:
  578. s.loc.r = mangleDynLibProc(s)
  579. if value != "":
  580. internalError(p.config, n.info, ".dynlib variables cannot have a value")
  581. return
  582. useHeader(p.module, s)
  583. if lfNoDecl in s.loc.flags: return
  584. if not containsOrIncl(p.module.declaredThings, s.id):
  585. if sfThread in s.flags:
  586. declareThreadVar(p.module, s, sfImportc in s.flags)
  587. if value != "":
  588. internalError(p.config, n.info, ".threadvar variables cannot have a value")
  589. else:
  590. var decl: Rope = ""
  591. let td = getTypeDesc(p.module, s.loc.t, dkVar)
  592. genGlobalVarDecl(p, n, td, value, decl)
  593. if s.constraint.isNil:
  594. if value != "":
  595. if p.module.compileToCpp and value.startsWith "{{}":
  596. # TODO: taking this branch, re"\{\{\}(,\s\{\})*\}" might be emitted, resulting in
  597. # either warnings (GCC 12.2+) or errors (Clang 15, MSVC 19.3+) of C++11+ compilers **when
  598. # explicit constructors are around** due to overload resolution rules in place [^0][^1][^2]
  599. # *Workaround* here: have C++'s static initialization mechanism do the default init work,
  600. # for us lacking a deeper knowledge of an imported object's constructors' ex-/implicitness
  601. # (so far) *and yet* trying to achieve default initialization.
  602. # Still, generating {}s in genConstObjConstr() just to omit them here is faaaar from ideal;
  603. # need to figure out a better way, possibly by keeping around more data about the
  604. # imported objects' contructors?
  605. #
  606. # [^0]: https://en.cppreference.com/w/cpp/language/aggregate_initialization
  607. # [^1]: https://cplusplus.github.io/CWG/issues/1518.html
  608. # [^2]: https://eel.is/c++draft/over.match.ctor
  609. decl.addf(" $1;$n", [s.loc.r])
  610. else:
  611. decl.addf(" $1 = $2;$n", [s.loc.r, value])
  612. else:
  613. decl.addf(" $1;$n", [s.loc.r])
  614. p.module.s[cfsVars].add(decl)
  615. if p.withinLoop > 0 and value == "":
  616. # fixes tests/run/tzeroarray:
  617. resetLoc(p, s.loc)
  618. proc assignParam(p: BProc, s: PSym, retType: PType) =
  619. assert(s.loc.r != "")
  620. scopeMangledParam(p, s)
  621. proc fillProcLoc(m: BModule; n: PNode) =
  622. let sym = n.sym
  623. if sym.loc.k == locNone:
  624. fillBackendName(m, sym)
  625. fillLoc(sym.loc, locProc, n, OnStack)
  626. proc getLabel(p: BProc): TLabel =
  627. inc(p.labels)
  628. result = "LA" & rope(p.labels) & "_"
  629. proc fixLabel(p: BProc, labl: TLabel) =
  630. p.s(cpsStmts).add("$1: ;$n" % [labl])
  631. proc genVarPrototype(m: BModule, n: PNode)
  632. proc requestConstImpl(p: BProc, sym: PSym)
  633. proc genStmts(p: BProc, t: PNode)
  634. proc expr(p: BProc, n: PNode, d: var TLoc)
  635. proc putLocIntoDest(p: BProc, d: var TLoc, s: TLoc)
  636. proc intLiteral(i: BiggestInt; result: var Rope)
  637. proc genLiteral(p: BProc, n: PNode; result: var Rope)
  638. proc genOtherArg(p: BProc; ri: PNode; i: int; typ: PType; result: var Rope; argsCounter: var int)
  639. proc raiseExit(p: BProc)
  640. proc initLocExpr(p: BProc, e: PNode, flags: TLocFlags = {}): TLoc =
  641. result = initLoc(locNone, e, OnUnknown, flags)
  642. expr(p, e, result)
  643. proc initLocExprSingleUse(p: BProc, e: PNode): TLoc =
  644. result = initLoc(locNone, e, OnUnknown)
  645. if e.kind in nkCallKinds and (e[0].kind != nkSym or e[0].sym.magic == mNone):
  646. # We cannot check for tfNoSideEffect here because of mutable parameters.
  647. discard "bug #8202; enforce evaluation order for nested calls for C++ too"
  648. # We may need to consider that 'f(g())' cannot be rewritten to 'tmp = g(); f(tmp)'
  649. # if 'tmp' lacks a move/assignment operator.
  650. if e[0].kind == nkSym and sfCompileToCpp in e[0].sym.flags:
  651. result.flags.incl lfSingleUse
  652. else:
  653. result.flags.incl lfSingleUse
  654. expr(p, e, result)
  655. include ccgcalls, "ccgstmts.nim"
  656. proc initFrame(p: BProc, procname, filename: Rope): Rope =
  657. const frameDefines = """
  658. $1define nimfr_(proc, file) \
  659. TFrame FR_; \
  660. FR_.procname = proc; FR_.filename = file; FR_.line = 0; FR_.len = 0; #nimFrame(&FR_);
  661. $1define nimfrs_(proc, file, slots, length) \
  662. struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename;NI len;VarSlot s[slots];} FR_; \
  663. FR_.procname = proc; FR_.filename = file; FR_.line = 0; FR_.len = length; #nimFrame((TFrame*)&FR_);
  664. $1define nimln_(n) \
  665. FR_.line = n;
  666. $1define nimlf_(n, file) \
  667. FR_.line = n; FR_.filename = file;
  668. """
  669. if p.module.s[cfsFrameDefines].len == 0:
  670. appcg(p.module, p.module.s[cfsFrameDefines], frameDefines, ["#"])
  671. cgsym(p.module, "nimFrame")
  672. result = ropecg(p.module, "\tnimfr_($1, $2);$n", [procname, filename])
  673. proc initFrameNoDebug(p: BProc; frame, procname, filename: Rope; line: int): Rope =
  674. cgsym(p.module, "nimFrame")
  675. p.blocks[0].sections[cpsLocals].addf("TFrame $1;$n", [frame])
  676. result = ropecg(p.module, "\t$1.procname = $2; $1.filename = $3; " &
  677. " $1.line = $4; $1.len = -1; nimFrame(&$1);$n",
  678. [frame, procname, filename, line])
  679. proc deinitFrameNoDebug(p: BProc; frame: Rope): Rope =
  680. result = ropecg(p.module, "\t#popFrameOfAddr(&$1);$n", [frame])
  681. proc deinitFrame(p: BProc): Rope =
  682. result = ropecg(p.module, "\t#popFrame();$n", [])
  683. include ccgexprs
  684. # ----------------------------- dynamic library handling -----------------
  685. # We don't finalize dynamic libs as the OS does this for us.
  686. proc isGetProcAddr(lib: PLib): bool =
  687. let n = lib.path
  688. result = n.kind in nkCallKinds and n.typ != nil and
  689. n.typ.kind in {tyPointer, tyProc}
  690. proc loadDynamicLib(m: BModule, lib: PLib) =
  691. assert(lib != nil)
  692. if not lib.generated:
  693. lib.generated = true
  694. var tmp = getTempName(m)
  695. assert(lib.name == "")
  696. lib.name = tmp # BUGFIX: cgsym has awful side-effects
  697. m.s[cfsVars].addf("static void* $1;$n", [tmp])
  698. if lib.path.kind in {nkStrLit..nkTripleStrLit}:
  699. var s: TStringSeq = @[]
  700. libCandidates(lib.path.strVal, s)
  701. rawMessage(m.config, hintDependency, lib.path.strVal)
  702. var loadlib: Rope = ""
  703. for i in 0..high(s):
  704. inc(m.labels)
  705. if i > 0: loadlib.add("||")
  706. let n = newStrNode(nkStrLit, s[i])
  707. n.info = lib.path.info
  708. appcg(m, loadlib, "($1 = #nimLoadLibrary(", [tmp])
  709. genStringLiteral(m, n, loadlib)
  710. loadlib.addf "))$n", []
  711. appcg(m, m.s[cfsDynLibInit],
  712. "if (!($1)) #nimLoadLibraryError(",
  713. [loadlib])
  714. genStringLiteral(m, lib.path, m.s[cfsDynLibInit])
  715. m.s[cfsDynLibInit].addf ");$n", []
  716. else:
  717. var p = newProc(nil, m)
  718. p.options.excl optStackTrace
  719. p.flags.incl nimErrorFlagDisabled
  720. var dest: TLoc = initLoc(locTemp, lib.path, OnStack)
  721. dest.r = getTempName(m)
  722. appcg(m, m.s[cfsDynLibInit],"$1 $2;$n",
  723. [getTypeDesc(m, lib.path.typ, dkVar), rdLoc(dest)])
  724. expr(p, lib.path, dest)
  725. m.s[cfsVars].add(p.s(cpsLocals))
  726. m.s[cfsDynLibInit].add(p.s(cpsInit))
  727. m.s[cfsDynLibInit].add(p.s(cpsStmts))
  728. appcg(m, m.s[cfsDynLibInit],
  729. "if (!($1 = #nimLoadLibrary($2))) #nimLoadLibraryError($2);$n",
  730. [tmp, rdLoc(dest)])
  731. if lib.name == "": internalError(m.config, "loadDynamicLib")
  732. proc mangleDynLibProc(sym: PSym): Rope =
  733. # we have to build this as a single rope in order not to trip the
  734. # optimization in genInfixCall, see test tests/cpp/t8241.nim
  735. if sfCompilerProc in sym.flags:
  736. # NOTE: sym.loc.r is the external name!
  737. result = rope(sym.name.s)
  738. else:
  739. result = rope(strutils.`%`("Dl_$1_", $sym.id))
  740. proc symInDynamicLib(m: BModule, sym: PSym) =
  741. var lib = sym.annex
  742. let isCall = isGetProcAddr(lib)
  743. var extname = sym.loc.r
  744. if not isCall: loadDynamicLib(m, lib)
  745. var tmp = mangleDynLibProc(sym)
  746. sym.loc.r = tmp # from now on we only need the internal name
  747. sym.typ.sym = nil # generate a new name
  748. inc(m.labels, 2)
  749. if isCall:
  750. let n = lib.path
  751. var a: TLoc = initLocExpr(m.initProc, n[0])
  752. var params = rdLoc(a) & "("
  753. for i in 1..<n.len-1:
  754. a = initLocExpr(m.initProc, n[i])
  755. params.add(rdLoc(a))
  756. params.add(", ")
  757. let load = "\t$1 = ($2) ($3$4));$n" %
  758. [tmp, getTypeDesc(m, sym.typ, dkVar), params, makeCString($extname)]
  759. var last = lastSon(n)
  760. if last.kind == nkHiddenStdConv: last = last[1]
  761. internalAssert(m.config, last.kind == nkStrLit)
  762. let idx = last.strVal
  763. if idx.len == 0:
  764. m.initProc.s(cpsStmts).add(load)
  765. elif idx.len == 1 and idx[0] in {'0'..'9'}:
  766. m.extensionLoaders[idx[0]].add(load)
  767. else:
  768. internalError(m.config, sym.info, "wrong index: " & idx)
  769. else:
  770. appcg(m, m.s[cfsDynLibInit],
  771. "\t$1 = ($2) #nimGetProcAddr($3, $4);$n",
  772. [tmp, getTypeDesc(m, sym.typ, dkVar), lib.name, makeCString($extname)])
  773. m.s[cfsVars].addf("$2 $1;$n", [sym.loc.r, getTypeDesc(m, sym.loc.t, dkVar)])
  774. proc varInDynamicLib(m: BModule, sym: PSym) =
  775. var lib = sym.annex
  776. var extname = sym.loc.r
  777. loadDynamicLib(m, lib)
  778. incl(sym.loc.flags, lfIndirect)
  779. var tmp = mangleDynLibProc(sym)
  780. sym.loc.r = tmp # from now on we only need the internal name
  781. inc(m.labels, 2)
  782. appcg(m, m.s[cfsDynLibInit],
  783. "$1 = ($2*) #nimGetProcAddr($3, $4);$n",
  784. [tmp, getTypeDesc(m, sym.typ, dkVar), lib.name, makeCString($extname)])
  785. m.s[cfsVars].addf("$2* $1;$n",
  786. [sym.loc.r, getTypeDesc(m, sym.loc.t, dkVar)])
  787. proc symInDynamicLibPartial(m: BModule, sym: PSym) =
  788. sym.loc.r = mangleDynLibProc(sym)
  789. sym.typ.sym = nil # generate a new name
  790. proc cgsymImpl(m: BModule; sym: PSym) {.inline.} =
  791. case sym.kind
  792. of skProc, skFunc, skMethod, skConverter, skIterator: genProc(m, sym)
  793. of skVar, skResult, skLet: genVarPrototype(m, newSymNode sym)
  794. of skType: discard getTypeDesc(m, sym.typ)
  795. else: internalError(m.config, "cgsym: " & $sym.kind)
  796. proc cgsym(m: BModule, name: string) =
  797. let sym = magicsys.getCompilerProc(m.g.graph, name)
  798. if sym != nil:
  799. cgsymImpl m, sym
  800. else:
  801. rawMessage(m.config, errGenerated, "system module needs: " & name)
  802. proc cgsymValue(m: BModule, name: string): Rope =
  803. let sym = magicsys.getCompilerProc(m.g.graph, name)
  804. if sym != nil:
  805. cgsymImpl m, sym
  806. else:
  807. rawMessage(m.config, errGenerated, "system module needs: " & name)
  808. result = sym.loc.r
  809. if m.hcrOn and sym != nil and sym.kind in {skProc..skIterator}:
  810. result.addActualSuffixForHCR(m.module, sym)
  811. proc generateHeaders(m: BModule) =
  812. var nimbase = m.config.nimbasePattern
  813. if nimbase == "": nimbase = "nimbase.h"
  814. m.s[cfsHeaders].addf("\L#include \"$1\"\L", [nimbase])
  815. for it in m.headerFiles:
  816. if it[0] == '#':
  817. m.s[cfsHeaders].add(rope(it.replace('`', '"') & "\L"))
  818. elif it[0] notin {'"', '<'}:
  819. m.s[cfsHeaders].addf("#include \"$1\"$N", [rope(it)])
  820. else:
  821. m.s[cfsHeaders].addf("#include $1$N", [rope(it)])
  822. m.s[cfsHeaders].add("""#undef LANGUAGE_C
  823. #undef MIPSEB
  824. #undef MIPSEL
  825. #undef PPC
  826. #undef R3000
  827. #undef R4000
  828. #undef i386
  829. #undef linux
  830. #undef mips
  831. #undef near
  832. #undef far
  833. #undef powerpc
  834. #undef unix
  835. """)
  836. proc openNamespaceNim(namespace: string; result: var Rope) =
  837. result.add("namespace ")
  838. result.add(namespace)
  839. result.add(" {\L")
  840. proc closeNamespaceNim(result: var Rope) =
  841. result.add("}\L")
  842. proc closureSetup(p: BProc, prc: PSym) =
  843. if tfCapturesEnv notin prc.typ.flags: return
  844. # prc.ast[paramsPos].last contains the type we're after:
  845. var ls = lastSon(prc.ast[paramsPos])
  846. if ls.kind != nkSym:
  847. internalError(p.config, prc.info, "closure generation failed")
  848. var env = ls.sym
  849. #echo "created environment: ", env.id, " for ", prc.name.s
  850. assignLocalVar(p, ls)
  851. # generate cast assignment:
  852. if p.config.selectedGC == gcGo:
  853. linefmt(p, cpsStmts, "#unsureAsgnRef((void**) $1, ($2) ClE_0);$n",
  854. [addrLoc(p.config, env.loc), getTypeDesc(p.module, env.typ)])
  855. else:
  856. linefmt(p, cpsStmts, "$1 = ($2) ClE_0;$n",
  857. [rdLoc(env.loc), getTypeDesc(p.module, env.typ)])
  858. const harmless = {nkConstSection, nkTypeSection, nkEmpty, nkCommentStmt, nkTemplateDef,
  859. nkMacroDef, nkMixinStmt, nkBindStmt, nkFormalParams} +
  860. declarativeDefs
  861. proc containsResult(n: PNode): bool =
  862. result = false
  863. case n.kind
  864. of succ(nkEmpty)..pred(nkSym), succ(nkSym)..nkNilLit, harmless:
  865. discard
  866. of nkReturnStmt:
  867. for i in 0..<n.len:
  868. if containsResult(n[i]): return true
  869. result = n.len > 0 and n[0].kind == nkEmpty
  870. of nkSym:
  871. if n.sym.kind == skResult:
  872. result = true
  873. else:
  874. for i in 0..<n.len:
  875. if containsResult(n[i]): return true
  876. proc easyResultAsgn(n: PNode): PNode =
  877. result = nil
  878. case n.kind
  879. of nkStmtList, nkStmtListExpr:
  880. var i = 0
  881. while i < n.len and n[i].kind in harmless: inc i
  882. if i < n.len: result = easyResultAsgn(n[i])
  883. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  884. if n[0].kind == nkSym and n[0].sym.kind == skResult and not containsResult(n[1]):
  885. incl n.flags, nfPreventCg
  886. return n[1]
  887. of nkReturnStmt:
  888. if n.len > 0:
  889. result = easyResultAsgn(n[0])
  890. if result != nil: incl n.flags, nfPreventCg
  891. else: discard
  892. type
  893. InitResultEnum = enum Unknown, InitSkippable, InitRequired
  894. proc allPathsAsgnResult(p: BProc; n: PNode): InitResultEnum =
  895. # Exceptions coming from calls don't have not be considered here:
  896. #
  897. # proc bar(): string = raise newException(...)
  898. #
  899. # proc foo(): string =
  900. # # optimized out: 'reset(result)'
  901. # result = bar()
  902. #
  903. # try:
  904. # a = foo()
  905. # except:
  906. # echo "a was not written to"
  907. #
  908. template allPathsInBranch(it) =
  909. let a = allPathsAsgnResult(p, it)
  910. case a
  911. of InitRequired: return InitRequired
  912. of InitSkippable: discard
  913. of Unknown:
  914. # sticky, but can be overwritten by InitRequired:
  915. result = Unknown
  916. result = Unknown
  917. case n.kind
  918. of nkStmtList, nkStmtListExpr:
  919. for it in n:
  920. result = allPathsAsgnResult(p, it)
  921. if result != Unknown: return result
  922. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  923. if n[0].kind == nkSym and n[0].sym.kind == skResult:
  924. if not containsResult(n[1]): result = InitSkippable
  925. else: result = InitRequired
  926. elif containsResult(n):
  927. result = InitRequired
  928. else:
  929. result = allPathsAsgnResult(p, n[1])
  930. of nkReturnStmt:
  931. if n.len > 0:
  932. if n[0].kind == nkEmpty and result != InitSkippable:
  933. # This is a bare `return` statement, if `result` was not initialized
  934. # anywhere else (or if we're not sure about this) let's require it to be
  935. # initialized. This avoids cases like #9286 where this heuristic lead to
  936. # wrong code being generated.
  937. result = InitRequired
  938. else: result = allPathsAsgnResult(p, n[0])
  939. of nkIfStmt, nkIfExpr:
  940. var exhaustive = false
  941. result = InitSkippable
  942. for it in n:
  943. # Every condition must not use 'result':
  944. if it.len == 2 and containsResult(it[0]):
  945. return InitRequired
  946. if it.len == 1: exhaustive = true
  947. allPathsInBranch(it.lastSon)
  948. # if the 'if' statement is not exhaustive and yet it touched 'result'
  949. # in some way, say Unknown.
  950. if not exhaustive: result = Unknown
  951. of nkCaseStmt:
  952. if containsResult(n[0]): return InitRequired
  953. result = InitSkippable
  954. var exhaustive = skipTypes(n[0].typ,
  955. abstractVarRange-{tyTypeDesc}).kind notin {tyFloat..tyFloat128, tyString, tyCstring}
  956. for i in 1..<n.len:
  957. let it = n[i]
  958. allPathsInBranch(it.lastSon)
  959. if it.kind == nkElse: exhaustive = true
  960. if not exhaustive: result = Unknown
  961. of nkWhileStmt:
  962. # some dubious code can assign the result in the 'while'
  963. # condition and that would be fine. Everything else isn't:
  964. result = allPathsAsgnResult(p, n[0])
  965. if result == Unknown:
  966. result = allPathsAsgnResult(p, n[1])
  967. # we cannot assume that the 'while' loop is really executed at least once:
  968. if result == InitSkippable: result = Unknown
  969. of harmless:
  970. result = Unknown
  971. of nkGotoState, nkBreakState:
  972. # give up for now.
  973. result = InitRequired
  974. of nkSym:
  975. # some path reads from 'result' before it was written to!
  976. if n.sym.kind == skResult: result = InitRequired
  977. of nkTryStmt, nkHiddenTryStmt:
  978. # We need to watch out for the following problem:
  979. # try:
  980. # result = stuffThatRaises()
  981. # except:
  982. # discard "result was not set"
  983. #
  984. # So ... even if the assignment to 'result' is the very first
  985. # assignment this is not good enough! The only pattern we allow for
  986. # is 'finally: result = x'
  987. result = InitSkippable
  988. allPathsInBranch(n[0])
  989. for i in 1..<n.len:
  990. if n[i].kind == nkFinally:
  991. result = allPathsAsgnResult(p, n[i].lastSon)
  992. else:
  993. allPathsInBranch(n[i].lastSon)
  994. of nkCallKinds:
  995. if canRaiseDisp(p, n[0]):
  996. result = InitRequired
  997. else:
  998. for i in 0..<n.safeLen:
  999. allPathsInBranch(n[i])
  1000. of nkRaiseStmt:
  1001. result = InitRequired
  1002. else:
  1003. for i in 0..<n.safeLen:
  1004. allPathsInBranch(n[i])
  1005. proc getProcTypeCast(m: BModule, prc: PSym): Rope =
  1006. result = getTypeDesc(m, prc.loc.t)
  1007. if prc.typ.callConv == ccClosure:
  1008. var rettype, params: Rope = ""
  1009. var check = initIntSet()
  1010. genProcParams(m, prc.typ, rettype, params, check)
  1011. result = "$1(*)$2" % [rettype, params]
  1012. proc genProcBody(p: BProc; procBody: PNode) =
  1013. genStmts(p, procBody) # modifies p.locals, p.init, etc.
  1014. if {nimErrorFlagAccessed, nimErrorFlagDeclared, nimErrorFlagDisabled} * p.flags == {nimErrorFlagAccessed}:
  1015. p.flags.incl nimErrorFlagDeclared
  1016. p.blocks[0].sections[cpsLocals].add(ropecg(p.module, "NIM_BOOL* nimErr_;$n", []))
  1017. p.blocks[0].sections[cpsInit].add(ropecg(p.module, "nimErr_ = #nimErrorFlag();$n", []))
  1018. proc isNoReturn(m: BModule; s: PSym): bool {.inline.} =
  1019. sfNoReturn in s.flags and m.config.exc != excGoto
  1020. proc genProcAux*(m: BModule, prc: PSym) =
  1021. var p = newProc(prc, m)
  1022. var header = newRopeAppender()
  1023. let isCppMember = m.config.backend == backendCpp and sfCppMember * prc.flags != {}
  1024. if isCppMember:
  1025. genMemberProcHeader(m, prc, header)
  1026. else:
  1027. genProcHeader(m, prc, header)
  1028. var returnStmt: Rope = ""
  1029. assert(prc.ast != nil)
  1030. var procBody = transformBody(m.g.graph, m.idgen, prc, {})
  1031. if sfInjectDestructors in prc.flags:
  1032. procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody)
  1033. let tmpInfo = prc.info
  1034. discard freshLineInfo(p, prc.info)
  1035. if sfPure notin prc.flags and prc.typ.returnType != nil:
  1036. if resultPos >= prc.ast.len:
  1037. internalError(m.config, prc.info, "proc has no result symbol")
  1038. let resNode = prc.ast[resultPos]
  1039. let res = resNode.sym # get result symbol
  1040. if not isInvalidReturnType(m.config, prc.typ) and sfConstructor notin prc.flags:
  1041. if sfNoInit in prc.flags: incl(res.flags, sfNoInit)
  1042. if sfNoInit in prc.flags and p.module.compileToCpp and (let val = easyResultAsgn(procBody); val != nil):
  1043. var decl = localVarDecl(p, resNode)
  1044. var a: TLoc = initLocExprSingleUse(p, val)
  1045. linefmt(p, cpsStmts, "$1 = $2;$n", [decl, rdLoc(a)])
  1046. else:
  1047. # declare the result symbol:
  1048. assignLocalVar(p, resNode)
  1049. assert(res.loc.r != "")
  1050. if p.config.selectedGC in {gcArc, gcAtomicArc, gcOrc} and
  1051. allPathsAsgnResult(p, procBody) == InitSkippable:
  1052. # In an ideal world the codegen could rely on injectdestructors doing its job properly
  1053. # and then the analysis step would not be required.
  1054. discard "result init optimized out"
  1055. else:
  1056. initLocalVar(p, res, immediateAsgn=false)
  1057. returnStmt = ropecg(p.module, "\treturn $1;$n", [rdLoc(res.loc)])
  1058. elif sfConstructor in prc.flags:
  1059. resNode.sym.loc.flags.incl lfIndirect
  1060. fillLoc(resNode.sym.loc, locParam, resNode, "this", OnHeap)
  1061. else:
  1062. fillResult(p.config, resNode, prc.typ)
  1063. assignParam(p, res, prc.typ.returnType)
  1064. # We simplify 'unsureAsgn(result, nil); unsureAsgn(result, x)'
  1065. # to 'unsureAsgn(result, x)'
  1066. # Sketch why this is correct: If 'result' points to a stack location
  1067. # the 'unsureAsgn' is a nop. If it points to a global variable the
  1068. # global is either 'nil' or points to valid memory and so the RC operation
  1069. # succeeds without touching not-initialized memory.
  1070. if sfNoInit in prc.flags: discard
  1071. elif allPathsAsgnResult(p, procBody) == InitSkippable: discard
  1072. else:
  1073. resetLoc(p, res.loc)
  1074. if skipTypes(res.typ, abstractInst).kind == tyArray:
  1075. #incl(res.loc.flags, lfIndirect)
  1076. res.loc.storage = OnUnknown
  1077. for i in 1..<prc.typ.n.len:
  1078. let param = prc.typ.n[i].sym
  1079. if param.typ.isCompileTimeOnly: continue
  1080. assignParam(p, param, prc.typ.returnType)
  1081. closureSetup(p, prc)
  1082. genProcBody(p, procBody)
  1083. prc.info = tmpInfo
  1084. var generatedProc: Rope = ""
  1085. generatedProc.genCLineDir prc.info, m.config
  1086. if isNoReturn(p.module, prc):
  1087. if hasDeclspec in extccomp.CC[p.config.cCompiler].props and not isCppMember:
  1088. header = "__declspec(noreturn) " & header
  1089. if sfPure in prc.flags:
  1090. if hasDeclspec in extccomp.CC[p.config.cCompiler].props and not isCppMember:
  1091. header = "__declspec(naked) " & header
  1092. generatedProc.add ropecg(p.module, "$1 {$n$2$3$4}$N$N",
  1093. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)])
  1094. else:
  1095. if m.hcrOn and isReloadable(m, prc):
  1096. # Add forward declaration for "_actual"-suffixed functions defined in the same module (or inline).
  1097. # This fixes the use of methods and also the case when 2 functions within the same module
  1098. # call each other using directly the "_actual" versions (an optimization) - see issue #11608
  1099. m.s[cfsProcHeaders].addf("$1;\n", [header])
  1100. generatedProc.add ropecg(p.module, "$1 {$n", [header])
  1101. if optStackTrace in prc.options:
  1102. generatedProc.add(p.s(cpsLocals))
  1103. var procname = makeCString(prc.name.s)
  1104. generatedProc.add(initFrame(p, procname, quotedFilename(p.config, prc.info)))
  1105. else:
  1106. generatedProc.add(p.s(cpsLocals))
  1107. if optProfiler in prc.options:
  1108. # invoke at proc entry for recursion:
  1109. appcg(p, cpsInit, "\t#nimProfile();$n", [])
  1110. # this pair of {} is required for C++ (C++ is weird with its
  1111. # control flow integrity checks):
  1112. if beforeRetNeeded in p.flags: generatedProc.add("{")
  1113. generatedProc.add(p.s(cpsInit))
  1114. generatedProc.add(p.s(cpsStmts))
  1115. if beforeRetNeeded in p.flags: generatedProc.add("\t}BeforeRet_: ;\n")
  1116. if optStackTrace in prc.options: generatedProc.add(deinitFrame(p))
  1117. generatedProc.add(returnStmt)
  1118. generatedProc.add("}\n")
  1119. m.s[cfsProcs].add(generatedProc)
  1120. if isReloadable(m, prc):
  1121. m.s[cfsDynLibInit].addf("\t$1 = ($3) hcrRegisterProc($4, \"$1\", (void*)$2);$n",
  1122. [prc.loc.r, prc.loc.r & "_actual", getProcTypeCast(m, prc), getModuleDllPath(m, prc)])
  1123. proc requiresExternC(m: BModule; sym: PSym): bool {.inline.} =
  1124. result = (sfCompileToCpp in m.module.flags and
  1125. sfCompileToCpp notin sym.getModule().flags and
  1126. m.config.backend != backendCpp) or (
  1127. sym.flags * {sfInfixCall, sfCompilerProc, sfMangleCpp} == {} and
  1128. sym.flags * {sfImportc, sfExportc} != {} and
  1129. sym.magic == mNone and
  1130. m.config.backend == backendCpp)
  1131. proc genProcPrototype(m: BModule, sym: PSym) =
  1132. useHeader(m, sym)
  1133. if lfNoDecl in sym.loc.flags or sfCppMember * sym.flags != {}: return
  1134. if lfDynamicLib in sym.loc.flags:
  1135. if sym.itemId.module != m.module.position and
  1136. not containsOrIncl(m.declaredThings, sym.id):
  1137. m.s[cfsVars].add(ropecg(m, "$1 $2 $3;$n",
  1138. [(if isReloadable(m, sym): "static" else: "extern"),
  1139. getTypeDesc(m, sym.loc.t), mangleDynLibProc(sym)]))
  1140. if isReloadable(m, sym):
  1141. m.s[cfsDynLibInit].addf("\t$1 = ($2) hcrGetProc($3, \"$1\");$n",
  1142. [mangleDynLibProc(sym), getTypeDesc(m, sym.loc.t), getModuleDllPath(m, sym)])
  1143. elif not containsOrIncl(m.declaredProtos, sym.id):
  1144. let asPtr = isReloadable(m, sym)
  1145. var header = newRopeAppender()
  1146. genProcHeader(m, sym, header, asPtr)
  1147. if not asPtr:
  1148. if isNoReturn(m, sym) and hasDeclspec in extccomp.CC[m.config.cCompiler].props:
  1149. header = "__declspec(noreturn) " & header
  1150. if sym.typ.callConv != ccInline and requiresExternC(m, sym):
  1151. header = "extern \"C\" " & header
  1152. if sfPure in sym.flags and hasAttribute in CC[m.config.cCompiler].props:
  1153. header.add(" __attribute__((naked))")
  1154. if isNoReturn(m, sym) and hasAttribute in CC[m.config.cCompiler].props:
  1155. header.add(" __attribute__((noreturn))")
  1156. m.s[cfsProcHeaders].add(ropecg(m, "$1;$N", [header]))
  1157. # TODO: figure out how to rename this - it DOES generate a forward declaration
  1158. proc genProcNoForward(m: BModule, prc: PSym) =
  1159. if lfImportCompilerProc in prc.loc.flags:
  1160. fillProcLoc(m, prc.ast[namePos])
  1161. useHeader(m, prc)
  1162. # dependency to a compilerproc:
  1163. cgsym(m, prc.name.s)
  1164. return
  1165. if lfNoDecl in prc.loc.flags:
  1166. fillProcLoc(m, prc.ast[namePos])
  1167. genProcPrototype(m, prc)
  1168. elif lfDynamicLib in prc.loc.flags:
  1169. var q = findPendingModule(m, prc)
  1170. fillProcLoc(q, prc.ast[namePos])
  1171. genProcPrototype(m, prc)
  1172. if q != nil and not containsOrIncl(q.declaredThings, prc.id):
  1173. symInDynamicLib(q, prc)
  1174. # register the procedure even though it is in a different dynamic library and will not be
  1175. # reloadable (and has no _actual suffix) - other modules will need to be able to get it through
  1176. # the hcr dynlib (also put it in the DynLibInit section - right after it gets loaded)
  1177. if isReloadable(q, prc):
  1178. q.s[cfsDynLibInit].addf("\t$1 = ($2) hcrRegisterProc($3, \"$1\", (void*)$1);$n",
  1179. [prc.loc.r, getTypeDesc(q, prc.loc.t), getModuleDllPath(m, q.module)])
  1180. else:
  1181. symInDynamicLibPartial(m, prc)
  1182. elif prc.typ.callConv == ccInline:
  1183. # We add inline procs to the calling module to enable C based inlining.
  1184. # This also means that a check with ``q.declaredThings`` is wrong, we need
  1185. # a check for ``m.declaredThings``.
  1186. if not containsOrIncl(m.declaredThings, prc.id):
  1187. #if prc.loc.k == locNone:
  1188. # mangle the inline proc based on the module where it is defined -
  1189. # not on the first module that uses it
  1190. let m2 = if m.config.symbolFiles != disabledSf: m
  1191. else: findPendingModule(m, prc)
  1192. fillProcLoc(m2, prc.ast[namePos])
  1193. #elif {sfExportc, sfImportc} * prc.flags == {}:
  1194. # # reset name to restore consistency in case of hashing collisions:
  1195. # echo "resetting ", prc.id, " by ", m.module.name.s
  1196. # prc.loc.r = nil
  1197. # prc.loc.r = mangleName(m, prc)
  1198. genProcPrototype(m, prc)
  1199. genProcAux(m, prc)
  1200. elif sfImportc notin prc.flags:
  1201. var q = findPendingModule(m, prc)
  1202. fillProcLoc(q, prc.ast[namePos])
  1203. # generate a getProc call to initialize the pointer for this
  1204. # externally-to-the-current-module defined proc, also important
  1205. # to do the declaredProtos check before the call to genProcPrototype
  1206. if isReloadable(m, prc) and prc.id notin m.declaredProtos and
  1207. q != nil and q.module.id != m.module.id:
  1208. m.s[cfsDynLibInit].addf("\t$1 = ($2) hcrGetProc($3, \"$1\");$n",
  1209. [prc.loc.r, getProcTypeCast(m, prc), getModuleDllPath(m, prc)])
  1210. genProcPrototype(m, prc)
  1211. if q != nil and not containsOrIncl(q.declaredThings, prc.id):
  1212. # make sure there is a "prototype" in the external module
  1213. # which will actually become a function pointer
  1214. if isReloadable(m, prc):
  1215. genProcPrototype(q, prc)
  1216. genProcAux(q, prc)
  1217. else:
  1218. fillProcLoc(m, prc.ast[namePos])
  1219. useHeader(m, prc)
  1220. if sfInfixCall notin prc.flags: genProcPrototype(m, prc)
  1221. proc requestConstImpl(p: BProc, sym: PSym) =
  1222. if genConstSetup(p, sym):
  1223. let m = p.module
  1224. # declare implementation:
  1225. var q = findPendingModule(m, sym)
  1226. if q != nil and not containsOrIncl(q.declaredThings, sym.id):
  1227. assert q.initProc.module == q
  1228. genConstDefinition(q, p, sym)
  1229. # declare header:
  1230. if q != m and not containsOrIncl(m.declaredThings, sym.id):
  1231. genConstHeader(m, q, p, sym)
  1232. proc isActivated(prc: PSym): bool = prc.typ != nil
  1233. proc genProc(m: BModule, prc: PSym) =
  1234. if sfBorrow in prc.flags or not isActivated(prc): return
  1235. if sfForward in prc.flags:
  1236. addForwardedProc(m, prc)
  1237. fillProcLoc(m, prc.ast[namePos])
  1238. else:
  1239. genProcNoForward(m, prc)
  1240. if {sfExportc, sfCompilerProc} * prc.flags == {sfExportc} and
  1241. m.g.generatedHeader != nil and lfNoDecl notin prc.loc.flags:
  1242. genProcPrototype(m.g.generatedHeader, prc)
  1243. if prc.typ.callConv == ccInline:
  1244. if not containsOrIncl(m.g.generatedHeader.declaredThings, prc.id):
  1245. genProcAux(m.g.generatedHeader, prc)
  1246. proc genVarPrototype(m: BModule, n: PNode) =
  1247. #assert(sfGlobal in sym.flags)
  1248. let sym = n.sym
  1249. useHeader(m, sym)
  1250. fillBackendName(m, sym)
  1251. fillLoc(sym.loc, locGlobalVar, n, OnHeap)
  1252. if treatGlobalDifferentlyForHCR(m, sym): incl(sym.loc.flags, lfIndirect)
  1253. if (lfNoDecl in sym.loc.flags) or contains(m.declaredThings, sym.id):
  1254. return
  1255. if sym.owner.id != m.module.id:
  1256. # else we already have the symbol generated!
  1257. assert(sym.loc.r != "")
  1258. incl(m.declaredThings, sym.id)
  1259. if sfThread in sym.flags:
  1260. declareThreadVar(m, sym, true)
  1261. else:
  1262. if sym.kind in {skLet, skVar, skField, skForVar} and sym.alignment > 0:
  1263. m.s[cfsVars].addf "NIM_ALIGN($1) ", [rope(sym.alignment)]
  1264. m.s[cfsVars].add(if m.hcrOn: "static " else: "extern ")
  1265. m.s[cfsVars].add(getTypeDesc(m, sym.loc.t, dkVar))
  1266. if m.hcrOn: m.s[cfsVars].add("*")
  1267. if lfDynamicLib in sym.loc.flags: m.s[cfsVars].add("*")
  1268. if sfRegister in sym.flags: m.s[cfsVars].add(" register")
  1269. if sfVolatile in sym.flags: m.s[cfsVars].add(" volatile")
  1270. if sfNoalias in sym.flags: m.s[cfsVars].add(" NIM_NOALIAS")
  1271. m.s[cfsVars].addf(" $1;$n", [sym.loc.r])
  1272. if m.hcrOn: m.initProc.procSec(cpsLocals).addf(
  1273. "\t$1 = ($2*)hcrGetGlobal($3, \"$1\");$n", [sym.loc.r,
  1274. getTypeDesc(m, sym.loc.t, dkVar), getModuleDllPath(m, sym)])
  1275. proc addNimDefines(result: var Rope; conf: ConfigRef) {.inline.} =
  1276. result.addf("#define NIM_INTBITS $1\L", [
  1277. platform.CPU[conf.target.targetCPU].intSize.rope])
  1278. if conf.cppCustomNamespace.len > 0:
  1279. result.add("#define USE_NIM_NAMESPACE ")
  1280. result.add(conf.cppCustomNamespace)
  1281. result.add("\L")
  1282. if conf.isDefined("nimEmulateOverflowChecks"):
  1283. result.add("#define NIM_EmulateOverflowChecks\L")
  1284. proc headerTop(): Rope =
  1285. result = "/* Generated by Nim Compiler v$1 */$N" % [rope(VersionAsString)]
  1286. proc getCopyright(conf: ConfigRef; cfile: Cfile): Rope =
  1287. result = headerTop()
  1288. if optCompileOnly notin conf.globalOptions:
  1289. result.add ("/* Compiled for: $1, $2, $3 */$N" &
  1290. "/* Command for C compiler:$n $4 */$N") %
  1291. [rope(platform.OS[conf.target.targetOS].name),
  1292. rope(platform.CPU[conf.target.targetCPU].name),
  1293. rope(extccomp.CC[conf.cCompiler].name),
  1294. rope(getCompileCFileCmd(conf, cfile))]
  1295. proc getFileHeader(conf: ConfigRef; cfile: Cfile): Rope =
  1296. result = getCopyright(conf, cfile)
  1297. if conf.hcrOn: result.add("#define NIM_HOT_CODE_RELOADING\L")
  1298. addNimDefines(result, conf)
  1299. proc getSomeNameForModule(conf: ConfigRef, filename: AbsoluteFile): Rope =
  1300. ## Returns a mangled module name.
  1301. result = ""
  1302. result.add mangleModuleName(conf, filename).mangle
  1303. proc getSomeNameForModule(m: BModule): Rope =
  1304. ## Returns a mangled module name.
  1305. assert m.module.kind == skModule
  1306. assert m.module.owner.kind == skPackage
  1307. result = ""
  1308. result.add mangleModuleName(m.g.config, m.filename).mangle
  1309. proc getSomeInitName(m: BModule, suffix: string): Rope =
  1310. if not m.hcrOn:
  1311. result = getSomeNameForModule(m)
  1312. else:
  1313. result = ""
  1314. result.add suffix
  1315. proc getInitName(m: BModule): Rope =
  1316. if sfMainModule in m.module.flags:
  1317. # generate constant name for main module, for "easy" debugging.
  1318. result = rope(m.config.nimMainPrefix) & rope"NimMainModule"
  1319. else:
  1320. result = getSomeInitName(m, "Init000")
  1321. proc getDatInitName(m: BModule): Rope = getSomeInitName(m, "DatInit000")
  1322. proc getHcrInitName(m: BModule): Rope = getSomeInitName(m, "HcrInit000")
  1323. proc hcrGetProcLoadCode(m: BModule, sym, prefix, handle, getProcFunc: string): Rope
  1324. proc genMainProc(m: BModule) =
  1325. ## this function is called in cgenWriteModules after all modules are closed,
  1326. ## it means raising dependency on the symbols is too late as it will not propagate
  1327. ## into other modules, only simple rope manipulations are allowed
  1328. var preMainCode: Rope = ""
  1329. if m.hcrOn:
  1330. proc loadLib(handle: string, name: string): Rope =
  1331. result = ""
  1332. let prc = magicsys.getCompilerProc(m.g.graph, name)
  1333. assert prc != nil
  1334. let n = newStrNode(nkStrLit, prc.annex.path.strVal)
  1335. n.info = prc.annex.path.info
  1336. var strLit = newRopeAppender()
  1337. genStringLiteral(m, n, strLit)
  1338. appcg(m, result, "\tif (!($1 = #nimLoadLibrary($2)))$N" &
  1339. "\t\t#nimLoadLibraryError($2);$N",
  1340. [handle, strLit])
  1341. preMainCode.add(loadLib("hcr_handle", "hcrGetProc"))
  1342. if m.config.selectedGC in {gcArc, gcAtomicArc, gcOrc}:
  1343. preMainCode.add("\t$1PreMain();\L" % [rope m.config.nimMainPrefix])
  1344. else:
  1345. preMainCode.add("\tvoid* rtl_handle;\L")
  1346. preMainCode.add(loadLib("rtl_handle", "nimGC_setStackBottom"))
  1347. preMainCode.add(hcrGetProcLoadCode(m, "nimGC_setStackBottom", "nimrtl_", "rtl_handle", "nimGetProcAddr"))
  1348. preMainCode.add("\tinner = $1PreMain;\L" % [rope m.config.nimMainPrefix])
  1349. preMainCode.add("\tinitStackBottomWith_actual((void *)&inner);\L")
  1350. preMainCode.add("\t(*inner)();\L")
  1351. else:
  1352. preMainCode.add("\t$1PreMain();\L" % [rope m.config.nimMainPrefix])
  1353. var posixCmdLine: Rope = ""
  1354. if optNoMain notin m.config.globalOptions:
  1355. posixCmdLine.add "N_LIB_PRIVATE int cmdCount;\L"
  1356. posixCmdLine.add "N_LIB_PRIVATE char** cmdLine;\L"
  1357. posixCmdLine.add "N_LIB_PRIVATE char** gEnv;\L"
  1358. const
  1359. # The use of a volatile function pointer to call Pre/NimMainInner
  1360. # prevents inlining of the NimMainInner function and dependent
  1361. # functions, which might otherwise merge their stack frames.
  1362. PreMainBody = "$N" &
  1363. "N_LIB_PRIVATE void $3PreMainInner(void) {$N" &
  1364. "$2" &
  1365. "}$N$N" &
  1366. "$4" &
  1367. "N_LIB_PRIVATE void $3PreMain(void) {$N" &
  1368. "##if $5$N" & # 1 for volatile call, 0 for non-volatile
  1369. "\tvoid (*volatile inner)(void);$N" &
  1370. "\tinner = $3PreMainInner;$N" &
  1371. "$1" &
  1372. "\t(*inner)();$N" &
  1373. "##else$N" &
  1374. "$1" &
  1375. "\t$3PreMainInner();$N" &
  1376. "##endif$N" &
  1377. "}$N$N"
  1378. MainProcs =
  1379. "\t$^NimMain();$N"
  1380. MainProcsWithResult =
  1381. MainProcs & ("\treturn $1nim_program_result;$N")
  1382. NimMainInner = "N_LIB_PRIVATE N_CDECL(void, $5NimMainInner)(void) {$N" &
  1383. "$1" &
  1384. "}$N$N"
  1385. NimMainProc =
  1386. "N_CDECL(void, $5NimMain)(void) {$N" &
  1387. "##if $6$N" & # 1 for volatile call, 0 for non-volatile
  1388. "\tvoid (*volatile inner)(void);$N" &
  1389. "$4" &
  1390. "\tinner = $5NimMainInner;$N" &
  1391. "$2" &
  1392. "\t(*inner)();$N" &
  1393. "##else$N" &
  1394. "$4" &
  1395. "$2" &
  1396. "\t$5NimMainInner();$N" &
  1397. "##endif$N" &
  1398. "}$N$N"
  1399. NimMainBody = NimMainInner & NimMainProc
  1400. PosixCMain =
  1401. "int main(int argc, char** args, char** env) {$N" &
  1402. "\tcmdLine = args;$N" &
  1403. "\tcmdCount = argc;$N" &
  1404. "\tgEnv = env;$N" &
  1405. MainProcsWithResult &
  1406. "}$N$N"
  1407. StandaloneCMain =
  1408. "int main(void) {$N" &
  1409. MainProcs &
  1410. "\treturn 0;$N" &
  1411. "}$N$N"
  1412. WinNimMain = NimMainBody
  1413. WinCMain = "N_STDCALL(int, WinMain)(HINSTANCE hCurInstance, $N" &
  1414. " HINSTANCE hPrevInstance, $N" &
  1415. " LPSTR lpCmdLine, int nCmdShow) {$N" &
  1416. MainProcsWithResult & "}$N$N"
  1417. WinNimDllMain = NimMainInner & "N_LIB_EXPORT " & NimMainProc
  1418. WinCDllMain =
  1419. "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, $N" &
  1420. " LPVOID lpvReserved) {$N" &
  1421. "\tif (fwdreason == DLL_PROCESS_ATTACH) {$N" & MainProcs & "\t}$N" &
  1422. "\treturn 1;$N}$N$N"
  1423. PosixNimDllMain = WinNimDllMain
  1424. PosixCDllMain =
  1425. "N_LIB_PRIVATE void NIM_POSIX_INIT NimMainInit(void) {$N" &
  1426. MainProcs &
  1427. "}$N$N"
  1428. GenodeNimMain =
  1429. "extern Genode::Env *nim_runtime_env;$N" &
  1430. "extern \"C\" void nim_component_construct(Genode::Env*);$N$N" &
  1431. NimMainBody
  1432. ComponentConstruct =
  1433. "void Libc::Component::construct(Libc::Env &env) {$N" &
  1434. "\t// Set Env used during runtime initialization$N" &
  1435. "\tnim_runtime_env = &env;$N" &
  1436. "\tLibc::with_libc([&] () {$N\t" &
  1437. "\t// Initialize runtime and globals$N" &
  1438. MainProcs &
  1439. "\t// Call application construct$N" &
  1440. "\t\tnim_component_construct(&env);$N" &
  1441. "\t});$N" &
  1442. "}$N$N"
  1443. if m.config.target.targetOS == osWindows and
  1444. m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
  1445. m.includeHeader("<windows.h>")
  1446. elif m.config.target.targetOS == osGenode:
  1447. m.includeHeader("<libc/component.h>")
  1448. let initStackBottomCall =
  1449. if m.config.target.targetOS == osStandalone or m.config.selectedGC in {gcNone, gcArc, gcAtomicArc, gcOrc}: "".rope
  1450. else: ropecg(m, "\t#initStackBottomWith((void *)&inner);$N", [])
  1451. inc(m.labels)
  1452. let isVolatile = if m.config.selectedGC notin {gcNone, gcArc, gcAtomicArc, gcOrc}: "1" else: "0"
  1453. appcg(m, m.s[cfsProcs], PreMainBody, [m.g.mainDatInit, m.g.otherModsInit, m.config.nimMainPrefix, posixCmdLine, isVolatile])
  1454. if m.config.target.targetOS == osWindows and
  1455. m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
  1456. if optGenGuiApp in m.config.globalOptions:
  1457. const nimMain = WinNimMain
  1458. appcg(m, m.s[cfsProcs], nimMain,
  1459. [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix, isVolatile])
  1460. else:
  1461. const nimMain = WinNimDllMain
  1462. appcg(m, m.s[cfsProcs], nimMain,
  1463. [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix, isVolatile])
  1464. elif m.config.target.targetOS == osGenode:
  1465. const nimMain = GenodeNimMain
  1466. appcg(m, m.s[cfsProcs], nimMain,
  1467. [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix, isVolatile])
  1468. elif optGenDynLib in m.config.globalOptions:
  1469. const nimMain = PosixNimDllMain
  1470. appcg(m, m.s[cfsProcs], nimMain,
  1471. [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix, isVolatile])
  1472. else:
  1473. const nimMain = NimMainBody
  1474. appcg(m, m.s[cfsProcs], nimMain,
  1475. [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix, isVolatile])
  1476. if optNoMain notin m.config.globalOptions:
  1477. if m.config.cppCustomNamespace.len > 0:
  1478. closeNamespaceNim(m.s[cfsProcs])
  1479. m.s[cfsProcs].add "using namespace " & m.config.cppCustomNamespace & ";\L"
  1480. if m.config.target.targetOS == osWindows and
  1481. m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
  1482. if optGenGuiApp in m.config.globalOptions:
  1483. const otherMain = WinCMain
  1484. appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: "", m.config.nimMainPrefix])
  1485. else:
  1486. const otherMain = WinCDllMain
  1487. appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
  1488. elif m.config.target.targetOS == osGenode:
  1489. const otherMain = ComponentConstruct
  1490. appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
  1491. elif optGenDynLib in m.config.globalOptions:
  1492. const otherMain = PosixCDllMain
  1493. appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
  1494. elif m.config.target.targetOS == osStandalone:
  1495. const otherMain = StandaloneCMain
  1496. appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
  1497. else:
  1498. const otherMain = PosixCMain
  1499. appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: "", m.config.nimMainPrefix])
  1500. if m.config.cppCustomNamespace.len > 0:
  1501. openNamespaceNim(m.config.cppCustomNamespace, m.s[cfsProcs])
  1502. proc registerInitProcs*(g: BModuleList; m: PSym; flags: set[ModuleBackendFlag]) =
  1503. ## Called from the IC backend.
  1504. if HasDatInitProc in flags:
  1505. let datInit = getSomeNameForModule(g.config, g.config.toFullPath(m.info.fileIndex).AbsoluteFile) & "DatInit000"
  1506. g.mainModProcs.addf("N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [datInit])
  1507. g.mainDatInit.addf("\t$1();$N", [datInit])
  1508. if HasModuleInitProc in flags:
  1509. let init = getSomeNameForModule(g.config, g.config.toFullPath(m.info.fileIndex).AbsoluteFile) & "Init000"
  1510. g.mainModProcs.addf("N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [init])
  1511. let initCall = "\t$1();$N" % [init]
  1512. if sfMainModule in m.flags:
  1513. g.mainModInit.add(initCall)
  1514. elif sfSystemModule in m.flags:
  1515. g.mainDatInit.add(initCall) # systemInit must called right after systemDatInit if any
  1516. else:
  1517. g.otherModsInit.add(initCall)
  1518. proc whichInitProcs*(m: BModule): set[ModuleBackendFlag] =
  1519. # called from IC.
  1520. result = {}
  1521. if m.hcrOn or m.preInitProc.s(cpsInit).len > 0 or m.preInitProc.s(cpsStmts).len > 0:
  1522. result.incl HasModuleInitProc
  1523. for i in cfsTypeInit1..cfsDynLibInit:
  1524. if m.s[i].len != 0:
  1525. result.incl HasDatInitProc
  1526. break
  1527. proc registerModuleToMain(g: BModuleList; m: BModule) =
  1528. let
  1529. init = m.getInitName
  1530. datInit = m.getDatInitName
  1531. if m.hcrOn:
  1532. var hcrModuleMeta = "$nN_LIB_PRIVATE const char* hcr_module_list[] = {$n" % []
  1533. let systemModulePath = getModuleDllPath(m, g.modules[g.graph.config.m.systemFileIdx.int].module)
  1534. let mainModulePath = getModuleDllPath(m, m.module)
  1535. if sfMainModule in m.module.flags:
  1536. hcrModuleMeta.addf("\t$1,$n", [systemModulePath])
  1537. g.graph.importDeps.withValue(FileIndex(m.module.position), deps):
  1538. for curr in deps[]:
  1539. hcrModuleMeta.addf("\t$1,$n", [getModuleDllPath(m, g.modules[curr.int].module)])
  1540. hcrModuleMeta.addf("\t\"\"};$n", [])
  1541. hcrModuleMeta.addf("$nN_LIB_EXPORT N_NIMCALL(void**, HcrGetImportedModules)() { return (void**)hcr_module_list; }$n", [])
  1542. hcrModuleMeta.addf("$nN_LIB_EXPORT N_NIMCALL(char*, HcrGetSigHash)() { return \"$1\"; }$n$n",
  1543. [($sigHash(m.module, m.config)).rope])
  1544. if sfMainModule in m.module.flags:
  1545. g.mainModProcs.add(hcrModuleMeta)
  1546. g.mainModProcs.addf("static void* hcr_handle;$N", [])
  1547. g.mainModProcs.addf("N_LIB_EXPORT N_NIMCALL(void, $1)(void);$N", [init])
  1548. g.mainModProcs.addf("N_LIB_EXPORT N_NIMCALL(void, $1)(void);$N", [datInit])
  1549. g.mainModProcs.addf("N_LIB_EXPORT N_NIMCALL(void, $1)(void*, N_NIMCALL_PTR(void*, getProcAddr)(void*, char*));$N", [m.getHcrInitName])
  1550. g.mainModProcs.addf("N_LIB_EXPORT N_NIMCALL(void, HcrCreateTypeInfos)(void);$N", [])
  1551. g.mainModInit.addf("\t$1();$N", [init])
  1552. g.otherModsInit.addf("\thcrInit((void**)hcr_module_list, $1, $2, $3, hcr_handle, nimGetProcAddr);$n",
  1553. [mainModulePath, systemModulePath, datInit])
  1554. g.mainDatInit.addf("\t$1(hcr_handle, nimGetProcAddr);$N", [m.getHcrInitName])
  1555. g.mainDatInit.addf("\thcrAddModule($1);\n", [mainModulePath])
  1556. g.mainDatInit.addf("\tHcrCreateTypeInfos();$N", [])
  1557. # nasty nasty hack to get the command line functionality working with HCR
  1558. # register the 2 variables on behalf of the os module which might not even
  1559. # be loaded (in which case it will get collected but that is not a problem)
  1560. # EDIT: indeed, this hack, in combination with another un-necessary one
  1561. # (`makeCString` was doing line wrap of string litterals) was root cause for
  1562. # bug #16265.
  1563. let osModulePath = ($systemModulePath).replace("stdlib_system", "stdlib_os").rope
  1564. g.mainDatInit.addf("\thcrAddModule($1);\n", [osModulePath])
  1565. g.mainDatInit.add("\tint* cmd_count;\n")
  1566. g.mainDatInit.add("\tchar*** cmd_line;\n")
  1567. g.mainDatInit.addf("\thcrRegisterGlobal($1, \"cmdCount\", sizeof(cmd_count), NULL, (void**)&cmd_count);$N", [osModulePath])
  1568. g.mainDatInit.addf("\thcrRegisterGlobal($1, \"cmdLine\", sizeof(cmd_line), NULL, (void**)&cmd_line);$N", [osModulePath])
  1569. g.mainDatInit.add("\t*cmd_count = cmdCount;\n")
  1570. g.mainDatInit.add("\t*cmd_line = cmdLine;\n")
  1571. else:
  1572. m.s[cfsInitProc].add(hcrModuleMeta)
  1573. return
  1574. if m.s[cfsDatInitProc].len > 0:
  1575. g.mainModProcs.addf("N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [datInit])
  1576. g.mainDatInit.addf("\t$1();$N", [datInit])
  1577. # Initialization of TLS and GC should be done in between
  1578. # systemDatInit and systemInit calls if any
  1579. if sfSystemModule in m.module.flags:
  1580. if emulatedThreadVars(m.config) and m.config.target.targetOS != osStandalone:
  1581. g.mainDatInit.add(ropecg(m, "\t#initThreadVarsEmulation();$N", []))
  1582. if m.config.target.targetOS != osStandalone and m.config.selectedGC notin {gcNone, gcArc, gcAtomicArc, gcOrc}:
  1583. g.mainDatInit.add(ropecg(m, "\t#initStackBottomWith((void *)&inner);$N", []))
  1584. if m.s[cfsInitProc].len > 0:
  1585. g.mainModProcs.addf("N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [init])
  1586. let initCall = "\t$1();$N" % [init]
  1587. if sfMainModule in m.module.flags:
  1588. g.mainModInit.add(initCall)
  1589. elif sfSystemModule in m.module.flags:
  1590. g.mainDatInit.add(initCall) # systemInit must called right after systemDatInit if any
  1591. else:
  1592. g.otherModsInit.add(initCall)
  1593. proc genDatInitCode(m: BModule) =
  1594. ## this function is called in cgenWriteModules after all modules are closed,
  1595. ## it means raising dependency on the symbols is too late as it will not propagate
  1596. ## into other modules, only simple rope manipulations are allowed
  1597. var moduleDatInitRequired = m.hcrOn
  1598. var prc = "$1 N_NIMCALL(void, $2)(void) {$N" %
  1599. [rope(if m.hcrOn: "N_LIB_EXPORT" else: "N_LIB_PRIVATE"), getDatInitName(m)]
  1600. # we don't want to break into such init code - could happen if a line
  1601. # directive from a function written by the user spills after itself
  1602. genCLineDir(prc, "generated_not_to_break_here", 999999, m.config)
  1603. for i in cfsTypeInit1..cfsDynLibInit:
  1604. if m.s[i].len != 0:
  1605. moduleDatInitRequired = true
  1606. prc.add(m.s[i])
  1607. prc.addf("}$N$N", [])
  1608. if moduleDatInitRequired:
  1609. m.s[cfsDatInitProc].add(prc)
  1610. #rememberFlag(m.g.graph, m.module, HasDatInitProc)
  1611. # Very similar to the contents of symInDynamicLib - basically only the
  1612. # things needed for the hot code reloading runtime procs to be loaded
  1613. proc hcrGetProcLoadCode(m: BModule, sym, prefix, handle, getProcFunc: string): Rope =
  1614. let prc = magicsys.getCompilerProc(m.g.graph, sym)
  1615. assert prc != nil
  1616. fillProcLoc(m, prc.ast[namePos])
  1617. var extname = prefix & sym
  1618. var tmp = mangleDynLibProc(prc)
  1619. prc.loc.r = tmp
  1620. prc.typ.sym = nil
  1621. if not containsOrIncl(m.declaredThings, prc.id):
  1622. m.s[cfsVars].addf("static $2 $1;$n", [prc.loc.r, getTypeDesc(m, prc.loc.t, dkVar)])
  1623. result = "\t$1 = ($2) $3($4, $5);$n" %
  1624. [tmp, getTypeDesc(m, prc.typ, dkVar), getProcFunc.rope, handle.rope, makeCString(prefix & sym)]
  1625. proc genInitCode(m: BModule) =
  1626. ## this function is called in cgenWriteModules after all modules are closed,
  1627. ## it means raising dependency on the symbols is too late as it will not propagate
  1628. ## into other modules, only simple rope manipulations are allowed
  1629. var moduleInitRequired = m.hcrOn
  1630. let initname = getInitName(m)
  1631. var prc = "$1 N_NIMCALL(void, $2)(void) {$N" %
  1632. [rope(if m.hcrOn: "N_LIB_EXPORT" else: "N_LIB_PRIVATE"), initname]
  1633. # we don't want to break into such init code - could happen if a line
  1634. # directive from a function written by the user spills after itself
  1635. genCLineDir(prc, "generated_not_to_break_here", 999999, m.config)
  1636. if m.typeNodes > 0:
  1637. if m.hcrOn:
  1638. appcg(m, m.s[cfsTypeInit1], "\t#TNimNode* $1;$N", [m.typeNodesName])
  1639. appcg(m, m.s[cfsTypeInit1], "\thcrRegisterGlobal($3, \"$1_$2\", sizeof(TNimNode) * $2, NULL, (void**)&$1);$N",
  1640. [m.typeNodesName, m.typeNodes, getModuleDllPath(m, m.module)])
  1641. else:
  1642. appcg(m, m.s[cfsTypeInit1], "static #TNimNode $1[$2];$n",
  1643. [m.typeNodesName, m.typeNodes])
  1644. if m.nimTypes > 0:
  1645. appcg(m, m.s[cfsTypeInit1], "static #TNimType $1[$2];$n",
  1646. [m.nimTypesName, m.nimTypes])
  1647. if m.hcrOn:
  1648. prc.addf("\tint* nim_hcr_dummy_ = 0;$n" &
  1649. "\tNIM_BOOL nim_hcr_do_init_ = " &
  1650. "hcrRegisterGlobal($1, \"module_initialized_\", 1, NULL, (void**)&nim_hcr_dummy_);$n",
  1651. [getModuleDllPath(m, m.module)])
  1652. template writeSection(thing: untyped, section: TCProcSection, addHcrGuards = false) =
  1653. if m.thing.s(section).len > 0:
  1654. moduleInitRequired = true
  1655. if addHcrGuards: prc.add("\tif (nim_hcr_do_init_) {\n\n")
  1656. prc.add(m.thing.s(section))
  1657. if addHcrGuards: prc.add("\n\t} // nim_hcr_do_init_\n")
  1658. if m.preInitProc.s(cpsInit).len > 0 or m.preInitProc.s(cpsStmts).len > 0:
  1659. # Give this small function its own scope
  1660. prc.addf("{$N", [])
  1661. # Keep a bogus frame in case the code needs one
  1662. prc.add("\tTFrame FR_; FR_.len = 0;\n")
  1663. writeSection(preInitProc, cpsLocals)
  1664. writeSection(preInitProc, cpsInit, m.hcrOn)
  1665. writeSection(preInitProc, cpsStmts)
  1666. prc.addf("}/* preInitProc end */$N", [])
  1667. when false:
  1668. m.initProc.blocks[0].sections[cpsLocals].add m.preInitProc.s(cpsLocals)
  1669. m.initProc.blocks[0].sections[cpsInit].prepend m.preInitProc.s(cpsInit)
  1670. m.initProc.blocks[0].sections[cpsStmts].prepend m.preInitProc.s(cpsStmts)
  1671. # add new scope for following code, because old vcc compiler need variable
  1672. # be defined at the top of the block
  1673. prc.addf("{$N", [])
  1674. writeSection(initProc, cpsLocals)
  1675. if m.initProc.s(cpsInit).len > 0 or m.initProc.s(cpsStmts).len > 0:
  1676. moduleInitRequired = true
  1677. if optStackTrace in m.initProc.options and frameDeclared notin m.flags:
  1678. # BUT: the generated init code might depend on a current frame, so
  1679. # declare it nevertheless:
  1680. incl m.flags, frameDeclared
  1681. if preventStackTrace notin m.flags:
  1682. var procname = makeCString(m.module.name.s)
  1683. prc.add(initFrame(m.initProc, procname, quotedFilename(m.config, m.module.info)))
  1684. else:
  1685. prc.add("\tTFrame FR_; FR_.len = 0;\n")
  1686. writeSection(initProc, cpsInit, m.hcrOn)
  1687. writeSection(initProc, cpsStmts)
  1688. if beforeRetNeeded in m.initProc.flags:
  1689. prc.add("\tBeforeRet_: ;\n")
  1690. if m.config.exc == excGoto:
  1691. if getCompilerProc(m.g.graph, "nimTestErrorFlag") != nil:
  1692. m.appcg(prc, "\t#nimTestErrorFlag();$n", [])
  1693. if optStackTrace in m.initProc.options and preventStackTrace notin m.flags:
  1694. prc.add(deinitFrame(m.initProc))
  1695. elif m.config.exc == excGoto:
  1696. if getCompilerProc(m.g.graph, "nimTestErrorFlag") != nil:
  1697. m.appcg(prc, "\t#nimTestErrorFlag();$n", [])
  1698. prc.addf("}$N", [])
  1699. prc.addf("}$N$N", [])
  1700. # we cannot simply add the init proc to ``m.s[cfsProcs]`` anymore because
  1701. # that would lead to a *nesting* of merge sections which the merger does
  1702. # not support. So we add it to another special section: ``cfsInitProc``
  1703. if m.hcrOn:
  1704. var procsToLoad = @["hcrRegisterProc", "hcrGetProc", "hcrRegisterGlobal", "hcrGetGlobal"]
  1705. m.s[cfsInitProc].addf("N_LIB_EXPORT N_NIMCALL(void, $1)(void* handle, N_NIMCALL_PTR(void*, getProcAddr)(void*, char*)) {$N", [getHcrInitName(m)])
  1706. if sfMainModule in m.module.flags:
  1707. # additional procs to load
  1708. procsToLoad.add("hcrInit")
  1709. procsToLoad.add("hcrAddModule")
  1710. # load procs
  1711. for curr in procsToLoad:
  1712. m.s[cfsInitProc].add(hcrGetProcLoadCode(m, curr, "", "handle", "getProcAddr"))
  1713. m.s[cfsInitProc].addf("}$N$N", [])
  1714. for i, el in pairs(m.extensionLoaders):
  1715. if el != "":
  1716. let ex = "NIM_EXTERNC N_NIMCALL(void, nimLoadProcs$1)(void) {$2}$N$N" %
  1717. [(i.ord - '0'.ord).rope, el]
  1718. moduleInitRequired = true
  1719. prc.add(ex)
  1720. if moduleInitRequired or sfMainModule in m.module.flags:
  1721. m.s[cfsInitProc].add(prc)
  1722. #rememberFlag(m.g.graph, m.module, HasModuleInitProc)
  1723. genDatInitCode(m)
  1724. if m.hcrOn:
  1725. m.s[cfsInitProc].addf("N_LIB_EXPORT N_NIMCALL(void, HcrCreateTypeInfos)(void) {$N", [])
  1726. m.s[cfsInitProc].add(m.hcrCreateTypeInfosProc)
  1727. m.s[cfsInitProc].addf("}$N$N", [])
  1728. registerModuleToMain(m.g, m)
  1729. proc genModule(m: BModule, cfile: Cfile): Rope =
  1730. var moduleIsEmpty = true
  1731. result = getFileHeader(m.config, cfile)
  1732. generateThreadLocalStorage(m)
  1733. generateHeaders(m)
  1734. result.add(m.s[cfsHeaders])
  1735. if m.config.cppCustomNamespace.len > 0:
  1736. openNamespaceNim(m.config.cppCustomNamespace, result)
  1737. if m.s[cfsFrameDefines].len > 0:
  1738. result.add(m.s[cfsFrameDefines])
  1739. for i in cfsForwardTypes..cfsProcs:
  1740. if m.s[i].len > 0:
  1741. moduleIsEmpty = false
  1742. result.add(m.s[i])
  1743. if m.s[cfsInitProc].len > 0:
  1744. moduleIsEmpty = false
  1745. result.add(m.s[cfsInitProc])
  1746. if m.s[cfsDatInitProc].len > 0 or m.hcrOn:
  1747. moduleIsEmpty = false
  1748. result.add(m.s[cfsDatInitProc])
  1749. if m.config.cppCustomNamespace.len > 0:
  1750. closeNamespaceNim(result)
  1751. if moduleIsEmpty:
  1752. result = ""
  1753. proc initProcOptions(m: BModule): TOptions =
  1754. let opts = m.config.options
  1755. if sfSystemModule in m.module.flags: opts-{optStackTrace} else: opts
  1756. proc rawNewModule(g: BModuleList; module: PSym, filename: AbsoluteFile): BModule =
  1757. new(result)
  1758. result.g = g
  1759. result.tmpBase = rope("TM" & $hashOwner(module) & "_")
  1760. result.headerFiles = @[]
  1761. result.declaredThings = initIntSet()
  1762. result.declaredProtos = initIntSet()
  1763. result.cfilename = filename
  1764. result.filename = filename
  1765. result.typeCache = initTable[SigHash, Rope]()
  1766. result.forwTypeCache = initTable[SigHash, Rope]()
  1767. result.module = module
  1768. result.typeInfoMarker = initTable[SigHash, Rope]()
  1769. result.sigConflicts = initCountTable[SigHash]()
  1770. result.initProc = newProc(nil, result)
  1771. for i in low(result.s)..high(result.s): result.s[i] = newRopeAppender()
  1772. result.initProc.options = initProcOptions(result)
  1773. result.preInitProc = newProc(nil, result)
  1774. result.preInitProc.flags.incl nimErrorFlagDisabled
  1775. result.preInitProc.labels = 100_000 # little hack so that unique temporaries are generated
  1776. result.dataCache = initNodeTable()
  1777. result.typeStack = @[]
  1778. result.typeNodesName = getTempName(result)
  1779. result.nimTypesName = getTempName(result)
  1780. # no line tracing for the init sections of the system module so that we
  1781. # don't generate a TFrame which can confuse the stack bottom initialization:
  1782. if sfSystemModule in module.flags:
  1783. incl result.flags, preventStackTrace
  1784. excl(result.preInitProc.options, optStackTrace)
  1785. let ndiName = if optCDebug in g.config.globalOptions: changeFileExt(completeCfilePath(g.config, filename), "ndi")
  1786. else: AbsoluteFile""
  1787. open(result.ndi, ndiName, g.config)
  1788. proc rawNewModule(g: BModuleList; module: PSym; conf: ConfigRef): BModule =
  1789. result = rawNewModule(g, module, AbsoluteFile toFullPath(conf, module.position.FileIndex))
  1790. proc newModule*(g: BModuleList; module: PSym; conf: ConfigRef): BModule =
  1791. # we should create only one cgen module for each module sym
  1792. result = rawNewModule(g, module, conf)
  1793. if module.position >= g.modules.len:
  1794. setLen(g.modules, module.position + 1)
  1795. #growCache g.modules, module.position
  1796. g.modules[module.position] = result
  1797. template injectG() {.dirty.} =
  1798. if graph.backend == nil:
  1799. graph.backend = newModuleList(graph)
  1800. let g = BModuleList(graph.backend)
  1801. proc setupCgen*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  1802. injectG()
  1803. result = newModule(g, module, graph.config)
  1804. result.idgen = idgen
  1805. if optGenIndex in graph.config.globalOptions and g.generatedHeader == nil:
  1806. let f = if graph.config.headerFile.len > 0: AbsoluteFile graph.config.headerFile
  1807. else: graph.config.projectFull
  1808. g.generatedHeader = rawNewModule(g, module,
  1809. changeFileExt(completeCfilePath(graph.config, f), hExt))
  1810. incl g.generatedHeader.flags, isHeaderFile
  1811. proc writeHeader(m: BModule) =
  1812. var result = headerTop()
  1813. var guard = "__$1__" % [m.filename.splitFile.name.rope]
  1814. result.addf("#ifndef $1$n#define $1$n", [guard])
  1815. addNimDefines(result, m.config)
  1816. generateHeaders(m)
  1817. generateThreadLocalStorage(m)
  1818. for i in cfsHeaders..cfsProcs:
  1819. result.add(m.s[i])
  1820. if m.config.cppCustomNamespace.len > 0 and i == cfsHeaders:
  1821. openNamespaceNim(m.config.cppCustomNamespace, result)
  1822. result.add(m.s[cfsInitProc])
  1823. if optGenDynLib in m.config.globalOptions:
  1824. result.add("N_LIB_IMPORT ")
  1825. result.addf("N_CDECL(void, $1NimMain)(void);$n", [rope m.config.nimMainPrefix])
  1826. if m.config.cppCustomNamespace.len > 0: closeNamespaceNim(result)
  1827. result.addf("#endif /* $1 */$n", [guard])
  1828. if not writeRope(result, m.filename):
  1829. rawMessage(m.config, errCannotOpenFile, m.filename.string)
  1830. proc getCFile(m: BModule): AbsoluteFile =
  1831. let ext =
  1832. if m.compileToCpp: ".nim.cpp"
  1833. elif m.config.backend == backendObjc or sfCompileToObjc in m.module.flags: ".nim.m"
  1834. else: ".nim.c"
  1835. result = changeFileExt(completeCfilePath(m.config, mangleModuleName(m.config, m.cfilename).AbsoluteFile), ext)
  1836. when false:
  1837. proc myOpenCached(graph: ModuleGraph; module: PSym, rd: PRodReader): PPassContext =
  1838. injectG()
  1839. var m = newModule(g, module, graph.config)
  1840. readMergeInfo(getCFile(m), m)
  1841. result = m
  1842. proc addHcrInitGuards(p: BProc, n: PNode, inInitGuard: var bool) =
  1843. if n.kind == nkStmtList:
  1844. for child in n:
  1845. addHcrInitGuards(p, child, inInitGuard)
  1846. else:
  1847. let stmtShouldExecute = n.kind in {nkVarSection, nkLetSection} or
  1848. nfExecuteOnReload in n.flags
  1849. if inInitGuard:
  1850. if stmtShouldExecute:
  1851. endBlock(p)
  1852. inInitGuard = false
  1853. else:
  1854. if not stmtShouldExecute:
  1855. line(p, cpsStmts, "if (nim_hcr_do_init_)\n")
  1856. startBlock(p)
  1857. inInitGuard = true
  1858. genStmts(p, n)
  1859. proc genTopLevelStmt*(m: BModule; n: PNode) =
  1860. ## Also called from `ic/cbackend.nim`.
  1861. if pipelineutils.skipCodegen(m.config, n): return
  1862. m.initProc.options = initProcOptions(m)
  1863. #softRnl = if optLineDir in m.config.options: noRnl else: rnl
  1864. # XXX replicate this logic!
  1865. var transformedN = transformStmt(m.g.graph, m.idgen, m.module, n)
  1866. if sfInjectDestructors in m.module.flags:
  1867. transformedN = injectDestructorCalls(m.g.graph, m.idgen, m.module, transformedN)
  1868. if m.hcrOn:
  1869. addHcrInitGuards(m.initProc, transformedN, m.inHcrInitGuard)
  1870. else:
  1871. genProcBody(m.initProc, transformedN)
  1872. proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool =
  1873. if optForceFullMake notin m.config.globalOptions:
  1874. if not moduleHasChanged(m.g.graph, m.module):
  1875. result = false
  1876. elif not equalsFile(code, cfile.cname):
  1877. when false:
  1878. #m.config.symbolFiles == readOnlySf: #isDefined(m.config, "nimdiff"):
  1879. if fileExists(cfile.cname):
  1880. copyFile(cfile.cname.string, cfile.cname.string & ".backup")
  1881. echo "diff ", cfile.cname.string, ".backup ", cfile.cname.string
  1882. else:
  1883. echo "new file ", cfile.cname.string
  1884. if not writeRope(code, cfile.cname):
  1885. rawMessage(m.config, errCannotOpenFile, cfile.cname.string)
  1886. result = true
  1887. elif fileExists(cfile.obj) and os.fileNewer(cfile.obj.string, cfile.cname.string):
  1888. result = false
  1889. else:
  1890. result = true
  1891. else:
  1892. if not writeRope(code, cfile.cname):
  1893. rawMessage(m.config, errCannotOpenFile, cfile.cname.string)
  1894. result = true
  1895. # We need 2 different logics here: pending modules (including
  1896. # 'nim__dat') may require file merging for the combination of dead code
  1897. # elimination and incremental compilation! Non pending modules need no
  1898. # such logic and in fact the logic hurts for the main module at least;
  1899. # it would generate multiple 'main' procs, for instance.
  1900. proc writeModule(m: BModule, pending: bool) =
  1901. template onExit() = close(m.ndi, m.config)
  1902. let cfile = getCFile(m)
  1903. if moduleHasChanged(m.g.graph, m.module):
  1904. genInitCode(m)
  1905. finishTypeDescriptions(m)
  1906. if sfMainModule in m.module.flags:
  1907. # generate main file:
  1908. genMainProc(m)
  1909. m.s[cfsProcHeaders].add(m.g.mainModProcs)
  1910. generateThreadVarsSize(m)
  1911. var cf = Cfile(nimname: m.module.name.s, cname: cfile,
  1912. obj: completeCfilePath(m.config, toObjFile(m.config, cfile)), flags: {})
  1913. var code = genModule(m, cf)
  1914. if code != "" or m.config.symbolFiles != disabledSf:
  1915. when hasTinyCBackend:
  1916. if m.config.cmd == cmdTcc:
  1917. tccgen.compileCCode($code, m.config)
  1918. onExit()
  1919. return
  1920. if not shouldRecompile(m, code, cf): cf.flags = {CfileFlag.Cached}
  1921. addFileToCompile(m.config, cf)
  1922. onExit()
  1923. proc updateCachedModule(m: BModule) =
  1924. let cfile = getCFile(m)
  1925. var cf = Cfile(nimname: m.module.name.s, cname: cfile,
  1926. obj: completeCfilePath(m.config, toObjFile(m.config, cfile)), flags: {})
  1927. if sfMainModule notin m.module.flags:
  1928. genMainProc(m)
  1929. cf.flags = {CfileFlag.Cached}
  1930. addFileToCompile(m.config, cf)
  1931. proc generateLibraryDestroyGlobals(graph: ModuleGraph; m: BModule; body: PNode; isDynlib: bool): PSym =
  1932. let procname = getIdent(graph.cache, "NimDestroyGlobals")
  1933. result = newSym(skProc, procname, m.idgen, m.module.owner, m.module.info)
  1934. result.typ = newProcType(m.module.info, m.idgen, m.module.owner)
  1935. result.typ.callConv = ccCDecl
  1936. incl result.flags, sfExportc
  1937. result.loc.r = "NimDestroyGlobals"
  1938. if isDynlib:
  1939. incl(result.loc.flags, lfExportLib)
  1940. let theProc = newNodeI(nkProcDef, m.module.info, bodyPos+1)
  1941. for i in 0..<theProc.len: theProc[i] = newNodeI(nkEmpty, m.module.info)
  1942. theProc[namePos] = newSymNode(result)
  1943. theProc[bodyPos] = body
  1944. result.ast = theProc
  1945. proc finalCodegenActions*(graph: ModuleGraph; m: BModule; n: PNode) =
  1946. ## Also called from IC.
  1947. if sfMainModule in m.module.flags:
  1948. # phase ordering problem here: We need to announce this
  1949. # dependency to 'nimTestErrorFlag' before system.c has been written to disk.
  1950. if m.config.exc == excGoto and getCompilerProc(graph, "nimTestErrorFlag") != nil:
  1951. cgsym(m, "nimTestErrorFlag")
  1952. if {optGenStaticLib, optGenDynLib, optNoMain} * m.config.globalOptions == {}:
  1953. for i in countdown(high(graph.globalDestructors), 0):
  1954. n.add graph.globalDestructors[i]
  1955. else:
  1956. var body = newNodeI(nkStmtList, m.module.info)
  1957. for i in countdown(high(graph.globalDestructors), 0):
  1958. body.add graph.globalDestructors[i]
  1959. body.flags.incl nfTransf # should not be further transformed
  1960. let dtor = generateLibraryDestroyGlobals(graph, m, body, optGenDynLib in m.config.globalOptions)
  1961. genProcAux(m, dtor)
  1962. if pipelineutils.skipCodegen(m.config, n): return
  1963. if moduleHasChanged(graph, m.module):
  1964. # if the module is cached, we don't regenerate the main proc
  1965. # nor the dispatchers? But if the dispatchers changed?
  1966. # XXX emit the dispatchers into its own .c file?
  1967. if n != nil:
  1968. m.initProc.options = initProcOptions(m)
  1969. genProcBody(m.initProc, n)
  1970. if m.hcrOn:
  1971. # make sure this is pulled in (meaning hcrGetGlobal() is called for it during init)
  1972. let sym = magicsys.getCompilerProc(m.g.graph, "programResult")
  1973. # ignore when not available, could be a module imported early in `system`
  1974. if sym != nil:
  1975. cgsymImpl m, sym
  1976. if m.inHcrInitGuard:
  1977. endBlock(m.initProc)
  1978. if sfMainModule in m.module.flags:
  1979. if m.hcrOn:
  1980. # pull ("define" since they are inline when HCR is on) these functions in the main file
  1981. # so it can load the HCR runtime and later pass the library handle to the HCR runtime which
  1982. # will in turn pass it to the other modules it initializes so they can initialize the
  1983. # register/get procs so they don't have to have the definitions of these functions as well
  1984. cgsym(m, "nimLoadLibrary")
  1985. cgsym(m, "nimLoadLibraryError")
  1986. cgsym(m, "nimGetProcAddr")
  1987. cgsym(m, "procAddrError")
  1988. cgsym(m, "rawWrite")
  1989. # raise dependencies on behalf of genMainProc
  1990. if m.config.target.targetOS != osStandalone and m.config.selectedGC notin {gcNone, gcArc, gcAtomicArc, gcOrc}:
  1991. cgsym(m, "initStackBottomWith")
  1992. if emulatedThreadVars(m.config) and m.config.target.targetOS != osStandalone:
  1993. cgsym(m, "initThreadVarsEmulation")
  1994. if m.g.forwardedProcs.len == 0:
  1995. incl m.flags, objHasKidsValid
  1996. if optMultiMethods in m.g.config.globalOptions or
  1997. m.g.config.selectedGC notin {gcArc, gcOrc, gcAtomicArc} or
  1998. vtables notin m.g.config.features:
  1999. generateIfMethodDispatchers(graph, m.idgen)
  2000. let mm = m
  2001. m.g.modulesClosed.add mm
  2002. proc genForwardedProcs(g: BModuleList) =
  2003. # Forward declared proc:s lack bodies when first encountered, so they're given
  2004. # a second pass here
  2005. # Note: ``genProcNoForward`` may add to ``forwardedProcs``
  2006. while g.forwardedProcs.len > 0:
  2007. let
  2008. prc = g.forwardedProcs.pop()
  2009. m = g.modules[prc.itemId.module]
  2010. if sfForward in prc.flags:
  2011. internalError(m.config, prc.info, "still forwarded: " & prc.name.s)
  2012. genProcNoForward(m, prc)
  2013. proc cgenWriteModules*(backend: RootRef, config: ConfigRef) =
  2014. let g = BModuleList(backend)
  2015. g.config = config
  2016. # we need to process the transitive closure because recursive module
  2017. # deps are allowed (and the system module is processed in the wrong
  2018. # order anyway)
  2019. genForwardedProcs(g)
  2020. for m in cgenModules(g):
  2021. m.writeModule(pending=true)
  2022. writeMapping(config, g.mapping)
  2023. if g.generatedHeader != nil: writeHeader(g.generatedHeader)