From 6866bce75736c176aba9788dd90cc344a57754ee Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 14 Apr 2008 05:34:04 +0000 Subject: [PATCH] * compile.c, compile.h (compile_debug): made runtime option. * debug.c (ruby_debug_print_indent): returns if debug_level exceeds the threashold. * debug.c (ruby_debug_printf): printf to stderr. * iseq.c (make_compile_option, make_compile_option_value): added debug_level option. * vm_core.h (rb_compile_option_t): added debug_level. * vm_core.h (struct iseq_compile_data): added node_level. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16003 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 16 ++++++ compile.c | 154 ++++++++++++++++++++++++++++++++++-------------------- compile.h | 63 ++++++++++++---------- debug.c | 13 ++++- debug.h | 2 +- iseq.c | 10 ++++ vm_core.h | 2 + 7 files changed, 172 insertions(+), 88 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc1a24b391..c3b4afac43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Mon Apr 14 14:33:59 2008 Nobuyoshi Nakada + + * compile.c, compile.h (compile_debug): made runtime option. + + * debug.c (ruby_debug_print_indent): returns if debug_level exceeds + the threashold. + + * debug.c (ruby_debug_printf): printf to stderr. + + * iseq.c (make_compile_option, make_compile_option_value): added + debug_level option. + + * vm_core.h (rb_compile_option_t): added debug_level. + + * vm_core.h (struct iseq_compile_data): added node_level. + Mon Apr 14 12:52:25 2008 Nobuyoshi Nakada * gc.c (Init_stack): use ruby_init_stack. [ruby-dev:34350] diff --git a/compile.c b/compile.c index 5966c2aa73..0aebc8c056 100644 --- a/compile.c +++ b/compile.c @@ -92,9 +92,19 @@ struct iseq_compile_data_ensure_node_stack { #endif /* for debug */ -#if CPDEBUG > 0 -static long gl_node_level = 0; -static void debug_list(LINK_ANCHOR *anchor); +#if CPDEBUG < 0 +#define ISEQ_ARG iseq, +#define ISEQ_ARG_DECLARE rb_iseq_t *iseq, +#else +#define ISEQ_ARG +#define ISEQ_ARG_DECLARE +#endif + +#if CPDEBUG +#define gl_node_level iseq->compile_data->node_level +#if 0 +static void debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor); +#endif #endif static void dump_disasm_list(LINK_ELEMENT *elem); @@ -103,7 +113,7 @@ static int insn_data_length(INSN *iobj); static int insn_data_line_no(INSN *iobj); static int calc_sp_depth(int depth, INSN *iobj); -static void ADD_ELEM(LINK_ANCHOR *anchor, LINK_ELEMENT *elem); +static void ADD_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor, LINK_ELEMENT *elem); static INSN *new_insn_body(rb_iseq_t *iseq, int line_no, int insn_id, int argc, ...); static LABEL *new_label_body(rb_iseq_t *iseq, int line); @@ -123,6 +133,57 @@ static int iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *anchor); static int iseq_set_exception_table(rb_iseq_t *iseq); static int iseq_set_optargs_table(rb_iseq_t *iseq); +/* + * To make Array to LinkedList, use link_anchor + */ + +static void +verify_list(ISEQ_ARG_DECLARE char *info, LINK_ANCHOR *anchor) +{ +#if CPDEBUG + int flag = 0; + LINK_ELEMENT *list, *plist; + + if (!compile_debug) return; + + list = anchor->anchor.next; + plist = &anchor->anchor; + while (list) { + if (plist != list->prev) { + flag += 1; + } + plist = list; + list = list->next; + } + + if (anchor->last != plist && anchor->last != 0) { + flag |= 0x70000; + } + + if (flag != 0) { + rb_bug("list verify error: %08x (%s)", flag, info); + } +#endif +} +#if CPDEBUG < 0 +#define verify_list(info, anchor) verify_list(iseq, info, anchor) +#endif + +/* + * elem1, elem2 => elem1, elem2, elem + */ +static void +ADD_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor, LINK_ELEMENT *elem) +{ + elem->prev = anchor->last; + anchor->last->next = elem; + anchor->last = elem; + verify_list("add", anchor); +} +#if CPDEBUG < 0 +#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, anchor, elem) +#endif + static int iseq_add_mark_object(rb_iseq_t *iseq, VALUE v) { @@ -309,47 +370,6 @@ compile_data_alloc_adjust(rb_iseq_t *iseq) return (ADJUST *)compile_data_alloc(iseq, sizeof(ADJUST)); } -/* - * To make Array to LinkedList, use link_anchor - */ - -static void -verify_list(char *info, LINK_ANCHOR *anchor) -{ -#if CPDEBUG > 0 - int flag = 0; - LINK_ELEMENT *list = anchor->anchor.next, *plist = &anchor->anchor; - - while (list) { - if (plist != list->prev) { - flag += 1; - } - plist = list; - list = list->next; - } - - if (anchor->last != plist && anchor->last != 0) { - flag |= 0x70000; - } - - if (flag != 0) { - rb_bug("list verify error: %08x (%s)", flag, info); - } -#endif -} - -/* - * elem1, elem2 => elem1, elem2, elem - */ -static void -ADD_ELEM(LINK_ANCHOR *anchor, LINK_ELEMENT *elem) -{ - elem->prev = anchor->last; - anchor->last->next = elem; - anchor->last = elem; - verify_list("add", anchor); -} - /* * elem1, elemX => elem1, elem2, elemX */ @@ -420,7 +440,7 @@ LAST_ELEMENT(LINK_ANCHOR *anchor) #endif static LINK_ELEMENT * -POP_ELEMENT(LINK_ANCHOR *anchor) +POP_ELEMENT(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor) { LINK_ELEMENT *elem = anchor->last; anchor->last = anchor->last->prev; @@ -428,6 +448,9 @@ POP_ELEMENT(LINK_ANCHOR *anchor) verify_list("pop", anchor); return elem; } +#if CPDEBUG < 0 +#define POP_ELEMENT(anchor) POP_ELEMENT(iseq, anchor) +#endif #if 0 /* unused */ static LINK_ELEMENT * @@ -474,7 +497,7 @@ LIST_SIZE_ZERO(LINK_ANCHOR *anchor) * anc2: e4, e5 (broken) */ static void -APPEND_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) +APPEND_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) { if (anc2->anchor.next) { anc1->last->next = anc2->anchor.next; @@ -483,6 +506,9 @@ APPEND_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) } verify_list("append", anc1); } +#if CPDEBUG < 0 +#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, anc1, anc2) +#endif /* * anc1: e1, e2, e3 @@ -492,7 +518,7 @@ APPEND_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) * anc2: e4, e5 (broken) */ static void -INSERT_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) +INSERT_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) { if (anc2->anchor.next) { LINK_ELEMENT *first = anc1->anchor.next; @@ -509,6 +535,9 @@ INSERT_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) verify_list("append", anc1); } +#if CPDEBUG < 0 +#define INSERT_LIST(anc1, anc2) INSERT_LIST(iseq, anc1, anc2) +#endif #if 0 /* unused */ /* @@ -519,7 +548,7 @@ INSERT_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) * anc2: e1, e2, e3 */ static void -SWAP_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) +SWAP_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) { LINK_ANCHOR tmp = *anc2; @@ -530,9 +559,12 @@ SWAP_LIST(LINK_ANCHOR *anc1, LINK_ANCHOR *anc2) verify_list("swap1", anc1); verify_list("swap2", anc2); } +#if CPDEBUG < 0 +#define SWAP_LIST(anc1, anc2) SWAP_LIST(iseq, anc1, anc2) +#endif static LINK_ANCHOR * -REVERSE_LIST(LINK_ANCHOR *anc) +REVERSE_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *anc) { LINK_ELEMENT *first, *last, *elem, *e; first = &anc->anchor; @@ -561,11 +593,14 @@ REVERSE_LIST(LINK_ANCHOR *anc) verify_list("reverse", anc); return anc; } +#if CPDEBUG < 0 +#define REVERSE_LIST(anc) REVERSE_LIST(iseq, anc) +#endif #endif -#if CPDEBUG > 0 +#if CPDEBUG && 0 static void -debug_list(LINK_ANCHOR *anchor) +debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor) { LINK_ELEMENT *list = FIRST_ELEMENT(anchor); printf("----\n"); @@ -581,6 +616,9 @@ debug_list(LINK_ANCHOR *anchor) dump_disasm_list(anchor->anchor.next); verify_list("debug list", anchor); } +#if CPDEBUG < 0 +#define debug_list(anc) debug_list(iseq, anc) +#endif #endif static LABEL * @@ -678,32 +716,32 @@ iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *anchor) { /* debugs("[compile step 2] (iseq_array_to_linkedlist)\n"); */ - if (CPDEBUG > 5) + if (compile_debug > 5) dump_disasm_list(FIRST_ELEMENT(anchor)); debugs("[compile step 3.1 (iseq_optimize)]\n"); iseq_optimize(iseq, anchor); - if (CPDEBUG > 5) + if (compile_debug > 5) dump_disasm_list(FIRST_ELEMENT(anchor)); if (iseq->compile_data->option->instructions_unification) { debugs("[compile step 3.2 (iseq_insns_unification)]\n"); iseq_insns_unification(iseq, anchor); - if (CPDEBUG > 5) + if (compile_debug > 5) dump_disasm_list(FIRST_ELEMENT(anchor)); } if (iseq->compile_data->option->stack_caching) { debugs("[compile step 3.3 (iseq_set_sequence_stackcaching)]\n"); iseq_set_sequence_stackcaching(iseq, anchor); - if (CPDEBUG > 5) + if (compile_debug > 5) dump_disasm_list(FIRST_ELEMENT(anchor)); } debugs("[compile step 4.1 (iseq_set_sequence)]\n"); iseq_set_sequence(iseq, anchor); - if (CPDEBUG > 5) + if (compile_debug > 5) dump_disasm_list(FIRST_ELEMENT(anchor)); debugs("[compile step 4.2 (iseq_set_exception_table)]\n"); @@ -715,7 +753,7 @@ iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *anchor) debugs("[compile step 5 (iseq_translate_threaded_code)] \n"); iseq_translate_threaded_code(iseq); - if (CPDEBUG > 1) { + if (compile_debug > 1) { VALUE str = ruby_iseq_disasm(iseq->self); printf("%s\n", StringValueCStr(str)); fflush(stdout); diff --git a/compile.h b/compile.h index 94ceb3d577..c7688949e5 100644 --- a/compile.h +++ b/compile.h @@ -15,6 +15,7 @@ /* */ /** * debug function(macro) interface depend on CPDEBUG + * if it is less than 0, runtime option is in effect. * * debug level: * 0: no debug output @@ -29,38 +30,43 @@ #define CPDEBUG 0 #endif -#if 0 -#undef CPDEBUG -#define CPDEBUG 2 +#if CPDEBUG >= 0 +#define compile_debug CPDEBUG +#else +#define compile_debug iseq->compile_data->option->debug_level #endif NORETURN(PRINTF_ARGS(void rb_compile_bug(const char*, int, const char*, ...), 3, 4)); -#if CPDEBUG > 0 +#if CPDEBUG -#define debugp(header, value) \ - (ruby_debug_print_indent(0, CPDEBUG, gl_node_level * 2), \ - ruby_debug_print_value(0, CPDEBUG, header, value)) +#define compile_debug_print_indent(level) \ + ruby_debug_print_indent(level, compile_debug, gl_node_level * 2) -#define debugi(header, id) \ - (ruby_debug_print_indent(0, CPDEBUG, gl_node_level * 2), \ - ruby_debug_print_id(0, CPDEBUG, header, id)) +#define debugp(header, value) (void) \ + (compile_debug_print_indent(1) && \ + ruby_debug_print_value(1, compile_debug, header, value)) -#define debugp_param(header, value) \ - (ruby_debug_print_indent(1, CPDEBUG, gl_node_level * 2), \ - ruby_debug_print_value(1, CPDEBUG, header, value)) +#define debugi(header, id) (void) \ + (compile_debug_print_indent(1) && \ + ruby_debug_print_id(1, compile_debug, header, id)) -#define debugp_verbose(header, value) \ - (ruby_debug_print_indent(2, CPDEBUG, gl_node_level * 2), \ - ruby_debug_print_value(2, CPDEBUG, header, value)) +#define debugp_param(header, value) (void) \ + (compile_debug_print_indent(1) && \ + ruby_debug_print_value(1, compile_debug, header, value)) -#define debugp_verbose_node(header, value) \ - (ruby_debug_print_indent(10, CPDEBUG, gl_node_level * 2), \ - ruby_debug_print_value(10, CPDEBUG, header, value)) +#define debugp_verbose(header, value) (void) \ + (compile_debug_print_indent(2) && \ + ruby_debug_print_value(2, compile_debug, header, value)) -#define debug_node_start(node) \ - (ruby_debug_print_indent(-1, CPDEBUG, gl_node_level*2), \ - ruby_debug_print_node(1, CPDEBUG, "", (NODE *)node), gl_node_level++) \ +#define debugp_verbose_node(header, value) (void) \ + (compile_debug_print_indent(10) && \ + ruby_debug_print_value(10, compile_debug, header, value)) + +#define debug_node_start(node) ((void) \ + (compile_debug_print_indent(1) && \ + (ruby_debug_print_node(1, CPDEBUG, "", (NODE *)node), gl_node_level)), \ + gl_node_level++) #define debug_node_end() gl_node_level --; @@ -83,13 +89,14 @@ r_value(VALUE value) #define debugp_verbose(header, value) r_value(value) #define debugp_verbose_node(header, value) r_value(value) #define debugp_param(header, value) r_value(value) -#define debug_node_start(node) -#define debug_node_end() +#define debug_node_start(node) ((void)0) +#define debug_node_end() ((void)0) #endif -#if CPDEBUG > 1 -#define debugs ruby_debug_print_indent(-1, CPDEBUG, gl_node_level*2), printf -#define debug_compile(msg, v) (ruby_debug_print_indent(-1, CPDEBUG, gl_node_level*2), printf("%s", msg), (v)) +#if CPDEBUG > 1 || CPDEBUG < 0 +PRINTF_ARGS(void ruby_debug_printf(const char*, ...), 1, 2); +#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf +#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs(msg, stderr)), (v)) #else #define debugs if(0)printf #define debug_compile(msg, v) (v) @@ -201,7 +208,7 @@ r_value(VALUE value) #define COMPILE_ERROR(strs) \ { \ VALUE tmp = GET_THREAD()->errinfo; \ - if(CPDEBUG)rb_bug strs; \ + if (compile_debug) rb_compile_bug strs; \ GET_THREAD()->errinfo = iseq->compile_data->err_info; \ rb_compile_error strs; \ iseq->compile_data->err_info = GET_THREAD()->errinfo; \ diff --git a/debug.c b/debug.c index be90698b82..1bcd095901 100644 --- a/debug.c +++ b/debug.c @@ -67,13 +67,24 @@ static const union { const VALUE RUBY_FL_USER20 = FL_USER20; -void +int ruby_debug_print_indent(int level, int debug_level, int indent_level) { if (level < debug_level) { fprintf(stderr, "%*s", indent_level, ""); fflush(stderr); + return Qtrue; } + return Qfalse; +} + +void +ruby_debug_printf(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); } VALUE diff --git a/debug.h b/debug.h index 6a657eb38f..5c4b19a78b 100644 --- a/debug.h +++ b/debug.h @@ -25,7 +25,7 @@ VALUE ruby_debug_print_value(int level, int debug_level, const char *header, VALUE v); ID ruby_debug_print_id(int level, int debug_level, const char *header, ID id); NODE *ruby_debug_print_node(int level, int debug_level, const char *header, const NODE *node); -void ruby_debug_print_indent(int level, int debug_level, int indent_level); +int ruby_debug_print_indent(int level, int debug_level, int indent_level); void ruby_debug_breakpoint(void); void ruby_debug_gc_check_func(void); diff --git a/iseq.c b/iseq.c index 515980b5ff..c54239a81d 100644 --- a/iseq.c +++ b/iseq.c @@ -240,6 +240,10 @@ make_compile_option(rb_compile_option_t *option, VALUE opt) { VALUE flag = rb_hash_aref(h, ID2SYM(rb_intern(#mem))); \ if (flag == Qtrue) { o->mem = 1; } \ else if (flag == Qfalse) { o->mem = 0; } \ + } +#define SET_COMPILE_OPTION_NUM(o, h, mem) \ + { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \ + if (!NIL_P(num)) o->mem = NUM2INT(num); \ } SET_COMPILE_OPTION(option, opt, inline_const_cache); SET_COMPILE_OPTION(option, opt, peephole_optimization); @@ -249,7 +253,9 @@ make_compile_option(rb_compile_option_t *option, VALUE opt) SET_COMPILE_OPTION(option, opt, instructions_unification); SET_COMPILE_OPTION(option, opt, stack_caching); SET_COMPILE_OPTION(option, opt, trace_instruction); + SET_COMPILE_OPTION_NUM(option, opt, debug_level); #undef SET_COMPILE_OPTION +#undef SET_COMPILE_OPTION_NUM } else { rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil"); @@ -262,6 +268,8 @@ make_compile_option_value(rb_compile_option_t *option) VALUE opt = rb_hash_new(); #define SET_COMPILE_OPTION(o, h, mem) \ rb_hash_aset(h, ID2SYM(rb_intern(#mem)), o->mem ? Qtrue : Qfalse) +#define SET_COMPILE_OPTION_NUM(o, h, mem) \ + rb_hash_aset(h, ID2SYM(rb_intern(#mem)), INT2NUM(o->mem)) { SET_COMPILE_OPTION(option, opt, inline_const_cache); SET_COMPILE_OPTION(option, opt, peephole_optimization); @@ -270,8 +278,10 @@ make_compile_option_value(rb_compile_option_t *option) SET_COMPILE_OPTION(option, opt, operands_unification); SET_COMPILE_OPTION(option, opt, instructions_unification); SET_COMPILE_OPTION(option, opt, stack_caching); + SET_COMPILE_OPTION_NUM(option, opt, debug_level); } #undef SET_COMPILE_OPTION +#undef SET_COMPILE_OPTION_NUM return opt; } diff --git a/vm_core.h b/vm_core.h index 540175cba5..ab5240ed30 100644 --- a/vm_core.h +++ b/vm_core.h @@ -139,6 +139,7 @@ typedef struct rb_compile_option_struct { int instructions_unification; int stack_caching; int trace_instruction; + int debug_level; } rb_compile_option_t; struct iseq_compile_data { @@ -161,6 +162,7 @@ struct iseq_compile_data { struct iseq_compile_data_storage *storage_current; int last_line; int flip_cnt; + int node_level; const rb_compile_option_t *option; };