language

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

commit 6f6e5d3c031528cc0ceb4089662e7970cacaaf14
parent aabc192bb719d0bdeb67726bd8716eea51a2aebe
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Sat Dec  5 19:28:12 2015

Made dynamic arguements work, refactored a smidge

Diffstat:
 src/vm/Makefile     | 10 ++++++----
 src/vm/inc/bc.h     | 27 +++++++++++++++++----------
 src/vm/inc/fh.h     | 19 +++++++++++++++++++
 src/vm/inc/is.h     |  7 ++++---
 src/vm/inc/is_mdata |  3 ++-
 src/vm/src/bc.c     | 37 +++++++++++--------------------------
 src/vm/src/fh.c     | 37 +++++++++++++++++++++++++++++++++++++
 src/vm/src/is.c     |  5 +++--
 src/vm/src/main.c   | 18 +++++++++++++++++-
 src/vm/test         | Bin 3 -> 14 bytes
 10 files changed, 116 insertions(+), 47 deletions(-)

diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -4,12 +4,14 @@ INC_DIR = inc CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) -DEPS = $(INC_DIR)/bc.h \ - is.h +DEPS = $(INC_DIR)/fh.h \ + is.h \ + bc.h OBJ = $(SRC_DIR)/main.o \ - $(SRC_DIR)/bc.o \ - $(SRC_DIR)/is.o + $(SRC_DIR)/fh.o \ + $(SRC_DIR)/is.o \ + $(SRC_DIR)/bc.o OUT = main diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -2,22 +2,26 @@ `bc` handles bytecode objects. */ -#ifndef FH_H -#define FH_H +#ifndef BC_H +#define BC_H #include <stdlib.h> #include <stdio.h> +#include "fh.h" + /* 'Bytecode Container' */ typedef struct bc_cont { - unsigned char op; - unsigned char* args[3]; - unsigned char mdata; + byte_t op; + byte_t* args[3]; + byte_t mdata; struct bc_cont* next; } bc_cont; +#include "is.h" + /* Handles allocation for new `bc_cont` instances */ @@ -33,10 +37,9 @@ 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*); +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. @@ -44,10 +47,14 @@ unsigned char get_dync_arg(FILE**, long*); */ long read_size(FILE**, char*); +void read_until_null(FILE**, long*, byte_t**); + +void read_bytes(FILE**, long*, int, byte_t**); + /* Initiates the first pass to take a raw binary file and translate it into a basic datastructure */ bc_cont* bc_read(char* fname); -#endif // FH_H +#endif // BC_H diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -0,0 +1,19 @@ +/* + 'fh.h' is here to provide useful functions to mess about with files. +*/ + +#ifndef FH_H +#define FH_H + +#include <stdio.h> +#include <stdlib.h> + +typedef unsigned char byte_t; + +void read_until_null(FILE**, long*, byte_t**); + +void read_bytes(FILE**, long*, int, byte_t**); + +long read_size(FILE**, char*); + +#endif // FH_H diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -11,6 +11,7 @@ #include <stdlib.h> #include "bc.h" +#include "fh.h" #define A_NULL 0 #define A_BYTE 1 @@ -21,15 +22,15 @@ ( n << 6 | a0 << 4 | a1 << 2 | a2 ); // This array is populated by inc/is_mdata -unsigned char INS_MDATA[256]; +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(char, bc_cont**); +void get_opcode(byte_t, bc_cont**); -void get_mdata(char, int*, int*); +void get_mdata(byte_t, int*, int*); void init(void); diff --git a/src/vm/inc/is_mdata b/src/vm/inc/is_mdata @@ -1,3 +1,4 @@ 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); +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 @@ -3,6 +3,7 @@ #include "bc.h" #include "is.h" +#include "fh.h" bc_cont* bc_cont_new(void) { @@ -33,52 +34,36 @@ void get_args(FILE** f, long* f_pos, bc_cont** ins) get_mdata((*ins)->mdata, &num_args, arg_types); - for (int x = 0; x <= num_args; x++) + for (int x = 0; x < num_args; x++) { - unsigned char arg; if (arg_types[x] == A_BYTE) { - arg = get_byte_arg(f, f_pos); + get_byte_arg(f, f_pos, &(*ins)->args[x]); } else if (arg_types[x] == A_WORD) { - arg = get_word_arg(f, f_pos); + get_word_arg(f, f_pos, &(*ins)->args[x]); } else if (arg_types[x] == A_DYNC) { - arg = get_dync_arg(f, f_pos); + get_dync_arg(f, f_pos, &(*ins)->args[x]); } } } -unsigned char get_byte_arg(FILE** f, long* f_pos) +void get_byte_arg(FILE** f, long* f_pos, byte_t** arg) { - unsigned char arg; - return arg; + read_bytes(f, f_pos, 1, arg); } -unsigned char get_word_arg(FILE** f, long* f_pos) +void get_word_arg(FILE** f, long* f_pos, byte_t** arg) { - unsigned char arg; - return arg; + read_bytes(f, f_pos, 2, arg); } -unsigned char get_dync_arg(FILE** f, long* f_pos) +void get_dync_arg(FILE** f, long* f_pos, byte_t** arg) { - unsigned char arg; - return arg; -} - -long read_size(FILE** f, char* fname) -{ - *f = fopen(fname, "rb"); - fseek(*f, 0, SEEK_END); - - long fsize = 0; - fsize = ftell(*f); - rewind(*f); - - return fsize; + read_until_null(f, f_pos, arg); } bc_cont* bc_read(char* fname) diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "fh.h" + +void read_until_null(FILE** f, long* f_pos, byte_t** buffer) +{ + long f_pos_i = *f_pos; + while (fgetc(*f) != NULL) (*f_pos)++; + + long bytes = *f_pos - f_pos_i; + fseek(*f, -bytes, SEEK_CUR); + *f_pos = f_pos_i; + + read_bytes(f, f_pos, bytes, buffer); +} + +void read_bytes(FILE** f, long* f_pos, int bytes, byte_t** buffer) +{ + *buffer = (byte_t*)malloc(bytes*sizeof(byte_t)); + if (*buffer == NULL) + return; + fread(*buffer, bytes, 1, *f); + *f_pos = *f_pos + bytes; +} + +long read_size(FILE** f, char* fname) +{ + *f = fopen(fname, "rb"); + fseek(*f, 0, SEEK_END); + + long fsize = 0; + fsize = ftell(*f); + rewind(*f); + + return fsize; +} diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -2,14 +2,15 @@ #include "is.h" #include "bc.h" +#include "fh.h" -void get_opcode(char byte, bc_cont** ins) +void get_opcode(byte_t byte, bc_cont** ins) { (*ins)->op = byte; (*ins)->mdata = INS_MDATA[byte]; } -void get_mdata(char byte, int* n, int* at) +void get_mdata(byte_t byte, int* n, int* at) { *n = (byte & (3 << 6)) >> 6; at[0] = (byte & (3 << 4)) >> 4; diff --git a/src/vm/src/main.c b/src/vm/src/main.c @@ -1,5 +1,6 @@ #include <stdio.h> +#include "fh.h" #include "is.h" #include "bc.h" @@ -11,10 +12,25 @@ 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)) { - printf("%x, %x\n", (*ptr)->op, (*ptr)->mdata); + thing = *ptr; + if (thing->op == 2) + { + printf("%x\n", thing->args[0][0]); + } + if (thing->op == 3) + { + printf("%x, %x %x\n", thing->args[0][0], thing->args[1][0], thing->args[1][1]); + } + if (thing->op == 4) + { + int len = sizeof(thing->args[0]); + for (int i = 0; i < len; i++) printf("%x ", thing->args[0][i]); + printf("\n"); + } } bc_cont_del(bc); diff --git a/src/vm/test b/src/vm/test Binary files differ