From 792724bcd14e86a4727104869674c90aadfd0057 Mon Sep 17 00:00:00 2001 From: AlexVanin Date: Mon, 27 Jan 2014 08:43:58 +0400 Subject: [PATCH] Added constant-support --- VaninVM/IOcode.c | 38 +++++++++++++++++++++++++++++++++----- VaninVM/IOcode.h | 3 ++- VaninVM/Main.c | 32 +++++++++++++++++++++++++++++++- VaninVM/OpCode.h | 2 ++ VaninVM/opc.txt | 8 ++++---- 5 files changed, 72 insertions(+), 11 deletions(-) diff --git a/VaninVM/IOcode.c b/VaninVM/IOcode.c index 3fa879a..c9c3c21 100644 --- a/VaninVM/IOcode.c +++ b/VaninVM/IOcode.c @@ -1,12 +1,40 @@ #include "IOcode.h" +#include "CodeHeader.h" -char* read_bytecode(char* path) +char* read_bytecode(FILE* stream) { - 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); + fread_s(buffer, 1000, sizeof(char), 1000, stream); //Early version without bytecode file return buffer; } + +int read_constant(FILE* stream, int* count, char*** index) +{ + int i, j; + char* buffer; + + fread_s(&First_Header, sizeof(First_Header), sizeof(First_Header), 1, stream); //Reading first part of header + *(count) = First_Header.count_const; + + buffer = (char*)malloc(First_Header.size_const); + *(index) = (char**)malloc(sizeof(char**)*2); + + + fread_s(buffer, First_Header.size_const, sizeof(char), First_Header.size_const, stream); //Reading constant values + + j=0; + if (First_Header.count_const>0) + { + (*(index))[j++] = buffer; + for (i = 0; i #include -char* read_bytecode(char*); +char* read_bytecode(FILE*); +int read_constant(FILE*, int*, char***); #endif \ No newline at end of file diff --git a/VaninVM/Main.c b/VaninVM/Main.c index 0ef1a76..ed562bf 100644 --- a/VaninVM/Main.c +++ b/VaninVM/Main.c @@ -1,19 +1,39 @@ #include "IOcode.h" #include "TOS.h" #include "OpCode.h" +#include "CodeHeader.h" + int main(int argc, char** argv) { + //ByteCode Variables + FILE* input; + + //ExecutionProcess Variables double d1, d2; long long i1, i2; + short s1; char* code; int ip; + //ConstSection Variables + int const_count; + char** const_index; + //Execution Variable int exec_status = 1; - code = read_bytecode("bytecode"); + fopen_s(&input, "bytecode", "rb"); + + //const_pull + read_constant(input, &const_count, &const_index); + + code=read_bytecode(input); + + fclose(input); + + initTOS(); ip = 0; @@ -35,6 +55,10 @@ int main(int argc, char** argv) i1 = *((long long*)(code+(++ip))); push_int(i1); ip++; break; + case SLOAD: + s1 = *((short*)(code+(++ip))); + push_int((long long)(const_index[s1])); + ip++; break; case DLOAD0: // DO(DLOAD0, "Load double 0 on TOS.", 1) push_double(0); @@ -158,6 +182,11 @@ int main(int argc, char** argv) push_double(d1); printf("%f", d1); ip++; break; + case SPRINT: + i1 = pop_int(); + push_int(i1); + printf("%s", (char*)i1); + ip++; break; case I2D: //DO(I2D, "Convert int on TOS to double.", 1) i1 = pop_int(); @@ -187,5 +216,6 @@ int main(int argc, char** argv) break; } } + getchar(); return 0; } \ No newline at end of file diff --git a/VaninVM/OpCode.h b/VaninVM/OpCode.h index 49eca60..ee490e1 100644 --- a/VaninVM/OpCode.h +++ b/VaninVM/OpCode.h @@ -5,6 +5,7 @@ enum opcode INVALID, DLOAD, ILOAD, + SLOAD, DLOAD0, ILOAD0, DLOAD1, @@ -27,6 +28,7 @@ enum opcode IAXOR, IPRINT, DPRINT, + SPRINT, I2D, D2I, SWAP, diff --git a/VaninVM/opc.txt b/VaninVM/opc.txt index 51b882b..eea5921 100644 --- a/VaninVM/opc.txt +++ b/VaninVM/opc.txt @@ -2,7 +2,7 @@ -DO(INVALID, "Invalid instruction.", 1) \ +DO(DLOAD, "Load double on TOS, inlined into insn stream.", 9) \ +DO(ILOAD, "Load int on TOS, inlined into insn stream.", 9) \ - DO(SLOAD, "Load string reference on TOS, next two bytes - constant id.", 3) \ + +DO(SLOAD, "Load string reference on TOS, next two bytes - constant id.", 3) \ +DO(DLOAD0, "Load double 0 on TOS.", 1) \ +DO(ILOAD0, "Load int 0 on TOS.", 1) \ DO(SLOAD0, "Load empty string on TOS.", 1) \ @@ -46,15 +46,15 @@ DO(LOADSVAR3, "Load string from variable 3, push on TOS.", 1) \ DO(STOREDVAR0, "Pop TOS and store to double variable 0.", 1) \ DO(STOREDVAR1, "Pop TOS and store to double variable 1.", 1) \ - DO(STOREDVAR2, "Pop TOS and store to double variable 0.", 1) \ + DO(STOREDVAR2, "Pop TOS and store to double variable 2.", 1) \ DO(STOREDVAR3, "Pop TOS and store to double variable 3.", 1) \ DO(STOREIVAR0, "Pop TOS and store to int variable 0.", 1) \ DO(STOREIVAR1, "Pop TOS and store to int variable 1.", 1) \ - DO(STOREIVAR2, "Pop TOS and store to int variable 0.", 1) \ + DO(STOREIVAR2, "Pop TOS and store to int variable 2.", 1) \ DO(STOREIVAR3, "Pop TOS and store to int variable 3.", 1) \ DO(STORESVAR0, "Pop TOS and store to string variable 0.", 1) \ DO(STORESVAR1, "Pop TOS and store to string variable 1.", 1) \ - DO(STORESVAR2, "Pop TOS and store to string variable 0.", 1) \ + DO(STORESVAR2, "Pop TOS and store to string variable 2.", 1) \ DO(STORESVAR3, "Pop TOS and store to string variable 3.", 1) \ DO(LOADDVAR, "Load double from variable, whose 2-byte is id inlined to insn stream, push on TOS.", 3) \ DO(LOADIVAR, "Load int from variable, whose 2-byte id is inlined to insn stream, push on TOS.", 3) \