Some more cleanup: Put the return type and other specifiers on the same line as the...
[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(const ir_node *old_node, ir_node *new_node)
51 {
52         unsigned size = firm_add_node_size;
53
54         assert(get_irn_op(old_node) == get_irn_op(new_node));
55         memcpy(&new_node->attr, &old_node->attr, get_op_attr_size(get_irn_op(old_node)));
56
57         if (size > 0) {
58                 /* copy additional node data */
59                 memcpy(get_irn_data(new_node, void, size), get_irn_data(old_node, void, size), size);
60         }
61 }  /* default_copy_attr */
62
63 /**
64  * Copies all Call attributes stored in the old node to the new node.
65  */
66 static void call_copy_attr(const ir_node *old_node, ir_node *new_node)
67 {
68         default_copy_attr(old_node, new_node);
69         remove_Call_callee_arr(new_node);
70 }  /* call_copy_attr */
71
72 /**
73  * Copies all Block attributes stored in the old node to the new node.
74  */
75 static void block_copy_attr(const ir_node *old_node, ir_node *new_node)
76 {
77         ir_graph *irg = current_ir_graph;
78
79         default_copy_attr(old_node, new_node);
80         new_node->attr.block.phis        = NULL;
81         new_node->attr.block.cg_backedge = NULL;
82         new_node->attr.block.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
83         INIT_LIST_HEAD(&new_node->attr.block.succ_head);
84 }  /* block_copy_attr */
85
86 /**
87  * Copies all phi attributes stored in old node to the new node
88  */
89 static void phi_copy_attr(const ir_node *old_node, ir_node *new_node)
90 {
91         ir_graph *irg = current_ir_graph;
92
93         default_copy_attr(old_node, new_node);
94         new_node->attr.phi.next       = NULL;
95         new_node->attr.phi.u.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
96 }
97
98 /**
99  * Copies all filter attributes stored in old node to the new node
100  */
101 static void filter_copy_attr(const ir_node *old_node, ir_node *new_node)
102 {
103         ir_graph *irg = current_ir_graph;
104
105         default_copy_attr(old_node, new_node);
106         new_node->attr.filter.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
107 }
108
109 /**
110  * Copies all ASM attributes stored in old node to the new node
111  */
112 static void ASM_copy_attr(const ir_node *old_node, ir_node *new_node)
113 {
114         ir_graph *irg = current_ir_graph;
115
116         default_copy_attr(old_node, new_node);
117         new_node->attr.assem.inputs  = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.inputs);
118         new_node->attr.assem.outputs = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.outputs);
119         new_node->attr.assem.clobber = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.clobber);
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 ir_op_ops *firm_set_default_copy_attr(ir_opcode code, ir_op_ops *ops)
132 {
133         switch (code) {
134         case iro_Call:
135                 ops->copy_attr = call_copy_attr;
136                 break;
137         case iro_Block:
138                 ops->copy_attr = block_copy_attr;
139                 break;
140         case iro_Phi:
141                 ops->copy_attr = phi_copy_attr;
142                 break;
143         case iro_Filter:
144                 ops->copy_attr = filter_copy_attr;
145                 break;
146         case iro_ASM:
147                 ops->copy_attr = ASM_copy_attr;
148                 break;
149         default:
150                 /* not allowed to be NULL */
151                 if (! ops->copy_attr)
152                         ops->copy_attr = default_copy_attr;
153         }
154         return ops;
155 }  /* firm_set_default_copy_attr */
156
157 /* Creates a new ir operation. */
158 ir_op *new_ir_op(unsigned code, const char *name, op_pin_state p,
159                  unsigned flags, op_arity opar, int op_index, size_t attr_size,
160                  const ir_op_ops *ops)
161 {
162         ir_op *res = XMALLOCZ(ir_op);
163
164         res->code      = code;
165         res->name      = new_id_from_chars(name, strlen(name));
166         res->pin_state = p;
167         res->attr_size = attr_size;
168         res->flags     = flags;
169         res->opar      = opar;
170         res->op_index  = op_index;
171         res->tag       = 0;
172
173         if (ops)
174                 memcpy(&res->ops, ops, sizeof(res->ops));
175         else /* no given ops, set all operations to NULL */
176                 memset(&res->ops, 0, sizeof(res->ops));
177
178         firm_set_default_operations(code, &res->ops);
179         firm_set_default_copy_attr(code, &res->ops);
180         firm_set_default_verifyer(code, &res->ops);
181         firm_set_default_reassoc(code, &res->ops);
182
183         add_irp_opcode(res);
184
185         hook_new_ir_op(res);
186         return res;
187 }  /* new_ir_op */
188
189 void free_ir_op(ir_op *code)
190 {
191         hook_free_ir_op(code);
192
193         remove_irp_opcode(code);
194         free(code);
195 }  /* free_ir_op */
196
197 /* Returns the string for the opcode. */
198 const char *get_op_name (const ir_op *op)
199 {
200         return get_id_str(op->name);
201 }  /* get_op_name */
202
203 unsigned (get_op_code)(const ir_op *op)
204 {
205   return _get_op_code(op);
206 }  /* get_op_code */
207
208 ident *(get_op_ident)(const ir_op *op)
209 {
210   return _get_op_ident(op);
211 }  /* get_op_ident */
212
213 const char *get_op_pin_state_name(op_pin_state s)
214 {
215         switch (s) {
216 #define XXX(s) case s: return #s
217         XXX(op_pin_state_floats);
218         XXX(op_pin_state_pinned);
219         XXX(op_pin_state_exc_pinned);
220         XXX(op_pin_state_mem_pinned);
221 #undef XXX
222         }
223         return "<none>";
224 }  /* get_op_pin_state_name */
225
226 op_pin_state (get_op_pinned)(const ir_op *op)
227 {
228         return _get_op_pinned(op);
229 }  /* get_op_pinned */
230
231 /* Sets op_pin_state_pinned in the opcode.  Setting it to floating has no effect
232    for Phi, Block and control flow nodes. */
233 void set_op_pinned(ir_op *op, op_pin_state pinned)
234 {
235         if (op == op_Block || op == op_Phi || is_op_cfopcode(op)) return;
236         op->pin_state = pinned;
237 }  /* set_op_pinned */
238
239 /* retrieve the next free opcode */
240 unsigned get_next_ir_opcode(void)
241 {
242         return next_iro++;
243 }  /* get_next_ir_opcode */
244
245 /* Returns the next free n IR opcode number, allows to register a bunch of user ops */
246 unsigned get_next_ir_opcodes(unsigned num)
247 {
248         unsigned base = next_iro;
249         next_iro += num;
250         return base;
251 }  /* get_next_ir_opcodes */
252
253 /* Returns the generic function pointer from an ir operation. */
254 op_func (get_generic_function_ptr)(const ir_op *op)
255 {
256         return _get_generic_function_ptr(op);
257 }  /* get_generic_function_ptr */
258
259 /* Store a generic function pointer into an ir operation. */
260 void (set_generic_function_ptr)(ir_op *op, op_func func)
261 {
262         _set_generic_function_ptr(op, func);
263 }  /* set_generic_function_ptr */
264
265 /* Returns the ir_op_ops of an ir_op. */
266 const ir_op_ops *(get_op_ops)(const ir_op *op)
267 {
268         return _get_op_ops(op);
269 }  /* get_op_ops */
270
271 irop_flags get_op_flags(const ir_op *op)
272 {
273         return op->flags;
274 }
275
276 #include "gen_irop.c.inl"