From 2091bfb9e4e42f0f9057fa54be7c1da1007f687d Mon Sep 17 00:00:00 2001 From: "e.gavrin" Date: Fri, 4 Jul 2014 23:51:42 +0400 Subject: [PATCH] opcodes --- src/libcoreint/interpreter.c | 55 +++++++++--------------------- src/libcoreint/interpreter.h | 4 +-- src/libcoreint/opcode-structures.h | 8 +++-- src/libcoreint/opcodes.c | 43 +++++++++-------------- src/libcoreint/opcodes.h | 15 ++++---- src/main.c | 4 +-- 6 files changed, 49 insertions(+), 80 deletions(-) diff --git a/src/libcoreint/interpreter.c b/src/libcoreint/interpreter.c index a36429e56..7673633d0 100644 --- a/src/libcoreint/interpreter.c +++ b/src/libcoreint/interpreter.c @@ -18,54 +18,31 @@ void gen_bytecode () { -#ifdef __HOST - FILE *file = fopen (FILE_NAME, "w+b"); -#endif + __int_data.pos = 0; - //TODO REMOVE - { - save_op_data (file, get_op_loop_inf (1)); - save_op_data (file, get_op_call_1 (0, 12)); - save_op_data (file, get_op_call_1 (0, 13)); - save_op_data (file, get_op_call_1 (0, 14)); - save_op_data (file, get_op_call_1 (0, 15)); - save_op_data (file, get_op_jmp (0)); // mandatory! - } - -#ifdef __HOST - fclose (file); + save_op_data (getop_loop_inf (1)); + save_op_data (getop_call_1 (0, 12)); + save_op_data (getop_call_1 (0, 13)); + save_op_data (getop_call_1 (0, 14)); + save_op_data (getop_call_1 (0, 15)); + //save_op_data (getop_jmp (0)); + +#ifdef __MCU + // It's mandatory to restart app! + save_op_data (getop_jmp (0)); #endif } void run_int () { - OPCODE op_curr; __int_data.pos = 0; + + printf("size%d", sizeof(OPCODE)); -#ifdef __HOST - FILE *file = fopen (FILE_NAME, "rb"); - - if (file == NULL) + while (true) { - fputs ("File error", stderr); - exit (1); + OPCODE *curr = &__program[__int_data.pos]; + curr->opfunc_ptr (*curr); } - - while (!feof (file)) - { - if (!fread (&op_curr, sizeof (OPCODE), 1, file)) - { - break; - } - - __int_data.pos++; - op_curr.opfunc_ptr (op_curr); - - fseek (file, __int_data.pos * sizeof (OPCODE), SEEK_SET); - } - - fclose (file); -#endif } - diff --git a/src/libcoreint/interpreter.h b/src/libcoreint/interpreter.h index f898cae95..f5ce372c9 100644 --- a/src/libcoreint/interpreter.h +++ b/src/libcoreint/interpreter.h @@ -20,12 +20,12 @@ #include #include #include - -#define FILE_NAME "application.bin" #endif #include "opcodes.h" +OPCODE __program[20]; + struct { int pos; diff --git a/src/libcoreint/opcode-structures.h b/src/libcoreint/opcode-structures.h index 146df4a4a..0a1e1b364 100644 --- a/src/libcoreint/opcode-structures.h +++ b/src/libcoreint/opcode-structures.h @@ -16,6 +16,11 @@ #ifndef OPCODE_STRUCTURES_H #define OPCODE_STRUCTURES_H + // Jerry bytecode ver:07/04/2014 + +// +#define OP_TYPE_IDX uint8_t + OP_DEF (nop) { }; OP_DEF (jmp) @@ -27,8 +32,7 @@ OP_DEF (decl) { }; OP_DEF (decl_func_named) { - OP_TYPE_IDX name_literal_idx; - + OP_TYPE_IDX name_literal_idx; }; OP_DEF (decl_func_anon) { }; diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index b113f6d60..0fd8bb47c 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -17,30 +17,17 @@ #include "interpreter.h" void -#ifdef __HOST -save_op_data (FILE *file, OPCODE opdata) -#elif __MCU save_op_data (OPCODE opdata) -#endif { -#ifdef __HOST - if (file == NULL) - { - return; - } - - fwrite (&opdata, sizeof (OPCODE), 1, file); - -#elif __MCU - JERRY_STATIC_ASSERT (false); -#endif + __program[__int_data.pos++] = opdata; } void opfunc_loop_inf (OPCODE opdata) { #ifdef __HOST - printf ("loop_inf:idx:%d\n", + printf ("%d::loop_inf:idx:%d\n", + __int_data.pos, opdata.data.loop_inf.opcode_idx); #endif @@ -48,20 +35,24 @@ opfunc_loop_inf (OPCODE opdata) } void -opfunc_op_call_1 (OPCODE opdata) +opfunc_call_1 (OPCODE opdata) { #ifdef __HOST - printf ("op_call_1:idx:%d:%d\n", + printf ("%d::op_call_1:idx:%d:%d\n", + __int_data.pos, opdata.data.call_1.name_literal_idx, opdata.data.call_1.arg1_literal_idx); #endif + + __int_data.pos++; } void -opfunc_op_jmp (OPCODE opdata) +opfunc_jmp (OPCODE opdata) { #ifdef __HOST - printf ("op_jmp:idx:%d\n", + printf ("%d::op_jmp:idx:%d\n", + __int_data.pos, opdata.data.jmp.opcode_idx); #endif @@ -69,22 +60,22 @@ opfunc_op_jmp (OPCODE opdata) } OPCODE -get_op_jmp (int arg1) +getop_jmp (int arg1) { OPCODE opdata; - opdata.opfunc_ptr = opfunc_op_jmp; + opdata.opfunc_ptr = opfunc_jmp; opdata.data.jmp.opcode_idx = arg1; return opdata; } OPCODE -get_op_call_1 (int arg1, int arg2) +getop_call_1 (int arg1, int arg2) { OPCODE opdata; - opdata.opfunc_ptr = opfunc_op_call_1; + opdata.opfunc_ptr = opfunc_call_1; opdata.data.call_1.name_literal_idx = arg1; opdata.data.call_1.arg1_literal_idx = arg2; @@ -92,11 +83,11 @@ get_op_call_1 (int arg1, int arg2) } OPCODE -get_op_loop_inf (int arg1) +getop_loop_inf (int arg1) { OPCODE opdata; - opdata.opfunc_ptr = opfunc_op_call_1; + opdata.opfunc_ptr = opfunc_loop_inf; opdata.data.loop_inf.opcode_idx = arg1; return opdata; diff --git a/src/libcoreint/opcodes.h b/src/libcoreint/opcodes.h index c6d369139..d3c3af68b 100644 --- a/src/libcoreint/opcodes.h +++ b/src/libcoreint/opcodes.h @@ -26,8 +26,6 @@ #define OP_DEF(name) struct __op_##name #define OP(name) struct __op_##name name -#define OP_TYPE_IDX uint8_t - OPCODE; typedef void (*opfunc)(OPCODE); @@ -51,16 +49,15 @@ OPCODE{ } __packed; -#ifdef __HOST -void save_op_data (FILE*, OPCODE); -#elif __MCU void save_op_data (OPCODE); -#endif +void opfunc_loop_inf (OPCODE); +void opfunc_call_1 (OPCODE); +void opfunc_jmp (OPCODE); -OPCODE get_op_loop_inf (int); -OPCODE get_op_call_1 (int, int); -OPCODE get_op_jmp (int arg1); +OPCODE getop_loop_inf (int); +OPCODE getop_call_1 (int, int); +OPCODE getop_jmp (int arg1); #endif /* OPCODES_H */ diff --git a/src/main.c b/src/main.c index 3ed82f54a..c04fcb64e 100644 --- a/src/main.c +++ b/src/main.c @@ -72,8 +72,8 @@ main (int argc, char **argv) } //gen_bytecode (generated_source); - gen_bytecode (file); - // run_int (); + gen_bytecode (); + run_int (); #ifdef __TARGET_MCU fake_exit ();