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 achitecture specific firm
23 * opcodes and the coresponding 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, int inout) {
69 char *dir = inout ? "out" : "in";
70 int max = inout ? get_mips_n_res(n) : get_irn_arity(n);
74 memset(buf, 0, sizeof(buf));
77 for (i = 0; i < max; i++) {
78 fprintf(F, "%sreq #%d =", dir, i);
80 if (reqs[i]->type == arch_register_req_type_none) {
84 if (reqs[i]->type & arch_register_req_type_normal) {
85 fprintf(F, " %s", reqs[i]->cls->name);
88 if (reqs[i]->type & arch_register_req_type_limited) {
90 arch_register_req_format(buf, sizeof(buf), reqs[i], n));
93 if (reqs[i]->type & arch_register_req_type_should_be_same) {
94 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
97 if (reqs[i]->type & arch_register_req_type_should_be_different) {
98 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
106 fprintf(F, "%sreq = N/A\n", dir);
112 * Dumper interface for dumping mips nodes in vcg.
113 * @param n the node to dump
114 * @param F the output file
115 * @param reason indicates which kind of information should be dumped
116 * @return 0 on success or != 0 on failure
118 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
119 ir_mode *mode = NULL;
124 const arch_register_req_t **reqs;
125 const arch_register_t **slots;
128 case dump_node_opcode_txt:
129 fprintf(F, "%s", get_irn_opname(n));
132 case dump_node_mode_txt:
133 mode = get_irn_mode(n);
136 fprintf(F, "[%s]", get_mode_name(mode));
139 fprintf(F, "[?NOMODE?]");
143 case dump_node_nodeattr_txt:
145 /* TODO: dump some attributes which should show up */
146 /* in node name in dump (e.g. consts or the like) */
150 case dump_node_info_txt:
151 attr = get_mips_attr(n);
152 fprintf(F, "=== mips attr begin ===\n");
154 /* dump IN requirements */
155 if (get_irn_arity(n) > 0) {
156 reqs = get_mips_in_req_all(n);
157 dump_reg_req(F, n, reqs, 0);
160 /* dump OUT requirements */
161 if (attr->n_res > 0) {
162 reqs = get_mips_out_req_all(n);
163 dump_reg_req(F, n, reqs, 1);
166 /* dump assigned registers */
167 slots = get_mips_slots(n);
168 if (slots && attr->n_res > 0) {
169 for (i = 0; i < attr->n_res; i++) {
171 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
174 fprintf(F, "reg #%d = n/a\n", i);
181 fprintf(F, "n_res = %d\n", get_mips_n_res(n));
184 fprintf(F, "flags =");
185 if (attr->flags == arch_irn_flags_none) {
189 if (attr->flags & arch_irn_flags_dont_spill) {
190 fprintf(F, " unspillable");
192 if (attr->flags & arch_irn_flags_rematerializable) {
193 fprintf(F, " remat");
195 if (attr->flags & arch_irn_flags_ignore) {
196 fprintf(F, " ignore");
199 fprintf(F, " (%d)\n", attr->flags);
201 if(attr->modes.load_store_mode != NULL) {
202 fprintf(F, " load_store_mode %s\n", get_mode_name(attr->modes.load_store_mode));
204 if(attr->stack_entity != NULL) {
205 fprintf(F, " stack entity %s\n", get_entity_name(attr->stack_entity));
207 if(attr->tv != NULL) {
208 tarval_snprintf(buf, sizeof(buf), attr->tv);
209 fprintf(F, " tarval %s\n", buf);
211 if(attr->symconst_id != NULL) {
212 fprintf(F, " symconst '%s'\n", get_id_str(attr->symconst_id));
215 fprintf(F, "=== mips attr end ===\n");
216 /* end of: case dump_node_info_txt */
226 /***************************************************************************************************
228 * | | | | | | / / | | | | | | | |
229 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
230 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
231 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
232 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
235 ***************************************************************************************************/
238 * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
239 * Firm was made by people hating const :-(
241 mips_attr_t *get_mips_attr(const ir_node *node) {
242 assert(is_mips_irn(node) && "need mips node to get attributes");
243 return (mips_attr_t *)get_irn_generic_attr((ir_node *)node);
247 * Returns the argument register requirements of a mips node.
249 const arch_register_req_t **get_mips_in_req_all(const ir_node *node) {
250 mips_attr_t *attr = get_mips_attr(node);
255 * Returns the result register requirements of an mips node.
257 const arch_register_req_t **get_mips_out_req_all(const ir_node *node) {
258 mips_attr_t *attr = get_mips_attr(node);
259 return attr->out_req;
263 * Returns the argument register requirement at position pos of an mips node.
265 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos) {
266 mips_attr_t *attr = get_mips_attr(node);
267 return attr->in_req[pos];
271 * Returns the result register requirement at position pos of an mips node.
273 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos) {
274 mips_attr_t *attr = get_mips_attr(node);
275 return attr->out_req[pos];
279 * Sets the OUT register requirements at position pos.
281 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
282 mips_attr_t *attr = get_mips_attr(node);
283 attr->out_req[pos] = req;
287 * Sets the IN register requirements at position pos.
289 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
290 mips_attr_t *attr = get_mips_attr(node);
291 attr->in_req[pos] = req;
295 * Returns the register flag of an mips node.
297 arch_irn_flags_t get_mips_flags(const ir_node *node) {
298 mips_attr_t *attr = get_mips_attr(node);
303 * Sets the register flag of an mips node.
305 void set_mips_flags(const ir_node *node, arch_irn_flags_t flags) {
306 mips_attr_t *attr = get_mips_attr(node);
311 * Returns the result register slots of an mips node.
313 const arch_register_t **get_mips_slots(const ir_node *node) {
314 mips_attr_t *attr = get_mips_attr(node);
319 * Returns the name of the OUT register at position pos.
321 const char *get_mips_out_reg_name(const ir_node *node, int pos) {
322 mips_attr_t *attr = get_mips_attr(node);
324 assert(is_mips_irn(node) && "Not an mips node.");
325 assert(pos < attr->n_res && "Invalid OUT position.");
326 assert(attr->slots[pos] && "No register assigned");
328 return arch_register_get_name(attr->slots[pos]);
332 * Returns the index of the OUT register at position pos within its register class.
334 int get_mips_out_regnr(const ir_node *node, int pos) {
335 mips_attr_t *attr = get_mips_attr(node);
337 assert(is_mips_irn(node) && "Not an mips node.");
338 assert(pos < attr->n_res && "Invalid OUT position.");
339 assert(attr->slots[pos] && "No register assigned");
341 return arch_register_get_index(attr->slots[pos]);
345 * Returns the OUT register at position pos.
347 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos) {
348 mips_attr_t *attr = get_mips_attr(node);
350 assert(is_mips_irn(node) && "Not an mips node.");
351 assert(pos < attr->n_res && "Invalid OUT position.");
352 assert(attr->slots[pos] && "No register assigned");
354 return attr->slots[pos];
358 * Sets the number of results.
360 void set_mips_n_res(ir_node *node, int n_res) {
361 mips_attr_t *attr = get_mips_attr(node);
366 * Returns the number of results.
368 int get_mips_n_res(const ir_node *node) {
369 mips_attr_t *attr = get_mips_attr(node);
374 * Initializes the nodes attributes.
376 void init_mips_attributes(ir_node *node, arch_irn_flags_t flags, const arch_register_req_t **in_reqs,
377 const arch_register_req_t **out_reqs,
378 const be_execution_unit_t ***execution_units,
379 int n_res, unsigned latency)
381 mips_attr_t *attr = get_mips_attr(node);
384 attr->out_req = out_reqs;
387 attr->in_req = in_reqs;
389 memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
392 /************************************************************************
394 * |_ _| ___|__ | | __| (_)_ __ __ _
395 * | || |_ / _ \| |/ _` | | '_ \ / _` |
396 * | || _| (_) | | (_| | | | | | (_| |
397 * |___|_| \___/|_|\__,_|_|_| |_|\__, |
399 ************************************************************************/
401 // test if a tarval can be expressed in a 16bit immediate value
402 static int is_tarval_16(ir_node* node)
404 mips_attr_t *attr = get_mips_attr(node);
405 tarval* tv = attr->tv;
406 long val = get_tarval_long(tv);
407 if(get_mode_sign(get_irn_mode(node))) {
408 if(val < -32768 || val > 32767)
411 unsigned long uval = (unsigned long) val;
419 #define MIPS_MAKE_IFOLDING_TRANSFORM(srcnode, inode, commutative) \
420 ir_node *mips_transform_##srcnode(ir_node* node) \
422 ir_node* op1 = get_irn_n(node, 0); \
423 ir_node* op2 = get_irn_n(node, 1); \
425 if(op1 == NULL || op2 == NULL) \
428 if((is_mips_lli(op2) || is_mips_lui(op2)) && is_tarval_16(op2)) { \
429 mips_attr_t *attr, *op_attr = get_mips_attr(op2); \
430 long val = get_tarval_long(op_attr->tv); \
431 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node), \
433 attr = get_mips_attr(result); \
434 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu); \
438 if(commutative && (is_mips_lli(op1) || is_mips_lui(op1)) && is_tarval_16(op1)) { \
439 mips_attr_t *attr, *op_attr = get_mips_attr(op1); \
440 long val = get_tarval_long(op_attr->tv); \
441 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node), \
443 attr = get_mips_attr(result); \
444 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu); \
451 MIPS_MAKE_IFOLDING_TRANSFORM(addu, addiu, 1)
452 MIPS_MAKE_IFOLDING_TRANSFORM(and, andi, 1)
453 MIPS_MAKE_IFOLDING_TRANSFORM(or, ori, 1)
454 MIPS_MAKE_IFOLDING_TRANSFORM(sra, srai, 0)
455 MIPS_MAKE_IFOLDING_TRANSFORM(xor, xori, 1)
456 MIPS_MAKE_IFOLDING_TRANSFORM(sl, sli, 0)
457 MIPS_MAKE_IFOLDING_TRANSFORM(sr, sri, 0)
458 MIPS_MAKE_IFOLDING_TRANSFORM(slt, slti, 0)
460 void mips_init_opcode_transforms(void) {
461 op_mips_addu->ops.transform_node = mips_transform_addu;
462 op_mips_and->ops.transform_node = mips_transform_and;
463 op_mips_or->ops.transform_node = mips_transform_or;
464 op_mips_sra->ops.transform_node = mips_transform_sra;
465 op_mips_xor->ops.transform_node = mips_transform_xor;
466 op_mips_sl->ops.transform_node = mips_transform_sl;
467 op_mips_sr->ops.transform_node = mips_transform_sr;
468 op_mips_slt->ops.transform_node = mips_transform_slt;
471 /***************************************************************************************
474 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
475 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
476 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
477 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
479 ***************************************************************************************/
481 /* Include the generated constructor functions */
482 #include "gen_mips_new_nodes.c.inl"