cgen.nim 87 KB

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