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