lowerings.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. ## This module implements common simple lowerings.
  10. const
  11. genPrefix* = ":tmp" # prefix for generated names
  12. import ast, astalgo, types, idents, magicsys, msgs, options, modulegraphs,
  13. lineinfos
  14. proc newDeref*(n: PNode): PNode {.inline.} =
  15. result = newNodeIT(nkHiddenDeref, n.info, n.typ[0])
  16. result.add n
  17. proc newTupleAccess*(g: ModuleGraph; tup: PNode, i: int): PNode =
  18. if tup.kind == nkHiddenAddr:
  19. result = newNodeIT(nkHiddenAddr, tup.info, tup.typ.skipTypes(abstractInst+{tyPtr, tyVar, tyLent}))
  20. result.add newNodeIT(nkBracketExpr, tup.info, tup.typ.skipTypes(abstractInst+{tyPtr, tyVar, tyLent})[i])
  21. result[0].add tup[0]
  22. var lit = newNodeIT(nkIntLit, tup.info, getSysType(g, tup.info, tyInt))
  23. lit.intVal = i
  24. result[0].add lit
  25. else:
  26. result = newNodeIT(nkBracketExpr, tup.info, tup.typ.skipTypes(
  27. abstractInst)[i])
  28. result.add copyTree(tup)
  29. var lit = newNodeIT(nkIntLit, tup.info, getSysType(g, tup.info, tyInt))
  30. lit.intVal = i
  31. result.add lit
  32. proc addVar*(father, v: PNode) =
  33. var vpart = newNodeI(nkIdentDefs, v.info, 3)
  34. vpart[0] = v
  35. vpart[1] = newNodeI(nkEmpty, v.info)
  36. vpart[2] = vpart[1]
  37. father.add vpart
  38. proc addVar*(father, v, value: PNode) =
  39. var vpart = newNodeI(nkIdentDefs, v.info, 3)
  40. vpart[0] = v
  41. vpart[1] = newNodeI(nkEmpty, v.info)
  42. vpart[2] = value
  43. father.add vpart
  44. proc newAsgnStmt*(le, ri: PNode): PNode =
  45. result = newNodeI(nkAsgn, le.info, 2)
  46. result[0] = le
  47. result[1] = ri
  48. proc newFastAsgnStmt*(le, ri: PNode): PNode =
  49. result = newNodeI(nkFastAsgn, le.info, 2)
  50. result[0] = le
  51. result[1] = ri
  52. proc newFastMoveStmt*(g: ModuleGraph, le, ri: PNode): PNode =
  53. result = newNodeI(nkFastAsgn, le.info, 2)
  54. result[0] = le
  55. result[1] = newNodeIT(nkCall, ri.info, ri.typ)
  56. result[1].add newSymNode(getSysMagic(g, ri.info, "move", mMove))
  57. result[1].add ri
  58. proc lowerTupleUnpacking*(g: ModuleGraph; n: PNode; idgen: IdGenerator; owner: PSym): PNode =
  59. assert n.kind == nkVarTuple
  60. let value = n.lastSon
  61. result = newNodeI(nkStmtList, n.info)
  62. var temp = newSym(skTemp, getIdent(g.cache, genPrefix), nextId(idgen),
  63. owner, value.info, g.config.options)
  64. temp.typ = skipTypes(value.typ, abstractInst)
  65. incl(temp.flags, sfFromGeneric)
  66. var v = newNodeI(nkVarSection, value.info)
  67. let tempAsNode = newSymNode(temp)
  68. v.addVar(tempAsNode, value)
  69. result.add(v)
  70. for i in 0..<n.len-2:
  71. let val = newTupleAccess(g, tempAsNode, i)
  72. if n[i].kind == nkSym: v.addVar(n[i], val)
  73. else: result.add newAsgnStmt(n[i], val)
  74. proc evalOnce*(g: ModuleGraph; value: PNode; idgen: IdGenerator; owner: PSym): PNode =
  75. ## Turns (value) into (let tmp = value; tmp) so that 'value' can be re-used
  76. ## freely, multiple times. This is frequently required and such a builtin would also be
  77. ## handy to have in macros.nim. The value that can be reused is 'result.lastSon'!
  78. result = newNodeIT(nkStmtListExpr, value.info, value.typ)
  79. var temp = newSym(skTemp, getIdent(g.cache, genPrefix), nextId(idgen),
  80. owner, value.info, g.config.options)
  81. temp.typ = skipTypes(value.typ, abstractInst)
  82. incl(temp.flags, sfFromGeneric)
  83. var v = newNodeI(nkLetSection, value.info)
  84. let tempAsNode = newSymNode(temp)
  85. v.addVar(tempAsNode)
  86. result.add(v)
  87. result.add newAsgnStmt(tempAsNode, value)
  88. result.add tempAsNode
  89. proc newTupleAccessRaw*(tup: PNode, i: int): PNode =
  90. result = newNodeI(nkBracketExpr, tup.info)
  91. result.add copyTree(tup)
  92. var lit = newNodeI(nkIntLit, tup.info)
  93. lit.intVal = i
  94. result.add lit
  95. proc newTryFinally*(body, final: PNode): PNode =
  96. result = newTree(nkHiddenTryStmt, body, newTree(nkFinally, final))
  97. proc lowerTupleUnpackingForAsgn*(g: ModuleGraph; n: PNode; idgen: IdGenerator; owner: PSym): PNode =
  98. let value = n.lastSon
  99. result = newNodeI(nkStmtList, n.info)
  100. var temp = newSym(skTemp, getIdent(g.cache, "_"), nextId(idgen), owner, value.info, owner.options)
  101. var v = newNodeI(nkLetSection, value.info)
  102. let tempAsNode = newSymNode(temp) #newIdentNode(getIdent(genPrefix & $temp.id), value.info)
  103. var vpart = newNodeI(nkIdentDefs, tempAsNode.info, 3)
  104. vpart[0] = tempAsNode
  105. vpart[1] = newNodeI(nkEmpty, value.info)
  106. vpart[2] = value
  107. v.add vpart
  108. result.add(v)
  109. let lhs = n[0]
  110. for i in 0..<lhs.len:
  111. result.add newAsgnStmt(lhs[i], newTupleAccessRaw(tempAsNode, i))
  112. proc lowerSwap*(g: ModuleGraph; n: PNode; idgen: IdGenerator; owner: PSym): PNode =
  113. result = newNodeI(nkStmtList, n.info)
  114. # note: cannot use 'skTemp' here cause we really need the copy for the VM :-(
  115. var temp = newSym(skVar, getIdent(g.cache, genPrefix), nextId(idgen), owner, n.info, owner.options)
  116. temp.typ = n[1].typ
  117. incl(temp.flags, sfFromGeneric)
  118. incl(temp.flags, sfGenSym)
  119. var v = newNodeI(nkVarSection, n.info)
  120. let tempAsNode = newSymNode(temp)
  121. var vpart = newNodeI(nkIdentDefs, v.info, 3)
  122. vpart[0] = tempAsNode
  123. vpart[1] = newNodeI(nkEmpty, v.info)
  124. vpart[2] = n[1]
  125. v.add vpart
  126. result.add(v)
  127. result.add newFastAsgnStmt(n[1], n[2])
  128. result.add newFastAsgnStmt(n[2], tempAsNode)
  129. proc createObj*(g: ModuleGraph; idgen: IdGenerator; owner: PSym, info: TLineInfo; final=true): PType =
  130. result = newType(tyObject, nextId(idgen), owner)
  131. if final:
  132. rawAddSon(result, nil)
  133. incl result.flags, tfFinal
  134. else:
  135. rawAddSon(result, getCompilerProc(g, "RootObj").typ)
  136. result.n = newNodeI(nkRecList, info)
  137. let s = newSym(skType, getIdent(g.cache, "Env_" & toFilename(g.config, info) & "_" & $owner.name.s),
  138. nextId(idgen),
  139. owner, info, owner.options)
  140. incl s.flags, sfAnon
  141. s.typ = result
  142. result.sym = s
  143. template fieldCheck {.dirty.} =
  144. when false:
  145. if tfCheckedForDestructor in obj.flags:
  146. echo "missed field ", field.name.s
  147. writeStackTrace()
  148. proc rawAddField*(obj: PType; field: PSym) =
  149. assert field.kind == skField
  150. field.position = obj.n.len
  151. obj.n.add newSymNode(field)
  152. propagateToOwner(obj, field.typ)
  153. fieldCheck()
  154. proc rawIndirectAccess*(a: PNode; field: PSym; info: TLineInfo): PNode =
  155. # returns a[].field as a node
  156. assert field.kind == skField
  157. var deref = newNodeI(nkHiddenDeref, info)
  158. deref.typ = a.typ.skipTypes(abstractInst)[0]
  159. deref.add a
  160. result = newNodeI(nkDotExpr, info)
  161. result.add deref
  162. result.add newSymNode(field)
  163. result.typ = field.typ
  164. proc rawDirectAccess*(obj, field: PSym): PNode =
  165. # returns a.field as a node
  166. assert field.kind == skField
  167. result = newNodeI(nkDotExpr, field.info)
  168. result.add newSymNode(obj)
  169. result.add newSymNode(field)
  170. result.typ = field.typ
  171. proc lookupInRecord(n: PNode, id: ItemId): PSym =
  172. result = nil
  173. case n.kind
  174. of nkRecList:
  175. for i in 0..<n.len:
  176. result = lookupInRecord(n[i], id)
  177. if result != nil: return
  178. of nkRecCase:
  179. if n[0].kind != nkSym: return
  180. result = lookupInRecord(n[0], id)
  181. if result != nil: return
  182. for i in 1..<n.len:
  183. case n[i].kind
  184. of nkOfBranch, nkElse:
  185. result = lookupInRecord(lastSon(n[i]), id)
  186. if result != nil: return
  187. else: discard
  188. of nkSym:
  189. if n.sym.itemId.module == id.module and n.sym.itemId.item == -abs(id.item): result = n.sym
  190. else: discard
  191. proc addField*(obj: PType; s: PSym; cache: IdentCache; idgen: IdGenerator) =
  192. # because of 'gensym' support, we have to mangle the name with its ID.
  193. # This is hacky but the clean solution is much more complex than it looks.
  194. var field = newSym(skField, getIdent(cache, s.name.s & $obj.n.len),
  195. nextId(idgen), s.owner, s.info, s.options)
  196. field.itemId = ItemId(module: s.itemId.module, item: -s.itemId.item)
  197. let t = skipIntLit(s.typ, idgen)
  198. field.typ = t
  199. assert t.kind != tyTyped
  200. propagateToOwner(obj, t)
  201. field.position = obj.n.len
  202. field.flags = s.flags * {sfCursor}
  203. obj.n.add newSymNode(field)
  204. fieldCheck()
  205. proc addUniqueField*(obj: PType; s: PSym; cache: IdentCache; idgen: IdGenerator): PSym {.discardable.} =
  206. result = lookupInRecord(obj.n, s.itemId)
  207. if result == nil:
  208. var field = newSym(skField, getIdent(cache, s.name.s & $obj.n.len), nextId(idgen),
  209. s.owner, s.info, s.options)
  210. field.itemId = ItemId(module: s.itemId.module, item: -s.itemId.item)
  211. let t = skipIntLit(s.typ, idgen)
  212. field.typ = t
  213. assert t.kind != tyTyped
  214. propagateToOwner(obj, t)
  215. field.position = obj.n.len
  216. obj.n.add newSymNode(field)
  217. result = field
  218. proc newDotExpr*(obj, b: PSym): PNode =
  219. result = newNodeI(nkDotExpr, obj.info)
  220. let field = lookupInRecord(obj.typ.n, b.itemId)
  221. assert field != nil, b.name.s
  222. result.add newSymNode(obj)
  223. result.add newSymNode(field)
  224. result.typ = field.typ
  225. proc indirectAccess*(a: PNode, b: ItemId, info: TLineInfo): PNode =
  226. # returns a[].b as a node
  227. var deref = newNodeI(nkHiddenDeref, info)
  228. deref.typ = a.typ.skipTypes(abstractInst)[0]
  229. var t = deref.typ.skipTypes(abstractInst)
  230. var field: PSym
  231. while true:
  232. assert t.kind == tyObject
  233. field = lookupInRecord(t.n, b)
  234. if field != nil: break
  235. t = t[0]
  236. if t == nil: break
  237. t = t.skipTypes(skipPtrs)
  238. #if field == nil:
  239. # echo "FIELD ", b
  240. # debug deref.typ
  241. assert field != nil
  242. deref.add a
  243. result = newNodeI(nkDotExpr, info)
  244. result.add deref
  245. result.add newSymNode(field)
  246. result.typ = field.typ
  247. proc indirectAccess*(a: PNode, b: string, info: TLineInfo; cache: IdentCache): PNode =
  248. # returns a[].b as a node
  249. var deref = newNodeI(nkHiddenDeref, info)
  250. deref.typ = a.typ.skipTypes(abstractInst)[0]
  251. var t = deref.typ.skipTypes(abstractInst)
  252. var field: PSym
  253. let bb = getIdent(cache, b)
  254. while true:
  255. assert t.kind == tyObject
  256. field = getSymFromList(t.n, bb)
  257. if field != nil: break
  258. t = t[0]
  259. if t == nil: break
  260. t = t.skipTypes(skipPtrs)
  261. #if field == nil:
  262. # echo "FIELD ", b
  263. # debug deref.typ
  264. assert field != nil
  265. deref.add a
  266. result = newNodeI(nkDotExpr, info)
  267. result.add deref
  268. result.add newSymNode(field)
  269. result.typ = field.typ
  270. proc getFieldFromObj*(t: PType; v: PSym): PSym =
  271. assert v.kind != skField
  272. var t = t
  273. while true:
  274. assert t.kind == tyObject
  275. result = lookupInRecord(t.n, v.itemId)
  276. if result != nil: break
  277. t = t[0]
  278. if t == nil: break
  279. t = t.skipTypes(skipPtrs)
  280. proc indirectAccess*(a: PNode, b: PSym, info: TLineInfo): PNode =
  281. # returns a[].b as a node
  282. result = indirectAccess(a, b.itemId, info)
  283. proc indirectAccess*(a, b: PSym, info: TLineInfo): PNode =
  284. result = indirectAccess(newSymNode(a), b, info)
  285. proc genAddrOf*(n: PNode; idgen: IdGenerator; typeKind = tyPtr): PNode =
  286. result = newNodeI(nkAddr, n.info, 1)
  287. result[0] = n
  288. result.typ = newType(typeKind, nextId(idgen), n.typ.owner)
  289. result.typ.rawAddSon(n.typ)
  290. proc genDeref*(n: PNode; k = nkHiddenDeref): PNode =
  291. result = newNodeIT(k, n.info,
  292. n.typ.skipTypes(abstractInst)[0])
  293. result.add n
  294. proc callCodegenProc*(g: ModuleGraph; name: string;
  295. info: TLineInfo = unknownLineInfo;
  296. arg1, arg2, arg3, optionalArgs: PNode = nil): PNode =
  297. result = newNodeI(nkCall, info)
  298. let sym = magicsys.getCompilerProc(g, name)
  299. if sym == nil:
  300. localError(g.config, info, "system module needs: " & name)
  301. else:
  302. result.add newSymNode(sym)
  303. if arg1 != nil: result.add arg1
  304. if arg2 != nil: result.add arg2
  305. if arg3 != nil: result.add arg3
  306. if optionalArgs != nil:
  307. for i in 1..<optionalArgs.len-2:
  308. result.add optionalArgs[i]
  309. result.typ = sym.typ[0]
  310. proc newIntLit*(g: ModuleGraph; info: TLineInfo; value: BiggestInt): PNode =
  311. result = nkIntLit.newIntNode(value)
  312. result.typ = getSysType(g, info, tyInt)
  313. proc genHigh*(g: ModuleGraph; n: PNode): PNode =
  314. if skipTypes(n.typ, abstractVar).kind == tyArray:
  315. result = newIntLit(g, n.info, toInt64(lastOrd(g.config, skipTypes(n.typ, abstractVar))))
  316. else:
  317. result = newNodeI(nkCall, n.info, 2)
  318. result.typ = getSysType(g, n.info, tyInt)
  319. result[0] = newSymNode(getSysMagic(g, n.info, "high", mHigh))
  320. result[1] = n
  321. proc genLen*(g: ModuleGraph; n: PNode): PNode =
  322. if skipTypes(n.typ, abstractVar).kind == tyArray:
  323. result = newIntLit(g, n.info, toInt64(lastOrd(g.config, skipTypes(n.typ, abstractVar)) + 1))
  324. else:
  325. result = newNodeI(nkCall, n.info, 2)
  326. result.typ = getSysType(g, n.info, tyInt)
  327. result[0] = newSymNode(getSysMagic(g, n.info, "len", mLengthSeq))
  328. result[1] = n