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