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