Added constant-support
This commit is contained in:
parent
17e632ad0b
commit
792724bcd1
5 changed files with 72 additions and 11 deletions
|
@ -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<First_Header.size_const; i++)
|
||||
{
|
||||
if (j==First_Header.count_const)
|
||||
break;
|
||||
|
||||
if (buffer[i] == 0)
|
||||
(*(index))[j++]=&buffer[i+1];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -4,5 +4,6 @@
|
|||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char* read_bytecode(char*);
|
||||
char* read_bytecode(FILE*);
|
||||
int read_constant(FILE*, int*, char***);
|
||||
#endif
|
|
@ -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;
|
||||
}
|
|
@ -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,
|
||||
|
|
|
@ -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) \
|
||||
|
|
Loading…
Reference in a new issue