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 MIPS
25 * @author Matthias Braun, Mehdi
33 #include "irgraph_t.h"
39 #include "firm_common_t.h"
44 #include "../bearch_t.h"
46 #include "mips_nodes_attr.h"
47 #include "mips_new_nodes.h"
48 #include "gen_mips_regalloc_if.h"
52 /***********************************************************************************
55 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
56 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
57 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
58 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
61 ***********************************************************************************/
64 * Dumps the register requirements for either in or out.
66 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
69 const mips_attr_t *attr = get_mips_attr_const(n);
70 char *dir = inout ? "out" : "in";
71 int max = inout ? ARR_LEN(attr->slots) : get_irn_arity(n);
75 memset(buf, 0, sizeof(buf));
78 for (i = 0; i < max; i++) {
79 fprintf(F, "%sreq #%d =", dir, i);
81 if (reqs[i]->type == arch_register_req_type_none) {
85 if (reqs[i]->type & arch_register_req_type_normal) {
86 fprintf(F, " %s", reqs[i]->cls->name);
89 if (reqs[i]->type & arch_register_req_type_limited) {
91 arch_register_req_format(buf, sizeof(buf), reqs[i], n));
94 if (reqs[i]->type & arch_register_req_type_should_be_same) {
95 const unsigned other = reqs[i]->other_same;
98 ir_fprintf(F, " same as");
99 for (i = 0; 1U << i <= other; ++i) {
100 if (other & (1U << i)) {
101 ir_fprintf(F, " %+F", i);
106 if (reqs[i]->type & arch_register_req_type_must_be_different) {
107 const unsigned other = reqs[i]->other_different;
110 ir_fprintf(F, " different from");
111 for (i = 0; 1U << i <= other; ++i) {
112 if (other & (1U << i)) {
113 ir_fprintf(F, " %+F", i);
123 fprintf(F, "%sreq = N/A\n", dir);
129 * Dumper interface for dumping mips nodes in vcg.
130 * @param n the node to dump
131 * @param F the output file
132 * @param reason indicates which kind of information should be dumped
133 * @return 0 on success or != 0 on failure
135 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
138 const mips_attr_t *attr = get_mips_attr_const(n);
139 const arch_register_req_t **reqs;
140 const arch_register_t **slots;
143 case dump_node_opcode_txt:
144 fprintf(F, "%s", get_irn_opname(n));
147 case dump_node_mode_txt:
150 case dump_node_nodeattr_txt:
152 if(is_mips_Immediate(n)) {
153 const mips_immediate_attr_t *attr
154 = get_mips_immediate_attr_const(n);
155 switch(attr->imm_type) {
157 fprintf(F, " %ld ", attr->val);
159 case MIPS_IMM_SYMCONST_LO:
160 fprintf(F, " lo(%s", get_entity_ld_name(attr->entity));
162 fprintf(F, "%+ld", attr->val);
166 case MIPS_IMM_SYMCONST_HI:
167 fprintf(F, " hi(%s", get_entity_ld_name(attr->entity));
169 fprintf(F, "%+ld", attr->val);
174 fprintf(F, " INVALID ");
180 case dump_node_info_txt:
181 attr = get_mips_attr(n);
182 fprintf(F, "=== mips attr begin ===\n");
184 /* dump IN requirements */
185 if (get_irn_arity(n) > 0) {
186 reqs = get_mips_in_req_all(n);
187 dump_reg_req(F, n, reqs, 0);
190 /* dump OUT requirements */
191 if (ARR_LEN(attr->slots) > 0) {
192 reqs = get_mips_out_req_all(n);
193 dump_reg_req(F, n, reqs, 1);
196 /* dump assigned registers */
197 slots = get_mips_slots(n);
198 if (slots && ARR_LEN(attr->slots) > 0) {
199 for (i = 0; i < ARR_LEN(attr->slots); i++) {
201 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
204 fprintf(F, "reg #%d = n/a\n", i);
211 fprintf(F, "n_res = %d\n", ARR_LEN(attr->slots));
214 fprintf(F, "flags =");
215 if (attr->flags == arch_irn_flags_none) {
219 if (attr->flags & arch_irn_flags_dont_spill) {
220 fprintf(F, " unspillable");
222 if (attr->flags & arch_irn_flags_rematerializable) {
223 fprintf(F, " remat");
225 if (attr->flags & arch_irn_flags_ignore) {
226 fprintf(F, " ignore");
229 fprintf(F, " (%d)\n", attr->flags);
231 fprintf(F, "=== mips attr end ===\n");
232 /* end of: case dump_node_info_txt */
242 /***************************************************************************************************
244 * | | | | | | / / | | | | | | | |
245 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
246 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
247 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
248 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
251 ***************************************************************************************************/
253 mips_attr_t *get_mips_attr(ir_node *node)
255 assert(is_mips_irn(node) && "need mips node to get attributes");
256 return (mips_attr_t *) get_irn_generic_attr(node);
259 const mips_attr_t *get_mips_attr_const(const ir_node *node)
261 assert(is_mips_irn(node) && "need mips node to get attributes");
262 return get_irn_generic_attr_const(node);
265 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
267 assert(is_mips_irn(node) && "need mips node to get attributes");
268 return get_irn_generic_attr_const(node);
271 const mips_load_store_attr_t *get_mips_load_store_attr_const(
274 assert(is_mips_irn(node) && "need mips node to get attributes");
275 return get_irn_generic_attr_const(node);
279 * Returns the argument register requirements of a mips node.
281 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
283 const mips_attr_t *attr = get_mips_attr_const(node);
288 * Returns the result register requirements of an mips node.
290 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
292 const mips_attr_t *attr = get_mips_attr_const(node);
293 return attr->out_req;
297 * Returns the argument register requirement at position pos of an mips node.
299 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
301 const mips_attr_t *attr = get_mips_attr_const(node);
302 return attr->in_req[pos];
306 * Returns the result register requirement at position pos of an mips node.
308 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
310 const mips_attr_t *attr = get_mips_attr_const(node);
311 return attr->out_req[pos];
315 * Sets the OUT register requirements at position pos.
317 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
319 mips_attr_t *attr = get_mips_attr(node);
320 attr->out_req[pos] = req;
324 * Sets the IN register requirements at position pos.
326 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
328 mips_attr_t *attr = get_mips_attr(node);
329 attr->in_req[pos] = req;
333 * Returns the register flag of an mips node.
335 arch_irn_flags_t get_mips_flags(const ir_node *node)
337 const mips_attr_t *attr = get_mips_attr_const(node);
342 * Sets the register flag of an mips node.
344 void set_mips_flags(ir_node *node, arch_irn_flags_t flags)
346 mips_attr_t *attr = get_mips_attr(node);
351 * Returns the result register slots of an mips node.
353 const arch_register_t **get_mips_slots(const ir_node *node)
355 const mips_attr_t *attr = get_mips_attr_const(node);
360 * Returns the name of the OUT register at position pos.
362 const char *get_mips_out_reg_name(const ir_node *node, int pos)
364 const mips_attr_t *attr = get_mips_attr_const(node);
366 assert(is_mips_irn(node) && "Not an mips node.");
367 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
368 assert(attr->slots[pos] && "No register assigned");
370 return arch_register_get_name(attr->slots[pos]);
374 * Returns the index of the OUT register at position pos within its register class.
376 int get_mips_out_regnr(const ir_node *node, int pos)
378 const mips_attr_t *attr = get_mips_attr_const(node);
380 assert(is_mips_irn(node) && "Not an mips node.");
381 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
382 assert(attr->slots[pos] && "No register assigned");
384 return arch_register_get_index(attr->slots[pos]);
388 * Returns the OUT register at position pos.
390 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos)
392 const mips_attr_t *attr = get_mips_attr_const(node);
394 assert(is_mips_irn(node) && "Not an mips node.");
395 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
396 assert(attr->slots[pos] && "No register assigned");
398 return attr->slots[pos];
402 * Initializes the nodes attributes.
404 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
405 const arch_register_req_t **in_reqs,
406 const arch_register_req_t **out_reqs,
407 const be_execution_unit_t ***execution_units,
410 ir_graph *irg = get_irn_irg(node);
411 struct obstack *obst = get_irg_obstack(irg);
412 mips_attr_t *attr = get_mips_attr(node);
413 (void) execution_units;
416 attr->out_req = out_reqs;
417 attr->in_req = in_reqs;
419 attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
420 memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
423 static void init_mips_immediate_attributes(ir_node *node,
424 mips_immediate_type_t type,
425 ir_entity *entity, long val)
427 mips_immediate_attr_t *attr = get_irn_generic_attr(node);
429 attr->imm_type = type;
430 attr->entity = entity;
434 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
437 mips_load_store_attr_t *attr = get_irn_generic_attr(node);
438 attr->stack_entity = entity;
439 attr->offset = offset;
442 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
444 const mips_attr_t *a = get_mips_attr_const(node_a);
445 const mips_attr_t *b = get_mips_attr_const(node_b);
447 if(a->flags != b->flags)
449 if(ARR_LEN(a->slots) != ARR_LEN(b->slots))
451 if(a->switch_default_pn != b->switch_default_pn)
457 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
459 const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
460 const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
462 if(a->attr.flags != b->attr.flags)
470 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
472 const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
473 const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
475 if(mips_compare_nodes_attr(node_a, node_b))
477 if(a->stack_entity != b->stack_entity)
479 if(a->offset != b->offset)
485 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
487 ir_graph *irg = get_irn_irg(new_node);
488 struct obstack *obst = get_irg_obstack(irg);
489 const mips_attr_t *attr_old = get_mips_attr_const(old_node);
490 mips_attr_t *attr_new = get_mips_attr(new_node);
492 /* copy the attributes */
493 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
495 /* copy register assignments */
496 attr_new->slots = DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
499 /***************************************************************************************
502 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
503 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
504 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
505 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
507 ***************************************************************************************/
509 /* Include the generated constructor functions */
510 #include "gen_mips_new_nodes.c.inl"