added PUSH_INTLIT and PUSH_NEGINTLIT instructions

This commit is contained in:
hyunghwan.chung 2015-07-02 15:45:48 +00:00
parent cf3e24a6cd
commit 37740da2d7
5 changed files with 68 additions and 59 deletions

View File

@ -141,7 +141,6 @@
#class SmallInteger(Number) #class SmallInteger(Number)
{ {
} }
#class Boolean(Object) #class Boolean(Object)

View File

@ -1513,6 +1513,8 @@ static int emit_single_param_instruction (stix_t* stix, int cmd, stix_oow_t para
case BCODE_JUMP2_FORWARD: case BCODE_JUMP2_FORWARD:
case BCODE_JUMP2_BACKWARD: case BCODE_JUMP2_BACKWARD:
case BCODE_PUSH_INTLIT:
case BCODE_PUSH_NEGINTLIT:
bc = cmd; bc = cmd;
goto write_long; goto write_long;
} }
@ -1605,10 +1607,16 @@ static int emit_push_smint_literal (stix_t* stix, stix_ooi_t i)
case 2: case 2:
return emit_byte_instruction (stix, BCODE_PUSH_TWO); return emit_byte_instruction (stix, BCODE_PUSH_TWO);
/* TODO: include some other numbers? like 3 */
} }
if (i >= 0 && i <= MAX_CODE_PARAM)
{
return emit_single_param_instruction(stix, BCODE_PUSH_INTLIT, i);
}
else if (i < 0 && i >= -(stix_ooi_t)MAX_CODE_PARAM)
{
return emit_single_param_instruction(stix, BCODE_PUSH_NEGINTLIT, -i);
}
if (add_literal(stix, STIX_OOP_FROM_SMINT(i), &index) <= -1 || if (add_literal(stix, STIX_OOP_FROM_SMINT(i), &index) <= -1 ||
emit_single_param_instruction(stix, BCODE_PUSH_LITERAL_0, index) <= -1) return -1; emit_single_param_instruction(stix, BCODE_PUSH_LITERAL_0, index) <= -1) return -1;
@ -2161,7 +2169,7 @@ static int compile_method_primitive (stix_t* stix)
/* /*
* method-primitive := "<" "primitive:" integer ">" * method-primitive := "<" "primitive:" integer ">"
*/ */
int prim_no; stix_ooi_t prim_no;
const stix_uch_t* ptr, * end; const stix_uch_t* ptr, * end;
if (!is_token_binsel(stix, VOCA_LT)) if (!is_token_binsel(stix, VOCA_LT))
@ -2188,14 +2196,14 @@ static int compile_method_primitive (stix_t* stix)
return -1; return -1;
} }
/*TODO: more checks the validity of the primitive number. support nubmer with radix and so on support more extensive syntax */ /*TODO: more checks the validity of the primitive number. support number with radix and so on support more extensive syntax. support primitive name, not number*/
ptr = stix->c->tok.name.ptr; ptr = stix->c->tok.name.ptr;
end = ptr + stix->c->tok.name.len; end = ptr + stix->c->tok.name.len;
prim_no = 0; prim_no = 0;
while (ptr < end && is_digitchar(*ptr)) while (ptr < end && is_digitchar(*ptr))
{ {
prim_no = prim_no * 10 + (*ptr - '0'); prim_no = prim_no * 10 + (*ptr - '0');
if (prim_no > MAX_CODE_PRIMNO) if (!STIX_OOI_IN_PREAMBLE_INDEX_RANGE(prim_no))
{ {
set_syntax_error (stix, STIX_SYNERR_PRIMNO, &stix->c->tok.loc, &stix->c->tok.name); set_syntax_error (stix, STIX_SYNERR_PRIMNO, &stix->c->tok.loc, &stix->c->tok.name);
return -1; return -1;
@ -3569,7 +3577,7 @@ static int add_compiled_method (stix_t* stix)
{ {
preamble_code = STIX_METHOD_PREAMBLE_RETURN_RECEIVER; preamble_code = STIX_METHOD_PREAMBLE_RETURN_RECEIVER;
} }
else if (stix->c->mth.code.len >= 2 && stix->c->mth.code.ptr[1] == BCODE_RETURN_STACKTOP) else if (stix->c->mth.code.len > 1 && stix->c->mth.code.ptr[1] == BCODE_RETURN_STACKTOP)
{ {
switch (stix->c->mth.code.ptr[0]) switch (stix->c->mth.code.ptr[0])
{ {
@ -3609,20 +3617,6 @@ static int add_compiled_method (stix_t* stix)
preamble_index = 2; preamble_index = 2;
break; break;
/*
case BCODE_PUSH_LITERAL_0:
case BCODE_PUSH_LITERAL_1:
case BCODE_PUSH_LITERAL_2:
case BCODE_PUSH_LITERAL_3:
case BCODE_PUSH_LITERAL_4:
case BCODE_PUSH_LITERAL_5:
case BCODE_PUSH_LITERAL_6:
case BCODE_PUSH_LITERAL_7:
TODO: check the literal frame. if the value in the literal frame is a small integer within the premable index range,
convert ito to PREAMBEL_RETURN_INDEX or NEGINDEX
break;
*/
case BCODE_PUSH_INSTVAR_0: case BCODE_PUSH_INSTVAR_0:
case BCODE_PUSH_INSTVAR_1: case BCODE_PUSH_INSTVAR_1:
case BCODE_PUSH_INSTVAR_2: case BCODE_PUSH_INSTVAR_2:
@ -3634,29 +3628,39 @@ static int add_compiled_method (stix_t* stix)
preamble_code = STIX_METHOD_PREAMBLE_RETURN_INSTVAR; preamble_code = STIX_METHOD_PREAMBLE_RETURN_INSTVAR;
preamble_index = stix->c->mth.code.ptr[0] & 0x7; /* low 3 bits */ preamble_index = stix->c->mth.code.ptr[0] & 0x7; /* low 3 bits */
break; break;
} }
} }
else if (stix->c->mth.code.ptr[0] == BCODE_PUSH_INSTVAR_X && else if (stix->c->mth.code.len > STIX_BCODE_LONG_PARAM_SIZE + 1 &&
stix->c->mth.code.ptr[STIX_BCODE_LONG_PARAM_SIZE + 1] == BCODE_RETURN_STACKTOP) stix->c->mth.code.ptr[STIX_BCODE_LONG_PARAM_SIZE + 1] == BCODE_RETURN_STACKTOP)
{ {
int i; int i;
switch (stix->c->mth.code.ptr[0])
STIX_ASSERT (stix->c->mth.code.len >= STIX_BCODE_LONG_PARAM_SIZE + 1);
preamble_code = STIX_METHOD_PREAMBLE_RETURN_INSTVAR;
preamble_index = 0;
for (i = 1; i <= STIX_BCODE_LONG_PARAM_SIZE; i++)
{ {
preamble_index = (preamble_index << 8) | stix->c->mth.code.ptr[i]; case BCODE_PUSH_INSTVAR_X:
if (!STIX_OOI_IN_PREAMBLE_INDEX_RANGE(preamble_index)) preamble_code = STIX_METHOD_PREAMBLE_RETURN_INSTVAR;
{ goto set_preamble_index;
/* the index got out of the range */
preamble_code = STIX_METHOD_PREAMBLE_NONE; case BCODE_PUSH_INTLIT:
preamble_code = STIX_METHOD_PREAMBLE_RETURN_INDEX;
goto set_preamble_index;
case BCODE_PUSH_NEGINTLIT:
preamble_code = STIX_METHOD_PREAMBLE_RETURN_NEGINDEX;
goto set_preamble_index;
set_preamble_index:
preamble_index = 0; preamble_index = 0;
break; for (i = 1; i <= STIX_BCODE_LONG_PARAM_SIZE; i++)
} {
preamble_index = (preamble_index << 8) | stix->c->mth.code.ptr[i];
}
if (!STIX_OOI_IN_PREAMBLE_INDEX_RANGE(preamble_index))
{
/* the index got out of the range */
preamble_code = STIX_METHOD_PREAMBLE_NONE;
preamble_index = 0;
}
} }
} }
} }

View File

@ -1410,28 +1410,18 @@ printf ("RETURN FALSE AT PREAMBLE\n");
ACTIVE_STACK_SETTOP (stix, stix->_false); ACTIVE_STACK_SETTOP (stix, stix->_false);
break; break;
case STIX_METHOD_PREAMBLE_RETURN_NEGINDEX:
printf ("RETURN %d AT PREAMBLE\n", (int)-STIX_METHOD_GET_PREAMBLE_INDEX(preamble));
ACTIVE_STACK_POPS (stix, b1);
ACTIVE_STACK_SETTOP (stix, STIX_OOP_FROM_SMINT(-STIX_METHOD_GET_PREAMBLE_INDEX(preamble)));
break;
case STIX_METHOD_PREAMBLE_RETURN_INDEX: case STIX_METHOD_PREAMBLE_RETURN_INDEX:
printf ("RETURN %d AT PREAMBLE\n", (int)STIX_METHOD_GET_PREAMBLE_INDEX(preamble)); printf ("RETURN %d AT PREAMBLE\n", (int)STIX_METHOD_GET_PREAMBLE_INDEX(preamble));
ACTIVE_STACK_POPS (stix, b1); ACTIVE_STACK_POPS (stix, b1);
ACTIVE_STACK_SETTOP (stix, STIX_OOP_FROM_SMINT(STIX_METHOD_GET_PREAMBLE_INDEX(preamble))); ACTIVE_STACK_SETTOP (stix, STIX_OOP_FROM_SMINT(STIX_METHOD_GET_PREAMBLE_INDEX(preamble)));
break; break;
#if 0 case STIX_METHOD_PREAMBLE_RETURN_NEGINDEX:
case STIX_METHOD_PREAMBLE_RETURN_ZERO: printf ("RETURN %d AT PREAMBLE\n", (int)-STIX_METHOD_GET_PREAMBLE_INDEX(preamble));
case STIX_METHOD_PREAMBLE_RETURN_ONE:
case STIX_METHOD_PREAMBLE_RETURN_TWO:
printf ("RETURN %d AT PREAMBLE\n", (int)(preamble_code - STIX_METHOD_PREAMBLE_RETURN_NEGONE - 1));
ACTIVE_STACK_POPS (stix, b1); ACTIVE_STACK_POPS (stix, b1);
ACTIVE_STACK_SETTOP (stix, STIX_OOP_FROM_SMINT(preamble_code - STIX_METHOD_PREAMBLE_RETURN_NEGONE - 1)); ACTIVE_STACK_SETTOP (stix, STIX_OOP_FROM_SMINT(-STIX_METHOD_GET_PREAMBLE_INDEX(preamble)));
break; break;
#endif
case STIX_METHOD_PREAMBLE_RETURN_INSTVAR: case STIX_METHOD_PREAMBLE_RETURN_INSTVAR:
{ {
stix_oop_oop_t rcv; stix_oop_oop_t rcv;
@ -1539,6 +1529,18 @@ printf ("PUSH_TWO\n");
ACTIVE_STACK_PUSH (stix, STIX_OOP_FROM_SMINT(2)); ACTIVE_STACK_PUSH (stix, STIX_OOP_FROM_SMINT(2));
break; break;
case BCODE_PUSH_INTLIT:
FETCH_PARAM_CODE_TO (stix, b1);
ACTIVE_STACK_PUSH (stix, STIX_OOP_FROM_SMINT(b1));
printf ("PUSH_INTLIT %d\n", (int)b1);
break;
case BCODE_PUSH_NEGINTLIT:
FETCH_PARAM_CODE_TO (stix, b1);
ACTIVE_STACK_PUSH (stix, STIX_OOP_FROM_SMINT(-b1));
printf ("PUSH_NEGINTLIT %d\n", (int)-b1);
break;
/* -------------------------------------------------------- */ /* -------------------------------------------------------- */
case BCODE_DUP_STACKTOP: case BCODE_DUP_STACKTOP:

View File

@ -30,7 +30,7 @@
#include "stix.h" #include "stix.h"
/* you can define this to either 1 or 2 */ /* you can define this to either 1 or 2 */
#define STIX_BCODE_LONG_PARAM_SIZE 1 #define STIX_BCODE_LONG_PARAM_SIZE 2
/* this is useful for debugging. stix_gc() can be called /* this is useful for debugging. stix_gc() can be called
* while stix has not been fully initialized when this is defined*/ * while stix has not been fully initialized when this is defined*/
@ -488,16 +488,16 @@ struct stix_compiler_t
# define MAX_CODE_NARGS (0xFFu) # define MAX_CODE_NARGS (0xFFu)
# define MAX_CODE_NBLKARGS (0xFFu) # define MAX_CODE_NBLKARGS (0xFFu)
# define MAX_CODE_NBLKTMPRS (0xFFu) # define MAX_CODE_NBLKTMPRS (0xFFu)
# define MAX_CODE_PRIMNO (0xFFFFu)
# define MAX_CODE_JUMP (0xFFu) # define MAX_CODE_JUMP (0xFFu)
# define MAX_CODE_PARAM (0xFFu)
#elif defined(STIX_BCODE_LONG_PARAM_SIZE) && (STIX_BCODE_LONG_PARAM_SIZE == 2) #elif defined(STIX_BCODE_LONG_PARAM_SIZE) && (STIX_BCODE_LONG_PARAM_SIZE == 2)
# define MAX_CODE_INDEX (0xFFFFu) # define MAX_CODE_INDEX (0xFFFFu)
# define MAX_CODE_NTMPRS (0xFFFFu) # define MAX_CODE_NTMPRS (0xFFFFu)
# define MAX_CODE_NARGS (0xFFFFu) # define MAX_CODE_NARGS (0xFFFFu)
# define MAX_CODE_NBLKARGS (0xFFFFu) # define MAX_CODE_NBLKARGS (0xFFFFu)
# define MAX_CODE_NBLKTMPRS (0xFFFFu) # define MAX_CODE_NBLKTMPRS (0xFFFFu)
# define MAX_CODE_PRIMNO (0xFFFFu)
# define MAX_CODE_JUMP (0xFFFFu) # define MAX_CODE_JUMP (0xFFFFu)
# define MAX_CODE_PARAM (0xFFFFu)
#else #else
# error Unsupported STIX_BCODE_LONG_PARAM_SIZE # error Unsupported STIX_BCODE_LONG_PARAM_SIZE
#endif #endif
@ -746,6 +746,7 @@ enum stix_bcode_t
BCODE_SEND_MESSAGE_X = 0xF0, /* 240 */ BCODE_SEND_MESSAGE_X = 0xF0, /* 240 */
BCODE_SEND_MESSAGE_TO_SUPER_X = 0xF4, /* 244 */ BCODE_SEND_MESSAGE_TO_SUPER_X = 0xF4, /* 244 */
/* -------------------------------------- */
BCODE_JUMP2_FORWARD = 0xC5, /* 197 */ BCODE_JUMP2_FORWARD = 0xC5, /* 197 */
BCODE_JUMP2_BACKWARD = 0xC9, /* 201 */ BCODE_JUMP2_BACKWARD = 0xC9, /* 201 */
@ -760,6 +761,9 @@ enum stix_bcode_t
BCODE_PUSH_ONE = 0x89, /* 137 */ BCODE_PUSH_ONE = 0x89, /* 137 */
BCODE_PUSH_TWO = 0x8A, /* 138 */ BCODE_PUSH_TWO = 0x8A, /* 138 */
BCODE_PUSH_INTLIT = 0xB1, /* 177 */
BCODE_PUSH_NEGINTLIT = 0xB2, /* 178 */
/* UNUSED 0xE8 - 0xF8 */ /* UNUSED 0xE8 - 0xF8 */
BCODE_DUP_STACKTOP = 0xF9, BCODE_DUP_STACKTOP = 0xF9,

View File

@ -616,9 +616,9 @@ struct stix_method_t
* 1 - return self * 1 - return self
* 2 - return nil * 2 - return nil
* 3 - return true * 3 - return true
* 4 - return false * 4 - return false
* 5 - return -index. * 5 - return index.
* 6 - return index. * 6 - return -index.
* 7 - return instvar[index] * 7 - return instvar[index]
* 8 - do primitive[index] * 8 - do primitive[index]
*/ */
@ -631,8 +631,8 @@ struct stix_method_t
#define STIX_METHOD_PREAMBLE_RETURN_NIL 2 #define STIX_METHOD_PREAMBLE_RETURN_NIL 2
#define STIX_METHOD_PREAMBLE_RETURN_TRUE 3 #define STIX_METHOD_PREAMBLE_RETURN_TRUE 3
#define STIX_METHOD_PREAMBLE_RETURN_FALSE 4 #define STIX_METHOD_PREAMBLE_RETURN_FALSE 4
#define STIX_METHOD_PREAMBLE_RETURN_NEGINDEX 5 #define STIX_METHOD_PREAMBLE_RETURN_INDEX 5
#define STIX_METHOD_PREAMBLE_RETURN_INDEX 6 #define STIX_METHOD_PREAMBLE_RETURN_NEGINDEX 6
#define STIX_METHOD_PREAMBLE_RETURN_INSTVAR 7 #define STIX_METHOD_PREAMBLE_RETURN_INSTVAR 7
#define STIX_METHOD_PREAMBLE_PRIMITIVE 8 #define STIX_METHOD_PREAMBLE_PRIMITIVE 8