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");
226 fprintf(F, " (%d)\n", attr->flags);
228 fprintf(F, "=== mips attr end ===\n");
229 /* end of: case dump_node_info_txt */
239 /***************************************************************************************************
241 * | | | | | | / / | | | | | | | |
242 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
243 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
244 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
245 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
248 ***************************************************************************************************/
250 mips_attr_t *get_mips_attr(ir_node *node)
252 assert(is_mips_irn(node) && "need mips node to get attributes");
253 return (mips_attr_t *) get_irn_generic_attr(node);
256 const mips_attr_t *get_mips_attr_const(const ir_node *node)
258 assert(is_mips_irn(node) && "need mips node to get attributes");
259 return get_irn_generic_attr_const(node);
262 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
264 assert(is_mips_irn(node) && "need mips node to get attributes");
265 return get_irn_generic_attr_const(node);
268 const mips_load_store_attr_t *get_mips_load_store_attr_const(
271 assert(is_mips_irn(node) && "need mips node to get attributes");
272 return get_irn_generic_attr_const(node);
276 * Returns the argument register requirements of a mips node.
278 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
280 const mips_attr_t *attr = get_mips_attr_const(node);
285 * Returns the result register requirements of an mips node.
287 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
289 const mips_attr_t *attr = get_mips_attr_const(node);
290 return attr->out_req;
294 * Returns the argument register requirement at position pos of an mips node.
296 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
298 const mips_attr_t *attr = get_mips_attr_const(node);
299 return attr->in_req[pos];
303 * Returns the result register requirement at position pos of an mips node.
305 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
307 const mips_attr_t *attr = get_mips_attr_const(node);
308 return attr->out_req[pos];
312 * Sets the OUT register requirements at position pos.
314 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
316 mips_attr_t *attr = get_mips_attr(node);
317 attr->out_req[pos] = req;
321 * Sets the IN register requirements at position pos.
323 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
325 mips_attr_t *attr = get_mips_attr(node);
326 attr->in_req[pos] = req;
330 * Returns the register flag of an mips node.
332 arch_irn_flags_t get_mips_flags(const ir_node *node)
334 const mips_attr_t *attr = get_mips_attr_const(node);
339 * Sets the register flag of an mips node.
341 void set_mips_flags(ir_node *node, arch_irn_flags_t flags)
343 mips_attr_t *attr = get_mips_attr(node);
348 * Returns the result register slots of an mips node.
350 const arch_register_t **get_mips_slots(const ir_node *node)
352 const mips_attr_t *attr = get_mips_attr_const(node);
357 * Returns the name of the OUT register at position pos.
359 const char *get_mips_out_reg_name(const ir_node *node, int pos)
361 const mips_attr_t *attr = get_mips_attr_const(node);
363 assert(is_mips_irn(node) && "Not an mips node.");
364 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
365 assert(attr->slots[pos] && "No register assigned");
367 return arch_register_get_name(attr->slots[pos]);
371 * Returns the index of the OUT register at position pos within its register class.
373 int get_mips_out_regnr(const ir_node *node, int pos)
375 const mips_attr_t *attr = get_mips_attr_const(node);
377 assert(is_mips_irn(node) && "Not an mips node.");
378 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
379 assert(attr->slots[pos] && "No register assigned");
381 return arch_register_get_index(attr->slots[pos]);
385 * Returns the OUT register at position pos.
387 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos)
389 const mips_attr_t *attr = get_mips_attr_const(node);
391 assert(is_mips_irn(node) && "Not an mips node.");
392 assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
393 assert(attr->slots[pos] && "No register assigned");
395 return attr->slots[pos];
399 * Initializes the nodes attributes.
401 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
402 const arch_register_req_t **in_reqs,
403 const arch_register_req_t **out_reqs,
404 const be_execution_unit_t ***execution_units,
407 ir_graph *irg = get_irn_irg(node);
408 struct obstack *obst = get_irg_obstack(irg);
409 mips_attr_t *attr = get_mips_attr(node);
410 (void) execution_units;
413 attr->out_req = out_reqs;
414 attr->in_req = in_reqs;
416 attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
417 memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
420 static void init_mips_immediate_attributes(ir_node *node,
421 mips_immediate_type_t type,
422 ir_entity *entity, long val)
424 mips_immediate_attr_t *attr = get_irn_generic_attr(node);
426 attr->imm_type = type;
427 attr->entity = entity;
431 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
434 mips_load_store_attr_t *attr = get_irn_generic_attr(node);
435 attr->stack_entity = entity;
436 attr->offset = offset;
439 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
441 const mips_attr_t *a = get_mips_attr_const(node_a);
442 const mips_attr_t *b = get_mips_attr_const(node_b);
444 if(a->flags != b->flags)
446 if(ARR_LEN(a->slots) != ARR_LEN(b->slots))
448 if(a->switch_default_pn != b->switch_default_pn)
454 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
456 const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
457 const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
459 if(a->attr.flags != b->attr.flags)
467 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
469 const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
470 const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
472 if(mips_compare_nodes_attr(node_a, node_b))
474 if(a->stack_entity != b->stack_entity)
476 if(a->offset != b->offset)
482 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
484 ir_graph *irg = get_irn_irg(new_node);
485 struct obstack *obst = get_irg_obstack(irg);
486 const mips_attr_t *attr_old = get_mips_attr_const(old_node);
487 mips_attr_t *attr_new = get_mips_attr(new_node);
489 /* copy the attributes */
490 memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
492 /* copy register assignments */
493 attr_new->slots = DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
496 /***************************************************************************************
499 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
500 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
501 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
502 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
504 ***************************************************************************************/
506 /* Include the generated constructor functions */
507 #include "gen_mips_new_nodes.c.inl"