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