summaryrefslogtreecommitdiff
path: root/uscript/vm.h
blob: cf9aa60c6c8d09b3f8133942c2dcb48f73bf208d (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
#ifndef __USCRIPT_VM_H__
#define __USCRIPT_VM_H__

#include "common.h"
#include "val.h"

#define MAX_CALL_FRAMES (64)
#define STACK_SIZE (MAX_CALL_FRAMES * 256)

#define vm_pop()   (*(--vm.stacktop))
#define vm_peek()  (vm.stacktop[-1])
#define vm_push(v) (*vm.stacktop++ = (v))

#define set_global(idx, v) (vm.gstack[idx].val = (v))

enum bytecode {
#define BC(name) BC_##name,
#include "xbytecode.h"
#undef BC
};

struct call_frame {
        struct us_val func;
        struct us_val *stackbot;
};

struct global {
        struct us_str *name;
        struct us_val val;
};

struct vm {
        struct us_val *objs;

        struct call_frame cf_stack[MAX_CALL_FRAMES];
        struct call_frame *cf;

        struct us_val stack[STACK_SIZE];
        struct us_val *stacktop;

        struct global *gstack; // dynarr
        struct global *gstacktop;

        struct us_upval *open_upvals;
};

extern struct vm vm;

void init_vm(void);
void deinit_vm(void);
void print_func(struct us_proto *proto);

int declare_global(struct us_str *name);

#endif // __USCRIPT_VM_H__