git.haldean.org cvm / 1720ae3
Update the way instructions are printed so they can be written straight out to file Will Brown 9 years ago
3 changed file(s) with 18 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
00 BINARY=cvm
11 PYTHON=pypy
2 CC=gcc
2 CC=clang
33 CFLAGS=-c -Wall
44 LDFLAGS=
55 SOURCES=$(wildcard *.c)
33
44 //#define DEBUG
55 #define MEMORY_SIZE 128
6 #define STACK_DEPTH 128
67
78 #define RS_PUSH(x) reg_stack_push(machine->reg_stack, (x))
89 #define RS_POP reg_stack_pop(machine->reg_stack)
2021
2122 struct machine_state_struct {
2223 register_stack *reg_stack;
24 register_stack *return_addresses;
2325
2426 memory_cell *memory;
2527 uint memory_size;
7375 machine->memory = mem_create(MEMORY_SIZE);
7476 machine->memory_size = MEMORY_SIZE;
7577 machine->reg_stack = reg_stack_create(32);
78 machine->return_addresses = reg_stack_create(STACK_DEPTH);
7679 machine->code = NULL;
7780 machine->code_len = 0;
7881 machine->program_counter = 0;
138141 case POP:
139142 RS_POP;
140143 break;
144
145 case CALL:
146 reg_stack_push(machine->return_addresses, machine->program_counter + 1);
147 machine->program_counter = argument;
148 goto skip_pc_incr;
149
150 case RETURN:
151 machine->program_counter = reg_stack_pop(machine->return_addresses);
152 goto skip_pc_incr;
141153
142154 case STORE:
143155 machine->memory[(uint) argument] = RS_POP;
3636 #define OJMP 31
3737 #define LDCONST 32
3838 #define PRINT 33
39 #define RETURN 34
40 #define CALL 35
3941 char **opcode_map() {
40 char **opcode_names = malloc(34 * sizeof(char *));
42 char **opcode_names = malloc(36 * sizeof(char *));
4143 opcode_names[NOP] = "NOP";
4244 opcode_names[HALT] = "HALT";
4345 opcode_names[PUSH] = "PUSH";
7274 opcode_names[OJMP] = "OJMP";
7375 opcode_names[LDCONST] = "LDCONST";
7476 opcode_names[PRINT] = "PRINT";
77 opcode_names[RETURN] = "RETURN";
78 opcode_names[CALL] = "CALL";
7579 return opcode_names;
7680 }
7781 #endif