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