2 * Copyright (C) 1995-2008 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
33 #include "irgraph_t.h"
39 #include "firm_common_t.h"
44 #include "../bearch_t.h"
46 #include "arm_nodes_attr.h"
47 #include "arm_new_nodes.h"
48 #include "arm_optimize.h"
51 #include "bearch_arm_t.h"
54 * Returns the shift modifier string.
56 const char *arm_shf_mod_name(arm_shift_modifier mod) {
57 static const char *names[] = { NULL, NULL, "asr", "lsl", "lsr", "ror", "rrx" };
62 * Return the fpa immediate from the encoding.
64 const char *arm_get_fpa_imm_name(long imm_value) {
65 static const char *fpa_imm[] = {
75 return fpa_imm[imm_value];
78 /***********************************************************************************
81 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
82 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
83 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
84 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
87 ***********************************************************************************/
90 * Dumps the register requirements for either in or out.
92 static void dump_reg_req(FILE *F, const ir_node *node,
93 const arch_register_req_t **reqs, int inout) {
94 char *dir = inout ? "out" : "in";
95 int max = inout ? get_arm_n_res(node) : get_irn_arity(node);
99 memset(buf, 0, sizeof(buf));
102 for (i = 0; i < max; i++) {
103 fprintf(F, "%sreq #%d =", dir, i);
105 if (reqs[i]->type == arch_register_req_type_none) {
109 if (reqs[i]->type & arch_register_req_type_normal) {
110 fprintf(F, " %s", reqs[i]->cls->name);
113 if (reqs[i]->type & arch_register_req_type_limited) {
115 arch_register_req_format(buf, sizeof(buf), reqs[i], node));
118 if (reqs[i]->type & arch_register_req_type_should_be_same) {
119 const unsigned other = reqs[i]->other_same;
122 ir_fprintf(F, " same as");
123 for (i = 0; 1U << i <= other; ++i) {
124 if (other & (1U << i)) {
125 ir_fprintf(F, " %+F", get_irn_n(node, i));
130 if (reqs[i]->type & arch_register_req_type_must_be_different) {
131 const unsigned other = reqs[i]->other_different;
134 ir_fprintf(F, " different from");
135 for (i = 0; 1U << i <= other; ++i) {
136 if (other & (1U << i)) {
137 ir_fprintf(F, " %+F", get_irn_n(node, i));
147 fprintf(F, "%sreq = N/A\n", dir);
152 * Dumper interface for dumping arm nodes in vcg.
153 * @param n the node to dump
154 * @param F the output file
155 * @param reason indicates which kind of information should be dumped
156 * @return 0 on success or != 0 on failure
158 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
159 ir_mode *mode = NULL;
162 arm_attr_t *attr = get_arm_attr(n);
163 const arch_register_req_t **reqs;
164 const arch_register_t **slots;
165 arm_shift_modifier mod;
168 case dump_node_opcode_txt:
169 fprintf(F, "%s", get_irn_opname(n));
172 case dump_node_mode_txt:
173 mode = get_irn_mode(n);
176 fprintf(F, "[%s]", get_mode_name(mode));
179 fprintf(F, "[?NOMODE?]");
183 case dump_node_nodeattr_txt:
184 mod = ARM_GET_SHF_MOD(attr);
185 if (ARM_HAS_SHIFT(mod)) {
186 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), attr->imm_value);
188 else if (mod == ARM_SHF_IMM) {
190 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->imm_value));
194 case dump_node_info_txt:
195 fprintf(F, "=== arm attr begin ===\n");
197 /* dump IN requirements */
198 if (get_irn_arity(n) > 0) {
199 reqs = get_arm_in_req_all(n);
200 dump_reg_req(F, n, reqs, 0);
203 /* dump OUT requirements */
204 if (ARR_LEN(attr->slots) > 0) {
205 reqs = get_arm_out_req_all(n);
206 dump_reg_req(F, n, reqs, 1);
209 /* dump assigned registers */
210 slots = get_arm_slots(n);
211 if (slots && ARR_LEN(attr->slots) > 0) {
212 for (i = 0; i < ARR_LEN(attr->slots); i++) {
214 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
217 fprintf(F, "reg #%d = n/a\n", i);
224 fprintf(F, "n_res = %d\n", get_arm_n_res(n));
227 fprintf(F, "flags =");
228 if (attr->flags == arch_irn_flags_none) {
232 if (attr->flags & arch_irn_flags_dont_spill) {
233 fprintf(F, " unspillable");
235 if (attr->flags & arch_irn_flags_rematerializable) {
236 fprintf(F, " remat");
239 fprintf(F, " (%d)\n", attr->flags);
241 if (is_arm_CopyB(n)) {
242 fprintf(F, "size = %lu\n", get_arm_imm_value(n));
244 long v = get_arm_imm_value(n);
245 if (ARM_GET_FPA_IMM(attr)) {
246 fprintf(F, "immediate float value = %s\n", arm_get_fpa_imm_name(v));
248 fprintf(F, "immediate value = %ld (0x%08lx)\n", v, v);
252 if (is_arm_CmpBra(n) && get_arm_CondJmp_proj_num(n) >= 0) {
253 fprintf(F, "proj_num = (%d)\n", get_arm_CondJmp_proj_num(n));
255 /* TODO: dump all additional attributes */
257 fprintf(F, "=== arm attr end ===\n");
258 /* end of: case dump_node_info_txt */
266 /***************************************************************************************************
268 * | | | | | | / / | | | | | | | |
269 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
270 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
271 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
272 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
275 ***************************************************************************************************/
277 /* Returns the attributes of a generic Arm node. */
278 arm_attr_t *get_arm_attr(ir_node *node) {
279 assert(is_arm_irn(node) && "need arm node to get attributes");
280 return get_irn_generic_attr(node);
283 const arm_attr_t *get_arm_attr_const(const ir_node *node) {
284 assert(is_arm_irn(node) && "need arm node to get attributes");
285 return get_irn_generic_attr_const(node);
289 * Returns the attributes of an ARM SymConst node.
291 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node) {
292 assert(is_arm_SymConst(node));
293 return get_irn_generic_attr(node);
296 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node) {
297 assert(is_arm_SymConst(node));
298 return get_irn_generic_attr_const(node);
301 static const arm_fpaConst_attr_t *get_arm_fpaConst_attr_const(const ir_node *node) {
302 const arm_attr_t *attr = get_arm_attr_const(node);
303 const arm_fpaConst_attr_t *fpa_attr = CONST_CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
308 static arm_fpaConst_attr_t *get_arm_fpaConst_attr(ir_node *node) {
309 arm_attr_t *attr = get_arm_attr(node);
310 arm_fpaConst_attr_t *fpa_attr = CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
315 static int is_arm_CondJmp(const ir_node *node) {
316 int code = get_arm_irn_opcode(node);
318 return (code == iro_arm_CmpBra || code == iro_arm_fpaCmfBra ||
319 code == iro_arm_fpaCnfBra || iro_arm_fpaCmfeBra ||
320 code == iro_arm_fpaCnfeBra);
323 /* Returns the attributes of a CondJmp node. */
324 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node) {
325 assert(is_arm_CondJmp(node));
326 return get_irn_generic_attr(node);
329 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node) {
330 assert(is_arm_CondJmp(node));
331 return get_irn_generic_attr_const(node);
334 /* Returns the attributes of a SwitchJmp node. */
335 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node) {
336 assert(is_arm_SwitchJmp(node));
337 return get_irn_generic_attr(node);
340 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node) {
341 assert(is_arm_SwitchJmp(node));
342 return get_irn_generic_attr_const(node);
346 * Returns the argument register requirements of a arm node.
348 const arch_register_req_t **get_arm_in_req_all(const ir_node *node) {
349 const arm_attr_t *attr = get_arm_attr_const(node);
354 * Returns the result register requirements of an arm node.
356 const arch_register_req_t **get_arm_out_req_all(const ir_node *node) {
357 const arm_attr_t *attr = get_arm_attr_const(node);
358 return attr->out_req;
362 * Returns the argument register requirement at position pos of an arm node.
364 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
365 const arm_attr_t *attr = get_arm_attr_const(node);
366 return attr->in_req[pos];
370 * Returns the result register requirement at position pos of an arm node.
372 const arch_register_req_t *get_arm_out_req(const ir_node *node, int pos) {
373 const arm_attr_t *attr = get_arm_attr_const(node);
374 return attr->out_req[pos];
378 * Sets the OUT register requirements at position pos.
380 void set_arm_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
381 arm_attr_t *attr = get_arm_attr(node);
382 attr->out_req[pos] = req;
386 * Sets the complete OUT requirements of node.
388 void set_arm_req_out_all(ir_node *node, const arch_register_req_t **reqs) {
389 arm_attr_t *attr = get_arm_attr(node);
390 attr->out_req = reqs;
394 * Sets the IN register requirements at position pos.
396 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
397 arm_attr_t *attr = get_arm_attr(node);
398 attr->in_req[pos] = req;
402 * Returns the register flag of an arm node.
404 arch_irn_flags_t get_arm_flags(const ir_node *node) {
405 const arm_attr_t *attr = get_arm_attr_const(node);
410 * Sets the register flag of an arm node.
412 void set_arm_flags(ir_node *node, arch_irn_flags_t flags) {
413 arm_attr_t *attr = get_arm_attr(node);
418 * Returns the result register slots of an arm node.
420 const arch_register_t **get_arm_slots(const ir_node *node) {
421 const arm_attr_t *attr = get_arm_attr_const(node);
426 * Returns the name of the OUT register at position pos.
428 const char *get_arm_out_reg_name(const ir_node *node, int pos) {
429 const arm_attr_t *attr = get_arm_attr_const(node);
431 assert(is_arm_irn(node) && "Not an arm node.");
432 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
433 assert(attr->slots[pos] && "No register assigned");
435 return arch_register_get_name(attr->slots[pos]);
439 * Returns the index of the OUT register at position pos within its register class.
441 int get_arm_out_regnr(const ir_node *node, int pos) {
442 const arm_attr_t *attr = get_arm_attr_const(node);
444 assert(is_arm_irn(node) && "Not an arm node.");
445 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
446 assert(attr->slots[pos] && "No register assigned");
448 return arch_register_get_index(attr->slots[pos]);
452 * Returns the OUT register at position pos.
454 const arch_register_t *get_arm_out_reg(const ir_node *node, int pos) {
455 const arm_attr_t *attr = get_arm_attr_const(node);
457 assert(is_arm_irn(node) && "Not an arm node.");
458 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
459 assert(attr->slots[pos] && "No register assigned");
461 return attr->slots[pos];
465 * Sets the flags for the n'th out.
467 void set_arm_out_flags(ir_node *node, arch_irn_flags_t flags, int pos) {
468 arm_attr_t *attr = get_arm_attr(node);
469 assert(pos < ARR_LEN(attr->out_flags) && "Invalid OUT position.");
470 attr->out_flags[pos] = flags;
474 * Gets the flags for the n'th out.
476 arch_irn_flags_t get_arm_out_flags(const ir_node *node, int pos) {
477 const arm_attr_t *attr = get_arm_attr_const(node);
478 assert(pos < ARR_LEN(attr->out_flags) && "Invalid OUT position.");
479 return attr->out_flags[pos];
483 * Returns the number of results.
485 int get_arm_n_res(const ir_node *node) {
486 const arm_attr_t *attr = get_arm_attr_const(node);
487 return ARR_LEN(attr->slots);
491 * Returns the immediate value
493 long get_arm_imm_value(const ir_node *node) {
494 const arm_attr_t *attr = get_arm_attr_const(node);
495 return attr->imm_value;
499 * Sets the tarval value
501 void set_arm_imm_value(ir_node *node, long imm_value) {
502 arm_attr_t *attr = get_arm_attr(node);
503 attr->imm_value = imm_value;
507 * Returns the fpaConst value
509 tarval *get_fpaConst_value(const ir_node *node) {
510 const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
515 * Sets the tarval value
517 void set_fpaConst_value(ir_node *node, tarval *tv) {
518 arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
523 * Returns the proj num
525 int get_arm_CondJmp_proj_num(const ir_node *node) {
526 const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
527 return attr->proj_num;
533 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
534 arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
535 attr->proj_num = proj_num;
539 * Returns the SymConst label
541 ident *get_arm_symconst_id(const ir_node *node) {
542 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
543 return attr->symconst_id;
547 * Sets the SymConst label
549 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
550 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
551 attr->symconst_id = symconst_id;
555 * Returns the number of projs of a SwitchJmp.
557 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
558 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
559 return attr->n_projs;
563 * Sets the number of projs.
565 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
566 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
567 attr->n_projs = n_projs;
571 * Returns the default_proj_num.
573 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
574 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
575 return attr->default_proj_num;
579 * Sets the default_proj_num.
581 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
582 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
583 attr->default_proj_num = default_proj_num;
587 * Gets the shift modifier attribute.
589 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
590 const arm_attr_t *attr = get_arm_attr_const(node);
591 return ARM_GET_SHF_MOD(attr);
594 /* Set the ARM machine node attributes to default values. */
595 static void init_arm_attributes(ir_node *node, int flags,
596 const arch_register_req_t ** in_reqs,
597 const arch_register_req_t ** out_reqs,
598 const be_execution_unit_t ***execution_units,
600 ir_graph *irg = get_irn_irg(node);
601 struct obstack *obst = get_irg_obstack(irg);
602 arm_attr_t *attr = get_arm_attr(node);
603 (void) execution_units;
605 attr->in_req = in_reqs;
606 attr->out_req = out_reqs;
608 attr->instr_fl = (ARM_COND_AL << 3) | ARM_SHF_NONE;
611 attr->out_flags = NEW_ARR_D(int, obst, n_res);
612 memset(attr->out_flags, 0, n_res * sizeof(attr->out_flags[0]));
614 attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
615 memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
618 /************************************************
620 * / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __ *
621 * | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__| *
622 * | |_| | |_) | |_| | | | | | | |/ / __/ | *
623 * \___/| .__/ \__|_|_| |_| |_|_/___\___|_| *
625 ************************************************/
627 typedef struct _opt_tuple {
628 ir_op *op_imm_left; /**< immediate is left */
629 ir_op *op_imm_right; /**< immediate is right */
630 ir_op *op_shf_left; /**< shift operand on left */
631 ir_op *op_shf_right; /**< shift operand on right */
634 //static const opt_tuple *opt_ops[iro_arm_last];
636 void arm_set_optimizers(void) {
638 #define STD(op) p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
639 #define LEFT(op) p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
640 #define SET(op) opt_ops[iro_arm_##op] = &p_##op;
642 static const opt_tuple
651 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
653 memset(opt_ops, 0, sizeof(opt_ops));
666 static int cmp_attr_arm(ir_node *a, ir_node *b) {
667 arm_attr_t *attr_a = get_irn_generic_attr(a);
668 arm_attr_t *attr_b = get_irn_generic_attr(b);
669 return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
672 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
673 const arm_SymConst_attr_t *attr_a;
674 const arm_SymConst_attr_t *attr_b;
676 if (cmp_attr_arm(a, b))
679 attr_a = get_irn_generic_attr_const(a);
680 attr_b = get_irn_generic_attr_const(b);
681 return attr_a->symconst_id != attr_b->symconst_id;
684 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
687 /* never identical */
691 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
694 /* never identical */
698 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
699 const arm_fpaConst_attr_t *attr_a;
700 const arm_fpaConst_attr_t *attr_b;
702 if (cmp_attr_arm(a, b))
705 attr_a = get_arm_fpaConst_attr_const(a);
706 attr_b = get_arm_fpaConst_attr_const(b);
708 return attr_a->tv != attr_b->tv;
711 /** copies the ARM attributes of a node. */
712 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
713 ir_graph *irg = get_irn_irg(new_node);
714 struct obstack *obst = get_irg_obstack(irg);
715 const arm_attr_t *attr_old = get_arm_attr_const(old_node);
716 arm_attr_t *attr_new = get_arm_attr(new_node);
718 /* copy the attributes */
719 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
722 attr_new->out_flags =
723 DUP_ARR_D(int, obst, attr_old->out_flags);
724 /* copy register assignments */
726 DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
731 /* Include the generated constructor functions */
732 #include "gen_arm_new_nodes.c.inl"