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
43 #include "be_dbgout_t.h"
46 #include "beemitter.h"
49 /* Non-Stab Symbol and Stab Symbol Types */
51 N_UNDF = 0x00, /**< 0: Undefined symbol */
52 N_ABS = 0x02, /**< 2: File scope absolute symbol */
53 N_TEXT = 0x04, /**< 4: File scope text symbol */
54 N_DATA = 0x06, /**< 6: File scope data symbol */
55 N_BSS = 0x08, /**< 8: File scope BSS symbol */
56 N_INDR = 0x0A, /**< 10: Symbol is indirected to another symbol */
57 N_FN_SEQ = 0x0C, /**< 12: Same as N_FN, for Sequent compilers */
58 N_COMM = 0x12, /**< 18: Common--visible after shared library dynamic link */
59 N_SETA = 0x14, /**< 20: Absolute set element */
60 N_SETT = 0x17, /**< 23: Text segment set element */
61 N_SETD = 0x18, /**< 24: Data segment set element */
62 N_SETB = 0x1A, /**< 26: BSS segment set element */
63 N_SETV = 0x1C, /**< 28: Pointer to set vector */
64 N_WARNING = 0x1E, /**< 30: Print a warning message during linking */
65 N_FN = 0x1F, /**< 31: File name of a `.o' file */
66 N_GSYM = 0x20, /**< 32: Global symbol */
67 N_FNAME = 0x22, /**< 34: Function name (for BSD Fortran) */
68 N_FUN = 0x24, /**< 36: Function name */
69 N_STSYM = 0x26, /**< 38: Data segment file-scope variable */
70 N_LCSYM = 0x28, /**< 40: BSS segment file-scope variable */
71 N_MAIN = 0x2A, /**< 42: Name of main routine */
72 N_ROSYM = 0x2C, /**< 44: Variable in .rodata section */
73 N_PC = 0x30, /**< 48: Global symbol (for Pascal) */
74 N_NSYMS = 0x32, /**< 50: Number of symbols (according to Ultrix V4.0) */
75 N_NOMAP = 0x34, /**< 52: No DST map */
76 N_OBJ = 0x38, /**< 56: Object file (Solaris2) */
77 N_OPT = 0x3C, /**< 60: Debugger options (Solaris2) */
78 N_RSYM = 0x40, /**< 64: Register variable */
79 N_M2C = 0x42, /**< 66: Modula-2 compilation unit */
80 N_SLINE = 0x44, /**< 68: Line number in text segment */
81 N_DSLINE = 0x46, /**< 70: Line number in data segment */
82 N_BSLINE = 0x48, /**< 72: Line number in bss segment */
83 N_BROWS = 0x48, /**< 72: Sun source code browser, path to `.cb' file */
84 N_DEFD = 0x4A, /**< 74: GNU Modula2 definition module dependency */
85 N_FLINE = 0x4C, /**< 76: Function start/body/end line numbers (Solaris2) */
86 N_EHDECL = 0x50, /**< 80: GNU C++ exception variable */
87 N_MOD2 = 0x50, /**< 80: Modula2 info "for imc" (according to Ultrix V4.0) */
88 N_CATCH = 0x54, /**< 84: GNU C++ catch clause */
89 N_SSYM = 0x60, /**< 96: Structure of union element */
90 N_ENDM = 0x62, /**< 98: Last stab for module (Solaris2) */
91 N_SO = 0x64, /**< 100: Path and name of source file */
92 N_LSYM = 0x80, /**< 128: Stack variable */
93 N_BINCL = 0x82, /**< 130: Beginning of an include file (Sun only) */
94 N_SOL = 0x84, /**< 132: Name of include file */
95 N_PSYM = 0xA0, /**< 160: Parameter variable */
96 N_EINCL = 0xA2, /**< 162: End of an include file */
97 N_ENTRY = 0xA4, /**< 164: Alternate entry point */
98 N_LBRAC = 0xC0, /**< 192: Beginning of a lexical block */
99 N_EXCL = 0xC2, /**< 194: Place holder for a deleted include file */
100 N_SCOPE = 0xC4, /**< 196: Modula2 scope information (Sun linker) */
101 N_RBRAC = 0xE0, /**< 224: End of a lexical block */
102 N_BCOMM = 0xE2, /**< 226: Begin named common block */
103 N_ECOMM = 0xE4, /**< 228: End named common block */
104 N_ECOML = 0xE8, /**< 232: Member of a common block */
105 N_WITH = 0xEA, /**< 234: Pascal with statement: type,,0,0,offset (Solaris2) */
106 N_NBTEXT = 0xF0, /**< 240: Gould non-base registers */
107 N_NBDATA = 0xF2, /**< 242: Gould non-base registers */
108 N_NBBSS = 0xF4, /**< 244: Gould non-base registers */
109 N_NBSTS = 0xF6, /**< 246: Gould non-base registers */
110 N_NBLCS = 0xF8, /**< 248: Gould non-base registers */
116 typedef struct stabs_handle {
117 dbg_handle base; /**< the base class */
118 ir_entity *cur_ent; /**< current method entity */
119 const be_stack_layout_t *layout; /**< current stack layout */
120 unsigned next_type_nr; /**< next type number */
121 pmap *type_map; /**< a map from type to type number */
122 const char *main_file; /**< name of the main source file */
123 const char *curr_file; /**< name of the current source file */
129 * Returns the stabs type number of a Firm type.
131 static unsigned get_type_number(stabs_handle *h, ir_type *tp) {
136 /* map to the void type */
139 entry = pmap_find(h->type_map, tp);
141 num = h->next_type_nr++;
142 pmap_insert(h->type_map, tp, INT_TO_PTR(num));
144 num = PTR_TO_INT(entry->value);
147 } /* get_type_number */
150 * Map a given Type to void by assigned the type number 0.
152 static void map_to_void(stabs_handle *h, ir_type *tp) {
153 pmap_insert(h->type_map, tp, INT_TO_PTR(0));
157 * generate the void type.
159 static void gen_void_type(stabs_handle *h) {
161 be_emit_irprintf("\t.stabs\t\"void:t%u=%u\",%d,0,0,0\n", 0, 0, N_LSYM);
162 be_emit_write_line();
163 } /* gen_void_type */
165 typedef struct walker_env {
170 /* a type is not ready: put it on the wait queue */
171 #define SET_TYPE_NOT_READY(wq, tp) \
173 set_type_link(tp, (void *)1); \
178 #define SET_TYPE_READY(tp) set_type_link(tp, NULL)
180 /* check whether a type is ready */
181 #define IS_TYPE_READY(tp) (get_type_link(tp) == NULL)
183 #ifdef EXPLICITE_PTR_TYPES
184 #define SKIP_PTR(tp) tp
186 #define SKIP_PTR(tp) (is_Pointer_type(tp) ? get_pointer_points_to_type(tp) : tp)
190 * mode_info for output as decimal
192 static const tarval_mode_info dec_output = {
199 * emit a tarval as decimal
201 static void be_emit_tv_as_decimal(tarval *tv) {
202 ir_mode *mode = get_tarval_mode(tv);
203 const tarval_mode_info *old = get_tarval_mode_output_option(mode);
205 set_tarval_mode_output_option(mode, &dec_output);
207 set_tarval_mode_output_option(mode, old);
210 static void emit_type_name(const ir_type *type)
213 ir_print_type(buf, sizeof(buf), type);
218 * Generates a primitive type.
220 * @param h the stabs handle
223 static void gen_primitive_type(stabs_handle *h, ir_type *tp) {
224 ir_mode *mode = get_type_mode(tp);
228 if (mode == mode_T) {
229 /* firmcc, jack and the FirmJC compiler use mode_T for the void type.
230 Ignore it here as it's name is remapped to "void". */
235 if (0 && get_mode_size_bits(mode) & 7) {
236 /* this is a bitfield type, ignore it */
240 type_num = get_type_number(h, tp);
242 if (mode_is_int(mode)) {
243 be_emit_cstring("\t.stabs\t\"");
245 be_emit_irprintf(":t%u=r%u;", type_num, type_num);
246 be_emit_tv_as_decimal(get_mode_min(mode));
248 be_emit_tv_as_decimal(get_mode_max(mode));
249 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
250 be_emit_write_line();
251 } else if (mode_is_float(mode)) {
252 int size = get_type_size_bytes(tp);
253 be_emit_cstring("\t.stabs\t\"");
255 be_emit_irprintf(":t%u=r1;%d;0;\",%d,0,0,0\n", type_num, size, N_LSYM);
256 be_emit_write_line();
258 } /* gen_primitive_type */
261 * Generates an enum type
263 * @param h the stabs handle
266 static void gen_enum_type(stabs_handle *h, ir_type *tp) {
267 unsigned type_num = get_type_number(h, tp);
271 be_emit_cstring("\t.stabs\t\"");
273 be_emit_irprintf(":T%u=e", type_num);
274 for (i = 0, n = get_enumeration_n_enums(tp); i < n; ++i) {
275 ir_enum_const *ec = get_enumeration_const(tp, i);
278 tarval_snprintf(buf, sizeof(buf), get_enumeration_value(ec));
279 be_emit_irprintf("%s:%s,", get_enumeration_const_name(ec), buf);
281 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
282 be_emit_write_line();
283 } /* gen_enum_type */
286 * print a pointer type
288 void print_pointer_type(stabs_handle *h, ir_type *tp, int local) {
289 unsigned type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
290 ir_type *el_tp = get_pointer_points_to_type(tp);
291 unsigned el_num = get_type_number(h, el_tp);
293 be_emit_irprintf("%u=*%u", type_num, el_num);
297 * Generates a pointer type
299 * @param env the walker environment
302 static void gen_pointer_type(wenv_t *env, ir_type *tp) {
303 stabs_handle *h = env->h;
304 ir_type *el_tp = get_pointer_points_to_type(tp);
307 if (! IS_TYPE_READY(el_tp))
308 waitq_put(env->wq, el_tp);
310 be_emit_cstring("\t.stabs\t\"");
312 be_emit_cstring(":t");
313 print_pointer_type(h, tp, 0);
314 be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
315 be_emit_write_line();
316 } /* gen_pointer_type */
319 * print an array type
321 static void print_array_type(stabs_handle *h, ir_type *tp, int local) {
322 ir_type *etp = get_array_element_type(tp);
323 int i, n = get_array_n_dimensions(tp);
324 unsigned type_num = local ? h->next_type_nr++ : get_type_number(h, tp);
327 be_emit_irprintf("%u=a", type_num);
328 NEW_ARR_A(int, perm, n);
329 for (i = 0; i < n; ++i) {
330 perm[i] = get_array_order(tp, i);
333 for (i = 0; i < n; ++i) {
336 if (is_Const(get_array_lower_bound(tp, dim)) && is_Const(get_array_upper_bound(tp, dim))) {
337 long min = get_array_lower_bound_int(tp, dim);
338 long max = get_array_upper_bound_int(tp, dim);
340 /* FIXME r1 must be integer type, but seems to work for now */
341 be_emit_irprintf("r1;%ld;%ld;", min, max-1);
345 type_num = get_type_number(h, etp);
346 be_emit_irprintf("%d", type_num);
350 * Generates an array type
352 * @param env the walker environment
355 static void gen_array_type(wenv_t *env, ir_type *tp) {
356 stabs_handle *h = env->h;
357 ir_type *etp = get_array_element_type(tp);
360 if (! IS_TYPE_READY(etp))
361 waitq_put(env->wq, etp);
363 be_emit_cstring("\t.stabs\t\"");
365 be_emit_cstring(":t");
367 print_array_type(h, tp, 0);
369 be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
370 be_emit_write_line();
371 } /* gen_array_type */
374 * Generates a struct/union type
376 * @param env the walker environment
379 static void gen_struct_union_type(wenv_t *env, ir_type *tp) {
380 stabs_handle *h = env->h;
381 unsigned type_num = get_type_number(h, tp);
386 if (is_Struct_type(tp)) {
388 if (get_type_mode(tp) != NULL) {
389 /* this is a bitfield type, ignore it safely */
393 else if (is_Union_type(tp))
396 be_emit_cstring("\t.stabs\t\"");
398 be_emit_irprintf(":Tt%u=%c%d", type_num, desc, get_type_size_bytes(tp));
400 for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
401 ir_entity *ent = get_compound_member(tp, i);
402 ir_type *mtp = get_entity_type(ent);
406 if (! IS_TYPE_READY(mtp))
407 waitq_put(env->wq, mtp);
408 ofs = get_entity_offset(ent);
409 if (is_Struct_type(mtp) && get_type_mode(mtp) != NULL) {
410 /* this structure is a bitfield, skip */
413 for (i = 0, n = get_struct_n_members(mtp); i < n; ++i) {
414 ir_entity *ent = get_struct_member(mtp, i);
415 ir_type *tp = get_entity_type(ent);
418 type_num = get_type_number(h, tp);
419 size = get_type_size_bytes(tp) * 8;
420 bofs = (ofs + get_entity_offset(ent)) * 8 + get_entity_offset_bits_remainder(ent);
422 /* name:type, bit offset from the start of the struct', number of bits in the element. */
423 be_emit_irprintf("%s:%u,%d,%u;", get_entity_name(ent), type_num, bofs, size);
427 be_emit_irprintf("%s:", get_entity_name(ent));
429 if (is_Array_type(mtp)) {
430 /* use a local array definition */
431 print_array_type(h, mtp, 1);
432 } else if (is_Pointer_type(mtp)) {
433 /* use local pointer definition */
434 print_pointer_type(h, mtp, 1);
436 type_num = get_type_number(h, mtp);
438 /* name:type, bit offset from the start of the struct', number of bits in the element. */
439 be_emit_irprintf("%u", type_num);
441 size = get_type_size_bytes(mtp) * 8;
442 be_emit_irprintf(",%d,%u;", ofs * 8, size);
445 be_emit_irprintf(";\",%d,0,0,0\n", N_LSYM);
446 be_emit_write_line();
447 } /* gen_struct_type */
450 * Generates a method type
452 * @param env the walker environment
455 static void gen_method_type(wenv_t *env, ir_type *tp) {
456 stabs_handle *h = env->h;
457 unsigned type_num = get_type_number(h, tp);
459 unsigned res_type_num;
460 int i, n = get_method_n_ress(tp);
464 rtp = get_method_res_type(tp, 0);
465 if (! IS_TYPE_READY(rtp))
466 waitq_put(env->wq, rtp);
468 res_type_num = get_type_number(h, rtp);
470 be_emit_cstring("\t.stabs\t\"");
472 be_emit_irprintf(":t%u=f%u", type_num, res_type_num);
474 /* handle more than one return type */
475 for (i = 1; i < n; ++i) {
476 rtp = get_method_res_type(tp, i);
477 if (! IS_TYPE_READY(rtp))
478 waitq_put(env->wq, rtp);
479 res_type_num = get_type_number(h, rtp);
480 be_emit_irprintf(",%u", res_type_num);
482 be_emit_irprintf("\",%d,0,0,0\n", N_LSYM);
483 be_emit_write_line();
484 } /* gen_method_type */
487 * type-walker: generate declaration for simple types,
488 * put all other types on a wait queue
490 static void walk_type(type_or_ent tore, void *ctx)
495 if (is_type(tore.typ)) {
498 /* ignore the unknown type */
499 if (tp == firm_unknown_type)
505 switch (get_type_tpop_code(tp)) {
507 if (tp == get_glob_type()) {
514 gen_struct_union_type(env, tp);
517 case tpo_enumeration:
518 gen_enum_type(env->h, tp);
522 gen_primitive_type(env->h, tp);
526 gen_method_type(env, tp);
530 gen_array_type(env, tp);
534 gen_pointer_type(env, tp);
538 /* the unknown type: ignore */
542 assert(! "Unknown tpop code");
547 * generate declaration for all types
549 static void finish_types(wenv_t *env)
554 while (! waitq_empty(wq)) {
556 if (IS_TYPE_READY(tp))
559 switch (get_type_tpop_code(tp)) {
561 gen_method_type(env, tp);
566 gen_struct_union_type(env, tp);
568 case tpo_enumeration:
569 gen_enum_type(env->h, tp);
572 gen_primitive_type(env->h, tp);
575 gen_array_type(env, tp);
578 gen_pointer_type(env, tp);
581 /* the unknown type: ignore */
585 assert(! "Unknown tpop code");
591 * generate all types.
593 static void gen_types(stabs_handle *h) {
597 env.wq = new_waitq();
598 type_walk(NULL, walk_type, &env);
604 /* -------------------------- I/F ----------------------------- */
607 * start a new source object (compilation unit)
609 static void stabs_so(dbg_handle *handle, const char *filename) {
610 stabs_handle *h = (stabs_handle *)handle;
611 h->main_file = h->curr_file = filename;
612 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,.Ltext0\n", filename, N_SO);
613 be_emit_write_line();
619 static void stabs_main_program(dbg_handle *handle) {
620 ir_graph *irg = get_irp_main_irg();
624 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", get_entity_name(get_irg_entity(irg)), N_MAIN);
625 be_emit_write_line();
627 } /* stabs_main_program */
629 static void stabs_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
631 stabs_handle *handle = (stabs_handle*) h;
633 const char *fname = ir_retrieve_dbg_info(dbgi, &lineno);
638 if (handle->curr_file != fname) {
639 if (fname != handle->main_file) {
640 be_emit_irprintf("\t.stabs\t\"%s\",%d,0,0,0\n", fname, N_SOL);
641 be_emit_write_line();
643 handle->curr_file = fname;
645 if (handle->last_line != lineno) {
648 snprintf(label, sizeof(label), ".LM%u", ++handle->label_num);
649 handle->last_line = lineno;
651 be_emit_irprintf("\t.stabn\t%d, 0, %u, %s-%s\n", N_SLINE, lineno,
652 label, get_entity_ld_name(handle->cur_ent));
653 be_emit_write_line();
655 be_emit_string(label);
656 be_emit_cstring(":\n");
657 be_emit_write_line();
662 * dump the stabs for a method begin
664 static void stabs_method_begin(dbg_handle *handle, ir_entity *ent, const be_stack_layout_t *layout) {
665 stabs_handle *h = (stabs_handle *)handle;
668 int i, n, between_size;
673 /* create the method entry */
674 mtp = get_entity_type(ent);
675 if (is_lowered_type(mtp))
676 mtp = get_associated_type(mtp);
677 if (get_method_n_ress(mtp) > 0)
678 rtp = get_method_res_type(mtp, 0);
681 type_num = get_type_number(h, rtp);
682 be_emit_irprintf("\t.stabs\t\"%s:%c%u\",%u,0,0,%s\n",
683 get_entity_name(ent),
684 get_entity_visibility(ent) == visibility_external_visible ? 'F' : 'f',
687 get_entity_ld_name(ent));
688 be_emit_write_line();
690 /* create parameter entries */
691 between_size = get_type_size_bytes(layout->between_type);
692 for (i = 0, n = get_method_n_params(mtp); i < n; ++i) {
693 ir_type *ptp = get_method_param_type(mtp, i);
694 const char *name = get_method_param_name(mtp, i);
695 unsigned type_num = get_type_number(h, ptp);
698 ir_entity *stack_ent;
701 snprintf(buf, sizeof(buf), "arg%d", i);
704 /* check if this parameter has a stack entity. If it has, it
705 it transmitted on the stack, else in a register */
706 stack_ent = layout->param_map[i];
708 ofs = get_entity_offset(stack_ent) + between_size;
710 be_emit_irprintf("\t.stabs\t\"%s:p", name);
711 if (is_Array_type(ptp)) {
712 /* use a local array definition */
713 print_array_type(h, ptp, 1);
714 } else if (is_Pointer_type(ptp)) {
715 /* use local pointer definition */
716 print_pointer_type(h, ptp, 1);
718 type_num = get_type_number(h, ptp);
720 /* name:type, bit offset from the start of the struct', number of bits in the element. */
721 be_emit_irprintf("%u", type_num);
724 be_emit_irprintf("\",%d,0,0,%d\n", N_PSYM, ofs);
725 be_emit_write_line();
727 } /* stabs_method_begin */
730 * dump the stabs for a method end
732 static void stabs_method_end(dbg_handle *handle) {
733 stabs_handle *h = (stabs_handle *)handle;
734 ir_entity *ent = h->cur_ent;
735 const be_stack_layout_t *layout = h->layout;
736 const char *ld_name = get_entity_ld_name(ent);
737 int i, n, frame_size;
738 static unsigned scope_nr = 0;
740 /* create entries for automatic variables on the stack */
741 frame_size = get_type_size_bytes(layout->frame_type);
742 for (i = 0, n = get_compound_n_members(layout->frame_type); i < n; ++i) {
743 ir_entity *ent = get_compound_member(layout->frame_type, i);
748 /* ignore spill slots and other helper objects */
749 if (is_entity_compiler_generated(ent))
752 tp = get_entity_type(ent);
753 /* should not happen in backend but ... */
754 if (is_Method_type(tp))
756 type_num = get_type_number(h, tp);
757 ofs = -frame_size + get_entity_offset(ent);
759 be_emit_irprintf("\t.stabs\t\"%s:%u\",%d,0,0,%d\n",
760 get_entity_name(ent), type_num, N_LSYM, ofs);
761 be_emit_write_line();
763 /* we need a lexical block here */
764 be_emit_irprintf("\t.stabn\t%d,0,0,%s-%s\n", N_LBRAC, ld_name, ld_name);
765 be_emit_write_line();
766 be_emit_irprintf("\t.stabn\t%d,0,0,.Lscope%u-%s\n", N_RBRAC, scope_nr, ld_name);
767 be_emit_write_line();
768 be_emit_irprintf(".Lscope%u:\n", scope_nr);
769 be_emit_write_line();
774 } /* stabs_method_end */
779 static void stabs_types(dbg_handle *handle) {
780 stabs_handle *h = (stabs_handle *)handle;
782 /* allocate the zero for the void type */
789 * dump a variable in the global type
791 static void stabs_variable(dbg_handle *handle, ir_entity *ent) {
792 stabs_handle *h = (stabs_handle *)handle;
793 unsigned tp_num = get_type_number(h, get_entity_type(ent));
796 if (get_entity_visibility(ent) == visibility_external_visible) {
797 /* a global variable */
798 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:G%u\",%d,0,0,0\n",
799 get_entity_name(ent), tp_num, N_GSYM);
800 } else { /* some kind of local */
801 ir_variability variability = get_entity_variability(ent);
804 if (variability == variability_uninitialized)
806 else if (variability == variability_constant)
808 snprintf(buf, sizeof(buf), "\t.stabs\t\"%s:S%u\",%d,0,0,%s\n",
809 get_entity_name(ent), tp_num, kind, get_entity_ld_name(ent));
811 buf[sizeof(buf) - 1] = '\0';
814 } /* stabs_variable */
817 * Close the stabs handler.
819 static void stabs_close(dbg_handle *handle) {
820 stabs_handle *h = (stabs_handle *)handle;
821 pmap_destroy(h->type_map);
825 /** The stabs operations. */
826 static const debug_ops stabs_ops = {
837 /* Opens a stabs handler */
838 dbg_handle *be_stabs_open(void) {
839 stabs_handle *h = XMALLOCZ(stabs_handle);
841 h->base.ops = &stabs_ops;
842 h->type_map = pmap_create_ex(64);
847 void be_init_stabs(void)
849 be_register_dbgout_module("stabs", be_stabs_open);
852 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_stabs);