lowerings.nim 12 KB

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