Hello,

On Sat, 18 Jan 2020, Herman ten Brugge via Tinycc-devel wrote:

Oops. Sent the wrong patch. See correct one in attachment.

 I created a path to add debug type information (see attachment).

 The patch only supports basic types and array/ptr types of them.

It also doesn't deal with scopes, i.e. represents this incorrectly:

int foo (void) {
  int i = 1;
  float f;
  if (i) {
    float i = 1.0;
    f = i;
  }
  return i + f;
}

It's still useful, of course, also with the limitation. Some time ago I approached the debug problem from that angle first before dealing with types, see attached patch (which makes all variable be int). But I didn't like the outcome in that it uses a new data structure for the scopes and the associated book keeping. The difficulty with stabs is that the variables for a scope need to be emitted directly _before_ the scope-open stab, which really goes against the structure of TCCs parser, as the open/close stabs still need to form a correct nesting, so there's no natural place to emit the variable stabs :-/


Ciao,
Michael.


 So:
 int main(int argc, char *argv[])

 is supported.

 Other things like structs/unions/enums/bitfields are all translated to
 void.
 This should probably change in the future.

 I also generate a new type whenever I see a pointer or array.
 This creates a lot of types but gdb seems happy with it.
 If I compile tcc with bounds-checking I see 1473 types.
 In the future types should be optimized.

 Can I apply this patch?
commit 0d9b48883997b5c93d30155895e6d8e6ca7052c7
Merge: 6cb68c3 949e634
Author: Michael Matz <m...@suse.de>
Date:   Mon Dec 16 05:39:37 2019 +0100

    WIP on mob: 6cb68c3 arm64: fix some casts

diff --cc tcc.h
index 30f7eb9,30f7eb9..7164a76
--- a/tcc.h
+++ b/tcc.h
@@@ -1488,8 -1488,8 +1488,10 @@@ ST_FUNC int find_elf_sym(Section *s, co
  ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
  ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
  
++ST_FUNC void put_stab(TCCState *s1, unsigned strndx, int type, int other, int desc, unsigned long value);
  ST_FUNC void put_stabs(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value);
--ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
++ST_FUNC void put_stab_r(TCCState *s1, unsigned strndx, int type, int other, int desc, unsigned long value, int sym_index);
++ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value, int sym_index);
  ST_FUNC void put_stabn(TCCState *s1, int type, int other, int desc, int value);
  
  ST_FUNC void resolve_common_syms(TCCState *s1);
diff --cc tccelf.c
index 9d7391c,9d7391c..c32a3d4
--- a/tccelf.c
+++ b/tccelf.c
@@@ -781,25 -781,25 +781,37 @@@ ST_FUNC void squeeze_multi_relocs(Secti
  
  /* put stab debug information */
  
--ST_FUNC void put_stabs(TCCState *s1, const char *str, int type, int other, int desc,
--                      unsigned long value)
++ST_FUNC void put_stab(TCCState *s1, unsigned strndx, int type, int other,
++                      int desc, unsigned long value)
  {
      Stab_Sym *sym;
  
      sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
--    if (str) {
--        sym->n_strx = put_elf_str(stab_section->link, str);
--    } else {
--        sym->n_strx = 0;
--    }
++    sym->n_strx = strndx;
      sym->n_type = type;
      sym->n_other = other;
      sym->n_desc = desc;
      sym->n_value = value;
  }
  
--ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc,
--                        unsigned long value, Section *sec, int sym_index)
++ST_FUNC void put_stabs(TCCState *s1, const char *str, int type, int other,
++                       int desc, unsigned long value)
++{
++    put_stab(s1, str ? put_elf_str(stab_section->link, str) : 0,
++             type, other, desc, value);
++}
++
++ST_FUNC void put_stab_r(TCCState *s1, unsigned strndx, int type, int other,
++                        int desc, unsigned long value, int sym_index)
++{
++    put_stab(s1, strndx, type, other, desc, value);
++    put_elf_reloc(symtab_section, stab_section,
++                  stab_section->data_offset - sizeof(unsigned int),
++                  R_DATA_32, sym_index);
++}
++
++ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other,
++                         int desc, unsigned long value, int sym_index)
  {
      put_stabs(s1, str, type, other, desc, value);
      put_elf_reloc(symtab_section, stab_section,
@@@ -809,7 -809,7 +821,7 @@@
  
  ST_FUNC void put_stabn(TCCState *s1, int type, int other, int desc, int value)
  {
--    put_stabs(s1, NULL, type, other, desc, value);
++    put_stab(s1, 0, type, other, desc, value);
  }
  
  ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
diff --cc tccgen.c
index 1d1fe8a,1d1fe8a..4340e3d
--- a/tccgen.c
+++ b/tccgen.c
@@@ -216,9 -216,9 +216,10 @@@ ST_FUNC void tcc_debug_start(TCCState *
  #endif
          pstrcat(buf, sizeof(buf), "/");
          put_stabs_r(s1, buf, N_SO, 0, 0,
--                    text_section->data_offset, text_section, section_sym);
++                    text_section->data_offset, section_sym);
          put_stabs_r(s1, file->prev->filename, N_SO, 0, 0,
--                    text_section->data_offset, text_section, section_sym);
++                    text_section->data_offset, section_sym);
++        put_stabs(s1, "int:t1=r1;-2147483648;2147483647;", N_LSYM, 0, 0, 0);
          new_file = last_line_num = 0;
          func_ind = -1;
          /* we're currently 'including' the <command line> */
@@@ -238,7 -238,7 +239,7 @@@ ST_FUNC void tcc_debug_end(TCCState *s1
      if (!s1->do_debug)
          return;
      put_stabs_r(s1, NULL, N_SO, 0, 0,
--        text_section->data_offset, text_section, section_sym);
++        text_section->data_offset, section_sym);
  }
  
  static BufferedFile* put_new_file(TCCState *s1)
