diff --git a/stix/lib/Stix.st b/stix/lib/Stix.st index aa7c0a9..13e1836 100644 --- a/stix/lib/Stix.st +++ b/stix/lib/Stix.st @@ -385,6 +385,3 @@ #dcl owner preamble ntmprs nargs code source. } -################################################################# -## MAIN -################################################################# diff --git a/stix/lib/comp.c b/stix/lib/comp.c index 15bdab4..1fdeb84 100644 --- a/stix/lib/comp.c +++ b/stix/lib/comp.c @@ -1565,6 +1565,10 @@ static int emit_double_param_instruction (stix_t* stix, int cmd, stix_size_t par bc = cmd | 0x80; goto write_long; } + + case BCODE_MAKE_BLOCK: + bc = cmd; + goto write_long; } stix->errnum = STIX_EINVAL; @@ -2471,6 +2475,10 @@ static int compile_block_expression (stix_t* stix) /* store the accumulated number of temporaries for the current block */ if (store_tmpr_count_for_block (stix, stix->c->mth.tmpr_count) <= -1) return -1; +#if defined(STIX_USE_MAKE_BLOCK) +printf ("\tmake_block nargs %d ntmprs %d\n", (int)block_arg_count, (int)stix->c->mth.tmpr_count /*block_tmpr_count*/); + if (emit_double_param_instruction(stix, BCODE_MAKE_BLOCK, block_arg_count, stix->c->mth.tmpr_count/*block_tmpr_count*/) <= -1) return -1; +#else printf ("\tpush_context nargs %d ntmprs %d\n", (int)block_arg_count, (int)stix->c->mth.tmpr_count /*block_tmpr_count*/); printf ("\tpush smint %d\n", (int)block_arg_count); printf ("\tpush smint %d\n", (int)stix->c->mth.tmpr_count /*block_tmpr_count*/); @@ -2479,6 +2487,8 @@ printf ("\tsend_block_copy\n"); emit_push_smint_literal(stix, block_arg_count) <= -1 || emit_push_smint_literal(stix, stix->c->mth.tmpr_count/*block_tmpr_count*/) <= -1 || emit_byte_instruction(stix, BCODE_SEND_BLOCK_COPY) <= -1) return -1; +#endif + printf ("\tjump\n"); /* insert dummy instructions before replacing them with a jump instruction */ @@ -3332,7 +3342,8 @@ static int compile_method_expression (stix_t* stix, int pop) GET_TOKEN (stix); -printf ("ASSIGNIUNG TO ...."); +printf ("ASSIGNING TO ...."); +assignee.ptr = &stix->c->mth.assignees.ptr[assignee_offset]; print_ucs (&assignee); printf ("\n"); diff --git a/stix/lib/exec.c b/stix/lib/exec.c index d9d6181..b694284 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -673,8 +673,6 @@ printf ("~~~~~~~~~~ BLOCK VALUING %p TO NEW BLOCK %p\n", org_blkctx, blkctx); } ACTIVE_STACK_POPS (stix, nargs + 1); /* pop arguments and receiver */ - /*blkctx->ip = blkctx->iip;*/ - STIX_ASSERT (blkctx->home != stix->_nil); /* the number of temporaries stored in the block context @@ -1640,6 +1638,51 @@ printf ("LEAVING_BLOCK\n"); ACTIVE_STACK_PUSH (stix, return_value); break; + case BCODE_MAKE_BLOCK: + { + stix_oop_context_t blkctx; + + /* b1 - number of block arguments + * b2 - number of block temporaries */ + FETCH_PARAM_CODE_TO (stix, b1); + FETCH_PARAM_CODE_TO (stix, b2); + +printf ("MAKE_BLOCK %d %d\n", (int)b1, (int)b2); + + STIX_ASSERT (b1 >= 0); + STIX_ASSERT (b2 >= b1); + + /* the block context object created here is used + * as a base object for block context activation. + * primitive_block_context_value() clones a block + * context and activates the cloned context. + * this base block context is created with no + * stack for this reason. */ + blkctx = (stix_oop_context_t)stix_instantiate (stix, stix->_block_context, STIX_NULL, 0); + if (!blkctx) return -1; + + /* the long forward jump instruction has the format of + * 11000100 KKKKKKKK or 11000100 KKKKKKKK KKKKKKKK + * depending on STIX_BCODE_LONG_PARAM_SIZE. change 'ip' to point to + * the instruction after the jump. */ + blkctx->ip = STIX_OOP_FROM_SMINT(stix->ip + STIX_BCODE_LONG_PARAM_SIZE + 1); + /* stack pointer below the bottom. this block context has an + * empty stack anyway. */ + blkctx->sp = STIX_OOP_FROM_SMINT(-1); + /* the number of arguments for a block context is local to the block */ + blkctx->method_or_nargs = STIX_OOP_FROM_SMINT(b1); + /* the number of temporaries here is an accumulated count including + * the number of temporaries of a home context */ + blkctx->ntmprs = STIX_OOP_FROM_SMINT(b2); + + blkctx->home = (stix_oop_t)stix->active_context; + blkctx->receiver_or_source = stix->_nil; /* no source */ + + blkctx->origin = stix->active_context->origin; + ACTIVE_STACK_PUSH (stix, (stix_oop_t)blkctx); + break; + } + case BCODE_SEND_BLOCK_COPY: { stix_ooi_t nargs, ntmprs; @@ -1672,6 +1715,7 @@ printf ("SEND_BLOCK_COPY\n"); /* get the receiver to the block copy message after block context instantiation * not to get affected by potential GC */ rctx = (stix_oop_context_t)ACTIVE_STACK_GETTOP(stix); + STIX_ASSERT (rctx == stix->active_context); /* [NOTE] * blkctx->caller is left to nil. it is set to the diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index 3038c08..e6c6fc3 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -36,9 +36,13 @@ * while stix has not been fully initialized when this is defined*/ #define STIX_SUPPORT_GC_DURING_IGNITION -/* define this to generate CTXTEMVAR instructions */ +/* define this to generate XXXX_CTXTEMVAR instructions */ #define STIX_USE_CTXTEMPVAR +/* define this to use the MAKE_BLOCK instruction instead of + * PUSH_CONTEXT, PUSH_INTLIT, PUSH_INTLIT, SEND_BLOCK_COPY */ +#define STIX_USE_MAKE_BLOCK + /* define this to allow an pointer(OOP) object to have trailing bytes * this is used to embed bytes codes into the back of a compile method * object instead of putting in in a separate byte array. */ @@ -47,7 +51,6 @@ /* this is for gc debugging */ #define STIX_DEBUG_GC_001 - #include /* TODO: delete these header inclusion lines */ #include #include @@ -764,13 +767,14 @@ enum stix_bcode_t BCODE_PUSH_INTLIT = 0xB1, /* 177 */ BCODE_PUSH_NEGINTLIT = 0xB2, /* 178 */ - /* UNUSED 0xE8 - 0xF8 */ + /* UNUSED 0xE8 - 0xF7 */ - BCODE_DUP_STACKTOP = 0xF9, - BCODE_POP_STACKTOP = 0xFA, - BCODE_RETURN_STACKTOP = 0xFB, /* ^something */ - BCODE_RETURN_RECEIVER = 0xFC, /* ^self */ - BCODE_RETURN_FROM_BLOCK = 0xFD, /* return the stack top from a block */ + BCODE_DUP_STACKTOP = 0xF8, + BCODE_POP_STACKTOP = 0xF9, + BCODE_RETURN_STACKTOP = 0xFA, /* ^something */ + BCODE_RETURN_RECEIVER = 0xFB, /* ^self */ + BCODE_RETURN_FROM_BLOCK = 0xFC, /* return the stack top from a block */ + BCODE_MAKE_BLOCK = 0xFD, BCODE_SEND_BLOCK_COPY = 0xFE, BCODE_NOOP = 0xFF }; diff --git a/stix/lib/stix.h b/stix/lib/stix.h index 5f5fe0c..75ef9a3 100644 --- a/stix/lib/stix.h +++ b/stix/lib/stix.h @@ -916,9 +916,8 @@ STIX_EXPORT int stix_ignite ( stix_t* stix ); - /** - * The stix_executes() function XXXXXXXXXXXXXX + * The stix_execute() function executes an activated context. */ STIX_EXPORT int stix_execute ( stix_t* stix