cgen.nim 99 KB

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