commit aabc192bb719d0bdeb67726bd8716eea51a2aebe parent 65895029243ea3962d2d2a4e379ab14b324c6816 Author: Paul Longtine <paullongtine@gmail.com> Date: Fri Dec 4 13:49:50 2015 added inc/is_mdata, starting to work on parsing arguements Diffstat: src/vm/Makefile | 2 +- src/vm/inc/bc.h | 10 ++++++--- src/vm/inc/is.h | 13 +++++++++++- src/vm/inc/is_mdata | 3 +++- src/vm/src/bc.c | 60 ++++++++++++++++++++++++++++++++++++++++++------------ src/vm/src/is.c | 20 +++++++++++++++++- src/vm/src/main.c | 9 ++++++++- src/vm/test | 1 +- 8 files changed, 99 insertions(+), 19 deletions(-)
diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -2,7 +2,7 @@ SRC_DIR = src INC_DIR = inc CC = gcc -CFLAGS = -Wall -I$(INC_DIR) +CFLAGS = -std=c99 -Wall -I$(INC_DIR) DEPS = $(INC_DIR)/bc.h \ is.h diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -12,9 +12,9 @@ 'Bytecode Container' */ typedef struct bc_cont { - char op; - char* args[2]; - int* mdata; + unsigned char op; + unsigned char* args[3]; + unsigned char mdata; struct bc_cont* next; } bc_cont; @@ -33,6 +33,10 @@ void bc_cont_del(bc_cont*); function will read arguements */ void get_args(FILE**, long*, bc_cont**); +// meta-functions +unsigned char get_byte_arg(FILE**, long*); +unsigned char get_word_arg(FILE**, long*); +unsigned char get_dync_arg(FILE**, long*); /* Takes a FILE arguement, reads, returns size of file. diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -15,7 +15,13 @@ #define A_NULL 0 #define A_BYTE 1 #define A_WORD 2 -#define A_DYNM 3 +#define A_DYNC 3 + +#define encode(n, a0, a1, a2) \ + ( n << 6 | a0 << 4 | a1 << 2 | a2 ); + +// This array is populated by inc/is_mdata +unsigned char INS_MDATA[256]; /* Takes an opcode, fills metadata about that opcode (given that it exists) in the @@ -23,5 +29,10 @@ */ void get_opcode(char, bc_cont**); +void get_mdata(char, int*, int*); + +void init(void); + +void init_mdata(void); #endif // IS_H diff --git a/src/vm/inc/is_mdata b/src/vm/inc/is_mdata @@ -0,0 +1,3 @@ +INS_MDATA[1] = encode(0, A_NULL, A_NULL, A_NULL); +INS_MDATA[2] = encode(1, A_BYTE, A_NULL, A_NULL); +INS_MDATA[3] = encode(2, A_BYTE, A_DYNC, A_NULL); diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -22,12 +22,51 @@ void bc_cont_del(bc_cont* root) } free(root->args[0]); free(root->args[1]); + free(root->args[2]); free(root); } -void get_args(FILE** f, long* i, bc_cont** ins) +void get_args(FILE** f, long* f_pos, bc_cont** ins) { - + int num_args, + arg_types[3]; + + get_mdata((*ins)->mdata, &num_args, arg_types); + + for (int x = 0; x <= num_args; x++) + { + unsigned char arg; + if (arg_types[x] == A_BYTE) + { + arg = get_byte_arg(f, f_pos); + } else + if (arg_types[x] == A_WORD) + { + arg = get_word_arg(f, f_pos); + } else + if (arg_types[x] == A_DYNC) + { + arg = get_dync_arg(f, f_pos); + } + } +} + +unsigned char get_byte_arg(FILE** f, long* f_pos) +{ + unsigned char arg; + return arg; +} + +unsigned char get_word_arg(FILE** f, long* f_pos) +{ + unsigned char arg; + return arg; +} + +unsigned char get_dync_arg(FILE** f, long* f_pos) +{ + unsigned char arg; + return arg; } long read_size(FILE** f, char* fname) @@ -48,30 +87,25 @@ bc_cont* bc_read(char* fname) begin to read file byte-by-byte */ FILE* f; char byte; - long i = 0; + long f_pos = 0; long fsize = read_size(&f, fname); bc_cont *root = bc_cont_new(); bc_cont **ptr = &root; /* Loop through file byte-by-byte */ - while (i<fsize) + while (f_pos<fsize) { byte = fgetc(f); - i++; + f_pos++; + get_opcode(byte, ptr); - get_args(&f, &i, ptr); - // Set `ptr` to next element + get_args(&f, &f_pos, ptr); + (*ptr)->next = bc_cont_new(); ptr = &((*ptr)->next); } - // START TESTING CODE - for (ptr = &root; (*ptr)->next != NULL; ptr = &((*ptr)->next)) - { - printf("%x\n", (*ptr)->op); - } - // END TESTING CODE fclose(f); return root; diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -6,5 +6,23 @@ void get_opcode(char byte, bc_cont** ins) { (*ins)->op = byte; - //(*ins)->mdata = INS_ARGS[byte]; + (*ins)->mdata = INS_MDATA[byte]; +} + +void get_mdata(char byte, int* n, int* at) +{ + *n = (byte & (3 << 6)) >> 6; + at[0] = (byte & (3 << 4)) >> 4; + at[1] = (byte & (3 << 2)) >> 2; + at[2] = (byte & 3) ; +} + +void init(void) +{ + init_mdata(); +} + +void init_mdata(void) +{ + #include "is_mdata" } diff --git a/src/vm/src/main.c b/src/vm/src/main.c @@ -1,12 +1,21 @@ #include <stdio.h> +#include "is.h" #include "bc.h" int main(int argc, char** argv) { if (argc < 2) return -1; + init(); + // start testing bc_cont* bc = bc_read(argv[1]); + bc_cont** ptr; + + for (ptr = &bc; (*ptr)->next != NULL; ptr = &((*ptr)->next)) + { + printf("%x, %x\n", (*ptr)->op, (*ptr)->mdata); + } bc_cont_del(bc); // end testing diff --git a/src/vm/test b/src/vm/test @@ -0,0 +1 @@ +3+ \ No newline at end of file