From e87dbbdfcd1e8aebcd7c5e46ad7d63ae9291d840 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 21 Jun 2015 14:45:45 +0000 Subject: [PATCH] fixed a bug of not initializing a variable buffer length in compile_class_definition() --- stix/lib/comp.c | 80 ++++++++++++++++++++++++--------------------- stix/lib/exec.c | 50 ++++++++++++++-------------- stix/lib/obj.c | 4 +-- stix/lib/stix-prv.h | 3 +- stix/lib/stix.h | 17 ++++------ 5 files changed, 80 insertions(+), 74 deletions(-) diff --git a/stix/lib/comp.c b/stix/lib/comp.c index 51c4944..6b26039 100644 --- a/stix/lib/comp.c +++ b/stix/lib/comp.c @@ -2121,14 +2121,16 @@ static int compile_block_expression (stix_t* stix) return -1; } -printf ("push_context nargs %d ntmprs %d\n", (int)block_arg_count, (int)block_tmpr_count); -printf ("send_block_copy\n"); +printf ("\tpush_context nargs %d ntmprs %d\n", (int)block_arg_count, (int)block_tmpr_count); +printf ("\tpush smint %d\n", (int)block_arg_count); +printf ("\tpush smint %d\n", (int)block_tmpr_count); +printf ("\tsend_block_copy\n"); if (emit_byte_instruction(stix, CODE_PUSH_CONTEXT) <= -1 || emit_push_smint_literal(stix, block_arg_count) <= -1 || emit_push_smint_literal(stix, block_tmpr_count) <= -1 || emit_byte_instruction(stix, CODE_SEND_BLOCK_COPY) <= -1) return -1; -printf ("jump\n"); +printf ("\tjump\n"); /* insert dummy instructions before replacing them with a jump instruction */ jump_inst_pos = stix->c->mth.code.len; if (emit_byte_instruction(stix, MAKE_CODE(CMD_EXTEND_DOUBLE, CMD_JUMP)) <= -1 || @@ -2227,24 +2229,25 @@ static int compile_expression_primary (stix_t* stix, const stix_ucs_t* ident, co case VAR_ARGUMENT: case VAR_TEMPORARY: if (emit_positional_instruction(stix, CMD_PUSH_TEMPVAR, var.pos) <= -1) return -1; -printf ("push tempvar %d\n", (int)var.pos); +printf ("\tpush tempvar %d\n", (int)var.pos); break; case VAR_INSTANCE: case VAR_CLASSINST: if (emit_positional_instruction(stix, CMD_PUSH_INSTVAR, var.pos) <= -1) return -1; -printf ("push instvar %d\n", (int)var.pos); +printf ("\tpush instvar %d\n", (int)var.pos); break; case VAR_CLASS: if (add_literal(stix, (stix_oop_t)var.cls, &index) <= -1 || emit_double_positional_instruction(stix, CMD_PUSH_OBJVAR, var.pos, index) <= -1) return -1; -printf ("PUSH OBJVAR %d %d\n", (int)var.pos, (int)index); +printf ("\tpush objvar %d %d\n", (int)var.pos, (int)index); break; case VAR_GLOBAL: - /* TODO: .............................. */ -stix->errnum = STIX_ENOIMPL; +/* TODO: .............................. */ +printf ("GLOBAL NOT IMPLMENTED.... \n"); + stix->errnum = STIX_ENOIMPL; return -1; default: @@ -2265,38 +2268,38 @@ stix->errnum = STIX_ENOIMPL; goto handle_ident; case STIX_IOTOK_SELF: -printf ("push receiver...\n"); +printf ("\tpush receiver...\n"); if (emit_byte_instruction(stix, CODE_PUSH_RECEIVER) <= -1) return -1; GET_TOKEN (stix); break; case STIX_IOTOK_SUPER: -printf ("push receiver(super)...\n"); +printf ("\tpush receiver(super)...\n"); if (emit_byte_instruction(stix, CODE_PUSH_RECEIVER) <= -1) return -1; GET_TOKEN (stix); *to_super = 1; break; case STIX_IOTOK_NIL: -printf ("push nil...\n"); +printf ("\tpush nil...\n"); if (emit_byte_instruction(stix, CODE_PUSH_NIL) <= -1) return -1; GET_TOKEN (stix); break; case STIX_IOTOK_TRUE: -printf ("push true...\n"); +printf ("\tpush true...\n"); if (emit_byte_instruction(stix, CODE_PUSH_TRUE) <= -1) return -1; GET_TOKEN (stix); break; case STIX_IOTOK_FALSE: -printf ("push false...\n"); +printf ("\tpush false...\n"); if (emit_byte_instruction(stix, CODE_PUSH_FALSE) <= -1) return -1; GET_TOKEN (stix); break; case STIX_IOTOK_THIS_CONTEXT: -printf ("push context...\n"); +printf ("\tpush context...\n"); if (emit_byte_instruction(stix, CODE_PUSH_CONTEXT) <= -1) return -1; GET_TOKEN (stix); break; @@ -2305,21 +2308,21 @@ printf ("push context...\n"); STIX_ASSERT (stix->c->tok.name.len == 1); if (add_character_literal(stix, stix->c->tok.name.ptr[0], &index) <= -1 || emit_positional_instruction(stix, CMD_PUSH_LITERAL, index) <= -1) return -1; -printf ("push character literal %d\n", (int)index); +printf ("\tpush character literal %d\n", (int)index); GET_TOKEN (stix); break; case STIX_IOTOK_STRLIT: if (add_string_literal(stix, &stix->c->tok.name, &index) <= -1 || emit_positional_instruction(stix, CMD_PUSH_LITERAL, index) <= -1) return -1; -printf ("push string literal %d\n", (int)index); +printf ("\tpush string literal %d\n", (int)index); GET_TOKEN (stix); break; case STIX_IOTOK_SYMLIT: if (add_symbol_literal(stix, &stix->c->tok.name, &index) <= -1 || emit_positional_instruction(stix, CMD_PUSH_LITERAL, index) <= -1) return -1; -printf ("push symbol literal %d\n", (int)index); +printf ("\tpush symbol literal %d\n", (int)index); GET_TOKEN (stix); break; @@ -2338,7 +2341,7 @@ printf ("NOT IMPLEMENTED LARGE_INTEGER or ERROR?\n"); } else { -printf ("push int literal\n"); +printf ("\tpush int literal\n"); if (emit_push_smint_literal(stix, tmp) <= -1) return -1; } @@ -2393,7 +2396,7 @@ static int compile_unary_message (stix_t* stix, int to_super) { if (add_symbol_literal(stix, &stix->c->tok.name, &index) <= -1 || emit_double_positional_instruction(stix, send_message_cmd[to_super], 0, index) <= -1) return -1; -printf ("send message %d with 0 arguments to %s\n", (int)index, (to_super? "super": "self")); +printf ("\tsend message %d with 0 arguments to %s\n", (int)index, (to_super? "super": "self")); GET_TOKEN (stix); } @@ -2478,7 +2481,7 @@ static int compile_keyword_message (stix_t* stix, int to_super) if (add_symbol_literal(stix, &kwsel, &index) <= -1 || emit_double_positional_instruction(stix, send_message_cmd[to_super], nargs, index) <= -1) goto oops; -printf ("Send message %d [", (int)index); +printf ("\tSend message %d [", (int)index); print_ucs (&kwsel); printf ("] with %d arguments to %s\n", (int)nargs, (to_super? "super": "self")); stix->c->mth.kwsels.len = kwsel_len; @@ -2512,12 +2515,12 @@ static int compile_message_expression (stix_t* stix, int to_super) while (stix->c->tok.type == STIX_IOTOK_SEMICOLON) { /* handle message cascading */ -printf ("DoSpecial(DUP_RECEIVER(CASCADE)) ....\n"); +printf ("TODO: DoSpecial(DUP_RECEIVER(CASCADE)) ....\n"); /*T ODO: emit code */ GET_TOKEN (stix); if (compile_keyword_message(stix, 0) <= -1) return -1; -printf ("DoSpecial(POP_TOP) ....\n"); +printf ("\tTODO: DoSpecial(POP_TOP) ....\n"); /*T ODO: emit code */ } @@ -2589,7 +2592,7 @@ printf ("\n"); goto oops; case VAR_TEMPORARY: -printf (" store to tempvar %d\n", (int)var.pos); +printf ("\tstore_into_tempvar %d\n", (int)var.pos); /* TODO: if pop is 1, emit CMD_POP_INTO_TEMPVAR. ret = pop; */ @@ -2598,7 +2601,7 @@ ret = pop; case VAR_INSTANCE: case VAR_CLASSINST: -printf (" store to instvar %d\n", (int)var.pos); +printf ("\tstore_into_instvar %d\n", (int)var.pos); /* TODO: if pop is 1, emit CMD_POP_INTO_INSTVAR ret = pop; */ @@ -2609,10 +2612,12 @@ ret = pop; /* TODO is this correct? */ if (add_literal (stix, (stix_oop_t)var.cls, &index) <= -1 || emit_double_positional_instruction (stix, CMD_STORE_INTO_OBJVAR, var.pos, index) <= -1) goto oops; +printf ("\tstore_into_objvar %d %d\n", (int)var.pos, (int)index); break; case VAR_GLOBAL: /* TODO: .............................. */ +printf ("\tSTORE_INTO_GLOBL NOT IMPLEMENTED YET\n"); goto oops; default: @@ -2649,7 +2654,7 @@ static int compile_block_statement (stix_t* stix) /* handle the return statement */ GET_TOKEN (stix); if (compile_method_expression(stix, 0) <= -1) return -1; -printf ("return_stacktop\n"); +printf ("\treturn_stacktop\n"); return emit_byte_instruction (stix, CODE_RETURN_STACKTOP); } else @@ -2670,7 +2675,7 @@ static int compile_method_statement (stix_t* stix) /* handle the return statement */ GET_TOKEN (stix); if (compile_method_expression(stix, 0) <= -1) return -1; -printf ("return_stacktop\n"); +printf ("\treturn_stacktop\n"); return emit_byte_instruction (stix, CODE_RETURN_STACKTOP); } else @@ -2687,7 +2692,7 @@ printf ("return_stacktop\n"); if (n <= -1) return -1; /* if n is 1, no stack popping is required */ -if (n == 0) printf ("pop_stacktop\n"); +if (n == 0) printf ("\tpop_stacktop\n"); return (n == 0)? emit_byte_instruction (stix, CODE_POP_STACKTOP): 0; } } @@ -2729,7 +2734,7 @@ static int compile_method_statements (stix_t* stix) /* arrange to return the receiver if execution reached * the end of the method without explicit return */ -printf ("return_receiver\n"); +printf ("\treturn_receiver\n"); return emit_byte_instruction (stix, CODE_RETURN_RECEIVER); } @@ -2885,6 +2890,9 @@ static int compile_method_definition (stix_t* stix) if (compile_method_name(stix) <= -1) return -1; +printf (">>METHOD "); +print_ucs (&stix->c->mth.name); +printf ("\n"); if (stix->c->tok.type != STIX_IOTOK_LBRACE) { /* { expected */ @@ -3094,15 +3102,9 @@ static int __compile_class_definition (stix_t* stix) { int super_is_nil = 0; -printf ("DEFININING..\n"); -{ -int i; -for (i = 0; i < stix->c->cls.name.len; i++) -{ -printf ("%c", stix->c->cls.name.ptr[i]); -} +printf ("DEFININING.."); +print_ucs (&stix->c->cls.name); printf ("\n"); -} /* superclass is specified. new class defintion. * for example, #class Class(Stix) @@ -3284,7 +3286,6 @@ printf ("\n"); return -1; } - if (!(stix->c->cls.flags & CLASS_EXTENDED)) { /* TODO: anything else to set? */ @@ -3309,8 +3310,12 @@ static int compile_class_definition (stix_t* stix) STIX_MEMSET (&stix->c->cls.name_loc, 0, STIX_SIZEOF(stix->c->cls.name_loc)); STIX_MEMSET (&stix->c->cls.supername_loc, 0, STIX_SIZEOF(stix->c->cls.supername_loc)); + STIX_ASSERT (STIX_COUNTOF(stix->c->cls.var_count) == STIX_COUNTOF(stix->c->cls.vars)); for (i = 0; i < STIX_COUNTOF(stix->c->cls.var_count); i++) + { stix->c->cls.var_count[i] = 0; + stix->c->cls.vars[i].len = 0; + } stix->c->cls.self_oop = STIX_NULL; stix->c->cls.super_oop = STIX_NULL; @@ -3318,6 +3323,7 @@ static int compile_class_definition (stix_t* stix) stix->c->cls.mthdic_oop[MTH_CLASS] = STIX_NULL; stix->c->mth.literal_count = 0; + /* do main compilation work */ n = __compile_class_definition (stix); diff --git a/stix/lib/exec.c b/stix/lib/exec.c index 7ea2672..d9d18cb 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -137,6 +137,8 @@ static int activate_new_method (stix_t* stix, stix_oop_method_t mth) */ ctx->sp = STIX_OOP_FROM_SMINT(ntmprs - 1); ctx->method = mth; + /*ctx->home = stix->_nil;*/ + ctx->origin = ctx; /* point to self */ /* * Assume this message sending expression: @@ -588,7 +590,6 @@ static int primitive_integer_add (stix_t* stix, stix_ooi_t nargs) return 0; } - typedef int (*primitive_handler_t) (stix_t* stix, stix_ooi_t nargs); struct primitive_t @@ -612,8 +613,8 @@ static primitive_t primitives[] = int stix_execute (stix_t* stix) { - stix_oop_method_t mth; - stix_oop_byte_t code; + volatile stix_oop_method_t mth; + volatile stix_oop_byte_t code; stix_byte_t bc, cmd; stix_ooi_t b1; @@ -622,11 +623,7 @@ int stix_execute (stix_t* stix) while (1) { -/* TODO: improve how to access method??? */ - if (stix->active_context->home == stix->_nil) - mth = stix->active_context->method; - else - mth = ((stix_oop_block_context_t)stix->active_context)->origin->method; + mth = stix->active_context->origin->method; code = mth->code; printf ("IP => %d ", (int)stix->ip); @@ -664,7 +661,7 @@ printf ("CMD => %d, B1 = %d, SP = %d, IP AFTER INC %d\n", (int)cmd, (int)b1, (in case CMD_PUSH_INSTVAR: printf ("PUSH_INSTVAR %d\n", (int)b1); STIX_ASSERT (STIX_OBJ_GET_FLAGS_TYPE(stix->active_context->receiver) == STIX_OBJ_TYPE_OOP); - STACK_PUSH (stix, ((stix_oop_oop_t)stix->active_context->receiver)->slot[b1]); + STACK_PUSH (stix, ((stix_oop_oop_t)stix->active_context->origin->receiver)->slot[b1]); break; case CMD_PUSH_TEMPVAR: @@ -678,15 +675,15 @@ printf ("\n"); case CMD_PUSH_LITERAL: printf ("PUSH_LITERAL idx=%d - ", (int)b1); -print_object (stix, mth->slot[b1]); +print_object (stix, stix->active_context->origin->method->slot[b1]); printf ("\n"); - STACK_PUSH (stix, mth->slot[b1]); + STACK_PUSH (stix, stix->active_context->origin->method->slot[b1]); break; case CMD_STORE_INTO_INSTVAR: printf ("STORE_INSTVAR %d\n", (int)b1); STIX_ASSERT (STIX_OBJ_GET_FLAGS_TYPE(stix->active_context->receiver) == STIX_OBJ_TYPE_OOP); - ((stix_oop_oop_t)stix->active_context->receiver)->slot[b1] = STACK_GETTOP(stix); + ((stix_oop_oop_t)stix->active_context->origin->receiver)->slot[b1] = STACK_GETTOP(stix); break; case CMD_STORE_INTO_TEMPVAR: @@ -713,12 +710,17 @@ printf ("JUMP %d\n", (int)b1); obj_index = code->slot[stix->ip++]; if (cmd == CMD_EXTEND_DOUBLE) + { obj_index = (obj_index << 8) | code->slot[stix->ip++]; + } + + obj = (stix_oop_oop_t)stix->active_context->origin->method->slot[obj_index]; +printf ("PUSH OBJVAR %d %d - ", (int)b1, (int)obj_index); - obj = (stix_oop_oop_t)mth->slot[obj_index]; -printf ("PUSH OBJVAR %d %d\n", (int)b1, (int)obj_index); STIX_ASSERT (STIX_OBJ_GET_FLAGS_TYPE(obj) == STIX_OBJ_TYPE_OOP); STIX_ASSERT (obj_index < STIX_OBJ_GET_SIZE(obj)); +print_object (stix, obj->slot[b1]); +printf ("\n"); STACK_PUSH (stix, obj->slot[b1]); break; } @@ -732,7 +734,7 @@ printf ("PUSH OBJVAR %d %d\n", (int)b1, (int)obj_index); obj_index = (obj_index << 8) | code->slot[stix->ip++]; printf ("STORE OBJVAR %d %d\n", (int)b1, (int)obj_index); - obj = (stix_oop_oop_t)mth->slot[obj_index]; + obj = (stix_oop_oop_t)stix->active_context->origin->method->slot[obj_index]; STIX_ASSERT (STIX_OBJ_GET_FLAGS_TYPE(obj) == STIX_OBJ_TYPE_OOP); STIX_ASSERT (obj_index < STIX_OBJ_GET_SIZE(obj)); obj->slot[b1] = STACK_GETTOP(stix); @@ -760,7 +762,7 @@ printf ("STORE OBJVAR %d %d\n", (int)b1, (int)obj_index); selector_index = (selector_index << 8) | code->slot[stix->ip++]; /* get the selector from the literal frame */ - selector = (stix_oop_char_t)mth->slot[selector_index]; + selector = (stix_oop_char_t)stix->active_context->origin->method->slot[selector_index]; if (cmd == CMD_SEND_MESSAGE) printf ("SEND_MESSAGE TO RECEIVER AT %d NARGS=%d\n", (int)(stix->sp - b1), (int)b1); @@ -841,8 +843,8 @@ printf ("RETURN INSTVAR AT PREAMBLE\n"); switch (b1) { case SUBCMD_PUSH_RECEIVER: -printf ("PUSH_RECEIVER %p TO STACK INDEX %d\n", stix->active_context->receiver, (int)stix->sp); - STACK_PUSH (stix, stix->active_context->receiver); +printf ("PUSH_RECEIVER %p TO STACK INDEX %d\n", stix->active_context->origin->receiver, (int)stix->sp); + STACK_PUSH (stix, stix->active_context->origin->receiver); break; case SUBCMD_PUSH_NIL: @@ -904,7 +906,7 @@ printf ("RETURN_STACKTOP\n"); case SUBCMD_RETURN_RECEIVER: printf ("RETURN_RECEIVER\n"); - return_value = stix->active_context->receiver; + return_value = stix->active_context->origin->receiver; goto handle_return; case SUBCMD_RETURN_FROM_BLOCK: @@ -921,7 +923,6 @@ printf ("RETURN_RECEIVER\n"); break; } - case SUBCMD_SEND_BLOCK_COPY: { printf ("SEND_BLOCK_COPY\n"); @@ -946,6 +947,8 @@ printf ("SEND_BLOCK_COPY\n"); blkctx = (stix_oop_block_context_t)stix_instantiate (stix, stix->_block_context, STIX_NULL, 255); /* TODO: proper stack size */ if (!blkctx) return -1; + /* get the receiver to the block copy message after block context instantiation + * not to get affected by potential GC */ rctx = STACK_GETTOP(stix); /* blkctx->caller is left to nil */ @@ -953,7 +956,7 @@ printf ("SEND_BLOCK_COPY\n"); blkctx->sp = STIX_OOP_FROM_SMINT(0); blkctx->nargs = STIX_OOP_FROM_SMINT(nargs); blkctx->ntmprs = STIX_OOP_FROM_SMINT(ntmprs); - blkctx->iip = STIX_OOP_FROM_SMINT(stix->ip + 3); + blkctx->iip = STIX_OOP_FROM_SMINT(stix->ip + 3); /* TOOD: change +3 to the configured JUMP SIZE */ blkctx->home = rctx; if (((stix_oop_context_t)rctx)->home == stix->_nil) @@ -987,7 +990,7 @@ printf ("SEND_BLOCK_COPY\n"); STACK_PUSH (stix, return_value); printf ("<>\n"); - if (stix->active_context->sender == stix->_nil) + if (stix->active_context->sender == stix->_nil) { /* the sending context of the intial context has been set to nil. * use this fact to tell an initial context from a normal context. */ @@ -1005,12 +1008,11 @@ printf ("<<>>\n"); } done: - stix->active_context_sp = STIX_NULL; return 0; oops: - stix->active_context_sp = STIX_NULL; + /* TODO: anything to do here? */ return -1; } diff --git a/stix/lib/obj.c b/stix/lib/obj.c index b8da7d4..f90fa36 100644 --- a/stix/lib/obj.c +++ b/stix/lib/obj.c @@ -30,8 +30,8 @@ void* stix_allocbytes (stix_t* stix, stix_size_t size) { stix_uint8_t* ptr; -#if defined(STIX_DEBUG_GC_1) - stix_gc (stix); +#if defined(STIX_DEBUG_GC_001) + if (!(stix->option.trait & STIX_NOGC)) stix_gc (stix); #endif ptr = stix_allocheapmem (stix, stix->curheap, size); diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index de75eaa..4687f88 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -32,8 +32,9 @@ /* this is useful for debugging. stix_gc() can be called * while stix has not been fully initialized when this is defined*/ #define STIX_SUPPORT_GC_DURING_IGNITION -#define STIX_DEBUG_GC_1 +/* this is for gc debugging */ +#define STIX_DEBUG_GC_001 #include /* TODO: delete these header inclusion lines */ #include diff --git a/stix/lib/stix.h b/stix/lib/stix.h index 132cac5..cbbdba0 100644 --- a/stix/lib/stix.h +++ b/stix/lib/stix.h @@ -603,18 +603,18 @@ struct stix_context_t { STIX_OBJ_HEADER; - stix_oop_t sender; /* message sending context - active context before new context activation*/ - stix_oop_t ip; /* instruction pointer */ - stix_oop_t sp; /* stack pointer */ + stix_oop_t sender; /* message sending context - active context before new context activation*/ + stix_oop_t ip; /* instruction pointer */ + stix_oop_t sp; /* stack pointer */ - stix_oop_method_t method; /* CompiledMethod */ + stix_oop_method_t method; /* CompiledMethod */ stix_oop_t unused; stix_oop_t receiver; /* receiver of the message. For a statement '#xxx do: #yyyy', #xxx is the receiver.*/ - stix_oop_t home; /* nil */ - stix_oop_t origin; /* nil */ + stix_oop_t home; /* nil */ + stix_oop_context_t origin; /* nil */ /* variable indexed part */ - stix_oop_t slot[1]; /* stack contents */ + stix_oop_t slot[1]; /* stack */ }; #define STIX_BLOCK_CONTEXT_NAMED_INSTVARS 8 @@ -741,9 +741,6 @@ struct stix_t stix_oop_context_t active_context; /* TODO: this could be either MethodContext or BlockContext. Some redefintion of stix_oop_context_t might be needed after having removed stix-oop_block-context. */ stix_ooi_t sp; stix_ooi_t ip; - - stix_ooi_t* active_context_sp; - /* stix_oop_context_t home_context; */ /* == END EXECUTION REGISTERS == */ #if defined(STIX_INCLUDE_COMPILER)