commit 1a57060ad24708d470619d3b9d0c80b1cae96662 parent 646c1b1a5bb99d9c8fd25fbc24f85222a151d95d Author: Paul Longtine <paullongtine@gmail.com> Date: Sun May 1 00:11:22 2016 Fixed an error with 'new' statements Diffstat: src/lc/parser.py | 4 ++-- src/vm/inc/bc.h | 10 +++++----- src/vm/inc/helper.h | 10 +++++----- src/vm/inc/ins_mdata.h | 4 ++-- src/vm/inc/is.h | 2 +- src/vm/src/bc.c | 48 +++++++++++++++++++++++++++++++++++++----------- src/vm/src/is.c | 2 +- 7 files changed, 53 insertions(+), 27 deletions(-)
diff --git a/src/lc/parser.py b/src/lc/parser.py @@ -297,8 +297,8 @@ class Parser(): init=(lambda x: [ x.new_name(1), NewClass(x.eval_label(1), - x.eval_label(5), - x.eval_args(6)) + x.eval_label(4), + x.eval_args(5)) ]) ) diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -44,11 +44,11 @@ void bc_cont_del(bc_cont*); /* Given a file object, and an instance of `bc_cont` with proper metadata, this * function will read arguements into bc_cont. */ -void get_args(FILE*, bc_cont*); -byte_t* get_byte_arg(FILE*, int*); -byte_t* get_name_arg(FILE*, int*); -byte_t* get_addr_arg(FILE*, int*); -byte_t* get_dync_arg(FILE*, int*); +void get_opcode_adata(FILE*, bc_cont*); +byte_t* get_byte_arg (FILE*, int*); +byte_t* get_name_arg (FILE*, int*); +byte_t* get_addr_arg (FILE*, int*); +byte_t* get_dync_arg (FILE*, int*); /* Process arguements into typed & readable data * bc_cont* - bytecode container diff --git a/src/vm/inc/helper.h b/src/vm/inc/helper.h @@ -13,14 +13,14 @@ exit(1); \ } -#define SIZE_ASSERT(condition) \ - if (!(condition)) \ - { \ +#define SIZE_ASSERT(condition) \ + if (!(condition)) \ + { \ fprintf(stderr, "address exeeded namespace limit\n"); \ - exit(1); \ + exit(1); \ } -#define M_ASSERT(x) \ +#define M_ASSERT(x) \ if (x == NULL) \ { \ fprintf(stderr, "Could not allocate memory\n");\ diff --git a/src/vm/inc/ins_mdata.h b/src/vm/inc/ins_mdata.h @@ -19,9 +19,9 @@ /* This definition is ran in `is.c` * - * INS_MDATA[ <opcode> ] = encode( <number of arguements>, <type..3> ) + * INS_MDATA[ <opcode> ] <- encode( <number of arguements>, <type..3> ) */ -#define INS_MDATA_DEF() \ +#define INS_MDATA_DEF() \ /* NULL */ INS_MDATA[0x00] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* SYNC */ INS_MDATA[0x01] = encode(1, A_BYTE, A_BYTE, A_BYTE); \ /* PRINT */ INS_MDATA[0x02] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -23,7 +23,7 @@ byte_t INS_ADATA[0x100]; * byte_t - opcode * bc_cont* - Bytecode instruction */ -void get_opcode(byte_t, bc_cont*); +void get_opcode_mdata(byte_t, bc_cont*); /* Fills in metadata in @param byte_t. * byte_t - un-expanded metadata diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -25,7 +25,7 @@ bc_cont* bc_cont_new(void) new->sarg[0] = 0; new->sarg[1] = 0; - new->sarg[2] = 0; + return new; } @@ -48,28 +48,36 @@ void bc_cont_del(bc_cont* ins) /* Given a file object, and an instance of `bc_cont` with proper metadata, this * function will read arguements into bc_cont. + * + * 'bc_cont' proper metadata is generated by */ -void get_args(FILE* f, bc_cont* ins) +void get_opcode_adata(FILE* f, bc_cont* ins) { int num_args, arg_types[3]; + // `is.h`-> unencode unencode(ins->mdata, &num_args, arg_types); - for (int x = 0; x < num_args; x++) + int x; + for (x = 0; x < num_args; x++) { + // A_BYTE -> byte-length arguement if (arg_types[x] == A_BYTE) { ins->args[x] = get_byte_arg(f, &ins->sarg[x]); } else + // A_NAME -> name-length arguement if (arg_types[x] == A_NAME) { ins->args[x] = get_name_arg(f, &ins->sarg[x]); } else + // A_ADDR -> address-length arguement if (arg_types[x] == A_ADDR) { ins->args[x] = get_addr_arg(f, &ins->sarg[x]); } else + // A_DYNC -> dynamic-length arguement if (arg_types[x] == A_DYNC) { ins->args[x] = get_dync_arg(f, &ins->sarg[x]); @@ -98,6 +106,20 @@ byte_t* get_dync_arg(FILE* f, int* size) byte_t byte = read_byte(f); // This bit gets the length in bytes it needs to read into a buffer + // NOTE: + // There are certain 'holes' where, a certain number, ex. + // 0xFF00, 0xFF0001, 0xFFFF00, etc + // will yeild improper values due to the while loop below encountering + // a null octet prematurely. + // Numbers that work are the following: + // 0xFF01, 0xFF0101, 0xFFFF01 + // Whenever a seralizer is using dynamic arguements the seralizer must + // implement a method that asserts if a number is such that each octet + // does not equal 0. For a given bitwidth, the set of numbers is + // described roughly: + // { <valid octets> | !(0 in <octets>) } + // if the length of the body of a set of bytes is a member of + // <valid octets>, it can work with this function. while (byte != 0) { n = (n << 8 | byte); @@ -117,6 +139,7 @@ void process_args(bc_cont* ins) int num_args, arg_types[3]; + // `is.h`-> unencode unencode(ins->adata, &num_args, arg_types); int x; @@ -165,20 +188,22 @@ bc_cont** bc_read(FILE* f, bc_addr* len) /* Loop through file byte-by-byte */ while (ftell(f) < fsize) { + // Creates a new bc_cont instance ptr = bc_cont_new(); + // Gets the opcode byte = read_byte(f); - - get_opcode(byte, ptr); - - get_args(f, ptr); - + // Gets opcode metadata + get_opcode_mdata(byte, ptr); + // Gets opcode arguement metadata + get_opcode_adata(f, ptr); + // Process raw arguements process_args(ptr); - + // Set the real address of instruction ptr->real_addr = addr; - + // Add instruction to program heap[addr] = ptr; - + // Increment address addr++; } @@ -202,6 +227,7 @@ bc_t* bc_init(char* fname) program = (bc_t*)malloc(sizeof(bc_t)); N_ASSERT(program, "bc_read\n"); + // Read bytecode into array of bc_cont* program->heap = bc_read(f, &program->size); fclose(f); diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -12,7 +12,7 @@ * byte_t - opcode * bc_cont* - Bytecode instruction */ -void get_opcode(byte_t byte, bc_cont* ins) +void get_opcode_mdata(byte_t byte, bc_cont* ins) { ins->op = byte; ins->mdata = INS_MDATA[byte];