2 * This file implements the creation of the achitecture specific firm opcodes
3 * and the coresponding node constructors for the mips assembler irg.
19 #include "irgraph_t.h"
25 #include "firm_common_t.h"
29 #include "../bearch.h"
31 #include "mips_nodes_attr.h"
32 #include "mips_new_nodes.h"
33 #include "gen_mips_regalloc_if.h"
37 /***********************************************************************************
40 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
41 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
42 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
43 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
46 ***********************************************************************************/
49 * Returns a string containing the names of all registers within the limited bitset
51 static char *get_limited_regs(const arch_register_req_t *req, char *buf, int max) {
52 bitset_t *bs = bitset_alloca(req->cls->n_regs);
57 req->limited(NULL, bs);
59 for (i = 0; i < req->cls->n_regs; i++) {
60 if (bitset_is_set(bs, i)) {
61 cnt = snprintf(p, max - size, " %s", req->cls->regs[i].name);
63 fprintf(stderr, "dumper problem, exiting\n");
79 * Dumps the register requirements for either in or out.
81 static void dump_reg_req(FILE *F, ir_node *n, const mips_register_req_t **reqs, int inout) {
82 char *dir = inout ? "out" : "in";
83 int max = inout ? get_mips_n_res(n) : get_irn_arity(n);
84 char *buf = alloca(1024);
90 for (i = 0; i < max; i++) {
91 fprintf(F, "%sreq #%d =", dir, i);
93 if (reqs[i]->req.type == arch_register_req_type_none) {
97 if (reqs[i]->req.type & arch_register_req_type_normal) {
98 fprintf(F, " %s", reqs[i]->req.cls->name);
101 if (reqs[i]->req.type & arch_register_req_type_limited) {
102 fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
105 if (reqs[i]->req.type & arch_register_req_type_should_be_same) {
106 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->same_pos));
109 if (reqs[i]->req.type & arch_register_req_type_should_be_different) {
110 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->different_pos));
119 fprintf(F, "%sreq = N/A\n", dir);
125 * Dumper interface for dumping mips nodes in vcg.
126 * @param n the node to dump
127 * @param F the output file
128 * @param reason indicates which kind of information should be dumped
129 * @return 0 on success or != 0 on failure
131 static int dump_node_mips(ir_node *n, FILE *F, dump_reason_t reason) {
132 ir_mode *mode = NULL;
137 const mips_register_req_t **reqs;
138 const arch_register_t **slots;
141 case dump_node_opcode_txt:
142 fprintf(F, "%s", get_irn_opname(n));
145 case dump_node_mode_txt:
146 mode = get_irn_mode(n);
149 fprintf(F, "[%s]", get_mode_name(mode));
152 fprintf(F, "[?NOMODE?]");
156 case dump_node_nodeattr_txt:
158 /* TODO: dump some attributes which should show up */
159 /* in node name in dump (e.g. consts or the like) */
163 case dump_node_info_txt:
164 attr = get_mips_attr(n);
165 fprintf(F, "=== mips attr begin ===\n");
167 /* dump IN requirements */
168 if (get_irn_arity(n) > 0) {
169 reqs = get_mips_in_req_all(n);
170 dump_reg_req(F, n, reqs, 0);
173 /* dump OUT requirements */
174 if (attr->n_res > 0) {
175 reqs = get_mips_out_req_all(n);
176 dump_reg_req(F, n, reqs, 1);
179 /* dump assigned registers */
180 slots = get_mips_slots(n);
181 if (slots && attr->n_res > 0) {
182 for (i = 0; i < attr->n_res; i++) {
184 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
187 fprintf(F, "reg #%d = n/a\n", i);
194 fprintf(F, "n_res = %d\n", get_mips_n_res(n));
197 fprintf(F, "flags =");
198 if (attr->flags == arch_irn_flags_none) {
202 if (attr->flags & arch_irn_flags_dont_spill) {
203 fprintf(F, " unspillable");
205 if (attr->flags & arch_irn_flags_rematerializable) {
206 fprintf(F, " remat");
208 if (attr->flags & arch_irn_flags_ignore) {
209 fprintf(F, " ignore");
212 fprintf(F, " (%d)\n", attr->flags);
214 if(attr->modes.load_store_mode != NULL) {
215 fprintf(F, " load_store_mode %s\n", get_mode_name(attr->modes.load_store_mode));
217 if(attr->stack_entity != NULL) {
218 fprintf(F, " stack entity %s\n", get_entity_name(attr->stack_entity));
220 if(attr->tv != NULL) {
221 tarval_snprintf(buf, sizeof(buf), attr->tv);
222 fprintf(F, " tarval %s\n", buf);
224 if(attr->symconst_id != NULL) {
225 fprintf(F, " symconst '%s'\n", get_id_str(attr->symconst_id));
228 fprintf(F, "=== mips attr end ===\n");
229 /* end of: case dump_node_info_txt */
239 /***************************************************************************************************
241 * | | | | | | / / | | | | | | | |
242 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
243 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
244 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
245 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
248 ***************************************************************************************************/
251 * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
252 * Firm was made by people hating const :-(
254 mips_attr_t *get_mips_attr(const ir_node *node) {
255 assert(is_mips_irn(node) && "need mips node to get attributes");
256 return (mips_attr_t *)get_irn_generic_attr((ir_node *)node);
260 * Returns the argument register requirements of a mips node.
262 const mips_register_req_t **get_mips_in_req_all(const ir_node *node) {
263 mips_attr_t *attr = get_mips_attr(node);
268 * Returns the result register requirements of an mips node.
270 const mips_register_req_t **get_mips_out_req_all(const ir_node *node) {
271 mips_attr_t *attr = get_mips_attr(node);
272 return attr->out_req;
276 * Returns the argument register requirement at position pos of an mips node.
278 const mips_register_req_t *get_mips_in_req(const ir_node *node, int pos) {
279 mips_attr_t *attr = get_mips_attr(node);
280 return attr->in_req[pos];
284 * Returns the result register requirement at position pos of an mips node.
286 const mips_register_req_t *get_mips_out_req(const ir_node *node, int pos) {
287 mips_attr_t *attr = get_mips_attr(node);
288 return attr->out_req[pos];
292 * Sets the OUT register requirements at position pos.
294 void set_mips_req_out(ir_node *node, const mips_register_req_t *req, int pos) {
295 mips_attr_t *attr = get_mips_attr(node);
296 attr->out_req[pos] = req;
300 * Sets the IN register requirements at position pos.
302 void set_mips_req_in(ir_node *node, const mips_register_req_t *req, int pos) {
303 mips_attr_t *attr = get_mips_attr(node);
304 attr->in_req[pos] = req;
308 * Returns the register flag of an mips node.
310 arch_irn_flags_t get_mips_flags(const ir_node *node) {
311 mips_attr_t *attr = get_mips_attr(node);
316 * Sets the register flag of an mips node.
318 void set_mips_flags(const ir_node *node, arch_irn_flags_t flags) {
319 mips_attr_t *attr = get_mips_attr(node);
324 * Returns the result register slots of an mips node.
326 const arch_register_t **get_mips_slots(const ir_node *node) {
327 mips_attr_t *attr = get_mips_attr(node);
332 * Returns the name of the OUT register at position pos.
334 const char *get_mips_out_reg_name(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_name(attr->slots[pos]);
345 * Returns the index of the OUT register at position pos within its register class.
347 int get_mips_out_regnr(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 arch_register_get_index(attr->slots[pos]);
358 * Returns the OUT register at position pos.
360 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos) {
361 mips_attr_t *attr = get_mips_attr(node);
363 assert(is_mips_irn(node) && "Not an mips node.");
364 assert(pos < attr->n_res && "Invalid OUT position.");
365 assert(attr->slots[pos] && "No register assigned");
367 return attr->slots[pos];
371 * Sets the number of results.
373 void set_mips_n_res(ir_node *node, int n_res) {
374 mips_attr_t *attr = get_mips_attr(node);
379 * Returns the number of results.
381 int get_mips_n_res(const ir_node *node) {
382 mips_attr_t *attr = get_mips_attr(node);
387 * Initializes the nodes attributes.
389 void init_mips_attributes(ir_node *node, arch_irn_flags_t flags, const mips_register_req_t **in_reqs,
390 const mips_register_req_t **out_reqs, int n_res)
392 mips_attr_t *attr = get_mips_attr(node);
395 attr->out_req = out_reqs;
399 attr->slots = xcalloc(n_res, sizeof(attr->slots[0]));
403 attr->in_req = in_reqs;
406 /************************************************************************
408 * |_ _| ___|__ | | __| (_)_ __ __ _
409 * | || |_ / _ \| |/ _` | | '_ \ / _` |
410 * | || _| (_) | | (_| | | | | | (_| |
411 * |___|_| \___/|_|\__,_|_|_| |_|\__, |
413 ************************************************************************/
415 // test if a tarval can be expressed in a 16bit immediate value
416 static int is_tarval_16(ir_node* node)
418 mips_attr_t *attr = get_mips_attr(node);
419 tarval* tv = attr->tv;
420 long val = get_tarval_long(tv);
421 if(get_mode_sign(get_irn_mode(node))) {
422 if(val < -32768 || val > 32767)
425 unsigned long uval = (unsigned long) val;
433 #define MIPS_MAKE_IFOLDING_TRANSFORM(srcnode, inode, commutative) \
434 ir_node *mips_transform_##srcnode(ir_node* node) \
436 ir_node* op1 = get_irn_n(node, 0); \
437 ir_node* op2 = get_irn_n(node, 1); \
439 if(op1 == NULL || op2 == NULL) \
442 if((is_mips_lli(op2) || is_mips_lui(op2)) && is_tarval_16(op2)) { \
443 mips_attr_t *attr, *op_attr = get_mips_attr(op2); \
444 long val = get_tarval_long(op_attr->tv); \
445 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node), \
446 op1, get_irn_mode(node)); \
447 attr = get_mips_attr(result); \
448 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu); \
452 if(commutative && (is_mips_lli(op1) || is_mips_lui(op1)) && is_tarval_16(op1)) { \
453 mips_attr_t *attr, *op_attr = get_mips_attr(op1); \
454 long val = get_tarval_long(op_attr->tv); \
455 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node), \
456 op2, get_irn_mode(node)); \
457 attr = get_mips_attr(result); \
458 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu); \
465 MIPS_MAKE_IFOLDING_TRANSFORM(add, addi, 1);
466 MIPS_MAKE_IFOLDING_TRANSFORM(and, andi, 1);
467 MIPS_MAKE_IFOLDING_TRANSFORM(or, ori, 1);
468 MIPS_MAKE_IFOLDING_TRANSFORM(sra, srai, 0);
469 MIPS_MAKE_IFOLDING_TRANSFORM(xor, xori, 1);
470 MIPS_MAKE_IFOLDING_TRANSFORM(sl, sli, 0);
471 MIPS_MAKE_IFOLDING_TRANSFORM(sr, sri, 0);
472 MIPS_MAKE_IFOLDING_TRANSFORM(slt, slti, 0);
474 void mips_init_opcode_transforms() {
475 op_mips_add->ops.transform_node = mips_transform_add;
476 op_mips_and->ops.transform_node = mips_transform_and;
477 op_mips_or->ops.transform_node = mips_transform_or;
478 op_mips_sra->ops.transform_node = mips_transform_sra;
479 op_mips_xor->ops.transform_node = mips_transform_xor;
480 op_mips_sl->ops.transform_node = mips_transform_sl;
481 op_mips_sr->ops.transform_node = mips_transform_sr;
482 op_mips_slt->ops.transform_node = mips_transform_slt;
485 /***************************************************************************************
488 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
489 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
490 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
491 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
493 ***************************************************************************************/
495 static void mips_register_additional_opcodes(int n) {
496 /* we don't need any additional opcodes */
499 /* Include the generated constructor functions */
500 #include "gen_mips_new_nodes.c.inl"