f427c970054a0db14654e1de5a29c704ccb74c5d
[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 argument register requirements of an ia32 node.
654  */
655 const arch_register_req_t **get_ia32_in_req_all(const ir_node *node)
656 {
657         const ia32_attr_t *attr = get_ia32_attr_const(node);
658         return attr->in_req;
659 }
660
661 /**
662  * Sets the argument register requirements of an ia32 node.
663  */
664 void set_ia32_in_req_all(ir_node *node, const arch_register_req_t **reqs)
665 {
666         ia32_attr_t *attr = get_ia32_attr(node);
667         attr->in_req      = reqs;
668 }
669
670 /**
671  * Returns the argument register requirement at position pos of an ia32 node.
672  */
673 const arch_register_req_t *get_ia32_in_req(const ir_node *node, int pos)
674 {
675         const ia32_attr_t *attr = get_ia32_attr_const(node);
676         return attr->in_req[pos];
677 }
678
679 /**
680  * Sets the IN register requirements at position pos.
681  */
682 void set_ia32_req_in(ir_node *node, const arch_register_req_t *req, int pos)
683 {
684         ia32_attr_t *attr = get_ia32_attr(node);
685         attr->in_req[pos] = req;
686 }
687
688 /**
689  * Returns the condition code of a node.
690  */
691 long get_ia32_condcode(const ir_node *node)
692 {
693         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
694         return attr->pn_code;
695 }
696
697 /**
698  * Sets the condition code of a node
699  */
700 void set_ia32_condcode(ir_node *node, long code)
701 {
702         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
703         attr->pn_code = code;
704 }
705
706 /**
707  * Returns the condition code of a node.
708  */
709 unsigned get_ia32_copyb_size(const ir_node *node)
710 {
711         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
712         return attr->size;
713 }
714
715 /**
716  * Get the list of available execution units.
717  */
718 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node)
719 {
720         const ia32_attr_t *attr = get_ia32_attr_const(node);
721         return attr->exec_units;
722 }
723
724 /**
725  * Get the exception label attribute.
726  */
727 unsigned get_ia32_exc_label(const ir_node *node)
728 {
729         const ia32_attr_t *attr = get_ia32_attr_const(node);
730         return attr->data.has_except_label;
731 }
732
733 /**
734  * Set the exception label attribute.
735  */
736 void set_ia32_exc_label(ir_node *node, unsigned flag)
737 {
738         ia32_attr_t *attr = get_ia32_attr(node);
739         attr->data.has_except_label = flag;
740 }
741
742 /**
743  * Return the exception label id.
744  */
745 ir_label_t get_ia32_exc_label_id(const ir_node *node)
746 {
747         const ia32_attr_t *attr = get_ia32_attr_const(node);
748
749         assert(attr->data.has_except_label);
750         return attr->exc_label;
751 }
752
753 /**
754  * Assign the exception label id.
755  */
756 void set_ia32_exc_label_id(ir_node *node, ir_label_t id)
757 {
758         ia32_attr_t *attr = get_ia32_attr(node);
759
760         assert(attr->data.has_except_label);
761         attr->exc_label = id;
762 }
763
764 #ifndef NDEBUG
765
766 /**
767  * Returns the name of the original ir node.
768  */
769 const char *get_ia32_orig_node(const ir_node *node)
770 {
771         const ia32_attr_t *attr = get_ia32_attr_const(node);
772         return attr->orig_node;
773 }
774
775 static const char *ia32_get_old_node_name(const ir_node *irn)
776 {
777         struct obstack *obst = env_cg->isa->name_obst;
778
779         lc_eoprintf(firm_get_arg_env(), obst, "%+F", irn);
780         obstack_1grow(obst, 0);
781         return obstack_finish(obst);
782 }
783
784 /**
785  * Sets the name of the original ir node.
786  */
787 void set_ia32_orig_node(ir_node *node, const ir_node *old)
788 {
789         const char  *name = ia32_get_old_node_name(old);
790         ia32_attr_t *attr = get_ia32_attr(node);
791         attr->orig_node   = name;
792 }
793
794 #endif /* NDEBUG */
795
796
797 /**
798  * Returns whether or not the node is an AddrModeS node.
799  */
800 int is_ia32_AddrModeS(const ir_node *node)
801 {
802         const ia32_attr_t *attr = get_ia32_attr_const(node);
803         return (attr->data.tp == ia32_AddrModeS);
804 }
805
806 /**
807  * Returns whether or not the node is an AddrModeD node.
808  */
809 int is_ia32_AddrModeD(const ir_node *node)
810 {
811         const ia32_attr_t *attr = get_ia32_attr_const(node);
812         return (attr->data.tp == ia32_AddrModeD);
813 }
814
815 void ia32_swap_left_right(ir_node *node)
816 {
817         ia32_attr_t *attr  = get_ia32_attr(node);
818         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
819         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
820
821         assert(is_ia32_commutative(node));
822         attr->data.ins_permuted = !attr->data.ins_permuted;
823         set_irn_n(node, n_ia32_binary_left,  right);
824         set_irn_n(node, n_ia32_binary_right, left);
825 }
826
827 /**
828  * Initializes the nodes attributes.
829  */
830 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
831                           const arch_register_req_t **in_reqs,
832                           const be_execution_unit_t ***execution_units,
833                           int n_res)
834 {
835         ir_graph        *irg  = get_irn_irg(node);
836         struct obstack  *obst = get_irg_obstack(irg);
837         ia32_attr_t     *attr = get_ia32_attr(node);
838         backend_info_t  *info;
839
840         arch_irn_set_flags(node, flags);
841         set_ia32_in_req_all(node, in_reqs);
842
843         attr->exec_units  = execution_units;
844 #ifndef NDEBUG
845         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
846 #endif
847
848         info            = be_get_info(node);
849         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
850         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
851 }
852
853 void init_ia32_x87_attributes(ir_node *res)
854 {
855 #ifndef NDEBUG
856         ia32_attr_t *attr  = get_ia32_attr(res);
857         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
858 #else
859         (void) res;
860 #endif
861         ia32_current_cg->do_x87_sim = 1;
862 }
863
864 void init_ia32_asm_attributes(ir_node *res)
865 {
866 #ifndef NDEBUG
867         ia32_attr_t *attr  = get_ia32_attr(res);
868         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
869 #else
870         (void) res;
871 #endif
872 }
873
874 void init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
875                                     int symconst_sign, int no_pic_adjust,
876                                     long offset)
877 {
878         ia32_immediate_attr_t *attr = get_irn_generic_attr(res);
879
880 #ifndef NDEBUG
881         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
882 #endif
883         attr->symconst      = symconst;
884         attr->sc_sign       = symconst_sign;
885         attr->no_pic_adjust = no_pic_adjust;
886         attr->offset        = offset;
887 }
888
889 void init_ia32_call_attributes(ir_node* res, unsigned pop, ir_type* call_tp)
890 {
891         ia32_call_attr_t *attr = get_irn_generic_attr(res);
892
893 #ifndef NDEBUG
894         attr->attr.attr_type  |= IA32_ATTR_ia32_call_attr_t;
895 #endif
896         attr->pop     = pop;
897         attr->call_tp = call_tp;
898 }
899
900 void init_ia32_copyb_attributes(ir_node *res, unsigned size)
901 {
902         ia32_copyb_attr_t *attr = get_irn_generic_attr(res);
903
904 #ifndef NDEBUG
905         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
906 #endif
907         attr->size = size;
908 }
909
910 void init_ia32_condcode_attributes(ir_node *res, long pnc)
911 {
912         ia32_condcode_attr_t *attr = get_irn_generic_attr(res);
913
914 #ifndef NDEBUG
915         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
916 #endif
917         attr->pn_code = pnc;
918 }
919
920 void init_ia32_climbframe_attributes(ir_node *res, unsigned count)
921 {
922         ia32_climbframe_attr_t *attr = get_irn_generic_attr(res);
923
924 #ifndef NDEBUG
925         attr->attr.attr_type  |= IA32_ATTR_ia32_climbframe_attr_t;
926 #endif
927         attr->count = count;
928 }
929
930
931 /* default compare operation to compare attributes */
932 static int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b)
933 {
934         if (a->data.tp != b->data.tp)
935                 return 1;
936
937         if (a->data.am_scale != b->data.am_scale
938             || a->data.am_sc_sign != b->data.am_sc_sign
939             || a->am_offs != b->am_offs
940             || a->am_sc != b->am_sc
941                 || a->data.am_sc_no_pic_adjust != b->data.am_sc_no_pic_adjust
942             || a->ls_mode != b->ls_mode)
943                 return 1;
944
945         /* nodes with not yet assigned entities shouldn't be CSEd (important for
946          * unsigned int -> double conversions */
947         if (a->data.use_frame && a->frame_ent == NULL)
948                 return 1;
949         if (b->data.use_frame && b->frame_ent == NULL)
950                 return 1;
951
952         if (a->data.use_frame != b->data.use_frame
953             || a->frame_ent != b->frame_ent)
954                 return 1;
955
956         if (a->data.has_except_label != b->data.has_except_label)
957                 return 1;
958
959         if (a->data.ins_permuted != b->data.ins_permuted
960                         || a->data.cmp_unsigned != b->data.cmp_unsigned)
961                 return 1;
962
963         return 0;
964 }
965
966 /** Compare nodes attributes for all "normal" nodes. */
967 static int ia32_compare_nodes_attr(ir_node *a, ir_node *b)
968 {
969         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
970         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
971
972         return ia32_compare_attr(attr_a, attr_b);
973 }
974
975 /** Compare node attributes for nodes with condition code. */
976 static int ia32_compare_condcode_attr(ir_node *a, ir_node *b)
977 {
978         const ia32_condcode_attr_t *attr_a;
979         const ia32_condcode_attr_t *attr_b;
980
981         if (ia32_compare_nodes_attr(a, b))
982                 return 1;
983
984         attr_a = get_ia32_condcode_attr_const(a);
985         attr_b = get_ia32_condcode_attr_const(b);
986
987         if (attr_a->pn_code != attr_b->pn_code)
988                 return 1;
989
990         return 0;
991 }
992
993 /** Compare node attributes for call nodes. */
994 static int ia32_compare_call_attr(ir_node *a, ir_node *b)
995 {
996         const ia32_call_attr_t *attr_a;
997         const ia32_call_attr_t *attr_b;
998
999         if (ia32_compare_nodes_attr(a, b))
1000                 return 1;
1001
1002         attr_a = get_ia32_call_attr_const(a);
1003         attr_b = get_ia32_call_attr_const(b);
1004
1005         if (attr_a->pop != attr_b->pop)
1006                 return 1;
1007
1008         if (attr_a->call_tp != attr_b->call_tp)
1009                 return 1;
1010
1011         return 0;
1012 }
1013
1014 /** Compare node attributes for CopyB nodes. */
1015 static int ia32_compare_copyb_attr(ir_node *a, ir_node *b)
1016 {
1017         const ia32_copyb_attr_t *attr_a;
1018         const ia32_copyb_attr_t *attr_b;
1019
1020         if (ia32_compare_nodes_attr(a, b))
1021                 return 1;
1022
1023         attr_a = get_ia32_copyb_attr_const(a);
1024         attr_b = get_ia32_copyb_attr_const(b);
1025
1026         if (attr_a->size != attr_b->size)
1027                 return 1;
1028
1029         return 0;
1030 }
1031
1032
1033 /** Compare ASM node attributes. */
1034 static int ia32_compare_asm_attr(ir_node *a, ir_node *b)
1035 {
1036         const ia32_asm_attr_t *attr_a;
1037         const ia32_asm_attr_t *attr_b;
1038
1039         if (ia32_compare_nodes_attr(a, b))
1040                 return 1;
1041
1042         attr_a = get_ia32_asm_attr_const(a);
1043         attr_b = get_ia32_asm_attr_const(b);
1044
1045         if (attr_a->asm_text != attr_b->asm_text)
1046                 return 1;
1047
1048         return 0;
1049 }
1050
1051 /**
1052  * Hash function for Immediates
1053  */
1054 static unsigned ia32_hash_Immediate(const ir_node *irn)
1055 {
1056         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1057
1058         return HASH_PTR(a->symconst) + (a->sc_sign << 16) + a->offset;
1059 }
1060
1061 /** Compare node attributes for Immediates. */
1062 static int ia32_compare_immediate_attr(ir_node *a, ir_node *b)
1063 {
1064         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1065         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1066
1067         if (attr_a->symconst != attr_b->symconst
1068                 || attr_a->sc_sign != attr_b->sc_sign
1069                 || attr_a->no_pic_adjust != attr_b->no_pic_adjust
1070                 || attr_a->offset != attr_b->offset) {
1071                 return 1;
1072         }
1073
1074         return 0;
1075 }
1076
1077 /** Compare node attributes for x87 nodes. */
1078 static int ia32_compare_x87_attr(ir_node *a, ir_node *b)
1079 {
1080         return ia32_compare_nodes_attr(a, b);
1081 }
1082
1083 /** Compare node attributes for ClimbFrame nodes. */
1084 static int ia32_compare_climbframe_attr(ir_node *a, ir_node *b)
1085 {
1086         const ia32_climbframe_attr_t *attr_a;
1087         const ia32_climbframe_attr_t *attr_b;
1088
1089         if (ia32_compare_nodes_attr(a, b))
1090                 return 1;
1091
1092         attr_a = get_ia32_climbframe_attr_const(a);
1093         attr_b = get_ia32_climbframe_attr_const(b);
1094
1095         if (attr_a->count != attr_b->count)
1096                 return 1;
1097
1098         return 0;
1099 }
1100
1101 /* copies the ia32 attributes */
1102 static void ia32_copy_attr(ir_graph *irg, const ir_node *old_node,
1103                            ir_node *new_node)
1104 {
1105         struct obstack    *obst     = get_irg_obstack(irg);
1106         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1107         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1108         backend_info_t    *old_info = be_get_info(old_node);
1109         backend_info_t    *new_info = be_get_info(new_node);
1110
1111         /* copy the attributes */
1112         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1113
1114         /* copy out flags */
1115         new_info->out_infos =
1116                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
1117 }
1118
1119 /* Include the generated constructor functions */
1120 #include "gen_ia32_new_nodes.c.inl"