we don't need no stinking selfs
[libfirm] / ir / be / bestabs.c
index e77aea5..baee9c3 100644 (file)
@@ -1,11 +1,29 @@
-/**
- * Stabs support.
+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
  *
- * @author Michael Beck
- * @date   11.9.2006
- * @cvsid  $Id$
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
+/**
+ * @file
+ * @brief   Stabs support.
+ * @author  Michael Beck
+ * @date    11.9.2006
+ * @version $Id$
+ */
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 #include <stdlib.h>
 #include <assert.h>
 
+#include "obst.h"
 #include "irprog.h"
-#include "entity.h"
 #include "irgraph.h"
-#include "typewalk.h"
 #include "tv.h"
 #include "xmalloc.h"
 #include "pmap.h"
 #include "irtools.h"
 #include "obst.h"
 #include "array.h"
-#include "be_dbgout.h"
+#include "be_dbgout_t.h"
 #include "beabi.h"
+#include "bemodule.h"
+#include "beemitter.h"
+#include "dbginfo.h"
 
 /* Non-Stab Symbol and Stab Symbol Types */
 enum stabs_types {
@@ -97,11 +117,14 @@ enum stabs_types {
  */
 typedef struct stabs_handle {
        dbg_handle              base;         /**< the base class */
-       FILE                    *f;           /**< the file write to */
-       entity                  *cur_ent;     /**< current method entity */
+       ir_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 */
+       const char              *main_file;   /**< name of the main source file */
+       const char              *curr_file;   /**< name of the current source file */
+       unsigned                label_num;
+       unsigned                last_line;
 } stabs_handle;
 
 /**
@@ -109,34 +132,36 @@ typedef struct stabs_handle {
  */
 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
        pmap_entry *entry;
+       unsigned num;
 
        if (tp == NULL) {
                /* map to the void type */
                return 0;
        }
        entry = pmap_find(h->type_map, tp);
-       return entry ? PTR_TO_INT(entry->value) : 0;
+       if (! entry) {
+               num = h->next_type_nr++;
+               pmap_insert(h->type_map, tp, INT_TO_PTR(num));
+       } else {
+               num = PTR_TO_INT(entry->value);
+       }
+       return num;
 }  /* get_type_number */
 
 /**
- * Assign a stabs type number to a Firm type.
+ * Map a given Type to void by assigned the type number 0.
  */
-static unsigned assign_type_number(stabs_handle *h, ir_type *tp) {
-       unsigned num;
-       if (tp == NULL) {
-               /* map to the void type */
-               return 0;
-       }
-       num = h->next_type_nr++;
-       pmap_insert(h->type_map, tp, INT_TO_PTR(num));
-       return num;
-}  /* assign_type_number */
+static void map_to_void(stabs_handle *h, ir_type *tp) {
+       pmap_insert(h->type_map, tp, INT_TO_PTR(0));
+}
 
 /**
  * generate the void type.
  */
 static void gen_void_type(stabs_handle *h) {
-       fprintf(h->f, "\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
+       (void) h;
+       be_emit_irprintf("\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
+       be_emit_write_line();
 }  /* gen_void_type */
 
 typedef struct walker_env {
@@ -169,14 +194,15 @@ typedef struct walker_env {
  * @param h    the stabs handle
  * @param tp   the type
  */
-static void gen_primitive_type(stabs_handle *h, ir_type *tp)
-{
+static void gen_primitive_type(stabs_handle *h, ir_type *tp) {
        ir_mode *mode = get_type_mode(tp);
        unsigned type_num;
 
+       SET_TYPE_READY(tp);
        if (mode == mode_T) {
-    /* jack and FirmJC compiler use mode_T for the void type.
+               /* firmcc, jack and the FirmJC compiler use mode_T for the void type.
                Ignore it here as it's name is remapped to "void". */
+               map_to_void(h, tp);
                return;
        }  /* if */
 
@@ -185,18 +211,19 @@ static void gen_primitive_type(stabs_handle *h, ir_type *tp)
                return;
        }  /* if */
 
-       type_num = assign_type_number(h, tp);
+       type_num = get_type_number(h, tp);
 
        if (mode_is_int(mode)) {
-               char buf[64];
-               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);
+               be_emit_irprintf("\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
+               be_emit_tarval(get_mode_min(mode));
+               be_emit_char(';');
+               be_emit_tarval(get_mode_max(mode));
+               be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
+               be_emit_write_line();
        } else if (mode_is_float(mode)) {
                int size = get_type_size_bytes(tp);
-               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);
+               be_emit_irprintf("\t.stabs\t\"%s:t%u=r1;%d;0;\",%d,0,0,0\n", get_type_name(tp), type_num, size, N_LSYM);
+               be_emit_write_line();
        }
 }  /* gen_primitive_type */
 
@@ -207,45 +234,119 @@ static void gen_primitive_type(stabs_handle *h, ir_type *tp)
  * @param tp   the type
  */
 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
-       unsigned type_num = assign_type_number(h, tp);
+       unsigned type_num = get_type_number(h, tp);
        int i, n;
 
-       fprintf(h->f, "\t.stabs\t\"%s:T%u=e", get_type_name(tp), type_num);
+       SET_TYPE_READY(tp);
+       be_emit_irprintf("\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];
 
                tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
-               fprintf(h->f, "%s:%s,", get_enumeration_name(ec), buf);
+               be_emit_irprintf("%s:%s,", get_enumeration_name(ec), buf);
        }
-       fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
+       be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
+       be_emit_write_line();
 }  /* gen_enum_type */
 
+/**
+ * print a pointer type
+ */
+void print_pointer_type(stabs_handle *h, ir_type *tp, int local) {
+       unsigned     type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
+       ir_type      *el_tp   = get_pointer_points_to_type(tp);
+       unsigned     el_num   = get_type_number(h, el_tp);
+
+       be_emit_irprintf("%u=*%u", type_num, el_num);
+}
+
 /**
  * Generates a pointer type
  *
- * @param h    the stabs handle
+ * @param env  the walker environment
  * @param tp   the type
  */
-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\t\"%s:t%u=*%u\",%d,0,0,0\n",
-               get_type_name(tp), type_num, el_num, N_LSYM);
+static void gen_pointer_type(wenv_t *env, ir_type *tp) {
+       stabs_handle *h       = env->h;
+       ir_type      *el_tp   = get_pointer_points_to_type(tp);
+
+       SET_TYPE_READY(tp);
+       if (! IS_TYPE_READY(el_tp))
+               waitq_put(env->wq, el_tp);
+
+       be_emit_irprintf("\t.stabs\t\"%s:t", get_type_name(tp));
+       print_pointer_type(h, tp, 0);
+       be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
+       be_emit_write_line();
 }  /* gen_pointer_type */
 
+/**
+ * print an array type
+ */
+static void print_array_type(stabs_handle *h, ir_type *tp, int local) {
+       ir_type      *etp     = get_array_element_type(tp);
+       int          i, n     = get_array_n_dimensions(tp);
+       unsigned     type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
+       int          *perm;
+
+       be_emit_irprintf("%u=a", type_num);
+       NEW_ARR_A(int, perm, n);
+       for (i = 0; i < n; ++i) {
+               perm[i] = get_array_order(tp, i);
+       }
+
+       for (i = 0; i < n; ++i) {
+               int dim = perm[i];
+
+               if (is_Const(get_array_lower_bound(tp, dim)) && is_Const(get_array_upper_bound(tp, dim))) {
+                       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 */
+                       be_emit_irprintf("r1;%ld;%ld;", min, max-1);
+               }
+       }
+
+       type_num = get_type_number(h, etp);
+       be_emit_irprintf("%d", type_num);
+}
+
+/**
+ * Generates an array type
+ *
+ * @param env  the walker environment
+ * @param tp   the type
+ */
+static void gen_array_type(wenv_t *env, ir_type *tp) {
+       stabs_handle *h   = env->h;
+       ir_type      *etp = get_array_element_type(tp);
+
+       SET_TYPE_READY(tp);
+       if (! IS_TYPE_READY(etp))
+               waitq_put(env->wq, etp);
+
+       be_emit_irprintf("\t.stabs\t\"%s:t", get_type_name(tp));
+
+       print_array_type(h, tp, 0);
+
+       be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
+       be_emit_write_line();
+}  /* gen_array_type */
+
 /**
  * Generates a struct/union type
  *
- * @param h    the stabs handle
+ * @param env  the walker environment
  * @param tp   the type
  */
-static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
-       unsigned type_num = assign_type_number(h, tp);
-       int i, n;
-       char desc;
+static void gen_struct_union_type(wenv_t *env, ir_type *tp) {
+       stabs_handle *h       = env->h;
+       unsigned     type_num = get_type_number(h, tp);
+       int          i, n;
+       char         desc = 's';
 
+       SET_TYPE_READY(tp);
        if (is_Struct_type(tp)) {
                desc = 's';
                if (get_type_mode(tp) != NULL) {
@@ -253,95 +354,94 @@ static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
                        return;
                }
        }
-       else
+       else if (is_Union_type(tp))
                desc = 'u';
 
-       fprintf(h->f, "\t.stabs\t\"%s:T%u=%c%d",
+       be_emit_irprintf("\t.stabs\t\"%s:Tt%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) {
-               entity  *ent = get_compound_member(tp, i);
-               ir_type *mtp = get_entity_type(ent);
-               int ofs, size;
+               ir_entity *ent = get_compound_member(tp, i);
+               ir_type   *mtp = get_entity_type(ent);
+               int ofs;
+               unsigned size;
 
-               ofs  = get_entity_offset_bits(ent);
+               if (! IS_TYPE_READY(mtp))
+                       waitq_put(env->wq, mtp);
+               ofs  = get_entity_offset(ent);
                if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
                        /* this structure is a bitfield, skip */
                        int i, n;
 
                        for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
-                               entity *ent = get_struct_member(mtp, i);
+                               ir_entity *ent = get_struct_member(mtp, i);
                                ir_type *tp = get_entity_type(ent);
                                int bofs;
 
                                type_num = get_type_number(h, tp);
-                               size = get_type_size_bits(tp);
-                               bofs = ofs + get_entity_offset_bits(ent);
+                               size = get_type_size_bytes(tp) * 8;
+                               bofs = (ofs + get_entity_offset(ent)) * 8 + get_entity_offset_bits_remainder(ent);
 
                                /* name:type, bit offset from the start of the struct', number of bits in the element. */
-                               fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, bofs, size);
+                               be_emit_irprintf("%s:%u,%d,%u;", get_entity_name(ent), type_num, bofs, size);
                        }
                } else {
                        /* no bitfield */
-                       type_num = get_type_number(h, mtp);
-                       size = get_type_size_bits(mtp);
+                       be_emit_irprintf("%s:", get_entity_name(ent));
 
-                       /* name:type, bit offset from the start of the struct', number of bits in the element. */
-                       fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, ofs, size);
+                       if (is_Array_type(mtp)) {
+                               /* use a local array definition */
+                               print_array_type(h, mtp, 1);
+                       } else if (is_Pointer_type(mtp)) {
+                               /* use local pointer definition */
+                               print_pointer_type(h, mtp, 1);
+                       } else {
+                               type_num = get_type_number(h, mtp);
+
+                               /* name:type, bit offset from the start of the struct', number of bits in the element. */
+                               be_emit_irprintf("%u", type_num);
+                       }
+                       size = get_type_size_bytes(mtp) * 8;
+                       be_emit_irprintf(",%d,%u;", ofs * 8, size);
                }
        }
