Arduino-FVM
Byte Token Threaded Forth Virtual Machine (FVM) for Arduino
FVM Class Reference

#include <FVM.h>

Collaboration diagram for FVM:
Collaboration graph

Classes

struct  const_t
 
struct  func_t
 
struct  obj_t
 
struct  Task
 
struct  task_t
 
struct  var_t
 

Public Types

enum  {
  OP_EXIT = 0, OP_ZERO_EXIT = 1, OP_LIT = 2, OP_CLIT = 3,
  OP_SLIT = 4, OP_VAR = 5, OP_CONST = 6, OP_FUNC = 7,
  OP_DOES = 8, OP_PARAM = 9, OP_BRANCH = 10, OP_ZERO_BRANCH = 11,
  OP_DO = 12, OP_I = 13, OP_J = 14, OP_LEAVE = 15,
  OP_LOOP = 16, OP_PLUS_LOOP = 17, OP_NOOP = 18, OP_EXECUTE = 19,
  OP_HALT = 20, OP_YIELD = 21, OP_SYSCALL = 22, OP_CALL = 23,
  OP_TRACE = 24, OP_ROOM = 25, OP_C_FETCH = 26, OP_C_STORE = 27,
  OP_FETCH = 28, OP_STORE = 29, OP_PLUS_STORE = 30, OP_DP = 31,
  OP_HERE = 32, OP_ALLOT = 33, OP_COMMA = 34, OP_C_COMMA = 35,
  OP_COMPILE = 36, OP_TO_R = 37, OP_R_FROM = 38, OP_R_FETCH = 39,
  OP_SP = 40, OP_DEPTH = 41, OP_DROP = 42, OP_NIP = 43,
  OP_EMPTY = 44, OP_DUP = 45, OP_QUESTION_DUP = 46, OP_OVER = 47,
  OP_TUCK = 48, OP_PICK = 49, OP_SWAP = 50, OP_ROT = 51,
  OP_MINUS_ROT = 52, OP_ROLL = 53, OP_TWO_SWAP = 54, OP_TWO_DUP = 55,
  OP_TWO_OVER = 56, OP_TWO_DROP = 57, OP_MINUS_TWO = 58, OP_MINUS_ONE = 59,
  OP_ZERO = 60, OP_ONE = 61, OP_TWO = 62, OP_CELL = 63,
  OP_CELLS = 64, OP_BOOL = 65, OP_NOT = 66, OP_TRUE = 67,
  OP_FALSE = 68, OP_INVERT = 69, OP_AND = 70, OP_OR = 71,
  OP_XOR = 72, OP_NEGATE = 73, OP_ONE_PLUS = 74, OP_ONE_MINUS = 75,
  OP_TWO_PLUS = 76, OP_TWO_MINUS = 77, OP_TWO_STAR = 78, OP_TWO_SLASH = 79,
  OP_PLUS = 80, OP_MINUS = 81, OP_STAR = 82, OP_STAR_SLASH = 83,
  OP_SLASH = 84, OP_MOD = 85, OP_SLASH_MOD = 86, OP_LSHIFT = 87,
  OP_RSHIFT = 88, OP_WITHIN = 89, OP_ABS = 90, OP_MIN = 91,
  OP_MAX = 92, OP_ZERO_NOT_EQUALS = 93, OP_ZERO_LESS = 94, OP_ZERO_EQUALS = 95,
  OP_ZERO_GREATER = 96, OP_NOT_EQUALS = 97, OP_LESS = 98, OP_EQUALS = 99,
  OP_GREATER = 100, OP_U_LESS = 101, OP_LOOKUP = 102, OP_TO_BODY = 103,
  OP_WORDS = 104, OP_BASE = 105, OP_HEX = 106, OP_DECIMAL = 107,
  OP_QUESTION_KEY = 108, OP_KEY = 109, OP_EMIT = 110, OP_CR = 111,
  OP_SPACE = 112, OP_SPACES = 113, OP_U_DOT = 114, OP_DOT = 115,
  OP_DOT_S = 116, OP_DOT_QUOTE = 117, OP_TYPE = 118, OP_DOT_NAME = 119,
  OP_QUESTION = 120, OP_MICROS = 121, OP_MILLIS = 122, OP_DELAY = 123,
  OP_PINMODE = 124, OP_DIGITALREAD = 125, OP_DIGITALWRITE = 126, OP_DIGITALTOGGLE = 127,
  OP_ANALOGREAD = 128, OP_ANALOGWRITE = 129, CORE_MAX = 128, KERNEL_MAX = 256,
  APPLICATION_MAX = 384, TOKEN_MAX = 511
}
 
typedef int32_t cell_t
 
typedef uint32_t ucell_t
 
typedef int64_t cell2_t
 
typedef uint64_t ucell2_t
 
