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
34 #include "irgraph_t.h"
44 #include "../bearch.h"
46 #include "arm_nodes_attr.h"
47 #include "arm_new_nodes.h"
48 #include "arm_optimize.h"
51 #include "bearch_arm_t.h"
53 const char *arm_get_fpa_imm_name(long imm_value)
55 static const char *fpa_imm[] = {
65 return fpa_imm[imm_value];
68 static bool arm_has_symconst_attr(const ir_node *node)
70 return is_arm_SymConst(node) || is_arm_FrameAddr(node) || is_arm_Bl(node);
73 static bool has_load_store_attr(const ir_node *node)
75 return is_arm_Ldr(node) || is_arm_Str(node) || is_arm_LinkLdrPC(node)
76 || is_arm_Ldf(node) || is_arm_Stf(node);
79 static bool has_shifter_operand(const ir_node *node)
81 return is_arm_Add(node) || is_arm_And(node) || is_arm_Or(node)
82 || is_arm_Eor(node) || is_arm_Bic(node) || is_arm_Sub(node)
83 || is_arm_Rsb(node) || is_arm_Mov(node) || is_arm_Mvn(node)
84 || is_arm_Cmp(node) || is_arm_Tst(node) || is_arm_LinkMovPC(node);
87 static bool has_cmp_attr(const ir_node *node)
89 return is_arm_Cmp(node) || is_arm_Tst(node);
92 static bool has_farith_attr(const ir_node *node)
94 return is_arm_Dvf(node) || is_arm_Adf(node);
98 * Dumper interface for dumping arm nodes in vcg.
99 * @param F the output file
100 * @param n the node to dump
101 * @param reason indicates which kind of information should be dumped
103 static void arm_dump_node(FILE *F, ir_node *n, dump_reason_t reason)
106 case dump_node_opcode_txt:
107 fprintf(F, "%s", get_irn_opname(n));
109 if (arm_has_symconst_attr(n)) {
110 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(n);
111 if (attr->entity != NULL) {
113 fputs(get_entity_name(attr->entity), F);
118 case dump_node_mode_txt:
119 /* mode isn't relevant in the backend */
122 case dump_node_nodeattr_txt:
123 /* TODO: dump shift modifiers */
126 case dump_node_info_txt:
127 arch_dump_reqs_and_registers(F, n);
129 if (has_load_store_attr(n)) {
130 const arm_load_store_attr_t *attr
131 = get_arm_load_store_attr_const(n);
132 ir_fprintf(F, "load_store_mode = %+F\n", attr->load_store_mode);
133 ir_fprintf(F, "entity = %+F\n", attr->entity);
134 fprintf(F, "offset = %ld\n", attr->offset);
135 fprintf(F, "is_frame_entity = %s\n",
136 attr->is_frame_entity ? "yes" : "no");
137 fprintf(F, "entity_sign = %s\n",
138 attr->entity_sign ? "yes" : "no");
140 if (has_shifter_operand(n)) {
141 const arm_shifter_operand_t *attr
142 = get_arm_shifter_operand_attr_const(n);
143 switch (attr->shift_modifier) {
147 fprintf(F, "modifier = imm %d ror %d\n",
148 attr->immediate_value, attr->shift_immediate);
150 case ARM_SHF_ASR_IMM:
151 fprintf(F, "modifier = V >>s %d\n", attr->shift_immediate);
153 case ARM_SHF_ASR_REG:
154 fprintf(F, "modifier = V >>s R\n");
156 case ARM_SHF_LSL_IMM:
157 fprintf(F, "modifier = V << %d\n", attr->shift_immediate);
159 case ARM_SHF_LSL_REG:
160 fprintf(F, "modifier = V << R\n");
162 case ARM_SHF_LSR_IMM:
163 fprintf(F, "modifier = V >> %d\n", attr->shift_immediate);
165 case ARM_SHF_LSR_REG:
166 fprintf(F, "modifier = V >> R\n");
168 case ARM_SHF_ROR_IMM:
169 fprintf(F, "modifier = V ROR %d\n", attr->shift_immediate);
171 case ARM_SHF_ROR_REG:
172 fprintf(F, "modifier = V ROR R\n");
175 fprintf(F, "modifier = RRX\n");
178 case ARM_SHF_INVALID:
179 fprintf(F, "modifier = INVALID SHIFT MODIFIER\n");
183 if (has_cmp_attr(n)) {
184 const arm_cmp_attr_t *attr = get_arm_cmp_attr_const(n);
185 fprintf(F, "cmp_attr =");
186 if (attr->is_unsigned) {
187 fprintf(F, " unsigned");
189 if (attr->ins_permuted) {
190 fprintf(F, " inputs swapped");
194 if (arm_has_symconst_attr(n)) {
195 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(n);
197 fprintf(F, "entity = ");
198 if (attr->entity != NULL) {
199 fprintf(F, "'%s'", get_entity_name(attr->entity));
204 fprintf(F, "frame offset = %d\n", attr->fp_offset);
206 if (has_farith_attr(n)) {
207 const arm_farith_attr_t *attr = get_arm_farith_attr_const(n);
208 ir_fprintf(F, "arithmetic mode = %+F\n", attr->mode);
214 arm_attr_t *get_arm_attr(ir_node *node)
216 assert(is_arm_irn(node) && "need arm node to get attributes");
217 return get_irn_generic_attr(node);
220 const arm_attr_t *get_arm_attr_const(const ir_node *node)
222 assert(is_arm_irn(node) && "need arm node to get attributes");
223 return get_irn_generic_attr_const(node);
226 static bool has_symconst_attr(const ir_node *node)
228 return is_arm_SymConst(node) || is_arm_FrameAddr(node) || is_arm_Bl(node);
231 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node)
233 assert(has_symconst_attr(node));
234 return get_irn_generic_attr(node);
237 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node)
239 assert(has_symconst_attr(node));
240 return get_irn_generic_attr_const(node);
243 static const arm_fConst_attr_t *get_arm_fConst_attr_const(const ir_node *node)
245 assert(is_arm_fConst(node));
246 return get_irn_generic_attr_const(node);
249 static arm_fConst_attr_t *get_arm_fConst_attr(ir_node *node)
251 assert(is_arm_fConst(node));
252 return get_irn_generic_attr(node);
255 arm_farith_attr_t *get_arm_farith_attr(ir_node *node)
257 assert(has_farith_attr(node));
258 return get_irn_generic_attr(node);
261 const arm_farith_attr_t *get_arm_farith_attr_const(const ir_node *node)
263 assert(has_farith_attr(node));
264 return get_irn_generic_attr_const(node);
267 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node)
269 assert(is_arm_B(node));
270 return get_irn_generic_attr(node);
273 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node)
275 assert(is_arm_B(node));
276 return get_irn_generic_attr_const(node);
279 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node)
281 assert(is_arm_SwitchJmp(node));
282 return get_irn_generic_attr(node);
285 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node)
287 assert(is_arm_SwitchJmp(node));
288 return get_irn_generic_attr_const(node);
291 void set_arm_in_req_all(ir_node *node, const arch_register_req_t **reqs)
293 arm_attr_t *attr = get_arm_attr(node);
297 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos)
299 const arm_attr_t *attr = get_arm_attr_const(node);
300 return attr->in_req[pos];
303 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos)
305 arm_attr_t *attr = get_arm_attr(node);
306 attr->in_req[pos] = req;
309 tarval *get_fConst_value(const ir_node *node)
311 const arm_fConst_attr_t *attr = get_arm_fConst_attr_const(node);
315 void set_fConst_value(ir_node *node, tarval *tv)
317 arm_fConst_attr_t *attr = get_arm_fConst_attr(node);
321 pn_Cmp get_arm_CondJmp_pnc(const ir_node *node)
323 const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
327 void set_arm_CondJmp_pnc(ir_node *node, pn_Cmp pnc)
329 arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
333 int get_arm_SwitchJmp_n_projs(const ir_node *node)
335 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
336 return attr->n_projs;
339 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs)
341 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
342 attr->n_projs = n_projs;
345 long get_arm_SwitchJmp_default_proj_num(const ir_node *node)
347 const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
348 return attr->default_proj_num;
351 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num)
353 arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
354 attr->default_proj_num = default_proj_num;
357 /* Set the ARM machine node attributes to default values. */
358 static void init_arm_attributes(ir_node *node, int flags,
359 const arch_register_req_t ** in_reqs,
360 const be_execution_unit_t ***execution_units,
363 ir_graph *irg = get_irn_irg(node);
364 struct obstack *obst = get_irg_obstack(irg);
365 arm_attr_t *attr = get_arm_attr(node);
366 backend_info_t *info;
367 (void) execution_units;
369 arch_irn_set_flags(node, flags);
370 attr->in_req = in_reqs;
372 info = be_get_info(node);
373 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
374 memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
377 static void init_arm_load_store_attributes(ir_node *res, ir_mode *ls_mode,
379 int entity_sign, long offset,
380 bool is_frame_entity)
382 arm_load_store_attr_t *attr = get_irn_generic_attr(res);
383 attr->load_store_mode = ls_mode;
384 attr->entity = entity;
385 attr->entity_sign = entity_sign;
386 attr->is_frame_entity = is_frame_entity;
387 attr->offset = offset;
388 attr->base.is_load_store = true;
391 static void init_arm_shifter_operand(ir_node *res, unsigned immediate_value,
392 arm_shift_modifier_t shift_modifier,
393 unsigned shift_immediate)
395 arm_shifter_operand_t *attr = get_irn_generic_attr(res);
396 attr->immediate_value = immediate_value;
397 attr->shift_modifier = shift_modifier;
398 attr->shift_immediate = shift_immediate;
401 static void init_arm_cmp_attr(ir_node *res, bool ins_permuted, bool is_unsigned)
403 arm_cmp_attr_t *attr = get_irn_generic_attr(res);
404 attr->ins_permuted = ins_permuted;
405 attr->is_unsigned = is_unsigned;
408 static void init_arm_SymConst_attributes(ir_node *res, ir_entity *entity,
411 arm_SymConst_attr_t *attr = get_irn_generic_attr(res);
412 attr->entity = entity;
413 attr->fp_offset = symconst_offset;
416 static void init_arm_farith_attributes(ir_node *res, ir_mode *mode)
418 arm_farith_attr_t *attr = get_irn_generic_attr(res);
422 static void init_arm_CopyB_attributes(ir_node *res, unsigned size)
424 arm_CopyB_attr_t *attr = get_irn_generic_attr(res);
428 static int cmp_attr_arm(ir_node *a, ir_node *b)
435 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b)
437 const arm_SymConst_attr_t *attr_a;
438 const arm_SymConst_attr_t *attr_b;
440 if (cmp_attr_arm(a, b))
443 attr_a = get_irn_generic_attr_const(a);
444 attr_b = get_irn_generic_attr_const(b);
445 return attr_a->entity != attr_b->entity
446 || attr_a->fp_offset != attr_b->fp_offset;
449 static int cmp_attr_arm_CopyB(ir_node *a, ir_node *b)
451 const arm_CopyB_attr_t *attr_a;
452 const arm_CopyB_attr_t *attr_b;
454 if (cmp_attr_arm(a, b))
457 attr_a = get_irn_generic_attr_const(a);
458 attr_b = get_irn_generic_attr_const(b);
459 return attr_a->size != attr_b->size;
462 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b)
466 /* never identical */
470 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b)
474 /* never identical */
478 static int cmp_attr_arm_fConst(ir_node *a, ir_node *b)
480 const arm_fConst_attr_t *attr_a;
481 const arm_fConst_attr_t *attr_b;
483 if (cmp_attr_arm(a, b))
486 attr_a = get_arm_fConst_attr_const(a);
487 attr_b = get_arm_fConst_attr_const(b);
489 return attr_a->tv != attr_b->tv;
493 arm_load_store_attr_t *get_arm_load_store_attr(ir_node *node)
495 return (arm_load_store_attr_t*) get_irn_generic_attr(node);
498 const arm_load_store_attr_t *get_arm_load_store_attr_const(const ir_node *node)
500 return (const arm_load_store_attr_t*) get_irn_generic_attr_const(node);
503 arm_shifter_operand_t *get_arm_shifter_operand_attr(ir_node *node)
505 return (arm_shifter_operand_t*) get_irn_generic_attr(node);
508 const arm_shifter_operand_t *get_arm_shifter_operand_attr_const(
511 return (const arm_shifter_operand_t*) get_irn_generic_attr_const(node);
514 arm_cmp_attr_t *get_arm_cmp_attr(ir_node *node)
516 return (arm_cmp_attr_t*) get_irn_generic_attr(node);
519 const arm_cmp_attr_t *get_arm_cmp_attr_const(const ir_node *node)
521 return (const arm_cmp_attr_t*) get_irn_generic_attr_const(node);
524 static int cmp_attr_arm_load_store(ir_node *a, ir_node *b)
526 const arm_load_store_attr_t *attr_a;
527 const arm_load_store_attr_t *attr_b;
529 if (cmp_attr_arm(a, b))
532 attr_a = get_arm_load_store_attr(a);
533 attr_b = get_arm_load_store_attr(b);
534 if (attr_a->entity != attr_b->entity
535 || attr_a->entity_sign != attr_b->entity_sign
536 || attr_a->offset != attr_b->offset)
542 static int cmp_attr_arm_shifter_operand(ir_node *a, ir_node *b)
544 const arm_shifter_operand_t *attr_a;
545 const arm_shifter_operand_t *attr_b;
547 if (cmp_attr_arm(a, b))
550 attr_a = get_arm_shifter_operand_attr(a);
551 attr_b = get_arm_shifter_operand_attr(b);
552 if (attr_a->shift_modifier != attr_b->shift_modifier
553 || attr_a->immediate_value != attr_b->immediate_value
554 || attr_a->shift_immediate != attr_b->shift_immediate)
560 static int cmp_attr_arm_cmp(ir_node *a, ir_node *b)
562 const arm_cmp_attr_t *attr_a;
563 const arm_cmp_attr_t *attr_b;
565 if (cmp_attr_arm(a, b))
568 attr_a = get_irn_generic_attr_const(a);
569 attr_b = get_irn_generic_attr_const(b);
570 if (attr_a->ins_permuted != attr_b->ins_permuted
571 || attr_a->is_unsigned != attr_b->is_unsigned)
576 static int cmp_attr_arm_farith(ir_node *a, ir_node *b)
578 const arm_farith_attr_t *attr_a;
579 const arm_farith_attr_t *attr_b;
581 if (cmp_attr_arm(a, b))
584 attr_a = get_arm_farith_attr_const(a);
585 attr_b = get_arm_farith_attr_const(b);
586 return attr_a->mode != attr_b->mode;
589 /** copies the ARM attributes of a node. */
590 static void arm_copy_attr(ir_graph *irg, const ir_node *old_node,
593 struct obstack *obst = get_irg_obstack(irg);
594 const arm_attr_t *attr_old = get_arm_attr_const(old_node);
595 arm_attr_t *attr_new = get_arm_attr(new_node);
596 backend_info_t *old_info = be_get_info(old_node);
597 backend_info_t *new_info = be_get_info(new_node);
599 /* copy the attributes */
600 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
603 new_info->out_infos =
604 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
608 /* Include the generated constructor functions */
609 #include "gen_arm_new_nodes.c.inl"