-       fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
+       be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
+       be_emit_write_line();
 }  /* 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 env  the walker environment
  * @param tp   the type
  */
-static void gen_method_type(stabs_handle *h, ir_type *tp) {
-       unsigned type_num = assign_type_number(h, tp);
+static void gen_method_type(wenv_t *env, ir_type *tp) {
+       stabs_handle *h       = env->h;
+       unsigned     type_num = get_type_number(h, tp);
        ir_type *rtp = NULL;
        unsigned res_type_num;
+       int i, n = get_method_n_ress(tp);
 
-       if (get_method_n_ress(tp) > 0)
+       SET_TYPE_READY(tp);
+       if (n > 0) {
                rtp = get_method_res_type(tp, 0);
+               if (! IS_TYPE_READY(rtp))
+                       waitq_put(env->wq, rtp);
+       }
        res_type_num = get_type_number(h, rtp);
 
-       fprintf(h->f, "\t.stabs\t\"%s:t%u=f%u\",%d,0,0,0\n",
-               get_type_name(tp), type_num, res_type_num, N_LSYM);
+       be_emit_irprintf("\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);
+               if (! IS_TYPE_READY(rtp))
+                       waitq_put(env->wq, rtp);
+               res_type_num = get_type_number(h, rtp);
+               be_emit_irprintf(",%u", res_type_num);
+       }
+       be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
+       be_emit_write_line();
 }  /* gen_method_type */
 
 /**
@@ -372,31 +472,32 @@ static void walk_type(type_or_ent *tore, void *ctx)
                /* fall through */
        case tpo_struct:
        case tpo_union:
-               SET_TYPE_NOT_READY(env->wq, tp);
+               gen_struct_union_type(env, tp);
                break;
 
        case tpo_enumeration:
                gen_enum_type(env->h, tp);
-               SET_TYPE_READY(tp);
                break;
 
        case tpo_primitive:
                gen_primitive_type(env->h, tp);
-               SET_TYPE_READY(tp);
                break;
 
        case tpo_method:
+               gen_method_type(env, tp);
+               break;
+
        case tpo_array:
-               SET_TYPE_NOT_READY(env->wq, tp);
+               gen_array_type(env, tp);
                break;
 
        case tpo_pointer:
-               /* must construct the pointer type */
-               SET_TYPE_NOT_READY(env->wq, tp);
+               gen_pointer_type(env, tp);
                break;
 
        case tpo_unknown:
                /* the unknown type: ignore */
+               SET_TYPE_READY(tp);
                break;
        default:
                assert(! "Unknown tpop code");
@@ -404,98 +505,47 @@ static void walk_type(type_or_ent *tore, void *ctx)
 }  /* walk_type */
 
 /**
- * check, if a method type can be generated
- */
-static int is_method_type_ready(ir_type *tp)
-{
-  int i;
-  ir_type *etp;
-
-  for (i = get_method_n_ress(tp) - 1; i >= 0; --i) {
-    etp = SKIP_PTR(get_method_res_type(tp, i));
-
-    if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
-      return 0;
-  }  /* for */
-
-  for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
-    etp = SKIP_PTR(get_method_param_type(tp, i));
-
-    if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
-      return 0;
-  }  /* for */
-  return 1;
-}  /* is_method_type_ready */
-
-/**
- * check, whether a compound type can be generated
+ * generate declaration for all types
  */
