Bugfixes
[libfirm] / ir / ana2 / pto_util.c
1 /* -*- c -*- */
2
3 /*
4    Project:     libFIRM
5    File name:   ir/ana/pto_util.c
6    Purpose:     Utilitites for PTO
7    Author:      Florian
8    Modified by:
9    Created:     Sat Nov 13 19:35:27 CET 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  pto_util: Utilitites for PTO
21 */
22
23 # include "pto_util.h"
24
25 # include "irnode.h"
26 # include "irgwalk.h"
27 # include "xmalloc.h"
28
29 # include "pto_debug.h"
30
31 /* Local Defines: */
32
33 /* Local Data Types: */
34 /* Environment for find_irg_args */
35 typedef struct find_irg_args_env {
36   ir_node **args;
37   ir_node *arg;
38 } find_irg_args_env_t;
39
40
41 /* Local Variables: */
42
43 /* Local Prototypes: */
44
45 /* ===================================================
46    Local Implementation:
47    =================================================== */
48 /* Helper for find_irg_args */
49 static void find_irg_arg (ir_node *node, void *env)
50 {
51   find_irg_args_env_t *arg_env = (find_irg_args_env_t*) env;
52
53   if (iro_Proj == get_irn_opcode (node)) {
54     if (arg_env->arg == get_Proj_pred (node)) {
55       long n = get_Proj_proj (node);
56
57       assert (! arg_env->args [n]);
58
59       arg_env->args [n] = node;
60     }
61   }
62 }
63
64 /* ===================================================
65    Exported Implementation:
66    =================================================== */
67 /* Find the arguments of a graph. For a method that has n args, the
68   result array has 'n+1' entries, the last of which is written NULL. */
69 ir_node **find_irg_args (ir_graph *graph)
70 {
71   type *tp = get_entity_type (get_irg_entity (graph));
72   const int n_args = get_method_n_params (tp);
73   ir_node **args = (ir_node**) xmalloc (sizeof (ir_node*) * (n_args+1));
74   ir_node *arg = get_irg_args (graph);
75   find_irg_args_env_t *arg_env =
76     (find_irg_args_env_t*) xmalloc (sizeof (find_irg_args_env_t));
77
78   arg_env->args = args;
79   arg_env->arg  = arg;
80
81   /* or use get_irg_end ?!? */
82   {
83     ir_graph *save = get_current_ir_graph ();
84     set_current_ir_graph (graph);
85     irg_walk (get_irg_end (graph), find_irg_arg, NULL, arg_env);
86     set_current_ir_graph (save);
87   }
88
89   free (arg_env);
90
91   args [n_args] = NULL;
92
93   return (args);
94 }
95 /* Get the entity of a ptr */
96 entity *get_ptr_ent (ir_node *ptr)
97 {
98   entity *ent = NULL;
99   const opcode ptr_op = get_irn_opcode (ptr);
100   switch (ptr_op) {
101   case (iro_Sel): {
102     ent = get_Sel_entity (ptr);
103   } break;
104
105   case (iro_SymConst): {
106     ent = get_SymConst_entity (ptr);
107   } break;
108
109   default: {
110     fprintf (stderr, "%s: no ent for ptr=%s[%ld]\n",
111              __FUNCTION__,
112              get_op_name (get_irn_op (ptr)),
113              get_irn_node_nr (ptr));
114     assert (0);
115   }
116   }
117
118   return (ent);
119 }
120
121
122
123 \f
124 /*
125   $Log$
126   Revision 1.7  2004/11/24 14:53:56  liekweg
127   Bugfixes
128
129   Revision 1.6  2004/11/18 16:37:07  liekweg
130   rewrite
131
132
133 */