added new callback to set frame entity
[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
247                         /* dump supported am */
248                         fprintf(F, "AM support = ");
249                         switch (get_ia32_am_support(n)) {
250                                 case ia32_am_None:
251                                         fprintf(F, "none");
252                                         break;
253                                 case ia32_am_Source:
254                                         fprintf(F, "source only (Load)");
255                                         break;
256                                 case ia32_am_Dest:
257                                         fprintf(F, "dest only (Load+Store)");
258                                         break;
259                                 case ia32_am_Full:
260                                         fprintf(F, "full");
261                                         break;
262                                 default:
263                                         fprintf(F, "unknown (%d)", get_ia32_am_support(n));
264                                         break;
265                         }
266                         fprintf(F, "\n");
267
268                         /* dump am flavour */
269                         fprintf(F, "AM flavour =");
270                         am_flav = get_ia32_am_flavour(n);
271                         if (am_flav == ia32_am_N) {
272                                 fprintf(F, " none");
273                         }
274                         else {
275                                 if (am_flav & ia32_O) {
276                                         fprintf(F, " O");
277                                 }
278                                 if (am_flav & ia32_B) {
279                                         fprintf(F, " B");
280                                 }
281                                 if (am_flav & ia32_I) {
282                                         fprintf(F, " I");
283                                 }
284                                 if (am_flav & ia32_S) {
285                                         fprintf(F, " S");
286                                 }
287                         }
288                         fprintf(F, " (%d)\n", am_flav);
289
290                         /* dump AM offset */
291                         fprintf(F, "AM offset = ");
292                         if (get_ia32_am_offs(n)) {
293                                 fprintf(F, "%s", get_ia32_am_offs(n));
294                         }
295                         else {
296                                 fprintf(F, "n/a");
297                         }
298                         fprintf(F, "\n");
299
300                         /* dump AM scale */
301                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
302
303                         /* dump pn code */
304                         fprintf(F, "pn_code = %ld\n", get_ia32_pncode(n));
305
306                         /* dump n_res */
307                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
308
309                         /* dump use_frame */
310                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
311
312                         /* commutative */
313                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
314
315                         /* dump flags */
316                         fprintf(F, "flags =");
317                         flags = get_ia32_flags(n);
318                         if (flags == arch_irn_flags_none) {
319                                 fprintf(F, " none");
320                         }
321                         else {
322                                 if (flags & arch_irn_flags_dont_spill) {
323                                         fprintf(F, " unspillable");
324                                 }
325                                 if (flags & arch_irn_flags_rematerializable) {
326                                         fprintf(F, " remat");
327                                 }
328                                 if (flags & arch_irn_flags_ignore) {
329                                         fprintf(F, " ignore");
330                                 }
331                         }
332                         fprintf(F, " (%d)\n", flags);
333
334                         /* dump frame entity */
335                         fprintf(F, "frame entity = ");
336                         if (get_ia32_frame_ent(n)) {
337                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
338                         }
339                         else {
340                                 fprintf(F, "n/a");
341                         }
342                         fprintf(F, "\n");
343
344 #ifndef NDEBUG
345                         /* dump original ir node name */
346                         fprintf(F, "orig node = ");
347                         if (get_ia32_orig_node(n)) {
348                                 fprintf(F, "%s", get_ia32_orig_node(n));
349                         }
350                         else {
351                                 fprintf(F, "n/a");
352                         }
353                         fprintf(F, "\n");
354 #endif /* NDEBUG */
355
356                         fprintf(F, "=== IA32 attr end ===\n");
357                         /* end of: case dump_node_info_txt */
358                         break;
359         }
360
361         return bad;
362 }
363
364
365
366 /***************************************************************************************************
367  *        _   _                   _       __        _                    _   _               _
368  *       | | | |                 | |     / /       | |                  | | | |             | |
369  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
370  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
371  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
372  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
373  *                                        __/ |
374  *                                       |___/
375  ***************************************************************************************************/
376
377 /**
378  * Returns an ident for the given tarval tv.
379  */
380 static ident *get_ident_for_tv(tarval *tv) {
381         char buf[1024];
382         int len = tarval_snprintf(buf, sizeof(buf), tv);
383         assert(len);
384         return new_id_from_str(buf);
385 }
386
387 /**
388  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
389  * Firm was made by people hating const :-(
390  */
391 ia32_attr_t *get_ia32_attr(const ir_node *node) {
392         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
393         return (ia32_attr_t *)get_irn_generic_attr((ir_node *)node);
394 }
395
396 /**
397  * Gets the type of an ia32 node.
398  */
399 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
400         ia32_attr_t *attr = get_ia32_attr(node);
401         return attr->data.tp;
402 }
403
404 /**
405  * Sets the type of an ia32 node.
406  */
407 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
408         ia32_attr_t *attr = get_ia32_attr(node);
409         attr->data.tp     = tp;
410 }
411
412 /**
413  * Gets the immediate op type of an ia32 node.
414  */
415 ia32_immop_type_t get_ia32_immop_type(const ir_node *node) {
416         ia32_attr_t *attr = get_ia32_attr(node);
417         return attr->data.imm_tp;
418 }
419
420 /**
421  * Sets the immediate op type of an ia32 node.
422  */
423 void set_ia32_immop_type(ir_node *node, ia32_immop_type_t tp) {
424         ia32_attr_t *attr = get_ia32_attr(node);
425         attr->data.imm_tp = tp;
426 }
427
428 /**
429  * Gets the supported addrmode of an ia32 node
430  */
431 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
432         ia32_attr_t *attr = get_ia32_attr(node);
433         return attr->data.am_support;
434 }
435
436 /**
437  * Sets the supported addrmode of an ia32 node
438  */
439 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
440         ia32_attr_t *attr     = get_ia32_attr(node);
441         attr->data.am_support = am_tp;
442 }
443
444 /**
445  * Gets the addrmode flavour of an ia32 node
446  */
447 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
448         ia32_attr_t *attr = get_ia32_attr(node);
449         return attr->data.am_flavour;
450 }
451
452 /**
453  * Sets the addrmode flavour of an ia32 node
454  */
455 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
456         ia32_attr_t *attr     = get_ia32_attr(node);
457         attr->data.am_flavour = am_flavour;
458 }
459
460 /**
461  * Joins all offsets to one string with adds.
462  */
463 char *get_ia32_am_offs(const ir_node *node) {
464         ia32_attr_t *attr = get_ia32_attr(node);
465         char        *res  = NULL;
466         int          size;
467
468         if (! attr->am_offs) {
469                 return NULL;
470         }
471
472         if (! attr->plain_offs)
473                 attr->plain_offs = (struct obstack *)_new_arr_d(get_irg_obstack(get_irn_irg(node)), 1, sizeof(*(attr->plain_offs)));
474         else
475                 obstack_free(attr->plain_offs, NULL);
476
477         obstack_init(attr->plain_offs);
478
479         size = obstack_object_size(attr->am_offs);
480         if (size > 0) {
481                 res = obstack_alloc(attr->plain_offs, size + 2);
482                 res[0] = attr->data.offs_sign ? '-' : '+';
483                 memcpy(&res[1], obstack_base(attr->am_offs), size);
484                 res[size + 1] = '\0';
485         }
486
487         return res;
488 }
489
490 /**
491  * Add an offset for addrmode.
492  */
493 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
494         ia32_attr_t *attr = get_ia32_attr(node);
495
496         if (! offset || strlen(offset) < 1)
497                 return;
498
499         /* offset could already have an explicit sign */
500         /* -> supersede op if necessary               */
501         if (offset[0] == '-' || offset[0] == '+') {
502                 if (offset[0] == '-') {
503                         op = (op == '-') ? '+' : '-';
504                 }
505
506                 /* skip explicit sign */
507                 offset++;
508         }
509
510         if (! attr->am_offs) {
511                 /* obstack is not initialized */
512                 attr->am_offs = (struct obstack *)_new_arr_d(get_irg_obstack(get_irn_irg(node)), 1, sizeof(*(attr->am_offs)));
513                 obstack_init(attr->am_offs);
514
515                 attr->data.offs_sign = (op == '-') ? 1 : 0;
516         }
517         else {
518                 /* If obstack is initialized, connect the new offset with op */
519                 obstack_printf(attr->am_offs, "%c", op);
520         }
521
522         obstack_printf(attr->am_offs, "%s", offset);
523 }
524
525 /**
526  * Add an offset for addrmode.
527  */
528 void add_ia32_am_offs(ir_node *node, const char *offset) {
529         extend_ia32_am_offs(node, (char *)offset, '+');
530 }
531
532 /**
533  * Sub an offset for addrmode.
534  */
535 void sub_ia32_am_offs(ir_node *node, const char *offset) {
536         extend_ia32_am_offs(node, (char *)offset, '-');
537 }
538
539 /**
540  * Returns the symconst ident associated to addrmode.
541  */
542 ident *get_ia32_am_sc(const ir_node *node) {
543         ia32_attr_t *attr = get_ia32_attr(node);
544         return attr->am_sc;
545 }
546
547 /**
548  * Sets the symconst ident associated to addrmode.
549  */
550 void set_ia32_am_sc(ir_node *node, ident *sc) {
551         ia32_attr_t *attr = get_ia32_attr(node);
552         attr->am_sc       = sc;
553 }
554
555 /**
556  * Sets the sign bit for address mode symconst.
557  */
558 void set_ia32_am_sc_sign(ir_node *node) {
559         ia32_attr_t *attr     = get_ia32_attr(node);
560         attr->data.am_sc_sign = 1;
561 }
562
563 /**
564  * Clears the sign bit for address mode symconst.
565  */
566 void clear_ia32_am_sc_sign(ir_node *node) {
567         ia32_attr_t *attr     = get_ia32_attr(node);
568         attr->data.am_sc_sign = 0;
569 }
570
571 /**
572  * Returns the sign bit for address mode symconst.
573  */
574 int is_ia32_am_sc_sign(const ir_node *node) {
575         ia32_attr_t *attr = get_ia32_attr(node);
576         return attr->data.am_sc_sign;
577 }
578
579 /**
580  * Gets the addr mode const.
581  */
582 int get_ia32_am_scale(const ir_node *node) {
583         ia32_attr_t *attr = get_ia32_attr(node);
584         return attr->data.am_scale;
585 }
586
587 /**
588  * Sets the index register scale for addrmode.
589  */
590 void set_ia32_am_scale(ir_node *node, int scale) {
591         ia32_attr_t *attr   = get_ia32_attr(node);
592         attr->data.am_scale = scale;
593 }
594
595 /**
596  * Return the tarval of an immediate operation or NULL in case of SymConst
597  */
598 tarval *get_ia32_Immop_tarval(const ir_node *node) {
599         ia32_attr_t *attr = get_ia32_attr(node);
600     return attr->cnst_val.tv;
601 }
602
603 /**
604  * Sets the attributes of an immediate operation to the specified tarval
605  */
606 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
607         ia32_attr_t *attr = get_ia32_attr(node);
608         attr->cnst_val.tv = tv;
609         attr->cnst        = get_ident_for_tv(tv);
610 }
611
612 /**
613  * Return the sc attribute.
614  */
615 ident *get_ia32_sc(const ir_node *node) {
616         ia32_attr_t *attr = get_ia32_attr(node);
617         return attr->cnst_val.sc;
618 }
619
620 /**
621  * Sets the sc attribute.
622  */
623 void set_ia32_sc(ir_node *node, ident *sc) {
624         ia32_attr_t *attr = get_ia32_attr(node);
625         attr->cnst_val.sc = sc;
626         attr->cnst        = attr->cnst_val.sc;
627 }
628
629 /**
630  * Gets the string representation of the internal const (tv or symconst)
631  */
632 const char *get_ia32_cnst(const ir_node *node) {
633         ia32_attr_t *attr = get_ia32_attr(node);
634         if (! attr->cnst)
635                 return NULL;
636         return get_id_str(attr->cnst);
637 }
638
639 /**
640  * Sets the string representation of the internal const.
641  */
642 void set_ia32_cnst(ir_node *node, char *cnst) {
643         ia32_attr_t *attr = get_ia32_attr(node);
644         attr->cnst        = new_id_from_str(cnst);
645 }
646
647 /**
648  * Gets the ident representation of the internal const (tv or symconst)
649  */
650 ident *get_ia32_id_cnst(const ir_node *node) {
651         ia32_attr_t *attr = get_ia32_attr(node);
652         return attr->cnst;
653 }
654
655 /**
656  * Sets the ident representation of the internal const.
657  */
658 void set_ia32_id_cnst(ir_node *node, ident *cnst) {
659         ia32_attr_t *attr = get_ia32_attr(node);
660         attr->cnst        = cnst;
661 }
662
663 /**
664  * Sets the uses_frame flag.
665  */
666 void set_ia32_use_frame(ir_node *node) {
667         ia32_attr_t *attr    = get_ia32_attr(node);
668         attr->data.use_frame = 1;
669 }
670
671 /**
672  * Clears the uses_frame flag.
673  */
674 void clear_ia32_use_frame(ir_node *node) {
675         ia32_attr_t *attr    = get_ia32_attr(node);
676         attr->data.use_frame = 0;
677 }
678
679 /**
680  * Gets the uses_frame flag.
681  */
682 int is_ia32_use_frame(const ir_node *node) {
683         ia32_attr_t *attr = get_ia32_attr(node);
684         return attr->data.use_frame;
685 }
686
687 /**
688  * Sets node to commutative.
689  */
690 void set_ia32_commutative(ir_node *node) {
691         ia32_attr_t *attr         = get_ia32_attr(node);
692         attr->data.is_commutative = 1;
693 }
694
695 /**
696  * Sets node to non-commutative.
697  */
698 void clear_ia32_commutative(ir_node *node) {
699         ia32_attr_t *attr         = get_ia32_attr(node);
700         attr->data.is_commutative = 0;
701 }
702
703 /**
704  * Checks if node is commutative.
705  */
706 int is_ia32_commutative(const ir_node *node) {
707         ia32_attr_t *attr = get_ia32_attr(node);
708         return attr->data.is_commutative;
709 }
710
711 /**
712  * Sets node emit_cl.
713  */
714 void set_ia32_emit_cl(ir_node *node) {
715         ia32_attr_t *attr  = get_ia32_attr(node);
716         attr->data.emit_cl = 1;
717 }
718
719 /**
720  * Clears node emit_cl.
721  */
722 void clear_ia32_emit_cl(ir_node *node) {
723         ia32_attr_t *attr  = get_ia32_attr(node);
724         attr->data.emit_cl = 0;
725 }
726
727 /**
728  * Checks if node needs %cl.
729  */
730 int is_ia32_emit_cl(const ir_node *node) {
731         ia32_attr_t *attr = get_ia32_attr(node);
732         return attr->data.emit_cl;
733 }
734
735 /**
736  * Sets node got_lea.
737  */
738 void set_ia32_got_lea(ir_node *node) {
739         ia32_attr_t *attr  = get_ia32_attr(node);
740         attr->data.got_lea = 1;
741 }
742
743 /**
744  * Clears node got_lea.
745  */
746 void clear_ia32_got_lea(ir_node *node) {
747         ia32_attr_t *attr  = get_ia32_attr(node);
748         attr->data.got_lea = 0;
749 }
750
751 /**
752  * Checks if node got lea.
753  */
754 int is_ia32_got_lea(const ir_node *node) {
755         ia32_attr_t *attr = get_ia32_attr(node);
756         return attr->data.got_lea;
757 }
758
759 /**
760  * Sets node got_reload.
761  */
762 void set_ia32_got_reload(ir_node *node) {
763         ia32_attr_t *attr     = get_ia32_attr(node);
764         attr->data.got_reload = 1;
765 }
766
767 /**
768  * Clears node got_reload.
769  */
770 void clear_ia32_got_reload(ir_node *node) {
771         ia32_attr_t *attr     = get_ia32_attr(node);
772         attr->data.got_reload = 0;
773 }
774
775 /**
776  * Checks if node got reload.
777  */
778 int is_ia32_got_reload(const ir_node *node) {
779         ia32_attr_t *attr = get_ia32_attr(node);
780         return attr->data.got_reload;
781 }
782
783 /**
784  * Gets the mode of the stored/loaded value (only set for Store/Load)
785  */
786 ir_mode *get_ia32_ls_mode(const ir_node *node) {
787         ia32_attr_t *attr = get_ia32_attr(node);
788         return attr->ls_mode;
789 }
790
791 /**
792  * Sets the mode of the stored/loaded value (only set for Store/Load)
793  */
794 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
795         ia32_attr_t *attr = get_ia32_attr(node);
796         attr->ls_mode     = mode;
797 }
798
799 /**
800  * Gets the mode of the result.
801  */
802 ir_mode *get_ia32_res_mode(const ir_node *node) {
803         ia32_attr_t *attr = get_ia32_attr(node);
804         return attr->res_mode;
805 }
806
807 /**
808  * Sets the mode of the result.
809  */
810 void set_ia32_res_mode(ir_node *node, ir_mode *mode) {
811         ia32_attr_t *attr = get_ia32_attr(node);
812         attr->res_mode    = mode;
813 }
814
815 /**
816  * Gets the source mode of conversion.
817  */
818 ir_mode *get_ia32_src_mode(const ir_node *node) {
819         ia32_attr_t *attr = get_ia32_attr(node);
820         return attr->src_mode;
821 }
822
823 /**
824  * Sets the source mode of conversion.
825  */
826 void set_ia32_src_mode(ir_node *node, ir_mode *mode) {
827         ia32_attr_t *attr = get_ia32_attr(node);
828         attr->src_mode    = mode;
829 }
830
831 /**
832  * Gets the target mode of conversion.
833  */
834 ir_mode *get_ia32_tgt_mode(const ir_node *node) {
835         ia32_attr_t *attr = get_ia32_attr(node);
836         return attr->tgt_mode;
837 }
838
839 /**
840  * Sets the target mode of conversion.
841  */
842 void set_ia32_tgt_mode(ir_node *node, ir_mode *mode) {
843         ia32_attr_t *attr = get_ia32_attr(node);
844         attr->tgt_mode    = mode;
845 }
846
847 /**
848  * Gets the frame entity assigned to this node.
849  */
850 entity *get_ia32_frame_ent(const ir_node *node) {
851         ia32_attr_t *attr = get_ia32_attr(node);
852         return attr->frame_ent;
853 }
854
855 /**
856  * Sets the frame entity for this node.
857  */
858 void set_ia32_frame_ent(ir_node *node, entity *ent) {
859         ia32_attr_t *attr = get_ia32_attr(node);
860         attr->frame_ent   = ent;
861         set_ia32_use_frame(node);
862 }
863
864 /**
865  * Returns the argument register requirements of an ia32 node.
866  */
867 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
868         ia32_attr_t *attr = get_ia32_attr(node);
869         return attr->in_req;
870 }
871
872 /**
873  * Sets the argument register requirements of an ia32 node.
874  */
875 void set_ia32_in_req_all(ir_node *node, const ia32_register_req_t **reqs) {
876         ia32_attr_t *attr = get_ia32_attr(node);
877         attr->in_req      = reqs;
878 }
879
880 /**
881  * Returns the result register requirements of an ia32 node.
882  */
883 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
884         ia32_attr_t *attr = get_ia32_attr(node);
885         return attr->out_req;
886 }
887
888 /**
889  * Sets the result register requirements of an ia32 node.
890  */
891 void set_ia32_out_req_all(ir_node *node, const ia32_register_req_t **reqs) {
892         ia32_attr_t *attr = get_ia32_attr(node);
893         attr->out_req     = reqs;
894 }
895
896 /**
897  * Returns the argument register requirement at position pos of an ia32 node.
898  */
899 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
900         ia32_attr_t *attr = get_ia32_attr(node);
901         return attr->in_req[pos];
902 }
903
904 /**
905  * Returns the result register requirement at position pos of an ia32 node.
906  */
907 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
908         ia32_attr_t *attr = get_ia32_attr(node);
909         return attr->out_req[pos];
910 }
911
912 /**
913  * Sets the OUT register requirements at position pos.
914  */
915 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
916         ia32_attr_t *attr  = get_ia32_attr(node);
917         attr->out_req[pos] = req;
918 }
919
920 /**
921  * Sets the IN register requirements at position pos.
922  */
923 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
924         ia32_attr_t *attr = get_ia32_attr(node);
925         attr->in_req[pos] = req;
926 }
927
928 /**
929  * Returns the register flag of an ia32 node.
930  */
931 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
932         ia32_attr_t *attr = get_ia32_attr(node);
933         return attr->data.flags;
934 }
935
936 /**
937  * Sets the register flag of an ia32 node.
938  */
939 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
940         ia32_attr_t *attr = get_ia32_attr(node);
941         attr->data.flags  = flags;
942 }
943
944 /**
945  * Returns the result register slots of an ia32 node.
946  */
947 const arch_register_t **get_ia32_slots(const ir_node *node) {
948         ia32_attr_t *attr = get_ia32_attr(node);
949         return attr->slots;
950 }
951
952 /**
953  * Sets the number of results.
954  */
955 void set_ia32_n_res(ir_node *node, int n_res) {
956         ia32_attr_t *attr = get_ia32_attr(node);
957         attr->data.n_res  = n_res;
958 }
959
960 /**
961  * Returns the number of results.
962  */
963 int get_ia32_n_res(const ir_node *node) {
964         ia32_attr_t *attr = get_ia32_attr(node);
965         return attr->data.n_res;
966 }
967
968 /**
969  * Returns the flavour of an ia32 node,
970  */
971 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
972         ia32_attr_t *attr = get_ia32_attr(node);
973         return attr->data.op_flav;
974 }
975
976 /**
977  * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
978  */
979 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
980         ia32_attr_t *attr  = get_ia32_attr(node);
981         attr->data.op_flav = op_flav;
982 }
983
984 /**
985  * Returns the projnum code.
986  */
987 long get_ia32_pncode(const ir_node *node) {
988         ia32_attr_t *attr = get_ia32_attr(node);
989         return attr->pn_code;
990 }
991
992 /**
993  * Sets the projnum code
994  */
995 void set_ia32_pncode(ir_node *node, long code) {
996         ia32_attr_t *attr = get_ia32_attr(node);
997         attr->pn_code     = code;
998 }
999
1000 #ifndef NDEBUG
1001
1002 /**
1003  * Returns the name of the original ir node.
1004  */
1005 const char *get_ia32_orig_node(const ir_node *node) {
1006         ia32_attr_t *attr = get_ia32_attr(node);
1007         return attr->orig_node;
1008 }
1009
1010 /**
1011  * Sets the name of the original ir node.
1012  */
1013 void set_ia32_orig_node(ir_node *node, const char *name) {
1014         ia32_attr_t *attr = get_ia32_attr(node);
1015         attr->orig_node   = name;
1016 }
1017
1018 #endif /* NDEBUG */
1019
1020 /******************************************************************************************************
1021  *                      _       _         _   _           __                  _   _
1022  *                     (_)     | |       | | | |         / _|                | | (_)
1023  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
1024  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
1025  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
1026  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
1027  *     | |
1028  *     |_|
1029  ******************************************************************************************************/
1030
1031 /**
1032  * Gets the type of an ia32_Const.
1033  */
1034 unsigned get_ia32_Const_type(const ir_node *node) {
1035         ia32_attr_t *attr = get_ia32_attr(node);
1036
1037         assert(is_ia32_Cnst(node) && "Need ia32_Const to get type");
1038
1039         return attr->data.tp;
1040 }
1041
1042 /**
1043  * Sets the type of an ia32_Const.
1044  */
1045 void set_ia32_Const_type(ir_node *node, int type) {
1046         ia32_attr_t *attr = get_ia32_attr(node);
1047
1048         assert(is_ia32_Cnst(node) && "Need ia32_Const to set type");
1049         assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
1050
1051         attr->data.tp = type;
1052 }
1053
1054 /**
1055  * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
1056  */
1057 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
1058         ia32_attr_t *na = get_ia32_attr(node);
1059         ia32_attr_t *ca = get_ia32_attr(cnst);
1060
1061         switch(get_ia32_Const_type(cnst)) {
1062                 case ia32_Const:
1063                         na->cnst_val.tv = ca->cnst_val.tv;
1064                         na->cnst        = ca->cnst;
1065                         set_ia32_immop_type(node, ia32_ImmConst);
1066                         break;
1067                 case ia32_SymConst:
1068                         na->cnst_val.sc = ca->cnst_val.sc;
1069                         na->cnst        = na->cnst_val.sc;
1070                         set_ia32_immop_type(node, ia32_ImmSymConst);
1071                         break;
1072                 default:
1073                         assert(0 && "Need ia32_Const to set Immop attr");
1074         }
1075 }
1076
1077 /**
1078  * Copy the attributes from Immop to an Immop
1079  */
1080 void copy_ia32_Immop_attr(ir_node *dst, ir_node *src) {
1081         ia32_attr_t *da = get_ia32_attr(dst);
1082         ia32_attr_t *sa = get_ia32_attr(src);
1083
1084         switch(get_ia32_immop_type(src)) {
1085                 case ia32_ImmConst:
1086                         da->cnst_val.tv = sa->cnst_val.tv;
1087                         da->cnst        = sa->cnst;
1088                         set_ia32_immop_type(dst, ia32_ImmConst);
1089                         break;
1090                 case ia32_ImmSymConst:
1091                         da->cnst_val.sc = sa->cnst_val.sc;
1092                         da->cnst        = sa->cnst;
1093                         set_ia32_immop_type(dst, ia32_ImmSymConst);
1094                         break;
1095                 default:
1096                         assert(0 && "Need Immop to copy Immop attr");
1097         }
1098 }
1099
1100 /**
1101  * Copy the attributes from a Firm Const to an ia32_Const
1102  */
1103 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
1104         ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
1105         ir_mode *mode;
1106
1107         assert(is_ia32_Cnst(ia32_cnst) && "Need ia32_Const to set Const attr");
1108
1109         switch (get_irn_opcode(cnst)) {
1110                 case iro_Const:
1111                         attr->data.tp     = ia32_Const;
1112                         attr->cnst_val.tv = get_Const_tarval(cnst);
1113                         mode = get_tarval_mode(attr->cnst_val.tv);
1114                         if (mode_is_reference(mode) &&
1115                             get_mode_null(mode) == attr->cnst_val.tv)
1116                                 attr->cnst_val.tv = get_mode_null(mode_Is);
1117                         attr->cnst        = get_ident_for_tv(attr->cnst_val.tv);
1118                         break;
1119                 case iro_SymConst:
1120                         attr->data.tp     = ia32_SymConst;
1121                         attr->cnst_val.sc = get_sc_ident(cnst);
1122                         attr->cnst        = attr->cnst_val.sc;
1123                         break;
1124                 case iro_Unknown:
1125                         assert(0 && "Unknown Const NYI");
1126                         break;
1127                 default:
1128                         assert(0 && "Cannot create ia32_Const for this opcode");
1129         }
1130 }
1131
1132 /**
1133  * Sets the AddrMode(S|D) attribute
1134  */
1135 void set_ia32_AddrMode(ir_node *node, char direction) {
1136         ia32_attr_t *attr = get_ia32_attr(node);
1137
1138         switch (direction) {
1139                 case 'D':
1140                         attr->data.tp = ia32_AddrModeD;
1141                         break;
1142                 case 'S':
1143                         attr->data.tp = ia32_AddrModeS;
1144                         break;
1145                 default:
1146                         assert(0 && "wrong AM type");
1147         }
1148 }
1149
1150 /**
1151  * Returns whether or not the node is an immediate operation with Const.
1152  */
1153 int is_ia32_ImmConst(const ir_node *node) {
1154         ia32_attr_t *attr = get_ia32_attr(node);
1155         return (attr->data.imm_tp == ia32_ImmConst);
1156 }
1157
1158 /**
1159  * Returns whether or not the node is an immediate operation with SymConst.
1160  */
1161 int is_ia32_ImmSymConst(const ir_node *node) {
1162         ia32_attr_t *attr = get_ia32_attr(node);
1163         return (attr->data.imm_tp == ia32_ImmSymConst);
1164 }
1165
1166 /**
1167  * Returns whether or not the node is an AddrModeS node.
1168  */
1169 int is_ia32_AddrModeS(const ir_node *node) {
1170         ia32_attr_t *attr = get_ia32_attr(node);
1171         return (attr->data.tp == ia32_AddrModeS);
1172 }
1173
1174 /**
1175  * Returns whether or not the node is an AddrModeD node.
1176  */
1177 int is_ia32_AddrModeD(const ir_node *node) {
1178         ia32_attr_t *attr = get_ia32_attr(node);
1179         return (attr->data.tp == ia32_AddrModeD);
1180 }
1181
1182 /**
1183  * Checks if node is a Load or xLoad/vfLoad.
1184  */
1185 int is_ia32_Ld(const ir_node *node) {
1186         return is_ia32_Load(node) || is_ia32_xLoad(node) || is_ia32_vfld(node) || is_ia32_fld(node);
1187 }
1188
1189 /**
1190  * Checks if node is a Store or xStore/vfStore.
1191  */
1192 int is_ia32_St(const ir_node *node) {
1193         return is_ia32_Store(node) || is_ia32_xStore(node) || is_ia32_vfst(node) || is_ia32_fst(node) || is_ia32_fstp(node);
1194 }
1195
1196 /**
1197  * Checks if node is a Const or xConst/vfConst.
1198  */
1199 int is_ia32_Cnst(const ir_node *node) {
1200         return is_ia32_Const(node) || is_ia32_xConst(node) || is_ia32_vfConst(node);
1201 }
1202
1203 /**
1204  * Returns the name of the OUT register at position pos.
1205  */
1206 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
1207         ia32_attr_t *attr = get_ia32_attr(node);
1208
1209         assert(is_ia32_irn(node) && "Not an ia32 node.");
1210         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1211         assert(attr->slots[pos]  && "No register assigned");
1212
1213         return arch_register_get_name(attr->slots[pos]);
1214 }
1215
1216 /**
1217  * Returns the index of the OUT register at position pos within its register class.
1218  */
1219 int get_ia32_out_regnr(const ir_node *node, int pos) {
1220         ia32_attr_t *attr = get_ia32_attr(node);
1221
1222         assert(is_ia32_irn(node) && "Not an ia32 node.");
1223         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1224         assert(attr->slots[pos]  && "No register assigned");
1225
1226         return arch_register_get_index(attr->slots[pos]);
1227 }
1228
1229 /**
1230  * Returns the OUT register at position pos.
1231  */
1232 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1233         ia32_attr_t *attr = get_ia32_attr(node);
1234
1235         assert(is_ia32_irn(node) && "Not an ia32 node.");
1236         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1237         assert(attr->slots[pos]  && "No register assigned");
1238
1239         return attr->slots[pos];
1240 }
1241
1242 /**
1243  * Initializes the nodes attributes.
1244  */
1245 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags, const ia32_register_req_t **in_reqs,
1246                                                   const ia32_register_req_t **out_reqs, int n_res)
1247 {
1248         ia32_attr_t *attr = get_ia32_attr(node);
1249         set_ia32_flags(node, flags);
1250         set_ia32_in_req_all(node, in_reqs);
1251         set_ia32_out_req_all(node, out_reqs);
1252
1253         attr->data.n_res = n_res;
1254         memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
1255 }
1256
1257 /***************************************************************************************
1258  *                  _                            _                   _
1259  *                 | |                          | |                 | |
1260  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1261  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1262  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1263  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1264  *
1265  ***************************************************************************************/
1266
1267 /* default compare operation to compare immediate ops */
1268 int ia32_compare_immop_attr(ia32_attr_t *a, ia32_attr_t *b) {
1269         int equ = 0;
1270
1271         if (a->data.tp == b->data.tp) {
1272                 equ = (a->cnst == b->cnst);
1273                 equ = equ ? (a->data.use_frame == b->data.use_frame) : 0;
1274
1275                 if (equ && a->data.use_frame && b->data.use_frame)
1276                         equ = (a->frame_ent == b->frame_ent);
1277         }
1278
1279         return !equ;
1280 }
1281
1282 /* compare converts */
1283 int ia32_compare_conv_attr(ia32_attr_t *a, ia32_attr_t *b) {
1284         int equ = ! ia32_compare_immop_attr(a, b);
1285
1286         equ = equ ? (a->src_mode == b->src_mode) && (a->tgt_mode == b->tgt_mode) : equ;
1287
1288         return !equ;
1289 }
1290
1291 /* Include the generated constructor functions */
1292 #include "gen_ia32_new_nodes.c.inl"