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