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