remove $Id$, it doesn't work with git anyway
[libfirm] / ir / be / ia32 / ia32_new_nodes.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Handling of ia32 specific firm opcodes.
23  * @author      Christian Wuerdig
24  *
25  * This file implements the creation of the architecture specific firm opcodes
26  * and the corresponding node constructors for the ia32 assembler irg.
27  */
28 #include "config.h"
29
30 #include <stdlib.h>
31 #include <stdbool.h>
32
33 #include "irargs_t.h"
34 #include "irprog_t.h"
35 #include "irgraph_t.h"
36 #include "irnode_t.h"
37 #include "irmode_t.h"
38 #include "ircons_t.h"
39 #include "iropt_t.h"
40 #include "irop.h"
41 #include "irverify_t.h"
42 #include "irprintf.h"
43 #include "iredges.h"
44 #include "error.h"
45 #include "raw_bitset.h"
46 #include "xmalloc.h"
47
48 #include "bearch.h"
49 #include "beinfo.h"
50
51 #include "bearch_ia32_t.h"
52 #include "ia32_common_transform.h"
53 #include "ia32_nodes_attr.h"
54 #include "ia32_new_nodes.h"
55 #include "gen_ia32_regalloc_if.h"
56 #include "gen_ia32_machine.h"
57
58 /**
59  * Dumper interface for dumping ia32 nodes in vcg.
60  * @param n        the node to dump
61  * @param F        the output file
62  * @param reason   indicates which kind of information should be dumped
63  * @return 0 on success or != 0 on failure
64  */
65 static void ia32_dump_node(FILE *F, ir_node *n, dump_reason_t reason)
66 {
67         ir_mode *mode = NULL;
68
69         switch (reason) {
70                 case dump_node_opcode_txt:
71                         fprintf(F, "%s", get_irn_opname(n));
72
73                         if (is_ia32_Immediate(n) || is_ia32_Const(n)) {
74                                 const ia32_immediate_attr_t *attr
75                                         = get_ia32_immediate_attr_const(n);
76
77                                 fputc(' ', F);
78                                 if (attr->symconst) {
79                                         if (attr->sc_sign) {
80                                                 fputc('-', F);
81                                         }
82                                         fputs(get_entity_name(attr->symconst), F);
83                                 }
84                                 if (attr->offset != 0 || attr->symconst == NULL) {
85                                         if (attr->offset > 0 && attr->symconst != NULL) {
86                                                 fputc('+', F);
87                                         }
88                                         fprintf(F, "%ld", attr->offset);
89                                         if (attr->no_pic_adjust) {
90                                                 fputs("(no_pic_adjust)", F);
91                                         }
92                                 }
93                         }
94                         else {
95                                 const ia32_attr_t *attr = get_ia32_attr_const(n);
96
97                                 if (attr->am_sc != NULL || attr->am_offs != 0)
98                                         fputs(" [", F);
99
100                                 if (attr->am_sc != NULL) {
101                                         if (attr->data.am_sc_sign) {
102                                                 fputc('-', F);
103                                         }
104                                         fputs(get_entity_name(attr->am_sc), F);
105                                         if (attr->data.am_sc_no_pic_adjust) {
106                                                 fputs("(no_pic_adjust)", F);
107                                         }
108                                 }
109                                 if (attr->am_offs != 0) {
110                                         if (attr->am_offs > 0 && attr->am_sc != NULL) {
111                                                 fputc('+', F);
112                                         }
113                                         fprintf(F, "%d", attr->am_offs);
114                                 }
115
116                                 if (attr->am_sc != NULL || attr->am_offs != 0)
117                                         fputc(']', F);
118                         }
119                         break;
120
121                 case dump_node_mode_txt:
122                         mode = get_ia32_ls_mode(n);
123                         if (mode != NULL)
124                                 fprintf(F, "[%s]", get_mode_name(mode));
125                         break;
126
127                 case dump_node_nodeattr_txt:
128                         if (! is_ia32_Lea(n)) {
129                                 if (is_ia32_AddrModeS(n)) {
130                                         fprintf(F, "[AM S] ");
131                                 } else if (is_ia32_AddrModeD(n)) {
132                                         fprintf(F, "[AM D] ");
133                                 }
134                         }
135
136                         break;
137
138                 case dump_node_info_txt:
139                         arch_dump_reqs_and_registers(F, n);
140
141                         /* dump op type */
142                         fprintf(F, "op = ");
143                         switch (get_ia32_op_type(n)) {
144                                 case ia32_Normal:
145                                         fprintf(F, "Normal");
146                                         break;
147                                 case ia32_AddrModeD:
148                                         fprintf(F, "AM Dest (Load+Store)");
149                                         break;
150                                 case ia32_AddrModeS:
151                                         fprintf(F, "AM Source (Load)");
152                                         break;
153                                 default:
154                                         fprintf(F, "unknown (%d)", (int)get_ia32_op_type(n));
155                                         break;
156                         }
157                         fprintf(F, "\n");
158
159                         /* dump supported am */
160                         fprintf(F, "AM support = ");
161                         switch (get_ia32_am_support(n)) {
162                                 case ia32_am_none:   fputs("none\n",            F); break;
163                                 case ia32_am_unary:  fputs("source (unary)\n",  F); break;
164                                 case ia32_am_binary: fputs("source (binary)\n", F); break;
165
166                                 default:
167                                         fprintf(F, "unknown (%d)\n", (int)get_ia32_am_support(n));
168                                         break;
169                         }
170
171                         /* dump AM offset */
172                         if (get_ia32_am_offs_int(n) != 0) {
173                                 fprintf(F, "AM offset = %d\n", get_ia32_am_offs_int(n));
174                         }
175
176                         /* dump AM symconst */
177                         if (get_ia32_am_sc(n) != NULL) {
178                                 ir_entity *ent = get_ia32_am_sc(n);
179                                 ident *id = get_entity_ld_ident(ent);
180                                 fprintf(F, "AM symconst = %s\n", get_id_str(id));
181                         }
182
183                         /* dump AM scale */
184                         fprintf(F, "AM scale = %u\n", get_ia32_am_scale(n));
185
186                         /* dump pn code */
187                         if (is_ia32_CMovcc(n) || is_ia32_Setcc(n) || is_ia32_Jcc(n)) {
188                                 ia32_attr_t *attr = get_ia32_attr(n);
189                                 fprintf(F, "condition_code = 0x%X\n", (unsigned)get_ia32_condcode(n));
190                                 fprintf(F, "ins_permuted = %u\n", (unsigned)attr->data.ins_permuted);
191                         }
192                         else if (is_ia32_CopyB(n) || is_ia32_CopyB_i(n)) {
193                                 fprintf(F, "size = %u\n", get_ia32_copyb_size(n));
194                         }
195
196                         fprintf(F, "use_frame = %d\n",     is_ia32_use_frame(n));
197                         fprintf(F, "commutative = %d\n",   is_ia32_commutative(n));
198                         fprintf(F, "need stackent = %d\n", is_ia32_need_stackent(n));
199                         fprintf(F, "is reload = %d\n",     is_ia32_is_reload(n));
200                         fprintf(F, "latency = %u\n",       get_ia32_latency(n));
201
202                         /* dump frame entity */
203                         fprintf(F, "frame entity = ");
204                         if (get_ia32_frame_ent(n)) {
205                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
206                         }
207                         else {
208                                 fprintf(F, "n/a");
209                         }
210                         fprintf(F, "\n");
211
212                         /* dump modes */
213                         fprintf(F, "ls_mode = ");
214                         if (get_ia32_ls_mode(n)) {
215                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
216                         }
217                         else {
218                                 fprintf(F, "n/a");
219                         }
220                         fprintf(F, "\n");
221
222 #ifndef NDEBUG
223                         /* dump original ir node name */
224                         fprintf(F, "orig node = ");
225                         if (get_ia32_orig_node(n)) {
226                                 fprintf(F, "%s", get_ia32_orig_node(n));
227                         }
228                         else {
229                                 fprintf(F, "n/a");
230                         }
231                         fprintf(F, "\n");
232 #endif /* NDEBUG */
233
234                         break;
235         }
236 }
237
238
239
240 ia32_attr_t *get_ia32_attr(ir_node *node)
241 {
242         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
243         return (ia32_attr_t *)get_irn_generic_attr(node);
244 }
245
246 const ia32_attr_t *get_ia32_attr_const(const ir_node *node)
247 {
248         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
249         return (const ia32_attr_t*) get_irn_generic_attr_const(node);
250 }
251
252 ia32_x87_attr_t *get_ia32_x87_attr(ir_node *node)
253 {
254         ia32_attr_t     *attr     = get_ia32_attr(node);
255         ia32_x87_attr_t *x87_attr = CAST_IA32_ATTR(ia32_x87_attr_t, attr);
256         return x87_attr;
257 }
258
259 const ia32_x87_attr_t *get_ia32_x87_attr_const(const ir_node *node)
260 {
261         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
262         const ia32_x87_attr_t *x87_attr = CONST_CAST_IA32_ATTR(ia32_x87_attr_t, attr);
263         return x87_attr;
264 }
265
266 const ia32_asm_attr_t *get_ia32_asm_attr_const(const ir_node *node)
267 {
268         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
269         const ia32_asm_attr_t *asm_attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, attr);
270
271         return asm_attr;
272 }
273
274 ia32_immediate_attr_t *get_ia32_immediate_attr(ir_node *node)
275 {
276         ia32_attr_t           *attr      = get_ia32_attr(node);
277         ia32_immediate_attr_t *imm_attr  = CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
278
279         return imm_attr;
280 }
281
282 const ia32_immediate_attr_t *get_ia32_immediate_attr_const(const ir_node *node)
283 {
284         const ia32_attr_t           *attr     = get_ia32_attr_const(node);
285         const ia32_immediate_attr_t *imm_attr = CONST_CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
286
287         return imm_attr;
288 }
289
290 ia32_condcode_attr_t *get_ia32_condcode_attr(ir_node *node)
291 {
292         ia32_attr_t          *attr    = get_ia32_attr(node);
293         ia32_condcode_attr_t *cc_attr = CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
294
295         return cc_attr;
296 }
297
298 const ia32_condcode_attr_t *get_ia32_condcode_attr_const(const ir_node *node)
299 {
300         const ia32_attr_t          *attr    = get_ia32_attr_const(node);
301         const ia32_condcode_attr_t *cc_attr = CONST_CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
302
303         return cc_attr;
304 }
305
306 ia32_switch_attr_t *get_ia32_switch_attr(ir_node *node)
307 {
308         ia32_attr_t        *attr        = get_ia32_attr(node);
309         ia32_switch_attr_t *switch_attr = CAST_IA32_ATTR(ia32_switch_attr_t, attr);
310         return switch_attr;
311 }
312
313 const ia32_switch_attr_t *get_ia32_switch_attr_const(const ir_node *node)
314 {
315         const ia32_attr_t        *attr        = get_ia32_attr_const(node);
316         const ia32_switch_attr_t *switch_attr = CONST_CAST_IA32_ATTR(ia32_switch_attr_t, attr);
317         return switch_attr;
318 }
319
320 ia32_call_attr_t *get_ia32_call_attr(ir_node *node)
321 {
322         ia32_attr_t      *attr      = get_ia32_attr(node);
323         ia32_call_attr_t *call_attr = CAST_IA32_ATTR(ia32_call_attr_t, attr);
324
325         return call_attr;
326 }
327
328 const ia32_call_attr_t *get_ia32_call_attr_const(const ir_node *node)
329 {
330         const ia32_attr_t      *attr      = get_ia32_attr_const(node);
331         const ia32_call_attr_t *call_attr = CONST_CAST_IA32_ATTR(ia32_call_attr_t, attr);
332
333         return call_attr;
334 }
335
336 ia32_copyb_attr_t *get_ia32_copyb_attr(ir_node *node)
337 {
338         ia32_attr_t       *attr       = get_ia32_attr(node);
339         ia32_copyb_attr_t *copyb_attr = CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
340
341         return copyb_attr;
342 }
343
344 const ia32_copyb_attr_t *get_ia32_copyb_attr_const(const ir_node *node)
345 {
346         const ia32_attr_t       *attr       = get_ia32_attr_const(node);
347         const ia32_copyb_attr_t *copyb_attr = CONST_CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
348
349         return copyb_attr;
350 }
351
352 ia32_climbframe_attr_t *get_ia32_climbframe_attr(ir_node *node)
353 {
354         ia32_attr_t            *attr            = get_ia32_attr(node);
355         ia32_climbframe_attr_t *climbframe_attr = CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
356
357         return climbframe_attr;
358 }
359
360 const ia32_climbframe_attr_t *get_ia32_climbframe_attr_const(const ir_node *node)
361 {
362         const ia32_attr_t            *attr            = get_ia32_attr_const(node);
363         const ia32_climbframe_attr_t *climbframe_attr = CONST_CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
364
365         return climbframe_attr;
366 }
367
368 /**
369  * Gets the type of an ia32 node.
370  */
371 ia32_op_type_t get_ia32_op_type(const ir_node *node)
372 {
373         const ia32_attr_t *attr = get_ia32_attr_const(node);
374         return (ia32_op_type_t)attr->data.tp;
375 }
376
377 /**
378  * Sets the type of an ia32 node.
379  */
380 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp)
381 {
382         ia32_attr_t *attr = get_ia32_attr(node);
383         attr->data.tp     = tp;
384 }
385
386 ia32_am_type_t get_ia32_am_support(const ir_node *node)
387 {
388         const ia32_attr_t *attr = get_ia32_attr_const(node);
389         return (ia32_am_type_t)attr->data.am_arity;
390 }
391
392 /**
393  * Sets the supported address mode of an ia32 node
394  */
395 void set_ia32_am_support(ir_node *node, ia32_am_type_t arity)
396 {
397         ia32_attr_t *attr   = get_ia32_attr(node);
398         attr->data.am_arity = arity;
399 }
400
401 /**
402  * Gets the address mode offset as int.
403  */
404 int get_ia32_am_offs_int(const ir_node *node)
405 {
406         const ia32_attr_t *attr = get_ia32_attr_const(node);
407         return attr->am_offs;
408 }
409
410 /**
411  * Sets the address mode offset from an int.
412  */
413 void set_ia32_am_offs_int(ir_node *node, int offset)
414 {
415         ia32_attr_t *attr = get_ia32_attr(node);
416         attr->am_offs = offset;
417 }
418
419 void add_ia32_am_offs_int(ir_node *node, int offset)
420 {
421         ia32_attr_t *attr = get_ia32_attr(node);
422         attr->am_offs += offset;
423 }
424
425 /**
426  * Returns the symconst entity associated to address mode.
427  */
428 ir_entity *get_ia32_am_sc(const ir_node *node)
429 {
430         const ia32_attr_t *attr = get_ia32_attr_const(node);
431         return attr->am_sc;
432 }
433
434 /**
435  * Sets the symconst entity associated to address mode.
436  */
437 void set_ia32_am_sc(ir_node *node, ir_entity *entity)
438 {
439         ia32_attr_t *attr = get_ia32_attr(node);
440         attr->am_sc       = entity;
441 }
442
443 /**
444  * Sets the sign bit for address mode symconst.
445  */
446 void set_ia32_am_sc_sign(ir_node *node)
447 {
448         ia32_attr_t *attr     = get_ia32_attr(node);
449         attr->data.am_sc_sign = 1;
450 }
451
452 /**
453  * Clears the sign bit for address mode symconst.
454  */
455 void clear_ia32_am_sc_sign(ir_node *node)
456 {
457         ia32_attr_t *attr     = get_ia32_attr(node);
458         attr->data.am_sc_sign = 0;
459 }
460
461 /**
462  * Returns the sign bit for address mode symconst.
463  */
464 int is_ia32_am_sc_sign(const ir_node *node)
465 {
466         const ia32_attr_t *attr = get_ia32_attr_const(node);
467         return attr->data.am_sc_sign;
468 }
469
470 void set_ia32_am_tls_segment(ir_node *node, bool value)
471 {
472         ia32_attr_t *attr = get_ia32_attr(node);
473         attr->data.am_tls_segment = value;
474 }
475
476 bool get_ia32_am_tls_segment(const ir_node *node)
477 {
478         const ia32_attr_t *attr = get_ia32_attr_const(node);
479         return attr->data.am_tls_segment;
480 }
481
482 /**
483  * Gets the addr mode const.
484  */
485 unsigned get_ia32_am_scale(const ir_node *node)
486 {
487         const ia32_attr_t *attr = get_ia32_attr_const(node);
488         return attr->data.am_scale;
489 }
490
491 /**
492  * Sets the index register scale for address mode.
493  */
494 void set_ia32_am_scale(ir_node *node, unsigned scale)
495 {
496         ia32_attr_t *attr = get_ia32_attr(node);
497         assert(scale <= 3 && "AM scale out of range [0 ... 3]");
498         attr->data.am_scale = scale;
499 }
500
501 void ia32_copy_am_attrs(ir_node *to, const ir_node *from)
502 {
503         set_ia32_ls_mode(to, get_ia32_ls_mode(from));
504         set_ia32_am_scale(to, get_ia32_am_scale(from));
505         set_ia32_am_sc(to, get_ia32_am_sc(from));
506         if (is_ia32_am_sc_sign(from))
507                 set_ia32_am_sc_sign(to);
508         add_ia32_am_offs_int(to, get_ia32_am_offs_int(from));
509         set_ia32_frame_ent(to, get_ia32_frame_ent(from));
510         if (is_ia32_use_frame(from))
511                 set_ia32_use_frame(to);
512 }
513
514 /**
515  * Sets the uses_frame flag.
516  */
517 void set_ia32_use_frame(ir_node *node)
518 {
519         ia32_attr_t *attr    = get_ia32_attr(node);
520         attr->data.use_frame = 1;
521 }
522
523 /**
524  * Clears the uses_frame flag.
525  */
526 void clear_ia32_use_frame(ir_node *node)
527 {
528         ia32_attr_t *attr    = get_ia32_attr(node);
529         attr->data.use_frame = 0;
530 }
531
532 /**
533  * Gets the uses_frame flag.
534  */
535 int is_ia32_use_frame(const ir_node *node)
536 {
537         const ia32_attr_t *attr = get_ia32_attr_const(node);
538         return attr->data.use_frame;
539 }
540
541 /**
542  * Sets node to commutative.
543  */
544 void set_ia32_commutative(ir_node *node)
545 {
546         ia32_attr_t *attr         = get_ia32_attr(node);
547         attr->data.is_commutative = 1;
548 }
549
550 /**
551  * Sets node to non-commutative.
552  */
553 void clear_ia32_commutative(ir_node *node)
554 {
555         ia32_attr_t *attr         = get_ia32_attr(node);
556         attr->data.is_commutative = 0;
557 }
558
559 /**
560  * Checks if node is commutative.
561  */
562 int is_ia32_commutative(const ir_node *node)
563 {
564         const ia32_attr_t *attr = get_ia32_attr_const(node);
565         return attr->data.is_commutative;
566 }
567
568 void set_ia32_need_stackent(ir_node *node)
569 {
570         ia32_attr_t *attr     = get_ia32_attr(node);
571         attr->data.need_stackent = 1;
572 }
573
574 void clear_ia32_need_stackent(ir_node *node)
575 {
576         ia32_attr_t *attr     = get_ia32_attr(node);
577         attr->data.need_stackent = 0;
578 }
579
580 int is_ia32_need_stackent(const ir_node *node)
581 {
582         const ia32_attr_t *attr = get_ia32_attr_const(node);
583         return attr->data.need_stackent;
584 }
585
586 void set_ia32_is_reload(ir_node *node)
587 {
588         ia32_attr_t *attr = get_ia32_attr(node);
589         attr->data.is_reload = 1;
590 }
591
592 int is_ia32_is_reload(const ir_node *node)
593 {
594         const ia32_attr_t *attr = get_ia32_attr_const(node);
595         return attr->data.is_reload;
596 }
597
598 void set_ia32_is_spill(ir_node *node)
599 {
600         ia32_attr_t *attr = get_ia32_attr(node);
601         attr->data.is_spill = 1;
602 }
603
604 int is_ia32_is_spill(const ir_node *node)
605 {
606         const ia32_attr_t *attr = get_ia32_attr_const(node);
607         return attr->data.is_spill;
608 }
609
610 void set_ia32_is_remat(ir_node *node)
611 {
612         ia32_attr_t *attr = get_ia32_attr(node);
613         attr->data.is_remat = 1;
614 }
615
616 int is_ia32_is_remat(const ir_node *node)
617 {
618         const ia32_attr_t *attr = get_ia32_attr_const(node);
619         return attr->data.is_remat;
620 }
621
622 /**
623  * Gets the mode of the stored/loaded value (only set for Store/Load)
624  */
625 ir_mode *get_ia32_ls_mode(const ir_node *node)
626 {
627         const ia32_attr_t *attr = get_ia32_attr_const(node);
628         return attr->ls_mode;
629 }
630
631 /**
632  * Sets the mode of the stored/loaded value (only set for Store/Load)
633  */
634 void set_ia32_ls_mode(ir_node *node, ir_mode *mode)
635 {
636         ia32_attr_t *attr = get_ia32_attr(node);
637         attr->ls_mode     = mode;
638 }
639
640 /**
641  * Gets the frame entity assigned to this node.
642  */
643 ir_entity *get_ia32_frame_ent(const ir_node *node)
644 {
645         const ia32_attr_t *attr = get_ia32_attr_const(node);
646         return attr->frame_ent;
647 }
648
649 /**
650  * Sets the frame entity for this node.
651  */
652 void set_ia32_frame_ent(ir_node *node, ir_entity *ent)
653 {
654         ia32_attr_t *attr = get_ia32_attr(node);
655         attr->frame_ent   = ent;
656         if (ent != NULL)
657                 set_ia32_use_frame(node);
658         else
659                 clear_ia32_use_frame(node);
660 }
661
662
663 /**
664  * Gets the instruction latency.
665  */
666 unsigned get_ia32_latency(const ir_node *node)
667 {
668         const ir_op *op               = get_irn_op(node);
669         const ia32_op_attr_t *op_attr = (ia32_op_attr_t*) get_op_attr(op);
670         return op_attr->latency;
671 }
672
673 const ir_switch_table *get_ia32_switch_table(const ir_node *node)
674 {
675         const ia32_switch_attr_t *attr = get_ia32_switch_attr_const(node);
676         return attr->table;
677 }
678
679 ia32_condition_code_t get_ia32_condcode(const ir_node *node)
680 {
681         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
682         return attr->condition_code;
683 }
684
685 /**
686  * Sets the condition code of a node
687  */
688 void set_ia32_condcode(ir_node *node, ia32_condition_code_t code)
689 {
690         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
691         attr->condition_code = code;
692 }
693
694 /**
695  * Returns the condition code of a node.
696  */
697 unsigned get_ia32_copyb_size(const ir_node *node)
698 {
699         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
700         return attr->size;
701 }
702
703 /**
704  * Get the list of available execution units.
705  */
706 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node)
707 {
708         const ia32_attr_t *attr = get_ia32_attr_const(node);
709         return attr->exec_units;
710 }
711
712 /**
713  * Get the exception label attribute.
714  */
715 unsigned get_ia32_exc_label(const ir_node *node)
716 {
717         const ia32_attr_t *attr = get_ia32_attr_const(node);
718         return attr->data.has_except_label;
719 }
720
721 /**
722  * Set the exception label attribute.
723  */
724 void set_ia32_exc_label(ir_node *node, unsigned flag)
725 {
726         ia32_attr_t *attr = get_ia32_attr(node);
727         attr->data.has_except_label = flag;
728 }
729
730 /**
731  * Return the exception label id.
732  */
733 ir_label_t get_ia32_exc_label_id(const ir_node *node)
734 {
735         const ia32_attr_t *attr = get_ia32_attr_const(node);
736
737         assert(attr->data.has_except_label);
738         return attr->exc_label;
739 }
740
741 /**
742  * Assign the exception label id.
743  */
744 void set_ia32_exc_label_id(ir_node *node, ir_label_t id)
745 {
746         ia32_attr_t *attr = get_ia32_attr(node);
747
748         assert(attr->data.has_except_label);
749         attr->exc_label = id;
750 }
751
752 #ifndef NDEBUG
753
754 /**
755  * Returns the name of the original ir node.
756  */
757 const char *get_ia32_orig_node(const ir_node *node)
758 {
759         const ia32_attr_t *attr = get_ia32_attr_const(node);
760         return attr->orig_node;
761 }
762
763 static const char *ia32_get_old_node_name(const ir_node *irn)
764 {
765         ir_graph       *irg  = get_irn_irg(irn);
766         struct obstack *obst = be_get_be_obst(irg);
767
768         lc_eoprintf(firm_get_arg_env(), obst, "%+F", irn);
769         obstack_1grow(obst, 0);
770         return (const char*)obstack_finish(obst);
771 }
772
773 /**
774  * Sets the name of the original ir node.
775  */
776 void set_ia32_orig_node(ir_node *node, const ir_node *old)
777 {
778         const char  *name = ia32_get_old_node_name(old);
779         ia32_attr_t *attr = get_ia32_attr(node);
780         attr->orig_node   = name;
781 }
782
783 #endif /* NDEBUG */
784
785
786 /**
787  * Returns whether or not the node is an AddrModeS node.
788  */
789 int is_ia32_AddrModeS(const ir_node *node)
790 {
791         const ia32_attr_t *attr = get_ia32_attr_const(node);
792         return (attr->data.tp == ia32_AddrModeS);
793 }
794
795 /**
796  * Returns whether or not the node is an AddrModeD node.
797  */
798 int is_ia32_AddrModeD(const ir_node *node)
799 {
800         const ia32_attr_t *attr = get_ia32_attr_const(node);
801         return (attr->data.tp == ia32_AddrModeD);
802 }
803
804 void ia32_swap_left_right(ir_node *node)
805 {
806         ia32_attr_t *attr  = get_ia32_attr(node);
807         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
808         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
809
810         assert(is_ia32_commutative(node));
811         attr->data.ins_permuted = !attr->data.ins_permuted;
812         set_irn_n(node, n_ia32_binary_left,  right);
813         set_irn_n(node, n_ia32_binary_right, left);
814 }
815
816 /**
817  * Initializes the nodes attributes.
818  */
819 static void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
820                                  const arch_register_req_t **in_reqs,
821                                  const be_execution_unit_t ***execution_units,
822                                  int n_res)
823 {
824         ir_graph        *irg  = get_irn_irg(node);
825         struct obstack  *obst = get_irg_obstack(irg);
826         ia32_attr_t     *attr = get_ia32_attr(node);
827         backend_info_t  *info;
828
829         arch_set_irn_flags(node, flags);
830         arch_set_irn_register_reqs_in(node, in_reqs);
831
832         attr->exec_units  = execution_units;
833 #ifndef NDEBUG
834         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
835 #endif
836
837         info            = be_get_info(node);
838         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
839         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
840 }
841
842 static void init_ia32_x87_attributes(ir_node *res)
843 {
844         ir_graph        *irg      = get_irn_irg(res);
845         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
846 #ifndef NDEBUG
847         ia32_attr_t *attr  = get_ia32_attr(res);
848         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
849 #else
850         (void) res;
851 #endif
852         irg_data->do_x87_sim = 1;
853 }
854
855 static void init_ia32_asm_attributes(ir_node *res)
856 {
857 #ifndef NDEBUG
858         ia32_attr_t *attr  = get_ia32_attr(res);
859         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
860 #else
861         (void) res;
862 #endif
863 }
864
865 static void init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
866                                            int symconst_sign, int no_pic_adjust,
867                                            long offset)
868 {
869         ia32_immediate_attr_t *attr = (ia32_immediate_attr_t*)get_irn_generic_attr(res);
870
871 #ifndef NDEBUG
872         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
873 #endif
874         attr->symconst      = symconst;
875         attr->sc_sign       = symconst_sign;
876         attr->no_pic_adjust = no_pic_adjust;
877         attr->offset        = offset;
878 }
879
880 static void init_ia32_call_attributes(ir_node* res, unsigned pop,
881                                       ir_type* call_tp)
882 {
883         ia32_call_attr_t *attr = (ia32_call_attr_t*)get_irn_generic_attr(res);
884
885 #ifndef NDEBUG
886         attr->attr.attr_type  |= IA32_ATTR_ia32_call_attr_t;
887 #endif
888         attr->pop     = pop;
889         attr->call_tp = call_tp;
890 }
891
892 static void init_ia32_copyb_attributes(ir_node *res, unsigned size)
893 {
894         ia32_copyb_attr_t *attr = (ia32_copyb_attr_t*)get_irn_generic_attr(res);
895
896 #ifndef NDEBUG
897         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
898 #endif
899         attr->size = size;
900 }
901
902 static void init_ia32_condcode_attributes(ir_node *res,
903                                           ia32_condition_code_t cc)
904 {
905         ia32_condcode_attr_t *attr = (ia32_condcode_attr_t*)get_irn_generic_attr(res);
906
907 #ifndef NDEBUG
908         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
909 #endif
910         attr->condition_code = cc;
911 }
912
913 static void init_ia32_climbframe_attributes(ir_node *res, unsigned count)
914 {
915         ia32_climbframe_attr_t *attr = (ia32_climbframe_attr_t*)get_irn_generic_attr(res);
916
917 #ifndef NDEBUG
918         attr->attr.attr_type  |= IA32_ATTR_ia32_climbframe_attr_t;
919 #endif
920         attr->count = count;
921 }
922
923 static void init_ia32_switch_attributes(ir_node *node,
924                                         const ir_switch_table *table)
925 {
926         unsigned n_outs = arch_get_irn_n_outs(node);
927         unsigned o;
928
929         ia32_switch_attr_t *attr = (ia32_switch_attr_t*) get_irn_generic_attr(node);
930 #ifndef NDEBUG
931         attr->attr.attr_type |= IA32_ATTR_ia32_switch_attr_t;
932 #endif
933         attr->table = table;
934
935         for (o = 0; o < n_outs; ++o) {
936                 arch_set_irn_register_req_out(node, o, arch_no_register_req);
937         }
938 }
939
940 /* default compare operation to compare attributes */
941 static int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b)
942 {
943         if (a->data.tp != b->data.tp)
944                 return 1;
945
946         if (a->data.am_scale != b->data.am_scale
947             || a->data.am_sc_sign != b->data.am_sc_sign
948             || a->am_offs != b->am_offs
949             || a->am_sc != b->am_sc
950                 || a->data.am_sc_no_pic_adjust != b->data.am_sc_no_pic_adjust
951             || a->ls_mode != b->ls_mode)
952                 return 1;
953
954         /* nodes with not yet assigned entities shouldn't be CSEd (important for
955          * unsigned int -> double conversions */
956         if (a->data.use_frame && a->frame_ent == NULL)
957                 return 1;
958         if (b->data.use_frame && b->frame_ent == NULL)
959                 return 1;
960
961         if (a->data.use_frame != b->data.use_frame
962             || a->frame_ent != b->frame_ent)
963                 return 1;
964
965         if (a->data.has_except_label != b->data.has_except_label)
966                 return 1;
967
968         if (a->data.ins_permuted != b->data.ins_permuted)
969                 return 1;
970
971         return 0;
972 }
973
974 /** Compare nodes attributes for all "normal" nodes. */
975 static int ia32_compare_nodes_attr(const ir_node *a, const ir_node *b)
976 {
977         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
978         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
979
980         return ia32_compare_attr(attr_a, attr_b);
981 }
982
983 /** Compare node attributes for nodes with condition code. */
984 static int ia32_compare_condcode_attr(const ir_node *a, const ir_node *b)
985 {
986         const ia32_condcode_attr_t *attr_a;
987         const ia32_condcode_attr_t *attr_b;
988
989         if (ia32_compare_nodes_attr(a, b))
990                 return 1;
991
992         attr_a = get_ia32_condcode_attr_const(a);
993         attr_b = get_ia32_condcode_attr_const(b);
994
995         if (attr_a->condition_code != attr_b->condition_code)
996                 return 1;
997
998         return 0;
999 }
1000
1001 /** Compare node attributes for call nodes. */
1002 static int ia32_compare_call_attr(const ir_node *a, const ir_node *b)
1003 {
1004         const ia32_call_attr_t *attr_a;
1005         const ia32_call_attr_t *attr_b;
1006
1007         if (ia32_compare_nodes_attr(a, b))
1008                 return 1;
1009
1010         attr_a = get_ia32_call_attr_const(a);
1011         attr_b = get_ia32_call_attr_const(b);
1012
1013         if (attr_a->pop != attr_b->pop)
1014                 return 1;
1015
1016         if (attr_a->call_tp != attr_b->call_tp)
1017                 return 1;
1018
1019         return 0;
1020 }
1021
1022 /** Compare node attributes for CopyB nodes. */
1023 static int ia32_compare_copyb_attr(const ir_node *a, const ir_node *b)
1024 {
1025         const ia32_copyb_attr_t *attr_a;
1026         const ia32_copyb_attr_t *attr_b;
1027
1028         if (ia32_compare_nodes_attr(a, b))
1029                 return 1;
1030
1031         attr_a = get_ia32_copyb_attr_const(a);
1032         attr_b = get_ia32_copyb_attr_const(b);
1033
1034         if (attr_a->size != attr_b->size)
1035                 return 1;
1036
1037         return 0;
1038 }
1039
1040
1041 /** Compare ASM node attributes. */
1042 static int ia32_compare_asm_attr(const ir_node *a, const ir_node *b)
1043 {
1044         const ia32_asm_attr_t *attr_a;
1045         const ia32_asm_attr_t *attr_b;
1046
1047         if (ia32_compare_nodes_attr(a, b))
1048                 return 1;
1049
1050         attr_a = get_ia32_asm_attr_const(a);
1051         attr_b = get_ia32_asm_attr_const(b);
1052
1053         if (attr_a->asm_text != attr_b->asm_text)
1054                 return 1;
1055
1056         return 0;
1057 }
1058
1059 /**
1060  * Hash function for Immediates
1061  */
1062 static unsigned ia32_hash_Immediate(const ir_node *irn)
1063 {
1064         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1065
1066         return HASH_PTR(a->symconst) + (a->sc_sign << 16) + a->offset;
1067 }
1068
1069 /** Compare node attributes for Immediates. */
1070 static int ia32_compare_immediate_attr(const ir_node *a, const ir_node *b)
1071 {
1072         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1073         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1074
1075         if (attr_a->symconst != attr_b->symconst
1076                 || attr_a->sc_sign != attr_b->sc_sign
1077                 || attr_a->no_pic_adjust != attr_b->no_pic_adjust
1078                 || attr_a->offset != attr_b->offset) {
1079                 return 1;
1080         }
1081
1082         return 0;
1083 }
1084
1085 /** Compare node attributes for x87 nodes. */
1086 static int ia32_compare_x87_attr(const ir_node *a, const ir_node *b)
1087 {
1088         return ia32_compare_nodes_attr(a, b);
1089 }
1090
1091 /** Compare node attributes for ClimbFrame nodes. */
1092 static int ia32_compare_climbframe_attr(const ir_node *a, const ir_node *b)
1093 {
1094         const ia32_climbframe_attr_t *attr_a;
1095         const ia32_climbframe_attr_t *attr_b;
1096
1097         if (ia32_compare_nodes_attr(a, b))
1098                 return 1;
1099
1100         attr_a = get_ia32_climbframe_attr_const(a);
1101         attr_b = get_ia32_climbframe_attr_const(b);
1102
1103         if (attr_a->count != attr_b->count)
1104                 return 1;
1105
1106         return 0;
1107 }
1108
1109 /* copies the ia32 attributes */
1110 static void ia32_copy_attr(ir_graph *irg, const ir_node *old_node,
1111                            ir_node *new_node)
1112 {
1113         struct obstack    *obst     = get_irg_obstack(irg);
1114         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1115         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1116         backend_info_t    *old_info = be_get_info(old_node);
1117         backend_info_t    *new_info = be_get_info(new_node);
1118
1119         /* copy the attributes */
1120         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1121
1122         /* copy out flags */
1123         new_info->out_infos =
1124                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
1125         new_info->in_reqs = old_info->in_reqs;
1126         new_info->flags = old_info->flags;
1127 }
1128
1129 /* Include the generated constructor functions */
1130 #include "gen_ia32_new_nodes.c.inl"