Replaced opcode defines with enum
This commit is contained in:
56
src/opcode.h
56
src/opcode.h
@@ -1,59 +1,61 @@
|
|||||||
#ifndef OPCODE_H
|
#ifndef OPCODE_H
|
||||||
#define OPCODE_H
|
#define OPCODE_H
|
||||||
|
enum {
|
||||||
/*Addition operation*/
|
/*Addition operation*/
|
||||||
#define IADD 1
|
IADD = 1,
|
||||||
/*Subtraction operation*/
|
/*Subtraction operation*/
|
||||||
#define ISUB 2
|
ISUB,
|
||||||
/*Multiplication operation*/
|
/*Multiplication operation*/
|
||||||
#define IMULT 3
|
IMULT,
|
||||||
/*Division operation*/
|
/*Division operation*/
|
||||||
#define IDIV 4
|
IDIV,
|
||||||
/*Modulo operation*/
|
/*Modulo operation*/
|
||||||
#define IMOD 5
|
IMOD,
|
||||||
/*< operation*/
|
/*< operation*/
|
||||||
#define ILT 6
|
ILT,
|
||||||
/*== operation*/
|
/*== operation*/
|
||||||
#define IEQ 7
|
IEQ,
|
||||||
/*> operation*/
|
/*> operation*/
|
||||||
#define IGT 8
|
IGT,
|
||||||
/*branch operation*/
|
/*branch operation*/
|
||||||
#define BR 9
|
BR,
|
||||||
/*branch if true operation*/
|
/*branch if true operation*/
|
||||||
#define BRT 10
|
BRT,
|
||||||
/*branch if false operation*/
|
/*branch if false operation*/
|
||||||
#define BRF 11
|
BRF,
|
||||||
/*put operation*/
|
/*put operation*/
|
||||||
#define ICONST 12
|
ICONST,
|
||||||
/*load variable operation*/
|
/*load variable operation*/
|
||||||
#define LOAD 13
|
LOAD,
|
||||||
/*load global variable operation*/
|
/*load global variable operation*/
|
||||||
#define GLOAD 14
|
GLOAD,
|
||||||
/*store variable operation*/
|
/*store variable operation*/
|
||||||
#define STORE 15
|
STORE,
|
||||||
/*store global variable operation*/
|
/*store global variable operation*/
|
||||||
#define GSTORE 16
|
GSTORE,
|
||||||
/*print operation*/
|
/*print operation*/
|
||||||
#define PRINT 17
|
PRINT,
|
||||||
/*pop operation*/
|
/*pop operation*/
|
||||||
#define POP 18
|
POP,
|
||||||
/*end/halt operation*/
|
/*end/halt operation*/
|
||||||
#define HALT 19
|
HALT,
|
||||||
/*<= operation*/
|
/*<= operation*/
|
||||||
#define ILEQ 20
|
ILEQ,
|
||||||
/*>= operation*/
|
/*>= operation*/
|
||||||
#define IGEQ 21
|
IGEQ,
|
||||||
/*call subroutine operation*/
|
/*call subroutine operation*/
|
||||||
#define CALL 22
|
CALL,
|
||||||
/*return from subroutine operation*/
|
/*return from subroutine operation*/
|
||||||
#define RET 23
|
RET,
|
||||||
/*print integer operation*/
|
/*print integer operation*/
|
||||||
#define IPRINT 24
|
IPRINT,
|
||||||
/*fetch operation*/
|
/*fetch operation*/
|
||||||
#define FETCH 25
|
FETCH,
|
||||||
/*++ operation*/
|
/*++ operation*/
|
||||||
#define IINC 26
|
IINC,
|
||||||
/*-- operation*/
|
/*-- operation*/
|
||||||
#define IDEC 27
|
IDEC
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief An instruction struct
|
* @brief An instruction struct
|
||||||
|
17
src/vm.c
17
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 sp = -1;
|
||||||
register int fp = -1;
|
register int fp = -1;
|
||||||
int nargs, addr, a, b;
|
int nargs, addr, a, b;
|
||||||
|
|
||||||
instruction* ins = setup_instructions();
|
instruction* ins = setup_instructions();
|
||||||
|
|
||||||
while(ip < length){
|
while(ip < length){
|
||||||
int opcode = code[ip];
|
int opcode = code[ip];
|
||||||
ip++;
|
ip++;
|
||||||
@@ -168,6 +170,7 @@ program vm_compile(char *filename){
|
|||||||
char** command = NULL;
|
char** command = NULL;
|
||||||
unsigned long size = 100;
|
unsigned long size = 100;
|
||||||
int* code = malloc(size * sizeof(int));
|
int* code = malloc(size * sizeof(int));
|
||||||
|
int* code_realloc = NULL;
|
||||||
unsigned long codep = 0;
|
unsigned long codep = 0;
|
||||||
register int i;
|
register int i;
|
||||||
instruction* ins = setup_instructions();
|
instruction* ins = setup_instructions();
|
||||||
@@ -187,9 +190,13 @@ program vm_compile(char *filename){
|
|||||||
|
|
||||||
if(codep == size){
|
if(codep == size){
|
||||||
size += 100;
|
size += 100;
|
||||||
code = (int *) realloc(code, size * sizeof(int));
|
code_realloc = (int *) realloc(code, size * sizeof(int));
|
||||||
if(code == NULL)
|
if(code_realloc == NULL){
|
||||||
|
free(code);
|
||||||
die(127, "Program too big, could not allocate enough storage.");
|
die(127, "Program too big, could not allocate enough storage.");
|
||||||
|
}
|
||||||
|
code = code_realloc;
|
||||||
|
code_realloc = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strcmp(command[0], "ENTRY") == 0){
|
if(strcmp(command[0], "ENTRY") == 0){
|
||||||
@@ -231,7 +238,11 @@ program vm_compile(char *filename){
|
|||||||
if(line)
|
if(line)
|
||||||
free(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.length = codep;
|
||||||
prog.entrypoint = entry;
|
prog.entrypoint = entry;
|
||||||
|
Reference in New Issue
Block a user