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