ccgstmts.nim 56 KB

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