language

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit ab6d680ecd2b936bcc6df8c1ac29eef899551c66
parent f51869be325abe62fa3ecccde0df0f3eabc3f516
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Mon Dec  7 16:54:05 2015

I am now sane.

Diffstat:
 src/vm/inc/bc.h   | 20 +++++---------------
 src/vm/inc/fh.h   |  8 ++++----
 src/vm/inc/is.h   |  2 +-
 src/vm/src/bc.c   | 41 +++++++++++++++++++++--------------------
 src/vm/src/fh.c   | 46 +++++++++++++++++++++++++++-------------------
 src/vm/src/is.c   |  6 +++---
 src/vm/src/main.c | 27 +++++++++++++++------------
 7 files changed, 76 insertions(+), 74 deletions(-)

diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -27,7 +27,7 @@ typedef struct bc_cont { */ bc_cont* bc_cont_new(void); -bc_cont** bc_cont_push(bc_cont**); +bc_cont* bc_cont_push(bc_cont*); /* Deallocates all the things, assuming the arguement is the root. @@ -38,20 +38,10 @@ void bc_cont_del(bc_cont*); Given a file object, and an instance of `bc_cont` with proper metadata, this function will read arguements */ -void get_args(FILE**, long*, bc_cont**); -void get_byte_arg(FILE**, long*, byte_t**); -void get_word_arg(FILE**, long*, byte_t**); -void get_dync_arg(FILE**, long*, byte_t**); - -/* - Takes a FILE arguement, reads, returns size of file. - Useful \w loops. -*/ -long read_size(FILE**, char*); - -void read_until_null(FILE**, long*, byte_t**); - -void read_bytes(FILE**, long*, int, byte_t**); +void get_args(FILE*, long*, bc_cont*); +byte_t* get_byte_arg(FILE*, long*); +byte_t* get_word_arg(FILE*, long*); +byte_t* get_dync_arg(FILE*, long*); /* Initiates the first pass to take a raw binary file and translate it into a diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -10,12 +10,12 @@ typedef unsigned char byte_t; -void read_until_null(FILE**, long*, byte_t**); +byte_t* read_until_null(FILE*, long*); -void read_bytes(FILE**, long*, int, byte_t**); +byte_t* read_bytes(FILE*, long*, long); -byte_t read_byte(FILE**, long*); +byte_t read_byte(FILE*, long*); -long read_size(FILE**, char*); +long read_size(FILE*); #endif // FH_H diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -28,7 +28,7 @@ byte_t INS_MDATA[256]; Takes an opcode, fills metadata about that opcode (given that it exists) in the `bc_cont` structure */ -void get_opcode(byte_t, bc_cont**); +void get_opcode(byte_t, bc_cont*); void get_mdata(byte_t, int*, int*); diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -15,10 +15,10 @@ bc_cont* bc_cont_new(void) return new; } -bc_cont** bc_cont_push(bc_cont** head) +bc_cont* bc_cont_push(bc_cont* head) { - (*head)->next = bc_cont_new(); - return &((*head)->next); + head->next = bc_cont_new(); + return head->next; } void bc_cont_del(bc_cont* root) @@ -33,43 +33,43 @@ void bc_cont_del(bc_cont* root) free(root); } -void get_args(FILE** f, long* f_pos, 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); + get_mdata(ins->mdata, &num_args, arg_types); for (int x = 0; x < num_args; x++) { if (arg_types[x] == A_BYTE) { - get_byte_arg(f, f_pos, &(*ins)->args[x]); + ins->args[x] = get_byte_arg(f, f_pos); } else if (arg_types[x] == A_WORD) { - get_word_arg(f, f_pos, &(*ins)->args[x]); + ins->args[x] = get_word_arg(f, f_pos); } else if (arg_types[x] == A_DYNC) { - get_dync_arg(f, f_pos, &(*ins)->args[x]); + ins->args[x] = get_dync_arg(f, f_pos); } } } -void get_byte_arg(FILE** f, long* f_pos, byte_t** arg) +byte_t* get_byte_arg(FILE* f, long* f_pos) { - read_bytes(f, f_pos, 1, arg); + return read_bytes(f, f_pos, 1); } -void get_word_arg(FILE** f, long* f_pos, byte_t** arg) +byte_t* get_word_arg(FILE* f, long* f_pos) { - read_bytes(f, f_pos, 2, arg); + return read_bytes(f, f_pos, 2); } -void get_dync_arg(FILE** f, long* f_pos, byte_t** arg) +byte_t* get_dync_arg(FILE* f, long* f_pos) { - read_until_null(f, f_pos, arg); + return read_until_null(f, f_pos); } bc_cont* bc_read(char* fname) @@ -79,23 +79,24 @@ bc_cont* bc_read(char* fname) FILE* f; byte_t byte; long f_pos = 0; - long fsize = read_size(&f, fname); + long fsize; + + f = fopen(fname, "rb"); + fsize = read_size(f); bc_cont *root = bc_cont_new(); - bc_cont **ptr = &root; + bc_cont *ptr = root; /* Loop through file byte-by-byte */ while (f_pos<fsize) { - byte = read_byte(&f, &f_pos); - + byte = read_byte(f, &f_pos); get_opcode(byte, ptr); - get_args(&f, &f_pos, ptr); + get_args(f, &f_pos, ptr); ptr = bc_cont_push(ptr); } fclose(f); - return root; } diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -3,42 +3,50 @@ #include "fh.h" -void read_until_null(FILE** f, long* f_pos, byte_t** buffer) +byte_t* read_until_null(FILE* f, long* f_pos) { long f_pos_i = *f_pos; - while (fgetc(*f) != NULL) (*f_pos)++; - - long bytes = *f_pos - f_pos_i; - fseek(*f, -bytes, SEEK_CUR); + + // This bit needs to be refactored. + byte_t byte = read_byte(f, f_pos); + while (byte != 0) + { + byte = read_byte(f, f_pos); + } + + long bytes = (*f_pos - f_pos_i); *f_pos = f_pos_i; - read_bytes(f, f_pos, bytes, buffer); + fseek(f, 0-bytes, SEEK_CUR); + + return read_bytes(f, f_pos, bytes); } -void read_bytes(FILE** f, long* f_pos, int bytes, byte_t** buffer) +byte_t* read_bytes(FILE* f, long* f_pos, long bytes) { - *buffer = (byte_t*)malloc(bytes*sizeof(byte_t)); - if (*buffer == NULL) - return; - fread(*buffer, bytes, 1, *f); + byte_t* buffer = (byte_t*)malloc(bytes*sizeof(byte_t)); + if (buffer == NULL) + return buffer; + + fread(buffer, bytes, 1, f); *f_pos = *f_pos + bytes; + + return buffer; } -byte_t read_byte(FILE** f, long* f_pos) +byte_t read_byte(FILE* f, long* f_pos) { - byte_t byte = fgetc(*f); (*f_pos)++; - return byte; + return fgetc(f); } -long read_size(FILE** f, char* fname) +long read_size(FILE* f) { - *f = fopen(fname, "rb"); - fseek(*f, 0, SEEK_END); + fseek(f, 0, SEEK_END); long fsize = 0; - fsize = ftell(*f); - rewind(*f); + fsize = ftell(f); + rewind(f); return fsize; } diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -4,10 +4,10 @@ #include "bc.h" #include "fh.h" -void get_opcode(byte_t byte, bc_cont** ins) +void get_opcode(byte_t byte, bc_cont* ins) { - (*ins)->op = byte; - (*ins)->mdata = INS_MDATA[byte]; + ins->op = byte; + ins->mdata = INS_MDATA[byte]; } void get_mdata(byte_t byte, int* n, int* at) diff --git a/src/vm/src/main.c b/src/vm/src/main.c @@ -11,24 +11,27 @@ int main(int argc, char** argv) // start testing bc_cont* bc = bc_read(argv[1]); - bc_cont** ptr; - bc_cont* thing; - - for (ptr = &bc; (*ptr)->next != NULL; ptr = &((*ptr)->next)) + bc_cont* ptr; + + for (ptr = bc; ptr->next != NULL; ptr = ptr->next) { - thing = *ptr; - if (thing->op == 2) + if (ptr->op == 1) + { + printf("%x:\n", ptr->op); + } + if (ptr->op == 2) { - printf("%x\n", thing->args[0][0]); + printf("%x: %x\n", ptr->op, ptr->args[0][0]); } - if (thing->op == 3) + if (ptr->op == 3) { - printf("%x, %x %x\n", thing->args[0][0], thing->args[1][0], thing->args[1][1]); + printf("%x: %x, %x %x\n", ptr->op, ptr->args[0][0], ptr->args[1][0], ptr->args[1][1]); } - if (thing->op == 4) + if (ptr->op == 4) { - int len = sizeof(thing->args[0]); - for (int i = 0; i < len; i++) printf("%x ", thing->args[0][i]); + printf("%x: ", ptr->op); + int len = sizeof(ptr->args[0]); + for (int i = 0; i < len; i++) printf("%x ", ptr->args[0][i]); printf("\n"); } }