typedef int8_t code_t
 
typedef const code_tcode_P
 
typedef void(* fn_t) (task_t &task, void *env)
 

Public Member Functions

 FVM (uint8_t *dp0=0, size_t bytes=0, uint8_t words=0)
 
uint8_t * dp ()
 
void dp (uint8_t *dp)
 
bool compile (int op)
 
void literal (int val)
 
bool create (const char *name)
 
bool variable (const char *name)
 
bool constant (const char *name, int val)
 
const char * name (int op)
 
code_tbody (int op)
 
bool forget (int op)
 
int scan (char *bp, task_t &task)
 
int lookup (const char *name)
 
int resume (task_t &task)
 
int execute (int op, task_t &task)
 
int execute (code_P fn, task_t &task)
 
int execute (const char *name, task_t &task)
 
int interpret (task_t &task)
 

Static Public Attributes

static const code_P fntab []
 
static const str_P fnstr []
 
static const uint16_t CODE_P_MAX = 0x8000
 

Protected Attributes

const size_t DICT_MAX
 
const uint8_t WORD_MAX
 
uint8_t m_next
 
uint8_t * m_dp
 
uint8_t * m_dp0
 
code_t ** m_body
 
char ** m_name
 

Static Protected Attributes

static const str_P opstr []
 

Detailed Description

Definition at line 34 of file FVM.h.

Member Typedef Documentation

typedef int64_t FVM::cell2_t

Definition at line 237 of file FVM.h.

typedef int32_t FVM::cell_t

Cell and double data type.

Definition at line 235 of file FVM.h.

typedef const code_t* FVM::code_P

Definition at line 249 of file FVM.h.

typedef int8_t FVM::code_t

Token and threaded code data type: 0..127 direct kernel words/switch, PROGMEM -1..-128 direct application words/threaded code table, PROGMEM 0..255 OP_SYSCALL prefix, direct kernel words/switch, PROGMEM 384..511 mapped 0..127 OP_CALL prefix, SRAM

Definition at line 248 of file FVM.h.

typedef void(* FVM::fn_t) (task_t &task, void *env)

Wrapper for extension functions.

Parameters
[in]task.

Definition at line 383 of file FVM.h.

typedef uint64_t FVM::ucell2_t

Definition at line 238 of file FVM.h.

typedef uint32_t FVM::ucell_t

Definition at line 236 of file FVM.h.

Member Enumeration Documentation

anonymous enum
Enumerator
OP_EXIT 

Threaded code return.

OP_ZERO_EXIT 

Threaded code return if zero/false.

OP_LIT 

Inline literal constant.

OP_CLIT 

Inline literal signed character constant.

OP_SLIT 

Push instruction pointer and branch always.

OP_VAR 

Handle variable reference.

OP_CONST 

Handle constant.

OP_FUNC 

Handle function wrapper call.

OP_DOES 

Handle object pointer.

OP_PARAM 

Duplicate inline indexed stack element.

OP_BRANCH 

Branch always (offset -128..127)

OP_ZERO_BRANCH 

Branch if zero/false (offset -128..127)

OP_DO 

Start loop block.

OP_I 

Current loop index.

OP_J 

Outer loop index.

OP_LEAVE 

Mark loop block as completed.

OP_LOOP 

End loop block (one increment)

OP_PLUS_LOOP 

End loop block (n increment)

OP_NOOP 

No operation.

OP_EXECUTE 

Execute operation token.

OP_HALT 

Halt virtual machine.

OP_YIELD 

Yield virtual machine.

OP_SYSCALL 

Call system token.

OP_CALL 

Call application token.

OP_TRACE 

Set trace mode.

OP_ROOM 

Dictionary state.

OP_C_FETCH 

Load character (signed byte)

OP_C_STORE 

Store character.

OP_FETCH 

Load data.

OP_STORE 

Store data.

OP_PLUS_STORE 

Update data.

OP_DP 

Data pointer variable.

OP_HERE 

Data pointer.

OP_ALLOT 

Allocate number of bytes.

OP_COMMA 

Allocate and assign from top of stack.

OP_C_COMMA 

Allocate and assign character.

OP_COMPILE 

Add inline token.

OP_TO_R 

Push data on return stack.

OP_R_FROM 

Pop data from return stack.

OP_R_FETCH 

Copy from return stack.

OP_SP 

Stack pointer.

OP_DEPTH 

Number of elements.

OP_DROP 

Drop top of stack.

OP_NIP 

Drop next top of stack.

OP_EMPTY 

Empty stack.

OP_DUP 

Duplicate top of stack.

OP_QUESTION_DUP 

Duplicate top of stack if not zero.

OP_OVER 

Duplicate next top of stack.

OP_TUCK 

Duplicate top of stack and rotate.

