2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Stabs support.
23 * @author Michael Beck
45 #include "be_dbgout_t.h"
48 #include "beemitter.h"
51 /* Non-Stab Symbol and Stab Symbol Types */
53 N_UNDF = 0x00, /**< 0: Undefined symbol */
54 N_ABS = 0x02, /**< 2: File scope absolute symbol */
55 N_TEXT = 0x04, /**< 4: File scope text symbol */
56 N_DATA = 0x06, /**< 6: File scope data symbol */
57 N_BSS = 0x08, /**< 8: File scope BSS symbol */
58 N_INDR = 0x0A, /**< 10: Symbol is indirected to another symbol */
59 N_FN_SEQ = 0x0C, /**< 12: Same as N_FN, for Sequent compilers */
60 N_COMM = 0x12, /**< 18: Common--visible after shared library dynamic link */
61 N_SETA = 0x14, /**< 20: Absolute set element */
62 N_SETT = 0x17, /**< 23: Text segment set element */
63 N_SETD = 0x18, /**< 24: Data segment set element */
64 N_SETB = 0x1A, /**< 26: BSS segment set element */
65 N_SETV = 0x1C, /**< 28: Pointer to set vector */
66 N_WARNING = 0x1E, /**< 30: Print a warning message during linking */
67 N_FN = 0x1F, /**< 31: File name of a `.o' file */
68 N_GSYM = 0x20, /**< 32: Global symbol */
69 N_FNAME = 0x22, /**< 34: Function name (for BSD Fortran) */
70 N_FUN = 0x24, /**< 36: Function name */
71 N_STSYM = 0x26, /**< 38: Data segment file-scope variable */
72 N_LCSYM = 0x28, /**< 40: BSS segment file-scope variable */
73 N_MAIN = 0x2A, /**< 42: Name of main routine */
74 N_ROSYM = 0x2C, /**< 44: Variable in .rodata section */
75 N_PC = 0x30, /**< 48: Global symbol (for Pascal) */
76 N_NSYMS = 0x32, /**< 50: Number of symbols (according to Ultrix V4.0) */
77 N_NOMAP = 0x34, /**< 52: No DST map */
78 N_OBJ = 0x38, /**< 56: Object file (Solaris2) */
79 N_OPT = 0x3C, /**< 60: Debugger options (Solaris2) */
80 N_RSYM = 0x40, /**< 64: Register variable */
81 N_M2C = 0x42, /**< 66: Modula-2 compilation unit */
82 N_SLINE = 0x44, /**< 68: Line number in text segment */
83 N_DSLINE = 0x46, /**< 70: Line number in data segment */
84 N_BSLINE = 0x48, /**< 72: Line number in bss segment */
85 N_BROWS = 0x48, /**< 72: Sun source code browser, path to `.cb' file */
86 N_DEFD = 0x4A, /**< 74: GNU Modula2 definition module dependency */
87 N_FLINE = 0x4C, /**< 76: Function start/body/end line numbers (Solaris2) */
88 N_EHDECL = 0x50, /**< 80: GNU C++ exception variable */
89 N_MOD2 = 0x50, /**< 80: Modula2 info "for imc" (according to Ultrix V4.0) */
90 N_CATCH = 0x54, /**< 84: GNU C++ catch clause */
91 N_SSYM = 0x60, /**< 96: Structure of union element */
92 N_ENDM = 0x62, /**< 98: Last stab for module (Solaris2) */
93 N_SO = 0x64, /**< 100: Path and name of source file */
94 N_LSYM = 0x80, /**< 128: Stack variable */
95 N_BINCL = 0x82, /**< 130: Beginning of an include file (Sun only) */
96 N_SOL = 0x84, /**< 132: Name of include file */
97 N_PSYM = 0xA0, /**< 160: Parameter variable */
98 N_EINCL = 0xA2, /**< 162: End of an include file */
99 N_ENTRY = 0xA4, /**< 164: Alternate entry point */
100 N_LBRAC = 0xC0, /**< 192: Beginning of a lexical block */
101 N_EXCL = 0xC2, /**< 194: Place holder for a deleted include file */
102 N_SCOPE = 0xC4, /**< 196: Modula2 scope information (Sun linker) */
103 N_RBRAC = 0xE0, /**< 224: End of a lexical block */
104 N_BCOMM = 0xE2, /**< 226: Begin named common block */
105 N_ECOMM = 0xE4, /**< 228: End named common block */
106 N_ECOML = 0xE8, /**< 232: Member of a common block */
107 N_WITH = 0xEA, /**< 234: Pascal with statement: type,,0,0,offset (Solaris2) */
108 N_NBTEXT = 0xF0, /**< 240: Gould non-base registers */
109 N_NBDATA = 0xF2, /**< 242: Gould non-base registers */
110 N_NBBSS = 0xF4, /**< 244: Gould non-base registers */
111 N_NBSTS = 0xF6, /**< 246: Gould non-base registers */
112 N_NBLCS = 0xF8, /**< 248: Gould non-base registers */
118 typedef struct stabs_handle {
119 dbg_handle base; /**< the base class */
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 the current source file */
131 * Returns the stabs type number of a Firm type.
133 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
138 /* map to the void type */
141 entry = pmap_find(h->type_map, tp);
143 num = h->next_type_nr++;
144 pmap_insert(h->type_map, tp, INT_TO_PTR(num));
146 num = PTR_TO_INT(entry->value);
149 } /* get_type_number */
152 * Map a given Type to void by assigned the type number 0.
154 static void map_to_void(stabs_handle *h, ir_type *tp) {
155 pmap_insert(h->type_map, tp, INT_TO_PTR(0));
159 * generate the void type.
161 static void gen_void_type(stabs_handle *h) {
163 be_emit_irprintf("\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
164 be_emit_write_line();
165 } /* gen_void_type */
167 typedef struct walker_env {
172 /* a type is not ready: put it on the wait queue */
173 #define SET_TYPE_NOT_READY(wq, tp) \
175 set_type_link(tp, (void *)1); \
180 #define SET_TYPE_READY(tp) set_type_link(tp, NULL)
182 /* check whether a type is ready */
183 #define IS_TYPE_READY(tp) (get_type_link(tp) == NULL)
185 #ifdef EXPLICITE_PTR_TYPES
186 #define SKIP_PTR(tp) tp
188 #define SKIP_PTR(tp) (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
192 * mode_info for output as decimal
194 static const tarval_mode_info dec_output = {
201 * emit a tarval as decimal
203 static void be_emit_tv_as_decimal(tarval *tv) {
204 ir_mode *mode = get_tarval_mode(tv);
205 const tarval_mode_info *old = get_tarval_mode_output_option(mode);
207 set_tarval_mode_output_option(mode, &dec_output);
209 set_tarval_mode_output_option(mode, old);
213 * Generates a primitive type.
215 * @param h the stabs handle
218 static void gen_primitive_type(stabs_handle *h, ir_type *tp) {
219 ir_mode *mode = get_type_mode(tp);
223 if (mode == mode_T) {
224 /* firmcc, jack and the FirmJC compiler use mode_T for the void type.
225 Ignore it here as it's name is remapped to "void". */
230 if (0 && get_mode_size_bits(mode) & 7) {
231 /* this is a bitfield type, ignore it */
235 type_num = get_type_number(h, tp);
237 if (mode_is_int(mode)) {
238 be_emit_irprintf("\t.stabs\t\"%s:t%u=r%u;", get_type_name(tp), type_num, type_num);
239 be_emit_tv_as_decimal(get_mode_min(mode));
241 be_emit_tv_as_decimal(get_mode_max(mode));
242 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
243 be_emit_write_line();
244 } else if (mode_is_float(mode)) {
245 int size = get_type_size_bytes(tp);
246 be_emit_irprintf("\t.stabs\t\"%s:t%u=r1;%d;0;\",%d,0,0,0\n", get_type_name(tp), type_num, size, N_LSYM);
247 be_emit_write_line();
249 } /* gen_primitive_type */
252 * Generates an enum type
254 * @param h the stabs handle
257 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
258 unsigned type_num = get_type_number(h, tp);
262 be_emit_irprintf("\t.stabs\t\"%s:T%u=e", get_type_name(tp), type_num);
263 for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
264 ir_enum_const *ec = get_enumeration_const(tp, i);
267 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
268 be_emit_irprintf("%s:%s,", get_enumeration_name(ec), buf);
270 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
271 be_emit_write_line();
272 } /* gen_enum_type */
275 * print a pointer type
277 void print_pointer_type(stabs_handle *h, ir_type *tp, int local) {
278 unsigned type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
279 ir_type *el_tp = get_pointer_points_to_type(tp);
280 unsigned el_num = get_type_number(h, el_tp);
282 be_emit_irprintf("%u=*%u", type_num, el_num);
286 * Generates a pointer type
288 * @param env the walker environment
291 static void gen_pointer_type(wenv_t *env, ir_type *tp) {
292 stabs_handle *h = env->h;
293 ir_type *el_tp = get_pointer_points_to_type(tp);
296 if (! IS_TYPE_READY(el_tp))
297 waitq_put(env->wq, el_tp);
299 be_emit_irprintf("\t.stabs\t\"%s:t", get_type_name(tp));
300 print_pointer_type(h, tp, 0);
301 be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
302 be_emit_write_line();
303 } /* gen_pointer_type */
306 * print an array type
308 static void print_array_type(stabs_handle *h, ir_type *tp, int local) {
309 ir_type *etp = get_array_element_type(tp);
310 int i, n = get_array_n_dimensions(tp);
311 unsigned type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
314 be_emit_irprintf("%u=a", type_num);
315 NEW_ARR_A(int, perm, n);
316 for (i = 0; i < n; ++i) {
317 perm[i] = get_array_order(tp, i);
320 for (i = 0; i < n; ++i) {
323 if (is_Const(get_array_lower_bound(tp, dim)) && is_Const(get_array_upper_bound(tp, dim))) {
324 long min = get_array_lower_bound_int(tp, dim);
325 long max = get_array_upper_bound_int(tp, dim);
327 /* FIXME r1 must be integer type, but seems to work for now */
328 be_emit_irprintf("r1;%ld;%ld;", min, max-1);
332 type_num = get_type_number(h, etp);
333 be_emit_irprintf("%d", type_num);
337 * Generates an array type
339 * @param env the walker environment
342 static void gen_array_type(wenv_t *env, ir_type *tp) {
343 stabs_handle *h = env->h;
344 ir_type *etp = get_array_element_type(tp);
347 if (! IS_TYPE_READY(etp))
348 waitq_put(env->wq, etp);
350 be_emit_irprintf("\t.stabs\t\"%s:t", get_type_name(tp));
352 print_array_type(h, tp, 0);
354 be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
355 be_emit_write_line();
356 } /* gen_array_type */
359 * Generates a struct/union type
361 * @param env the walker environment
364 static void gen_struct_union_type(wenv_t *env, ir_type *tp) {
365 stabs_handle *h = env->h;
366 unsigned type_num = get_type_number(h, tp);
371 if (is_Struct_type(tp)) {
373 if (get_type_mode(tp) != NULL) {
374 /* this is a bitfield type, ignore it safely */
378 else if (is_Union_type(tp))
381 be_emit_irprintf("\t.stabs\t\"%s:Tt%u=%c%d",
382 get_type_name(tp), type_num, desc, get_type_size_bytes(tp));
384 for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
385 ir_entity *ent = get_compound_member(tp, i);
386 ir_type *mtp = get_entity_type(ent);
390 if (! IS_TYPE_READY(mtp))
391 waitq_put(env->wq, mtp);
392 ofs = get_entity_offset(ent);
393 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
394 /* this structure is a bitfield, skip */
397 for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
398 ir_entity *ent = get_struct_member(mtp, i);
399 ir_type *tp = get_entity_type(ent);
402 type_num = get_type_number(h, tp);
403 size = get_type_size_bytes(tp) * 8;
404 bofs = (ofs + get_entity_offset(ent)) * 8 + get_entity_offset_bits_remainder(ent);
406 /* name:type, bit offset from the start of the struct', number of bits in the element. */
407 be_emit_irprintf("%s:%u,%d,%u;", get_entity_name(ent), type_num, bofs, size);
411 be_emit_irprintf("%s:", get_entity_name(ent));
413 if (is_Array_type(mtp)) {
414 /* use a local array definition */
415 print_array_type(h, mtp, 1);
416 } else if (is_Pointer_type(mtp)) {
417 /* use local pointer definition */
418 print_pointer_type(h, mtp, 1);
420 type_num = get_type_number(h, mtp);
422 /* name:type, bit offset from the start of the struct', number of bits in the element. */
423 be_emit_irprintf("%u", type_num);
425 size = get_type_size_bytes(mtp) * 8;
426 be_emit_irprintf(",%d,%u;", ofs * 8, size);
429 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
430 be_emit_write_line();
431 } /* gen_struct_type */
434 * Generates a method type
436 * @param env the walker environment
439 static void gen_method_type(wenv_t *env, ir_type *tp) {
440 stabs_handle *h = env->h;
441 unsigned type_num = get_type_number(h, tp);
443 unsigned res_type_num;
444 int i, n = get_method_n_ress(tp);
448 rtp = get_method_res_type(tp, 0);
449 if (! IS_TYPE_READY(rtp))
450 waitq_put(env->wq, rtp);
452 res_type_num = get_type_number(h, rtp);
454 be_emit_irprintf("\t.stabs\t\"%s:t%u=f%u", get_type_name(tp), type_num, res_type_num);
456 /* handle more than one return type */
457 for (i = 1; i < n; ++i) {
458 rtp = get_method_res_type(tp, i);
459 if (! IS_TYPE_READY(rtp))
460 waitq_put(env->wq, rtp);
461 res_type_num = get_type_number(h, rtp);
462 be_emit_irprintf(",%u", res_type_num);
464 be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
465 be_emit_write_line();
466 } /* gen_method_type */
469 * type-walker: generate declaration for simple types,
470 * put all other types on a wait queue
472 static void walk_type(type_or_ent tore, void *ctx)
477 if (is_type(tore.typ)) {
480 /* ignore the unknown type */
481 if (tp == firm_unknown_type)
487 switch (get_type_tpop_code(tp)) {
489 if (tp == get_glob_type()) {
496 gen_struct_union_type(env, tp);
499 case tpo_enumeration:
500 gen_enum_type(env->h, tp);
504 gen_primitive_type(env->h, tp);
508 gen_method_type(env, tp);
512 gen_array_type(env, tp);
516 gen_pointer_type(env, tp);
520 /* the unknown type: ignore */
524 assert(! "Unknown tpop code");
529 * generate declaration for all types
531 static void finish_types(wenv_t *env)
536 while (! waitq_empty(wq)) {
538 if (IS_TYPE_READY(tp))
541 switch (get_type_tpop_code(tp)) {
543 gen_method_type(env, tp);
548 gen_struct_union_type(env, tp);
550 case tpo_enumeration:
551 gen_enum_type(env->h, tp);
554 gen_primitive_type(env->h, tp);
557 gen_array_type(env, tp);
560 gen_pointer_type(env, tp);
563 /* the unknown type: ignore */
567 assert(! "Unknown tpop code");
573 * generate all types.
575 static void gen_types(stabs_handle *h) {
579 env.wq = new_waitq();
580 type_walk(NULL, walk_type, &env);
586 /* -------------------------- I/F ----------------------------- */
589 * start a new source object (compilation unit)
591 static void stabs_so(dbg_handle *handle, const char *filename) {
592 stabs_handle *h = (stabs_handle *)handle;
593 h->main_file = h->curr_file = filename;
594 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
595 be_emit_write_line();
601 static void stabs_main_program(dbg_handle *handle) {
602 ir_graph *irg = get_irp_main_irg();
606 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
607 be_emit_write_line();
609 } /* stabs_main_program */
611 static void stabs_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
613 stabs_handle *handle = (stabs_handle*) h;
615 const char *fname = ir_retrieve_dbg_info(dbgi, &lineno);
620 if (handle->curr_file != fname) {
621 if (fname != handle->main_file) {
622 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", fname, N_SOL);
623 be_emit_write_line();
625 handle->curr_file = fname;
627 if (handle->last_line != lineno) {
630 snprintf(label, sizeof(label), ".LM%u", ++handle->label_num);
631 handle->last_line = lineno;
633 be_emit_irprintf("\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno,
634 label, get_entity_ld_name(handle->cur_ent));
635 be_emit_write_line();
637 be_emit_string(label);
638 be_emit_cstring(":\n");
639 be_emit_write_line();
644 * dump the stabs for a method begin
646 static void stabs_method_begin(dbg_handle *handle, ir_entity *ent, const be_stack_layout_t *layout) {
647 stabs_handle *h = (stabs_handle *)handle;
650 int i, n, between_size;
655 /* create the method entry */
656 mtp = get_entity_type(ent);
657 if (is_lowered_type(mtp))
658 mtp = get_associated_type(mtp);
659 if (get_method_n_ress(mtp) > 0)
660 rtp = get_method_res_type(mtp, 0);
663 type_num = get_type_number(h, rtp);
664 be_emit_irprintf("\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
665 get_entity_name(ent),
666 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
669 get_entity_ld_name(ent));
670 be_emit_write_line();
672 /* create parameter entries */
673 between_size = get_type_size_bytes(layout->between_type);
674 for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
675 ir_type *ptp = get_method_param_type(mtp, i);
676 const char *name = get_method_param_name(mtp, i);
677 unsigned type_num = get_type_number(h, ptp);
680 ir_entity *stack_ent;
683 snprintf(buf, sizeof(buf), "arg%d", i);
686 /* check if this parameter has a stack entity. If it has, it
687 it transmitted on the stack, else in a register */
688 stack_ent = layout->param_map[i];
690 ofs = get_entity_offset(stack_ent) + between_size;
692 be_emit_irprintf("\t.stabs\t\"%s:p", name);
693 if (is_Array_type(ptp)) {
694 /* use a local array definition */
695 print_array_type(h, ptp, 1);
696 } else if (is_Pointer_type(ptp)) {
697 /* use local pointer definition */
698 print_pointer_type(h, ptp, 1);
700 type_num = get_type_number(h, ptp);
702 /* name:type, bit offset from the start of the struct', number of bits in the element. */
703 be_emit_irprintf("%u", type_num);
706 be_emit_irprintf("\",%d,0,0,%d\n", N_PSYM, ofs);
707 be_emit_write_line();
709 } /* stabs_method_begin */
712 * dump the stabs for a method end
714 static void stabs_method_end(dbg_handle *handle) {
715 stabs_handle *h = (stabs_handle *)handle;
716 ir_entity *ent = h->cur_ent;
717 const be_stack_layout_t *layout = h->layout;
718 const char *ld_name = get_entity_ld_name(ent);
719 int i, n, frame_size;
720 static unsigned scope_nr = 0;
722 /* create entries for automatic variables on the stack */
723 frame_size = get_type_size_bytes(layout->frame_type);
724 for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
725 ir_entity *ent = get_compound_member(layout->frame_type, i);
730 /* ignore spill slots and other helper objects */
731 if (is_entity_compiler_generated(ent))
734 tp = get_entity_type(ent);
735 /* should not happen in backend but ... */
736 if (is_Method_type(tp))
738 type_num = get_type_number(h, tp);
739 ofs = -frame_size + get_entity_offset(ent);
741 be_emit_irprintf("\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
742 get_entity_name(ent), type_num, N_LSYM, ofs);
743 be_emit_write_line();
745 /* we need a lexical block here */
746 be_emit_irprintf("\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
747 be_emit_write_line();
748 be_emit_irprintf("\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
749 be_emit_write_line();
750 be_emit_irprintf(".Lscope%u:\n", scope_nr);
751 be_emit_write_line();
756 } /* stabs_method_end */
761 static void stabs_types(dbg_handle *handle) {
762 stabs_handle *h = (stabs_handle *)handle;
764 /* allocate the zero for the void type */
771 * dump a variable in the global type
773 static void stabs_variable(dbg_handle *handle, ir_entity *ent) {
774 stabs_handle *h = (stabs_handle *)handle;
775 unsigned tp_num = get_type_number(h, get_entity_type(ent));
778 if (get_entity_visibility(ent) == visibility_external_visible) {
779 /* a global variable */
780 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
781 get_entity_name(ent), tp_num, N_GSYM);
782 } else { /* some kind of local */
783 ir_variability variability = get_entity_variability(ent);
786 if (variability == variability_uninitialized)
788 else if (variability == variability_constant)
790 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
791 get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
793 buf[sizeof(buf) - 1] = '\0';
796 } /* stabs_variable */
799 * Close the stabs handler.
801 static void stabs_close(dbg_handle *handle) {
802 stabs_handle *h = (stabs_handle *)handle;
803 pmap_destroy(h->type_map);
807 /** The stabs operations. */
808 static const debug_ops stabs_ops = {
819 /* Opens a stabs handler */
820 dbg_handle *be_stabs_open(void) {
821 stabs_handle *h = xmalloc(sizeof(*h));
823 h->base.ops = &stabs_ops;
827 h->type_map = pmap_create_ex(64);
834 void be_init_stabs(void)
836 be_register_dbgout_module("stabs", be_stabs_open);
839 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_stabs);