language

Some fools attempt at an interpreted language
Log | Files | Refs

commit 982257dadd4f34cc13de98199a6b7afd51737f0c
parent 6e7aa81b58e74af3c25ce7cc3edb77e8264bd0fd
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Wed Mar 23 21:53:20 2016

made the stack work,  in progress,  is revisioned and also working

Diffstat:
 .gitignore                             |  1 +-
 src/vm/inc/bc.h                        | 19 ++++++-
 src/vm/inc/pc.h                        |  6 +-
 src/vm/inc/rt.h                        |  3 +-
 src/vm/src/bc.c                        | 93 ++++++++++++++++++++++++++---------
 src/vm/src/pc.c                        | 46 +----------------
 src/vm/src/stk.c                       | 29 +++++++++--
 src/vm/tests/cases/bc/Makefile         | 18 +-------
 src/vm/tests/cases/bc/bytecode         | Bin 13 -> 0 bytes
 src/vm/tests/cases/bc/expected_output  | 27 ++++++++--
 src/vm/tests/cases/bc/test.c           | 23 +++------
 src/vm/tests/cases/ht/Makefile         | 25 +---------
 src/vm/tests/cases/ht/expected_output  |  6 +--
 src/vm/tests/cases/ht/test.c           | 46 +-----------------
 src/vm/tests/cases/stk/expected_output |  9 +--
 src/vm/tests/cases/stk/test.c          | 29 ++++++-----
 16 files changed, 180 insertions(+), 200 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ .*.swp *.o +toi diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -25,6 +25,11 @@ typedef struct bc_cont { int sarg[3]; } bc_cont; +typedef struct bc_t { + bc_addr size; + bc_cont** heap; +} bc_t; + #include "is.h" /* Handles allocation for new `bc_cont` instances @@ -50,9 +55,19 @@ byte_t* get_dync_arg(FILE*, int*); */ void process_args(bc_cont*); -/* Gets bytecode size +/* Reads program into bc_cont instances + * FILE* - File descriptor + * bc_addr* - pointer to size variable + */ +bc_cont** bc_read(FILE* f, bc_addr*); + +/* Reads program into bc_t instance * char* - filename */ -size_t bc_getsize(char*); +bc_t* bc_init(char*); + +/* Deletes instance of bc_t* + */ +void bc_del(bc_t*); #endif // BC_H diff --git a/src/vm/inc/pc.h b/src/vm/inc/pc.h @@ -25,11 +25,11 @@ typedef struct pc_addr_stk { /* pc_t structure */ typedef struct pc_t { - size_t size; + pc_addr limit; pc_addr address; - bc_cont* line; pc_addr_stk* stk; - bc_cont** heap; + bc_cont* line; + bc_t program; } pc_t; /* Initalizes program counter, returns pc_t* instance diff --git a/src/vm/inc/rt.h b/src/vm/inc/rt.h @@ -19,9 +19,8 @@ * stack - `stk.h` stack instance[0] * argstk- `stk.h` stack instance[1] * vars - `ns.h` namespace instance - * gvars - `ns.h` namespace container instance * - * [0] This is the stack register used at runtime to push/pop variable + * [0] This is the stack register used at runtime to push/pop variable * containers. * [1] Function calls implement this stack to load variables as arguements. */ diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -3,10 +3,10 @@ #include "bc.h" -#include "is.h" #include "fh.h" #include "var.h" #include "helper.h" +#include "is.h" /* Handles allocation for new `bc_cont` instances */ @@ -30,19 +30,19 @@ bc_cont* bc_cont_new(void) return new; } -void bc_cont_del(bc_cont* root) +void bc_cont_del(bc_cont* ins) { - if (root != NULL) + if (ins != NULL) { - if (root->args[0] != NULL) free(root->args[0]); - if (root->args[1] != NULL) free(root->args[1]); - if (root->args[2] != NULL) free(root->args[2]); + if (ins->args[0] != NULL) free(ins->args[0]); + if (ins->args[1] != NULL) free(ins->args[1]); + if (ins->args[2] != NULL) free(ins->args[2]); - if (root->varg[0] != NULL) var_del(root->varg[0]); - if (root->varg[1] != NULL) var_del(root->varg[1]); - if (root->varg[2] != NULL) var_del(root->varg[2]); + if (ins->varg[0] != NULL) var_del(ins->varg[0]); + if (ins->varg[1] != NULL) var_del(ins->varg[1]); + if (ins->varg[2] != NULL) var_del(ins->varg[2]); - free(root); + free(ins); } } @@ -140,20 +140,25 @@ void process_args(bc_cont* ins) } } } -/* Gets bytecode size - * char* - filename + +/* Reads program into bc_cont instances + * FILE* - File descriptor + * bc_addr* - pointer to size variable */ -size_t bc_getsize(char* fname) +bc_cont** bc_read(FILE* f, bc_addr* len) { - FILE* f; - size_t rv = 0; - size_t fsize; - byte_t byte; + N_ASSERT(f, "bc_read\n"); + + long fsize = read_size(f); + + bc_addr addr = 0; bc_cont* ptr; + byte_t byte; - f = fopen(fname, "rb"); - fsize = read_size(f); + bc_cont** heap = (bc_cont**)malloc(sizeof(bc_cont*)*fsize); + N_ASSERT(heap, "bc_read\n"); + /* Loop through file byte-by-byte */ while (ftell(f) < fsize) { ptr = bc_cont_new(); @@ -164,12 +169,56 @@ size_t bc_getsize(char* fname) get_args(f, ptr); - bc_cont_del(ptr); + process_args(ptr); + + ptr->real_addr = addr; - rv++; + heap[addr] = ptr; + + addr++; } + *len = addr; + return heap; +} + +/* Reads program into bc_t instance + * char* - filename + */ +bc_t* bc_init(char* fname) +{ + N_ASSERT(fname, "bc_init\n"); + + FILE* f; + + bc_t* program; + + f = fopen(fname, "rb"); + + program = (bc_t*)malloc(sizeof(bc_t)); + N_ASSERT(program, "bc_read\n"); + + program->heap = bc_read(f, &program->size); + fclose(f); - return rv; + return program; +} + +/* Deletes instance of bc_t* + */ +void bc_del(bc_t* program) +{ + N_ASSERT(program, "bc_del\n"); + + int i; + for (i=0; i < program->size; i++) + { + if (program->heap[i] != NULL) + bc_cont_del(program->heap[i]); + } + + free(program->heap); + + free(program); } diff --git a/src/vm/src/pc.c b/src/vm/src/pc.c @@ -20,53 +20,13 @@ pc_t* pc_new(char* fname) pc->stk = pc_addr_stk_new(0); - pc->size = bc_getsize(fname); - - pc->heap = (bc_cont**)malloc(sizeof(bc_cont*)*pc->size); - - pc_read(fname, pc); + pc->program = bc_init(fname); pc_update(pc); - return pc; -} - -/* Initiates the first pass to take a raw binary file and translate it into a - * basic datastructure - */ -void pc_read(char* fname, pc_t* program) -{ - FILE* f; - byte_t byte; - long fsize; - - f = fopen(fname, "rb"); - fsize = read_size(f); - - bc_cont* ptr; - bc_addr addr = 0; + N_ASSERT(pc->line, "Error in creating new program counter\n"); - /* Loop through file byte-by-byte */ - while (ftell(f) < fsize) - { - ptr = bc_cont_new(); - - byte = read_byte(f); - - get_opcode(byte, ptr); - - get_args(f, ptr); - - process_args(ptr); - - ptr->real_addr = addr; - - pc_push(program, ptr); - - addr++; - } - - fclose(f); + return pc; } pc_addr_stk* pc_addr_stk_new(ns_addr address) diff --git a/src/vm/src/stk.c b/src/vm/src/stk.c @@ -109,15 +109,17 @@ void stk_scale(stk_line* stack) var_cont* stk_pop(stk_t* stack) { N_ASSERT(stack, "stk_pop\n"); - - ASSERT(((stack->stack->ptr - 1) > 1), "Stack Underflow\n"); + + ASSERT(((stack->stack->ptr - 1) > 0), "Stack Underflow\n"); + + var_cont* rv = stack->stack->data[stack->stack->ptr]; stack->stack->ptr = stack->stack->ptr - 1; N_ASSERT(stack->stack->data[stack->stack->ptr], "Found NULL value in stack, running away for obvious reasons.\n"); - return stack->stack->data[stack->stack->ptr]; + return rv; } /* Pushes var_cont* to the stack @@ -143,7 +145,7 @@ var_cont* stk_at(stk_t* stack, int n) { ASSERT(((stack->stack->ptr + n) < stack->stack->size), "Out of Bounds\n"); - var_cont* rv = stack->stack->data[stack->stack->ptr + n]; + var_cont* rv = stack->stack->data[stack->stack->ptr - n]; return rv; } @@ -155,6 +157,14 @@ var_cont* stk_at(stk_t* stack, int n) void stk_rot_top(stk_t* stack) { N_ASSERT(stack, "stk_rot_top\n"); + + var_cont* a; + var_cont* b; + + a = stk_pop(stack); + b = stk_pop(stack); + stk_push(stack, a); + stk_push(stack, b); } /* Rotates the top three elements of the stack @@ -164,4 +174,15 @@ void stk_rot_top(stk_t* stack) void stk_rot_three(stk_t* stack) { N_ASSERT(stack, "stk_rot_three\n"); + + var_cont* a; + var_cont* b; + var_cont* c; + + a = stk_pop(stack); + b = stk_pop(stack); + c = stk_pop(stack); + stk_push(stack, a); + stk_push(stack, b); + stk_push(stack, c); } diff --git a/src/vm/tests/cases/bc/Makefile b/src/vm/tests/cases/bc/Makefile @@ -9,27 +9,13 @@ DEPS = $(INC_DIR)/is_mdata.h \ fh.h \ bc.h \ is.h \ - var.h \ - stk.h \ - ht.h \ - ns.h \ - pc.h \ - rt.h \ - ins_def.h \ - proc.h + var.h OBJ = test.o \ $(SRC_DIR)/fh.o \ $(SRC_DIR)/bc.o \ $(SRC_DIR)/is.o \ - $(SRC_DIR)/var.o \ - $(SRC_DIR)/stk.o \ - $(SRC_DIR)/ht.o \ - $(SRC_DIR)/ns.o \ - $(SRC_DIR)/pc.o \ - $(SRC_DIR)/rt.o \ - $(SRC_DIR)/ins_def.o \ - $(SRC_DIR)/proc.o + $(SRC_DIR)/var.o OUT = test diff --git a/src/vm/tests/cases/bc/bytecode b/src/vm/tests/cases/bc/bytecode Binary files differ diff --git a/src/vm/tests/cases/bc/expected_output b/src/vm/tests/cases/bc/expected_output @@ -1,3 +1,24 @@ -ff: 1 0, 1, 20 , -20: 1, 1, 1 0, -f0: +20: 1, 6, 3 0, +23: 1, 3 0, 6 ff +20: 1, 6, 1 0, +23: 1, 1 0, 6 0 +20: 1, 6, 2 0, +23: 1, 2 0, 6 1 +60: +21: 1, 2 0, +21: 1, 3 0, +50: +61: +21: 1, 1 0, +21: 1, 2 0, +40: +22: 1, 2 0, +21: 1, 2 0, +2: +21: 1, 1 0, +21: 1, 2 0, +40: +22: 1, 1 0, +21: 1, 1 0, +2: +6f: diff --git a/src/vm/tests/cases/bc/test.c b/src/vm/tests/cases/bc/test.c @@ -17,20 +17,19 @@ void print_op(bc_cont* op) printf("%x: ", op->op); for (int i = 0; i < num_args && num_args != 0; i++) { - if (arg_types[i] == 1) + if (arg_types[i] == A_BYTE) { - printf("%x", op->args[i][0]); + printf("%x, ", op->args[i][0]); } else - if (arg_types[i] == 2) + if (arg_types[i] == A_NAME) { - printf("%x %x", op->args[i][1], op->args[i][0]); + printf("%x %x, ", op->args[i][1], op->args[i][0]); } else - if (arg_types[i] == 3) + if (arg_types[i] == A_DYNC) { for (int x = 0; x < op->sarg[i]; x++) printf("%x ", op->args[i][x]); } - printf(", "); } printf("\n"); @@ -41,16 +40,14 @@ int main(int argc, char** argv) init_mdata(); init_adata(); - // start testing - bc_cont* bc = bc_read("bytecode"); - bc_cont* ptr; + bc_t* bc = bc_init("bytecode"); - for (ptr = bc; ptr->next != NULL; ptr = ptr->next) + int i; + for (i = 0; i < bc->size; i++) { - print_op(ptr); + print_op(bc->heap[i]); } - bc_cont_del(bc); - // end testing + bc_del(bc); return 0; } diff --git a/src/vm/tests/cases/ht/Makefile b/src/vm/tests/cases/ht/Makefile @@ -1,25 +0,0 @@ -SRC_DIR = ../../../src -INC_DIR = ../../../inc - -CC = gcc -CFLAGS = -std=c99 -Wall -I$(INC_DIR) - -DEPS = i$(INC_DIR)/helper.h \ - var.h \ - ht.h - -OBJ = test.o \ - $(SRC_DIR)/var.o \ - $(SRC_DIR)/ht.o - -OUT = test - -%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(OUT): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ - -clean: - rm *.o - rm $(OUT) diff --git a/src/vm/tests/cases/ht/expected_output b/src/vm/tests/cases/ht/expected_output @@ -1,6 +0,0 @@ -0 -3 -4 -key2 value: 42 -key2 value: 32 -key3 value: 3.141600 diff --git a/src/vm/tests/cases/ht/test.c b/src/vm/tests/cases/ht/test.c @@ -1,46 +0,0 @@ -#include <stdio.h> - -#include "var.h" -#include "ht.h" - -int main(int argc, char* argv[]) -{ - ht_t* hashtable = ht_init(2); - ht_set(hashtable, "key1", var_new(VOID)); - ht_set(hashtable, "key2", var_new(G_INT)); - ht_set(hashtable, "key3", var_new(G_FLOAT)); - - printf("%i\n", ht_get(hashtable, "key1")->type); - printf("%i\n", ht_get(hashtable, "key2")->type); - printf("%i\n", ht_get(hashtable, "key3")->type); - - var_cont* var = ht_get(hashtable, "key2"); - - int idata; - - var_set(var, var_data_alloc_G_INT(42), G_INT); - - idata = var_data_get_G_INT(var); - - printf("key2 value: %i\n", idata); - - var_set(var, var_data_alloc_G_INT(32), G_INT); - - idata = var_data_get_G_INT(var); - - printf("key2 value: %i\n", idata); - - double ddata; - - var_cont* var1 = ht_get(hashtable, "key3"); - - var_set(var1, var_data_alloc_G_FLOAT(3.1416), G_FLOAT); - - ddata = var_data_get_G_FLOAT(var1); - - printf("key3 value: %f\n", ddata); - - ht_destroy(hashtable); - - return 0; -} diff --git a/src/vm/tests/cases/stk/expected_output b/src/vm/tests/cases/stk/expected_output @@ -1,4 +1,5 @@ -7, 5, 4, 3 -5, 4, 3, 0 -4, 5, 3, 0 -3, 4, 5, 0 +9, 8, 7, 6 +8, 7, 6, 0 +7, 8, 6, 0 +6, 7, 8, 0 +8, 7, 6, 0 diff --git a/src/vm/tests/cases/stk/test.c b/src/vm/tests/cases/stk/test.c @@ -18,28 +18,35 @@ int main(int argc, char* argv[]) { stk_t* new = stk_new(); - new->data = var_new(VOID); - - stk_push(&new, var_new(VOID)); - stk_push(&new, var_new(G_INT)); - stk_push(&new, var_new(G_FLOAT)); - stk_push(&new, var_new(G_CHAR)); - stk_push(&new, var_new(G_STR)); + stk_push(new, var_new(TEMPORARY, VOID)); + stk_push(new, var_new(TEMPORARY, G_INT)); + stk_push(new, var_new(TEMPORARY, G_FLOAT)); + stk_push(new, var_new(TEMPORARY, G_CHAR)); + stk_push(new, var_new(TEMPORARY, G_STR)); + printf("init: \n"); printstk(new); - stk_pop(&new); + stk_pop(new); + printf("stk_pop: \n"); printstk(new); - stk_rot_top(&new); + stk_rot_top(new); + printf("stk_rot_top: \n"); printstk(new); - stk_rot_top(&new); + stk_rot_top(new); + + stk_rot_three(new); + + printf("stk_rot_top + stk_rot_three: \n"); + printstk(new); - stk_rot_three(&new); + stk_rot_three(new); + printf("stk_rot_three: \n"); printstk(new); stk_del(new);