This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
minivm.h
88 lines (61 loc) · 2.43 KB
/
minivm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//-----------------------------------------------------------------------------
// minivm.h -- Prototypes and definitions for the vm.
// Copyright (C) Philip Conrad 4/30/2013 @ 3:56 PM -- MIT License
//
//-----------------------------------------------------------------------------
#if defined(_MSC_VER)
#include "stdint.h"
#else
#include <stdint.h>
#endif
#ifndef MINIVM_H
#define MINIVM_H
//---------------------------------------------------------
// MACRO DEFINITIONS:
// Size of the global function pointer table
#define MVM_NUM_FUNS 256
#define MVM_NUM_REGISTERS 16 // Default
//---------------------------------------------------------
// DATA STRUCTURES & TYPEDEFS:
struct VMContext;
typedef void (*FunPtr)(struct VMContext* ctx, const uint32_t);
//CHANGE THE INTERNALS OF THIS FOR YOUR OWN VM!
typedef struct Reg {
uint32_t type;
uint32_t value;
} Reg;
typedef struct VMContext {
uint32_t numRegs;
uint32_t numFuns;
Reg* r; // Ptr to register array.
FunPtr* funtable; // Ptr to a funptr table.
} VMContext;
//---------------------------------------------------------
// ESOTERIC ITEMS:
#ifdef MVM_GLOBAL_FUNTABLE
// The global function pointer table.
static FunPtr mvm_function_table[MVM_NUM_FUNS];
#endif
// Byte extraction macros.
#define EXTRACT_B0(i) (i & 0xFF)
#define EXTRACT_B1(i) ((i >> 8) & 0xFF)
#define EXTRACT_B2(i) ((i >> 16) & 0xFF)
#define EXTRACT_B3(i) ((i >> 24) & 0xFF)
//---------------------------------------------------------
// FUNCTIONS:
// Selects and executes an opcode function from the function pointer table.
// Passes the entire bytecode instruction as the argument.
// dispatch :: VMContext -> uint32_t -> Effect()
void dispatch(struct VMContext* ctx, const uint32_t instr);
// Initializes a VMContext in-place.
// initVMContext :: VMContext -> uint32_t -> uint32_t -> [Reg] -> [FunPtr] -> Effect()
void initVMContext(struct VMContext* ctx,
const uint32_t numRegs,
const uint32_t numFuns,
Reg* registers,
FunPtr* funtable);
// Reads an instruction, executes it, then steps to the next instruction.
// stepVMContext :: VMContext -> uint32_t** -> Effect()
void stepVMContext(struct VMContext* ctx, uint32_t** pc);
//---------------------------------------------------------
#endif //ifndef MINIVM_H