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 ir_entity *cur_ent; /**< current method entity */
102 const be_stack_layout_t *layout; /**< current stack layout */
103 unsigned next_type_nr; /**< next type number */
104 pmap *type_map; /**< a map from type to type number */
105 const char *main_file; /**< name of the main source file */
106 const char *curr_file; /**< name of teh current source file */
110 * Returns the stabs type number of a Firm type.
112 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
117 /* map to the void type */
120 entry = pmap_find(h->type_map, tp);
122 num = h->next_type_nr++;
123 pmap_insert(h->type_map, tp, INT_TO_PTR(num));
125 num = PTR_TO_INT(entry->value);
128 } /* get_type_number */
131 * generate the void type.
133 static void gen_void_type(stabs_handle *h) {
134 fprintf(h->f, "\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
135 } /* gen_void_type */
137 typedef struct walker_env {
142 /* a type is not ready: put it on the wait queue */
143 #define SET_TYPE_NOT_READY(wq, tp) \
145 set_type_link(tp, (void *)1); \
150 #define SET_TYPE_READY(tp) set_type_link(tp, NULL)
152 /* check whether a type is ready */
153 #define IS_TYPE_READY(tp) (get_type_link(tp) == NULL)
155 #ifdef EXPLICITE_PTR_TYPES
156 #define SKIP_PTR(tp) tp
158 #define SKIP_PTR(tp) (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
162 * Generates a primitive type.
164 * @param h the stabs handle
167 static void gen_primitive_type(stabs_handle *h, ir_type *tp)
169 ir_mode *mode = get_type_mode(tp);
173 if (mode == mode_T) {
174 /* jack and FirmJC compiler use mode_T for the void type.
175 Ignore it here as it's name is remapped to "void". */
179 if (0 && get_mode_size_bits(mode) & 7) {
180 /* this is a bitfield type, ignore it */
184 type_num = get_type_number(h, tp);
186 if (mode_is_int(mode)) {
188 fprintf(h->f, "\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
189 tarval_snprintf(buf, sizeof(buf), get_mode_min(mode));
190 fprintf(h->f, "%s;", buf);
191 tarval_snprintf(buf, sizeof(buf), get_mode_max(mode));
192 fprintf(h->f, "%s;\",%d,0,0,0\n", buf, N_LSYM);
193 } else if (mode_is_float(mode)) {
194 int size = get_type_size_bytes(tp);
195 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);
197 } /* gen_primitive_type */
200 * Generates an enum type
202 * @param h the stabs handle
205 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
206 unsigned type_num = get_type_number(h, tp);
210 fprintf(h->f, "\t.stabs\t\"%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 env the walker environment
227 static void gen_pointer_type(wenv_t *env, ir_type *tp) {
228 stabs_handle *h = env->h;
229 unsigned type_num = get_type_number(h, tp);
230 ir_type *el_tp = get_pointer_points_to_type(tp);
231 unsigned el_num = get_type_number(h, el_tp);
234 if (! IS_TYPE_READY(el_tp))
235 waitq_put(env->wq, el_tp);
236 fprintf(h->f, "\t.stabs\t\"%s:t%u=*%u\",%d,0,0,0\n",
237 get_type_name(tp), type_num, el_num, N_LSYM);
238 } /* gen_pointer_type */
241 * Generates a struct/union type
243 * @param env the walker environment
246 static void gen_struct_union_type(wenv_t *env, ir_type *tp) {
247 stabs_handle *h = env->h;
248 unsigned type_num = get_type_number(h, tp);
253 if (is_Struct_type(tp)) {
255 if (get_type_mode(tp) != NULL) {
256 /* this is a bitfield type, ignore it safely */
260 else if (is_Union_type(tp))
263 fprintf(h->f, "\t.stabs\t\"%s:Tt%u=%c%d",
264 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
266 for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
267 ir_entity *ent = get_compound_member(tp, i);
268 ir_type *mtp = get_entity_type(ent);
271 if (! IS_TYPE_READY(mtp))
272 waitq_put(env->wq, mtp);
273 ofs = get_entity_offset(ent);
274 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
275 /* this structure is a bitfield, skip */
278 for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
279 ir_entity *ent = get_struct_member(mtp, i);
280 ir_type *tp = get_entity_type(ent);
283 type_num = get_type_number(h, tp);
284 size = get_type_size_bits(tp);
285 bofs = (ofs + get_entity_offset(ent)) * 8 + get_entity_offset_bits_remainder(ent);
287 /* name:type, bit offset from the start of the struct', number of bits in the element. */
288 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, bofs, size);
292 type_num = get_type_number(h, mtp);
293 size = get_type_size_bits(mtp);
295 /* name:type, bit offset from the start of the struct', number of bits in the element. */
296 fprintf(h->f, "%s:%u,%d,%d;", get_entity_name(ent), type_num, ofs, size);
299 fprintf(h->f, ";\",%d,0,0,0\n", N_LSYM);
300 } /* gen_struct_type */
303 * Generates an array type
305 * @param env the walker environment
308 static void gen_array_type(wenv_t *env, ir_type *tp) {
309 stabs_handle *h = env->h;
310 unsigned type_num = get_type_number(h, tp);
311 int i, n = get_array_n_dimensions(tp);
312 ir_type *etp = get_array_element_type(tp);
316 if (! IS_TYPE_READY(etp))
317 waitq_put(env->wq, etp);
319 NEW_ARR_A(int, perm, n);
320 for (i = 0; i < n; ++i) {
321 perm[i] = get_array_order(tp, i);
323 fprintf(h->f, "\t.stabs\t\"%s:t%u=a", get_type_name(tp), type_num);
325 for (i = 0; i < n; ++i) {
327 long min = get_array_lower_bound_int(tp, dim);
328 long max = get_array_upper_bound_int(tp, dim);
330 /* FIXME r1 must be integer type, but seems to work for now */
331 fprintf(h->f, "r1;%ld;%ld;", min, max-1);
334 type_num = get_type_number(h, etp);
335 fprintf(h->f, "%d\",%d,0,0,0\n", type_num, N_LSYM);
336 } /* gen_array_type */
339 * Generates a method type
341 * @param env the walker environment
344 static void gen_method_type(wenv_t *env, ir_type *tp) {
345 stabs_handle *h = env->h;
346 unsigned type_num = get_type_number(h, tp);
348 unsigned res_type_num;
349 int i, n = get_method_n_ress(tp);
353 rtp = get_method_res_type(tp, 0);
354 if (! IS_TYPE_READY(rtp))
355 waitq_put(env->wq, rtp);
357 res_type_num = get_type_number(h, rtp);
359 fprintf(h->f, "\t.stabs\t\"%s:t%u=f%u", get_type_name(tp), type_num, res_type_num);
361 /* handle more than one return type */
362 for (i = 1; i < n; ++i) {
363 rtp = get_method_res_type(tp, i);
364 if (! IS_TYPE_READY(rtp))
365 waitq_put(env->wq, rtp);
366 res_type_num = get_type_number(h, rtp);
367 fprintf(h->f, ",%u", res_type_num);
369 fprintf(h->f, "\",%d,0,0,0\n", N_LSYM);
370 } /* gen_method_type */
373 * type-walker: generate declaration for simple types,
374 * put all other types on a wait queue
376 static void walk_type(type_or_ent *tore, void *ctx)
381 if (get_kind(tore) == k_type) {
382 tp = (ir_type *)tore;
384 /* ignore the unknown type */
385 if (tp == firm_unknown_type)
391 switch (get_type_tpop_code(tp)) {
393 if (tp == get_glob_type()) {
400 gen_struct_union_type(env, tp);
403 case tpo_enumeration:
404 gen_enum_type(env->h, tp);
408 gen_primitive_type(env->h, tp);
412 gen_method_type(env, tp);
416 gen_array_type(env, tp);
420 gen_pointer_type(env, tp);
424 /* the unknown type: ignore */
428 assert(! "Unknown tpop code");
433 * generate declaration for all types
435 static void finish_types(wenv_t *env)
440 while (! waitq_empty(wq)) {
442 if (IS_TYPE_READY(tp))
445 switch (get_type_tpop_code(tp)) {
447 gen_method_type(env, tp);
452 gen_struct_union_type(env, tp);
454 case tpo_enumeration:
455 gen_enum_type(env->h, tp);
458 gen_primitive_type(env->h, tp);
461 gen_array_type(env, tp);
464 gen_pointer_type(env, tp);
467 /* the unknown type: ignore */
471 assert(! "Unknown tpop code");
477 * generate all types.
479 static void gen_types(stabs_handle *h) {
483 env.wq = new_waitq();
484 type_walk(NULL, walk_type, &env);
490 /* -------------------------- I/F ----------------------------- */
493 * start a new source object (compilation unit)
495 static void stabs_so(dbg_handle *handle, const char *filename) {
496 stabs_handle *h = (stabs_handle *)handle;
497 h->main_file = h->curr_file = filename;
498 fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
502 * end an include file
504 static void stabs_include_end(dbg_handle *handle) {
505 stabs_handle *h = (stabs_handle *)handle;
506 h->curr_file = h->main_file;
507 } /* stabs_include_end */
510 * start an include file
512 static void stabs_include_begin(dbg_handle *handle, const char *filename) {
513 stabs_handle *h = (stabs_handle *)handle;
514 if (h->main_file != h->curr_file)
515 stabs_include_end(handle);
516 h->curr_file = filename;
517 fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", filename, N_SOL);
518 } /* stabs_include_begin */
523 static void stabs_main_program(dbg_handle *handle) {
524 stabs_handle *h = (stabs_handle *)handle;
525 ir_graph *irg = get_irp_main_irg();
527 fprintf(h->f, "\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
529 } /* stabs_main_program */
532 * prints a line number
534 static void stabs_line(dbg_handle *handle, unsigned lineno, const char *address) {
535 stabs_handle *h = (stabs_handle *)handle;
536 fprintf(h->f, "\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno, address, get_entity_ld_name(h->cur_ent));
540 * dump the stabs for a method begin
542 static void stabs_method_begin(dbg_handle *handle, ir_entity *ent, const be_stack_layout_t *layout) {
543 stabs_handle *h = (stabs_handle *)handle;
546 int i, n, between_size;
551 /* create the method entry */
552 mtp = get_entity_type(ent);
553 if (is_lowered_type(mtp))
554 mtp = get_associated_type(mtp);
555 if (get_method_n_ress(mtp) > 0)
556 rtp = get_method_res_type(mtp, 0);
559 type_num = get_type_number(h, rtp);
560 fprintf(h->f, "\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
561 get_entity_name(ent),
562 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
565 get_entity_ld_name(ent));
567 /* create parameter entries */
568 between_size = get_type_size_bytes(layout->between_type);
569 for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
570 ir_type *ptp = get_method_param_type(mtp, i);
571 const char *name = get_method_param_name(mtp, i);
572 unsigned type_num = get_type_number(h, ptp);
575 ir_entity *stack_ent;
578 snprintf(buf, sizeof(buf), "arg%d", i);
581 /* check if this parameter has a stack entity. If it has, it
582 it transmitted on the stack, else in a register */
583 stack_ent = layout->param_map[i];
585 ofs = get_entity_offset(stack_ent) + between_size;
587 fprintf(h->f, "\t.stabs\t\"%s:p%u\",%d,0,0,%d\n", name, type_num, N_PSYM, ofs);
589 } /* stabs_method_begin */
592 * dump the stabs for a method end
594 static void stabs_method_end(dbg_handle *handle) {
595 stabs_handle *h = (stabs_handle *)handle;
596 ir_entity *ent = h->cur_ent;
597 const be_stack_layout_t *layout = h->layout;
598 const char *ld_name = get_entity_ld_name(ent);
599 int i, n, frame_size;
600 static unsigned scope_nr = 0;
602 /* create entries for automatic variables on the stack */
603 frame_size = get_type_size_bytes(layout->frame_type);
604 for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
605 ir_entity *ent = get_compound_member(layout->frame_type, i);
610 /* ignore spill slots and other helper objects */
611 if (is_entity_compiler_generated(ent))
614 tp = get_entity_type(ent);
615 /* should not happen in backend but ... */
616 if (is_Method_type(tp))
618 type_num = get_type_number(h, tp);
619 ofs = -frame_size + get_entity_offset(ent);
621 fprintf(h->f, "\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
622 get_entity_name(ent), type_num, N_LSYM, ofs);
624 /* we need a lexical block here */
625 fprintf(h->f, "\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
626 fprintf(h->f, "\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
627 fprintf(h->f, ".Lscope%u:\n", scope_nr);
632 } /* stabs_method_end */
637 static void stabs_types(dbg_handle *handle) {
638 stabs_handle *h = (stabs_handle *)handle;
640 /* allocate the zero for the void type */
647 * dump a variable in the global type
649 static void stabs_variable(dbg_handle *handle, struct obstack *obst, ir_entity *ent) {
650 stabs_handle *h = (stabs_handle *)handle;
651 unsigned tp_num = get_type_number(h, get_entity_type(ent));
654 if (get_entity_visibility(ent) == visibility_external_visible) {
655 /* a global variable */
656 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
657 get_entity_name(ent), tp_num, N_GSYM);
658 } else { /* some kind of local */
659 ir_variability variability = get_entity_variability(ent);
662 if (variability == variability_uninitialized)
664 else if (variability == variability_constant)
666 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
667 get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
669 buf[sizeof(buf) - 1] = '\0';
672 obstack_printf(obst, "%s", buf);
674 fprintf(h->f, "%s", buf);
675 } /* stabs_variable */
678 * Close the stabs handler.
680 static void stabs_close(dbg_handle *handle) {
681 stabs_handle *h = (stabs_handle *)handle;
682 pmap_destroy(h->type_map);
686 /** The stabs operations. */
687 static const debug_ops stabs_ops = {
700 /* Opens the NULL handler */
701 dbg_handle *be_nulldbg_open(void) {
703 } /* be_nulldbg_open */
705 /* Opens a stabs handler */
706 dbg_handle *be_stabs_open(FILE *out) {
707 stabs_handle *h = xmalloc(sizeof(*h));
709 h->base.ops = &stabs_ops;
714 h->type_map = pmap_create_ex(64);
721 /** close a debug handler. */
722 void be_dbg_close(dbg_handle *h) {
723 if (h && h->ops->close)
728 * start a new source object (compilation unit)
730 void be_dbg_so(dbg_handle *h, const char *filename) {
732 h->ops->so(h, filename);
736 * start an include file
738 void be_dbg_include_begin(dbg_handle *h, const char *filename) {
739 if (h && h->ops->include_begin)
740 h->ops->include_begin(h, filename);
741 } /* stabs_include_begin */
744 * end an include file
746 void be_dbg_include_end(dbg_handle *h) {
747 if (h && h->ops->include_end)
748 h->ops->include_end(h);
749 } /* stabs_include_end */
754 void be_dbg_main_program(dbg_handle *h) {
755 if (h && h->ops->main_program)
756 h->ops->main_program(h);
757 } /* be_dbg_main_program */
759 /** debug for a method begin */
760 void be_dbg_method_begin(dbg_handle *h, ir_entity *ent, const be_stack_layout_t *layout) {
761 if (h && h->ops->method_begin)
762 h->ops->method_begin(h, ent, layout);
763 } /* be_dbg_method_begin */
765 /** debug for a method end */
766 void be_dbg_method_end(dbg_handle *h) {
767 if (h && h->ops->method_end)
768 h->ops->method_end(h);
769 } /* be_dbg_method_end */
771 /** debug for line number */
772 void be_dbg_line(dbg_handle *h, unsigned lineno, const char *address) {
773 if (h && h->ops->line)
774 h->ops->line(h, lineno, address);
778 void be_dbg_types(dbg_handle *h) {
779 if (h && h->ops->types)
784 void be_dbg_variable(dbg_handle *h, struct obstack *obst, ir_entity *ent) {
785 if (h && h->ops->variable)
786 h->ops->variable(h, obst, ent);
787 } /* be_dbg_variable */