ia32: Remove the ia32_x87_attr_t from ia32_asm_attr_t.
[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
57 struct obstack opcodes_obst;
58
59 /**
60  * Dumper interface for dumping ia32 nodes in vcg.
61  * @param n        the node to dump
62  * @param F        the output file
63  * @param reason   indicates which kind of information should be dumped
64  * @return 0 on success or != 0 on failure
65  */
66 static void ia32_dump_node(FILE *F, const ir_node *n, dump_reason_t reason)
67 {
68         ir_mode *mode = NULL;
69
70         switch (reason) {
71                 case dump_node_opcode_txt:
72                         fprintf(F, "%s", get_irn_opname(n));
73
74                         if (is_ia32_Immediate(n) || is_ia32_Const(n)) {
75                                 const ia32_immediate_attr_t *attr
76                                         = get_ia32_immediate_attr_const(n);
77
78                                 fputc(' ', F);
79                                 if (attr->symconst) {
80                                         if (attr->sc_sign) {
81                                                 fputc('-', F);
82                                         }
83                                         fputs(get_entity_name(attr->symconst), F);
84                                 }
85                                 if (attr->offset != 0 || attr->symconst == NULL) {
86                                         if (attr->offset > 0 && attr->symconst != NULL) {
87                                                 fputc('+', F);
88                                         }
89                                         fprintf(F, "%ld", attr->offset);
90                                         if (attr->no_pic_adjust) {
91                                                 fputs("(no_pic_adjust)", F);
92                                         }
93                                 }
94                         }
95                         else {
96                                 const ia32_attr_t *attr = get_ia32_attr_const(n);
97
98                                 if (attr->am_sc != NULL || attr->am_offs != 0)
99                                         fputs(" [", F);
100
101                                 if (attr->am_sc != NULL) {
102                                         if (attr->data.am_sc_sign) {
103                                                 fputc('-', F);
104                                         }
105                                         fputs(get_entity_name(attr->am_sc), F);
106                                         if (attr->data.am_sc_no_pic_adjust) {
107                                                 fputs("(no_pic_adjust)", F);
108                                         }
109                                 }
110                                 if (attr->am_offs != 0) {
111                                         if (attr->am_offs > 0 && attr->am_sc != NULL) {
112                                                 fputc('+', F);
113                                         }
114                                         fprintf(F, "%d", attr->am_offs);
115                                 }
116
117                                 if (attr->am_sc != NULL || attr->am_offs != 0)
118                                         fputc(']', F);
119                         }
120                         break;
121
122                 case dump_node_mode_txt:
123                         mode = get_ia32_ls_mode(n);
124                         if (mode != NULL)
125                                 fprintf(F, "[%s]", get_mode_name(mode));
126                         break;
127
128                 case dump_node_nodeattr_txt:
129                         if (! is_ia32_Lea(n)) {
130                                 switch (get_ia32_op_type(n)) {
131                                 case ia32_Normal:    break;
132                                 case ia32_AddrModeS: fprintf(F, "[AM S] "); break;
133                                 case ia32_AddrModeD: fprintf(F, "[AM D] "); break;
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                                 const ia32_attr_t *attr = get_ia32_attr_const(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                         char const *orig = get_ia32_attr_const(n)->orig_node;
225                         fprintf(F, "orig node = %s\n", orig ? orig : "n/a");
226 #endif /* NDEBUG */
227
228                         break;
229         }
230 }
231
232
233
234 ia32_attr_t *get_ia32_attr(ir_node *node)
235 {
236         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
237         return (ia32_attr_t *)get_irn_generic_attr(node);
238 }
239
240 const ia32_attr_t *get_ia32_attr_const(const ir_node *node)
241 {
242         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
243         return (const ia32_attr_t*) get_irn_generic_attr_const(node);
244 }
245
246 ia32_x87_attr_t *get_ia32_x87_attr(ir_node *node)
247 {
248         ia32_attr_t     *attr     = get_ia32_attr(node);
249         ia32_x87_attr_t *x87_attr = CAST_IA32_ATTR(ia32_x87_attr_t, attr);
250         return x87_attr;
251 }
252
253 const ia32_x87_attr_t *get_ia32_x87_attr_const(const ir_node *node)
254 {
255         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
256         const ia32_x87_attr_t *x87_attr = CONST_CAST_IA32_ATTR(ia32_x87_attr_t, attr);
257         return x87_attr;
258 }
259
260 const ia32_asm_attr_t *get_ia32_asm_attr_const(const ir_node *node)
261 {
262         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
263         const ia32_asm_attr_t *asm_attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, attr);
264
265         return asm_attr;
266 }
267
268 ia32_immediate_attr_t *get_ia32_immediate_attr(ir_node *node)
269 {
270         ia32_attr_t           *attr      = get_ia32_attr(node);
271         ia32_immediate_attr_t *imm_attr  = CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
272
273         return imm_attr;
274 }
275
276 const ia32_immediate_attr_t *get_ia32_immediate_attr_const(const ir_node *node)
277 {
278         const ia32_attr_t           *attr     = get_ia32_attr_const(node);
279         const ia32_immediate_attr_t *imm_attr = CONST_CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
280
281         return imm_attr;
282 }
283
284 ia32_condcode_attr_t *get_ia32_condcode_attr(ir_node *node)
285 {
286         ia32_attr_t          *attr    = get_ia32_attr(node);
287         ia32_condcode_attr_t *cc_attr = CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
288
289         return cc_attr;
290 }
291
292 const ia32_condcode_attr_t *get_ia32_condcode_attr_const(const ir_node *node)
293 {
294         const ia32_attr_t          *attr    = get_ia32_attr_const(node);
295         const ia32_condcode_attr_t *cc_attr = CONST_CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
296
297         return cc_attr;
298 }
299
300 ia32_switch_attr_t *get_ia32_switch_attr(ir_node *node)
301 {
302         ia32_attr_t        *attr        = get_ia32_attr(node);
303         ia32_switch_attr_t *switch_attr = CAST_IA32_ATTR(ia32_switch_attr_t, attr);
304         return switch_attr;
305 }
306
307 const ia32_switch_attr_t *get_ia32_switch_attr_const(const ir_node *node)
308 {
309         const ia32_attr_t        *attr        = get_ia32_attr_const(node);
310         const ia32_switch_attr_t *switch_attr = CONST_CAST_IA32_ATTR(ia32_switch_attr_t, attr);
311         return switch_attr;
312 }
313
314 ia32_call_attr_t *get_ia32_call_attr(ir_node *node)
315 {
316         ia32_attr_t      *attr      = get_ia32_attr(node);
317         ia32_call_attr_t *call_attr = CAST_IA32_ATTR(ia32_call_attr_t, attr);
318
319         return call_attr;
320 }
321
322 const ia32_call_attr_t *get_ia32_call_attr_const(const ir_node *node)
323 {
324         const ia32_attr_t      *attr      = get_ia32_attr_const(node);
325         const ia32_call_attr_t *call_attr = CONST_CAST_IA32_ATTR(ia32_call_attr_t, attr);
326
327         return call_attr;
328 }
329
330 ia32_copyb_attr_t *get_ia32_copyb_attr(ir_node *node)
331 {
332         ia32_attr_t       *attr       = get_ia32_attr(node);
333         ia32_copyb_attr_t *copyb_attr = CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
334
335         return copyb_attr;
336 }
337
338 const ia32_copyb_attr_t *get_ia32_copyb_attr_const(const ir_node *node)
339 {
340         const ia32_attr_t       *attr       = get_ia32_attr_const(node);
341         const ia32_copyb_attr_t *copyb_attr = CONST_CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
342
343         return copyb_attr;
344 }
345
346 ia32_climbframe_attr_t *get_ia32_climbframe_attr(ir_node *node)
347 {
348         ia32_attr_t            *attr            = get_ia32_attr(node);
349         ia32_climbframe_attr_t *climbframe_attr = CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
350
351         return climbframe_attr;
352 }
353
354 const ia32_climbframe_attr_t *get_ia32_climbframe_attr_const(const ir_node *node)
355 {
356         const ia32_attr_t            *attr            = get_ia32_attr_const(node);
357         const ia32_climbframe_attr_t *climbframe_attr = CONST_CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
358
359         return climbframe_attr;
360 }
361
362 /**
363  * Gets the type of an ia32 node.
364  */
365 ia32_op_type_t get_ia32_op_type(const ir_node *node)
366 {
367         const ia32_attr_t *attr = get_ia32_attr_const(node);
368         return (ia32_op_type_t)attr->data.tp;
369 }
370
371 /**
372  * Sets the type of an ia32 node.
373  */
374 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp)
375 {
376         ia32_attr_t *attr = get_ia32_attr(node);
377         attr->data.tp     = tp;
378 }
379
380 ia32_am_type_t get_ia32_am_support(const ir_node *node)
381 {
382         const ia32_attr_t *attr = get_ia32_attr_const(node);
383         return (ia32_am_type_t)attr->data.am_arity;
384 }
385
386 /**
387  * Sets the supported address mode of an ia32 node
388  */
389 void set_ia32_am_support(ir_node *node, ia32_am_type_t arity)
390 {
391         ia32_attr_t *attr   = get_ia32_attr(node);
392         attr->data.am_arity = arity;
393 }
394
395 /**
396  * Gets the address mode offset as int.
397  */
398 int get_ia32_am_offs_int(const ir_node *node)
399 {
400         const ia32_attr_t *attr = get_ia32_attr_const(node);
401         return attr->am_offs;
402 }
403
404 /**
405  * Sets the address mode offset from an int.
406  */
407 void set_ia32_am_offs_int(ir_node *node, int offset)
408 {
409         ia32_attr_t *attr = get_ia32_attr(node);
410         attr->am_offs = offset;
411 }
412
413 void add_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 /**
420  * Returns the symconst entity associated to address mode.
421  */
422 ir_entity *get_ia32_am_sc(const ir_node *node)
423 {
424         const ia32_attr_t *attr = get_ia32_attr_const(node);
425         return attr->am_sc;
426 }
427
428 /**
429  * Sets the symconst entity associated to address mode.
430  */
431 void set_ia32_am_sc(ir_node *node, ir_entity *entity)
432 {
433         ia32_attr_t *attr = get_ia32_attr(node);
434         attr->am_sc       = entity;
435 }
436
437 /**
438  * Sets the sign bit for address mode symconst.
439  */
440 void set_ia32_am_sc_sign(ir_node *node)
441 {
442         ia32_attr_t *attr     = get_ia32_attr(node);
443         attr->data.am_sc_sign = 1;
444 }
445
446 /**
447  * Clears the sign bit for address mode symconst.
448  */
449 void clear_ia32_am_sc_sign(ir_node *node)
450 {
451         ia32_attr_t *attr     = get_ia32_attr(node);
452         attr->data.am_sc_sign = 0;
453 }
454
455 /**
456  * Returns the sign bit for address mode symconst.
457  */
458 int is_ia32_am_sc_sign(const ir_node *node)
459 {
460         const ia32_attr_t *attr = get_ia32_attr_const(node);
461         return attr->data.am_sc_sign;
462 }
463
464 void set_ia32_am_tls_segment(ir_node *node, bool value)
465 {
466         ia32_attr_t *attr = get_ia32_attr(node);
467         attr->data.am_tls_segment = value;
468 }
469
470 bool get_ia32_am_tls_segment(const ir_node *node)
471 {
472         const ia32_attr_t *attr = get_ia32_attr_const(node);
473         return attr->data.am_tls_segment;
474 }
475
476 /**
477  * Gets the addr mode const.
478  */
479 unsigned get_ia32_am_scale(const ir_node *node)
480 {
481         const ia32_attr_t *attr = get_ia32_attr_const(node);
482         return attr->data.am_scale;
483 }
484
485 /**
486  * Sets the index register scale for address mode.
487  */
488 void set_ia32_am_scale(ir_node *node, unsigned scale)
489 {
490         ia32_attr_t *attr = get_ia32_attr(node);
491         assert(scale <= 3 && "AM scale out of range [0 ... 3]");
492         attr->data.am_scale = scale;
493 }
494
495 void ia32_copy_am_attrs(ir_node *to, const ir_node *from)
496 {
497         set_ia32_ls_mode(to, get_ia32_ls_mode(from));
498         set_ia32_am_scale(to, get_ia32_am_scale(from));
499         set_ia32_am_sc(to, get_ia32_am_sc(from));
500         if (is_ia32_am_sc_sign(from))
501                 set_ia32_am_sc_sign(to);
502         add_ia32_am_offs_int(to, get_ia32_am_offs_int(from));
503         set_ia32_frame_ent(to, get_ia32_frame_ent(from));
504         if (is_ia32_use_frame(from))
505                 set_ia32_use_frame(to);
506 }
507
508 /**
509  * Sets the uses_frame flag.
510  */
511 void set_ia32_use_frame(ir_node *node)
512 {
513         ia32_attr_t *attr    = get_ia32_attr(node);
514         attr->data.use_frame = 1;
515 }
516
517 /**
518  * Clears the uses_frame flag.
519  */
520 void clear_ia32_use_frame(ir_node *node)
521 {
522         ia32_attr_t *attr    = get_ia32_attr(node);
523         attr->data.use_frame = 0;
524 }
525
526 /**
527  * Gets the uses_frame flag.
528  */
529 int is_ia32_use_frame(const ir_node *node)
530 {
531         const ia32_attr_t *attr = get_ia32_attr_const(node);
532         return attr->data.use_frame;
533 }
534
535 /**
536  * Sets node to commutative.
537  */
538 void set_ia32_commutative(ir_node *node)
539 {
540         ia32_attr_t *attr         = get_ia32_attr(node);
541         attr->data.is_commutative = 1;
542 }
543
544 /**
545  * Sets node to non-commutative.
546  */
547 void clear_ia32_commutative(ir_node *node)
548 {
549         ia32_attr_t *attr         = get_ia32_attr(node);
550         attr->data.is_commutative = 0;
551 }
552
553 /**
554  * Checks if node is commutative.
555  */
556 int is_ia32_commutative(const ir_node *node)
557 {
558         const ia32_attr_t *attr = get_ia32_attr_const(node);
559         return attr->data.is_commutative;
560 }
561
562 void set_ia32_need_stackent(ir_node *node)
563 {
564         ia32_attr_t *attr     = get_ia32_attr(node);
565         attr->data.need_stackent = 1;
566 }
567
568 void clear_ia32_need_stackent(ir_node *node)
569 {
570         ia32_attr_t *attr     = get_ia32_attr(node);
571         attr->data.need_stackent = 0;
572 }
573
574 int is_ia32_need_stackent(const ir_node *node)
575 {
576         const ia32_attr_t *attr = get_ia32_attr_const(node);
577         return attr->data.need_stackent;
578 }
579
580 void set_ia32_is_reload(ir_node *node)
581 {
582         ia32_attr_t *attr = get_ia32_attr(node);
583         attr->data.is_reload = 1;
584 }
585
586 int is_ia32_is_reload(const ir_node *node)
587 {
588         const ia32_attr_t *attr = get_ia32_attr_const(node);
589         return attr->data.is_reload;
590 }
591
592 void set_ia32_is_spill(ir_node *node)
593 {
594         ia32_attr_t *attr = get_ia32_attr(node);
595         attr->data.is_spill = 1;
596 }
597
598 int is_ia32_is_spill(const ir_node *node)
599 {
600         const ia32_attr_t *attr = get_ia32_attr_const(node);
601         return attr->data.is_spill;
602 }
603
604 void set_ia32_is_remat(ir_node *node)
605 {
606         ia32_attr_t *attr = get_ia32_attr(node);
607         attr->data.is_remat = 1;
608 }
609
610 int is_ia32_is_remat(const ir_node *node)
611 {
612         const ia32_attr_t *attr = get_ia32_attr_const(node);
613         return attr->data.is_remat;
614 }
615
616 /**
617  * Gets the mode of the stored/loaded value (only set for Store/Load)
618  */
619 ir_mode *get_ia32_ls_mode(const ir_node *node)
620 {
621         const ia32_attr_t *attr = get_ia32_attr_const(node);
622         return attr->ls_mode;
623 }
624
625 /**
626  * Sets the mode of the stored/loaded value (only set for Store/Load)
627  */
628 void set_ia32_ls_mode(ir_node *node, ir_mode *mode)
629 {
630         ia32_attr_t *attr = get_ia32_attr(node);
631         attr->ls_mode     = mode;
632 }
633
634 /**
635  * Gets the frame entity assigned to this node.
636  */
637 ir_entity *get_ia32_frame_ent(const ir_node *node)
638 {
639         const ia32_attr_t *attr = get_ia32_attr_const(node);
640         return attr->frame_ent;
641 }
642
643 /**
644  * Sets the frame entity for this node.
645  */
646 void set_ia32_frame_ent(ir_node *node, ir_entity *ent)
647 {
648         ia32_attr_t *attr = get_ia32_attr(node);
649         attr->frame_ent   = ent;
650         if (ent != NULL)
651                 set_ia32_use_frame(node);
652         else
653                 clear_ia32_use_frame(node);
654 }
655
656
657 /**
658  * Gets the instruction latency.
659  */
660 unsigned get_ia32_latency(const ir_node *node)
661 {
662         const ir_op *op               = get_irn_op(node);
663         const ia32_op_attr_t *op_attr = (ia32_op_attr_t*) get_op_attr(op);
664         return op_attr->latency;
665 }
666
667 const ir_switch_table *get_ia32_switch_table(const ir_node *node)
668 {
669         const ia32_switch_attr_t *attr = get_ia32_switch_attr_const(node);
670         return attr->table;
671 }
672
673 ia32_condition_code_t get_ia32_condcode(const ir_node *node)
674 {
675         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
676         return attr->condition_code;
677 }
678
679 /**
680  * Sets the condition code of a node
681  */
682 void set_ia32_condcode(ir_node *node, ia32_condition_code_t code)
683 {
684         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
685         attr->condition_code = code;
686 }
687
688 /**
689  * Returns the condition code of a node.
690  */
691 unsigned get_ia32_copyb_size(const ir_node *node)
692 {
693         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
694         return attr->size;
695 }
696
697 /**
698  * Get the exception label attribute.
699  */
700 unsigned get_ia32_exc_label(const ir_node *node)
701 {
702         const ia32_attr_t *attr = get_ia32_attr_const(node);
703         return attr->data.has_except_label;
704 }
705
706 /**
707  * Set the exception label attribute.
708  */
709 void set_ia32_exc_label(ir_node *node, unsigned flag)
710 {
711         ia32_attr_t *attr = get_ia32_attr(node);
712         attr->data.has_except_label = flag;
713 }
714
715 /**
716  * Return the exception label id.
717  */
718 ir_label_t get_ia32_exc_label_id(const ir_node *node)
719 {
720         const ia32_attr_t *attr = get_ia32_attr_const(node);
721
722         assert(attr->data.has_except_label);
723         return attr->exc_label;
724 }
725
726 /**
727  * Assign the exception label id.
728  */
729 void set_ia32_exc_label_id(ir_node *node, ir_label_t id)
730 {
731         ia32_attr_t *attr = get_ia32_attr(node);
732
733         assert(attr->data.has_except_label);
734         attr->exc_label = id;
735 }
736
737 #ifndef NDEBUG
738
739 static const char *ia32_get_old_node_name(const ir_node *irn)
740 {
741         ir_graph       *irg  = get_irn_irg(irn);
742         struct obstack *obst = be_get_be_obst(irg);
743
744         lc_eoprintf(firm_get_arg_env(), obst, "%+F", irn);
745         obstack_1grow(obst, 0);
746         return (const char*)obstack_finish(obst);
747 }
748
749 /**
750  * Sets the name of the original ir node.
751  */
752 void set_ia32_orig_node(ir_node *node, const ir_node *old)
753 {
754         const char  *name = ia32_get_old_node_name(old);
755         ia32_attr_t *attr = get_ia32_attr(node);
756         attr->orig_node   = name;
757 }
758
759 #endif /* NDEBUG */
760
761 void ia32_swap_left_right(ir_node *node)
762 {
763         ia32_attr_t *attr  = get_ia32_attr(node);
764         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
765         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
766
767         assert(is_ia32_commutative(node));
768         attr->data.ins_permuted = !attr->data.ins_permuted;
769         set_irn_n(node, n_ia32_binary_left,  right);
770         set_irn_n(node, n_ia32_binary_right, left);
771 }
772
773 /**
774  * Initializes the nodes attributes.
775  */
776 static void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
777                                  const arch_register_req_t **in_reqs,
778                                  int n_res)
779 {
780         ir_graph        *irg  = get_irn_irg(node);
781         struct obstack  *obst = get_irg_obstack(irg);
782         ia32_attr_t     *attr = get_ia32_attr(node);
783         backend_info_t  *info;
784
785         arch_set_irn_flags(node, flags);
786         arch_set_irn_register_reqs_in(node, in_reqs);
787
788 #ifndef NDEBUG
789         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
790 #endif
791
792         info            = be_get_info(node);
793         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
794         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
795 }
796
797 static void init_ia32_x87_attributes(ir_node *res)
798 {
799 #ifndef NDEBUG
800         ia32_attr_t *attr  = get_ia32_attr(res);
801         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
802 #endif
803         ir_graph *const irg = get_irn_irg(res);
804         ia32_request_x87_sim(irg);
805 }
806
807 static void init_ia32_asm_attributes(ir_node *res)
808 {
809 #ifndef NDEBUG
810         ia32_attr_t *attr  = get_ia32_attr(res);
811         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
812 #endif
813
814         ir_graph *const irg = get_irn_irg(res);
815         ia32_request_x87_sim(irg); /* asm might have fp operands. */
816 }
817
818 static void init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
819                                            int symconst_sign, int no_pic_adjust,
820                                            long offset)
821 {
822         ia32_immediate_attr_t *attr = (ia32_immediate_attr_t*)get_irn_generic_attr(res);
823
824 #ifndef NDEBUG
825         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
826 #endif
827         attr->symconst      = symconst;
828         attr->sc_sign       = symconst_sign;
829         attr->no_pic_adjust = no_pic_adjust;
830         attr->offset        = offset;
831 }
832
833 static void init_ia32_call_attributes(ir_node* res, unsigned pop,
834                                       ir_type* call_tp)
835 {
836         ia32_call_attr_t *attr = (ia32_call_attr_t*)get_irn_generic_attr(res);
837
838 #ifndef NDEBUG
839         attr->attr.attr_type  |= IA32_ATTR_ia32_call_attr_t;
840 #endif
841         attr->pop     = pop;
842         attr->call_tp = call_tp;
843 }
844
845 static void init_ia32_copyb_attributes(ir_node *res, unsigned size)
846 {
847         ia32_copyb_attr_t *attr = (ia32_copyb_attr_t*)get_irn_generic_attr(res);
848
849 #ifndef NDEBUG
850         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
851 #endif
852         attr->size = size;
853 }
854
855 static void init_ia32_condcode_attributes(ir_node *res,
856                                           ia32_condition_code_t cc)
857 {
858         ia32_condcode_attr_t *attr = (ia32_condcode_attr_t*)get_irn_generic_attr(res);
859
860 #ifndef NDEBUG
861         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
862 #endif
863         attr->condition_code = cc;
864 }
865
866 static void init_ia32_climbframe_attributes(ir_node *res, unsigned count)
867 {
868         ia32_climbframe_attr_t *attr = (ia32_climbframe_attr_t*)get_irn_generic_attr(res);
869
870 #ifndef NDEBUG
871         attr->attr.attr_type  |= IA32_ATTR_ia32_climbframe_attr_t;
872 #endif
873         attr->count = count;
874 }
875
876 static void init_ia32_switch_attributes(ir_node *node,
877                                         const ir_switch_table *table)
878 {
879         ia32_switch_attr_t *attr = (ia32_switch_attr_t*) get_irn_generic_attr(node);
880 #ifndef NDEBUG
881         attr->attr.attr_type |= IA32_ATTR_ia32_switch_attr_t;
882 #endif
883         attr->table = table;
884
885         be_foreach_out(node, o) {
886                 arch_set_irn_register_req_out(node, o, arch_no_register_req);
887         }
888 }
889
890 /* default compare operation to compare attributes */
891 static int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b)
892 {
893         if (a->data.tp != b->data.tp)
894                 return 1;
895
896         if (a->data.am_scale != b->data.am_scale
897             || a->data.am_sc_sign != b->data.am_sc_sign
898             || a->am_offs != b->am_offs
899             || a->am_sc != b->am_sc
900                 || a->data.am_sc_no_pic_adjust != b->data.am_sc_no_pic_adjust
901             || a->ls_mode != b->ls_mode)
902                 return 1;
903
904         /* nodes with not yet assigned entities shouldn't be CSEd (important for
905          * unsigned int -> double conversions */
906         if (a->data.use_frame && a->frame_ent == NULL)
907                 return 1;
908         if (b->data.use_frame && b->frame_ent == NULL)
909                 return 1;
910
911         if (a->data.use_frame != b->data.use_frame
912             || a->frame_ent != b->frame_ent)
913                 return 1;
914
915         if (a->data.has_except_label != b->data.has_except_label)
916                 return 1;
917
918         if (a->data.ins_permuted != b->data.ins_permuted)
919                 return 1;
920
921         return 0;
922 }
923
924 /** Compare nodes attributes for all "normal" nodes. */
925 static int ia32_compare_nodes_attr(const ir_node *a, const ir_node *b)
926 {
927         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
928         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
929
930         return ia32_compare_attr(attr_a, attr_b);
931 }
932
933 /** Compare node attributes for nodes with condition code. */
934 static int ia32_compare_condcode_attr(const ir_node *a, const ir_node *b)
935 {
936         const ia32_condcode_attr_t *attr_a;
937         const ia32_condcode_attr_t *attr_b;
938
939         if (ia32_compare_nodes_attr(a, b))
940                 return 1;
941
942         attr_a = get_ia32_condcode_attr_const(a);
943         attr_b = get_ia32_condcode_attr_const(b);
944
945         if (attr_a->condition_code != attr_b->condition_code)
946                 return 1;
947
948         return 0;
949 }
950
951 /** Compare node attributes for call nodes. */
952 static int ia32_compare_call_attr(const ir_node *a, const ir_node *b)
953 {
954         const ia32_call_attr_t *attr_a;
955         const ia32_call_attr_t *attr_b;
956
957         if (ia32_compare_nodes_attr(a, b))
958                 return 1;
959
960         attr_a = get_ia32_call_attr_const(a);
961         attr_b = get_ia32_call_attr_const(b);
962
963         if (attr_a->pop != attr_b->pop)
964                 return 1;
965
966         if (attr_a->call_tp != attr_b->call_tp)
967                 return 1;
968
969         return 0;
970 }
971
972 /** Compare node attributes for CopyB nodes. */
973 static int ia32_compare_copyb_attr(const ir_node *a, const ir_node *b)
974 {
975         const ia32_copyb_attr_t *attr_a;
976         const ia32_copyb_attr_t *attr_b;
977
978         if (ia32_compare_nodes_attr(a, b))
979                 return 1;
980
981         attr_a = get_ia32_copyb_attr_const(a);
982         attr_b = get_ia32_copyb_attr_const(b);
983
984         if (attr_a->size != attr_b->size)
985                 return 1;
986
987         return 0;
988 }
989
990
991 /** Compare ASM node attributes. */
992 static int ia32_compare_asm_attr(const ir_node *a, const ir_node *b)
993 {
994         const ia32_asm_attr_t *attr_a;
995         const ia32_asm_attr_t *attr_b;
996
997         if (ia32_compare_nodes_attr(a, b))
998                 return 1;
999
1000         attr_a = get_ia32_asm_attr_const(a);
1001         attr_b = get_ia32_asm_attr_const(b);
1002
1003         if (attr_a->asm_text != attr_b->asm_text)
1004                 return 1;
1005
1006         return 0;
1007 }
1008
1009 /**
1010  * Hash function for Immediates
1011  */
1012 static unsigned ia32_hash_Immediate(const ir_node *irn)
1013 {
1014         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1015
1016         return hash_ptr(a->symconst) + (a->sc_sign << 16) + a->offset;
1017 }
1018
1019 /** Compare node attributes for Immediates. */
1020 static int ia32_compare_immediate_attr(const ir_node *a, const ir_node *b)
1021 {
1022         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1023         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1024
1025         if (attr_a->symconst != attr_b->symconst
1026                 || attr_a->sc_sign != attr_b->sc_sign
1027                 || attr_a->no_pic_adjust != attr_b->no_pic_adjust
1028                 || attr_a->offset != attr_b->offset) {
1029                 return 1;
1030         }
1031
1032         return 0;
1033 }
1034
1035 /** Compare node attributes for x87 nodes. */
1036 static int ia32_compare_x87_attr(const ir_node *a, const ir_node *b)
1037 {
1038         return ia32_compare_nodes_attr(a, b);
1039 }
1040
1041 /** Compare node attributes for ClimbFrame nodes. */
1042 static int ia32_compare_climbframe_attr(const ir_node *a, const ir_node *b)
1043 {
1044         const ia32_climbframe_attr_t *attr_a;
1045         const ia32_climbframe_attr_t *attr_b;
1046
1047         if (ia32_compare_nodes_attr(a, b))
1048                 return 1;
1049
1050         attr_a = get_ia32_climbframe_attr_const(a);
1051         attr_b = get_ia32_climbframe_attr_const(b);
1052
1053         if (attr_a->count != attr_b->count)
1054                 return 1;
1055
1056         return 0;
1057 }
1058
1059 /* copies the ia32 attributes */
1060 static void ia32_copy_attr(ir_graph *irg, const ir_node *old_node,
1061                            ir_node *new_node)
1062 {
1063         struct obstack    *obst     = get_irg_obstack(irg);
1064         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1065         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1066         backend_info_t    *old_info = be_get_info(old_node);
1067         backend_info_t    *new_info = be_get_info(new_node);
1068
1069         /* copy the attributes */
1070         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1071
1072         /* copy out flags */
1073         new_info->out_infos =
1074                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
1075         new_info->in_reqs = old_info->in_reqs;
1076         new_info->flags = old_info->flags;
1077 }
1078
1079 static void ia32_init_op(ir_op *op, unsigned latency)
1080 {
1081         ia32_op_attr_t *attr = OALLOCZ(&opcodes_obst, ia32_op_attr_t);
1082         attr->latency = latency;
1083         set_op_attr(op, attr);
1084 }
1085
1086 /* Include the generated constructor functions */
1087 #include "gen_ia32_new_nodes.c.inl"