From b6f397fea739d64c500a07b0a9b251c855264f4a Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 7 Jun 2015 14:36:26 +0000 Subject: [PATCH] added some VM code --- stix/lib/comp.c | 30 -------------- stix/lib/exec.c | 99 ++++++++++++++++++++++++++++++++++++++++++--- stix/lib/stix-prv.h | 59 +++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 35 deletions(-) diff --git a/stix/lib/comp.c b/stix/lib/comp.c index 6464c82..7102229 100644 --- a/stix/lib/comp.c +++ b/stix/lib/comp.c @@ -1007,36 +1007,6 @@ static int end_include (stix_t* stix) * Byte-Code Generator * --------------------------------------------------------------------- */ -#define MAKE_CODE(x,y) (((x) << 4) | y) -#define MAX_CODE_INDEX 0xFFFFu -#define MAX_CODE_NARGS 0xFFFFu - -#define CMD_EXTEND 0x0 -#define CMD_EXTEND_DOUBLE 0x1 -#define CMD_PUSH_INSTVAR 0x2 -#define CMD_PUSH_TEMPVAR 0x3 -#define CMD_PUSH_LITERAL 0x4 -#define CMD_POP_AND_STORE_INTO_INSTVAR 0x5 /* pop and store */ -#define CMD_POP_AND_STORE_INTO_CLASSVAR 0x6 /* pop and store */ -#define CMD_POP_AND_STORE_INTO_TEMPVAR 0x7 /* pop and store */ - -#define CMD_SEND_MESSAGE_TO_SELF 0xA -#define CMD_SEND_MESSAGE_TO_SUPER 0xB - -/* ---------------------------------- */ -#define CODE_PUSH_RECEIVER 0xE0 -#define CODE_PUSH_NIL 0xE1 -#define CODE_PUSH_TRUE 0xE2 -#define CODE_PUSH_FALSE 0xE3 - -/* special code */ -#define CODE_DUP_STACKTOP 0xF1 -#define CODE_POP_STACKTOP 0xF2 -#define CODE_RETURN_MESSAGE_STACKTOP 0xF3 -#define CODE_RETURN_BLOCK_STACKTOP 0xF4 -#define CODE_RETURN_MESSAGE_RECEIVER 0xF5 -#define CODE_EXEC_PRIMITIVE 0xFF - static STIX_INLINE int emit_byte_instruction (stix_t* stix, stix_byte_t code) { stix_size_t i; diff --git a/stix/lib/exec.c b/stix/lib/exec.c index 7761ae0..17e312b 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -219,30 +219,119 @@ static stix_oop_process_t make_process (stix_t* stix, stix_oop_context_t ctx) int stix_execute (stix_t* stix) { + stix_oop_method_t mth; stix_oop_byte_t code; - stix_ooi_t ip; - stix_byte_t bc; + stix_ooi_t ip, sp; + stix_oop_t receiver; + + stix_byte_t bc, cmd; + stix_oow_t b1, b2; + STIX_ASSERT (stix->active_context != STIX_NULL); mth = stix->active_context->method; ip = STIX_OOP_TO_SMINT(stix->active_context->ip); + sp = STIX_OOP_TO_SMINT(stix->active_context->sp); code = mth->code; + receiver = stix->active_context->receiver; while (1) { - - bc = code->slot[ip++]; cmd = bc >> 4; if (cmd == CMD_EXTEND) { cmd = bc & 0xF; + b1 = code->slot[ip++]; + } + else + { + b1 = bc & 0xF; } +printf ("CMD => %d, B1 = %d\n", (int)cmd, (int)b1); + switch (cmd) + { + + case CMD_PUSH_INSTVAR: +printf ("PUSHING INSTVAR %d\n", (int)b1); + STIX_ASSERT (STIX_OBJ_GET_FLAGS_TYPE(receiver) == STIX_OBJ_TYPE_OOP); + stix->active_context->slot[++sp] = ((stix_oop_oop_t)receiver)->slot[b1]; + break; + + case CMD_PUSH_TEMPVAR: +printf ("PUSHING TEMPVAR %d\n", (int)b1); + stix->active_context->slot[++sp] = stix->active_context->slot[b1]; + break; + + case CMD_PUSH_LITERAL: + stix->active_context->slot[++sp] = mth->slot[b1]; + break; + + case CMD_POP_AND_STORE_INTO_INSTVAR: +printf ("STORING INSTVAR %d\n", (int)b1); + ((stix_oop_oop_t)receiver)->slot[b1] = stix->active_context->slot[sp--]; + break; + + case CMD_POP_AND_STORE_INTO_TEMPVAR: +printf ("STORING TEMPVAR %d\n", (int)b1); + stix->active_context->slot[b1] = stix->active_context->slot[sp--]; + break; + + /* + case CMD_POP_AND_STORE_INTO_OBJECT_POINTED_TO_BY_LITERAL??? + */ + + case CMD_SEND_MESSAGE_TO_SELF: + case CMD_SEND_MESSAGE_TO_SUPER: + break; + + + case CMD_PUSH_SPECIAL: + switch (b1) + { + case SUBCMD_PUSH_RECEIVER: +printf ("PUSHING RECEIVER\n"); + stix->active_context->slot[++sp] = receiver; + break; + + case SUBCMD_PUSH_NIL: + stix->active_context->slot[++sp] = stix->_nil; + break; + + case SUBCMD_PUSH_TRUE: +printf ("PUSHING TRUE\n"); + stix->active_context->slot[++sp] = stix->_true; + break; + + case SUBCMD_PUSH_FALSE: + stix->active_context->slot[++sp] = stix->_false; + break; + } + break; + + case CMD_DO_SPECIAL: + switch (b1) + { + case SUBCMD_RETURN_MESSAGE_RECEIVER: +printf ("RETURNING. RECEIVER...........\n"); + /* TO RETURN SAVE REGISTERS... RELOAD REGISTERS.. */ goto done; + break; + + case SUBCMD_RETURN_MESSAGE_STACKTOP: +printf ("RETURNING FrOM MESSAGE..........\n"); goto done; + break; + + /*case CMD_RETURN_BLOCK_STACKTOP:*/ + + } + break; + } } - return -1; +done: + return 0; } int stix_invoke (stix_t* stix, const stix_ucs_t* objname, const stix_ucs_t* mthname) diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index 4b65d27..d013f44 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -437,6 +437,65 @@ struct stix_compiler_t #endif +#define MAKE_CODE(x,y) (((x) << 4) | y) +#define MAX_CODE_INDEX 0xFFFFu +#define MAX_CODE_NARGS 0xFFFFu + +#define CMD_EXTEND 0x0 +#define CMD_EXTEND_DOUBLE 0x1 + +/* positional instructions + * XXXXJJJJ + * 0000XXXX JJJJJJJJ + * 0001XXXX JJJJJJJJ JJJJJJJJ + * + * XXXX is one of the following positional instructions. + * JJJJ or JJJJJJJJ is the position. + */ +#define CMD_PUSH_INSTVAR 0x2 +#define CMD_PUSH_TEMPVAR 0x3 +#define CMD_PUSH_LITERAL 0x4 +#define CMD_POP_AND_STORE_INTO_INSTVAR 0x5 /* pop and store */ +#define CMD_POP_AND_STORE_INTO_CLASSVAR 0x6 /* pop and store */ +#define CMD_POP_AND_STORE_INTO_TEMPVAR 0x7 /* pop and store */ + +/* + * XXXXJJJJ KKKKKKKK + * 0000XXXX JJJJJJJJ KKKKKKKK + * 0001XXXX JJJJJJJJ JJJJJJJJ KKKKKKKK KKKKKKKK + */ +#define CMD_SEND_MESSAGE_TO_SELF 0xA +#define CMD_SEND_MESSAGE_TO_SUPER 0xB + +#define CMD_PUSH_SPECIAL 0xE +#define CMD_DO_SPECIAL 0xF + + +#define SUBCMD_PUSH_RECEIVER 0x0 +#define SUBCMD_PUSH_NIL 0x1 +#define SUBCMD_PUSH_TRUE 0x2 +#define SUBCMD_PUSH_FALSE 0x3 + +#define SUBCMD_DUP_STACKTOP 0x0 +#define SUBCMD_POP_STACKTOP 0x1 +#define SUBCMD_RETURN_MESSAGE_STACKTOP 0x2 +#define SUBCMD_RETURN_BLOCK_STACKTOP 0x3 +#define SUBCMD_RETURN_MESSAGE_RECEIVER 0x4 +#define SUBCMD_EXEC_PRIMITIVE 0xF +/* ---------------------------------- */ +#define CODE_PUSH_RECEIVER MAKE_CODE(CMD_PUSH_SPECIAL, SUBCMD_PUSH_RECEIVER) +#define CODE_PUSH_NIL MAKE_CODE(CMD_PUSH_SPECIAL, SUBCMD_PUSH_NIL) +#define CODE_PUSH_TRUE MAKE_CODE(CMD_PUSH_SPECIAL, SUBCMD_PUSH_TRUE) +#define CODE_PUSH_FALSE MAKE_CODE(CMD_PUSH_SPECIAL, SUBCMD_PUSH_FALSE) + +/* special code */ +#define CODE_DUP_STACKTOP MAKE_CODE(CMD_DO_SPECIAL, SUBCMD_DUP_STACKTOP) +#define CODE_POP_STACKTOP MAKE_CODE(CMD_DO_SPECIAL, SUBCMD_POP_STACKTOP) +#define CODE_RETURN_MESSAGE_STACKTOP MAKE_CODE(CMD_DO_SPECIAL, SUBCMD_RETURN_MESSAGE_STACKTOP) +#define CODE_RETURN_BLOCK_STACKTOP MAKE_CODE(CMD_DO_SPECIAL, SUBCMD_RETURN_BLOCK_STACKTOP) +#define CODE_RETURN_MESSAGE_RECEIVER MAKE_CODE(CMD_DO_SPECIAL, SUBCMD_RETURN_MESSAGE_RECEIVER) +#define CODE_EXEC_PRIMITIVE MAKE_CODE(CMD_DO_SPECIAL, SUBCMD_EXEC_PRIMITIVE) + #if defined(__cplusplus) extern "C" { #endif