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