28 #include "be_dbgout.h"
31 /* Non-Stab Symbol and Stab Symbol Types */
33 N_UNDF = 0x00, /**< 0: Undefined symbol */
34 N_ABS = 0x02, /**< 2: File scope absolute symbol */
35 N_TEXT = 0x04, /**< 4: File scope text symbol */
36 N_DATA = 0x06, /**< 6: File scope data symbol */
37 N_BSS = 0x08, /**< 8: File scope BSS symbol */
38 N_INDR = 0x0A, /**< 10: Symbol is indirected to another symbol */
39 N_FN_SEQ = 0x0C, /**< 12: Same as N_FN, for Sequent compilers */
40 N_COMM = 0x12, /**< 18: Common--visible after shared library dynamic link */
41 N_SETA = 0x14, /**< 20: Absolute set element */
42 N_SETT = 0x17, /**< 23: Text segment set element */
43 N_SETD = 0x18, /**< 24: Data segment set element */
44 N_SETB = 0x1A, /**< 26: BSS segment set element */
45 N_SETV = 0x1C, /**< 28: Pointer to set vector */
46 N_WARNING = 0x1E, /**< 30: Print a warning message during linking */
47 N_FN = 0x1F, /**< 31: File name of a `.o' file */
48 N_GSYM = 0x20, /**< 32: Global symbol */
49 N_FNAME = 0x22, /**< 34: Function name (for BSD Fortran) */
50 N_FUN = 0x24, /**< 36: Function name */
51 N_STSYM = 0x26, /**< 38: Data segment file-scope variable */
52 N_LCSYM = 0x28, /**< 40: BSS segment file-scope variable */
53 N_MAIN = 0x2A, /**< 42: Name of main routine */
54 N_ROSYM = 0x2C, /**< 44: Variable in .rodata section */
55 N_PC = 0x30, /**< 48: Global symbol (for Pascal) */
56 N_NSYMS = 0x32, /**< 50: Number of symbols (according to Ultrix V4.0) */
57 N_NOMAP = 0x34, /**< 52: No DST map */
58 N_OBJ = 0x38, /**< 56: Object file (Solaris2) */
59 N_OPT = 0x3C, /**< 60: Debugger options (Solaris2) */
60 N_RSYM = 0x40, /**< 64: Register variable */
61 N_M2C = 0x42, /**< 66: Modula-2 compilation unit */
62 N_SLINE = 0x44, /**< 68: Line number in text segment */
63 N_DSLINE = 0x46, /**< 70: Line number in data segment */
64 N_BSLINE = 0x48, /**< 72: Line number in bss segment */
65 N_BROWS = 0x48, /**< 72: Sun source code browser, path to `.cb' file */
66 N_DEFD = 0x4A, /**< 74: GNU Modula2 definition module dependency */
67 N_FLINE = 0x4C, /**< 76: Function start/body/end line numbers (Solaris2) */
68 N_EHDECL = 0x50, /**< 80: GNU C++ exception variable */
69 N_MOD2 = 0x50, /**< 80: Modula2 info "for imc" (according to Ultrix V4.0) */
70 N_CATCH = 0x54, /**< 84: GNU C++ catch clause */
71 N_SSYM = 0x60, /**< 96: Structure of union element */
72 N_ENDM = 0x62, /**< 98: Last stab for module (Solaris2) */
73 N_SO = 0x64, /**< 100: Path and name of source file */
74 N_LSYM = 0x80, /**< 128: Stack variable */
75 N_BINCL = 0x82, /**< 130: Beginning of an include file (Sun only) */
76 N_SOL = 0x84, /**< 132: Name of include file */
77 N_PSYM = 0xA0, /**< 160: Parameter variable */
78 N_EINCL = 0xA2, /**< 162: End of an include file */
79 N_ENTRY = 0xA4, /**< 164: Alternate entry point */
80 N_LBRAC = 0xC0, /**< 192: Beginning of a lexical block */
81 N_EXCL = 0xC2, /**< 194: Place holder for a deleted include file */
82 N_SCOPE = 0xC4, /**< 196: Modula2 scope information (Sun linker) */
83 N_RBRAC = 0xE0, /**< 224: End of a lexical block */
84 N_BCOMM = 0xE2, /**< 226: Begin named common block */
85 N_ECOMM = 0xE4, /**< 228: End named common block */
86 N_ECOML = 0xE8, /**< 232: Member of a common block */
87 N_WITH = 0xEA, /**< 234: Pascal with statement: type,,0,0,offset (Solaris2) */
88 N_NBTEXT = 0xF0, /**< 240: Gould non-base registers */
89 N_NBDATA = 0xF2, /**< 242: Gould non-base registers */
90 N_NBBSS = 0xF4, /**< 244: Gould non-base registers */
91 N_NBSTS = 0xF6, /**< 246: Gould non-base registers */
92 N_NBLCS = 0xF8, /**< 248: Gould non-base registers */
98 typedef struct stabs_handle {
99 dbg_handle base; /**< the base class */
100 FILE *f; /**< the file write to */
101 entity *cur_ent; /**< current method entity */
102 unsigned next_type_nr; /**< next type number */
103 pmap *type_map; /**< a map from type to type number */
107 * Returns the stabs type number of a Firm type.
109 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
113 /* map to the void type */
116 entry = pmap_find(h->type_map, tp);
117 return entry ? PTR_TO_INT(entry->value) : 0;
118 } /* get_type_number */
121 * Assign a stabs type number to a Firm type.
123 static unsigned assign_type_number(stabs_handle *h, ir_type *tp) {
126 /* map to the void type */
129 num = h->next_type_nr++;
130 pmap_insert(h->type_map, tp, INT_TO_PTR(num));
132 } /* assign_type_number */
135 * generate the void type.
137 static void gen_void_type(stabs_handle *h) {
138 fprintf(h->f, "\t.stabs \"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
139 } /* gen_void_type */
141 typedef struct walker_env {
146 /* a type is not ready: put it on the wait queue */
147 #define SET_TYPE_NOT_READY(wq, tp) \
149 set_type_link(tp, (void *)1); \
154 #define SET_TYPE_READY(tp) set_type_link(tp, NULL)
156 /* check whether a type is ready */
157 #define IS_TYPE_READY(tp) (get_type_link(tp) == NULL)
159 #ifdef EXPLICITE_PTR_TYPES
160 #define SKIP_PTR(tp) tp
162 #define SKIP_PTR(tp) (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
166 * Generates a primitive type.
168 * @param h the stabs handle
171 static void gen_primitive_type(stabs_handle *h, ir_type *tp)
173 ir_mode *mode = get_type_mode(tp);
176 if (mode == mode_T) {
177 /* jack and FirmJC compiler use mode_T for the void type.
178 Ignore it here as it's name is remapped to "void". */
182 if (0 && get_mode_size_bits(mode) & 7) {
183 /* this is a bitfield type, ignore it */
187 type_num = assign_type_number(h, tp);
189 if (mode_is_int(mode)) {
191 fprintf(h->f, "\t.stabs \"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
192 tarval_snprintf(buf, sizeof(buf), get_mode_min(mode));
193 fprintf(h->f, "%s;", buf);
194 tarval_snprintf(buf, sizeof(buf), get_mode_max(mode));
195 fprintf(h->f, "%s;\",%d,0,0,0\n", buf, N_LSYM);
196 } else if (mode_is_float(mode)) {
197 int size = get_type_size_bytes(tp);
198 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);
200 } /* gen_primitive_type */
203 * Generates an enum type
205 * @param h the stabs handle
208 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
209 unsigned type_num = assign_type_number(h, tp);
212 fprintf(h->f, "\t.stabs \"%s:T%u=e", get_type_name(tp), type_num);
213 for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
214 ir_enum_const *ec = get_enumeration_const(tp, i);
217 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
218 fprintf(h->f, "%s:%s,", get_enumeration_name(ec), buf);
220 fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
221 } /* gen_enum_type */
224 * Generates a pointer type
226 * @param h the stabs handle
229 static void gen_pointer_type(stabs_handle *h, ir_type *tp) {
230 unsigned type_num = assign_type_number(h, tp);
231 unsigned el_num = get_type_number(h, get_pointer_points_to_type(tp));
233 fprintf(h->f, "\t.stabs \"%s:t%u=*%u\",%d,0,0,0\n",
234 get_type_name(tp), type_num, el_num, N_LSYM);
235 } /* gen_pointer_type */
238 * Generates a struct/union type
240 * @param h the stabs handle
243 static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
244 unsigned type_num = assign_type_number(h, tp);
248 if (is_Struct_type(tp)) {
250 if (get_type_mode(tp) != NULL) {
251 /* this is a bitfield type, ignore it safely */
258 fprintf(h->f, "\t.stabs \"%s:T%u=%c%d",
259 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
261 for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
262 entity *ent = get_compound_member(tp, i);
263 ir_type *mtp = get_entity_type(ent);
266 ofs = get_entity_offset_bits(ent);
267 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
268 /* this structure is a bitfield, skip */
271 for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
272 entity *ent = get_struct_member(mtp, i);
273 ir_type *tp = get_entity_type(ent);
276 type_num = get_type_number(h, tp);
277 size = get_type_size_bits(tp);
278 bofs = ofs + get_entity_offset_bits(ent);
280 /* name:type, bit offset from the start of the struct', number of bits in the element. */
281 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, bofs, size);
285 type_num = get_type_number(h, mtp);
286 size = get_type_size_bits(mtp);
288 /* name:type, bit offset from the start of the struct', number of bits in the element. */
289 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, ofs, size);
292 fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
293 } /* gen_struct_type */
296 * Generates an array type
298 * @param h the stabs handle
301 static void gen_array_type(stabs_handle *h, ir_type *tp) {
302 unsigned type_num = assign_type_number(h, tp);
303 int i, n = get_array_n_dimensions(tp);
307 NEW_ARR_A(int, perm, n);
308 for (i = 0; i < n; ++i) {
309 perm[i] = get_array_order(tp, i);
311 fprintf(h->f, "\t.stabs \"%s:t%u=a", get_type_name(tp), type_num);
313 for (i = 0; i < n; ++i) {
315 long min = get_array_lower_bound_int(tp, dim);
316 long max = get_array_upper_bound_int(tp, dim);
318 /* FIXME r1 must be integer type, but seems to work for now */
319 fprintf(h->f, "r1;%ld;%ld;", min, max-1);
322 etp = get_array_element_type(tp);
323 type_num = get_type_number(h, etp);
324 fprintf(h->f, "%d\",%d,0,0,0\n", type_num, N_LSYM);
325 } /* gen_array_type */
328 * Generates a method type
330 * @param h the stabs handle
333 static void gen_method_type(stabs_handle *h, ir_type *tp) {
334 unsigned type_num = assign_type_number(h, tp);
336 unsigned res_type_num;
338 if (get_method_n_ress(tp) > 0)
339 rtp = get_method_res_type(tp, 0);
340 res_type_num = get_type_number(h, rtp);
342 fprintf(h->f, "\t.stabs \"%s:t%u=f%u\",%d,0,0,0\n",
343 get_type_name(tp), type_num, res_type_num, N_LSYM);
344 } /* gen_method_type */
347 * type-walker: generate declaration for simple types,
348 * put all other types on a wait queue
350 static void walk_type(type_or_ent *tore, void *ctx)
355 if (get_kind(tore) == k_type) {
356 tp = (ir_type *)tore;
358 /* ignore the unknown type */
359 if (tp == firm_unknown_type)
365 switch (get_type_tpop_code(tp)) {
367 if (tp == get_glob_type()) {
374 SET_TYPE_NOT_READY(env->wq, tp);
377 case tpo_enumeration:
378 gen_enum_type(env->h, tp);
383 gen_primitive_type(env->h, tp);
389 SET_TYPE_NOT_READY(env->wq, tp);
393 /* must construct the pointer type */
394 SET_TYPE_NOT_READY(env->wq, tp);
398 /* the unknown type: ignore */
401 assert(! "Unknown tpop code");
406 * check, if a method type can be generated
408 static int is_method_type_ready(ir_type *tp)
413 for (i = get_method_n_ress(tp) - 1; i >= 0; --i) {
414 etp = SKIP_PTR(get_method_res_type(tp, i));
416 if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
420 for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
421 etp = SKIP_PTR(get_method_param_type(tp, i));
423 if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
427 } /* is_method_type_ready */
430 * check, whether a compound type can be generated
432 static int is_compound_type_ready(ir_type *tp)
437 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
438 etp = SKIP_PTR(get_entity_type(get_compound_member(tp, i)));
440 if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
444 } /* is_compound_type_ready */
447 * generate declaration for all types
449 static void finish_types(stabs_handle *h, waitq *wq)
453 while (! waitq_empty(wq)) {
456 switch (get_type_tpop_code(tp)) {
458 if (is_method_type_ready(tp)) {
459 if (get_method_n_ress(tp) <= 1)
460 gen_method_type(h, tp);
462 fprintf(stderr, "Warning: Cannot create stabs debug info for type %s\n", get_type_name(tp));
471 if (is_compound_type_ready(tp)) {
472 gen_struct_union_type(h, tp);
478 if (IS_TYPE_READY(get_array_element_type(tp))) {
479 gen_array_type(h, tp);
485 if (IS_TYPE_READY(get_pointer_points_to_type(tp))) {
486 gen_pointer_type(h, tp);
492 assert(! "Unknown tpop code");
495 /* still not ready, defer type output */
501 * generate all types.
503 static void gen_types(stabs_handle *h) {
507 env.wq = new_waitq();
508 type_walk(NULL, walk_type, &env);
509 finish_types(h, env.wq);
514 /* -------------------------- I/F ----------------------------- */
517 * start a new source object (compilation unit)
519 static void stabs_so(dbg_handle *handle, const char *filename) {
520 stabs_handle *h = (stabs_handle *)handle;
521 fprintf(h->f, "\t.stabs \"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
527 static void stabs_main_program(dbg_handle *handle) {
528 stabs_handle *h = (stabs_handle *)handle;
529 ir_graph *irg = get_irp_main_irg();
531 fprintf(h->f, "\t.stabs \"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
533 } /* stabs_main_program */
536 * prints a line number
538 static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
539 stabs_handle *h = (stabs_handle *)handle;
540 fprintf(h->f, ".stabn %d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
544 * dump the stabs for a function
546 static void stabs_method(dbg_handle *handle, entity *ent, const be_stack_layout_t *layout) {
547 stabs_handle *h = (stabs_handle *)handle;
550 int i, n, between_size;
554 mtp = get_entity_type(ent);
555 if (is_lowered_type(mtp))
556 mtp = get_associated_type(mtp);
557 if (get_method_n_ress(mtp) > 0)
558 rtp = get_method_res_type(mtp, 0);
561 type_num = get_type_number(h, rtp);
562 fprintf(h->f, "\t.stabs \"%s:%c%u\",%u,0,0,%s\n",
563 get_entity_name(ent),
564 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
567 get_entity_ld_name(ent));
569 between_size = get_type_size_bytes(layout->between_type);
570 for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
571 ir_type *ptp = get_method_param_type(mtp, i);
572 const char *name = get_method_param_name(mtp, i);
573 unsigned type_num = get_type_number(h, ptp);
579 snprintf(buf, sizeof(buf), "arg%d", i);
582 /* check if this parameter has a stack entity. If it has, it
583 it transmitted on the stack, else in a register */
584 stack_ent = layout->param_map[i];
586 ofs = get_entity_offset_bytes(stack_ent) + between_size;
588 fprintf(h->f, "\t.stabs \"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
595 static void stabs_types(dbg_handle *handle) {
596 stabs_handle *h = (stabs_handle *)handle;
598 /* allocate the zero for the void type */
607 static void stabs_global(dbg_handle *handle, struct obstack *obst, entity *ent) {
608 stabs_handle *h = (stabs_handle *)handle;
609 unsigned tp_num = get_type_number(h, get_entity_type(ent));
612 obstack_printf(obst, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
613 get_entity_name(ent), tp_num, N_GSYM);
615 fprintf(h->f, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
616 get_entity_name(ent), tp_num, N_GSYM);
621 * Close the stabs handler.
623 static void stabs_close(dbg_handle *handle) {
624 stabs_handle *h = (stabs_handle *)handle;
625 pmap_destroy(h->type_map);
629 /** The stabs operations. */
630 static const debug_ops stabs_ops = {
640 /* Opens a stabs handler */
641 dbg_handle *be_stabs_open(FILE *out) {
642 stabs_handle *h = xmalloc(sizeof(*h));
644 h->base.ops = &stabs_ops;
648 h->type_map = pmap_create_ex(64);
652 /** close a debug handler. */
653 void be_dbg_close(dbg_handle *h) {
659 * start a new source object (compilation unit)
661 void be_dbg_so(dbg_handle *h, const char *filename) {
663 h->ops->so(h, filename);
669 void be_dbg_main_program(dbg_handle *h) {
670 if (h->ops->main_program)
671 h->ops->main_program(h);
672 } /* be_dbg_main_program */
674 /** debug for a function */
675 void be_dbg_method(dbg_handle *h, entity *ent, const be_stack_layout_t *layout) {
677 h->ops->method(h, ent, layout);
678 } /* be_dbg_method */
680 /** debug for line number */
681 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
683 h->ops->line(h, lineno, address);
687 void be_dbg_types(dbg_handle *h) {
693 void be_dbg_global(dbg_handle *h, struct obstack *obst, entity *ent) {
695 h->ops->global(h, obst, ent);
696 } /* be_dbg_global */