-static int is_compound_type_ready(ir_type *tp)
+static void finish_types(wenv_t *env)
 {
-  int i;
-  ir_type *etp;
-
-  for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
-    etp = SKIP_PTR(get_entity_type(get_compound_member(tp, i)));
+       waitq *wq = env->wq;
+       ir_type *tp;
 
-    if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
-      return 0;
-  }  /* for */
-  return 1;
-}  /* is_compound_type_ready */
+       while (! waitq_empty(wq)) {
+               tp = waitq_get(wq);
+               if (IS_TYPE_READY(tp))
+                       continue;
 
-/**
- * generate declaration for all types
- */
-static void finish_types(stabs_handle *h, waitq *wq)
-{
-  ir_type *tp;
-
-  while (! waitq_empty(wq)) {
-    tp = waitq_get(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 */
-        SET_TYPE_READY(tp);
-        continue;
-      }  /* if */
-      break;
-    case tpo_class:
-    case tpo_union:
-    case tpo_struct:
-      if (is_compound_type_ready(tp)) {
-        gen_struct_union_type(h, tp);
-        SET_TYPE_READY(tp);
-        continue;
-      }  /* if */
-      break;
-    case tpo_array:
-      if (IS_TYPE_READY(get_array_element_type(tp))) {
-        gen_array_type(h, tp);
-        SET_TYPE_READY(tp);
-        continue;
-      }  /* if */
-      break;
-    case tpo_pointer:
-      if (IS_TYPE_READY(get_pointer_points_to_type(tp))) {
-        gen_pointer_type(h, tp);
-        SET_TYPE_READY(tp);
-        continue;
-      }  /* if */
-      break;
-    default:
-      assert(! "Unknown tpop code");
-    }  /* switch */
-
-    /* still not ready, defer type output */
-    pdeq_putr(wq, tp);
-  }  /* while */
+               switch (get_type_tpop_code(tp)) {
+               case tpo_method:
+                       gen_method_type(env, tp);
+                       break;
+               case tpo_class:
+               case tpo_union:
+               case tpo_struct:
+                       gen_struct_union_type(env, tp);
+                       break;
+               case tpo_enumeration:
+                       gen_enum_type(env->h, tp);
+                       break;
+               case tpo_primitive:
+                       gen_primitive_type(env->h, tp);
+                       break;
+               case tpo_array:
+                       gen_array_type(env, tp);
+                       break;
+               case tpo_pointer:
+                       gen_pointer_type(env, tp);
+                       break;
+               case tpo_unknown:
+                       /* the unknown type: ignore */
+                       SET_TYPE_READY(tp);
+                       break;
+               default:
+                       assert(! "Unknown tpop code");
+               }  /* switch */
+       }  /* while */
 }  /* finish_types */
 
 /**
@@ -507,7 +557,7 @@ static void gen_types(stabs_handle *h) {
        env.h  = h;
        env.wq = new_waitq();
        type_walk(NULL, walk_type, &env);
-       finish_types(h, env.wq);
+       finish_types(&env);
        del_waitq(env.wq);
 }  /* gen_types */
 
