20a0ae13845dcdbc929d8157f8de37ad740a6375
[libfirm] / ir / ir / irdumptxt.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Write text representation of firm to file.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Hubert Schmidt,
24  *          Matthias Braun
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdbool.h>
33
34 #include "irdump_t.h"
35 #include "irgraph_t.h"
36
37 #include "irprog_t.h"
38 #include "entity_t.h"
39 #include "trouts.h"
40 #include "irgwalk.h"
41 #include "tv_t.h"
42 #include "irprintf.h"
43 #include "error.h"
44
45 #include "irdom.h"
46
47 static ir_dump_verbosity_t  verbosity = dump_verbosity_max;
48
49 void ir_set_dump_verbosity(ir_dump_verbosity_t new_verbosity)
50 {
51         verbosity = new_verbosity;
52 }
53
54 ir_dump_verbosity_t ir_get_dump_verbosity(void)
55 {
56         return verbosity;
57 }
58
59 /* Write the irnode and all its attributes to the file passed. */
60 void dump_irnode_to_file(FILE *F, ir_node *n)
61 {
62         char     comma;
63         ir_graph *irg;
64
65         dump_node_opcode(F, n);
66         fprintf(F, " %ld\n", get_irn_node_nr(n));
67
68         fprintf(F, "  index: %u\n", get_irn_idx(n));
69         if (ir_get_dump_flags() & ir_dump_flag_analysed_types)
70                 fprintf (F, "  addr:    %p\n", (void *)n);
71         fprintf (F, "  mode:    %s\n", get_mode_name(get_irn_mode(n)));
72         fprintf (F, "  visited: %lu\n", get_irn_visited(n));
73         irg = get_irn_irg(n);
74         if (irg != get_const_code_irg())
75                 fprintf (F, "  irg:     %s\n", get_ent_dump_name(get_irg_entity(irg)));
76
77         if (get_irn_pinned(n) == op_pin_state_floats &&
78                 get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats) {
79                 fprintf(F, "  node was pinned in ");
80                 dump_node_opcode(F, get_irn_n(n, -1));
81                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, -1)));
82         }
83
84         fprintf(F, "  arity:   %d\n", get_irn_arity(n));
85         /* show all predecessor nodes */
86         fprintf(F, "  pred nodes:\n");
87         if (!is_Block(n)) {
88                 fprintf(F, "    -1:    ");
89                 dump_node_opcode(F, get_irn_n(n, -1));
90                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, -1)));
91         }
92
93         {
94                 int i;
95                 for (i = 0; i < get_irn_arity(n); ++i) {
96                         fprintf(F, "     %d: %s ", i, is_backedge(n, i) ? "be" : "  ");
97                         dump_node_opcode(F, get_irn_n(n, i));
98                         fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, i)));
99                 }
100         }
101
102         fprintf(F, "  Private Attributes:\n");
103
104         if (is_Proj(n))
105                 fprintf(F, "  proj nr: %ld\n", get_Proj_proj(n));
106
107         if (is_fragile_op(n)) {
108                 fprintf(F, "  pinned state: %s\n", get_op_pin_state_name(get_irn_pinned(n)));
109                 /* not dumped: frag array */
110         }
111
112         /* This is not nice, output it as a marker in the predecessor list. */
113         if (is_Block(n) || get_irn_op(n) == op_Phi) {
114             int i;
115                 fprintf(F, "  backedges:");
116                 comma = ' ';
117                 for (i = 0; i < get_irn_arity(n); i++)
118                         if (is_backedge(n, i)) { fprintf(F, "%c %d", comma, i); comma = ','; }
119                         fprintf(F, "\n");
120         }
121
122         /* Loop node.   Someone else please tell me what's wrong ... */
123         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO)) {
124                 ir_loop *loop = get_irn_loop(n);
125                 if (loop != NULL) {
126                         fprintf(F, "  in loop %ld with depth %u\n",
127                                 get_loop_loop_nr(loop), get_loop_depth(loop));
128                 }
129         }
130
131         /* Source types */
132         switch (get_irn_opcode(n)) {
133         case iro_Block: {
134                 if (has_Block_entity(n))
135                         fprintf(F, "  Label: %lu\n", get_entity_label(get_Block_entity(n)));
136                 fprintf(F, "  block visited: %lu\n", get_Block_block_visited(n));
137                 fprintf(F, "  block marked: %u\n", get_Block_mark(n));
138                 if (is_irg_state(get_irn_irg(n), IR_GRAPH_STATE_CONSISTENT_DOMINANCE)) {
139                         fprintf(F, "  dom depth %d\n", get_Block_dom_depth(n));
140                         fprintf(F, "  domtree pre num %u\n", get_Block_dom_tree_pre_num(n));
141                         fprintf(F, "  max subtree pre num %u\n", get_Block_dom_max_subtree_pre_num(n));
142                 }
143                 if (is_irg_state(get_irn_irg(n), IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE)) {
144                         fprintf(F, "  pdom depth %d\n", get_Block_postdom_depth(n));
145                         fprintf(F, "  pdomtree pre num %u\n", get_Block_pdom_tree_pre_num(n));
146                         fprintf(F, "  max pdomsubtree pre num %u\n", get_Block_pdom_max_subtree_pre_num(n));
147                 }
148
149                 /* not dumped: graph_arr */
150                 /* not dumped: mature    */
151         }  break;
152         case iro_Start: {
153                 size_t   i;
154                 ir_type *tp = get_entity_type(get_irg_entity(get_irn_irg(n)));
155                 ir_fprintf(F, "  start of method of type %+F\n", tp);
156                 for (i = 0; i < get_method_n_params(tp); ++i)
157                         ir_fprintf(F, "    param %d type: %+F\n", i, get_method_param_type(tp, i));
158         } break;
159         case iro_Cond: {
160                 fprintf(F, "  default ProjNr: %ld\n", get_Cond_default_proj(n));
161                 if (get_Cond_jmp_pred(n) != COND_JMP_PRED_NONE) {
162                         fprintf(F, "  jump prediction: %s\n",
163                                 get_cond_jmp_predicate_name(get_Cond_jmp_pred(n)));
164                 }
165         } break;
166         case iro_Alloc: {
167                 ir_fprintf(F, "  allocating entity of type: %+F\n", get_Alloc_type(n));
168                 fprintf(F, "  allocating on: the %s\n", (get_Alloc_where(n) == stack_alloc) ? "stack" : "heap");
169         } break;
170         case iro_Free: {
171                 ir_fprintf(F, "  freeing entity of type %+F\n", get_Free_type(n));
172                 fprintf(F, "  allocated on: the %s\n", (get_Free_where(n) == stack_alloc) ? "stack" : "heap");
173         } break;
174         case iro_Sel: {
175                 ir_entity *ent = get_Sel_entity(n);
176                 if (ent) {
177                         fprintf(F, "  Selecting entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
178                         ir_fprintf(F, "    of type    %+F\n",  get_entity_type(ent));
179                         ir_fprintf(F, "    with owner %+F.\n", get_entity_owner(ent));
180                 } else {
181                         fprintf(F, "  <NULL entity>\n");
182                 }
183         } break;
184         case iro_Call: {
185                 ir_type *tp = get_Call_type(n);
186                 if (get_Call_tail_call(n))
187                         fprintf(F, "  tail call\n");
188                 ir_fprintf(F, "  calling method of type %+F\n", tp);
189                 if (get_unknown_type() != tp) {
190                         size_t i;
191                         for (i = 0; i < get_method_n_params(tp); ++i)
192                                 ir_fprintf(F, "    param %d type: %+F\n", i, get_method_param_type(tp, i));
193                         for (i = 0; i < get_method_n_ress(tp); ++i)
194                                 ir_fprintf(F, "    result %d type: %+F\n", i, get_method_res_type(tp, i));
195                 }
196                 if (Call_has_callees(n)) {
197                         size_t i;
198                         fprintf(F, "  possible callees:\n");
199                         for (i = 0; i < get_Call_n_callees(n); i++) {
200                                 ir_fprintf(F, "    %zu: %s\n", i, get_ent_dump_name(get_Call_callee(n, i)));
201                         }
202                 }
203         } break;
204         case iro_Cast: {
205                 ir_fprintf(F, "  cast to type: %+F\n", get_Cast_type(n));
206         } break;
207         case iro_Cmp: {
208                 ir_relation relation = get_Cmp_relation(n);
209                 ir_fprintf(F, "  relation: %s\n", get_relation_string(relation));
210         } break;
211         case iro_Return: {
212                 size_t   i;
213                 ir_type *tp = get_entity_type(get_irg_entity(get_irn_irg(n)));
214                 ir_fprintf(F, "  return in method of type %+F\n", tp);
215                 for (i = 0; i < get_method_n_ress(tp); ++i) {
216                         ir_fprintf(F, "    result %d type: %+F\n", i,
217                                            get_method_res_type(tp, i));
218                 }
219         } break;
220         case iro_SymConst: {
221                 switch (get_SymConst_kind(n)) {
222                 case symconst_addr_ent:
223                         fprintf(F, "  kind:   addr_ent\n");
224                         fprintf(F, "  entity: ");
225                         dump_entity_to_file(F, get_SymConst_entity(n));
226                         break;
227                 case symconst_ofs_ent:
228                         fprintf(F, "  kind:   offset\n");
229                         fprintf(F, "  entity: ");
230                         dump_entity_to_file(F, get_SymConst_entity(n));
231                         break;
232                 case symconst_type_tag:
233                         fprintf(F, "  kind: type_tag\n");
234                         fprintf(F, "  type: ");
235                         dump_type_to_file(F, get_SymConst_type(n));
236                         break;
237                 case symconst_type_size:
238                         fprintf(F, "  kind: size\n");
239                         fprintf(F, "  type: ");
240                         dump_type_to_file(F, get_SymConst_type(n));
241                         break;
242                 case symconst_type_align:
243                         fprintf(F, "  kind: alignment\n");
244                         fprintf(F, "  type: ");
245                         dump_type_to_file(F, get_SymConst_type(n));
246                         break;
247                 case symconst_enum_const:
248                         fprintf(F, "  kind: enumeration\n");
249                         fprintf(F, "  name: %s\n", get_enumeration_const_name(get_SymConst_enum(n)));
250                         break;
251                 }
252         } break;
253         case iro_Load:
254                 fprintf(F, "  mode of loaded value: %s\n", get_mode_name_ex(get_Load_mode(n), NULL));
255                 fprintf(F, "  volatility: %s\n", get_volatility_name(get_Load_volatility(n)));
256                 fprintf(F, "  align: %s\n", get_align_name(get_Load_unaligned(n)));
257                 break;
258         case iro_Store:
259                 fprintf(F, "  volatility: %s\n", get_volatility_name(get_Store_volatility(n)));
260                 fprintf(F, "  align: %s\n", get_align_name(get_Store_unaligned(n)));
261                 break;
262         case iro_Confirm:
263                 fprintf(F, "  compare operation: %s\n", get_relation_string(get_Confirm_relation(n)));
264                 break;
265         case iro_ASM: {
266                 const ir_asm_constraint *cons;
267                 ident **clobber;
268                 int l;
269
270                 fprintf(F, "  assembler text: %s", get_id_str(get_ASM_text(n)));
271                 l = get_ASM_n_input_constraints(n);
272                 if (l > 0) {
273                         int i;
274                         fprintf(F, "\n  inputs:  ");
275                         cons = get_ASM_input_constraints(n);
276                         for (i = 0; i < l; ++i)
277                                 fprintf(F, "%%%u %s ", cons[i].pos, get_id_str(cons[i].constraint));
278                 }
279                 l = get_ASM_n_output_constraints(n);
280                 if (l > 0) {
281                         int i;
282                         fprintf(F, "\n  outputs: ");
283                         cons = get_ASM_output_constraints(n);
284                         for (i = 0; i < l; ++i)
285                                 fprintf(F, "%%%u %s ", cons[i].pos, get_id_str(cons[i].constraint));
286                 }
287                 l = get_ASM_n_clobbers(n);
288                 if (l > 0) {
289                         int i;
290                         fprintf(F, "\n  clobber: ");
291                         clobber = get_ASM_clobbers(n);
292                         for (i = 0; i < l; ++i)
293                                 fprintf(F, "%s ", get_id_str(clobber[i]));
294                 }
295                 if (get_irn_pinned(n) != op_pin_state_floats)
296                         fprintf(F, "\n  volatile");
297                 fprintf(F, "\n");
298         } break;
299
300         default:
301                 break;
302         }
303
304         if (get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_consistent  ||
305                 get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_inconsistent  )
306                 if (get_irn_typeinfo_type(n) != firm_none_type)
307                         ir_fprintf (F, "  Analysed type: %s\n", get_irn_typeinfo_type(n));
308 }
309
310 void dump_graph_as_text(FILE *out, ir_graph *irg)
311 {
312         fprintf(out, "graph %s\n", get_irg_dump_name(irg));
313 }
314
315 static int need_nl = 1;
316
317 static bool is_init_string(ir_initializer_t const* const init, ir_type *const type)
318 {
319         ir_type *const element_type = get_array_element_type(type);
320         ir_mode *      mode;
321         size_t         n;
322         size_t         i;
323
324         if (!is_Primitive_type(element_type))
325                 return false;
326
327         mode = get_type_mode(element_type);
328         if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
329                 return false;
330
331         n = get_initializer_compound_n_entries(init);
332         for (i = 0; i != n; ++i) {
333                 ir_initializer_t const* const val = get_initializer_compound_value(init, i);
334                 ir_tarval*              const tv  = get_initializer_tarval_value(val);
335                 long                          v;
336
337                 if (!tarval_is_constant(tv))
338                         return false;
339
340                 v = get_tarval_long(tv);
341                 if (v != 0 && v != 0x09 && v != 0x0A && v != 0x0C && v != 0x0D && v != 0x1B && (v < 0x20 || 0x80 <= v) && (v < 0xA0 || 0x100 <= v))
342                         return false;
343         }
344
345         return true;
346 }
347
348 /**
349  * Dump initializers.
350  */
351 static void dump_ir_initializers_to_file(FILE *F, const char *prefix,
352                                          const ir_initializer_t *initializer,
353                                          ir_type *type)
354 {
355         ir_tarval *tv;
356         ir_node   *value;
357
358         if (need_nl) {
359                 fprintf(F, "\n%s    ", prefix);
360                 need_nl = 0;
361         }
362         switch (get_initializer_kind(initializer)) {
363         case IR_INITIALIZER_NULL:
364                 fprintf(F, "\t = <NOT_SET>");
365                 break;
366         case IR_INITIALIZER_TARVAL:
367                 tv = get_initializer_tarval_value(initializer);
368                 ir_fprintf(F, "\t = <TV>%F", tv);
369                 break;
370         case IR_INITIALIZER_CONST:
371                 value = get_initializer_const_value(initializer);
372                 ir_fprintf(F, "\t = %F", value);
373                 break;
374         case IR_INITIALIZER_COMPOUND:
375                 if (is_Array_type(type)) {
376                         size_t const n = get_initializer_compound_n_entries(initializer);
377                         size_t       i;
378
379                         if (is_init_string(initializer, type)) {
380                                 fprintf(F, "\t[0...%u] = '", (unsigned)n - 1);
381                                 for (i = 0; i != n; ++i) {
382                                         ir_initializer_t const* const val = get_initializer_compound_value(initializer, i);
383                                         ir_tarval*              const tv  = get_initializer_tarval_value(val);
384                                         long                    const v   = get_tarval_long(tv);
385
386                                         switch (v) {
387                                                 case 0x00: fprintf(F, "<NUL>");       break;
388                                                 case 0x09: fprintf(F, "<HT>");        break;
389                                                 case 0x0A: fprintf(F, "<NL>");        break;
390                                                 case 0x0C: fprintf(F, "<FF>");        break;
391                                                 case 0x0D: fprintf(F, "<CR>");        break;
392                                                 case 0x1B: fprintf(F, "<ESC>");       break;
393                                                 case 0x22: fprintf(F, "<QUOTE>");     break;
394                                                 case 0x5C: fprintf(F, "<BACKSLASH>"); break;
395                                                 default:   fprintf(F, "%c", (unsigned char)v); break;
396                                         }
397                                 }
398                                 fprintf(F, "'");
399                         } else {
400                                 ir_type *const element_type = get_array_element_type(type);
401
402                                 for (i = 0; i < n; ++i) {
403                                         ir_initializer_t *sub_initializer
404                                                 = get_initializer_compound_value(initializer, i);
405
406                                         if (need_nl) {
407                                                 fprintf(F, "\n%s    ", prefix);
408                                                 need_nl = 0;
409                                         }
410                                         fprintf(F, "[%d]", (int) i);
411                                         dump_ir_initializers_to_file(F, prefix, sub_initializer, element_type);
412                                 }
413                         }
414                 } else {
415                         size_t i, n;
416                         assert(is_compound_type(type));
417                         n = get_compound_n_members(type);
418                         for (i = 0; i < n; ++i) {
419                                 ir_entity        *member    = get_compound_member(type, i);
420                                 ir_type          *subtype   = get_entity_type(member);
421                                 ir_initializer_t *sub_initializer;
422
423                                 assert(i < get_initializer_compound_n_entries(initializer));
424                                 sub_initializer
425                                         = get_initializer_compound_value(initializer, i);
426
427                                 if (need_nl) {
428                                         fprintf(F, "\n%s    ", prefix);
429                                         need_nl = 0;
430                                 }
431                                 ir_fprintf(F, ".%F", member);
432                                 dump_ir_initializers_to_file(F, prefix, sub_initializer, subtype);
433                         }
434                 }
435                 break;
436         default:
437                 panic("invalid ir_initializer kind found");
438         }
439         need_nl = 1;
440 }
441
442 static void dump_entity_linkage(FILE *F, const ir_entity *entity)
443 {
444         ir_linkage linkage = get_entity_linkage(entity);
445
446         if (linkage == IR_LINKAGE_DEFAULT) {
447                 fprintf(F, " default");
448                 return;
449         }
450         if (linkage & IR_LINKAGE_CONSTANT)
451                 fprintf(F, " constant");
452         if (linkage & IR_LINKAGE_WEAK)
453                 fprintf(F, " weak");
454         if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
455                 fprintf(F, " garbage_collect");
456         if (linkage & IR_LINKAGE_MERGE)
457                 fprintf(F, " merge");
458         if (linkage & IR_LINKAGE_HIDDEN_USER)
459                 fprintf(F, " hidden_user");
460 }
461
462 static void dump_entity_to_file_prefix(FILE *F, ir_entity *ent, const char *prefix)
463 {
464         ir_type *owner, *type;
465
466         assert(is_entity(ent));
467         owner = get_entity_owner(ent);
468         type  = get_entity_type(ent);
469         if (verbosity & dump_verbosity_onlynames) {
470                 fprintf(F, "%sentity %s.%s (%ld)\n", prefix, get_compound_name(get_entity_owner(ent)),
471                         get_entity_name(ent), get_entity_nr(ent));
472                 return;
473         }
474
475         if (verbosity & dump_verbosity_entattrs) {
476                 fprintf(F, "%sentity %s (%ld)\n", prefix, get_entity_name(ent), get_entity_nr(ent));
477                 ir_fprintf(F, "%s  type:  %+F\n", prefix, type);
478                 ir_fprintf(F, "%s  owner: %+F\n", prefix, owner);
479
480                 if (is_Class_type(get_entity_owner(ent))) {
481                         if (get_entity_n_overwrites(ent) > 0) {
482                                 size_t i;
483                                 fprintf(F, "%s  overwrites:\n", prefix);
484                                 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
485                                         ir_entity *ov = get_entity_overwrites(ent, i);
486                                         ir_fprintf(F, "%s    %d: %s of class %+F\n", prefix, i,
487                                                 get_entity_name(ov), get_entity_owner(ov));
488                                 }
489                         } else {
490                                 fprintf(F, "%s  Does not overwrite other entities.\n", prefix);
491                         }
492                         if (get_entity_n_overwrittenby(ent) > 0) {
493                                 size_t i;
494                                 fprintf(F, "%s  overwritten by:\n", prefix);
495                                 for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
496                                         ir_entity *ov = get_entity_overwrittenby(ent, i);
497                                         ir_fprintf(F, "%s    %d: %s of class %+F\n", prefix, i,
498                                                    get_entity_name(ov), get_entity_owner(ov));
499                                 }
500                         } else {
501                                 fprintf(F, "%s  Is not overwritten by other entities.\n",
502                                         prefix);
503                         }
504
505                         if (get_irp_inh_transitive_closure_state() != inh_transitive_closure_none) {
506                                 ir_entity *ov;
507                                 fprintf(F, "%s  transitive overwrites:\n", prefix);
508                                 for (ov = get_entity_trans_overwrites_first(ent);
509                                 ov;
510                                 ov = get_entity_trans_overwrites_next(ent)) {
511                                         ir_fprintf(F, "%s    : %s of class %+F\n", prefix,
512                                                    get_entity_name(ov), get_entity_owner(ov));
513                                 }
514                                 fprintf(F, "%s  transitive overwritten by:\n", prefix);
515                                 for (ov = get_entity_trans_overwrittenby_first(ent);
516                                 ov;
517                                 ov = get_entity_trans_overwrittenby_next(ent)) {
518                                         ir_fprintf(F, "%s    : %s of class %+F\n", prefix,
519                                                    get_entity_name(ov), get_entity_owner(ov));
520                                 }
521                         }
522                 }
523
524                 if (is_Method_type(get_entity_type(ent))) {
525                         unsigned mask = get_entity_additional_properties(ent);
526                         unsigned cc   = get_method_calling_convention(get_entity_type(ent));
527                         ir_graph *irg = get_entity_irg(ent);
528
529                         if (irg) {
530                                 fprintf(F, "%s  estimated node count: %u\n", prefix, get_irg_estimated_node_cnt(irg));
531                                 fprintf(F, "%s  maximum node index:   %u\n", prefix, get_irg_last_idx(irg));
532                         }
533
534                         if (mask) {
535                                 fprintf(F, "%s  additional prop: ", prefix);
536
537                                 if (mask & mtp_property_const)         fputs("const_function, ", F);
538                                 if (mask & mtp_property_pure)          fputs("pure_function, ", F);
539                                 if (mask & mtp_property_noreturn)      fputs("noreturn_function, ", F);
540                                 if (mask & mtp_property_nothrow)       fputs("nothrow_function, ", F);
541                                 if (mask & mtp_property_naked)         fputs("naked_function, ", F);
542                                 if (mask & mtp_property_malloc)        fputs("malloc_function, ", F);
543                                 if (mask & mtp_property_returns_twice) fputs("weak_function, ", F);
544                                 if (mask & mtp_property_intrinsic)     fputs("intrinsic_function, ", F);
545                                 if (mask & mtp_property_runtime)       fputs("runtime_function, ", F);
546                                 if (mask & mtp_property_private)       fputs("private_function, ", F);
547                                 if (mask & mtp_property_has_loop)      fputs("has_loop_function, ", F);
548                                 fputc('\n', F);
549                         }
550                         fprintf(F, "%s  calling convention: ", prefix);
551                         if (cc & cc_reg_param)           fputs("regparam, ", F);
552                         if (cc & cc_this_call)           fputs("thiscall, ", F);
553                         if (cc & cc_compound_ret)        fputs("compound_ret, ", F);
554                         if (cc & cc_frame_on_caller_stk) fputs("frame on caller's stack, ", F);
555                         cc &= ~(cc_compound_ret|cc_frame_on_caller_stk);
556                         if (IS_CDECL(cc))
557                                 fputs("cdecl", F);
558                         else if (IS_STDCALL(cc))
559                                 fputs("stdcall", F);
560                         else {
561                                 fputs(cc & cc_last_on_top      ? "last param on top, " : "first param on top, ", F);
562                                 fputs(cc & cc_callee_clear_stk ? "callee clear stack" : "caller clear stack", F);
563                         }
564                         fprintf(F, "\n%s  vtable number:        %u\n", prefix, get_entity_vtable_number(ent));
565                 }
566         } else {  /* no entattrs */
567                 ir_fprintf(F, "%s(%3d:%d) %+F: %s", prefix,
568                         get_entity_offset(ent), get_entity_offset_bits_remainder(ent),
569                         get_entity_type(ent), get_entity_name(ent));
570                 if (is_Method_type(get_entity_type(ent))) fputs("(...)", F);
571
572                 if (verbosity & dump_verbosity_accessStats) {
573                         dump_entity_linkage(F, ent);
574                 }
575                 fputc('\n', F);
576         }
577
578         if (verbosity & dump_verbosity_entconsts) {
579                 if (ent->initializer != NULL) {
580                         const ir_initializer_t *initializer = get_entity_initializer(ent);
581                         fprintf(F, "\n%s  Initializers:", prefix);
582                         need_nl = 1;
583                         dump_ir_initializers_to_file(F, prefix, initializer, get_entity_type(ent));
584                         fputc('\n', F);
585                 } else if (entity_has_compound_ent_values(ent)) {
586                         size_t i;
587                         fprintf(F, "%s  compound values:", prefix);
588                         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
589                                 size_t j;
590                                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
591                                 ir_entity *ent0 = get_compound_graph_path_node(path, 0);
592                                 fprintf(F, "\n%s    %3d:%d ", prefix, get_entity_offset(ent0), get_entity_offset_bits_remainder(ent0));
593                                 if (get_type_state(type) == layout_fixed)
594                                         fprintf(F, "(%3u:%u) ",   get_compound_ent_value_offset_bytes(ent, i), get_compound_ent_value_offset_bit_remainder(ent, i));
595                                 fprintf(F, "%s", get_entity_name(ent));
596                                 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
597                                         ir_entity *node = get_compound_graph_path_node(path, j);
598                                         fprintf(F, ".%s", get_entity_name(node));
599                                         if (is_Array_type(get_entity_owner(node)))
600                                                 fprintf(F, "[%ld]", get_compound_graph_path_array_index(path, j));
601                                 }
602                                 fprintf(F, "\t = ");
603                                 dump_node_opcode(F, get_compound_ent_value(ent, i));
604                         }
605                         fputc('\n', F);
606                 }
607         }
608
609         if (verbosity & dump_verbosity_entattrs) {
610                 fprintf(F, "%s  linkage:", prefix);
611                 dump_entity_linkage(F, ent);
612                 fprintf(F, "\n%s  volatility:  %s", prefix, get_volatility_name(get_entity_volatility(ent)));
613                 fprintf(F, "\n%s  aligned:  %s", prefix, get_align_name(get_entity_aligned(ent)));
614                 fprintf(F, "\n%s  alignment:  %u", prefix, get_entity_alignment(ent));
615                 fprintf(F, "\n%s  ld_name: %s", prefix, ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
616                 fprintf(F, "\n%s  offset:  %d bytes, %d rem bits", prefix, get_entity_offset(ent), get_entity_offset_bits_remainder(ent));
617                 if (is_Method_type(get_entity_type(ent))) {
618                         if (get_entity_irg(ent))   /* can be null */ {
619                                 fprintf(F, "\n%s  irg = %ld", prefix, get_irg_graph_nr(get_entity_irg(ent)));
620                         } else {
621                                 fprintf(F, "\n%s  irg = NULL", prefix);
622                         }
623                 }
624                 fputc('\n', F);
625         }
626 }
627
628 void dump_entity_to_file(FILE *out, ir_entity *ent)
629 {
630         dump_entity_to_file_prefix(out, ent, "");
631         fprintf(out, "\n");
632 }
633
634 void dump_type_to_file(FILE *F, ir_type *tp)
635 {
636         size_t i;
637
638         if ((is_Class_type(tp))       && (verbosity & dump_verbosity_noClassTypes)) return;
639         if ((is_Struct_type(tp))      && (verbosity & dump_verbosity_noStructTypes)) return;
640         if ((is_Union_type(tp))       && (verbosity & dump_verbosity_noUnionTypes)) return;
641         if ((is_Array_type(tp))       && (verbosity & dump_verbosity_noArrayTypes)) return;
642         if ((is_Pointer_type(tp))     && (verbosity & dump_verbosity_noPointerTypes)) return;
643         if ((is_Method_type(tp))      && (verbosity & dump_verbosity_noMethodTypes)) return;
644         if ((is_Primitive_type(tp))   && (verbosity & dump_verbosity_noPrimitiveTypes)) return;
645         if ((is_Enumeration_type(tp)) && (verbosity & dump_verbosity_noEnumerationTypes)) return;
646
647         ir_fprintf(F, "%+F", tp);
648         if (verbosity & dump_verbosity_onlynames) { fprintf(F, "\n"); return; }
649
650         switch (get_type_tpop_code(tp)) {
651
652         case tpo_class:
653                 if ((verbosity & dump_verbosity_methods) || (verbosity & dump_verbosity_fields)) {
654                         fprintf(F, "\n  members:\n");
655                 }
656                 for (i = 0; i < get_class_n_members(tp); ++i) {
657                         ir_entity *mem = get_class_member(tp, i);
658                         if (((verbosity & dump_verbosity_methods) &&  is_Method_type(get_entity_type(mem))) ||
659                                 ((verbosity & dump_verbosity_fields)  && !is_Method_type(get_entity_type(mem)))   ) {
660                                 if (!(verbosity & dump_verbosity_nostatic)) {
661                                         dump_entity_to_file_prefix(F, mem, "    ");
662                                 }
663                         }
664                 }
665                 if (verbosity & dump_verbosity_typeattrs) {
666                         fprintf(F, "  supertypes: ");
667                         for (i = 0; i < get_class_n_supertypes(tp); ++i) {
668                                 ir_type *stp = get_class_supertype(tp, i);
669                                 ir_fprintf(F, "\n    %d %+F", i, stp);
670                         }
671                         fprintf(F, "\n  subtypes: ");
672                         for (i = 0; i < get_class_n_subtypes(tp); ++i) {
673                                 ir_type *stp = get_class_subtype(tp, i);
674                                 ir_fprintf(F, "\n    %d %+F", i, stp);
675                         }
676
677                         if (get_irp_inh_transitive_closure_state() != inh_transitive_closure_none) {
678                                 ir_type *stp;
679                                 fprintf(F, "\n  transitive supertypes: ");
680                                 for (stp = get_class_trans_supertype_first(tp);
681                                 stp;
682                                 stp = get_class_trans_supertype_next(tp)) {
683                                         ir_fprintf(F, "\n    %+F", stp);
684                                 }
685                                 fprintf(F, "\n  transitive subtypes: ");
686                                 for (stp = get_class_trans_subtype_first(tp);
687                                 stp;
688                                 stp = get_class_trans_subtype_next(tp)) {
689                                         ir_fprintf(F, "\n    %+F", stp);
690                                 }
691                         }
692
693                         fprintf(F, "\n  flags:       ");
694                         if (is_class_final(tp))
695                                 fprintf(F, "final, ");
696                         if (is_class_interface(tp))
697                                 fprintf(F, "interface, ");
698                         if (is_class_abstract(tp))
699                                 fprintf(F, "abstract, ");
700                         fprintf(F, "\n");
701                 }
702                 break;
703
704         case tpo_union:
705         case tpo_struct:
706                 if (verbosity & dump_verbosity_fields) fprintf(F, "\n  members: ");
707                 for (i = 0; i < get_compound_n_members(tp); ++i) {
708                         ir_entity *mem = get_compound_member(tp, i);
709                         if (verbosity & dump_verbosity_fields) {
710                                 dump_entity_to_file_prefix(F, mem, "    ");
711                         }
712                 }
713                 break;
714
715         case tpo_array:
716                 if (verbosity & dump_verbosity_typeattrs) {
717                         size_t n_dim;
718                         ir_type *elem_tp = get_array_element_type(tp);
719
720                         fprintf(F, "\n  array ");
721
722                         n_dim = get_array_n_dimensions(tp);
723                         for (i = 0; i < n_dim; ++i) {
724                                 ir_node *lower, *upper;
725
726                                 lower = get_array_lower_bound(tp, i);
727                                 upper = get_array_upper_bound(tp, i);
728
729                                 fprintf(F, "[");
730
731                                 if (is_Const(lower)) {
732                                         fprintf(F, "%ld .. ", get_tarval_long(get_Const_tarval(lower)));
733                                 } else {
734                                         dump_node_opcode(F, lower);
735                                         fprintf(F, " %ld .. ", get_irn_node_nr(lower));
736                                 }
737
738                                 if (is_Const(upper)) {
739                                         fprintf(F, "%ld]", get_tarval_long(get_Const_tarval(lower)));
740                                 } else {
741                                         dump_node_opcode(F, upper);
742                                         fprintf(F, " %ld]", get_irn_node_nr(upper));
743                                 }
744                         }
745                         ir_fprintf(F, " of <%+F>", elem_tp);
746
747                         fprintf(F, "\n  order: ");
748                         for (i = 0; i < n_dim; ++i)
749                                 fprintf(F, "<%zu>", get_array_order(tp, i));
750
751                         fprintf(F, "\n");
752
753                         if (verbosity & dump_verbosity_fields) {
754                                 dump_entity_to_file_prefix(F, get_array_element_entity(tp),
755                                                            "    ");
756                         }
757                 }
758                 break;
759
760         case tpo_pointer:
761                 if (verbosity & dump_verbosity_typeattrs) {
762                         ir_type *tt = get_pointer_points_to_type(tp);
763                         ir_fprintf(F, "\n  points to %+F\n", tt);
764                 }
765                 break;
766
767         case tpo_method:
768                 if (verbosity & dump_verbosity_typeattrs) {
769                         mtp_additional_properties mtp = get_method_additional_properties(tp);
770                         unsigned cconv = get_method_calling_convention(tp);
771                         fprintf(F, "\n  variadicity: %s", get_variadicity_name(get_method_variadicity(tp)));
772                         fprintf(F, "\n  return types: %lu",
773                                 (unsigned long) get_method_n_ress(tp));
774                         for (i = 0; i < get_method_n_ress(tp); ++i) {
775                                 ir_type *rtp = get_method_res_type(tp, i);
776                                 ir_fprintf(F, "\n    %+F", rtp);
777                         }
778
779                         fprintf(F, "\n  parameter types: %lu",
780                                 (unsigned long) get_method_n_params(tp));
781                         for (i = 0; i < get_method_n_params(tp); ++i) {
782                                 ir_type *ptp = get_method_param_type(tp, i);
783                                 ir_fprintf(F, "\n    %+F", ptp);
784                         }
785                         fprintf(F, "\n  properties:");
786                         if (mtp & mtp_property_const)
787                                 fputs(" const", F);
788                         if (mtp & mtp_property_pure)
789                                 fputs(" pure", F);
790                         if (mtp & mtp_property_noreturn)
791                                 fputs(" noreturn", F);
792                         if (mtp & mtp_property_nothrow)
793                                 fputs(" nothrow", F);
794                         if (mtp & mtp_property_naked)
795                                 fputs(" naked", F);
796                         if (mtp & mtp_property_malloc)
797                                 fputs(" malloc", F);
798                         if (mtp & mtp_property_returns_twice)
799                                 fputs(" returns_twice", F);
800                         if (mtp & mtp_property_intrinsic)
801                                 fputs(" intrinsic", F);
802                         if (mtp & mtp_property_runtime)
803                                 fputs(" runtime", F);
804                         if (mtp & mtp_property_private)
805                                 fputs(" private", F);
806                         if (mtp & mtp_property_has_loop)
807                                 fputs(" has_Loop", F);
808
809                         fprintf(F, "\n  calling convention:");
810                         if (cconv & cc_reg_param)
811                                 fputs(" regparam", F);
812                         if (cconv & cc_last_on_top)
813                                 fputs(" last_on_top", F);
814                         if (cconv & cc_callee_clear_stk)
815                                 fputs(" calle_clear_stk", F);
816                         if (cconv & cc_this_call)
817                                 fputs(" this_call", F);
818                         if (cconv & cc_compound_ret)
819                                 fputs(" compound_ret", F);
820                         if (cconv & cc_frame_on_caller_stk)
821                                 fputs(" frame_on_caller_stk", F);
822                         if (cconv & cc_fpreg_param)
823                                 fputs(" fpreg_param", F);
824
825                         if (get_method_variadicity(tp)) {
826                                 fprintf(F, "\n    ...");
827                         }
828                         fprintf(F, "\n");
829                 }
830                 break;
831
832         case tpo_primitive:
833                 if (verbosity & dump_verbosity_typeattrs) {
834                         ir_type *base_tp = get_primitive_base_type(tp);
835                         if (base_tp != NULL)
836                                 ir_fprintf(F, "\n  base type: %+F", tp);
837                         fprintf(F, "\n");
838                 }
839                 break;
840
841         case tpo_none:
842         case tpo_unknown:
843                 fprintf(F, "\n");
844                 break;
845
846         default:
847                 if (verbosity & dump_verbosity_typeattrs) {
848                         fprintf(F, ": details not implemented\n");
849                 }
850         }
851
852         fprintf(F, "  state:      %s,\n", get_type_state_name(get_type_state(tp)));
853         fprintf(F, "  size:       %2u Bytes,\n", get_type_size_bytes(tp));
854         fprintf(F, "  alignment:  %2u Bytes,\n", get_type_alignment_bytes(tp));
855         if (is_atomic_type(tp) || is_Method_type(tp))
856                 fprintf(F, "  mode:       %s,\n",  get_mode_name(get_type_mode(tp)));
857
858         fprintf(F, "\n\n");
859 }
860
861 void dump_types_as_text(FILE *out)
862 {
863         size_t i, n_types = get_irp_n_types();
864
865         for (i = 0; i < n_types; ++i) {
866                 ir_type *type = get_irp_type(i);
867                 dump_type_to_file(out, type);
868         }
869 }
870
871 void dump_globals_as_text(FILE *out)
872 {
873         ir_type *global_type = get_glob_type();
874         size_t   n_members   = get_class_n_members(global_type);
875         size_t   i;
876
877         for (i = 0; i < n_members; ++i) {
878                 ir_entity *entity = get_class_member(global_type, i);
879                 dump_entity_to_file(out, entity);
880         }
881 }