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