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