8 #define STACK_SIZE 4096
13 * Essa pilha eh a usada por praticamente tds as
14 * rotinas da maquina virtual
17 static int stack
[STACK_SIZE
];
20 * Pilha usada para armazenar informacoes especificas
21 * das chamadas de funcao
23 static int fun_top
= -1;
24 static int fun_stack
[STACK_SIZE
];
29 /* Operacoes binarias */
31 O_ADD
, O_SUB
, O_MUL
, O_DIV
, O_MOD
,
32 O_EQL
, O_NE
, O_GEQL
, O_LEQL
, O_GT
, O_LT
,
41 stack
[top
-1] = stack
[top
-1] + stack
[top
];
44 stack
[top
-1] = stack
[top
-1] - stack
[top
];
47 stack
[top
-1] = stack
[top
-1] * stack
[top
];
50 stack
[top
-1] = stack
[top
-1] / stack
[top
];
53 stack
[top
-1] = stack
[top
-1] % stack
[top
];
56 stack
[top
-1] = stack
[top
-1] == stack
[top
];
59 stack
[top
-1] = stack
[top
-1] != stack
[top
];
62 stack
[top
-1] = stack
[top
-1] >= stack
[top
];
65 stack
[top
-1] = stack
[top
-1] <= stack
[top
];
68 stack
[top
-1] = stack
[top
-1] > stack
[top
];
71 stack
[top
-1] = stack
[top
-1] < stack
[top
];
74 stack
[top
-1] = stack
[top
-1] || stack
[top
];
77 stack
[top
-1] = stack
[top
-1] && stack
[top
];
80 stack
[top
-1] = !stack
[top
-1];
81 top
++; /* we do not reduce the stack */
84 fprintf(stderr
, "Operacao %d nao reconhecida\n", op
);
183 int off
= stack
[top
--];
184 stack
[++top
] = data_get(arg
, off
);
205 int off
= stack
[top
--];
207 data_put(arg
, off
, stack
[top
]);
213 vm_jmp_false(int arg
)
229 vm_write_int(int arg
)
231 printf("%d\n", stack
[top
]);
239 int nargs
= fun_stack
[fun_top
-1];
242 /* store the base pointer */
243 fun_stack
[++fun_top
] = top
-nargs
+1;
250 fun_stack
[++fun_top
] = arg
;
257 fun_stack
[++fun_top
] = data_get(arg
, stack
[top
--]);
266 fun_top
--; /* take off the base pointer */
267 pc
= fun_stack
[fun_top
--];
268 nargs
= fun_stack
[fun_top
--];
272 assert(fun_top
>= -1 && top
>= -1);
280 int bp
= fun_stack
[fun_top
];
281 stack
[++top
] = stack
[bp
];
282 fun_stack
[fun_top
]++;
289 while (code
[pc
].op
!= HALT
) {
290 ops
[code
[pc
].op
].op(code
[pc
].arg
);