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 * Dumper interface for dumping arm nodes in vcg.
90 * @param n the node to dump
91 * @param F the output file
92 * @param reason indicates which kind of information should be dumped
93 * @return 0 on success or != 0 on failure
95 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason)
98 arm_attr_t *attr = get_arm_attr(n);
99 arm_shift_modifier mod;
102 case dump_node_opcode_txt:
103 fprintf(F, "%s", get_irn_opname(n));
106 case dump_node_mode_txt:
107 mode = get_irn_mode(n);
110 fprintf(F, "[%s]", get_mode_name(mode));
113 fprintf(F, "[?NOMODE?]");
117 case dump_node_nodeattr_txt:
118 mod = ARM_GET_SHF_MOD(attr);
119 if (ARM_HAS_SHIFT(mod)) {
120 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), attr->imm_value);
122 else if (mod == ARM_SHF_IMM) {
124 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->imm_value));
128 case dump_node_info_txt:
129 arch_dump_reqs_and_registers(F, n);
131 if (is_arm_CopyB(n)) {
132 fprintf(F, "size = %lu\n", get_arm_imm_value(n));
134 long v = get_arm_imm_value(n);
135 if (ARM_GET_FPA_IMM(attr)) {
136 fprintf(F, "immediate float value = %s\n", arm_get_fpa_imm_name(v));
138 fprintf(F, "immediate value = %ld (0x%08lx)\n", v, v);
142 if (is_arm_CmpBra(n) && get_arm_CondJmp_proj_num(n) >= 0) {
143 fprintf(F, "proj_num = (%d)\n", get_arm_CondJmp_proj_num(n));
153 /***************************************************************************************************
155 * | | | | | | / / | | | | | | | |
156 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
157 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
158 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
159 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
162 ***************************************************************************************************/
164 /* Returns the attributes of a generic Arm node. */
165 arm_attr_t *get_arm_attr(ir_node *node) {
166 assert(is_arm_irn(node) && "need arm node to get attributes");
167 return get_irn_generic_attr(node);
170 const arm_attr_t *get_arm_attr_const(const ir_node *node) {
171 assert(is_arm_irn(node) && "need arm node to get attributes");
172 return get_irn_generic_attr_const(node);
176 * Returns the attributes of an ARM SymConst node.
178 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node) {
179 assert(is_arm_SymConst(node));
180 return get_irn_generic_attr(node);
183 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node) {
184 assert(is_arm_SymConst(node));
185 return get_irn_generic_attr_const(node);
188 static const arm_fpaConst_attr_t *get_arm_fpaConst_attr_const(const ir_node *node) {
189 const arm_attr_t *attr = get_arm_attr_const(node);
190 const arm_fpaConst_attr_t *fpa_attr = CONST_CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
195 static arm_fpaConst_attr_t *get_arm_fpaConst_attr(ir_node *node) {
196 arm_attr_t *attr = get_arm_attr(node);
197 arm_fpaConst_attr_t *fpa_attr = CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
203 static int is_arm_CondJmp(const ir_node *node) {
204 int code = get_arm_irn_opcode(node);
206 return (code == iro_arm_CmpBra || code == iro_arm_fpaCmfBra ||
207 code == iro_arm_fpaCnfBra || iro_arm_fpaCmfeBra ||
208 code == iro_arm_fpaCnfeBra);
212 /* Returns the attributes of a CondJmp node. */
213 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node) {
214 assert(is_arm_CondJmp(node));
215 return get_irn_generic_attr(node);
218 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node) {
219 assert(is_arm_CondJmp(node));
220 return get_irn_generic_attr_const(node);
223 /* Returns the attributes of a SwitchJmp node. */
224 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node) {
225 assert(is_arm_SwitchJmp(node));
226 return get_irn_generic_attr(node);
229 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node) {
230 assert(is_arm_SwitchJmp(node));
231 return get_irn_generic_attr_const(node);
235 * Returns the argument register requirements of a arm node.
237 const arch_register_req_t **get_arm_in_req_all(const ir_node *node) {
238 const arm_attr_t *attr = get_arm_attr_const(node);
243 * Returns the argument register requirement at position pos of an arm node.
245 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
246 const arm_attr_t *attr = get_arm_attr_const(node);
247 return attr->in_req[pos];
251 * Sets the IN register requirements at position pos.
253 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
254 arm_attr_t *attr = get_arm_attr(node);
255 attr->in_req[pos] = req;
259 * Returns the immediate value
261 long get_arm_imm_value(const ir_node *node) {
262 const arm_attr_t *attr = get_arm_attr_const(node);
263 return attr->imm_value;
267 * Sets the tarval value
269 void set_arm_imm_value(ir_node *node, long imm_value) {
270 arm_attr_t *attr = get_arm_attr(node);
271 attr->imm_value = imm_value;
275 * Returns the fpaConst value
277 tarval *get_fpaConst_value(const ir_node *node) {
278 const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
283 * Sets the tarval value
285 void set_fpaConst_value(ir_node *node, tarval *tv) {
286 arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
291 * Returns the proj num
293 int get_arm_CondJmp_proj_num(const ir_node *node) {
294 const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
295 return attr->proj_num;
301 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
302 arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
303 attr->proj_num = proj_num;
307 * Returns the SymConst label
309 ident *get_arm_symconst_id(const ir_node *node) {
310 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
311 return attr->symconst_id;
315 * Sets the SymConst label
317 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
318 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
319 attr->symconst_id = symconst_id;
323 * Returns the number of projs of a SwitchJmp.
325 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
326 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
327 return attr->n_projs;
331 * Sets the number of projs.
333 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
334 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
335 attr->n_projs = n_projs;
339 * Returns the default_proj_num.
341 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
342 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
343 return attr->default_proj_num;
347 * Sets the default_proj_num.
349 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
350 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
351 attr->default_proj_num = default_proj_num;
355 * Gets the shift modifier attribute.
357 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
358 const arm_attr_t *attr = get_arm_attr_const(node);
359 return ARM_GET_SHF_MOD(attr);
362 /* Set the ARM machine node attributes to default values. */
363 static void init_arm_attributes(ir_node *node, int flags,
364 const arch_register_req_t ** in_reqs,
365 const be_execution_unit_t ***execution_units,
368 ir_graph *irg = get_irn_irg(node);
369 struct obstack *obst = get_irg_obstack(irg);
370 arm_attr_t *attr = get_arm_attr(node);
371 backend_info_t *info;
372 (void) execution_units;
374 arch_irn_set_flags(node, flags);
375 attr->in_req = in_reqs;
376 attr->instr_fl = (ARM_COND_AL << 3) | ARM_SHF_NONE;
379 info = be_get_info(node);
380 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
381 memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
384 void init_arm_load_store_attributes(ir_node *res, ir_entity *entity,
385 int entity_sign, long offset)
387 arm_load_store_attr_t *attr = get_irn_generic_attr(res);
388 attr->entity = entity;
389 attr->entity_sign = entity_sign;
390 attr->offset = offset;
393 /************************************************
395 * / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __ *
396 * | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__| *
397 * | |_| | |_) | |_| | | | | | | |/ / __/ | *
398 * \___/| .__/ \__|_|_| |_| |_|_/___\___|_| *
400 ************************************************/
402 typedef struct _opt_tuple {
403 ir_op *op_imm_left; /**< immediate is left */
404 ir_op *op_imm_right; /**< immediate is right */
405 ir_op *op_shf_left; /**< shift operand on left */
406 ir_op *op_shf_right; /**< shift operand on right */
409 //static const opt_tuple *opt_ops[iro_arm_last];
411 void arm_set_optimizers(void) {
413 #define STD(op) p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
414 #define LEFT(op) p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
415 #define SET(op) opt_ops[iro_arm_##op] = &p_##op;
417 static const opt_tuple
426 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
428 memset(opt_ops, 0, sizeof(opt_ops));
441 static int cmp_attr_arm(ir_node *a, ir_node *b) {
442 arm_attr_t *attr_a = get_irn_generic_attr(a);
443 arm_attr_t *attr_b = get_irn_generic_attr(b);
444 return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
447 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
448 const arm_SymConst_attr_t *attr_a;
449 const arm_SymConst_attr_t *attr_b;
451 if (cmp_attr_arm(a, b))
454 attr_a = get_irn_generic_attr_const(a);
455 attr_b = get_irn_generic_attr_const(b);
456 return attr_a->symconst_id != attr_b->symconst_id;
459 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
462 /* never identical */
466 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
469 /* never identical */
473 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
474 const arm_fpaConst_attr_t *attr_a;
475 const arm_fpaConst_attr_t *attr_b;
477 if (cmp_attr_arm(a, b))
480 attr_a = get_arm_fpaConst_attr_const(a);
481 attr_b = get_arm_fpaConst_attr_const(b);
483 return attr_a->tv != attr_b->tv;
486 arm_load_store_attr_t *get_arm_load_store_attr(ir_node *node)
488 return (arm_load_store_attr_t*) get_irn_generic_attr(node);
491 static int cmp_attr_arm_load_store(ir_node *a, ir_node *b)
493 const arm_load_store_attr_t *attr_a;
494 const arm_load_store_attr_t *attr_b;
496 if (cmp_attr_arm(a, b))
499 attr_a = get_arm_load_store_attr(a);
500 attr_b = get_arm_load_store_attr(b);
501 if (attr_a->entity != attr_b->entity
502 || attr_a->entity_sign != attr_b->entity_sign
503 || attr_a->offset != attr_b->offset)
509 /** copies the ARM attributes of a node. */
510 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
511 ir_graph *irg = get_irn_irg(new_node);
512 struct obstack *obst = get_irg_obstack(irg);
513 const arm_attr_t *attr_old = get_arm_attr_const(old_node);
514 arm_attr_t *attr_new = get_arm_attr(new_node);
515 backend_info_t *old_info = be_get_info(old_node);
516 backend_info_t *new_info = be_get_info(new_node);
518 /* copy the attributes */
519 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
522 new_info->out_infos =
523 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
528 /* Include the generated constructor functions */
529 #include "gen_arm_new_nodes.c.inl"