27 #include "be_dbgout.h"
29 /* Non-Stab Symbol and Stab Symbol 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 */
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 */
105 * Returns the stabs type number of a Firm type.
107 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
111 /* map to the void type */
114 entry = pmap_find(h->type_map, tp);
115 return entry ? PTR_TO_INT(entry->value) : 0;
116 } /* get_type_number */
119 * Assign a stabs type number to a Firm type.
121 static unsigned assign_type_number(stabs_handle *h, ir_type *tp) {
124 /* map to the void type */
127 num = h->next_type_nr++;
128 pmap_insert(h->type_map, tp, INT_TO_PTR(num));
130 } /* assign_type_number */
133 * generate the void type.
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 */
139 typedef struct walker_env {
144 /* a type is not ready: put it on the wait queue */
145 #define SET_TYPE_NOT_READY(wq, tp) \
147 set_type_link(tp, (void *)1); \
152 #define SET_TYPE_READY(tp) set_type_link(tp, NULL)
154 /* check whether a type is ready */
155 #define IS_TYPE_READY(tp) (get_type_link(tp) == NULL)
157 #ifdef EXPLICITE_PTR_TYPES
158 #define SKIP_PTR(tp) tp
160 #define SKIP_PTR(tp) (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
164 * Generates a primitive type.
166 * @param h the stabs handle
169 static void gen_primitive_type(stabs_handle *h, ir_type *tp)
171 ir_mode *mode = get_type_mode(tp);
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". */
180 if (0 && get_mode_size_bits(mode) & 7) {
181 /* this is a bitfield type, ignore it */
185 type_num = assign_type_number(h, tp);
187 if (mode_is_int(mode)) {
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);
198 } /* gen_primitive_type */
201 * Generates an enum type
203 * @param h the stabs handle
206 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
207 unsigned type_num = assign_type_number(h, tp);
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);
215 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
216 fprintf(h->f, "%s:%s,", get_enumeration_name(ec), buf);
218 fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
219 } /* gen_enum_type */
222 * Generates a pointer type
224 * @param h the stabs handle
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));
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 */
236 * Generates a struct/union type
238 * @param h the stabs handle
241 static void gen_struct_union_type(stabs_handle *h, ir_type *tp) {
242 unsigned type_num = assign_type_number(h, tp);
246 if (is_Struct_type(tp)) {
248 if (get_type_mode(tp) != NULL) {
249 /* this is a bitfield type, ignore it safely */
256 fprintf(h->f, "\t.stabs \"%s:T%u=%c%d",
257 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
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);
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 */
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);
274 type_num = get_type_number(h, tp);
275 size = get_type_size_bits(tp);
276 bofs = ofs + get_entity_offset_bits(ent);
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);
283 type_num = get_type_number(h, mtp);
284 size = get_type_size_bits(mtp);
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);
290 fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
291 } /* gen_struct_type */
294 * type-walker: generate declaration for simple types,
295 * put all other types on a wait queue
297 static void walk_type(type_or_ent *tore, void *ctx)
305 if (get_kind(tore) == k_type) {
306 tp = (ir_type *)tore;
308 /* ignore the unknown type */
309 if (tp == firm_unknown_type)
315 switch (get_type_tpop_code(tp)) {
317 if (tp == get_glob_type()) {
324 SET_TYPE_NOT_READY(env->wq, tp);
327 case tpo_enumeration:
328 gen_enum_type(env->h, tp);
333 gen_primitive_type(env->h, tp);
339 SET_TYPE_NOT_READY(env->wq, tp);
343 /* must construct the pointer type */
344 SET_TYPE_NOT_READY(env->wq, tp);
348 /* the unknown type: ignore */
351 assert(! "Unknown tpop code");
356 * check, if a method type can be generated
358 static int is_method_type_ready(ir_type *tp)
363 for (i = get_method_n_ress(tp) - 1; i >= 0; --i) {
364 etp = SKIP_PTR(get_method_res_type(tp, i));
366 if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
370 for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
371 etp = SKIP_PTR(get_method_param_type(tp, i));
373 if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
377 } /* is_method_type_ready */
380 * check, whether a compound type can be generated
382 static int is_compound_type_ready(ir_type *tp)
387 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
388 etp = SKIP_PTR(get_entity_type(get_compound_member(tp, i)));
390 if (! IS_TYPE_READY(etp) && (! is_compound_type(etp) || is_Array_type(etp)))
394 } /* is_compound_type_ready */
397 * generate declaration for all types
399 static void finish_types(stabs_handle *h, waitq *wq)
403 while (! waitq_empty(wq)) {
406 switch (get_type_tpop_code(tp)) {
408 if (is_method_type_ready(tp)) {
409 if (get_method_n_ress(tp) <= 1)
410 //gen_method_type(h, tp)
413 fprintf(stderr, "Warning: Cannot create stabs debug info for type %s\n", get_type_name(tp));
422 if (is_compound_type_ready(tp)) {
423 gen_struct_union_type(h, tp);
429 if (IS_TYPE_READY(get_array_element_type(tp))) {
430 //gen_array_type(h, tp);
436 if (IS_TYPE_READY(get_pointer_points_to_type(tp))) {
437 gen_pointer_type(h, tp);
443 assert(! "Unknown tpop code");
446 /* still not ready, defer type output */
452 * generate all types.
454 static void gen_types(stabs_handle *h) {
458 env.wq = new_waitq();
459 type_walk(NULL, walk_type, &env);
460 finish_types(h, env.wq);
465 /* -------------------------- I/F ----------------------------- */
468 * start a new source object (compilation unit)
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);
478 static void stabs_main_program(dbg_handle *handle) {
479 stabs_handle *h = (stabs_handle *)handle;
480 ir_graph *irg = get_irp_main_irg();
482 fprintf(h->f, "\t.stabs \"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
484 } /* stabs_main_program */
487 * prints a line number
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));
495 * dump the stabs for a function
497 static void stabs_method(dbg_handle *handle, entity *ent) {
498 stabs_handle *h = (stabs_handle *)handle;
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);
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',
518 get_entity_ld_name(ent));
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);
528 snprintf(buf, sizeof(buf), "arg%d", i);
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);
539 static void stabs_types(dbg_handle *handle) {
540 stabs_handle *h = (stabs_handle *)handle;
542 /* allocate the zero for the void type */
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));
556 obstack_printf(obst, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
557 get_entity_name(ent), tp_num, N_GSYM);
559 fprintf(h->f, "\t.stabs \"%s:G%u\",%d,0,0,0\n",
560 get_entity_name(ent), tp_num, N_GSYM);
565 * Close the stabs handler.
567 static void stabs_close(dbg_handle *handle) {
568 stabs_handle *h = (stabs_handle *)handle;
569 pmap_destroy(h->type_map);
573 /** The stabs operations. */
574 static const debug_ops stabs_ops = {
584 /* Opens a stabs handler */
585 dbg_handle *be_stabs_open(FILE *out) {
586 stabs_handle *h = xmalloc(sizeof(*h));
588 h->base.ops = &stabs_ops;
592 h->type_map = pmap_create_ex(64);
596 /** close a debug handler. */
597 void be_dbg_close(dbg_handle *h) {
603 * start a new source object (compilation unit)
605 void be_dbg_so(dbg_handle *h, const char *filename) {
607 h->ops->so(h, filename);
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 */
618 /** debug for a function */
619 void be_dbg_method(dbg_handle *h, entity *ent) {
621 h->ops->method(h, ent);
622 } /* be_dbg_method */
624 /** debug for line number */
625 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
627 h->ops->line(h, lineno, address);
631 void be_dbg_types(dbg_handle *h) {
637 void be_dbg_global(dbg_handle *h, struct obstack *obst, entity *ent) {
639 h->ops->global(h, obst, ent);
640 } /* be_dbg_global */