aece7a2617831a8ade4d57c378e3badb580ecd34
[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 /***********************************************************************************
46  *      _                                   _       _             __
47  *     | |                                 (_)     | |           / _|
48  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
49  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
50  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
51  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
52  *                       | |
53  *                       |_|
54  ***********************************************************************************/
55
56 /**
57  * Prints a tarval to file F.
58  * @param F         output file
59  * @param tv        tarval
60  * @param brackets  1 == print square brackets around tarval
61  */
62 static void fprintf_tv(FILE *F, tarval *tv, int brackets) {
63         char buf[1024];
64         tarval_snprintf(buf, sizeof(buf), tv);
65
66         if (brackets)
67                 fprintf(F, "[%s]", buf);
68         else
69                 fprintf(F, "%s", buf);
70 }
71
72 /**
73  * Returns the name of a SymConst.
74  * @param symc  the SymConst
75  * @return name of the SymConst
76  */
77 const char *get_sc_name(ir_node *symc) {
78         if (get_irn_opcode(symc) != iro_SymConst)
79                 return "NONE";
80
81         switch (get_SymConst_kind(symc)) {
82                 case symconst_addr_name:
83                         return get_id_str(get_SymConst_name(symc));
84
85                 case symconst_addr_ent:
86                         return get_entity_ld_name(get_SymConst_entity(symc));
87
88                 default:
89                         assert(0 && "Unsupported SymConst");
90         }
91
92         return NULL;
93 }
94
95 /**
96  * Returns a string containing the names of all registers within the limited bitset
97  */
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);
100         char     *p    = buf;
101         int       size = 0;
102         int       i, cnt;
103
104         req->limited(NULL, bs);
105
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);
109                         if (cnt < 0) {
110                                 fprintf(stderr, "dumper problem, exiting\n");
111                                 exit(1);
112                         }
113
114                         p    += cnt;
115                         size += cnt;
116
117                         if (size >= max)
118                                 break;
119                 }
120         }
121
122         return buf;
123 }
124
125 /**
126  * Dumps the register requirements for either in or out.
127  */
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);
132         int   i;
133
134         memset(buf, 0, 1024);
135
136         if (reqs) {
137                 for (i = 0; i < max; i++) {
138                         fprintf(F, "%sreq #%d =", dir, i);
139
140                         if (reqs[i]->req.type == arch_register_req_type_none) {
141                                 fprintf(F, " n/a");
142                         }
143
144                         if (reqs[i]->req.type & arch_register_req_type_normal) {
145                                 fprintf(F, " %s", reqs[i]->req.cls->name);
146                         }
147
148                         if (reqs[i]->req.type & arch_register_req_type_limited) {
149                                 fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
150                         }
151
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));
154                         }
155
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));
158                         }
159
160                         fprintf(F, "\n");
161                 }
162
163                 fprintf(F, "\n");
164         }
165         else {
166                 fprintf(F, "%sreq = N/A\n", dir);
167         }
168 }
169
170 /**
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
176  */
177 static int dump_node_ia32(ir_node *n, FILE *F, dump_reason_t reason) {
178         ir_mode     *mode = NULL;
179         int          bad  = 0;
180         int          i;
181         ia32_attr_t *attr;
182         const ia32_register_req_t **reqs;
183         const arch_register_t     **slots;
184
185         switch (reason) {
186                 case dump_node_opcode_txt:
187                         fprintf(F, "%s", get_irn_opname(n));
188                         break;
189
190                 case dump_node_mode_txt:
191                         mode = get_irn_mode(n);
192
193                         if (is_ia32_Load(n) || is_ia32_Store(n)) {
194                                 mode = get_ia32_ls_mode(n);
195                         }
196
197                         if (mode) {
198                                 fprintf(F, "[%s]", get_mode_name(mode));
199                         }
200                         else {
201                                 fprintf(F, "[?NOMODE?]");
202                         }
203                         break;
204
205                 case dump_node_nodeattr_txt:
206                         if (is_ia32_Call(n)) {
207                                 fprintf(F, "&%s ", get_ia32_sc(n));
208                         }
209                         else if (get_ia32_cnst(n)) {
210                                 char *pref = "";
211
212                                 if (get_ia32_sc(n)) {
213                                         pref = "SymC ";
214                                 }
215
216                                 fprintf(F, "[%s%s]", pref, get_ia32_cnst(n));
217                         }
218
219                         if (! is_ia32_Lea(n)) {
220                                 if (is_ia32_AddrModeS(n)) {
221                                         fprintf(F, "[AM S] ");
222                                 }
223                                 else if (is_ia32_AddrModeD(n)) {
224                                         fprintf(F, "[AM D] ");
225                                 }
226                         }
227
228                         break;
229
230                 case dump_node_info_txt:
231                         attr = get_ia32_attr(n);
232                         fprintf(F, "=== IA32 attr begin ===\n");
233
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);
238                         }
239
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);
244                         }
245
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++) {
250                                         if (slots[i]) {
251                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
252                                         }
253                                         else {
254                                                 fprintf(F, "reg #%d = n/a\n", i);
255                                         }
256                                 }
257                         }
258                         fprintf(F, "\n");
259
260                         /* dump op type */
261                         fprintf(F, "op = ");
262                         switch (attr->tp) {
263                                 case ia32_Normal:
264                                         fprintf(F, "Normal");
265                                         break;
266                                 case ia32_Const:
267                                         fprintf(F, "Const");
268                                         break;
269                                 case ia32_SymConst:
270                                         fprintf(F, "SymConst");
271                                         break;
272                                 case ia32_AddrModeD:
273                                         fprintf(F, "AM Dest (Load+Store)");
274                                         break;
275                                 case ia32_AddrModeS:
276                                         fprintf(F, "AM Source (Load)");
277                                         break;
278                                 default:
279                                         fprintf(F, "unknown (%d)", attr->tp);
280                                         break;
281                         }
282                         fprintf(F, "\n");
283
284
285                         /* dump supported am */
286                         fprintf(F, "AM support = ");
287                         switch (attr->am_support) {
288                                 case ia32_am_None:
289                                         fprintf(F, "none");
290                                         break;
291                                 case ia32_am_Source:
292                                         fprintf(F, "source only (Load)");
293                                         break;
294                                 case ia32_am_Dest:
295                                         fprintf(F, "dest only (Load+Store)");
296                                         break;
297                                 case ia32_am_Full:
298                                         fprintf(F, "full");
299                                         break;
300                                 default:
301                                         fprintf(F, "unknown (%d)", attr->am_support);
302                                         break;
303                         }
304                         fprintf(F, "\n");
305
306                         /* dump am flavour */
307                         fprintf(F, "AM flavour =");
308                         if (attr->am_flavour == ia32_am_N) {
309                                 fprintf(F, " none");
310                         }
311                         else {
312                                 if (attr->am_flavour & ia32_O) {
313                                         fprintf(F, " O");
314                                 }
315                                 if (attr->am_flavour & ia32_B) {
316                                         fprintf(F, " B");
317                                 }
318                                 if (attr->am_flavour & ia32_I) {
319                                         fprintf(F, " I");
320                                 }
321                                 if (attr->am_flavour & ia32_S) {
322                                         fprintf(F, " S");
323                                 }
324                         }
325                         fprintf(F, " (%d)\n", attr->am_flavour);
326
327                         /* dump AM offset */
328                         fprintf(F, "AM offset = ");
329                         if (attr->am_offs) {
330                                 fprintf(F, "%s", get_ia32_am_offs(n));
331                         }
332                         else {
333                                 fprintf(F, "n/a");
334                         }
335                         fprintf(F, "\n");
336
337                         /* dump AM scale */
338                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
339
340                         /* dump pn code */
341                         fprintf(F, "pn_code = %d\n", get_ia32_pncode(n));
342
343                         /* dump n_res */
344                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
345
346                         /* dump flags */
347                         fprintf(F, "flags =");
348                         if (attr->flags == arch_irn_flags_none) {
349                                 fprintf(F, " none");
350                         }
351                         else {
352                                 if (attr->flags & arch_irn_flags_dont_spill) {
353                                         fprintf(F, " unspillable");
354                                 }
355                                 if (attr->flags & arch_irn_flags_rematerializable) {
356                                         fprintf(F, " remat");
357                                 }
358                                 if (attr->flags & arch_irn_flags_ignore) {
359                                         fprintf(F, " ignore");
360                                 }
361                         }
362                         fprintf(F, " (%d)\n", attr->flags);
363
364                         fprintf(F, "=== IA32 attr end ===\n");
365                         /* end of: case dump_node_info_txt */
366                         break;
367         }
368
369         return bad;
370 }
371
372
373
374 /***************************************************************************************************
375  *        _   _                   _       __        _                    _   _               _
376  *       | | | |                 | |     / /       | |                  | | | |             | |
377  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
378  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
379  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
380  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
381  *                                        __/ |
382  *                                       |___/
383  ***************************************************************************************************/
384
385  static char *copy_str(char *dst, const char *src) {
386          dst = xcalloc(1, strlen(src) + 1);
387          strncpy(dst, src, strlen(src) + 1);
388          return dst;
389  }
390
391  static char *set_cnst_from_tv(char *cnst, tarval *tv) {
392          if (cnst) {
393                  free(cnst);
394          }
395
396          cnst = xcalloc(1, 64);
397          assert(tarval_snprintf(cnst, 63, tv));
398          return cnst;
399  }
400
401 /**
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 :-(
404  */
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);
408 }
409
410 /**
411  * Gets the type of an ia32 node.
412  */
413 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
414         ia32_attr_t *attr = get_ia32_attr(node);
415         return attr->tp;
416 }
417
418 /**
419  * Sets the type of an ia32 node.
420  */
421 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
422         ia32_attr_t *attr = get_ia32_attr(node);
423         attr->tp          = tp;
424 }
425
426 /**
427  * Gets the supported addrmode of an ia32 node
428  */
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;
432 }
433
434 /**
435  * Sets the supported addrmode of an ia32 node
436  */
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;
440 }
441
442 /**
443  * Gets the addrmode flavour of an ia32 node
444  */
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;
448 }
449
450 /**
451  * Sets the addrmode flavour of an ia32 node
452  */
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;
456 }
457
458 /**
459  * Joins all offsets to one string with adds.
460  */
461 char *get_ia32_am_offs(const ir_node *node) {
462         ia32_attr_t *attr = get_ia32_attr(node);
463         char        *res  = NULL;
464         int          size;
465
466         if (! attr->am_offs) {
467                 return NULL;
468         }
469
470         size = obstack_object_size(attr->am_offs);
471     if (size > 0) {
472                 res    = xcalloc(1, size + 2);
473                 res[0] = attr->offs_sign;
474                 memcpy(&res[1], obstack_base(attr->am_offs), size);
475     }
476
477         res[size + 1] = '\0';
478         return res;
479 }
480
481 /**
482  * Add an offset for addrmode.
483  */
484 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
485         ia32_attr_t *attr = get_ia32_attr(node);
486
487         if (! offset)
488                 return;
489
490         /* offset could already have an explicit sign */
491         /* -> supersede op if necessary               */
492         if (offset[0] == '-' || offset[0] == '+') {
493                 op = offset[0];
494                 offset++;
495         }
496
497         if (!attr->am_offs) {
498                 /* obstack is not initialized */
499                 attr->am_offs = xcalloc(1, sizeof(*(attr->am_offs)));
500                 obstack_init(attr->am_offs);
501                 attr->offs_sign = op;
502         }
503         else {
504                 /* If obstack is initialized, connect the new offset with op */
505                 obstack_printf(attr->am_offs, "%c", op);
506         }
507
508         obstack_printf(attr->am_offs, "%s", offset);
509 }
510
511 /**
512  * Add an offset for addrmode.
513  */
514 void add_ia32_am_offs(ir_node *node, char *offset) {
515         extend_ia32_am_offs(node, offset, '+');
516 }
517
518 /**
519  * Sub an offset for addrmode.
520  */
521 void sub_ia32_am_offs(ir_node *node, char *offset) {
522         extend_ia32_am_offs(node, offset, '-');
523 }
524
525 /**
526  * Gets the addr mode const.
527  */
528 int get_ia32_am_scale(const ir_node *node) {
529         ia32_attr_t *attr = get_ia32_attr(node);
530         return attr->am_scale;
531 }
532
533 /**
534  * Sets the index register scale for addrmode.
535  */
536 void set_ia32_am_scale(ir_node *node, int scale) {
537         ia32_attr_t *attr = get_ia32_attr(node);
538         attr->am_scale    = scale;
539 }
540
541 /**
542  * Return the tarval of an immediate operation or NULL in case of SymConst
543  */
544 tarval *get_ia32_Immop_tarval(const ir_node *node) {
545         ia32_attr_t *attr = get_ia32_attr(node);
546     return attr->tv;
547 }
548
549 /**
550  * Sets the attributes of an immediate operation to the specified tarval
551  */
552 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
553         ia32_attr_t *attr = get_ia32_attr(node);
554         attr->tv          = tv;
555         attr->cnst        = set_cnst_from_tv(attr->cnst, attr->tv);
556 }
557
558 /**
559  * Return the sc attribute.
560  */
561 char *get_ia32_sc(const ir_node *node) {
562         ia32_attr_t *attr = get_ia32_attr(node);
563         return attr->sc;
564 }
565
566 /**
567  * Sets the sc attribute.
568  */
569 void set_ia32_sc(ir_node *node, char *sc) {
570         ia32_attr_t *attr = get_ia32_attr(node);
571         attr->sc          = copy_str(attr->sc, sc);
572
573         if (attr->cnst) {
574                 free(attr->cnst);
575         }
576         attr->cnst = attr->sc;
577 }
578
579 /**
580  * Gets the string representation of the internal const (tv or symconst)
581  */
582 char *get_ia32_cnst(const ir_node *node) {
583         ia32_attr_t *attr = get_ia32_attr(node);
584         return attr->cnst;
585 }
586
587 /**
588  * Sets the uses_frame attribute.
589  */
590 void set_ia32_use_frame(ir_node *node, char flag) {
591         ia32_attr_t *attr = get_ia32_attr(node);
592         attr->use_frame = flag;
593 }
594
595 /**
596  * Gets the uses_frame attribute
597  */
598 char get_ia32_use_frame(const ir_node *node) {
599         ia32_attr_t *attr = get_ia32_attr(node);
600         return attr->use_frame;
601 }
602
603 /**
604  * Gets the mode of the stored/loaded value (only set for Store/Load)
605  */
606 ir_mode *get_ia32_ls_mode(const ir_node *node) {
607         ia32_attr_t *attr = get_ia32_attr(node);
608         return attr->ls_mode;
609 }
610
611 /**
612  * Sets the mode of the stored/loaded value (only set for Store/Load)
613  */
614 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
615         ia32_attr_t *attr = get_ia32_attr(node);
616         attr->ls_mode     = mode;
617 }
618
619 /**
620  * Returns the argument register requirements of an ia32 node.
621  */
622 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
623         ia32_attr_t *attr = get_ia32_attr(node);
624         return attr->in_req;
625 }
626
627 /**
628  * Returns the result register requirements of an ia32 node.
629  */
630 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
631         ia32_attr_t *attr = get_ia32_attr(node);
632         return attr->out_req;
633 }
634
635 /**
636  * Returns the argument register requirement at position pos of an ia32 node.
637  */
638 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
639         ia32_attr_t *attr = get_ia32_attr(node);
640         return attr->in_req[pos];
641 }
642
643 /**
644  * Returns the result register requirement at position pos of an ia32 node.
645  */
646 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
647         ia32_attr_t *attr = get_ia32_attr(node);
648         return attr->out_req[pos];
649 }
650
651 /**
652  * Sets the OUT register requirements at position pos.
653  */
654 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
655         ia32_attr_t *attr  = get_ia32_attr(node);
656         attr->out_req[pos] = req;
657 }
658
659 /**
660  * Sets the IN register requirements at position pos.
661  */
662 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
663         ia32_attr_t *attr = get_ia32_attr(node);
664         attr->in_req[pos] = req;
665 }
666
667 /**
668  * Returns the register flag of an ia32 node.
669  */
670 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
671         ia32_attr_t *attr = get_ia32_attr(node);
672         return attr->flags;
673 }
674
675 /**
676  * Sets the register flag of an ia32 node.
677  */
678 void set_ia32_flags(const ir_node *node, arch_irn_flags_t flags) {
679         ia32_attr_t *attr = get_ia32_attr(node);
680         attr->flags       = flags;
681 }
682
683 /**
684  * Returns the result register slots of an ia32 node.
685  */
686 const arch_register_t **get_ia32_slots(const ir_node *node) {
687         ia32_attr_t *attr = get_ia32_attr(node);
688         return attr->slots;
689 }
690
691 /**
692  * Returns the name of the OUT register at position pos.
693  */
694 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
695         ia32_attr_t *attr = get_ia32_attr(node);
696
697         assert(is_ia32_irn(node) && "Not an ia32 node.");
698         assert(pos < attr->n_res && "Invalid OUT position.");
699         assert(attr->slots[pos]  && "No register assigned");
700
701         return arch_register_get_name(attr->slots[pos]);
702 }
703
704 /**
705  * Returns the index of the OUT register at position pos within its register class.
706  */
707 int get_ia32_out_regnr(const ir_node *node, int pos) {
708         ia32_attr_t *attr = get_ia32_attr(node);
709
710         assert(is_ia32_irn(node) && "Not an ia32 node.");
711         assert(pos < attr->n_res && "Invalid OUT position.");
712         assert(attr->slots[pos]  && "No register assigned");
713
714         return arch_register_get_index(attr->slots[pos]);
715 }
716
717 /**
718  * Returns the OUT register at position pos.
719  */
720 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
721         ia32_attr_t *attr = get_ia32_attr(node);
722
723         assert(is_ia32_irn(node) && "Not an ia32 node.");
724         assert(pos < attr->n_res && "Invalid OUT position.");
725         assert(attr->slots[pos]  && "No register assigned");
726
727         return attr->slots[pos];
728 }
729
730 /**
731  * Sets the number of results.
732  */
733 void set_ia32_n_res(ir_node *node, int n_res) {
734         ia32_attr_t *attr = get_ia32_attr(node);
735         attr->n_res       = n_res;
736 }
737
738 /**
739  * Returns the number of results.
740  */
741 int get_ia32_n_res(const ir_node *node) {
742         ia32_attr_t *attr = get_ia32_attr(node);
743         return attr->n_res;
744 }
745
746 /**
747  * Returns the flavour of an ia32 node,
748  */
749 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
750         ia32_attr_t *attr = get_ia32_attr(node);
751         return attr->op_flav;
752 }
753
754 /**
755  * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
756  */
757 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
758         ia32_attr_t *attr = get_ia32_attr(node);
759         attr->op_flav     = op_flav;
760 }
761
762 /**
763  * Returns the projnum code.
764  */
765 long get_ia32_pncode(const ir_node *node) {
766         ia32_attr_t *attr = get_ia32_attr(node);
767         return attr->pn_code;
768 }
769
770 /**
771  * Sets the projnum code
772  */
773 void set_ia32_pncode(ir_node *node, long code) {
774         ia32_attr_t *attr = get_ia32_attr(node);
775         attr->pn_code     = code;
776 }
777
778
779 /******************************************************************************************************
780  *                      _       _         _   _           __                  _   _
781  *                     (_)     | |       | | | |         / _|                | | (_)
782  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
783  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
784  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
785  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
786  *     | |
787  *     |_|
788  ******************************************************************************************************/
789
790 /**
791  * Gets the type of an ia32_Const.
792  */
793 unsigned get_ia32_Const_type(const ir_node *node) {
794         ia32_attr_t *attr = get_ia32_attr(node);
795
796         assert((is_ia32_Const(node) || is_ia32_fConst(node)) && "Need ia32_Const to get type");
797
798         return attr->tp;
799 }
800
801 /**
802  * Sets the type of an ia32_Const.
803  */
804 void set_ia32_Const_type(ir_node *node, int type) {
805         ia32_attr_t *attr = get_ia32_attr(node);
806
807         assert((is_ia32_Const(node) || is_ia32_fConst(node)) && "Need ia32_Const to set type");
808         assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
809
810         attr->tp = type;
811 }
812
813 /**
814  * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
815  */
816 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
817         ia32_attr_t *na = get_ia32_attr(node);
818         ia32_attr_t *ca = get_ia32_attr(cnst);
819
820         assert((is_ia32_Const(cnst) || is_ia32_fConst(cnst)) && "Need ia32_Const to set Immop attr");
821
822         na->tv = ca->tv;
823
824         if (ca->sc) {
825                 na->sc   = copy_str(na->sc, ca->sc);
826                 na->cnst = na->sc;
827         }
828         else {
829                 na->cnst = set_cnst_from_tv(na->cnst, na->tv);
830                 na->sc   = NULL;
831         }
832 }
833
834 /**
835  * Copy the attributes from a Const to an ia32_Const
836  */
837 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
838         ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
839
840         assert((is_ia32_Const(ia32_cnst) || is_ia32_fConst(ia32_cnst)) && "Need ia32_Const to set Const attr");
841
842         switch (get_irn_opcode(cnst)) {
843                 case iro_Const:
844                         attr->tp   = ia32_Const;
845                         attr->tv   = get_Const_tarval(cnst);
846                         attr->cnst = set_cnst_from_tv(attr->cnst, attr->tv);
847                         break;
848                 case iro_SymConst:
849                         attr->tp   = ia32_SymConst;
850                         attr->tv   = NULL;
851                         attr->sc   = copy_str(attr->sc, get_sc_name(cnst));
852                         attr->cnst = attr->sc;
853                         break;
854                 case iro_Unknown:
855                         assert(0 && "Unknown Const NYI");
856                         break;
857                 default:
858                         assert(0 && "Cannot create ia32_Const for this opcode");
859         }
860 }
861
862 /**
863  * Sets the AddrMode(S|D) attribute
864  */
865 void set_ia32_AddrMode(ir_node *node, char direction) {
866         ia32_attr_t *attr = get_ia32_attr(node);
867
868         switch (direction) {
869                 case 'D':
870                         attr->tp = ia32_AddrModeD;
871                         break;
872                 case 'S':
873                         attr->tp = ia32_AddrModeS;
874                         break;
875                 default:
876                         assert(0 && "wrong AM type");
877         }
878 }
879
880 /**
881  * Returns whether or not the node is an AddrModeS node.
882  */
883 int is_ia32_AddrModeS(const ir_node *node) {
884         ia32_attr_t *attr = get_ia32_attr(node);
885         return (attr->tp == ia32_AddrModeS);
886 }
887
888 /**
889  * Returns whether or not the node is an AddrModeD node.
890  */
891 int is_ia32_AddrModeD(const ir_node *node) {
892         ia32_attr_t *attr = get_ia32_attr(node);
893         return (attr->tp == ia32_AddrModeD);
894 }
895
896 /**
897  * Checks if node is a Load or fLoad.
898  */
899 int is_ia32_Ld(const ir_node *node) {
900         return is_ia32_Load(node) || is_ia32_fLoad(node);
901 }
902
903 /**
904  * Checks if node is a Store or fStore.
905  */
906 int is_ia32_St(const ir_node *node) {
907         return is_ia32_Store(node) || is_ia32_fStore(node);
908 }
909
910 /***************************************************************************************
911  *                  _                            _                   _
912  *                 | |                          | |                 | |
913  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
914  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
915  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
916  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
917  *
918  ***************************************************************************************/
919
920 /* Include the generated constructor functions */
921 #include "gen_ia32_new_nodes.c.inl"