cgen.nim 85 KB

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