2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief This file implements the creation of the architecture specific firm
23 * opcodes and the corresponding node constructors for the arm
25 * @author Oliver Richter, Tobias Gneist
35 #include "irgraph_t.h"
41 #include "firm_common_t.h"
46 #include "../bearch_t.h"
48 #include "arm_nodes_attr.h"
49 #include "arm_new_nodes.h"
50 #include "gen_arm_regalloc_if_t.h"
53 #include "bearch_arm_t.h"
56 * Returns the shift modifier string.
58 const char *arm_shf_mod_name(arm_shift_modifier mod) {
59 static const char *names[] = { NULL, NULL, "asr", "lsl", "lsr", "ror", "rrx" };
63 /***********************************************************************************
66 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
67 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
68 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
69 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
72 ***********************************************************************************/
75 * Dumps the register requirements for either in or out.
77 static void dump_reg_req(FILE *F, const ir_node *node,
78 const arch_register_req_t **reqs, int inout) {
79 char *dir = inout ? "out" : "in";
80 int max = inout ? get_arm_n_res(node) : get_irn_arity(node);
84 memset(buf, 0, sizeof(buf));
87 for (i = 0; i < max; i++) {
88 fprintf(F, "%sreq #%d =", dir, i);
90 if (reqs[i]->type == arch_register_req_type_none) {
94 if (reqs[i]->type & arch_register_req_type_normal) {
95 fprintf(F, " %s", reqs[i]->cls->name);
98 if (reqs[i]->type & arch_register_req_type_limited) {
100 arch_register_req_format(buf, sizeof(buf), reqs[i], node));
103 if (reqs[i]->type & arch_register_req_type_should_be_same) {
104 ir_fprintf(F, " same as %+F", get_irn_n(node, reqs[i]->other_same));
107 if (reqs[i]->type & arch_register_req_type_should_be_different) {
108 ir_fprintf(F, " different from %+F", get_irn_n(node, reqs[i]->other_different));
116 fprintf(F, "%sreq = N/A\n", dir);
121 * Dumper interface for dumping arm nodes in vcg.
122 * @param n the node to dump
123 * @param F the output file
124 * @param reason indicates which kind of information should be dumped
125 * @return 0 on success or != 0 on failure
127 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
128 ir_mode *mode = NULL;
131 arm_attr_t *attr = get_arm_attr(n);
132 const arch_register_req_t **reqs;
133 const arch_register_t **slots;
134 arm_shift_modifier mod;
137 case dump_node_opcode_txt:
138 fprintf(F, "%s", get_irn_opname(n));
141 case dump_node_mode_txt:
142 mode = get_irn_mode(n);
145 fprintf(F, "[%s]", get_mode_name(mode));
148 fprintf(F, "[?NOMODE?]");
152 case dump_node_nodeattr_txt:
153 mod = ARM_GET_SHF_MOD(attr);
154 if (ARM_HAS_SHIFT(mod)) {
155 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), get_tarval_long(attr->value));
157 else if (mod == ARM_SHF_IMM) {
159 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->value));
163 case dump_node_info_txt:
164 fprintf(F, "=== arm attr begin ===\n");
166 /* dump IN requirements */
167 if (get_irn_arity(n) > 0) {
168 reqs = get_arm_in_req_all(n);
169 dump_reg_req(F, n, reqs, 0);
172 /* dump OUT requirements */
173 if (attr->n_res > 0) {
174 reqs = get_arm_out_req_all(n);
175 dump_reg_req(F, n, reqs, 1);
178 /* dump assigned registers */
179 slots = get_arm_slots(n);
180 if (slots && attr->n_res > 0) {
181 for (i = 0; i < attr->n_res; i++) {
183 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
186 fprintf(F, "reg #%d = n/a\n", i);
193 fprintf(F, "n_res = %d\n", get_arm_n_res(n));
196 fprintf(F, "flags =");
197 if (attr->flags == arch_irn_flags_none) {
201 if (attr->flags & arch_irn_flags_dont_spill) {
202 fprintf(F, " unspillable");
204 if (attr->flags & arch_irn_flags_rematerializable) {
205 fprintf(F, " remat");
207 if (attr->flags & arch_irn_flags_ignore) {
208 fprintf(F, " ignore");
211 fprintf(F, " (%d)\n", attr->flags);
213 if (get_arm_value(n)) {
214 if (is_arm_CopyB(n)) {
215 fprintf(F, "size = %lu\n", get_tarval_long(get_arm_value(n)));
217 if (mode_is_float(get_irn_mode(n))) {
218 fprintf(F, "float value = (%f)\n", (double) get_tarval_double(get_arm_value(n)));
219 } else if (mode_is_int(get_irn_mode(n))) {
220 long v = get_tarval_long(get_arm_value(n));
221 fprintf(F, "long value = %ld (0x%08lx)\n", v, v);
222 } else if (mode_is_reference(get_irn_mode(n))) {
223 fprintf(F, "pointer\n");
225 assert(0 && "unbehandelter Typ im const-Knoten");
229 if (get_arm_proj_num(n) >= 0) {
230 fprintf(F, "proj_num = (%d)\n", get_arm_proj_num(n));
232 /* TODO: dump all additional attributes */
234 fprintf(F, "=== arm attr end ===\n");
235 /* end of: case dump_node_info_txt */
243 /***************************************************************************************************
245 * | | | | | | / / | | | | | | | |
246 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
247 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
248 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
249 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
252 ***************************************************************************************************/
255 * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
256 * Firm was made by people hating const :-(
258 arm_attr_t *get_arm_attr(const ir_node *node) {
259 assert(is_arm_irn(node) && "need arm node to get attributes");
260 return (arm_attr_t *)get_irn_generic_attr((ir_node *)node);
264 * Returns the argument register requirements of a arm node.
266 const arch_register_req_t **get_arm_in_req_all(const ir_node *node) {
267 arm_attr_t *attr = get_arm_attr(node);
272 * Returns the result register requirements of an arm node.
274 const arch_register_req_t **get_arm_out_req_all(const ir_node *node) {
275 arm_attr_t *attr = get_arm_attr(node);
276 return attr->out_req;
280 * Returns the argument register requirement at position pos of an arm node.
282 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
283 arm_attr_t *attr = get_arm_attr(node);
284 return attr->in_req[pos];
288 * Returns the result register requirement at position pos of an arm node.
290 const arch_register_req_t *get_arm_out_req(const ir_node *node, int pos) {
291 arm_attr_t *attr = get_arm_attr(node);
292 return attr->out_req[pos];
296 * Sets the OUT register requirements at position pos.
298 void set_arm_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
299 arm_attr_t *attr = get_arm_attr(node);
300 attr->out_req[pos] = req;
304 * Sets the complete OUT requirements of node.
306 void set_arm_req_out_all(ir_node *node, const arch_register_req_t **reqs) {
307 arm_attr_t *attr = get_arm_attr(node);
308 attr->out_req = reqs;
312 * Sets the IN register requirements at position pos.
314 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
315 arm_attr_t *attr = get_arm_attr(node);
316 attr->in_req[pos] = req;
320 * Returns the register flag of an arm node.
322 arch_irn_flags_t get_arm_flags(const ir_node *node) {
323 arm_attr_t *attr = get_arm_attr(node);
328 * Sets the register flag of an arm node.
330 void set_arm_flags(const ir_node *node, arch_irn_flags_t flags) {
331 arm_attr_t *attr = get_arm_attr(node);
336 * Returns the result register slots of an arm node.
338 const arch_register_t **get_arm_slots(const ir_node *node) {
339 arm_attr_t *attr = get_arm_attr(node);
344 * Returns the name of the OUT register at position pos.
346 const char *get_arm_out_reg_name(const ir_node *node, int pos) {
347 arm_attr_t *attr = get_arm_attr(node);
349 assert(is_arm_irn(node) && "Not an arm node.");
350 assert(pos < attr->n_res && "Invalid OUT position.");
351 assert(attr->slots[pos] && "No register assigned");
353 return arch_register_get_name(attr->slots[pos]);
357 * Returns the index of the OUT register at position pos within its register class.
359 int get_arm_out_regnr(const ir_node *node, int pos) {
360 arm_attr_t *attr = get_arm_attr(node);
362 assert(is_arm_irn(node) && "Not an arm node.");
363 assert(pos < attr->n_res && "Invalid OUT position.");
364 assert(attr->slots[pos] && "No register assigned");
366 return arch_register_get_index(attr->slots[pos]);
370 * Returns the OUT register at position pos.
372 const arch_register_t *get_arm_out_reg(const ir_node *node, int pos) {
373 arm_attr_t *attr = get_arm_attr(node);
375 assert(is_arm_irn(node) && "Not an arm node.");
376 assert(pos < attr->n_res && "Invalid OUT position.");
377 assert(attr->slots[pos] && "No register assigned");
379 return attr->slots[pos];
383 * Sets the number of results.
385 void set_arm_n_res(ir_node *node, int n_res) {
386 arm_attr_t *attr = get_arm_attr(node);
391 * Returns the number of results.
393 int get_arm_n_res(const ir_node *node) {
394 arm_attr_t *attr = get_arm_attr(node);
398 * Returns the tarvalue
400 tarval *get_arm_value(const ir_node *node) {
401 arm_attr_t *attr = get_arm_attr(node);
408 void set_arm_value(ir_node *node, tarval *tv) {
409 arm_attr_t *attr = get_arm_attr(node);
414 * Returns the proj num
416 int get_arm_proj_num(const ir_node *node) {
417 arm_attr_t *attr = get_arm_attr(node);
418 return attr->proj_num;
424 void set_arm_proj_num(ir_node *node, int proj_num) {
425 arm_attr_t *attr = get_arm_attr(node);
426 attr->proj_num = proj_num;
430 * Returns the SymConst label
432 ident *get_arm_symconst_id(const ir_node *node) {
433 arm_attr_t *attr = get_arm_attr(node);
434 return attr->symconst_id;
438 * Sets the SymConst label
440 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
441 arm_attr_t *attr = get_arm_attr(node);
442 attr->symconst_id = symconst_id;
447 * Returns the number of projs.
449 int get_arm_n_projs(const ir_node *node) {
450 arm_attr_t *attr = get_arm_attr(node);
451 return attr->n_projs;
455 * Sets the number of projs.
457 void set_arm_n_projs(ir_node *node, int n_projs) {
458 arm_attr_t *attr = get_arm_attr(node);
459 attr->n_projs = n_projs;
463 * Returns the default_proj_num.
465 long get_arm_default_proj_num(const ir_node *node) {
466 arm_attr_t *attr = get_arm_attr(node);
467 return attr->default_proj_num;
471 * Sets the default_proj_num.
473 void set_arm_default_proj_num(ir_node *node, long default_proj_num) {
474 arm_attr_t *attr = get_arm_attr(node);
475 attr->default_proj_num = default_proj_num;
479 * Gets the shift modifier attribute.
481 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
482 arm_attr_t *attr = get_arm_attr(node);
483 return ARM_GET_SHF_MOD(attr);
486 /* Set the ARM machine node attributes to default values. */
487 void init_arm_attributes(ir_node *node, int flags, const arch_register_req_t ** in_reqs,
488 const arch_register_req_t ** out_reqs, const be_execution_unit_t ***execution_units,
489 int n_res, unsigned latency) {
490 arm_attr_t *attr = get_arm_attr(node);
491 attr->in_req = in_reqs;
492 attr->out_req = out_reqs;
495 attr->instr_fl = (ARM_COND_AL << 3) | ARM_SHF_NONE;
497 attr->proj_num = -42;
498 attr->symconst_id = NULL;
500 attr->default_proj_num = 0;
502 memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
505 static int arm_comp_condJmp(arm_attr_t *attr_a, arm_attr_t *attr_b) {
510 /***************************************************************************************
513 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
514 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
515 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
516 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
518 ***************************************************************************************/
523 #define BIT(x) (1 << (x % 32))
525 static unsigned arm_req_sp_limited[] = { BIT(REG_SP) };
526 static const arch_register_req_t _arm_req_sp = {
527 arch_register_req_type_limited,
528 &arm_reg_classes[CLASS_arm_gp],
534 /* construct Store: Store(ptr, val, mem) = ST ptr,val */
535 ir_node *new_r_arm_StoreStackMInc(ir_graph *irg, ir_node *block, ir_node *mem,
536 ir_node *sp, int n_regs, ir_node **regs,
541 static const arch_register_req_t *_in_req_arm_StoreStackM4Inc[] =
543 &arm_StoreStackM4Inc_reg_req_in_0,
544 &arm_StoreStackM4Inc_reg_req_in_1,
545 &arm_StoreStackM4Inc_reg_req_in_2,
546 &arm_StoreStackM4Inc_reg_req_in_2,
547 &arm_StoreStackM4Inc_reg_req_in_2,
548 &arm_StoreStackM4Inc_reg_req_in_2,
549 &arm_StoreStackM4Inc_reg_req_in_2,
550 &arm_StoreStackM4Inc_reg_req_in_2,
551 &arm_StoreStackM4Inc_reg_req_in_2,
552 &arm_StoreStackM4Inc_reg_req_in_2,
553 &arm_StoreStackM4Inc_reg_req_in_2,
554 &arm_StoreStackM4Inc_reg_req_in_2,
555 &arm_StoreStackM4Inc_reg_req_in_2,
556 &arm_StoreStackM4Inc_reg_req_in_2,
557 &arm_StoreStackM4Inc_reg_req_in_2,
558 &arm_StoreStackM4Inc_reg_req_in_2,
559 &arm_StoreStackM4Inc_reg_req_in_2,
562 assert(n_regs <= 15);
566 memcpy(&in[2], regs, n_regs * sizeof(in[0]));
567 res = new_ir_node(NULL, irg, block, op_arm_StoreStackM4Inc, mode, 2 + n_regs, in);
568 flags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */
570 /* init node attributes */
571 init_arm_attributes(res, flags, _in_req_arm_StoreStackM4Inc, NULL, NULL, 0, 1);
573 res = optimize_node(res);
574 irn_vrfy_irg(res, irg);
579 /************************************************
581 * / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __ *
582 * | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__| *
583 * | |_| | |_) | |_| | | | | | | |/ / __/ | *
584 * \___/| .__/ \__|_|_| |_| |_|_/___\___|_| *
586 ************************************************/
588 typedef struct _opt_tuple {
589 ir_op *op_imm_left; /**< immediate is left */
590 ir_op *op_imm_right; /**< immediate is right */
591 ir_op *op_shf_left; /**< shift operand on left */
592 ir_op *op_shf_right; /**< shift operand on right */
595 //static const opt_tuple *opt_ops[iro_arm_last];
597 void arm_set_optimizers(void) {
599 #define STD(op) p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
600 #define LEFT(op) p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
601 #define SET(op) opt_ops[iro_arm_##op] = &p_##op;
603 static const opt_tuple
612 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
614 memset(opt_ops, 0, sizeof(opt_ops));
627 /* Include the generated constructor functions */
628 #include "gen_arm_new_nodes.c.inl"