From 9d30a3f8f502d32ea0c1d1370e259f7ec6380a7a Mon Sep 17 00:00:00 2001 From: AlexVanin Date: Tue, 14 Jan 2014 22:13:31 +0400 Subject: [PATCH] First alpha version. Added support for putting\getting from TOS int and double --- VaninVM/IOcode.c | 12 ++++++++++++ VaninVM/IOcode.h | 8 ++++++++ VaninVM/Main.c | 36 ++++++++++++++++++++++++++++++++++++ VaninVM/TOS.c | 35 +++++++++++++++++++++++++++++++++++ VaninVM/TOS.h | 18 ++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 VaninVM/IOcode.c create mode 100644 VaninVM/IOcode.h create mode 100644 VaninVM/Main.c create mode 100644 VaninVM/TOS.c create mode 100644 VaninVM/TOS.h diff --git a/VaninVM/IOcode.c b/VaninVM/IOcode.c new file mode 100644 index 0000000..3fa879a --- /dev/null +++ b/VaninVM/IOcode.c @@ -0,0 +1,12 @@ +#include "IOcode.h" + +char* read_bytecode(char* path) +{ + FILE* input; + char* buffer = (char*)calloc(1, 1000); //Early version without bytecode file + fopen_s(&input, "bytecode", "rb"); + fread_s(buffer, 1000, sizeof(char), 1000, input); //Early version without bytecode file + fclose(input); + + return buffer; +} diff --git a/VaninVM/IOcode.h b/VaninVM/IOcode.h new file mode 100644 index 0000000..4cdd8f5 --- /dev/null +++ b/VaninVM/IOcode.h @@ -0,0 +1,8 @@ +#ifndef IOCODE_H +#define IOCODE_H + +#include +#include + +char* read_bytecode(char*); +#endif \ No newline at end of file diff --git a/VaninVM/Main.c b/VaninVM/Main.c new file mode 100644 index 0000000..daa0427 --- /dev/null +++ b/VaninVM/Main.c @@ -0,0 +1,36 @@ +#include "IOcode.h" +#include "TOS.h" + + +int main(int argc, char** argv) +{ + char* code; + int ip; + + + int exec_status = 1; + + code = read_bytecode("bytecode"); + initTOS(); + + ip = 0; + + while (exec_status) + { + switch (code[ip]) + { + case 1: + push_double(12.1); + ip++; + break; + + case 78: + exec_status = 0; + break; + } + } + printf("%f", pop_double()); + getchar(); + return 0; + +} \ No newline at end of file diff --git a/VaninVM/TOS.c b/VaninVM/TOS.c new file mode 100644 index 0000000..cf38a6f --- /dev/null +++ b/VaninVM/TOS.c @@ -0,0 +1,35 @@ +#include +#include "TOS.h" + +void push_int(int num) +{ + TOS[tp++] = num; +} + +int pop_int() +{ + int num = TOS[--tp]; + return num; +} + +void push_double(double num) +{ + tos_num cont = { num }; + push_int(cont.num_i[0]); + push_int(cont.num_i[1]); +} + +double pop_double() +{ + tos_num cont; + cont.num_i[1] = pop_int(); + cont.num_i[0] = pop_int(); + return cont.num_d; +} + +int initTOS() +{ + TOS = (int*)calloc(1, 1500); + tp = 0; + return 0; +} \ No newline at end of file diff --git a/VaninVM/TOS.h b/VaninVM/TOS.h new file mode 100644 index 0000000..6d48711 --- /dev/null +++ b/VaninVM/TOS.h @@ -0,0 +1,18 @@ +#ifndef TOS_H +#define TOS_H + +typedef union +{ + double num_d; + int num_i[2]; +} tos_num; + +int* TOS; +int tp; +int initTOS(); +void push_int(int); +void push_double(double); +int pop_int(); +double pop_double(); + +#endif \ No newline at end of file