686129e3a6d4325b96890c45205cb4d5e174a309
[libfirm] / ir / be / bestabs.c
1 /**
2  * Stabs support.
3  *
4  * @author Michael Beck
5  * @date   11.9.2006
6  * @cvsid  $Id$
7  */
8
9 #ifdef HAVE_CONFIG_H
10 # include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16
17 #include "irprog.h"
18 #include "entity.h"
19 #include "irgraph.h"
20 #include "typewalk.h"
21 #include "tv.h"
22 #include "xmalloc.h"
23 #include "pmap.h"
24 #include "pdeq.h"
25 #include "irtools.h"
26 #include "obst.h"
27 #include "be_dbgout.h"
28
29 /* Non-Stab Symbol and Stab Symbol Types */
30 enum stabs_types {
31         N_UNDF    = 0x00,    /**< 0: Undefined symbol */
32         N_ABS     = 0x02,    /**< 2: File scope absolute symbol */
33         N_TEXT    = 0x04,    /**< 4: File scope text symbol */
34         N_DATA    = 0x06,    /**< 6: File scope data symbol */
35         N_BSS     = 0x08,    /**< 8: File scope BSS symbol */
36         N_INDR    = 0x0A,    /**< 10: Symbol is indirected to another symbol */
37         N_FN_SEQ  = 0x0C,    /**< 12: Same as N_FN, for Sequent compilers */
38         N_COMM    = 0x12,    /**< 18: Common--visible after shared library dynamic link */
39         N_SETA    = 0x14,    /**< 20: Absolute set element */
40         N_SETT    = 0x17,    /**< 23: Text segment set element */
41         N_SETD    = 0x18,    /**< 24: Data segment set element */
42         N_SETB    = 0x1A,    /**< 26: BSS segment set element */
43         N_SETV    = 0x1C,    /**< 28: Pointer to set vector */
44         N_WARNING = 0x1E,    /**< 30: Print a warning message during linking */
45         N_FN      = 0x1F,    /**< 31: File name of a `.o' file */
46         N_GSYM    = 0x20,    /**< 32: Global symbol */
47         N_FNAME   = 0x22,    /**< 34: Function name (for BSD Fortran) */
48         N_FUN     = 0x24,    /**< 36: Function name */
49         N_STSYM   = 0x26,    /**< 38: Data segment file-scope variable */
50         N_LCSYM   = 0x28,    /**< 40: BSS segment file-scope variable */
51         N_MAIN    = 0x2A,    /**< 42: Name of main routine */
52         N_ROSYM   = 0x2C,    /**< 44: Variable in .rodata section */
53         N_PC      = 0x30,    /**< 48: Global symbol (for Pascal) */
54         N_NSYMS   = 0x32,    /**< 50: Number of symbols (according to Ultrix V4.0) */
55         N_NOMAP   = 0x34,    /**< 52: No DST map */
56         N_OBJ     = 0x38,    /**< 56: Object file (Solaris2) */
57         N_OPT     = 0x3C,    /**< 60: Debugger options (Solaris2) */
58         N_RSYM    = 0x40,    /**< 64: Register variable */
59         N_M2C     = 0x42,    /**< 66: Modula-2 compilation unit */
60         N_SLINE   = 0x44,    /**< 68: Line number in text segment */
61         N_DSLINE  = 0x46,    /**< 70: Line number in data segment */
62         N_BSLINE  = 0x48,    /**< 72: Line number in bss segment */
63         N_BROWS   = 0x48,    /**< 72: Sun source code browser, path to `.cb' file */
64         N_DEFD    = 0x4A,    /**< 74: GNU Modula2 definition module dependency */
65         N_FLINE   = 0x4C,    /**< 76: Function start/body/end line numbers (Solaris2) */
66         N_EHDECL  = 0x50,    /**< 80: GNU C++ exception variable */
67         N_MOD2    = 0x50,    /**< 80: Modula2 info "for imc" (according to Ultrix V4.0) */
68         N_CATCH   = 0x54,    /**< 84: GNU C++ catch clause */
69         N_SSYM    = 0x60,    /**< 96: Structure of union element */
70         N_ENDM    = 0x62,    /**< 98: Last stab for module (Solaris2) */
71         N_SO      = 0x64,    /**< 100: Path and name of source file */
72         N_LSYM    = 0x80,    /**< 128: Stack variable */
73         N_BINCL   = 0x82,    /**< 130: Beginning of an include file (Sun only) */
74         N_SOL     = 0x84,    /**< 132: Name of include file */
75         N_PSYM    = 0xA0,    /**< 160: Parameter variable */
76         N_EINCL   = 0xA2,    /**< 162: End of an include file */
77         N_ENTRY   = 0xA4,    /**< 164: Alternate entry point */
78         N_LBRAC   = 0xC0,    /**< 192: Beginning of a lexical block */
79         N_EXCL    = 0xC2,    /**< 194: Place holder for a deleted include file */
80         N_SCOPE   = 0xC4,    /**< 196: Modula2 scope information (Sun linker) */
81         N_RBRAC   = 0xE0,    /**< 224: End of a lexical block */
82         N_BCOMM   = 0xE2,    /**< 226: Begin named common block */
83         N_ECOMM   = 0xE4,    /**< 228: End named common block */
84         N_ECOML   = 0xE8,    /**< 232: Member of a common block */
85         N_WITH    = 0xEA,    /**< 234: Pascal with statement: type,,0,0,offset (Solaris2) */
86         N_NBTEXT  = 0xF0,    /**< 240: Gould non-base registers */
87         N_NBDATA  = 0xF2,    /**< 242: Gould non-base registers */
88         N_NBBSS   = 0xF4,    /**< 244: Gould non-base registers */
89         N_NBSTS   = 0xF6,    /**< 246: Gould non-base registers */
90         N_NBLCS   = 0xF8,    /**< 248: Gould non-base registers */
91 };
92
93 /**
94  * The stabs handle.
95  */
96 typedef struct stabs_handle {
97         dbg_handle base;         /**< the base class */
98         FILE       *f;           /**< the file write to */
99         entity     *cur_ent;     /**< current method entity */
100         unsigned   next_type_nr; /**< next type number */
101         pmap       *type_map;    /**< a map from type to type number */
102 } stabs_handle;
103
104 /**
105  * Returns the stabs type number of a Firm type.
106  */
107 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
108         pmap_entry *entry;
109
110         if (tp == NULL) {
111                 /* map to the void type */
112                 return 0;
113         }
114         entry = pmap_find(h->type_map, tp);
115         return entry ? PTR_TO_INT(entry->value) : 0;
116 }  /* get_type_number */
117
118 /**
119  * Assign a stabs type number to a Firm type.
120  */
121 static unsigned assign_type_number(stabs_handle *h, ir_type *tp) {
122         unsigned num;
123         if (tp == NULL) {
124                 /* map to the void type */
125                 return 0;
126         }
127         num = h->next_type_nr++;
128         pmap_insert(h->type_map, tp, INT_TO_PTR(num));
129         return num;
130 }  /* assign_type_number */
131
132 /**
133  * generate the void type.
134  */
135 static void gen_void_type(stabs_handle *h) {
136         fprintf(h->f, "\t.stabs \"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
137 }  /* gen_void_type */
138
139 typedef struct walker_env {
140         stabs_handle *h;
141         waitq        *wq;
142 } wenv_t;
143
144 /* a type is not ready: put it on the wait queue */
145 #define SET_TYPE_NOT_READY(wq, tp) \
146   do { \
147     set_type_link(tp, (void *)1);  \
148     waitq_put(wq, tp);              \
149   } while(0)
150
151 /* a the is ready */
152 #define SET_TYPE_READY(tp)     set_type_link(tp, NULL)
153
154 /* check whether a type is ready */
155 #define IS_TYPE_READY(tp)      (get_type_link(tp) == NULL)
156
157 #ifdef EXPLICITE_PTR_TYPES
158 #define SKIP_PTR(tp)  tp
159 #else
160 #define SKIP_PTR(tp)   (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
161 #endif
162
163 /**
164  * Generates a primitive type.
165  *
166  * @param h    the stabs handle
167  * @param tp   the type
168  */
169 static void gen_primitive_type(stabs_handle *h, ir_type *tp)
170 {
171         ir_mode *mode = get_type_mode(tp);
172         unsigned type_num;
173
174         if (mode == mode_T) {
175     /* jack and FirmJC compiler use mode_T for the void type.
176                 Ignore it here as it's name is remapped to "void". */
177                 return;
178         }  /* if */
179
180         if (0 && get_mode_size_bits(mode) & 7) {
181                 /* this is a bitfield type, ignore it */
182                 return;
183         }  /* if */
184
185         type_num = assign_type_number(h, tp);
186
187         if (mode_is_int(mode)) {
188                 char buf[64];
189                 fprintf(h->f, "\t.stabs \"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
190                 tarval_snprintf(buf, sizeof(buf), get_mode_min(mode));
191                 fprintf(h->f, "%s;", buf);
192                 tarval_snprintf(buf, sizeof(buf), get_mode_max(mode));
193                 fprintf(h->f, "%s;\",%d,0,0,0\n", buf, N_LSYM);
194         } else if (mode_is_float(mode)) {
195                 int size = get_type_size_bytes(tp);
196                 fprintf(h->f, "\t.stabs \"%s:t%u=r1;%d;0;\",%d,0,0,0\n", get_type_name(tp), type_num, size, N_LSYM);
197         }
198 }  /* gen_primitive_type */
199
200 /**
201  * Generates an enum type
202  *
203  * @param h    the stabs handle
204  * @param tp   the type
205  */
206 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
207         unsigned type_num = assign_type_number(h, tp);
208         int i, n;
209
210         fprintf(h->f, "\t.stabs \"%s:T%u=e", get_type_name(tp), type_num);
211         for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
212                 ir_enum_const *ec = get_enumeration_const(tp, i);
213                 char buf[64];
214
215                 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
216                 fprintf(h->f, "%s:%s,", get_enumeration_name(ec), buf);
217         }
218         fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
219 }  /* gen_enum_type */
220
221 /**
222  * Generates a pointer type
223  *
224  * @param h    the stabs handle
225  * @param tp   the type
226  */
227 static void gen_pointer_type(stabs_handle *h, ir_type *tp) {
228         unsigned type_num = assign_type_number(h, tp);
229         unsigned el_num   = get_type_number(h, get_pointer_points_to_type(tp));
230
231         fprintf(h->f, "\t.stabs \"%s:T%u=*%u\",%d,0,0,0\n",
232                 get_type_name(tp), type_num, el_num, N_LSYM);
233 }  /* gen_pointer_type */
234
235 /**
236  * Generates a struct/union type
237  *
238  * @param h    the stabs handle
239  * @param tp   the type
240  */
241 static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
242         unsigned type_num = assign_type_number(h, tp);
243         int i, n;
244         char desc;
245
246         if (is_Struct_type(tp)) {
247                 desc = 's';
248                 if (get_type_mode(tp) != NULL) {
249                         /* this is a bitfield type, ignore it safely */
250                         return;
251                 }
252         }
253         else
254                 desc = 'u';
255
256         fprintf(h->f, "\t.stabs \"%s:T%u=%c%d",
257                 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
258
259         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
260                 entity  *ent = get_compound_member(tp, i);
261                 ir_type *mtp = get_entity_type(ent);
262                 int ofs, size;
263
264                 ofs  = get_entity_offset_bits(ent);
265                 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
266                         /* this structure is a bitfield, skip */
267                         int i, n;
268
269                         for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
270                                 entity *ent = get_struct_member(mtp, i);
271                                 ir_type *tp = get_entity_type(ent);
272                                 int bofs;
273
274                                 type_num = get_type_number(h, tp);
275                                 size = get_type_size_bits(tp);
276                                 bofs = ofs + get_entity_offset_bits(ent);
277
278                                 /* name:type, bit offset from the start of the struct', number of bits in the element. */
279                                 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, bofs, size);
280                         }
281                 } else {
282                         /* no bitfield */
283                         type_num = get_type_number(h, mtp);
284                         size = get_type_size_bits(mtp);
285
286                         /* name:type, bit offset from the start of the struct', number of bits in the element. */
287                         fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, ofs, size);
288                 }
289         }
290         fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
291 }  /* gen_struct_type */
292
293 /**
294  * type-walker: generate declaration for simple types,
295  * put all other types on a wait queue
296  */
297 static void walk_type(type_or_ent *tore, void *ctx)
298 {
299         wenv_t *env = ctx;
300         FILE *f = env->h->f;
301         entity   *ent;
302         ir_type  *tp;
303         ir_graph *irg;
304
305         if (get_kind(tore) == k_type) {
306                 tp = (ir_type *)tore;
307
308                 /* ignore the unknown type */
309                 if (tp == firm_unknown_type)
310                         return;
311         } else {
312                 return;
313         }  /* if */
314
315         switch (get_type_tpop_code(tp)) {
316         case tpo_class:
317                 if (tp == get_glob_type()) {
318                         SET_TYPE_READY(tp);
319                         break;
320                 }
321                 /* fall through */
322         case tpo_struct:
323         case tpo_union:
324                 SET_TYPE_NOT_READY(env->wq, tp);
325                 break;
326
327         case tpo_enumeration:
328                 gen_enum_type(env->h, tp);
329                 SET_TYPE_READY(tp);
330                 break;
331
332         case tpo_primitive:
333                 gen_primitive_type(env->h, tp);
334                 SET_TYPE_READY(tp);
335                 break;
336
337         case tpo_method:
338         case tpo_array:
339                 SET_TYPE_NOT_READY(env->wq, tp);
340                 break;
341
342         case tpo_pointer:
343                 /* must construct the pointer type */
344                 SET_TYPE_NOT_READY(env->wq, tp);
345                 break;
346
347         case tpo_unknown:
348                 /* the unknown type: ignore */
349                 break;
350         default:
351                 assert(! "Unknown tpop code");
352         }  /* switch */
353 }  /* walk_type */
354
355 /**
356  * check, if a method type can be generated
357  */
358 static int is_method_type_ready(ir_type *tp)
359 {
360   int i;
361   ir_type *etp;
362
363   for (i = get_method_n_ress(tp) - 1; i >= 0; --i) {
364     etp = SKIP_PTR(get_method_res_type(tp, i));
365
366     if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
367       return 0;
368   }  /* for */
369
370   for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
371     etp = SKIP_PTR(get_method_param_type(tp, i));
372
373     if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
374       return 0;
375   }  /* for */
376   return 1;
377 }  /* is_method_type_ready */
378
379 /**
380  * check, whether a compound type can be generated
381  */
382 static int is_compound_type_ready(ir_type *tp)
383 {
384   int i;
385   ir_type *etp;
386
387   for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
388     etp = SKIP_PTR(get_entity_type(get_compound_member(tp, i)));
389
390     if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
391       return 0;
392   }  /* for */
393   return 1;
394 }  /* is_compound_type_ready */
395
396 /**
397  * generate declaration for all types
398  */
399 static void finish_types(stabs_handle *h, waitq *wq)
400 {
401   ir_type *tp;
402
403   while (! waitq_empty(wq)) {
404     tp = waitq_get(wq);
405
406     switch (get_type_tpop_code(tp)) {
407     case tpo_method:
408       if (is_method_type_ready(tp)) {
409         if (get_method_n_ress(tp) <= 1)
410           //gen_method_type(h, tp)
411                   ;
412         else {
413           fprintf(stderr, "Warning: Cannot create stabs debug info for type %s\n", get_type_name(tp));
414         }  /* if */
415         SET_TYPE_READY(tp);
416         continue;
417       }  /* if */
418       break;
419     case tpo_class:
420     case tpo_union:
421     case tpo_struct:
422       if (is_compound_type_ready(tp)) {
423         gen_struct_union_type(h, tp);
424         SET_TYPE_READY(tp);
425         continue;
426       }  /* if */
427       break;
428     case tpo_array:
429       if (IS_TYPE_READY(get_array_element_type(tp))) {
430         //gen_array_type(h, tp);
431         SET_TYPE_READY(tp);
432         continue;
433       }  /* if */
434       break;
435     case tpo_pointer:
436       if (IS_TYPE_READY(get_pointer_points_to_type(tp))) {
437         gen_pointer_type(h, tp);
438         SET_TYPE_READY(tp);
439         continue;
440       }  /* if */
441       break;
442     default:
443       assert(! "Unknown tpop code");
444     }  /* switch */
445
446     /* still not ready, defer type output */
447     pdeq_putr(wq, tp);
448   }  /* while */
449 }  /* finish_types */
450
451 /**
452  * generate all types.
453  */
454 static void gen_types(stabs_handle *h) {
455         wenv_t env;
456
457         env.h  = h;
458         env.wq = new_waitq();
459         type_walk(NULL, walk_type, &env);
460         finish_types(h, env.wq);
461         del_waitq(env.wq);
462 }  /* gen_types */
463
464
465 /* -------------------------- I/F ----------------------------- */
466
467 /**
468  * start a new source object (compilation unit)
469  */
470 static void stabs_so(dbg_handle *handle, const char *filename) {
471         stabs_handle *h = (stabs_handle *)handle;
472         fprintf(h->f, "\t.stabs \"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
473 }  /* stabs_so */
474
475 /**
476  * Main Program
477  */
478 static void stabs_main_program(dbg_handle *handle) {
479         stabs_handle *h = (stabs_handle *)handle;
480         ir_graph *irg = get_irp_main_irg();
481         if (irg) {
482                 fprintf(h->f, "\t.stabs \"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
483         }
484 }  /* stabs_main_program */
485
486 /**
487  * prints a line number
488  */
489 static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
490         stabs_handle *h = (stabs_handle *)handle;
491         fprintf(h->f, ".stabn %d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
492 }  /* stabs_line */
493
494 /**
495  * dump the stabs for a function
496  */
497 static void stabs_method(dbg_handle *handle, entity *ent) {
498         stabs_handle *h = (stabs_handle *)handle;
499         ir_type *mtp, *rtp;
500         unsigned type_num;
501         int i, n;
502
503         h->cur_ent = ent;
504
505         mtp = get_entity_type(ent);
506         if (is_lowered_type(mtp))
507                 mtp = get_associated_type(mtp);
508         if (get_method_n_ress(mtp) > 0)
509                 rtp = get_method_res_type(mtp, 0);
510         else
511                 rtp = NULL;
512         type_num = get_type_number(h, rtp);
513         fprintf(h->f, "\t.stabs \"%s:%c%u\",%u,0,0,%s\n",
514                 get_entity_name(ent),
515                 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
516                 type_num,
517                 N_FUN,
518                 get_entity_ld_name(ent));
519
520         for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
521                 ir_type *ptp      = get_method_param_type(mtp, i);
522         const char *name  = get_method_param_name(mtp, i);
523                 unsigned type_num = get_type_number(h, ptp);
524         char buf[16];
525         int ofs = 0;
526
527         if (! name) {
528           snprintf(buf, sizeof(buf), "arg%d", i);
529           name = buf;
530         }
531         /* FIXME: calculate the offset */
532                 fprintf(h->f, "\t.stabs \"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
533         }
534 }  /* stabs_method */
535
536 /**
537  * dump types
538  */
539 static void stabs_types(dbg_handle *handle) {
540         stabs_handle *h = (stabs_handle *)handle;
541
542         /* allocate the zero for the void type */
543         h->next_type_nr++;
544         gen_void_type(h);
545         gen_types(h);
546 }  /* stabs_types */
547
548 /**
549  * dump a global
550  */
551 static void stabs_global(dbg_handle *handle, struct obstack *obst, entity *ent) {
552         stabs_handle *h = (stabs_handle *)handle;
553         unsigned tp_num = get_type_number(h, get_entity_type(ent));
554
555         if (obst) {
556                 obstack_printf(obst, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
557                         get_entity_name(ent), tp_num, N_GSYM);
558         } else {
559                 fprintf(h->f, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
560                         get_entity_name(ent), tp_num, N_GSYM);
561         }
562 }  /* stabs_global */
563
564 /**
565  * Close the stabs handler.
566  */
567 static void stabs_close(dbg_handle *handle) {
568         stabs_handle *h = (stabs_handle *)handle;
569         pmap_destroy(h->type_map);
570         free(h);
571 }  /* stabs_close */
572
573 /** The stabs operations. */
574 static const debug_ops stabs_ops = {
575         stabs_close,
576         stabs_so,
577         stabs_main_program,
578         stabs_method,
579         stabs_line,
580         stabs_types,
581         stabs_global
582 };
583
584 /* Opens a stabs handler */
585 dbg_handle *be_stabs_open(FILE *out) {
586         stabs_handle *h = xmalloc(sizeof(*h));
587
588         h->base.ops     = &stabs_ops;
589         h->f            = out;
590         h->cur_ent      = NULL;
591         h->next_type_nr = 0;
592         h->type_map     = pmap_create_ex(64);
593         return &h->base;
594 }  /* stabs_open */
595
596 /** close a debug handler. */
597 void be_dbg_close(dbg_handle *h) {
598         if (h->ops->close)
599                 h->ops->close(h);
600 }  /* be_dbg_close */
601
602 /**
603  * start a new source object (compilation unit)
604  */
605 void be_dbg_so(dbg_handle *h, const char *filename) {
606         if (h->ops->so)
607                 h->ops->so(h, filename);
608 }  /* be_dbg_begin */
609
610 /**
611  * Main program
612  */
613 void be_dbg_main_program(dbg_handle *h) {
614         if (h->ops->main_program)
615                 h->ops->main_program(h);
616 }  /* be_dbg_main_program */
617
618 /** debug for a function */
619 void be_dbg_method(dbg_handle *h, entity *ent) {
620         if (h->ops->method)
621                 h->ops->method(h, ent);
622 }  /* be_dbg_method */
623
624 /** debug for line number */
625 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
626         if (h->ops->line)
627                 h->ops->line(h, lineno, address);
628 }  /* be_dbg_line */
629
630 /** dump types */
631 void be_dbg_types(dbg_handle *h) {
632         if (h->ops->types)
633                 h->ops->types(h);
634 }  /* be_dbg_types */
635
636 /** dump a global */
637 void be_dbg_global(dbg_handle *h, struct obstack *obst, entity *ent) {
638         if (h->ops->global)
639                 h->ops->global(h, obst, ent);
640 }  /* be_dbg_global */