From 3d937334d80abcbdd07e94dd7e23b00283d6d561 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 11 Feb 2016 14:26:26 +0000 Subject: [PATCH] touched up code a bit --- stix/kernel/Process.st | 2 +- stix/lib/comp.c | 22 ++++++------ stix/lib/exec.c | 78 ++++++++++++++++++++---------------------- stix/lib/main.c | 2 ++ stix/lib/stix.c | 26 ++++++++++++++ stix/lib/stix.h | 33 ++++++++++++------ 6 files changed, 99 insertions(+), 64 deletions(-) diff --git a/stix/kernel/Process.st b/stix/kernel/Process.st index 56379b3..1203377 100644 --- a/stix/kernel/Process.st +++ b/stix/kernel/Process.st @@ -26,7 +26,7 @@ #class(#pointer) Process(Object) { - #dcl initial active state prev next. + #dcl initial active state prev next sp. #method new { diff --git a/stix/lib/comp.c b/stix/lib/comp.c index e6b9b82..cf8d7f4 100644 --- a/stix/lib/comp.c +++ b/stix/lib/comp.c @@ -3838,18 +3838,6 @@ static int compile_message_expression (stix_t* stix, int to_super) } while (1); -#if 0 - if (compile_keyword_message(stix, to_super) <= -1) return -1; - - while (stix->c->tok.type == STIX_IOTOK_SEMICOLON) - { - printf ("\tpop_stacktop for cascading\n"); - if (emit_byte_instruction(stix, CODE_POP_STACKTOP) <= -1) return -1; - GET_TOKEN (stix); - if (compile_keyword_message(stix, 0) <= -1) return -1; - } -#endif - done: return 0; } @@ -3862,6 +3850,8 @@ static int compile_basic_expression (stix_t* stix, const stix_oocs_t* ident, con int to_super; if (compile_expression_primary(stix, ident, ident_loc, ident_dotted, &to_super) <= -1) return -1; + +#if 0 if (stix->c->tok.type != STIX_IOTOK_EOF && stix->c->tok.type != STIX_IOTOK_RBRACE && stix->c->tok.type != STIX_IOTOK_PERIOD && @@ -3869,6 +3859,14 @@ static int compile_basic_expression (stix_t* stix, const stix_oocs_t* ident, con { if (compile_message_expression(stix, to_super) <= -1) return -1; } +#else + if (stix->c->tok.type == STIX_IOTOK_IDENT || + stix->c->tok.type == STIX_IOTOK_BINSEL || + stix->c->tok.type == STIX_IOTOK_KEYWORD) + { + if (compile_message_expression(stix, to_super) <= -1) return -1; + } +#endif return 0; } diff --git a/stix/lib/exec.c b/stix/lib/exec.c index 70e2ccb..c43df75 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -106,7 +106,6 @@ static stix_oop_process_t make_process (stix_t* stix, stix_oop_context_t c) { stix_oop_process_t proc; -/* TODO: do something about the stack. */ stix_pushtmp (stix, (stix_oop_t*)&c); proc = (stix_oop_process_t)stix_instantiate (stix, stix->_process, STIX_NULL, stix->option.dfl_procstk_size); stix_poptmp (stix); @@ -114,6 +113,7 @@ static stix_oop_process_t make_process (stix_t* stix, stix_oop_context_t c) proc->state = STIX_SMOOI_TO_OOP(0); proc->initial_context = c; + proc->sp = STIX_SMOOI_TO_OOP(-1); return proc; } @@ -516,12 +516,12 @@ TODO: overcome this problem /* the initial context starts the life of the entire VM * and is not really worked on except that it is used to call the - * initial method. so it doesn't really require any extra stack space. - * TODO: verify this theory of mine. */ + * initial method. so it doesn't really require any extra stack space. */ +/* TODO: verify this theory of mine. */ stix->ip = 0; stix->sp = -1; - ctx->origin = ctx; + ctx->origin = ctx; /* point to self */ ctx->method_or_nargs = (stix_oop_t)mth; /* fake. help SWITCH_ACTIVE_CONTEXT() not fail*/ /* [NOTE] @@ -540,7 +540,7 @@ TODO: overcome this problem STORE_ACTIVE_SP (stix); /* stix->active_context->sp = STIX_SMOOI_TO_OOP(stix->sp) */ stix_pushtmp (stix, (stix_oop_t*)&mth); - /* call start_initial_process() intead of start_new_process() */ + /* call start_initial_process() instead of start_new_process() */ proc = start_initial_process (stix, ctx); stix_poptmp (stix); if (!proc) return -1; @@ -899,6 +899,10 @@ static int prim_basic_at_put (stix_t* stix, stix_ooi_t nargs) static int __block_value (stix_t* stix, stix_ooi_t nargs, stix_ooi_t num_first_arg_elems, stix_oop_context_t* pblkctx) { + /* prepare a new block context for activation. + * the receiver must be a block context which becomes the base + * for a new block context. */ + stix_oop_context_t blkctx, org_blkctx; stix_ooi_t local_ntmprs, i; stix_ooi_t actual_arg_count; @@ -1036,6 +1040,12 @@ printf ("<>\n"); static int prim_block_new_process (stix_t* stix, stix_ooi_t nargs) { + /* create a new process from a block context. + * the receiver must be be a block. + * [ 1 + 2 ] newProcess. + * [ :a :b | a + b ] newProcess: #(1 2) + */ + int x; stix_oop_context_t blkctx; stix_oop_process_t proc; @@ -1055,6 +1065,8 @@ static int prim_block_new_process (stix_t* stix, stix_ooi_t nargs) xarg = ACTIVE_STACK_GETTOP(stix); if (!STIX_ISTYPEOF(stix,xarg,STIX_OBJ_TYPE_OOP)) { + /* the only optional argument must be an OOP-indexable + * object like an array */ return 0; } @@ -1064,35 +1076,15 @@ static int prim_block_new_process (stix_t* stix, stix_ooi_t nargs) /* this primitive creates a new process with a block as if the block * is sent the value message */ x = __block_value (stix, nargs, num_first_arg_elems, &blkctx); - if (x <= 0) return x; /* hard failure and soft failure */ + if (x <= 0) return x; /* both hard failure and soft failure */ proc = make_process (stix, blkctx); - if (!proc) return -1; /* hard failure */ /* TOOD: can't this be a soft failure? */ + if (!proc) return -1; /* hard failure */ /* TOOD: can't this be treated as a soft failure? */ /* __block_value() has popped all arguments and the receiver. * PUSH the return value instead of changing the stack top */ ACTIVE_STACK_PUSH (stix, (stix_oop_t)proc); return 1; - -#if 0 - stix_oop_process_t proc; - stix_oop_context_t rcv; - - rcv = (stix_oop_context_t)ACTIVE_STACK_GETTOP(stix); - if (STIX_CLASSOF(stix, rcv) != stix->_block_context) - { -#if defined(STIX_DEBUG_EXEC) -printf ("PRIMITVE VALUE RECEIVER IS NOT A BLOCK CONTEXT\n"); -#endif - return 0; - } - - proc = make_process (stix, rcv); - if (!proc) return -1; /* hard failure */ /* TOOD: can't this be a soft failure? */ - - ACTIVE_STACK_SETTOP (stix, (stix_oop_t)proc); - return 1; -#endif } static int prim_integer_add (stix_t* stix, stix_ooi_t nargs) @@ -2352,6 +2344,8 @@ printf ("BCODE = %x\n", bcode); do { + /* ntmprs contains the number of defined temporaries + * including those defined in the home context */ home_ntmprs = STIX_OOP_TO_SMOOI(((stix_oop_context_t)home)->ntmprs); if (b1 >= home_ntmprs) break; @@ -2365,6 +2359,8 @@ printf ("BCODE = %x\n", bcode); } while (1); + /* bx is the actual index within the actual context + * containing the temporary */ bx = b1 - home_ntmprs; } else @@ -2376,7 +2372,7 @@ printf ("BCODE = %x\n", bcode); if ((bcode >> 4) & 1) { - /* push - bit 4 on*/ + /* push - bit 4 on */ DBGOUT_EXEC_1 ("PUSH_TEMPVAR %d", (int)b1); ACTIVE_STACK_PUSH (stix, ctx->slot[bx]); } @@ -2836,7 +2832,7 @@ printf ("<> SP=%d\n", (int)stix->sp); if (stix->processor->active->initial_context == stix->active_context) { -/* TODO: terminate a proces... */ +/* TODO: terminate a process... */ printf ("TERMINATING XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); } else @@ -2895,12 +2891,11 @@ printf ("TERMINATE A PROCESS............\n"); STIX_ASSERT (b1 >= 0); STIX_ASSERT (b2 >= b1); - /* the block context object created here is used - * as a base object for block context activation. - * prim_block_value() clones a block - * context and activates the cloned context. - * this base block context is created with no - * stack for this reason. */ + /* the block context object created here is used as a base + * object for block context activation. prim_block_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; @@ -2909,8 +2904,8 @@ printf ("TERMINATE A PROCESS............\n"); * depending on STIX_BCODE_LONG_PARAM_SIZE. change 'ip' to point to * the instruction after the jump. */ blkctx->ip = STIX_SMOOI_TO_OOP(stix->ip + STIX_BCODE_LONG_PARAM_SIZE + 1); - /* stack pointer below the bottom. this block context has an - * empty stack anyway. */ + /* stack pointer below the bottom. this base block context + * has an empty stack anyway. */ blkctx->sp = STIX_SMOOI_TO_OOP(-1); /* the number of arguments for a block context is local to the block */ blkctx->method_or_nargs = STIX_SMOOI_TO_OOP(b1); @@ -2918,11 +2913,14 @@ printf ("TERMINATE A PROCESS............\n"); * the number of temporaries of a home context */ blkctx->ntmprs = STIX_SMOOI_TO_OOP(b2); - - blkctx->home = (stix_oop_t)stix->active_context; - blkctx->receiver_or_source = stix->_nil; /* no source */ + /* set the home context where it's defined */ + blkctx->home = (stix_oop_t)stix->active_context; + /* no source for a base block context. */ + blkctx->receiver_or_source = stix->_nil; blkctx->origin = stix->active_context->origin; + + /* push the new block context to the stack of the active context */ ACTIVE_STACK_PUSH (stix, (stix_oop_t)blkctx); break; } diff --git a/stix/lib/main.c b/stix/lib/main.c index f224140..c6262e5 100644 --- a/stix/lib/main.c +++ b/stix/lib/main.c @@ -449,6 +449,8 @@ int main (int argc, char* argv[]) stix_setoption (stix, STIX_SYMTAB_SIZE, &tab_size); tab_size = 5000; stix_setoption (stix, STIX_SYSDIC_SIZE, &tab_size); + tab_size = 5000; + stix_setoption (stix, STIX_PROCSTK_SIZE, &tab_size); } { diff --git a/stix/lib/stix.c b/stix/lib/stix.c index fe145dd..3c4b8b9 100644 --- a/stix/lib/stix.c +++ b/stix/lib/stix.c @@ -89,6 +89,10 @@ int stix_init (stix_t* stix, stix_mmgr_t* mmgr, stix_oow_t heapsz, const stix_vm stix->mmgr = mmgr; stix->vmprim = *vmprim; + stix->option.dfl_symtab_size = STIX_DFL_SYMTAB_SIZE; + stix->option.dfl_sysdic_size = STIX_DFL_SYSDIC_SIZE; + stix->option.dfl_procstk_size = STIX_DFL_PROCSTK_SIZE; + /*stix->permheap = stix_makeheap (stix, what is the best size???); if (!stix->curheap) goto oops; */ stix->curheap = stix_makeheap (stix, heapsz); @@ -171,18 +175,40 @@ int stix_setoption (stix_t* stix, stix_option_t id, const void* value) return 0; case STIX_SYMTAB_SIZE: + { + stix_oow_t w; + + w = *(stix_oow_t*)value; + if (w <= 0 || w > STIX_SMOOI_MAX) goto einval; + stix->option.dfl_symtab_size = *(stix_oow_t*)value; return 0; + } case STIX_SYSDIC_SIZE: + { + stix_oow_t w; + + w = *(stix_oow_t*)value; + if (w <= 0 || w > STIX_SMOOI_MAX) goto einval; + stix->option.dfl_sysdic_size = *(stix_oow_t*)value; return 0; + } case STIX_PROCSTK_SIZE: + { + stix_oow_t w; + + w = *(stix_oow_t*)value; + if (w <= 0 || w > STIX_SMOOI_MAX) goto einval; + stix->option.dfl_procstk_size = *(stix_oow_t*)value; return 0; + } } +einval: stix->errnum = STIX_EINVAL; return -1; } diff --git a/stix/lib/stix.h b/stix/lib/stix.h index 8c9e7ec..ce60c8d 100644 --- a/stix/lib/stix.h +++ b/stix/lib/stix.h @@ -71,6 +71,14 @@ enum stix_option_t }; typedef enum stix_option_t stix_option_t; +enum stix_option_dflval_t +{ + STIX_DFL_SYMTAB_SIZE = 5000, + STIX_DFL_SYSDIC_SIZE = 5000, + STIX_DFL_PROCSTK_SIZE = 5000 +}; +typedef enum stix_option_dflval_t stix_option_dflval_t; + enum stix_trait_t { /* perform no garbage collection when the heap is full. @@ -502,31 +510,31 @@ struct stix_context_t stix_oop_t method_or_nargs; /* it points to the receiver of the message for a method context. - * a block context created but not yet activated has nil in this + * a base block context(created but not yet activated) has nil in this * field. if a block context is activated by 'value', it points * to the block context object used as a base for shallow-copy. */ stix_oop_t receiver_or_source; /* it is set to nil for a method context. * for a block context, it points to the active context at the - * moment the block context was created. that is, it ponts to - * a method context where the block has been defined. an activated - * block context copies this field from the source. */ + * moment the block context was created. that is, it points to + * a method context where the base block has been defined. + * an activated block context copies this field from the source. */ stix_oop_t home; - /* when a method context is created, it is set to itself. - * no change is made when the method context is activated. - * when a block context is created, it is set to the origin - * of the active context. when the block context is shallow-copied - * for activation, it is set to the origin of the source block contxt. */ + /* when a method context is created, it is set to itself. no change is + * made when the method context is activated. when a block context is + * created (when MAKE_BLOCK or BLOCK_COPY is executed), it is set to the + * origin of the active context. when the block context is shallow-copied + * for activation (when it is sent 'value'), it is set to the origin of + * the source block context. */ stix_oop_context_t origin; /* variable indexed part */ stix_oop_t slot[1]; /* stack */ }; - -#define STIX_PROCESS_NAMED_INSTVARS 5 +#define STIX_PROCESS_NAMED_INSTVARS 6 typedef struct stix_process_t stix_process_t; typedef struct stix_process_t* stix_oop_process_t; struct stix_process_t @@ -538,6 +546,9 @@ struct stix_process_t stix_oop_process_t prev; stix_oop_process_t next; + /* stack pointer. SmallInteger */ + stix_oop_t sp; + /* == variable indexed part == */ stix_oop_t slot[1]; /* process stack */ };