OP_PICK 

Duplicate index stack element.

OP_SWAP 

Swap two top stack elements.

OP_ROT 

Rotate three top stack elements.

OP_MINUS_ROT 

Inverse rotate three top stack elements.

OP_ROLL 

Rotate given number of stack elements.

OP_TWO_SWAP 

Swap two double stack elements.

OP_TWO_DUP 

Duplicate double stack elements.

OP_TWO_OVER 

Duplicate double next top of stack.

OP_TWO_DROP 

Drop double top of stack.

OP_MINUS_TWO 

Push constant(-2)

OP_MINUS_ONE 

Push constant(-1)

OP_ZERO 

Push constant(0)

OP_ONE 

Push constant(1)

OP_TWO 

Push constant(2)

OP_CELL 

Stack width in bytes.

OP_CELLS 

Convert cells to bytes for allot.

OP_BOOL 

Convert top of stack to boolean.

OP_NOT 

Convert top of stack to invert boolean.

OP_TRUE 

Push true(-1)

OP_FALSE 

Push false(0)

OP_INVERT 

Bitwise inverse top element.

OP_AND 

Bitwise AND top two elements.

OP_OR 

Bitwise OR top two elements.

OP_XOR 

Bitwise XOR top two elements.

OP_NEGATE 

Negate top of stack.

OP_ONE_PLUS 

Increment top of stack.

OP_ONE_MINUS 

Decrement top of stack.

OP_TWO_PLUS 

Increment by two.

OP_TWO_MINUS 

Decrement by two.

OP_TWO_STAR 

Multiply by two.

OP_TWO_SLASH 

Divide by two.

OP_PLUS 

Add top two elements.

OP_MINUS 

Substract top two elements.

OP_STAR 

Multiply top two elements.

OP_STAR_SLASH 

Multiply/Divide top three elements.

OP_SLASH 

Quotient for division of top two elements.

OP_MOD 

Remainder for division of top two elements.

OP_SLASH_MOD 

Quotient and remainder.

OP_LSHIFT 

Left shift.

OP_RSHIFT 

Right shift.

OP_WITHIN 

Within boundard.

OP_ABS 

Absolute value.

OP_MIN 

Minimum value.

OP_MAX 

Maximum value.

OP_ZERO_NOT_EQUALS 

Not equal zero.

OP_ZERO_LESS 

Less than zero.

OP_ZERO_EQUALS 

Equal to zero.

OP_ZERO_GREATER 

Greater than zero.

OP_NOT_EQUALS 

Not equal.

OP_LESS 

Less than.

OP_EQUALS 

Equal.

OP_GREATER 

Greater than.

OP_U_LESS 

Unsigned less than.

OP_LOOKUP 

Lookup word in dictionary.

OP_TO_BODY 

Access data area application variable.

OP_WORDS 

List dictionaries.

OP_BASE 

Base for number conversion.

OP_HEX 

Set hexa-decimal number conversion base.

OP_DECIMAL 

Set decimal number conversion base.

OP_QUESTION_KEY 

Read character if available.

OP_KEY 

Wait for character and read.

OP_EMIT 

Print character.

OP_CR 

Print new-line.

OP_SPACE 

Print space.

OP_SPACES 

Print spaces.

OP_U_DOT 

Print top of stack as unsigned.

OP_DOT 

Print top of stack.

OP_DOT_S 

Print contents of parameter stack.

OP_DOT_QUOTE 

Print program memory string.

OP_TYPE 

Print string.

OP_DOT_NAME 

Print name of token.

OP_QUESTION 

Print value of variable.

OP_MICROS 

Micro-seconds.

OP_MILLIS 

Milli-seconds.

OP_DELAY 

Delay milli-seconds (yield)

OP_PINMODE 

Digital pin mode.

OP_DIGITALREAD 

Read digital pin.

OP_DIGITALWRITE 

Write digital pin.

OP_DIGITALTOGGLE 

Toggle digital pin.

OP_ANALOGREAD 

Read analog pin.

OP_ANALOGWRITE 

Write pwm pin.

CORE_MAX 

0..127: direct kernel words/switch, PROGMEM.

KERNEL_MAX 

128..255: extended kernel words/prefix/threaded code table, PROGMEM.

APPLICATION_MAX 

256..383: direct application words/threaded code table, PROGMEM.

TOKEN_MAX 

384..511: extended application words/prefix/threaded code table, SRAM.

Definition at line 36 of file FVM.h.

Constructor & Destructor Documentation

FVM::FVM ( uint8_t *  dp0 = 0,
size_t  bytes = 0,
uint8_t  words = 0 
)
inline

Construct forth virtual machine with given data area and dynamic dictionary.

Parameters
[in]dp0initial data pointer (default none).
[in]bytesin data area (default 0).
[in]wordsnumber of words in dynamic dictionary (default 0).

