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