2016-05-19 10:42:39 +00:00
|
|
|
#include "vm.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
Instruction code[] =
|
2016-05-19 10:42:39 +00:00
|
|
|
{
|
2016-05-19 12:09:15 +00:00
|
|
|
// { VM_EXEC_X, VM_EXEC_X, VM_INPUT_ZERO, VM_INPUT_ZERO, VM_CMD_COPY, 0, VM_FLAG_NO, VM_OUTPUT_DISCARD, 0 },
|
2016-05-19 13:41:18 +00:00
|
|
|
{ // push 0
|
2016-05-19 12:09:15 +00:00
|
|
|
VM_EXEC_X,
|
|
|
|
VM_EXEC_X,
|
2016-05-19 13:41:18 +00:00
|
|
|
VM_INPUT_ARG,
|
2016-05-19 12:09:15 +00:00
|
|
|
VM_INPUT_ZERO,
|
2016-05-19 13:41:18 +00:00
|
|
|
VM_CMD_COPY,
|
|
|
|
0,
|
|
|
|
VM_FLAG_NO,
|
|
|
|
VM_OUTPUT_PUSH,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
{ // dup
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_INPUT_PEEK,
|
2016-05-19 12:09:15 +00:00
|
|
|
VM_INPUT_ZERO,
|
|
|
|
VM_CMD_COPY,
|
|
|
|
0,
|
|
|
|
VM_FLAG_NO,
|
2016-05-19 13:41:18 +00:00
|
|
|
VM_OUTPUT_PUSH,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
{ // cmp 10
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_INPUT_ARG,
|
|
|
|
VM_INPUT_POP,
|
|
|
|
VM_CMD_MATH,
|
|
|
|
VM_MATH_SUB,
|
|
|
|
VM_FLAG_YES,
|
2016-05-19 12:09:15 +00:00
|
|
|
VM_OUTPUT_DISCARD,
|
2016-05-19 13:41:18 +00:00
|
|
|
10
|
|
|
|
},
|
|
|
|
{ // jmp 7
|
|
|
|
VM_EXEC_1,
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_INPUT_ARG,
|
|
|
|
VM_INPUT_ZERO,
|
|
|
|
VM_CMD_COPY,
|
|
|
|
0,
|
|
|
|
VM_FLAG_NO,
|
|
|
|
VM_OUTPUT_JUMP,
|
|
|
|
7
|
|
|
|
},
|
|
|
|
{ // add 1
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_INPUT_ARG,
|
|
|
|
VM_INPUT_POP,
|
|
|
|
VM_CMD_MATH,
|
|
|
|
0,
|
|
|
|
VM_FLAG_NO,
|
|
|
|
VM_OUTPUT_PUSH,
|
|
|
|
1
|
|
|
|
},
|
|
|
|
{ // jmp 1
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_EXEC_X,
|
|
|
|
VM_INPUT_ARG,
|
|
|
|
VM_INPUT_ZERO,
|
|
|
|
VM_CMD_COPY,
|
|
|
|
0,
|
|
|
|
VM_FLAG_NO,
|
|
|
|
VM_OUTPUT_JUMP,
|
|
|
|
1
|
|
|
|
},
|
2016-05-19 11:19:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void dump_proc(Process *process)
|
|
|
|
{
|
2016-05-19 13:41:18 +00:00
|
|
|
if(process->flags & VM_FLAG_Z)
|
|
|
|
printf("z");
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
if(process->flags & VM_FLAG_N)
|
|
|
|
printf("n");
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
printf("[ ");
|
|
|
|
for(int i = 1; i <= process->stackPointer; i++) {
|
|
|
|
printf("%d ", process->stack[i]);
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
2016-05-19 10:42:39 +00:00
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
void vm_assert(int assertion, const char *msg)
|
|
|
|
{
|
|
|
|
if(assertion != 0)
|
|
|
|
return;
|
|
|
|
printf("Assertion failed: %s\n", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-05-19 10:42:39 +00:00
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
2016-05-19 13:41:18 +00:00
|
|
|
Module module = { code, 6 };
|
2016-05-19 11:19:38 +00:00
|
|
|
|
|
|
|
Process process = { &module, 0, 0, 0, 0 };
|
|
|
|
Process *p = &process;
|
|
|
|
|
2016-05-19 12:09:15 +00:00
|
|
|
dump_proc(p);
|
2016-05-19 13:41:18 +00:00
|
|
|
while(vm_step_process(p)) {
|
|
|
|
dump_proc(p);
|
|
|
|
}
|
2016-05-19 12:09:15 +00:00
|
|
|
dump_proc(p);
|
2016-05-19 11:19:38 +00:00
|
|
|
|
2016-05-19 10:42:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|