Adicionado operadores relacionais.
[myPerl.git] / code.c
bloba8ee01d8b8f1abe9f7fa601917374de567c3f023
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 { "CMP_EQL", vm_cmp_eql },
18 { "CMP_NE", vm_cmp_ne },
19 { "CMP_GEQL", vm_cmp_geql },
20 { "CMP_LEQL", vm_cmp_leql },
21 { "CMP_GT", vm_cmp_gt },
22 { "CMP_LT", vm_cmp_lt },
23 { "LOAD_VAR", vm_load_var },
24 { "LOAD_INT", vm_load_int },
25 { "LOAD_OFF", vm_load_off },
26 { "STORE", vm_store },
27 { "JMP_FALSE", vm_jmp_false },
28 { "GOTO", vm_goto },
29 { "WRITE_INT", vm_write_int },
30 { "PUSH_INT", vm_push_int },
31 { "PUSH_VAR", vm_push_var },
32 { "CALL", vm_call },
33 { "RET", vm_ret }
36 int
37 reserve_loc(void)
39 return code_offset++;
42 int
43 gen_label(void)
45 return code_offset;
48 void
49 gen_code(int op, int arg)
51 code[code_offset].op = op;
52 code[code_offset++].arg = arg;
55 void
56 back_patch(int addr, int op, int arg)
58 code[addr].op = op;
59 code[addr].arg = arg;
62 void
63 print_code(char *outfile)
65 int i;
66 FILE *fp;
68 if (outfile) {
69 fp = fopen(outfile, "w");
70 if (! fp) {
71 perror("fopen");
72 fprintf(stderr, "Erro ao abrir %s\n", outfile);
74 } else {
75 fp = stdout;
78 for (i = 0; i < code_offset; ++i) {
79 fprintf(fp, "%d: %s %d\n", i, ops[code[i].op].opname, code[i].arg);