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 * Dumps the register requirements for either in or out.
51 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs, int inout) {
52 char *dir = inout ? "out" : "in";
53 int max = inout ? get_mips_n_res(n) : get_irn_arity(n);
57 memset(buf, 0, sizeof(buf));
60 for (i = 0; i < max; i++) {
61 fprintf(F, "%sreq #%d =", dir, i);
63 if (reqs[i]->type == arch_register_req_type_none) {
67 if (reqs[i]->type & arch_register_req_type_normal) {
68 fprintf(F, " %s", reqs[i]->cls->name);
71 if (reqs[i]->type & arch_register_req_type_limited) {
73 arch_register_req_format(buf, sizeof(buf), reqs[i], n));
76 if (reqs[i]->type & arch_register_req_type_should_be_same) {
77 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
80 if (reqs[i]->type & arch_register_req_type_should_be_different) {
81 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
89 fprintf(F, "%sreq = N/A\n", dir);
95 * Dumper interface for dumping mips nodes in vcg.
96 * @param n the node to dump
97 * @param F the output file
98 * @param reason indicates which kind of information should be dumped
99 * @return 0 on success or != 0 on failure
101 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
102 ir_mode *mode = NULL;
107 const arch_register_req_t **reqs;
108 const arch_register_t **slots;
111 case dump_node_opcode_txt:
112 fprintf(F, "%s", get_irn_opname(n));
115 case dump_node_mode_txt:
116 mode = get_irn_mode(n);
119 fprintf(F, "[%s]", get_mode_name(mode));
122 fprintf(F, "[?NOMODE?]");
126 case dump_node_nodeattr_txt:
128 /* TODO: dump some attributes which should show up */
129 /* in node name in dump (e.g. consts or the like) */
133 case dump_node_info_txt:
134 attr = get_mips_attr(n);
135 fprintf(F, "=== mips attr begin ===\n");
137 /* dump IN requirements */
138 if (get_irn_arity(n) > 0) {
139 reqs = get_mips_in_req_all(n);
140 dump_reg_req(F, n, reqs, 0);
143 /* dump OUT requirements */
144 if (attr->n_res > 0) {
145 reqs = get_mips_out_req_all(n);
146 dump_reg_req(F, n, reqs, 1);
149 /* dump assigned registers */
150 slots = get_mips_slots(n);
151 if (slots && attr->n_res > 0) {
152 for (i = 0; i < attr->n_res; i++) {
154 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
157 fprintf(F, "reg #%d = n/a\n", i);
164 fprintf(F, "n_res = %d\n", get_mips_n_res(n));
167 fprintf(F, "flags =");
168 if (attr->flags == arch_irn_flags_none) {
172 if (attr->flags & arch_irn_flags_dont_spill) {
173 fprintf(F, " unspillable");
175 if (attr->flags & arch_irn_flags_rematerializable) {
176 fprintf(F, " remat");
178 if (attr->flags & arch_irn_flags_ignore) {
179 fprintf(F, " ignore");
182 fprintf(F, " (%d)\n", attr->flags);
184 if(attr->modes.load_store_mode != NULL) {
185 fprintf(F, " load_store_mode %s\n", get_mode_name(attr->modes.load_store_mode));
187 if(attr->stack_entity != NULL) {
188 fprintf(F, " stack entity %s\n", get_entity_name(attr->stack_entity));
190 if(attr->tv != NULL) {
191 tarval_snprintf(buf, sizeof(buf), attr->tv);
192 fprintf(F, " tarval %s\n", buf);
194 if(attr->symconst_id != NULL) {
195 fprintf(F, " symconst '%s'\n", get_id_str(attr->symconst_id));
198 fprintf(F, "=== mips attr end ===\n");
199 /* end of: case dump_node_info_txt */
209 /***************************************************************************************************
211 * | | | | | | / / | | | | | | | |
212 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
213 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
214 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
215 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
218 ***************************************************************************************************/
221 * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
222 * Firm was made by people hating const :-(
224 mips_attr_t *get_mips_attr(const ir_node *node) {
225 assert(is_mips_irn(node) && "need mips node to get attributes");
226 return (mips_attr_t *)get_irn_generic_attr((ir_node *)node);
230 * Returns the argument register requirements of a mips node.
232 const arch_register_req_t **get_mips_in_req_all(const ir_node *node) {
233 mips_attr_t *attr = get_mips_attr(node);
238 * Returns the result register requirements of an mips node.
240 const arch_register_req_t **get_mips_out_req_all(const ir_node *node) {
241 mips_attr_t *attr = get_mips_attr(node);
242 return attr->out_req;
246 * Returns the argument register requirement at position pos of an mips node.
248 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos) {
249 mips_attr_t *attr = get_mips_attr(node);
250 return attr->in_req[pos];
254 * Returns the result register requirement at position pos of an mips node.
256 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos) {
257 mips_attr_t *attr = get_mips_attr(node);
258 return attr->out_req[pos];
262 * Sets the OUT register requirements at position pos.
264 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
265 mips_attr_t *attr = get_mips_attr(node);
266 attr->out_req[pos] = req;
270 * Sets the IN register requirements at position pos.
272 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
273 mips_attr_t *attr = get_mips_attr(node);
274 attr->in_req[pos] = req;
278 * Returns the register flag of an mips node.
280 arch_irn_flags_t get_mips_flags(const ir_node *node) {
281 mips_attr_t *attr = get_mips_attr(node);
286 * Sets the register flag of an mips node.
288 void set_mips_flags(const ir_node *node, arch_irn_flags_t flags) {
289 mips_attr_t *attr = get_mips_attr(node);
294 * Returns the result register slots of an mips node.
296 const arch_register_t **get_mips_slots(const ir_node *node) {
297 mips_attr_t *attr = get_mips_attr(node);
302 * Returns the name of the OUT register at position pos.
304 const char *get_mips_out_reg_name(const ir_node *node, int pos) {
305 mips_attr_t *attr = get_mips_attr(node);
307 assert(is_mips_irn(node) && "Not an mips node.");
308 assert(pos < attr->n_res && "Invalid OUT position.");
309 assert(attr->slots[pos] && "No register assigned");
311 return arch_register_get_name(attr->slots[pos]);
315 * Returns the index of the OUT register at position pos within its register class.
317 int get_mips_out_regnr(const ir_node *node, int pos) {
318 mips_attr_t *attr = get_mips_attr(node);
320 assert(is_mips_irn(node) && "Not an mips node.");
321 assert(pos < attr->n_res && "Invalid OUT position.");
322 assert(attr->slots[pos] && "No register assigned");
324 return arch_register_get_index(attr->slots[pos]);
328 * Returns the OUT register at position pos.
330 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos) {
331 mips_attr_t *attr = get_mips_attr(node);
333 assert(is_mips_irn(node) && "Not an mips node.");
334 assert(pos < attr->n_res && "Invalid OUT position.");
335 assert(attr->slots[pos] && "No register assigned");
337 return attr->slots[pos];
341 * Sets the number of results.
343 void set_mips_n_res(ir_node *node, int n_res) {
344 mips_attr_t *attr = get_mips_attr(node);
349 * Returns the number of results.
351 int get_mips_n_res(const ir_node *node) {
352 mips_attr_t *attr = get_mips_attr(node);
357 * Initializes the nodes attributes.
359 void init_mips_attributes(ir_node *node, arch_irn_flags_t flags, const arch_register_req_t **in_reqs,
360 const arch_register_req_t **out_reqs,
361 const be_execution_unit_t ***execution_units,
362 int n_res, unsigned latency)
364 mips_attr_t *attr = get_mips_attr(node);
367 attr->out_req = out_reqs;
370 attr->in_req = in_reqs;
372 memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
375 /************************************************************************
377 * |_ _| ___|__ | | __| (_)_ __ __ _
378 * | || |_ / _ \| |/ _` | | '_ \ / _` |
379 * | || _| (_) | | (_| | | | | | (_| |
380 * |___|_| \___/|_|\__,_|_|_| |_|\__, |
382 ************************************************************************/
384 // test if a tarval can be expressed in a 16bit immediate value
385 static int is_tarval_16(ir_node* node)
387 mips_attr_t *attr = get_mips_attr(node);
388 tarval* tv = attr->tv;
389 long val = get_tarval_long(tv);
390 if(get_mode_sign(get_irn_mode(node))) {
391 if(val < -32768 || val > 32767)
394 unsigned long uval = (unsigned long) val;
402 #define MIPS_MAKE_IFOLDING_TRANSFORM(srcnode, inode, commutative) \
403 ir_node *mips_transform_##srcnode(ir_node* node) \
405 ir_node* op1 = get_irn_n(node, 0); \
406 ir_node* op2 = get_irn_n(node, 1); \
408 if(op1 == NULL || op2 == NULL) \
411 if((is_mips_lli(op2) || is_mips_lui(op2)) && is_tarval_16(op2)) { \
412 mips_attr_t *attr, *op_attr = get_mips_attr(op2); \
413 long val = get_tarval_long(op_attr->tv); \
414 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node), \
416 attr = get_mips_attr(result); \
417 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu); \
421 if(commutative && (is_mips_lli(op1) || is_mips_lui(op1)) && is_tarval_16(op1)) { \
422 mips_attr_t *attr, *op_attr = get_mips_attr(op1); \
423 long val = get_tarval_long(op_attr->tv); \
424 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node), \
426 attr = get_mips_attr(result); \
427 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu); \
434 MIPS_MAKE_IFOLDING_TRANSFORM(addu, addiu, 1)
435 MIPS_MAKE_IFOLDING_TRANSFORM(and, andi, 1)
436 MIPS_MAKE_IFOLDING_TRANSFORM(or, ori, 1)
437 MIPS_MAKE_IFOLDING_TRANSFORM(sra, srai, 0)
438 MIPS_MAKE_IFOLDING_TRANSFORM(xor, xori, 1)
439 MIPS_MAKE_IFOLDING_TRANSFORM(sl, sli, 0)
440 MIPS_MAKE_IFOLDING_TRANSFORM(sr, sri, 0)
441 MIPS_MAKE_IFOLDING_TRANSFORM(slt, slti, 0)
443 void mips_init_opcode_transforms(void) {
444 op_mips_addu->ops.transform_node = mips_transform_addu;
445 op_mips_and->ops.transform_node = mips_transform_and;
446 op_mips_or->ops.transform_node = mips_transform_or;
447 op_mips_sra->ops.transform_node = mips_transform_sra;
448 op_mips_xor->ops.transform_node = mips_transform_xor;
449 op_mips_sl->ops.transform_node = mips_transform_sl;
450 op_mips_sr->ops.transform_node = mips_transform_sr;
451 op_mips_slt->ops.transform_node = mips_transform_slt;
454 /***************************************************************************************
457 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
458 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
459 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
460 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
462 ***************************************************************************************/
464 /* Include the generated constructor functions */
465 #include "gen_mips_new_nodes.c.inl"