Comecando a ficar mais com a cara de Yacc.
[myPerl.git] / code.c
blobb4f3fdf50fb117f7e2554fb9af24bf1eee8699d7
1 #include <stdio.h>
2 #include "code.h"
3 #include "vm.h"
5 #define SEG_SIZE 4096
7 int code_offset = 0;
8 code_t code[SEG_SIZE];
10 struct ops ops[] = {
11 { "HALT", vm_halt },
12 { "ADD", vm_add },
13 { "SUB", vm_sub },
14 { "MUL", vm_mul },
15 { "DIV", vm_div },
16 { "MOD", vm_mod },
17 { "LOAD_VAR", vm_load_var },
18 { "LOAD_INT", vm_load_int },
19 { "STORE", vm_store },
20 { "JMP_FALSE", vm_jmp_false },
21 { "GOTO", vm_goto },
22 { "WRITE_INT", vm_write_int },
23 { "PUSH_INT", vm_push_int },
24 { "PUSH_VAR", vm_push_var },
25 { "CALL", vm_call },
26 { "RET", vm_ret }
29 int
30 reserve_loc(void)
32 return code_offset++;
35 int
36 gen_label(void)
38 return code_offset;
41 void
42 gen_code(int op, int arg)
44 code[code_offset].op = op;
45 code[code_offset++].arg = arg;
48 void
49 back_patch(int addr, int op, int arg)
51 code[addr].op = op;
52 code[addr].arg = arg;
55 void
56 print_code(char *outfile)
58 int i;
59 FILE *fp;
61 if (outfile) {
62 fp = fopen(outfile, "w");
63 if (! fp) {
64 perror("fopen");
65 fprintf(stderr, "Erro ao abrir %s\n", outfile);
67 } else {
68 fp = stdout;
71 for (i = 0; i < code_offset; ++i) {
72 fprintf(fp, "%d: %s %d\n", i, ops[code[i].op].opname, code[i].arg);