4 * (C) 2005 Michael Beck beck@ipd.info.uni-karlsruhe.de
6 #define WIN32_LEAN_AND_MEAN
14 /* ugly, but I must include array.h WITHOUT NDEBUG */
24 #include "irgraph_t.h"
30 #include "compound_path_t.h"
40 #include "firmEvaluator.h"
42 /** get the address of a pointer */
43 #define ADD_ADR(p, off) ((void *)((char *)(p) + (off)))
46 static void debug(char *fmt, ...)
52 vsprintf(buf, fmt, ap);
54 OutputDebugString(buf);
59 * return the size of a firm object
61 int get_firm_object_size(firm_kind kind)
64 case k_entity: /* an entity */
65 return sizeof(ir_entity);
66 case k_type: /* a type */
67 return sizeof(ir_type);
68 case k_ir_graph: /* an ir graph */
69 return sizeof(ir_graph);
70 case k_ir_node: /* an ir node */
71 return sizeof(ir_node);
72 case k_ir_mode: /* an ir mode */
73 return sizeof(ir_mode);
74 case k_ir_op: /* an ir opcode */
76 case k_tarval: /* a tarval */
77 return sizeof(tarval);
78 case k_ir_loop: /* a loop */
79 return sizeof(ir_loop);
80 case k_ir_compound_graph_path: /* a compound graph path, see entity.h */
81 return sizeof(compound_graph_path);
82 case k_ir_prog: /* a program representation (irp) */
83 return sizeof(ir_prog);
87 } /* get_firm_object_size */
90 * returns the string length of a string in debuggee space
92 * @return string length or negative value on error
94 static int strlen_debuggee(DEBUGHELPER *pHelper, const void *address, size_t max)
98 const char *p = address;
100 for (i = 0; i < max; ++i) {
101 if (copy_from_debuggee(p + i, pHelper, &v, sizeof(v)) != S_OK)
108 } /* strlen_debuggee */
113 HRESULT format_ident(DEBUGHELPER *pHelper, const void *address, char *pResult, size_t max)
115 set_entry *data = NULL;
119 if (copy_from_debuggee(address, pHelper, &id, sizeof(id)) != S_OK)
123 if (id.size < 1 || id.size > 256)
127 len = offsetof(set_entry, dptr) + slen;
131 if (copy_from_debuggee(address, pHelper, data, len) != S_OK)
134 _tcsncpy(pResult, (const char *)data->dptr, max);
141 static HRESULT format_tp_op(DEBUGHELPER *pHelper, const void *addr, char *pResult, size_t max)
145 #define X(a) case tpo_##a: _tcsncpy(pResult, #a, max); return S_OK
146 #define Y(a, b) case tpo_##a: _tcsncpy(pResult, b, max); return S_OK
148 if (copy_from_debuggee(addr, pHelper, &op, sizeof(op)) != S_OK)
158 Y(enumeration, "enum");
160 Y(primitive, "prim");
171 * Checks whether a type is the global type
173 * @param type the address of the type in debuggee's space
175 static HRESULT is_global_type(DEBUGHELPER *pHelper, const void *type, int *flag)
180 if (copy_from_debuggee(type, pHelper, &tp, sizeof(tp)) != S_OK)
183 *flag = tp.flags & tf_global_type;
185 } /* is_global_type */
190 static HRESULT format_entity(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max, int top)
197 if (copy_from_debuggee(addr, pHelper, &ent, sizeof(ent)) != S_OK) {
200 if (is_global_type(pHelper, ent.owner, &is_global) != S_OK)
205 _tcsncpy(pResult, "ENT: ", max);
208 if (copy_from_debuggee(ent.owner, pHelper, &owner, sizeof(owner)) != S_OK)
210 if (format_ident(pHelper, (void *)owner.name, name, sizeof(name)) != S_OK)
212 _tcsncat(pResult, name, max);
213 _tcsncat(pResult, "::", max);
216 if (format_ident(pHelper, (void *)ent.name, name, sizeof(name)) != S_OK)
218 _tcsncat(pResult, name, max);
222 _snprintf(name, sizeof(name), " [0x%lx]", ent.nr);
225 _snprintf(name, sizeof(name), " [0%lo]", ent.nr);
228 _snprintf(name, sizeof(name), " [%ld]", ent.nr);
230 _tcsncat(pResult, name, max);
233 } /* format_entity */
238 static HRESULT format_mode(DEBUGHELPER *pHelper, const void *addr, char *pResult, size_t max)
242 if (copy_from_debuggee(addr, pHelper, &mode, sizeof(mode)) != S_OK)
244 if (format_ident(pHelper, mode.name, pResult, max) != S_OK)
252 static HRESULT format_type(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max, int top)
257 if (copy_from_debuggee(addr, pHelper, &tp, sizeof(tp)) != S_OK)
262 if (format_tp_op(pHelper, tp.type_op, pResult, max) != S_OK)
265 _tcsncat(pResult, " ", max);
270 if (format_ident(pHelper, tp.name, name, sizeof(name)) != S_OK)
273 if (format_mode(pHelper, tp.mode, name, sizeof(name)) != S_OK)
277 _tcsncat(pResult, name, max);
280 _snprintf(name, sizeof(name), " [0x%lx]", tp.nr);
283 _snprintf(name, sizeof(name), " [0%lo]", tp.nr);
286 _snprintf(name, sizeof(name), " [%ld]", tp.nr);
288 _tcsncat(pResult, name, max);
296 static HRESULT format_irg(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max, int top)
301 if (copy_from_debuggee(addr, pHelper, &irg, sizeof(irg)) != S_OK)
306 _tcsncpy(pResult, "IRG: ", max);
313 if (copy_from_debuggee(irg.ent, pHelper, &ent, sizeof(ent)) != S_OK)
315 if (is_global_type(pHelper, ent.owner, &is_global) != S_OK)
318 if (copy_from_debuggee(ent.owner, pHelper, &owner, sizeof(owner)) != S_OK)
320 if (format_ident(pHelper, (void *)owner.name, name, sizeof(name)) != S_OK)
322 _tcsncat(pResult, name, max);
323 _tcsncat(pResult, "::", max);
325 if (format_ident(pHelper, ent.name, name, sizeof(name)) != S_OK)
327 _tcsncat(pResult, name, max);
330 _tcsncat(pResult, "NULL", max);
334 _snprintf(name, sizeof(name), " [0x%lx, 0x%u nodes]", irg.graph_nr, irg.last_node_idx);
337 _snprintf(name, sizeof(name), " [0%lo, 0%o nodes]", irg.graph_nr, irg.last_node_idx);
340 _snprintf(name, sizeof(name), " [%ld, %u nodes]", irg.graph_nr, irg.last_node_idx);
342 _tcsncat(pResult, name, max);
349 HRESULT format_op(DEBUGHELPER *pHelper, const void *addr, char *pResult, size_t max)
353 if (copy_from_debuggee(addr, pHelper, &op, sizeof(op)) != S_OK)
355 if (format_ident(pHelper, op.name, pResult, max) != S_OK)
360 /** get a temporary string */
361 #define get_string(str) \
366 len = strlen_debuggee(pHelper, str, 256); \
369 s = alloca(len + 1); \
370 if (copy_from_debuggee(str, pHelper, s, (DWORD)len) != S_OK) \
380 static HRESULT format_tarval(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max)
386 tarval_mode_info modinfo;
389 static int initialized = 0;
392 /* from init_tarval_1() */
400 if (copy_from_debuggee(addr, pHelper, &tv, sizeof(tv)) != S_OK)
407 if (copy_from_debuggee(tv.mode, pHelper, &mode, sizeof(mode)) != S_OK)
412 if (mode_is_int(&mode)) {
415 modinfo.mode_output = TVO_HEX;
416 modinfo.mode_prefix = "0x";
417 modinfo.mode_suffix = "";
420 modinfo.mode_output = TVO_OCTAL;
421 modinfo.mode_prefix = "0";
422 modinfo.mode_suffix = "";
425 modinfo.mode_output = TVO_DECIMAL;
426 modinfo.mode_prefix = "";
427 modinfo.mode_suffix = "";
432 if (copy_from_debuggee(mode.tv_priv, pHelper, &modinfo, sizeof(modinfo)) != S_OK)
435 get_string(modinfo.mode_prefix);
436 get_string(modinfo.mode_suffix);
439 mode.tv_priv = &modinfo;
448 if (copy_from_debuggee(tv.value, pHelper, value, len) != S_OK)
453 tarval_snprintf(pResult, max, &tv);
456 /* might be a reserved tarval */
457 int resid = PTR_TO_INT(tv.value);
460 case resid_tarval_bad:
461 _tcsncat(pResult, "BAD", max);
463 case resid_tarval_undefined:
464 _tcsncat(pResult, "UNDEF", max);
466 case resid_tarval_b_false:
467 _tcsncat(pResult, "FALSE", max);
469 case resid_tarval_b_true:
470 _tcsncat(pResult, "TRUE", max);
474 tarval_snprintf(pResult, max, &tv);
478 } /* format_tarval */
483 static HRESULT format_node(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max, int top)
489 if (copy_from_debuggee(addr, pHelper, &n, sizeof(n)) != S_OK)
493 if (format_op(pHelper, n.op, pResult, max) != S_OK)
497 if (format_mode(pHelper, n.mode, name, sizeof(name)) != S_OK)
499 _tcsncat(pResult, name, max);
501 if (copy_from_debuggee(n.op, pHelper, &op, sizeof(op)) != S_OK)
504 /* show show node attributes */
507 if (format_tarval(pHelper, nBase, n.attr.con.tarval, name, sizeof(name)) != S_OK) {
508 _tcsncat(pResult, "<???>", max);
511 _tcsncat(pResult, "<", max);
512 _tcsncat(pResult, name, max);
513 _tcsncat(pResult, ">", max);
517 _tcsncat(pResult, "<", max);
518 switch (n.attr.symc.kind) {
519 case symconst_type_size:
520 _tcsncat(pResult, "SIZE:", max);
521 if (format_type(pHelper, nBase, n.attr.symc.sym.type_p, name, sizeof(name), 0) != S_OK)
523 _tcsncat(pResult, name, max);
525 case symconst_type_align:
526 _tcsncat(pResult, "ALGN:", max);
527 if (format_type(pHelper, nBase, n.attr.symc.sym.type_p, name, sizeof(name), 0) != S_OK)
529 _tcsncat(pResult, name, max);
531 case symconst_addr_ent:
532 _tcsncat(pResult, "ENT:", max);
533 if (format_entity(pHelper, nBase, n.attr.symc.sym.entity_p, name, sizeof(name), 0) != S_OK)
535 _tcsncat(pResult, name, max);
538 _tcsncat(pResult, ">", max);
541 _tcsncat(pResult, "<", max);
542 if (format_entity(pHelper, nBase, n.attr.sel.entity, name, sizeof(name), 0) != S_OK)
544 _tcsncat(pResult, name, max);
545 _tcsncat(pResult, ">", max);
548 _tcsncat(pResult, "<", max);
549 if (format_type(pHelper, nBase, n.attr.cast.type, name, sizeof(name), 0) != S_OK)
551 _tcsncat(pResult, name, max);
552 _tcsncat(pResult, ">", max);
555 _tcsncat(pResult, "<", max);
556 if (format_type(pHelper, nBase, n.attr.alloc.type, name, sizeof(name), 0) != S_OK)
558 _tcsncat(pResult, name, max);
559 _snprintf(name, sizeof(name), ", %s", n.attr.alloc.where == stack_alloc ? "stack" : "heap");
560 _tcsncat(pResult, name, max);
561 _tcsncat(pResult, ">", max);
564 _tcsncat(pResult, "<", max);
565 if (format_type(pHelper, nBase, n.attr.free.type, name, sizeof(name), 0) != S_OK)
567 _tcsncat(pResult, name, max);
568 _snprintf(name, sizeof(name), ", %s", n.attr.free.where == stack_alloc ? "stack" : "heap");
569 _tcsncat(pResult, name, max);
570 _tcsncat(pResult, ">", max);
573 _tcsncat(pResult, "<", max);
574 if (format_type(pHelper, nBase, n.attr.copyb.type, name, sizeof(name), 0) != S_OK)
576 _tcsncat(pResult, name, max);
577 _tcsncat(pResult, ">", max);
583 _snprintf(name, sizeof(name), " [0x%lx:0x%x]", n.node_nr, n.node_idx);
586 _snprintf(name, sizeof(name), " [0%lo:0%o]", n.node_nr, n.node_idx);
589 _snprintf(name, sizeof(name), " [%ld:%u]", n.node_nr, n.node_idx);
591 _tcsncat(pResult, name, max);
599 static HRESULT format_loop(DEBUGHELPER *pHelper, const void *addr, char *pResult, size_t max)
603 if (copy_from_debuggee(addr, pHelper, &loop, sizeof(loop)) != S_OK)
609 * Get an array descriptor
611 static HRESULT get_array_desc(DEBUGHELPER *pHelper, const void *address, ir_arr_descr *desc)
613 address = ARR_DESCR(address);
614 if (copy_from_debuggee(address, pHelper, desc, sizeof(*desc)) != S_OK)
618 } /* get_array_desc */
623 static HRESULT format_prog(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max)
626 ir_arr_descr graphs, types;
629 if (copy_from_debuggee(addr, pHelper, &irp, sizeof(irp)) != S_OK)
632 if (get_array_desc(pHelper, irp.graphs, &graphs) != S_OK)
635 irp.graphs = (ir_graph**)&graphs.v.elts;
639 if (get_array_desc(pHelper, irp.types, &types) != S_OK)
642 irp.types = (ir_type**)&types.v.elts;
647 _snprintf(name, sizeof(name), "0x%x graphs 0x%x types", ARR_LEN(irp.graphs), ARR_LEN(irp.types));
650 _snprintf(name, sizeof(name), "0%o graphs 0%o types", ARR_LEN(irp.graphs), ARR_LEN(irp.types));
653 _snprintf(name, sizeof(name), "%d graphs %d types", ARR_LEN(irp.graphs), ARR_LEN(irp.types));
655 _tcsncpy(pResult, name, max);
661 * Format an array descriptor
663 HRESULT format_arr_descr(DEBUGHELPER *pHelper, int nBase, const void *addr, char *pResult, size_t max)
668 if (copy_from_debuggee(addr, pHelper, &desc, sizeof(desc)) != S_OK)
671 switch (desc.magic) {
673 _tcsncpy(pResult, "DynArr ", max); break;
675 _tcsncpy(pResult, "AutoArr ", max); break;
677 _tcsncpy(pResult, "FlexArr ", max); break;
679 _tcsncpy(pResult, "UNKN ", max);
684 _snprintf(name, sizeof(name), "nelts 0x%x", desc.nelts);
687 _snprintf(name, sizeof(name), "nelts 0%o", desc.nelts);
690 _snprintf(name, sizeof(name), "nelts %d", desc.nelts);
692 _tcsncat(pResult, name, max);
695 } /* format_arr_descr */
698 * format a firm object
700 HRESULT FormatFirmObject(DEBUGHELPER *pHelper, int nBase, firm_kind kind, const void *addr, char *pResult, size_t max)
703 case k_entity: /* an entity */
704 return format_entity(pHelper, nBase, addr, pResult, max, 1);
705 case k_type: /* a type */
706 return format_type(pHelper, nBase, addr, pResult, max, 1);
707 case k_ir_graph: /* an ir graph */
708 return format_irg(pHelper, nBase, addr, pResult, max, 1);
709 case k_ir_node: /* an ir node */
710 return format_node(pHelper, nBase, addr, pResult, max, 1);
711 case k_ir_mode: /* an ir mode */
712 return format_mode(pHelper, addr, pResult, max);
713 case k_ir_op: /* an ir opcode */
714 return format_op(pHelper, addr, pResult, max);
715 case k_tarval: /* a tarval */
716 return format_tarval(pHelper, nBase, addr, pResult, max);
717 case k_ir_loop: /* a loop */
718 return format_loop(pHelper, addr, pResult, max);
719 case k_ir_compound_graph_path: /* a compound graph path, see entity.h */
721 case k_ir_prog: /* a program representation (irp) */
722 return format_prog(pHelper, nBase, addr, pResult, max);
726 } /* FormatFirmObject */
728 #define SEGMENT_SIZE_SHIFT 8
729 #define SEGMENT_SIZE (1 << SEGMENT_SIZE_SHIFT)
730 #define DIRECTORY_SIZE_SHIFT 8
731 #define DIRECTORY_SIZE (1 << DIRECTORY_SIZE_SHIFT)
732 #define MAX_LOAD_FACTOR 4
734 typedef struct pset_element {
735 struct pset_element *chain; /**< for chaining Elements */
737 } pset_Element, *pset_Segment;
739 /* not visible from outside */
741 unsigned p; /**< Next bucket to be split */
742 unsigned maxp; /**< upper bound on p during expansion */
743 unsigned nkey; /**< current # keys */
744 unsigned nseg; /**< current # segments */
745 pset_Segment *dir[DIRECTORY_SIZE];
746 int (*cmp)(); /**< function comparing entries */
747 unsigned iter_i, iter_j;
748 pset_Element *iter_tail; /**< non-NULL while iterating over elts */
749 pset_Element *free_list; /**< list of free Elements */
750 struct obstack obst; /**< obstack for allocation all data */
753 typedef struct set_element {
754 struct set_element *chain; /**< for chaining Elements */
756 } set_Element, *set_Segment;
758 /* not visible from outside */
760 unsigned p; /**< Next bucket to be split */
761 unsigned maxp; /**< upper bound on p during expansion */
762 unsigned nkey; /**< current # keys */
763 unsigned nseg; /**< current # segments */
764 set_Segment *dir[DIRECTORY_SIZE];
765 int (*cmp)(); /**< function comparing entries */
766 unsigned iter_i, iter_j;
767 set_Element *iter_tail; /**< non-NULL while iterating over elts */
768 struct obstack obst; /**< obstack for allocation all data */
772 * Find the longest chain of a pset
774 static HRESULT find_longest_pset_chain(DEBUGHELPER *pHelper, pset *set,
775 int *chains, int *lenght, size_t *size)
778 pset_Segment *seg, *curr;
781 int len, nchains = 0, max_len = 0;
784 for (i = 0; i < set->nseg; ++i) {
787 dyns += sizeof(seg[j]) * SEGMENT_SIZE;
788 for (j = 0; j < SEGMENT_SIZE; ++j) {
789 if (copy_from_debuggee(&seg[j], pHelper, &curr, sizeof(curr)) != S_OK)
795 for (len = 0; address != NULL; address = elem.chain) {
796 if (copy_from_debuggee(address, pHelper, &elem, sizeof(elem)) != S_OK)
798 dyns += sizeof(pset_Element);
810 } /* find_longest_pset_chain */
813 * Find the longest chain of a set
815 static HRESULT find_longest_set_chain(DEBUGHELPER *pHelper, set *set,
816 int *chains, int *lenght, size_t *size)
819 set_Segment *seg, *curr;
822 int len, nchains = 0, max_len = 0;
825 for (i = 0; i < set->nseg; ++i) {
828 dyns += sizeof(seg[j]) * SEGMENT_SIZE;
829 for (j = 0; j < SEGMENT_SIZE; ++j) {
830 if (copy_from_debuggee(&seg[j], pHelper, &curr, sizeof(curr)) != S_OK)
836 for (len = 0; address != NULL; address = elem.chain) {
837 if (copy_from_debuggee(address, pHelper, &elem, sizeof(elem)) != S_OK)
839 dyns += offsetof(set_Element, entry.dptr) + elem.entry.size;
851 } /* find_longest_set_chain */
856 HRESULT format_pset(DEBUGHELPER *pHelper, int nBase, const void *address, char *pResult, size_t max)
860 int nchains, chain_len;
863 if (copy_from_debuggee(address, pHelper, &set, sizeof(set)) != S_OK)
866 if (find_longest_pset_chain(pHelper, &set, &nchains, &chain_len, &size) != S_OK)
871 _snprintf(name, sizeof(name), "nkey 0x%x nseg 0x%x nchain 0x%x maxlen 0x%x size %u kB", set.nkey, set.nseg, nchains, chain_len, (size + 1023) >> 10);
874 _snprintf(name, sizeof(name), "nkey 0%o nseg 0%o nchain 0%o maxlen 0%o size %u kB", set.nkey, set.nseg, nchains, chain_len, (size + 1023) >> 10);
877 _snprintf(name, sizeof(name), "nkey %u nseg %d nchain %d maxlen %d size %u kB", set.nkey, set.nseg, nchains, chain_len, (size + 1023) >> 10);
879 _tcsncpy(pResult, name, max);
887 HRESULT format_set(DEBUGHELPER *pHelper, int nBase, const void *address, char *pResult, size_t max)
891 int nchains, chain_len;
894 if (copy_from_debuggee(address, pHelper, &set, sizeof(set)) != S_OK)
897 if (find_longest_set_chain(pHelper, &set, &nchains, &chain_len, &size) != S_OK)
902 _snprintf(name, sizeof(name), "nkey 0x%x nseg 0x%x nchain 0x%x maxlen 0x%x size %u kB", set.nkey, set.nseg, nchains, chain_len, (size + 1023) >> 10);
905 _snprintf(name, sizeof(name), "nkey 0%o nseg 0%o nchain 0%o maxlen 0%o size %u kB", set.nkey, set.nseg, nchains, chain_len, (size + 1023) >> 10);
908 _snprintf(name, sizeof(name), "nkey %u nseg %d nchain %d maxlen %d size %u kB", set.nkey, set.nseg, nchains, chain_len, (size + 1023) >> 10);
910 _tcsncpy(pResult, name, max);
916 unsigned magic; /**< debug magic, only available in DEBUG builds */
917 pdeq *l_end, *r_end; /**< left and right ends of the queue */
918 pdeq *l, *r; /**< left and right neighbor */
919 int n; /**< number of elements in the current chunk */
920 int p; /**< the read/write pointer */
921 const void *data[1]; /**< storage for elements */
924 /** Returns the length of a double ended pointer list. */
925 static int get_pdeq_len(DEBUGHELPER *pHelper, pdeq *dq)
934 if (copy_from_debuggee(q, pHelper, &pdeq, sizeof(pdeq)) != S_OK)
944 if (copy_from_debuggee(q, pHelper, &pdeq, sizeof(pdeq)) != S_OK)
955 HRESULT format_pdeq(DEBUGHELPER *pHelper, int nBase, const void *address, char *pResult, size_t max)
961 if (copy_from_debuggee(address, pHelper, &pdeq, sizeof(pdeq)) != S_OK)
964 len = get_pdeq_len(pHelper, &pdeq);
970 _snprintf(name, sizeof(name), "pdeq 0x%x elem", len);
973 _snprintf(name, sizeof(name), "pdeq 0%o elem", len);
976 _snprintf(name, sizeof(name), "pdeq %d elem", len);
978 _tcsncpy(pResult, name, max);
983 /** show the first 2 units */
984 static HRESULT fill_bits(DEBUGHELPER *pHelper, bitset_t *bs, char *pResult)
986 unsigned i, units = bs->size;
987 int l = 0, o = 0, breaked = 0;
990 for (i = 0; i < units; ++i) {
993 if (copy_from_debuggee((void *)(&bs->data[i]), pHelper, &data, sizeof(data)) != S_OK)
996 for (j = 0; j < 32; ++j) {
997 if (data & (1 << j)) {
998 sprintf(pResult + l, "%d,", i * sizeof(data) * 8 + j);
999 l += strlen(pResult + l);
1010 sprintf(pResult + l, "...");
1013 sprintf(pResult + l, "}");
1020 HRESULT format_bitset(DEBUGHELPER *pHelper, int nBase, const void *address, char *pResult, size_t max)
1026 if (copy_from_debuggee(address, pHelper, &bs, sizeof(bs)) != S_OK)
1031 _snprintf(name, sizeof(name), "bitset{0x%x:", bs.size);
1034 _snprintf(name, sizeof(name), "bitset{0%o:", bs.size);
1037 _snprintf(name, sizeof(name), "bitset{%u:", bs.size);
1041 if (fill_bits(pHelper, &bs, &name[l]) != S_OK)
1045 _tcsncpy(pResult, name, max);
1048 } /* format_bitset */