cleanup sparc attribute getters+setters
[libfirm] / ir / be / sparc / sparc_new_nodes.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   This file implements the creation of the achitecture specific firm
23  *          opcodes and the coresponding node constructors for the sparc
24  *          assembler irg.
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "irprog_t.h"
32 #include "irgraph_t.h"
33 #include "irnode_t.h"
34 #include "irmode_t.h"
35 #include "ircons_t.h"
36 #include "iropt_t.h"
37 #include "irop.h"
38 #include "irvrfy_t.h"
39 #include "irprintf.h"
40 #include "xmalloc.h"
41
42 #include "../bearch.h"
43
44 #include "sparc_nodes_attr.h"
45 #include "sparc_new_nodes.h"
46 #include "gen_sparc_regalloc_if.h"
47
48 static bool has_symconst_attr(const ir_node *node)
49 {
50         return is_sparc_SymConst(node) || is_sparc_FrameAddr(node);
51 }
52
53 static bool has_load_store_attr(const ir_node *node)
54 {
55         return is_sparc_Ld(node) || is_sparc_St(node);
56 }
57
58 static bool has_cmp_attr(const ir_node *node)
59 {
60         return is_sparc_Cmp(node) || is_sparc_Tst(node);
61 }
62
63 static bool has_jmp_cond_attr(const ir_node *node)
64 {
65         return is_sparc_BXX(node);
66 }
67
68 static bool has_jmp_switch_attr(const ir_node *node)
69 {
70         return is_sparc_SwitchJmp(node);
71 }
72
73 static bool has_save_attr(const ir_node *node)
74 {
75         return is_sparc_Save(node);
76 }
77
78 /**
79  * Dumper interface for dumping sparc nodes in vcg.
80  * @param F        the output file
81  * @param n        the node to dump
82  * @param reason   indicates which kind of information should be dumped
83  */
84 static void sparc_dump_node(FILE *F, ir_node *n, dump_reason_t reason)
85 {
86         switch (reason) {
87         case dump_node_opcode_txt:
88                 fprintf(F, "%s", get_irn_opname(n));
89                 break;
90
91         case dump_node_mode_txt:
92                 break;
93
94         case dump_node_info_txt:
95                 arch_dump_reqs_and_registers(F, n);
96                 break;
97
98         case dump_node_nodeattr_txt:
99                 if (has_symconst_attr(n)) {
100                         const sparc_symconst_attr_t *attr = get_sparc_symconst_attr_const(n);
101                         fprintf(F, "fp_offset: 0x%X\n", attr->fp_offset);
102                 }
103                 if (has_load_store_attr(n)) {
104                         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(n);
105                         fprintf(F, "offset: 0x%lX\n", attr->offset);
106                         fprintf(F, "is_frame_entity: %s\n", attr->is_frame_entity == true ? "true" : "false");
107                 }
108                 break;
109         }
110 }
111
112 static void sparc_set_attr_imm(ir_node *res, int immediate_value)
113 {
114         sparc_attr_t *attr = get_irn_generic_attr(res);
115         attr->immediate_value = immediate_value;
116 }
117
118 void set_sparc_jmp_cond_proj_num(ir_node *node, int proj_num)
119 {
120         sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr(node);
121         attr->proj_num = proj_num;
122 }
123
124 void set_sparc_jmp_switch_n_projs(ir_node *node, int n_projs)
125 {
126         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
127         attr->n_projs = n_projs;
128 }
129
130 void set_sparc_jmp_switch_default_proj_num(ir_node *node, long def_proj_num)
131 {
132         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
133         attr->default_proj_num = def_proj_num;
134 }
135
136
137
138 int get_sparc_jmp_cond_proj_num(const ir_node *node)
139 {
140         const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
141         return attr->proj_num;
142 }
143
144 int get_sparc_jmp_switch_n_projs(const ir_node *node)
145 {
146         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
147         return attr->n_projs;
148 }
149
150 long get_sparc_jmp_switch_default_proj_num(const ir_node *node)
151 {
152         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
153         return attr->default_proj_num;
154 }
155
156 sparc_attr_t *get_sparc_attr(ir_node *node)
157 {
158         assert(is_sparc_irn(node));
159         return (sparc_attr_t*) get_irn_generic_attr(node);
160 }
161
162 const sparc_attr_t *get_sparc_attr_const(const ir_node *node)
163 {
164         assert(is_sparc_irn(node));
165         return (const sparc_attr_t*) get_irn_generic_attr_const(node);
166 }
167
168 sparc_load_store_attr_t *get_sparc_load_store_attr(ir_node *node)
169 {
170         assert(has_load_store_attr(node));
171         return (sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
172 }
173
174 const sparc_load_store_attr_t *get_sparc_load_store_attr_const(const ir_node *node)
175 {
176         assert(has_load_store_attr(node));
177         return (const sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
178 }
179
180 sparc_symconst_attr_t *get_sparc_symconst_attr(ir_node *node)
181 {
182         assert(has_symconst_attr(node));
183         return (sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
184 }
185
186 const sparc_symconst_attr_t *get_sparc_symconst_attr_const(const ir_node *node)
187 {
188         assert(has_symconst_attr(node));
189         return (const sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
190 }
191
192 sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr(ir_node *node)
193 {
194         assert(has_jmp_cond_attr(node));
195         return (sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
196 }
197
198 const sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr_const(const ir_node *node)
199 {
200         assert(has_jmp_cond_attr(node));
201         return (const sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
202 }
203
204 sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr(ir_node *node)
205 {
206         assert(has_jmp_switch_attr(node));
207         return (sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
208 }
209
210 const sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr_const(const ir_node *node)
211 {
212         assert(has_jmp_switch_attr(node));
213         return (const sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
214 }
215
216 sparc_cmp_attr_t *get_sparc_cmp_attr(ir_node *node)
217 {
218         assert(has_cmp_attr(node));
219         return (sparc_cmp_attr_t*) get_irn_generic_attr_const(node);
220 }
221
222 const sparc_cmp_attr_t *get_sparc_cmp_attr_const(const ir_node *node)
223 {
224         assert(has_cmp_attr(node));
225         return (const sparc_cmp_attr_t*) get_irn_generic_attr_const(node);
226 }
227
228 sparc_save_attr_t *get_sparc_save_attr(ir_node *node)
229 {
230         assert(has_save_attr(node));
231         return (sparc_save_attr_t*) get_irn_generic_attr_const(node);
232 }
233
234 const sparc_save_attr_t *get_sparc_save_attr_const(const ir_node *node)
235 {
236         assert(has_save_attr(node));
237         return (const sparc_save_attr_t*) get_irn_generic_attr_const(node);
238 }
239
240 /**
241  * Returns the argument register requirements of a sparc node.
242  */
243 const arch_register_req_t **get_sparc_in_req_all(const ir_node *node)
244 {
245         const sparc_attr_t *attr = get_sparc_attr_const(node);
246         return attr->in_req;
247 }
248
249 /**
250  * Returns the argument register requirement at position pos of an sparc node.
251  */
252 const arch_register_req_t *get_sparc_in_req(const ir_node *node, int pos)
253 {
254         const sparc_attr_t *attr = get_sparc_attr_const(node);
255         return attr->in_req[pos];
256 }
257
258 /**
259  * Sets the IN register requirements at position pos.
260  */
261 void set_sparc_req_in(ir_node *node, const arch_register_req_t *req, int pos)
262 {
263         sparc_attr_t *attr  = get_sparc_attr(node);
264         attr->in_req[pos] = req;
265 }
266
267 /**
268  * Initializes the nodes attributes.
269  */
270 static void init_sparc_attributes(ir_node *node, arch_irn_flags_t flags,
271                                   const arch_register_req_t **in_reqs,
272                                   const be_execution_unit_t ***execution_units,
273                                   int n_res)
274 {
275         ir_graph        *irg  = get_irn_irg(node);
276         struct obstack  *obst = get_irg_obstack(irg);
277         sparc_attr_t *attr = get_sparc_attr(node);
278         backend_info_t  *info;
279         (void) execution_units;
280
281         arch_irn_set_flags(node, flags);
282         attr->in_req  = in_reqs;
283         attr->is_load_store = false;
284
285         info            = be_get_info(node);
286         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
287         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
288 }
289
290 /* CUSTOM ATTRIBUTE INIT FUNCTIONS */
291 static void init_sparc_load_store_attributes(ir_node *res, ir_mode *ls_mode,
292                                                                                         ir_entity *entity,
293                                                                                         int entity_sign, long offset,
294                                                                                         bool is_frame_entity)
295 {
296         sparc_load_store_attr_t *attr = get_sparc_load_store_attr(res);
297         attr->load_store_mode    = ls_mode;
298         attr->entity             = entity;
299         attr->entity_sign        = entity_sign;
300         attr->is_frame_entity    = is_frame_entity;
301         attr->offset             = offset;
302         attr->base.is_load_store = true;
303 }
304
305 static void init_sparc_cmp_attr(ir_node *res, bool ins_permuted, bool is_unsigned)
306 {
307         sparc_cmp_attr_t *attr = get_sparc_cmp_attr(res);
308         attr->ins_permuted = ins_permuted;
309         attr->is_unsigned  = is_unsigned;
310 }
311
312 static void init_sparc_symconst_attributes(ir_node *res, ir_entity *entity)
313 {
314         sparc_symconst_attr_t *attr = get_sparc_symconst_attr(res);
315         attr->entity    = entity;
316         attr->fp_offset = 0;
317 }
318
319 static void init_sparc_save_attr(ir_node *res, int initial_stacksize)
320 {
321         sparc_save_attr_t *attr = get_sparc_save_attr(res);
322         attr->initial_stacksize = initial_stacksize;
323 }
324
325 /**
326  * copies sparc attributes of  node
327  */
328 static void sparc_copy_attr(ir_graph *irg, const ir_node *old_node,
329                             ir_node *new_node)
330 {
331         struct obstack     *obst    = get_irg_obstack(irg);
332         const sparc_attr_t *attr_old = get_sparc_attr_const(old_node);
333         sparc_attr_t       *attr_new = get_sparc_attr(new_node);
334         backend_info_t     *old_info = be_get_info(old_node);
335         backend_info_t     *new_info = be_get_info(new_node);
336
337         /* copy the attributes */
338         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
339         /* copy out flags */
340         new_info->out_infos =
341                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
342 }
343
344 /**
345  * compare some node's attributes
346  */
347 static int cmp_attr_sparc(ir_node *a, ir_node *b)
348 {
349         const sparc_attr_t *attr_a = get_sparc_attr_const(a);
350         const sparc_attr_t *attr_b = get_sparc_attr_const(b);
351
352         return attr_a->immediate_value != attr_b->immediate_value
353                         || attr_a->is_load_store != attr_b->is_load_store;
354 }
355
356
357 /* CUSTOM ATTRIBUTE CMP FUNCTIONS */
358 static int cmp_attr_sparc_load_store(ir_node *a, ir_node *b)
359 {
360         const sparc_load_store_attr_t *attr_a = get_sparc_load_store_attr_const(a);
361         const sparc_load_store_attr_t *attr_b = get_sparc_load_store_attr_const(b);
362
363         if (cmp_attr_sparc(a, b))
364                         return 1;
365
366         return attr_a->entity != attr_b->entity
367                         || attr_a->entity_sign != attr_b->entity_sign
368                         || attr_a->is_frame_entity != attr_b->is_frame_entity
369                         || attr_a->load_store_mode != attr_b->load_store_mode
370                         || attr_a->offset != attr_b->offset;
371 }
372
373 static int cmp_attr_sparc_symconst(ir_node *a, ir_node *b)
374 {
375         const sparc_symconst_attr_t *attr_a = get_sparc_symconst_attr_const(a);
376         const sparc_symconst_attr_t *attr_b = get_sparc_symconst_attr_const(b);
377
378         if (cmp_attr_sparc(a, b))
379                         return 1;
380
381         return attr_a->entity != attr_b->entity
382                         || attr_a->fp_offset != attr_b->fp_offset;
383 }
384
385 static int cmp_attr_sparc_jmp_cond(ir_node *a, ir_node *b)
386 {
387         const sparc_jmp_cond_attr_t *attr_a = get_sparc_jmp_cond_attr_const(a);
388         const sparc_jmp_cond_attr_t *attr_b = get_sparc_jmp_cond_attr_const(b);
389
390         if (cmp_attr_sparc(a, b))
391                         return 1;
392
393         return attr_a->proj_num != attr_b->proj_num;
394 }
395
396 static int cmp_attr_sparc_jmp_switch(ir_node *a, ir_node *b)
397 {
398         const sparc_jmp_switch_attr_t *attr_a = get_sparc_jmp_switch_attr_const(a);
399         const sparc_jmp_switch_attr_t *attr_b = get_sparc_jmp_switch_attr_const(b);
400
401         if (cmp_attr_sparc(a, b))
402                         return 1;
403
404         return attr_a->default_proj_num != attr_b->default_proj_num
405                         || attr_a->n_projs != attr_b->n_projs;
406 }
407
408 static int cmp_attr_sparc_cmp(ir_node *a, ir_node *b)
409 {
410         const sparc_cmp_attr_t *attr_a = get_sparc_cmp_attr_const(a);
411         const sparc_cmp_attr_t *attr_b = get_sparc_cmp_attr_const(b);
412
413         if (cmp_attr_sparc(a, b))
414                         return 1;
415
416         return attr_a->ins_permuted != attr_b->ins_permuted
417                         || attr_a->is_unsigned != attr_b->is_unsigned;
418 }
419
420 static int cmp_attr_sparc_save(ir_node *a, ir_node *b)
421 {
422         const sparc_save_attr_t *attr_a = get_sparc_save_attr_const(a);
423         const sparc_save_attr_t *attr_b = get_sparc_save_attr_const(b);
424
425         if (cmp_attr_sparc(a, b))
426                         return 1;
427
428         return attr_a->initial_stacksize != attr_b->initial_stacksize;
429 }
430
431 /* Include the generated constructor functions */
432 #include "gen_sparc_new_nodes.c.inl"