added support for static variables
[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 "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         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 } stabs_handle;
106
107 /**
108  * Returns the stabs type number of a Firm type.
109  */
110 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
111         pmap_entry *entry;
112
113         if (tp == NULL) {
114                 /* map to the void type */
115                 return 0;
116         }
117         entry = pmap_find(h->type_map, tp);
118         return entry ? PTR_TO_INT(entry->value) : 0;
119 }  /* get_type_number */
120
121 /**
122  * Assign a stabs type number to a Firm type.
123  */
124 static unsigned assign_type_number(stabs_handle *h, ir_type *tp) {
125         unsigned num;
126         if (tp == NULL) {
127                 /* map to the void type */
128                 return 0;
129         }
130         num = h->next_type_nr++;
131         pmap_insert(h->type_map, tp, INT_TO_PTR(num));
132         return num;
133 }  /* assign_type_number */
134
135 /**
136  * generate the void type.
137  */
138 static void gen_void_type(stabs_handle *h) {
139         fprintf(h->f, "\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
140 }  /* gen_void_type */
141
142 typedef struct walker_env {
143         stabs_handle *h;
144         waitq        *wq;
145 } wenv_t;
146
147 /* a type is not ready: put it on the wait queue */
148 #define SET_TYPE_NOT_READY(wq, tp) \
149   do { \
150     set_type_link(tp, (void *)1);  \
151     waitq_put(wq, tp);             \
152   } while(0)
153
154 /* a the is ready */
155 #define SET_TYPE_READY(tp)     set_type_link(tp, NULL)
156
157 /* check whether a type is ready */
158 #define IS_TYPE_READY(tp)      (get_type_link(tp) == NULL)
159
160 #ifdef EXPLICITE_PTR_TYPES
161 #define SKIP_PTR(tp)  tp
162 #else
163 #define SKIP_PTR(tp)   (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
164 #endif
165
166 /**
167  * Generates a primitive type.
168  *
169  * @param h    the stabs handle
170  * @param tp   the type
171  */
172 static void gen_primitive_type(stabs_handle *h, ir_type *tp)
173 {
174         ir_mode *mode = get_type_mode(tp);
175         unsigned type_num;
176
177         if (mode == mode_T) {
178     /* jack and FirmJC compiler use mode_T for the void type.
179                 Ignore it here as it's name is remapped to "void". */
180                 return;
181         }  /* if */
182
183         if (0 && get_mode_size_bits(mode) & 7) {
184                 /* this is a bitfield type, ignore it */
185                 return;
186         }  /* if */
187
188         type_num = assign_type_number(h, tp);
189
190         if (mode_is_int(mode)) {
191                 char buf[64];
192                 fprintf(h->f, "\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
193                 tarval_snprintf(buf, sizeof(buf), get_mode_min(mode));
194                 fprintf(h->f, "%s;", buf);
195                 tarval_snprintf(buf, sizeof(buf), get_mode_max(mode));
196                 fprintf(h->f, "%s;\",%d,0,0,0\n", buf, N_LSYM);
197         } else if (mode_is_float(mode)) {
198                 int size = get_type_size_bytes(tp);
199                 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);
200         }
201 }  /* gen_primitive_type */
202
203 /**
204  * Generates an enum type
205  *
206  * @param h    the stabs handle
207  * @param tp   the type
208  */
209 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
210         unsigned type_num = assign_type_number(h, tp);
211         int i, n;
212
213         fprintf(h->f, "\t.stabs\t\"%s:T%u=e", get_type_name(tp), type_num);
214         for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
215                 ir_enum_const *ec = get_enumeration_const(tp, i);
216                 char buf[64];
217
218                 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
219                 fprintf(h->f, "%s:%s,", get_enumeration_name(ec), buf);
220         }
221         fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
222 }  /* gen_enum_type */
223
224 /**
225  * Generates a pointer type
226  *
227  * @param h    the stabs handle
228  * @param tp   the type
229  */
230 static void gen_pointer_type(stabs_handle *h, ir_type *tp) {
231         unsigned type_num = assign_type_number(h, tp);
232         unsigned el_num   = get_type_number(h, get_pointer_points_to_type(tp));
233
234         fprintf(h->f, "\t.stabs\t\"%s:t%u=*%u\",%d,0,0,0\n",
235                 get_type_name(tp), type_num, el_num, N_LSYM);
236 }  /* gen_pointer_type */
237
238 /**
239  * Generates a struct/union type
240  *
241  * @param h    the stabs handle
242  * @param tp   the type
243  */
244 static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
245         unsigned type_num = assign_type_number(h, tp);
246         int i, n;
247         char desc;
248
249         if (is_Struct_type(tp)) {
250                 desc = 's';
251                 if (get_type_mode(tp) != NULL) {
252                         /* this is a bitfield type, ignore it safely */
253                         return;
254                 }
255         }
256         else
257                 desc = 'u';
258
259         fprintf(h->f, "\t.stabs\t\"%s:T%u=%c%d",
260                 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
261
262         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
263                 entity  *ent = get_compound_member(tp, i);
264                 ir_type *mtp = get_entity_type(ent);
265                 int ofs, size;
266
267                 ofs  = get_entity_offset_bits(ent);
268                 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
269                         /* this structure is a bitfield, skip */
270                         int i, n;
271
272                         for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
273                                 entity *ent = get_struct_member(mtp, i);
274                                 ir_type *tp = get_entity_type(ent);
275                                 int bofs;
276
277                                 type_num = get_type_number(h, tp);
278                                 size = get_type_size_bits(tp);
279                                 bofs = ofs + get_entity_offset_bits(ent);
280
281                                 /* name:type, bit offset from the start of the struct', number of bits in the element. */
282                                 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, bofs, size);
283                         }
284                 } else {
285                         /* no bitfield */
286                         type_num = get_type_number(h, mtp);
287                         size = get_type_size_bits(mtp);
288
289                         /* name:type, bit offset from the start of the struct', number of bits in the element. */
290                         fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, ofs, size);
291                 }
292         }
293         fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
294 }  /* gen_struct_type */
295
296 /**
297  * Generates an array type
298  *
299  * @param h    the stabs handle
300  * @param tp   the type
301  */
302 static void gen_array_type(stabs_handle *h, ir_type *tp) {
303         unsigned type_num = assign_type_number(h, tp);
304         int i, n = get_array_n_dimensions(tp);
305         int *perm;
306         ir_type *etp;
307
308         NEW_ARR_A(int, perm, n);
309         for (i = 0; i < n; ++i) {
310                 perm[i] = get_array_order(tp, i);
311         }
312         fprintf(h->f, "\t.stabs\t\"%s:t%u=a", get_type_name(tp), type_num);
313
314         for (i = 0; i < n; ++i) {
315                 int dim = perm[i];
316                 long min = get_array_lower_bound_int(tp, dim);
317                 long max = get_array_upper_bound_int(tp, dim);
318
319                 /* FIXME r1 must be integer type, but seems to work for now */
320                 fprintf(h->f, "r1;%ld;%ld;", min, max-1);
321         }
322
323         etp = get_array_element_type(tp);
324         type_num = get_type_number(h, etp);
325         fprintf(h->f, "%d\",%d,0,0,0\n", type_num, N_LSYM);
326 }  /* gen_array_type */
327
328 /**
329  * Generates a method type
330  *
331  * @param h    the stabs handle
332  * @param tp   the type
333  */
334 static void gen_method_type(stabs_handle *h, ir_type *tp) {
335         unsigned type_num = assign_type_number(h, tp);
336         ir_type *rtp = NULL;
337         unsigned res_type_num;
338         int i, n = get_method_n_ress(tp);
339
340         if (n > 0)
341                 rtp = get_method_res_type(tp, 0);
342         res_type_num = get_type_number(h, rtp);
343
344         fprintf(h->f, "\t.stabs\t\"%s:t%u=f%u", get_type_name(tp), type_num, res_type_num);
345
346         /* handle more than one return type */
347         for (i = 1; i < n; ++i) {
348                 rtp = get_method_res_type(tp, i);
349                 res_type_num = get_type_number(h, rtp);
350                 fprintf(h->f, ",%u", res_type_num);
351         }
352         fprintf(h->f, "\",%d,0,0,0\n", N_LSYM);
353 }  /* gen_method_type */
354
355 /**
356  * type-walker: generate declaration for simple types,
357  * put all other types on a wait queue
358  */
359 static void walk_type(type_or_ent *tore, void *ctx)
360 {
361         wenv_t *env = ctx;
362         ir_type  *tp;
363
364         if (get_kind(tore) == k_type) {
365                 tp = (ir_type *)tore;
366
367                 /* ignore the unknown type */
368                 if (tp == firm_unknown_type)
369                         return;
370         } else {
371                 return;
372         }  /* if */
373
374         switch (get_type_tpop_code(tp)) {
375         case tpo_class:
376                 if (tp == get_glob_type()) {
377                         SET_TYPE_READY(tp);
378                         break;
379                 }
380                 /* fall through */
381         case tpo_struct:
382         case tpo_union:
383                 SET_TYPE_NOT_READY(env->wq, tp);
384                 break;
385
386         case tpo_enumeration:
387                 gen_enum_type(env->h, tp);
388                 SET_TYPE_READY(tp);
389                 break;
390
391         case tpo_primitive:
392                 gen_primitive_type(env->h, tp);
393                 SET_TYPE_READY(tp);
394                 break;
395
396         case tpo_method:
397         case tpo_array:
398                 SET_TYPE_NOT_READY(env->wq, tp);
399                 break;
400
401         case tpo_pointer:
402                 /* must construct the pointer type */
403                 SET_TYPE_NOT_READY(env->wq, tp);
404                 break;
405
406         case tpo_unknown:
407                 /* the unknown type: ignore */
408                 break;
409         default:
410                 assert(! "Unknown tpop code");
411         }  /* switch */
412 }  /* walk_type */
413
414 /**
415  * check, if a method type can be generated
416  */
417 static int is_method_type_ready(ir_type *tp)
418 {
419   int i;
420   ir_type *etp;
421
422   for (i = get_method_n_ress(tp) - 1; i >= 0; --i) {
423     etp = SKIP_PTR(get_method_res_type(tp, i));
424
425     if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
426       return 0;
427   }  /* for */
428
429   for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
430     etp = SKIP_PTR(get_method_param_type(tp, i));
431
432     if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
433       return 0;
434   }  /* for */
435   return 1;
436 }  /* is_method_type_ready */
437
438 /**
439  * check, whether a compound type can be generated
440  */
441 static int is_compound_type_ready(ir_type *tp)
442 {
443   int i;
444   ir_type *etp;
445
446   for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
447     etp = SKIP_PTR(get_entity_type(get_compound_member(tp, i)));
448
449     if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
450       return 0;
451   }  /* for */
452   return 1;
453 }  /* is_compound_type_ready */
454
455 /**
456  * generate declaration for all types
457  */
458 static void finish_types(stabs_handle *h, waitq *wq)
459 {
460   ir_type *tp;
461
462   while (! waitq_empty(wq)) {
463     tp = waitq_get(wq);
464
465     switch (get_type_tpop_code(tp)) {
466     case tpo_method:
467       if (is_method_type_ready(tp)) {
468         gen_method_type(h, tp);
469         SET_TYPE_READY(tp);
470         continue;
471       }  /* if */
472       break;
473     case tpo_class:
474     case tpo_union:
475     case tpo_struct:
476       if (is_compound_type_ready(tp)) {
477         gen_struct_union_type(h, tp);
478         SET_TYPE_READY(tp);
479         continue;
480       }  /* if */
481       break;
482     case tpo_array:
483       if (IS_TYPE_READY(get_array_element_type(tp))) {
484         gen_array_type(h, tp);
485         SET_TYPE_READY(tp);
486         continue;
487       }  /* if */
488       break;
489     case tpo_pointer:
490       if (IS_TYPE_READY(get_pointer_points_to_type(tp))) {
491         gen_pointer_type(h, tp);
492         SET_TYPE_READY(tp);
493         continue;
494       }  /* if */
495       break;
496     default:
497       assert(! "Unknown tpop code");
498     }  /* switch */
499
500     /* still not ready, defer type output */
501     pdeq_putr(wq, tp);
502   }  /* while */
503 }  /* finish_types */
504
505 /**
506  * generate all types.
507  */
508 static void gen_types(stabs_handle *h) {
509         wenv_t env;
510
511         env.h  = h;
512         env.wq = new_waitq();
513         type_walk(NULL, walk_type, &env);
514         finish_types(h, env.wq);
515         del_waitq(env.wq);
516 }  /* gen_types */
517
518
519 /* -------------------------- I/F ----------------------------- */
520
521 /**
522  * start a new source object (compilation unit)
523  */
524 static void stabs_so(dbg_handle *handle, const char *filename) {
525         stabs_handle *h = (stabs_handle *)handle;
526         fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
527 }  /* stabs_so */
528
529 /**
530  * Main Program
531  */
532 static void stabs_main_program(dbg_handle *handle) {
533         stabs_handle *h = (stabs_handle *)handle;
534         ir_graph *irg = get_irp_main_irg();
535         if (irg) {
536                 fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
537         }
538 }  /* stabs_main_program */
539
540 /**
541  * prints a line number
542  */
543 static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
544         stabs_handle *h = (stabs_handle *)handle;
545         fprintf(h->f, "\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
546 }  /* stabs_line */
547
548 /**
549  * dump the stabs for a method begin
550  */
551 static void stabs_method_begin(dbg_handle *handle, entity *ent, const be_stack_layout_t *layout) {
552         stabs_handle *h = (stabs_handle *)handle;
553         ir_type      *mtp, *rtp;
554         unsigned     type_num;
555         int          i, n, between_size;
556
557         h->cur_ent = ent;
558         h->layout  = layout;
559
560         /* create the method entry */
561         mtp = get_entity_type(ent);
562         if (is_lowered_type(mtp))
563                 mtp = get_associated_type(mtp);
564         if (get_method_n_ress(mtp) > 0)
565                 rtp = get_method_res_type(mtp, 0);
566         else
567                 rtp = NULL;
568         type_num = get_type_number(h, rtp);
569         fprintf(h->f, "\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
570                 get_entity_name(ent),
571                 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
572                 type_num,
573                 N_FUN,
574                 get_entity_ld_name(ent));
575
576         /* create parameter entries */
577         between_size = get_type_size_bytes(layout->between_type);
578         for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
579                 ir_type *ptp      = get_method_param_type(mtp, i);
580         const char *name  = get_method_param_name(mtp, i);
581                 unsigned type_num = get_type_number(h, ptp);
582         char buf[16];
583         int ofs = 0;
584                 entity *stack_ent;
585
586         if (! name) {
587           snprintf(buf, sizeof(buf), "arg%d", i);
588           name = buf;
589         }
590                 /* check if this parameter has a stack entity. If it has, it
591                    it transmitted on the stack, else in a register */
592                 stack_ent = layout->param_map[i];
593                 if (stack_ent) {
594                         ofs = get_entity_offset_bytes(stack_ent) + between_size;
595                 }
596                 fprintf(h->f, "\t.stabs\t\"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
597         }
598 }  /* stabs_method_begin */
599
600 /**
601  * dump the stabs for a method end
602  */
603 static void stabs_method_end(dbg_handle *handle) {
604         stabs_handle            *h = (stabs_handle *)handle;
605         entity                  *ent = h->cur_ent;
606         const be_stack_layout_t *layout = h->layout;
607         const char              *ld_name = get_entity_ld_name(ent);
608         int                     i, n, frame_size;
609         static unsigned         scope_nr = 0;
610
611         /* create entries for automatic variables on the stack */
612         frame_size = get_type_size_bytes(layout->frame_type);
613         for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
614                 entity *ent = get_compound_member(layout->frame_type, i);
615                 ir_type *tp;
616                 int ofs;
617                 unsigned type_num;
618
619                 /* ignore spill slots and other helper objects */
620                 if (is_entity_compiler_generated(ent))
621                         continue;
622
623                 tp = get_entity_type(ent);
624                 /* should not happen in backend but ... */
625                 if (is_Method_type(tp))
626                         continue;
627                 type_num = get_type_number(h, tp);
628                 ofs      = -frame_size + get_entity_offset_bytes(ent);
629
630                 fprintf(h->f, "\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
631                         get_entity_name(ent), type_num, N_LSYM, ofs);
632         }
633         /* we need a lexical block here */
634         fprintf(h->f, "\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
635         fprintf(h->f, "\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
636         fprintf(h->f, ".Lscope%u:\n", scope_nr);
637         ++scope_nr;
638
639         h->cur_ent = NULL;
640         h->layout  = NULL;
641 }  /* stabs_method_end */
642
643 /**
644  * dump types
645  */
646 static void stabs_types(dbg_handle *handle) {
647         stabs_handle *h = (stabs_handle *)handle;
648
649         /* allocate the zero for the void type */
650         h->next_type_nr++;
651         gen_void_type(h);
652         gen_types(h);
653 }  /* stabs_types */
654
655 /**
656  * dump a variable in the global type
657  */
658 static void stabs_variable(dbg_handle *handle, struct obstack *obst, entity *ent) {
659         stabs_handle *h = (stabs_handle *)handle;
660         unsigned tp_num = get_type_number(h, get_entity_type(ent));
661         char buf[1024];
662
663         if (get_entity_visibility(ent) == visibility_external_visible) {
664                 /* a global variable */
665                 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
666                         get_entity_name(ent), tp_num, N_GSYM);
667         } else { /* some kind of local */
668                 ir_variability variability = get_entity_variability(ent);
669                 int kind = N_STSYM;
670
671                 if (variability == variability_uninitialized)
672                         kind = N_LCSYM;
673                 else if (variability == variability_constant)
674                         kind == N_ROSYM;
675                 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
676                         get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
677         }
678         buf[sizeof(buf) - 1] = '\0';
679
680         if (obst)
681                 obstack_printf(obst, "%s", buf);
682         else
683                 fprintf(h->f, "%s", buf);
684 }  /* stabs_variable */
685
686 /**
687  * Close the stabs handler.
688  */
689 static void stabs_close(dbg_handle *handle) {
690         stabs_handle *h = (stabs_handle *)handle;
691         pmap_destroy(h->type_map);
692         free(h);
693 }  /* stabs_close */
694
695 /** The stabs operations. */
696 static const debug_ops stabs_ops = {
697         stabs_close,
698         stabs_so,
699         stabs_main_program,
700         stabs_method_begin,
701         stabs_method_end,
702         stabs_line,
703         stabs_types,
704         stabs_variable
705 };
706
707 /* Opens the NULL handler */
708 dbg_handle *be_nulldbg_open(void) {
709         return NULL;
710 }  /* be_nulldbg_open */
711
712 /* Opens a stabs handler */
713 dbg_handle *be_stabs_open(FILE *out) {
714         stabs_handle *h = xmalloc(sizeof(*h));
715
716         h->base.ops     = &stabs_ops;
717         h->f            = out;
718         h->cur_ent      = NULL;
719         h->layout       = NULL;
720         h->next_type_nr = 0;
721         h->type_map     = pmap_create_ex(64);
722         return &h->base;
723 }  /* stabs_open */
724
725 /** close a debug handler. */
726 void be_dbg_close(dbg_handle *h) {
727         if (h && h->ops->close)
728                 h->ops->close(h);
729 }  /* be_dbg_close */
730
731 /**
732  * start a new source object (compilation unit)
733  */
734 void be_dbg_so(dbg_handle *h, const char *filename) {
735         if (h && h->ops->so)
736                 h->ops->so(h, filename);
737 }  /* be_dbg_begin */
738
739 /**
740  * Main program
741  */
742 void be_dbg_main_program(dbg_handle *h) {
743         if (h && h->ops->main_program)
744                 h->ops->main_program(h);
745 }  /* be_dbg_main_program */
746
747 /** debug for a method begin */
748 void be_dbg_method_begin(dbg_handle *h, entity *ent, const be_stack_layout_t *layout) {
749         if (h && h->ops->method_begin)
750                 h->ops->method_begin(h, ent, layout);
751 }  /* be_dbg_method_begin */
752
753 /** debug for a method end */
754 void be_dbg_method_end(dbg_handle *h) {
755         if (h && h->ops->method_end)
756                 h->ops->method_end(h);
757 }  /* be_dbg_method_end */
758
759 /** debug for line number */
760 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
761         if (h && h->ops->line)
762                 h->ops->line(h, lineno, address);
763 }  /* be_dbg_line */
764
765 /** dump types */
766 void be_dbg_types(dbg_handle *h) {
767         if (h && h->ops->types)
768                 h->ops->types(h);
769 }  /* be_dbg_types */
770
771 /** dump a global */
772 void be_dbg_variable(dbg_handle *h, struct obstack *obst, entity *ent) {
773         if (h && h->ops->variable)
774                 h->ops->variable(h, obst, ent);
775 }  /* be_dbg_variable */