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