diff --git a/stix/lib/decode.c b/stix/lib/decode.c index 80cfedd..0fca4d9 100644 --- a/stix/lib/decode.c +++ b/stix/lib/decode.c @@ -29,10 +29,10 @@ #define DECODE_LOG_MASK (STIX_LOG_MNEMONIC) -#define LOG_INST_0(stix,fmt) STIX_LOG0(stix, DECODE_LOG_MASK, "\t" fmt "\n") -#define LOG_INST_1(stix,fmt,a1) STIX_LOG1(stix, DECODE_LOG_MASK, "\t" fmt "\n",a1) -#define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG2(stix, DECODE_LOG_MASK, "\t" fmt "\n", a1, a2) -#define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG3(stix, DECODE_LOG_MASK, "\t" fmt "\n", a1, a2, a3) +#define LOG_INST_0(stix,fmt) STIX_LOG1(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer) +#define LOG_INST_1(stix,fmt,a1) STIX_LOG2(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1) +#define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG3(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2) +#define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG4(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3) #define FETCH_BYTE_CODE(stix) (cdptr[ip++]) #define FETCH_BYTE_CODE_TO(stix,v_oow) (v_oow = FETCH_BYTE_CODE(stix)) @@ -51,6 +51,7 @@ int stix_decode (stix_t* stix, stix_oop_method_t mth, const stix_oocs_t* classfq { stix_oob_t bcode, * cdptr; stix_ooi_t ip = 0, cdlen; /* byte code length is limited by the compiler. so stix_ooi_t is good enough */ + stix_ooi_t fetched_instruction_pointer; stix_oow_t b1, b2; cdptr = STIX_METHOD_GET_CODE_BYTE(mth); @@ -64,6 +65,7 @@ int stix_decode (stix_t* stix, stix_oop_method_t mth, const stix_oocs_t* classfq /* TODO: check if ip increases beyond bcode when fetching parameters too */ while (ip < cdlen) { + fetched_instruction_pointer = ip; FETCH_BYTE_CODE_TO(stix, bcode); switch (bcode) @@ -503,7 +505,7 @@ return -1; /* print literal frame contents */ for (ip = 0; ip < STIX_OBJ_GET_SIZE(mth) - STIX_METHOD_NAMED_INSTVARS; ip++) { - LOG_INST_2 (stix, " @%-3zd %O", ip, mth->slot[ip]); + STIX_LOG2(stix, DECODE_LOG_MASK, " @%-5zd %O\n", ip, mth->slot[ip]); } return 0; diff --git a/stix/lib/exec.c b/stix/lib/exec.c index 812e52d..46e1fe8 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -116,11 +116,11 @@ #if defined(STIX_DEBUG_VM_EXEC) # define LOG_MASK_INST (STIX_LOG_IC | STIX_LOG_MNEMONIC) -# define LOG_INST_0(stix,fmt) STIX_LOG0(stix, LOG_MASK_INST, "\t" fmt "\n") -# define LOG_INST_1(stix,fmt,a1) STIX_LOG1(stix, LOG_MASK_INST, "\t" fmt "\n",a1) -# define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG2(stix, LOG_MASK_INST, "\t" fmt "\n", a1, a2) -# define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG3(stix, LOG_MASK_INST, "\t" fmt "\n", a1, a2, a3) - +/* TODO: for send_message, display the method name. or include the method name before 'ip' */ +# define LOG_INST_0(stix,fmt) STIX_LOG1(stix, LOG_MASK_INST, " %06zd " fmt "\n", fetched_instruction_pointer) +# define LOG_INST_1(stix,fmt,a1) STIX_LOG2(stix, LOG_MASK_INST, " %06zd " fmt "\n",fetched_instruction_pointer, a1) +# define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG3(stix, LOG_MASK_INST, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2) +# define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG4(stix, LOG_MASK_INST, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3) #else # define LOG_INST_0(stix,fmt) # define LOG_INST_1(stix,fmt,a1) @@ -128,7 +128,6 @@ # define LOG_INST_3(stix,fmt,a1,a2,a3) #endif - #define __PRIMITIVE_NAME__ (&__FUNCTION__[4]) /* ------------------------------------------------------------------------- */ @@ -2909,6 +2908,10 @@ static int start_method (stix_t* stix, stix_oop_method_t method, stix_oow_t narg { stix_ooi_t preamble, preamble_code; +#if defined(STIX_DEBUG_VM_EXEC) + stix_ooi_t fetched_instruction_pointer = 0; /* set it to a fake value */ +#endif + STIX_ASSERT (STIX_OOP_TO_SMOOI(method->tmpr_nargs) == nargs); preamble = STIX_OOP_TO_SMOOI(method->preamble); @@ -3151,6 +3154,10 @@ int stix_execute (stix_t* stix) stix_uintmax_t inst_counter = 0; #endif +#if defined(STIX_DEBUG_VM_EXEC) + stix_ooi_t fetched_instruction_pointer; +#endif + STIX_ASSERT (stix->active_context != STIX_NULL); vm_startup (stix); @@ -3222,12 +3229,12 @@ int stix_execute (stix_t* stix) STIX_ASSERT (stix->processor->tally = STIX_SMOOI_TO_OOP(0)); STIX_LOG0 (stix, STIX_LOG_IC | STIX_LOG_DEBUG, "No more runnable process\n"); -#if 0 -if (there is semaphore awaited.... ) -{ -/* DO SOMETHING */ -} -#endif + #if 0 + if (there is semaphore awaited.... ) + { + /* DO SOMETHING */ + } + #endif break; } @@ -3254,6 +3261,9 @@ if (there is semaphore awaited.... ) stix->proc_switched = 0; +#if defined(STIX_DEBUG_VM_EXEC) + fetched_instruction_pointer = stix->ip; +#endif FETCH_BYTE_CODE_TO (stix, bcode); /*while (bcode == BCODE_NOOP) FETCH_BYTE_CODE_TO (stix, bcode);*/ diff --git a/stix/lib/main.c b/stix/lib/main.c index d89eab5..26a59b5 100644 --- a/stix/lib/main.c +++ b/stix/lib/main.c @@ -372,6 +372,30 @@ static void* mod_getsym (stix_t* stix, void* handle, const stix_ooch_t* name) /* ========================================================================= */ +static int write_all (int fd, const char* ptr, stix_oow_t len) +{ + while (len > 0) + { + stix_ooi_t wr; + + wr = write (1, ptr, len); + + if (wr <= -1) + { + /*if (errno == EAGAIN || errno == EWOULDBLOCK) + { + push it to internal buffers? before writing data just converted, need to write buffered data first. + }*/ + return -1; + } + + ptr += wr; + len -= wr; + } + + return 0; +} + static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, stix_oow_t len) { #if defined(_WIN32) @@ -381,26 +405,24 @@ static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, st stix_bch_t buf[256]; stix_oow_t ucslen, bcslen, msgidx; int n; - - - + char ts[32]; + struct tm tm, *tmp; + time_t now; /*if (mask & STIX_LOG_GC) return;*/ /* don't show gc logs */ /* TODO: beautify the log message. * do classification based on mask. */ -/* -{ - char ts[32]; - struct tm tm; - time_t now; now = time(NULL); - localtime_r (&now, &tm); - strftime (ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", &tm); - write (1, ts, strlen(ts)); -} -*/ +#if defined(__MSDOS__) + tmp = localtime (&now); +#else + tmp = localtime_r (&now, &tm); +#endif + strftime (ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); + write_all (1, ts, strlen(ts)); + msgidx = 0; while (len > 0) { @@ -410,8 +432,6 @@ static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, st n = stix_ucstoutf8 (&msg[msgidx], &ucslen, buf, &bcslen); if (n == 0 || n == -2) { - stix_oow_t rem; - const stix_bch_t* ptr; /* n = 0: * converted all successfully * n == -2: @@ -421,25 +441,7 @@ static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, st STIX_ASSERT (ucslen > 0); /* if this fails, the buffer size must be increased */ /* attempt to write all converted characters */ - rem = bcslen; - ptr = buf; - while (rem > 0) - { - stix_ooi_t wr; - - wr = write (1, ptr, rem); /* TODO: write all */ - if (wr <= -1) - { - /*if (errno == EAGAIN || errno == EWOULDBLOCK) - { - push it to internal buffers? before writing data just converted, need to write buffered data first. - }*/ - break; - } - - ptr += wr; - rem -= wr; - } + if (write_all (1, buf, bcslen) <= -1) break; if (n == 0) break; else @@ -517,11 +519,9 @@ static char* syntax_error_msg[] = /* ========================================================================= */ -stix_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' }; -stix_ooch_t str_main[] = { 'm', 'a', 'i', 'n' }; - - -stix_t* g_stix = STIX_NULL; +static stix_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' }; +static stix_ooch_t str_main[] = { 'm', 'a', 'i', 'n' }; +static stix_t* g_stix = STIX_NULL; /* ========================================================================= */