ccgstmts.nim 41 KB

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