removed double entered header file
[libfirm] / ir / ana2 / ecg.c
1 /* -*- c -*- */
2
3 /*
4  * Project:     libFIRM
5  * File name:   ir/ana/ecg.c
6  * Purpose:     Extended Call Graph
7  * Author:      Florian
8  * Modified by:
9  * Created:     14.09.2004
10  * CVS-ID:      $Id$
11  * Copyright:   (c) 1999-2004 Universität Karlsruhe
12  * Licence:     This file is protected by the GPL -  GNU GENERAL PUBLIC LICENSE.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18
19 /**
20    Erweiterter Aufrufgraph.
21 */
22
23 #include "irnode.h"
24 #include "pmap.h"
25 /* #include "eset.h" */
26 #include "irgwalk.h"
27 #include "irgmod.h"
28 #include "irvrfy.h"
29 #include "trvrfy.h"
30 #include "xmalloc.h"
31
32 # ifndef TRUE
33 #  define TRUE 1
34 #  define FALSE 0
35 # endif /* not defined TRUE */
36
37 # define BUF_SIZE 1024
38
39 # include "ecg.h"
40 # include "typalise.h"
41 # include "lset.h"
42
43 /*
44   le flag
45 */
46 /* static int verbose     = 0; */
47 static int do_typalise = 0;
48
49 /*
50   globals
51 */
52
53 /* Ids for the ctxs */
54 static int ctx_id = 0;
55 ctx_info_t *main_ctx = NULL;
56
57 /* mapping from method graphs (callR) to method graphs (lset_ts of callEds) */
58 /* static pmap *calls; */
59 static pmap *graph_infos;
60
61 /* linked list of all graph_infos: */
62 static graph_info_t *graph_infos_list = NULL;
63
64 /* Counters for ecg_ecg and friends */
65 static long _graphs = 0;
66 static long _calls  = 0;
67 static long _allocs = 0;
68
69 static int _depth = 0;
70 static int _max_depth = 0;
71
72 static int _max_callEds = 0;
73 static entity* _max_callEds_callR = NULL;
74
75 /* Protos */
76 void set_main_ctx (ctx_info_t*);
77
78 /* ====================
79    Alloc stuff
80    ==================== */
81 static void append_alloc (graph_info_t *ginfo, ir_node *alloc, type *tp)
82 {
83   alloc_info_t *ainfo = (alloc_info_t*) xmalloc (sizeof (alloc_info_t));
84
85   ainfo->graph = ginfo->graph;
86   ainfo->alloc = alloc;
87   ainfo->tp    = tp;
88
89   ainfo->prev = ginfo->allocs;
90   ginfo->allocs = ainfo;
91 }
92
93
94 /* ====================
95    CallEd stuff
96    ==================== */
97 /**
98    Append the given callEd to the given callEd info.
99 */
100 static callEd_info_t *append_callEd_info (callEd_info_t *ced, ir_graph *callEd)
101 {
102   callEd_info_t *nced = (callEd_info_t*) xmalloc (sizeof (sizeof (callEd_info_t)));
103
104   nced->callEd = callEd;
105   nced->prev = ced;
106
107   return (nced);
108 }
109
110 /**
111    Append all callEd methods of the given (call) node to the given graph_info.
112 */
113 static void append_calls (graph_info_t *info, ir_node *call, lset_t *callEds)
114 {
115   call_info_t *cinfo = (call_info_t*) xmalloc (sizeof (call_info_t));
116
117   /* setup */
118   cinfo->call = call;
119   cinfo->prev = info->calls;
120   info->calls = cinfo;
121   cinfo->callEds = NULL;
122
123   /* enter */
124   ir_graph *callEd = lset_first (callEds);
125   while (callEd) {
126     cinfo->callEds = append_callEd_info (cinfo->callEds, callEd);
127
128     callEd = lset_next (callEds);
129   }
130 }
131
132 /**
133    Append the (single) callEd to the given (call) node of the given graph_info.
134 */
135 static void append_call (graph_info_t *info, ir_node *call, ir_graph *callEd)
136 {
137   call_info_t *cinfo = (call_info_t*) xmalloc (sizeof (call_info_t));
138
139   cinfo->call = call;
140   cinfo->prev = info->calls;
141   info->calls = cinfo;
142
143   cinfo->callEds = append_callEd_info (cinfo->callEds, callEd);
144 }
145
146 /**
147    Given a method, find the firm graph that implements that method.
148    Return NULL for abstract and native methods.
149 */
150 static ir_graph *_get_implementing_graph (entity *method)
151 {
152   ir_graph *graph = NULL;
153
154   /* What's up with the fenced out stuff in rta? */
155   if (peculiarity_existent == get_entity_peculiarity (method)) {
156     if (visibility_external_allocated == get_entity_visibility (method)) {
157       /* Todo: native implementation */
158
159       return (NULL);
160     } else {
161       graph = get_entity_irg (get_SymConst_entity (get_atomic_ent_value (method)));
162       assert (graph && "no graph");
163
164       return (graph);
165     }
166   } else if (0 && (peculiarity_description == get_entity_peculiarity (method))) {
167     /* abstract --- can't find an implementation */
168     graph = get_entity_irg (method);
169     assert (!graph && "graph in abstract method");
170
171     return (NULL);
172   } else if ((peculiarity_description == get_entity_peculiarity (method)) ||
173              (peculiarity_inherited == get_entity_peculiarity (method))) {
174     /* search UPWARDS */
175     int i;
176     int n_over = get_entity_n_overwrites (method);
177
178     assert (!graph);
179
180     for (i = 0; (NULL == graph) && (i < n_over); i ++) {
181       entity *over = get_entity_overwrites (method, i);
182
183       graph = _get_implementing_graph (over);
184     }
185   } else {
186     assert (0 && "invalid peculiarity");
187   }
188
189
190   return (graph);
191 }
192
193 /**
194    Collect all graphs of 'method' in the given set.
195 */
196 static void _collect_implementing_graphs (entity *method, lset_t *set)
197 {
198   /* search DOWN-wards in clazz hierarchy */
199   int i;
200   int n_over = get_entity_n_overwrittenby (method);
201   ir_graph *graph = get_entity_irg (method);
202
203   if (NULL == graph) {
204     graph = _get_implementing_graph (method);
205   }
206
207   if (graph) {
208     lset_insert (set, graph);
209   }
210
211   for (i = 0; i < n_over; i ++) {
212     entity *over = get_entity_overwrittenby (method, i);
213
214     _collect_implementing_graphs (over, set);
215   }
216 }
217
218
219 /**
220    Collect all graphs that could possibly be executed when 'method' is called.
221 */
222 static lset_t *get_implementing_graphs (entity *method, ir_node *select)
223 {
224   lset_t *set = lset_create ();
225   {
226     ir_graph *impl = _get_implementing_graph (method);
227
228     if (NULL != impl) {
229       lset_insert (set, impl);
230     } else {
231       /* actually, abstract OR native */
232     }
233   }
234
235   _collect_implementing_graphs (method, set);
236
237   if (lset_empty (set)) {
238     /* then it's a method which is only implemented natively, and we
239        don' bother to analyse anything */
240     return (set);
241   }
242
243   /* void *tmp = lset_first (set); */
244   int n_graphs = lset_n_entries (set);
245
246   /* typalise select_in */
247   if (do_typalise) {
248     ir_node *select_in = get_Sel_ptr (select);
249     typalise_t *ta = typalise (select_in);
250     assert (ta && "typalise failed (go figure)");
251
252     /* const char *res = ta_name (ta); */
253
254     /* fprintf (stdout, "typalyse res = %s\n", res); */
255
256     if (1 != n_graphs) {
257       set = filter_for_ta (set, ta);
258
259       int n_filtered_graphs = lset_n_entries (set);
260
261       /*
262         fprintf (stdout, "%s: %02d %02d\n",
263         __FUNCTION__,
264         n_graphs,
265         n_filtered_graphs,
266         n_graphs - n_filtered_graphs);
267       */
268       n_graphs = n_filtered_graphs;
269     }
270   }
271
272   if (n_graphs > _max_callEds) {
273     _max_callEds = n_graphs;
274     _max_callEds_callR = method;
275   }
276
277
278   if (visibility_external_allocated != get_entity_visibility (method)) {
279     if (0 == n_graphs) {
280       /* fprintf (stdout, "no graphs for method %s\n", get_entity_name (method)); */
281       assert (n_graphs && "no graphs for method");
282     }
283   }
284
285   return (set);
286 }
287
288 /**
289    Action for the graph.
290 */
291 static void ecg_calls_act (ir_node *node, void *env)
292 {
293   opcode op = get_irn_opcode (node);
294   graph_info_t *graph_info = (graph_info_t*) env;
295
296   if (iro_Call == op) {         /* CALL */
297     entity *ent = NULL;
298     ir_node *ptr = get_Call_ptr (node);
299
300     /* CALL SEL */
301     if (iro_Sel == get_irn_opcode (ptr)) {
302       ent = get_Sel_entity (ptr);
303       lset_t *graphs = get_implementing_graphs (ent, ptr);
304
305       append_calls (graph_info, node, graphs);
306     } else if (iro_SymConst == get_irn_opcode (ptr)) {
307       if (get_SymConst_kind (ptr) == symconst_addr_ent) {
308         ent = get_SymConst_entity (ptr);
309         ir_graph *graph = get_entity_irg (ent);
310
311         if (graph) {
312           append_call (graph_info, node, graph);
313         } else {
314           /* it's an externally allocated thingy */
315         }
316       } else if (get_SymConst_kind (ptr) == symconst_addr_name) {
317         /* If this SymConst refers to a method the method is external_visible
318            and therefore must be considered live anyways. */
319         if (get_SymConst_name (ptr) != new_id_from_str ("iro_Catch")) {
320           assert (ent && "couldn't determine entity of call to symConst");
321         }
322       } else {
323         /* other symconst. */
324         assert (0 && "This SymConst can not be an address for a method call.");
325       }
326
327       /* STRANGE, no less ... */
328     } else {
329       DDMN (ptr);
330       assert (0 && "Unexpected address expression");
331     }
332   } else if (iro_Alloc == op) {
333     type *tp = get_Alloc_type (node);
334     /* const char *name = get_type_name (tp); */
335
336     append_alloc (graph_info, node, tp);
337
338     /* fprintf (stdout, "NEW \"%s\"\n", name); */
339   }
340 }
341
342 /**
343    Collect called graphs for the given graph.
344 */
345 static void ecg_fill_graph_calls (ir_graph *graph)
346 {
347   graph_info_t *graph_info = (graph_info_t*) xmalloc (sizeof (graph_info_t));
348
349   graph_info->graph = graph;
350   graph_info->calls = NULL;
351   graph_info->ecg_seen = 0;
352   graph_info->ctxs = NULL;
353   graph_info->n_ctxs = 0;
354
355   /* link up into global list */
356   graph_info->prev = graph_infos_list;
357   graph_infos_list = graph_info;
358
359   irg_walk_graph (graph, ecg_calls_act, NULL, graph_info);
360
361   pmap_insert (graph_infos, graph, graph_info);
362 }
363
364 /**
365    For each graph, collect called graphs, and enter them into calls.
366 */
367 static void ecg_fill_calls (void)
368 {
369   int i;
370
371   for (i = 0; i < get_irp_n_irgs (); i++) {
372     ir_graph *graph = get_irp_irg (i);
373
374     ecg_fill_graph_calls (graph);
375   }
376 }
377
378 /**
379    Allocate a new ctx for the given graph and the given enclosing ctx.
380 */
381 static ctx_info_t *new_ctx (ir_graph *graph, ir_node *call, ctx_info_t *enc)
382 {
383   ctx_info_t *res = xmalloc (sizeof (ctx_info_t));
384
385   res->graph = graph;
386   res->call = call;
387   res->enc = enc;
388   res->id = ctx_id ++;
389
390   return (res);
391 }
392
393
394 /**
395    Fill in the ctxs parts of the graph_infos
396 */
397 static void ecg_fill_ctxs_count (ir_graph *graph)
398 {
399   graph_info_t *ginfo = ecg_get_info (graph);
400
401   /* count how many ctxs we have per graph */
402   if (0 == ginfo->ecg_seen) {
403
404     ginfo->ecg_seen = 1;
405     call_info_t *cinfo = ginfo->calls;
406
407     while (NULL != cinfo) {
408       callEd_info_t *ced = cinfo->callEds;
409
410       while (NULL != ced) {
411         ir_graph *callEd_graph = ced->callEd;
412
413         /* first step: we have a new ctx */
414         graph_info_t *callEd_info = ecg_get_info (callEd_graph);
415         callEd_info->n_ctxs ++;
416
417         /* Calling graph -> callEd_graph */
418         ecg_fill_ctxs_count (callEd_graph);
419
420         ced = ced->prev;
421       } /* end forall callEds (call) */
422
423       cinfo = cinfo->prev;
424     } /* end forall (calls(graph)) */
425
426     ginfo->ecg_seen = 0;
427   }
428 }
429
430 static void ecg_fill_ctxs_alloc (void)
431 {
432   /* allocate the memory needed for the ctxts: */
433   graph_info_t *ginfo = graph_infos_list;
434
435   while (NULL != ginfo) {
436     ginfo->ctxs = (ctx_info_t **) xmalloc (ginfo->n_ctxs * sizeof (ctx_info_t*));
437
438     /*
439       fprintf (stdout, "graph of \"%s\": n_ctxs = %i\n",
440       get_entity_name (get_irg_entity (ginfo->graph)), ginfo->n_ctxs);
441     */
442     ginfo->n_ctxs = 0;
443
444     ginfo = ginfo->prev;
445   }
446 }
447
448 /**
449    Fill in the ctxs parts of the graph_infos
450 */
451 static void ecg_fill_ctxs_write (ir_graph *graph, ctx_info_t *enc_ctx)
452 {
453   graph_info_t *ginfo = ecg_get_info (graph);
454
455   /* enter a new ctx for all callEds along the call edges of this graph */
456   if (0 == ginfo->ecg_seen) {
457     ginfo->ecg_seen = 1;
458     call_info_t *cinfo = ginfo->calls;
459
460     while (NULL != cinfo) {
461       callEd_info_t *ced = cinfo->callEds;
462
463       while (NULL != ced) {
464         ctx_info_t *ctx = new_ctx (graph, cinfo->call, enc_ctx);
465
466         ir_graph *callEd_graph = ced->callEd;
467
468         /* write the ctx of this call into the callEd graph */
469         graph_info_t *callEd_info = ecg_get_info (callEd_graph);
470
471         callEd_info->ctxs [callEd_info->n_ctxs] = ctx;
472         callEd_info->n_ctxs ++;
473
474         /* Calling graph -> callEd_graph */
475         ecg_fill_ctxs_write (callEd_graph, ctx);
476
477         ced = ced->prev;
478       } /* end forall callEds (call) */
479
480       cinfo = cinfo->prev;
481     } /* end forall (calls(graph)) */
482
483     ginfo->ecg_seen = 0;
484   }
485 }
486
487 /**
488    Fill in the ctxs parts of the graph_infos
489 */
490 static void ecg_fill_ctxs (void)
491 {
492   ecg_fill_ctxs_count (get_irp_main_irg ());
493   ecg_fill_ctxs_alloc ();
494
495   ctx_info_t *main_ctx = new_ctx (get_irp_main_irg (), NULL, NULL);
496   ir_graph *main_irg = get_irp_main_irg ();
497
498   set_main_ctx (main_ctx);
499
500   /* Grrr, have to add this ctx manually to main.ginfo ... */
501   graph_info_t *ginfo = ecg_get_info (main_irg);
502   ginfo->n_ctxs = 1;
503   ginfo->ctxs = (ctx_info_t **) xmalloc (1 * sizeof (ctx_info_t*));
504   ginfo->ctxs [0] = main_ctx;
505
506   ecg_fill_ctxs_write (main_irg, main_ctx);
507 }
508
509 /* ====================
510    CTX stuff
511    ==================== */
512 /*
513   Nicely print a ctx_info_t to the given output stream
514 */
515 void ecg_print_ctx (ctx_info_t *ctx, FILE *stream)
516 {
517   entity *ent = get_irg_ent (ctx->graph);
518   ir_node *call = ctx->call;
519   const char *ent_name = (char*) get_entity_name (ent);
520   const char *own_name = (char*) get_type_name (get_entity_owner (ent));
521
522   fprintf (stream, "CTX[%i](%s.%s->%s[%li])",
523            ctx->id, own_name, ent_name,
524            get_op_name (get_irn_op (call)),
525            get_irn_node_nr (call));
526
527   if (NULL != ctx->enc) {
528     fprintf (stream, "->%i", ctx->enc->id);
529   }
530
531   fprintf (stream, "\n");
532 }
533
534 /*
535   Get a ctx of the given graph info
536 */
537 ctx_info_t *get_ctx (graph_info_t *ginfo, int ctx_idx)
538 {
539   assert (ginfo->n_ctxs > ctx_idx);
540
541   return (ginfo->ctxs [ctx_idx]);
542 }
543
544 /*
545   Get the pseudo-ctx of 'main'
546 */
547 ctx_info_t *get_main_ctx ()
548 {
549   return (main_ctx);
550 }
551
552 /*
553   Set the pseudo-ctx of 'main'
554 */
555 void set_main_ctx (ctx_info_t *ctx)
556 {
557   main_ctx = ctx;
558 }
559
560
561 /* ====================
562    ECG stuff
563    ==================== */
564
565 /* ====================
566    Iterator stuff
567    ==================== */
568 /*
569    Iterate over all graphs
570 */
571 void ecg_iterate_graphs (graph_hnd_t *hnd, void *env)
572 {
573   graph_info_t *ginfo = graph_infos_list;
574
575   while (NULL != ginfo) {
576     hnd (ginfo, env);
577
578     ginfo = ginfo->prev;
579   }
580 }
581
582
583 /*
584    Iterate of all allocs of a given graph info
585 */
586 void ecg_iterate_allocs (graph_info_t *ginfo, alloc_hnd_t *hnd, void *env)
587 {
588   alloc_info_t *ainfo = ginfo->allocs;
589
590   while (NULL != ainfo) {
591     hnd (ainfo, env);
592
593     ainfo = ainfo->prev;
594   }
595 }
596
597
598 /*
599   Iterate over all calls of the given graph info
600 */
601 void ecg_iterate_calls  (graph_info_t *ginfo, call_hnd_t *hnd, void *env)
602 {
603   call_info_t *cinfo = ginfo->calls;
604
605   while (NULL != cinfo) {
606     hnd (cinfo, env);
607
608     cinfo = cinfo->prev;
609   }
610 }
611
612
613 /*
614   Iterate over all callEds of the given call info
615 */
616 void ecg_iterate_callEds  (call_info_t *cinfo, callEd_hnd_t *hnd, void *env)
617 {
618   callEd_info_t *ced = cinfo->callEds;
619
620   while (NULL != ced) {
621     hnd (ced, env);
622
623     ced = ced->prev;
624   }
625 }
626
627
628 /*
629   get the call infos for the given graph
630 */
631 graph_info_t *ecg_get_info (ir_graph *graph)
632 {
633   graph_info_t *ginfo = (graph_info_t*) pmap_get (graph_infos, graph);
634
635   assert (ginfo && "no info for graph");
636
637   return (ginfo);
638 }
639
640 /*
641   Get the Alloc Infos for the given graph
642 */
643 alloc_info_t *ecg_get_alloc_info (ir_graph *graph)
644 {
645   graph_info_t *ginfo = ecg_get_info (graph);
646
647   return (ginfo->allocs);
648 }
649
650 /*
651   Get the Call Info for the given call
652 */
653 callEd_info_t *ecg_get_callEd_info (ir_node *call)
654 {
655   ir_graph *graph = get_irn_irg (call);
656   graph_info_t *ginfo = ecg_get_info (graph);
657
658   call_info_t *call_info = ginfo->calls;
659
660   while (NULL != call_info) {
661     if (call == call_info->call) {
662       return (call_info->callEds);
663     }
664
665     call_info = call_info->prev;
666   }
667
668   return (NULL);
669 }
670
671
672 /**
673    Dump the given graph and it's calls and it's calls callEds to the given file.
674 */
675 static int ecg_ecg_graph (FILE *dot, ir_graph *graph)
676 {
677   const char *name = get_irg_entity (graph) ?
678     get_entity_name (get_irg_entity (graph)) : "noEntity";
679   const char *color =
680     (get_entity_stickyness
681      (get_irg_entity (graph)) == stickyness_sticky) ?
682     "red" : "lightyellow";
683
684   /* graph_info_t *ginfo = (graph_info_t*) pmap_get (graph_infos, graph); */
685   graph_info_t *ginfo = ecg_get_info (graph);
686
687   if (0 != ginfo->ecg_seen) {
688     fprintf (dot, "\t/* recursive call to \"%s\" (%d) */\n",
689              name, (int) ginfo->ecg_seen);
690 # if 0
691     fprintf (dot, "\t/* recursive call to \"%s\" (0x%08x) */\n",
692              name, (int) graph);
693 # endif /* 0 */
694     return (ginfo->ecg_seen);
695   }
696
697   assert (0L <= _graphs);
698
699   const int graph_no = _graphs ++;
700   ginfo->ecg_seen = graph_no;
701
702   fprintf (dot, "\t/* Graph of \"%s.%s\" */\n",
703            get_type_name (get_entity_owner (get_irg_entity (graph))),
704            name);
705   fprintf (dot, "\tgraph_%i [label=\"<HEAD>%s\\l%s\\l|<CTX>n_ctx = %i\\l\", color=\"%s\"];\n",
706            graph_no,
707            get_type_name (get_entity_owner (get_irg_entity (graph))),
708            name,
709            ginfo->n_ctxs,
710            color);
711   fprintf (dot, "\n");
712
713   if (visibility_external_allocated ==
714       get_entity_visibility (get_irg_entity (graph))) {
715     fprintf (dot, "\t/* graph \"%s\" is external */\n", name);
716
717     return (graph_no);
718   }
719
720   call_info_t *cinfo = ginfo->calls;
721   while (NULL != cinfo) {
722     ir_node *call = cinfo->call;
723     callEd_info_t *ced = cinfo->callEds;
724     const int call_no = _calls ++;
725     const char *call_color = (NULL == ced->prev) ? "lightblue" : "blue3";
726
727     fprintf (dot, "\t/* Call %li */\n", get_irn_node_nr (call));
728     fprintf (dot, "\tcall_%i [label=\"call\\[%li\\]\", color=\"%s\", shape=\"ellipse\"];\n",
729              call_no, get_irn_node_nr (call), call_color);
730     fprintf (dot, "\tgraph_%i -> call_%i [color=\"black\"];\n", graph_no, call_no);
731
732     while (NULL != ced) {
733       ir_graph *callEd_graph = ced->callEd;
734       const int callEd_no = ecg_ecg_graph (dot, callEd_graph);
735       const char *callEd_name = get_irg_entity (callEd_graph) ?
736         get_entity_name (get_irg_entity (callEd_graph)) : "noEntity";
737       const char *direction = (callEd_no <= graph_no) ? "forward" : "forward";
738       const char *callEd_color     = (callEd_no <= graph_no) ? "red" : "black";
739
740       fprintf (dot, "\t/* Call from graph \"%s\" to graph \"%s\" */\n",
741                name,
742                callEd_name);
743       /* Check for recursive calls */
744       /* if (callEd_no > graph_no) */ { /* do recursive calls (for now) */
745         fprintf (dot, "\tcall_%i -> graph_%i:HEAD [color=\"%s\", dir=\"%s\"];\n",
746                  call_no, callEd_no, callEd_color, direction);
747       }
748
749       ced = ced->prev;
750       /* ced = NULL; */
751     } /* done all calEds (call) */
752
753     cinfo = cinfo->prev;
754   } /* done all calls (graph) */
755
756   /* now the allocs */
757   alloc_info_t *ainfo = ecg_get_alloc_info (graph);
758   if (ainfo) {
759     fprintf (dot, "\t/* now the allocs */\n");
760   } else {
761     fprintf (dot, "\t/* no allocs */\n");
762   }
763
764   while (NULL != ainfo) {
765     ir_node *alloc = ainfo->alloc;
766     const char *name = get_type_name (ainfo->tp);
767     const char *color = "red1";
768
769     _allocs ++;
770     fprintf (dot, "\talloc_0x%08x_%i [label=\"%s\", color=\"%s\"];\n",
771              (int) alloc, graph_no, name, color);
772
773     fprintf (dot, "\tgraph_%i -> alloc_0x%08x_%i;\n",
774              graph_no, (int) alloc, graph_no);
775
776     ainfo = ainfo->prev;
777   }
778
779   if (0 == ginfo->allocs_seen) {
780     ginfo->allocs_seen = 1;
781   }
782
783   /* write table of ctxs */
784   {
785     fprintf (dot, "\tctx_%i [label=\"<HEAD>", graph_no);
786
787     int i;
788     const int max_ctxs = 30;
789     const int n_ctxs = (ginfo->n_ctxs > max_ctxs) ? max_ctxs : ginfo->n_ctxs;
790
791     assert (ginfo->ctxs && "no ctx");
792     for (i = 0; i < n_ctxs; i ++) {
793       ctx_info_t *ctx_info = ginfo->ctxs [i];
794
795       if (NULL != ctx_info->enc) {
796         fprintf (dot, "ctx_info \\[%i\\] = ctx\\[%i\\-\\>%i\\]\\l",
797                  i,
798                  ctx_info->id,
799                  ctx_info->enc->id);
800       } else {
801         fprintf (dot, "ctx_info \\[%i\\] = ctx\\[%i\\]\\l",
802                  i, ctx_info->id);
803       }
804
805       if (i+1 != n_ctxs) {
806         fprintf (dot, "|");
807       }
808     }
809
810     if (0 < ginfo->n_ctxs - max_ctxs) {
811       fprintf (dot, "(%i more)\\l", ginfo->n_ctxs - max_ctxs);
812     }
813
814     fprintf (dot, "\", color=\"green3\"];\n");
815
816     fprintf (dot,
817              "\tgraph_%i:CTX -> ctx_%i:HEAD [label=\"ctx\", dir=\"none\", style=\"dotted\"];\n",
818              graph_no, graph_no);
819   }
820
821   fprintf (dot, "\t/* done with graph of \"%s\" */\n\n", name);
822
823   fflush (dot);
824   ginfo->ecg_seen = 0;
825
826   return (graph_no);
827 }
828
829 /**
830    Count how many nodes the ECG will have
831 */
832 static char spaces [BUF_SIZE];
833
834 static void ecg_ecg_count (ir_graph *graph)
835 {
836   graph_info_t *ginfo = (graph_info_t*) pmap_get (graph_infos, graph);
837
838   if (0 != ginfo->ecg_seen) {
839     return;
840   }
841
842   _depth ++;
843   if (_depth > _max_depth) {
844     _max_depth = _depth;
845
846     /*
847       fprintf (stdout, "_max_depth = %i\n", _max_depth);
848       fprintf (stdout, "\tn_graphs: %i\n", _graphs);
849     */
850   }
851
852   assert (0L <= _graphs);
853
854   /*
855     if (0 == (_graphs % 1000000)) {
856     fprintf (stdout, "\tn_graphs: %i\n", _graphs);
857     fprintf (stdout, "_depth = %i\n", _depth);
858     }
859   */
860
861   const int graph_no = _graphs ++;
862   ginfo->ecg_seen = graph_no;
863
864   fprintf (stdout, "%sMethod \"%s.%s\"\n",
865            spaces + BUF_SIZE - _depth,
866            get_type_name (get_entity_owner (get_irg_entity (graph))),
867            get_entity_name (get_irg_entity (graph)));
868
869   call_info_t *cinfo = ginfo->calls;
870   while (NULL != cinfo) {
871
872     callEd_info_t *ced = cinfo->callEds;
873
874     fprintf (stdout, "%sCall \"0x%08x\"\n",
875              spaces + BUF_SIZE - _depth,
876              (int) cinfo->call);
877
878     while (NULL != ced) {
879       ir_graph *callEd_graph = ced->callEd;
880
881       fprintf (stdout, "%sCall Target \"%s.%s\"\n",
882                spaces + BUF_SIZE - _depth,
883                get_type_name (get_entity_owner (get_irg_entity (callEd_graph))),
884                get_entity_name (get_irg_entity (callEd_graph)));
885
886       ecg_ecg_count (callEd_graph);
887
888       ced = ced->prev;
889     } /* done all calEds (call) */
890     cinfo = cinfo->prev;
891   } /* done all calls (graph) */
892
893   ginfo->ecg_seen = 0;
894   _depth --;
895 }
896
897 /* ====================
898    Public Interface
899    ==================== */
900
901 /**
902    Initialise our data structures.
903 */
904 void ecg_init (int typalise)
905 {
906   do_typalise = typalise;
907
908   graph_infos = pmap_create ();
909
910   ecg_fill_calls ();
911   ecg_fill_ctxs ();
912
913   ecg_ecg ();
914 }
915
916 /**
917    Clean up our mess.
918 */
919 void ecg_cleanup ()
920 {
921   int i;
922
923   for (i = 0; i < get_irp_n_irgs (); i++) {
924     ir_graph *graph = get_irp_irg (i);
925
926     graph_info_t *info = pmap_get (graph_infos, graph);
927     call_info_t *cinfo = info->calls;
928
929     while (NULL != cinfo) {
930       cinfo->call = NULL;
931
932       callEd_info_t *ced = cinfo->callEds;
933
934       while (NULL != ced) {
935         callEd_info_t *nced = ced->prev;
936         free (ced);
937         ced->prev = NULL;
938         ced->callEd = NULL;
939         ced = nced;
940       }
941
942       cinfo->callEds = NULL;
943
944       free (cinfo);
945       cinfo = cinfo->prev;
946     }
947
948     free (info);
949     pmap_insert (graph_infos, graph, NULL);
950   }
951
952
953   pmap_destroy (graph_infos);
954
955   /*  paranoia mode */
956   graph_infos = NULL;
957 }
958
959 /**
960    Show what we have found.
961 */
962 void ecg_report ()
963 {
964   int i;
965
966   FILE *dot = fopen ("calls.dot", "w");
967
968   fprintf (dot, "digraph \"calls\" {\n");
969   fprintf (dot, "\tnode [shape = \"record\", style = \"filled\"];\n");
970   fprintf (dot, "\tedge [color = \"black\"];\n");
971   fprintf (dot, "\n");
972   fprintf (dot, "\tsize = \"11, 7\";\n");
973   fprintf (dot, "\trotate = \"90\";\n");
974   fprintf (dot, "\tratio = \"fill\";\n");
975   fprintf (dot, "\trankdir = \"LR\";\n");
976   fprintf (dot, "\n");
977
978   for (i = 0; i < get_irp_n_irgs (); i++) {
979     ir_graph *graph = get_irp_irg (i);
980     graph_info_t *info = (graph_info_t*) pmap_get (graph_infos, graph);
981
982     const char *name = get_irg_entity (graph) ?
983       get_entity_name (get_irg_entity (graph)) : "noEntity";
984
985     const char *oname = get_type_name
986       (get_entity_owner (get_irg_entity (graph)));
987
988     const char *color =
989       (get_entity_stickyness
990        (get_irg_entity (graph)) == stickyness_sticky) ?
991       "red3" : "lightyellow";
992
993     fprintf (dot, "\t/* graph_0x%08x (\"%s\") */\n", (int) graph, name);
994     fprintf (dot,
995              "\tgraph_0x%08x [label=\"%s\\l%s\", color=\"%s\"];\n",
996              (int) graph, oname, name, color);
997     fprintf (dot, "\n");
998
999     call_info_t *cinfo = info->calls;
1000     if (cinfo) {
1001       fprintf (dot, "\t/* now the calls */\n");
1002     } else {
1003       fprintf (dot, "\t/* no calls, nothing to see, move along! */\n");
1004     }
1005
1006     while (NULL != cinfo) {
1007       ir_node *call = cinfo->call;
1008
1009       fprintf (dot, "\t/* call_0x%08x */\n", (int) call);
1010       fprintf (dot, "\tcall_0x%08x [label=\"call\\[%li\\]\\l0x%08x\"];\n",
1011                (int) call, get_irn_node_nr (call), (int) call);
1012       fprintf (dot, "\tgraph_0x%08x -> call_0x%08x;\n",
1013                (int) graph, (int) call);
1014
1015       callEd_info_t *ced = cinfo->callEds;
1016       while (NULL != ced) {
1017         fprintf (dot, "\tcall_0x%08x -> graph_0x%08x;\n",
1018                  (int) call, (int) ced->callEd);
1019         ced = ced->prev;
1020       }
1021       fprintf (dot, "\n");
1022
1023       cinfo = cinfo->prev;
1024     }
1025     fprintf (dot, "\n");
1026
1027     alloc_info_t *ainfo = info->allocs;
1028     if (ainfo) {
1029       fprintf (dot, "\t/* now the allocs */\n");
1030     } else {
1031       fprintf (dot, "\t/* no allocs */\n");
1032     }
1033
1034
1035     while (NULL != ainfo) {
1036       ir_node *alloc = ainfo->alloc;
1037       const char *name = get_type_name (ainfo->tp);
1038       const char *color = "green3";
1039
1040       fprintf (dot, "\talloc_0x%08x [label=\"%s\", color=\"%s\"];\n",
1041                (int) alloc, name, color);
1042       fprintf (dot, "\tgraph_0x%08x -> alloc_0x%08x;\n",
1043                (int) graph, (int) alloc);
1044
1045       ainfo = ainfo->prev;
1046     }
1047   }
1048   fprintf (dot, "}\n");
1049
1050   /*
1051     fprintf (stdout, " max_callEds: %i\n", _max_callEds);
1052     fprintf (stdout, " max_callEds_callR: \"%s\"\n",
1053     get_entity_name (_max_callEds_callR));
1054   */
1055   fclose (dot);
1056 }
1057
1058 /**
1059    Experimental:  Print the ecg
1060 */
1061 void ecg_ecg ()
1062 {
1063   _graphs = 0;
1064   _calls  = 0;
1065
1066   ir_graph *main_graph = get_irp_main_irg ();
1067
1068   /*
1069     memset (spaces, '.', BUF_SIZE);
1070     spaces [BUF_SIZE-1] = '\0';
1071
1072     ecg_ecg_count (main_graph);
1073     fprintf (stdout, "n_graphs: %i\n", _graphs);
1074     fprintf (stdout, "max_depth = %i\n", _max_depth);
1075   */
1076
1077   /* return; */
1078
1079   _graphs = 0;
1080   _calls  = 0;
1081
1082   FILE *dot = fopen ("ecg.dot", "w");
1083
1084   fprintf (dot, "digraph \"ecg\" {\n");
1085   fprintf (dot, "\tnode [shape = \"record\", style = \"filled\"];\n");
1086   fprintf (dot, "\tedge [color = \"black\"];\n");
1087   fprintf (dot, "\n");
1088   fprintf (dot, "\tsize = \"11, 7\";\n");
1089   fprintf (dot, "\trotate = \"90\";\n");
1090   fprintf (dot, "\tratio = \"fill\";\n");
1091   fprintf (dot, "\trankdir = \"LR\";\n");
1092   fprintf (dot, "\n");
1093
1094   /* ir_graph *main_graph = get_irp_main_irg (); */
1095   ecg_ecg_graph (dot, main_graph);
1096
1097   fprintf (dot, "\t/* Grand Total: */\n");
1098   fprintf (dot, "\t/* calls:  %i */\n", (int) _calls);
1099   fprintf (dot, "\t/* graphs: %i */\n", (int) _graphs);
1100   fprintf (dot, "\t/* allocs: %i */\n", (int) _allocs);
1101   fprintf (dot, "\t/* (sales tax not included) */\n");
1102
1103   fprintf (dot, "}\n");
1104
1105   fclose (dot);
1106 }
1107
1108 \f
1109
1110 /*
1111   $Log$
1112   Revision 1.5  2004/11/20 21:20:29  liekweg
1113   Added iterator functions
1114
1115   Revision 1.4  2004/11/18 16:36:37  liekweg
1116   Added unique ids for debugging, added access functions
1117
1118   Revision 1.3  2004/11/04 14:54:44  liekweg
1119   Nicer Colors
1120
1121   Revision 1.2  2004/10/21 11:09:37  liekweg
1122   Moved memwalk stuf into irmemwalk
1123   Moved lset stuff into lset
1124   Moved typalise stuff into typalise
1125
1126   Revision 1.1  2004/10/20 14:59:41  liekweg
1127   Added ana2, added ecg and pto
1128
1129   Revision 1.6  2004/10/18 12:47:19  liekweg
1130   minor fix
1131
1132   Revision 1.5  2004/10/14 11:31:28  liekweg
1133   SHUTUP_GCC
1134
1135   Revision 1.4  2004/10/12 11:02:01  liekweg
1136   wtf?
1137
1138 */