commit 65895029243ea3962d2d2a4e379ab14b324c6816 parent ef0128e522c973a7379962a48fd65545f88f0225 Author: Paul Longtine <paullongtine@gmail.com> Date: Thu Dec 3 14:13:59 2015 Made framework to load bytecode objects into a datastructure Diffstat: doc/SPECIFICATION | 3 +-- src/vm/Makefile | 6 ++-- src/vm/inc/bc.h | 49 +++++++++++++++++++++++++++++++++++- src/vm/inc/fh.h | 27 +------------------- src/vm/inc/is.h | 27 +++++++++++++++++++- src/vm/main | Bin 9114 -> 0 bytes src/vm/src/bc.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/vm/src/fh.c | 62 +--------------------------------------------- src/vm/src/is.c | 9 ++++++- src/vm/src/main.c | 21 +++------------ src/vm/test | 1 +- 11 files changed, 173 insertions(+), 110 deletions(-)
diff --git a/doc/SPECIFICATION b/doc/SPECIFICATION @@ -83,7 +83,7 @@ Variable management DEC S<type> D<ref> - declare variable of type LOV D<ref> - loads reference variable on to stack STV D<ref> - stores TOS to reference variable - LOC D<ref> - loads constant + LOC D<ref> D<data> - loads constant ------------------------------------------------------------------------------- Type management @@ -140,7 +140,6 @@ Manipulating High-order Object Variables sorts) for <ref> object. ------------------------------------------------------------------------------- - LEXICAL ANALYSIS Going from code to bytecode is what this section is all about. First off an diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -4,10 +4,12 @@ INC_DIR = inc CC = gcc CFLAGS = -Wall -I$(INC_DIR) -DEPS = $(INC_DIR)/fh.h +DEPS = $(INC_DIR)/bc.h \ + is.h OBJ = $(SRC_DIR)/main.o \ - $(SRC_DIR)/fh.o + $(SRC_DIR)/bc.o \ + $(SRC_DIR)/is.o OUT = main diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -0,0 +1,49 @@ +/* + `bc` handles bytecode objects. +*/ + +#ifndef FH_H +#define FH_H + +#include <stdlib.h> +#include <stdio.h> + +/* + 'Bytecode Container' +*/ +typedef struct bc_cont { + char op; + char* args[2]; + int* mdata; + struct bc_cont* next; +} bc_cont; + +/* + Handles allocation for new `bc_cont` instances +*/ +bc_cont* bc_cont_new(void); + +/* + Deallocates all the things, assuming the arguement is the root. +*/ +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**); + +/* + Takes a FILE arguement, reads, returns size of file. + Useful \w loops. +*/ +long read_size(FILE**, char*); + +/* + 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 diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -1,27 +0,0 @@ -#ifndef FH_H -#define FH_H - -#include <stdlib.h> -#include <stdio.h> - -typedef struct func { - char op; - char* args; - struct func* next; -} func; - -func* at(func*, int); - -void push(func**, func*); - -void pop(func**); - -void del(func*); - -/*Takes a FILE arguement, reads, returns size of file. - Useful \w loops. */ -long read_size(FILE**, char*); - -int init(FILE**); - -#endif diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -0,0 +1,27 @@ +/* + `is` holds the instruction set specification. + + Anything having to do with getting information about a given instruction will + be handled by this file. +*/ + +#ifndef IS_H +#define IS_H + +#include <stdlib.h> + +#include "bc.h" + +#define A_NULL 0 +#define A_BYTE 1 +#define A_WORD 2 +#define A_DYNM 3 + +/* + Takes an opcode, fills metadata about that opcode (given that it exists) in the + `bc_cont` structure +*/ +void get_opcode(char, bc_cont**); + + +#endif // IS_H diff --git a/src/vm/main b/src/vm/main Binary files differ diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "bc.h" +#include "is.h" + +bc_cont* bc_cont_new(void) +{ + bc_cont *new = (bc_cont*)malloc(sizeof(bc_cont)); + if (new == NULL) { + fprintf(stderr, "Cannot allocate memory\n"); + exit(1); + } + return new; +} + +void bc_cont_del(bc_cont* root) +{ + if (root->next != NULL) + { + bc_cont_del(root->next); + } + free(root->args[0]); + free(root->args[1]); + free(root); +} + +void get_args(FILE** f, long* i, bc_cont** ins) +{ + +} + +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; +} + +bc_cont* bc_read(char* fname) +{ + /* initialize datastructures for instructionstuffs, + begin to read file byte-by-byte */ + FILE* f; + char byte; + long i = 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) + { + byte = fgetc(f); + i++; + get_opcode(byte, ptr); + get_args(&f, &i, ptr); + // Set `ptr` to next element + (*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/fh.c b/src/vm/src/fh.c @@ -1,62 +0,0 @@ -#include <stdlib.h> - -#include "fh.h" - -func* at(func* root, int x) -{ - x--; - - if (root->next == NULL) - { - return root; - } - else - { - return at(root->next, x); - } -} - -void push(func** root, func* new_node) -{ - new_node->next = *root; - *root = new_node; -} - -void pop(func** root) -{ - if ((*root)->next != NULL) - { - func *old_node = (*root)->next; - free(*root); - *root = old_node; - } -} - -void del(func* root) -{ - if (root->next != NULL) - { - del(root->next); - } - - free(root); -} - -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; -} - -int init(char* fname) -{ - /* initialize datastructures for instructionstuffs, - begin to read file byte-by-byte */ - return 0; -} diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -1 +1,10 @@ +#include <stdlib.h> +#include "is.h" +#include "bc.h" + +void get_opcode(char byte, bc_cont** ins) +{ + (*ins)->op = byte; + //(*ins)->mdata = INS_ARGS[byte]; +} diff --git a/src/vm/src/main.c b/src/vm/src/main.c @@ -1,25 +1,14 @@ #include <stdio.h> -#include "fh.h" +#include "bc.h" int main(int argc, char** argv) { if (argc < 2) return -1; + // start testing + bc_cont* bc = bc_read(argv[1]); - /* START TESTING STUFF */ - FILE* f; - long i = 0; - long fsize = read_size(&f, argv[1]); - - while (i<fsize) - { - int test = fgetc(f); - i++; - printf("%x\n", (unsigned)test); - } - - fclose(f); - /* END TESTING STUFF */ - + bc_cont_del(bc); + // end testing return 0; } diff --git a/src/vm/test b/src/vm/test @@ -1 +0,0 @@ -loliyeateawtawet