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