add support for x87 fp unit
[libfirm] / ir / be / ia32 / ia32_new_nodes.c
1 /**
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
5  * $Id$
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #ifdef _WIN32
13 #include <malloc.h>
14 #else
15 #include <alloca.h>
16 #endif
17
18 #include <stdlib.h>
19
20 #include "irprog_t.h"
21 #include "irgraph_t.h"
22 #include "irnode_t.h"
23 #include "irmode_t.h"
24 #include "ircons_t.h"
25 #include "iropt_t.h"
26 #include "irop.h"
27 #include "firm_common_t.h"
28 #include "irvrfy_t.h"
29 #include "irprintf.h"
30
31 #include "../bearch.h"
32
33 #include "ia32_nodes_attr.h"
34 #include "ia32_new_nodes.h"
35 #include "gen_ia32_regalloc_if.h"
36
37 #ifdef obstack_chunk_alloc
38 # undef obstack_chunk_alloc
39 # define obstack_chunk_alloc xmalloc
40 #else
41 # define obstack_chunk_alloc xmalloc
42 # define obstack_chunk_free free
43 #endif
44
45 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
46
47 /**
48  * Returns the ident of a SymConst.
49  * @param symc  The SymConst
50  * @return The ident of the SymConst
51  */
52 static ident *get_sc_ident(ir_node *symc) {
53         assert(get_irn_opcode(symc) == iro_SymConst && "need symconst to get ident");
54
55         switch (get_SymConst_kind(symc)) {
56                 case symconst_addr_name:
57                         return get_SymConst_name(symc);
58
59                 case symconst_addr_ent:
60                         return get_entity_ld_ident(get_SymConst_entity(symc));
61
62                 default:
63                         assert(0 && "Unsupported SymConst");
64         }
65
66         return NULL;
67 }
68
69
70
71 /***********************************************************************************
72  *      _                                   _       _             __
73  *     | |                                 (_)     | |           / _|
74  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
75  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
76  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
77  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
78  *                       | |
79  *                       |_|
80  ***********************************************************************************/
81
82 /**
83  * Returns a string containing the names of all registers within the limited bitset
84  */
85 static char *get_limited_regs(const arch_register_req_t *req, char *buf, int max) {
86         bitset_t *bs   = bitset_alloca(req->cls->n_regs);
87         char     *p    = buf;
88         int       size = 0;
89         int       i, cnt;
90
91         req->limited(NULL, bs);
92
93         for (i = 0; i < req->cls->n_regs; i++) {
94                 if (bitset_is_set(bs, i)) {
95                         cnt = snprintf(p, max - size, " %s", req->cls->regs[i].name);
96                         if (cnt < 0) {
97                                 fprintf(stderr, "dumper problem, exiting\n");
98                                 exit(1);
99                         }
100
101                         p    += cnt;
102                         size += cnt;
103
104                         if (size >= max)
105                                 break;
106                 }
107         }
108
109         return buf;
110 }
111
112 /**
113  * Dumps the register requirements for either in or out.
114  */
115 static void dump_reg_req(FILE *F, ir_node *n, const ia32_register_req_t **reqs, int inout) {
116         char *dir = inout ? "out" : "in";
117         int   max = inout ? get_ia32_n_res(n) : get_irn_arity(n);
118         char *buf = alloca(1024);
119         int   i;
120
121         memset(buf, 0, 1024);
122
123         if (reqs) {
124                 for (i = 0; i < max; i++) {
125                         fprintf(F, "%sreq #%d =", dir, i);
126
127                         if (reqs[i]->req.type == arch_register_req_type_none) {
128                                 fprintf(F, " n/a");
129                         }
130
131                         if (reqs[i]->req.type & arch_register_req_type_normal) {
132                                 fprintf(F, " %s", reqs[i]->req.cls->name);
133                         }
134
135                         if (reqs[i]->req.type & arch_register_req_type_limited) {
136                                 fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
137                         }
138
139                         if (reqs[i]->req.type & arch_register_req_type_should_be_same) {
140                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->same_pos));
141                         }
142
143                         if (reqs[i]->req.type & arch_register_req_type_should_be_different) {
144                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->different_pos));
145                         }
146
147                         fprintf(F, "\n");
148                 }
149
150                 fprintf(F, "\n");
151         }
152         else {
153                 fprintf(F, "%sreq = N/A\n", dir);
154         }
155 }
156
157 /**
158  * Dumper interface for dumping ia32 nodes in vcg.
159  * @param n        the node to dump
160  * @param F        the output file
161  * @param reason   indicates which kind of information should be dumped
162  * @return 0 on success or != 0 on failure
163  */
164 static int dump_node_ia32(ir_node *n, FILE *F, dump_reason_t reason) {
165         ir_mode     *mode = NULL;
166         int          bad  = 0;
167         int          i, n_res, am_flav, flags;
168         const ia32_register_req_t **reqs;
169         const arch_register_t     **slots;
170
171         switch (reason) {
172                 case dump_node_opcode_txt:
173                         fprintf(F, "%s", get_irn_opname(n));
174                         break;
175
176                 case dump_node_mode_txt:
177                         mode = get_irn_mode(n);
178
179                         if (is_ia32_Ld(n) || is_ia32_St(n)) {
180                                 mode = get_ia32_ls_mode(n);
181                         }
182
183                         fprintf(F, "[%s]", mode ? get_mode_name(mode) : "?NOMODE?");
184                         break;
185
186                 case dump_node_nodeattr_txt:
187                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
188                                 char *pref = is_ia32_ImmSymConst(n) ? "SymC" : "";
189
190                                 fprintf(F, "[%s%s]", pref, get_ia32_cnst(n));
191                         }
192
193                         if (! is_ia32_Lea(n)) {
194                                 if (is_ia32_AddrModeS(n)) {
195                                         fprintf(F, "[AM S] ");
196                                 }
197                                 else if (is_ia32_AddrModeD(n)) {
198                                         fprintf(F, "[AM D] ");
199                                 }
200                         }
201
202                         break;
203
204                 case dump_node_info_txt:
205                         n_res = get_ia32_n_res(n);
206                         fprintf(F, "=== IA32 attr begin ===\n");
207
208                         /* dump IN requirements */
209                         if (get_irn_arity(n) > 0) {
210                                 reqs = get_ia32_in_req_all(n);
211                                 dump_reg_req(F, n, reqs, 0);
212                         }
213
214                         /* dump OUT requirements */
215                         if (n_res > 0) {
216                                 reqs = get_ia32_out_req_all(n);
217                                 dump_reg_req(F, n, reqs, 1);
218                         }
219
220                         /* dump assigned registers */
221                         slots = get_ia32_slots(n);
222                         if (slots && n_res > 0) {
223                                 for (i = 0; i < n_res; i++) {
224                                         fprintf(F, "reg #%d = %s\n", i, slots[i] ? slots[i]->name : "n/a");
225                                 }
226                                 fprintf(F, "\n");
227                         }
228
229                         /* dump op type */
230                         fprintf(F, "op = ");
231                         switch (get_ia32_op_type(n)) {
232                                 case ia32_Normal:
233                                         fprintf(F, "Normal");
234                                         break;
235                                 case ia32_Const:
236                                         fprintf(F, "Const");
237                                         break;
238                                 case ia32_SymConst:
239                                         fprintf(F, "SymConst");
240                                         break;
241                                 case ia32_AddrModeD:
242                                         fprintf(F, "AM Dest (Load+Store)");
243                                         break;
244                                 case ia32_AddrModeS:
245                                         fprintf(F, "AM Source (Load)");
246                                         break;
247                                 default:
248                                         fprintf(F, "unknown (%d)", get_ia32_op_type(n));
249                                         break;
250                         }
251                         fprintf(F, "\n");
252
253
254                         /* dump supported am */
255                         fprintf(F, "AM support = ");
256                         switch (get_ia32_am_support(n)) {
257                                 case ia32_am_None:
258                                         fprintf(F, "none");
259                                         break;
260                                 case ia32_am_Source:
261                                         fprintf(F, "source only (Load)");
262                                         break;
263                                 case ia32_am_Dest:
264                                         fprintf(F, "dest only (Load+Store)");
265                                         break;
266                                 case ia32_am_Full:
267                                         fprintf(F, "full");
268                                         break;
269                                 default:
270                                         fprintf(F, "unknown (%d)", get_ia32_am_support(n));
271                                         break;
272                         }
273                         fprintf(F, "\n");
274
275                         /* dump am flavour */
276                         fprintf(F, "AM flavour =");
277                         am_flav = get_ia32_am_flavour(n);
278                         if (am_flav == ia32_am_N) {
279                                 fprintf(F, " none");
280                         }
281                         else {
282                                 if (am_flav & ia32_O) {
283                                         fprintf(F, " O");
284                                 }
285                                 if (am_flav & ia32_B) {
286                                         fprintf(F, " B");
287                                 }
288                                 if (am_flav & ia32_I) {
289                                         fprintf(F, " I");
290                                 }
291                                 if (am_flav & ia32_S) {
292                                         fprintf(F, " S");
293                                 }
294                         }
295                         fprintf(F, " (%d)\n", am_flav);
296
297                         /* dump AM offset */
298                         fprintf(F, "AM offset = ");
299                         if (get_ia32_am_offs(n)) {
300                                 fprintf(F, "%s", get_ia32_am_offs(n));
301                         }
302                         else {
303                                 fprintf(F, "n/a");
304                         }
305                         fprintf(F, "\n");
306
307                         /* dump AM scale */
308                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
309
310                         /* dump pn code */
311                         fprintf(F, "pn_code = %ld\n", get_ia32_pncode(n));
312
313                         /* dump n_res */
314                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
315
316                         /* dump use_frame */
317                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
318
319                         /* commutative */
320                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
321
322                         /* dump flags */
323                         fprintf(F, "flags =");
324                         flags = get_ia32_flags(n);
325                         if (flags == arch_irn_flags_none) {
326                                 fprintf(F, " none");
327                         }
328                         else {
329                                 if (flags & arch_irn_flags_dont_spill) {
330                                         fprintf(F, " unspillable");
331                                 }
332                                 if (flags & arch_irn_flags_rematerializable) {
333                                         fprintf(F, " remat");
334                                 }
335                                 if (flags & arch_irn_flags_ignore) {
336                                         fprintf(F, " ignore");
337                                 }
338                         }
339                         fprintf(F, " (%d)\n", flags);
340
341                         /* dump frame entity */
342                         fprintf(F, "frame entity = ");
343                         if (get_ia32_frame_ent(n)) {
344                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
345                         }
346                         else {
347                                 fprintf(F, "n/a");
348                         }
349                         fprintf(F, "\n");
350
351 #ifndef NDEBUG
352                         /* dump original ir node name */
353                         fprintf(F, "orig node = ");
354                         if (get_ia32_orig_node(n)) {
355                                 fprintf(F, "%s", get_ia32_orig_node(n));
356                         }
357                         else {
358                                 fprintf(F, "n/a");
359                         }
360                         fprintf(F, "\n");
361 #endif /* NDEBUG */
362
363                         fprintf(F, "=== IA32 attr end ===\n");
364                         /* end of: case dump_node_info_txt */
365                         break;
366         }
367
368         return bad;
369 }
370
371
372
373 /***************************************************************************************************
374  *        _   _                   _       __        _                    _   _               _
375  *       | | | |                 | |     / /       | |                  | | | |             | |
376  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
377  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
378  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
379  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
380  *                                        __/ |
381  *                                       |___/
382  ***************************************************************************************************/
383
384 /**
385  * Returns an ident for the given tarval tv.
386  */
387 static ident *get_ident_for_tv(tarval *tv) {
388         char buf[1024];
389
390         assert(tarval_snprintf(buf, sizeof(buf), tv));
391         return new_id_from_str(buf);
392 }
393
394 /**
395  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
396  * Firm was made by people hating const :-(
397  */
398 ia32_attr_t *get_ia32_attr(const ir_node *node) {
399         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
400         return (ia32_attr_t *)get_irn_generic_attr((ir_node *)node);
401 }
402
403 /**
404  * Gets the type of an ia32 node.
405  */
406 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
407         ia32_attr_t *attr = get_ia32_attr(node);
408         return attr->data.tp;
409 }
410
411 /**
412  * Sets the type of an ia32 node.
413  */
414 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
415         ia32_attr_t *attr = get_ia32_attr(node);
416         attr->data.tp     = tp;
417 }
418
419 /**
420  * Gets the immediate op type of an ia32 node.
421  */
422 ia32_immop_type_t get_ia32_immop_type(const ir_node *node) {
423         ia32_attr_t *attr = get_ia32_attr(node);
424         return attr->data.imm_tp;
425 }
426
427 /**
428  * Sets the immediate op type of an ia32 node.
429  */
430 void set_ia32_immop_type(ir_node *node, ia32_immop_type_t tp) {
431         ia32_attr_t *attr = get_ia32_attr(node);
432         attr->data.imm_tp = tp;
433 }
434
435 /**
436  * Gets the supported addrmode of an ia32 node
437  */
438 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
439         ia32_attr_t *attr = get_ia32_attr(node);
440         return attr->data.am_support;
441 }
442
443 /**
444  * Sets the supported addrmode of an ia32 node
445  */
446 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
447         ia32_attr_t *attr = get_ia32_attr(node);
448         attr->data.am_support  = am_tp;
449 }
450
451 /**
452  * Gets the addrmode flavour of an ia32 node
453  */
454 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
455         ia32_attr_t *attr = get_ia32_attr(node);
456         return attr->data.am_flavour;
457 }
458
459 /**
460  * Sets the addrmode flavour of an ia32 node
461  */
462 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
463         ia32_attr_t *attr = get_ia32_attr(node);
464         attr->data.am_flavour  = am_flavour;
465 }
466
467 /**
468  * Joins all offsets to one string with adds.
469  */
470 char *get_ia32_am_offs(const ir_node *node) {
471         ia32_attr_t *attr = get_ia32_attr(node);
472         char        *res  = NULL;
473         int          size;
474
475         if (! attr->am_offs) {
476                 return NULL;
477         }
478
479         size = obstack_object_size(attr->am_offs);
480         if (size > 0) {
481                 res    = xmalloc(size + 2);
482                 res[0] = attr->data.offs_sign ? '-' : '+';
483                 memcpy(&res[1], obstack_base(attr->am_offs), size);
484                 res[size + 1] = '\0';
485         }
486         return res;
487 }
488
489 /**
490  * Add an offset for addrmode.
491  */
492 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
493         ia32_attr_t *attr = get_ia32_attr(node);
494
495         if (! offset || strlen(offset) < 1)
496                 return;
497
498         /* offset could already have an explicit sign */
499         /* -> supersede op if necessary               */
500         if (offset[0] == '-' || offset[0] == '+') {
501                 if (offset[0] == '-') {
502                         op = (op == '-') ? '+' : '-';
503                 }
504
505                 /* skip explicit sign */
506                 offset++;
507         }
508
509         if (! attr->am_offs) {
510                 /* obstack is not initialized */
511                 attr->am_offs = xcalloc(1, sizeof(*(attr->am_offs)));
512                 obstack_init(attr->am_offs);
513
514                 attr->data.offs_sign = (op == '-') ? 1 : 0;
515         }
516         else {
517                 /* If obstack is initialized, connect the new offset with op */
518                 obstack_printf(attr->am_offs, "%c", op);
519         }
520
521         obstack_printf(attr->am_offs, "%s", offset);
522 }
523
524 /**
525  * Add an offset for addrmode.
526  */
527 void add_ia32_am_offs(ir_node *node, const char *offset) {
528         extend_ia32_am_offs(node, (char *)offset, '+');
529 }
530
531 /**
532  * Sub an offset for addrmode.
533  */
534 void sub_ia32_am_offs(ir_node *node, const char *offset) {
535         extend_ia32_am_offs(node, (char *)offset, '-');
536 }
537
538 /**
539  * Gets the addr mode const.
540  */
541 int get_ia32_am_scale(const ir_node *node) {
542         ia32_attr_t *attr = get_ia32_attr(node);
543         return attr->data.am_scale;
544 }
545
546 /**
547  * Sets the index register scale for addrmode.
548  */
549 void set_ia32_am_scale(ir_node *node, int scale) {
550         ia32_attr_t *attr   = get_ia32_attr(node);
551         attr->data.am_scale = scale;
552 }
553
554 /**
555  * Return the tarval of an immediate operation or NULL in case of SymConst
556  */
557 tarval *get_ia32_Immop_tarval(const ir_node *node) {
558         ia32_attr_t *attr = get_ia32_attr(node);
559     return attr->cnst_val.tv;
560 }
561
562 /**
563  * Sets the attributes of an immediate operation to the specified tarval
564  */
565 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
566         ia32_attr_t *attr = get_ia32_attr(node);
567         attr->cnst_val.tv = tv;
568         attr->cnst        = get_ident_for_tv(tv);
569 }
570
571 /**
572  * Return the sc attribute.
573  */
574 ident *get_ia32_sc(const ir_node *node) {
575         ia32_attr_t *attr = get_ia32_attr(node);
576         return attr->cnst_val.sc;
577 }
578
579 /**
580  * Sets the sc attribute.
581  */
582 void set_ia32_sc(ir_node *node, ident *sc) {
583         ia32_attr_t *attr = get_ia32_attr(node);
584         attr->cnst_val.sc = sc;
585         attr->cnst        = attr->cnst_val.sc;
586 }
587
588 /**
589  * Gets the string representation of the internal const (tv or symconst)
590  */
591 const char *get_ia32_cnst(const ir_node *node) {
592         ia32_attr_t *attr = get_ia32_attr(node);
593         return get_id_str(attr->cnst);
594 }
595
596 /**
597  * Sets the string representation of the internal const.
598  */
599 void set_ia32_cnst(ir_node *node, char *cnst) {
600         ia32_attr_t *attr = get_ia32_attr(node);
601         attr->cnst        = new_id_from_str(cnst);
602 }
603
604 /**
605  * Gets the ident representation of the internal const (tv or symconst)
606  */
607 ident *get_ia32_id_cnst(const ir_node *node) {
608         ia32_attr_t *attr = get_ia32_attr(node);
609         return attr->cnst;
610 }
611
612 /**
613  * Sets the ident representation of the internal const.
614  */
615 void set_ia32_id_cnst(ir_node *node, ident *cnst) {
616         ia32_attr_t *attr = get_ia32_attr(node);
617         attr->cnst        = cnst;
618 }
619
620 /**
621  * Sets the uses_frame flag.
622  */
623 void set_ia32_use_frame(ir_node *node) {
624         ia32_attr_t *attr    = get_ia32_attr(node);
625         attr->data.use_frame = 1;
626 }
627
628 /**
629  * Clears the uses_frame flag.
630  */
631 void clear_ia32_use_frame(ir_node *node) {
632         ia32_attr_t *attr    = get_ia32_attr(node);
633         attr->data.use_frame = 0;
634 }
635
636 /**
637  * Gets the uses_frame flag.
638  */
639 int is_ia32_use_frame(const ir_node *node) {
640         ia32_attr_t *attr = get_ia32_attr(node);
641         return attr->data.use_frame;
642 }
643
644 /**
645  * Sets node to commutative.
646  */
647 void set_ia32_commutative(ir_node *node) {
648         ia32_attr_t *attr         = get_ia32_attr(node);
649         attr->data.is_commutative = 1;
650 }
651
652 /**
653  * Sets node to non-commutative.
654  */
655 void clear_ia32_commutative(ir_node *node) {
656         ia32_attr_t *attr         = get_ia32_attr(node);
657         attr->data.is_commutative = 0;
658 }
659
660 /**
661  * Checks if node is commutative.
662  */
663 int is_ia32_commutative(const ir_node *node) {
664         ia32_attr_t *attr = get_ia32_attr(node);
665         return attr->data.is_commutative;
666 }
667
668 /**
669  * Gets the mode of the stored/loaded value (only set for Store/Load)
670  */
671 ir_mode *get_ia32_ls_mode(const ir_node *node) {
672         ia32_attr_t *attr = get_ia32_attr(node);
673         return attr->ls_mode;
674 }
675
676 /**
677  * Sets the mode of the stored/loaded value (only set for Store/Load)
678  */
679 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
680         ia32_attr_t *attr = get_ia32_attr(node);
681         attr->ls_mode     = mode;
682 }
683
684 /**
685  * Gets the mode of the result.
686  */
687 ir_mode *get_ia32_res_mode(const ir_node *node) {
688         ia32_attr_t *attr = get_ia32_attr(node);
689         return attr->res_mode;
690 }
691
692 /**
693  * Sets the mode of the result.
694  */
695 void set_ia32_res_mode(ir_node *node, ir_mode *mode) {
696         ia32_attr_t *attr = get_ia32_attr(node);
697         attr->res_mode    = mode;
698 }
699
700 /**
701  * Gets the frame entity assigned to this node;
702  */
703 entity *get_ia32_frame_ent(const ir_node *node) {
704         ia32_attr_t *attr = get_ia32_attr(node);
705         return attr->frame_ent;
706 }
707
708 /**
709  * Sets the frame entity for this node;
710  */
711 void set_ia32_frame_ent(ir_node *node, entity *ent) {
712         ia32_attr_t *attr = get_ia32_attr(node);
713         attr->frame_ent   = ent;
714 }
715
716 /**
717  * Returns the argument register requirements of an ia32 node.
718  */
719 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
720         ia32_attr_t *attr = get_ia32_attr(node);
721         return attr->in_req;
722 }
723
724 /**
725  * Sets the argument register requirements of an ia32 node.
726  */
727 void set_ia32_in_req_all(ir_node *node, const ia32_register_req_t **reqs) {
728         ia32_attr_t *attr = get_ia32_attr(node);
729         attr->in_req      = reqs;
730 }
731
732 /**
733  * Returns the result register requirements of an ia32 node.
734  */
735 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
736         ia32_attr_t *attr = get_ia32_attr(node);
737         return attr->out_req;
738 }
739
740 /**
741  * Sets the result register requirements of an ia32 node.
742  */
743 void set_ia32_out_req_all(ir_node *node, const ia32_register_req_t **reqs) {
744         ia32_attr_t *attr = get_ia32_attr(node);
745         attr->out_req     = reqs;
746 }
747
748 /**
749  * Returns the argument register requirement at position pos of an ia32 node.
750  */
751 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
752         ia32_attr_t *attr = get_ia32_attr(node);
753         return attr->in_req[pos];
754 }
755
756 /**
757  * Returns the result register requirement at position pos of an ia32 node.
758  */
759 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
760         ia32_attr_t *attr = get_ia32_attr(node);
761         return attr->out_req[pos];
762 }
763
764 /**
765  * Sets the OUT register requirements at position pos.
766  */
767 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
768         ia32_attr_t *attr  = get_ia32_attr(node);
769         attr->out_req[pos] = req;
770 }
771
772 /**
773  * Sets the IN register requirements at position pos.
774  */
775 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
776         ia32_attr_t *attr = get_ia32_attr(node);
777         attr->in_req[pos] = req;
778 }
779
780 /**
781  * Returns the register flag of an ia32 node.
782  */
783 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
784         ia32_attr_t *attr = get_ia32_attr(node);
785         return attr->data.flags;
786 }
787
788 /**
789  * Sets the register flag of an ia32 node.
790  */
791 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
792         ia32_attr_t *attr = get_ia32_attr(node);
793         attr->data.flags  = flags;
794 }
795
796 /**
797  * Returns the result register slots of an ia32 node.
798  */
799 const arch_register_t **get_ia32_slots(const ir_node *node) {
800         ia32_attr_t *attr = get_ia32_attr(node);
801         return attr->slots;
802 }
803
804 /**
805  * Sets the number of results.
806  */
807 void set_ia32_n_res(ir_node *node, int n_res) {
808         ia32_attr_t *attr = get_ia32_attr(node);
809         attr->data.n_res  = n_res;
810 }
811
812 /**
813  * Returns the number of results.
814  */
815 int get_ia32_n_res(const ir_node *node) {
816         ia32_attr_t *attr = get_ia32_attr(node);
817         return attr->data.n_res;
818 }
819
820 /**
821  * Returns the flavour of an ia32 node,
822  */
823 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
824         ia32_attr_t *attr = get_ia32_attr(node);
825         return attr->data.op_flav;
826 }
827
828 /**
829  * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
830  */
831 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
832         ia32_attr_t *attr  = get_ia32_attr(node);
833         attr->data.op_flav = op_flav;
834 }
835
836 /**
837  * Returns the projnum code.
838  */
839 long get_ia32_pncode(const ir_node *node) {
840         ia32_attr_t *attr = get_ia32_attr(node);
841         return attr->pn_code;
842 }
843
844 /**
845  * Sets the projnum code
846  */
847 void set_ia32_pncode(ir_node *node, long code) {
848         ia32_attr_t *attr = get_ia32_attr(node);
849         attr->pn_code     = code;
850 }
851
852 #ifndef NDEBUG
853
854 /**
855  * Returns the name of the original ir node.
856  */
857 const char *get_ia32_orig_node(const ir_node *node) {
858         ia32_attr_t *attr = get_ia32_attr(node);
859         return attr->orig_node;
860 }
861
862 /**
863  * Sets the name of the original ir node.
864  */
865 void set_ia32_orig_node(ir_node *node, const char *name) {
866         ia32_attr_t *attr = get_ia32_attr(node);
867         attr->orig_node   = name;
868 }
869
870 #endif /* NDEBUG */
871
872 /******************************************************************************************************
873  *                      _       _         _   _           __                  _   _
874  *                     (_)     | |       | | | |         / _|                | | (_)
875  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
876  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
877  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
878  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
879  *     | |
880  *     |_|
881  ******************************************************************************************************/
882
883 /**
884  * Gets the type of an ia32_Const.
885  */
886 unsigned get_ia32_Const_type(const ir_node *node) {
887         ia32_attr_t *attr = get_ia32_attr(node);
888
889         assert(is_ia32_Cnst(node) && "Need ia32_Const to get type");
890
891         return attr->data.tp;
892 }
893
894 /**
895  * Sets the type of an ia32_Const.
896  */
897 void set_ia32_Const_type(ir_node *node, int type) {
898         ia32_attr_t *attr = get_ia32_attr(node);
899
900         assert(is_ia32_Cnst(node) && "Need ia32_Const to set type");
901         assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
902
903         attr->data.tp = type;
904 }
905
906 /**
907  * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
908  */
909 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
910         ia32_attr_t *na = get_ia32_attr(node);
911         ia32_attr_t *ca = get_ia32_attr(cnst);
912
913         switch(get_ia32_Const_type(cnst)) {
914                 case ia32_Const:
915                         na->cnst_val.tv = ca->cnst_val.tv;
916                         na->cnst        = ca->cnst;
917                         set_ia32_immop_type(node, ia32_ImmConst);
918                         break;
919                 case ia32_SymConst:
920                         na->cnst_val.sc = ca->cnst_val.sc;
921                         na->cnst        = na->cnst_val.sc;
922                         set_ia32_immop_type(node, ia32_ImmSymConst);
923                         break;
924                 default:
925                         assert(0 && "Need ia32_Const to set Immop attr");
926         }
927 }
928
929 /**
930  * Copy the attributes from Immop to an Immop
931  */
932 void copy_ia32_Immop_attr(ir_node *dst, ir_node *src) {
933         ia32_attr_t *da = get_ia32_attr(dst);
934         ia32_attr_t *sa = get_ia32_attr(src);
935
936         switch(get_ia32_immop_type(src)) {
937                 case ia32_ImmConst:
938                         da->cnst_val.tv = sa->cnst_val.tv;
939                         da->cnst        = sa->cnst;
940                         set_ia32_immop_type(dst, ia32_ImmConst);
941                         break;
942                 case ia32_ImmSymConst:
943                         da->cnst_val.sc = sa->cnst_val.sc;
944                         da->cnst        = sa->cnst;
945                         set_ia32_immop_type(dst, ia32_ImmSymConst);
946                         break;
947                 default:
948                         assert(0 && "Need Immop to copy Immop attr");
949         }
950 }
951
952 /**
953  * Copy the attributes from a Firm Const to an ia32_Const
954  */
955 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
956         ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
957
958         assert(is_ia32_Cnst(ia32_cnst) && "Need ia32_Const to set Const attr");
959
960         switch (get_irn_opcode(cnst)) {
961                 case iro_Const:
962                         attr->data.tp     = ia32_Const;
963                         attr->cnst_val.tv = get_Const_tarval(cnst);
964                         attr->cnst        = get_ident_for_tv(attr->cnst_val.tv);
965                         break;
966                 case iro_SymConst:
967                         attr->data.tp     = ia32_SymConst;
968                         attr->cnst_val.sc = get_sc_ident(cnst);
969                         attr->cnst        = attr->cnst_val.sc;
970                         break;
971                 case iro_Unknown:
972                         assert(0 && "Unknown Const NYI");
973                         break;
974                 default:
975                         assert(0 && "Cannot create ia32_Const for this opcode");
976         }
977 }
978
979 /**
980  * Sets the AddrMode(S|D) attribute
981  */
982 void set_ia32_AddrMode(ir_node *node, char direction) {
983         ia32_attr_t *attr = get_ia32_attr(node);
984
985         switch (direction) {
986                 case 'D':
987                         attr->data.tp = ia32_AddrModeD;
988                         break;
989                 case 'S':
990                         attr->data.tp = ia32_AddrModeS;
991                         break;
992                 default:
993                         assert(0 && "wrong AM type");
994         }
995 }
996
997 /**
998  * Returns whether or not the node is an immediate operation with Const.
999  */
1000 int is_ia32_ImmConst(const ir_node *node) {
1001         ia32_attr_t *attr = get_ia32_attr(node);
1002         return (attr->data.imm_tp == ia32_ImmConst);
1003 }
1004
1005 /**
1006  * Returns whether or not the node is an immediate operation with SymConst.
1007  */
1008 int is_ia32_ImmSymConst(const ir_node *node) {
1009         ia32_attr_t *attr = get_ia32_attr(node);
1010         return (attr->data.imm_tp == ia32_ImmSymConst);
1011 }
1012
1013 /**
1014  * Returns whether or not the node is an AddrModeS node.
1015  */
1016 int is_ia32_AddrModeS(const ir_node *node) {
1017         ia32_attr_t *attr = get_ia32_attr(node);
1018         return (attr->data.tp == ia32_AddrModeS);
1019 }
1020
1021 /**
1022  * Returns whether or not the node is an AddrModeD node.
1023  */
1024 int is_ia32_AddrModeD(const ir_node *node) {
1025         ia32_attr_t *attr = get_ia32_attr(node);
1026         return (attr->data.tp == ia32_AddrModeD);
1027 }
1028
1029 /**
1030  * Checks if node is a Load or fLoad/vfLoad.
1031  */
1032 int is_ia32_Ld(const ir_node *node) {
1033         return is_ia32_Load(node) || is_ia32_fLoad(node) || is_ia32_vfld(node);
1034 }
1035
1036 /**
1037  * Checks if node is a Store or fStore/vfStore.
1038  */
1039 int is_ia32_St(const ir_node *node) {
1040         return is_ia32_Store(node) || is_ia32_fStore(node) || is_ia32_vfst(node);
1041 }
1042
1043 /**
1044  * Checks if node is a Const or fConst/vfConst.
1045  */
1046 int is_ia32_Cnst(const ir_node *node) {
1047         return is_ia32_Const(node) || is_ia32_fConst(node) || is_ia32_vfConst(node);
1048 }
1049
1050 /**
1051  * Returns the name of the OUT register at position pos.
1052  */
1053 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
1054         ia32_attr_t *attr = get_ia32_attr(node);
1055
1056         assert(is_ia32_irn(node) && "Not an ia32 node.");
1057         assert(pos < attr->data.n_res && "Invalid OUT position.");
1058         assert(attr->slots[pos]  && "No register assigned");
1059
1060         return arch_register_get_name(attr->slots[pos]);
1061 }
1062
1063 /**
1064  * Returns the index of the OUT register at position pos within its register class.
1065  */
1066 int get_ia32_out_regnr(const ir_node *node, int pos) {
1067         ia32_attr_t *attr = get_ia32_attr(node);
1068
1069         assert(is_ia32_irn(node) && "Not an ia32 node.");
1070         assert(pos < attr->data.n_res && "Invalid OUT position.");
1071         assert(attr->slots[pos]  && "No register assigned");
1072
1073         return arch_register_get_index(attr->slots[pos]);
1074 }
1075
1076 /**
1077  * Returns the OUT register at position pos.
1078  */
1079 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1080         ia32_attr_t *attr = get_ia32_attr(node);
1081
1082         assert(is_ia32_irn(node) && "Not an ia32 node.");
1083         assert(pos < attr->data.n_res && "Invalid OUT position.");
1084         assert(attr->slots[pos]  && "No register assigned");
1085
1086         return attr->slots[pos];
1087 }
1088
1089 /**
1090  * Allocates num register slots for node.
1091  */
1092 void alloc_ia32_reg_slots(ir_node *node, int num) {
1093         ia32_attr_t *attr = get_ia32_attr(node);
1094
1095         if (num) {
1096                 attr->slots = NEW_ARR_D(arch_register_t *, get_irg_obstack(get_irn_irg(node)), num);
1097                 memset(attr->slots, 0, sizeof(attr->slots[0]) * num);
1098         }
1099         else {
1100                 attr->slots = NULL;
1101         }
1102
1103         attr->data.n_res = num;
1104 }
1105
1106 /**
1107  * Initializes the nodes attributes.
1108  */
1109 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags, const ia32_register_req_t **in_reqs,
1110                                                   const ia32_register_req_t **out_reqs, int n_res)
1111 {
1112         set_ia32_flags(node, flags);
1113         set_ia32_in_req_all(node, in_reqs);
1114         set_ia32_out_req_all(node, out_reqs);
1115         alloc_ia32_reg_slots(node, n_res);
1116 }
1117
1118 /***************************************************************************************
1119  *                  _                            _                   _
1120  *                 | |                          | |                 | |
1121  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1122  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1123  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1124  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1125  *
1126  ***************************************************************************************/
1127
1128 /* default compare operation to compare immediate ops */
1129 int ia32_compare_immop_attr(ia32_attr_t *a, ia32_attr_t *b) {
1130         if (a->data.tp == b->data.tp) {
1131                 return a->cnst == b->cnst;
1132         }
1133
1134         return 1;
1135 }
1136
1137 /* copies the ia32 attributes */
1138 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node) {
1139         ia32_attr_t    *attr_old = get_ia32_attr(old_node);
1140         ia32_attr_t    *attr_new = get_ia32_attr(new_node);
1141         int             n_res    = get_ia32_n_res(old_node);
1142
1143         /* copy the attributes */
1144         memcpy(attr_new, attr_old, sizeof(*attr_new));
1145
1146         /* copy the register slots */
1147         attr_new->slots = NEW_ARR_D(arch_register_t *, get_irg_obstack(get_irn_irg(new_node)), n_res);
1148         memcpy((void *)attr_new->slots, (void *)attr_old->slots, sizeof(attr_new->slots[0]) * n_res);
1149 }
1150
1151 /**
1152  * Registers the ia32_copy_attr function for all ia32 opcodes.
1153  */
1154 void ia32_register_copy_attr_func(void) {
1155         unsigned i, f = get_ia32_opcode_first(), l = get_ia32_opcode_last();
1156
1157         for (i = f; i < l; i++) {
1158                 ir_op *op = get_irp_opcode(i);
1159                 op->ops.copy_attr = ia32_copy_attr;
1160         }
1161 }
1162
1163 static void ia32_register_additional_opcodes(int n) {
1164         /* we don't need any additional opcodes */
1165 }
1166
1167 /* Include the generated constructor functions */
1168 #include "gen_ia32_new_nodes.c.inl"