Let the emitter generator generate fewer calls.
[libfirm] / ir / ir / irdumptxt.c
1 /*
2  * Copyright (C) 1995-2008 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 vcg representation of firm to file.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Hubert Schmidt
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include "irdump_t.h"
33 #include "irgraph_t.h"
34
35 #include "firm_common_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 #include "field_temperature.h"
47
48 #define MY_SIZE 1024     /* Size of an array that actually should be computed. */
49
50 /**
51  * Just opens a file, mangling a file name.
52  *
53  * The file name results from the concatenation of the following parts:
54  *
55  * @param basename  The basis of the name telling about the content.
56  * @param suffix1   The first suffix.
57  * @param suffix2   The second suffix.
58  * @param suffix3   The third suffix.
59  */
60 static FILE *text_open(const char *basename, const char * suffix1, const char *suffix2, const char *suffix3) {
61         FILE *F;
62         int len = strlen(basename), i, j;
63         char *fname;  /* filename to put the vcg information in */
64
65         if (!basename) assert(basename);
66         if (!suffix1) suffix1 = "";
67         if (!suffix2) suffix2 = "";
68         if (!suffix3) suffix3 = ".txt";
69
70         /* open file for vcg graph */
71         fname = XMALLOCN(char, strlen(basename)*2 + strlen(suffix1) + strlen(suffix2) + 5); /* *2: space for escapes. */
72
73         j = 0;
74         for (i = 0; i < len; ++i) {  /* replace '/' in the name: escape by @. */
75                 if (basename[i] == '/') {
76                         fname[j] = '@'; j++; fname[j] = '1'; j++;
77                 } else if (basename[i] == '@') {
78                         fname[j] = '@'; j++; fname[j] = '2'; j++;
79                 } else {
80                         fname[j] = basename[i]; j++;
81                 }
82         }
83         fname[j] = '\0';
84         strcat(fname, suffix1);  /* append file suffix */
85         strcat(fname, suffix2);  /* append file suffix */
86         strcat(fname, suffix3);  /* append the .txt suffix */
87
88         F = fopen(fname, "w");   /* open file for writing */
89         if (!F) {
90                 perror(fname);
91                 abort();
92         }
93         free(fname);
94
95         return F;
96 }
97
98 /* Write the irnode and all its attributes to the file passed. */
99 int dump_irnode_to_file(FILE *F, ir_node *n) {
100         int i, bad = 0;
101         char comma;
102         ir_graph *irg;
103
104         dump_node_opcode(F, n);
105         fprintf(F, " %ld\n", get_irn_node_nr(n));
106
107         fprintf(F, "  index: %u\n", get_irn_idx(n));
108         if (opt_dump_pointer_values_to_info)
109                 fprintf (F, "  addr:    %p \n", (void *)n);
110         fprintf (F, "  mode:    %s\n", get_mode_name(get_irn_mode(n)));
111         fprintf (F, "  visited: %ld \n", get_irn_visited(n));
112         irg = get_irn_irg(n);
113         if (irg != get_const_code_irg())
114                 fprintf (F, "  irg:     %s\n", get_ent_dump_name(get_irg_entity(irg)));
115
116         if (get_irn_pinned(n) == op_pin_state_floats &&
117                 get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats) {
118                 fprintf(F, "  node was pinned in ");
119                 dump_node_opcode(F, get_irn_n(n, -1));
120                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, -1)));
121         }
122
123 #ifdef INTERPROCEDURAL_VIEW
124         fprintf(F, "  arity:   %d\n", get_irn_intra_arity(n));
125         /* show all predecessor nodes */
126         fprintf(F, "  pred nodes: \n");
127         if (!is_Block(n)) {
128                 fprintf(F, "    -1:    ");
129                 dump_node_opcode(F, get_irn_n(n, -1));
130                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, -1)));
131         }
132         for ( i = 0; i < get_irn_intra_arity(n); ++i) {
133                 fprintf(F, "     %d: %s ", i, is_intra_backedge(n, i) ? "be" : "  ");
134                 dump_node_opcode(F, get_irn_intra_n(n, i));
135                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_intra_n(n, i)));
136         }
137 #else
138         fprintf(F, "  arity:   %d\n", get_irn_arity(n));
139         /* show all predecessor nodes */
140         fprintf(F, "  pred nodes: \n");
141         if (!is_Block(n)) {
142                 fprintf(F, "    -1:    ");
143                 dump_node_opcode(F, get_irn_n(n, -1));
144                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, -1)));
145         }
146         for ( i = 0; i < get_irn_arity(n); ++i) {
147                 fprintf(F, "     %d: %s ", i, is_backedge(n, i) ? "be" : "  ");
148                 dump_node_opcode(F, get_irn_n(n, i));
149                 fprintf(F, " %ld\n", get_irn_node_nr(get_irn_n(n, i)));
150         }
151 #endif
152
153         fprintf(F, "  Private Attributes:\n");
154
155         if (is_Proj(n))
156                 fprintf(F, "  proj nr: %ld\n", get_Proj_proj(n));
157
158 #ifdef INTERPROCEDURAL_VIEW
159         if ((get_irp_ip_view_state() != ip_view_no) && (is_Filter(n) || is_Block(n))) {
160                 fprintf(F, "  inter arity: %d\n", get_irn_inter_arity(n));
161                 fprintf(F, "  inter pred nodes: \n");
162                 for (i = 0; i < get_irn_inter_arity(n); ++i) {
163                         fprintf(F, "     %d: %s ", i, is_intra_backedge(n, i) ? "be" : "  ");
164                         dump_node_opcode(F, get_irn_inter_n(n, i));
165                         fprintf(F, " %ld\n", get_irn_node_nr(get_irn_inter_n(n, i)));
166                 }
167         }
168 #endif
169
170         if (is_fragile_op(n)) {
171                 fprintf(F, "  pinned state: %s\n", get_op_pin_state_name(get_irn_pinned(n)));
172                 /* not dumped: frag array */
173         }
174
175         /* This is not nice, output it as a marker in the predecessor list. */
176         if (is_Block(n)             ||
177             get_irn_op(n) == op_Phi ||
178             (is_Filter(n) && get_interprocedural_view())) {
179                 fprintf(F, "  backedges:");
180                 comma = ' ';
181                 for (i = 0; i < get_irn_arity(n); i++)
182                         if (is_backedge(n, i)) { fprintf(F, "%c %d", comma, i); comma = ','; }
183                         fprintf(F, "\n");
184         }
185
186         /* Loop node.   Someone else please tell me what's wrong ... */
187         if (get_irn_loop(n)) {
188                 ir_loop *loop = get_irn_loop(n);
189                 assert(loop);
190                 fprintf(F, "  in loop %d with depth %d\n",
191                         get_loop_loop_nr(loop), get_loop_depth(loop));
192         }
193
194         /* Source types */
195         switch (get_irn_opcode(n)) {
196         case iro_Block: {
197                 if (has_Block_label(n))
198                         fprintf(F, "  Label: %lu\n", get_Block_label(n));
199                 ir_fprintf(F, "  macro Block: %+F\n", get_Block_MacroBlock(n));
200                 fprintf(F, "  block visited: %ld\n", get_Block_block_visited(n));
201                 fprintf(F, "  block marked: %u\n", get_Block_mark(n));
202                 if (get_irg_dom_state(get_irn_irg(n)) != dom_none) {
203                         fprintf(F, "  dom depth %d\n", get_Block_dom_depth(n));
204                         fprintf(F, "  tree pre num %d\n", get_Block_dom_tree_pre_num(n));
205                         fprintf(F, "  max subtree pre num %d\n", get_Block_dom_max_subtree_pre_num(n));
206                 }
207
208                 fprintf(F, "  Execution frequency statistics:\n");
209                 if (get_irg_exec_freq_state(get_irn_irg(n)) != exec_freq_none)
210                         fprintf(F, "    procedure local evaluation:   %8.2lf\n", get_irn_exec_freq(n));
211 #ifdef INTERPROCEDURAL_VIEW
212                 if (get_irp_loop_nesting_depth_state() != loop_nesting_depth_none)
213                         fprintf(F, "    call frequency of procedure:   %8.2lf\n",
214                         get_irg_method_execution_frequency(get_irn_irg(n)));
215                 if (get_irp_callgraph_state() == irp_callgraph_and_calltree_consistent)
216                         fprintf(F, "    recursion depth of procedure: %8.2lf\n", (double)get_irn_recursion_depth(n));
217                 if ((get_irg_exec_freq_state(get_irn_irg(n)) != exec_freq_none) &&
218                         (get_irp_loop_nesting_depth_state() != loop_nesting_depth_none) &&
219                         (get_irp_callgraph_state() == irp_callgraph_and_calltree_consistent))
220                         fprintf(F, "    final evaluation:           **%8.2lf**\n", get_irn_final_cost(n));
221 #endif
222
223                 /* not dumped: graph_arr */
224                 /* not dumped: mature    */
225         }  break;
226         case iro_Start: {
227                 ir_type *tp = get_entity_type(get_irg_entity(get_irn_irg(n)));
228                 fprintf(F, "  start of method of type %s \n", get_type_name_ex(tp, &bad));
229                 for (i = 0; i < get_method_n_params(tp); ++i)
230                         fprintf(F, "    param %d type: %s \n", i, get_type_name_ex(get_method_param_type(tp, i), &bad));
231 #ifdef INTERPROCEDURAL_VIEW
232                 if ((get_irp_ip_view_state() == ip_view_valid) && !get_interprocedural_view()) {
233                         ir_node *sbl = get_nodes_block(n);
234                         int i, n_cfgpreds = get_Block_cg_n_cfgpreds(sbl);
235                         fprintf(F, "  graph has %d interprocedural predecessors:\n", n_cfgpreds);
236                         for (i = 0; i < n_cfgpreds; ++i) {
237                                 ir_node *cfgpred = get_Block_cg_cfgpred(sbl, i);
238                                 fprintf(F, "    %d: Call %ld in graph %s\n", i, get_irn_node_nr(cfgpred),
239                                         get_irg_dump_name(get_irn_irg(cfgpred)));
240                         }
241                 }
242 #endif
243         } break;
244         case iro_Cond: {
245                 fprintf(F, "  condition kind: %s\n",  get_Cond_kind(n) == dense ? "dense" : "fragmentary");
246                 fprintf(F, "  default ProjNr: %ld\n", get_Cond_defaultProj(n));
247                 if (get_Cond_jmp_pred(n) != COND_JMP_PRED_NONE)
248                         fprintf(F, "  jump prediction: %s\n", get_cond_jmp_predicate_name(get_Cond_jmp_pred(n)));
249         } break;
250         case iro_Alloc: {
251                 fprintf(F, "  allocating entity of type: %s \n", get_type_name_ex(get_Alloc_type(n), &bad));
252                 fprintf(F, "  allocating on: the %s\n", (get_Alloc_where(n) == stack_alloc) ? "stack" : "heap");
253         } break;
254         case iro_Free: {
255                 fprintf(F, "  freeing entity of type %s \n", get_type_name_ex(get_Free_type(n), &bad));
256                 fprintf(F, "  allocated on: the %s\n", (get_Free_where(n) == stack_alloc) ? "stack" : "heap");
257         } break;
258         case iro_Sel: {
259                 ir_entity *ent = get_Sel_entity(n);
260                 if (ent) {
261                         fprintf(F, "  Selecting entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
262                         fprintf(F, "    of type    %s\n",  get_type_name_ex(get_entity_type(ent),  &bad));
263                         fprintf(F, "    with owner %s.\n", get_type_name_ex(get_entity_owner(ent), &bad));
264                 }
265                 else {
266                         fprintf(F, "  <NULL entity>\n");
267                         bad = 1;
268                 }
269         } break;
270         case iro_Call: {
271                 ir_type *tp = get_Call_type(n);
272                 fprintf(F, "  calling method of type %s \n", get_type_name_ex(tp, &bad));
273                 if(get_unknown_type() != tp) {
274                         for (i = 0; i < get_method_n_params(tp); ++i)
275                                 fprintf(F, "    param %d type: %s \n", i, get_type_name_ex(get_method_param_type(tp, i), &bad));
276                         for (i = 0; i < get_method_n_ress(tp); ++i)
277                                 fprintf(F, "    resul %d type: %s \n", i, get_type_name_ex(get_method_res_type(tp, i), &bad));
278                 }
279                 if (Call_has_callees(n)) {
280                         fprintf(F, "  possible callees: \n");
281                         for (i = 0; i < get_Call_n_callees(n); i++) {
282                                 fprintf(F, "    %d: %s\n", i, get_ent_dump_name(get_Call_callee(n, i)));
283                         }
284                 }
285         } break;
286         case iro_CallBegin: {
287                 ir_node *call = get_CallBegin_call(n);
288                 fprintf(F, "  Call: %ld\n", get_irn_node_nr(call));
289                 if (Call_has_callees(call)) {
290                         fprintf(F, "  possible callees: \n");
291                         for (i = 0; i < get_Call_n_callees(call); i++) {
292                                 fprintf(F, "    %d: %s\n", i, get_ent_dump_name(get_Call_callee(call, i)));
293                         }
294                 }
295         } break;
296         case iro_Cast: {
297                 fprintf(F, "  cast to type: %s\n", get_type_name_ex(get_Cast_type(n), &bad));
298         } break;
299         case iro_Return: {
300                 if (!get_interprocedural_view()) {
301                         ir_type *tp = get_entity_type(get_irg_entity(get_irn_irg(n)));
302                         fprintf(F, "  return in method of type %s \n", get_type_name_ex(tp, &bad));
303                         for (i = 0; i < get_method_n_ress(tp); ++i)
304                                 fprintf(F, "    res %d type: %s \n", i, get_type_name_ex(get_method_res_type(tp, i), &bad));
305                 }
306         } break;
307         case iro_Const: {
308                 assert(get_Const_type(n) != firm_none_type);
309                 fprintf(F, "  Const of type %s \n", get_type_name_ex(get_Const_type(n), &bad));
310         } break;
311         case iro_SymConst: {
312                 switch(get_SymConst_kind(n)) {
313                 case symconst_addr_name:
314                         fprintf(F, "  kind: addr_name\n");
315                         fprintf(F, "  name: %s\n", get_id_str(get_SymConst_name(n)));
316                         break;
317                 case symconst_addr_ent:
318                         fprintf(F, "  kind:   addr_ent\n");
319                         fprintf(F, "  entity: ");
320                         dump_entity_to_file(F, get_SymConst_entity(n), dump_verbosity_onlynames);
321                         break;
322                 case symconst_ofs_ent:
323                         fprintf(F, "  kind:   offset\n");
324                         fprintf(F, "  entity: ");
325                         dump_entity_to_file(F, get_SymConst_entity(n), dump_verbosity_onlynames);
326                         break;
327                 case symconst_type_tag:
328                         fprintf(F, "  kind: type_tag\n");
329                         fprintf(F, "  type: ");
330                         dump_type_to_file(F, get_SymConst_type(n), dump_verbosity_onlynames);
331                         break;
332                 case symconst_type_size:
333                         fprintf(F, "  kind: size\n");
334                         fprintf(F, "  type: ");
335                         dump_type_to_file(F, get_SymConst_type(n), dump_verbosity_onlynames);
336                         break;
337                 case symconst_type_align:
338                         fprintf(F, "  kind: alignment\n");
339                         fprintf(F, "  type: ");
340                         dump_type_to_file(F, get_SymConst_type(n), dump_verbosity_onlynames);
341                         break;
342                 case symconst_enum_const:
343                         fprintf(F, "  kind: enumeration\n");
344                         fprintf(F, "  name: %s\n", get_enumeration_name(get_SymConst_enum(n)));
345                         break;
346                 case symconst_label:
347                         fprintf(F, "  kind: label\n");
348                         fprintf(F, "  label: %lu\n", get_SymConst_label(n));
349                         break;
350                 }
351                 fprintf(F, "  type of value: %s \n", get_type_name_ex(get_SymConst_value_type(n), &bad));
352         } break;
353         case iro_Load:
354                 fprintf(F, "  mode of loaded value: %s\n", get_mode_name_ex(get_Load_mode(n), &bad));
355                 fprintf(F, "  volatility: %s\n", get_volatility_name(get_Load_volatility(n)));
356                 fprintf(F, "  align: %s\n", get_align_name(get_Load_align(n)));
357                 break;
358         case iro_Store:
359                 fprintf(F, "  volatility: %s\n", get_volatility_name(get_Store_volatility(n)));
360                 fprintf(F, "  align: %s\n", get_align_name(get_Store_align(n)));
361                 break;
362         case iro_Confirm:
363                 fprintf(F, "  compare operation: %s\n", get_pnc_string(get_Confirm_cmp(n)));
364                 break;
365         case iro_ASM: {
366                 const ir_asm_constraint *cons;
367                 ident **clobber;
368                 int l;
369
370                 fprintf(F, "  assembler text: %s", get_id_str(get_ASM_text(n)));
371                 l = get_ASM_n_input_constraints(n);
372                 if (l > 0) {
373                         fprintf(F, "\n  inputs:  ");
374                         cons = get_ASM_input_constraints(n);
375                         for (i = 0; i < l; ++i)
376                                 fprintf(F, "%%%u %s ", cons[i].pos, get_id_str(cons[i].constraint));
377                 }
378                 l = get_ASM_n_output_constraints(n);
379                 if (l > 0) {
380                         fprintf(F, "\n  outputs: ");
381                         cons = get_ASM_output_constraints(n);
382                         for (i = 0; i < l; ++i)
383                                 fprintf(F, "%%%u %s ", cons[i].pos, get_id_str(cons[i].constraint));
384                 }
385                 l = get_ASM_n_clobbers(n);
386                 if (l > 0) {
387                         fprintf(F, "\n  clobber: ");
388                         clobber = get_ASM_clobbers(n);
389                         for (i = 0; i < l; ++i)
390                                 fprintf(F, "%s ", get_id_str(clobber[i]));
391                 }
392                 if (get_irn_pinned(n) != op_pin_state_floats)
393                         fprintf(F, "\n  volatile");
394                 fprintf(F, "\n");
395         } break;
396         default: ;
397         }
398
399         if (get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_consistent  ||
400                 get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_inconsistent  )
401                 if (get_irn_typeinfo_type(n) != firm_none_type)
402                         fprintf (F, "  Analysed type: %s\n", get_type_name_ex(get_irn_typeinfo_type(n), &bad));
403
404         return bad;
405 }
406
407
408
409 void dump_irnode(ir_node *n) {
410         dump_irnode_to_file(stdout, n);
411 }
412
413
414 void dump_graph_to_file(FILE *F, ir_graph *irg) {
415         fprintf(F, "graph %s\n", get_irg_dump_name(irg));
416 }
417
418 void dump_graph(ir_graph *g) {
419         dump_graph_to_file(stdout, g);
420 }
421
422 static void dump_node_to_graph_file(ir_node *n, void *env) {
423         FILE *F = (FILE *)env;
424
425         dump_irnode_to_file(F, n);
426         fprintf(F, "\n");
427 }
428
429 void dump_graph_as_text(ir_graph *irg, const char *suffix) {
430         const char *basename = get_irg_dump_name(irg);
431         FILE *F;
432
433         F = text_open(basename, suffix, "", ".txt");
434
435         dump_graph_to_file(F, irg);
436         fprintf(F, "\n\n");
437         irg_walk_graph(irg, NULL, dump_node_to_graph_file, F);
438
439         fclose (F);
440 }
441
442 #ifdef EXTENDED_ACCESS_STATS
443 static int addr_is_alloc(ir_node *acc) {
444         ir_node *addr = NULL;
445         ir_opcode addr_op;
446         if (is_memop(acc)) {
447                 addr = get_memop_ptr(acc);
448         } else {
449                 assert(is_Call(acc));
450                 addr = get_Call_ptr(acc);
451         }
452
453         addr_op = get_irn_opcode(addr);
454
455         while (addr_op != iro_Alloc) {
456                 switch (addr_op) {
457                 case iro_Sel:
458                         addr = get_Sel_ptr(addr);
459                         break;
460                 case iro_Cast:
461                         addr = get_Cast_op(addr);
462                         break;
463                 case iro_Proj:
464                         addr = get_Proj_pred(addr);
465                         break;
466                 case iro_SymConst:
467                 case iro_Const:
468                         return 0;
469                         break;
470                 case iro_Phi:
471                 case iro_Load:
472                 case iro_Call:
473                 case iro_Start:
474                         return 0;
475                         break;
476
477                 default:
478                         //DDMN(addr);
479                         //assert(0 && "unexpected address node");
480                         ;
481                 }
482                 addr_op = get_irn_opcode(addr);
483         }
484
485         /* In addition, the alloc must be in the same loop. */
486         return 1;
487 }
488 #endif
489
490 /** dumps something like:
491  *
492  *  "prefix"  "Name" (x): node1, ... node7,\n
493  *  "prefix"    node8, ... node15,\n
494  *  "prefix"    node16, node17\n
495  */
496 static void dump_node_list(FILE *F, firm_kind *k, char *prefix,
497                            int (*get_entity_n_nodes)(firm_kind *ent),
498                            ir_node *(*get_entity_node)(firm_kind *ent, int pos),
499                            char *name) {
500         int i, n_nodes = get_entity_n_nodes(k);
501         char *comma = "";
502
503         fprintf(F, "%s  %s (%d):", prefix, name, n_nodes);
504         for (i = 0; i < n_nodes; ++i) {
505                 int rem;
506                 if (i > 7 && !(i & 7)) { /* line break every eight node. */
507                         fprintf(F, ",\n%s   ", prefix);
508                         comma = "";
509                 }
510                 fprintf(F, "%s ", comma);
511                 rem = opt_dump_analysed_type_info;
512                 opt_dump_analysed_type_info = 0;
513                 dump_node_label(F, get_entity_node(k, i));
514                 opt_dump_analysed_type_info = rem;
515                 comma = ",";
516         }
517         fprintf(F, "\n");
518 }
519
520 /** dumps something like:
521  *
522  *  "prefix"  "Name" (x): node1, ... node7,\n
523  *  "prefix"    node8, ... node15,\n
524  *  "prefix"    node16, node17\n
525  */
526 static void dump_type_list(FILE *F, ir_type *tp, char *prefix,
527                            int (*get_n_types)(const ir_type *tp),
528                            ir_type *(*get_type)(const ir_type *tp, int pos),
529                            const char *name) {
530         int i, n_nodes = get_n_types(tp);
531         char *comma = "";
532
533         fprintf(F, "%s  %s (%d):", prefix, name, n_nodes);
534         for (i = 0; i < n_nodes; ++i) {
535                 if (i > 7 && !(i & 7)) { /* line break every eight node. */
536                         fprintf(F, ",\n%s   ", prefix);
537                         comma = "";
538                 }
539                 fprintf(F, "%s %s(%ld)", comma, get_type_name(get_type(tp, i)), get_type_nr(tp));
540                 //dump_type_to_file(F, get_type(tp, i), dump_verbosity_onlynames);
541                 comma = ",";
542         }
543         fprintf(F, "\n");
544 }
545
546 static int need_nl = 1;
547
548 /**
549  * Dump initializers.
550  */
551 static void dump_ir_initializers_to_file(FILE *F, const char *prefix,
552                                                                                  const ir_initializer_t *initializer, ir_type *type) {
553         tarval  *tv;
554         ir_node *value;
555
556         if (need_nl) {
557                 fprintf(F, "\n%s    ", prefix);
558                 need_nl = 0;
559         }
560         switch (get_initializer_kind(initializer)) {
561         case IR_INITIALIZER_NULL:
562                 fprintf(F, "\t = <NOT_SET>");
563                 break;
564         case IR_INITIALIZER_TARVAL:
565                 tv = get_initializer_tarval_value(initializer);
566                 ir_fprintf(F, "\t = <TV>%F", tv);
567                 break;
568         case IR_INITIALIZER_CONST:
569                 value = get_initializer_const_value(initializer);
570                 fprintf(F, "\t = <CONST>");
571                 dump_node_opcode(F, value);
572                 break;
573         case IR_INITIALIZER_COMPOUND:
574                 if (is_Array_type(type)) {
575                         size_t i, n = get_initializer_compound_n_entries(initializer);
576                         ir_type *element_type = get_array_element_type(type);
577                         for (i = 0; i < n; ++i) {
578                                 ir_initializer_t *sub_initializer
579                                         = get_initializer_compound_value(initializer, i);
580
581                                 if (need_nl) {
582                                         fprintf(F, "\n%s    ", prefix);
583                                         need_nl = 0;
584                                 }
585                                 fprintf(F, "[%d]", (int) i);
586                                 dump_ir_initializers_to_file(F, prefix, sub_initializer, element_type);
587                         }
588                 } else {
589                         size_t i, n;
590                         assert(is_compound_type(type));
591                         n = get_compound_n_members(type);
592                         for (i = 0; i < n; ++i) {
593                                 ir_entity        *member    = get_compound_member(type, i);
594                                 ir_type          *subtype   = get_entity_type(member);
595                                 ir_initializer_t *sub_initializer;
596
597                                 assert(i < get_initializer_compound_n_entries(initializer));
598                                 sub_initializer
599                                         = get_initializer_compound_value(initializer, i);
600
601                                 if (need_nl) {
602                                         fprintf(F, "\n%s    ", prefix);
603                                         need_nl = 0;
604                                 }
605                                 ir_fprintf(F, ".%F", member);
606                                 dump_ir_initializers_to_file(F, prefix, sub_initializer, subtype);
607                         }
608                 }
609                 break;
610         default:
611                 panic("invalid ir_initializer kind found");
612         }
613         need_nl = 1;
614 }
615
616
617 void dump_entity_to_file_prefix(FILE *F, ir_entity *ent, char *prefix, unsigned verbosity) {
618         int i, j;
619         ir_type *owner, *type;
620
621         assert(is_entity(ent));
622         owner = get_entity_owner(ent);
623         type  = get_entity_type(ent);
624         if (verbosity & dump_verbosity_onlynames) {
625                 fprintf(F, "%sentity %s.%s (%ld)\n", prefix, get_type_name(get_entity_owner(ent)),
626                         get_entity_name(ent), get_entity_nr(ent));
627                 return;
628         }
629
630         if (verbosity & dump_verbosity_entattrs) {
631                 fprintf(F, "%sentity %s (%ld)\n", prefix, get_entity_name(ent), get_entity_nr(ent));
632                 fprintf(F, "%s  type:  %s (%ld)\n", prefix, get_type_name(type),  get_type_nr(type));
633                 fprintf(F, "%s  owner: %s (%ld)\n", prefix, get_type_name(owner), get_type_nr(owner));
634
635                 if (is_Class_type(get_entity_owner(ent))) {
636                         if (get_entity_n_overwrites(ent) > 0) {
637                                 fprintf(F, "%s  overwrites:\n", prefix);
638                                 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
639                                         ir_entity *ov = get_entity_overwrites(ent, i);
640                                         fprintf(F, "%s    %d: %s of class %s\n", prefix, i, get_entity_name(ov),
641                                                 get_type_name(get_entity_owner(ov)));
642                                 }
643                         } else {
644                                 fprintf(F, "%s  Does not overwrite other entities. \n", prefix);
645                         }
646                         if (get_entity_n_overwrittenby(ent) > 0) {
647                                 fprintf(F, "%s  overwritten by:\n", prefix);
648                                 for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
649                                         ir_entity *ov = get_entity_overwrittenby(ent, i);
650                                         fprintf(F, "%s    %d: %s of class %s\n", prefix, i, get_entity_name(ov),
651                                                 get_type_name(get_entity_owner(ov)));
652                                 }
653                         } else {
654                                 fprintf(F, "%s  Is not overwritten by other entities. \n", prefix);
655                         }
656
657                         if (get_irp_inh_transitive_closure_state() != inh_transitive_closure_none) {
658                                 ir_entity *ov;
659                                 fprintf(F, "%s  transitive overwrites:\n", prefix);
660                                 for (ov = get_entity_trans_overwrites_first(ent);
661                                 ov;
662                                 ov = get_entity_trans_overwrites_next(ent)) {
663                                         fprintf(F, "%s    : %s of class %s\n", prefix, get_entity_name(ov),
664                                                 get_type_name(get_entity_owner(ov)));
665                                 }
666                                 fprintf(F, "%s  transitive overwritten by:\n", prefix);
667                                 for (ov = get_entity_trans_overwrittenby_first(ent);
668                                 ov;
669                                 ov = get_entity_trans_overwrittenby_next(ent)) {
670                                         fprintf(F, "%s    : %s of class %s\n", prefix, get_entity_name(ov),
671                                                 get_type_name(get_entity_owner(ov)));
672                                 }
673                         }
674                 }
675
676                 fprintf(F, "%s  allocation:  %s", prefix, get_allocation_name(get_entity_allocation(ent)));
677                 fprintf(F, "\n%s  visibility:  %s", prefix, get_visibility_name(get_entity_visibility(ent)));
678                 fprintf(F, "\n%s  variability: %s", prefix, get_variability_name(get_entity_variability(ent)));
679
680                 if (is_Method_type(get_entity_type(ent))) {
681                         unsigned mask = get_entity_additional_properties(ent);
682                         unsigned cc   = get_method_calling_convention(get_entity_type(ent));
683                         ir_graph *irg = get_entity_irg(ent);
684
685                         if (irg) {
686                                 fprintf(F, "\n%s  estimated node count: %u", prefix, get_irg_estimated_node_cnt(irg));
687                                 fprintf(F, "\n%s  maximum node index:   %u", prefix, get_irg_last_idx(irg));
688                         }
689
690                         if (mask) {
691                                 fprintf(F, "\n%s  additional prop: ", prefix);
692
693                                 if (mask & mtp_property_const)         fputs("const_function, ", F);
694                                 if (mask & mtp_property_pure)          fputs("pure_function, ", F);
695                                 if (mask & mtp_property_noreturn)      fputs("noreturn_function, ", F);
696                                 if (mask & mtp_property_nothrow)       fputs("nothrow_function, ", F);
697                                 if (mask & mtp_property_naked)         fputs("naked_function, ", F);
698                                 if (mask & mtp_property_malloc)        fputs("malloc_function, ", F);
699                                 if (mask & mtp_property_weak)          fputs("weak_function, ", F);
700                                 if (mask & mtp_property_returns_twice) fputs("weak_function, ", F);
701                                 if (mask & mtp_property_intrinsic)     fputs("intrinsic_function, ", F);
702                                 if (mask & mtp_property_runtime)       fputs("runtime_function, ", F);
703                                 if (mask & mtp_property_private)       fputs("private_function, ", F);
704                                 if (mask & mtp_property_has_loop)      fputs("has_loop_function, ", F);
705                         }
706                         fprintf(F, "\n%s  calling convention: ", prefix);
707                         if (cc & cc_reg_param)           fputs("regparam, ", F);
708                         if (cc & cc_this_call)           fputs("thiscall, ", F);
709                         if (cc & cc_compound_ret)        fputs("compound_ret, ", F);
710                         if (cc & cc_frame_on_caller_stk) fputs("frame on caller's stack, ", F);
711                         cc &= ~(cc_compound_ret|cc_frame_on_caller_stk);
712                         if (IS_CDECL(cc))
713                                 fputs("cdecl", F);
714                         else if (IS_STDCALL(cc))
715                                 fputs("stdcall", F);
716                         else {
717                                 fputs(cc & cc_last_on_top      ? "last param on top, " : "first param on top, ", F);
718                                 fputs(cc & cc_callee_clear_stk ? "callee clear stack" : "caller clear stack", F);
719                         }
720                         fprintf(F, "\n%s  vtable number:        %u", prefix, get_entity_vtable_number(ent));
721                 }
722
723                 fputc('\n', F);
724         } else {  /* no entattrs */
725                 fprintf(F, "%s(%3d:%d) %-40s: %s", prefix,
726                         get_entity_offset(ent), get_entity_offset_bits_remainder(ent),
727                         get_type_name(get_entity_type(ent)), get_entity_name(ent));
728                 if (is_Method_type(get_entity_type(ent))) fputs("(...)", F);
729
730                 if (verbosity & dump_verbosity_accessStats) {
731                         if (get_entity_allocation(ent) == allocation_static)        fputs(" (stat)", F);
732                         if (get_entity_peculiarity(ent) == peculiarity_description) fputs(" (desc)", F);
733                         if (get_entity_peculiarity(ent) == peculiarity_inherited)   fputs(" (inh)", F);
734                 }
735                 fputc('\n', F);
736         }
737
738         if (verbosity & dump_verbosity_entconsts) {
739                 if (get_entity_variability(ent) != variability_uninitialized) {
740                         if (ent->has_initializer) {
741                                 const ir_initializer_t *initializer = get_entity_initializer(ent);
742                                 fprintf(F, "\n%s  Initializers:", prefix);
743                                 need_nl = 1;
744                                 dump_ir_initializers_to_file(F, prefix, initializer, get_entity_type(ent));
745                         } else {
746                                 /* old compound_graph_path based initializers */
747                                 if (is_atomic_entity(ent)) {
748                                         fprintf(F, "%s  atomic value: ", prefix);
749                                         dump_node_opcode(F, get_atomic_ent_value(ent));
750                                 } else {
751                                         fprintf(F, "%s  compound values:", prefix);
752                                         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
753                                                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
754                                                 ir_entity *ent0 = get_compound_graph_path_node(path, 0);
755                                                 fprintf(F, "\n%s    %3d:%u ", prefix, get_entity_offset(ent0), get_entity_offset_bits_remainder(ent0));
756                                                 if (get_type_state(type) == layout_fixed)
757                                                         fprintf(F, "(%3u:%u) ",   get_compound_ent_value_offset_bytes(ent, i), get_compound_ent_value_offset_bit_remainder(ent, i));
758                                                 fprintf(F, "%s", get_entity_name(ent));
759                                                 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
760                                                         ir_entity *node = get_compound_graph_path_node(path, j);
761                                                         fprintf(F, ".%s", get_entity_name(node));
762                                                         if (is_Array_type(get_entity_owner(node)))
763                                                                 fprintf(F, "[%d]", get_compound_graph_path_array_index(path, j));
764                                                 }
765                                                 fprintf(F, "\t = ");
766                                                 dump_node_opcode(F, get_compound_ent_value(ent, i));
767                                         }
768                                 }
769                         }
770                         fputc('\n', F);
771                 }
772         }
773
774         if (verbosity & dump_verbosity_entattrs) {
775                 fprintf(F, "%s  volatility:  %s", prefix, get_volatility_name(get_entity_volatility(ent)));
776                 fprintf(F, "\n%s  alignment:  %s", prefix, get_align_name(get_entity_align(ent)));
777                 fprintf(F, "\n%s  peculiarity: %s", prefix, get_peculiarity_name(get_entity_peculiarity(ent)));
778                 fprintf(F, "\n%s  ld_name: %s", prefix, ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
779                 fprintf(F, "\n%s  offset:  %d bytes, %d rem bits", prefix, get_entity_offset(ent), get_entity_offset_bits_remainder(ent));
780                 if (is_Method_type(get_entity_type(ent))) {
781                         if (get_entity_irg(ent))   /* can be null */ {
782                                 fprintf(F, "\n%s  irg = %ld", prefix, get_irg_graph_nr(get_entity_irg(ent)));
783 #ifdef INTERPROCEDURAL_VIEW
784                                 if (get_irp_callgraph_state() == irp_callgraph_and_calltree_consistent) {
785                                         fprintf(F, "\n%s    recursion depth %d", prefix, get_irg_recursion_depth(get_entity_irg(ent)));
786                                         fprintf(F, "\n%s    loop depth      %d", prefix, get_irg_loop_depth(get_entity_irg(ent)));
787                                 }
788 #endif
789                         } else {
790                                 fprintf(F, "\n%s  irg = NULL", prefix);
791                         }
792                 }
793                 fputc('\n', F);
794         }
795
796         if (get_trouts_state()) {
797                 fprintf(F, "%s  Entity outs:\n", prefix);
798                 dump_node_list(F, (firm_kind *)ent, prefix, (int(*)(firm_kind *))get_entity_n_accesses,
799                         (ir_node *(*)(firm_kind *, int))get_entity_access, "Accesses");
800                 dump_node_list(F, (firm_kind *)ent, prefix, (int(*)(firm_kind *))get_entity_n_references,
801                         (ir_node *(*)(firm_kind *, int))get_entity_reference, "References");
802         }
803
804         if (verbosity & dump_verbosity_accessStats) {
805 #ifdef EXTENDED_ACCESS_STATS
806                 int n_acc = get_entity_n_accesses(ent);
807                 int max_depth = 0;
808                 int max_L_freq = -1;
809                 int max_S_freq = -1;
810                 int max_LA_freq = -1;
811                 int max_SA_freq = -1;
812                 int *L_freq;
813                 int *S_freq;
814                 int *LA_freq;
815                 int *SA_freq;
816
817                 /* Find maximal depth */
818                 for (i = 0; i < n_acc; ++i) {
819                         ir_node *acc = get_entity_access(ent, i);
820                         int depth = get_weighted_loop_depth(acc);
821                         max_depth = (depth > max_depth) ? depth : max_depth ;
822                 }
823
824                 L_freq = XMALLOCNZ(int, 4 * max_depth);
825
826                 S_freq  = L_freq + 1*max_depth;
827                 LA_freq = L_freq + 2*max_depth;
828                 SA_freq = L_freq + 3*max_depth;
829
830                 for (i = 0; i < n_acc; ++i) {
831                         ir_node *acc = get_entity_access(ent, i);
832                         int depth = get_weighted_loop_depth(acc);
833                         assert(depth < max_depth);
834                         if (is_Load(acc) || is_Call(acc)) {
835                                 L_freq[depth]++;
836                                 max_L_freq = (depth > max_L_freq) ? depth : max_L_freq;
837                                 if (addr_is_alloc(acc)) {
838                                         LA_freq[depth]++;
839                                         max_LA_freq = (depth > max_LA_freq) ? depth : max_LA_freq;
840                                 }
841                         } else if (is_Store(acc)) {
842                                 S_freq[depth]++;
843                                 max_S_freq = (depth > max_S_freq) ? depth : max_S_freq;
844                                 if (addr_is_alloc(acc)) {
845                                         SA_freq[depth]++;
846                                         max_SA_freq = (depth > max_SA_freq) ? depth : max_SA_freq;
847                                 }
848                         } else {
849                                 assert(0);
850                         }
851                 }
852
853                 if (max_L_freq >= 0) {
854                         char comma = ':';
855
856                         fprintf(F, "%s  Load  Stats", prefix);
857                         for (i = 0; i <= max_L_freq; ++i) {
858                                 if (L_freq[i])
859                                         fprintf(F, "%c %d x  L%d", comma, L_freq[i], i);
860                                 else
861                                         fprintf(F, "         ");
862                                 comma = ',';
863                         }
864                         fprintf(F, "\n");
865                 }
866                 if (max_LA_freq >= 0) {
867                         //fprintf(F, "%s  LoadA Stats", prefix);
868                         char comma = ':';
869                         for (i = 0; i <= max_LA_freq; ++i) {
870                                 //if (LA_freq[i])
871                                 //fprintf(F, "%c %d x LA%d", comma, LA_freq[i], i);
872                                 //else
873                                 //fprintf(F, "         ");
874                                 comma = ',';
875                         }
876                         fprintf(F, "\n");
877                 }
878                 if (max_S_freq >= 0) {
879                         char comma = ':';
880
881                         fprintf(F, "%s  Store Stats", prefix);
882                         for (i = 0; i <= max_S_freq; ++i) {
883                                 if (S_freq[i])
884                                         fprintf(F, "%c %d x  S%d", comma, S_freq[i], i);
885                                 else
886                                         fprintf(F, "         ");
887                                 comma = ',';
888                         }
889                         fprintf(F, "\n");
890                 }
891                 if (max_SA_freq >= 0) {
892                         //fprintf(F, "%s  StoreAStats", prefix);
893                         char comma = ':';
894                         for (i = 0; i <= max_SA_freq; ++i) {
895                                 //if (SA_freq[i])
896                                 //fprintf(F, "%c %d x SA%d", comma, SA_freq[i], i);
897                                 //else
898                                 //fprintf(F, "         ");
899                                 comma = ',';
900                         }
901                         fprintf(F, "\n");
902                 }
903
904                 /* free allocated space */
905                 free(L_freq);
906 #endif
907                 if (get_trouts_state() != outs_none) {
908 #ifdef INTERPROCEDURAL_VIEW
909                         if (is_Method_type(get_entity_type(ent))) {
910                                 fprintf(F, "%s  Estimated #Calls:    %lf\n", prefix, get_entity_estimated_n_calls(ent));
911                                 fprintf(F, "%s  Estimated #dynCalls: %lf\n", prefix, get_entity_estimated_n_calls(ent));
912                         } else {
913                                 fprintf(F, "%s  Estimated #Loads:  %lf\n", prefix, get_entity_estimated_n_loads(ent));
914                                 fprintf(F, "%s  Estimated #Stores: %lf\n", prefix, get_entity_estimated_n_stores(ent));
915                         }
916 #endif
917                 }
918         }
919 }
920
921 void    dump_entity_to_file (FILE *F, ir_entity *ent, unsigned verbosity) {
922         dump_entity_to_file_prefix (F, ent, "", verbosity);
923         fprintf(F, "\n");
924 }
925
926 void dump_entity(ir_entity *ent) {
927   dump_entity_to_file(stdout, ent, dump_verbosity_max);
928 }
929
930 void dump_entitycsv_to_file_prefix(FILE *F, ir_entity *ent, char *prefix,
931                                    unsigned verbosity, int *max_disp,
932                                    int disp[], const char *comma)
933 {
934         (void) verbosity;
935         (void) max_disp;
936         (void) disp;
937         (void) comma;
938 #if 0   /* Outputs loop depth of all occurrences. */
939         int n_acc = get_entity_n_accesses(ent);
940         int max_L_freq = -1;
941         int max_S_freq = -1;
942         int max_LA_freq = -1;
943         int max_SA_freq = -1;
944         int *L_freq;
945         int *S_freq;
946         int *LA_freq;
947         int *SA_freq;
948         int i, max_depth = 0;
949
950         /* Find maximal depth */
951         for (i = 0; i < n_acc; ++i) {
952                 ir_node *acc = get_entity_access(ent, i);
953                 int depth = get_weighted_loop_depth(acc);
954                 max_depth = (depth > max_depth) ? depth : max_depth ;
955         }
956
957         L_freq = XMALLOCNZ(int, 4 * (max_depth + 1));
958
959         S_freq  = L_freq + 1*max_depth;
960         LA_freq = L_freq + 2*max_depth;
961         SA_freq = L_freq + 3*max_depth;
962
963         for (i = 0; i < n_acc; ++i) {
964                 ir_node *acc = get_entity_access(ent, i);
965                 int depth = get_weighted_loop_depth(acc);
966                 assert(depth <= max_depth);
967                 if (is_Load(acc) || is_Call(acc)) {
968                         L_freq[depth]++;
969                         max_L_freq = (depth > max_L_freq) ? depth : max_L_freq;
970                         if (addr_is_alloc(acc)) {
971                                 LA_freq[depth]++;
972                                 max_LA_freq = (depth > max_LA_freq) ? depth : max_LA_freq;
973                         }
974                         if (get_entity_allocation(ent) == allocation_static) {
975                                 disp[depth]++;
976                                 *max_disp = (depth > *max_disp) ? depth : *max_disp;
977                         }
978                 } else if (is_Store(acc)) {
979                         S_freq[depth]++;
980                         max_S_freq = (depth > max_S_freq) ? depth : max_S_freq;
981                         if (addr_is_alloc(acc)) {
982                                 SA_freq[depth]++;
983                                 max_SA_freq = (depth > max_SA_freq) ? depth : max_SA_freq;
984                         }
985                         if (get_entity_allocation(ent) == allocation_static) {
986                                 assert(0);
987                         }
988                 } else {
989                         assert(0);
990                 }
991         }
992
993         if (get_entity_allocation(ent) != allocation_static) {
994
995                 fprintf(F, "%s_%s", get_type_name(get_entity_owner(ent)), get_entity_name(ent));
996
997                 if (max_L_freq >= 0) {
998                         fprintf(F, "%s Load", comma);
999                         for (i = 0; i <= max_L_freq; ++i) {
1000                                 fprintf(F, "%s %d", comma, L_freq[i]);
1001                         }
1002                 }
1003                 if (max_S_freq >= 0) {
1004                         if (max_L_freq >= 0)    fprintf(F, "\n%s_%s", get_type_name(get_entity_owner(ent)), get_entity_name(ent));
1005                         fprintf(F, "%s Store", comma);
1006                         for (i = 0; i <= max_S_freq; ++i) {
1007                                 fprintf(F, "%s %d", comma, S_freq[i]);
1008                         }
1009                 }
1010                 fprintf(F, "\n");
1011         }
1012         free(L_freq);
1013 #endif
1014
1015         if (get_entity_allocation(ent) != allocation_static) {
1016                 if (is_Method_type(get_entity_type(ent))) return;
1017
1018                 /* Output the entity name. */
1019                 fprintf(F, "%s%-40s ", prefix, get_entity_ld_name(ent));
1020
1021 #ifdef INTERPROCEDURAL_VIEW
1022                 if (get_trouts_state() != outs_none) {
1023                         if (is_Method_type(get_entity_type(ent))) {
1024                                 //fprintf(F, "%s  Estimated #Calls:    %lf\n", prefix, get_entity_estimated_n_calls(ent));
1025                                 //fprintf(F, "%s  Estimated #dynCalls: %lf\n", prefix, get_entity_estimated_n_calls(ent));
1026                         } else {
1027                                 fprintf(F, "%6.2lf ", get_entity_estimated_n_loads(ent));
1028                                 fprintf(F, "%6.2lf", get_entity_estimated_n_stores(ent));
1029                         }
1030                 }
1031 #endif
1032
1033                 fprintf(F, "\n");
1034         }
1035 }
1036
1037 #ifdef INTERPROCEDURAL_VIEW
1038 /* A fast hack to dump a CSV-file. */
1039 void dump_typecsv_to_file(FILE *F, ir_type *tp, dump_verbosity verbosity, const char *comma) {
1040         int i;
1041         char buf[1024 + 10];
1042         (void) comma;
1043
1044         if (!is_Class_type(tp)) return;   // we also want array types. Stupid, these are classes in java.
1045
1046         if (verbosity & dump_verbosity_accessStats) {
1047
1048 #if 0
1049                 /* Outputs loop depth of all occurrences. */
1050                 int max_freq = -1;
1051                 int max_disp = -1;
1052                 int *freq, *disp; /* Accumulated accesses to static members: dispatch table. */
1053                 int n_all = get_type_n_allocs(tp);
1054                 int max_depth = 0;
1055                 /* Find maximal depth */
1056                 for (i = 0; i < n_all; ++i) {
1057                         ir_node *all = get_type_alloc(tp, i);
1058                         int depth = get_weighted_loop_depth(all);
1059                         max_depth = (depth > max_depth) ? depth : max_depth ;
1060                 }
1061
1062                 freq = XMALLOCNZ(int, 2 * (max_depth + 1));
1063
1064                 disp = freq + max_depth;
1065
1066                 for (i = 0; i < n_all; ++i) {
1067                         ir_node *all = get_type_alloc(tp, i);
1068                         int depth = get_weighted_loop_depth(all);
1069                         assert(depth <= max_depth);
1070                         freq[depth]++;
1071                         max_freq = (depth > max_freq) ? depth : max_freq;
1072                         assert(is_Alloc(all));
1073                 }
1074
1075                 fprintf(F, "%s ", get_type_name(tp));
1076                 fprintf(F, "%s Alloc ", comma);
1077
1078                 if (max_freq >= 0) {
1079                         for (i = 0; i <= max_freq; ++i) {
1080                                 fprintf(F, "%s %d", comma, freq[i]);
1081                         }
1082                 }
1083                 fprintf(F, "\n");
1084
1085                 for (i = 0; i < get_class_n_members(tp); ++i) {
1086                         ir_entity *mem = get_class_member(tp, i);
1087                         if (((verbosity & dump_verbosity_methods) &&  is_Method_type(get_entity_type(mem))) ||
1088                                 ((verbosity & dump_verbosity_fields)  && !is_Method_type(get_entity_type(mem)))   ) {
1089                                 if (!((verbosity & dump_verbosity_nostatic) && (get_entity_allocation(mem) == allocation_static))) {
1090                                         dump_entitycsv_to_file_prefix(F, mem, "    ", verbosity, &max_disp, disp, comma);
1091                                 }
1092                         }
1093                 }
1094
1095                 if (max_disp >= 0) {
1096                         fprintf(F, "%s__disp_tab%s Load", get_type_name(tp), comma);
1097                         for (i = 0; i <= max_disp; ++i) {
1098                                 fprintf(F, "%s %d", comma, disp[i]);
1099                         }
1100                         fprintf(F, "\n");
1101                 }
1102
1103                 /* free allocated space */
1104                 free(freq);
1105 #endif
1106
1107 #define DISP_TAB_SUFFIX "__disp_tab"
1108                 if (get_trouts_state() != outs_none) {
1109                         assert(strlen(get_type_name(tp)) < 1024);
1110                         fprintf(F, "%-44s %6.2lf  -1.00\n", get_type_name(tp), get_type_estimated_n_instances(tp));
1111                         sprintf(buf, "%s%s", get_type_name(tp), DISP_TAB_SUFFIX);
1112                         fprintf(F, "%-44s %6.2lf   0.00\n", buf, get_class_estimated_n_dyncalls(tp));
1113                 }
1114
1115                 for (i = 0; i < get_class_n_members(tp); ++i) {
1116                         ir_entity *mem = get_class_member(tp, i);
1117                         if (((verbosity & dump_verbosity_methods) &&  is_Method_type(get_entity_type(mem))) ||
1118                                 ((verbosity & dump_verbosity_fields)  && !is_Method_type(get_entity_type(mem)))   ) {
1119                                 if (!((verbosity & dump_verbosity_nostatic) && (get_entity_allocation(mem) == allocation_static))) {
1120                                         dump_entitycsv_to_file_prefix(F, mem, "    ", verbosity, NULL, 0, 0);
1121                                 }
1122                         }
1123                 }
1124         }
1125 }
1126 #endif
1127
1128 void dump_type_to_file(FILE *F, ir_type *tp, dump_verbosity verbosity) {
1129         int i;
1130
1131         if ((is_Class_type(tp))       && (verbosity & dump_verbosity_noClassTypes)) return;
1132         if ((is_Struct_type(tp))      && (verbosity & dump_verbosity_noStructTypes)) return;
1133         if ((is_Union_type(tp))       && (verbosity & dump_verbosity_noUnionTypes)) return;
1134         if ((is_Array_type(tp))       && (verbosity & dump_verbosity_noArrayTypes)) return;
1135         if ((is_Pointer_type(tp))     && (verbosity & dump_verbosity_noPointerTypes)) return;
1136         if ((is_Method_type(tp))      && (verbosity & dump_verbosity_noMethodTypes)) return;
1137         if ((is_Primitive_type(tp))   && (verbosity & dump_verbosity_noPrimitiveTypes)) return;
1138         if ((is_Enumeration_type(tp)) && (verbosity & dump_verbosity_noEnumerationTypes)) return;
1139
1140         fprintf(F, "%s type %s (%ld)", get_tpop_name(get_type_tpop(tp)), get_type_name(tp), get_type_nr(tp));
1141         if (verbosity & dump_verbosity_onlynames) { fprintf(F, "\n"); return; }
1142
1143         switch (get_type_tpop_code(tp)) {
1144
1145         case tpo_class:
1146                 if ((verbosity & dump_verbosity_methods) || (verbosity & dump_verbosity_fields)) {
1147                         fprintf(F, "\n  members: \n");
1148                 }
1149                 for (i = 0; i < get_class_n_members(tp); ++i) {
1150                         ir_entity *mem = get_class_member(tp, i);
1151                         if (((verbosity & dump_verbosity_methods) &&  is_Method_type(get_entity_type(mem))) ||
1152                                 ((verbosity & dump_verbosity_fields)  && !is_Method_type(get_entity_type(mem)))   ) {
1153                                 if (!((verbosity & dump_verbosity_nostatic) && (get_entity_allocation(mem) == allocation_static))) {
1154                                         dump_entity_to_file_prefix(F, mem, "    ", verbosity);
1155                                 }
1156                         }
1157                 }
1158                 if (verbosity & dump_verbosity_typeattrs) {
1159                         fprintf(F, "  supertypes: ");
1160                         for (i = 0; i < get_class_n_supertypes(tp); ++i) {
1161                                 ir_type *stp = get_class_supertype(tp, i);
1162                                 fprintf(F, "\n    %d %s", i, get_type_name(stp));
1163                         }
1164                         fprintf(F, "\n  subtypes: ");
1165                         for (i = 0; i < get_class_n_subtypes(tp); ++i) {
1166                                 ir_type *stp = get_class_subtype(tp, i);
1167                                 fprintf(F, "\n    %d %s", i, get_type_name(stp));
1168                         }
1169
1170                         if (get_irp_inh_transitive_closure_state() != inh_transitive_closure_none) {
1171                                 ir_type *stp;
1172                                 fprintf(F, "\n  transitive supertypes: ");
1173                                 for (stp = get_class_trans_supertype_first(tp);
1174                                 stp;
1175                                 stp = get_class_trans_supertype_next(tp)) {
1176                                         fprintf(F, "\n    %s", get_type_name(stp));
1177                                 }
1178                                 fprintf(F, "\n  transitive subtypes: ");
1179                                 for (stp = get_class_trans_subtype_first(tp);
1180                                 stp;
1181                                 stp = get_class_trans_subtype_next(tp)) {
1182                                         fprintf(F, "\n    %s", get_type_name(stp));
1183                                 }
1184                         }
1185
1186                         fprintf(F, "\n  peculiarity: %s\n", get_peculiarity_name(get_class_peculiarity(tp)));
1187                         fprintf(F, "\n  flags:       ");
1188                         if (is_class_final(tp))
1189                                 fprintf(F, "final, ");
1190                         if (is_class_interface(tp))
1191                                 fprintf(F, "interface, ");
1192                         if (is_class_abstract(tp))
1193                                 fprintf(F, "abstract, ");
1194                         fprintf(F, "\n");
1195                 }
1196                 break;
1197
1198         case tpo_union:
1199         case tpo_struct:
1200                 if (verbosity & dump_verbosity_fields) fprintf(F, "\n  members: ");
1201                 for (i = 0; i < get_compound_n_members(tp); ++i) {
1202                         ir_entity *mem = get_compound_member(tp, i);
1203                         if (verbosity & dump_verbosity_fields) {
1204                                 dump_entity_to_file_prefix(F, mem, "    ", verbosity);
1205                         }
1206                 }
1207                 break;
1208
1209         case tpo_array:
1210                 if (verbosity & dump_verbosity_typeattrs) {
1211                         int i, n_dim;
1212                         ir_type *elem_tp = get_array_element_type(tp);
1213
1214                         fprintf(F, "\n  array ");
1215
1216                         n_dim = get_array_n_dimensions(tp);
1217                         for (i = 0; i < n_dim; ++i) {
1218                                 ir_node *lower, *upper;
1219
1220                                 lower = get_array_lower_bound(tp, i);
1221                                 upper = get_array_upper_bound(tp, i);
1222
1223                                 fprintf(F, "[");
1224
1225                                 if (is_Const(lower)) {
1226                                         fprintf(F, "%ld .. ", get_tarval_long(get_Const_tarval(lower)));
1227                                 } else {
1228                                         dump_node_opcode(F, lower);
1229                                         fprintf(F, " %ld .. ", get_irn_node_nr(lower));
1230                                 }
1231
1232                                 if (is_Const(upper)) {
1233                                         fprintf(F, "%ld]", get_tarval_long(get_Const_tarval(lower)));
1234                                 } else {
1235                                         dump_node_opcode(F, upper);
1236                                         fprintf(F, " %ld]", get_irn_node_nr(upper));
1237                                 }
1238                         }
1239                         fprintf(F, " of <%s (%ld)>", get_type_name(elem_tp), get_type_nr(elem_tp));
1240
1241                         fprintf(F, "\n  order: ");
1242                         for (i = 0; i < n_dim; ++i)
1243                                 fprintf(F, "<%d>", get_array_order(tp, i));
1244
1245                         fprintf(F, "\n");
1246
1247                         if (verbosity & dump_verbosity_fields) {
1248                                 dump_entity_to_file_prefix(F, get_array_element_entity(tp),
1249                                         "    ", verbosity);
1250                         }
1251                 }
1252                 break;
1253
1254         case tpo_pointer:
1255                 if (verbosity & dump_verbosity_typeattrs) {
1256                         ir_type *tt = get_pointer_points_to_type(tp);
1257                         fprintf(F, "\n  points to %s (%ld)\n", get_type_name(tt), get_type_nr(tt));
1258                 }
1259                 break;
1260
1261         case tpo_method:
1262                 if (verbosity & dump_verbosity_typeattrs) {
1263                         fprintf(F, "\n  variadicity: %s", get_variadicity_name(get_method_variadicity(tp)));
1264                         fprintf(F, "\n  return types: %d", get_method_n_ress(tp));
1265                         for (i = 0; i < get_method_n_ress(tp); ++i) {
1266                                 ir_type *rtp = get_method_res_type(tp, i);
1267                                 fprintf(F, "\n    %s", get_type_name(rtp));
1268                         }
1269
1270                         fprintf(F, "\n  parameter types: %d", get_method_n_params(tp));
1271                         for (i = 0; i < get_method_n_params(tp); ++i) {
1272                                 ir_type *ptp = get_method_param_type(tp, i);
1273                                 fprintf(F, "\n    %s", get_type_name(ptp));
1274                         }
1275                         if (get_method_variadicity(tp)) {
1276                                 fprintf(F, "\n    ...");
1277                         }
1278                         fprintf(F, "\n");
1279                 }
1280                 break;
1281
1282         case tpo_primitive:
1283                 if (verbosity & dump_verbosity_typeattrs) {
1284                         ir_type *base_tp = get_primitive_base_type(tp);
1285                         if (base_tp != NULL)
1286                                 fprintf(F, "\n  base type: %s (%ld)", get_type_name(tp), get_type_nr(tp));
1287                         fprintf(F, "\n");
1288                 }
1289                 break;
1290
1291         case tpo_id:
1292         case tpo_none:
1293         case tpo_unknown:
1294                 fprintf(F, "\n");
1295                 break;
1296
1297         default:
1298                 if (verbosity & dump_verbosity_typeattrs) {
1299                         fprintf(F, ": details not implemented\n");
1300                 }
1301         }
1302
1303         fprintf(F, "  visibility: %s,\n", get_visibility_name(get_type_visibility(tp)));
1304         fprintf(F, "  state:      %s,\n", get_type_state_name(get_type_state(tp)));
1305         fprintf(F, "  size:       %2u Bytes,\n", get_type_size_bytes(tp));
1306         fprintf(F, "  alignment:  %2u Bytes,\n", get_type_alignment_bytes(tp));
1307         if (is_atomic_type(tp) || is_Method_type(tp))
1308                 fprintf(F, "  mode:       %s,\n",  get_mode_name(get_type_mode(tp)));
1309
1310         if (get_trouts_state()) {
1311                 fprintf(F, "\n  Type outs:\n");
1312                 dump_node_list(F, (firm_kind *)tp, "  ", (int(*)(firm_kind *))get_type_n_allocs,
1313                         (ir_node *(*)(firm_kind *, int))get_type_alloc, "Allocations");
1314                 dump_node_list(F, (firm_kind *)tp, "  ", (int(*)(firm_kind *))get_type_n_casts,
1315                         (ir_node *(*)(firm_kind *, int))get_type_cast, "Casts");
1316                 dump_type_list(F, tp, "  ", get_type_n_pointertypes_to, get_type_pointertype_to, "PointerTpsTo");
1317         }
1318
1319
1320         if (verbosity & dump_verbosity_accessStats) {
1321 #if 0
1322                 int n_all = get_type_n_allocs(tp);
1323                 int max_depth = 0;
1324                 int max_freq = -1;
1325                 int *freq;
1326
1327                 /* Find maximal depth */
1328                 for (i = 0; i < n_all; ++i) {
1329                         ir_node *all = get_type_alloc(tp, i);
1330                         int depth = get_weighted_loop_depth(all);
1331                         max_depth = (depth > max_depth) ? depth : max_depth ;
1332                 }
1333
1334                 freq = XMALLOCNZ(int, max_depth + 1);
1335
1336                 for (i = 0; i < n_all; ++i) {
1337                         ir_node *all = get_type_alloc(tp, i);
1338                         int depth = get_weighted_loop_depth(all);
1339                         assert(depth <= max_depth);
1340                         freq[depth]++;
1341                         max_freq = (depth > max_freq) ? depth : max_freq;
1342                         assert(is_Alloc(all));
1343                 }
1344
1345                 if (max_freq >= 0) {
1346                         char comma = ':';
1347
1348                         fprintf(F, "  Alloc Stats");
1349                         for (i = 0; i <= max_freq; ++i) {
1350                                 fprintf(F, "%c %d x A%d", comma, freq[i], i);
1351                                 comma = ',';
1352                         }
1353                         fprintf(F, "\n");
1354                 }
1355
1356                 free(freq);
1357 #endif
1358 #ifdef INTERPROCEDURAL_VIEW
1359                 if (get_trouts_state() != outs_none) {
1360                         fprintf(F, "  Estimated #Instances: %lf\n", get_type_estimated_n_instances(tp));
1361                         if (is_Class_type(tp) && (get_irp_typeinfo_state() != ir_typeinfo_none)) {
1362                                 fprintf(F, "  Estimated #dyn Calls: %lf\n", get_class_estimated_n_dyncalls(tp));
1363                                 fprintf(F, "  Estimated #Upcasts:   %lf (#CastOps: %d)\n", get_class_estimated_n_upcasts(tp), get_class_n_upcasts(tp));
1364                                 fprintf(F, "  Estimated #Downcasts: %lf (#CastOps: %d)\n", get_class_estimated_n_downcasts(tp), get_class_n_downcasts(tp));
1365                                 assert(get_class_n_upcasts(tp) + get_class_n_downcasts(tp) == get_type_n_casts(tp));
1366                         }
1367                 }
1368 #endif
1369
1370         }
1371
1372         fprintf(F, "\n\n");
1373 }
1374
1375 void dump_type(ir_type *tp) {
1376         dump_type_to_file (stdout, tp, dump_verbosity_max);
1377 }
1378
1379
1380 void dump_types_as_text(unsigned verbosity, const char *suffix) {
1381         const char *basename;
1382         FILE *F, *CSV = NULL;
1383         int i, n_types = get_irp_n_types();
1384
1385         basename = irp_prog_name_is_set() ? get_irp_prog_name() : "TextTypes";
1386         F = text_open(basename, suffix, "-types", ".txt");
1387
1388         if (verbosity & dump_verbosity_csv) {
1389                 CSV = text_open(basename, suffix, "-types", ".csv");
1390                 //fprintf(CSV, "Class, Field, Operation, L0, L1, L2, L3\n");
1391         }
1392
1393         for (i = 0; i < n_types; ++i) {
1394                 ir_type *t = get_irp_type(i);
1395
1396                 //if (is_jack_rts_class(t)) continue;
1397
1398                 dump_type_to_file(F, t, verbosity);
1399 #ifdef INTERPROCEDURAL_VIEW
1400                 if (CSV) {
1401                         dump_typecsv_to_file(CSV, t, verbosity, "");
1402                 }
1403 #endif
1404         }
1405
1406         fclose(F);
1407         if (CSV) fclose(CSV);
1408 }
1409
1410
1411 void dump_globals_as_text(unsigned verbosity, const char *suffix) {
1412         const char *basename;
1413         FILE *F, *CSV = NULL;
1414         ir_type *g = get_glob_type();
1415         int i, n_mems = get_class_n_members(g);
1416
1417         basename = irp_prog_name_is_set() ? get_irp_prog_name() : "TextGlobals";
1418         F = text_open (basename, suffix, "-globals", ".txt");
1419
1420         if (verbosity & dump_verbosity_csv) {
1421                 CSV = text_open (basename, suffix, "-types", ".csv");
1422                 //fprintf(CSV, "Class, Field, Operation, L0, L1, L2, L3\n");
1423         }
1424
1425         for (i = 0; i < n_mems; ++i) {
1426                 ir_entity *e = get_class_member(g, i);
1427
1428                 dump_entity_to_file(F, e, verbosity);
1429                 if (CSV) {
1430                         //dump_entitycsv_to_file_prefix(CSV, e, "", verbosity, ""???);
1431                 }
1432         }
1433
1434         fclose (F);
1435         if (CSV) fclose (CSV);
1436 }