2 * Copyright (C) 1995-2007 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 MIPS
25 * @author Matthias Braun, Mehdi
35 #include "irgraph_t.h"
41 #include "firm_common_t.h"
46 #include "../bearch_t.h"
48 #include "mips_nodes_attr.h"
49 #include "mips_new_nodes.h"
50 #include "gen_mips_regalloc_if.h"
54 /***********************************************************************************
57 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
58 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
59 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
60 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
63 ***********************************************************************************/
66 * Dumps the register requirements for either in or out.
68 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
71 const mips_attr_t *attr = get_mips_attr_const(n);
72 char *dir = inout ? "out" : "in";
73 int max = inout ? ARR_LEN(attr->slots) : get_irn_arity(n);
77 memset(buf, 0, sizeof(buf));
80 for (i = 0; i < max; i++) {
81 fprintf(F, "%sreq #%d =", dir, i);
83 if (reqs[i]->type == arch_register_req_type_none) {
87 if (reqs[i]->type & arch_register_req_type_normal) {
88 fprintf(F, " %s", reqs[i]->cls->name);
91 if (reqs[i]->type & arch_register_req_type_limited) {
93 arch_register_req_format(buf, sizeof(buf), reqs[i], n));
96 if (reqs[i]->type & arch_register_req_type_should_be_same) {
97 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same[0]));
98 if (reqs[i]->other_same[1] != -1)
99 ir_fprintf(F, " or %+F", get_irn_n(n, reqs[i]->other_same[1]));
102 if (reqs[i]->type & arch_register_req_type_should_be_different) {
103 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
111 fprintf(F, "%sreq = N/A\n", dir);
117 * Dumper interface for dumping mips nodes in vcg.
118 * @param n the node to dump
119 * @param F the output file
120 * @param reason indicates which kind of information should be dumped
121 * @return 0 on success or != 0 on failure
123 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
126 const mips_attr_t *attr = get_mips_attr_const(n);
127 const arch_register_req_t **reqs;
128 const arch_register_t **slots;
131 case dump_node_opcode_txt:
132 fprintf(F, "%s", get_irn_opname(n));
135 case dump_node_mode_txt:
138 case dump_node_nodeattr_txt:
140 if(is_mips_Immediate(n)) {
141 const mips_immediate_attr_t *attr
142 = get_mips_immediate_attr_const(n);
143 switch(attr->imm_type) {
145 fprintf(F, " %ld ", attr->val);
147 case MIPS_IMM_SYMCONST_LO:
148 fprintf(F, " lo(%s", get_entity_ld_name(attr->entity));
150 fprintf(F, "%+ld", attr->val);
154 case MIPS_IMM_SYMCONST_HI:
155 fprintf(F, " hi(%s", get_entity_ld_name(attr->entity));
157 fprintf(F, "%+ld", attr->val);
162 fprintf(F, " INVALID ");
168 case dump_node_info_txt:
169 attr = get_mips_attr(n);
170 fprintf(F, "=== mips attr begin ===\n");
172 /* dump IN requirements */
173 if (get_irn_arity(n) > 0) {
174 reqs = get_mips_in_req_all(n);
175 dump_reg_req(F, n, reqs, 0);
178 /* dump OUT requirements */
179 if (ARR_LEN(attr->slots) > 0) {
180 reqs = get_mips_out_req_all(n);
181 dump_reg_req(F, n, reqs, 1);
184 /* dump assigned registers */
185 slots = get_mips_slots(n);
186 if (slots && ARR_LEN(attr->slots) > 0) {
187 for (i = 0; i < ARR_LEN(attr->slots); i++) {
189 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
192 fprintf(F, "reg #%d = n/a\n", i);
199 fprintf(F, "n_res = %d\n", ARR_LEN(attr->slots));
202 fprintf(F, "flags =");
203 if (attr->flags == arch_irn_flags_none) {
207 if (attr->flags & arch_irn_flags_dont_spill) {
208 fprintf(F, " unspillable");
210 if (attr->flags & arch_irn_flags_rematerializable) {
211 fprintf(F, " remat");
213 if (attr->flags & arch_irn_flags_ignore) {
214 fprintf(F, " ignore");
217 fprintf(F, " (%d)\n", attr->flags);
219 fprintf(F, "=== mips attr end ===\n");
220 /* end of: case dump_node_info_txt */
230 /***************************************************************************************************
232 * | | | | | | / / | | | | | | | |
233 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
234 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
235 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
236 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
239 ***************************************************************************************************/
241 mips_attr_t *get_mips_attr(ir_node *node)
243 assert(is_mips_irn(node) && "need mips node to get attributes");
244 return (mips_attr_t *) get_irn_generic_attr(node);
247 const mips_attr_t *get_mips_attr_const(const ir_node *node)
249 assert(is_mips_irn(node) && "need mips node to get attributes");
250 return get_irn_generic_attr_const(node);
253 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
255 assert(is_mips_irn(node) && "need mips node to get attributes");
256 return get_irn_generic_attr_const(node);
259 const mips_load_store_attr_t *get_mips_load_store_attr_const(
262 assert(is_mips_irn(node) && "need mips node to get attributes");
263 return get_irn_generic_attr_const(node);
267 * Returns the argument register requirements of a mips node.
269 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
271 const mips_attr_t *attr = get_mips_attr_const(node);
276 * Returns the result register requirements of an mips node.
278 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
280 const mips_attr_t *attr = get_mips_attr_const(node);
281 return attr->out_req;
285 * Returns the argument register requirement at position pos of an mips node.
287 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
289 const mips_attr_t *attr = get_mips_attr_const(node);
290 return attr->in_req[pos];
294 * Returns the result register requirement at position pos of an mips node.
296 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
298 const mips_attr_t *attr = get_mips_attr_const(node);
299 return attr->out_req[pos];
303 * Sets the OUT register requirements at position pos.
305 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
307 mips_attr_t *attr = get_mips_attr(node);
308 attr->out_req[pos] = req;
312 * Sets the IN register requirements at position pos.
314 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
316 mips_attr_t *attr = get_mips_attr(node);
317 attr->in_req[pos] = req;
321 * Returns the register flag of an mips node.
323 arch_irn_flags_t get_mips_flags(const ir_node *node)
325 const mips_attr_t *attr = get_mips_attr_const(node);
330 * Sets the register flag of an mips node.
332 void set_mips_flags(ir_node *node, arch_irn_flags_t flags)
334 mips_attr_t *attr = get_mips_attr(node);
339 * Returns the result register slots of an mips node.
341 const arch_register_t **get_mips_slots(const ir_node *node)
343 const mips_attr_t *attr = get_mips_attr_const(node);
348 * Returns the name of the OUT register at position pos.
350 const char *get_mips_out_reg_name(const ir_node *node, int pos)
352 const mips_attr_t *attr = get_mips_attr_const(node);
354 assert(is_mips_irn(node) && "Not an mips node.");
355 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
356 assert(attr->slots[pos] && "No register assigned");
358 return arch_register_get_name(attr->slots[pos]);
362 * Returns the index of the OUT register at position pos within its register class.
364 int get_mips_out_regnr(const ir_node *node, int pos)
366 const mips_attr_t *attr = get_mips_attr_const(node);
368 assert(is_mips_irn(node) && "Not an mips node.");
369 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
370 assert(attr->slots[pos] && "No register assigned");
372 return arch_register_get_index(attr->slots[pos]);
376 * Returns the OUT register at position pos.
378 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos)
380 const mips_attr_t *attr = get_mips_attr_const(node);
382 assert(is_mips_irn(node) && "Not an mips node.");
383 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
384 assert(attr->slots[pos] && "No register assigned");
386 return attr->slots[pos];
390 * Initializes the nodes attributes.
392 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
393 const arch_register_req_t **in_reqs,
394 const arch_register_req_t **out_reqs,
395 const be_execution_unit_t ***execution_units,
398 ir_graph *irg = get_irn_irg(node);
399 struct obstack *obst = get_irg_obstack(irg);
400 mips_attr_t *attr = get_mips_attr(node);
401 (void) execution_units;
404 attr->out_req = out_reqs;
405 attr->in_req = in_reqs;
407 attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
408 memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
411 static void init_mips_immediate_attributes(ir_node *node,
412 mips_immediate_type_t type,
413 ir_entity *entity, long val)
415 mips_immediate_attr_t *attr = get_irn_generic_attr(node);
417 attr->imm_type = type;
418 attr->entity = entity;
422 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
425 mips_load_store_attr_t *attr = get_irn_generic_attr(node);
426 attr->stack_entity = entity;
427 attr->offset = offset;
430 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
432 const mips_attr_t *a = get_mips_attr_const(node_a);
433 const mips_attr_t *b = get_mips_attr_const(node_b);
435 if(a->flags != b->flags)
437 if(ARR_LEN(a->slots) != ARR_LEN(b->slots))
439 if(a->switch_default_pn != b->switch_default_pn)
445 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
447 const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
448 const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
450 if(a->attr.flags != b->attr.flags)
458 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
460 const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
461 const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
463 if(mips_compare_nodes_attr(node_a, node_b))
465 if(a->stack_entity != b->stack_entity)
467 if(a->offset != b->offset)
473 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
475 ir_graph *irg = get_irn_irg(new_node);
476 struct obstack *obst = get_irg_obstack(irg);
477 const mips_attr_t *attr_old = get_mips_attr_const(old_node);
478 mips_attr_t *attr_new = get_mips_attr(new_node);
480 /* copy the attributes */
481 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
483 /* copy register assignments */
484 attr_new->slots = DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
487 /***************************************************************************************
490 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
491 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
492 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
493 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
495 ***************************************************************************************/
497 /* Include the generated constructor functions */
498 #include "gen_mips_new_nodes.c.inl"