7452cb9ef0792bac58594a0d37f773ac3ed55924
[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         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 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
762 /**
763  * Returns whether or not the node is an AddrModeS node.
764  */
765 int is_ia32_AddrModeS(const ir_node *node)
766 {
767         const ia32_attr_t *attr = get_ia32_attr_const(node);
768         return (attr->data.tp == ia32_AddrModeS);
769 }
770
771 /**
772  * Returns whether or not the node is an AddrModeD node.
773  */
774 int is_ia32_AddrModeD(const ir_node *node)
775 {
776         const ia32_attr_t *attr = get_ia32_attr_const(node);
777         return (attr->data.tp == ia32_AddrModeD);
778 }
779
780 void ia32_swap_left_right(ir_node *node)
781 {
782         ia32_attr_t *attr  = get_ia32_attr(node);
783         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
784         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
785
786         assert(is_ia32_commutative(node));
787         attr->data.ins_permuted = !attr->data.ins_permuted;
788         set_irn_n(node, n_ia32_binary_left,  right);
789         set_irn_n(node, n_ia32_binary_right, left);
790 }
791
792 /**
793  * Initializes the nodes attributes.
794  */
795 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
796                           const arch_register_req_t **in_reqs,
797                           const be_execution_unit_t ***execution_units,
798                           int n_res)
799 {
800         ir_graph        *irg  = get_irn_irg(node);
801         struct obstack  *obst = get_irg_obstack(irg);
802         ia32_attr_t     *attr = get_ia32_attr(node);
803         backend_info_t  *info;
804
805         arch_irn_set_flags(node, flags);
806         arch_set_in_register_reqs(node, in_reqs);
807
808         attr->exec_units  = execution_units;
809 #ifndef NDEBUG
810         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
811 #endif
812
813         info            = be_get_info(node);
814         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
815         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
816 }
817
818 void init_ia32_x87_attributes(ir_node *res)
819 {
820         ir_graph        *irg      = get_irn_irg(res);
821         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
822 #ifndef NDEBUG
823         ia32_attr_t *attr  = get_ia32_attr(res);
824         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
825 #else
826         (void) res;
827 #endif
828         irg_data->do_x87_sim = 1;
829 }
830
831 void init_ia32_asm_attributes(ir_node *res)
832 {
833 #ifndef NDEBUG
834         ia32_attr_t *attr  = get_ia32_attr(res);
835         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
836 #else
837         (void) res;
838 #endif
839 }
840
841 void init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
842                                     int symconst_sign, int no_pic_adjust,
843                                     long offset)
844 {
845         ia32_immediate_attr_t *attr = get_irn_generic_attr(res);
846
847 #ifndef NDEBUG
848         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
849 #endif
850         attr->symconst      = symconst;
851         attr->sc_sign       = symconst_sign;
852         attr->no_pic_adjust = no_pic_adjust;
853         attr->offset        = offset;
854 }
855
856 void init_ia32_call_attributes(ir_node* res, unsigned pop, ir_type* call_tp)
857 {
858         ia32_call_attr_t *attr = get_irn_generic_attr(res);
859
860 #ifndef NDEBUG
861         attr->attr.attr_type  |= IA32_ATTR_ia32_call_attr_t;
862 #endif
863         attr->pop     = pop;
864         attr->call_tp = call_tp;
865 }
866
867 void init_ia32_copyb_attributes(ir_node *res, unsigned size)
868 {
869         ia32_copyb_attr_t *attr = get_irn_generic_attr(res);
870
871 #ifndef NDEBUG
872         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
873 #endif
874         attr->size = size;
875 }
876
877 void init_ia32_condcode_attributes(ir_node *res, long pnc)
878 {
879         ia32_condcode_attr_t *attr = get_irn_generic_attr(res);
880
881 #ifndef NDEBUG
882         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
883 #endif
884         attr->pn_code = pnc;
885 }
886
887 void init_ia32_climbframe_attributes(ir_node *res, unsigned count)
888 {
889         ia32_climbframe_attr_t *attr = get_irn_generic_attr(res);
890
891 #ifndef NDEBUG
892         attr->attr.attr_type  |= IA32_ATTR_ia32_climbframe_attr_t;
893 #endif
894         attr->count = count;
895 }
896
897
898 /* default compare operation to compare attributes */
899 static int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b)
900 {
901         if (a->data.tp != b->data.tp)
902                 return 1;
903
904         if (a->data.am_scale != b->data.am_scale
905             || a->data.am_sc_sign != b->data.am_sc_sign
906             || a->am_offs != b->am_offs
907             || a->am_sc != b->am_sc
908                 || a->data.am_sc_no_pic_adjust != b->data.am_sc_no_pic_adjust
909             || a->ls_mode != b->ls_mode)
910                 return 1;
911
912         /* nodes with not yet assigned entities shouldn't be CSEd (important for
913          * unsigned int -> double conversions */
914         if (a->data.use_frame && a->frame_ent == NULL)
915                 return 1;
916         if (b->data.use_frame && b->frame_ent == NULL)
917                 return 1;
918
919         if (a->data.use_frame != b->data.use_frame
920             || a->frame_ent != b->frame_ent)
921                 return 1;
922
923         if (a->data.has_except_label != b->data.has_except_label)
924                 return 1;
925
926         if (a->data.ins_permuted != b->data.ins_permuted
927                         || a->data.cmp_unsigned != b->data.cmp_unsigned)
928                 return 1;
929
930         return 0;
931 }
932
933 /** Compare nodes attributes for all "normal" nodes. */
934 static int ia32_compare_nodes_attr(ir_node *a, ir_node *b)
935 {
936         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
937         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
938
939         return ia32_compare_attr(attr_a, attr_b);
940 }
941
942 /** Compare node attributes for nodes with condition code. */
943 static int ia32_compare_condcode_attr(ir_node *a, ir_node *b)
944 {
945         const ia32_condcode_attr_t *attr_a;
946         const ia32_condcode_attr_t *attr_b;
947
948         if (ia32_compare_nodes_attr(a, b))
949                 return 1;
950
951         attr_a = get_ia32_condcode_attr_const(a);
952         attr_b = get_ia32_condcode_attr_const(b);
953
954         if (attr_a->pn_code != attr_b->pn_code)
955                 return 1;
956
957         return 0;
958 }
959
960 /** Compare node attributes for call nodes. */
961 static int ia32_compare_call_attr(ir_node *a, ir_node *b)
962 {
963         const ia32_call_attr_t *attr_a;
964         const ia32_call_attr_t *attr_b;
965
966         if (ia32_compare_nodes_attr(a, b))
967                 return 1;
968
969         attr_a = get_ia32_call_attr_const(a);
970         attr_b = get_ia32_call_attr_const(b);
971
972         if (attr_a->pop != attr_b->pop)
973                 return 1;
974
975         if (attr_a->call_tp != attr_b->call_tp)
976                 return 1;
977
978         return 0;
979 }
980
981 /** Compare node attributes for CopyB nodes. */
982 static int ia32_compare_copyb_attr(ir_node *a, ir_node *b)
983 {
984         const ia32_copyb_attr_t *attr_a;
985         const ia32_copyb_attr_t *attr_b;
986
987         if (ia32_compare_nodes_attr(a, b))
988                 return 1;
989
990         attr_a = get_ia32_copyb_attr_const(a);
991         attr_b = get_ia32_copyb_attr_const(b);
992
993         if (attr_a->size != attr_b->size)
994                 return 1;
995
996         return 0;
997 }
998
999
1000 /** Compare ASM node attributes. */
1001 static int ia32_compare_asm_attr(ir_node *a, ir_node *b)
1002 {
1003         const ia32_asm_attr_t *attr_a;
1004         const ia32_asm_attr_t *attr_b;
1005
1006         if (ia32_compare_nodes_attr(a, b))
1007                 return 1;
1008
1009         attr_a = get_ia32_asm_attr_const(a);
1010         attr_b = get_ia32_asm_attr_const(b);
1011
1012         if (attr_a->asm_text != attr_b->asm_text)
1013                 return 1;
1014
1015         return 0;
1016 }
1017
1018 /**
1019  * Hash function for Immediates
1020  */
1021 static unsigned ia32_hash_Immediate(const ir_node *irn)
1022 {
1023         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1024
1025         return HASH_PTR(a->symconst) + (a->sc_sign << 16) + a->offset;
1026 }
1027
1028 /** Compare node attributes for Immediates. */
1029 static int ia32_compare_immediate_attr(ir_node *a, ir_node *b)
1030 {
1031         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1032         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1033
1034         if (attr_a->symconst != attr_b->symconst
1035                 || attr_a->sc_sign != attr_b->sc_sign
1036                 || attr_a->no_pic_adjust != attr_b->no_pic_adjust
1037                 || attr_a->offset != attr_b->offset) {
1038                 return 1;
1039         }
1040
1041         return 0;
1042 }
1043
1044 /** Compare node attributes for x87 nodes. */
1045 static int ia32_compare_x87_attr(ir_node *a, ir_node *b)
1046 {
1047         return ia32_compare_nodes_attr(a, b);
1048 }
1049
1050 /** Compare node attributes for ClimbFrame nodes. */
1051 static int ia32_compare_climbframe_attr(ir_node *a, ir_node *b)
1052 {
1053         const ia32_climbframe_attr_t *attr_a;
1054         const ia32_climbframe_attr_t *attr_b;
1055
1056         if (ia32_compare_nodes_attr(a, b))
1057                 return 1;
1058
1059         attr_a = get_ia32_climbframe_attr_const(a);
1060         attr_b = get_ia32_climbframe_attr_const(b);
1061
1062         if (attr_a->count != attr_b->count)
1063                 return 1;
1064
1065         return 0;
1066 }
1067
1068 /* copies the ia32 attributes */
1069 static void ia32_copy_attr(ir_graph *irg, const ir_node *old_node,
1070                            ir_node *new_node)
1071 {
1072         struct obstack    *obst     = get_irg_obstack(irg);
1073         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1074         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1075         backend_info_t    *old_info = be_get_info(old_node);
1076         backend_info_t    *new_info = be_get_info(new_node);
1077
1078         /* copy the attributes */
1079         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1080
1081         /* copy out flags */
1082         new_info->out_infos =
1083                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
1084         new_info->in_reqs = old_info->in_reqs;
1085 }
1086
1087 /* Include the generated constructor functions */
1088 #include "gen_ia32_new_nodes.c.inl"