Replaced opcode defines with enum

This commit is contained in:
Veit Heller
2014-12-21 12:31:10 +01:00
parent e676d40e62
commit e16553d93b
2 changed files with 43 additions and 30 deletions

View File

@@ -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

View File

@@ -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;