Reuse already fetched values.
[libfirm] / ir / be / ppc32 / ppc32_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 architecture specific firm
23  *         opcodes and the corresponding node constructors for the ppc assembler
24  *         irg.
25  * @author  Moritz Kroll, Jens Mueller
26  * @version $Id$
27  */
28 #include "config.h"
29
30 #include <stdlib.h>
31
32 #include "irprog_t.h"
33 #include "irgraph_t.h"
34 #include "irnode_t.h"
35 #include "irmode_t.h"
36 #include "ircons_t.h"
37 #include "iropt_t.h"
38 #include "irop.h"
39 #include "irvrfy_t.h"
40 #include "irprintf.h"
41 #include "xmalloc.h"
42
43 #include "../bearch.h"
44
45 #include "ppc32_nodes_attr.h"
46 #include "ppc32_new_nodes.h"
47 #include "gen_ppc32_regalloc_if.h"
48
49
50
51 /***********************************************************************************
52  *      _                                   _       _             __
53  *     | |                                 (_)     | |           / _|
54  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
55  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
56  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
57  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
58  *                       | |
59  *                       |_|
60  ***********************************************************************************/
61
62 /**
63  * Dumps the register requirements for either in or out.
64  */
65 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs, int inout) {
66         char *dir = inout ? "out" : "in";
67         int   max = inout ? (int) arch_irn_get_n_outs(n)  : get_irn_arity(n);
68         char  buf[1024];
69         int   i;
70
71         memset(buf, 0, sizeof(buf));
72
73         if (reqs) {
74                 for (i = 0; i < max; i++) {
75                         fprintf(F, "%sreq #%d =", dir, i);
76                         arch_dump_register_req(F, reqs[i], n);
77                         fprintf(F, "\n");
78                 }
79
80                 fprintf(F, "\n");
81         } else {
82                 fprintf(F, "%sreq = N/A\n", dir);
83         }
84 }
85
86
87 /**
88  * Dumper interface for dumping ppc32 nodes in vcg.
89  * @param n        the node to dump
90  * @param F        the output file
91  * @param reason   indicates which kind of information should be dumped
92  * @return 0 on success or != 0 on failure
93  */
94 static int ppc32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
95         ir_mode     *mode = NULL;
96         int          bad  = 0;
97         int          i, n_res, flags;
98         const arch_register_req_t **reqs;
99
100         switch (reason) {
101                 case dump_node_opcode_txt:
102                         fprintf(F, "%s", get_irn_opname(n));
103                         break;
104
105                 case dump_node_mode_txt:
106                         mode = get_irn_mode(n);
107
108                         if (mode) {
109                                 fprintf(F, "[%s]", get_mode_name(mode));
110                         }
111                         else {
112                                 fprintf(F, "[?NOMODE?]");
113                         }
114                         break;
115
116                 case dump_node_nodeattr_txt:
117
118                         /* TODO: dump some attributes which should show up */
119                         /* in node name in dump (e.g. consts or the like)  */
120
121                         break;
122
123                 case dump_node_info_txt:
124                         fprintf(F, "=== ppc attr begin ===\n");
125
126                         /* dump IN requirements */
127                         if (get_irn_arity(n) > 0) {
128                                 reqs = get_ppc32_in_req_all(n);
129                                 dump_reg_req(F, n, reqs, 0);
130                         }
131
132                         n_res = arch_irn_get_n_outs(n);
133                         if (n_res > 0) {
134                                 /* dump OUT requirements */
135                                 reqs = get_ppc32_out_req_all(n);
136                                 dump_reg_req(F, n, reqs, 1);
137
138                                 /* dump assigned registers */
139                                 for (i = 0; i < n_res; i++) {
140                                         const arch_register_t *reg = arch_irn_get_register(n, i);
141
142                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
143                                 }
144                                 fprintf(F, "\n");
145                         }
146
147                         /* dump n_res */
148                         fprintf(F, "n_res = %d\n", n_res);
149
150                         /* dump flags */
151                         fprintf(F, "flags =");
152                         flags = arch_irn_get_flags(n);
153                         if (flags == arch_irn_flags_none) {
154                                 fprintf(F, " none");
155                         }
156                         else {
157                                 if (flags & arch_irn_flags_dont_spill) {
158                                         fprintf(F, " unspillable");
159                                 }
160                                 if (flags & arch_irn_flags_rematerializable) {
161                                         fprintf(F, " remat");
162                                 }
163                                 if (flags & arch_irn_flags_modify_flags) {
164                                         fprintf(F, " modify_flags");
165                                 }
166                         }
167                         fprintf(F, " (%d)\n", flags);
168
169                         /* TODO: dump all additional attributes */
170
171                         fprintf(F, "=== ppc attr end ===\n");
172                         /* end of: case dump_node_info_txt */
173                         break;
174         }
175
176
177         return bad;
178 }
179
180
181
182 /***************************************************************************************************
183  *        _   _                   _       __        _                    _   _               _
184  *       | | | |                 | |     / /       | |                  | | | |             | |
185  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
186  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
187  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
188  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
189  *                                        __/ |
190  *                                       |___/
191  ***************************************************************************************************/
192
193 ppc32_attr_t *get_ppc32_attr(ir_node *node) {
194         assert(is_ppc32_irn(node) && "need ppc node to get attributes");
195         return (ppc32_attr_t *)get_irn_generic_attr(node);
196 }
197
198 const ppc32_attr_t *get_ppc32_attr_const(const ir_node *node) {
199         assert(is_ppc32_irn(node) && "need ppc node to get attributes");
200         return (const ppc32_attr_t *)get_irn_generic_attr_const(node);
201 }
202
203
204
205 /**
206  * Returns the argument register requirements of a ppc node.
207  */
208 const arch_register_req_t **get_ppc32_in_req_all(const ir_node *node) {
209         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
210         return attr->in_req;
211 }
212
213 /**
214  * Returns the result register requirements of an ppc node.
215  */
216 const arch_register_req_t **get_ppc32_out_req_all(const ir_node *node) {
217         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
218         return attr->out_req;
219 }
220
221 /**
222  * Returns the argument register requirement at position pos of an ppc node.
223  */
224 const arch_register_req_t *get_ppc32_in_req(const ir_node *node, int pos) {
225         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
226         return attr->in_req[pos];
227 }
228
229 /**
230  * Returns the result register requirement at position pos of an ppc node.
231  */
232 const arch_register_req_t *get_ppc32_out_req(const ir_node *node, int pos) {
233         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
234         return attr->out_req[pos];
235 }
236
237 /**
238  * Sets the OUT register requirements at position pos.
239  */
240 void set_ppc32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
241         ppc32_attr_t *attr   = get_ppc32_attr(node);
242         attr->out_req[pos] = req;
243 }
244
245 /**
246  * Sets the IN register requirements at position pos.
247  */
248 void set_ppc32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
249         ppc32_attr_t *attr  = get_ppc32_attr(node);
250         attr->in_req[pos] = req;
251 }
252
253 /**
254  * Sets the type of the constant (if any)
255  * May be either iro_Const or iro_SymConst
256  */
257 /* void set_ppc32_type(const ir_node *node, opcode type) {
258         ppc32_attr_t *attr = get_ppc32_attr(node);
259         attr->type      = type;
260 }  */
261
262 /**
263  * Returns the type of the content (if any)
264  */
265 ppc32_attr_content_type get_ppc32_type(const ir_node *node) {
266         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
267         return attr->content_type;
268 }
269
270 /**
271  * Sets a tarval type content (also updating the content_type)
272  */
273 void set_ppc32_constant_tarval(ir_node *node, tarval *const_tarval) {
274         ppc32_attr_t *attr = get_ppc32_attr(node);
275         attr->content_type = ppc32_ac_Const;
276         attr->data.constant_tarval = const_tarval;
277 }
278
279 /**
280  * Returns a tarval type constant
281  */
282 tarval *get_ppc32_constant_tarval(const ir_node *node) {
283         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
284         return attr->data.constant_tarval;
285 }
286
287 /**
288  * Sets an ident type constant (also updating the content_type)
289  */
290 void set_ppc32_symconst_ident(ir_node *node, ident *symconst_ident) {
291         ppc32_attr_t *attr = get_ppc32_attr(node);
292         attr->content_type = ppc32_ac_SymConst;
293         attr->data.symconst_ident = symconst_ident;
294 }
295
296 /**
297  * Returns an ident type constant
298  */
299 ident *get_ppc32_symconst_ident(const ir_node *node) {
300         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
301         return attr->data.symconst_ident;
302 }
303
304
305 /**
306  * Sets an entity (also updating the content_type)
307  */
308 void set_ppc32_frame_entity(ir_node *node, ir_entity *ent) {
309         ppc32_attr_t *attr = get_ppc32_attr(node);
310         attr->content_type = ppc32_ac_FrameEntity;
311         attr->data.frame_entity = ent;
312 }
313
314 /**
315  * Returns an entity
316  */
317 ir_entity *get_ppc32_frame_entity(const ir_node *node) {
318         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
319         return attr->data.frame_entity;
320 }
321
322 /**
323  * Sets a Rlwimi const (also updating the content_type)
324  */
325 void set_ppc32_rlwimi_const(ir_node *node, unsigned shift, unsigned maskA, unsigned maskB) {
326         ppc32_attr_t *attr = get_ppc32_attr(node);
327         attr->content_type = ppc32_ac_RlwimiConst;
328         attr->data.rlwimi_const.shift = shift;
329         attr->data.rlwimi_const.maskA = maskA;
330         attr->data.rlwimi_const.maskB = maskB;
331 }
332
333 /**
334  * Returns the rlwimi const structure
335  */
336 const rlwimi_const_t *get_ppc32_rlwimi_const(const ir_node *node) {
337         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
338         return &attr->data.rlwimi_const;
339 }
340
341 /**
342  * Sets a Proj number (also updating the content_type)
343  */
344 void set_ppc32_proj_nr(ir_node *node, int proj_nr) {
345         ppc32_attr_t *attr = get_ppc32_attr(node);
346         attr->content_type = ppc32_ac_BranchProj;
347         attr->data.proj_nr = proj_nr;
348 }
349
350 /**
351  * Returns the proj number
352  */
353 int get_ppc32_proj_nr(const ir_node *node) {
354         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
355         return attr->data.proj_nr;
356 }
357
358 /**
359  * Sets an offset for a memory access (also updating the content_type)
360  */
361 void set_ppc32_offset(ir_node *node, int offset) {
362         ppc32_attr_t *attr = get_ppc32_attr(node);
363         attr->content_type = ppc32_ac_Offset;
364         attr->data.offset  = offset;
365 }
366
367 /**
368  * Returns the offset
369  */
370 int get_ppc32_offset(const ir_node *node) {
371         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
372         return attr->data.offset;
373 }
374
375 /**
376  * Sets the offset mode (ppc32_ao_None, ppc32_ao_Lo16, ppc32_ao_Hi16 or ppc32_ao_Ha16)
377  */
378 void set_ppc32_offset_mode(ir_node *node, ppc32_attr_offset_mode mode) {
379         ppc32_attr_t *attr = get_ppc32_attr(node);
380         attr->offset_mode = mode;
381 }
382
383 /**
384  * Returns the offset mode
385  */
386 ppc32_attr_offset_mode get_ppc32_offset_mode(const ir_node *node) {
387         const ppc32_attr_t *attr = get_ppc32_attr_const(node);
388         return attr->offset_mode;
389 }
390
391
392 /**
393  * Initializes ppc specific node attributes
394  */
395 void init_ppc32_attributes(ir_node *node, int flags,
396                                                  const arch_register_req_t **in_reqs, const arch_register_req_t **out_reqs,
397                                                  const be_execution_unit_t ***execution_units,
398                                                  int n_res) {
399         ir_graph       *irg  = get_irn_irg(node);
400         struct obstack *obst = get_irg_obstack(irg);
401         ppc32_attr_t   *attr = get_ppc32_attr(node);
402         backend_info_t  *info;
403         (void) execution_units;
404
405         arch_irn_set_flags(node, flags);
406         attr->in_req  = in_reqs;
407         attr->out_req = out_reqs;
408
409         attr->content_type = ppc32_ac_None;
410         attr->offset_mode  = ppc32_ao_Illegal;
411         attr->data.empty   = NULL;
412
413         info            = be_get_info(node);
414         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
415         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
416 }
417
418
419 /***************************************************************************************
420  *                  _                            _                   _
421  *                 | |                          | |                 | |
422  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
423  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
424  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
425  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
426  *
427  ***************************************************************************************/
428
429 /* Include the generated constructor functions */
430 #include "gen_ppc32_new_nodes.c.inl"