8 #define STACK_SIZE 4096
11 static int stack
[STACK_SIZE
];
13 static int fun_top
= -1;
14 static int fun_stack
[STACK_SIZE
];
16 static int pc
= 0; /* program counter */
19 O_ADD
, O_SUB
, O_MUL
, O_DIV
, O_MOD
,
20 O_EQL
, O_NE
, O_GEQL
, O_LEQL
, O_GT
, O_LT
};
28 stack
[top
-1] = stack
[top
-1] + stack
[top
];
31 stack
[top
-1] = stack
[top
-1] - stack
[top
];
34 stack
[top
-1] = stack
[top
-1] * stack
[top
];
37 stack
[top
-1] = stack
[top
-1] / stack
[top
];
40 stack
[top
-1] = stack
[top
-1] % stack
[top
];
43 stack
[top
-1] = stack
[top
-1] == stack
[top
];
46 stack
[top
-1] = stack
[top
-1] != stack
[top
];
49 stack
[top
-1] = stack
[top
-1] >= stack
[top
];
52 stack
[top
-1] = stack
[top
-1] <= stack
[top
];
55 stack
[top
-1] = stack
[top
-1] > stack
[top
];
58 stack
[top
-1] = stack
[top
-1] < stack
[top
];
61 fprintf(stderr
, "Operacao %d nao reconhecida\n", op
);
142 int off
= stack
[top
--];
143 stack
[++top
] = data_get(arg
, off
);
164 int off
= stack
[top
--];
166 data_put(arg
, off
, stack
[top
]);
172 vm_jmp_false(int arg
)
188 vm_write_int(int arg
)
190 printf("%d\n", stack
[top
]);
205 fun_stack
[++fun_top
] = arg
;
212 fun_stack
[++fun_top
] = data_get(arg
, stack
[top
--]);
221 pc
= fun_stack
[fun_top
--];
222 nargs
= fun_stack
[fun_top
--];
226 assert(fun_top
>= -1 && top
>= -1);
234 while (code
[pc
].op
!= HALT
) {
235 ops
[code
[pc
].op
].op(code
[pc
].arg
);