2 * Copyright (C) 1995-2011 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"
44 #include "arm_nodes_attr.h"
45 #include "arm_new_nodes.h"
46 #include "arm_optimize.h"
49 #include "bearch_arm_t.h"
51 static bool arm_has_symconst_attr(const ir_node *node)
53 return is_arm_SymConst(node) || is_arm_FrameAddr(node) || is_arm_Bl(node);
56 static bool has_load_store_attr(const ir_node *node)
58 return is_arm_Ldr(node) || is_arm_Str(node) || is_arm_LinkLdrPC(node)
59 || is_arm_Ldf(node) || is_arm_Stf(node);
62 static bool has_shifter_operand(const ir_node *node)
64 return is_arm_Add(node) || is_arm_And(node) || is_arm_Or(node)
65 || is_arm_Eor(node) || is_arm_Bic(node) || is_arm_Sub(node)
66 || is_arm_Rsb(node) || is_arm_Mov(node) || is_arm_Mvn(node)
67 || is_arm_Cmp(node) || is_arm_Tst(node) || is_arm_LinkMovPC(node);
70 static bool has_cmp_attr(const ir_node *node)
72 return is_arm_Cmp(node) || is_arm_Tst(node);
75 static bool has_farith_attr(const ir_node *node)
77 return is_arm_Adf(node) || is_arm_Muf(node) || is_arm_Suf(node)
78 || is_arm_Dvf(node) || is_arm_Mvf(node) || is_arm_FltX(node);
82 * Dumper interface for dumping arm nodes in vcg.
83 * @param F the output file
84 * @param n the node to dump
85 * @param reason indicates which kind of information should be dumped
87 static void arm_dump_node(FILE *F, const ir_node *n, dump_reason_t reason)
90 case dump_node_opcode_txt:
91 fprintf(F, "%s", get_irn_opname(n));
93 if (arm_has_symconst_attr(n)) {
94 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(n);
95 if (attr->entity != NULL) {
97 fputs(get_entity_name(attr->entity), F);
102 case dump_node_mode_txt:
103 /* mode isn't relevant in the backend */
106 case dump_node_nodeattr_txt:
107 /* TODO: dump shift modifiers */
110 case dump_node_info_txt:
111 arch_dump_reqs_and_registers(F, n);
113 if (has_load_store_attr(n)) {
114 const arm_load_store_attr_t *attr
115 = get_arm_load_store_attr_const(n);
116 ir_fprintf(F, "load_store_mode = %+F\n", attr->load_store_mode);
117 ir_fprintf(F, "entity = %+F\n", attr->entity);
118 fprintf(F, "offset = %ld\n", attr->offset);
119 fprintf(F, "is_frame_entity = %s\n",
120 attr->is_frame_entity ? "yes" : "no");
121 fprintf(F, "entity_sign = %s\n",
122 attr->entity_sign ? "yes" : "no");
124 if (has_shifter_operand(n)) {
125 const arm_shifter_operand_t *attr
126 = get_arm_shifter_operand_attr_const(n);
127 switch (attr->shift_modifier) {
131 fprintf(F, "modifier = imm %d ror %d\n",
132 attr->immediate_value, attr->shift_immediate);
134 case ARM_SHF_ASR_IMM:
135 fprintf(F, "modifier = V >>s %d\n", attr->shift_immediate);
137 case ARM_SHF_ASR_REG:
138 fprintf(F, "modifier = V >>s R\n");
140 case ARM_SHF_LSL_IMM:
141 fprintf(F, "modifier = V << %d\n", attr->shift_immediate);
143 case ARM_SHF_LSL_REG:
144 fprintf(F, "modifier = V << R\n");
146 case ARM_SHF_LSR_IMM:
147 fprintf(F, "modifier = V >> %d\n", attr->shift_immediate);
149 case ARM_SHF_LSR_REG:
150 fprintf(F, "modifier = V >> R\n");
152 case ARM_SHF_ROR_IMM:
153 fprintf(F, "modifier = V ROR %d\n", attr->shift_immediate);
155 case ARM_SHF_ROR_REG:
156 fprintf(F, "modifier = V ROR R\n");
159 fprintf(F, "modifier = RRX\n");
162 case ARM_SHF_INVALID:
163 fprintf(F, "modifier = INVALID SHIFT MODIFIER\n");
167 if (has_cmp_attr(n)) {
168 const arm_cmp_attr_t *attr = get_arm_cmp_attr_const(n);
169 fprintf(F, "cmp_attr =");
170 if (attr->is_unsigned) {
171 fprintf(F, " unsigned");
173 if (attr->ins_permuted) {
174 fprintf(F, " inputs swapped");
178 if (arm_has_symconst_attr(n)) {
179 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(n);
181 fprintf(F, "entity = ");
182 if (attr->entity != NULL) {
183 fprintf(F, "'%s'", get_entity_name(attr->entity));
188 fprintf(F, "frame offset = %d\n", attr->fp_offset);
190 if (has_farith_attr(n)) {
191 const arm_farith_attr_t *attr = get_arm_farith_attr_const(n);
192 ir_fprintf(F, "arithmetic mode = %+F\n", attr->mode);
198 arm_attr_t *get_arm_attr(ir_node *node)
200 assert(is_arm_irn(node) && "need arm node to get attributes");
201 return (arm_attr_t*)get_irn_generic_attr(node);
204 const arm_attr_t *get_arm_attr_const(const ir_node *node)
206 assert(is_arm_irn(node) && "need arm node to get attributes");
207 return (const arm_attr_t*)get_irn_generic_attr_const(node);
210 static bool has_symconst_attr(const ir_node *node)
212 return is_arm_SymConst(node) || is_arm_FrameAddr(node) || is_arm_Bl(node);
215 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node)
217 assert(has_symconst_attr(node));
218 return (arm_SymConst_attr_t*)get_irn_generic_attr(node);
221 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node)
223 assert(has_symconst_attr(node));
224 return (const arm_SymConst_attr_t*)get_irn_generic_attr_const(node);
227 static const arm_fConst_attr_t *get_arm_fConst_attr_const(const ir_node *node)
229 assert(is_arm_fConst(node));
230 return (const arm_fConst_attr_t*)get_irn_generic_attr_const(node);
233 static arm_fConst_attr_t *get_arm_fConst_attr(ir_node *node)
235 assert(is_arm_fConst(node));
236 return (arm_fConst_attr_t*)get_irn_generic_attr(node);
239 arm_farith_attr_t *get_arm_farith_attr(ir_node *node)
241 assert(has_farith_attr(node));
242 return (arm_farith_attr_t*)get_irn_generic_attr(node);
245 const arm_farith_attr_t *get_arm_farith_attr_const(const ir_node *node)
247 assert(has_farith_attr(node));
248 return (const arm_farith_attr_t*)get_irn_generic_attr_const(node);
251 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node)
253 assert(is_arm_B(node));
254 return (arm_CondJmp_attr_t*)get_irn_generic_attr(node);
257 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node)
259 assert(is_arm_B(node));
260 return (const arm_CondJmp_attr_t*)get_irn_generic_attr_const(node);
263 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node)
265 assert(is_arm_SwitchJmp(node));
266 return (arm_SwitchJmp_attr_t*)get_irn_generic_attr(node);
269 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node)
271 assert(is_arm_SwitchJmp(node));
272 return (const arm_SwitchJmp_attr_t*)get_irn_generic_attr_const(node);
275 arm_CopyB_attr_t *get_arm_CopyB_attr(ir_node *node)
277 assert(is_arm_CopyB(node));
278 return (arm_CopyB_attr_t*)get_irn_generic_attr(node);
281 const arm_CopyB_attr_t *get_arm_CopyB_attr_const(const ir_node *node)
283 assert(is_arm_CopyB(node));
284 return (const arm_CopyB_attr_t*)get_irn_generic_attr_const(node);
287 ir_tarval *get_fConst_value(const ir_node *node)
289 const arm_fConst_attr_t *attr = get_arm_fConst_attr_const(node);
293 void set_fConst_value(ir_node *node, ir_tarval *tv)
295 arm_fConst_attr_t *attr = get_arm_fConst_attr(node);
299 ir_relation get_arm_CondJmp_relation(const ir_node *node)
301 const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
302 return attr->relation;
305 void set_arm_CondJmp_relation(ir_node *node, ir_relation relation)
307 arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
308 attr->relation = relation;
311 /* Set the ARM machine node attributes to default values. */
312 static void init_arm_attributes(ir_node *node, arch_irn_flags_t flags,
313 const arch_register_req_t ** in_reqs,
316 ir_graph *irg = get_irn_irg(node);
317 struct obstack *obst = get_irg_obstack(irg);
318 arm_attr_t *attr = get_arm_attr(node);
319 backend_info_t *info;
321 arch_set_irn_flags(node, flags);
322 arch_set_irn_register_reqs_in(node, in_reqs);
323 attr->is_load_store = false;
325 info = be_get_info(node);
326 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
327 memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
330 static void init_arm_load_store_attributes(ir_node *res, ir_mode *ls_mode,
332 int entity_sign, long offset,
333 bool is_frame_entity)
335 arm_load_store_attr_t *attr = get_arm_load_store_attr(res);
336 attr->load_store_mode = ls_mode;
337 attr->entity = entity;
338 attr->entity_sign = entity_sign;
339 attr->is_frame_entity = is_frame_entity;
340 attr->offset = offset;
341 attr->base.is_load_store = true;
344 static void init_arm_shifter_operand(ir_node *res, unsigned immediate_value,
345 arm_shift_modifier_t shift_modifier,
346 unsigned shift_immediate)
348 arm_shifter_operand_t *attr = get_arm_shifter_operand_attr(res);
349 attr->immediate_value = immediate_value;
350 attr->shift_modifier = shift_modifier;
351 attr->shift_immediate = shift_immediate;
354 static void init_arm_cmp_attr(ir_node *res, bool ins_permuted, bool is_unsigned)
356 arm_cmp_attr_t *attr = get_arm_cmp_attr(res);
357 attr->ins_permuted = ins_permuted;
358 attr->is_unsigned = is_unsigned;
361 static void init_arm_SymConst_attributes(ir_node *res, ir_entity *entity,
364 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(res);
365 attr->entity = entity;
366 attr->fp_offset = symconst_offset;
369 static void init_arm_farith_attributes(ir_node *res, ir_mode *mode)
371 arm_farith_attr_t *attr = get_arm_farith_attr(res);
375 static void init_arm_CopyB_attributes(ir_node *res, unsigned size)
377 arm_CopyB_attr_t *attr = get_arm_CopyB_attr(res);
381 static void init_arm_SwitchJmp_attributes(ir_node *res,
382 const ir_switch_table *table)
384 unsigned n_outs = arch_get_irn_n_outs(res);
387 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(res);
390 for (o = 0; o < n_outs; ++o) {
391 arch_set_irn_register_req_out(res, o, arch_no_register_req);
395 static int cmp_attr_arm(const ir_node *a, const ir_node *b)
402 static int cmp_attr_arm_SymConst(const ir_node *a, const ir_node *b)
404 const arm_SymConst_attr_t *attr_a;
405 const arm_SymConst_attr_t *attr_b;
407 if (cmp_attr_arm(a, b))
410 attr_a = get_arm_SymConst_attr_const(a);
411 attr_b = get_arm_SymConst_attr_const(b);
412 return attr_a->entity != attr_b->entity
413 || attr_a->fp_offset != attr_b->fp_offset;
416 static int cmp_attr_arm_CopyB(const ir_node *a, const ir_node *b)
418 const arm_CopyB_attr_t *attr_a;
419 const arm_CopyB_attr_t *attr_b;
421 if (cmp_attr_arm(a, b))
424 attr_a = get_arm_CopyB_attr_const(a);
425 attr_b = get_arm_CopyB_attr_const(b);
426 return attr_a->size != attr_b->size;
429 static int cmp_attr_arm_CondJmp(const ir_node *a, const ir_node *b)
433 /* never identical */
437 static int cmp_attr_arm_SwitchJmp(const ir_node *a, const ir_node *b)
441 /* never identical */
445 static int cmp_attr_arm_fConst(const ir_node *a, const ir_node *b)
447 const arm_fConst_attr_t *attr_a;
448 const arm_fConst_attr_t *attr_b;
450 if (cmp_attr_arm(a, b))
453 attr_a = get_arm_fConst_attr_const(a);
454 attr_b = get_arm_fConst_attr_const(b);
456 return attr_a->tv != attr_b->tv;
460 arm_load_store_attr_t *get_arm_load_store_attr(ir_node *node)
462 return (arm_load_store_attr_t*) get_irn_generic_attr(node);
465 const arm_load_store_attr_t *get_arm_load_store_attr_const(const ir_node *node)
467 return (const arm_load_store_attr_t*) get_irn_generic_attr_const(node);
470 arm_shifter_operand_t *get_arm_shifter_operand_attr(ir_node *node)
472 return (arm_shifter_operand_t*) get_irn_generic_attr(node);
475 const arm_shifter_operand_t *get_arm_shifter_operand_attr_const(
478 return (const arm_shifter_operand_t*) get_irn_generic_attr_const(node);
481 arm_cmp_attr_t *get_arm_cmp_attr(ir_node *node)
483 return (arm_cmp_attr_t*) get_irn_generic_attr(node);
486 const arm_cmp_attr_t *get_arm_cmp_attr_const(const ir_node *node)
488 return (const arm_cmp_attr_t*) get_irn_generic_attr_const(node);
491 static int cmp_attr_arm_load_store(const ir_node *a, const 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_const(a);
500 attr_b = get_arm_load_store_attr_const(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 static int cmp_attr_arm_shifter_operand(const ir_node *a, const ir_node *b)
511 const arm_shifter_operand_t *attr_a;
512 const arm_shifter_operand_t *attr_b;
514 if (cmp_attr_arm(a, b))
517 attr_a = get_arm_shifter_operand_attr_const(a);
518 attr_b = get_arm_shifter_operand_attr_const(b);
519 if (attr_a->shift_modifier != attr_b->shift_modifier
520 || attr_a->immediate_value != attr_b->immediate_value
521 || attr_a->shift_immediate != attr_b->shift_immediate)
527 static int cmp_attr_arm_cmp(const ir_node *a, const ir_node *b)
529 const arm_cmp_attr_t *attr_a;
530 const arm_cmp_attr_t *attr_b;
532 if (cmp_attr_arm(a, b))
535 attr_a = get_arm_cmp_attr_const(a);
536 attr_b = get_arm_cmp_attr_const(b);
537 if (attr_a->ins_permuted != attr_b->ins_permuted
538 || attr_a->is_unsigned != attr_b->is_unsigned)
543 static int cmp_attr_arm_farith(const ir_node *a, const ir_node *b)
545 const arm_farith_attr_t *attr_a;
546 const arm_farith_attr_t *attr_b;
548 if (cmp_attr_arm(a, b))
551 attr_a = get_arm_farith_attr_const(a);
552 attr_b = get_arm_farith_attr_const(b);
553 return attr_a->mode != attr_b->mode;
556 /** copies the ARM attributes of a node. */
557 static void arm_copy_attr(ir_graph *irg, const ir_node *old_node,
560 struct obstack *obst = get_irg_obstack(irg);
561 const arm_attr_t *attr_old = get_arm_attr_const(old_node);
562 arm_attr_t *attr_new = get_arm_attr(new_node);
563 backend_info_t *old_info = be_get_info(old_node);
564 backend_info_t *new_info = be_get_info(new_node);
566 /* copy the attributes */
567 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
570 new_info->flags = old_info->flags;
571 new_info->out_infos =
572 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
573 new_info->in_reqs = old_info->in_reqs;
577 /* Include the generated constructor functions */
578 #include "gen_arm_new_nodes.c.inl"