ccgstmts.nim 61 KB

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