fixed dump opcode struct

This commit is contained in:
e.gavrin
2014-07-03 18:27:49 +04:00
parent 3fde3400f4
commit b05eb83966
4 changed files with 62 additions and 78 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ release:
-o $(TARGET) -o $(TARGET)
clean: 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)
rm -f $(TARGET).elf rm -f $(TARGET).elf
rm -f $(TARGET).bin rm -f $(TARGET).bin
+7 -3
View File
@@ -40,13 +40,17 @@ gen_bytecode (FILE *src_file)
FILE *file = fopen (FILE_NAME, "w+b"); FILE *file = fopen (FILE_NAME, "w+b");
union __opcodes op0; OPCODE op0;
save_op_loop_inf (file, op0, 1); save_op_loop_inf (file, op0, 1);
save_op_call_1 (file, op0, 0, LED_GREEN); 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_BLUE);
save_op_call_1 (file, op0, 0, LED_ORANGE); 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_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); save_op_jmp (file, op0, 0);
fclose (file); fclose (file);
@@ -56,7 +60,7 @@ void
run_int () run_int ()
{ {
FILE *file = fopen (FILE_NAME, "rb"); FILE *file = fopen (FILE_NAME, "rb");
union __opcodes op_curr; OPCODE op_curr;
if (file == NULL) if (file == NULL)
{ {
@@ -66,7 +70,7 @@ run_int ()
while (!feof (file)) while (!feof (file))
{ {
if (!fread (&op_curr, sizeof (union __opcodes), 1, file)) if (!fread (&op_curr, sizeof (OPCODE), 1, file))
{ {
break; break;
} }
+20 -43
View File
@@ -19,92 +19,69 @@
#include <stdlib.h> #include <stdlib.h>
void void
opfunc_loop_inf (union __opcodes opdata) opfunc_loop_inf (OPCODE opdata)
{ {
printf ("loop_inf:idx:%d\n", printf ("loop_inf:idx:%d\n",
opdata.op_loop_inf.opcode_idx); opdata.data.loop_inf.opcode_idx);
} }
void void
opfunc_op_call_1 (union __opcodes opdata) opfunc_op_call_1 (OPCODE opdata)
{ {
printf ("op_call_1:idx:%d:%d\n", printf ("op_call_1:idx:%d:%d\n",
opdata.op_call_1.name_literal_idx, opdata.data.call_1.name_literal_idx,
opdata.op_call_1.arg_literal_idx); opdata.data.call_1.arg_literal_idx);
} }
void void
opfunc_op_jmp (union __opcodes opdata) opfunc_op_jmp (OPCODE opdata)
{ {
printf ("op_jmp:idx:%d\n", 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 void
save_op_jmp (FILE *file, union __opcodes opdata, int arg1) save_op_jmp (FILE *file, OPCODE opdata, int arg1)
{ {
if (file == NULL) if (file == NULL)
{ {
return; return;
} }
opdata.opfunc_ptr = proxy_op_jmp; opdata.opfunc_ptr = opfunc_op_jmp;
opdata.data.jmp.opcode_idx = arg1;
opdata.op_jmp.opfunc_ptr = opfunc_op_jmp;
opdata.op_jmp.opcode_idx = arg1;
fwrite (&opdata, sizeof (union __opcodes), 1, file); fwrite (&opdata, sizeof (OPCODE), 1, file);
} }
void 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) if (file == NULL)
{ {
return; return;
} }
opdata.opfunc_ptr = proxy_op_call_1; opdata.opfunc_ptr = opfunc_op_call_1;
opdata.data.call_1.name_literal_idx = arg1;
opdata.op_call_1.opfunc_ptr = opfunc_op_call_1; opdata.data.call_1.arg_literal_idx = arg2;
opdata.op_call_1.name_literal_idx = arg1;
opdata.op_call_1.arg_literal_idx = arg2;
fwrite (&opdata, sizeof (union __opcodes), 1, file); fwrite (&opdata, sizeof (OPCODE), 1, file);
} }
void 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) if (file == NULL)
{ {
return; 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; fwrite (&opdata, sizeof (OPCODE), 1, file);
opdata.op_loop_inf.opcode_idx = arg1;
fwrite (&opdata, sizeof (union __opcodes), 1, file);
} }
+34 -31
View File
@@ -18,44 +18,47 @@
#include "stdio.h" #include "stdio.h"
#define OP_RET_TYPE void #define OPCODE struct __opcode
#define OP_INT_TYPE int #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); OP_DEF (loop_inf)
typedef OP_RET_TYPE (*opfunc_int_int_ptr)(union __opcodes);
union __opcodes
{ {
op_proxy_ptr opfunc_ptr; int opcode_idx;
};
struct __op_loop_inf
{
opfunc_int_ptr opfunc_ptr;
int opcode_idx;
} op_loop_inf;
/** Call with 1 argument */ /** Call with 1 argument */
struct __op_call_1 OP_DEF (call_1)
{ {
opfunc_int_int_ptr opfunc_ptr; int name_literal_idx;
int name_literal_idx; int arg_literal_idx;
int arg_literal_idx; };
} op_call_1;
struct __op_jmp OP_DEF (jmp)
{ {
opfunc_int_ptr opfunc_ptr; int opcode_idx;
int opcode_idx; };
} op_jmp;
} __packed;
void save_op_jmp(FILE *, union __opcodes, int); OPCODE{
void save_op_call_1(FILE *, union __opcodes, int, int); opfunc opfunc_ptr;
void save_op_loop_inf(FILE *, union __opcodes, int);
/** 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 */ #endif /* OPCODES_H */