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