ccgstmts.nim 38 KB

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