@@@ -248,7 -248,7 +249,7 @@@
      if (f->filename[0] == ':')
          f = f->prev;
      if (f && new_file) {
--        put_stabs_r(s1, f->filename, N_SOL, 0, 0, ind, text_section, section_sym);
++        put_stabs_r(s1, f->filename, N_SOL, 0, 0, ind, section_sym);
          new_file = last_line_num = 0;
      }
      return f;
@@@ -268,11 -268,11 +269,88 @@@ ST_FUNC void tcc_debug_line(TCCState *s
          put_stabn(s1, N_SLINE, 0, f->line_num, ind - func_ind);
      } else {
          /* from tcc_assemble */
--        put_stabs_r(s1, NULL, N_SLINE, 0, f->line_num, ind, text_section, section_sym);
++        put_stabs_r(s1, NULL, N_SLINE, 0, f->line_num, ind, section_sym);
      }
      last_line_num = f->line_num;
  }
  
++struct Dbgblock {
++    int sind, eind; /* start and end ind */
++    int nvars, avars; /* count and alloced vars */
++    struct Dbgvar {
++        unsigned char type;
++        unsigned int value;
++        unsigned int strndx;
++    } *vars;
++    struct Dbgblock *child, *next, *parent;
++};
++static struct Dbgblock *dbgblock, *rootdbgblock;
++static void dbg_open_block(void)
++{
++    struct Dbgblock *b = tcc_mallocz(sizeof *b);
++    b->sind = ind;
++    b->parent = dbgblock;
++    if (dbgblock) {
++      b->next = dbgblock->child;
++      dbgblock->child = b;
++    } else
++      rootdbgblock = b;
++    dbgblock = b;
++}
++
++static void dbg_close_block(void)
++{
++    dbgblock->eind = ind;
++    dbgblock = dbgblock->parent;
++}
++
++static void dbg_add_var(unsigned char type, unsigned value, unsigned strndx)
++{
++    struct Dbgvar *d;
++    if (dbgblock->nvars == dbgblock->avars) {
++        dbgblock->vars = tcc_realloc(dbgblock->vars, (1 + 2 * dbgblock->avars) * sizeof(dbgblock->vars[0]));
++        memset(dbgblock->vars + dbgblock->nvars, 0, (1 + dbgblock->avars) * sizeof(dbgblock->vars[0]));
++        dbgblock->avars = 1 + 2 * dbgblock->avars;
++    }
++    d = dbgblock->vars + dbgblock->nvars++;
++    d->type = type;
++    d->value = value;
++    d->strndx = strndx;
++}
++
++static void dbg_add_funcstabs(void)
++{
++    struct Dbgblock *b, *n;
++    if (dbgblock)
++      tcc_error("huh");
++    for (b = rootdbgblock; b;) {
++        int i;
++        for (i = b->nvars; i--;) {
++            if (b->vars[i].type == N_LCSYM || b->vars[i].type == N_STSYM)
++              put_stab_r(tcc_state, b->vars[i].strndx, b->vars[i].type, 0, 0, 0, b->vars[i].value);
++            else
++              put_stab(tcc_state, b->vars[i].strndx, b->vars[i].type, 0, 0, b->vars[i].value);
++        }
++        tcc_free(b->vars);
++        b->nvars = 0;
++        b->vars = NULL;
++        put_stabn(tcc_state, N_LBRAC, 0, 0, b->sind - func_ind);
++        n = b->child;
++        if (!n) {
++            while (1) {
++                put_stabn(tcc_state, N_RBRAC, 0, 0, b->eind - func_ind);
++                if ((n = b->next) || !(n = b->parent))
++                  break;
++                tcc_free(b);
++                b = n;
++            }
++            tcc_free(b);
++        }
++        b = n;
++    }
++    rootdbgblock = NULL;
++}
++
  /* put function symbol */
  ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
  {
@@@ -283,8 -283,8 +361,9 @@@
      /* XXX: we put here a dummy type */
      snprintf(buf, sizeof(buf), "%s:%c1",
               funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
--    put_stabs_r(s1, buf, N_FUN, 0, f->line_num, 0, cur_text_section, sym->c);
++    put_stabs_r(s1, buf, N_FUN, 0, f->line_num, 0, sym->c);
      tcc_debug_line(s1);
++    dbg_open_block();
  }
  
  /* put function size */
@@@ -292,6 -292,6 +371,8 @@@ ST_FUNC void tcc_debug_funcend(TCCStat
  {
      if (!s1->do_debug)
          return;
++    dbg_close_block();
++    dbg_add_funcstabs();
  #if 0 // this seems to confuse gnu tools
      put_stabn(s1, N_FUN, 0, 0, size);
  #endif
@@@ -324,6 -324,6 +405,37 @@@ ST_FUNC void tcc_debug_eincl(TCCState *
      new_file = 1;
  }
  
++static CString stabstr;
++static void tcc_debug_sym(Sym *sym)
++{
++    unsigned char type;
++    unsigned strndx;
++    unsigned long value;
++    char *symdesc = "";
++    if (!tcc_state->do_debug)
++        return;
++    cstr_reset(&stabstr);
++    value = 0;
++    if ((sym->r & VT_VALMASK) == VT_LOCAL) {
++        type = N_LSYM, value = (unsigned)sym->c;
++    } else if (sym->type.t & VT_STATIC) {
++        ElfSym *esym = elfsym(sym);
++        symdesc = sym->sym_scope ? "V" : "S";
++        type = esym->st_shndx == bss_section->sh_num ? N_LCSYM : N_STSYM;
++        value = sym->c; /* symindex for reloc */
++    } else {
++        type = N_GSYM;
++        symdesc = "G";
++    }
++    cstr_printf(&stabstr, "%s:%s%d", get_tok_str(sym->v, NULL), symdesc, 1);
++    strndx = put_elf_str(stab_section->link, stabstr.data);
++    if (dbgblock)
++      dbg_add_var(type, value, strndx);
++    else if (type == N_LCSYM || type == N_STSYM)
++      put_stab_r(tcc_state, strndx, type, 0, 0, 0, value);
++    else
++      put_stab(tcc_state, strndx, type, 0, 0, value);
++}
  /* ------------------------------------------------------------------------- */
  ST_FUNC int tccgen_compile(TCCState *s1)
  {
@@@ -6441,6 -6441,6 +6553,9 @@@ void new_scope(struct scope *o
      o->llstk = local_label_stack;
  
      ++local_scope;
++
++    if (tcc_state->do_debug)
++      dbg_open_block();
  }
  
  void prev_scope(struct scope *o, int is_expr)
@@@ -6450,6 -6450,6 +6565,9 @@@
      if (o->cl.s != o->prev->cl.s)
          block_cleanup(o->prev);
  
++    if (tcc_state->do_debug)
++      dbg_close_block();
++
      /* pop locally defined labels */
      label_pop(&local_label_stack, o->llstk, is_expr);
  
@@@ -7616,6 -7616,6 +7734,8 @@@ static void decl_initializer_alloc(CTyp
          }
  #endif
      }
++    if (v && sym)
++      tcc_debug_sym(sym);
  
      if (type->t & VT_VLA) {
          int a;
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to