language

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

commit 9b1100a3cf7a0af9a0a9c19dc57263de6073160f
parent ab6d680ecd2b936bcc6df8c1ac29eef899551c66
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Mon Dec  7 20:22:21 2015

moved is_mdata to proper home, got rid of useless file position location

Diffstat:
 src/vm/Makefile        |  5 +++--
 src/vm/inc/bc.h        |  8 ++++----
 src/vm/inc/fh.h        |  6 +++---
 src/vm/inc/ins_mdata.h | 10 ++++++++++
 src/vm/inc/is.h        |  2 ++
 src/vm/inc/is_mdata    |  4 ----
 src/vm/src/bc.c        | 31 +++++++++++++++++--------------
 src/vm/src/fh.c        | 19 ++++++++-----------
 src/vm/src/is.c        |  4 +++-
 9 files changed, 50 insertions(+), 39 deletions(-)

diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -4,9 +4,10 @@ INC_DIR = inc CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) -DEPS = $(INC_DIR)/fh.h \ +DEPS = $(INC_DIR)/is_mdata.h \ + fh.h \ is.h \ - bc.h + bc.h \ OBJ = $(SRC_DIR)/main.o \ $(SRC_DIR)/fh.o \ diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -38,10 +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*); -byte_t* get_byte_arg(FILE*, long*); -byte_t* get_word_arg(FILE*, long*); -byte_t* get_dync_arg(FILE*, long*); +void get_args(FILE*, bc_cont*); +byte_t* get_byte_arg(FILE*); +byte_t* get_word_arg(FILE*); +byte_t* get_dync_arg(FILE*); /* 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,11 +10,11 @@ typedef unsigned char byte_t; -byte_t* read_until_null(FILE*, long*); +byte_t* read_until_null(FILE*); -byte_t* read_bytes(FILE*, long*, long); +byte_t* read_bytes(FILE*, long); -byte_t read_byte(FILE*, long*); +byte_t read_byte(FILE*); long read_size(FILE*); diff --git a/src/vm/inc/ins_mdata.h b/src/vm/inc/ins_mdata.h @@ -0,0 +1,10 @@ +#ifndef INS_MDATA_H +#define INS_MDATA_H + +#define INS_MDATA_DEF() \ +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_WORD, A_NULL); \ +INS_MDATA[4] = encode(1, A_DYNC, A_NULL, A_NULL); + +#endif diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -13,6 +13,8 @@ #include "bc.h" #include "fh.h" +#include "ins_mdata.h" + #define A_NULL 0 #define A_BYTE 1 #define A_WORD 2 diff --git a/src/vm/inc/is_mdata b/src/vm/inc/is_mdata @@ -1,4 +0,0 @@ -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_WORD, A_NULL); -INS_MDATA[4] = encode(1, A_DYNC, A_NULL, A_NULL); diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -12,6 +12,10 @@ bc_cont* bc_cont_new(void) fprintf(stderr, "Cannot allocate memory\n"); exit(1); } + new->args[0] = NULL; + new->args[1] = NULL; + new->args[2] = NULL; + return new; } @@ -33,7 +37,7 @@ 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, bc_cont* ins) { int num_args, arg_types[3]; @@ -44,32 +48,32 @@ void get_args(FILE* f, long* f_pos, bc_cont* ins) { if (arg_types[x] == A_BYTE) { - ins->args[x] = get_byte_arg(f, f_pos); + ins->args[x] = get_byte_arg(f); } else if (arg_types[x] == A_WORD) { - ins->args[x] = get_word_arg(f, f_pos); + ins->args[x] = get_word_arg(f); } else if (arg_types[x] == A_DYNC) { - ins->args[x] = get_dync_arg(f, f_pos); + ins->args[x] = get_dync_arg(f); } } } -byte_t* get_byte_arg(FILE* f, long* f_pos) +byte_t* get_byte_arg(FILE* f) { - return read_bytes(f, f_pos, 1); + return read_bytes(f, 1); } -byte_t* get_word_arg(FILE* f, long* f_pos) +byte_t* get_word_arg(FILE* f) { - return read_bytes(f, f_pos, 2); + return read_bytes(f, 2); } -byte_t* get_dync_arg(FILE* f, long* f_pos) +byte_t* get_dync_arg(FILE* f) { - return read_until_null(f, f_pos); + return read_until_null(f); } bc_cont* bc_read(char* fname) @@ -78,7 +82,6 @@ bc_cont* bc_read(char* fname) begin to read file byte-by-byte */ FILE* f; byte_t byte; - long f_pos = 0; long fsize; f = fopen(fname, "rb"); @@ -88,11 +91,11 @@ bc_cont* bc_read(char* fname) bc_cont *ptr = root; /* Loop through file byte-by-byte */ - while (f_pos<fsize) + while (ftell(f)<fsize) { - byte = read_byte(f, &f_pos); + byte = read_byte(f); get_opcode(byte, ptr); - get_args(f, &f_pos, ptr); + get_args(f, ptr); ptr = bc_cont_push(ptr); } diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -3,40 +3,37 @@ #include "fh.h" -byte_t* read_until_null(FILE* f, long* f_pos) +byte_t* read_until_null(FILE* f) { - long f_pos_i = *f_pos; + long f_pos_i = ftell(f); // This bit needs to be refactored. - byte_t byte = read_byte(f, f_pos); + byte_t byte = read_byte(f); while (byte != 0) { - byte = read_byte(f, f_pos); + byte = read_byte(f); } - long bytes = (*f_pos - f_pos_i); - *f_pos = f_pos_i; + long bytes = (ftell(f) - f_pos_i); fseek(f, 0-bytes, SEEK_CUR); - return read_bytes(f, f_pos, bytes); + return read_bytes(f, bytes); } -byte_t* read_bytes(FILE* f, long* f_pos, long bytes) +byte_t* read_bytes(FILE* f, long bytes) { 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) { - (*f_pos)++; return fgetc(f); } diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -4,6 +4,8 @@ #include "bc.h" #include "fh.h" +#include "ins_mdata.h" + void get_opcode(byte_t byte, bc_cont* ins) { ins->op = byte; @@ -25,5 +27,5 @@ void init(void) void init_mdata(void) { - #include "is_mdata" + INS_MDATA_DEF(); }