2 * This file implements the creation of the achitecture specific firm opcodes
3 * and the coresponding node constructors for the $arch assembler irg.
4 * @author Christian Wuerdig
21 #include "irgraph_t.h"
27 #include "firm_common_t.h"
31 #include "../bearch.h"
33 #include "ia32_nodes_attr.h"
34 #include "ia32_new_nodes.h"
35 #include "gen_ia32_regalloc_if.h"
37 #ifdef obstack_chunk_alloc
38 # undef obstack_chunk_alloc
39 # define obstack_chunk_alloc xmalloc
41 # define obstack_chunk_alloc xmalloc
42 # define obstack_chunk_free free
45 /***********************************************************************************
48 * __| |_ _ _ __ ___ _ __ ___ _ __ _ _ __ | |_ ___ _ __| |_ __ _ ___ ___
49 * / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__| _/ _` |/ __/ _ \
50 * | (_| | |_| | | | | | | |_) | __/ | | | | | | || __/ | | || (_| | (_| __/
51 * \__,_|\__,_|_| |_| |_| .__/ \___|_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
54 ***********************************************************************************/
57 * Prints a tarval to file F.
58 * @param F output file
60 * @param brackets 1 == print square brackets around tarval
62 static void fprintf_tv(FILE *F, tarval *tv, int brackets) {
64 tarval_snprintf(buf, sizeof(buf), tv);
67 fprintf(F, "[%s]", buf);
69 fprintf(F, "%s", buf);
73 * Returns the name of a SymConst.
74 * @param symc the SymConst
75 * @return name of the SymConst
77 const char *get_sc_name(ir_node *symc) {
78 if (get_irn_opcode(symc) != iro_SymConst)
81 switch (get_SymConst_kind(symc)) {
82 case symconst_addr_name:
83 return get_id_str(get_SymConst_name(symc));
85 case symconst_addr_ent:
86 return get_entity_ld_name(get_SymConst_entity(symc));
89 assert(0 && "Unsupported SymConst");
96 * Returns a string containing the names of all registers within the limited bitset
98 static char *get_limited_regs(const arch_register_req_t *req, char *buf, int max) {
99 bitset_t *bs = bitset_alloca(req->cls->n_regs);
104 req->limited(NULL, bs);
106 for (i = 0; i < req->cls->n_regs; i++) {
107 if (bitset_is_set(bs, i)) {
108 cnt = snprintf(p, max - size, " %s", req->cls->regs[i].name);
110 fprintf(stderr, "dumper problem, exiting\n");
126 * Dumps the register requirements for either in or out.
128 static void dump_reg_req(FILE *F, ir_node *n, const ia32_register_req_t **reqs, int inout) {
129 char *dir = inout ? "out" : "in";
130 int max = inout ? get_ia32_n_res(n) : get_irn_arity(n);
131 char *buf = alloca(1024);
134 memset(buf, 0, 1024);
137 for (i = 0; i < max; i++) {
138 fprintf(F, "%sreq #%d =", dir, i);
140 if (reqs[i]->req.type == arch_register_req_type_none) {
144 if (reqs[i]->req.type & arch_register_req_type_normal) {
145 fprintf(F, " %s", reqs[i]->req.cls->name);
148 if (reqs[i]->req.type & arch_register_req_type_limited) {
149 fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
152 if (reqs[i]->req.type & arch_register_req_type_should_be_same) {
153 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->same_pos));
156 if (reqs[i]->req.type & arch_register_req_type_should_be_different) {
157 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->different_pos));
166 fprintf(F, "%sreq = N/A\n", dir);
171 * Dumper interface for dumping ia32 nodes in vcg.
172 * @param n the node to dump
173 * @param F the output file
174 * @param reason indicates which kind of information should be dumped
175 * @return 0 on success or != 0 on failure
177 static int dump_node_ia32(ir_node *n, FILE *F, dump_reason_t reason) {
178 ir_mode *mode = NULL;
182 const ia32_register_req_t **reqs;
183 const arch_register_t **slots;
186 case dump_node_opcode_txt:
187 fprintf(F, "%s", get_irn_opname(n));
190 case dump_node_mode_txt:
191 mode = get_irn_mode(n);
193 if (is_ia32_Load(n)) {
194 mode = get_irn_mode(get_irn_n(n, 0));
196 else if (is_ia32_Store(n)) {
197 mode = get_irn_mode(get_irn_n(n, 2));
201 fprintf(F, "[%s]", get_mode_name(mode));
205 case dump_node_nodeattr_txt:
206 if (is_ia32_Call(n)) {
207 fprintf(F, "&%s ", get_ia32_sc(n));
209 else if (get_ia32_cnst(n)) {
212 if (get_ia32_sc(n)) {
216 fprintf(F, "[%s%s]", pref, get_ia32_cnst(n));
219 if (is_ia32_AddrModeS(n)) {
220 fprintf(F, "[AM S] ");
222 else if (is_ia32_AddrModeD(n)) {
223 fprintf(F, "[AM D] ");
228 case dump_node_info_txt:
229 attr = get_ia32_attr(n);
230 fprintf(F, "=== IA32 attr begin ===\n");
232 /* dump IN requirements */
233 if (get_irn_arity(n) > 0) {
234 reqs = get_ia32_in_req_all(n);
235 dump_reg_req(F, n, reqs, 0);
238 /* dump OUT requirements */
239 if (attr->n_res > 0) {
240 reqs = get_ia32_out_req_all(n);
241 dump_reg_req(F, n, reqs, 1);
244 /* dump assigned registers */
245 slots = get_ia32_slots(n);
246 if (slots && attr->n_res > 0) {
247 for (i = 0; i < attr->n_res; i++) {
249 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
252 fprintf(F, "reg #%d = n/a\n", i);
262 fprintf(F, "Normal");
268 fprintf(F, "SymConst");
271 fprintf(F, "AM Dest (Load+Store)");
274 fprintf(F, "AM Source (Load)");
277 fprintf(F, "unknown (%d)", attr->tp);
283 /* dump supported am */
284 fprintf(F, "AM support = ");
285 switch (attr->am_support) {
290 fprintf(F, "source only (Load)");
293 fprintf(F, "dest only (Load+Store)");
299 fprintf(F, "unknown (%d)", attr->am_support);
304 /* dump am flavour */
305 fprintf(F, "AM flavour =");
306 if (attr->am_flavour == ia32_am_N) {
310 if (attr->am_flavour & ia32_O) {
313 if (attr->am_flavour & ia32_B) {
316 if (attr->am_flavour & ia32_I) {
319 if (attr->am_flavour & ia32_S) {
323 fprintf(F, " (%d)\n", attr->am_flavour);
326 fprintf(F, "AM offset = ");
328 fprintf(F, "%s", get_ia32_am_offs(n));
336 fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
339 fprintf(F, "pn_code = %d\n", get_ia32_pncode(n));
342 fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
345 fprintf(F, "flags =");
346 if (attr->flags == arch_irn_flags_none) {
350 if (attr->flags & arch_irn_flags_dont_spill) {
351 fprintf(F, " unspillable");
353 if (attr->flags & arch_irn_flags_rematerializable) {
354 fprintf(F, " remat");
356 if (attr->flags & arch_irn_flags_ignore) {
357 fprintf(F, " ignore");
360 fprintf(F, " (%d)\n", attr->flags);
362 fprintf(F, "=== IA32 attr end ===\n");
363 /* end of: case dump_node_info_txt */
372 /***************************************************************************************************
374 * | | | | | | / / | | | | | | | |
375 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
376 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
377 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
378 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
381 ***************************************************************************************************/
383 static char *copy_str(char *dst, const char *src) {
384 dst = xcalloc(1, strlen(src) + 1);
385 strncpy(dst, src, strlen(src) + 1);
389 static char *set_cnst_from_tv(char *cnst, tarval *tv) {
394 cnst = xcalloc(1, 64);
395 assert(tarval_snprintf(cnst, 63, tv));
400 * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
401 * Firm was made by people hating const :-(
403 ia32_attr_t *get_ia32_attr(const ir_node *node) {
404 assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
405 return (ia32_attr_t *)get_irn_generic_attr((ir_node *)node);
409 * Gets the type of an ia32 node.
411 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
412 ia32_attr_t *attr = get_ia32_attr(node);
417 * Sets the type of an ia32 node.
419 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
420 ia32_attr_t *attr = get_ia32_attr(node);
425 * Gets the supported addrmode of an ia32 node
427 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
428 ia32_attr_t *attr = get_ia32_attr(node);
429 return attr->am_support;
433 * Sets the supported addrmode of an ia32 node
435 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
436 ia32_attr_t *attr = get_ia32_attr(node);
437 attr->am_support = am_tp;
441 * Gets the addrmode flavour of an ia32 node
443 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
444 ia32_attr_t *attr = get_ia32_attr(node);
445 return attr->am_flavour;
449 * Sets the addrmode flavour of an ia32 node
451 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
452 ia32_attr_t *attr = get_ia32_attr(node);
453 attr->am_flavour = am_flavour;
457 * Joins all offsets to one string with adds.
459 char *get_ia32_am_offs(const ir_node *node) {
460 ia32_attr_t *attr = get_ia32_attr(node);
464 if (! attr->am_offs) {
468 size = obstack_object_size(attr->am_offs);
470 res = xcalloc(1, size + 1);
471 memcpy(res, obstack_base(attr->am_offs), size);
479 * Add an offset for addrmode.
481 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
482 ia32_attr_t *attr = get_ia32_attr(node);
484 if (!attr->am_offs) {
485 /* obstack is not initialized */
486 attr->am_offs = xcalloc(1, sizeof(*(attr->am_offs)));
487 obstack_init(attr->am_offs);
490 /* obstack is initialized -> there is already one offset */
491 /* present -> connect the offsets with an add */
492 obstack_printf(attr->am_offs, " %c ", op);
495 obstack_printf(attr->am_offs, "%s", offset);
499 * Add an offset for addrmode.
501 void add_ia32_am_offs(ir_node *node, char *offset) {
502 extend_ia32_am_offs(node, offset, '+');
506 * Sub an offset for addrmode.
508 void sub_ia32_am_offs(ir_node *node, char *offset) {
509 extend_ia32_am_offs(node, offset, '-');
513 * Gets the addr mode const.
515 int get_ia32_am_scale(const ir_node *node) {
516 ia32_attr_t *attr = get_ia32_attr(node);
517 return attr->am_scale;
521 * Sets the index register scale for addrmode.
523 void set_ia32_am_scale(ir_node *node, int scale) {
524 ia32_attr_t *attr = get_ia32_attr(node);
525 attr->am_scale = scale;
529 * Return the tarval of an immediate operation or NULL in case of SymConst
531 tarval *get_ia32_Immop_tarval(const ir_node *node) {
532 ia32_attr_t *attr = get_ia32_attr(node);
537 * Sets the attributes of an immediate operation to the specified tarval
539 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
540 ia32_attr_t *attr = get_ia32_attr(node);
542 attr->cnst = set_cnst_from_tv(attr->cnst, attr->tv);
546 * Return the sc attribute.
548 char *get_ia32_sc(const ir_node *node) {
549 ia32_attr_t *attr = get_ia32_attr(node);
554 * Sets the sc attribute.
556 void set_ia32_sc(ir_node *node, char *sc) {
557 ia32_attr_t *attr = get_ia32_attr(node);
558 attr->sc = copy_str(attr->sc, sc);
563 attr->cnst = attr->sc;
567 * Gets the string representation of the internal const (tv or symconst)
569 char *get_ia32_cnst(ir_node *node) {
570 ia32_attr_t *attr = get_ia32_attr(node);
575 * Returns the argument register requirements of an ia32 node.
577 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
578 ia32_attr_t *attr = get_ia32_attr(node);
583 * Returns the result register requirements of an ia32 node.
585 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
586 ia32_attr_t *attr = get_ia32_attr(node);
587 return attr->out_req;
591 * Returns the argument register requirement at position pos of an ia32 node.
593 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
594 ia32_attr_t *attr = get_ia32_attr(node);
595 return attr->in_req[pos];
599 * Returns the result register requirement at position pos of an ia32 node.
601 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
602 ia32_attr_t *attr = get_ia32_attr(node);
603 return attr->out_req[pos];
607 * Sets the OUT register requirements at position pos.
609 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
610 ia32_attr_t *attr = get_ia32_attr(node);
611 attr->out_req[pos] = req;
615 * Sets the IN register requirements at position pos.
617 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
618 ia32_attr_t *attr = get_ia32_attr(node);
619 attr->in_req[pos] = req;
623 * Returns the register flag of an ia32 node.
625 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
626 ia32_attr_t *attr = get_ia32_attr(node);
631 * Sets the register flag of an ia32 node.
633 void set_ia32_flags(const ir_node *node, arch_irn_flags_t flags) {
634 ia32_attr_t *attr = get_ia32_attr(node);
639 * Returns the result register slots of an ia32 node.
641 const arch_register_t **get_ia32_slots(const ir_node *node) {
642 ia32_attr_t *attr = get_ia32_attr(node);
647 * Returns the name of the OUT register at position pos.
649 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
650 ia32_attr_t *attr = get_ia32_attr(node);
652 assert(is_ia32_irn(node) && "Not an ia32 node.");
653 assert(pos < attr->n_res && "Invalid OUT position.");
654 assert(attr->slots[pos] && "No register assigned");
656 return arch_register_get_name(attr->slots[pos]);
660 * Returns the index of the OUT register at position pos within its register class.
662 int get_ia32_out_regnr(const ir_node *node, int pos) {
663 ia32_attr_t *attr = get_ia32_attr(node);
665 assert(is_ia32_irn(node) && "Not an ia32 node.");
666 assert(pos < attr->n_res && "Invalid OUT position.");
667 assert(attr->slots[pos] && "No register assigned");
669 return arch_register_get_index(attr->slots[pos]);
673 * Returns the OUT register at position pos.
675 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
676 ia32_attr_t *attr = get_ia32_attr(node);
678 assert(is_ia32_irn(node) && "Not an ia32 node.");
679 assert(pos < attr->n_res && "Invalid OUT position.");
680 assert(attr->slots[pos] && "No register assigned");
682 return attr->slots[pos];
686 * Sets the number of results.
688 void set_ia32_n_res(ir_node *node, int n_res) {
689 ia32_attr_t *attr = get_ia32_attr(node);
694 * Returns the number of results.
696 int get_ia32_n_res(const ir_node *node) {
697 ia32_attr_t *attr = get_ia32_attr(node);
702 * Returns the flavour of an ia32 node,
704 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
705 ia32_attr_t *attr = get_ia32_attr(node);
706 return attr->op_flav;
710 * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
712 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
713 ia32_attr_t *attr = get_ia32_attr(node);
714 attr->op_flav = op_flav;
718 * Returns the projnum code.
720 long get_ia32_pncode(const ir_node *node) {
721 ia32_attr_t *attr = get_ia32_attr(node);
722 return attr->pn_code;
726 * Sets the projnum code
728 void set_ia32_pncode(ir_node *node, long code) {
729 ia32_attr_t *attr = get_ia32_attr(node);
730 attr->pn_code = code;
734 /******************************************************************************************************
736 * (_) | | | | | | / _| | | (_)
737 * ___ _ __ ___ ___ _ __ _| | __ _| |_| |_ _ __ | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
738 * / __| '_ \ / _ \/ __| |/ _` | | / _` | __| __| '__| | _| | | | '_ \ / __| __| |/ _ \| '_ \ / __|
739 * \__ \ |_) | __/ (__| | (_| | | | (_| | |_| |_| | | | | |_| | | | | (__| |_| | (_) | | | | \__ \
740 * |___/ .__/ \___|\___|_|\__,_|_| \__,_|\__|\__|_| |_| \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
743 ******************************************************************************************************/
746 * Gets the type of an ia32_Const.
748 unsigned get_ia32_Const_type(ir_node *node) {
749 ia32_attr_t *attr = get_ia32_attr(node);
751 assert((is_ia32_Const(node) || is_ia32_fConst(node)) && "Need ia32_Const to get type");
757 * Sets the type of an ia32_Const.
759 void set_ia32_Const_type(ir_node *node, int type) {
760 ia32_attr_t *attr = get_ia32_attr(node);
762 assert((is_ia32_Const(node) || is_ia32_fConst(node)) && "Need ia32_Const to set type");
763 assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
769 * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
771 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
772 ia32_attr_t *na = get_ia32_attr(node);
773 ia32_attr_t *ca = get_ia32_attr(cnst);
775 assert((is_ia32_Const(cnst) || is_ia32_fConst(cnst)) && "Need ia32_Const to set Immop attr");
780 na->sc = copy_str(na->sc, ca->sc);
783 na->cnst = set_cnst_from_tv(na->cnst, na->tv);
789 * Copy the attributes from a Const to an ia32_Const
791 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
792 ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
794 assert((is_ia32_Const(ia32_cnst) || is_ia32_fConst(ia32_cnst)) && "Need ia32_Const to set Const attr");
796 switch (get_irn_opcode(cnst)) {
798 attr->tp = ia32_Const;
799 attr->tv = get_Const_tarval(cnst);
800 attr->cnst = set_cnst_from_tv(attr->cnst, attr->tv);
803 attr->tp = ia32_SymConst;
805 attr->sc = copy_str(attr->sc, get_sc_name(cnst));
806 attr->cnst = attr->sc;
809 assert(0 && "Unknown Const NYI");
812 assert(0 && "Cannot create ia32_Const for this opcode");
817 * Sets the AddrMode(S|D) attribute
819 void set_ia32_AddrMode(ir_node *node, char direction) {
820 ia32_attr_t *attr = get_ia32_attr(node);
824 attr->tp = ia32_AddrModeD;
827 attr->tp = ia32_AddrModeS;
830 assert(0 && "wrong AM type");
835 * Returns whether or not the node is an AddrModeS node.
837 int is_ia32_AddrModeS(ir_node *node) {
838 ia32_attr_t *attr = get_ia32_attr(node);
839 return (attr->tp == ia32_AddrModeS);
843 * Returns whether or not the node is an AddrModeD node.
845 int is_ia32_AddrModeD(ir_node *node) {
846 ia32_attr_t *attr = get_ia32_attr(node);
847 return (attr->tp == ia32_AddrModeD);
852 /***************************************************************************************
855 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
856 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
857 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
858 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
860 ***************************************************************************************/
862 /* Include the generated constructor functions */
863 #include "gen_ia32_new_nodes.c.inl"