diff --git a/README.md b/README.md index 797e35a..6660758 100644 --- a/README.md +++ b/README.md @@ -29,41 +29,40 @@ concise as possible. Instruction set --------------- -Up until now, all the operations are executed on integers(hence the -I\* prefix). +Up until now, all the operations are executed on integers. *Operations overview*: * ENTRY - Specifies an entry point for the routine by its index. -* IADD - Adds the two items to each other that are on top of the stack +* ADD - Adds the two items to each other that are on top of the stack and stores the result on top of the stack. -* ISUB - Subtracts the two items from each other that are on top of the +* SUB - Subtracts the two items from each other that are on top of the stack and stores the result on top of the stack. -* IMULT - Multiplies the two items to each other that are on top of the +* MULT - Multiplies the two items to each other that are on top of the stack and stores the result on top of the stack. -* IDIV - Performs a division operation to the two items that are on top +* DIV - Performs a division operation to the two items that are on top of the stack and stores the result on top of the stack. -* IMOD - Performs a modulo operation to the two items that are on top +* MOD - Performs a modulo operation to the two items that are on top of the stack and stores the result on top of the stack. -* ILT - Checks whether the item on top of the stack is greater than the +* LT - Checks whether the item on top of the stack is greater than the item below it and stores a boolean on top of the stack. -* IEQ - Checks whether the item on top of the stack is equal to the +* EQ - Checks whether the item on top of the stack is equal to the item below it and stores a boolean on top of the stack. -* IGT - Checks whether the item on top of the stack is smaller than the +* GT - Checks whether the item on top of the stack is smaller than the item below it and stores a boolean on top of the stack. -* ILEQ - Checks whether the item on top of the stack is smaller than or +* LEQ - Checks whether the item on top of the stack is smaller than or equal to the item below it and stores a boolean on top of the stack. -* IGEQ - Checks whether the item on top of the stack is greater than or +* GEQ - Checks whether the item on top of the stack is greater than or equal to the item below it and stores a boolean on top of the stack. * BR - Jumps to the instruction that is provided as an argument. @@ -74,7 +73,7 @@ value on top of the stack is TRUE. * BRF - Jumps to the instruction that is provided as an argument if the value on top of the stack is FALSE. -* ICONST - Puts the argument provided to the operation on top of the +* CONST - Puts the argument provided to the operation on top of the stack. * LOAD - Loads an element from any position on the stack on top of the @@ -101,7 +100,7 @@ top of the stack. * FETCH - Fetches a value. -* IINC -Increments the value on top of the stack by one(equal to ++). +* INC -Increments the value on top of the stack by one(equal to ++). -* IDEC -Decrements the value on top of the stack by one(equal to --). +* DEC -Decrements the value on top of the stack by one(equal to --). diff --git a/src/main.c b/src/main.c index c304cdd..e960bbf 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ * argc -> the argument counter * argv -> the argument vector * - * executes code in my dsl. + * executes code in my dsl/vm. */ int main(int argc, char** argv){ program prog; diff --git a/src/opcode.c b/src/opcode.c index afae0eb..43ae590 100644 --- a/src/opcode.c +++ b/src/opcode.c @@ -1,63 +1,63 @@ #include "opcode.h" /*The opcodes as chars*/ -const char* opcodes[] = { "ADD", - "SUB", - "MULT", - "DIV", +const char* opcodes[] = { "ADD", + "SUB", + "MULT", + "DIV", "MOD", - "LT", - "EQ", - "GT", - "BR", - "BRT", - "BRF", - "CONST", - "LOAD", + "LT", + "EQ", + "GT", + "BR", + "BRT", + "BRF", + "CONST", + "LOAD", "GLOAD", - "STORE", - "GSTORE", - "PRINT", - "POP", - "HALT", - "LEQ", - "GEQ", - "CALL", + "STORE", + "GSTORE", + "PRINT", + "POP", + "HALT", + "LEQ", + "GEQ", + "CALL", "RET", - "IPRINT", - "FETCH", - "INC", - "DEC" + "IPRINT", + "FETCH", + "INC", + "DEC" }; /*The argument counter for the opcodes*/ -int nargs[] = { 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 2, - 0, - 0, - 0, - 0, - 0 +int nargs[] = { 0, /* ADD */ + 0, /* SUB */ + 0, /* MULT */ + 0, /* DIV */ + 0, /* MOD */ + 0, /* LT */ + 0, /* EQ */ + 0, /* GT */ + 1, /* BR */ + 1, /* BRT */ + 1, /* BRF */ + 1, /* CONST */ + 1, /* LOAD */ + 1, /* GLOAD */ + 1, /* STORE */ + 1, /* GSTORE */ + 0, /* PRINT */ + 0, /* POP */ + 0, /* HALT */ + 0, /* LEQ */ + 0, /* GEQ */ + 2, /* CALL */ + 0, /* RET */ + 0, /* IPRINT */ + 0, /* FETCH */ + 0, /* INC */ + 0 /* DEC */ }; instruction* setup_instructions(){ diff --git a/src/opcode.h b/src/opcode.h index b6835a3..4cb0455 100644 --- a/src/opcode.h +++ b/src/opcode.h @@ -69,7 +69,11 @@ typedef struct{ const char* name; }instruction; -/*sets up the instruction struct*/ +/** + * @brief setup_instructions + * + * sets up the instruction struct for the VM. + */ instruction* setup_instructions(); #endif diff --git a/src/util.h b/src/util.h index 0ed5def..4f1a355 100644 --- a/src/util.h +++ b/src/util.h @@ -6,7 +6,7 @@ /** * @brief die - * @param code -> the error code + * @param code -> the error code * @param message -> the error message * * Lets the program die and emits an error message. diff --git a/src/vm.c b/src/vm.c index ee220d4..497b79f 100644 --- a/src/vm.c +++ b/src/vm.c @@ -9,13 +9,13 @@ /** * @brief disassemble - * @param sp -> stack pointer - * @param fp -> function pointer - * @param ip -> instruction pointer + * @param sp -> stack pointer + * @param fp -> function pointer + * @param ip -> instruction pointer * @param opcode -> current opcode - * @param ins -> instruction struct - * @param code -> opcodes - * @param stack -> stack + * @param ins -> instruction struct + * @param code -> opcodes + * @param stack -> stack * * prints current operations and a stack trace. * Is invoked if DEBUG is defined. @@ -34,25 +34,15 @@ static inline void disassemble(int sp, int fp, int ip, int opcode, instruction* printf("\n"); } -/** - * @brief vm_execute - * @param code -> a list of opcodes - * @param ip -> the instruction pointer - * @param datasize -> the maximal standard datasize - * @param length -> the length of the program - * - * the main virtual machine loop. takes opcodes and - * metainformation and executes it. - */ void vm_execute(int code[], int ip, int datasize, unsigned long length){ int* data = (int *) alloca((size_t)datasize * sizeof(int)); int stack[MAX_SIZE]; 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++; @@ -195,7 +185,7 @@ program vm_compile(char *filename){ size_t linelength = 0; int entry = 0; program prog; - + if(!file) die(127, "Could not open file."); @@ -229,8 +219,8 @@ program vm_compile(char *filename){ } if(found == FALSE){ - fprintf(stderr, - "Line %s called with unknown command: %s\n", + fprintf(stderr, + "Line %s called with unknown command: %s\n", line, command[0]); die(127, "Compilation failed."); } @@ -238,8 +228,8 @@ program vm_compile(char *filename){ if(ins[i].operands > 0){ int nargs = ins[i].operands; if(nargs != elemc-1){ - fprintf(stderr, - "Line %s called with wrong number of arguments (got %d, expected %d)\n", + fprintf(stderr, + "Line %s called with wrong number of arguments (got %d, expected %d)\n", line, elemc-1, nargs); die(127, "Compilation failed."); } @@ -249,7 +239,7 @@ program vm_compile(char *filename){ } } } - + fclose(file); if(line) @@ -260,7 +250,7 @@ program vm_compile(char *filename){ code = code_realloc; code_realloc = NULL; } - + prog.length = codep; prog.entrypoint = entry; prog.code = code; diff --git a/src/vm.h b/src/vm.h index a5a176f..0fd2047 100644 --- a/src/vm.h +++ b/src/vm.h @@ -17,10 +17,10 @@ typedef struct{ /** * @brief vm_execute - * @param code -> instructions - * @param ip -> starting point + * @param code -> instructions + * @param ip -> starting point * @param datasize -> maximum data size - * @param length -> length of program + * @param length -> length of program */ void vm_execute(int[], int, int, unsigned long); @@ -32,9 +32,9 @@ program vm_compile(char*); /** * @brief str_split - * @param a_str -> the input string + * @param a_str -> the input string * @param a_delim -> the delimiter - * @param elemc -> the counter of elements + * @param elemc -> the counter of elements * */ char** str_split(char*, const char, unsigned int*);