@@ -519,36 +569,70 @@ 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\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
+       h->main_file = h->curr_file = filename;
+       be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
+       be_emit_write_line();
 }  /* stabs_so */
 
 /**
  * Main Program
  */
 static void stabs_main_program(dbg_handle *handle) {
-       stabs_handle *h = (stabs_handle *)handle;
        ir_graph *irg = get_irp_main_irg();
+
+       (void) handle;
        if (irg) {
-               fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
+               be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
+               be_emit_write_line();
        }
 }  /* stabs_main_program */
 
-/**
- * prints a line number
- */
-static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
-       stabs_handle *h = (stabs_handle *)handle;
-       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 */
+static void stabs_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
+{
+       stabs_handle *handle = (stabs_handle*) h;
+       unsigned      lineno;
+       const char   *fname  = ir_retrieve_dbg_info(dbgi, &lineno);
+
+       if (fname == NULL)
+               return;
+
+       if (handle->curr_file != fname) {
+               /* TODO: escape filename correctly */
+               if (handle->curr_file != handle->main_file) {
+                       be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", handle->curr_file,
+                                        N_EINCL);
+                       be_emit_write_line();
+               }
+               if (fname != handle->main_file) {
+                       be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", fname, N_SOL);
+                       be_emit_write_line();
+               }
+               handle->curr_file = fname;
+       }
+       if (handle->last_line != lineno) {
+               char label[64];
+
+               snprintf(label, sizeof(label), ".LM%u", ++handle->label_num);
+               handle->last_line = lineno;
+
+               be_emit_irprintf("\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno,
+                                label, get_entity_ld_name(handle->cur_ent));
+               be_emit_write_line();
+
+               be_emit_string(label);
+               be_emit_cstring(":\n");
+               be_emit_write_line();
+       }
+}
 
 /**
  * dump the stabs for a method begin
  */
