- moved the imul mem,imm32 splitting into peephole optimizations
[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  * Generates a primitive type.
193  *
194  * @param h    the stabs handle
195  * @param tp   the type
196  */
197 static void gen_primitive_type(stabs_handle *h, ir_type *tp) {
198         ir_mode *mode = get_type_mode(tp);
199         unsigned type_num;
200
201         SET_TYPE_READY(tp);
202         if (mode == mode_T) {
203                 /* firmcc, jack and the FirmJC compiler use mode_T for the void type.
204                 Ignore it here as it's name is remapped to "void". */
205                 map_to_void(h, tp);
206                 return;
207         }  /* if */
208
209         if (0 && get_mode_size_bits(mode) & 7) {
210                 /* this is a bitfield type, ignore it */
211                 return;
212         }  /* if */
213
214         type_num = get_type_number(h, tp);
215
216         if (mode_is_int(mode)) {
217                 be_emit_irprintf("\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
218                 be_emit_tarval(get_mode_min(mode));
219                 be_emit_char(';');
220                 be_emit_tarval(get_mode_max(mode));
221                 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
222                 be_emit_write_line();
223         } else if (mode_is_float(mode)) {
224                 int size = get_type_size_bytes(tp);
225                 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);
226                 be_emit_write_line();
227         }
228 }  /* gen_primitive_type */
229
230 /**
231  * Generates an enum type
232  *
233  * @param h    the stabs handle
234  * @param tp   the type
235  */
236 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
237         unsigned type_num = get_type_number(h, tp);
238         int i, n;
239
240         SET_TYPE_READY(tp);
241         be_emit_irprintf("\t.stabs\t\"%s:T%u=e", get_type_name(tp), type_num);
242         for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
243                 ir_enum_const *ec = get_enumeration_const(tp, i);
244                 char buf[64];
245
246                 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
247                 be_emit_irprintf("%s:%s,", get_enumeration_name(ec), buf);
248         }
249         be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
250         be_emit_write_line();
251 }  /* gen_enum_type */
252
253 /**
254  * print a pointer type
255  */
256 void print_pointer_type(stabs_handle *h, ir_type *tp, int local) {
257         unsigned     type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
258         ir_type      *el_tp   = get_pointer_points_to_type(tp);
259         unsigned     el_num   = get_type_number(h, el_tp);
260
261         be_emit_irprintf("%u=*%u", type_num, el_num);
262 }
263
264 /**
265  * Generates a pointer type
266  *
267  * @param env  the walker environment
268  * @param tp   the type
269  */
270 static void gen_pointer_type(wenv_t *env, ir_type *tp) {
271         stabs_handle *h       = env->h;
272         ir_type      *el_tp   = get_pointer_points_to_type(tp);
273
274         SET_TYPE_READY(tp);
275         if (! IS_TYPE_READY(el_tp))
276                 waitq_put(env->wq, el_tp);
277
278         be_emit_irprintf("\t.stabs\t\"%s:t", get_type_name(tp));
279         print_pointer_type(h, tp, 0);
280         be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
281         be_emit_write_line();
282 }  /* gen_pointer_type */
283
284 /**
285  * print an array type
286  */
287 static void print_array_type(stabs_handle *h, ir_type *tp, int local) {
288         ir_type      *etp     = get_array_element_type(tp);
289         int          i, n     = get_array_n_dimensions(tp);
290         unsigned     type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
291         int          *perm;
292
293         be_emit_irprintf("%u=a", type_num);
294         NEW_ARR_A(int, perm, n);
295         for (i = 0; i < n; ++i) {
296                 perm[i] = get_array_order(tp, i);
297         }
298
299         for (i = 0; i < n; ++i) {
300                 int dim = perm[i];
301
302                 if (is_Const(get_array_lower_bound(tp, dim)) && is_Const(get_array_upper_bound(tp, dim))) {
303                         long min = get_array_lower_bound_int(tp, dim);
304                         long max = get_array_upper_bound_int(tp, dim);
305
306                         /* FIXME r1 must be integer type, but seems to work for now */
307                         be_emit_irprintf("r1;%ld;%ld;", min, max-1);
308                 }
309         }
310
311         type_num = get_type_number(h, etp);
312         be_emit_irprintf("%d", type_num);
313 }
314
315 /**
316  * Generates an array type
317  *
318  * @param env  the walker environment
319  * @param tp   the type
320  */
321 static void gen_array_type(wenv_t *env, ir_type *tp) {
322         stabs_handle *h   = env->h;
323         ir_type      *etp = get_array_element_type(tp);
324
325         SET_TYPE_READY(tp);
326         if (! IS_TYPE_READY(etp))
327                 waitq_put(env->wq, etp);
328
329         be_emit_irprintf("\t.stabs\t\"%s:t", get_type_name(tp));
330
331         print_array_type(h, tp, 0);
332
333         be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
334         be_emit_write_line();
335 }  /* gen_array_type */
336
337 /**
338  * Generates a struct/union type
339  *
340  * @param env  the walker environment
341  * @param tp   the type
342  */
343 static void gen_struct_union_type(wenv_t *env, ir_type *tp) {
344         stabs_handle *h       = env->h;
345         unsigned     type_num = get_type_number(h, tp);
346         int          i, n;
347         char         desc = 's';
348
349         SET_TYPE_READY(tp);
350         if (is_Struct_type(tp)) {
351                 desc = 's';
352                 if (get_type_mode(tp) != NULL) {
353                         /* this is a bitfield type, ignore it safely */
354                         return;
355                 }
356         }
357         else if (is_Union_type(tp))
358                 desc = 'u';
359
360         be_emit_irprintf("\t.stabs\t\"%s:Tt%u=%c%d",
361                 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
362
363         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
364                 ir_entity *ent = get_compound_member(tp, i);
365                 ir_type   *mtp = get_entity_type(ent);
366                 int ofs;
367                 unsigned size;
368
369                 if (! IS_TYPE_READY(mtp))
370                         waitq_put(env->wq, mtp);
371                 ofs  = get_entity_offset(ent);
372                 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
373                         /* this structure is a bitfield, skip */
374                         int i, n;
375
376                         for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
377                                 ir_entity *ent = get_struct_member(mtp, i);
378                                 ir_type *tp = get_entity_type(ent);
379                                 int bofs;
380
381                                 type_num = get_type_number(h, tp);
382                                 size = get_type_size_bytes(tp) * 8;
383                                 bofs = (ofs + get_entity_offset(ent)) * 8 + get_entity_offset_bits_remainder(ent);
384
385                                 /* name:type, bit offset from the start of the struct', number of bits in the element. */
386                                 be_emit_irprintf("%s:%u,%d,%u;", get_entity_name(ent), type_num, bofs, size);
387                         }
388                 } else {
389                         /* no bitfield */
390                         be_emit_irprintf("%s:", get_entity_name(ent));
391
392                         if (is_Array_type(mtp)) {
393                                 /* use a local array definition */
394                                 print_array_type(h, mtp, 1);
395                         } else if (is_Pointer_type(mtp)) {
396                                 /* use local pointer definition */
397                                 print_pointer_type(h, mtp, 1);
398                         } else {
399                                 type_num = get_type_number(h, mtp);
400
401                                 /* name:type, bit offset from the start of the struct', number of bits in the element. */
402                                 be_emit_irprintf("%u", type_num);
403                         }
404                         size = get_type_size_bytes(mtp) * 8;
405                         be_emit_irprintf(",%d,%u;", ofs * 8, size);
406                 }
407         }
408         be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
409         be_emit_write_line();
410 }  /* gen_struct_type */
411
412 /**
413  * Generates a method type
414  *
415  * @param env  the walker environment
416  * @param tp   the type
417  */
418 static void gen_method_type(wenv_t *env, ir_type *tp) {
419         stabs_handle *h       = env->h;
420         unsigned     type_num = get_type_number(h, tp);
421         ir_type *rtp = NULL;
422         unsigned res_type_num;
423         int i, n = get_method_n_ress(tp);
424
425         SET_TYPE_READY(tp);
426         if (n > 0) {
427                 rtp = get_method_res_type(tp, 0);
428                 if (! IS_TYPE_READY(rtp))
429                         waitq_put(env->wq, rtp);
430         }
431         res_type_num = get_type_number(h, rtp);
432
433         be_emit_irprintf("\t.stabs\t\"%s:t%u=f%u", get_type_name(tp), type_num, res_type_num);
434
435         /* handle more than one return type */
436         for (i = 1; i < n; ++i) {
437                 rtp = get_method_res_type(tp, i);
438                 if (! IS_TYPE_READY(rtp))
439                         waitq_put(env->wq, rtp);
440                 res_type_num = get_type_number(h, rtp);
441                 be_emit_irprintf(",%u", res_type_num);
442         }
443         be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
444         be_emit_write_line();
445 }  /* gen_method_type */
446
447 /**
448  * type-walker: generate declaration for simple types,
449  * put all other types on a wait queue
450  */
451 static void walk_type(type_or_ent *tore, void *ctx)
452 {
453         wenv_t *env = ctx;
454         ir_type  *tp;
455
456         if (get_kind(tore) == k_type) {
457                 tp = (ir_type *)tore;
458
459                 /* ignore the unknown type */
460                 if (tp == firm_unknown_type)
461                         return;
462         } else {
463                 return;
464         }  /* if */
465
466         switch (get_type_tpop_code(tp)) {
467         case tpo_class:
468                 if (tp == get_glob_type()) {
469                         SET_TYPE_READY(tp);
470                         break;
471                 }
472                 /* fall through */
473         case tpo_struct:
474         case tpo_union:
475                 gen_struct_union_type(env, tp);
476                 break;
477
478         case tpo_enumeration:
479                 gen_enum_type(env->h, tp);
480                 break;
481
482         case tpo_primitive:
483                 gen_primitive_type(env->h, tp);
484                 break;
485
486         case tpo_method:
487                 gen_method_type(env, tp);
488                 break;
489
490         case tpo_array:
491                 gen_array_type(env, tp);
492                 break;
493
494         case tpo_pointer:
495                 gen_pointer_type(env, tp);
496                 break;
497
498         case tpo_unknown:
499                 /* the unknown type: ignore */
500                 SET_TYPE_READY(tp);
501                 break;
502         default:
503                 assert(! "Unknown tpop code");
504         }  /* switch */
505 }  /* walk_type */
506
507 /**
508  * generate declaration for all types
509  */
510 static void finish_types(wenv_t *env)
511 {
512         waitq *wq = env->wq;
513         ir_type *tp;
514
515         while (! waitq_empty(wq)) {
516                 tp = waitq_get(wq);
517                 if (IS_TYPE_READY(tp))
518                         continue;
519
520                 switch (get_type_tpop_code(tp)) {
521                 case tpo_method:
522                         gen_method_type(env, tp);
523                         break;
524                 case tpo_class:
525                 case tpo_union:
526                 case tpo_struct:
527                         gen_struct_union_type(env, tp);
528                         break;
529                 case tpo_enumeration:
530                         gen_enum_type(env->h, tp);
531                         break;
532                 case tpo_primitive:
533                         gen_primitive_type(env->h, tp);
534                         break;
535                 case tpo_array:
536                         gen_array_type(env, tp);
537                         break;
538                 case tpo_pointer:
539                         gen_pointer_type(env, tp);
540                         break;
541                 case tpo_unknown:
542                         /* the unknown type: ignore */
543                         SET_TYPE_READY(tp);
544                         break;
545                 default:
546                         assert(! "Unknown tpop code");
547                 }  /* switch */
548         }  /* while */
549 }  /* finish_types */
550
551 /**
552  * generate all types.
553  */
554 static void gen_types(stabs_handle *h) {
555         wenv_t env;
556
557         env.h  = h;
558         env.wq = new_waitq();
559         type_walk(NULL, walk_type, &env);
560         finish_types(&env);
561         del_waitq(env.wq);
562 }  /* gen_types */
563
564
565 /* -------------------------- I/F ----------------------------- */
566
567 /**
568  * start a new source object (compilation unit)
569  */
570 static void stabs_so(dbg_handle *handle, const char *filename) {
571         stabs_handle *h = (stabs_handle *)handle;
572         h->main_file = h->curr_file = filename;
573         be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
574         be_emit_write_line();
575 }  /* stabs_so */
576
577 /**
578  * Main Program
579  */
580 static void stabs_main_program(dbg_handle *handle) {
581         ir_graph *irg = get_irp_main_irg();
582
583         (void) handle;
584         if (irg) {
585                 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
586                 be_emit_write_line();
587         }
588 }  /* stabs_main_program */
589
590 static void stabs_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
591 {
592         stabs_handle *handle = (stabs_handle*) h;
593         unsigned      lineno;
594         const char   *fname  = ir_retrieve_dbg_info(dbgi, &lineno);
595
596         if (fname == NULL)
597                 return;
598
599         if (handle->curr_file != fname) {
600                 /* TODO: escape filename correctly */
601                 if (handle->curr_file != handle->main_file) {
602                         be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", handle->curr_file,
603                                          N_EINCL);
604                         be_emit_write_line();
605                 }
606                 if (fname != handle->main_file) {
607                         be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", fname, N_SOL);
608                         be_emit_write_line();
609                 }
610                 handle->curr_file = fname;
611         }
612         if (handle->last_line != lineno) {
613                 char label[64];
614
615                 snprintf(label, sizeof(label), ".LM%u", ++handle->label_num);
616                 handle->last_line = lineno;
617
618                 be_emit_irprintf("\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno,
619                                  label, get_entity_ld_name(handle->cur_ent));
620                 be_emit_write_line();
621
622                 be_emit_string(label);
623                 be_emit_cstring(":\n");
624                 be_emit_write_line();
625         }
626 }
627
628 /**
629  * dump the stabs for a method begin
630  */
631 static void stabs_method_begin(dbg_handle *handle, ir_entity *ent, const be_stack_layout_t *layout) {
632         stabs_handle *h = (stabs_handle *)handle;
633         ir_type      *mtp, *rtp;
634         unsigned     type_num;
635         int          i, n, between_size;
636
637         h->cur_ent = ent;
638         h->layout  = layout;
639
640         /* create the method entry */
641         mtp = get_entity_type(ent);
642         if (is_lowered_type(mtp))
643                 mtp = get_associated_type(mtp);
644         if (get_method_n_ress(mtp) > 0)
645                 rtp = get_method_res_type(mtp, 0);
646         else
647                 rtp = NULL;
648         type_num = get_type_number(h, rtp);
649         be_emit_irprintf("\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
650                 get_entity_name(ent),
651                 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
652                 type_num,
653                 N_FUN,
654                 get_entity_ld_name(ent));
655         be_emit_write_line();
656
657         /* create parameter entries */
658         between_size = get_type_size_bytes(layout->between_type);
659         for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
660                 ir_type *ptp      = get_method_param_type(mtp, i);
661         const char *name  = get_method_param_name(mtp, i);
662                 unsigned type_num = get_type_number(h, ptp);
663         char buf[16];
664         int ofs = 0;
665                 ir_entity *stack_ent;
666
667         if (! name) {
668           snprintf(buf, sizeof(buf), "arg%d", i);
669           name = buf;
670         }
671                 /* check if this parameter has a stack entity. If it has, it
672                    it transmitted on the stack, else in a register */
673                 stack_ent = layout->param_map[i];
674                 if (stack_ent) {
675                         ofs = get_entity_offset(stack_ent) + between_size;
676                 }
677                 be_emit_irprintf("\t.stabs\t\"%s:p", name);
678                 if (is_Array_type(ptp)) {
679                         /* use a local array definition */
680                         print_array_type(h, ptp, 1);
681                 } else if (is_Pointer_type(ptp)) {
682                         /* use local pointer definition */
683                         print_pointer_type(h, ptp, 1);
684                 } else {
685                         type_num = get_type_number(h, ptp);
686
687                         /* name:type, bit offset from the start of the struct', number of bits in the element. */
688                         be_emit_irprintf("%u", type_num);
689                 }
690
691                 be_emit_irprintf("\",%d,0,0,%d\n", N_PSYM, ofs);
692                 be_emit_write_line();
693         }
694 }  /* stabs_method_begin */
695
696 /**
697  * dump the stabs for a method end
698  */
699 static void stabs_method_end(dbg_handle *handle) {
700         stabs_handle            *h = (stabs_handle *)handle;
701         ir_entity               *ent = h->cur_ent;
702         const be_stack_layout_t *layout = h->layout;
703         const char              *ld_name = get_entity_ld_name(ent);
704         int                     i, n, frame_size;
705         static unsigned         scope_nr = 0;
706
707         /* create entries for automatic variables on the stack */
708         frame_size = get_type_size_bytes(layout->frame_type);
709         for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
710                 ir_entity *ent = get_compound_member(layout->frame_type, i);
711                 ir_type *tp;
712                 int ofs;
713                 unsigned type_num;
714
715                 /* ignore spill slots and other helper objects */
716                 if (is_entity_compiler_generated(ent))
717                         continue;
718
719                 tp = get_entity_type(ent);
720                 /* should not happen in backend but ... */
721                 if (is_Method_type(tp))
722                         continue;
723                 type_num = get_type_number(h, tp);
724                 ofs      = -frame_size + get_entity_offset(ent);
725
726                 be_emit_irprintf("\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
727                         get_entity_name(ent), type_num, N_LSYM, ofs);
728                 be_emit_write_line();
729         }
730         /* we need a lexical block here */
731         be_emit_irprintf("\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
732         be_emit_write_line();
733         be_emit_irprintf("\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
734         be_emit_write_line();
735         be_emit_irprintf(".Lscope%u:\n", scope_nr);
736         be_emit_write_line();
737         ++scope_nr;
738
739         h->cur_ent = NULL;
740         h->layout  = NULL;
741 }  /* stabs_method_end */
742
743 /**
744  * dump types
745  */
746 static void stabs_types(dbg_handle *handle) {
747         stabs_handle *h = (stabs_handle *)handle;
748
749         /* allocate the zero for the void type */
750         h->next_type_nr++;
751         gen_void_type(h);
752         gen_types(h);
753 }  /* stabs_types */
754
755 /**
756  * dump a variable in the global type
757  */
758 static void stabs_variable(dbg_handle *handle, ir_entity *ent) {
759         stabs_handle *h = (stabs_handle *)handle;
760         unsigned tp_num = get_type_number(h, get_entity_type(ent));
761         char buf[1024];
762
763         if (get_entity_visibility(ent) == visibility_external_visible) {
764                 /* a global variable */
765                 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
766                         get_entity_name(ent), tp_num, N_GSYM);
767         } else { /* some kind of local */
768                 ir_variability variability = get_entity_variability(ent);
769                 int kind = N_STSYM;
770
771                 if (variability == variability_uninitialized)
772                         kind = N_LCSYM;
773                 else if (variability == variability_constant)
774                         kind = N_ROSYM;
775                 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
776                         get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
777         }
778         buf[sizeof(buf) - 1] = '\0';
779
780         be_emit_string(buf);
781 }  /* stabs_variable */
782
783 /**
784  * Close the stabs handler.
785  */
786 static void stabs_close(dbg_handle *handle) {
787         stabs_handle *h = (stabs_handle *)handle;
788         pmap_destroy(h->type_map);
789         free(h);
790 }  /* stabs_close */
791
792 /** The stabs operations. */
793 static const debug_ops stabs_ops = {
794         stabs_close,
795         stabs_so,
796         stabs_main_program,
797         stabs_method_begin,
798         stabs_method_end,
799         stabs_types,
800         stabs_variable,
801         stabs_set_dbg_info
802 };
803
804 /* Opens a stabs handler */
805 dbg_handle *be_stabs_open(void) {
806         stabs_handle *h = xmalloc(sizeof(*h));
807
808         h->base.ops     = &stabs_ops;
809         h->cur_ent      = NULL;
810         h->layout       = NULL;
811         h->next_type_nr = 0;
812         h->type_map     = pmap_create_ex(64);
813         h->main_file    = NULL;
814         h->curr_file    = NULL;
815
816         return &h->base;
817 }
818
819 void be_init_stabs(void)
820 {
821         be_register_dbgout_module("stabs", be_stabs_open);
822 }
823
824 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_stabs);