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