vmdef.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 std/[tables, strutils]
  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, opcEqCString, 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. opcNGetType,
  105. opcNStrVal,
  106. opcNSigHash,
  107. opcNGetSize,
  108. opcNSetIntVal,
  109. opcNSetFloatVal, opcNSetStrVal,
  110. opcNNewNimNode, opcNCopyNimNode, opcNCopyNimTree, opcNDel, opcGenSym,
  111. opcNccValue, opcNccInc, opcNcsAdd, opcNcsIncl, opcNcsLen, opcNcsAt,
  112. opcNctPut, opcNctLen, opcNctGet, opcNctHasNext, opcNctNext, opcNodeId,
  113. opcSlurp,
  114. opcGorge,
  115. opcParseExprToAst,
  116. opcParseStmtToAst,
  117. opcQueryErrorFlag,
  118. opcNError,
  119. opcNWarning,
  120. opcNHint,
  121. opcNGetLineInfo, opcNCopyLineInfo, opcNSetLineInfoLine,
  122. opcNSetLineInfoColumn, opcNSetLineInfoFile
  123. opcEqIdent,
  124. opcStrToIdent,
  125. opcGetImpl,
  126. opcGetImplTransf
  127. opcEcho,
  128. opcIndCall, # dest = call regStart, n; where regStart = fn, arg1, ...
  129. opcIndCallAsgn, # dest = call regStart, n; where regStart = fn, arg1, ...
  130. opcRaise,
  131. opcNChild,
  132. opcNSetChild,
  133. opcCallSite,
  134. opcNewStr,
  135. opcTJmp, # jump Bx if A != 0
  136. opcFJmp, # jump Bx if A == 0
  137. opcJmp, # jump Bx
  138. opcJmpBack, # jump Bx; resulting from a while loop
  139. opcBranch, # branch for 'case'
  140. opcTry,
  141. opcExcept,
  142. opcFinally,
  143. opcFinallyEnd,
  144. opcNew,
  145. opcNewSeq,
  146. opcLdNull, # dest = nullvalue(types[Bx])
  147. opcLdNullReg,
  148. opcLdConst, # dest = constants[Bx]
  149. opcAsgnConst, # dest = copy(constants[Bx])
  150. opcLdGlobal, # dest = globals[Bx]
  151. opcLdGlobalAddr, # dest = addr(globals[Bx])
  152. opcLdGlobalDerefFFI, # dest = globals[Bx][]
  153. opcLdGlobalAddrDerefFFI, # globals[Bx][] = ...
  154. opcLdImmInt, # dest = immediate value
  155. opcNBindSym, opcNDynBindSym,
  156. opcSetType, # dest.typ = types[Bx]
  157. opcTypeTrait,
  158. opcSymOwner,
  159. opcSymIsInstantiationOf
  160. TBlock* = object
  161. label*: PSym
  162. fixups*: seq[TPosition]
  163. TEvalMode* = enum ## reason for evaluation
  164. emRepl, ## evaluate because in REPL mode
  165. emConst, ## evaluate for 'const' according to spec
  166. emOptimize, ## evaluate for optimization purposes (same as
  167. ## emConst?)
  168. emStaticExpr, ## evaluate for enforced compile time eval
  169. ## ('static' context)
  170. emStaticStmt ## 'static' as an expression
  171. TSandboxFlag* = enum ## what the evaluation engine should allow
  172. allowCast, ## allow unsafe language feature: 'cast'
  173. allowInfiniteLoops ## allow endless loops
  174. TSandboxFlags* = set[TSandboxFlag]
  175. TSlotKind* = enum # We try to re-use slots in a smart way to
  176. # minimize allocations; however the VM supports arbitrary
  177. # temporary slot usage. This is required for the parameter
  178. # passing implementation.
  179. slotEmpty, # slot is unused
  180. slotFixedVar, # slot is used for a fixed var/result (requires copy then)
  181. slotFixedLet, # slot is used for a fixed param/let
  182. slotTempUnknown, # slot but type unknown (argument of proc call)
  183. slotTempInt, # some temporary int
  184. slotTempFloat, # some temporary float
  185. slotTempStr, # some temporary string
  186. slotTempComplex, # some complex temporary (s.node field is used)
  187. slotTempPerm # slot is temporary but permanent (hack)
  188. TRegisterKind* = enum
  189. rkNone, rkNode, rkInt, rkFloat, rkRegisterAddr, rkNodeAddr
  190. TFullReg* = object # with a custom mark proc, we could use the same
  191. # data representation as LuaJit (tagged NaNs).
  192. case kind*: TRegisterKind
  193. of rkNone: nil
  194. of rkInt: intVal*: BiggestInt
  195. of rkFloat: floatVal*: BiggestFloat
  196. of rkNode: node*: PNode
  197. of rkRegisterAddr: regAddr*: ptr TFullReg
  198. of rkNodeAddr: nodeAddr*: ptr PNode
  199. PProc* = ref object
  200. blocks*: seq[TBlock] # blocks; temp data structure
  201. sym*: PSym
  202. regInfo*: seq[tuple[inUse: bool, kind: TSlotKind]]
  203. VmArgs* = object
  204. ra*, rb*, rc*: Natural
  205. slots*: ptr UncheckedArray[TFullReg]
  206. currentException*: PNode
  207. currentLineInfo*: TLineInfo
  208. VmCallback* = proc (args: VmArgs) {.closure.}
  209. PCtx* = ref TCtx
  210. TCtx* = object of TPassContext # code gen context
  211. code*: seq[TInstr]
  212. debug*: seq[TLineInfo] # line info for every instruction; kept separate
  213. # to not slow down interpretation
  214. globals*: PNode #
  215. constants*: PNode # constant data
  216. types*: seq[PType] # some instructions reference types (e.g. 'except')
  217. currentExceptionA*, currentExceptionB*: PNode
  218. exceptionInstr*: int # index of instruction that raised the exception
  219. prc*: PProc
  220. module*: PSym
  221. callsite*: PNode
  222. mode*: TEvalMode
  223. features*: TSandboxFlags
  224. traceActive*: bool
  225. loopIterations*: int
  226. comesFromHeuristic*: TLineInfo # Heuristic for better macro stack traces
  227. callbacks*: seq[VmCallback]
  228. callbackIndex*: Table[string, int]
  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: @[], callbackIndex: initTable[string, int](), 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 reverseName(s: string): string =
  265. result = newStringOfCap(s.len)
  266. let y = s.split('.')
  267. for i in 1..y.len:
  268. result.add y[^i]
  269. if i != y.len:
  270. result.add '.'
  271. proc registerCallback*(c: PCtx; name: string; callback: VmCallback): int {.discardable.} =
  272. result = c.callbacks.len
  273. c.callbacks.add(callback)
  274. c.callbackIndex[reverseName(name)] = result
  275. const
  276. firstABxInstr* = opcTJmp
  277. largeInstrs* = { # instructions which use 2 int32s instead of 1:
  278. opcConv, opcCast, opcNewSeq, opcOf
  279. }
  280. slotSomeTemp* = slotTempUnknown
  281. relativeJumps* = {opcTJmp, opcFJmp, opcJmp, opcJmpBack}
  282. # flag is used to signal opcSeqLen if node is NimNode.
  283. const nimNodeFlag* = 16
  284. template opcode*(x: TInstr): TOpcode = TOpcode(x.TInstrType shr regOShift and regOMask)
  285. template regA*(x: TInstr): TRegister = TRegister(x.TInstrType shr regAShift and regAMask)
  286. template regB*(x: TInstr): TRegister = TRegister(x.TInstrType shr regBShift and regBMask)
  287. template regC*(x: TInstr): TRegister = TRegister(x.TInstrType shr regCShift and regCMask)
  288. template regBx*(x: TInstr): int = (x.TInstrType shr regBxShift and regBxMask).int
  289. template jmpDiff*(x: TInstr): int = regBx(x) - wordExcess