ccgstmts.nim 55 KB

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