fix fehler174
[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 "irvrfy_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  *      _                                   _       _             __
60  *     | |                                 (_)     | |           / _|
61  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
62  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
63  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
64  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
65  *                       | |
66  *                       |_|
67  ***********************************************************************************/
68
69 /**
70  * Dumper interface for dumping ia32 nodes in vcg.
71  * @param n        the node to dump
72  * @param F        the output file
73  * @param reason   indicates which kind of information should be dumped
74  * @return 0 on success or != 0 on failure
75  */
76 static int ia32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
77         ir_mode     *mode = NULL;
78         int          bad  = 0;
79
80         switch (reason) {
81                 case dump_node_opcode_txt:
82                         fprintf(F, "%s", get_irn_opname(n));
83
84                         if(is_ia32_Immediate(n) || is_ia32_Const(n)) {
85                                 const ia32_immediate_attr_t *attr
86                                         = get_ia32_immediate_attr_const(n);
87
88                                 fputc(' ', F);
89                                 if(attr->symconst) {
90                                         if(attr->sc_sign) {
91                                                 fputc('-', F);
92                                         }
93                                         fputs(get_entity_name(attr->symconst), F);
94                                 }
95                                 if(attr->offset != 0 || attr->symconst == NULL) {
96                                         if(attr->offset > 0 && attr->symconst != NULL) {
97                                                 fputc('+', F);
98                                         }
99                                         fprintf(F, "%ld", attr->offset);
100                                         if (attr->no_pic_adjust) {
101                                                 fputs("(no_pic_adjust)", F);
102                                         }
103                                 }
104                         }
105                         else {
106                                 const ia32_attr_t *attr = get_ia32_attr_const(n);
107
108                                 if(attr->am_sc != NULL || attr->am_offs != 0)
109                                         fputs(" [", F);
110
111                                 if(attr->am_sc != NULL) {
112                                         if(attr->data.am_sc_sign) {
113                                                 fputc('-', F);
114                                         }
115                                         fputs(get_entity_name(attr->am_sc), F);
116                                         if(attr->data.am_sc_no_pic_adjust) {
117                                                 fputs("(no_pic_adjust)", F);
118                                         }
119                                 }
120                                 if(attr->am_offs != 0) {
121                                         if(attr->am_offs > 0 && attr->am_sc != NULL) {
122                                                 fputc('+', F);
123                                         }
124                                         fprintf(F, "%d", attr->am_offs);
125                                 }
126
127                                 if(attr->am_sc != NULL || attr->am_offs != 0)
128                                         fputc(']', F);
129                         }
130                         break;
131
132                 case dump_node_mode_txt:
133                         mode = get_ia32_ls_mode(n);
134                         if (mode != NULL)
135                                 fprintf(F, "[%s]", get_mode_name(mode));
136                         break;
137
138                 case dump_node_nodeattr_txt:
139                         if (! is_ia32_Lea(n)) {
140                                 if (is_ia32_AddrModeS(n)) {
141                                         fprintf(F, "[AM S] ");
142                                 } else if (is_ia32_AddrModeD(n)) {
143                                         fprintf(F, "[AM D] ");
144                                 }
145                         }
146
147                         break;
148
149                 case dump_node_info_txt:
150                         arch_dump_reqs_and_registers(F, n);
151
152                         /* dump op type */
153                         fprintf(F, "op = ");
154                         switch (get_ia32_op_type(n)) {
155                                 case ia32_Normal:
156                                         fprintf(F, "Normal");
157                                         break;
158                                 case ia32_AddrModeD:
159                                         fprintf(F, "AM Dest (Load+Store)");
160                                         break;
161                                 case ia32_AddrModeS:
162                                         fprintf(F, "AM Source (Load)");
163                                         break;
164                                 default:
165                                         fprintf(F, "unknown (%d)", get_ia32_op_type(n));
166                                         break;
167                         }
168                         fprintf(F, "\n");
169
170                         /* dump supported am */
171                         fprintf(F, "AM support = ");
172                         switch (get_ia32_am_support(n)) {
173                                 case ia32_am_none:   fputs("none\n",            F); break;
174                                 case ia32_am_unary:  fputs("source (unary)\n",  F); break;
175                                 case ia32_am_binary: fputs("source (binary)\n", F); break;
176
177                                 default:
178                                         fprintf(F, "unknown (%d)\n", get_ia32_am_support(n));
179                                         break;
180                         }
181
182                         /* dump AM offset */
183                         if(get_ia32_am_offs_int(n) != 0) {
184                                 fprintf(F, "AM offset = %d\n", get_ia32_am_offs_int(n));
185                         }
186
187                         /* dump AM symconst */
188                         if(get_ia32_am_sc(n) != NULL) {
189                                 ir_entity *ent = get_ia32_am_sc(n);
190                                 ident *id = get_entity_ld_ident(ent);
191                                 fprintf(F, "AM symconst = %s\n", get_id_str(id));
192                         }
193
194                         /* dump AM scale */
195                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
196
197                         /* dump pn code */
198                         if (is_ia32_SwitchJmp(n)) {
199                                 fprintf(F, "pn_code = %ld\n", get_ia32_condcode(n));
200                         } else if (is_ia32_CMovcc(n) || is_ia32_Setcc(n) || is_ia32_Jcc(n)) {
201                                 ia32_attr_t *attr = get_ia32_attr(n);
202                                 long pnc = get_ia32_condcode(n);
203                                 fprintf(F, "pn_code = 0x%lX (%s)\n", pnc, get_pnc_string(pnc & pn_Cmp_True));
204                                 fprintf(F, "ins_permuted = %u \n", attr->data.ins_permuted);
205                                 fprintf(F, "cmp_unsigned = %u \n", attr->data.cmp_unsigned);
206                         }
207                         else if (is_ia32_CopyB(n) || is_ia32_CopyB_i(n)) {
208                                 fprintf(F, "size = %u\n", get_ia32_copyb_size(n));
209                         }
210
211                         fprintf(F, "use_frame = %d\n",     is_ia32_use_frame(n));
212                         fprintf(F, "commutative = %d\n",   is_ia32_commutative(n));
213                         fprintf(F, "need stackent = %d\n", is_ia32_need_stackent(n));
214                         fprintf(F, "is reload = %d\n",     is_ia32_is_reload(n));
215                         fprintf(F, "latency = %d\n",       get_ia32_latency(n));
216
217                         /* dump frame entity */
218                         fprintf(F, "frame entity = ");
219                         if (get_ia32_frame_ent(n)) {
220                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
221                         }
222                         else {
223                                 fprintf(F, "n/a");
224                         }
225                         fprintf(F, "\n");
226
227                         /* dump modes */
228                         fprintf(F, "ls_mode = ");
229                         if (get_ia32_ls_mode(n)) {
230                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
231                         }
232                         else {
233                                 fprintf(F, "n/a");
234                         }
235                         fprintf(F, "\n");
236
237 #ifndef NDEBUG
238                         /* dump original ir node name */
239                         fprintf(F, "orig node = ");
240                         if (get_ia32_orig_node(n)) {
241                                 fprintf(F, "%s", get_ia32_orig_node(n));
242                         }
243                         else {
244                                 fprintf(F, "n/a");
245                         }
246                         fprintf(F, "\n");
247 #endif /* NDEBUG */
248
249                         break;
250         }
251
252         return bad;
253 }
254
255
256
257 /***************************************************************************************************
258  *        _   _                   _       __        _                    _   _               _
259  *       | | | |                 | |     / /       | |                  | | | |             | |
260  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
261  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
262  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
263  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
264  *                                        __/ |
265  *                                       |___/
266  ***************************************************************************************************/
267
268 ia32_attr_t *get_ia32_attr(ir_node *node) {
269         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
270         return (ia32_attr_t *)get_irn_generic_attr(node);
271 }
272
273 const ia32_attr_t *get_ia32_attr_const(const ir_node *node) {
274         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
275         return (const ia32_attr_t*) get_irn_generic_attr_const(node);
276 }
277
278 ia32_x87_attr_t *get_ia32_x87_attr(ir_node *node) {
279         ia32_attr_t     *attr     = get_ia32_attr(node);
280         ia32_x87_attr_t *x87_attr = CAST_IA32_ATTR(ia32_x87_attr_t, attr);
281         return x87_attr;
282 }
283
284 const ia32_x87_attr_t *get_ia32_x87_attr_const(const ir_node *node) {
285         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
286         const ia32_x87_attr_t *x87_attr = CONST_CAST_IA32_ATTR(ia32_x87_attr_t, attr);
287         return x87_attr;
288 }
289
290 const ia32_asm_attr_t *get_ia32_asm_attr_const(const ir_node *node) {
291         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
292         const ia32_asm_attr_t *asm_attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, attr);
293
294         return asm_attr;
295 }
296
297 ia32_immediate_attr_t *get_ia32_immediate_attr(ir_node *node) {
298         ia32_attr_t           *attr      = get_ia32_attr(node);
299         ia32_immediate_attr_t *imm_attr  = CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
300
301         return imm_attr;
302 }
303
304 const ia32_immediate_attr_t *get_ia32_immediate_attr_const(const ir_node *node)
305 {
306         const ia32_attr_t           *attr     = get_ia32_attr_const(node);
307         const ia32_immediate_attr_t *imm_attr = CONST_CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
308
309         return imm_attr;
310 }
311
312 ia32_condcode_attr_t *get_ia32_condcode_attr(ir_node *node) {
313         ia32_attr_t          *attr    = get_ia32_attr(node);
314         ia32_condcode_attr_t *cc_attr = CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
315
316         return cc_attr;
317 }
318
319 const ia32_condcode_attr_t *get_ia32_condcode_attr_const(const ir_node *node) {
320         const ia32_attr_t          *attr    = get_ia32_attr_const(node);
321         const ia32_condcode_attr_t *cc_attr = CONST_CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
322
323         return cc_attr;
324 }
325
326 ia32_call_attr_t *get_ia32_call_attr(ir_node *node)
327 {
328         ia32_attr_t      *attr      = get_ia32_attr(node);
329         ia32_call_attr_t *call_attr = CAST_IA32_ATTR(ia32_call_attr_t, attr);
330
331         return call_attr;
332 }
333
334 const ia32_call_attr_t *get_ia32_call_attr_const(const ir_node *node)
335 {
336         const ia32_attr_t      *attr      = get_ia32_attr_const(node);
337         const ia32_call_attr_t *call_attr = CONST_CAST_IA32_ATTR(ia32_call_attr_t, attr);
338
339         return call_attr;
340 }
341
342 ia32_copyb_attr_t *get_ia32_copyb_attr(ir_node *node) {
343         ia32_attr_t       *attr       = get_ia32_attr(node);
344         ia32_copyb_attr_t *copyb_attr = CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
345
346         return copyb_attr;
347 }
348
349 const ia32_copyb_attr_t *get_ia32_copyb_attr_const(const ir_node *node) {
350         const ia32_attr_t       *attr       = get_ia32_attr_const(node);
351         const ia32_copyb_attr_t *copyb_attr = CONST_CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
352
353         return copyb_attr;
354 }
355
356 ia32_climbframe_attr_t *get_ia32_climbframe_attr(ir_node *node) {
357         ia32_attr_t            *attr            = get_ia32_attr(node);
358         ia32_climbframe_attr_t *climbframe_attr = CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
359
360         return climbframe_attr;
361 }
362
363 const ia32_climbframe_attr_t *get_ia32_climbframe_attr_const(const ir_node *node) {
364         const ia32_attr_t            *attr            = get_ia32_attr_const(node);
365         const ia32_climbframe_attr_t *climbframe_attr = CONST_CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
366
367         return climbframe_attr;
368 }
369
370 /**
371  * Gets the type of an ia32 node.
372  */
373 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
374         const ia32_attr_t *attr = get_ia32_attr_const(node);
375         return attr->data.tp;
376 }
377
378 /**
379  * Sets the type of an ia32 node.
380  */
381 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
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 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         const ia32_attr_t *attr = get_ia32_attr_const(node);
406         return attr->am_offs;
407 }
408
409 /**
410  * Sets the address mode offset from an int.
411  */
412 void set_ia32_am_offs_int(ir_node *node, int offset) {
413         ia32_attr_t *attr = get_ia32_attr(node);
414         attr->am_offs = offset;
415 }
416
417 void add_ia32_am_offs_int(ir_node *node, int offset) {
418         ia32_attr_t *attr = get_ia32_attr(node);
419         attr->am_offs += offset;
420 }
421
422 /**
423  * Returns the symconst entity associated to address mode.
424  */
425 ir_entity *get_ia32_am_sc(const ir_node *node) {
426         const ia32_attr_t *attr = get_ia32_attr_const(node);
427         return attr->am_sc;
428 }
429
430 /**
431  * Sets the symconst entity associated to address mode.
432  */
433 void set_ia32_am_sc(ir_node *node, ir_entity *entity) {
434         ia32_attr_t *attr = get_ia32_attr(node);
435         attr->am_sc       = entity;
436 }
437
438 /**
439  * Sets the sign bit for address mode symconst.
440  */
441 void set_ia32_am_sc_sign(ir_node *node) {
442         ia32_attr_t *attr     = get_ia32_attr(node);
443         attr->data.am_sc_sign = 1;
444 }
445
446 /**
447  * Clears the sign bit for address mode symconst.
448  */
449 void clear_ia32_am_sc_sign(ir_node *node) {
450         ia32_attr_t *attr     = get_ia32_attr(node);
451         attr->data.am_sc_sign = 0;
452 }
453
454 /**
455  * Returns the sign bit for address mode symconst.
456  */
457 int is_ia32_am_sc_sign(const ir_node *node) {
458         const ia32_attr_t *attr = get_ia32_attr_const(node);
459         return attr->data.am_sc_sign;
460 }
461
462 /**
463  * Gets the addr mode const.
464  */
465 unsigned get_ia32_am_scale(const ir_node *node) {
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         ia32_attr_t *attr = get_ia32_attr(node);
475         assert(scale <= 3 && "AM scale out of range [0 ... 3]");
476         attr->data.am_scale = scale;
477 }
478
479 void ia32_copy_am_attrs(ir_node *to, const ir_node *from)
480 {
481         set_ia32_ls_mode(to, get_ia32_ls_mode(from));
482         set_ia32_am_scale(to, get_ia32_am_scale(from));
483         set_ia32_am_sc(to, get_ia32_am_sc(from));
484         if(is_ia32_am_sc_sign(from))
485                 set_ia32_am_sc_sign(to);
486         add_ia32_am_offs_int(to, get_ia32_am_offs_int(from));
487         set_ia32_frame_ent(to, get_ia32_frame_ent(from));
488         if (is_ia32_use_frame(from))
489                 set_ia32_use_frame(to);
490 }
491
492 /**
493  * Sets the uses_frame flag.
494  */
495 void set_ia32_use_frame(ir_node *node) {
496         ia32_attr_t *attr    = get_ia32_attr(node);
497         attr->data.use_frame = 1;
498 }
499
500 /**
501  * Clears the uses_frame flag.
502  */
503 void clear_ia32_use_frame(ir_node *node) {
504         ia32_attr_t *attr    = get_ia32_attr(node);
505         attr->data.use_frame = 0;
506 }
507
508 /**
509  * Gets the uses_frame flag.
510  */
511 int is_ia32_use_frame(const ir_node *node) {
512         const ia32_attr_t *attr = get_ia32_attr_const(node);
513         return attr->data.use_frame;
514 }
515
516 /**
517  * Sets node to commutative.
518  */
519 void set_ia32_commutative(ir_node *node) {
520         ia32_attr_t *attr         = get_ia32_attr(node);
521         attr->data.is_commutative = 1;
522 }
523
524 /**
525  * Sets node to non-commutative.
526  */
527 void clear_ia32_commutative(ir_node *node) {
528         ia32_attr_t *attr         = get_ia32_attr(node);
529         attr->data.is_commutative = 0;
530 }
531
532 /**
533  * Checks if node is commutative.
534  */
535 int is_ia32_commutative(const ir_node *node) {
536         const ia32_attr_t *attr = get_ia32_attr_const(node);
537         return attr->data.is_commutative;
538 }
539
540 void set_ia32_need_stackent(ir_node *node) {
541         ia32_attr_t *attr     = get_ia32_attr(node);
542         attr->data.need_stackent = 1;
543 }
544
545 void clear_ia32_need_stackent(ir_node *node) {
546         ia32_attr_t *attr     = get_ia32_attr(node);
547         attr->data.need_stackent = 0;
548 }
549
550 int is_ia32_need_stackent(const ir_node *node) {
551         const ia32_attr_t *attr = get_ia32_attr_const(node);
552         return attr->data.need_stackent;
553 }
554
555 void set_ia32_is_reload(ir_node *node) {
556         ia32_attr_t *attr = get_ia32_attr(node);
557         attr->data.is_reload = 1;
558 }
559
560 int is_ia32_is_reload(const ir_node *node) {
561         const ia32_attr_t *attr = get_ia32_attr_const(node);
562         return attr->data.is_reload;
563 }
564
565 void set_ia32_is_spill(ir_node *node) {
566         ia32_attr_t *attr = get_ia32_attr(node);
567         attr->data.is_spill = 1;
568 }
569
570 int is_ia32_is_spill(const ir_node *node) {
571         const ia32_attr_t *attr = get_ia32_attr_const(node);
572         return attr->data.is_spill;
573 }
574
575 void set_ia32_is_remat(ir_node *node) {
576         ia32_attr_t *attr = get_ia32_attr(node);
577         attr->data.is_remat = 1;
578 }
579
580 int is_ia32_is_remat(const ir_node *node) {
581         const ia32_attr_t *attr = get_ia32_attr_const(node);
582         return attr->data.is_remat;
583 }
584
585 /**
586  * Gets the mode of the stored/loaded value (only set for Store/Load)
587  */
588 ir_mode *get_ia32_ls_mode(const ir_node *node) {
589         const ia32_attr_t *attr = get_ia32_attr_const(node);
590         return attr->ls_mode;
591 }
592
593 /**
594  * Sets the mode of the stored/loaded value (only set for Store/Load)
595  */
596 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
597         ia32_attr_t *attr = get_ia32_attr(node);
598         attr->ls_mode     = mode;
599 }
600
601 /**
602  * Gets the frame entity assigned to this node.
603  */
604 ir_entity *get_ia32_frame_ent(const ir_node *node) {
605         const ia32_attr_t *attr = get_ia32_attr_const(node);
606         return attr->frame_ent;
607 }
608
609 /**
610  * Sets the frame entity for this node.
611  */
612 void set_ia32_frame_ent(ir_node *node, ir_entity *ent) {
613         ia32_attr_t *attr = get_ia32_attr(node);
614         attr->frame_ent   = ent;
615         if(ent != NULL)
616                 set_ia32_use_frame(node);
617         else
618                 clear_ia32_use_frame(node);
619 }
620
621
622 /**
623  * Gets the instruction latency.
624  */
625 unsigned get_ia32_latency(const ir_node *node) {
626         const ir_op *op               = get_irn_op(node);
627         const ia32_op_attr_t *op_attr = (ia32_op_attr_t*) get_op_attr(op);
628         return op_attr->latency;
629 }
630
631 /**
632  * Returns the argument register requirements of an ia32 node.
633  */
634 const arch_register_req_t **get_ia32_in_req_all(const ir_node *node) {
635         const ia32_attr_t *attr = get_ia32_attr_const(node);
636         return attr->in_req;
637 }
638
639 /**
640  * Sets the argument register requirements of an ia32 node.
641  */
642 void set_ia32_in_req_all(ir_node *node, const arch_register_req_t **reqs) {
643         ia32_attr_t *attr = get_ia32_attr(node);
644         attr->in_req      = reqs;
645 }
646
647 /**
648  * Returns the argument register requirement at position pos of an ia32 node.
649  */
650 const arch_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
651         const ia32_attr_t *attr = get_ia32_attr_const(node);
652         if(attr->in_req == NULL)
653                 return arch_no_register_req;
654
655         return attr->in_req[pos];
656 }
657
658 /**
659  * Sets the IN register requirements at position pos.
660  */
661 void set_ia32_req_in(ir_node *node, const arch_register_req_t *req, int pos)
662 {
663         ia32_attr_t *attr = get_ia32_attr(node);
664         attr->in_req[pos] = req;
665 }
666
667 /**
668  * Returns the condition code of a node.
669  */
670 long get_ia32_condcode(const ir_node *node)
671 {
672         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
673         return attr->pn_code;
674 }
675
676 /**
677  * Sets the condition code of a node
678  */
679 void set_ia32_condcode(ir_node *node, long code)
680 {
681         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
682         attr->pn_code = code;
683 }
684
685 /**
686  * Returns the condition code of a node.
687  */
688 unsigned get_ia32_copyb_size(const ir_node *node)
689 {
690         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
691         return attr->size;
692 }
693
694 /**
695  * Get the list of available execution units.
696  */
697 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node)
698 {
699         const ia32_attr_t *attr = get_ia32_attr_const(node);
700         return attr->exec_units;
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         struct obstack *obst = env_cg->isa->name_obst;
757
758         lc_eoprintf(firm_get_arg_env(), obst, "%+F", irn);
759         obstack_1grow(obst, 0);
760         return obstack_finish(obst);
761 }
762
763 /**
764  * Sets the name of the original ir node.
765  */
766 void set_ia32_orig_node(ir_node *node, const ir_node *old)
767 {
768         const char  *name = ia32_get_old_node_name(old);
769         ia32_attr_t *attr = get_ia32_attr(node);
770         attr->orig_node   = name;
771 }
772
773 #endif /* NDEBUG */
774
775 /******************************************************************************************************
776  *                      _       _         _   _           __                  _   _
777  *                     (_)     | |       | | | |         / _|                | | (_)
778  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
779  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
780  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
781  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
782  *     | |
783  *     |_|
784  ******************************************************************************************************/
785
786 /**
787  * Returns whether or not the node is an AddrModeS node.
788  */
789 int is_ia32_AddrModeS(const ir_node *node) {
790         const ia32_attr_t *attr = get_ia32_attr_const(node);
791         return (attr->data.tp == ia32_AddrModeS);
792 }
793
794 /**
795  * Returns whether or not the node is an AddrModeD node.
796  */
797 int is_ia32_AddrModeD(const ir_node *node) {
798         const ia32_attr_t *attr = get_ia32_attr_const(node);
799         return (attr->data.tp == ia32_AddrModeD);
800 }
801
802 void ia32_swap_left_right(ir_node *node)
803 {
804         ia32_attr_t *attr  = get_ia32_attr(node);
805         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
806         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
807
808         assert(is_ia32_commutative(node));
809         attr->data.ins_permuted = !attr->data.ins_permuted;
810         set_irn_n(node, n_ia32_binary_left,  right);
811         set_irn_n(node, n_ia32_binary_right, left);
812 }
813
814 /**
815  * Initializes the nodes attributes.
816  */
817 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
818                           const arch_register_req_t **in_reqs,
819                           const be_execution_unit_t ***execution_units,
820                           int n_res)
821 {
822         ir_graph        *irg  = get_irn_irg(node);
823         struct obstack  *obst = get_irg_obstack(irg);
824         ia32_attr_t     *attr = get_ia32_attr(node);
825         backend_info_t  *info;
826
827         arch_irn_set_flags(node, flags);
828         set_ia32_in_req_all(node, in_reqs);
829
830         attr->exec_units  = execution_units;
831 #ifndef NDEBUG
832         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
833 #endif
834
835         info            = be_get_info(node);
836         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
837         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
838 }
839
840 void
841 init_ia32_x87_attributes(ir_node *res)
842 {
843 #ifndef NDEBUG
844         ia32_attr_t *attr  = get_ia32_attr(res);
845         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
846 #else
847         (void) res;
848 #endif
849         ia32_current_cg->do_x87_sim = 1;
850 }
851
852 void
853 init_ia32_asm_attributes(ir_node *res)
854 {
855 #ifndef NDEBUG
856         ia32_attr_t *attr  = get_ia32_attr(res);
857         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
858 #else
859         (void) res;
860 #endif
861 }
862
863 void
864 init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
865                                int symconst_sign, int no_pic_adjust,
866                                                            long offset)
867 {
868         ia32_immediate_attr_t *attr = get_irn_generic_attr(res);
869
870 #ifndef NDEBUG
871         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
872 #endif
873         attr->symconst      = symconst;
874         attr->sc_sign       = symconst_sign;
875         attr->no_pic_adjust = no_pic_adjust;
876         attr->offset        = offset;
877 }
878
879 void init_ia32_call_attributes(ir_node* res, unsigned pop, ir_type* call_tp)
880 {
881         ia32_call_attr_t *attr = get_irn_generic_attr(res);
882
883 #ifndef NDEBUG
884         attr->attr.attr_type  |= IA32_ATTR_ia32_call_attr_t;
885 #endif
886         attr->pop     = pop;
887         attr->call_tp = call_tp;
888 }
889
890 void
891 init_ia32_copyb_attributes(ir_node *res, unsigned size) {
892         ia32_copyb_attr_t *attr = get_irn_generic_attr(res);
893
894 #ifndef NDEBUG
895         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
896 #endif
897         attr->size = size;
898 }
899
900 void
901 init_ia32_condcode_attributes(ir_node *res, long pnc) {
902         ia32_condcode_attr_t *attr = get_irn_generic_attr(res);
903
904 #ifndef NDEBUG
905         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
906 #endif
907         attr->pn_code = pnc;
908 }
909
910 void
911 init_ia32_climbframe_attributes(ir_node *res, unsigned count) {
912         ia32_climbframe_attr_t *attr = get_irn_generic_attr(res);
913
914 #ifndef NDEBUG
915         attr->attr.attr_type  |= IA32_ATTR_ia32_climbframe_attr_t;
916 #endif
917         attr->count = count;
918 }
919
920 /***************************************************************************************
921  *                  _                            _                   _
922  *                 | |                          | |                 | |
923  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
924  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
925  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
926  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
927  *
928  ***************************************************************************************/
929
930 /* default compare operation to compare attributes */
931 int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b)
932 {
933         if (a->data.tp != b->data.tp)
934                 return 1;
935
936         if (a->data.am_scale != b->data.am_scale
937             || a->data.am_sc_sign != b->data.am_sc_sign
938             || a->am_offs != b->am_offs
939             || a->am_sc != b->am_sc
940                 || a->data.am_sc_no_pic_adjust != b->data.am_sc_no_pic_adjust
941             || a->ls_mode != b->ls_mode)
942                 return 1;
943
944         /* nodes with not yet assigned entities shouldn't be CSEd (important for
945          * unsigned int -> double conversions */
946         if(a->data.use_frame && a->frame_ent == NULL)
947                 return 1;
948         if(b->data.use_frame && b->frame_ent == NULL)
949                 return 1;
950
951         if (a->data.use_frame != b->data.use_frame
952             || a->frame_ent != b->frame_ent)
953                 return 1;
954
955         if (a->data.has_except_label != b->data.has_except_label)
956                 return 1;
957
958         if (a->data.ins_permuted != b->data.ins_permuted
959                         || a->data.cmp_unsigned != b->data.cmp_unsigned)
960                 return 1;
961
962         return 0;
963 }
964
965 /** Compare nodes attributes for all "normal" nodes. */
966 static
967 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
977 int ia32_compare_condcode_attr(ir_node *a, ir_node *b)
978 {
979         const ia32_condcode_attr_t *attr_a;
980         const ia32_condcode_attr_t *attr_b;
981
982         if (ia32_compare_nodes_attr(a, b))
983                 return 1;
984
985         attr_a = get_ia32_condcode_attr_const(a);
986         attr_b = get_ia32_condcode_attr_const(b);
987
988         if (attr_a->pn_code != attr_b->pn_code)
989                 return 1;
990
991         return 0;
992 }
993
994 /** Compare node attributes for call nodes. */
995 static int ia32_compare_call_attr(ir_node *a, ir_node *b)
996 {
997         const ia32_call_attr_t *attr_a;
998         const ia32_call_attr_t *attr_b;
999
1000         if (ia32_compare_nodes_attr(a, b))
1001                 return 1;
1002
1003         attr_a = get_ia32_call_attr_const(a);
1004         attr_b = get_ia32_call_attr_const(b);
1005
1006         if (attr_a->pop != attr_b->pop)
1007                 return 1;
1008
1009         if (attr_a->call_tp != attr_b->call_tp)
1010                 return 1;
1011
1012         return 0;
1013 }
1014
1015 /** Compare node attributes for CopyB nodes. */
1016 static
1017 int ia32_compare_copyb_attr(ir_node *a, ir_node *b)
1018 {
1019         const ia32_copyb_attr_t *attr_a;
1020         const ia32_copyb_attr_t *attr_b;
1021
1022         if (ia32_compare_nodes_attr(a, b))
1023                 return 1;
1024
1025         attr_a = get_ia32_copyb_attr_const(a);
1026         attr_b = get_ia32_copyb_attr_const(b);
1027
1028         if (attr_a->size != attr_b->size)
1029                 return 1;
1030
1031         return 0;
1032 }
1033
1034
1035 /** Compare ASM node attributes. */
1036 static
1037 int ia32_compare_asm_attr(ir_node *a, ir_node *b)
1038 {
1039         const ia32_asm_attr_t *attr_a;
1040         const ia32_asm_attr_t *attr_b;
1041
1042         if (ia32_compare_nodes_attr(a, b))
1043                 return 1;
1044
1045         attr_a = get_ia32_asm_attr_const(a);
1046         attr_b = get_ia32_asm_attr_const(b);
1047
1048         if(attr_a->asm_text != attr_b->asm_text)
1049                 return 1;
1050
1051         return 0;
1052 }
1053
1054 /**
1055  * Hash function for Immediates
1056  */
1057 static unsigned ia32_hash_Immediate(const ir_node *irn) {
1058         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1059
1060         return HASH_PTR(a->symconst) + (a->sc_sign << 16) + a->offset;
1061 }
1062
1063 /** Compare node attributes for Immediates. */
1064 static
1065 int ia32_compare_immediate_attr(ir_node *a, ir_node *b)
1066 {
1067         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1068         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1069
1070         if (attr_a->symconst != attr_b->symconst
1071                 || attr_a->sc_sign != attr_b->sc_sign
1072                 || attr_a->no_pic_adjust != attr_b->no_pic_adjust
1073                 || attr_a->offset != attr_b->offset) {
1074                 return 1;
1075         }
1076
1077         return 0;
1078 }
1079
1080 /** Compare node attributes for x87 nodes. */
1081 static
1082 int ia32_compare_x87_attr(ir_node *a, ir_node *b)
1083 {
1084         return ia32_compare_nodes_attr(a, b);
1085 }
1086
1087 /** Compare node attributes for ClimbFrame nodes. */
1088 static
1089 int ia32_compare_climbframe_attr(ir_node *a, ir_node *b)
1090 {
1091         const ia32_climbframe_attr_t *attr_a;
1092         const ia32_climbframe_attr_t *attr_b;
1093
1094         if (ia32_compare_nodes_attr(a, b))
1095                 return 1;
1096
1097         attr_a = get_ia32_climbframe_attr_const(a);
1098         attr_b = get_ia32_climbframe_attr_const(b);
1099
1100         if (attr_a->count != attr_b->count)
1101                 return 1;
1102
1103         return 0;
1104 }
1105
1106 /* copies the ia32 attributes */
1107 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node)
1108 {
1109         ir_graph          *irg      = get_irn_irg(new_node);
1110         struct obstack    *obst     = get_irg_obstack(irg);
1111         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1112         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1113         backend_info_t    *old_info = be_get_info(old_node);
1114         backend_info_t    *new_info = be_get_info(new_node);
1115
1116         /* copy the attributes */
1117         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1118
1119         /* copy out flags */
1120         new_info->out_infos =
1121                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
1122 }
1123
1124 /* Include the generated constructor functions */
1125 #include "gen_ia32_new_nodes.c.inl"