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"
43 #include "../bearch.h"
45 #include "arm_nodes_attr.h"
46 #include "arm_new_nodes.h"
47 #include "arm_optimize.h"
50 #include "bearch_arm_t.h"
53 * Returns the shift modifier string.
55 const char *arm_shf_mod_name(arm_shift_modifier mod) {
56 static const char *names[] = { NULL, NULL, "asr", "lsl", "lsr", "ror", "rrx" };
61 * Return the fpa immediate from the encoding.
63 const char *arm_get_fpa_imm_name(long imm_value) {
64 static const char *fpa_imm[] = {
74 return fpa_imm[imm_value];
77 /***********************************************************************************
80 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
81 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
82 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
83 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
86 ***********************************************************************************/
89 * Dumps the register requirements for either in or out.
91 static void dump_reg_req(FILE *F, const ir_node *node,
92 const arch_register_req_t **reqs, int inout) {
93 char *dir = inout ? "out" : "in";
94 int max = inout ? (int) arch_irn_get_n_outs(node) : get_irn_arity(node);
98 memset(buf, 0, sizeof(buf));
101 for (i = 0; i < max; i++) {
102 fprintf(F, "%sreq #%d =", dir, i);
104 if (reqs[i]->type == arch_register_req_type_none) {
108 if (reqs[i]->type & arch_register_req_type_normal) {
109 fprintf(F, " %s", reqs[i]->cls->name);
112 if (reqs[i]->type & arch_register_req_type_limited) {
114 arch_register_req_format(buf, sizeof(buf), reqs[i], node));
117 if (reqs[i]->type & arch_register_req_type_should_be_same) {
118 const unsigned other = reqs[i]->other_same;
121 ir_fprintf(F, " same as");
122 for (i = 0; 1U << i <= other; ++i) {
123 if (other & (1U << i)) {
124 ir_fprintf(F, " %+F", get_irn_n(node, i));
129 if (reqs[i]->type & arch_register_req_type_must_be_different) {
130 const unsigned other = reqs[i]->other_different;
133 ir_fprintf(F, " different from");
134 for (i = 0; 1U << i <= other; ++i) {
135 if (other & (1U << i)) {
136 ir_fprintf(F, " %+F", get_irn_n(node, i));
146 fprintf(F, "%sreq = N/A\n", dir);
151 * Dumper interface for dumping arm nodes in vcg.
152 * @param n the node to dump
153 * @param F the output file
154 * @param reason indicates which kind of information should be dumped
155 * @return 0 on success or != 0 on failure
157 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
158 ir_mode *mode = NULL;
161 arm_attr_t *attr = get_arm_attr(n);
162 const arch_register_req_t **reqs;
163 arm_shift_modifier mod;
166 case dump_node_opcode_txt:
167 fprintf(F, "%s", get_irn_opname(n));
170 case dump_node_mode_txt:
171 mode = get_irn_mode(n);
174 fprintf(F, "[%s]", get_mode_name(mode));
177 fprintf(F, "[?NOMODE?]");
181 case dump_node_nodeattr_txt:
182 mod = ARM_GET_SHF_MOD(attr);
183 if (ARM_HAS_SHIFT(mod)) {
184 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), attr->imm_value);
186 else if (mod == ARM_SHF_IMM) {
188 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->imm_value));
192 case dump_node_info_txt:
193 fprintf(F, "=== arm attr begin ===\n");
195 /* dump IN requirements */
196 if (get_irn_arity(n) > 0) {
197 reqs = get_arm_in_req_all(n);
198 dump_reg_req(F, n, reqs, 0);
201 n_res = arch_irn_get_n_outs(n);
203 /* dump OUT requirements */
204 reqs = get_arm_out_req_all(n);
205 dump_reg_req(F, n, reqs, 1);
207 /* dump assigned registers */
208 for (i = 0; i < n_res; i++) {
209 const arch_register_t *reg = arch_irn_get_register(n, i);
211 fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
218 fprintf(F, "n_res = %d\n", n_res);
221 fprintf(F, "flags =");
222 flags = arch_irn_get_flags(n);
223 if (flags == arch_irn_flags_none) {
227 if (flags & arch_irn_flags_dont_spill) {
228 fprintf(F, " unspillable");
230 if (flags & arch_irn_flags_rematerializable) {
231 fprintf(F, " remat");
233 if (flags & arch_irn_flags_modify_flags) {
234 fprintf(F, " modify_flags");
237 fprintf(F, " (%d)\n", flags);
239 if (is_arm_CopyB(n)) {
240 fprintf(F, "size = %lu\n", get_arm_imm_value(n));
242 long v = get_arm_imm_value(n);
243 if (ARM_GET_FPA_IMM(attr)) {
244 fprintf(F, "immediate float value = %s\n", arm_get_fpa_imm_name(v));
246 fprintf(F, "immediate value = %ld (0x%08lx)\n", v, v);
250 if (is_arm_CmpBra(n) && get_arm_CondJmp_proj_num(n) >= 0) {
251 fprintf(F, "proj_num = (%d)\n", get_arm_CondJmp_proj_num(n));
253 /* TODO: dump all additional attributes */
255 fprintf(F, "=== arm attr end ===\n");
256 /* end of: case dump_node_info_txt */
264 /***************************************************************************************************
266 * | | | | | | / / | | | | | | | |
267 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
268 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
269 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
270 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
273 ***************************************************************************************************/
275 /* Returns the attributes of a generic Arm node. */
276 arm_attr_t *get_arm_attr(ir_node *node) {
277 assert(is_arm_irn(node) && "need arm node to get attributes");
278 return get_irn_generic_attr(node);
281 const arm_attr_t *get_arm_attr_const(const ir_node *node) {
282 assert(is_arm_irn(node) && "need arm node to get attributes");
283 return get_irn_generic_attr_const(node);
287 * Returns the attributes of an ARM SymConst node.
289 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node) {
290 assert(is_arm_SymConst(node));
291 return get_irn_generic_attr(node);
294 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node) {
295 assert(is_arm_SymConst(node));
296 return get_irn_generic_attr_const(node);
299 static const arm_fpaConst_attr_t *get_arm_fpaConst_attr_const(const ir_node *node) {
300 const arm_attr_t *attr = get_arm_attr_const(node);
301 const arm_fpaConst_attr_t *fpa_attr = CONST_CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
306 static arm_fpaConst_attr_t *get_arm_fpaConst_attr(ir_node *node) {
307 arm_attr_t *attr = get_arm_attr(node);
308 arm_fpaConst_attr_t *fpa_attr = CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
314 static int is_arm_CondJmp(const ir_node *node) {
315 int code = get_arm_irn_opcode(node);
317 return (code == iro_arm_CmpBra || code == iro_arm_fpaCmfBra ||
318 code == iro_arm_fpaCnfBra || iro_arm_fpaCmfeBra ||
319 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 immediate value
404 long get_arm_imm_value(const ir_node *node) {
405 const arm_attr_t *attr = get_arm_attr_const(node);
406 return attr->imm_value;
410 * Sets the tarval value
412 void set_arm_imm_value(ir_node *node, long imm_value) {
413 arm_attr_t *attr = get_arm_attr(node);
414 attr->imm_value = imm_value;
418 * Returns the fpaConst value
420 tarval *get_fpaConst_value(const ir_node *node) {
421 const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
426 * Sets the tarval value
428 void set_fpaConst_value(ir_node *node, tarval *tv) {
429 arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
434 * Returns the proj num
436 int get_arm_CondJmp_proj_num(const ir_node *node) {
437 const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
438 return attr->proj_num;
444 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
445 arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
446 attr->proj_num = proj_num;
450 * Returns the SymConst label
452 ident *get_arm_symconst_id(const ir_node *node) {
453 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
454 return attr->symconst_id;
458 * Sets the SymConst label
460 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
461 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
462 attr->symconst_id = symconst_id;
466 * Returns the number of projs of a SwitchJmp.
468 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
469 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
470 return attr->n_projs;
474 * Sets the number of projs.
476 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
477 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
478 attr->n_projs = n_projs;
482 * Returns the default_proj_num.
484 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
485 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
486 return attr->default_proj_num;
490 * Sets the default_proj_num.
492 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
493 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
494 attr->default_proj_num = default_proj_num;
498 * Gets the shift modifier attribute.
500 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
501 const arm_attr_t *attr = get_arm_attr_const(node);
502 return ARM_GET_SHF_MOD(attr);
505 /* Set the ARM machine node attributes to default values. */
506 static void init_arm_attributes(ir_node *node, int flags,
507 const arch_register_req_t ** in_reqs,
508 const arch_register_req_t ** out_reqs,
509 const be_execution_unit_t ***execution_units,
511 ir_graph *irg = get_irn_irg(node);
512 struct obstack *obst = get_irg_obstack(irg);
513 arm_attr_t *attr = get_arm_attr(node);
514 backend_info_t *info;
515 (void) execution_units;
517 arch_irn_set_flags(node, flags);
518 attr->in_req = in_reqs;
519 attr->out_req = out_reqs;
520 attr->instr_fl = (ARM_COND_AL << 3) | ARM_SHF_NONE;
523 info = be_get_info(node);
524 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
525 memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
528 /************************************************
530 * / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __ *
531 * | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__| *
532 * | |_| | |_) | |_| | | | | | | |/ / __/ | *
533 * \___/| .__/ \__|_|_| |_| |_|_/___\___|_| *
535 ************************************************/
537 typedef struct _opt_tuple {
538 ir_op *op_imm_left; /**< immediate is left */
539 ir_op *op_imm_right; /**< immediate is right */
540 ir_op *op_shf_left; /**< shift operand on left */
541 ir_op *op_shf_right; /**< shift operand on right */
544 //static const opt_tuple *opt_ops[iro_arm_last];
546 void arm_set_optimizers(void) {
548 #define STD(op) p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
549 #define LEFT(op) p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
550 #define SET(op) opt_ops[iro_arm_##op] = &p_##op;
552 static const opt_tuple
561 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
563 memset(opt_ops, 0, sizeof(opt_ops));
576 static int cmp_attr_arm(ir_node *a, ir_node *b) {
577 arm_attr_t *attr_a = get_irn_generic_attr(a);
578 arm_attr_t *attr_b = get_irn_generic_attr(b);
579 return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
582 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
583 const arm_SymConst_attr_t *attr_a;
584 const arm_SymConst_attr_t *attr_b;
586 if (cmp_attr_arm(a, b))
589 attr_a = get_irn_generic_attr_const(a);
590 attr_b = get_irn_generic_attr_const(b);
591 return attr_a->symconst_id != attr_b->symconst_id;
594 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
597 /* never identical */
601 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
604 /* never identical */
608 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
609 const arm_fpaConst_attr_t *attr_a;
610 const arm_fpaConst_attr_t *attr_b;
612 if (cmp_attr_arm(a, b))
615 attr_a = get_arm_fpaConst_attr_const(a);
616 attr_b = get_arm_fpaConst_attr_const(b);
618 return attr_a->tv != attr_b->tv;
621 /** copies the ARM attributes of a node. */
622 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
623 ir_graph *irg = get_irn_irg(new_node);
624 struct obstack *obst = get_irg_obstack(irg);
625 const arm_attr_t *attr_old = get_arm_attr_const(old_node);
626 arm_attr_t *attr_new = get_arm_attr(new_node);
627 backend_info_t *old_info = be_get_info(old_node);
628 backend_info_t *new_info = be_get_info(new_node);
630 /* copy the attributes */
631 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
634 new_info->out_infos =
635 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
640 /* Include the generated constructor functions */
641 #include "gen_arm_new_nodes.c.inl"