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