Definition at line 397 of file FVM.h.

Member Function Documentation

code_t* FVM::body ( int  op)
inline

Access body from dynamic dictionary.

Parameters
[in]opoperation code (token).

Definition at line 538 of file FVM.h.

bool FVM::compile ( int  op)
inline

Allocate and copy given operation code to data area.

Parameters
[in]opoperation code (token).

Definition at line 436 of file FVM.h.

bool FVM::constant ( const char *  name,
int  val 
)
inline

Create constant in dictionary.

Parameters
[in]namestring.
[in]valvalue.

Definition at line 512 of file FVM.h.

bool FVM::create ( const char *  name)
inline

Create word in dictionary. Initiate body reference to the dynamic dictionary.

Parameters
[in]namestring.

Definition at line 475 of file FVM.h.

uint8_t* FVM::dp ( )
inline

Get data allocation pointer.

Returns
pointer.

Definition at line 418 of file FVM.h.

void FVM::dp ( uint8_t *  dp)
inline

Set data allocation pointer.

Parameters
[in]dpdata allocation pointer.

Definition at line 427 of file FVM.h.

int FVM::execute ( int  op,
task_t task 
)

Execute given token with given task.

Parameters
[in]optoken to execute.
[in]taskto resume.
Returns
error code.

Definition at line 1837 of file FVM.cpp.

int FVM::execute ( code_P  fn,
task_t task 
)
inline

Execute given threaded code pointer with task. Returns on yield(1), halt(0) or illegal instruction (-1).

Parameters
[in]fnthreaded code pointer.
[in]taskto resume.
Returns
error code.

Definition at line 601 of file FVM.h.

int FVM::execute ( const char *  name,
task_t task 
)
inline

Lookup given name in dictionary and execute with given task. Convert to number if possible, and push on task stack. Returns on yield(1), halt(0) or illegal instruction (-1).

Parameters
[in]nameof word to execute.
[in]taskto resume.
Returns
error code.

Definition at line 614 of file FVM.h.

bool FVM::forget ( int  op)
inline

Forget latest dynamic dictionary words up to and including given token/word.

Parameters
[in]opoperation code (token).

Definition at line 552 of file FVM.h.

int FVM::interpret ( task_t task)

Interpret; scan, lookup and execute until halt or error. Returns on halt(0) or illegal instruction (-1).

Parameters
[in]taskto run.
Returns
error code.

Definition at line 1848 of file FVM.cpp.

void FVM::literal ( int  val)
inline

Compile literal to data area.

Parameters
[in]valliteral value.

Definition at line 457 of file FVM.h.

int FVM::lookup ( const char *  name)

Lookup given string in dictionary. Return token otherwise negative error code(-1).

Parameters
[in]namestring.
Returns
toker or negative error code.

Definition at line 101 of file FVM.cpp.

const char* FVM::name ( int  op)
inline

Access name from dynamic dictionary.

Parameters
[in]opoperation code (token).

Definition at line 529 of file FVM.h.

int FVM::resume ( task_t task)

Resume task in virtual machine with given task. Return yield(1), halt(0), or error code(-1).

Parameters
[in]taskto resume.
Returns
error code.

Definition at line 143 of file FVM.cpp.

int FVM::scan ( char *  bp,
task_t task 
)

Scan token to given buffer. Return break character or negative error code(-1).

Parameters
[in]bpbuffer pointer.
[in]taskstream to use.
Returns
break character or negative error code.

Definition at line 121 of file FVM.cpp.

bool FVM::variable ( const char *  name)
inline

Create variable in dictionary.

Parameters
[in]namestring.

Definition at line 494 of file FVM.h.

Member Data Documentation

const uint16_t FVM::CODE_P_MAX = 0x8000
static

Definition at line 632 of file FVM.h.

const size_t FVM::DICT_MAX
protected

Definition at line 639 of file FVM.h.

const str_P FVM::fnstr
static
Initial value:

Definition at line 629 of file FVM.h.

const FVM::code_P FVM::fntab
static
Initial value:

Definition at line 628 of file FVM.h.

code_t** FVM::m_body
protected

Definition at line 644 of file FVM.h.

uint8_t* FVM::m_dp
protected

Definition at line 642 of file FVM.h.

uint8_t* FVM::m_dp0
protected

Definition at line 643 of file FVM.h.

char** FVM::m_name
protected

Definition at line 645 of file FVM.h.

uint8_t FVM::m_next
protected

Definition at line 641 of file FVM.h.

const str_P FVM::opstr
staticprotected

Definition at line 636 of file FVM.h.

const uint8_t FVM::WORD_MAX
protected

Definition at line 640 of file FVM.h.


The documentation for this class was generated from the following files: