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