8b3fc01f24fd7e66fb32e58755cebdc270257438
[libfirm] / ir / be / bestabs.c
1 /**
2  * Stabs support.
3  *
4  * @author Michael Beck
5  * @date   11.9.2006
6  * @cvsid  $Id$
7  */
8
9 #ifdef HAVE_CONFIG_H
10 # include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16
17 #include "obst.h"
18 #include "irprog.h"
19 #include "entity.h"
20 #include "irgraph.h"
21 #include "typewalk.h"
22 #include "tv.h"
23 #include "xmalloc.h"
24 #include "pmap.h"
25 #include "pdeq.h"
26 #include "irtools.h"
27 #include "obst.h"
28 #include "array.h"
29 #include "be_dbgout.h"
30 #include "beabi.h"
31
32 /* Non-Stab Symbol and Stab Symbol Types */
33 enum stabs_types {
34         N_UNDF    = 0x00,    /**< 0: Undefined symbol */
35         N_ABS     = 0x02,    /**< 2: File scope absolute symbol */
36         N_TEXT    = 0x04,    /**< 4: File scope text symbol */
37         N_DATA    = 0x06,    /**< 6: File scope data symbol */
38         N_BSS     = 0x08,    /**< 8: File scope BSS symbol */
39         N_INDR    = 0x0A,    /**< 10: Symbol is indirected to another symbol */
40         N_FN_SEQ  = 0x0C,    /**< 12: Same as N_FN, for Sequent compilers */
41         N_COMM    = 0x12,    /**< 18: Common--visible after shared library dynamic link */
42         N_SETA    = 0x14,    /**< 20: Absolute set element */
43         N_SETT    = 0x17,    /**< 23: Text segment set element */
44         N_SETD    = 0x18,    /**< 24: Data segment set element */
45         N_SETB    = 0x1A,    /**< 26: BSS segment set element */
46         N_SETV    = 0x1C,    /**< 28: Pointer to set vector */
47         N_WARNING = 0x1E,    /**< 30: Print a warning message during linking */
48         N_FN      = 0x1F,    /**< 31: File name of a `.o' file */
49         N_GSYM    = 0x20,    /**< 32: Global symbol */
50         N_FNAME   = 0x22,    /**< 34: Function name (for BSD Fortran) */
51         N_FUN     = 0x24,    /**< 36: Function name */
52         N_STSYM   = 0x26,    /**< 38: Data segment file-scope variable */
53         N_LCSYM   = 0x28,    /**< 40: BSS segment file-scope variable */
54         N_MAIN    = 0x2A,    /**< 42: Name of main routine */
55         N_ROSYM   = 0x2C,    /**< 44: Variable in .rodata section */
56         N_PC      = 0x30,    /**< 48: Global symbol (for Pascal) */
57         N_NSYMS   = 0x32,    /**< 50: Number of symbols (according to Ultrix V4.0) */
58         N_NOMAP   = 0x34,    /**< 52: No DST map */
59         N_OBJ     = 0x38,    /**< 56: Object file (Solaris2) */
60         N_OPT     = 0x3C,    /**< 60: Debugger options (Solaris2) */
61         N_RSYM    = 0x40,    /**< 64: Register variable */
62         N_M2C     = 0x42,    /**< 66: Modula-2 compilation unit */
63         N_SLINE   = 0x44,    /**< 68: Line number in text segment */
64         N_DSLINE  = 0x46,    /**< 70: Line number in data segment */
65         N_BSLINE  = 0x48,    /**< 72: Line number in bss segment */
66         N_BROWS   = 0x48,    /**< 72: Sun source code browser, path to `.cb' file */
67         N_DEFD    = 0x4A,    /**< 74: GNU Modula2 definition module dependency */
68         N_FLINE   = 0x4C,    /**< 76: Function start/body/end line numbers (Solaris2) */
69         N_EHDECL  = 0x50,    /**< 80: GNU C++ exception variable */
70         N_MOD2    = 0x50,    /**< 80: Modula2 info "for imc" (according to Ultrix V4.0) */
71         N_CATCH   = 0x54,    /**< 84: GNU C++ catch clause */
72         N_SSYM    = 0x60,    /**< 96: Structure of union element */
73         N_ENDM    = 0x62,    /**< 98: Last stab for module (Solaris2) */
74         N_SO      = 0x64,    /**< 100: Path and name of source file */
75         N_LSYM    = 0x80,    /**< 128: Stack variable */
76         N_BINCL   = 0x82,    /**< 130: Beginning of an include file (Sun only) */
77         N_SOL     = 0x84,    /**< 132: Name of include file */
78         N_PSYM    = 0xA0,    /**< 160: Parameter variable */
79         N_EINCL   = 0xA2,    /**< 162: End of an include file */
80         N_ENTRY   = 0xA4,    /**< 164: Alternate entry point */
81         N_LBRAC   = 0xC0,    /**< 192: Beginning of a lexical block */
82         N_EXCL    = 0xC2,    /**< 194: Place holder for a deleted include file */
83         N_SCOPE   = 0xC4,    /**< 196: Modula2 scope information (Sun linker) */
84         N_RBRAC   = 0xE0,    /**< 224: End of a lexical block */
85         N_BCOMM   = 0xE2,    /**< 226: Begin named common block */
86         N_ECOMM   = 0xE4,    /**< 228: End named common block */
87         N_ECOML   = 0xE8,    /**< 232: Member of a common block */
88         N_WITH    = 0xEA,    /**< 234: Pascal with statement: type,,0,0,offset (Solaris2) */
89         N_NBTEXT  = 0xF0,    /**< 240: Gould non-base registers */
90         N_NBDATA  = 0xF2,    /**< 242: Gould non-base registers */
91         N_NBBSS   = 0xF4,    /**< 244: Gould non-base registers */
92         N_NBSTS   = 0xF6,    /**< 246: Gould non-base registers */
93         N_NBLCS   = 0xF8,    /**< 248: Gould non-base registers */
94 };
95
96 /**
97  * The stabs handle.
98  */
99 typedef struct stabs_handle {
100         dbg_handle              base;         /**< the base class */
101         FILE                    *f;           /**< the file write to */
102         ir_entity               *cur_ent;     /**< current method entity */
103         const be_stack_layout_t *layout;      /**< current stack layout */
104         unsigned                next_type_nr; /**< next type number */
105         pmap                    *type_map;    /**< a map from type to type number */
106         const char              *main_file;   /**< name of the main source file */
107         const char              *curr_file;   /**< name of teh current source file */
108 } stabs_handle;
109
110 /**
111  * Returns the stabs type number of a Firm type.
112  */
113 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
114         pmap_entry *entry;
115         unsigned num;
116
117         if (tp == NULL) {
118                 /* map to the void type */
119                 return 0;
120         }
121         entry = pmap_find(h->type_map, tp);
122         if (! entry) {
123                 num = h->next_type_nr++;
124                 pmap_insert(h->type_map, tp, INT_TO_PTR(num));
125         } else {
126                 num = PTR_TO_INT(entry->value);
127         }
128         return num;
129 }  /* get_type_number */
130
131 /**
132  * generate the void type.
133  */
134 static void gen_void_type(stabs_handle *h) {
135         fprintf(h->f, "\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
136 }  /* gen_void_type */
137
138 typedef struct walker_env {
139         stabs_handle *h;
140         waitq        *wq;
141 } wenv_t;
142
143 /* a type is not ready: put it on the wait queue */
144 #define SET_TYPE_NOT_READY(wq, tp) \
145   do { \
146     set_type_link(tp, (void *)1);  \
147     waitq_put(wq, tp);             \
148   } while(0)
149
150 /* a the is ready */
151 #define SET_TYPE_READY(tp)     set_type_link(tp, NULL)
152
153 /* check whether a type is ready */
154 #define IS_TYPE_READY(tp)      (get_type_link(tp) == NULL)
155
156 #ifdef EXPLICITE_PTR_TYPES
157 #define SKIP_PTR(tp)  tp
158 #else
159 #define SKIP_PTR(tp)   (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
160 #endif
161
162 /**
163  * Generates a primitive type.
164  *
165  * @param h    the stabs handle
166  * @param tp   the type
167  */
168 static void gen_primitive_type(stabs_handle *h, ir_type *tp)
169 {
170         ir_mode *mode = get_type_mode(tp);
171         unsigned type_num;
172
173         SET_TYPE_READY(tp);
174         if (mode == mode_T) {
175     /* jack and FirmJC compiler use mode_T for the void type.
176                 Ignore it here as it's name is remapped to "void". */
177                 return;
178         }  /* if */
179
180         if (0 && get_mode_size_bits(mode) & 7) {
181                 /* this is a bitfield type, ignore it */
182                 return;
183         }  /* if */
184
185         type_num = get_type_number(h, tp);
186
187         if (mode_is_int(mode)) {
188                 char buf[64];
189                 fprintf(h->f, "\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
190                 tarval_snprintf(buf, sizeof(buf), get_mode_min(mode));
191                 fprintf(h->f, "%s;", buf);
192                 tarval_snprintf(buf, sizeof(buf), get_mode_max(mode));
193                 fprintf(h->f, "%s;\",%d,0,0,0\n", buf, N_LSYM);
194         } else if (mode_is_float(mode)) {
195                 int size = get_type_size_bytes(tp);
196                 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);
197         }
198 }  /* gen_primitive_type */
199
200 /**
201  * Generates an enum type
202  *
203  * @param h    the stabs handle
204  * @param tp   the type
205  */
206 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
207         unsigned type_num = get_type_number(h, tp);
208         int i, n;
209
210         SET_TYPE_READY(tp);
211         fprintf(h->f, "\t.stabs\t\"%s:T%u=e", get_type_name(tp), type_num);
212         for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
213                 ir_enum_const *ec = get_enumeration_const(tp, i);
214                 char buf[64];
215
216                 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
217                 fprintf(h->f, "%s:%s,", get_enumeration_name(ec), buf);
218         }
219         fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
220 }  /* gen_enum_type */
221
222 /**
223  * Generates a pointer type
224  *
225  * @param env  the walker environment
226  * @param tp   the type
227  */
228 static void gen_pointer_type(wenv_t *env, ir_type *tp) {
229         stabs_handle *h       = env->h;
230         unsigned     type_num = get_type_number(h, tp);
231         ir_type      *el_tp   = get_pointer_points_to_type(tp);
232         unsigned     el_num   = get_type_number(h, el_tp);
233
234         SET_TYPE_READY(tp);
235         if (! IS_TYPE_READY(el_tp))
236                 waitq_put(env->wq, el_tp);
237         fprintf(h->f, "\t.stabs\t\"%s:t%u=*%u\",%d,0,0,0\n",
238                 get_type_name(tp), type_num, el_num, N_LSYM);
239 }  /* gen_pointer_type */
240
241 /**
242  * Generates a struct/union type
243  *
244  * @param env  the walker environment
245  * @param tp   the type
246  */
247 static void gen_struct_union_type(wenv_t *env, ir_type *tp) {
248         stabs_handle *h       = env->h;
249         unsigned     type_num = get_type_number(h, tp);
250         int          i, n;
251         char         desc = 's';
252
253         SET_TYPE_READY(tp);
254         if (is_Struct_type(tp)) {
255                 desc = 's';
256                 if (get_type_mode(tp) != NULL) {
257                         /* this is a bitfield type, ignore it safely */
258                         return;
259                 }
260         }
261         else if (is_Union_type(tp))
262                 desc = 'u';
263
264         fprintf(h->f, "\t.stabs\t\"%s:Tt%u=%c%d",
265                 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
266
267         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
268                 ir_entity *ent = get_compound_member(tp, i);
269                 ir_type   *mtp = get_entity_type(ent);
270                 int ofs, size;
271
272                 if (! IS_TYPE_READY(mtp))
273                         waitq_put(env->wq, mtp);
274                 ofs  = get_entity_offset(ent);
275                 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
276                         /* this structure is a bitfield, skip */
277                         int i, n;
278
279                         for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
280                                 ir_entity *ent = get_struct_member(mtp, i);
281                                 ir_type *tp = get_entity_type(ent);
282                                 int bofs;
283
284                                 type_num = get_type_number(h, tp);
285                                 size = get_type_size_bits(tp);
286                                 bofs = (ofs + get_entity_offset(ent)) * 8 + get_entity_offset_bits_remainder(ent);
287
288                                 /* name:type, bit offset from the start of the struct', number of bits in the element. */
289                                 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, bofs, size);
290                         }
291                 } else {
292                         /* no bitfield */
293                         type_num = get_type_number(h, mtp);
294                         size = get_type_size_bits(mtp);
295
296                         /* name:type, bit offset from the start of the struct', number of bits in the element. */
297                         fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, ofs, size);
298                 }
299         }
300         fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
301 }  /* gen_struct_type */
302
303 /**
304  * Generates an array type
305  *
306  * @param env  the walker environment
307  * @param tp   the type
308  */
309 static void gen_array_type(wenv_t *env, ir_type *tp) {
310         stabs_handle *h       = env->h;
311         unsigned     type_num = get_type_number(h, tp);
312         int          i, n = get_array_n_dimensions(tp);
313         ir_type      *etp = get_array_element_type(tp);
314         int          *perm;
315
316         SET_TYPE_READY(tp);
317         if (! IS_TYPE_READY(etp))
318                 waitq_put(env->wq, etp);
319
320         NEW_ARR_A(int, perm, n);
321         for (i = 0; i < n; ++i) {
322                 perm[i] = get_array_order(tp, i);
323         }
324         fprintf(h->f, "\t.stabs\t\"%s:t%u=a", get_type_name(tp), type_num);
325
326         for (i = 0; i < n; ++i) {
327                 int dim = perm[i];
328                 long min = get_array_lower_bound_int(tp, dim);
329                 long max = get_array_upper_bound_int(tp, dim);
330
331                 /* FIXME r1 must be integer type, but seems to work for now */
332                 fprintf(h->f, "r1;%ld;%ld;", min, max-1);
333         }
334
335         type_num = get_type_number(h, etp);
336         fprintf(h->f, "%d\",%d,0,0,0\n", type_num, N_LSYM);
337 }  /* gen_array_type */
338
339 /**
340  * Generates a method type
341  *
342  * @param env  the walker environment
343  * @param tp   the type
344  */
345 static void gen_method_type(wenv_t *env, ir_type *tp) {
346         stabs_handle *h       = env->h;
347         unsigned     type_num = get_type_number(h, tp);
348         ir_type *rtp = NULL;
349         unsigned res_type_num;
350         int i, n = get_method_n_ress(tp);
351
352         SET_TYPE_READY(tp);
353         if (n > 0) {
354                 rtp = get_method_res_type(tp, 0);
355                 if (! IS_TYPE_READY(rtp))
356                         waitq_put(env->wq, rtp);
357         }
358         res_type_num = get_type_number(h, rtp);
359
360         fprintf(h->f, "\t.stabs\t\"%s:t%u=f%u", get_type_name(tp), type_num, res_type_num);
361
362         /* handle more than one return type */
363         for (i = 1; i < n; ++i) {
364                 rtp = get_method_res_type(tp, i);
365                 if (! IS_TYPE_READY(rtp))
366                         waitq_put(env->wq, rtp);
367                 res_type_num = get_type_number(h, rtp);
368                 fprintf(h->f, ",%u", res_type_num);
369         }
370         fprintf(h->f, "\",%d,0,0,0\n", N_LSYM);
371 }  /* gen_method_type */
372
373 /**
374  * type-walker: generate declaration for simple types,
375  * put all other types on a wait queue
376  */
377 static void walk_type(type_or_ent *tore, void *ctx)
378 {
379         wenv_t *env = ctx;
380         ir_type  *tp;
381
382         if (get_kind(tore) == k_type) {
383                 tp = (ir_type *)tore;
384
385                 /* ignore the unknown type */
386                 if (tp == firm_unknown_type)
387                         return;
388         } else {
389                 return;
390         }  /* if */
391
392         switch (get_type_tpop_code(tp)) {
393         case tpo_class:
394                 if (tp == get_glob_type()) {
395                         SET_TYPE_READY(tp);
396                         break;
397                 }
398                 /* fall through */
399         case tpo_struct:
400         case tpo_union:
401                 gen_struct_union_type(env, tp);
402                 break;
403
404         case tpo_enumeration:
405                 gen_enum_type(env->h, tp);
406                 break;
407
408         case tpo_primitive:
409                 gen_primitive_type(env->h, tp);
410                 break;
411
412         case tpo_method:
413                 gen_method_type(env, tp);
414                 break;
415
416         case tpo_array:
417                 gen_array_type(env, tp);
418                 break;
419
420         case tpo_pointer:
421                 gen_pointer_type(env, tp);
422                 break;
423
424         case tpo_unknown:
425                 /* the unknown type: ignore */
426                 SET_TYPE_READY(tp);
427                 break;
428         default:
429                 assert(! "Unknown tpop code");
430         }  /* switch */
431 }  /* walk_type */
432
433 /**
434  * generate declaration for all types
435  */
436 static void finish_types(wenv_t *env)
437 {
438         waitq *wq = env->wq;
439         ir_type *tp;
440
441         while (! waitq_empty(wq)) {
442                 tp = waitq_get(wq);
443                 if (IS_TYPE_READY(tp))
444                         continue;
445
446                 switch (get_type_tpop_code(tp)) {
447                 case tpo_method:
448                         gen_method_type(env, tp);
449                         break;
450                 case tpo_class:
451                 case tpo_union:
452                 case tpo_struct:
453                         gen_struct_union_type(env, tp);
454                         break;
455                 case tpo_enumeration:
456                         gen_enum_type(env->h, tp);
457                         break;
458                 case tpo_primitive:
459                         gen_primitive_type(env->h, tp);
460                         break;
461                 case tpo_array:
462                         gen_array_type(env, tp);
463                         break;
464                 case tpo_pointer:
465                         gen_pointer_type(env, tp);
466                         break;
467                 case tpo_unknown:
468                         /* the unknown type: ignore */
469                         SET_TYPE_READY(tp);
470                         break;
471                 default:
472                         assert(! "Unknown tpop code");
473                 }  /* switch */
474         }  /* while */
475 }  /* finish_types */
476
477 /**
478  * generate all types.
479  */
480 static void gen_types(stabs_handle *h) {
481         wenv_t env;
482
483         env.h  = h;
484         env.wq = new_waitq();
485         type_walk(NULL, walk_type, &env);
486         finish_types(&env);
487         del_waitq(env.wq);
488 }  /* gen_types */
489
490
491 /* -------------------------- I/F ----------------------------- */
492
493 /**
494  * start a new source object (compilation unit)
495  */
496 static void stabs_so(dbg_handle *handle, const char *filename) {
497         stabs_handle *h = (stabs_handle *)handle;
498         h->main_file = h->curr_file = filename;
499         fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
500 }  /* stabs_so */
501
502 /**
503  * end an include file
504  */
505 static void stabs_include_end(dbg_handle *handle) {
506         stabs_handle *h = (stabs_handle *)handle;
507         h->curr_file = h->main_file;
508 }  /* stabs_include_end */
509
510 /**
511  * start an include file
512  */
513 static void stabs_include_begin(dbg_handle *handle, const char *filename) {
514         stabs_handle *h = (stabs_handle *)handle;
515         if (h->main_file != h->curr_file)
516                 stabs_include_end(handle);
517         h->curr_file = filename;
518         fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", filename, N_SOL);
519 }  /* stabs_include_begin */
520
521 /**
522  * Main Program
523  */
524 static void stabs_main_program(dbg_handle *handle) {
525         stabs_handle *h = (stabs_handle *)handle;
526         ir_graph *irg = get_irp_main_irg();
527         if (irg) {
528                 fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
529         }
530 }  /* stabs_main_program */
531
532 /**
533  * prints a line number
534  */
535 static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
536         stabs_handle *h = (stabs_handle *)handle;
537         fprintf(h->f, "\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
538 }  /* stabs_line */
539
540 /**
541  * dump the stabs for a method begin
542  */
543 static void stabs_method_begin(dbg_handle *handle, ir_entity *ent, const be_stack_layout_t *layout) {
544         stabs_handle *h = (stabs_handle *)handle;
545         ir_type      *mtp, *rtp;
546         unsigned     type_num;
547         int          i, n, between_size;
548
549         h->cur_ent = ent;
550         h->layout  = layout;
551
552         /* create the method entry */
553         mtp = get_entity_type(ent);
554         if (is_lowered_type(mtp))
555                 mtp = get_associated_type(mtp);
556         if (get_method_n_ress(mtp) > 0)
557                 rtp = get_method_res_type(mtp, 0);
558         else
559                 rtp = NULL;
560         type_num = get_type_number(h, rtp);
561         fprintf(h->f, "\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
562                 get_entity_name(ent),
563                 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
564                 type_num,
565                 N_FUN,
566                 get_entity_ld_name(ent));
567
568         /* create parameter entries */
569         between_size = get_type_size_bytes(layout->between_type);
570         for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
571                 ir_type *ptp      = get_method_param_type(mtp, i);
572         const char *name  = get_method_param_name(mtp, i);
573                 unsigned type_num = get_type_number(h, ptp);
574         char buf[16];
575         int ofs = 0;
576                 ir_entity *stack_ent;
577
578         if (! name) {
579           snprintf(buf, sizeof(buf), "arg%d", i);
580           name = buf;
581         }
582                 /* check if this parameter has a stack entity. If it has, it
583                    it transmitted on the stack, else in a register */
584                 stack_ent = layout->param_map[i];
585                 if (stack_ent) {
586                         ofs = get_entity_offset(stack_ent) + between_size;
587                 }
588                 fprintf(h->f, "\t.stabs\t\"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
589         }
590 }  /* stabs_method_begin */
591
592 /**
593  * dump the stabs for a method end
594  */
595 static void stabs_method_end(dbg_handle *handle) {
596         stabs_handle            *h = (stabs_handle *)handle;
597         ir_entity               *ent = h->cur_ent;
598         const be_stack_layout_t *layout = h->layout;
599         const char              *ld_name = get_entity_ld_name(ent);
600         int                     i, n, frame_size;
601         static unsigned         scope_nr = 0;
602
603         /* create entries for automatic variables on the stack */
604         frame_size = get_type_size_bytes(layout->frame_type);
605         for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
606                 ir_entity *ent = get_compound_member(layout->frame_type, i);
607                 ir_type *tp;
608                 int ofs;
609                 unsigned type_num;
610
611                 /* ignore spill slots and other helper objects */
612                 if (is_entity_compiler_generated(ent))
613                         continue;
614
615                 tp = get_entity_type(ent);
616                 /* should not happen in backend but ... */
617                 if (is_Method_type(tp))
618                         continue;
619                 type_num = get_type_number(h, tp);
620                 ofs      = -frame_size + get_entity_offset(ent);
621
622                 fprintf(h->f, "\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
623                         get_entity_name(ent), type_num, N_LSYM, ofs);
624         }
625         /* we need a lexical block here */
626         fprintf(h->f, "\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
627         fprintf(h->f, "\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
628         fprintf(h->f, ".Lscope%u:\n", scope_nr);
629         ++scope_nr;
630
631         h->cur_ent = NULL;
632         h->layout  = NULL;
633 }  /* stabs_method_end */
634
635 /**
636  * dump types
637  */
638 static void stabs_types(dbg_handle *handle) {
639         stabs_handle *h = (stabs_handle *)handle;
640
641         /* allocate the zero for the void type */
642         h->next_type_nr++;
643         gen_void_type(h);
644         gen_types(h);
645 }  /* stabs_types */
646
647 /**
648  * dump a variable in the global type
649  */
650 static void stabs_variable(dbg_handle *handle, struct obstack *obst, ir_entity *ent) {
651         stabs_handle *h = (stabs_handle *)handle;
652         unsigned tp_num = get_type_number(h, get_entity_type(ent));
653         char buf[1024];
654
655         if (get_entity_visibility(ent) == visibility_external_visible) {
656                 /* a global variable */
657                 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
658                         get_entity_name(ent), tp_num, N_GSYM);
659         } else { /* some kind of local */
660                 ir_variability variability = get_entity_variability(ent);
661                 int kind = N_STSYM;
662
663                 if (variability == variability_uninitialized)
664                         kind = N_LCSYM;
665                 else if (variability == variability_constant)
666                         kind = N_ROSYM;
667                 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
668                         get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
669         }
670         buf[sizeof(buf) - 1] = '\0';
671
672         if (obst)
673                 obstack_printf(obst, "%s", buf);
674         else
675                 fprintf(h->f, "%s", buf);
676 }  /* stabs_variable */
677
678 /**
679  * Close the stabs handler.
680  */
681 static void stabs_close(dbg_handle *handle) {
682         stabs_handle *h = (stabs_handle *)handle;
683         pmap_destroy(h->type_map);
684         free(h);
685 }  /* stabs_close */
686
687 /** The stabs operations. */
688 static const debug_ops stabs_ops = {
689         stabs_close,
690         stabs_so,
691     stabs_include_begin,
692     stabs_include_end,
693         stabs_main_program,
694         stabs_method_begin,
695         stabs_method_end,
696         stabs_line,
697         stabs_types,
698         stabs_variable
699 };
700
701 /* Opens the NULL handler */
702 dbg_handle *be_nulldbg_open(void) {
703         return NULL;
704 }  /* be_nulldbg_open */
705
706 /* Opens a stabs handler */
707 dbg_handle *be_stabs_open(FILE *out) {
708         stabs_handle *h = xmalloc(sizeof(*h));
709
710         h->base.ops     = &stabs_ops;
711         h->f            = out;
712         h->cur_ent      = NULL;
713         h->layout       = NULL;
714         h->next_type_nr = 0;
715         h->type_map     = pmap_create_ex(64);
716         h->main_file    = NULL;
717         h->curr_file    = NULL;
718
719         return &h->base;
720 }  /* stabs_open */
721
722 /** close a debug handler. */
723 void be_dbg_close(dbg_handle *h) {
724         if (h && h->ops->close)
725                 h->ops->close(h);
726 }  /* be_dbg_close */
727
728 /**
729  * start a new source object (compilation unit)
730  */
731 void be_dbg_so(dbg_handle *h, const char *filename) {
732         if (h && h->ops->so)
733                 h->ops->so(h, filename);
734 }  /* be_dbg_begin */
735
736 /**
737  * start an include file
738  */
739 void be_dbg_include_begin(dbg_handle *h, const char *filename) {
740         if (h && h->ops->include_begin)
741                 h->ops->include_begin(h, filename);
742 }  /* stabs_include_begin */
743
744 /**
745  * end an include file
746  */
747 void be_dbg_include_end(dbg_handle *h) {
748         if (h && h->ops->include_end)
749                 h->ops->include_end(h);
750 }  /* stabs_include_end */
751
752 /**
753  * Main program
754  */
755 void be_dbg_main_program(dbg_handle *h) {
756         if (h && h->ops->main_program)
757                 h->ops->main_program(h);
758 }  /* be_dbg_main_program */
759
760 /** debug for a method begin */
761 void be_dbg_method_begin(dbg_handle *h, ir_entity *ent, const be_stack_layout_t *layout) {
762         if (h && h->ops->method_begin)
763                 h->ops->method_begin(h, ent, layout);
764 }  /* be_dbg_method_begin */
765
766 /** debug for a method end */
767 void be_dbg_method_end(dbg_handle *h) {
768         if (h && h->ops->method_end)
769                 h->ops->method_end(h);
770 }  /* be_dbg_method_end */
771
772 /** debug for line number */
773 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
774         if (h && h->ops->line)
775                 h->ops->line(h, lineno, address);
776 }  /* be_dbg_line */
777
778 /** dump types */
779 void be_dbg_types(dbg_handle *h) {
780         if (h && h->ops->types)
781                 h->ops->types(h);
782 }  /* be_dbg_types */
783
784 /** dump a global */
785 void be_dbg_variable(dbg_handle *h, struct obstack *obst, ir_entity *ent) {
786         if (h && h->ops->variable)
787                 h->ops->variable(h, obst, ent);
788 }  /* be_dbg_variable */