From e16553d93b1acc1771ac3429042c2e8759f7e656 Mon Sep 17 00:00:00 2001 From: Veit Heller Date: Sun, 21 Dec 2014 12:31:10 +0100 Subject: [PATCH] Replaced opcode defines with enum --- src/opcode.h | 56 +++++++++++++++++++++++++++------------------------- src/vm.c | 17 +++++++++++++--- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/opcode.h b/src/opcode.h index f064ac7..7807abd 100644 --- a/src/opcode.h +++ b/src/opcode.h @@ -1,59 +1,61 @@ #ifndef OPCODE_H #define OPCODE_H +enum { /*Addition operation*/ -#define IADD 1 + IADD = 1, /*Subtraction operation*/ -#define ISUB 2 + ISUB, /*Multiplication operation*/ -#define IMULT 3 + IMULT, /*Division operation*/ -#define IDIV 4 + IDIV, /*Modulo operation*/ -#define IMOD 5 + IMOD, /*< operation*/ -#define ILT 6 + ILT, /*== operation*/ -#define IEQ 7 + IEQ, /*> operation*/ -#define IGT 8 + IGT, /*branch operation*/ -#define BR 9 + BR, /*branch if true operation*/ -#define BRT 10 + BRT, /*branch if false operation*/ -#define BRF 11 + BRF, /*put operation*/ -#define ICONST 12 + ICONST, /*load variable operation*/ -#define LOAD 13 + LOAD, /*load global variable operation*/ -#define GLOAD 14 + GLOAD, /*store variable operation*/ -#define STORE 15 + STORE, /*store global variable operation*/ -#define GSTORE 16 + GSTORE, /*print operation*/ -#define PRINT 17 + PRINT, /*pop operation*/ -#define POP 18 + POP, /*end/halt operation*/ -#define HALT 19 + HALT, /*<= operation*/ -#define ILEQ 20 + ILEQ, /*>= operation*/ -#define IGEQ 21 + IGEQ, /*call subroutine operation*/ -#define CALL 22 + CALL, /*return from subroutine operation*/ -#define RET 23 + RET, /*print integer operation*/ -#define IPRINT 24 + IPRINT, /*fetch operation*/ -#define FETCH 25 + FETCH, /*++ operation*/ -#define IINC 26 + IINC, /*-- operation*/ -#define IDEC 27 + IDEC +}; /** * @brief An instruction struct diff --git a/src/vm.c b/src/vm.c index 71827f6..4af3032 100644 --- a/src/vm.c +++ b/src/vm.c @@ -33,7 +33,9 @@ void vm_execute(int code[], int ip, int datasize, unsigned long length){ register int sp = -1; register int fp = -1; int nargs, addr, a, b; + instruction* ins = setup_instructions(); + while(ip < length){ int opcode = code[ip]; ip++; @@ -168,6 +170,7 @@ program vm_compile(char *filename){ char** command = NULL; unsigned long size = 100; int* code = malloc(size * sizeof(int)); + int* code_realloc = NULL; unsigned long codep = 0; register int i; instruction* ins = setup_instructions(); @@ -187,9 +190,13 @@ program vm_compile(char *filename){ if(codep == size){ size += 100; - code = (int *) realloc(code, size * sizeof(int)); - if(code == NULL) + code_realloc = (int *) realloc(code, size * sizeof(int)); + if(code_realloc == NULL){ + free(code); die(127, "Program too big, could not allocate enough storage."); + } + code = code_realloc; + code_realloc = NULL; } if(strcmp(command[0], "ENTRY") == 0){ @@ -231,7 +238,11 @@ program vm_compile(char *filename){ if(line) free(line); - code = (int *) realloc(code, codep * sizeof(int)); + code_realloc = (int *) realloc(code, codep * sizeof(int)); + if(code_realloc != NULL){ + code = code_realloc; + code_realloc = NULL; + } prog.length = codep; prog.entrypoint = entry;