# compiler/vmdef

Dark Mode

- Index

Search:
Group by:
Source Edit

This module contains the type definitions for the new evaluation engine. An instruction is 1-3 int32s in memory, it is a register based VM.

# Imports

ast, idents, options, modulegraphs, lineinfos

# Types

    PCtx = ref TCtx

  Source Edit

    PEvalContext = PCtx

  Source Edit

    PProc = ref object
      blocks*: seq[TBlock]
      sym*: PSym
      regInfo*: seq[tuple[inUse: bool, kind: TSlotKind]]

  Source Edit

    Profiler = object
      tEnter*: float
      tos*: PStackFrame

  Source Edit

    PStackFrame = ref TStackFrame

  Source Edit

    TBlock = object
      label*: PSym
      fixups*: seq[TPosition]

  Source Edit

    TCtx = object of TPassContext
      code*: seq[TInstr]
      debug*: seq[TLineInfo]
      globals*: PNode
      constants*: PNode
      types*: seq[PType]
      currentExceptionA*, currentExceptionB*: PNode
      exceptionInstr*: int
      prc*: PProc
      module*: PSym
      callsite*: PNode
      mode*: TEvalMode
      features*: TSandboxFlags
      traceActive*: bool
      loopIterations*: int
      comesFromHeuristic*: TLineInfo
      callbacks*: seq[tuple[key: string, value: VmCallback]]
      errorFlag*: string
      cache*: IdentCache
      config*: ConfigRef
      graph*: ModuleGraph
      oldErrorCount*: int
      profiler*: Profiler
      templInstCounter*: ref int
      vmstateDiff*: seq[(PSym, PNode)]
      procToCodePos*: Table[int, int]

  Source Edit

    TDest = range[-1 .. 65535'u64.int]

  Source Edit

    TEvalMode = enum
      emRepl,                   ## evaluate because in REPL mode
      emConst,                  ## evaluate for 'const' according to spec
      emOptimize,               ## evaluate for optimization purposes (same as
                                 ## emConst?)
      emStaticExpr,             ## evaluate for enforced compile time eval
                                 ## ('static' context)
      emStaticStmt               ## 'static' as an expression

  reason for evaluation Source Edit

    TFullReg = object
      case kind*: TRegisterKind
      of rkNone:
        nil
      of rkInt:
        intVal*: BiggestInt
      of rkFloat:
        floatVal*: BiggestFloat
      of rkNode:
        node*: PNode
      of rkRegisterAddr:
        regAddr*: ptr TFullReg
      of rkNodeAddr:
        nodeAddr*: ptr PNode

  Source Edit

    TInstr = distinct TInstrType

  Source Edit

    TInstrType = uint64

  Source Edit

    TOpcode = enum
      opcEof, opcRet, opcYldYoid, opcYldVal, opcAsgnInt, opcAsgnFloat, opcAsgnRef,
      opcAsgnComplex, opcCastIntToFloat32, opcCastIntToFloat64, opcCastFloatToInt32,
      opcCastFloatToInt64, opcCastPtrToInt, opcCastIntToPtr, opcFastAsgnComplex,
      opcNodeToReg, opcLdArr, opcLdArrAddr, opcWrArr, opcLdObj, opcLdObjAddr,
      opcWrObj, opcAddrReg, opcAddrNode, opcLdDeref, opcWrDeref, opcWrStrIdx,
      opcLdStrIdx, opcLdStrIdxAddr, opcSlice, opcAddInt, opcAddImmInt, opcSubInt,
      opcSubImmInt, opcLenSeq, opcLenStr, opcLenCstring, opcIncl, opcInclRange,
      opcExcl, opcCard, opcMulInt, opcDivInt, opcModInt, opcAddFloat, opcSubFloat,
      opcMulFloat, opcDivFloat, opcShrInt, opcShlInt, opcAshrInt, opcBitandInt,
      opcBitorInt, opcBitxorInt, opcAddu, opcSubu, opcMulu, opcDivu, opcModu,
      opcEqInt, opcLeInt, opcLtInt, opcEqFloat, opcLeFloat, opcLtFloat, opcLeu,
      opcLtu, opcEqRef, opcEqNimNode, opcSameNodeType, opcXor, opcNot,
      opcUnaryMinusInt, opcUnaryMinusFloat, opcBitnotInt, opcEqStr, opcLeStr,
      opcLtStr, opcEqSet, opcLeSet, opcLtSet, opcMulSet, opcPlusSet, opcMinusSet,
      opcConcatStr, opcContainsSet, opcRepr, opcSetLenStr, opcSetLenSeq, opcIsNil,
      opcOf, opcIs, opcSubStr, opcParseFloat, opcConv, opcCast, opcQuit,
      opcInvalidField, opcNarrowS, opcNarrowU, opcSignExtend, opcAddStrCh,
      opcAddStrStr, opcAddSeqElem, opcRangeChck, opcNAdd, opcNAddMultiple, opcNKind,
      opcNSymKind, opcNIntVal, opcNFloatVal, opcNSymbol, opcNIdent, opcNGetType,
      opcNStrVal, opcNSigHash, opcNGetSize, opcNSetIntVal, opcNSetFloatVal,
      opcNSetSymbol, opcNSetIdent, opcNSetType, opcNSetStrVal, opcNNewNimNode,
      opcNCopyNimNode, opcNCopyNimTree, opcNDel, opcGenSym, opcNccValue, opcNccInc,
      opcNcsAdd, opcNcsIncl, opcNcsLen, opcNcsAt, opcNctPut, opcNctLen, opcNctGet,
      opcNctHasNext, opcNctNext, opcNodeId, opcSlurp, opcGorge, opcParseExprToAst,
      opcParseStmtToAst, opcQueryErrorFlag, opcNError, opcNWarning, opcNHint,
      opcNGetLineInfo, opcNCopyLineInfo, opcNSetLineInfoLine, opcNSetLineInfoColumn,
      opcNSetLineInfoFile, opcEqIdent, opcStrToIdent, opcGetImpl, opcGetImplTransf,
      opcEcho, opcIndCall, opcIndCallAsgn, opcRaise, opcNChild, opcNSetChild,
      opcCallSite, opcNewStr, opcTJmp, opcFJmp, opcJmp, opcJmpBack, opcBranch,
      opcTry, opcExcept, opcFinally, opcFinallyEnd, opcNew, opcNewSeq, opcLdNull,
      opcLdNullReg, opcLdConst, opcAsgnConst, opcLdGlobal, opcLdGlobalAddr,
      opcLdGlobalDerefFFI, opcLdGlobalAddrDerefFFI, opcLdImmInt, opcNBindSym,
      opcNDynBindSym, opcSetType, opcTypeTrait, opcMarshalLoad, opcMarshalStore,
      opcSymOwner, opcSymIsInstantiationOf

  Source Edit

    TPosition = distinct int

  Source Edit

    TRegister = range[0 .. 65535'u64.int]

  Source Edit

    TRegisterKind = enum
      rkNone, rkNode, rkInt, rkFloat, rkRegisterAddr, rkNodeAddr

  Source Edit

    TSandboxFlag = enum
      allowCast,                ## allow unsafe language feature: 'cast'
      allowInfiniteLoops         ## allow endless loops

  what the evaluation engine should allow Source Edit

    TSandboxFlags = set[TSandboxFlag]

  Source Edit

    TSlotKind = enum
      slotEmpty, slotFixedVar, slotFixedLet, slotTempUnknown, slotTempInt,
      slotTempFloat, slotTempStr, slotTempComplex, slotTempPerm

  Source Edit

    TStackFrame {.acyclic.} = object
      prc*: PSym
      slots*: seq[TFullReg]
      next*: PStackFrame
      comesFrom*: int
      safePoints*: seq[int]

  Source Edit

    VmArgs = object
      ra*, rb*, rc*: Natural
      slots*: ptr UncheckedArray[TFullReg]
      currentException*: PNode
      currentLineInfo*: TLineInfo

  Source Edit

    VmCallback = proc (args: VmArgs) {.closure.}

  Source Edit

# Consts

    byteExcess = 128

  Source Edit

    firstABxInstr = opcTJmp

  Source Edit

    largeInstrs = {opcSubStr, opcConv, opcCast, opcNewSeq, opcOf, opcMarshalLoad,
                   opcMarshalStore}

  Source Edit

    nimNodeFlag = 16

  Source Edit

    regAMask = 65535'u64

  Source Edit

    regAShift = 8'u64

  Source Edit

    regBMask = 65535'u64

  Source Edit

    regBShift = 24'u64

  Source Edit

    regBxMask = 16777215'u64

  Source Edit

    regBxMax = 8388607

  Source Edit

    regBxMin = -8388607

  Source Edit

    regBxShift = 24'u64

  Source Edit

    regCMask = 65535'u64

  Source Edit

    regCShift = 40'u64

  Source Edit

    regOMask = 255'u64

  Source Edit

    regOShift = 0'u

  Source Edit

    relativeJumps = {opcTJmp, opcFJmp, opcJmp, opcJmpBack}

  Source Edit

    slotSomeTemp = slotTempUnknown

  Source Edit

    wordExcess = 8388608

  Source Edit

# Procs

    proc newCtx(module: PSym; cache: IdentCache; g: ModuleGraph; idgen: IdGenerator): PCtx {.
        ...raises: [], tags: [].}

  Source Edit

    proc refresh(c: PCtx; module: PSym; idgen: IdGenerator) {....raises: [], tags: [].}

  Source Edit

    proc registerCallback(c: PCtx; name: string; callback: VmCallback): int {.
        discardable, ...raises: [], tags: [].}

  Source Edit

# Templates

    template jmpDiff(x: TInstr): int

  Source Edit

    template opcode(x: TInstr): TOpcode

  Source Edit

    template regA(x: TInstr): TRegister

  Source Edit

    template regB(x: TInstr): TRegister

  Source Edit

    template regBx(x: TInstr): int

  Source Edit

    template regC(x: TInstr): TRegister

  Source Edit