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