vmdef.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains the type definitions for the new evaluation engine.
  10. ## An instruction is 1-3 int32s in memory, it is a register based VM.
  11. import tables
  12. import ast, idents, options, modulegraphs, lineinfos
  13. type TInstrType* = uint64
  14. const
  15. regOBits = 8 # Opcode
  16. regABits = 16
  17. regBBits = 16
  18. regCBits = 16
  19. regBxBits = 24
  20. byteExcess* = 128 # we use excess-K for immediates
  21. # Calculate register shifts, masks and ranges
  22. const
  23. regOShift* = 0.TInstrType
  24. regAShift* = (regOShift + regOBits)
  25. regBShift* = (regAShift + regABits)
  26. regCShift* = (regBShift + regBBits)
  27. regBxShift* = (regAShift + regABits)
  28. regOMask* = ((1.TInstrType shl regOBits) - 1)
  29. regAMask* = ((1.TInstrType shl regABits) - 1)
  30. regBMask* = ((1.TInstrType shl regBBits) - 1)
  31. regCMask* = ((1.TInstrType shl regCBits) - 1)
  32. regBxMask* = ((1.TInstrType shl regBxBits) - 1)
  33. wordExcess* = 1 shl (regBxBits-1)
  34. regBxMin* = -wordExcess+1
  35. regBxMax* = wordExcess-1
  36. type
  37. TRegister* = range[0..regAMask.int]
  38. TDest* = range[-1..regAMask.int]
  39. TInstr* = distinct TInstrType
  40. TOpcode* = enum
  41. opcEof, # end of code
  42. opcRet, # return
  43. opcYldYoid, # yield with no value
  44. opcYldVal, # yield with a value
  45. opcAsgnInt,
  46. opcAsgnFloat,
  47. opcAsgnRef,
  48. opcAsgnComplex,
  49. opcCastIntToFloat32, # int and float must be of the same byte size
  50. opcCastIntToFloat64, # int and float must be of the same byte size
  51. opcCastFloatToInt32, # int and float must be of the same byte size
  52. opcCastFloatToInt64, # int and float must be of the same byte size
  53. opcCastPtrToInt,
  54. opcCastIntToPtr,
  55. opcFastAsgnComplex,
  56. opcNodeToReg,
  57. opcLdArr, # a = b[c]
  58. opcLdArrAddr, # a = addr(b[c])
  59. opcWrArr, # a[b] = c
  60. opcLdObj, # a = b.c
  61. opcLdObjAddr, # a = addr(b.c)
  62. opcWrObj, # a.b = c
  63. opcAddrReg,
  64. opcAddrNode,
  65. opcLdDeref,
  66. opcWrDeref,
  67. opcWrStrIdx,
  68. opcLdStrIdx, # a = b[c]
  69. opcLdStrIdxAddr, # a = addr(b[c])
  70. opcSlice, # toOpenArray(collection, left, right)
  71. opcAddInt,
  72. opcAddImmInt,
  73. opcSubInt,
  74. opcSubImmInt,
  75. opcLenSeq,
  76. opcLenStr,
  77. opcLenCstring,
  78. opcIncl, opcInclRange, opcExcl, opcCard, opcMulInt, opcDivInt, opcModInt,
  79. opcAddFloat, opcSubFloat, opcMulFloat, opcDivFloat,
  80. opcShrInt, opcShlInt, opcAshrInt,
  81. opcBitandInt, opcBitorInt, opcBitxorInt, opcAddu, opcSubu, opcMulu,
  82. opcDivu, opcModu, opcEqInt, opcLeInt, opcLtInt, opcEqFloat,
  83. opcLeFloat, opcLtFloat, opcLeu, opcLtu,
  84. opcEqRef, opcEqNimNode, opcSameNodeType,
  85. opcXor, opcNot, opcUnaryMinusInt, opcUnaryMinusFloat, opcBitnotInt,
  86. opcEqStr, opcLeStr, opcLtStr, opcEqSet, opcLeSet, opcLtSet,
  87. opcMulSet, opcPlusSet, opcMinusSet, opcConcatStr,
  88. opcContainsSet, opcRepr, opcSetLenStr, opcSetLenSeq,
  89. opcIsNil, opcOf, opcIs,
  90. opcParseFloat, opcConv, opcCast,
  91. opcQuit, opcInvalidField,
  92. opcNarrowS, opcNarrowU,
  93. opcSignExtend,
  94. opcAddStrCh,
  95. opcAddStrStr,
  96. opcAddSeqElem,
  97. opcRangeChck,
  98. opcNAdd,
  99. opcNAddMultiple,
  100. opcNKind,
  101. opcNSymKind,
  102. opcNIntVal,
  103. opcNFloatVal,
  104. opcNSymbol,
  105. opcNIdent,
  106. opcNGetType,
  107. opcNStrVal,
  108. opcNSigHash,
  109. opcNGetSize,
  110. opcNSetIntVal,
  111. opcNSetFloatVal, opcNSetSymbol, opcNSetIdent, opcNSetType, opcNSetStrVal,
  112. opcNNewNimNode, opcNCopyNimNode, opcNCopyNimTree, opcNDel, opcGenSym,
  113. opcNccValue, opcNccInc, opcNcsAdd, opcNcsIncl, opcNcsLen, opcNcsAt,
  114. opcNctPut, opcNctLen, opcNctGet, opcNctHasNext, opcNctNext, opcNodeId,
  115. opcSlurp,
  116. opcGorge,
  117. opcParseExprToAst,
  118. opcParseStmtToAst,
  119. opcQueryErrorFlag,
  120. opcNError,
  121. opcNWarning,
  122. opcNHint,
  123. opcNGetLineInfo, opcNSetLineInfo,
  124. opcEqIdent,
  125. opcStrToIdent,
  126. opcGetImpl,
  127. opcGetImplTransf
  128. opcEcho,
  129. opcIndCall, # dest = call regStart, n; where regStart = fn, arg1, ...
  130. opcIndCallAsgn, # dest = call regStart, n; where regStart = fn, arg1, ...
  131. opcRaise,
  132. opcNChild,
  133. opcNSetChild,
  134. opcCallSite,
  135. opcNewStr,
  136. opcTJmp, # jump Bx if A != 0
  137. opcFJmp, # jump Bx if A == 0
  138. opcJmp, # jump Bx
  139. opcJmpBack, # jump Bx; resulting from a while loop
  140. opcBranch, # branch for 'case'
  141. opcTry,
  142. opcExcept,
  143. opcFinally,
  144. opcFinallyEnd,
  145. opcNew,
  146. opcNewSeq,
  147. opcLdNull, # dest = nullvalue(types[Bx])
  148. opcLdNullReg,
  149. opcLdConst, # dest = constants[Bx]
  150. opcAsgnConst, # dest = copy(constants[Bx])
  151. opcLdGlobal, # dest = globals[Bx]
  152. opcLdGlobalAddr, # dest = addr(globals[Bx])
  153. opcLdGlobalDerefFFI, # dest = globals[Bx][]
  154. opcLdGlobalAddrDerefFFI, # globals[Bx][] = ...
  155. opcLdImmInt, # dest = immediate value
  156. opcNBindSym, opcNDynBindSym,
  157. opcSetType, # dest.typ = types[Bx]
  158. opcTypeTrait,
  159. opcSymOwner,
  160. opcSymIsInstantiationOf
  161. TBlock* = object
  162. label*: PSym
  163. fixups*: seq[TPosition]
  164. TEvalMode* = enum ## reason for evaluation
  165. emRepl, ## evaluate because in REPL mode
  166. emConst, ## evaluate for 'const' according to spec
  167. emOptimize, ## evaluate for optimization purposes (same as
  168. ## emConst?)
  169. emStaticExpr, ## evaluate for enforced compile time eval
  170. ## ('static' context)
  171. emStaticStmt ## 'static' as an expression
  172. TSandboxFlag* = enum ## what the evaluation engine should allow
  173. allowCast, ## allow unsafe language feature: 'cast'
  174. allowInfiniteLoops ## allow endless loops
  175. TSandboxFlags* = set[TSandboxFlag]
  176. TSlotKind* = enum # We try to re-use slots in a smart way to
  177. # minimize allocations; however the VM supports arbitrary
  178. # temporary slot usage. This is required for the parameter
  179. # passing implementation.
  180. slotEmpty, # slot is unused
  181. slotFixedVar, # slot is used for a fixed var/result (requires copy then)
  182. slotFixedLet, # slot is used for a fixed param/let
  183. slotTempUnknown, # slot but type unknown (argument of proc call)
  184. slotTempInt, # some temporary int
  185. slotTempFloat, # some temporary float
  186. slotTempStr, # some temporary string
  187. slotTempComplex, # some complex temporary (s.node field is used)
  188. slotTempPerm # slot is temporary but permanent (hack)
  189. TRegisterKind* = enum
  190. rkNone, rkNode, rkInt, rkFloat, rkRegisterAddr, rkNodeAddr
  191. TFullReg* = object # with a custom mark proc, we could use the same
  192. # data representation as LuaJit (tagged NaNs).
  193. case kind*: TRegisterKind
  194. of rkNone: nil
  195. of rkInt: intVal*: BiggestInt
  196. of rkFloat: floatVal*: BiggestFloat
  197. of rkNode: node*: PNode
  198. of rkRegisterAddr: regAddr*: ptr TFullReg
  199. of rkNodeAddr: nodeAddr*: ptr PNode
  200. PProc* = ref object
  201. blocks*: seq[TBlock] # blocks; temp data structure
  202. sym*: PSym
  203. regInfo*: seq[tuple[inUse: bool, kind: TSlotKind]]
  204. VmArgs* = object
  205. ra*, rb*, rc*: Natural
  206. slots*: ptr UncheckedArray[TFullReg]
  207. currentException*: PNode
  208. currentLineInfo*: TLineInfo
  209. VmCallback* = proc (args: VmArgs) {.closure.}
  210. PCtx* = ref TCtx
  211. TCtx* = object of TPassContext # code gen context
  212. code*: seq[TInstr]
  213. debug*: seq[TLineInfo] # line info for every instruction; kept separate
  214. # to not slow down interpretation
  215. globals*: PNode #
  216. constants*: PNode # constant data
  217. types*: seq[PType] # some instructions reference types (e.g. 'except')
  218. currentExceptionA*, currentExceptionB*: PNode
  219. exceptionInstr*: int # index of instruction that raised the exception
  220. prc*: PProc
  221. module*: PSym
  222. callsite*: PNode
  223. mode*: TEvalMode
  224. features*: TSandboxFlags
  225. traceActive*: bool
  226. loopIterations*: int
  227. comesFromHeuristic*: TLineInfo # Heuristic for better macro stack traces
  228. callbacks*: seq[tuple[key: string, value: VmCallback]]
  229. errorFlag*: string
  230. cache*: IdentCache
  231. config*: ConfigRef
  232. graph*: ModuleGraph
  233. oldErrorCount*: int
  234. profiler*: Profiler
  235. templInstCounter*: ref int # gives every template instantiation a unique ID, needed here for getAst
  236. vmstateDiff*: seq[(PSym, PNode)] # we remember the "diff" to global state here (feature for IC)
  237. procToCodePos*: Table[int, int]
  238. PStackFrame* = ref TStackFrame
  239. TStackFrame* {.acyclic.} = object
  240. prc*: PSym # current prc; proc that is evaluated
  241. slots*: seq[TFullReg] # parameters passed to the proc + locals;
  242. # parameters come first
  243. next*: PStackFrame # for stacking
  244. comesFrom*: int
  245. safePoints*: seq[int] # used for exception handling
  246. # XXX 'break' should perform cleanup actions
  247. # What does the C backend do for it?
  248. Profiler* = object
  249. tEnter*: float
  250. tos*: PStackFrame
  251. TPosition* = distinct int
  252. PEvalContext* = PCtx
  253. proc newCtx*(module: PSym; cache: IdentCache; g: ModuleGraph; idgen: IdGenerator): PCtx =
  254. PCtx(code: @[], debug: @[],
  255. globals: newNode(nkStmtListExpr), constants: newNode(nkStmtList), types: @[],
  256. prc: PProc(blocks: @[]), module: module, loopIterations: g.config.maxLoopIterationsVM,
  257. comesFromHeuristic: unknownLineInfo, callbacks: @[], errorFlag: "",
  258. cache: cache, config: g.config, graph: g, idgen: idgen)
  259. proc refresh*(c: PCtx, module: PSym; idgen: IdGenerator) =
  260. c.module = module
  261. c.prc = PProc(blocks: @[])
  262. c.loopIterations = c.config.maxLoopIterationsVM
  263. c.idgen = idgen
  264. proc registerCallback*(c: PCtx; name: string; callback: VmCallback): int {.discardable.} =
  265. result = c.callbacks.len
  266. c.callbacks.add((name, callback))
  267. const
  268. firstABxInstr* = opcTJmp
  269. largeInstrs* = { # instructions which use 2 int32s instead of 1:
  270. opcConv, opcCast, opcNewSeq, opcOf
  271. }
  272. slotSomeTemp* = slotTempUnknown
  273. relativeJumps* = {opcTJmp, opcFJmp, opcJmp, opcJmpBack}
  274. # flag is used to signal opcSeqLen if node is NimNode.
  275. const nimNodeFlag* = 16
  276. template opcode*(x: TInstr): TOpcode = TOpcode(x.TInstrType shr regOShift and regOMask)
  277. template regA*(x: TInstr): TRegister = TRegister(x.TInstrType shr regAShift and regAMask)
  278. template regB*(x: TInstr): TRegister = TRegister(x.TInstrType shr regBShift and regBMask)
  279. template regC*(x: TInstr): TRegister = TRegister(x.TInstrType shr regCShift and regCMask)
  280. template regBx*(x: TInstr): int = (x.TInstrType shr regBxShift and regBxMask).int
  281. template jmpDiff*(x: TInstr): int = regBx(x) - wordExcess