From 6544340db4fd43d8132191578b25080a06f469b8 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Wed, 2 Dec 2015 16:14:37 +0000 Subject: [PATCH] fixed a bug of wrong short number conversion in stix_strtoint() when STIX_LIW_BITS == STIX_OOHW_BITS --- stix/lib/bigint.c | 97 +++++++++++++++++++-------------------------- stix/lib/comp.c | 11 ++++- stix/lib/exec.c | 20 +++++++++- stix/lib/stix-prv.h | 2 + 4 files changed, 71 insertions(+), 59 deletions(-) diff --git a/stix/lib/bigint.c b/stix/lib/bigint.c index 9948f54..99fb20e 100644 --- a/stix/lib/bigint.c +++ b/stix/lib/bigint.c @@ -27,10 +27,12 @@ #include "stix-prv.h" -#if defined(STIX_USE_FULL_WORD) +#if (STIX_LIW_BITS == STIX_OOW_BITS) /* nothing special */ -#else +#elif (STIX_LIW_BITS == STIX_OOHW_BITS) # define MAKE_WORD(hw1,hw2) ((stix_oow_t)(hw1) | (stix_oow_t)(hw2) << STIX_LIW_BITS) +#else +# error UNSUPPORTED LIW BIT SIZE #endif /*#define IS_POWER_OF_2(ui) (((ui) > 0) && (((ui) & (~(ui)+ 1)) == (ui)))*/ @@ -167,7 +169,7 @@ static STIX_INLINE int is_integer (stix_t* stix, stix_oop_t oop) static STIX_INLINE stix_oop_t make_bigint_with_ooi (stix_t* stix, stix_ooi_t i) { -#if defined(STIX_USE_FULL_WORD) +#if (STIX_LIW_BITS == STIX_OOW_BITS) stix_oow_t w; STIX_ASSERT (STIX_SIZEOF(stix_oow_t) == STIX_SIZEOF(stix_liw_t)); @@ -185,7 +187,7 @@ static STIX_INLINE stix_oop_t make_bigint_with_ooi (stix_t* stix, stix_ooi_t i) w = -i; return stix_instantiate (stix, stix->_large_negative_integer, &w, 1); } -#else +#elif (STIX_LIW_BITS == STIX_OOHW_BITS) stix_liw_t hw[2]; stix_oow_t w; @@ -204,6 +206,8 @@ static STIX_INLINE stix_oop_t make_bigint_with_ooi (stix_t* stix, stix_ooi_t i) hw[1] = w >> STIX_LIW_BITS; return stix_instantiate (stix, stix->_large_negative_integer, &hw, (hw[1] > 0? 2: 1)); } +#else +# error UNSUPPORTED LIW BIT SIZE #endif } @@ -317,7 +321,7 @@ static stix_oop_t normalize_bigint (stix_t* stix, stix_oop_t oop) STIX_ASSERT (STIX_OOP_IS_POINTER(oop)); count = count_effective_digits (oop); -#if defined(STIX_USE_FULL_WORD) +#if (STIX_LIW_BITS == STIX_OOW_BITS) if (count == 1) /* 1 word */ { stix_oow_t w; @@ -334,7 +338,8 @@ static stix_oop_t normalize_bigint (stix_t* stix, stix_oop_t oop) if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP(-(stix_ooi_t)w); } } -#else +#elif (STIX_LIW_BITS == STIX_OOHW_BITS) + if (count == 1) /* 1 half-word */ { if (STIX_OBJ_GET_CLASS(oop) == stix->_large_positive_integer) @@ -363,6 +368,8 @@ static stix_oop_t normalize_bigint (stix_t* stix, stix_oop_t oop) if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP(-(stix_ooi_t)w); } } +#else +# error UNSUPPORTED LIW BIT SIZE #endif if (STIX_OBJ_GET_SIZE(oop) == count) { @@ -1077,27 +1084,6 @@ stix_oop_t stix_mulints (stix_t* stix, stix_oop_t x, stix_oop_t y) STIX_OBJ_SET_CLASS(z, stix->_large_negative_integer); } -{ int i; -printf ("MUL=>"); -for (i = STIX_OBJ_GET_SIZE(z); i > 0;) -{ -printf ("%0*lX ", (int)(STIX_SIZEOF(stix_liw_t) * 2), (unsigned long)((stix_oop_liword_t)z)->slot[--i]); -} -printf ("\n"); -} - -/* -lshift_unsigned_array (((stix_oop_liword_t)z)->slot, STIX_OBJ_GET_SIZE(z), 16 * 5 + 4); -{ int i; -printf ("LSHIFT10=>"); -for (i = STIX_OBJ_GET_SIZE(z); i > 0;) -{ -printf ("%0*lX ", (int)(STIX_SIZEOF(stix_liw_t) * 2), (unsigned long)((stix_oop_liword_t)z)->slot[--i]); -} -printf ("\n"); -}*/ - - return normalize_bigint (stix, z); oops_einval: @@ -1117,6 +1103,7 @@ stix_oop_t stix_divints (stix_t* stix, stix_oop_t x, stix_oop_t y, int modulo, s xv = STIX_OOP_TO_SMOOI(x); yv = STIX_OOP_TO_SMOOI(y); +printf ("%d %d\n", (int)xv, (int)yv); if (yv == 0) { stix->errnum = STIX_EDIVBY0; @@ -1382,7 +1369,8 @@ static stix_uint8_t ooch_val_tab[] = stix_oop_t stix_strtoint (stix_t* stix, const stix_ooch_t* str, stix_oow_t len, int radix) { - int neg = 0; + //int neg = 0; + int sign = 1; const stix_ooch_t* ptr, * start, * end; stix_lidw_t w, v; stix_liw_t hw[16], * hwp = STIX_NULL; @@ -1391,7 +1379,8 @@ stix_oop_t stix_strtoint (stix_t* stix, const stix_ooch_t* str, stix_oow_t len, if (radix < 0) { - neg = 1; + /* when radix is less than 0, it treats it as if '-' is preceeding */ + sign = -1; radix = -radix; } @@ -1406,7 +1395,7 @@ stix_oop_t stix_strtoint (stix_t* stix, const stix_ooch_t* str, stix_oow_t len, else if (*ptr == '-') { ptr++; - neg = 1; + sign = -1; } } @@ -1579,38 +1568,31 @@ stix_oop_t stix_strtoint (stix_t* stix, const stix_ooch_t* str, stix_oow_t len, } STIX_ASSERT (hwlen >= 1); -#if defined(STIX_USE_FULL_WORD) + +#if (STIX_LIW_BITS == STIX_OOW_BITS) if (hwlen == 1) { w = hwp[0]; - if (neg) - { - STIX_ASSERT (-STIX_SMOOI_MAX == STIX_SMOOI_MIN); - if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP(-(stix_ooi_t)w); - } - else - { - if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP(w); - } + STIX_ASSERT (-STIX_SMOOI_MAX == STIX_SMOOI_MIN); + if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP((stix_ooi_t)w * sign); + } +#elif (STIX_LIW_BITS == STIX_OOHW_BITS) + if (hwlen == 1) + { + STIX_ASSERT (hwp[0] <= STIX_SMOOI_MAX); + return STIX_SMOOI_TO_OOP((stix_ooi_t)hwp[0] * sign); } -#else - if (hwlen == 1) return STIX_SMOOI_TO_OOP((stix_ooi_t)hwp[0] * -neg); else if (hwlen == 2) { w = MAKE_WORD(hwp[0], hwp[1]); - if (neg) - { - STIX_ASSERT (-STIX_SMOOI_MAX == STIX_SMOOI_MIN); - if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP(-(stix_ooi_t)w); - } - else - { - if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP(w); - } + STIX_ASSERT (-STIX_SMOOI_MAX == STIX_SMOOI_MIN); + if (w <= STIX_SMOOI_MAX) return STIX_SMOOI_TO_OOP((stix_ooi_t)w * sign); } +#else +# error UNSUPPORTED LIW BIT SIZE #endif - res = stix_instantiate (stix, (neg? stix->_large_negative_integer: stix->_large_positive_integer), hwp, hwlen); + res = stix_instantiate (stix, (sign < 0? stix->_large_negative_integer: stix->_large_positive_integer), hwp, hwlen); if (hwp && hw != hwp) stix_freemem (stix, hwp); return res; @@ -1677,13 +1659,13 @@ stix_oop_t stix_inttostr (stix_t* stix, stix_oop_t num, int radix) } else { - #if STIX_LIW_BITS == STIX_OOW_BITS + #if (STIX_LIW_BITS == STIX_OOW_BITS) if (STIX_OBJ_GET_SIZE(num) == 1) { w = ((stix_oop_word_t)num)->slot[0]; v = (STIX_OBJ_GET_CLASS(num) == stix->_large_negative_integer)? -1: 1; } - #elif STIX_LIW_BITS == STIX_OOHW_BITS + #elif (STIX_LIW_BITS == STIX_OOHW_BITS) if (STIX_OBJ_GET_SIZE(num) == 1) { w = ((stix_oop_halfword_t)num)->slot[0]; @@ -1692,8 +1674,10 @@ stix_oop_t stix_inttostr (stix_t* stix, stix_oop_t num, int radix) else if (STIX_OBJ_GET_SIZE(num) == 2) { w = MAKE_WORD (((stix_oop_halfword_t)num)->slot[0], ((stix_oop_halfword_t)num)->slot[1]); - v = (STIX_OBJ_GET_CLASS(oop) == stix->_large_negative_integer)? -1: 1; + v = (STIX_OBJ_GET_CLASS(num) == stix->_large_negative_integer)? -1: 1; } + #else + # error UNSUPPORTED LIW BIT SIZE #endif } @@ -1714,7 +1698,7 @@ stix_oop_t stix_inttostr (stix_t* stix, stix_oop_t num, int radix) return stix_makestring (stix, buf, len); } - +#if 0 /* Do it in a hard way */ do { @@ -1728,6 +1712,7 @@ stix_oop_t stix_inttostr (stix_t* stix, stix_oop_t num, int radix) } while (1); +#endif oops_einval: stix->errnum = STIX_EINVAL; diff --git a/stix/lib/comp.c b/stix/lib/comp.c index 61d5b48..e3fbd5d 100644 --- a/stix/lib/comp.c +++ b/stix/lib/comp.c @@ -1778,29 +1778,37 @@ static int emit_push_smint_literal (stix_t* stix, stix_ooi_t i) switch (i) { case -1: +printf ("\tpush negone\n"); return emit_byte_instruction (stix, BCODE_PUSH_NEGONE); case 0: +printf ("\tpush zero\n"); return emit_byte_instruction (stix, BCODE_PUSH_ZERO); case 1: +printf ("\tpush one\n"); return emit_byte_instruction (stix, BCODE_PUSH_ONE); case 2: +printf ("\tpush two\n"); return emit_byte_instruction (stix, BCODE_PUSH_TWO); } if (i >= 0 && i <= MAX_CODE_PARAM) { +printf ("\tpush intlit %d\n", (int)i); return emit_single_param_instruction(stix, BCODE_PUSH_INTLIT, i); } else if (i < 0 && i >= -(stix_ooi_t)MAX_CODE_PARAM) { +printf ("\tpush negintlit %d\n", (int)i); return emit_single_param_instruction(stix, BCODE_PUSH_NEGINTLIT, -i); } + if (add_literal(stix, STIX_SMOOI_TO_OOP(i), &index) <= -1 || emit_single_param_instruction(stix, BCODE_PUSH_LITERAL_0, index) <= -1) return -1; +printf ("\tpush litral_0 %d\n", (int)index); return 0; } @@ -3517,18 +3525,19 @@ printf ("\tpush symbol literal %d\n", (int)index); /* TODO: floating pointer number */ /* TODO: other types of numbers, etc */ stix_oop_t tmp; + tmp = string_to_num (stix, &stix->c->tok.name, stix->c->tok.type == STIX_IOTOK_RADNUMLIT); if (!tmp) return -1; if (STIX_OOP_IS_SMOOI(tmp)) { -printf ("\tpush int literal\n"); if (emit_push_smint_literal(stix, STIX_OOP_TO_SMOOI(tmp)) <= -1) return -1; } else { if (add_literal(stix, tmp, &index) <= -1 || emit_single_param_instruction(stix, BCODE_PUSH_LITERAL_0, index) <= -1) return -1; +printf ("\tpush_literal_0 %d\n", (int)index); } GET_TOKEN (stix); diff --git a/stix/lib/exec.c b/stix/lib/exec.c index 4f486e4..1560a45 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -122,14 +122,18 @@ static void switch_process (stix_t* stix, stix_oop_process_t proc) { if (stix->processor->active != proc) { +#if defined(STIX_DEBUG_PROCESSOR) printf ("ACTUAL PROCESS SWITCHING BF...%d %p\n", (int)stix->ip, stix->active_context); +#endif /* store the active context to the active process */ STIX_ASSERT ((stix_oop_t)stix->processor->active != stix->_nil); stix->processor->active->active_context = stix->active_context; SWITCH_ACTIVE_CONTEXT (stix, proc->active_context); +#if defined(STIX_DEBUG_PROCESSOR) printf ("ACTUAL PROCESS SWITCHING AF...%d %p\n", (int)stix->ip, stix->active_context); +#endif /*TODO: set the state to RUNNING */ stix->processor->active = proc; } @@ -140,12 +144,16 @@ static void switch_to_next_process (stix_t* stix) /* TODO: this is experimental. rewrite it */ if ((stix_oop_t)stix->processor->active->next == stix->_nil) { +#if defined(STIX_DEBUG_PROCESSOR) printf ("SWITCHING TO THE HEAD PROCESS\n"); +#endif switch_process (stix, stix->processor->head); } else { +#if defined(STIX_DEBUG_PROCESSOR) printf ("SWITCHING TO THE NEXT PROCESS\n"); +#endif switch_process (stix, stix->processor->active->next); } } @@ -169,22 +177,27 @@ static STIX_INLINE int register_new_process (stix_t* stix, stix_oop_process_t pr stix->processor->head = proc; stix->processor->tail = proc; stix->processor->tally = STIX_SMOOI_TO_OOP(1); +#if defined(STIX_DEBUG_PROCESSOR) printf ("ADD NEW PROCESS X - %d\n", (int)1); +#endif } else if (tally >= STIX_SMOOI_MAX) { +#if defined(STIX_DEBUG_PROCESSOR) printf ("TOO MANY PROCESS\n"); +#endif stix->errnum = STIX_EPFULL; return -1; } else { - /* TODO: over flow check or maximum number of process check using the tally field? */ proc->next = stix->processor->head; stix->processor->head->prev = proc; stix->processor->head = proc; stix->processor->tally = STIX_SMOOI_TO_OOP(tally + 1); +#if defined(STIX_DEBUG_PROCESSOR) printf ("ADD NEW PROCESS Y - %d\n", (int)tally + 1); +#endif } proc->state = STIX_SMOOI_TO_OOP(1); /* TODO: change the code properly... changing state alone doesn't help */ @@ -1115,6 +1128,7 @@ static int prim_integer_quo (stix_t* stix, stix_ooi_t nargs) quo = stix_divints (stix, rcv, arg, 0, STIX_NULL); if (!quo) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */ +/* TODO: STIX_EDIVBY0 soft or hard failure? */ ACTIVE_STACK_POP (stix); ACTIVE_STACK_SETTOP (stix, quo); @@ -1132,6 +1146,7 @@ static int prim_integer_rem (stix_t* stix, stix_ooi_t nargs) quo = stix_divints (stix, rcv, arg, 0, &rem); if (!quo) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */ +/* TODO: STIX_EDIVBY0 soft or hard failure? */ ACTIVE_STACK_POP (stix); ACTIVE_STACK_SETTOP (stix, rem); @@ -1149,6 +1164,7 @@ static int prim_integer_quo2 (stix_t* stix, stix_ooi_t nargs) quo = stix_divints (stix, rcv, arg, 1, STIX_NULL); if (!quo) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */ +/* TODO: STIX_EDIVBY0 soft or hard failure? */ ACTIVE_STACK_POP (stix); ACTIVE_STACK_SETTOP (stix, quo); @@ -1166,6 +1182,7 @@ static int prim_integer_rem2 (stix_t* stix, stix_ooi_t nargs) quo = stix_divints (stix, rcv, arg, 1, &rem); if (!quo) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */ +/* TODO: STIX_EDIVBY0 soft or hard failure? */ ACTIVE_STACK_POP (stix); ACTIVE_STACK_SETTOP (stix, rem); @@ -2397,7 +2414,6 @@ fflush (stdout); newrcv = ACTIVE_STACK_GET(stix, stix->sp - b1); #if defined(STIX_DEBUG_EXEC) -printf ("NEWRCV => %p\n", newrcv); printf (" RECEIVER = "); print_object(stix, newrcv); printf ("\n"); diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index c65dc77..b7dccb1 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -50,10 +50,12 @@ #define STIX_USE_OBJECT_TRAILER /* this is for gc debugging */ +/*#define STIX_DEBUG_PROCESSOR*/ #define STIX_DEBUG_GC_001 /*#define STIX_DEBUG_EXEC*/ #define STIX_PROFILE_EXEC + #include /* TODO: delete these header inclusion lines */ #include #include