compiler/vm/compilerbridge

  Source   Edit

This module implements the interface between the VM and the rest of the compiler. The VM is only interacted with through this interface. Do note that right now, the compiler still indirectly interacts with the VM through the vmdef.TCtx object.

The interface includes:

  • the procedure that sets up a VM instance for use during compilation (setupGlobalCtx)
  • the routines for executing expressions, statements, and macros with the VM
  • an implementation of the passes interface that executes processed code with the VM (evalPass)
  • the VM-related compilerapi
what follows is an implementation of the passes interface that evaluates the code directly inside the VM. It is used for NimScript execution and by the nimeval interface

Types

EvalContext {.final.} = object of RootObj
  vm*: TCtx
  jit*: JitState
All state required to on-demand translate AST to VM bytecode and execute it. An EvalContext instance makes up everything that is required for running code at compile-time.   Source   Edit
ExecErrorKind = enum
  execErrorVm, execErrorVmGen, execErrorQuit
  Source   Edit
ExecErrorReport = object
  stackTrace*: VmStackTrace  ## The VM stack-trace
  location*: TLineInfo       ## Source location of the trace
  instLoc*: InstantiationInfo ## report instantiation location
  case kind*: ExecErrorKind  ## kind of error execution of vm code gen
  of execErrorVm:
      vmErr*: VmEvent

  of execErrorVmGen:
      genErr*: VmGenDiag

  of execErrorQuit:
      exitCode*: int

  
  Source   Edit
ExecutionResult = Result[PNode, ExecErrorReport]
  Source   Edit
PVmCtx = ref object of RootObj
  context*: TCtx
Wrapper type intended for storing only a VM instance (without a JIT environment) in the module graph.   Source   Edit

Consts

evalPass = (open: myOpen, process: myProcess, close: myClose, isFrontend: false)
  Source   Edit

Procs

proc dumpVmProfilerData(graph: ModuleGraph): string {.
    ...raises: [KeyError, Exception], tags: [ReadDirEffect, RootEffect].}
Dumps the profiler data collected by the profiler of the VM instance associated with graph to a string.   Source   Edit
proc evalConstExpr(module: PSym; idgen: IdGenerator; g: ModuleGraph; e: PNode): PNode {.
    inline, ...raises: [KeyError, Exception, ERecoverableError,
                     ResultErrorRef[system.void], ValueError,
                     ResultErrorRef[vmdef.VmGenDiag], IOError,
                     ResultErrorRef[compilerbridge.ExecErrorReport],
                     ResultErrorRef[vm.VmException]],
    tags: [ReadDirEffect, RootEffect, WriteIOEffect, TimeEffect, ReadIOEffect].}
  Source   Edit
proc evalMacroCall(jit: var JitState; c: var TCtx; call, args: PNode; sym: PSym): PNode {....raises: [
    KeyError, Exception, ERecoverableError, ValueError,
    ResultErrorRef[system.void], ResultErrorRef[vmdef.VmGenDiag], IOError,
    ResultErrorRef[compilerbridge.ExecErrorReport],
    ResultErrorRef[vm.VmException]],
    tags: [RootEffect, ReadDirEffect, WriteIOEffect, TimeEffect, ReadIOEffect].}

Evaluates a call to the macro sym with arguments arg with the VM.

call is the original call expression, which is used as the wrongNode in case of an error, as the node returned by the callsite macro API procedure, and for providing line information.

  Source   Edit
proc evalMacroCall(module: PSym; idgen: IdGenerator; g: ModuleGraph;
                   templInstCounter: ref int; call, args: PNode; sym: PSym): PNode {....raises: [
    KeyError, Exception, ERecoverableError, ValueError,
    ResultErrorRef[system.void], ResultErrorRef[vmdef.VmGenDiag], IOError,
    ResultErrorRef[compilerbridge.ExecErrorReport],
    ResultErrorRef[vm.VmException]],
    tags: [ReadDirEffect, RootEffect, WriteIOEffect, TimeEffect, ReadIOEffect].}
Similar to the other evalMacroCall overload, but also updates the compile-time execution context with the provided module, idgen, g, and templInstCounter.   Source   Edit
proc evalStaticExpr(module: PSym; idgen: IdGenerator; g: ModuleGraph; e: PNode;
                    prc: PSym): PNode {.inline, ...raises: [KeyError, Exception,
    ERecoverableError, ResultErrorRef[system.void], ValueError,
    ResultErrorRef[vmdef.VmGenDiag], IOError,
    ResultErrorRef[compilerbridge.ExecErrorReport],
    ResultErrorRef[vm.VmException]], tags: [ReadDirEffect, RootEffect,
    WriteIOEffect, TimeEffect, ReadIOEffect].}
  Source   Edit
proc evalStaticStmt(module: PSym; idgen: IdGenerator; g: ModuleGraph; e: PNode;
                    prc: PSym): PNode {.inline, ...raises: [KeyError, Exception,
    ERecoverableError, ResultErrorRef[system.void], ValueError,
    ResultErrorRef[vmdef.VmGenDiag], IOError,
    ResultErrorRef[compilerbridge.ExecErrorReport],
    ResultErrorRef[vm.VmException]], tags: [ReadDirEffect, RootEffect,
    WriteIOEffect, TimeEffect, ReadIOEffect].}
  Source   Edit
proc execProc(jit: var JitState; c: var TCtx; sym: PSym; args: openArray[PNode]): PNode {....raises: [
    Exception, ERecoverableError, KeyError, ValueError,
    ResultErrorRef[system.void], IOError, ResultErrorRef[vm.VmException],
    ResultErrorRef[vmdef.VmGenDiag],
    ResultErrorRef[compilerbridge.ExecErrorReport]],
    tags: [RootEffect, ReadDirEffect, TimeEffect, ReadIOEffect, WriteIOEffect].}
  Source   Edit
proc getGlobalValue(c: EvalContext; s: PSym): PNode {.
    ...raises: [Exception, ERecoverableError, KeyError], tags: [RootEffect].}
Does not perform type checking, so ensure that s.typ matches the global's type   Source   Edit
proc registerAdditionalOps(c: var TCtx; disallowDangerous: bool) {....raises: [],
    tags: [].}
Convenience proc used for setting up the overrides relevant during compile-time execution. If disallowDangerous is set to 'true', all operations that are able to modify the host's environment are replaced with no-ops   Source   Edit
proc setGlobalValue(c: var EvalContext; s: PSym; val: PNode) {.
    ...raises: [Exception, ERecoverableError, KeyError],
    tags: [RootEffect, ReadDirEffect].}
Does not do type checking so ensure the val matches the s.typ   Source   Edit
proc setupCompileTimeVar(module: PSym; idgen: IdGenerator; g: ModuleGraph;
                         n: PNode) {.inline, ...raises: [KeyError, Exception,
    ERecoverableError, ResultErrorRef[system.void], ValueError,
    ResultErrorRef[vmdef.VmGenDiag], IOError,
    ResultErrorRef[compilerbridge.ExecErrorReport],
    ResultErrorRef[vm.VmException]], tags: [ReadDirEffect, RootEffect,
    WriteIOEffect, TimeEffect, ReadIOEffect].}
  Source   Edit
proc setupGlobalCtx(module: PSym; graph: ModuleGraph; idgen: IdGenerator) {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
  Source   Edit