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