From 808f62b97e81d66bdcb0041818f2633aae64d04f Mon Sep 17 00:00:00 2001 From: AlexVanin Date: Tue, 28 Jan 2014 13:56:45 +0400 Subject: [PATCH] Changed the way local variables are works, now it is a bunch of unions. ReturnStack now have changable size. Strins are now char* instead int --- VaninVM/Context.c | 2 +- VaninVM/Context.h | 8 +++++++- VaninVM/LocalVars.c | 21 +++++++++++---------- VaninVM/LocalVars.h | 4 ++-- VaninVM/Main.c | 6 +++--- VaninVM/ReturnStack.c | 4 ++-- VaninVM/ReturnStack.h | 2 +- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/VaninVM/Context.c b/VaninVM/Context.c index ff65c06..e1cc2e5 100644 --- a/VaninVM/Context.c +++ b/VaninVM/Context.c @@ -6,7 +6,7 @@ context* create_context(int function, func** hash, char** code) cont = (context*)malloc(sizeof(context)); cont->id = function; - cont->locals=malloc(8*hash[function]->count_locals); + cont->locals=(l_var*)malloc(sizeof(l_var)*hash[function]->count_locals); *(code)=hash[function]->code; return cont; } diff --git a/VaninVM/Context.h b/VaninVM/Context.h index f0eedec..936ca24 100644 --- a/VaninVM/Context.h +++ b/VaninVM/Context.h @@ -4,11 +4,17 @@ #include "CodeHeader.h" #include +typedef union +{ + double d_data; + long long i_data; + char* s_data; +} l_var; typedef struct { int id; - void* locals; + l_var* locals; } context; typedef struct NODE diff --git a/VaninVM/LocalVars.c b/VaninVM/LocalVars.c index fbaab2c..21e5a83 100644 --- a/VaninVM/LocalVars.c +++ b/VaninVM/LocalVars.c @@ -4,35 +4,36 @@ long long getlocal_int(context* cont, int id) { long long result; - result = ((long long*)(cont->locals))[id]; + result = (cont->locals)[id].i_data; return result; } double getlocal_double(context* cont, int id) { double result; - result = ((double*)(cont->locals))[id]; + result = (cont->locals)[id].d_data; return result; } -int getlocal_string(context* cont, int id) +char* getlocal_string(context* cont, int id) { - long long result; - result = ((long long*)(cont->locals))[id]; - return (int)result; + char* result; + result = (cont->locals)[id].s_data; + return result; } void putlocal_int(long long* num, context* cont, int id) { - memcpy(((long long*)cont->locals)+id, num, sizeof(long long)); + //memcpy(((long long*)cont->locals)+id, num, sizeof(long long)); + memcpy((cont->locals)+id, num, sizeof (long long)); } void putlocal_double(double* num, context* cont, int id) { - memcpy(((double*)cont->locals)+id, num, sizeof(double)); + memcpy((cont->locals)+id, num, sizeof(double)); } -void putlocal_string(int* str, context* cont, int id) +void putlocal_string(char* str, context* cont, int id) { - memcpy(((long long*)cont->locals)+id, (long long*)str, sizeof(long long)); + memcpy((cont->locals)+id, str, sizeof(int*)); } diff --git a/VaninVM/LocalVars.h b/VaninVM/LocalVars.h index efb873d..581a0db 100644 --- a/VaninVM/LocalVars.h +++ b/VaninVM/LocalVars.h @@ -4,9 +4,9 @@ long long getlocal_int(context*, int); double getlocal_double(context*, int); -int getlocal_string(context*, int); +char* getlocal_string(context*, int); void putlocal_int(long long*, context*, int); void putlocal_double(double*, context*, int); -void putlocal_string(int*, context*, int); +void putlocal_string(char*, context*, int); #endif \ No newline at end of file diff --git a/VaninVM/Main.c b/VaninVM/Main.c index c476d4e..f7c07b3 100644 --- a/VaninVM/Main.c +++ b/VaninVM/Main.c @@ -21,7 +21,7 @@ int main(int argc, char** argv) double d1, d2; long long i1, i2; short s1, s2; - char* code; + char *code; int ip, startfunc; //ConstSection Variables @@ -52,7 +52,7 @@ int main(int argc, char** argv) initTOS(); - initRStack(); + initRStack(1000); ip = 0; current_context = create_context(startfunc, phash_table, &code); @@ -596,7 +596,7 @@ int main(int argc, char** argv) } } remove_context(current_context); - + getchar(); return 0; } diff --git a/VaninVM/ReturnStack.c b/VaninVM/ReturnStack.c index 45e33b1..85b9522 100644 --- a/VaninVM/ReturnStack.c +++ b/VaninVM/ReturnStack.c @@ -12,9 +12,9 @@ int pop_ret() return num; } -int initRStack() +int initRStack(int size) { - RStack = (int*)calloc(1, 1000); + RStack = (int*)calloc(1, size); rp = 0; return 0; } \ No newline at end of file diff --git a/VaninVM/ReturnStack.h b/VaninVM/ReturnStack.h index 317baf1..74fdab7 100644 --- a/VaninVM/ReturnStack.h +++ b/VaninVM/ReturnStack.h @@ -3,7 +3,7 @@ int* RStack; int rp; -int initRStack(); +int initRStack(int); void push_ret(int); int pop_ret(); #endif \ No newline at end of file