ccgstmts.nim 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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. # included from cgen.nim
  10. const
  11. RangeExpandLimit = 256 # do not generate ranges
  12. # over 'RangeExpandLimit' elements
  13. stringCaseThreshold = 8
  14. # above X strings a hash-switch for strings is generated
  15. proc getTraverseProc(p: BProc, v: PSym): Rope =
  16. if p.config.selectedGC in {gcMarkAndSweep, gcHooks, gcV2, gcRefc} and
  17. optOwnedRefs notin p.config.globalOptions and
  18. containsGarbageCollectedRef(v.loc.t):
  19. # we register a specialized marked proc here; this has the advantage
  20. # that it works out of the box for thread local storage then :-)
  21. result = genTraverseProcForGlobal(p.module, v, v.info)
  22. proc registerTraverseProc(p: BProc, v: PSym, traverseProc: Rope) =
  23. if sfThread in v.flags:
  24. appcg(p.module, p.module.preInitProc.procSec(cpsInit),
  25. "$n\t#nimRegisterThreadLocalMarker($1);$n$n", [traverseProc])
  26. else:
  27. appcg(p.module, p.module.preInitProc.procSec(cpsInit),
  28. "$n\t#nimRegisterGlobalMarker($1);$n$n", [traverseProc])
  29. proc isAssignedImmediately(conf: ConfigRef; n: PNode): bool {.inline.} =
  30. if n.kind == nkEmpty: return false
  31. if isInvalidReturnType(conf, n.typ):
  32. # var v = f()
  33. # is transformed into: var v; f(addr v)
  34. # where 'f' **does not** initialize the result!
  35. return false
  36. result = true
  37. proc inExceptBlockLen(p: BProc): int =
  38. for x in p.nestedTryStmts:
  39. if x.inExcept: result.inc
  40. proc startBlockInternal(p: BProc): int {.discardable.} =
  41. inc(p.labels)
  42. result = p.blocks.len
  43. setLen(p.blocks, result + 1)
  44. p.blocks[result].id = p.labels
  45. p.blocks[result].nestedTryStmts = p.nestedTryStmts.len.int16
  46. p.blocks[result].nestedExceptStmts = p.inExceptBlockLen.int16
  47. template startBlock(p: BProc, start: FormatStr = "{$n",
  48. args: varargs[Rope]): int =
  49. lineCg(p, cpsStmts, start, args)
  50. startBlockInternal(p)
  51. proc endBlock(p: BProc)
  52. proc genVarTuple(p: BProc, n: PNode) =
  53. var tup, field: TLoc
  54. if n.kind != nkVarTuple: internalError(p.config, n.info, "genVarTuple")
  55. # if we have a something that's been captured, use the lowering instead:
  56. for i in 0..<n.len-2:
  57. if n[i].kind != nkSym:
  58. genStmts(p, lowerTupleUnpacking(p.module.g.graph, n, p.prc))
  59. return
  60. # check only the first son
  61. var forHcr = treatGlobalDifferentlyForHCR(p.module, n[0].sym)
  62. let hcrCond = if forHcr: getTempName(p.module) else: nil
  63. var hcrGlobals: seq[tuple[loc: TLoc, tp: Rope]]
  64. # determine if the tuple is constructed at top-level scope or inside of a block (if/while/block)
  65. let isGlobalInBlock = forHcr and p.blocks.len > 2
  66. # do not close and reopen blocks if this is a 'global' but inside of a block (if/while/block)
  67. forHcr = forHcr and not isGlobalInBlock
  68. if forHcr:
  69. # check with the boolean if the initializing code for the tuple should be ran
  70. lineCg(p, cpsStmts, "if ($1)$n", [hcrCond])
  71. startBlock(p)
  72. genLineDir(p, n)
  73. initLocExpr(p, n[^1], tup)
  74. var t = tup.t.skipTypes(abstractInst)
  75. for i in 0..<n.len-2:
  76. let vn = n[i]
  77. let v = vn.sym
  78. if sfCompileTime in v.flags: continue
  79. var traverseProc: Rope
  80. if sfGlobal in v.flags:
  81. assignGlobalVar(p, vn, nil)
  82. genObjectInit(p, cpsInit, v.typ, v.loc, constructObj)
  83. traverseProc = getTraverseProc(p, v)
  84. if traverseProc != nil and not p.hcrOn:
  85. registerTraverseProc(p, v, traverseProc)
  86. else:
  87. assignLocalVar(p, vn)
  88. initLocalVar(p, v, immediateAsgn=isAssignedImmediately(p.config, n[^1]))
  89. initLoc(field, locExpr, vn, tup.storage)
  90. if t.kind == tyTuple:
  91. field.r = "$1.Field$2" % [rdLoc(tup), rope(i)]
  92. else:
  93. if t.n[i].kind != nkSym: internalError(p.config, n.info, "genVarTuple")
  94. field.r = "$1.$2" % [rdLoc(tup), mangleRecFieldName(p.module, t.n[i].sym)]
  95. putLocIntoDest(p, v.loc, field)
  96. if forHcr or isGlobalInBlock:
  97. hcrGlobals.add((loc: v.loc, tp: if traverseProc == nil: ~"NULL" else: traverseProc))
  98. if forHcr:
  99. # end the block where the tuple gets initialized
  100. endBlock(p)
  101. if forHcr or isGlobalInBlock:
  102. # insert the registration of the globals for the different parts of the tuple at the
  103. # start of the current scope (after they have been iterated) and init a boolean to
  104. # check if any of them is newly introduced and the initializing code has to be ran
  105. lineCg(p, cpsLocals, "NIM_BOOL $1 = NIM_FALSE;$n", [hcrCond])
  106. for curr in hcrGlobals:
  107. lineCg(p, cpsLocals, "$1 |= hcrRegisterGlobal($4, \"$2\", sizeof($3), $5, (void**)&$2);$N",
  108. [hcrCond, curr.loc.r, rdLoc(curr.loc), getModuleDllPath(p.module, n[0].sym), curr.tp])
  109. proc loadInto(p: BProc, le, ri: PNode, a: var TLoc) {.inline.} =
  110. if ri.kind in nkCallKinds and (ri[0].kind != nkSym or
  111. ri[0].sym.magic == mNone):
  112. genAsgnCall(p, le, ri, a)
  113. else:
  114. # this is a hacky way to fix #1181 (tmissingderef)::
  115. #
  116. # var arr1 = cast[ptr array[4, int8]](addr foo)[]
  117. #
  118. # However, fixing this properly really requires modelling 'array' as
  119. # a 'struct' in C to preserve dereferencing semantics completely. Not
  120. # worth the effort until version 1.0 is out.
  121. a.flags.incl(lfEnforceDeref)
  122. expr(p, ri, a)
  123. proc assignLabel(b: var TBlock): Rope {.inline.} =
  124. b.label = "LA" & b.id.rope
  125. result = b.label
  126. proc blockBody(b: var TBlock): Rope =
  127. result = b.sections[cpsLocals]
  128. if b.frameLen > 0:
  129. result.addf("FR_.len+=$1;$n", [b.frameLen.rope])
  130. result.add(b.sections[cpsInit])
  131. result.add(b.sections[cpsStmts])
  132. proc endBlock(p: BProc, blockEnd: Rope) =
  133. let topBlock = p.blocks.len-1
  134. # the block is merged into the parent block
  135. p.blocks[topBlock-1].sections[cpsStmts].add(p.blocks[topBlock].blockBody)
  136. setLen(p.blocks, topBlock)
  137. # this is done after the block is popped so $n is
  138. # properly indented when pretty printing is enabled
  139. line(p, cpsStmts, blockEnd)
  140. proc endBlock(p: BProc) =
  141. let topBlock = p.blocks.len - 1
  142. let frameLen = p.blocks[topBlock].frameLen
  143. var blockEnd: Rope
  144. if frameLen > 0:
  145. blockEnd.addf("FR_.len-=$1;$n", [frameLen.rope])
  146. if p.blocks[topBlock].label != nil:
  147. blockEnd.addf("} $1: ;$n", [p.blocks[topBlock].label])
  148. else:
  149. blockEnd.addf("}$n", [])
  150. endBlock(p, blockEnd)
  151. proc genSimpleBlock(p: BProc, stmts: PNode) {.inline.} =
  152. startBlock(p)
  153. genStmts(p, stmts)
  154. endBlock(p)
  155. proc exprBlock(p: BProc, n: PNode, d: var TLoc) =
  156. startBlock(p)
  157. expr(p, n, d)
  158. endBlock(p)
  159. template preserveBreakIdx(body: untyped): untyped =
  160. var oldBreakIdx = p.breakIdx
  161. body
  162. p.breakIdx = oldBreakIdx
  163. proc genState(p: BProc, n: PNode) =
  164. internalAssert p.config, n.len == 1
  165. let n0 = n[0]
  166. if n0.kind == nkIntLit:
  167. let idx = n[0].intVal
  168. linefmt(p, cpsStmts, "STATE$1: ;$n", [idx])
  169. elif n0.kind == nkStrLit:
  170. linefmt(p, cpsStmts, "$1: ;$n", [n0.strVal])
  171. proc blockLeaveActions(p: BProc, howManyTrys, howManyExcepts: int) =
  172. # Called by return and break stmts.
  173. # Deals with issues faced when jumping out of try/except/finally stmts.
  174. var stack = newSeq[tuple[fin: PNode, inExcept: bool, label: Natural]](0)
  175. for i in 1..howManyTrys:
  176. let tryStmt = p.nestedTryStmts.pop
  177. if p.config.exc == excSetjmp:
  178. # Pop safe points generated by try
  179. if not tryStmt.inExcept:
  180. linefmt(p, cpsStmts, "#popSafePoint();$n", [])
  181. # Pop this try-stmt of the list of nested trys
  182. # so we don't infinite recurse on it in the next step.
  183. stack.add(tryStmt)
  184. # Find finally-stmt for this try-stmt
  185. # and generate a copy of its sons
  186. var finallyStmt = tryStmt.fin
  187. if finallyStmt != nil:
  188. genStmts(p, finallyStmt[0])
  189. # push old elements again:
  190. for i in countdown(howManyTrys-1, 0):
  191. p.nestedTryStmts.add(stack[i])
  192. # Pop exceptions that was handled by the
  193. # except-blocks we are in
  194. if noSafePoints notin p.flags:
  195. for i in countdown(howManyExcepts-1, 0):
  196. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  197. proc genGotoState(p: BProc, n: PNode) =
  198. # we resist the temptation to translate it into duff's device as it later
  199. # will be translated into computed gotos anyway for GCC at least:
  200. # switch (x.state) {
  201. # case 0: goto STATE0;
  202. # ...
  203. var a: TLoc
  204. initLocExpr(p, n[0], a)
  205. lineF(p, cpsStmts, "switch ($1) {$n", [rdLoc(a)])
  206. p.flags.incl beforeRetNeeded
  207. lineF(p, cpsStmts, "case -1:$n", [])
  208. blockLeaveActions(p,
  209. howManyTrys = p.nestedTryStmts.len,
  210. howManyExcepts = p.inExceptBlockLen)
  211. lineF(p, cpsStmts, " goto BeforeRet_;$n", [])
  212. var statesCounter = lastOrd(p.config, n[0].typ)
  213. if n.len >= 2 and n[1].kind == nkIntLit:
  214. statesCounter = getInt(n[1])
  215. let prefix = if n.len == 3 and n[2].kind == nkStrLit: n[2].strVal.rope
  216. else: rope"STATE"
  217. for i in 0i64..toInt64(statesCounter):
  218. lineF(p, cpsStmts, "case $2: goto $1$2;$n", [prefix, rope(i)])
  219. lineF(p, cpsStmts, "}$n", [])
  220. proc genBreakState(p: BProc, n: PNode, d: var TLoc) =
  221. var a: TLoc
  222. initLoc(d, locExpr, n, OnUnknown)
  223. if n[0].kind == nkClosure:
  224. initLocExpr(p, n[0][1], a)
  225. d.r = "(((NI*) $1)[1] < 0)" % [rdLoc(a)]
  226. else:
  227. initLocExpr(p, n[0], a)
  228. # the environment is guaranteed to contain the 'state' field at offset 1:
  229. d.r = "((((NI*) $1.ClE_0)[1]) < 0)" % [rdLoc(a)]
  230. proc genGotoVar(p: BProc; value: PNode) =
  231. if value.kind notin {nkCharLit..nkUInt64Lit}:
  232. localError(p.config, value.info, "'goto' target must be a literal value")
  233. else:
  234. lineF(p, cpsStmts, "goto NIMSTATE_$#;$n", [value.intVal.rope])
  235. proc genBracedInit(p: BProc, n: PNode; isConst: bool): Rope
  236. proc potentialValueInit(p: BProc; v: PSym; value: PNode): Rope =
  237. if lfDynamicLib in v.loc.flags or sfThread in v.flags or p.hcrOn:
  238. result = nil
  239. elif sfGlobal in v.flags and value != nil and isDeepConstExpr(value, p.module.compileToCpp) and
  240. p.withinLoop == 0 and not containsGarbageCollectedRef(v.typ):
  241. #echo "New code produced for ", v.name.s, " ", p.config $ value.info
  242. result = genBracedInit(p, value, isConst = false)
  243. else:
  244. result = nil
  245. proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
  246. if sfGoto in v.flags:
  247. # translate 'var state {.goto.} = X' into 'goto LX':
  248. genGotoVar(p, value)
  249. return
  250. var targetProc = p
  251. var traverseProc: Rope
  252. let valueAsRope = potentialValueInit(p, v, value)
  253. if sfGlobal in v.flags:
  254. if v.flags * {sfImportc, sfExportc} == {sfImportc} and
  255. value.kind == nkEmpty and
  256. v.loc.flags * {lfHeader, lfNoDecl} != {}:
  257. return
  258. if sfPure in v.flags:
  259. # v.owner.kind != skModule:
  260. targetProc = p.module.preInitProc
  261. assignGlobalVar(targetProc, vn, valueAsRope)
  262. # XXX: be careful here.
  263. # Global variables should not be zeromem-ed within loops
  264. # (see bug #20).
  265. # That's why we are doing the construction inside the preInitProc.
  266. # genObjectInit relies on the C runtime's guarantees that
  267. # global variables will be initialized to zero.
  268. if valueAsRope == nil:
  269. var loc = v.loc
  270. # When the native TLS is unavailable, a global thread-local variable needs
  271. # one more layer of indirection in order to access the TLS block.
  272. # Only do this for complex types that may need a call to `objectInit`
  273. if sfThread in v.flags and emulatedThreadVars(p.config) and
  274. isComplexValueType(v.typ):
  275. initLocExprSingleUse(p.module.preInitProc, vn, loc)
  276. genObjectInit(p.module.preInitProc, cpsInit, v.typ, loc, constructObj)
  277. # Alternative construction using default constructor (which may zeromem):
  278. # if sfImportc notin v.flags: constructLoc(p.module.preInitProc, v.loc)
  279. if sfExportc in v.flags and p.module.g.generatedHeader != nil:
  280. genVarPrototype(p.module.g.generatedHeader, vn)
  281. traverseProc = getTraverseProc(p, v)
  282. if traverseProc != nil and not p.hcrOn:
  283. registerTraverseProc(p, v, traverseProc)
  284. else:
  285. let imm = isAssignedImmediately(p.config, value)
  286. if imm and p.module.compileToCpp and p.splitDecls == 0 and
  287. not containsHiddenPointer(v.typ):
  288. # C++ really doesn't like things like 'Foo f; f = x' as that invokes a
  289. # parameterless constructor followed by an assignment operator. So we
  290. # generate better code here: 'Foo f = x;'
  291. genLineDir(p, vn)
  292. let decl = localVarDecl(p, vn)
  293. var tmp: TLoc
  294. if value.kind in nkCallKinds and value[0].kind == nkSym and
  295. sfConstructor in value[0].sym.flags:
  296. var params: Rope
  297. let typ = skipTypes(value[0].typ, abstractInst)
  298. assert(typ.kind == tyProc)
  299. for i in 1..<value.len:
  300. if params != nil: params.add(~", ")
  301. assert(typ.len == typ.n.len)
  302. params.add(genOtherArg(p, value, i, typ))
  303. if params == nil:
  304. lineF(p, cpsStmts, "$#;$n", [decl])
  305. else:
  306. lineF(p, cpsStmts, "$#($#);$n", [decl, params])
  307. else:
  308. initLocExprSingleUse(p, value, tmp)
  309. lineF(p, cpsStmts, "$# = $#;$n", [decl, tmp.rdLoc])
  310. return
  311. assignLocalVar(p, vn)
  312. initLocalVar(p, v, imm)
  313. if traverseProc == nil: traverseProc = ~"NULL"
  314. # If the var is in a block (control flow like if/while or a block) in global scope just
  315. # register the so called "global" so it can be used later on. There is no need to close
  316. # and reopen of if (nim_hcr_do_init_) blocks because we are in one already anyway.
  317. var forHcr = treatGlobalDifferentlyForHCR(p.module, v)
  318. if forHcr and targetProc.blocks.len > 3 and v.owner.kind == skModule:
  319. # put it in the locals section - mainly because of loops which
  320. # use the var in a call to resetLoc() in the statements section
  321. lineCg(targetProc, cpsLocals, "hcrRegisterGlobal($3, \"$1\", sizeof($2), $4, (void**)&$1);$n",
  322. [v.loc.r, rdLoc(v.loc), getModuleDllPath(p.module, v), traverseProc])
  323. # nothing special left to do later on - let's avoid closing and reopening blocks
  324. forHcr = false
  325. # we close and reopen the global if (nim_hcr_do_init_) blocks in the main Init function
  326. # for the module so we can have globals and top-level code be interleaved and still
  327. # be able to re-run it but without the top level code - just the init of globals
  328. if forHcr:
  329. lineCg(targetProc, cpsStmts, "if (hcrRegisterGlobal($3, \"$1\", sizeof($2), $4, (void**)&$1))$N",
  330. [v.loc.r, rdLoc(v.loc), getModuleDllPath(p.module, v), traverseProc])
  331. startBlock(targetProc)
  332. if value.kind != nkEmpty and valueAsRope == nil:
  333. genLineDir(targetProc, vn)
  334. loadInto(targetProc, vn, value, v.loc)
  335. if forHcr:
  336. endBlock(targetProc)
  337. proc genSingleVar(p: BProc, a: PNode) =
  338. let v = a[0].sym
  339. if sfCompileTime in v.flags: return
  340. genSingleVar(p, v, a[0], a[2])
  341. proc genClosureVar(p: BProc, a: PNode) =
  342. var immediateAsgn = a[2].kind != nkEmpty
  343. var v: TLoc
  344. initLocExpr(p, a[0], v)
  345. genLineDir(p, a)
  346. if immediateAsgn:
  347. loadInto(p, a[0], a[2], v)
  348. else:
  349. constructLoc(p, v)
  350. proc genVarStmt(p: BProc, n: PNode) =
  351. for it in n.sons:
  352. if it.kind == nkCommentStmt: continue
  353. if it.kind == nkIdentDefs:
  354. # can be a lifted var nowadays ...
  355. if it[0].kind == nkSym:
  356. genSingleVar(p, it)
  357. else:
  358. genClosureVar(p, it)
  359. else:
  360. genVarTuple(p, it)
  361. proc genIf(p: BProc, n: PNode, d: var TLoc) =
  362. #
  363. # { if (!expr1) goto L1;
  364. # thenPart }
  365. # goto LEnd
  366. # L1:
  367. # { if (!expr2) goto L2;
  368. # thenPart2 }
  369. # goto LEnd
  370. # L2:
  371. # { elsePart }
  372. # Lend:
  373. var
  374. a: TLoc
  375. lelse: TLabel
  376. if not isEmptyType(n.typ) and d.k == locNone:
  377. getTemp(p, n.typ, d)
  378. genLineDir(p, n)
  379. let lend = getLabel(p)
  380. for it in n.sons:
  381. # bug #4230: avoid false sharing between branches:
  382. if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
  383. if it.len == 2:
  384. startBlock(p)
  385. initLocExprSingleUse(p, it[0], a)
  386. lelse = getLabel(p)
  387. inc(p.labels)
  388. lineF(p, cpsStmts, "if (!$1) goto $2;$n",
  389. [rdLoc(a), lelse])
  390. if p.module.compileToCpp:
  391. # avoid "jump to label crosses initialization" error:
  392. p.s(cpsStmts).add "{"
  393. expr(p, it[1], d)
  394. p.s(cpsStmts).add "}"
  395. else:
  396. expr(p, it[1], d)
  397. endBlock(p)
  398. if n.len > 1:
  399. lineF(p, cpsStmts, "goto $1;$n", [lend])
  400. fixLabel(p, lelse)
  401. elif it.len == 1:
  402. startBlock(p)
  403. expr(p, it[0], d)
  404. endBlock(p)
  405. else: internalError(p.config, n.info, "genIf()")
  406. if n.len > 1: fixLabel(p, lend)
  407. proc genReturnStmt(p: BProc, t: PNode) =
  408. if nfPreventCg in t.flags: return
  409. p.flags.incl beforeRetNeeded
  410. genLineDir(p, t)
  411. if (t[0].kind != nkEmpty): genStmts(p, t[0])
  412. blockLeaveActions(p,
  413. howManyTrys = p.nestedTryStmts.len,
  414. howManyExcepts = p.inExceptBlockLen)
  415. if (p.finallySafePoints.len > 0) and noSafePoints notin p.flags:
  416. # If we're in a finally block, and we came here by exception
  417. # consume it before we return.
  418. var safePoint = p.finallySafePoints[^1]
  419. linefmt(p, cpsStmts, "if ($1.status != 0) #popCurrentException();$n", [safePoint])
  420. lineF(p, cpsStmts, "goto BeforeRet_;$n", [])
  421. proc genGotoForCase(p: BProc; caseStmt: PNode) =
  422. for i in 1..<caseStmt.len:
  423. startBlock(p)
  424. let it = caseStmt[i]
  425. for j in 0..<it.len-1:
  426. if it[j].kind == nkRange:
  427. localError(p.config, it.info, "range notation not available for computed goto")
  428. return
  429. let val = getOrdValue(it[j])
  430. lineF(p, cpsStmts, "NIMSTATE_$#:$n", [val.rope])
  431. genStmts(p, it.lastSon)
  432. endBlock(p)
  433. iterator fieldValuePairs(n: PNode): tuple[memberSym, valueSym: PNode] =
  434. assert(n.kind in {nkLetSection, nkVarSection})
  435. for identDefs in n:
  436. if identDefs.kind == nkIdentDefs:
  437. let valueSym = identDefs[^1]
  438. for i in 0..<identDefs.len-2:
  439. let memberSym = identDefs[i]
  440. yield((memberSym: memberSym, valueSym: valueSym))
  441. proc genComputedGoto(p: BProc; n: PNode) =
  442. # first pass: Generate array of computed labels:
  443. # flatten the loop body because otherwise let and var sections
  444. # wrapped inside stmt lists by inject destructors won't be recognised
  445. let n = n.flattenStmts()
  446. var casePos = -1
  447. var arraySize: int
  448. for i in 0..<n.len:
  449. let it = n[i]
  450. if it.kind == nkCaseStmt:
  451. if lastSon(it).kind != nkOfBranch:
  452. localError(p.config, it.info,
  453. "case statement must be exhaustive for computed goto"); return
  454. casePos = i
  455. if enumHasHoles(it[0].typ):
  456. localError(p.config, it.info,
  457. "case statement cannot work on enums with holes for computed goto"); return
  458. let aSize = lengthOrd(p.config, it[0].typ)
  459. if aSize > 10_000:
  460. localError(p.config, it.info,
  461. "case statement has too many cases for computed goto"); return
  462. arraySize = toInt(aSize)
  463. if firstOrd(p.config, it[0].typ) != 0:
  464. localError(p.config, it.info,
  465. "case statement has to start at 0 for computed goto"); return
  466. if casePos < 0:
  467. localError(p.config, n.info, "no case statement found for computed goto"); return
  468. var id = p.labels+1
  469. inc p.labels, arraySize+1
  470. let tmp = "TMP$1_" % [id.rope]
  471. var gotoArray = "static void* $#[$#] = {" % [tmp, arraySize.rope]
  472. for i in 1..arraySize-1:
  473. gotoArray.addf("&&TMP$#_, ", [rope(id+i)])
  474. gotoArray.addf("&&TMP$#_};$n", [rope(id+arraySize)])
  475. line(p, cpsLocals, gotoArray)
  476. for j in 0..<casePos:
  477. genStmts(p, n[j])
  478. let caseStmt = n[casePos]
  479. var a: TLoc
  480. initLocExpr(p, caseStmt[0], a)
  481. # first goto:
  482. lineF(p, cpsStmts, "goto *$#[$#];$n", [tmp, a.rdLoc])
  483. for i in 1..<caseStmt.len:
  484. startBlock(p)
  485. let it = caseStmt[i]
  486. for j in 0..<it.len-1:
  487. if it[j].kind == nkRange:
  488. localError(p.config, it.info, "range notation not available for computed goto")
  489. return
  490. let val = getOrdValue(it[j])
  491. lineF(p, cpsStmts, "TMP$#_:$n", [intLiteral(toInt64(val)+id+1)])
  492. genStmts(p, it.lastSon)
  493. for j in casePos+1..<n.len:
  494. genStmts(p, n[j])
  495. for j in 0..<casePos:
  496. # prevent new local declarations
  497. # compile declarations as assignments
  498. let it = n[j]
  499. if it.kind in {nkLetSection, nkVarSection}:
  500. let asgn = copyNode(it)
  501. asgn.transitionSonsKind(nkAsgn)
  502. asgn.sons.setLen 2
  503. for sym, value in it.fieldValuePairs:
  504. if value.kind != nkEmpty:
  505. asgn[0] = sym
  506. asgn[1] = value
  507. genStmts(p, asgn)
  508. else:
  509. genStmts(p, it)
  510. var a: TLoc
  511. initLocExpr(p, caseStmt[0], a)
  512. lineF(p, cpsStmts, "goto *$#[$#];$n", [tmp, a.rdLoc])
  513. endBlock(p)
  514. for j in casePos+1..<n.len:
  515. genStmts(p, n[j])
  516. proc genWhileStmt(p: BProc, t: PNode) =
  517. # we don't generate labels here as for example GCC would produce
  518. # significantly worse code
  519. var
  520. a: TLoc
  521. assert(t.len == 2)
  522. inc(p.withinLoop)
  523. genLineDir(p, t)
  524. preserveBreakIdx:
  525. var loopBody = t[1]
  526. if loopBody.stmtsContainPragma(wComputedGoto) and
  527. hasComputedGoto in CC[p.config.cCompiler].props:
  528. # for closure support weird loop bodies are generated:
  529. if loopBody.len == 2 and loopBody[0].kind == nkEmpty:
  530. loopBody = loopBody[1]
  531. genComputedGoto(p, loopBody)
  532. else:
  533. p.breakIdx = startBlock(p, "while (1) {$n")
  534. p.blocks[p.breakIdx].isLoop = true
  535. initLocExpr(p, t[0], a)
  536. if (t[0].kind != nkIntLit) or (t[0].intVal == 0):
  537. let label = assignLabel(p.blocks[p.breakIdx])
  538. lineF(p, cpsStmts, "if (!$1) goto $2;$n", [rdLoc(a), label])
  539. genStmts(p, loopBody)
  540. if optProfiler in p.options:
  541. # invoke at loop body exit:
  542. linefmt(p, cpsStmts, "#nimProfile();$n", [])
  543. endBlock(p)
  544. dec(p.withinLoop)
  545. proc genBlock(p: BProc, n: PNode, d: var TLoc) =
  546. if not isEmptyType(n.typ):
  547. # bug #4505: allocate the temp in the outer scope
  548. # so that it can escape the generated {}:
  549. if d.k == locNone:
  550. getTemp(p, n.typ, d)
  551. d.flags.incl(lfEnforceDeref)
  552. preserveBreakIdx:
  553. p.breakIdx = startBlock(p)
  554. if n[0].kind != nkEmpty:
  555. # named block?
  556. assert(n[0].kind == nkSym)
  557. var sym = n[0].sym
  558. sym.loc.k = locOther
  559. sym.position = p.breakIdx+1
  560. expr(p, n[1], d)
  561. endBlock(p)
  562. proc genParForStmt(p: BProc, t: PNode) =
  563. assert(t.len == 3)
  564. inc(p.withinLoop)
  565. genLineDir(p, t)
  566. preserveBreakIdx:
  567. let forLoopVar = t[0].sym
  568. var rangeA, rangeB: TLoc
  569. assignLocalVar(p, t[0])
  570. #initLoc(forLoopVar.loc, locLocalVar, forLoopVar.typ, onStack)
  571. #discard mangleName(forLoopVar)
  572. let call = t[1]
  573. assert(call.len in {4, 5})
  574. initLocExpr(p, call[1], rangeA)
  575. initLocExpr(p, call[2], rangeB)
  576. # $n at the beginning because of #9710
  577. if call.len == 4: # `||`(a, b, annotation)
  578. lineF(p, cpsStmts, "$n#pragma omp $4$n" &
  579. "for ($1 = $2; $1 <= $3; ++$1)",
  580. [forLoopVar.loc.rdLoc,
  581. rangeA.rdLoc, rangeB.rdLoc,
  582. call[3].getStr.rope])
  583. else: # `||`(a, b, step, annotation)
  584. var step: TLoc
  585. initLocExpr(p, call[3], step)
  586. lineF(p, cpsStmts, "$n#pragma omp $5$n" &
  587. "for ($1 = $2; $1 <= $3; $1 += $4)",
  588. [forLoopVar.loc.rdLoc,
  589. rangeA.rdLoc, rangeB.rdLoc, step.rdLoc,
  590. call[4].getStr.rope])
  591. p.breakIdx = startBlock(p)
  592. p.blocks[p.breakIdx].isLoop = true
  593. genStmts(p, t[2])
  594. endBlock(p)
  595. dec(p.withinLoop)
  596. proc genBreakStmt(p: BProc, t: PNode) =
  597. var idx = p.breakIdx
  598. if t[0].kind != nkEmpty:
  599. # named break?
  600. assert(t[0].kind == nkSym)
  601. var sym = t[0].sym
  602. doAssert(sym.loc.k == locOther)
  603. idx = sym.position-1
  604. else:
  605. # an unnamed 'break' can only break a loop after 'transf' pass:
  606. while idx >= 0 and not p.blocks[idx].isLoop: dec idx
  607. if idx < 0 or not p.blocks[idx].isLoop:
  608. internalError(p.config, t.info, "no loop to break")
  609. let label = assignLabel(p.blocks[idx])
  610. blockLeaveActions(p,
  611. p.nestedTryStmts.len - p.blocks[idx].nestedTryStmts,
  612. p.inExceptBlockLen - p.blocks[idx].nestedExceptStmts)
  613. genLineDir(p, t)
  614. lineF(p, cpsStmts, "goto $1;$n", [label])
  615. proc raiseExit(p: BProc) =
  616. assert p.config.exc == excGoto
  617. if nimErrorFlagDisabled notin p.flags:
  618. p.flags.incl nimErrorFlagAccessed
  619. if p.nestedTryStmts.len == 0:
  620. p.flags.incl beforeRetNeeded
  621. # easy case, simply goto 'ret':
  622. lineCg(p, cpsStmts, "if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;$n", [])
  623. else:
  624. lineCg(p, cpsStmts, "if (NIM_UNLIKELY(*nimErr_)) goto LA$1_;$n",
  625. [p.nestedTryStmts[^1].label])
  626. proc finallyActions(p: BProc) =
  627. if p.config.exc != excGoto and p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept:
  628. # if the current try stmt have a finally block,
  629. # we must execute it before reraising
  630. let finallyBlock = p.nestedTryStmts[^1].fin
  631. if finallyBlock != nil:
  632. genSimpleBlock(p, finallyBlock[0])
  633. proc raiseInstr(p: BProc): Rope =
  634. if p.config.exc == excGoto:
  635. let L = p.nestedTryStmts.len
  636. if L == 0:
  637. p.flags.incl beforeRetNeeded
  638. # easy case, simply goto 'ret':
  639. result = ropecg(p.module, "goto BeforeRet_;$n", [])
  640. else:
  641. # raise inside an 'except' must go to the finally block,
  642. # raise outside an 'except' block must go to the 'except' list.
  643. result = ropecg(p.module, "goto LA$1_;$n",
  644. [p.nestedTryStmts[L-1].label])
  645. # + ord(p.nestedTryStmts[L-1].inExcept)])
  646. else:
  647. result = nil
  648. proc genRaiseStmt(p: BProc, t: PNode) =
  649. if t[0].kind != nkEmpty:
  650. var a: TLoc
  651. initLocExprSingleUse(p, t[0], a)
  652. finallyActions(p)
  653. var e = rdLoc(a)
  654. var typ = skipTypes(t[0].typ, abstractPtrs)
  655. # XXX For reasons that currently escape me, this is only required by the new
  656. # C++ based exception handling:
  657. if p.config.exc == excCpp:
  658. blockLeaveActions(p, howManyTrys = 0, howManyExcepts = p.inExceptBlockLen)
  659. genLineDir(p, t)
  660. if isImportedException(typ, p.config):
  661. lineF(p, cpsStmts, "throw $1;$n", [e])
  662. else:
  663. lineCg(p, cpsStmts, "#raiseExceptionEx((#Exception*)$1, $2, $3, $4, $5);$n",
  664. [e, makeCString(typ.sym.name.s),
  665. makeCString(if p.prc != nil: p.prc.name.s else: p.module.module.name.s),
  666. quotedFilename(p.config, t.info), toLinenumber(t.info)])
  667. if optOwnedRefs in p.config.globalOptions:
  668. lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [e])
  669. else:
  670. finallyActions(p)
  671. genLineDir(p, t)
  672. # reraise the last exception:
  673. if p.config.exc == excCpp:
  674. line(p, cpsStmts, ~"throw;$n")
  675. else:
  676. linefmt(p, cpsStmts, "#reraiseException();$n", [])
  677. let gotoInstr = raiseInstr(p)
  678. if gotoInstr != nil:
  679. line(p, cpsStmts, gotoInstr)
  680. template genCaseGenericBranch(p: BProc, b: PNode, e: TLoc,
  681. rangeFormat, eqFormat: FormatStr, labl: TLabel) =
  682. var x, y: TLoc
  683. for i in 0..<b.len - 1:
  684. if b[i].kind == nkRange:
  685. initLocExpr(p, b[i][0], x)
  686. initLocExpr(p, b[i][1], y)
  687. lineCg(p, cpsStmts, rangeFormat,
  688. [rdCharLoc(e), rdCharLoc(x), rdCharLoc(y), labl])
  689. else:
  690. initLocExpr(p, b[i], x)
  691. lineCg(p, cpsStmts, eqFormat, [rdCharLoc(e), rdCharLoc(x), labl])
  692. proc genCaseSecondPass(p: BProc, t: PNode, d: var TLoc,
  693. labId, until: int): TLabel =
  694. var lend = getLabel(p)
  695. for i in 1..until:
  696. # bug #4230: avoid false sharing between branches:
  697. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  698. lineF(p, cpsStmts, "LA$1_: ;$n", [rope(labId + i)])
  699. if t[i].kind == nkOfBranch:
  700. exprBlock(p, t[i][^1], d)
  701. lineF(p, cpsStmts, "goto $1;$n", [lend])
  702. else:
  703. exprBlock(p, t[i][0], d)
  704. result = lend
  705. template genIfForCaseUntil(p: BProc, t: PNode, d: var TLoc,
  706. rangeFormat, eqFormat: FormatStr,
  707. until: int, a: TLoc): TLabel =
  708. # generate a C-if statement for a Nim case statement
  709. var res: TLabel
  710. var labId = p.labels
  711. for i in 1..until:
  712. inc(p.labels)
  713. if t[i].kind == nkOfBranch: # else statement
  714. genCaseGenericBranch(p, t[i], a, rangeFormat, eqFormat,
  715. "LA" & rope(p.labels) & "_")
  716. else:
  717. lineF(p, cpsStmts, "goto LA$1_;$n", [rope(p.labels)])
  718. if until < t.len-1:
  719. inc(p.labels)
  720. var gotoTarget = p.labels
  721. lineF(p, cpsStmts, "goto LA$1_;$n", [rope(gotoTarget)])
  722. res = genCaseSecondPass(p, t, d, labId, until)
  723. lineF(p, cpsStmts, "LA$1_: ;$n", [rope(gotoTarget)])
  724. else:
  725. res = genCaseSecondPass(p, t, d, labId, until)
  726. res
  727. template genCaseGeneric(p: BProc, t: PNode, d: var TLoc,
  728. rangeFormat, eqFormat: FormatStr) =
  729. var a: TLoc
  730. initLocExpr(p, t[0], a)
  731. var lend = genIfForCaseUntil(p, t, d, rangeFormat, eqFormat, t.len-1, a)
  732. fixLabel(p, lend)
  733. proc genCaseStringBranch(p: BProc, b: PNode, e: TLoc, labl: TLabel,
  734. branches: var openArray[Rope]) =
  735. var x: TLoc
  736. for i in 0..<b.len - 1:
  737. assert(b[i].kind != nkRange)
  738. initLocExpr(p, b[i], x)
  739. assert(b[i].kind in {nkStrLit..nkTripleStrLit})
  740. var j = int(hashString(p.config, b[i].strVal) and high(branches))
  741. appcg(p.module, branches[j], "if (#eqStrings($1, $2)) goto $3;$n",
  742. [rdLoc(e), rdLoc(x), labl])
  743. proc genStringCase(p: BProc, t: PNode, d: var TLoc) =
  744. # count how many constant strings there are in the case:
  745. var strings = 0
  746. for i in 1..<t.len:
  747. if t[i].kind == nkOfBranch: inc(strings, t[i].len - 1)
  748. if strings > stringCaseThreshold:
  749. var bitMask = math.nextPowerOfTwo(strings) - 1
  750. var branches: seq[Rope]
  751. newSeq(branches, bitMask + 1)
  752. var a: TLoc
  753. initLocExpr(p, t[0], a) # fist pass: generate ifs+goto:
  754. var labId = p.labels
  755. for i in 1..<t.len:
  756. inc(p.labels)
  757. if t[i].kind == nkOfBranch:
  758. genCaseStringBranch(p, t[i], a, "LA" & rope(p.labels) & "_",
  759. branches)
  760. else:
  761. # else statement: nothing to do yet
  762. # but we reserved a label, which we use later
  763. discard
  764. linefmt(p, cpsStmts, "switch (#hashString($1) & $2) {$n",
  765. [rdLoc(a), bitMask])
  766. for j in 0..high(branches):
  767. if branches[j] != nil:
  768. lineF(p, cpsStmts, "case $1: $n$2break;$n",
  769. [intLiteral(j), branches[j]])
  770. lineF(p, cpsStmts, "}$n", []) # else statement:
  771. if t[^1].kind != nkOfBranch:
  772. lineF(p, cpsStmts, "goto LA$1_;$n", [rope(p.labels)])
  773. # third pass: generate statements
  774. var lend = genCaseSecondPass(p, t, d, labId, t.len-1)
  775. fixLabel(p, lend)
  776. else:
  777. genCaseGeneric(p, t, d, "", "if (#eqStrings($1, $2)) goto $3;$n")
  778. proc branchHasTooBigRange(b: PNode): bool =
  779. for i in 0..<b.len-1:
  780. # last son is block
  781. if (b[i].kind == nkRange) and
  782. b[i][1].intVal - b[i][0].intVal > RangeExpandLimit:
  783. return true
  784. proc ifSwitchSplitPoint(p: BProc, n: PNode): int =
  785. for i in 1..<n.len:
  786. var branch = n[i]
  787. var stmtBlock = lastSon(branch)
  788. if stmtBlock.stmtsContainPragma(wLinearScanEnd):
  789. result = i
  790. elif hasSwitchRange notin CC[p.config.cCompiler].props:
  791. if branch.kind == nkOfBranch and branchHasTooBigRange(branch):
  792. result = i
  793. proc genCaseRange(p: BProc, branch: PNode) =
  794. for j in 0..<branch.len-1:
  795. if branch[j].kind == nkRange:
  796. if hasSwitchRange in CC[p.config.cCompiler].props:
  797. lineF(p, cpsStmts, "case $1 ... $2:$n", [
  798. genLiteral(p, branch[j][0]),
  799. genLiteral(p, branch[j][1])])
  800. else:
  801. var v = copyNode(branch[j][0])
  802. while v.intVal <= branch[j][1].intVal:
  803. lineF(p, cpsStmts, "case $1:$n", [genLiteral(p, v)])
  804. inc(v.intVal)
  805. else:
  806. lineF(p, cpsStmts, "case $1:$n", [genLiteral(p, branch[j])])
  807. proc genOrdinalCase(p: BProc, n: PNode, d: var TLoc) =
  808. # analyse 'case' statement:
  809. var splitPoint = ifSwitchSplitPoint(p, n)
  810. # generate if part (might be empty):
  811. var a: TLoc
  812. initLocExpr(p, n[0], a)
  813. var lend = if splitPoint > 0: genIfForCaseUntil(p, n, d,
  814. rangeFormat = "if ($1 >= $2 && $1 <= $3) goto $4;$n",
  815. eqFormat = "if ($1 == $2) goto $3;$n",
  816. splitPoint, a) else: nil
  817. # generate switch part (might be empty):
  818. if splitPoint+1 < n.len:
  819. lineF(p, cpsStmts, "switch ($1) {$n", [rdCharLoc(a)])
  820. var hasDefault = false
  821. for i in splitPoint+1..<n.len:
  822. # bug #4230: avoid false sharing between branches:
  823. if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
  824. var branch = n[i]
  825. if branch.kind == nkOfBranch:
  826. genCaseRange(p, branch)
  827. else:
  828. # else part of case statement:
  829. lineF(p, cpsStmts, "default:$n", [])
  830. hasDefault = true
  831. exprBlock(p, branch.lastSon, d)
  832. lineF(p, cpsStmts, "break;$n", [])
  833. if (hasAssume in CC[p.config.cCompiler].props) and not hasDefault:
  834. lineF(p, cpsStmts, "default: __assume(0);$n", [])
  835. lineF(p, cpsStmts, "}$n", [])
  836. if lend != nil: fixLabel(p, lend)
  837. proc genCase(p: BProc, t: PNode, d: var TLoc) =
  838. genLineDir(p, t)
  839. if not isEmptyType(t.typ) and d.k == locNone:
  840. getTemp(p, t.typ, d)
  841. case skipTypes(t[0].typ, abstractVarRange).kind
  842. of tyString:
  843. genStringCase(p, t, d)
  844. of tyFloat..tyFloat128:
  845. genCaseGeneric(p, t, d, "if ($1 >= $2 && $1 <= $3) goto $4;$n",
  846. "if ($1 == $2) goto $3;$n")
  847. else:
  848. if t[0].kind == nkSym and sfGoto in t[0].sym.flags:
  849. genGotoForCase(p, t)
  850. else:
  851. genOrdinalCase(p, t, d)
  852. proc genRestoreFrameAfterException(p: BProc) =
  853. if optStackTrace in p.module.config.options:
  854. if hasCurFramePointer notin p.flags:
  855. p.flags.incl hasCurFramePointer
  856. p.procSec(cpsLocals).add(ropecg(p.module, "\tTFrame* _nimCurFrame;$n", []))
  857. p.procSec(cpsInit).add(ropecg(p.module, "\t_nimCurFrame = #getFrame();$n", []))
  858. linefmt(p, cpsStmts, "#setFrame(_nimCurFrame);$n", [])
  859. proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
  860. #[ code to generate:
  861. std::exception_ptr error = nullptr;
  862. try {
  863. body;
  864. } catch (Exception e) {
  865. error = std::current_exception();
  866. if (ofExpr(e, TypeHere)) {
  867. error = nullptr; // handled
  868. } else if (...) {
  869. } else {
  870. throw;
  871. }
  872. } catch(...) {
  873. // C++ exception occured, not under Nim's control.
  874. }
  875. {
  876. /* finally: */
  877. printf('fin!\n');
  878. if (error) std::rethrow_exception(error); // re-raise the exception
  879. }
  880. ]#
  881. p.module.includeHeader("<exception>")
  882. if not isEmptyType(t.typ) and d.k == locNone:
  883. getTemp(p, t.typ, d)
  884. genLineDir(p, t)
  885. inc(p.labels, 2)
  886. let etmp = p.labels
  887. p.procSec(cpsInit).add(ropecg(p.module, "\tstd::exception_ptr T$1_ = nullptr;", [etmp]))
  888. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  889. p.nestedTryStmts.add((fin, false, 0.Natural))
  890. startBlock(p, "try {$n")
  891. expr(p, t[0], d)
  892. endBlock(p)
  893. # First pass: handle Nim based exceptions:
  894. lineCg(p, cpsStmts, "catch (#Exception* T$1_) {$n", [etmp+1])
  895. genRestoreFrameAfterException(p)
  896. # an unhandled exception happened!
  897. lineCg(p, cpsStmts, "T$1_ = std::current_exception();$n", [etmp])
  898. p.nestedTryStmts[^1].inExcept = true
  899. var hasImportedCppExceptions = false
  900. var i = 1
  901. var hasIf = false
  902. var hasElse = false
  903. while (i < t.len) and (t[i].kind == nkExceptBranch):
  904. # bug #4230: avoid false sharing between branches:
  905. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  906. if t[i].len == 1:
  907. hasImportedCppExceptions = true
  908. # general except section:
  909. hasElse = true
  910. if hasIf: lineF(p, cpsStmts, "else ", [])
  911. startBlock(p)
  912. # we handled the error:
  913. linefmt(p, cpsStmts, "T$1_ = nullptr;$n", [etmp])
  914. expr(p, t[i][0], d)
  915. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  916. endBlock(p)
  917. else:
  918. var orExpr = Rope(nil)
  919. var exvar = PNode(nil)
  920. for j in 0..<t[i].len - 1:
  921. var typeNode = t[i][j]
  922. if t[i][j].isInfixAs():
  923. typeNode = t[i][j][1]
  924. exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
  925. assert(typeNode.kind == nkType)
  926. if isImportedException(typeNode.typ, p.config):
  927. hasImportedCppExceptions = true
  928. else:
  929. if orExpr != nil: orExpr.add("||")
  930. let checkFor = if optTinyRtti in p.config.globalOptions:
  931. genTypeInfo2Name(p.module, typeNode.typ)
  932. else:
  933. genTypeInfo(p.module, typeNode.typ, typeNode.info)
  934. let memberName = if p.module.compileToCpp: "m_type" else: "Sup.m_type"
  935. appcg(p.module, orExpr, "#isObj(#nimBorrowCurrentException()->$1, $2)", [memberName, checkFor])
  936. if orExpr != nil:
  937. if hasIf:
  938. startBlock(p, "else if ($1) {$n", [orExpr])
  939. else:
  940. startBlock(p, "if ($1) {$n", [orExpr])
  941. hasIf = true
  942. if exvar != nil:
  943. fillLoc(exvar.sym.loc, locTemp, exvar, mangleLocalName(p, exvar.sym), OnStack)
  944. linefmt(p, cpsStmts, "$1 $2 = T$3_;$n", [getTypeDesc(p.module, exvar.sym.typ),
  945. rdLoc(exvar.sym.loc), rope(etmp+1)])
  946. # we handled the error:
  947. linefmt(p, cpsStmts, "T$1_ = nullptr;$n", [etmp])
  948. expr(p, t[i][^1], d)
  949. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  950. endBlock(p)
  951. inc(i)
  952. if hasIf and not hasElse:
  953. linefmt(p, cpsStmts, "else throw;$n", [etmp])
  954. linefmt(p, cpsStmts, "}$n", [])
  955. # Second pass: handle C++ based exceptions:
  956. template genExceptBranchBody(body: PNode) {.dirty.} =
  957. genRestoreFrameAfterException(p)
  958. #linefmt(p, cpsStmts, "T$1_ = std::current_exception();$n", [etmp])
  959. expr(p, body, d)
  960. var catchAllPresent = false
  961. incl p.flags, noSafePoints # mark as not needing 'popCurrentException'
  962. if hasImportedCppExceptions:
  963. for i in 1..<t.len:
  964. if t[i].kind != nkExceptBranch: break
  965. # bug #4230: avoid false sharing between branches:
  966. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  967. if t[i].len == 1:
  968. # general except section:
  969. startBlock(p, "catch (...) {", [])
  970. genExceptBranchBody(t[i][0])
  971. endBlock(p)
  972. catchAllPresent = true
  973. else:
  974. for j in 0..<t[i].len-1:
  975. var typeNode = t[i][j]
  976. if t[i][j].isInfixAs():
  977. typeNode = t[i][j][1]
  978. if isImportedException(typeNode.typ, p.config):
  979. let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
  980. fillLoc(exvar.sym.loc, locTemp, exvar, mangleLocalName(p, exvar.sym), OnStack)
  981. startBlock(p, "catch ($1& $2) {$n", getTypeDesc(p.module, typeNode.typ), rdLoc(exvar.sym.loc))
  982. genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
  983. endBlock(p)
  984. elif isImportedException(typeNode.typ, p.config):
  985. startBlock(p, "catch ($1&) {$n", getTypeDesc(p.module, t[i][j].typ))
  986. genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
  987. endBlock(p)
  988. excl p.flags, noSafePoints
  989. discard pop(p.nestedTryStmts)
  990. # general finally block:
  991. if t.len > 0 and t[^1].kind == nkFinally:
  992. if not catchAllPresent:
  993. startBlock(p, "catch (...) {", [])
  994. genRestoreFrameAfterException(p)
  995. linefmt(p, cpsStmts, "T$1_ = std::current_exception();$n", [etmp])
  996. endBlock(p)
  997. startBlock(p)
  998. genStmts(p, t[^1][0])
  999. linefmt(p, cpsStmts, "if (T$1_) std::rethrow_exception(T$1_);$n", [etmp])
  1000. endBlock(p)
  1001. proc genTryCppOld(p: BProc, t: PNode, d: var TLoc) =
  1002. # There are two versions we generate, depending on whether we
  1003. # catch C++ exceptions, imported via .importcpp or not. The
  1004. # code can be easier if there are no imported C++ exceptions
  1005. # to deal with.
  1006. # code to generate:
  1007. #
  1008. # try
  1009. # {
  1010. # myDiv(4, 9);
  1011. # } catch (NimExceptionType1&) {
  1012. # body
  1013. # } catch (NimExceptionType2&) {
  1014. # finallyPart()
  1015. # raise;
  1016. # }
  1017. # catch(...) {
  1018. # general_handler_body
  1019. # }
  1020. # finallyPart();
  1021. template genExceptBranchBody(body: PNode) {.dirty.} =
  1022. genRestoreFrameAfterException(p)
  1023. expr(p, body, d)
  1024. if not isEmptyType(t.typ) and d.k == locNone:
  1025. getTemp(p, t.typ, d)
  1026. genLineDir(p, t)
  1027. discard cgsym(p.module, "popCurrentExceptionEx")
  1028. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1029. p.nestedTryStmts.add((fin, false, 0.Natural))
  1030. startBlock(p, "try {$n")
  1031. expr(p, t[0], d)
  1032. endBlock(p)
  1033. var catchAllPresent = false
  1034. p.nestedTryStmts[^1].inExcept = true
  1035. for i in 1..<t.len:
  1036. if t[i].kind != nkExceptBranch: break
  1037. # bug #4230: avoid false sharing between branches:
  1038. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1039. if t[i].len == 1:
  1040. # general except section:
  1041. catchAllPresent = true
  1042. startBlock(p, "catch (...) {$n")
  1043. genExceptBranchBody(t[i][0])
  1044. endBlock(p)
  1045. else:
  1046. for j in 0..<t[i].len-1:
  1047. if t[i][j].isInfixAs():
  1048. let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
  1049. fillLoc(exvar.sym.loc, locTemp, exvar, mangleLocalName(p, exvar.sym), OnUnknown)
  1050. startBlock(p, "catch ($1& $2) {$n", getTypeDesc(p.module, t[i][j][1].typ), rdLoc(exvar.sym.loc))
  1051. else:
  1052. startBlock(p, "catch ($1&) {$n", getTypeDesc(p.module, t[i][j].typ))
  1053. genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
  1054. endBlock(p)
  1055. discard pop(p.nestedTryStmts)
  1056. if t[^1].kind == nkFinally:
  1057. # c++ does not have finally, therefore code needs to be generated twice
  1058. if not catchAllPresent:
  1059. # finally requires catch all presence
  1060. startBlock(p, "catch (...) {$n")
  1061. genStmts(p, t[^1][0])
  1062. line(p, cpsStmts, ~"throw;$n")
  1063. endBlock(p)
  1064. genSimpleBlock(p, t[^1][0])
  1065. proc bodyCanRaise(p: BProc; n: PNode): bool =
  1066. case n.kind
  1067. of nkCallKinds:
  1068. result = canRaiseDisp(p, n[0])
  1069. if not result:
  1070. # also check the arguments:
  1071. for i in 1 ..< n.len:
  1072. if bodyCanRaise(p, n[i]): return true
  1073. of nkRaiseStmt:
  1074. result = true
  1075. of nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef, nkIteratorDef,
  1076. nkMacroDef, nkTemplateDef, nkLambda, nkDo, nkFuncDef:
  1077. result = false
  1078. else:
  1079. for i in 0 ..< safeLen(n):
  1080. if bodyCanRaise(p, n[i]): return true
  1081. result = false
  1082. proc genTryGoto(p: BProc; t: PNode; d: var TLoc) =
  1083. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1084. inc p.labels
  1085. let lab = p.labels
  1086. let hasExcept = t[1].kind == nkExceptBranch
  1087. if hasExcept: inc p.withinTryWithExcept
  1088. p.nestedTryStmts.add((fin, false, Natural lab))
  1089. p.flags.incl nimErrorFlagAccessed
  1090. expr(p, t[0], d)
  1091. if 1 < t.len and t[1].kind == nkExceptBranch:
  1092. startBlock(p, "if (NIM_UNLIKELY(*nimErr_)) {$n")
  1093. else:
  1094. startBlock(p)
  1095. linefmt(p, cpsStmts, "LA$1_:;$n", [lab])
  1096. p.nestedTryStmts[^1].inExcept = true
  1097. var i = 1
  1098. while (i < t.len) and (t[i].kind == nkExceptBranch):
  1099. inc p.labels
  1100. let nextExcept = p.labels
  1101. p.nestedTryStmts[^1].label = nextExcept
  1102. # bug #4230: avoid false sharing between branches:
  1103. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1104. if t[i].len == 1:
  1105. # general except section:
  1106. if i > 1: lineF(p, cpsStmts, "else", [])
  1107. startBlock(p)
  1108. # we handled the exception, remember this:
  1109. linefmt(p, cpsStmts, "*nimErr_ = NIM_FALSE;$n", [])
  1110. expr(p, t[i][0], d)
  1111. else:
  1112. var orExpr: Rope = nil
  1113. for j in 0..<t[i].len - 1:
  1114. assert(t[i][j].kind == nkType)
  1115. if orExpr != nil: orExpr.add("||")
  1116. let checkFor = if optTinyRtti in p.config.globalOptions:
  1117. genTypeInfo2Name(p.module, t[i][j].typ)
  1118. else:
  1119. genTypeInfo(p.module, t[i][j].typ, t[i][j].info)
  1120. let memberName = if p.module.compileToCpp: "m_type" else: "Sup.m_type"
  1121. appcg(p.module, orExpr, "#isObj(#nimBorrowCurrentException()->$1, $2)", [memberName, checkFor])
  1122. if i > 1: line(p, cpsStmts, "else ")
  1123. startBlock(p, "if ($1) {$n", [orExpr])
  1124. # we handled the exception, remember this:
  1125. linefmt(p, cpsStmts, "*nimErr_ = NIM_FALSE;$n", [])
  1126. expr(p, t[i][^1], d)
  1127. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  1128. linefmt(p, cpsStmts, "LA$1_:;$n", [nextExcept])
  1129. endBlock(p)
  1130. inc(i)
  1131. discard pop(p.nestedTryStmts)
  1132. endBlock(p)
  1133. if i < t.len and t[i].kind == nkFinally:
  1134. startBlock(p)
  1135. if not bodyCanRaise(p, t[i][0]):
  1136. # this is an important optimization; most destroy blocks are detected not to raise an
  1137. # exception and so we help the C optimizer by not mutating nimErr_ pointlessly:
  1138. genStmts(p, t[i][0])
  1139. else:
  1140. # pretend we did handle the error for the safe execution of the 'finally' section:
  1141. p.procSec(cpsLocals).add(ropecg(p.module, "NIM_BOOL oldNimErrFin$1_;$n", [lab]))
  1142. linefmt(p, cpsStmts, "oldNimErrFin$1_ = *nimErr_; *nimErr_ = NIM_FALSE;$n", [lab])
  1143. genStmts(p, t[i][0])
  1144. # this is correct for all these cases:
  1145. # 1. finally is run during ordinary control flow
  1146. # 2. finally is run after 'except' block handling: these however set the
  1147. # error back to nil.
  1148. # 3. finally is run for exception handling code without any 'except'
  1149. # handler present or only handlers that did not match.
  1150. linefmt(p, cpsStmts, "*nimErr_ = oldNimErrFin$1_;$n", [lab])
  1151. endBlock(p)
  1152. if p.prc != nil: raiseExit(p)
  1153. if hasExcept: inc p.withinTryWithExcept
  1154. proc genTrySetjmp(p: BProc, t: PNode, d: var TLoc) =
  1155. # code to generate:
  1156. #
  1157. # XXX: There should be a standard dispatch algorithm
  1158. # that's used both here and with multi-methods
  1159. #
  1160. # TSafePoint sp;
  1161. # pushSafePoint(&sp);
  1162. # sp.status = setjmp(sp.context);
  1163. # if (sp.status == 0) {
  1164. # myDiv(4, 9);
  1165. # popSafePoint();
  1166. # } else {
  1167. # popSafePoint();
  1168. # /* except DivisionByZero: */
  1169. # if (sp.status == DivisionByZero) {
  1170. # printf('Division by Zero\n');
  1171. # clearException();
  1172. # } else {
  1173. # clearException();
  1174. # }
  1175. # }
  1176. # {
  1177. # /* finally: */
  1178. # printf('fin!\n');
  1179. # }
  1180. # if (exception not cleared)
  1181. # propagateCurrentException();
  1182. #
  1183. if not isEmptyType(t.typ) and d.k == locNone:
  1184. getTemp(p, t.typ, d)
  1185. let quirkyExceptions = p.config.exc == excQuirky or
  1186. (t.kind == nkHiddenTryStmt and sfSystemModule in p.module.module.flags)
  1187. if not quirkyExceptions:
  1188. p.module.includeHeader("<setjmp.h>")
  1189. else:
  1190. p.flags.incl noSafePoints
  1191. genLineDir(p, t)
  1192. discard cgsym(p.module, "Exception")
  1193. var safePoint: Rope
  1194. if not quirkyExceptions:
  1195. safePoint = getTempName(p.module)
  1196. linefmt(p, cpsLocals, "#TSafePoint $1;$n", [safePoint])
  1197. linefmt(p, cpsStmts, "#pushSafePoint(&$1);$n", [safePoint])
  1198. if isDefined(p.config, "nimStdSetjmp"):
  1199. linefmt(p, cpsStmts, "$1.status = setjmp($1.context);$n", [safePoint])
  1200. elif isDefined(p.config, "nimSigSetjmp"):
  1201. linefmt(p, cpsStmts, "$1.status = sigsetjmp($1.context, 0);$n", [safePoint])
  1202. elif isDefined(p.config, "nimBuiltinSetjmp"):
  1203. linefmt(p, cpsStmts, "$1.status = __builtin_setjmp($1.context);$n", [safePoint])
  1204. elif isDefined(p.config, "nimRawSetjmp"):
  1205. if isDefined(p.config, "mswindows"):
  1206. # The Windows `_setjmp()` takes two arguments, with the second being an
  1207. # undocumented buffer used by the SEH mechanism for stack unwinding.
  1208. # Mingw-w64 has been trying to get it right for years, but it's still
  1209. # prone to stack corruption during unwinding, so we disable that by setting
  1210. # it to NULL.
  1211. # More details: https://github.com/status-im/nimbus-eth2/issues/3121
  1212. linefmt(p, cpsStmts, "$1.status = _setjmp($1.context, 0);$n", [safePoint])
  1213. else:
  1214. linefmt(p, cpsStmts, "$1.status = _setjmp($1.context);$n", [safePoint])
  1215. else:
  1216. linefmt(p, cpsStmts, "$1.status = setjmp($1.context);$n", [safePoint])
  1217. startBlock(p, "if ($1.status == 0) {$n", [safePoint])
  1218. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1219. p.nestedTryStmts.add((fin, quirkyExceptions, 0.Natural))
  1220. expr(p, t[0], d)
  1221. if not quirkyExceptions:
  1222. linefmt(p, cpsStmts, "#popSafePoint();$n", [])
  1223. endBlock(p)
  1224. startBlock(p, "else {$n")
  1225. linefmt(p, cpsStmts, "#popSafePoint();$n", [])
  1226. genRestoreFrameAfterException(p)
  1227. elif 1 < t.len and t[1].kind == nkExceptBranch:
  1228. startBlock(p, "if (#nimBorrowCurrentException()) {$n")
  1229. else:
  1230. startBlock(p)
  1231. p.nestedTryStmts[^1].inExcept = true
  1232. var i = 1
  1233. while (i < t.len) and (t[i].kind == nkExceptBranch):
  1234. # bug #4230: avoid false sharing between branches:
  1235. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1236. if t[i].len == 1:
  1237. # general except section:
  1238. if i > 1: lineF(p, cpsStmts, "else", [])
  1239. startBlock(p)
  1240. if not quirkyExceptions:
  1241. linefmt(p, cpsStmts, "$1.status = 0;$n", [safePoint])
  1242. expr(p, t[i][0], d)
  1243. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  1244. endBlock(p)
  1245. else:
  1246. var orExpr: Rope = nil
  1247. for j in 0..<t[i].len - 1:
  1248. assert(t[i][j].kind == nkType)
  1249. if orExpr != nil: orExpr.add("||")
  1250. let checkFor = if optTinyRtti in p.config.globalOptions:
  1251. genTypeInfo2Name(p.module, t[i][j].typ)
  1252. else:
  1253. genTypeInfo(p.module, t[i][j].typ, t[i][j].info)
  1254. let memberName = if p.module.compileToCpp: "m_type" else: "Sup.m_type"
  1255. appcg(p.module, orExpr, "#isObj(#nimBorrowCurrentException()->$1, $2)", [memberName, checkFor])
  1256. if i > 1: line(p, cpsStmts, "else ")
  1257. startBlock(p, "if ($1) {$n", [orExpr])
  1258. if not quirkyExceptions:
  1259. linefmt(p, cpsStmts, "$1.status = 0;$n", [safePoint])
  1260. expr(p, t[i][^1], d)
  1261. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  1262. endBlock(p)
  1263. inc(i)
  1264. discard pop(p.nestedTryStmts)
  1265. endBlock(p) # end of else block
  1266. if i < t.len and t[i].kind == nkFinally:
  1267. p.finallySafePoints.add(safePoint)
  1268. startBlock(p)
  1269. genStmts(p, t[i][0])
  1270. # pretend we handled the exception in a 'finally' so that we don't
  1271. # re-raise the unhandled one but instead keep the old one (it was
  1272. # not popped either):
  1273. if not quirkyExceptions and getCompilerProc(p.module.g.graph, "nimLeaveFinally") != nil:
  1274. linefmt(p, cpsStmts, "if ($1.status != 0) #nimLeaveFinally();$n", [safePoint])
  1275. endBlock(p)
  1276. discard pop(p.finallySafePoints)
  1277. if not quirkyExceptions:
  1278. linefmt(p, cpsStmts, "if ($1.status != 0) #reraiseException();$n", [safePoint])
  1279. proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope =
  1280. var res = ""
  1281. for it in t.sons:
  1282. case it.kind
  1283. of nkStrLit..nkTripleStrLit:
  1284. res.add(it.strVal)
  1285. of nkSym:
  1286. var sym = it.sym
  1287. if sym.kind in {skProc, skFunc, skIterator, skMethod}:
  1288. var a: TLoc
  1289. initLocExpr(p, it, a)
  1290. res.add($rdLoc(a))
  1291. elif sym.kind == skType:
  1292. res.add($getTypeDesc(p.module, sym.typ))
  1293. else:
  1294. discard getTypeDesc(p.module, skipTypes(sym.typ, abstractPtrs))
  1295. var r = sym.loc.r
  1296. if r == nil:
  1297. # if no name has already been given,
  1298. # it doesn't matter much:
  1299. r = mangleName(p.module, sym)
  1300. sym.loc.r = r # but be consequent!
  1301. res.add($r)
  1302. of nkTypeOfExpr:
  1303. res.add($getTypeDesc(p.module, it.typ))
  1304. else:
  1305. discard getTypeDesc(p.module, skipTypes(it.typ, abstractPtrs))
  1306. var a: TLoc
  1307. initLocExpr(p, it, a)
  1308. res.add($a.rdLoc)
  1309. if isAsmStmt and hasGnuAsm in CC[p.config.cCompiler].props:
  1310. for x in splitLines(res):
  1311. var j = 0
  1312. while j < x.len and x[j] in {' ', '\t'}: inc(j)
  1313. if j < x.len:
  1314. if x[j] in {'"', ':'}:
  1315. # don't modify the line if already in quotes or
  1316. # some clobber register list:
  1317. result.add(x); result.add("\L")
  1318. else:
  1319. # ignore empty lines
  1320. result.add("\"")
  1321. result.add(x)
  1322. result.add("\\n\"\n")
  1323. else:
  1324. res.add("\L")
  1325. result = res.rope
  1326. proc genAsmStmt(p: BProc, t: PNode) =
  1327. assert(t.kind == nkAsmStmt)
  1328. genLineDir(p, t)
  1329. var s = genAsmOrEmitStmt(p, t, isAsmStmt=true)
  1330. # see bug #2362, "top level asm statements" seem to be a mis-feature
  1331. # but even if we don't do this, the example in #2362 cannot possibly
  1332. # work:
  1333. if p.prc == nil:
  1334. # top level asm statement?
  1335. p.module.s[cfsProcHeaders].add runtimeFormat(CC[p.config.cCompiler].asmStmtFrmt, [s])
  1336. else:
  1337. p.s(cpsStmts).add indentLine(p, runtimeFormat(CC[p.config.cCompiler].asmStmtFrmt, [s]))
  1338. proc determineSection(n: PNode): TCFileSection =
  1339. result = cfsProcHeaders
  1340. if n.len >= 1 and n[0].kind in {nkStrLit..nkTripleStrLit}:
  1341. let sec = n[0].strVal
  1342. if sec.startsWith("/*TYPESECTION*/"): result = cfsTypes
  1343. elif sec.startsWith("/*VARSECTION*/"): result = cfsVars
  1344. elif sec.startsWith("/*INCLUDESECTION*/"): result = cfsHeaders
  1345. proc genEmit(p: BProc, t: PNode) =
  1346. var s = genAsmOrEmitStmt(p, t[1])
  1347. if p.prc == nil:
  1348. # top level emit pragma?
  1349. let section = determineSection(t[1])
  1350. genCLineDir(p.module.s[section], t.info, p.config)
  1351. p.module.s[section].add(s)
  1352. else:
  1353. genLineDir(p, t)
  1354. line(p, cpsStmts, s)
  1355. proc genPragma(p: BProc, n: PNode) =
  1356. for it in n.sons:
  1357. case whichPragma(it)
  1358. of wEmit: genEmit(p, it)
  1359. of wInjectStmt:
  1360. var p = newProc(nil, p.module)
  1361. p.options = p.options - {optLineTrace, optStackTrace}
  1362. genStmts(p, it[1])
  1363. p.module.injectStmt = p.s(cpsStmts)
  1364. else: discard
  1365. proc genDiscriminantCheck(p: BProc, a, tmp: TLoc, objtype: PType,
  1366. field: PSym) =
  1367. var t = skipTypes(objtype, abstractVar)
  1368. assert t.kind == tyObject
  1369. discard genTypeInfo(p.module, t, a.lode.info)
  1370. if not containsOrIncl(p.module.declaredThings, field.id):
  1371. appcg(p.module, cfsVars, "extern $1",
  1372. [discriminatorTableDecl(p.module, t, field)])
  1373. lineCg(p, cpsStmts,
  1374. "#FieldDiscriminantCheck((NI)(NU)($1), (NI)(NU)($2), $3, $4);$n",
  1375. [rdLoc(a), rdLoc(tmp), discriminatorTableName(p.module, t, field),
  1376. intLiteral(toInt64(lengthOrd(p.config, field.typ))+1)])
  1377. proc genCaseObjDiscMapping(p: BProc, e: PNode, t: PType, field: PSym; d: var TLoc) =
  1378. const ObjDiscMappingProcSlot = -5
  1379. var theProc: PSym = nil
  1380. for idx, p in items(t.methods):
  1381. if idx == ObjDiscMappingProcSlot:
  1382. theProc = p
  1383. break
  1384. if theProc == nil:
  1385. theProc = genCaseObjDiscMapping(t, field, e.info, p.module.g.graph)
  1386. t.methods.add((ObjDiscMappingProcSlot, theProc))
  1387. var call = newNodeIT(nkCall, e.info, getSysType(p.module.g.graph, e.info, tyUInt8))
  1388. call.add newSymNode(theProc)
  1389. call.add e
  1390. expr(p, call, d)
  1391. proc asgnFieldDiscriminant(p: BProc, e: PNode) =
  1392. var a, tmp: TLoc
  1393. var dotExpr = e[0]
  1394. if dotExpr.kind == nkCheckedFieldExpr: dotExpr = dotExpr[0]
  1395. initLocExpr(p, e[0], a)
  1396. getTemp(p, a.t, tmp)
  1397. expr(p, e[1], tmp)
  1398. if optTinyRtti notin p.config.globalOptions:
  1399. let field = dotExpr[1].sym
  1400. genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field)
  1401. message(p.config, e.info, warnCaseTransition)
  1402. genAssignment(p, a, tmp, {})
  1403. proc genAsgn(p: BProc, e: PNode, fastAsgn: bool) =
  1404. if e[0].kind == nkSym and sfGoto in e[0].sym.flags:
  1405. genLineDir(p, e)
  1406. genGotoVar(p, e[1])
  1407. elif optFieldCheck in p.options and isDiscriminantField(e[0]):
  1408. genLineDir(p, e)
  1409. asgnFieldDiscriminant(p, e)
  1410. else:
  1411. let le = e[0]
  1412. let ri = e[1]
  1413. var a: TLoc
  1414. discard getTypeDesc(p.module, le.typ.skipTypes(skipPtrs))
  1415. initLoc(a, locNone, le, OnUnknown)
  1416. a.flags.incl(lfEnforceDeref)
  1417. a.flags.incl(lfPrepareForMutation)
  1418. expr(p, le, a)
  1419. a.flags.excl(lfPrepareForMutation)
  1420. if fastAsgn: incl(a.flags, lfNoDeepCopy)
  1421. assert(a.t != nil)
  1422. genLineDir(p, ri)
  1423. loadInto(p, le, ri, a)
  1424. proc genStmts(p: BProc, t: PNode) =
  1425. var a: TLoc
  1426. let isPush = p.config.hasHint(hintExtendedContext)
  1427. if isPush: pushInfoContext(p.config, t.info)
  1428. expr(p, t, a)
  1429. if isPush: popInfoContext(p.config)
  1430. internalAssert p.config, a.k in {locNone, locTemp, locLocalVar, locExpr}