-static void stabs_method_begin(dbg_handle *handle, entity *ent, const be_stack_layout_t *layout) {
+static void stabs_method_begin(dbg_handle *handle, ir_entity *ent, const be_stack_layout_t *layout) {
        stabs_handle *h = (stabs_handle *)handle;
-       ir_type *mtp, *rtp;
-       unsigned type_num;
-       int i, between_size;
+       ir_type      *mtp, *rtp;
+       unsigned     type_num;
+       int          i, n, between_size;
 
        h->cur_ent = ent;
        h->layout  = layout;
@@ -562,22 +646,23 @@ static void stabs_method_begin(dbg_handle *handle, entity *ent, const be_stack_l
        else
                rtp = NULL;
        type_num = get_type_number(h, rtp);
-       fprintf(h->f, "\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
+       be_emit_irprintf("\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));
+       be_emit_write_line();
 
        /* create parameter entries */
        between_size = get_type_size_bytes(layout->between_type);
-       for (i = get_method_n_params(mtp) - 1; i >= 0; --i) {
+       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;
+               ir_entity *stack_ent;
 
         if (! name) {
           snprintf(buf, sizeof(buf), "arg%d", i);
@@ -587,9 +672,24 @@ static void stabs_method_begin(dbg_handle *handle, entity *ent, const be_stack_l
                   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;
+                       ofs = get_entity_offset(stack_ent) + between_size;
+               }
+               be_emit_irprintf("\t.stabs\t\"%s:p", name);
+               if (is_Array_type(ptp)) {
+                       /* use a local array definition */
+                       print_array_type(h, ptp, 1);
+               } else if (is_Pointer_type(ptp)) {
+                       /* use local pointer definition */
+                       print_pointer_type(h, ptp, 1);
+               } else {
+                       type_num = get_type_number(h, ptp);
+
+                       /* name:type, bit offset from the start of the struct', number of bits in the element. */
+                       be_emit_irprintf("%u", type_num);
                }
-               fprintf(h->f, "\t.stabs\t\"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
+
+               be_emit_irprintf("\",%d,0,0,%d\n", N_PSYM, ofs);
+               be_emit_write_line();
        }
 }  /* stabs_method_begin */
 
@@ -598,16 +698,16 @@ static void stabs_method_begin(dbg_handle *handle, entity *ent, const be_stack_l
  */
 static void stabs_method_end(dbg_handle *handle) {
        stabs_handle            *h = (stabs_handle *)handle;
-       entity                  *ent = h->cur_ent;
+       ir_entity               *ent = h->cur_ent;
        const be_stack_layout_t *layout = h->layout;
        const char              *ld_name = get_entity_ld_name(ent);
-       int                     i, frame_size;
+       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 = get_compound_n_members(layout->frame_type) - 1; i >= 0; --i) {
-               entity *ent = get_compound_member(layout->frame_type, i);
+       for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
+               ir_entity *ent = get_compound_member(layout->frame_type, i);
                ir_type *tp;
                int ofs;
                unsigned type_num;
@@ -621,15 +721,19 @@ static void stabs_method_end(dbg_handle *handle) {
                if (is_Method_type(tp))
                        continue;
                type_num = get_type_number(h, tp);
-               ofs      = -frame_size + get_entity_offset_bytes(ent);
+               ofs      = -frame_size + get_entity_offset(ent);
 
-               fprintf(h->f, "\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
+               be_emit_irprintf("\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
                        get_entity_name(ent), type_num, N_LSYM, ofs);
+               be_emit_write_line();
        }
        /* 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);
+       be_emit_irprintf("\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
+       be_emit_write_line();
+       be_emit_irprintf("\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
+       be_emit_write_line();
+       be_emit_irprintf(".Lscope%u:\n", scope_nr);
+       be_emit_write_line();
        ++scope_nr;
 
        h->cur_ent = NULL;
@@ -649,20 +753,32 @@ 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, ir_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\t\"%s:G%u\",%d,0,0,0\n",
-                       get_entity_name(ent), tp_num, N_GSYM);
-       } else {
-               fprintf(h->f, "\t.stabs\t\"%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';
+
+       be_emit_string(buf);
+}  /* stabs_variable */
 
 /**
  * Close the stabs handler.
@@ -680,77 +796,29 @@ static const debug_ops stabs_ops = {
        stabs_main_program,
        stabs_method_begin,
        stabs_method_end,
-       stabs_line,
        stabs_types,
-       stabs_global
+       stabs_variable,
+       stabs_set_dbg_info
 };
 
-/* 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) {
+dbg_handle *be_stabs_open(void) {
        stabs_handle *h = xmalloc(sizeof(*h));
 
        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;
-}  /* stabs_open */
+       h->main_file    = NULL;
+       h->curr_file    = NULL;
 
-/** close a debug handler. */
-void be_dbg_close(dbg_handle *h) {
-       if (h && h->ops->close)
-               h->ops->close(h);
-}  /* be_dbg_close */
+       return &h->base;
+}
 
-/**
- * start a new source object (compilation unit)
- */
-void be_dbg_so(dbg_handle *h, const char *filename) {
-       if (h && h->ops->so)
-               h->ops->so(h, filename);
-}  /* be_dbg_begin */
+void be_init_stabs(void)
+{
+       be_register_dbgout_module("stabs", be_stabs_open);
+}
 
-/**
- * Main program
- */
-void be_dbg_main_program(dbg_handle *h) {
-       if (h && h->ops->main_program)
-               h->ops->main_program(h);
-}  /* be_dbg_main_program */
-
-/** 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 && h->ops->line)
-               h->ops->line(h, lineno, address);
-}  /* be_dbg_line */
-
-/** dump types */
-void be_dbg_types(dbg_handle *h) {
-       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 && h->ops->global)
-               h->ops->global(h, obst, ent);
-}  /* be_dbg_global */
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_stabs);