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