History

tccgen.c: cleanup debug support

from 3e731e3a78c0b1f9a299afee2665b11d3c4c25fe tccgen.c: - make 'struct default_debug' const - pass TCCState* as parameter to tcc_debug_xxx functions - always check tcc_state->do_debug before calling functions - factor out tcc_debug_extern_sym() - remove formats "%lld"/"%llu" (not reliable on windows) xxx-gen files: - set func_vt/var from caller

grischka committed 6 years ago

Commit 9c283491 parent 5bc17207 files · +85 −78Browse files

Unified · Split

7 files
  1. arm-gen.c +0 −2
  2. arm64-gen.c +0 −2
  3. c67-gen.c +0 −2
  4. i386-gen.c +0 −2
  5. riscv64-gen.c +1 −2
  6. tccgen.c +82 −63
  7. x86_64-gen.c +2 −5
arm-gen.c+0−2
@@ -1278,8 +1278,6 @@ void gfunc_prolog(Sym *func_sym) · 1277 unmodified lines
12781278 #endif
12791279
12801280 sym = func_type->ref;
1281− func_vt = sym->type;
1282− func_var = (func_type->ref->f.func_type == FUNC_ELLIPSIS);
12831281
12841282 n = nf = 0;
12851283 if ((func_vt.t & VT_BTYPE) == VT_STRUCT &&
arm64-gen.c+0−2
@@ -1010,8 +1010,6 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 1009 unmodified lines
10101010 CType **t;
10111011 unsigned long *a;
10121012
1013− // Why doesn't the caller (gen_function) set func_vt?
1014− func_vt = func_type->ref->type;
10151013 func_vc = 144; // offset of where x8 is stored
10161014
10171015 for (sym = func_type->ref; sym; sym = sym->next)
c67-gen.c+0−2
@@ -1952,8 +1952,6 @@ void gfunc_prolog(Sym *func_sym) · 1951 unmodified lines
19521952 addr = 8;
19531953 /* if the function returns a structure, then add an
19541954 implicit pointer parameter */
1955− func_vt = sym->type;
1956− func_var = (sym->f.func_type == FUNC_ELLIPSIS);
19571955 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
19581956 func_vc = addr;
19591957 addr += 4;
i386-gen.c+0−2
@@ -533,8 +533,6 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 532 unmodified lines
533533 func_sub_sp_offset = ind;
534534 /* if the function returns a structure, then add an
535535 implicit pointer parameter */
536− func_vt = sym->type;
537− func_var = (sym->f.func_type == FUNC_ELLIPSIS);
538536 #ifdef TCC_TARGET_PE
539537 size = type_size(&func_vt,&align);
540538 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
riscv64-gen.c+1−2
@@ -627,7 +627,6 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 626 unmodified lines
627627 CType *type;
628628
629629 sym = func_type->ref;
630− func_vt = sym->type;
631630 loc = -16; // for ra and s0
632631 func_sub_sp_offset = ind;
633632 ind += 5 * 4;
@@ -686,7 +685,7 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 52 unmodified lines
686685 }
687686 func_va_list_ofs = addr;
688687 num_va_regs = 0;
689− if (func_type->ref->f.func_type == FUNC_ELLIPSIS) {
688+ if (func_var) {
690689 for (; areg[0] < 8; areg[0]++) {
691690 num_va_regs++;
692691 ES(0x23, 3, 8, 10 + areg[0], -8 + num_va_regs * 8); // sd aX, loc(s0)
tccgen.c+82−63
@@ -122,7 +122,10 @@ static struct scope { · 121 unmodified lines
122122 Sym *lstk, *llstk;
123123 } *cur_scope, *loop_scope, *root_scope;
124124
125−static struct {
125+/********************************************************/
126+/* stab debug support */
127+
128+static const struct {
126129 int type;
127130 const char *name;
128131 } default_debug[] = {
@@ -184,8 +187,6 @@ static struct debug_info { · 55 unmodified lines
184187 struct debug_info *child, *next, *last, *parent;
185188 } *debug_info, *debug_info_root;
186189
187−static CString debug_str;
188−
189190 /********************************************************/
190191 #if 1
191192 #define precedence_parser
@@ -404,15 +405,19 @@ ST_FUNC void tcc_debug_start(TCCState *s1) · 212 unmodified lines
404405 normalize_slashes(buf);
405406 #endif
406407 pstrcat(buf, sizeof(buf), "/");
407− debug_next_type = sizeof(default_debug) / sizeof(default_debug[0]);
408408 put_stabs_r(s1, buf, N_SO, 0, 0,
409409 text_section->data_offset, text_section, section_sym);
410410 put_stabs_r(s1, file->prev->filename, N_SO, 0, 0,
411411 text_section->data_offset, text_section, section_sym);
412412 for (i = 0; i < sizeof (default_debug) / sizeof (default_debug[0]); i++)
413413 put_stabs(s1, default_debug[i].name, N_LSYM, 0, 0, 0);
414+
414415 new_file = last_line_num = 0;
415416 func_ind = -1;
417+ debug_next_type = sizeof(default_debug) / sizeof(default_debug[0]);
418+ debug_hash = NULL;
419+ n_debug_hash = 0;
420+
416421 /* we're currently 'including' the <command line> */
417422 tcc_debug_bincl(s1);
418423 }
@@ -424,7 +429,7 @@ ST_FUNC void tcc_debug_start(TCCState *s1) · 5 unmodified lines
424429 SHN_ABS, file->filename);
425430 }
426431
427−static void tcc_debug_stabs (const char *str, int type, unsigned long value,
432+static void tcc_debug_stabs (TCCState *s1, const char *str, int type, unsigned long value,
428433 Section *sec, int sym_index)
429434 {
430435 struct debug_sym *s;
@@ -442,15 +447,13 @@ static void tcc_debug_stabs (const char *str, int type, unsigned long value, · 11 unmodified lines
442447 s->sym_index = sym_index;
443448 }
444449 else if (sec)
445− put_stabs_r (tcc_state, str, type, 0, 0, value, sec, sym_index);
450+ put_stabs_r (s1, str, type, 0, 0, value, sec, sym_index);
446451 else
447− put_stabs (tcc_state, str, type, 0, 0, value);
452+ put_stabs (s1, str, type, 0, 0, value);
448453 }
449454
450455 static void tcc_debug_stabn(int type, int value)
451456 {
452− if (!tcc_state->do_debug)
453− return;
454457 if (type == N_LBRAC) {
455458 struct debug_info *info =
456459 (struct debug_info *) tcc_mallocz(sizeof (*info));
@@ -478,7 +481,7 @@ static void tcc_debug_stabn(int type, int value) · 21 unmodified lines
478481 }
479482 }
480483
481−static void tcc_get_debug_info(Sym *s, CString *result)
484+static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result)
482485 {
483486 int type;
484487 int n = 0;
@@ -526,7 +529,7 @@ static void tcc_get_debug_info(Sym *s, CString *result) · 41 unmodified lines
526529 cstr_printf (&str, "%s:",
527530 (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
528531 ? "" : get_tok_str(t->v & ~SYM_FIELD, NULL));
529− tcc_get_debug_info (t, &str);
532+ tcc_get_debug_info (s1, t, &str);
530533 if (t->type.t & VT_BITFIELD) {
531534 pos = t->c * 8 + BIT_POS(t->type.t);
532535 size = BIT_SIZE(t->type.t);
@@ -538,7 +541,7 @@ static void tcc_get_debug_info(Sym *s, CString *result) · 5 unmodified lines
538541 cstr_printf (&str, ",%d,%d;", pos, size);
539542 }
540543 cstr_printf (&str, ";");
541− tcc_debug_stabs(str.data, N_LSYM, 0, NULL, 0);
544+ tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0);
542545 cstr_free (&str);
543546 }
544547 }
@@ -556,11 +559,11 @@ static void tcc_get_debug_info(Sym *s, CString *result) · 11 unmodified lines
556559 cstr_printf (&str, "%s:",
557560 (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
558561 ? "" : get_tok_str(t->v & ~SYM_FIELD, NULL));
559− cstr_printf (&str, e->type.t & VT_UNSIGNED ? "%llu," : "%lld,",
560− t->enum_val);
562+ cstr_printf (&str, e->type.t & VT_UNSIGNED ? "%u," : "%d,",
563+ (int)t->enum_val);
561564 }
562565 cstr_printf (&str, ";");
563− tcc_debug_stabs(str.data, N_LSYM, 0, NULL, 0);
566+ tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0);
564567 cstr_free (&str);
565568 }
566569 else {
@@ -592,7 +595,7 @@ static void tcc_get_debug_info(Sym *s, CString *result) · 25 unmodified lines
592595 cstr_printf (result, "%d", debug_type);
593596 }
594597
595−static void tcc_debug_finish (struct debug_info *cur)
598+static void tcc_debug_finish (TCCState *s1, struct debug_info *cur)
596599 {
597600 while (cur) {
598601 int i;
@@ -602,33 +605,54 @@ static void tcc_debug_finish (struct debug_info *cur) · 3 unmodified lines
602605 struct debug_sym *s = &cur->sym[i];
603606
604607 if (s->sec)
605− put_stabs_r(tcc_state, s->str, s->type, 0, 0, s->value,
608+ put_stabs_r(s1, s->str, s->type, 0, 0, s->value,
606609 s->sec, s->sym_index);
607610 else
608− put_stabs(tcc_state, s->str, s->type, 0, 0, s->value);
611+ put_stabs(s1, s->str, s->type, 0, 0, s->value);
609612 tcc_free (s->str);
610613 }
611614 tcc_free (cur->sym);
612− put_stabn(tcc_state, N_LBRAC, 0, 0, cur->start);
613− tcc_debug_finish (cur->child);
614− put_stabn(tcc_state, N_RBRAC, 0, 0, cur->end);
615+ put_stabn(s1, N_LBRAC, 0, 0, cur->start);
616+ tcc_debug_finish (s1, cur->child);
617+ put_stabn(s1, N_RBRAC, 0, 0, cur->end);
615618 tcc_free (cur);
616619 cur = next;
617620 }
618621 }
619622
620−static void tcc_add_debug_info(int param, Sym *s, Sym *e)
623+static void tcc_add_debug_info(TCCState *s1, int param, Sym *s, Sym *e)
621624 {
622− if (!tcc_state->do_debug)
623− return;
625+ CString debug_str;
626+ cstr_new (&debug_str);
624627 for (; s != e; s = s->prev) {
625628 if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
626629 continue;
627630 cstr_reset (&debug_str);
628631 cstr_printf (&debug_str, "%s:%s", get_tok_str(s->v, NULL), param ? "p" : "");
629− tcc_get_debug_info(s, &debug_str);
630− tcc_debug_stabs(debug_str.data, param ? N_PSYM : N_LSYM, s->c, NULL, 0);
632+ tcc_get_debug_info(s1, s, &debug_str);
633+ tcc_debug_stabs(s1, debug_str.data, param ? N_PSYM : N_LSYM, s->c, NULL, 0);
631634 }
635+ cstr_free (&debug_str);
636+}
637+
638+static void tcc_debug_extern_sym(TCCState *s1, Sym *sym, int sh_num, int sym_bind)
639+{
640+ Section *s = s1->sections[sh_num];
641+ CString str;
642+
643+ cstr_new (&str);
644+ cstr_printf (&str, "%s:%c",
645+ get_tok_str(sym->v, NULL),
646+ sym_bind == STB_GLOBAL ? 'G' : local_scope ? 'V' : 'S'
647+ );
648+ tcc_get_debug_info(s1, sym, &str);
649+ if (sym_bind == STB_GLOBAL)
650+ tcc_debug_stabs(s1, str.data, N_GSYM, 0, NULL, 0);
651+ else
652+ tcc_debug_stabs(s1, str.data,
653+ (sym->type.t & VT_STATIC) && data_section == s
654+ ? N_STSYM : N_LCSYM, 0, s, sym->c);
655+ cstr_free (&str);
632656 }
633657
634658 /* put end of translation unit info */
@@ -639,8 +663,6 @@ ST_FUNC void tcc_debug_end(TCCState *s1) · 4 unmodified lines
639663 put_stabs_r(s1, NULL, N_SO, 0, 0,
640664 text_section->data_offset, text_section, section_sym);
641665 tcc_free(debug_hash);
642− debug_hash = NULL;
643− n_debug_hash = 0;
644666 }
645667
646668 static BufferedFile* put_new_file(TCCState *s1)
@@ -677,14 +699,21 @@ ST_FUNC void tcc_debug_line(TCCState *s1) · 30 unmodified lines
677699 /* put function symbol */
678700 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
679701 {
702+ CString debug_str;
680703 BufferedFile *f;
681− if (!s1->do_debug || !(f = put_new_file(s1)))
704+ if (!s1->do_debug)
682705 return;
706+ debug_info_root = NULL;
707+ debug_info = NULL;
683708 tcc_debug_stabn(N_LBRAC, ind - func_ind);
684− cstr_reset (&debug_str);
709+ if (!(f = put_new_file(s1)))
710+ return;
711+ cstr_new (&debug_str);
685712 cstr_printf(&debug_str, "%s:%c", funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
686− tcc_get_debug_info(sym->type.ref, &debug_str);
713+ tcc_get_debug_info(s1, sym->type.ref, &debug_str);
687714 put_stabs_r(s1, debug_str.data, N_FUN, 0, f->line_num, 0, cur_text_section, sym->c);
715+ cstr_free (&debug_str);
716+
688717 tcc_debug_line(s1);
689718 }
690719
@@ -694,10 +723,7 @@ ST_FUNC void tcc_debug_funcend(TCCState *s1, int size) · 3 unmodified lines
694723 if (!s1->do_debug)
695724 return;
696725 tcc_debug_stabn(N_RBRAC, size);
697− tcc_debug_finish (debug_info_root);
698− debug_info_root = NULL;
699− debug_info = NULL;
700− cstr_free (&debug_str);
726+ tcc_debug_finish (s1, debug_info_root);
701727 }
702728
703729 /* put alternative filename */
@@ -926,25 +952,12 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 222 unmodified lines
926952 name = get_tok_str(sym->asm_label, NULL);
927953 info = ELFW(ST_INFO)(sym_bind, sym_type);
928954 sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
929− if (tcc_state->do_debug && sym_type != STT_FUNC && sym->v < SYM_FIRST_ANOM) {
930− CString str;
931955
932− cstr_new (&str);
933− cstr_printf (&str, "%s:%c", get_tok_str(sym->v, NULL),
934− debug_info ? 'V' : sym_bind == STB_GLOBAL ? 'G' : 'S');
935− tcc_get_debug_info(sym, &str);
936− if (sym_bind == STB_GLOBAL)
937− tcc_debug_stabs(str.data, N_GSYM, 0, NULL, 0);
938− else
939− tcc_debug_stabs(str.data,
940− (t & VT_STATIC) && data_section->sh_num == sh_num
941− ? N_STSYM : N_LCSYM, 0,
942− data_section->sh_num == sh_num ? data_section :
943− bss_section->sh_num == sh_num ? bss_section :
944− common_section->sh_num == sh_num ? common_section :
945− text_section, sym->c);
946− cstr_free (&str);
947− }
956+ if (tcc_state->do_debug
957+ && sym_type != STT_FUNC
958+ && sym->v < SYM_FIRST_ANOM)
959+ tcc_debug_extern_sym(tcc_state, sym, sh_num, sym_bind);
960+
948961 } else {
949962 esym = elfsym(sym);
950963 esym->st_value = value;
@@ -1920,13 +1933,14 @@ static void add_local_bounds(Sym *s, Sym *e) · 969 unmodified lines
19201933 #endif
19211934
19221935 /* Wrapper around sym_pop, that potentially also registers local bounds. */
1923−static void pop_local_syms(int param, Sym **ptop, Sym *b, int keep, int ellipsis)
1936+static void pop_local_syms(Sym **ptop, Sym *b, int keep, int ellipsis)
19241937 {
19251938 #ifdef CONFIG_TCC_BCHECK
1926− if (!ellipsis && !keep && tcc_state->do_bounds_check)
1939+ if (tcc_state->do_bounds_check && !ellipsis && !keep)
19271940 add_local_bounds(*ptop, b);
19281941 #endif
1929− tcc_add_debug_info (param, *ptop, b);
1942+ if (tcc_state->do_debug)
1943+ tcc_add_debug_info (tcc_state, !local_scope, *ptop, b);
19301944 sym_pop(ptop, b, keep);
19311945 }
19321946
@@ -6717,7 +6731,9 @@ void new_scope(struct scope *o) · 4784 unmodified lines
67176731 o->llstk = local_label_stack;
67186732
67196733 ++local_scope;
6720− tcc_debug_stabn(N_LBRAC, ind - func_ind);
6734+
6735+ if (tcc_state->do_debug)
6736+ tcc_debug_stabn(N_LBRAC, ind - func_ind);
67216737 }
67226738
67236739 void prev_scope(struct scope *o, int is_expr)
@@ -6739,12 +6755,12 @@ void prev_scope(struct scope *o, int is_expr) · 15 unmodified lines
67396755 tables, though. sym_pop will do that. */
67406756
67416757 /* pop locally defined symbols */
6742− pop_local_syms(0, &local_stack, o->lstk, is_expr, 0);
6743−
6744− tcc_debug_stabn(N_RBRAC, ind - func_ind);
6745−
6758+ pop_local_syms(&local_stack, o->lstk, is_expr, 0);
67466759 cur_scope = o->prev;
67476760 --local_scope;
6761+
6762+ if (tcc_state->do_debug)
6763+ tcc_debug_stabn(N_RBRAC, ind - func_ind);
67486764 }
67496765
67506766 /* leave a scope via break/continue(/goto) */
@@ -7952,8 +7968,12 @@ static void gen_function(Sym *sym) · 1201 unmodified lines
79527968 add_array (tcc_state, ".init_array", sym->c);
79537969 if (sym->type.ref->f.func_dtor)
79547970 add_array (tcc_state, ".fini_array", sym->c);
7971+
79557972 funcname = get_tok_str(sym->v, NULL);
79567973 func_ind = ind;
7974+ func_vt = sym->type.ref->type;
7975+ func_var = sym->type.ref->f.func_type == FUNC_ELLIPSIS;
7976+
79577977 /* put debug symbol */
79587978 tcc_debug_funcstart(tcc_state, sym);
79597979 /* push a dummy symbol to enable local sym storage */
@@ -7967,8 +7987,7 @@ static void gen_function(Sym *sym) · 7 unmodified lines
79677987 gsym(rsym);
79687988 nocode_wanted = 0;
79697989 /* reset local stack */
7970− pop_local_syms(1, &local_stack, NULL, 0,
7971− sym->type.ref->f.func_type == FUNC_ELLIPSIS);
7990+ pop_local_syms(&local_stack, NULL, 0, func_var);
79727991 gfunc_epilog();
79737992 cur_text_section->data_offset = ind;
79747993 local_scope = 0;
x86_64-gen.c+2−5
@@ -991,8 +991,6 @@ void gfunc_prolog(Sym *func_sym) · 990 unmodified lines
991991
992992 /* if the function returns a structure, then add an
993993 implicit pointer parameter */
994− func_vt = sym->type;
995− func_var = (sym->f.func_type == FUNC_ELLIPSIS);
996994 size = gfunc_arg_size(&func_vt);
997995 if (!using_regs(size)) {
998996 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
@@ -1032,7 +1030,7 @@ void gfunc_prolog(Sym *func_sym) · 33 unmodified lines
10321030 }
10331031
10341032 while (reg_param_index < REGN) {
1035− if (func_type->ref->f.func_type == FUNC_ELLIPSIS) {
1033+ if (func_var) {
10361034 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
10371035 addr += 8;
10381036 }
@@ -1491,7 +1489,7 @@ void gfunc_prolog(Sym *func_sym) · 452 unmodified lines
14911489 func_sub_sp_offset = ind;
14921490 func_ret_sub = 0;
14931491
1494− if (sym->f.func_type == FUNC_ELLIPSIS) {
1492+ if (func_var) {
14951493 int seen_reg_num, seen_sse_num, seen_stack_size;
14961494 seen_reg_num = seen_sse_num = 0;
14971495 /* frame pointer and return address */
@@ -1562,7 +1560,6 @@ void gfunc_prolog(Sym *func_sym) · 64 unmodified lines
15621560
15631561 /* if the function returns a structure, then add an
15641562 implicit pointer parameter */
1565− func_vt = sym->type;
15661563 mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, &reg_count);
15671564 if (mode == x86_64_mode_memory) {
15681565 push_arg_reg(reg_param_index);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t