diff --git a/Makefile b/Makefile index e312b97b6..29a579bd5 100644 --- a/Makefile +++ b/Makefile @@ -47,21 +47,21 @@ OBJS = \ $(sort \ $(patsubst %.c,./$(OBJ_DIR)/%.o,$(notdir $(SOURCES)))) -CC = gcc-4.9 +CC = gcc#-4.9 LD = ld OBJDUMP = objdump OBJCOPY = objcopy SIZE = size STRIP = strip -CROSS_CC = $(CROSS_COMPILE)gcc-4.9 +CROSS_CC = $(CROSS_COMPILE)gcc#-4.9 CROSS_LD = $(CROSS_COMPILE)ld CROSS_OBJDUMP = $(CROSS_COMPILE)objdump CROSS_OBJCOPY = $(CROSS_COMPILE)objcopy CROSS_SIZE = $(CROSS_COMPILE)size # General flags -CFLAGS ?= $(INCLUDES) -std=c99 -m32 -fdiagnostics-color=always +CFLAGS ?= $(INCLUDES) -std=c99 -m32 #-fdiagnostics-color=always #CFLAGS += -Wall -Wextra -Wpedantic -Wlogical-op -Winline #CFLAGS += -Wformat-nonliteral -Winit-self -Wstack-protector #CFLAGS += -Wconversion -Wsign-conversion -Wformat-security diff --git a/src/libcoreint/interpreter.c b/src/libcoreint/interpreter.c index 7673633d0..4a531e8b5 100644 --- a/src/libcoreint/interpreter.c +++ b/src/libcoreint/interpreter.c @@ -33,16 +33,25 @@ gen_bytecode () #endif } +void +init_int () +{ +#define INIT_OP_FUNC(name) __int_data.func[ name ] = opfunc_##name ; + JERRY_STATIC_ASSERT (sizeof (OPCODE) <= 4); + + __int_data.pos = 0; + OP_LIST (INIT_OP_FUNC) +} + void run_int () { - __int_data.pos = 0; - - printf("size%d", sizeof(OPCODE)); + init_int (); while (true) - { - OPCODE *curr = &__program[__int_data.pos]; - curr->opfunc_ptr (*curr); - } + { + printf ("size %lu\n", sizeof (OPCODE)); + OPCODE *curr = &__program[__int_data.pos]; + __int_data.func[curr->op_idx](*curr); + } } diff --git a/src/libcoreint/interpreter.h b/src/libcoreint/interpreter.h index f5ce372c9..545939d76 100644 --- a/src/libcoreint/interpreter.h +++ b/src/libcoreint/interpreter.h @@ -24,16 +24,16 @@ #include "opcodes.h" -OPCODE __program[20]; +OPCODE __program[128]; -struct -{ - int pos; - int *root_op_addr; +struct { + int pos; + opfunc func[LAST_OP]; + int *root_op_addr; } __int_data; -void gen_bytecode (); -void run_int (); +void gen_bytecode(); +void run_int(); #endif /* INTERPRETER_H */ diff --git a/src/libcoreint/opcode-structures.h b/src/libcoreint/opcode-structures.h index 0a1e1b364..f14bc2862 100644 --- a/src/libcoreint/opcode-structures.h +++ b/src/libcoreint/opcode-structures.h @@ -16,77 +16,45 @@ #ifndef OPCODE_STRUCTURES_H #define OPCODE_STRUCTURES_H - // Jerry bytecode ver:07/04/2014 +// Jerry bytecode ver:07/04/2014 +#define OP_DEF(name, list) struct __op_##name { list ; } -// -#define OP_TYPE_IDX uint8_t +#define OP_CODE_DECL(name, type, ... ) \ + OP_DEF (name, type##_DECL( __VA_ARGS__ ) ); \ + OPCODE getop_##name ( type ); -OP_DEF (nop) { }; +#define T_IDX uint8_t +#define T_IDX_IDX T_IDX, T_IDX +#define T_IDX_IDX_IDX T_IDX, T_IDX, T_IDX -OP_DEF (jmp) -{ - OP_TYPE_IDX opcode_idx; -}; +#define T_IDX_DECL(name) uint8_t name +#define T_IDX_IDX_DECL(name1, name2) \ + T_IDX_DECL( name1 ) ; \ + T_IDX_DECL( name2 ) +#define T_IDX_IDX_IDX_DECL(name1, name2, name3) \ + T_IDX_DECL( name1 ) ; \ + T_IDX_DECL( name2 ); \ + T_IDX_DECL( name3 ) -OP_DEF (decl) { }; -OP_DEF (decl_func_named) -{ - OP_TYPE_IDX name_literal_idx; -}; +OP_CODE_DECL(nop, T_IDX, opcode_idx); +OP_CODE_DECL(jmp, T_IDX, opcode_idx); +OP_CODE_DECL(loop_inf, T_IDX, opcode_idx); -OP_DEF (decl_func_anon) { }; +OP_CODE_DECL(call_1, T_IDX_IDX, + name_literal_idx, + arg1_literal_idx); -OP_DEF (decl_var_global) { }; +OP_CODE_DECL(call_2, T_IDX_IDX_IDX, + name_literal_idx, + arg1_literal_idx, + arg2_literal_idx); -OP_DEF (decl_var_local) { }; +OP_CODE_DECL(call_n, T_IDX_IDX_IDX, + name_literal_idx, + arg1_literal_idx, + arg2_literal_idx); -/** Call with 1 argument */ -OP_DEF (call_1) -{ - OP_TYPE_IDX name_literal_idx; - OP_TYPE_IDX arg1_literal_idx; -}; - -OP_DEF (call_2) -{ - OP_TYPE_IDX name_literal_idx; - OP_TYPE_IDX arg1_literal_idx; - OP_TYPE_IDX arg2_literal_idx; -}; - -OP_DEF (call_n) { }; - -OP_DEF (list_header) { }; -OP_DEF (list_element) { }; -OP_DEF (list_tail) { }; - -OP_DEF (try_begin) { }; - -OP_DEF (try_end) { }; - -OP_DEF (catch_begin) { }; - -OP_DEF (catch_end) { }; - -OP_DEF (finally_begin) { }; - -OP_DEF (finally_end) { }; - -OP_DEF (with_begin) { }; - -OP_DEF (with_end) { }; - -OP_DEF (loop_inf) -{ - OP_TYPE_IDX opcode_idx; -}; - -OP_DEF (loop_cond_pre) { }; - -OP_DEF (loop_cond_post) { }; - -OP_DEF (swtch) { }; #endif /* OPCODE_STRUCTURES_H */ diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index 0fd8bb47c..b3c996ff0 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -60,22 +60,22 @@ opfunc_jmp (OPCODE opdata) } OPCODE -getop_jmp (int arg1) +getop_jmp (T_IDX arg1) { OPCODE opdata; - opdata.opfunc_ptr = opfunc_jmp; + opdata.op_idx = jmp; opdata.data.jmp.opcode_idx = arg1; return opdata; } OPCODE -getop_call_1 (int arg1, int arg2) +getop_call_1 (T_IDX arg1, T_IDX arg2) { OPCODE opdata; - opdata.opfunc_ptr = opfunc_call_1; + opdata.op_idx = call_1; opdata.data.call_1.name_literal_idx = arg1; opdata.data.call_1.arg1_literal_idx = arg2; @@ -83,11 +83,11 @@ getop_call_1 (int arg1, int arg2) } OPCODE -getop_loop_inf (int arg1) +getop_loop_inf (T_IDX arg1) { OPCODE opdata; - opdata.opfunc_ptr = opfunc_loop_inf; + opdata.op_idx = loop_inf; opdata.data.loop_inf.opcode_idx = arg1; return opdata; diff --git a/src/libcoreint/opcodes.h b/src/libcoreint/opcodes.h index d3c3af68b..4131b323f 100644 --- a/src/libcoreint/opcodes.h +++ b/src/libcoreint/opcodes.h @@ -23,41 +23,44 @@ #include "globals.h" #define OPCODE struct __opcode -#define OP_DEF(name) struct __op_##name -#define OP(name) struct __op_##name name + +#define OP_STRUCT_FIELD(name) struct __op_##name name; +#define OP_ENUM_FIELD(name) name , +#define OP_FUNC_DECL(name) void opfunc_##name (OPCODE); OPCODE; - typedef void (*opfunc)(OPCODE); +#define OP_LIST(op) \ + op(loop_inf) \ + op(call_1) \ + op(jmp) \ + //op(call_2) \ + op(call_n) \ + op(nop) + #include "opcode-structures.h" +enum __opcode_idx +{ + OP_LIST (OP_ENUM_FIELD) + LAST_OP +}; + union __opdata { - OP (loop_inf); - OP (call_1); - OP (call_2); - OP (call_n); + OP_LIST (OP_STRUCT_FIELD) +}; - OP (jmp); - OP (nop); -} data; +OP_LIST (OP_FUNC_DECL) OPCODE{ - opfunc opfunc_ptr; + T_IDX op_idx; union __opdata data; } __packed; void save_op_data (OPCODE); -void opfunc_loop_inf (OPCODE); -void opfunc_call_1 (OPCODE); -void opfunc_jmp (OPCODE); - -OPCODE getop_loop_inf (int); -OPCODE getop_call_1 (int, int); -OPCODE getop_jmp (int arg1); - #endif /* OPCODES_H */