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) || is_ia32_Store(n)) {
194 mode = get_ia32_ls_mode(n);
198 fprintf(F, "[%s]", get_mode_name(mode));
201 fprintf(F, "[?NOMODE?]");
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_Lea(n)) {
220 if (is_ia32_AddrModeS(n)) {
221 fprintf(F, "[AM S] ");
223 else if (is_ia32_AddrModeD(n)) {
224 fprintf(F, "[AM D] ");
230 case dump_node_info_txt:
231 attr = get_ia32_attr(n);
232 fprintf(F, "=== IA32 attr begin ===\n");
234 /* dump IN requirements */
235 if (get_irn_arity(n) > 0) {
236 reqs = get_ia32_in_req_all(n);
237 dump_reg_req(F, n, reqs, 0);
240 /* dump OUT requirements */
241 if (attr->n_res > 0) {
242 reqs = get_ia32_out_req_all(n);
243 dump_reg_req(F, n, reqs, 1);
246 /* dump assigned registers */
247 slots = get_ia32_slots(n);
248 if (slots && attr->n_res > 0) {
249 for (i = 0; i < attr->n_res; i++) {
251 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
254 fprintf(F, "reg #%d = n/a\n", i);
264 fprintf(F, "Normal");
270 fprintf(F, "SymConst");
273 fprintf(F, "AM Dest (Load+Store)");
276 fprintf(F, "AM Source (Load)");
279 fprintf(F, "unknown (%d)", attr->tp);
285 /* dump supported am */
286 fprintf(F, "AM support = ");
287 switch (attr->am_support) {
292 fprintf(F, "source only (Load)");
295 fprintf(F, "dest only (Load+Store)");
301 fprintf(F, "unknown (%d)", attr->am_support);
306 /* dump am flavour */
307 fprintf(F, "AM flavour =");
308 if (attr->am_flavour == ia32_am_N) {
312 if (attr->am_flavour & ia32_O) {
315 if (attr->am_flavour & ia32_B) {
318 if (attr->am_flavour & ia32_I) {
321 if (attr->am_flavour & ia32_S) {
325 fprintf(F, " (%d)\n", attr->am_flavour);
328 fprintf(F, "AM offset = ");
330 fprintf(F, "%s", get_ia32_am_offs(n));
338 fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
341 fprintf(F, "pn_code = %d\n", get_ia32_pncode(n));
344 fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
347 fprintf(F, "flags =");
348 if (attr->flags == arch_irn_flags_none) {
352 if (attr->flags & arch_irn_flags_dont_spill) {
353 fprintf(F, " unspillable");
355 if (attr->flags & arch_irn_flags_rematerializable) {
356 fprintf(F, " remat");
358 if (attr->flags & arch_irn_flags_ignore) {
359 fprintf(F, " ignore");
362 fprintf(F, " (%d)\n", attr->flags);
364 fprintf(F, "=== IA32 attr end ===\n");
365 /* end of: case dump_node_info_txt */
374 /***************************************************************************************************
376 * | | | | | | / / | | | | | | | |
377 * __ _| |_| |_ _ __ ___ ___| |_ / /_ _ ___| |_ _ __ ___ ___| |_| |__ ___ __| |___
378 * / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
379 * | (_| | |_| |_| | \__ \ __/ |_ / / (_| | __/ |_ | | | | | | __/ |_| | | | (_) | (_| \__ \
380 * \__,_|\__|\__|_| |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
383 ***************************************************************************************************/
385 static char *copy_str(char *dst, const char *src) {
386 dst = xcalloc(1, strlen(src) + 1);
387 strncpy(dst, src, strlen(src) + 1);
391 static char *set_cnst_from_tv(char *cnst, tarval *tv) {
396 cnst = xcalloc(1, 64);
397 assert(tarval_snprintf(cnst, 63, tv));
402 * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
403 * Firm was made by people hating const :-(
405 ia32_attr_t *get_ia32_attr(const ir_node *node) {
406 assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
407 return (ia32_attr_t *)get_irn_generic_attr((ir_node *)node);
411 * Gets the type of an ia32 node.
413 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
414 ia32_attr_t *attr = get_ia32_attr(node);
419 * Sets the type of an ia32 node.
421 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
422 ia32_attr_t *attr = get_ia32_attr(node);
427 * Gets the supported addrmode of an ia32 node
429 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
430 ia32_attr_t *attr = get_ia32_attr(node);
431 return attr->am_support;
435 * Sets the supported addrmode of an ia32 node
437 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
438 ia32_attr_t *attr = get_ia32_attr(node);
439 attr->am_support = am_tp;
443 * Gets the addrmode flavour of an ia32 node
445 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
446 ia32_attr_t *attr = get_ia32_attr(node);
447 return attr->am_flavour;
451 * Sets the addrmode flavour of an ia32 node
453 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
454 ia32_attr_t *attr = get_ia32_attr(node);
455 attr->am_flavour = am_flavour;
459 * Joins all offsets to one string with adds.
461 char *get_ia32_am_offs(const ir_node *node) {
462 ia32_attr_t *attr = get_ia32_attr(node);
466 if (! attr->am_offs) {
470 size = obstack_object_size(attr->am_offs);
472 res = xcalloc(1, size + 1);
473 memcpy(res, obstack_base(attr->am_offs), size);
481 * Add an offset for addrmode.
483 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
484 ia32_attr_t *attr = get_ia32_attr(node);
486 if (!attr->am_offs) {
487 /* obstack is not initialized */
488 attr->am_offs = xcalloc(1, sizeof(*(attr->am_offs)));
489 obstack_init(attr->am_offs);
492 /* obstack is initialized -> there is already one offset */
493 /* present -> connect the offsets with an add */
494 obstack_printf(attr->am_offs, " %c ", op);
497 obstack_printf(attr->am_offs, "%s", offset);
501 * Add an offset for addrmode.
503 void add_ia32_am_offs(ir_node *node, char *offset) {
504 extend_ia32_am_offs(node, offset, '+');
508 * Sub an offset for addrmode.
510 void sub_ia32_am_offs(ir_node *node, char *offset) {
511 extend_ia32_am_offs(node, offset, '-');
515 * Gets the addr mode const.
517 int get_ia32_am_scale(const ir_node *node) {
518 ia32_attr_t *attr = get_ia32_attr(node);
519 return attr->am_scale;
523 * Sets the index register scale for addrmode.
525 void set_ia32_am_scale(ir_node *node, int scale) {
526 ia32_attr_t *attr = get_ia32_attr(node);
527 attr->am_scale = scale;
531 * Return the tarval of an immediate operation or NULL in case of SymConst
533 tarval *get_ia32_Immop_tarval(const ir_node *node) {
534 ia32_attr_t *attr = get_ia32_attr(node);
539 * Sets the attributes of an immediate operation to the specified tarval
541 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
542 ia32_attr_t *attr = get_ia32_attr(node);
544 attr->cnst = set_cnst_from_tv(attr->cnst, attr->tv);
548 * Return the sc attribute.
550 char *get_ia32_sc(const ir_node *node) {
551 ia32_attr_t *attr = get_ia32_attr(node);
556 * Sets the sc attribute.
558 void set_ia32_sc(ir_node *node, char *sc) {
559 ia32_attr_t *attr = get_ia32_attr(node);
560 attr->sc = copy_str(attr->sc, sc);
565 attr->cnst = attr->sc;
569 * Gets the string representation of the internal const (tv or symconst)
571 char *get_ia32_cnst(ir_node *node) {
572 ia32_attr_t *attr = get_ia32_attr(node);
577 * Gets the mode of the stored/loaded value (only set for Store/Load)
579 ir_mode *get_ia32_ls_mode(const ir_node *node) {
580 ia32_attr_t *attr = get_ia32_attr(node);
581 return attr->ls_mode;
585 * Sets the mode of the stored/loaded value (only set for Store/Load)
587 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
588 ia32_attr_t *attr = get_ia32_attr(node);
589 attr->ls_mode = mode;
593 * Returns the argument register requirements of an ia32 node.
595 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
596 ia32_attr_t *attr = get_ia32_attr(node);
601 * Returns the result register requirements of an ia32 node.
603 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
604 ia32_attr_t *attr = get_ia32_attr(node);
605 return attr->out_req;
609 * Returns the argument register requirement at position pos of an ia32 node.
611 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
612 ia32_attr_t *attr = get_ia32_attr(node);
613 return attr->in_req[pos];
617 * Returns the result register requirement at position pos of an ia32 node.
619 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
620 ia32_attr_t *attr = get_ia32_attr(node);
621 return attr->out_req[pos];
625 * Sets the OUT register requirements at position pos.
627 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
628 ia32_attr_t *attr = get_ia32_attr(node);
629 attr->out_req[pos] = req;
633 * Sets the IN register requirements at position pos.
635 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
636 ia32_attr_t *attr = get_ia32_attr(node);
637 attr->in_req[pos] = req;
641 * Returns the register flag of an ia32 node.
643 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
644 ia32_attr_t *attr = get_ia32_attr(node);
649 * Sets the register flag of an ia32 node.
651 void set_ia32_flags(const ir_node *node, arch_irn_flags_t flags) {
652 ia32_attr_t *attr = get_ia32_attr(node);
657 * Returns the result register slots of an ia32 node.
659 const arch_register_t **get_ia32_slots(const ir_node *node) {
660 ia32_attr_t *attr = get_ia32_attr(node);
665 * Returns the name of the OUT register at position pos.
667 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
668 ia32_attr_t *attr = get_ia32_attr(node);
670 assert(is_ia32_irn(node) && "Not an ia32 node.");
671 assert(pos < attr->n_res && "Invalid OUT position.");
672 assert(attr->slots[pos] && "No register assigned");
674 return arch_register_get_name(attr->slots[pos]);
678 * Returns the index of the OUT register at position pos within its register class.
680 int get_ia32_out_regnr(const ir_node *node, int pos) {
681 ia32_attr_t *attr = get_ia32_attr(node);
683 assert(is_ia32_irn(node) && "Not an ia32 node.");
684 assert(pos < attr->n_res && "Invalid OUT position.");
685 assert(attr->slots[pos] && "No register assigned");
687 return arch_register_get_index(attr->slots[pos]);
691 * Returns the OUT register at position pos.
693 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
694 ia32_attr_t *attr = get_ia32_attr(node);
696 assert(is_ia32_irn(node) && "Not an ia32 node.");
697 assert(pos < attr->n_res && "Invalid OUT position.");
698 assert(attr->slots[pos] && "No register assigned");
700 return attr->slots[pos];
704 * Sets the number of results.
706 void set_ia32_n_res(ir_node *node, int n_res) {
707 ia32_attr_t *attr = get_ia32_attr(node);
712 * Returns the number of results.
714 int get_ia32_n_res(const ir_node *node) {
715 ia32_attr_t *attr = get_ia32_attr(node);
720 * Returns the flavour of an ia32 node,
722 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
723 ia32_attr_t *attr = get_ia32_attr(node);
724 return attr->op_flav;
728 * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
730 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
731 ia32_attr_t *attr = get_ia32_attr(node);
732 attr->op_flav = op_flav;
736 * Returns the projnum code.
738 long get_ia32_pncode(const ir_node *node) {
739 ia32_attr_t *attr = get_ia32_attr(node);
740 return attr->pn_code;
744 * Sets the projnum code
746 void set_ia32_pncode(ir_node *node, long code) {
747 ia32_attr_t *attr = get_ia32_attr(node);
748 attr->pn_code = code;
752 /******************************************************************************************************
754 * (_) | | | | | | / _| | | (_)
755 * ___ _ __ ___ ___ _ __ _| | __ _| |_| |_ _ __ | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
756 * / __| '_ \ / _ \/ __| |/ _` | | / _` | __| __| '__| | _| | | | '_ \ / __| __| |/ _ \| '_ \ / __|
757 * \__ \ |_) | __/ (__| | (_| | | | (_| | |_| |_| | | | | |_| | | | | (__| |_| | (_) | | | | \__ \
758 * |___/ .__/ \___|\___|_|\__,_|_| \__,_|\__|\__|_| |_| \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
761 ******************************************************************************************************/
764 * Gets the type of an ia32_Const.
766 unsigned get_ia32_Const_type(ir_node *node) {
767 ia32_attr_t *attr = get_ia32_attr(node);
769 assert((is_ia32_Const(node) || is_ia32_fConst(node)) && "Need ia32_Const to get type");
775 * Sets the type of an ia32_Const.
777 void set_ia32_Const_type(ir_node *node, int type) {
778 ia32_attr_t *attr = get_ia32_attr(node);
780 assert((is_ia32_Const(node) || is_ia32_fConst(node)) && "Need ia32_Const to set type");
781 assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
787 * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
789 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
790 ia32_attr_t *na = get_ia32_attr(node);
791 ia32_attr_t *ca = get_ia32_attr(cnst);
793 assert((is_ia32_Const(cnst) || is_ia32_fConst(cnst)) && "Need ia32_Const to set Immop attr");
798 na->sc = copy_str(na->sc, ca->sc);
801 na->cnst = set_cnst_from_tv(na->cnst, na->tv);
807 * Copy the attributes from a Const to an ia32_Const
809 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
810 ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
812 assert((is_ia32_Const(ia32_cnst) || is_ia32_fConst(ia32_cnst)) && "Need ia32_Const to set Const attr");
814 switch (get_irn_opcode(cnst)) {
816 attr->tp = ia32_Const;
817 attr->tv = get_Const_tarval(cnst);
818 attr->cnst = set_cnst_from_tv(attr->cnst, attr->tv);
821 attr->tp = ia32_SymConst;
823 attr->sc = copy_str(attr->sc, get_sc_name(cnst));
824 attr->cnst = attr->sc;
827 assert(0 && "Unknown Const NYI");
830 assert(0 && "Cannot create ia32_Const for this opcode");
835 * Sets the AddrMode(S|D) attribute
837 void set_ia32_AddrMode(ir_node *node, char direction) {
838 ia32_attr_t *attr = get_ia32_attr(node);
842 attr->tp = ia32_AddrModeD;
845 attr->tp = ia32_AddrModeS;
848 assert(0 && "wrong AM type");
853 * Returns whether or not the node is an AddrModeS node.
855 int is_ia32_AddrModeS(ir_node *node) {
856 ia32_attr_t *attr = get_ia32_attr(node);
857 return (attr->tp == ia32_AddrModeS);
861 * Returns whether or not the node is an AddrModeD node.
863 int is_ia32_AddrModeD(ir_node *node) {
864 ia32_attr_t *attr = get_ia32_attr(node);
865 return (attr->tp == ia32_AddrModeD);
870 /***************************************************************************************
873 * _ __ ___ __| | ___ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
874 * | '_ \ / _ \ / _` |/ _ \ / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
875 * | | | | (_) | (_| | __/ | (_| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
876 * |_| |_|\___/ \__,_|\___| \___\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
878 ***************************************************************************************/
880 /* Include the generated constructor functions */
881 #include "gen_ia32_new_nodes.c.inl"