summaryrefslogtreecommitdiff
path: root/src/vm.h
blob: 143275fa310cce1cf22d8bee7e49a3995e2af6a4 (plain)
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
/*
 * Copyright (C) 2025  Johnny Richard <johnny@johnnyrichard.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef VM_H
#define VM_H
#include "stack.h"

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(program[0]))
#define STACK_CAPACITY 1024
#define HEAP_CAPACITY 1024

typedef enum inst_type
{
    // stack
    INST_PUSH,
    INST_DUP,
    INST_COPY,
    INST_SWAP,
    INST_DROP,
    INST_SLIDE,
    // arithmetics
    INST_ADD,
    INST_SUB,
    INST_MUL,
    INST_DIV,
    INST_MOD,
    // heap access
    INST_STORE,
    INST_LOAD,
    // Flow control
    INST_CALL,
    INST_RET,
    INST_LABEL,
    INST_JMP,
    INST_JMPZ,
    INST_JMPN,
    // I/O
    INST_PRINTI,
    INST_PRINTC,
    INST_READI,
    INST_READC,
    INST_END,
} inst_type_t;

typedef struct instr
{
    inst_type_t type;
    int operand;
} inst_t;

typedef struct label
{
    int name;
    int index;
} label_t;

typedef struct vm
{
    stack_t stack;
    stack_t call_stack;
    int* heap;
    label_t* labels;
} vm_t;

void
vm_init(vm_t* vm);

void
vm_run(vm_t* vm, inst_t* insts);

#endif /* VM_H */