nanoFORTH  v2.2
n4_vm.cpp File Reference

nanoForth Virtual Machine class implementation More...

#include "n4_core.h"
#include "n4_asm.h"
#include "n4_intr.h"
#include "n4_vm.h"
Include dependency graph for n4_vm.cpp:

Namespaces

 N4VM
 

Macros

#define DUMP_PER_LINE   0x10
 
#define S2D(u, v)   (((DU2)(u)<<16) | D_LO(v))
 : stand-alone functions to reduce register allocation in _invoke More...
 
#define D_HI(u)   ((U16)((u)>>16))
 
#define D_LO(u)   ((U16)((u)&0xffff))
 
#define DISPATCH(op)   goto *vt[op];
 
#define CODE(i, g)   L_##i: { g; } return xt
 
#define LL(i)
 
Data Stack and Return Stack Ops
  • VM built with core units
#define SP0   ((DU*)&dic[N4_DIC_SZ + N4_STK_SZ])
 
#define TOS   (*vm.sp)
 
#define SS(i)   (*(vm.sp+(i)))
 
#define PUSH(v)   (*(--vm.sp)=(DU)(v))
 
#define POP()   (*vm.sp++)
 
#define RPUSH(a)   (*(vm.rp++)=(IU)(a))
 
#define RPOP()   (*(--vm.rp))
 
#define BOOL(f)   ((f) ? -1 : 0)
 
Dictionary Index <=> Pointer Converters
#define DIC(n)   ((U8*)dic + (n))
 
#define IDX(p)   ((IU)((U8*)(p) - dic))
 

Functions

void N4VM::_nest (IU xt)
 
void N4VM::_init ()
 
void N4VM::_dump (IU p0, U16 sz0)
 
void N4VM::_immediate (U16 op)
 
void N4VM::_clock ()
 
void N4VM::_dplus ()
 
void N4VM::_dminus ()
 
void N4VM::_dneg ()
 
IU N4VM::_invoke (U8 op, IU xt=0)
 
void N4VM::setup (const char *code, Stream &io, U8 ucase)
 
void N4VM::push (int v)
 
int N4VM::pop ()
 
void N4VM::serv_isr ()
 interrupt service routine More...
 
void N4VM::outer ()
 outer-interpreter More...
 

Detailed Description

nanoForth Virtual Machine class implementation

Forth VM stack opcode macros (notes: rp grows upward and may collide with sp)

memory space
SP0 (sp max to protect overwritten of vm object)
mem[...dic_sz...|...stk_sz...|......heap......]max
| | | |
dic--> +-->rp sp<--+-->tib auto<--+
TOS NOS

Macro Definition Documentation

◆ SP0

#define SP0   ((DU*)&dic[N4_DIC_SZ + N4_STK_SZ])

◆ TOS

#define TOS   (*vm.sp)

pointer to top of current stack

◆ SS

#define SS (   i)    (*(vm.sp+(i)))

pointer to the nth on stack

◆ PUSH

#define PUSH (   v)    (*(--vm.sp)=(DU)(v))

push v onto parameter stack

◆ POP

#define POP ( )    (*vm.sp++)

pop value off parameter stack

◆ RPUSH

#define RPUSH (   a)    (*(vm.rp++)=(IU)(a))

push address onto return stack

◆ RPOP

#define RPOP ( )    (*(--vm.rp))

pop address from return stack

◆ BOOL

#define BOOL (   f)    ((f) ? -1 : 0)

TRUE=-1 per common Forth idiom

◆ DIC

#define DIC (   n)    ((U8*)dic + (n))

convert dictionary index to a memory pointer

◆ IDX

#define IDX (   p)    ((IU)((U8*)(p) - dic))

convert memory pointer to a dictionary index

◆ DUMP_PER_LINE

#define DUMP_PER_LINE   0x10

show a section of memory in Forth dump format

◆ S2D

#define S2D (   u,
 
)    (((DU2)(u)<<16) | D_LO(v))

: stand-alone functions to reduce register allocation in _invoke

32-bit operators

◆ D_HI

#define D_HI (   u)    ((U16)((u)>>16))

◆ D_LO

#define D_LO (   u)    ((U16)((u)&0xffff))

◆ DISPATCH

#define DISPATCH (   op)    goto *vt[op];

◆ CODE

#define CODE (   i,
 
)    L_##i: { g; } return xt

◆ LL

#define LL (   i)
Value:
&&L_##i##0,&&L_##i##1,&&L_##i##2,&&L_##i##3,&&L_##i##4, \
&&L_##i##5,&&L_##i##6,&&L_##i##7,&&L_##i##8,&&L_##i##9
N4Core::vm
N4Task vm
VM context.
Definition: n4_core.cpp:17
dic
static unsigned char dic[DIC_SIZE]
Definition: tforth.c:31
SP0
#define SP0
Definition: n4_vm.cpp:24
TOS
#define TOS
Definition: n4_vm.cpp:25