Added support for SymConst(ofs_ent)
[libfirm] / ir / be / bestabs.c
index 686129e..7121a07 100644 (file)
@@ -24,7 +24,9 @@
 #include "pdeq.h"
 #include "irtools.h"
 #include "obst.h"
+#include "array.h"
 #include "be_dbgout.h"
+#include "beabi.h"
 
 /* Non-Stab Symbol and Stab Symbol Types */
 enum stabs_types {
@@ -94,11 +96,12 @@ enum stabs_types {
  * The stabs handle.
  */
 typedef struct stabs_handle {
-       dbg_handle base;         /**< the base class */
-       FILE       *f;           /**< the file write to */
-       entity     *cur_ent;     /**< current method entity */
-       unsigned   next_type_nr; /**< next type number */
-       pmap       *type_map;    /**< a map from type to type number */
+       dbg_handle              base;         /**< the base class */
+       FILE                    *f;           /**< the file write to */
+       entity                  *cur_ent;     /**< current method entity */
+       const be_stack_layout_t *layout;      /**< current stack layout */
+       unsigned                next_type_nr; /**< next type number */
+       pmap                    *type_map;    /**< a map from type to type number */
 } stabs_handle;
 
 /**
@@ -133,7 +136,7 @@ static unsigned assign_type_number(stabs_handle *h, ir_type *tp) {
  * generate the void type.
  */
 static void gen_void_type(stabs_handle *h) {
-       fprintf(h->f, "\t.stabs \"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
+       fprintf(h->f, "\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
 }  /* gen_void_type */
 
 typedef struct walker_env {
@@ -145,7 +148,7 @@ typedef struct walker_env {
 #define SET_TYPE_NOT_READY(wq, tp) \
   do { \
     set_type_link(tp, (void *)1);  \
-    waitq_put(wq, tp);              \
+    waitq_put(wq, tp);             \
   } while(0)
 
 /* a the is ready */
@@ -186,14 +189,14 @@ static void gen_primitive_type(stabs_handle *h, ir_type *tp)
 
        if (mode_is_int(mode)) {
                char buf[64];
-               fprintf(h->f, "\t.stabs \"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
+               fprintf(h->f, "\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
                tarval_snprintf(buf, sizeof(buf), get_mode_min(mode));
                fprintf(h->f, "%s;", buf);
                tarval_snprintf(buf, sizeof(buf), get_mode_max(mode));
                fprintf(h->f, "%s;\",%d,0,0,0\n", buf, N_LSYM);
        } else if (mode_is_float(mode)) {
                int size = get_type_size_bytes(tp);
-               fprintf(h->f, "\t.stabs \"%s:t%u=r1;%d;0;\",%d,0,0,0\n", get_type_name(tp), type_num, size, N_LSYM);
+               fprintf(h->f, "\t.stabs\t\"%s:t%u=r1;%d;0;\",%d,0,0,0\n", get_type_name(tp), type_num, size, N_LSYM);
        }
 }  /* gen_primitive_type */
 
@@ -207,7 +210,7 @@ static void gen_enum_type(stabs_handle *h, ir_type *tp) {
        unsigned type_num = assign_type_number(h, tp);
        int i, n;
 
-       fprintf(h->f, "\t.stabs \"%s:T%u=e", get_type_name(tp), type_num);
+       fprintf(h->f, "\t.stabs\t\"%s:T%u=e", get_type_name(tp), type_num);
        for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
                ir_enum_const *ec = get_enumeration_const(tp, i);
                char buf[64];
@@ -228,7 +231,7 @@ static void gen_pointer_type(stabs_handle *h, ir_type *tp) {
        unsigned type_num = assign_type_number(h, tp);
        unsigned el_num   = get_type_number(h, get_pointer_points_to_type(tp));
 
-       fprintf(h->f, "\t.stabs \"%s:T%u=*%u\",%d,0,0,0\n",
+       fprintf(h->f, "\t.stabs\t\"%s:t%u=*%u\",%d,0,0,0\n",
                get_type_name(tp), type_num, el_num, N_LSYM);
 }  /* gen_pointer_type */
 
@@ -253,7 +256,7 @@ static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
        else
                desc = 'u';
 
-       fprintf(h->f, "\t.stabs \"%s:T%u=%c%d",
+       fprintf(h->f, "\t.stabs\t\"%s:T%u=%c%d",
                get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
 
        for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
@@ -290,6 +293,65 @@ static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
        fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
 }  /* gen_struct_type */
 
+/**
+ * Generates an array type
+ *
+ * @param h    the stabs handle
+ * @param tp   the type
+ */
+static void gen_array_type(stabs_handle *h, ir_type *tp) {
+       unsigned type_num = assign_type_number(h, tp);
+       int i, n = get_array_n_dimensions(tp);
+       int *perm;
+       ir_type *etp;
+
+       NEW_ARR_A(int, perm, n);
+       for (i = 0; i < n; ++i) {
+               perm[i] = get_array_order(tp, i);
+       }
+       fprintf(h->f, "\t.stabs\t\"%s:t%u=a", get_type_name(tp), type_num);
+
+       for (i = 0; i < n; ++i) {
+               int dim = perm[i];
+               long min = get_array_lower_bound_int(tp, dim);
+               long max = get_array_upper_bound_int(tp, dim);
+
+               /* FIXME r1 must be integer type, but seems to work for now */
+               fprintf(h->f, "r1;%ld;%ld;", min, max-1);
+       }
+
+       etp = get_array_element_type(tp);
+       type_num = get_type_number(h, etp);
+       fprintf(h->f, "%d\",%d,0,0,0\n", type_num, N_LSYM);
+}  /* gen_array_type */
+
+/**
+ * Generates a method type
+ *
+ * @param h    the stabs handle
+ * @param tp   the type
+ */
+static void gen_method_type(stabs_handle *h, ir_type *tp) {
+       unsigned type_num = assign_type_number(h, tp);
+       ir_type *rtp = NULL;
+       unsigned res_type_num;
+       int i, n = get_method_n_ress(tp);
+
+       if (n > 0)
+               rtp = get_method_res_type(tp, 0);
+       res_type_num = get_type_number(h, rtp);
+
+       fprintf(h->f, "\t.stabs\t\"%s:t%u=f%u", get_type_name(tp), type_num, res_type_num);
+
+       /* handle more than one return type */
+       for (i = 1; i < n; ++i) {
+               rtp = get_method_res_type(tp, i);
+               res_type_num = get_type_number(h, rtp);
+               fprintf(h->f, ",%u", res_type_num);
+       }
+       fprintf(h->f, "\",%d,0,0,0\n", N_LSYM);
+}  /* gen_method_type */
+
 /**
  * type-walker: generate declaration for simple types,
  * put all other types on a wait queue
@@ -297,10 +359,7 @@ static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
 static void walk_type(type_or_ent *tore, void *ctx)
 {
        wenv_t *env = ctx;
-       FILE *f = env->h->f;
-       entity   *ent;
        ir_type  *tp;
-       ir_graph *irg;
 
        if (get_kind(tore) == k_type) {
                tp = (ir_type *)tore;
@@ -406,12 +465,7 @@ static void finish_types(stabs_handle *h, waitq *wq)
     switch (get_type_tpop_code(tp)) {
     case tpo_method:
       if (is_method_type_ready(tp)) {
-        if (get_method_n_ress(tp) <= 1)
-          //gen_method_type(h, tp)
-                 ;
-        else {
-          fprintf(stderr, "Warning: Cannot create stabs debug info for type %s\n", get_type_name(tp));
-        }  /* if */
+        gen_method_type(h, tp);
         SET_TYPE_READY(tp);
         continue;
       }  /* if */
@@ -427,7 +481,7 @@ static void finish_types(stabs_handle *h, waitq *wq)
       break;
     case tpo_array:
       if (IS_TYPE_READY(get_array_element_type(tp))) {
-        //gen_array_type(h, tp);
+        gen_array_type(h, tp);
         SET_TYPE_READY(tp);
         continue;
       }  /* if */
@@ -469,7 +523,7 @@ static void gen_types(stabs_handle *h) {
  */
 static void stabs_so(dbg_handle *handle, const char *filename) {
        stabs_handle *h = (stabs_handle *)handle;
-       fprintf(h->f, "\t.stabs \"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
+       fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
 }  /* stabs_so */
 
 /**
@@ -479,7 +533,7 @@ static void stabs_main_program(dbg_handle *handle) {
        stabs_handle *h = (stabs_handle *)handle;
        ir_graph *irg = get_irp_main_irg();
        if (irg) {
-               fprintf(h->f, "\t.stabs \"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
+               fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
        }
 }  /* stabs_main_program */
 
@@ -488,20 +542,22 @@ static void stabs_main_program(dbg_handle *handle) {
  */
 static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
        stabs_handle *h = (stabs_handle *)handle;
-       fprintf(h->f, ".stabn %d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
+       fprintf(h->f, "\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
 }  /* stabs_line */
 
 /**
- * dump the stabs for a function
+ * dump the stabs for a method begin
  */
-static void stabs_method(dbg_handle *handle, entity *ent) {
+static void stabs_method_begin(dbg_handle *handle, entity *ent, const be_stack_layout_t *layout) {
        stabs_handle *h = (stabs_handle *)handle;
-       ir_type *mtp, *rtp;
-       unsigned type_num;
-       int i, n;
+       ir_type      *mtp, *rtp;
+       unsigned     type_num;
+       int          i, n, between_size;
 
        h->cur_ent = ent;
+       h->layout  = layout;
 
+       /* create the method entry */
        mtp = get_entity_type(ent);
        if (is_lowered_type(mtp))
                mtp = get_associated_type(mtp);
@@ -510,28 +566,79 @@ static void stabs_method(dbg_handle *handle, entity *ent) {
        else
                rtp = NULL;
        type_num = get_type_number(h, rtp);
-       fprintf(h->f, "\t.stabs \"%s:%c%u\",%u,0,0,%s\n",
+       fprintf(h->f, "\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
                get_entity_name(ent),
                get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
                type_num,
                N_FUN,
                get_entity_ld_name(ent));
 
+       /* create parameter entries */
+       between_size = get_type_size_bytes(layout->between_type);
        for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
                ir_type *ptp      = get_method_param_type(mtp, i);
         const char *name  = get_method_param_name(mtp, i);
                unsigned type_num = get_type_number(h, ptp);
         char buf[16];
         int ofs = 0;
+               entity *stack_ent;
 
         if (! name) {
           snprintf(buf, sizeof(buf), "arg%d", i);
           name = buf;
         }
-        /* FIXME: calculate the offset */
-               fprintf(h->f, "\t.stabs \"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
+               /* check if this parameter has a stack entity. If it has, it
+                  it transmitted on the stack, else in a register */
+               stack_ent = layout->param_map[i];
+               if (stack_ent) {
+                       ofs = get_entity_offset_bytes(stack_ent) + between_size;
+               }
+               fprintf(h->f, "\t.stabs\t\"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
        }
-}  /* stabs_method */
+}  /* stabs_method_begin */
+
+/**
+ * dump the stabs for a method end
+ */
+static void stabs_method_end(dbg_handle *handle) {
+       stabs_handle            *h = (stabs_handle *)handle;
+       entity                  *ent = h->cur_ent;
+       const be_stack_layout_t *layout = h->layout;
+       const char              *ld_name = get_entity_ld_name(ent);
+       int                     i, n, frame_size;
+       static unsigned         scope_nr = 0;
+
+       /* create entries for automatic variables on the stack */
+       frame_size = get_type_size_bytes(layout->frame_type);
+       for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
+               entity *ent = get_compound_member(layout->frame_type, i);
+               ir_type *tp;
+               int ofs;
+               unsigned type_num;
+
+               /* ignore spill slots and other helper objects */
+               if (is_entity_compiler_generated(ent))
+                       continue;
+
+               tp = get_entity_type(ent);
+               /* should not happen in backend but ... */
+               if (is_Method_type(tp))
+                       continue;
+               type_num = get_type_number(h, tp);
+               ofs      = -frame_size + get_entity_offset_bytes(ent);
+
+               fprintf(h->f, "\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
+                       get_entity_name(ent), type_num, N_LSYM, ofs);
+       }
+       /* we need a lexical block here */
+       fprintf(h->f, "\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
+       fprintf(h->f, "\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
+       fprintf(h->f, ".Lscope%u:\n", scope_nr);
+       ++scope_nr;
+
+       h->cur_ent = NULL;
+       h->layout  = NULL;
+}  /* stabs_method_end */
 
 /**
  * dump types
@@ -546,20 +653,35 @@ static void stabs_types(dbg_handle *handle) {
 }  /* stabs_types */
 
 /**
- * dump a global
+ * dump a variable in the global type
  */
-static void stabs_global(dbg_handle *handle, struct obstack *obst, entity *ent) {
+static void stabs_variable(dbg_handle *handle, struct obstack *obst, entity *ent) {
        stabs_handle *h = (stabs_handle *)handle;
        unsigned tp_num = get_type_number(h, get_entity_type(ent));
+       char buf[1024];
 
-       if (obst) {
-               obstack_printf(obst, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
-                       get_entity_name(ent), tp_num, N_GSYM);
-       } else {
-               fprintf(h->f, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
+       if (get_entity_visibility(ent) == visibility_external_visible) {
+               /* a global variable */
+               snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
                        get_entity_name(ent), tp_num, N_GSYM);
+       } else { /* some kind of local */
+               ir_variability variability = get_entity_variability(ent);
+               int kind = N_STSYM;
+
+               if (variability == variability_uninitialized)
+                       kind = N_LCSYM;
+               else if (variability == variability_constant)
+                       kind = N_ROSYM;
+               snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
+                       get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
        }
-}  /* stabs_global */
+       buf[sizeof(buf) - 1] = '\0';
+
+       if (obst)
+               obstack_printf(obst, "%s", buf);
+       else
+               fprintf(h->f, "%s", buf);
+}  /* stabs_variable */
 
 /**
  * Close the stabs handler.
@@ -575,12 +697,18 @@ static const debug_ops stabs_ops = {
        stabs_close,
        stabs_so,
        stabs_main_program,
-       stabs_method,
+       stabs_method_begin,
+       stabs_method_end,
        stabs_line,
        stabs_types,
-       stabs_global
+       stabs_variable
 };
 
+/* Opens the NULL handler */
+dbg_handle *be_nulldbg_open(void) {
+       return NULL;
+}  /* be_nulldbg_open */
+
 /* Opens a stabs handler */
 dbg_handle *be_stabs_open(FILE *out) {
        stabs_handle *h = xmalloc(sizeof(*h));
@@ -588,6 +716,7 @@ dbg_handle *be_stabs_open(FILE *out) {
        h->base.ops     = &stabs_ops;
        h->f            = out;
        h->cur_ent      = NULL;
+       h->layout       = NULL;
        h->next_type_nr = 0;
        h->type_map     = pmap_create_ex(64);
        return &h->base;
@@ -595,7 +724,7 @@ dbg_handle *be_stabs_open(FILE *out) {
 
 /** close a debug handler. */
 void be_dbg_close(dbg_handle *h) {
-       if (h->ops->close)
+       if (h && h->ops->close)
                h->ops->close(h);
 }  /* be_dbg_close */
 
@@ -603,7 +732,7 @@ void be_dbg_close(dbg_handle *h) {
  * start a new source object (compilation unit)
  */
 void be_dbg_so(dbg_handle *h, const char *filename) {
-       if (h->ops->so)
+       if (h && h->ops->so)
                h->ops->so(h, filename);
 }  /* be_dbg_begin */
 
@@ -611,30 +740,36 @@ void be_dbg_so(dbg_handle *h, const char *filename) {
  * Main program
  */
 void be_dbg_main_program(dbg_handle *h) {
-       if (h->ops->main_program)
+       if (h && h->ops->main_program)
                h->ops->main_program(h);
 }  /* be_dbg_main_program */
 
-/** debug for a function */
-void be_dbg_method(dbg_handle *h, entity *ent) {
-       if (h->ops->method)
-               h->ops->method(h, ent);
-}  /* be_dbg_method */
+/** debug for a method begin */
+void be_dbg_method_begin(dbg_handle *h, entity *ent, const be_stack_layout_t *layout) {
+       if (h && h->ops->method_begin)
+               h->ops->method_begin(h, ent, layout);
+}  /* be_dbg_method_begin */
+
+/** debug for a method end */
+void be_dbg_method_end(dbg_handle *h) {
+       if (h && h->ops->method_end)
+               h->ops->method_end(h);
+}  /* be_dbg_method_end */
 
 /** debug for line number */
 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
-       if (h->ops->line)
+       if (h && h->ops->line)
                h->ops->line(h, lineno, address);
 }  /* be_dbg_line */
 
 /** dump types */
 void be_dbg_types(dbg_handle *h) {
-       if (h->ops->types)
+       if (h && h->ops->types)
                h->ops->types(h);
 }  /* be_dbg_types */
 
 /** dump a global */
-void be_dbg_global(dbg_handle *h, struct obstack *obst, entity *ent) {
-       if (h->ops->global)
-               h->ops->global(h, obst, ent);
-}  /* be_dbg_global */
+void be_dbg_variable(dbg_handle *h, struct obstack *obst, entity *ent) {
+       if (h && h->ops->variable)
+               h->ops->variable(h, obst, ent);
+}  /* be_dbg_variable */