diff --git a/Makefile b/Makefile index 65b2051ab..4fedd301d 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ release: -o $(TARGET) clean: - rm -f $(OBJ_DIR)/*.o *.o *~ lexer.log parser.log + rm -f $(OBJ_DIR)/*.o *.bin *.o *~ lexer.log parser.log rm -f $(TARGET) rm -f $(TARGET).elf rm -f $(TARGET).bin diff --git a/src/libcoreint/interpreter.c b/src/libcoreint/interpreter.c index aa8284d71..2be1beb3f 100644 --- a/src/libcoreint/interpreter.c +++ b/src/libcoreint/interpreter.c @@ -40,13 +40,17 @@ gen_bytecode (FILE *src_file) FILE *file = fopen (FILE_NAME, "w+b"); - union __opcodes op0; + OPCODE op0; save_op_loop_inf (file, op0, 1); save_op_call_1 (file, op0, 0, LED_GREEN); save_op_call_1 (file, op0, 0, LED_BLUE); save_op_call_1 (file, op0, 0, LED_ORANGE); save_op_call_1 (file, op0, 0, LED_RED); + save_op_call_1 (file, op0, 0, LED_GREEN); + save_op_call_1 (file, op0, 0, LED_BLUE); + save_op_call_1 (file, op0, 0, LED_ORANGE); + save_op_call_1 (file, op0, 0, LED_RED); save_op_jmp (file, op0, 0); fclose (file); @@ -56,7 +60,7 @@ void run_int () { FILE *file = fopen (FILE_NAME, "rb"); - union __opcodes op_curr; + OPCODE op_curr; if (file == NULL) { @@ -66,7 +70,7 @@ run_int () while (!feof (file)) { - if (!fread (&op_curr, sizeof (union __opcodes), 1, file)) + if (!fread (&op_curr, sizeof (OPCODE), 1, file)) { break; } diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index 864a582d4..78eae6979 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -19,92 +19,69 @@ #include void -opfunc_loop_inf (union __opcodes opdata) +opfunc_loop_inf (OPCODE opdata) { printf ("loop_inf:idx:%d\n", - opdata.op_loop_inf.opcode_idx); + opdata.data.loop_inf.opcode_idx); } void -opfunc_op_call_1 (union __opcodes opdata) +opfunc_op_call_1 (OPCODE opdata) { printf ("op_call_1:idx:%d:%d\n", - opdata.op_call_1.name_literal_idx, - opdata.op_call_1.arg_literal_idx); + opdata.data.call_1.name_literal_idx, + opdata.data.call_1.arg_literal_idx); } - - void -opfunc_op_jmp (union __opcodes opdata) +opfunc_op_jmp (OPCODE opdata) { printf ("op_jmp:idx:%d\n", - opdata.op_jmp.opcode_idx); + opdata.data.jmp.opcode_idx); } -void proxy_loop_inf (union __opcodes opdata) -{ - opdata.op_loop_inf.opfunc_ptr (opdata); -} - - -void proxy_op_call_1 (union __opcodes opdata) -{ - opdata.op_call_1.opfunc_ptr (opdata); -} - -void proxy_op_jmp (union __opcodes opdata) -{ - opdata.op_jmp.opfunc_ptr (opdata); -} void -save_op_jmp (FILE *file, union __opcodes opdata, int arg1) +save_op_jmp (FILE *file, OPCODE opdata, int arg1) { if (file == NULL) { return; } - opdata.opfunc_ptr = proxy_op_jmp; - - opdata.op_jmp.opfunc_ptr = opfunc_op_jmp; - opdata.op_jmp.opcode_idx = arg1; + opdata.opfunc_ptr = opfunc_op_jmp; + opdata.data.jmp.opcode_idx = arg1; - fwrite (&opdata, sizeof (union __opcodes), 1, file); + fwrite (&opdata, sizeof (OPCODE), 1, file); } void -save_op_call_1 (FILE *file, union __opcodes opdata, int arg1, int arg2) +save_op_call_1 (FILE *file, OPCODE opdata, int arg1, int arg2) { if (file == NULL) { return; } - opdata.opfunc_ptr = proxy_op_call_1; - - opdata.op_call_1.opfunc_ptr = opfunc_op_call_1; - opdata.op_call_1.name_literal_idx = arg1; - opdata.op_call_1.arg_literal_idx = arg2; + opdata.opfunc_ptr = opfunc_op_call_1; + opdata.data.call_1.name_literal_idx = arg1; + opdata.data.call_1.arg_literal_idx = arg2; - fwrite (&opdata, sizeof (union __opcodes), 1, file); + fwrite (&opdata, sizeof (OPCODE), 1, file); } void -save_op_loop_inf (FILE *file, union __opcodes opdata, int arg1) +save_op_loop_inf (FILE *file, OPCODE opdata, int arg1) { if (file == NULL) { return; } - opdata.opfunc_ptr = proxy_loop_inf; + opdata.opfunc_ptr = opfunc_loop_inf; + opdata.data.loop_inf.opcode_idx = arg1; - opdata.op_loop_inf.opfunc_ptr = opfunc_loop_inf; - opdata.op_loop_inf.opcode_idx = arg1; - - fwrite (&opdata, sizeof (union __opcodes), 1, file); + fwrite (&opdata, sizeof (OPCODE), 1, file); } \ No newline at end of file diff --git a/src/libcoreint/opcodes.h b/src/libcoreint/opcodes.h index dc5ca6bd1..62f1e6ea0 100644 --- a/src/libcoreint/opcodes.h +++ b/src/libcoreint/opcodes.h @@ -18,44 +18,47 @@ #include "stdio.h" -#define OP_RET_TYPE void -#define OP_INT_TYPE int +#define OPCODE struct __opcode +#define OP_DEF(name) struct __op_##name +#define OP(name) struct __op_##name name -union __opcodes; +OPCODE; -typedef OP_RET_TYPE (*op_proxy_ptr)(union __opcodes); +typedef void (*opfunc)(OPCODE); -typedef OP_RET_TYPE (*opfunc_int_ptr)(union __opcodes); -typedef OP_RET_TYPE (*opfunc_int_int_ptr)(union __opcodes); - -union __opcodes +OP_DEF (loop_inf) { - op_proxy_ptr opfunc_ptr; - - struct __op_loop_inf - { - opfunc_int_ptr opfunc_ptr; - int opcode_idx; - } op_loop_inf; + int opcode_idx; +}; - /** Call with 1 argument */ - struct __op_call_1 - { - opfunc_int_int_ptr opfunc_ptr; - int name_literal_idx; - int arg_literal_idx; - } op_call_1; +/** Call with 1 argument */ +OP_DEF (call_1) +{ + int name_literal_idx; + int arg_literal_idx; +}; - struct __op_jmp - { - opfunc_int_ptr opfunc_ptr; - int opcode_idx; - } op_jmp; -} __packed; +OP_DEF (jmp) +{ + int opcode_idx; +}; -void save_op_jmp(FILE *, union __opcodes, int); -void save_op_call_1(FILE *, union __opcodes, int, int); -void save_op_loop_inf(FILE *, union __opcodes, int); +OPCODE{ + opfunc opfunc_ptr; + + /** OPCODES */ + union __opdata + { + OP (loop_inf); + OP (call_1); + OP (jmp); + } data; +} +__packed; + +void save_op_jmp (FILE *, OPCODE, int); +void save_op_call_1 (FILE *, OPCODE, int, int); +void save_op_loop_inf (FILE *, OPCODE, int); #endif /* OPCODES_H */