ccgstmts.nim 45 KB

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