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);
103 arch_dump_register_req(F, reqs[i], node);
109 fprintf(F, "%sreq = N/A\n", dir);
114 * Dumper interface for dumping arm nodes in vcg.
115 * @param n the node to dump
116 * @param F the output file
117 * @param reason indicates which kind of information should be dumped
118 * @return 0 on success or != 0 on failure
120 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
121 ir_mode *mode = NULL;
124 arm_attr_t *attr = get_arm_attr(n);
125 const arch_register_req_t **reqs;
126 arm_shift_modifier mod;
129 case dump_node_opcode_txt:
130 fprintf(F, "%s", get_irn_opname(n));
133 case dump_node_mode_txt:
134 mode = get_irn_mode(n);
137 fprintf(F, "[%s]", get_mode_name(mode));
140 fprintf(F, "[?NOMODE?]");
144 case dump_node_nodeattr_txt:
145 mod = ARM_GET_SHF_MOD(attr);
146 if (ARM_HAS_SHIFT(mod)) {
147 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), attr->imm_value);
149 else if (mod == ARM_SHF_IMM) {
151 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->imm_value));
155 case dump_node_info_txt:
156 fprintf(F, "=== arm attr begin ===\n");
158 /* dump IN requirements */
159 if (get_irn_arity(n) > 0) {
160 reqs = get_arm_in_req_all(n);
161 dump_reg_req(F, n, reqs, 0);
164 n_res = arch_irn_get_n_outs(n);
166 /* dump OUT requirements */
167 reqs = get_arm_out_req_all(n);
168 dump_reg_req(F, n, reqs, 1);
170 /* dump assigned registers */
171 for (i = 0; i < n_res; i++) {
172 const arch_register_t *reg = arch_irn_get_register(n, i);
174 fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
181 fprintf(F, "n_res = %d\n", n_res);
184 fprintf(F, "flags =");
185 flags = arch_irn_get_flags(n);
186 if (flags == arch_irn_flags_none) {
190 if (flags & arch_irn_flags_dont_spill) {
191 fprintf(F, " unspillable");
193 if (flags & arch_irn_flags_rematerializable) {
194 fprintf(F, " remat");
196 if (flags & arch_irn_flags_modify_flags) {
197 fprintf(F, " modify_flags");
200 fprintf(F, " (%d)\n", flags);
202 if (is_arm_CopyB(n)) {
203 fprintf(F, "size = %lu\n", get_arm_imm_value(n));
205 long v = get_arm_imm_value(n);
206 if (ARM_GET_FPA_IMM(attr)) {
207 fprintf(F, "immediate float value = %s\n", arm_get_fpa_imm_name(v));
209 fprintf(F, "immediate value = %ld (0x%08lx)\n", v, v);
213 if (is_arm_CmpBra(n) && get_arm_CondJmp_proj_num(n) >= 0) {
214 fprintf(F, "proj_num = (%d)\n", get_arm_CondJmp_proj_num(n));
216 /* TODO: dump all additional attributes */
218 fprintf(F, "=== arm attr end ===\n");
219 /* end of: case dump_node_info_txt */
227 /***************************************************************************************************
229 * | | | | | | / / | | | | | | | |
230 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
231 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
232 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
233 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
236 ***************************************************************************************************/
238 /* Returns the attributes of a generic Arm node. */
239 arm_attr_t *get_arm_attr(ir_node *node) {
240 assert(is_arm_irn(node) && "need arm node to get attributes");
241 return get_irn_generic_attr(node);
244 const arm_attr_t *get_arm_attr_const(const ir_node *node) {
245 assert(is_arm_irn(node) && "need arm node to get attributes");
246 return get_irn_generic_attr_const(node);
250 * Returns the attributes of an ARM SymConst node.
252 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node) {
253 assert(is_arm_SymConst(node));
254 return get_irn_generic_attr(node);
257 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node) {
258 assert(is_arm_SymConst(node));
259 return get_irn_generic_attr_const(node);
262 static const arm_fpaConst_attr_t *get_arm_fpaConst_attr_const(const ir_node *node) {
263 const arm_attr_t *attr = get_arm_attr_const(node);
264 const arm_fpaConst_attr_t *fpa_attr = CONST_CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
269 static arm_fpaConst_attr_t *get_arm_fpaConst_attr(ir_node *node) {
270 arm_attr_t *attr = get_arm_attr(node);
271 arm_fpaConst_attr_t *fpa_attr = CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
277 static int is_arm_CondJmp(const ir_node *node) {
278 int code = get_arm_irn_opcode(node);
280 return (code == iro_arm_CmpBra || code == iro_arm_fpaCmfBra ||
281 code == iro_arm_fpaCnfBra || iro_arm_fpaCmfeBra ||
282 code == iro_arm_fpaCnfeBra);
286 /* Returns the attributes of a CondJmp node. */
287 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node) {
288 assert(is_arm_CondJmp(node));
289 return get_irn_generic_attr(node);
292 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node) {
293 assert(is_arm_CondJmp(node));
294 return get_irn_generic_attr_const(node);
297 /* Returns the attributes of a SwitchJmp node. */
298 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node) {
299 assert(is_arm_SwitchJmp(node));
300 return get_irn_generic_attr(node);
303 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node) {
304 assert(is_arm_SwitchJmp(node));
305 return get_irn_generic_attr_const(node);
309 * Returns the argument register requirements of a arm node.
311 const arch_register_req_t **get_arm_in_req_all(const ir_node *node) {
312 const arm_attr_t *attr = get_arm_attr_const(node);
317 * Returns the result register requirements of an arm node.
319 const arch_register_req_t **get_arm_out_req_all(const ir_node *node) {
320 const arm_attr_t *attr = get_arm_attr_const(node);
321 return attr->out_req;
325 * Returns the argument register requirement at position pos of an arm node.
327 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
328 const arm_attr_t *attr = get_arm_attr_const(node);
329 return attr->in_req[pos];
333 * Returns the result register requirement at position pos of an arm node.
335 const arch_register_req_t *get_arm_out_req(const ir_node *node, int pos) {
336 const arm_attr_t *attr = get_arm_attr_const(node);
337 return attr->out_req[pos];
341 * Sets the OUT register requirements at position pos.
343 void set_arm_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
344 arm_attr_t *attr = get_arm_attr(node);
345 attr->out_req[pos] = req;
349 * Sets the complete OUT requirements of node.
351 void set_arm_req_out_all(ir_node *node, const arch_register_req_t **reqs) {
352 arm_attr_t *attr = get_arm_attr(node);
353 attr->out_req = reqs;
357 * Sets the IN register requirements at position pos.
359 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
360 arm_attr_t *attr = get_arm_attr(node);
361 attr->in_req[pos] = req;
365 * Returns the immediate value
367 long get_arm_imm_value(const ir_node *node) {
368 const arm_attr_t *attr = get_arm_attr_const(node);
369 return attr->imm_value;
373 * Sets the tarval value
375 void set_arm_imm_value(ir_node *node, long imm_value) {
376 arm_attr_t *attr = get_arm_attr(node);
377 attr->imm_value = imm_value;
381 * Returns the fpaConst value
383 tarval *get_fpaConst_value(const ir_node *node) {
384 const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
389 * Sets the tarval value
391 void set_fpaConst_value(ir_node *node, tarval *tv) {
392 arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
397 * Returns the proj num
399 int get_arm_CondJmp_proj_num(const ir_node *node) {
400 const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
401 return attr->proj_num;
407 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
408 arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
409 attr->proj_num = proj_num;
413 * Returns the SymConst label
415 ident *get_arm_symconst_id(const ir_node *node) {
416 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
417 return attr->symconst_id;
421 * Sets the SymConst label
423 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
424 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
425 attr->symconst_id = symconst_id;
429 * Returns the number of projs of a SwitchJmp.
431 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
432 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
433 return attr->n_projs;
437 * Sets the number of projs.
439 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
440 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
441 attr->n_projs = n_projs;
445 * Returns the default_proj_num.
447 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
448 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
449 return attr->default_proj_num;
453 * Sets the default_proj_num.
455 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
456 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
457 attr->default_proj_num = default_proj_num;
461 * Gets the shift modifier attribute.
463 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
464 const arm_attr_t *attr = get_arm_attr_const(node);
465 return ARM_GET_SHF_MOD(attr);
468 /* Set the ARM machine node attributes to default values. */
469 static void init_arm_attributes(ir_node *node, int flags,
470 const arch_register_req_t ** in_reqs,
471 const arch_register_req_t ** out_reqs,
472 const be_execution_unit_t ***execution_units,
474 ir_graph *irg = get_irn_irg(node);
475 struct obstack *obst = get_irg_obstack(irg);
476 arm_attr_t *attr = get_arm_attr(node);
477 backend_info_t *info;
478 (void) execution_units;
480 arch_irn_set_flags(node, flags);
481 attr->in_req = in_reqs;
482 attr->out_req = out_reqs;
483 attr->instr_fl = (ARM_COND_AL << 3) | ARM_SHF_NONE;
486 info = be_get_info(node);
487 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
488 memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
491 /************************************************
493 * / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __ *
494 * | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__| *
495 * | |_| | |_) | |_| | | | | | | |/ / __/ | *
496 * \___/| .__/ \__|_|_| |_| |_|_/___\___|_| *
498 ************************************************/
500 typedef struct _opt_tuple {
501 ir_op *op_imm_left; /**< immediate is left */
502 ir_op *op_imm_right; /**< immediate is right */
503 ir_op *op_shf_left; /**< shift operand on left */
504 ir_op *op_shf_right; /**< shift operand on right */
507 //static const opt_tuple *opt_ops[iro_arm_last];
509 void arm_set_optimizers(void) {
511 #define STD(op) p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
512 #define LEFT(op) p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
513 #define SET(op) opt_ops[iro_arm_##op] = &p_##op;
515 static const opt_tuple
524 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
526 memset(opt_ops, 0, sizeof(opt_ops));
539 static int cmp_attr_arm(ir_node *a, ir_node *b) {
540 arm_attr_t *attr_a = get_irn_generic_attr(a);
541 arm_attr_t *attr_b = get_irn_generic_attr(b);
542 return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
545 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
546 const arm_SymConst_attr_t *attr_a;
547 const arm_SymConst_attr_t *attr_b;
549 if (cmp_attr_arm(a, b))
552 attr_a = get_irn_generic_attr_const(a);
553 attr_b = get_irn_generic_attr_const(b);
554 return attr_a->symconst_id != attr_b->symconst_id;
557 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
560 /* never identical */
564 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
567 /* never identical */
571 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
572 const arm_fpaConst_attr_t *attr_a;
573 const arm_fpaConst_attr_t *attr_b;
575 if (cmp_attr_arm(a, b))
578 attr_a = get_arm_fpaConst_attr_const(a);
579 attr_b = get_arm_fpaConst_attr_const(b);
581 return attr_a->tv != attr_b->tv;
584 /** copies the ARM attributes of a node. */
585 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
586 ir_graph *irg = get_irn_irg(new_node);
587 struct obstack *obst = get_irg_obstack(irg);
588 const arm_attr_t *attr_old = get_arm_attr_const(old_node);
589 arm_attr_t *attr_new = get_arm_attr(new_node);
590 backend_info_t *old_info = be_get_info(old_node);
591 backend_info_t *new_info = be_get_info(new_node);
593 /* copy the attributes */
594 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
597 new_info->out_infos =
598 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
603 /* Include the generated constructor functions */
604 #include "gen_arm_new_nodes.c.inl"