do not use current_ir_graph in irdump.c
[libfirm] / ir / ir / irop.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   Representation of opcode of intermediate operation.
23  * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  */
25 #include "config.h"
26
27 #include <string.h>
28
29 #include "irop_t.h"
30 #include "irnode_t.h"
31 #include "irhooks.h"
32 #include "irbackedge_t.h"
33
34 #include "iropt_t.h"
35 #include "irverify_t.h"
36 #include "reassoc_t.h"
37
38 #include "xmalloc.h"
39 #include "benode.h"
40
41 static ir_op **opcodes;
42 /** the available next opcode */
43 static unsigned next_iro = iro_MaxOpcode;
44
45 void default_copy_attr(ir_graph *irg, const ir_node *old_node,
46                        ir_node *new_node)
47 {
48         unsigned size = firm_add_node_size;
49         (void) irg;
50
51         assert(get_irn_op(old_node) == get_irn_op(new_node));
52         memcpy(&new_node->attr, &old_node->attr, get_op_attr_size(get_irn_op(old_node)));
53
54         if (size > 0) {
55                 /* copy additional node data */
56                 memcpy(get_irn_data(new_node, void, size), get_irn_data(old_node, void, size), size);
57         }
58 }
59
60 /**
61  * Copies all Call attributes stored in the old node to the new node.
62  */
63 static void call_copy_attr(ir_graph *irg, const ir_node *old_node,
64                            ir_node *new_node)
65 {
66         default_copy_attr(irg, old_node, new_node);
67         remove_Call_callee_arr(new_node);
68 }
69
70 /**
71  * Copies all Block attributes stored in the old node to the new node.
72  */
73 static void block_copy_attr(ir_graph *irg, const ir_node *old_node,
74                             ir_node *new_node)
75 {
76         default_copy_attr(irg, old_node, new_node);
77         new_node->attr.block.irg.irg       = irg;
78         new_node->attr.block.phis          = NULL;
79         new_node->attr.block.backedge      = new_backedge_arr(irg->obst, get_irn_arity(new_node));
80         new_node->attr.block.block_visited = 0;
81         memset(&new_node->attr.block.dom, 0, sizeof(new_node->attr.block.dom));
82         memset(&new_node->attr.block.pdom, 0, sizeof(new_node->attr.block.pdom));
83         /* It should be safe to copy the entity here, as it has no back-link to the old block.
84          * It serves just as a label number, so copying a labeled block results in an exact copy.
85          * This is at least what we need for DCE to work. */
86         new_node->attr.block.entity         = old_node->attr.block.entity;
87         new_node->attr.block.phis           = NULL;
88         INIT_LIST_HEAD(&new_node->attr.block.succ_head);
89 }
90
91 /**
92  * Copies all phi attributes stored in old node to the new node
93  */
94 static void phi_copy_attr(ir_graph *irg, const ir_node *old_node,
95                           ir_node *new_node)
96 {
97         default_copy_attr(irg, old_node, new_node);
98         new_node->attr.phi.next       = NULL;
99         new_node->attr.phi.u.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
100 }
101
102 /**
103  * Copies all ASM attributes stored in old node to the new node
104  */
105 static void ASM_copy_attr(ir_graph *irg, const ir_node *old_node,
106                           ir_node *new_node)
107 {
108         default_copy_attr(irg, old_node, new_node);
109         new_node->attr.assem.input_constraints  = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.input_constraints);
110         new_node->attr.assem.output_constraints = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.output_constraints);
111         new_node->attr.assem.clobbers = DUP_ARR_D(ident*, irg->obst, old_node->attr.assem.clobbers);
112 }
113
114 static void switch_copy_attr(ir_graph *irg, const ir_node *old_node,
115                              ir_node *new_node)
116 {
117         const ir_switch_table *table = get_Switch_table(old_node);
118         new_node->attr.switcha.table = ir_switch_table_duplicate(irg, table);
119         new_node->attr.switcha.n_outs = old_node->attr.switcha.n_outs;
120 }
121
122 /**
123  * Sets the default copy_attr operation for an ir_ops
124  *
125  * @param code   the opcode for the default operation
126  * @param ops    the operations initialized
127  *
128  * @return
129  *    The operations.
130  */
131 static void firm_set_default_copy_attr(unsigned code, ir_op_ops *ops)
132 {
133         switch (code) {
134         case iro_Call:   ops->copy_attr = call_copy_attr;   break;
135         case iro_Block:  ops->copy_attr = block_copy_attr;  break;
136         case iro_Phi:    ops->copy_attr = phi_copy_attr;    break;
137         case iro_ASM:    ops->copy_attr = ASM_copy_attr;    break;
138         case iro_Switch: ops->copy_attr = switch_copy_attr; break;
139         default:
140                 if (ops->copy_attr == NULL)
141                         ops->copy_attr = default_copy_attr;
142         }
143 }
144
145 /*
146  * Sets the default operation for an ir_ops.
147  */
148 static void set_default_operations(unsigned code, ir_op_ops *ops)
149 {
150         firm_set_default_hash(code, ops);
151         firm_set_default_computed_value(code, ops);
152         firm_set_default_equivalent_node(code, ops);
153         firm_set_default_transform_node(code, ops);
154         firm_set_default_node_cmp_attr(code, ops);
155         firm_set_default_get_type_attr(code, ops);
156         firm_set_default_get_entity_attr(code, ops);
157         firm_set_default_copy_attr(code, ops);
158         firm_set_default_verifier(code, ops);
159         firm_set_default_reassoc(code, ops);
160 }
161
162 ir_op *new_ir_op(unsigned code, const char *name, op_pin_state p,
163                  unsigned flags, op_arity opar, int op_index, size_t attr_size,
164                  const ir_op_ops *ops)
165 {
166         ir_op *res = XMALLOCZ(ir_op);
167
168         res->code      = code;
169         res->name      = new_id_from_chars(name, strlen(name));
170         res->pin_state = p;
171         res->attr_size = attr_size;
172         res->flags     = flags;
173         res->opar      = opar;
174         res->op_index  = op_index;
175         res->tag       = 0;
176
177         if (ops)
178                 res->ops = *ops;
179         else /* no given ops, set all operations to NULL */
180                 memset(&res->ops, 0, sizeof(res->ops));
181
182         set_default_operations(code, &res->ops);
183
184         {
185                 size_t len = ARR_LEN(opcodes);
186                 if ((size_t)code >= len) {
187                         ARR_RESIZE(ir_op*, opcodes, (size_t)code+1);
188                         memset(&opcodes[len], 0, (code-len+1) * sizeof(opcodes[0]));
189                 }
190                 if (opcodes[code] != NULL)
191                         panic("opcode registered twice");
192                 opcodes[code] = res;
193         }
194
195         hook_new_ir_op(res);
196         return res;
197 }
198
199 void free_ir_op(ir_op *code)
200 {
201         hook_free_ir_op(code);
202
203         assert(opcodes[code->code] == code);
204         opcodes[code->code] = NULL;
205
206         free(code);
207 }
208
209 unsigned ir_get_n_opcodes(void)
210 {
211         return ARR_LEN(opcodes);
212 }
213
214 ir_op *ir_get_opcode(unsigned code)
215 {
216         assert((size_t)code < ARR_LEN(opcodes));
217         return opcodes[code];
218 }
219
220 void ir_clear_opcodes_generic_func(void)
221 {
222         size_t n = ir_get_n_opcodes();
223         size_t i;
224
225         for (i = 0; i < n; ++i) {
226                 ir_op *op = ir_get_opcode(i);
227                 if (op != NULL)
228                         op->ops.generic = (op_func)NULL;
229         }
230 }
231
232 void ir_op_set_memory_index(ir_op *op, int memory_index)
233 {
234         assert(op->flags & irop_flag_uses_memory);
235         op->memory_index = memory_index;
236 }
237
238 void ir_op_set_fragile_indices(ir_op *op, int pn_x_regular, int pn_x_except)
239 {
240         assert(op->flags & irop_flag_fragile);
241         op->pn_x_regular = pn_x_regular;
242         op->pn_x_except = pn_x_except;
243 }
244
245 const char *get_op_name (const ir_op *op)
246 {
247         return get_id_str(op->name);
248 }
249
250 unsigned (get_op_code)(const ir_op *op)
251 {
252   return get_op_code_(op);
253 }
254
255 ident *(get_op_ident)(const ir_op *op)
256 {
257   return get_op_ident_(op);
258 }
259
260 const char *get_op_pin_state_name(op_pin_state s)
261 {
262         switch (s) {
263 #define XXX(s) case s: return #s
264         XXX(op_pin_state_floats);
265         XXX(op_pin_state_pinned);
266         XXX(op_pin_state_exc_pinned);
267         XXX(op_pin_state_mem_pinned);
268 #undef XXX
269         }
270         return "<none>";
271 }
272
273 op_pin_state (get_op_pinned)(const ir_op *op)
274 {
275         return get_op_pinned_(op);
276 }
277
278 void set_op_pinned(ir_op *op, op_pin_state pinned)
279 {
280         if (op == op_Block || op == op_Phi || is_op_cfopcode(op)) return;
281         op->pin_state = pinned;
282 }
283
284 unsigned get_next_ir_opcode(void)
285 {
286         return next_iro++;
287 }
288
289 unsigned get_next_ir_opcodes(unsigned num)
290 {
291         unsigned base = next_iro;
292         next_iro += num;
293         return base;
294 }
295
296 op_func (get_generic_function_ptr)(const ir_op *op)
297 {
298         return get_generic_function_ptr_(op);
299 }
300
301 void (set_generic_function_ptr)(ir_op *op, op_func func)
302 {
303         set_generic_function_ptr_(op, func);
304 }
305
306 const ir_op_ops *(get_op_ops)(const ir_op *op)
307 {
308         return get_op_ops_(op);
309 }
310
311 irop_flags get_op_flags(const ir_op *op)
312 {
313         return (irop_flags)op->flags;
314 }
315
316 static void generated_init_op(void);
317 static void generated_finish_op(void);
318
319 void firm_init_op(void)
320 {
321         opcodes = NEW_ARR_F(ir_op*, 0);
322         generated_init_op();
323         be_init_op();
324 }
325
326 void firm_finish_op(void)
327 {
328         be_finish_op();
329         generated_finish_op();
330         DEL_ARR_F(opcodes);
331         opcodes = NULL;
332 }
333
334 #include "gen_irop.c.inl"