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