cgen.nim 86 KB

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