automatically generate most getter/setter functions of firm nodes
[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  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "irop_t.h"
31 #include "irnode_t.h"
32 #include "irhooks.h"
33 #include "irbackedge_t.h"
34
35 #include "iropt_t.h"
36 #include "irvrfy_t.h"
37 #include "reassoc_t.h"
38
39 #include "xmalloc.h"
40
41 void be_init_op(void);
42
43 /** the available next opcode */
44 static unsigned next_iro = iro_MaxOpcode;
45
46 /*
47  * Copies all attributes stored in the old node to the new node.
48  * Assumes both have the same opcode and sufficient size.
49  */
50 void default_copy_attr(ir_graph *irg, const ir_node *old_node,
51                        ir_node *new_node)
52 {
53         unsigned size = firm_add_node_size;
54         (void) irg;
55
56         assert(get_irn_op(old_node) == get_irn_op(new_node));
57         memcpy(&new_node->attr, &old_node->attr, get_op_attr_size(get_irn_op(old_node)));
58
59         if (size > 0) {
60                 /* copy additional node data */
61                 memcpy(get_irn_data(new_node, void, size), get_irn_data(old_node, void, size), size);
62         }
63 }
64
65 /**
66  * Copies all Call attributes stored in the old node to the new node.
67  */
68 static void call_copy_attr(ir_graph *irg, const ir_node *old_node,
69                            ir_node *new_node)
70 {
71         default_copy_attr(irg, old_node, new_node);
72         remove_Call_callee_arr(new_node);
73 }
74
75 /**
76  * Copies all Block attributes stored in the old node to the new node.
77  */
78 static void block_copy_attr(ir_graph *irg, const ir_node *old_node,
79                             ir_node *new_node)
80 {
81         default_copy_attr(irg, old_node, new_node);
82         new_node->attr.block.irg.irg       = irg;
83         new_node->attr.block.phis          = NULL;
84         new_node->attr.block.cg_backedge   = NULL;
85         new_node->attr.block.backedge      = new_backedge_arr(irg->obst, get_irn_arity(new_node));
86         new_node->attr.block.block_visited = 0;
87         memset(&new_node->attr.block.dom, 0, sizeof(new_node->attr.block.dom));
88         memset(&new_node->attr.block.pdom, 0, sizeof(new_node->attr.block.pdom));
89         /* TODO: we should probably create a new entity. But we somehow have to
90          * patch the stuff at the same time */
91         new_node->attr.block.entity            = NULL;
92         new_node->attr.block.phis              = NULL;
93         INIT_LIST_HEAD(&new_node->attr.block.succ_head);
94 }
95
96 /**
97  * Copies all phi attributes stored in old node to the new node
98  */
99 static void phi_copy_attr(ir_graph *irg, const ir_node *old_node,
100                           ir_node *new_node)
101 {
102         default_copy_attr(irg, old_node, new_node);
103         new_node->attr.phi.next       = NULL;
104         new_node->attr.phi.u.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
105 }
106
107 /**
108  * Copies all filter attributes stored in old node to the new node
109  */
110 static void filter_copy_attr(ir_graph *irg, const ir_node *old_node,
111                              ir_node *new_node)
112 {
113         default_copy_attr(irg, old_node, new_node);
114         new_node->attr.filter.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
115 }
116
117 /**
118  * Copies all ASM attributes stored in old node to the new node
119  */
120 static void ASM_copy_attr(ir_graph *irg, const ir_node *old_node,
121                           ir_node *new_node)
122 {
123         default_copy_attr(irg, old_node, new_node);
124         new_node->attr.assem.input_constraints  = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.input_constraints);
125         new_node->attr.assem.output_constraints = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.output_constraints);
126         new_node->attr.assem.clobbers = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.clobbers);
127 }
128
129 /**
130  * Sets the default copy_attr operation for an ir_ops
131  *
132  * @param code   the opcode for the default operation
133  * @param ops    the operations initialized
134  *
135  * @return
136  *    The operations.
137  */
138 static ir_op_ops *firm_set_default_copy_attr(ir_opcode code, ir_op_ops *ops)
139 {
140         switch (code) {
141         case iro_Call:
142                 ops->copy_attr = call_copy_attr;
143                 break;
144         case iro_Block:
145                 ops->copy_attr = block_copy_attr;
146                 break;
147         case iro_Phi:
148                 ops->copy_attr = phi_copy_attr;
149                 break;
150         case iro_Filter:
151                 ops->copy_attr = filter_copy_attr;
152                 break;
153         case iro_ASM:
154                 ops->copy_attr = ASM_copy_attr;
155                 break;
156         default:
157                 /* not allowed to be NULL */
158                 if (! ops->copy_attr)
159                         ops->copy_attr = default_copy_attr;
160         }
161         return ops;
162 }  /* firm_set_default_copy_attr */
163
164 /* Creates a new ir operation. */
165 ir_op *new_ir_op(unsigned code, const char *name, op_pin_state p,
166                  unsigned flags, op_arity opar, int op_index, size_t attr_size,
167                  const ir_op_ops *ops)
168 {
169         ir_op *res = XMALLOCZ(ir_op);
170
171         res->code      = code;
172         res->name      = new_id_from_chars(name, strlen(name));
173         res->pin_state = p;
174         res->attr_size = attr_size;
175         res->flags     = flags;
176         res->opar      = opar;
177         res->op_index  = op_index;
178         res->tag       = 0;
179
180         if (ops)
181                 memcpy(&res->ops, ops, sizeof(res->ops));
182         else /* no given ops, set all operations to NULL */
183                 memset(&res->ops, 0, sizeof(res->ops));
184
185         firm_set_default_operations(code, &res->ops);
186         firm_set_default_copy_attr(code, &res->ops);
187         firm_set_default_verifyer(code, &res->ops);
188         firm_set_default_reassoc(code, &res->ops);
189
190         add_irp_opcode(res);
191
192         hook_new_ir_op(res);
193         return res;
194 }  /* new_ir_op */
195
196 void free_ir_op(ir_op *code)
197 {
198         hook_free_ir_op(code);
199
200         remove_irp_opcode(code);
201         free(code);
202 }  /* free_ir_op */
203
204 /* Returns the string for the opcode. */
205 const char *get_op_name (const ir_op *op)
206 {
207         return get_id_str(op->name);
208 }  /* get_op_name */
209
210 unsigned (get_op_code)(const ir_op *op)
211 {
212   return _get_op_code(op);
213 }  /* get_op_code */
214
215 ident *(get_op_ident)(const ir_op *op)
216 {
217   return _get_op_ident(op);
218 }  /* get_op_ident */
219
220 const char *get_op_pin_state_name(op_pin_state s)
221 {
222         switch (s) {
223 #define XXX(s) case s: return #s
224         XXX(op_pin_state_floats);
225         XXX(op_pin_state_pinned);
226         XXX(op_pin_state_exc_pinned);
227         XXX(op_pin_state_mem_pinned);
228 #undef XXX
229         }
230         return "<none>";
231 }  /* get_op_pin_state_name */
232
233 op_pin_state (get_op_pinned)(const ir_op *op)
234 {
235         return _get_op_pinned(op);
236 }  /* get_op_pinned */
237
238 /* Sets op_pin_state_pinned in the opcode.  Setting it to floating has no effect
239    for Phi, Block and control flow nodes. */
240 void set_op_pinned(ir_op *op, op_pin_state pinned)
241 {
242         if (op == op_Block || op == op_Phi || is_op_cfopcode(op)) return;
243         op->pin_state = pinned;
244 }  /* set_op_pinned */
245
246 /* retrieve the next free opcode */
247 unsigned get_next_ir_opcode(void)
248 {
249         return next_iro++;
250 }  /* get_next_ir_opcode */
251
252 /* Returns the next free n IR opcode number, allows to register a bunch of user ops */
253 unsigned get_next_ir_opcodes(unsigned num)
254 {
255         unsigned base = next_iro;
256         next_iro += num;
257         return base;
258 }  /* get_next_ir_opcodes */
259
260 /* Returns the generic function pointer from an ir operation. */
261 op_func (get_generic_function_ptr)(const ir_op *op)
262 {
263         return _get_generic_function_ptr(op);
264 }  /* get_generic_function_ptr */
265
266 /* Store a generic function pointer into an ir operation. */
267 void (set_generic_function_ptr)(ir_op *op, op_func func)
268 {
269         _set_generic_function_ptr(op, func);
270 }  /* set_generic_function_ptr */
271
272 /* Returns the ir_op_ops of an ir_op. */
273 const ir_op_ops *(get_op_ops)(const ir_op *op)
274 {
275         return _get_op_ops(op);
276 }  /* get_op_ops */
277
278 irop_flags get_op_flags(const ir_op *op)
279 {
280         return op->flags;
281 }
282
283 #include "gen_irop.c.inl"