lowerings.nim 12 KB

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