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