ccgstmts.nim 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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 registerTraverseProc(p: BProc, v: PSym) =
  16. var traverseProc = ""
  17. if p.config.selectedGC in {gcMarkAndSweep, gcHooks, gcRefc} and
  18. optOwnedRefs notin p.config.globalOptions and
  19. containsGarbageCollectedRef(v.loc.t):
  20. # we register a specialized marked proc here; this has the advantage
  21. # that it works out of the box for thread local storage then :-)
  22. traverseProc = genTraverseProcForGlobal(p.module, v, v.info)
  23. if traverseProc.len != 0 and not p.hcrOn:
  24. p.module.preInitProc.procSec(cpsInit).add("\n\t")
  25. let fnName = cgsymValue(p.module,
  26. if sfThread in v.flags: "nimRegisterThreadLocalMarker"
  27. else: "nimRegisterGlobalMarker")
  28. p.module.preInitProc.procSec(cpsInit).addCallStmt(fnName, traverseProc)
  29. p.module.preInitProc.procSec(cpsInit).add("\n")
  30. proc isAssignedImmediately(conf: ConfigRef; n: PNode): bool {.inline.} =
  31. if n.kind == nkEmpty:
  32. result = false
  33. elif n.kind in nkCallKinds and n[0] != nil and n[0].typ != nil and n[0].typ.skipTypes(abstractInst).kind == tyProc:
  34. if n[0].kind == nkSym and sfConstructor in n[0].sym.flags:
  35. result = true
  36. elif isInvalidReturnType(conf, n[0].typ, true):
  37. # var v = f()
  38. # is transformed into: var v; f(addr v)
  39. # where 'f' **does not** initialize the result!
  40. result = false
  41. else:
  42. result = true
  43. elif isInvalidReturnType(conf, n.typ, false):
  44. result = false
  45. else:
  46. result = true
  47. proc inExceptBlockLen(p: BProc): int =
  48. result = 0
  49. for x in p.nestedTryStmts:
  50. if x.inExcept: result.inc
  51. proc startBlockInside(p: BProc): int {.discardable.} =
  52. inc(p.labels)
  53. result = p.blocks.len
  54. p.blocks.add initBlock()
  55. p.blocks[result].id = p.labels
  56. p.blocks[result].nestedTryStmts = p.nestedTryStmts.len.int16
  57. p.blocks[result].nestedExceptStmts = p.inExceptBlockLen.int16
  58. template startBlockWith(p: BProc, body: typed): int =
  59. body
  60. startBlockInside(p)
  61. proc blockBody(b: var TBlock; result: var Builder) =
  62. result.add extract(b.sections[cpsLocals])
  63. if b.frameLen > 0:
  64. result.addInPlaceOp(Add, NimInt, dotField("FR_", "len"), cIntValue(b.frameLen.int))
  65. result.add(extract(b.sections[cpsInit]))
  66. result.add(extract(b.sections[cpsStmts]))
  67. if b.frameLen > 0:
  68. result.addInPlaceOp(Sub, NimInt, dotField("FR_", "len"), cIntValue(b.frameLen.int))
  69. proc endBlockInside(p: BProc) =
  70. let topBlock = p.blocks.len-1
  71. # the block is merged into the parent block
  72. p.blocks[topBlock].blockBody(p.blocks[topBlock-1].sections[cpsStmts])
  73. setLen(p.blocks, topBlock)
  74. proc endBlockOutside(p: BProc, label: TLabel) =
  75. if label.len != 0:
  76. let topBlock = p.blocks.len - 1
  77. p.blocks[topBlock].sections[cpsStmts].addLabel(label)
  78. template endBlockWith(p: BProc, body: typed) =
  79. let label = p.blocks[p.blocks.len - 1].label
  80. endBlockInside(p)
  81. body
  82. endBlockOutside(p, label)
  83. proc genVarTuple(p: BProc, n: PNode) =
  84. if n.kind != nkVarTuple: internalError(p.config, n.info, "genVarTuple")
  85. # if we have a something that's been captured, use the lowering instead:
  86. for i in 0..<n.len-2:
  87. if n[i].kind != nkSym:
  88. genStmts(p, lowerTupleUnpacking(p.module.g.graph, n, p.module.idgen, p.prc))
  89. return
  90. # check only the first son
  91. var forHcr = treatGlobalDifferentlyForHCR(p.module, n[0].sym)
  92. let hcrCond = if forHcr: getTempName(p.module) else: ""
  93. var hcrGlobals: seq[tuple[loc: TLoc, tp: Rope]] = @[]
  94. # determine if the tuple is constructed at top-level scope or inside of a block (if/while/block)
  95. let isGlobalInBlock = forHcr and p.blocks.len > 2
  96. # do not close and reopen blocks if this is a 'global' but inside of a block (if/while/block)
  97. forHcr = forHcr and not isGlobalInBlock
  98. var hcrIf = default(IfBuilder)
  99. if forHcr:
  100. startBlockWith(p):
  101. # check with the boolean if the initializing code for the tuple should be ran
  102. hcrIf = initIfStmt(p.s(cpsStmts))
  103. initElifBranch(p.s(cpsStmts), hcrIf, hcrCond)
  104. genLineDir(p, n)
  105. var tup = initLocExpr(p, n[^1])
  106. var t = tup.t.skipTypes(abstractInst)
  107. for i in 0..<n.len-2:
  108. let vn = n[i]
  109. let v = vn.sym
  110. if sfCompileTime in v.flags: continue
  111. if sfGlobal in v.flags:
  112. assignGlobalVar(p, vn, "")
  113. genObjectInit(p, cpsInit, v.typ, v.loc, constructObj)
  114. registerTraverseProc(p, v)
  115. else:
  116. assignLocalVar(p, vn)
  117. initLocalVar(p, v, immediateAsgn=isAssignedImmediately(p.config, n[^1]))
  118. var field = initLoc(locExpr, vn, tup.storage)
  119. let rtup = rdLoc(tup)
  120. let fieldName =
  121. if t.kind == tyTuple:
  122. "Field" & $i
  123. else:
  124. if t.n[i].kind != nkSym: internalError(p.config, n.info, "genVarTuple")
  125. mangleRecFieldName(p.module, t.n[i].sym)
  126. field.snippet = dotField(rtup, fieldName)
  127. putLocIntoDest(p, v.loc, field)
  128. if forHcr or isGlobalInBlock:
  129. hcrGlobals.add((loc: v.loc, tp: CNil))
  130. if forHcr:
  131. # end the block where the tuple gets initialized
  132. endBlockWith(p):
  133. finishBranch(p.s(cpsStmts), hcrIf)
  134. finishIfStmt(p.s(cpsStmts), hcrIf)
  135. if forHcr or isGlobalInBlock:
  136. # insert the registration of the globals for the different parts of the tuple at the
  137. # start of the current scope (after they have been iterated) and init a boolean to
  138. # check if any of them is newly introduced and the initializing code has to be ran
  139. p.s(cpsLocals).addVar(kind = Local,
  140. name = hcrCond,
  141. typ = NimBool,
  142. initializer = NimFalse)
  143. for curr in hcrGlobals:
  144. let rc = rdLoc(curr.loc)
  145. p.s(cpsLocals).addInPlaceOp(BitOr, NimBool,
  146. hcrCond,
  147. cCall("hcrRegisterGlobal",
  148. getModuleDllPath(p.module, n[0].sym),
  149. '"' & curr.loc.snippet & '"',
  150. cSizeof(rc),
  151. curr.tp,
  152. cCast(ptrType(CPointer), cAddr(curr.loc.snippet))))
  153. proc loadInto(p: BProc, le, ri: PNode, a: var TLoc) {.inline.} =
  154. if ri.kind in nkCallKinds and (ri[0].kind != nkSym or
  155. ri[0].sym.magic == mNone):
  156. genAsgnCall(p, le, ri, a)
  157. else:
  158. # this is a hacky way to fix #1181 (tmissingderef)::
  159. #
  160. # var arr1 = cast[ptr array[4, int8]](addr foo)[]
  161. #
  162. # However, fixing this properly really requires modelling 'array' as
  163. # a 'struct' in C to preserve dereferencing semantics completely. Not
  164. # worth the effort until version 1.0 is out.
  165. a.flags.incl(lfEnforceDeref)
  166. expr(p, ri, a)
  167. proc assignLabel(b: var TBlock; result: var TLabel) {.inline.} =
  168. b.label = "LA" & b.id.rope
  169. result = b.label
  170. proc startSimpleBlock(p: BProc, scope: out ScopeBuilder): int {.discardable, inline.} =
  171. startBlockWith(p):
  172. scope = initScope(p.s(cpsStmts))
  173. proc endSimpleBlock(p: BProc, scope: var ScopeBuilder) {.inline.} =
  174. endBlockWith(p):
  175. finishScope(p.s(cpsStmts), scope)
  176. proc genSimpleBlock(p: BProc, stmts: PNode) {.inline.} =
  177. var scope: ScopeBuilder
  178. startSimpleBlock(p, scope)
  179. genStmts(p, stmts)
  180. endSimpleBlock(p, scope)
  181. proc exprBlock(p: BProc, n: PNode, d: var TLoc) =
  182. var scope: ScopeBuilder
  183. startSimpleBlock(p, scope)
  184. expr(p, n, d)
  185. endSimpleBlock(p, scope)
  186. template preserveBreakIdx(body: untyped): untyped =
  187. var oldBreakIdx = p.breakIdx
  188. body
  189. p.breakIdx = oldBreakIdx
  190. proc genState(p: BProc, n: PNode) =
  191. internalAssert p.config, n.len == 1
  192. let n0 = n[0]
  193. if n0.kind == nkIntLit:
  194. let idx = n[0].intVal
  195. p.s(cpsStmts).addLabel("STATE" & $idx)
  196. elif n0.kind == nkStrLit:
  197. p.s(cpsStmts).addLabel(n0.strVal)
  198. proc blockLeaveActions(p: BProc, howManyTrys, howManyExcepts: int) =
  199. # Called by return and break stmts.
  200. # Deals with issues faced when jumping out of try/except/finally stmts.
  201. var stack = newSeq[tuple[fin: PNode, inExcept: bool, label: Natural]](0)
  202. inc p.withinBlockLeaveActions
  203. for i in 1..howManyTrys:
  204. let tryStmt = p.nestedTryStmts.pop
  205. if p.config.exc == excSetjmp:
  206. # Pop safe points generated by try
  207. if not tryStmt.inExcept:
  208. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popSafePoint"))
  209. # Pop this try-stmt of the list of nested trys
  210. # so we don't infinite recurse on it in the next step.
  211. stack.add(tryStmt)
  212. # Find finally-stmt for this try-stmt
  213. # and generate a copy of its sons
  214. var finallyStmt = tryStmt.fin
  215. if finallyStmt != nil:
  216. genStmts(p, finallyStmt[0])
  217. dec p.withinBlockLeaveActions
  218. # push old elements again:
  219. for i in countdown(howManyTrys-1, 0):
  220. p.nestedTryStmts.add(stack[i])
  221. # Pop exceptions that was handled by the
  222. # except-blocks we are in
  223. if noSafePoints notin p.flags:
  224. for i in countdown(howManyExcepts-1, 0):
  225. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popCurrentException"))
  226. proc genGotoState(p: BProc, n: PNode) =
  227. # we resist the temptation to translate it into duff's device as it later
  228. # will be translated into computed gotos anyway for GCC at least:
  229. # switch (x.state) {
  230. # case 0: goto STATE0;
  231. # ...
  232. var a: TLoc = initLocExpr(p, n[0])
  233. let ra = rdLoc(a)
  234. p.s(cpsStmts).addSwitchStmt(ra):
  235. p.flags.incl beforeRetNeeded
  236. p.s(cpsStmts).addSingleSwitchCase(cIntValue(-1)):
  237. blockLeaveActions(p,
  238. howManyTrys = p.nestedTryStmts.len,
  239. howManyExcepts = p.inExceptBlockLen)
  240. p.s(cpsStmts).addGoto("BeforeRet_")
  241. var statesCounter = lastOrd(p.config, n[0].typ)
  242. if n.len >= 2 and n[1].kind == nkIntLit:
  243. statesCounter = getInt(n[1])
  244. let prefix = if n.len == 3 and n[2].kind == nkStrLit: n[2].strVal.rope
  245. else: rope"STATE"
  246. for i in 0i64..toInt64(statesCounter):
  247. p.s(cpsStmts).addSingleSwitchCase(cIntValue(i)):
  248. p.s(cpsStmts).addGoto(prefix & $i)
  249. proc genBreakState(p: BProc, n: PNode, d: var TLoc) =
  250. var a: TLoc
  251. d = initLoc(locExpr, n, OnUnknown)
  252. if n[0].kind == nkClosure:
  253. a = initLocExpr(p, n[0][1])
  254. let ra = a.rdLoc
  255. d.snippet = cOp(LessThan,
  256. subscript(
  257. cCast(ptrType(NimInt), ra),
  258. cIntValue(1)),
  259. cIntValue(0))
  260. else:
  261. a = initLocExpr(p, n[0])
  262. let ra = a.rdLoc
  263. # the environment is guaranteed to contain the 'state' field at offset 1:
  264. d.snippet = cOp(LessThan,
  265. subscript(
  266. cCast(ptrType(NimInt), dotField(ra, "ClE_0")),
  267. cIntValue(1)),
  268. cIntValue(0))
  269. proc genGotoVar(p: BProc; value: PNode) =
  270. if value.kind notin {nkCharLit..nkUInt64Lit}:
  271. localError(p.config, value.info, "'goto' target must be a literal value")
  272. else:
  273. p.s(cpsStmts).addGoto("NIMSTATE_" & $value.intVal)
  274. proc genBracedInit(p: BProc, n: PNode; isConst: bool; optionalType: PType; result: var Builder)
  275. proc potentialValueInit(p: BProc; v: PSym; value: PNode; result: var Builder) =
  276. if lfDynamicLib in v.loc.flags or sfThread in v.flags or p.hcrOn:
  277. discard "nothing to do"
  278. elif sfGlobal in v.flags and value != nil and isDeepConstExpr(value, p.module.compileToCpp) and
  279. p.withinLoop == 0 and not containsGarbageCollectedRef(v.typ):
  280. #echo "New code produced for ", v.name.s, " ", p.config $ value.info
  281. genBracedInit(p, value, isConst = false, v.typ, result)
  282. proc genCppParamsForCtor(p: BProc; call: PNode; didGenTemp: var bool): Snippet =
  283. var res = newBuilder("")
  284. var argBuilder = default(CallBuilder) # not init, only building params
  285. let typ = skipTypes(call[0].typ, abstractInst)
  286. assert(typ.kind == tyProc)
  287. for i in 1..<call.len:
  288. #if it's a type we can just generate here another initializer as we are in an initializer context
  289. if call[i].kind == nkCall and call[i][0].kind == nkSym and call[i][0].sym.kind == skType:
  290. res.addArgument(argBuilder):
  291. res.add genCppInitializer(p.module, p, call[i][0].sym.typ, didGenTemp)
  292. else:
  293. #We need to test for temp in globals, see: #23657
  294. let param =
  295. if typ[i].kind in {tyVar} and call[i].kind == nkHiddenAddr:
  296. call[i][0]
  297. else:
  298. call[i]
  299. if param.kind != nkBracketExpr or param.typ.kind in
  300. {tyRef, tyPtr, tyUncheckedArray, tyArray, tyOpenArray,
  301. tyVarargs, tySequence, tyString, tyCstring, tyTuple}:
  302. let tempLoc = initLocExprSingleUse(p, param)
  303. didGenTemp = didGenTemp or tempLoc.k == locTemp
  304. genOtherArg(p, call, i, typ, res, argBuilder)
  305. result = extract(res)
  306. proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
  307. if sfGoto in v.flags:
  308. # translate 'var state {.goto.} = X' into 'goto LX':
  309. genGotoVar(p, value)
  310. return
  311. let imm = isAssignedImmediately(p.config, value)
  312. let isCppCtorCall = p.module.compileToCpp and imm and
  313. value.kind in nkCallKinds and value[0].kind == nkSym and
  314. v.typ.kind != tyPtr and sfConstructor in value[0].sym.flags
  315. var targetProc = p
  316. var valueBuilder = newBuilder("")
  317. potentialValueInit(p, v, value, valueBuilder)
  318. let valueAsRope = extract(valueBuilder)
  319. if sfGlobal in v.flags:
  320. if v.flags * {sfImportc, sfExportc} == {sfImportc} and
  321. value.kind == nkEmpty and
  322. v.loc.flags * {lfHeader, lfNoDecl} != {}:
  323. return
  324. if sfPure in v.flags:
  325. # v.owner.kind != skModule:
  326. targetProc = p.module.preInitProc
  327. if isCppCtorCall and not containsHiddenPointer(v.typ):
  328. var didGenTemp = false
  329. callGlobalVarCppCtor(targetProc, v, vn, value, didGenTemp)
  330. if didGenTemp:
  331. message(p.config, vn.info, warnGlobalVarConstructorTemporary, vn.sym.name.s)
  332. #We fail to call the constructor in the global scope so we do the call inside the main proc
  333. assignGlobalVar(targetProc, vn, valueAsRope)
  334. var loc = initLocExprSingleUse(targetProc, value)
  335. genAssignment(targetProc, v.loc, loc, {})
  336. else:
  337. assignGlobalVar(targetProc, vn, valueAsRope)
  338. # XXX: be careful here.
  339. # Global variables should not be zeromem-ed within loops
  340. # (see bug #20).
  341. # That's why we are doing the construction inside the preInitProc.
  342. # genObjectInit relies on the C runtime's guarantees that
  343. # global variables will be initialized to zero.
  344. if valueAsRope.len == 0:
  345. var loc = v.loc
  346. # When the native TLS is unavailable, a global thread-local variable needs
  347. # one more layer of indirection in order to access the TLS block.
  348. # Only do this for complex types that may need a call to `objectInit`
  349. if sfThread in v.flags and emulatedThreadVars(p.config) and
  350. isComplexValueType(v.typ):
  351. loc = initLocExprSingleUse(p.module.preInitProc, vn)
  352. genObjectInit(p.module.preInitProc, cpsInit, v.typ, loc, constructObj)
  353. # Alternative construction using default constructor (which may zeromem):
  354. # if sfImportc notin v.flags: constructLoc(p.module.preInitProc, v.loc)
  355. if sfExportc in v.flags and p.module.g.generatedHeader != nil:
  356. genVarPrototype(p.module.g.generatedHeader, vn)
  357. registerTraverseProc(p, v)
  358. else:
  359. if imm and p.module.compileToCpp and p.splitDecls == 0 and
  360. not containsHiddenPointer(v.typ) and
  361. nimErrorFlagAccessed notin p.flags:
  362. # C++ really doesn't like things like 'Foo f; f = x' as that invokes a
  363. # parameterless constructor followed by an assignment operator. So we
  364. # generate better code here: 'Foo f = x;'
  365. genLineDir(p, vn)
  366. var initializer: Snippet = ""
  367. var initializerKind: VarInitializerKind = Assignment
  368. if isCppCtorCall:
  369. var didGenTemp = false
  370. initializer = genCppParamsForCtor(p, value, didGenTemp)
  371. if initializer.len != 0:
  372. initializer = "(" & initializer & ")"
  373. initializerKind = CppConstructor
  374. else:
  375. var tmp = initLocExprSingleUse(p, value)
  376. if value.kind != nkEmpty:
  377. initializer = tmp.rdLoc
  378. localVarDecl(p.s(cpsStmts), p, vn, initializer, initializerKind)
  379. return
  380. assignLocalVar(p, vn)
  381. initLocalVar(p, v, imm)
  382. let traverseProc = CNil
  383. # If the var is in a block (control flow like if/while or a block) in global scope just
  384. # register the so called "global" so it can be used later on. There is no need to close
  385. # and reopen of if (nim_hcr_do_init_) blocks because we are in one already anyway.
  386. var forHcr = treatGlobalDifferentlyForHCR(p.module, v)
  387. if forHcr and targetProc.blocks.len > 3 and v.owner.kind == skModule:
  388. # put it in the locals section - mainly because of loops which
  389. # use the var in a call to resetLoc() in the statements section
  390. let rv = rdLoc(v.loc)
  391. p.s(cpsLocals).addCallStmt("hcrRegisterGlobal",
  392. getModuleDllPath(p.module, v),
  393. '"' & v.loc.snippet & '"',
  394. cSizeof(rv),
  395. traverseProc,
  396. cCast(ptrType(CPointer), cAddr(v.loc.snippet)))
  397. # nothing special left to do later on - let's avoid closing and reopening blocks
  398. forHcr = false
  399. # we close and reopen the global if (nim_hcr_do_init_) blocks in the main Init function
  400. # for the module so we can have globals and top-level code be interleaved and still
  401. # be able to re-run it but without the top level code - just the init of globals
  402. var hcrInit = default(IfBuilder)
  403. if forHcr:
  404. startBlockWith(targetProc):
  405. hcrInit = initIfStmt(p.s(cpsStmts))
  406. initElifBranch(p.s(cpsStmts), hcrInit, cCall("hcrRegisterGlobal",
  407. getModuleDllPath(p.module, v),
  408. '"' & v.loc.snippet & '"',
  409. cSizeof(rdLoc(v.loc)),
  410. traverseProc,
  411. cCast(ptrType(CPointer), cAddr(v.loc.snippet))))
  412. if value.kind != nkEmpty and valueAsRope.len == 0:
  413. genLineDir(targetProc, vn)
  414. if not isCppCtorCall:
  415. loadInto(targetProc, vn, value, v.loc)
  416. if forHcr:
  417. endBlockWith(targetProc):
  418. finishBranch(p.s(cpsStmts), hcrInit)
  419. finishIfStmt(p.s(cpsStmts), hcrInit)
  420. proc genSingleVar(p: BProc, a: PNode) =
  421. let v = a[0].sym
  422. if sfCompileTime in v.flags:
  423. # fix issue #12640
  424. # {.global, compileTime.} pragma in proc
  425. if sfGlobal in v.flags and p.prc != nil and p.prc.kind == skProc:
  426. discard
  427. else:
  428. return
  429. genSingleVar(p, v, a[0], a[2])
  430. proc genClosureVar(p: BProc, a: PNode) =
  431. var immediateAsgn = a[2].kind != nkEmpty
  432. var v: TLoc = initLocExpr(p, a[0])
  433. genLineDir(p, a)
  434. if immediateAsgn:
  435. loadInto(p, a[0], a[2], v)
  436. elif sfNoInit notin a[0][1].sym.flags:
  437. constructLoc(p, v)
  438. proc genVarStmt(p: BProc, n: PNode) =
  439. for it in n.sons:
  440. if it.kind == nkCommentStmt: continue
  441. if it.kind == nkIdentDefs:
  442. # can be a lifted var nowadays ...
  443. if it[0].kind == nkSym:
  444. genSingleVar(p, it)
  445. else:
  446. genClosureVar(p, it)
  447. else:
  448. genVarTuple(p, it)
  449. proc genIf(p: BProc, n: PNode, d: var TLoc) =
  450. #
  451. # { if (!expr1) goto L1;
  452. # thenPart }
  453. # goto LEnd
  454. # L1:
  455. # { if (!expr2) goto L2;
  456. # thenPart2 }
  457. # goto LEnd
  458. # L2:
  459. # { elsePart }
  460. # Lend:
  461. var
  462. a: TLoc
  463. lelse: TLabel
  464. if not isEmptyType(n.typ) and d.k == locNone:
  465. d = getTemp(p, n.typ)
  466. genLineDir(p, n)
  467. let lend = getLabel(p)
  468. for it in n.sons:
  469. # bug #4230: avoid false sharing between branches:
  470. if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
  471. if it.len == 2:
  472. var scope: ScopeBuilder
  473. startSimpleBlock(p, scope)
  474. a = initLocExprSingleUse(p, it[0])
  475. lelse = getLabel(p)
  476. inc(p.labels)
  477. let ra = rdLoc(a)
  478. p.s(cpsStmts).addSingleIfStmt(cOp(Not, ra)):
  479. p.s(cpsStmts).addGoto(lelse)
  480. if p.module.compileToCpp:
  481. # avoid "jump to label crosses initialization" error:
  482. p.s(cpsStmts).addScope():
  483. expr(p, it[1], d)
  484. else:
  485. expr(p, it[1], d)
  486. endSimpleBlock(p, scope)
  487. if n.len > 1:
  488. p.s(cpsStmts).addGoto(lend)
  489. fixLabel(p, lelse)
  490. elif it.len == 1:
  491. var scope: ScopeBuilder
  492. startSimpleBlock(p, scope)
  493. expr(p, it[0], d)
  494. endSimpleBlock(p, scope)
  495. else: internalError(p.config, n.info, "genIf()")
  496. if n.len > 1: fixLabel(p, lend)
  497. proc genReturnStmt(p: BProc, t: PNode) =
  498. if nfPreventCg in t.flags: return
  499. p.flags.incl beforeRetNeeded
  500. genLineDir(p, t)
  501. if (t[0].kind != nkEmpty): genStmts(p, t[0])
  502. blockLeaveActions(p,
  503. howManyTrys = p.nestedTryStmts.len,
  504. howManyExcepts = p.inExceptBlockLen)
  505. if (p.finallySafePoints.len > 0) and noSafePoints notin p.flags:
  506. # If we're in a finally block, and we came here by exception
  507. # consume it before we return.
  508. var safePoint = p.finallySafePoints[^1]
  509. p.s(cpsStmts).addSingleIfStmt(
  510. cOp(NotEqual,
  511. dotField(safePoint, "status"),
  512. cIntValue(0))):
  513. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popCurrentException"))
  514. p.s(cpsStmts).addGoto("BeforeRet_")
  515. proc genGotoForCase(p: BProc; caseStmt: PNode) =
  516. for i in 1..<caseStmt.len:
  517. var scope: ScopeBuilder
  518. startSimpleBlock(p, scope)
  519. let it = caseStmt[i]
  520. for j in 0..<it.len-1:
  521. if it[j].kind == nkRange:
  522. localError(p.config, it.info, "range notation not available for computed goto")
  523. return
  524. let val = getOrdValue(it[j])
  525. p.s(cpsStmts).addLabel("NIMSTATE_" & $val)
  526. genStmts(p, it.lastSon)
  527. endSimpleBlock(p, scope)
  528. iterator fieldValuePairs(n: PNode): tuple[memberSym, valueSym: PNode] =
  529. assert(n.kind in {nkLetSection, nkVarSection})
  530. for identDefs in n:
  531. if identDefs.kind == nkIdentDefs:
  532. let valueSym = identDefs[^1]
  533. for i in 0..<identDefs.len-2:
  534. let memberSym = identDefs[i]
  535. yield((memberSym: memberSym, valueSym: valueSym))
  536. proc genComputedGoto(p: BProc; n: PNode) =
  537. # first pass: Generate array of computed labels:
  538. # flatten the loop body because otherwise let and var sections
  539. # wrapped inside stmt lists by inject destructors won't be recognised
  540. let n = n.flattenStmts()
  541. var casePos = -1
  542. var arraySize: int = 0
  543. for i in 0..<n.len:
  544. let it = n[i]
  545. if it.kind == nkCaseStmt:
  546. if lastSon(it).kind != nkOfBranch:
  547. localError(p.config, it.info,
  548. "case statement must be exhaustive for computed goto"); return
  549. casePos = i
  550. if enumHasHoles(it[0].typ):
  551. localError(p.config, it.info,
  552. "case statement cannot work on enums with holes for computed goto"); return
  553. let aSize = lengthOrd(p.config, it[0].typ)
  554. if aSize > 10_000:
  555. localError(p.config, it.info,
  556. "case statement has too many cases for computed goto"); return
  557. arraySize = toInt(aSize)
  558. if firstOrd(p.config, it[0].typ) != 0:
  559. localError(p.config, it.info,
  560. "case statement has to start at 0 for computed goto"); return
  561. if casePos < 0:
  562. localError(p.config, n.info, "no case statement found for computed goto"); return
  563. var id = p.labels+1
  564. inc p.labels, arraySize+1
  565. let tmp = "TMP$1_" % [id.rope]
  566. p.s(cpsStmts).addArrayVarWithInitializer(kind = Global,
  567. name = tmp,
  568. elementType = CPointer,
  569. len = arraySize):
  570. var labelsInit: StructInitializer
  571. p.s(cpsStmts).addStructInitializer(labelsInit, kind = siArray):
  572. for i in 1..arraySize:
  573. p.s(cpsStmts).addField(labelsInit, ""):
  574. p.s(cpsStmts).add(cLabelAddr("TMP" & $(id+i) & "_"))
  575. for j in 0..<casePos:
  576. genStmts(p, n[j])
  577. let caseStmt = n[casePos]
  578. var a: TLoc = initLocExpr(p, caseStmt[0])
  579. let ra = a.rdLoc
  580. # first goto:
  581. p.s(cpsStmts).addComputedGoto(subscript(tmp, ra))
  582. for i in 1..<caseStmt.len:
  583. var scope: ScopeBuilder
  584. startSimpleBlock(p, scope)
  585. let it = caseStmt[i]
  586. for j in 0..<it.len-1:
  587. if it[j].kind == nkRange:
  588. localError(p.config, it.info, "range notation not available for computed goto")
  589. return
  590. let val = getOrdValue(it[j])
  591. let lit = cIntLiteral(toInt64(val)+id+1)
  592. p.s(cpsStmts).addLabel("TMP" & lit & "_")
  593. genStmts(p, it.lastSon)
  594. for j in casePos+1..<n.len:
  595. genStmts(p, n[j])
  596. for j in 0..<casePos:
  597. # prevent new local declarations
  598. # compile declarations as assignments
  599. let it = n[j]
  600. if it.kind in {nkLetSection, nkVarSection}:
  601. let asgn = copyNode(it)
  602. asgn.transitionSonsKind(nkAsgn)
  603. asgn.sons.setLen 2
  604. for sym, value in it.fieldValuePairs:
  605. if value.kind != nkEmpty:
  606. asgn[0] = sym
  607. asgn[1] = value
  608. genStmts(p, asgn)
  609. else:
  610. genStmts(p, it)
  611. var a: TLoc = initLocExpr(p, caseStmt[0])
  612. let ra = a.rdLoc
  613. p.s(cpsStmts).addComputedGoto(subscript(tmp, ra))
  614. endSimpleBlock(p, scope)
  615. for j in casePos+1..<n.len:
  616. genStmts(p, n[j])
  617. proc genWhileStmt(p: BProc, t: PNode) =
  618. # we don't generate labels here as for example GCC would produce
  619. # significantly worse code
  620. var
  621. a: TLoc
  622. assert(t.len == 2)
  623. inc(p.withinLoop)
  624. genLineDir(p, t)
  625. preserveBreakIdx:
  626. var loopBody = t[1]
  627. if loopBody.stmtsContainPragma(wComputedGoto) and
  628. hasComputedGoto in CC[p.config.cCompiler].props:
  629. # for closure support weird loop bodies are generated:
  630. if loopBody.len == 2 and loopBody[0].kind == nkEmpty:
  631. loopBody = loopBody[1]
  632. genComputedGoto(p, loopBody)
  633. else:
  634. var stmt: WhileBuilder
  635. p.breakIdx = startBlockWith(p):
  636. stmt = initWhileStmt(p.s(cpsStmts), cIntValue(1))
  637. p.blocks[p.breakIdx].isLoop = true
  638. a = initLocExpr(p, t[0])
  639. if (t[0].kind != nkIntLit) or (t[0].intVal == 0):
  640. let ra = a.rdLoc
  641. var label: TLabel = ""
  642. assignLabel(p.blocks[p.breakIdx], label)
  643. p.s(cpsStmts).addSingleIfStmt(cOp(Not, ra)):
  644. p.s(cpsStmts).addGoto(label)
  645. genStmts(p, loopBody)
  646. if optProfiler in p.options:
  647. # invoke at loop body exit:
  648. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimProfile"))
  649. endBlockWith(p):
  650. finishWhileStmt(p.s(cpsStmts), stmt)
  651. dec(p.withinLoop)
  652. proc genBlock(p: BProc, n: PNode, d: var TLoc) =
  653. if not isEmptyType(n.typ):
  654. # bug #4505: allocate the temp in the outer scope
  655. # so that it can escape the generated {}:
  656. if d.k == locNone:
  657. d = getTemp(p, n.typ)
  658. d.flags.incl(lfEnforceDeref)
  659. preserveBreakIdx:
  660. var scope: ScopeBuilder
  661. p.breakIdx = startSimpleBlock(p, scope)
  662. if n[0].kind != nkEmpty:
  663. # named block?
  664. assert(n[0].kind == nkSym)
  665. var sym = n[0].sym
  666. sym.loc.k = locOther
  667. sym.position = p.breakIdx+1
  668. expr(p, n[1], d)
  669. endSimpleBlock(p, scope)
  670. proc genParForStmt(p: BProc, t: PNode) =
  671. assert(t.len == 3)
  672. inc(p.withinLoop)
  673. genLineDir(p, t)
  674. preserveBreakIdx:
  675. let forLoopVar = t[0].sym
  676. assignLocalVar(p, t[0])
  677. #initLoc(forLoopVar.loc, locLocalVar, forLoopVar.typ, onStack)
  678. #discard mangleName(forLoopVar)
  679. let call = t[1]
  680. assert(call.len == 4 or call.len == 5)
  681. var rangeA = initLocExpr(p, call[1])
  682. var rangeB = initLocExpr(p, call[2])
  683. var stepNode: PNode = nil
  684. # $n at the beginning because of #9710
  685. if call.len == 4: # procName(a, b, annotation)
  686. if call[0].sym.name.s == "||": # `||`(a, b, annotation)
  687. p.s(cpsStmts).addCPragma("omp " & call[3].getStr)
  688. else:
  689. p.s(cpsStmts).addCPragma(call[3].getStr)
  690. else: # `||`(a, b, step, annotation)
  691. stepNode = call[3]
  692. p.s(cpsStmts).addCPragma("omp " & call[4].getStr)
  693. p.breakIdx = startBlockWith(p):
  694. if stepNode == nil:
  695. initForRange(p.s(cpsStmts), forLoopVar.loc.rdLoc, rangeA.rdLoc, rangeB.rdLoc, true)
  696. else:
  697. var step: TLoc = initLocExpr(p, stepNode)
  698. initForStep(p.s(cpsStmts), forLoopVar.loc.rdLoc, rangeA.rdLoc, rangeB.rdLoc, step.rdLoc, true)
  699. p.blocks[p.breakIdx].isLoop = true
  700. genStmts(p, t[2])
  701. endBlockWith(p):
  702. finishFor(p.s(cpsStmts))
  703. dec(p.withinLoop)
  704. proc genBreakStmt(p: BProc, t: PNode) =
  705. var idx = p.breakIdx
  706. if t[0].kind != nkEmpty:
  707. # named break?
  708. assert(t[0].kind == nkSym)
  709. var sym = t[0].sym
  710. doAssert(sym.loc.k == locOther)
  711. idx = sym.position-1
  712. else:
  713. # an unnamed 'break' can only break a loop after 'transf' pass:
  714. while idx >= 0 and not p.blocks[idx].isLoop: dec idx
  715. if idx < 0 or not p.blocks[idx].isLoop:
  716. internalError(p.config, t.info, "no loop to break")
  717. p.blocks[idx].label = "LA" & p.blocks[idx].id.rope
  718. blockLeaveActions(p,
  719. p.nestedTryStmts.len - p.blocks[idx].nestedTryStmts,
  720. p.inExceptBlockLen - p.blocks[idx].nestedExceptStmts)
  721. genLineDir(p, t)
  722. p.s(cpsStmts).addGoto(p.blocks[idx].label)
  723. proc raiseExit(p: BProc) =
  724. assert p.config.exc == excGoto
  725. if nimErrorFlagDisabled notin p.flags:
  726. p.flags.incl nimErrorFlagAccessed
  727. p.s(cpsStmts).addSingleIfStmt(cUnlikely(cDeref("nimErr_"))):
  728. if p.nestedTryStmts.len == 0:
  729. p.flags.incl beforeRetNeeded
  730. # easy case, simply goto 'ret':
  731. p.s(cpsStmts).addGoto("BeforeRet_")
  732. else:
  733. p.s(cpsStmts).addGoto("LA" & $p.nestedTryStmts[^1].label & "_")
  734. proc raiseExitCleanup(p: BProc, destroy: string) =
  735. assert p.config.exc == excGoto
  736. if nimErrorFlagDisabled notin p.flags:
  737. p.flags.incl nimErrorFlagAccessed
  738. p.s(cpsStmts).addSingleIfStmt(cUnlikely(cDeref("nimErr_"))):
  739. p.s(cpsStmts).addStmt():
  740. p.s(cpsStmts).add(destroy)
  741. if p.nestedTryStmts.len == 0:
  742. p.flags.incl beforeRetNeeded
  743. # easy case, simply goto 'ret':
  744. p.s(cpsStmts).addGoto("BeforeRet_")
  745. else:
  746. p.s(cpsStmts).addGoto("LA" & $p.nestedTryStmts[^1].label & "_")
  747. proc finallyActions(p: BProc) =
  748. if p.config.exc != excGoto and p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept:
  749. # if the current try stmt have a finally block,
  750. # we must execute it before reraising
  751. let finallyBlock = p.nestedTryStmts[^1].fin
  752. if finallyBlock != nil:
  753. genSimpleBlock(p, finallyBlock[0])
  754. proc raiseInstr(p: BProc; result: var Builder) =
  755. if p.config.exc == excGoto:
  756. let L = p.nestedTryStmts.len
  757. if L == 0:
  758. p.flags.incl beforeRetNeeded
  759. # easy case, simply goto 'ret':
  760. result.addGoto("BeforeRet_")
  761. else:
  762. # raise inside an 'except' must go to the finally block,
  763. # raise outside an 'except' block must go to the 'except' list.
  764. result.addGoto("LA" & $p.nestedTryStmts[L-1].label & "_")
  765. # + ord(p.nestedTryStmts[L-1].inExcept)])
  766. proc genRaiseStmt(p: BProc, t: PNode) =
  767. if t[0].kind != nkEmpty:
  768. var a: TLoc = initLocExprSingleUse(p, t[0])
  769. finallyActions(p)
  770. var e = rdLoc(a)
  771. discard getTypeDesc(p.module, t[0].typ)
  772. var typ = skipTypes(t[0].typ, abstractPtrs)
  773. case p.config.exc
  774. of excCpp:
  775. blockLeaveActions(p, howManyTrys = 0, howManyExcepts = p.inExceptBlockLen)
  776. of excGoto:
  777. blockLeaveActions(p, howManyTrys = 0,
  778. howManyExcepts = (if p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept: 1 else: 0))
  779. else:
  780. discard
  781. genLineDir(p, t)
  782. if isImportedException(typ, p.config):
  783. lineF(p, cpsStmts, "throw $1;$n", [e])
  784. else:
  785. let eName = makeCString(typ.sym.name.s)
  786. let pName = makeCString(if p.prc != nil: p.prc.name.s else: p.module.module.name.s)
  787. let fName = quotedFilename(p.config, t.info)
  788. let ln = toLinenumber(t.info)
  789. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "raiseExceptionEx"),
  790. cCast(ptrType(cgsymValue(p.module, "Exception")), e),
  791. eName,
  792. pName,
  793. fName,
  794. cIntValue(ln))
  795. if optOwnedRefs in p.config.globalOptions:
  796. p.s(cpsStmts).addAssignment(e, NimNil)
  797. else:
  798. finallyActions(p)
  799. genLineDir(p, t)
  800. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "reraiseException"))
  801. raiseInstr(p, p.s(cpsStmts))
  802. template genCaseGenericBranch(p: BProc, b: PNode, e: TLoc, labl: TLabel,
  803. rangeFormat, eqFormat: untyped) =
  804. var x, y: TLoc
  805. for i in 0..<b.len - 1:
  806. let rlabel {.inject.} = labl
  807. if b[i].kind == nkRange:
  808. x = initLocExpr(p, b[i][0])
  809. y = initLocExpr(p, b[i][1])
  810. let ra {.inject.} = rdCharLoc(e)
  811. let rb {.inject.} = rdCharLoc(x)
  812. let rc {.inject.} = rdCharLoc(y)
  813. rangeFormat
  814. else:
  815. x = initLocExpr(p, b[i])
  816. let ra {.inject.} = rdCharLoc(e)
  817. let rb {.inject.} = rdCharLoc(x)
  818. eqFormat
  819. proc genCaseSecondPass(p: BProc, t: PNode, d: var TLoc,
  820. labId, until: int): TLabel =
  821. var lend = getLabel(p)
  822. for i in 1..until:
  823. # bug #4230: avoid false sharing between branches:
  824. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  825. p.s(cpsStmts).addLabel("LA" & $(labId + i) & "_")
  826. if t[i].kind == nkOfBranch:
  827. exprBlock(p, t[i][^1], d)
  828. p.s(cpsStmts).addGoto(lend)
  829. else:
  830. exprBlock(p, t[i][0], d)
  831. result = lend
  832. template genIfForCaseUntil(p: BProc, t: PNode, d: var TLoc,
  833. until: int, a: TLoc,
  834. rangeFormat, eqFormat: untyped): TLabel =
  835. # generate a C-if statement for a Nim case statement
  836. var res: TLabel
  837. var labId = p.labels
  838. for i in 1..until:
  839. inc(p.labels)
  840. let lab = "LA" & $p.labels & "_"
  841. if t[i].kind == nkOfBranch: # else statement
  842. genCaseGenericBranch(p, t[i], a, lab, rangeFormat, eqFormat)
  843. else:
  844. p.s(cpsStmts).addGoto(lab)
  845. if until < t.len-1:
  846. inc(p.labels)
  847. var gotoTarget = "LA" & $p.labels & "_"
  848. p.s(cpsStmts).addGoto(gotoTarget)
  849. res = genCaseSecondPass(p, t, d, labId, until)
  850. p.s(cpsStmts).addLabel(gotoTarget)
  851. else:
  852. res = genCaseSecondPass(p, t, d, labId, until)
  853. res
  854. template genCaseGeneric(p: BProc, t: PNode, d: var TLoc,
  855. rangeFormat, eqFormat: untyped) =
  856. var a: TLoc = initLocExpr(p, t[0])
  857. var lend = genIfForCaseUntil(p, t, d, t.len-1, a, rangeFormat, eqFormat)
  858. fixLabel(p, lend)
  859. proc genCaseStringBranch(p: BProc, b: PNode, e: TLoc, labl: TLabel,
  860. stringKind: TTypeKind,
  861. branches: var openArray[Builder]) =
  862. var x: TLoc
  863. for i in 0..<b.len - 1:
  864. assert(b[i].kind != nkRange)
  865. x = initLocExpr(p, b[i])
  866. var j: int = 0
  867. case b[i].kind
  868. of nkStrLit..nkTripleStrLit:
  869. j = int(hashString(p.config, b[i].strVal) and high(branches))
  870. of nkNilLit: j = 0
  871. else:
  872. assert false, "invalid string case branch node kind"
  873. let re = rdLoc(e)
  874. let rx = rdLoc(x)
  875. branches[j].addSingleIfStmtWithCond():
  876. let eqName = if stringKind == tyCstring: "eqCstrings" else: "eqStrings"
  877. branches[j].addCall(cgsymValue(p.module, eqName), re, rx)
  878. do:
  879. branches[j].addGoto(labl)
  880. proc genStringCase(p: BProc, t: PNode, stringKind: TTypeKind, d: var TLoc) =
  881. # count how many constant strings there are in the case:
  882. var strings = 0
  883. for i in 1..<t.len:
  884. if t[i].kind == nkOfBranch: inc(strings, t[i].len - 1)
  885. if strings > stringCaseThreshold:
  886. var bitMask = math.nextPowerOfTwo(strings) - 1
  887. var branches: seq[Builder]
  888. newSeq(branches, bitMask + 1)
  889. var a: TLoc = initLocExpr(p, t[0]) # first pass: generate ifs+goto:
  890. var labId = p.labels
  891. for i in 1..<t.len:
  892. inc(p.labels)
  893. if t[i].kind == nkOfBranch:
  894. genCaseStringBranch(p, t[i], a, "LA" & rope(p.labels) & "_",
  895. stringKind, branches)
  896. else:
  897. # else statement: nothing to do yet
  898. # but we reserved a label, which we use later
  899. discard
  900. let fnName = if stringKind == tyCstring: "hashCstring" else: "hashString"
  901. let ra = rdLoc(a)
  902. p.s(cpsStmts).addSwitchStmt(
  903. cOp(BitAnd, NimInt,
  904. cCall(cgsymValue(p.module, fnName), ra),
  905. cIntValue(bitMask))):
  906. for j in 0..high(branches):
  907. if branches[j].buf.len != 0:
  908. let lit = cIntLiteral(j)
  909. p.s(cpsStmts).addSingleSwitchCase(lit):
  910. p.s(cpsStmts).add(extract(branches[j]))
  911. p.s(cpsStmts).addBreak()
  912. # else statement:
  913. if t[^1].kind != nkOfBranch:
  914. p.s(cpsStmts).addGoto("LA" & rope(p.labels) & "_")
  915. # third pass: generate statements
  916. var lend = genCaseSecondPass(p, t, d, labId, t.len-1)
  917. fixLabel(p, lend)
  918. else:
  919. let eqFn = cgsymValue(p.module,
  920. if stringKind == tyCstring: "eqCstrings"
  921. else: "eqStrings")
  922. genCaseGeneric(p, t, d):
  923. discard
  924. do:
  925. p.s(cpsStmts).addSingleIfStmt(
  926. cCall(eqFn, ra, rb)):
  927. p.s(cpsStmts).addGoto(rlabel)
  928. proc branchHasTooBigRange(b: PNode): bool =
  929. result = false
  930. for it in b:
  931. # last son is block
  932. if (it.kind == nkRange) and
  933. it[1].intVal - it[0].intVal > RangeExpandLimit:
  934. return true
  935. proc ifSwitchSplitPoint(p: BProc, n: PNode): int =
  936. result = 0
  937. for i in 1..<n.len:
  938. var branch = n[i]
  939. var stmtBlock = lastSon(branch)
  940. if stmtBlock.stmtsContainPragma(wLinearScanEnd):
  941. result = i
  942. elif hasSwitchRange notin CC[p.config.cCompiler].props:
  943. if branch.kind == nkOfBranch and branchHasTooBigRange(branch):
  944. result = i
  945. proc genCaseRange(p: BProc, branch: PNode, info: var SwitchCaseBuilder) =
  946. for j in 0..<branch.len-1:
  947. if branch[j].kind == nkRange:
  948. if hasSwitchRange in CC[p.config.cCompiler].props:
  949. var litA = newBuilder("")
  950. var litB = newBuilder("")
  951. genLiteral(p, branch[j][0], litA)
  952. genLiteral(p, branch[j][1], litB)
  953. p.s(cpsStmts).addCaseRange(info, extract(litA), extract(litB))
  954. else:
  955. var v = copyNode(branch[j][0])
  956. while v.intVal <= branch[j][1].intVal:
  957. var litA = newBuilder("")
  958. genLiteral(p, v, litA)
  959. p.s(cpsStmts).addCase(info, extract(litA))
  960. inc(v.intVal)
  961. else:
  962. var litA = newBuilder("")
  963. genLiteral(p, branch[j], litA)
  964. p.s(cpsStmts).addCase(info, extract(litA))
  965. proc genOrdinalCase(p: BProc, n: PNode, d: var TLoc) =
  966. # analyse 'case' statement:
  967. var splitPoint = ifSwitchSplitPoint(p, n)
  968. # generate if part (might be empty):
  969. var a: TLoc = initLocExpr(p, n[0])
  970. var lend: TLabel = ""
  971. if splitPoint > 0:
  972. lend = genIfForCaseUntil(p, n, d, splitPoint, a):
  973. p.s(cpsStmts).addSingleIfStmt(cOp(And,
  974. cOp(GreaterEqual, ra, rb),
  975. cOp(LessEqual, ra, rc))):
  976. p.s(cpsStmts).addGoto(rlabel)
  977. do:
  978. p.s(cpsStmts).addSingleIfStmt(
  979. removeSinglePar(cOp(Equal, ra, rb))):
  980. p.s(cpsStmts).addGoto(rlabel)
  981. # generate switch part (might be empty):
  982. if splitPoint+1 < n.len:
  983. let rca = rdCharLoc(a)
  984. p.s(cpsStmts).addSwitchStmt(rca):
  985. var hasDefault = false
  986. for i in splitPoint+1..<n.len:
  987. # bug #4230: avoid false sharing between branches:
  988. if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
  989. var branch = n[i]
  990. var caseBuilder: SwitchCaseBuilder
  991. p.s(cpsStmts).addSwitchCase(caseBuilder):
  992. if branch.kind == nkOfBranch:
  993. genCaseRange(p, branch, caseBuilder)
  994. else:
  995. # else part of case statement:
  996. hasDefault = true
  997. p.s(cpsStmts).addCaseElse(caseBuilder)
  998. do:
  999. exprBlock(p, branch.lastSon, d)
  1000. p.s(cpsStmts).addBreak()
  1001. if not hasDefault:
  1002. if hasBuiltinUnreachable in CC[p.config.cCompiler].props:
  1003. p.s(cpsStmts).addSwitchElse():
  1004. p.s(cpsStmts).addCallStmt("__builtin_unreachable")
  1005. elif hasAssume in CC[p.config.cCompiler].props:
  1006. p.s(cpsStmts).addSwitchElse():
  1007. p.s(cpsStmts).addCallStmt("__assume", cIntValue(0))
  1008. if lend != "": fixLabel(p, lend)
  1009. proc genCase(p: BProc, t: PNode, d: var TLoc) =
  1010. genLineDir(p, t)
  1011. if not isEmptyType(t.typ) and d.k == locNone:
  1012. d = getTemp(p, t.typ)
  1013. case skipTypes(t[0].typ, abstractVarRange).kind
  1014. of tyString:
  1015. genStringCase(p, t, tyString, d)
  1016. of tyCstring:
  1017. genStringCase(p, t, tyCstring, d)
  1018. of tyFloat..tyFloat128:
  1019. genCaseGeneric(p, t, d):
  1020. p.s(cpsStmts).addSingleIfStmt(cOp(And,
  1021. cOp(GreaterEqual, ra, rb),
  1022. cOp(LessEqual, ra, rc))):
  1023. p.s(cpsStmts).addGoto(rlabel)
  1024. do:
  1025. p.s(cpsStmts).addSingleIfStmt(
  1026. removeSinglePar(cOp(Equal, ra, rb))):
  1027. p.s(cpsStmts).addGoto(rlabel)
  1028. else:
  1029. if t[0].kind == nkSym and sfGoto in t[0].sym.flags:
  1030. genGotoForCase(p, t)
  1031. else:
  1032. genOrdinalCase(p, t, d)
  1033. proc genRestoreFrameAfterException(p: BProc) =
  1034. if optStackTrace in p.module.config.options:
  1035. if hasCurFramePointer notin p.flags:
  1036. p.flags.incl hasCurFramePointer
  1037. p.procSec(cpsLocals).add('\t')
  1038. p.procSec(cpsLocals).addVar(kind = Local, name = "_nimCurFrame", typ = ptrType("TFrame"))
  1039. p.procSec(cpsInit).add('\t')
  1040. p.procSec(cpsInit).addAssignmentWithValue("_nimCurFrame"):
  1041. p.procSec(cpsInit).addCall(cgsymValue(p.module, "getFrame"))
  1042. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "setFrame"), "_nimCurFrame")
  1043. proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
  1044. #[ code to generate:
  1045. std::exception_ptr error;
  1046. try {
  1047. body;
  1048. } catch (Exception e) {
  1049. error = std::current_exception();
  1050. if (ofExpr(e, TypeHere)) {
  1051. error = nullptr; // handled
  1052. } else if (...) {
  1053. } else {
  1054. throw;
  1055. }
  1056. } catch(...) {
  1057. // C++ exception occured, not under Nim's control.
  1058. }
  1059. {
  1060. /* finally: */
  1061. printf('fin!\n');
  1062. if (error) std::rethrow_exception(error); // re-raise the exception
  1063. }
  1064. ]#
  1065. p.module.includeHeader("<exception>")
  1066. if not isEmptyType(t.typ) and d.k == locNone:
  1067. d = getTemp(p, t.typ)
  1068. genLineDir(p, t)
  1069. inc(p.labels, 2)
  1070. let etmp = p.labels
  1071. #init on locals, fixes #23306
  1072. lineCg(p, cpsLocals, "std::exception_ptr T$1_;$n", [etmp])
  1073. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1074. p.nestedTryStmts.add((fin, false, 0.Natural))
  1075. if t.kind == nkHiddenTryStmt:
  1076. lineCg(p, cpsStmts, "try {$n", [])
  1077. expr(p, t[0], d)
  1078. lineCg(p, cpsStmts, "}$n", [])
  1079. else:
  1080. startBlockWith(p):
  1081. p.s(cpsStmts).add("try {\n")
  1082. expr(p, t[0], d)
  1083. endBlockWith(p):
  1084. p.s(cpsStmts).add("}\n")
  1085. # First pass: handle Nim based exceptions:
  1086. lineCg(p, cpsStmts, "catch (#Exception* T$1_) {$n", [etmp+1])
  1087. genRestoreFrameAfterException(p)
  1088. # an unhandled exception happened!
  1089. lineCg(p, cpsStmts, "T$1_ = std::current_exception();$n", [etmp])
  1090. p.nestedTryStmts[^1].inExcept = true
  1091. var hasImportedCppExceptions = false
  1092. var i = 1
  1093. var ifStmt = default(IfBuilder)
  1094. var hasIf = false
  1095. var hasElse = false
  1096. while (i < t.len) and (t[i].kind == nkExceptBranch):
  1097. # bug #4230: avoid false sharing between branches:
  1098. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1099. if t[i].len == 1:
  1100. hasImportedCppExceptions = true
  1101. hasElse = true
  1102. # general except section:
  1103. var scope = default(ScopeBuilder)
  1104. startBlockWith(p):
  1105. if hasIf:
  1106. initElseBranch(p.s(cpsStmts), ifStmt)
  1107. else:
  1108. scope = initScope(p.s(cpsStmts))
  1109. # we handled the error:
  1110. expr(p, t[i][0], d)
  1111. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  1112. endBlockWith(p):
  1113. if hasIf:
  1114. finishBranch(p.s(cpsStmts), ifStmt)
  1115. else:
  1116. finishScope(p.s(cpsStmts), scope)
  1117. else:
  1118. var orExpr = newRopeAppender()
  1119. var exvar = PNode(nil)
  1120. for j in 0..<t[i].len - 1:
  1121. var typeNode = t[i][j]
  1122. if t[i][j].isInfixAs():
  1123. typeNode = t[i][j][1]
  1124. exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
  1125. assert(typeNode.kind == nkType)
  1126. if isImportedException(typeNode.typ, p.config):
  1127. hasImportedCppExceptions = true
  1128. else:
  1129. if orExpr.len != 0: orExpr.add("||")
  1130. let memberName = if p.module.compileToCpp: "m_type" else: "Sup.m_type"
  1131. if optTinyRtti in p.config.globalOptions:
  1132. let checkFor = $getObjDepth(typeNode.typ)
  1133. appcg(p.module, orExpr, "#isObjDisplayCheck(#nimBorrowCurrentException()->$1, $2, $3)", [memberName, checkFor, $genDisplayElem(MD5Digest(hashType(typeNode.typ, p.config)))])
  1134. else:
  1135. let checkFor = genTypeInfoV1(p.module, typeNode.typ, typeNode.info)
  1136. appcg(p.module, orExpr, "#isObj(#nimBorrowCurrentException()->$1, $2)", [memberName, checkFor])
  1137. if orExpr.len != 0:
  1138. if not hasIf:
  1139. hasIf = true
  1140. ifStmt = initIfStmt(p.s(cpsStmts))
  1141. startBlockWith(p):
  1142. initElifBranch(p.s(cpsStmts), ifStmt, orExpr)
  1143. if exvar != nil:
  1144. fillLocalName(p, exvar.sym)
  1145. fillLoc(exvar.sym.loc, locTemp, exvar, OnStack)
  1146. linefmt(p, cpsStmts, "$1 $2 = T$3_;$n", [getTypeDesc(p.module, exvar.sym.typ),
  1147. rdLoc(exvar.sym.loc), rope(etmp+1)])
  1148. # we handled the error:
  1149. linefmt(p, cpsStmts, "T$1_ = nullptr;$n", [etmp])
  1150. expr(p, t[i][^1], d)
  1151. linefmt(p, cpsStmts, "#popCurrentException();$n", [])
  1152. endBlockWith(p):
  1153. finishBranch(p.s(cpsStmts), ifStmt)
  1154. inc(i)
  1155. if hasIf and not hasElse:
  1156. p.s(cpsStmts).addElseBranch(ifStmt):
  1157. p.s(cpsStmts).add("throw;\n")
  1158. if hasIf:
  1159. finishIfStmt(p.s(cpsStmts), ifStmt)
  1160. linefmt(p, cpsStmts, "}$n", [])
  1161. # Second pass: handle C++ based exceptions:
  1162. template genExceptBranchBody(body: PNode) {.dirty.} =
  1163. genRestoreFrameAfterException(p)
  1164. #linefmt(p, cpsStmts, "T$1_ = std::current_exception();$n", [etmp])
  1165. expr(p, body, d)
  1166. var catchAllPresent = false
  1167. incl p.flags, noSafePoints # mark as not needing 'popCurrentException'
  1168. if hasImportedCppExceptions:
  1169. for i in 1..<t.len:
  1170. if t[i].kind != nkExceptBranch: break
  1171. # bug #4230: avoid false sharing between branches:
  1172. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1173. if t[i].len == 1:
  1174. # general except section:
  1175. startBlockWith(p):
  1176. p.s(cpsStmts).add("catch (...) {\n")
  1177. genExceptBranchBody(t[i][0])
  1178. endBlockWith(p):
  1179. p.s(cpsStmts).add("}\n")
  1180. catchAllPresent = true
  1181. else:
  1182. for j in 0..<t[i].len-1:
  1183. var typeNode = t[i][j]
  1184. if t[i][j].isInfixAs():
  1185. typeNode = t[i][j][1]
  1186. if isImportedException(typeNode.typ, p.config):
  1187. let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
  1188. fillLocalName(p, exvar.sym)
  1189. fillLoc(exvar.sym.loc, locTemp, exvar, OnStack)
  1190. startBlockWith(p):
  1191. lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, typeNode.typ), rdLoc(exvar.sym.loc)])
  1192. genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
  1193. endBlockWith(p):
  1194. p.s(cpsStmts).add("}\n")
  1195. elif isImportedException(typeNode.typ, p.config):
  1196. startBlockWith(p):
  1197. lineCg(p, cpsStmts, "catch ($1&) {$n", [getTypeDesc(p.module, t[i][j].typ)])
  1198. genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
  1199. endBlockWith(p):
  1200. p.s(cpsStmts).add("}\n")
  1201. excl p.flags, noSafePoints
  1202. discard pop(p.nestedTryStmts)
  1203. # general finally block:
  1204. if t.len > 0 and t[^1].kind == nkFinally:
  1205. if not catchAllPresent:
  1206. startBlockWith(p):
  1207. p.s(cpsStmts).add("catch (...) {\n")
  1208. genRestoreFrameAfterException(p)
  1209. linefmt(p, cpsStmts, "T$1_ = std::current_exception();$n", [etmp])
  1210. endBlockWith(p):
  1211. p.s(cpsStmts).add("}\n")
  1212. var scope: ScopeBuilder
  1213. startSimpleBlock(p, scope)
  1214. genStmts(p, t[^1][0])
  1215. linefmt(p, cpsStmts, "if (T$1_) std::rethrow_exception(T$1_);$n", [etmp])
  1216. endSimpleBlock(p, scope)
  1217. proc genTryCppOld(p: BProc, t: PNode, d: var TLoc) =
  1218. # There are two versions we generate, depending on whether we
  1219. # catch C++ exceptions, imported via .importcpp or not. The
  1220. # code can be easier if there are no imported C++ exceptions
  1221. # to deal with.
  1222. # code to generate:
  1223. #
  1224. # try
  1225. # {
  1226. # myDiv(4, 9);
  1227. # } catch (NimExceptionType1&) {
  1228. # body
  1229. # } catch (NimExceptionType2&) {
  1230. # finallyPart()
  1231. # raise;
  1232. # }
  1233. # catch(...) {
  1234. # general_handler_body
  1235. # }
  1236. # finallyPart();
  1237. template genExceptBranchBody(body: PNode) {.dirty.} =
  1238. genRestoreFrameAfterException(p)
  1239. expr(p, body, d)
  1240. if not isEmptyType(t.typ) and d.k == locNone:
  1241. d = getTemp(p, t.typ)
  1242. genLineDir(p, t)
  1243. cgsym(p.module, "popCurrentExceptionEx")
  1244. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1245. p.nestedTryStmts.add((fin, false, 0.Natural))
  1246. startBlockWith(p):
  1247. p.s(cpsStmts).add("try {\n")
  1248. expr(p, t[0], d)
  1249. endBlockWith(p):
  1250. p.s(cpsStmts).add("}\n")
  1251. var catchAllPresent = false
  1252. p.nestedTryStmts[^1].inExcept = true
  1253. for i in 1..<t.len:
  1254. if t[i].kind != nkExceptBranch: break
  1255. # bug #4230: avoid false sharing between branches:
  1256. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1257. if t[i].len == 1:
  1258. # general except section:
  1259. catchAllPresent = true
  1260. startBlockWith(p):
  1261. p.s(cpsStmts).add("catch (...) {\n")
  1262. genExceptBranchBody(t[i][0])
  1263. endBlockWith(p):
  1264. p.s(cpsStmts).add("}\n")
  1265. else:
  1266. for j in 0..<t[i].len-1:
  1267. if t[i][j].isInfixAs():
  1268. let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
  1269. fillLocalName(p, exvar.sym)
  1270. fillLoc(exvar.sym.loc, locTemp, exvar, OnUnknown)
  1271. startBlockWith(p):
  1272. lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, t[i][j][1].typ), rdLoc(exvar.sym.loc)])
  1273. else:
  1274. startBlockWith(p):
  1275. lineCg(p, cpsStmts, "catch ($1&) {$n", [getTypeDesc(p.module, t[i][j].typ)])
  1276. genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
  1277. endBlockWith(p):
  1278. p.s(cpsStmts).add("}\n")
  1279. discard pop(p.nestedTryStmts)
  1280. if t[^1].kind == nkFinally:
  1281. # c++ does not have finally, therefore code needs to be generated twice
  1282. if not catchAllPresent:
  1283. # finally requires catch all presence
  1284. startBlockWith(p):
  1285. p.s(cpsStmts).add("catch (...) {\n")
  1286. genStmts(p, t[^1][0])
  1287. line(p, cpsStmts, "throw;\n")
  1288. endBlockWith(p):
  1289. p.s(cpsStmts).add("}\n")
  1290. genSimpleBlock(p, t[^1][0])
  1291. proc bodyCanRaise(p: BProc; n: PNode): bool =
  1292. case n.kind
  1293. of nkCallKinds:
  1294. result = canRaiseDisp(p, n[0])
  1295. if not result:
  1296. # also check the arguments:
  1297. for i in 1 ..< n.len:
  1298. if bodyCanRaise(p, n[i]): return true
  1299. of nkRaiseStmt:
  1300. result = true
  1301. of nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef, nkIteratorDef,
  1302. nkMacroDef, nkTemplateDef, nkLambda, nkDo, nkFuncDef:
  1303. result = false
  1304. else:
  1305. for i in 0 ..< safeLen(n):
  1306. if bodyCanRaise(p, n[i]): return true
  1307. result = false
  1308. proc genTryGoto(p: BProc; t: PNode; d: var TLoc) =
  1309. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1310. inc p.labels
  1311. let lab = p.labels
  1312. let hasExcept = t[1].kind == nkExceptBranch
  1313. if hasExcept: inc p.withinTryWithExcept
  1314. p.nestedTryStmts.add((fin, false, Natural lab))
  1315. p.flags.incl nimErrorFlagAccessed
  1316. if not isEmptyType(t.typ) and d.k == locNone:
  1317. d = getTemp(p, t.typ)
  1318. expr(p, t[0], d)
  1319. var ifStmt = default(IfBuilder)
  1320. var scope = default(ScopeBuilder)
  1321. var isIf = false
  1322. if 1 < t.len and t[1].kind == nkExceptBranch:
  1323. startBlockWith(p):
  1324. isIf = true
  1325. ifStmt = initIfStmt(p.s(cpsStmts))
  1326. initElifBranch(p.s(cpsStmts), ifStmt, cUnlikely(cDeref("nimErr_")))
  1327. else:
  1328. startBlockWith(p):
  1329. scope = initScope(p.s(cpsStmts))
  1330. p.s(cpsStmts).addLabel("LA" & $lab & "_")
  1331. p.nestedTryStmts[^1].inExcept = true
  1332. var i = 1
  1333. var innerIfStmt = default(IfBuilder)
  1334. var innerScope = default(ScopeBuilder)
  1335. var innerIsIf = false
  1336. while (i < t.len) and (t[i].kind == nkExceptBranch):
  1337. inc p.labels
  1338. let nextExcept = p.labels
  1339. p.nestedTryStmts[^1].label = nextExcept
  1340. var isScope = false
  1341. # bug #4230: avoid false sharing between branches:
  1342. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1343. if t[i].len == 1:
  1344. # general except section:
  1345. startBlockWith(p):
  1346. if innerIsIf:
  1347. initElseBranch(p.s(cpsStmts), innerIfStmt)
  1348. else:
  1349. isScope = true
  1350. innerScope = initScope(p.s(cpsStmts))
  1351. # we handled the exception, remember this:
  1352. p.s(cpsStmts).addAssignment(cDeref("nimErr_"), NimFalse)
  1353. expr(p, t[i][0], d)
  1354. else:
  1355. if not innerIsIf:
  1356. innerIsIf = true
  1357. innerIfStmt = initIfStmt(p.s(cpsStmts))
  1358. var orExpr: Snippet = ""
  1359. for j in 0..<t[i].len - 1:
  1360. assert(t[i][j].kind == nkType)
  1361. var excVal = cCall(cgsymValue(p.module, "nimBorrowCurrentException"))
  1362. let member =
  1363. if p.module.compileToCpp:
  1364. derefField(excVal, "m_type")
  1365. else:
  1366. dotField(derefField(excVal, "Sup"), "m_type")
  1367. var branch: Snippet = ""
  1368. if optTinyRtti in p.config.globalOptions:
  1369. let checkFor = $getObjDepth(t[i][j].typ)
  1370. branch = cCall(cgsymValue(p.module, "isObjDisplayCheck"),
  1371. member,
  1372. checkFor,
  1373. $genDisplayElem(MD5Digest(hashType(t[i][j].typ, p.config))))
  1374. else:
  1375. let checkFor = genTypeInfoV1(p.module, t[i][j].typ, t[i][j].info)
  1376. branch = cCall(cgsymValue(p.module, "isObj"),
  1377. member,
  1378. checkFor)
  1379. if orExpr.len == 0:
  1380. orExpr = branch
  1381. else:
  1382. orExpr = cOp(Or, orExpr, branch)
  1383. startBlockWith(p):
  1384. initElifBranch(p.s(cpsStmts), innerIfStmt, orExpr)
  1385. # we handled the exception, remember this:
  1386. p.s(cpsStmts).addAssignment(cDeref("nimErr_"), NimFalse)
  1387. expr(p, t[i][^1], d)
  1388. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popCurrentException"))
  1389. p.s(cpsStmts).addLabel("LA" & $nextExcept & "_")
  1390. endBlockWith(p):
  1391. if isScope:
  1392. finishScope(p.s(cpsStmts), innerScope)
  1393. else:
  1394. finishBranch(p.s(cpsStmts), innerIfStmt)
  1395. inc(i)
  1396. if innerIsIf:
  1397. finishIfStmt(p.s(cpsStmts), innerIfStmt)
  1398. discard pop(p.nestedTryStmts)
  1399. endBlockWith(p):
  1400. if isIf:
  1401. finishBranch(p.s(cpsStmts), ifStmt)
  1402. finishIfStmt(p.s(cpsStmts), ifStmt)
  1403. else:
  1404. finishScope(p.s(cpsStmts), scope)
  1405. if i < t.len and t[i].kind == nkFinally:
  1406. var finallyScope: ScopeBuilder
  1407. startSimpleBlock(p, finallyScope)
  1408. if not bodyCanRaise(p, t[i][0]):
  1409. # this is an important optimization; most destroy blocks are detected not to raise an
  1410. # exception and so we help the C optimizer by not mutating nimErr_ pointlessly:
  1411. genStmts(p, t[i][0])
  1412. else:
  1413. # pretend we did handle the error for the safe execution of the 'finally' section:
  1414. p.procSec(cpsLocals).addVar(kind = Local, name = "oldNimErrFin" & $lab & "_", typ = NimBool)
  1415. p.s(cpsStmts).addAssignment("oldNimErrFin" & $lab & "_", cDeref("nimErr_"))
  1416. p.s(cpsStmts).addAssignment(cDeref("nimErr_"), NimFalse)
  1417. genStmts(p, t[i][0])
  1418. # this is correct for all these cases:
  1419. # 1. finally is run during ordinary control flow
  1420. # 2. finally is run after 'except' block handling: these however set the
  1421. # error back to nil.
  1422. # 3. finally is run for exception handling code without any 'except'
  1423. # handler present or only handlers that did not match.
  1424. p.s(cpsStmts).addAssignment(cDeref("nimErr_"), "oldNimErrFin" & $lab & "_")
  1425. endSimpleBlock(p, finallyScope)
  1426. raiseExit(p)
  1427. if hasExcept: inc p.withinTryWithExcept
  1428. proc genTrySetjmp(p: BProc, t: PNode, d: var TLoc) =
  1429. # code to generate:
  1430. #
  1431. # XXX: There should be a standard dispatch algorithm
  1432. # that's used both here and with multi-methods
  1433. #
  1434. # TSafePoint sp;
  1435. # pushSafePoint(&sp);
  1436. # sp.status = setjmp(sp.context);
  1437. # if (sp.status == 0) {
  1438. # myDiv(4, 9);
  1439. # popSafePoint();
  1440. # } else {
  1441. # popSafePoint();
  1442. # /* except DivisionByZero: */
  1443. # if (sp.status == DivisionByZero) {
  1444. # printf('Division by Zero\n');
  1445. # clearException();
  1446. # } else {
  1447. # clearException();
  1448. # }
  1449. # }
  1450. # {
  1451. # /* finally: */
  1452. # printf('fin!\n');
  1453. # }
  1454. # if (exception not cleared)
  1455. # propagateCurrentException();
  1456. #
  1457. if not isEmptyType(t.typ) and d.k == locNone:
  1458. d = getTemp(p, t.typ)
  1459. let quirkyExceptions = p.config.exc == excQuirky or
  1460. (t.kind == nkHiddenTryStmt and sfSystemModule in p.module.module.flags)
  1461. if not quirkyExceptions:
  1462. p.module.includeHeader("<setjmp.h>")
  1463. else:
  1464. p.flags.incl noSafePoints
  1465. genLineDir(p, t)
  1466. cgsym(p.module, "Exception")
  1467. var safePoint: Rope = ""
  1468. var nonQuirkyIf = default(IfBuilder)
  1469. if not quirkyExceptions:
  1470. safePoint = getTempName(p.module)
  1471. p.s(cpsLocals).addVar(name = safePoint, typ = cgsymValue(p.module, "TSafePoint"))
  1472. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "pushSafePoint"), cAddr(safePoint))
  1473. if isDefined(p.config, "nimStdSetjmp"):
  1474. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1475. p.s(cpsStmts).addCall("setjmp", dotField(safePoint, "context"))
  1476. elif isDefined(p.config, "nimSigSetjmp"):
  1477. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1478. p.s(cpsStmts).addCall("sigsetjmp", dotField(safePoint, "context"), cIntValue(0))
  1479. elif isDefined(p.config, "nimBuiltinSetjmp"):
  1480. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1481. p.s(cpsStmts).addCall("__builtin_setjmp", dotField(safePoint, "context"))
  1482. elif isDefined(p.config, "nimRawSetjmp"):
  1483. if isDefined(p.config, "mswindows"):
  1484. if isDefined(p.config, "vcc") or isDefined(p.config, "clangcl"):
  1485. # For the vcc compiler, use `setjmp()` with one argument.
  1486. # See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setjmp?view=msvc-170
  1487. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1488. p.s(cpsStmts).addCall("setjmp", dotField(safePoint, "context"))
  1489. else:
  1490. # The Windows `_setjmp()` takes two arguments, with the second being an
  1491. # undocumented buffer used by the SEH mechanism for stack unwinding.
  1492. # Mingw-w64 has been trying to get it right for years, but it's still
  1493. # prone to stack corruption during unwinding, so we disable that by setting
  1494. # it to NULL.
  1495. # More details: https://github.com/status-im/nimbus-eth2/issues/3121
  1496. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1497. p.s(cpsStmts).addCall("_setjmp", dotField(safePoint, "context"), cIntValue(0))
  1498. else:
  1499. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1500. p.s(cpsStmts).addCall("_setjmp", dotField(safePoint, "context"))
  1501. else:
  1502. p.s(cpsStmts).addFieldAssignmentWithValue(safePoint, "status"):
  1503. p.s(cpsStmts).addCall("setjmp", dotField(safePoint, "context"))
  1504. nonQuirkyIf = initIfStmt(p.s(cpsStmts))
  1505. initElifBranch(p.s(cpsStmts), nonQuirkyIf, removeSinglePar(
  1506. cOp(Equal, dotField(safePoint, "status"), cIntValue(0))))
  1507. let fin = if t[^1].kind == nkFinally: t[^1] else: nil
  1508. p.nestedTryStmts.add((fin, quirkyExceptions, 0.Natural))
  1509. expr(p, t[0], d)
  1510. var quirkyIf = default(IfBuilder)
  1511. var quirkyScope = default(ScopeBuilder)
  1512. var isScope = false
  1513. if not quirkyExceptions:
  1514. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popSafePoint"))
  1515. finishBranch(p.s(cpsStmts), nonQuirkyIf)
  1516. startBlockWith(p):
  1517. initElseBranch(p.s(cpsStmts), nonQuirkyIf)
  1518. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popSafePoint"))
  1519. genRestoreFrameAfterException(p)
  1520. elif 1 < t.len and t[1].kind == nkExceptBranch:
  1521. startBlockWith(p):
  1522. quirkyIf = initIfStmt(p.s(cpsStmts))
  1523. initElifBranch(p.s(cpsStmts), quirkyIf,
  1524. cCall(cgsymValue(p.module, "nimBorrowCurrentException")))
  1525. else:
  1526. isScope = true
  1527. startBlockWith(p):
  1528. quirkyScope = initScope(p.s(cpsStmts))
  1529. p.nestedTryStmts[^1].inExcept = true
  1530. var i = 1
  1531. var exceptIf = default(IfBuilder)
  1532. var exceptIfInited = false
  1533. while (i < t.len) and (t[i].kind == nkExceptBranch):
  1534. # bug #4230: avoid false sharing between branches:
  1535. if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
  1536. if t[i].len == 1:
  1537. # general except section:
  1538. var scope = default(ScopeBuilder)
  1539. startBlockWith(p):
  1540. if exceptIfInited:
  1541. initElseBranch(p.s(cpsStmts), exceptIf)
  1542. else:
  1543. scope = initScope(p.s(cpsStmts))
  1544. if not quirkyExceptions:
  1545. p.s(cpsStmts).addFieldAssignment(safePoint, "status", cIntValue(0))
  1546. expr(p, t[i][0], d)
  1547. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popCurrentException"))
  1548. endBlockWith(p):
  1549. if exceptIfInited:
  1550. finishBranch(p.s(cpsStmts), exceptIf)
  1551. else:
  1552. finishScope(p.s(cpsStmts), scope)
  1553. else:
  1554. var orExpr: Snippet = ""
  1555. for j in 0..<t[i].len - 1:
  1556. assert(t[i][j].kind == nkType)
  1557. var excVal = cCall(cgsymValue(p.module, "nimBorrowCurrentException"))
  1558. let member =
  1559. if p.module.compileToCpp:
  1560. derefField(excVal, "m_type")
  1561. else:
  1562. dotField(derefField(excVal, "Sup"), "m_type")
  1563. var branch: Snippet = ""
  1564. if optTinyRtti in p.config.globalOptions:
  1565. let checkFor = $getObjDepth(t[i][j].typ)
  1566. branch = cCall(cgsymValue(p.module, "isObjDisplayCheck"),
  1567. member,
  1568. checkFor,
  1569. $genDisplayElem(MD5Digest(hashType(t[i][j].typ, p.config))))
  1570. else:
  1571. let checkFor = genTypeInfoV1(p.module, t[i][j].typ, t[i][j].info)
  1572. branch = cCall(cgsymValue(p.module, "isObj"),
  1573. member,
  1574. checkFor)
  1575. if orExpr.len == 0:
  1576. orExpr = branch
  1577. else:
  1578. orExpr = cOp(Or, orExpr, branch)
  1579. if not exceptIfInited:
  1580. exceptIf = initIfStmt(p.s(cpsStmts))
  1581. exceptIfInited = true
  1582. startBlockWith(p):
  1583. initElifBranch(p.s(cpsStmts), exceptIf, orExpr)
  1584. if not quirkyExceptions:
  1585. p.s(cpsStmts).addFieldAssignment(safePoint, "status", cIntValue(0))
  1586. expr(p, t[i][^1], d)
  1587. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "popCurrentException"))
  1588. endBlockWith(p):
  1589. finishBranch(p.s(cpsStmts), exceptIf)
  1590. inc(i)
  1591. if exceptIfInited:
  1592. finishIfStmt(p.s(cpsStmts), exceptIf)
  1593. discard pop(p.nestedTryStmts)
  1594. endBlockWith(p):
  1595. # end of else block
  1596. if not quirkyExceptions:
  1597. finishBranch(p.s(cpsStmts), nonQuirkyIf)
  1598. finishIfStmt(p.s(cpsStmts), nonQuirkyIf)
  1599. elif isScope:
  1600. finishScope(p.s(cpsStmts), quirkyScope)
  1601. else:
  1602. finishBranch(p.s(cpsStmts), quirkyIf)
  1603. finishIfStmt(p.s(cpsStmts), quirkyIf)
  1604. if i < t.len and t[i].kind == nkFinally:
  1605. p.finallySafePoints.add(safePoint)
  1606. var finallyScope: ScopeBuilder
  1607. startSimpleBlock(p, finallyScope)
  1608. genStmts(p, t[i][0])
  1609. # pretend we handled the exception in a 'finally' so that we don't
  1610. # re-raise the unhandled one but instead keep the old one (it was
  1611. # not popped either):
  1612. if not quirkyExceptions and getCompilerProc(p.module.g.graph, "nimLeaveFinally") != nil:
  1613. p.s(cpsStmts).addSingleIfStmt(
  1614. cOp(NotEqual,
  1615. dotField(safePoint, "status"),
  1616. cIntValue(0))):
  1617. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimLeaveFinally"))
  1618. endSimpleBlock(p, finallyScope)
  1619. discard pop(p.finallySafePoints)
  1620. if not quirkyExceptions:
  1621. p.s(cpsStmts).addSingleIfStmt(
  1622. cOp(NotEqual,
  1623. dotField(safePoint, "status"),
  1624. cIntValue(0))):
  1625. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "reraiseException"))
  1626. proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false; result: var Rope) =
  1627. var res = ""
  1628. let offset =
  1629. if isAsmStmt: 1 # first son is pragmas
  1630. else: 0
  1631. for i in offset..<t.len:
  1632. let it = t[i]
  1633. case it.kind
  1634. of nkStrLit..nkTripleStrLit:
  1635. res.add(it.strVal)
  1636. of nkSym:
  1637. var sym = it.sym
  1638. if sym.kind in {skProc, skFunc, skIterator, skMethod}:
  1639. var a: TLoc = initLocExpr(p, it)
  1640. res.add($rdLoc(a))
  1641. elif sym.kind == skType:
  1642. res.add($getTypeDesc(p.module, sym.typ))
  1643. else:
  1644. discard getTypeDesc(p.module, skipTypes(sym.typ, abstractPtrs))
  1645. fillBackendName(p.module, sym)
  1646. res.add($sym.loc.snippet)
  1647. of nkTypeOfExpr:
  1648. res.add($getTypeDesc(p.module, it.typ))
  1649. else:
  1650. discard getTypeDesc(p.module, skipTypes(it.typ, abstractPtrs))
  1651. var a: TLoc = initLocExpr(p, it)
  1652. res.add($a.rdLoc)
  1653. if isAsmStmt and hasGnuAsm in CC[p.config.cCompiler].props:
  1654. for x in splitLines(res):
  1655. var j = 0
  1656. while j < x.len and x[j] in {' ', '\t'}: inc(j)
  1657. if j < x.len:
  1658. if x[j] in {'"', ':'}:
  1659. # don't modify the line if already in quotes or
  1660. # some clobber register list:
  1661. result.add(x); result.add("\L")
  1662. else:
  1663. # ignore empty lines
  1664. result.add("\"")
  1665. result.add(x.replace("\"", "\\\""))
  1666. result.add("\\n\"\n")
  1667. else:
  1668. res.add("\L")
  1669. result.add res.rope
  1670. proc genAsmStmt(p: BProc, t: PNode) =
  1671. assert(t.kind == nkAsmStmt)
  1672. genLineDir(p, t)
  1673. var s = newRopeAppender()
  1674. var asmSyntax = ""
  1675. if (let p = t[0]; p.kind == nkPragma):
  1676. for i in p:
  1677. if whichPragma(i) == wAsmSyntax:
  1678. asmSyntax = i[1].strVal
  1679. if asmSyntax != "" and
  1680. not (
  1681. asmSyntax == "gcc" and hasGnuAsm in CC[p.config.cCompiler].props or
  1682. asmSyntax == "vcc" and hasGnuAsm notin CC[p.config.cCompiler].props):
  1683. localError(
  1684. p.config, t.info,
  1685. "Your compiler does not support the specified inline assembler")
  1686. genAsmOrEmitStmt(p, t, isAsmStmt=true, s)
  1687. # see bug #2362, "top level asm statements" seem to be a mis-feature
  1688. # but even if we don't do this, the example in #2362 cannot possibly
  1689. # work:
  1690. if p.prc == nil:
  1691. # top level asm statement?
  1692. p.module.s[cfsProcHeaders].add runtimeFormat(CC[p.config.cCompiler].asmStmtFrmt, [s])
  1693. else:
  1694. addIndent p, p.s(cpsStmts)
  1695. p.s(cpsStmts).add runtimeFormat(CC[p.config.cCompiler].asmStmtFrmt, [s])
  1696. proc determineSection(n: PNode): TCFileSection =
  1697. result = cfsProcHeaders
  1698. if n.len >= 1 and n[0].kind in {nkStrLit..nkTripleStrLit}:
  1699. let sec = n[0].strVal
  1700. if sec.startsWith("/*TYPESECTION*/"): result = cfsForwardTypes # TODO WORKAROUND
  1701. elif sec.startsWith("/*VARSECTION*/"): result = cfsVars
  1702. elif sec.startsWith("/*INCLUDESECTION*/"): result = cfsHeaders
  1703. proc genEmit(p: BProc, t: PNode) =
  1704. var s = newRopeAppender()
  1705. genAsmOrEmitStmt(p, t[1], false, s)
  1706. if p.prc == nil:
  1707. # top level emit pragma?
  1708. let section = determineSection(t[1])
  1709. genCLineDir(p.module.s[section], t.info, p.config)
  1710. p.module.s[section].add(s)
  1711. else:
  1712. genLineDir(p, t)
  1713. line(p, cpsStmts, s)
  1714. proc genPragma(p: BProc, n: PNode) =
  1715. for i in 0..<n.len:
  1716. let it = n[i]
  1717. case whichPragma(it)
  1718. of wEmit: genEmit(p, it)
  1719. of wPush:
  1720. processPushBackendOption(p.config, p.optionsStack, p.options, n, i+1)
  1721. of wPop:
  1722. processPopBackendOption(p.config, p.optionsStack, p.options)
  1723. else: discard
  1724. proc genDiscriminantCheck(p: BProc, a, tmp: TLoc, objtype: PType,
  1725. field: PSym) =
  1726. var t = skipTypes(objtype, abstractVar)
  1727. assert t.kind == tyObject
  1728. discard genTypeInfoV1(p.module, t, a.lode.info)
  1729. if not containsOrIncl(p.module.declaredThings, field.id):
  1730. p.module.s[cfsVars].addDeclWithVisibility(Extern):
  1731. discriminatorTableDecl(p.module, t, field, p.module.s[cfsVars])
  1732. let lit = cIntLiteral(toInt64(lengthOrd(p.config, field.typ))+1)
  1733. let ra = rdLoc(a)
  1734. let rtmp = rdLoc(tmp)
  1735. let dn = discriminatorTableName(p.module, t, field)
  1736. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "FieldDiscriminantCheck"),
  1737. cCast(NimInt, cCast(NimUint, ra)),
  1738. cCast(NimInt, cCast(NimUint, rtmp)),
  1739. dn,
  1740. lit)
  1741. if p.config.exc == excGoto:
  1742. raiseExit(p)
  1743. when false:
  1744. proc genCaseObjDiscMapping(p: BProc, e: PNode, t: PType, field: PSym; d: var TLoc) =
  1745. const ObjDiscMappingProcSlot = -5
  1746. var theProc: PSym = nil
  1747. for idx, p in items(t.methods):
  1748. if idx == ObjDiscMappingProcSlot:
  1749. theProc = p
  1750. break
  1751. if theProc == nil:
  1752. theProc = genCaseObjDiscMapping(t, field, e.info, p.module.g.graph, p.module.idgen)
  1753. t.methods.add((ObjDiscMappingProcSlot, theProc))
  1754. var call = newNodeIT(nkCall, e.info, getSysType(p.module.g.graph, e.info, tyUInt8))
  1755. call.add newSymNode(theProc)
  1756. call.add e
  1757. expr(p, call, d)
  1758. proc asgnFieldDiscriminant(p: BProc, e: PNode) =
  1759. var dotExpr = e[0]
  1760. if dotExpr.kind == nkCheckedFieldExpr: dotExpr = dotExpr[0]
  1761. var a = initLocExpr(p, e[0])
  1762. var tmp: TLoc = getTemp(p, a.t)
  1763. expr(p, e[1], tmp)
  1764. if p.inUncheckedAssignSection == 0:
  1765. let field = dotExpr[1].sym
  1766. genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field)
  1767. message(p.config, e.info, warnCaseTransition)
  1768. genAssignment(p, a, tmp, {})
  1769. proc genAsgn(p: BProc, e: PNode, fastAsgn: bool) =
  1770. if e[0].kind == nkSym and sfGoto in e[0].sym.flags:
  1771. genLineDir(p, e)
  1772. genGotoVar(p, e[1])
  1773. elif optFieldCheck in p.options and isDiscriminantField(e[0]):
  1774. genLineDir(p, e)
  1775. asgnFieldDiscriminant(p, e)
  1776. else:
  1777. let le = e[0]
  1778. let ri = e[1]
  1779. var a: TLoc = initLoc(locNone, le, OnUnknown)
  1780. discard getTypeDesc(p.module, le.typ.skipTypes(skipPtrs), dkVar)
  1781. a.flags.incl(lfEnforceDeref)
  1782. a.flags.incl(lfPrepareForMutation)
  1783. genLineDir(p, le) # it can be a nkBracketExpr, which may raise
  1784. expr(p, le, a)
  1785. a.flags.excl(lfPrepareForMutation)
  1786. if fastAsgn: incl(a.flags, lfNoDeepCopy)
  1787. assert(a.t != nil)
  1788. genLineDir(p, ri)
  1789. loadInto(p, le, ri, a)
  1790. proc genStmts(p: BProc, t: PNode) =
  1791. var a: TLoc = default(TLoc)
  1792. let isPush = p.config.hasHint(hintExtendedContext)
  1793. if isPush: pushInfoContext(p.config, t.info)
  1794. expr(p, t, a)
  1795. if isPush: popInfoContext(p.config)
  1796. internalAssert p.config, a.k in {locNone, locTemp, locLocalVar, locExpr}