From 8e5e5234cbee5a8fba49c3129f14fc3718d08bca Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 12 Jul 2019 03:38:45 +0000 Subject: [PATCH] simplified source line and source text representation of a methond in dbgi --- moo/kernel/Context.moo | 8 +-- moo/kernel/Except.moo | 1 - moo/lib/comp.c | 27 ++------ moo/lib/debug.c | 58 ++++++---------- moo/lib/exec.c | 147 ++++++++++++++++++++++------------------- moo/lib/moo-prv.h | 3 +- moo/lib/moo.h | 29 ++------ 7 files changed, 115 insertions(+), 158 deletions(-) diff --git a/moo/kernel/Context.moo b/moo/kernel/Context.moo index e05afdb..810437c 100644 --- a/moo/kernel/Context.moo +++ b/moo/kernel/Context.moo @@ -322,9 +322,7 @@ class(#pointer) CompiledMethod(Object) ntmprs, nargs, //code, <-- only if moo is built with MOO_USE_METHOD_TRAILER disable. - dbi_text_offset, dbi_file_offset, - source_line, dbi_method_offset. method preamble @@ -350,10 +348,6 @@ class(#pointer) CompiledMethod(Object) method(#primitive) sourceText. method(#primitive) sourceFile. + method(#primitive) sourceLine. method(#primitive) ipSourceLine: ip. - - method sourceLine - { - ^self.source_line - } } diff --git a/moo/kernel/Except.moo b/moo/kernel/Except.moo index 1084458..a20fa52 100644 --- a/moo/kernel/Except.moo +++ b/moo/kernel/Except.moo @@ -79,7 +79,6 @@ while (ctx notNil) System logNl: (" " & ctx method owner name & ">>" & ctx method name & " (" & ctx method sourceFile & " " & (ctx method ipSourceLine: (ctx pc)) asString & ")"). // TODO: get location of the current pc and include it... (ctx method sourceLine: ctx pc) asString dump. - }. // TODO: include blockcontext??? ctx := ctx sender. diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 7158ab8..1911fc3 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -2518,10 +2518,10 @@ static MOO_INLINE int emit_byte_instruction (moo_t* moo, moo_oob_t code, const m cc->mth.code.ptr[cc->mth.code.len] = code; if (srcloc) { - /* store the distance from the method body start */ if (srcloc->file == cc->mth.code_start_loc.file && srcloc->line >= cc->mth.code_start_loc.line) { - cc->mth.code.locptr[cc->mth.code.len] = srcloc->line - cc->mth.code_start_loc.line; + /*cc->mth.code.locptr[cc->mth.code.len] = srcloc->line - cc->mth.code_start_loc.line;*/ + cc->mth.code.locptr[cc->mth.code.len] = srcloc->line; } else { @@ -6975,17 +6975,6 @@ static int add_compiled_method (moo_t* moo) MOO_STORE_OOP (moo, (moo_oop_t*)&mth->code, (moo_oop_t)code); #endif - if (cc->mth.code_start_loc.line <= MOO_SMOOI_MAX) - { - mth->source_line = MOO_SMOOI_TO_OOP(cc->mth.code_start_loc.line); - } - else - { - /* the source line is too large to be held as a SmallInteger. - * Just set it to 0 to indicate that the information is not available */ - mth->source_line = MOO_SMOOI_TO_OOP(0); - } - if (moo->dbgi) { moo_oow_t file_offset; @@ -7005,9 +6994,10 @@ static int add_compiled_method (moo_t* moo) /* TODO: warning */ file_offset = 0; } - mth->dbi_file_offset = MOO_SMOOI_TO_OOP(file_offset); + mth->dbgi_file_offset = MOO_SMOOI_TO_OOP(file_offset); - if (moo_addmethodtodbgi(moo, file_offset, cc->dbgi_class_offset, cc->mth.name.ptr, cc->mth.code.locptr, cc->mth.code.len, &method_offset) <= -1) +/* TODO: preserve source text... */ + if (moo_addmethodtodbgi(moo, file_offset, cc->dbgi_class_offset, cc->mth.name.ptr, cc->mth.code_start_loc.line, cc->mth.code.locptr, cc->mth.code.len, MOO_NULL, 0, &method_offset) <= -1) { /* TODO: warning. no debug information about this method will be available */ method_offset = 0; @@ -7016,14 +7006,9 @@ static int add_compiled_method (moo_t* moo) { method_offset = 0; } - mth->dbi_method_offset = MOO_SMOOI_TO_OOP(method_offset); + mth->dbgi_method_offset = MOO_SMOOI_TO_OOP(method_offset); } - /*TODO: preserve source??? mth->text = cc->mth.text -the compiler must collect all source method string collected so far. -need to write code to collect string. -*/ - #if defined(MOO_DEBUG_COMPILER) moo_decode (moo, mth, &cc->fqn); #endif diff --git a/moo/lib/debug.c b/moo/lib/debug.c index a0d8187..021af28 100644 --- a/moo/lib/debug.c +++ b/moo/lib/debug.c @@ -169,39 +169,6 @@ int moo_addfiletodbgi (moo_t* moo, const moo_ooch_t* file_name, moo_oow_t* start return 0; } -int moo_addtexttodbgi (moo_t* moo, const moo_ooch_t* text_ptr, moo_oow_t text_len, moo_oow_t* start_offset) -{ - moo_oow_t text_bytes, text_bytes_aligned, req_bytes; - moo_dbgi_text_t* di; - - if (!moo->dbgi) - { - if (start_offset) *start_offset = MOO_NULL; - return 0; /* debug information is disabled*/ - } - - /* NOTE: there is no duplication check for text */ - - text_bytes = (text_len + 1) * MOO_SIZEOF(*text_ptr); - text_bytes_aligned = MOO_ALIGN_POW2(text_bytes, MOO_SIZEOF_OOW_T); - req_bytes = MOO_SIZEOF(moo_dbgi_text_t) + text_bytes_aligned; - - di = (moo_dbgi_text_t*)secure_dbgi_space(moo, req_bytes); - if (!di) return -1; - - di->_type = MOO_DBGI_MAKE_TYPE(MOO_DBGI_TYPE_CODE_TEXT, 0); - di->_len = req_bytes; - di->_next = moo->dbgi->_last_text; - di->text_len = text_len; - moo_copy_oochars ((moo_ooch_t*)(di + 1), text_ptr, text_len); - - moo->dbgi->_last_text = moo->dbgi->_len; - moo->dbgi->_len += req_bytes; - - if (start_offset) *start_offset = moo->dbgi->_last_text; - return 0; -} - int moo_addclasstodbgi (moo_t* moo, const moo_ooch_t* class_name, moo_oow_t file_offset, moo_oow_t file_line, moo_oow_t* start_offset) { moo_oow_t name_len, name_bytes, name_bytes_aligned, req_bytes; @@ -248,10 +215,11 @@ int moo_addclasstodbgi (moo_t* moo, const moo_ooch_t* class_name, moo_oow_t file return 0; } -int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offset, const moo_ooch_t* method_name, const moo_oow_t* code_loc_ptr, moo_oow_t code_loc_len, moo_oow_t* start_offset) +int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offset, const moo_ooch_t* method_name, moo_oow_t start_line, const moo_oow_t* code_loc_ptr, moo_oow_t code_loc_len, const moo_ooch_t* text_ptr, moo_oow_t text_len, moo_oow_t* start_offset) { - moo_oow_t name_len, name_bytes, name_bytes_aligned, code_loc_bytes, code_loc_bytes_aligned, req_bytes; + moo_oow_t name_len, name_bytes, name_bytes_aligned, code_loc_bytes, code_loc_bytes_aligned, text_bytes, text_bytes_aligned, req_bytes; moo_dbgi_method_t* di; + moo_uint8_t* curptr; if (!moo->dbgi) return 0; /* debug information is disabled*/ @@ -260,7 +228,9 @@ int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offs name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T); code_loc_bytes = code_loc_len * MOO_SIZEOF(*code_loc_ptr); code_loc_bytes_aligned = MOO_ALIGN_POW2(code_loc_bytes, MOO_SIZEOF_OOW_T); - req_bytes = MOO_SIZEOF(moo_dbgi_method_t) + name_bytes_aligned + code_loc_bytes_aligned; + text_bytes = text_len * MOO_SIZEOF(*text_ptr); + text_bytes_aligned = MOO_ALIGN_POW2(text_bytes, MOO_SIZEOF_OOW_T); + req_bytes = MOO_SIZEOF(moo_dbgi_method_t) + name_bytes_aligned + code_loc_bytes_aligned + text_bytes_aligned; di = (moo_dbgi_method_t*)secure_dbgi_space(moo, req_bytes); if (!di) return -1; @@ -270,11 +240,23 @@ int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offs di->_next = moo->dbgi->_last_method; di->_file = file_offset; di->_class = class_offset; + di->start_line = start_line; di->code_loc_start = name_bytes_aligned; di->code_loc_len = code_loc_len; + di->text_start = name_bytes_aligned + code_loc_bytes_aligned; + di->text_len = text_len; - moo_copy_oocstr ((moo_ooch_t*)(di + 1), name_len + 1, method_name); - MOO_MEMCPY ((moo_uint8_t*)(di + 1) + name_bytes_aligned, code_loc_ptr, code_loc_bytes); + curptr = (moo_uint8_t*)(di + 1); + moo_copy_oocstr ((moo_ooch_t*)curptr, name_len + 1, method_name); + + curptr += name_bytes_aligned; + MOO_MEMCPY (curptr, code_loc_ptr, code_loc_bytes); + + if (text_len > 0) + { + curptr += code_loc_bytes_aligned; + moo_copy_oochars ((moo_ooch_t*)curptr, text_ptr, text_len); + } moo->dbgi->_last_method = moo->dbgi->_len; moo->dbgi->_len += req_bytes; diff --git a/moo/lib/exec.c b/moo/lib/exec.c index c49affa..d2d2269 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -2216,45 +2216,26 @@ static moo_pfrc_t pf_context_find_exception_handler (moo_t* moo, moo_mod_t* mod, } /* ------------------------------------------------------------------ */ -static moo_pfrc_t pf_method_get_source_text (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) +static MOO_INLINE moo_dbgi_method_t* get_dbgi_method (moo_t* moo, moo_oop_method_t mth) { - moo_oop_method_t rcv; - moo_oop_t retv = moo->_nil; /* TODO: empty string instead of nil? */ + moo_dbgi_method_t* di = MOO_NULL; -/* TODO: return something else lighter-weight than a string? */ - rcv = (moo_oop_method_t)MOO_STACK_GETRCV(moo, nargs); - MOO_PF_CHECK_RCV (moo, MOO_CLASSOF(moo, rcv) == moo->_method); - - if (moo->dbgi) + if (moo->dbgi && MOO_OOP_IS_SMOOI(mth->dbgi_method_offset)) { - if (MOO_OOP_IS_SMOOI(rcv->dbi_text_offset)) + moo_ooi_t dbgi_method_offset; + + dbgi_method_offset = MOO_OOP_TO_SMOOI(mth->dbgi_method_offset); + if (dbgi_method_offset > 0) { - moo_ooi_t dbi_text_offset; - - dbi_text_offset = MOO_OOP_TO_SMOOI(rcv->dbi_text_offset); - if (dbi_text_offset > 0) - { - moo_dbgi_text_t* di; - const moo_ooch_t* text_ptr; - - MOO_ASSERT (moo, dbi_text_offset < moo->dbgi->_len); - di = (moo_dbgi_text_t*)&((moo_uint8_t*)moo->dbgi)[dbi_text_offset]; - MOO_ASSERT (moo, MOO_DBGI_GET_TYPE_CODE(di->_type) == MOO_DBGI_TYPE_CODE_TEXT); - - text_ptr = (const moo_ooch_t*)(di + 1); - moo_pushvolat (moo, &retv); - retv = moo_makestring(moo, text_ptr, di->text_len); - moo_popvolat (moo); - if (!retv) return MOO_PF_FAILURE; - } + MOO_ASSERT (moo, dbgi_method_offset < moo->dbgi->_len); + di = (moo_dbgi_method_t*)&((moo_uint8_t*)moo->dbgi)[dbgi_method_offset]; + MOO_ASSERT (moo, MOO_DBGI_GET_TYPE_CODE(di->_type) == MOO_DBGI_TYPE_CODE_METHOD); } } - MOO_STACK_SETRET (moo, nargs, retv); - return MOO_PF_SUCCESS; + return di; } - static moo_pfrc_t pf_method_get_source_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { moo_oop_method_t rcv; @@ -2266,18 +2247,18 @@ static moo_pfrc_t pf_method_get_source_file (moo_t* moo, moo_mod_t* mod, moo_ooi if (moo->dbgi) { - if (MOO_OOP_IS_SMOOI(rcv->dbi_file_offset)) + if (MOO_OOP_IS_SMOOI(rcv->dbgi_file_offset)) { - moo_ooi_t dbi_file_offset; + moo_ooi_t dbgi_file_offset; - dbi_file_offset = MOO_OOP_TO_SMOOI(rcv->dbi_file_offset); - if (dbi_file_offset > 0) + dbgi_file_offset = MOO_OOP_TO_SMOOI(rcv->dbgi_file_offset); + if (dbgi_file_offset > 0) { moo_dbgi_file_t* di; const moo_ooch_t* file_name; - MOO_ASSERT (moo, dbi_file_offset < moo->dbgi->_len); - di = (moo_dbgi_file_t*)&((moo_uint8_t*)moo->dbgi)[dbi_file_offset]; + MOO_ASSERT (moo, dbgi_file_offset < moo->dbgi->_len); + di = (moo_dbgi_file_t*)&((moo_uint8_t*)moo->dbgi)[dbgi_file_offset]; MOO_ASSERT (moo, MOO_DBGI_GET_TYPE_CODE(di->_type) == MOO_DBGI_TYPE_CODE_FILE); file_name = (const moo_ooch_t*)(di + 1); @@ -2293,52 +2274,83 @@ static moo_pfrc_t pf_method_get_source_file (moo_t* moo, moo_mod_t* mod, moo_ooi return MOO_PF_SUCCESS; } + static moo_pfrc_t pf_method_get_ip_source_line (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { /* get source line of the given instruction pointer */ moo_oop_method_t rcv; - moo_oow_t line = 0; + moo_oop_t ip; + moo_dbgi_method_t* di; + moo_oow_t retv = MOO_SMOOI_TO_OOP(0); rcv = (moo_oop_method_t)MOO_STACK_GETRCV(moo, nargs); MOO_PF_CHECK_RCV (moo, MOO_CLASSOF(moo, rcv) == moo->_method); - if (moo->dbgi && MOO_OOP_IS_SMOOI(rcv->source_line)) + ip = MOO_STACK_GETARG(moo, nargs, 0); + MOO_PF_CHECK_ARGS (moo, nargs, MOO_OOP_IS_SMOOI(ip)); + + di = get_dbgi_method(moo, rcv); + if (di) { - moo_oop_t ip; - moo_ooi_t source_line, ipv; + moo_ooi_t ipv; + moo_oow_t* code_loc_ptr; - ip = MOO_STACK_GETARG(moo, nargs, 0); - MOO_PF_CHECK_ARGS (moo, nargs, MOO_OOP_IS_SMOOI(ip)); - - source_line = MOO_OOP_TO_SMOOI(rcv->source_line); ipv = MOO_OOP_TO_SMOOI(ip); - - if (source_line >= 0 && ipv >= 0 && MOO_OOP_IS_SMOOI(rcv->dbi_method_offset)) + code_loc_ptr = (moo_oow_t*)((moo_uint8_t*)(di + 1) + di->code_loc_start); + if (ipv < di->code_loc_len && code_loc_ptr[ipv] <= MOO_SMOOI_MAX) { - moo_ooi_t dbi_method_offset; - - dbi_method_offset = MOO_OOP_TO_SMOOI(rcv->dbi_method_offset); - if (dbi_method_offset > 0) - { - moo_dbgi_method_t* di; - moo_oow_t* code_loc_ptr; - - MOO_ASSERT (moo, dbi_method_offset < moo->dbgi->_len); - di = (moo_dbgi_method_t*)&((moo_uint8_t*)moo->dbgi)[dbi_method_offset]; - MOO_ASSERT (moo, MOO_DBGI_GET_TYPE_CODE(di->_type) == MOO_DBGI_TYPE_CODE_METHOD); - - code_loc_ptr = (moo_oow_t*)((moo_uint8_t*)(di + 1) + di->code_loc_start); - if (ipv < di->code_loc_len && code_loc_ptr[ipv] <= MOO_SMOOI_MAX) - { - line = code_loc_ptr[ipv] + source_line; /* this won't overflow but can exceed MOO_SMOOI_MAX */ - if (line > MOO_SMOOI_MAX) line = 0; - } - } + retv = moo_oowtoint(moo, code_loc_ptr[ipv]); + if (!retv) return MOO_PF_FAILURE; } /* TODO: security check for data corruption? check if the ipv offset and the size calcualted doesn't exceed di->_len... */ } - MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(line)); + MOO_STACK_SETRET (moo, nargs, retv); + return MOO_PF_SUCCESS; +} + +static moo_pfrc_t pf_method_get_source_line (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) +{ + moo_oop_method_t rcv; + moo_dbgi_method_t* di; + moo_oow_t retv = MOO_SMOOI_TO_OOP(0); + + rcv = (moo_oop_method_t)MOO_STACK_GETRCV(moo, nargs); + MOO_PF_CHECK_RCV (moo, MOO_CLASSOF(moo, rcv) == moo->_method); + + di = get_dbgi_method(moo, rcv); + if (di) + { + retv = moo_oowtoint(moo, di->start_line); + if (!retv) return MOO_PF_FAILURE; + } + + MOO_STACK_SETRET (moo, nargs,retv); + return MOO_PF_SUCCESS; +} + +static moo_pfrc_t pf_method_get_source_text (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) +{ + moo_oop_method_t rcv; + moo_dbgi_method_t* di; + moo_oop_t retv = moo->_nil; + + rcv = (moo_oop_method_t)MOO_STACK_GETRCV(moo, nargs); + MOO_PF_CHECK_RCV (moo, MOO_CLASSOF(moo, rcv) == moo->_method); + + di = get_dbgi_method(moo, rcv); + if (di) + { + if (di->text_len > 0) + { + const moo_ooch_t* text_ptr; + text_ptr = (const moo_ooch_t*)((moo_uint8_t*)(di + 1) + di->text_start); + retv = moo_makestring(moo, text_ptr, di->text_len); + if (!retv) return MOO_PF_FAILURE; + } + } + + MOO_STACK_SETRET (moo, nargs, retv); return MOO_PF_SUCCESS; } @@ -4067,7 +4079,8 @@ static pf_t pftab[] = { "CompiledMethod_ipSourceLine:", { pf_method_get_ip_source_line, 1, 1 } }, { "CompiledMethod_sourceFile", { pf_method_get_source_file, 0, 0 } }, - { "CompiledMethod_sourceText", { pf_method_get_source_text, 0, 0 } }, + { "CompiledMethod_sourceLine", { pf_method_get_source_line, 0, 0 } }, + { "CompiledMethod_sourceText", { pf_method_get_source_text, 0, 0 } }, { "Error_asCharacter", { pf_error_as_character, 0, 0 } }, { "Error_asInteger", { pf_error_as_integer, 0, 0 } }, diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index 012d776..634f5d1 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -1670,9 +1670,8 @@ void moo_dumpsymtab (moo_t* moo); void moo_dumpdic (moo_t* moo, moo_oop_dic_t dic, const moo_bch_t* title); int moo_addfiletodbgi (moo_t* moo, const moo_ooch_t* file_name, moo_oow_t* start_offset); -int moo_addtexttodbgi (moo_t* moo, const moo_ooch_t* text_ptr, moo_oow_t text_len, moo_oow_t* start_offset); int moo_addclasstodbgi (moo_t* moo, const moo_ooch_t* class_name, moo_oow_t file_offset, moo_oow_t file_line, moo_oow_t* start_offset); -int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offset, const moo_ooch_t* method_name, const moo_oow_t* code_loc_ptr, moo_oow_t code_loc_len, moo_oow_t* start_offset); +int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offset, const moo_ooch_t* method_name, moo_oow_t start_line, const moo_oow_t* code_loc_ptr, moo_oow_t code_loc_len, const moo_ooch_t* text_ptr, moo_oow_t text_len, moo_oow_t* start_offset); /* ========================================================================= */ /* comp.c */ diff --git a/moo/lib/moo.h b/moo/lib/moo.h index f703b57..a1693f2 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -555,9 +555,9 @@ struct moo_methsig_t }; #if defined(MOO_USE_METHOD_TRAILER) -# define MOO_METHOD_NAMED_INSTVARS 11 +# define MOO_METHOD_NAMED_INSTVARS 9 #else -# define MOO_METHOD_NAMED_INSTVARS 12 +# define MOO_METHOD_NAMED_INSTVARS 10 #endif typedef struct moo_method_t moo_method_t; typedef struct moo_method_t* moo_oop_method_t; @@ -585,10 +585,8 @@ struct moo_method_t moo_oop_byte_t code; /* ByteArray */ #endif - moo_oop_t dbi_text_offset; /* SmallInteger. Offset to source text in moo->dbgi. 0 if unavailable */ - moo_oop_t dbi_file_offset; /* SmallInteger. source file path that contains the definition of this method. offset from moo->dbgi. 0 if unavailable */ - moo_oop_t source_line; /* SmallInteger. line of the source file where the method definition begins. valid only if dbi_file_offset is greater than 0 */ - moo_oop_t dbi_method_offset; /* SmallInteger */ + moo_oop_t dbgi_file_offset; /* SmallInteger. source file path that contains the definition of this method. offset from moo->dbgi. 0 if unavailable */ + moo_oop_t dbgi_method_offset; /* SmallInteger */ /* == variable indexed part == */ moo_oop_t literal_frame[1]; /* it stores literals */ @@ -984,22 +982,6 @@ struct moo_dbgi_file_t /* ... file path here ... */ }; -typedef struct moo_dbgi_text_t moo_dbgi_text_t; -struct moo_dbgi_text_t -{ - moo_oow_t _type; - moo_oow_t _len; /* length of this record including the header and the text payload */ - moo_oow_t _next; /* offset to a previous text */ - /*moo_oow_t _file; - moo_oow_t _line; */ - moo_oow_t text_len; - /*moo_oow_t class_name_start; <--- needed for compaction. - moo_oow_t method_name_start; <--- needed for compaction */ - /* ... text ... */ -/* class name... */ -/* method name ... */ -}; - typedef struct moo_dbgi_class_t moo_dbgi_class_t; struct moo_dbgi_class_t { @@ -1019,8 +1001,11 @@ struct moo_dbgi_method_t moo_oow_t _next; moo_oow_t _file; moo_oow_t _class; + moo_oow_t start_line; moo_oow_t code_loc_start; /* start offset from the payload beginning within this record */ moo_oow_t code_loc_len; + moo_oow_t text_start; + moo_oow_t text_len; /* ... method name here ... */ /* ... code line numbers here ... */ };