d741aef3fe86dc1e95bcf1d24bd4a38480bddc26
[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 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdlib.h>
34
35 #include "irprog_t.h"
36 #include "irgraph_t.h"
37 #include "irnode_t.h"
38 #include "irmode_t.h"
39 #include "ircons_t.h"
40 #include "iropt_t.h"
41 #include "irop.h"
42 #include "firm_common_t.h"
43 #include "irvrfy_t.h"
44 #include "irprintf.h"
45 #include "iredges.h"
46 #include "error.h"
47 #include "raw_bitset.h"
48 #include "xmalloc.h"
49
50 #include "../bearch_t.h"
51
52 #include "bearch_ia32_t.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  * Dumps the register requirements for either in or out.
71  */
72 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
73                          int inout) {
74         char *dir = inout ? "out" : "in";
75         int   max = inout ? get_ia32_n_res(n) : get_irn_arity(n);
76         char  buf[1024];
77         int   i;
78
79         memset(buf, 0, sizeof(buf));
80
81         if (reqs) {
82                 for (i = 0; i < max; i++) {
83                         fprintf(F, "%sreq #%d =", dir, i);
84
85                         if (reqs[i]->type == arch_register_req_type_none) {
86                                 fprintf(F, " n/a");
87                         }
88
89                         if (reqs[i]->type & arch_register_req_type_normal) {
90                                 fprintf(F, " %s", reqs[i]->cls->name);
91                         }
92
93                         if (reqs[i]->type & arch_register_req_type_limited) {
94                                 fprintf(F, " %s",
95                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
96                         }
97
98                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
99                                 unsigned other = reqs[i]->other_same;
100                                 int i;
101
102                                 ir_fprintf(F, " same as");
103                                 for (i = 0; 1U << i <= other; ++i) {
104                                         if (other & (1U << i)) {
105                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
106                                         }
107                                 }
108                         }
109
110                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
111                                 unsigned other = reqs[i]->other_different;
112                                 int i;
113
114                                 ir_fprintf(F, " different from");
115                                 for (i = 0; 1U << i <= other; ++i) {
116                                         if (other & (1U << i)) {
117                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
118                                         }
119                                 }
120                         }
121
122                         fprintf(F, "\n");
123                 }
124
125                 fprintf(F, "\n");
126         }
127         else {
128                 fprintf(F, "%sreq = N/A\n", dir);
129         }
130 }
131
132 /**
133  * Dumper interface for dumping ia32 nodes in vcg.
134  * @param n        the node to dump
135  * @param F        the output file
136  * @param reason   indicates which kind of information should be dumped
137  * @return 0 on success or != 0 on failure
138  */
139 static int ia32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
140         ir_mode     *mode = NULL;
141         int          bad  = 0;
142         int          i, n_res, flags;
143         const arch_register_req_t **reqs;
144         const arch_register_t     **slots;
145
146         switch (reason) {
147                 case dump_node_opcode_txt:
148                         fprintf(F, "%s", get_irn_opname(n));
149
150                         if(is_ia32_Immediate(n) || is_ia32_Const(n)) {
151                                 const ia32_immediate_attr_t *attr
152                                         = get_ia32_immediate_attr_const(n);
153
154                                 fputc(' ', F);
155                                 if(attr->symconst) {
156                                         if(attr->sc_sign) {
157                                                 fputc('-', F);
158                                         }
159                                         fputs(get_entity_name(attr->symconst), F);
160                                 }
161                                 if(attr->offset != 0 || attr->symconst == NULL) {
162                                         if(attr->offset > 0 && attr->symconst != NULL) {
163                                                 fputc('+', F);
164                                         }
165                                         fprintf(F, "%ld", attr->offset);
166                                 }
167                         }
168                         else {
169                                 const ia32_attr_t *attr = get_ia32_attr_const(n);
170
171                                 if(attr->am_sc != NULL || attr->am_offs != 0)
172                                         fputs(" [", F);
173
174                                 if(attr->am_sc != NULL) {
175                                         if(attr->data.am_sc_sign) {
176                                                 fputc('-', F);
177                                         }
178                                         fputs(get_entity_name(attr->am_sc), F);
179                                 }
180                                 if(attr->am_offs != 0) {
181                                         if(attr->am_offs > 0 && attr->am_sc != NULL) {
182                                                 fputc('+', F);
183                                         }
184                                         fprintf(F, "%d", attr->am_offs);
185                                 }
186
187                                 if(attr->am_sc != NULL || attr->am_offs != 0)
188                                         fputc(']', F);
189                         }
190                         break;
191
192                 case dump_node_mode_txt:
193                         if (is_ia32_Ld(n) || is_ia32_St(n)) {
194                                 mode = get_ia32_ls_mode(n);
195                                 fprintf(F, "[%s]", mode ? get_mode_name(mode) : "?NOMODE?");
196                         }
197                         break;
198
199                 case dump_node_nodeattr_txt:
200                         if (! is_ia32_Lea(n)) {
201                                 if (is_ia32_AddrModeS(n)) {
202                                         fprintf(F, "[AM S] ");
203                                 } else if (is_ia32_AddrModeD(n)) {
204                                         fprintf(F, "[AM D] ");
205                                 }
206                         }
207
208                         break;
209
210                 case dump_node_info_txt:
211                         n_res = get_ia32_n_res(n);
212                         fprintf(F, "=== IA32 attr begin ===\n");
213
214                         /* dump IN requirements */
215                         if (get_irn_arity(n) > 0) {
216                                 reqs = get_ia32_in_req_all(n);
217                                 dump_reg_req(F, n, reqs, 0);
218                         }
219
220                         /* dump OUT requirements */
221                         if (n_res > 0) {
222                                 reqs = get_ia32_out_req_all(n);
223                                 dump_reg_req(F, n, reqs, 1);
224                         }
225
226                         /* dump assigned registers */
227                         slots = get_ia32_slots(n);
228                         if (slots && n_res > 0) {
229                                 for (i = 0; i < n_res; i++) {
230                                         const arch_register_t *reg;
231
232                                         reg = slots[i];
233
234                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
235                                 }
236                                 fprintf(F, "\n");
237                         }
238
239                         /* dump op type */
240                         fprintf(F, "op = ");
241                         switch (get_ia32_op_type(n)) {
242                                 case ia32_Normal:
243                                         fprintf(F, "Normal");
244                                         break;
245                                 case ia32_AddrModeD:
246                                         fprintf(F, "AM Dest (Load+Store)");
247                                         break;
248                                 case ia32_AddrModeS:
249                                         fprintf(F, "AM Source (Load)");
250                                         break;
251                                 default:
252                                         fprintf(F, "unknown (%d)", get_ia32_op_type(n));
253                                         break;
254                         }
255                         fprintf(F, "\n");
256
257                         /* dump supported am */
258                         fprintf(F, "AM support = ");
259                         switch (get_ia32_am_support(n)) {
260                                 case ia32_am_None:
261                                         fprintf(F, "none");
262                                         break;
263                                 case ia32_am_Source:
264                                         fprintf(F, "source only (Load)");
265                                         break;
266                                 default:
267                                         fprintf(F, "unknown (%d)", get_ia32_am_support(n));
268                                         break;
269                         }
270                         fprintf(F, "\n");
271
272                         /* dump AM offset */
273                         if(get_ia32_am_offs_int(n) != 0) {
274                                 fprintf(F, "AM offset = %d\n", get_ia32_am_offs_int(n));
275                         }
276
277                         /* dump AM symconst */
278                         if(get_ia32_am_sc(n) != NULL) {
279                                 ir_entity *ent = get_ia32_am_sc(n);
280                                 ident *id = get_entity_ld_ident(ent);
281                                 fprintf(F, "AM symconst = %s\n", get_id_str(id));
282                         }
283
284                         /* dump AM scale */
285                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
286
287                         /* dump pn code */
288                         if (is_ia32_SwitchJmp(n)) {
289                                 fprintf(F, "pn_code = %ld\n", get_ia32_condcode(n));
290                         } else if (is_ia32_CMov(n) || is_ia32_Set(n) || is_ia32_Jcc(n)) {
291                                 long pnc = get_ia32_condcode(n);
292                                 fprintf(F, "pn_code = 0x%X (%s)\n", pnc, get_pnc_string(pnc & pn_Cmp_True));
293                         }
294                         else if (is_ia32_CopyB(n) || is_ia32_CopyB_i(n)) {
295                                 fprintf(F, "size = %u\n", get_ia32_copyb_size(n));
296                         }
297
298                         /* dump n_res */
299                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
300
301                         /* dump use_frame */
302                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
303
304                         /* commutative */
305                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
306
307                         /* need stackent */
308                         fprintf(F, "need stackent = %d\n", is_ia32_need_stackent(n));
309
310                         /* dump latency */
311                         fprintf(F, "latency = %d\n", get_ia32_latency(n));
312
313                         /* dump flags */
314                         fprintf(F, "flags =");
315                         flags = get_ia32_flags(n);
316                         if (flags == arch_irn_flags_none) {
317                                 fprintf(F, " none");
318                         }
319                         else {
320                                 if (flags & arch_irn_flags_dont_spill) {
321                                         fprintf(F, " unspillable");
322                                 }
323                                 if (flags & arch_irn_flags_rematerializable) {
324                                         fprintf(F, " remat");
325                                 }
326                                 if (flags & arch_irn_flags_ignore) {
327                                         fprintf(F, " ignore");
328                                 }
329                                 if (flags & arch_irn_flags_modify_sp) {
330                                         fprintf(F, " modify_sp");
331                                 }
332                         }
333                         fprintf(F, " (%d)\n", flags);
334
335                         /* dump frame entity */
336                         fprintf(F, "frame entity = ");
337                         if (get_ia32_frame_ent(n)) {
338                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
339                         }
340                         else {
341                                 fprintf(F, "n/a");
342                         }
343                         fprintf(F, "\n");
344
345                         /* dump modes */
346                         fprintf(F, "ls_mode = ");
347                         if (get_ia32_ls_mode(n)) {
348                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
349                         }
350                         else {
351                                 fprintf(F, "n/a");
352                         }
353                         fprintf(F, "\n");
354
355 #ifndef NDEBUG
356                         /* dump original ir node name */
357                         fprintf(F, "orig node = ");
358                         if (get_ia32_orig_node(n)) {
359                                 fprintf(F, "%s", get_ia32_orig_node(n));
360                         }
361                         else {
362                                 fprintf(F, "n/a");
363                         }
364                         fprintf(F, "\n");
365 #endif /* NDEBUG */
366
367                         fprintf(F, "=== IA32 attr end ===\n");
368                         /* end of: case dump_node_info_txt */
369                         break;
370         }
371
372         return bad;
373 }
374
375
376
377 /***************************************************************************************************
378  *        _   _                   _       __        _                    _   _               _
379  *       | | | |                 | |     / /       | |                  | | | |             | |
380  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
381  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
382  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
383  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
384  *                                        __/ |
385  *                                       |___/
386  ***************************************************************************************************/
387
388 ia32_attr_t *get_ia32_attr(ir_node *node) {
389         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
390         return (ia32_attr_t *)get_irn_generic_attr(node);
391 }
392
393 const ia32_attr_t *get_ia32_attr_const(const ir_node *node) {
394         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
395         return (const ia32_attr_t*) get_irn_generic_attr_const(node);
396 }
397
398 ia32_x87_attr_t *get_ia32_x87_attr(ir_node *node) {
399         ia32_attr_t     *attr     = get_ia32_attr(node);
400         ia32_x87_attr_t *x87_attr = CAST_IA32_ATTR(ia32_x87_attr_t, attr);
401         return x87_attr;
402 }
403
404 const ia32_x87_attr_t *get_ia32_x87_attr_const(const ir_node *node) {
405         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
406         const ia32_x87_attr_t *x87_attr = CONST_CAST_IA32_ATTR(ia32_x87_attr_t, attr);
407         return x87_attr;
408 }
409
410 const ia32_asm_attr_t *get_ia32_asm_attr_const(const ir_node *node) {
411         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
412         const ia32_asm_attr_t *asm_attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, attr);
413
414         return asm_attr;
415 }
416
417 ia32_immediate_attr_t *get_ia32_immediate_attr(ir_node *node) {
418         ia32_attr_t           *attr      = get_ia32_attr(node);
419         ia32_immediate_attr_t *imm_attr  = CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
420
421         return imm_attr;
422 }
423
424 const ia32_immediate_attr_t *get_ia32_immediate_attr_const(const ir_node *node)
425 {
426         const ia32_attr_t           *attr     = get_ia32_attr_const(node);
427         const ia32_immediate_attr_t *imm_attr = CONST_CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
428
429         return imm_attr;
430 }
431
432 ia32_condcode_attr_t *get_ia32_condcode_attr(ir_node *node) {
433         ia32_attr_t          *attr    = get_ia32_attr(node);
434         ia32_condcode_attr_t *cc_attr = CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
435
436         return cc_attr;
437 }
438
439 const ia32_condcode_attr_t *get_ia32_condcode_attr_const(const ir_node *node) {
440         const ia32_attr_t          *attr    = get_ia32_attr_const(node);
441         const ia32_condcode_attr_t *cc_attr = CONST_CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
442
443         return cc_attr;
444 }
445
446 ia32_copyb_attr_t *get_ia32_copyb_attr(ir_node *node) {
447         ia32_attr_t       *attr       = get_ia32_attr(node);
448         ia32_copyb_attr_t *copyb_attr = CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
449
450         return copyb_attr;
451 }
452
453 const ia32_copyb_attr_t *get_ia32_copyb_attr_const(const ir_node *node) {
454         const ia32_attr_t       *attr       = get_ia32_attr_const(node);
455         const ia32_copyb_attr_t *copyb_attr = CONST_CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
456
457         return copyb_attr;
458 }
459
460 /**
461  * Gets the type of an ia32 node.
462  */
463 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
464         const ia32_attr_t *attr = get_ia32_attr_const(node);
465         return attr->data.tp;
466 }
467
468 /**
469  * Sets the type of an ia32 node.
470  */
471 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
472         ia32_attr_t *attr = get_ia32_attr(node);
473         attr->data.tp     = tp;
474 }
475
476 /**
477  * Gets the supported address mode of an ia32 node
478  */
479 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
480         const ia32_attr_t *attr = get_ia32_attr_const(node);
481         return attr->data.am_support;
482 }
483
484 ia32_am_arity_t get_ia32_am_arity(const ir_node *node) {
485         const ia32_attr_t *attr = get_ia32_attr_const(node);
486         return attr->data.am_arity;
487 }
488
489 /**
490  * Sets the supported address mode of an ia32 node
491  */
492 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp,
493                          ia32_am_arity_t arity) {
494         ia32_attr_t *attr     = get_ia32_attr(node);
495         attr->data.am_support = am_tp;
496         attr->data.am_arity   = arity;
497
498         assert((am_tp == ia32_am_None && arity == ia32_am_arity_none) ||
499                (am_tp != ia32_am_None &&
500                ((arity == ia32_am_unary) || (arity == ia32_am_binary) || (arity == ia32_am_ternary))));
501 }
502
503 /**
504  * Gets the address mode offset as int.
505  */
506 int get_ia32_am_offs_int(const ir_node *node) {
507         const ia32_attr_t *attr = get_ia32_attr_const(node);
508         return attr->am_offs;
509 }
510
511 /**
512  * Sets the address mode offset from an int.
513  */
514 void set_ia32_am_offs_int(ir_node *node, int offset) {
515         ia32_attr_t *attr = get_ia32_attr(node);
516         attr->am_offs = offset;
517 }
518
519 void add_ia32_am_offs_int(ir_node *node, int offset) {
520         ia32_attr_t *attr = get_ia32_attr(node);
521         attr->am_offs += offset;
522 }
523
524 /**
525  * Returns the symconst entity associated to address mode.
526  */
527 ir_entity *get_ia32_am_sc(const ir_node *node) {
528         const ia32_attr_t *attr = get_ia32_attr_const(node);
529         return attr->am_sc;
530 }
531
532 /**
533  * Sets the symconst entity associated to address mode.
534  */
535 void set_ia32_am_sc(ir_node *node, ir_entity *entity) {
536         ia32_attr_t *attr = get_ia32_attr(node);
537         attr->am_sc       = entity;
538 }
539
540 /**
541  * Sets the sign bit for address mode symconst.
542  */
543 void set_ia32_am_sc_sign(ir_node *node) {
544         ia32_attr_t *attr     = get_ia32_attr(node);
545         attr->data.am_sc_sign = 1;
546 }
547
548 /**
549  * Clears the sign bit for address mode symconst.
550  */
551 void clear_ia32_am_sc_sign(ir_node *node) {
552         ia32_attr_t *attr     = get_ia32_attr(node);
553         attr->data.am_sc_sign = 0;
554 }
555
556 /**
557  * Returns the sign bit for address mode symconst.
558  */
559 int is_ia32_am_sc_sign(const ir_node *node) {
560         const ia32_attr_t *attr = get_ia32_attr_const(node);
561         return attr->data.am_sc_sign;
562 }
563
564 /**
565  * Gets the addr mode const.
566  */
567 int get_ia32_am_scale(const ir_node *node) {
568         const ia32_attr_t *attr = get_ia32_attr_const(node);
569         return attr->data.am_scale;
570 }
571
572 /**
573  * Sets the index register scale for address mode.
574  */
575 void set_ia32_am_scale(ir_node *node, int scale) {
576         ia32_attr_t *attr   = get_ia32_attr(node);
577         attr->data.am_scale = scale;
578 }
579
580 void ia32_copy_am_attrs(ir_node *to, const ir_node *from)
581 {
582         set_ia32_ls_mode(to, get_ia32_ls_mode(from));
583         set_ia32_am_scale(to, get_ia32_am_scale(from));
584         set_ia32_am_sc(to, get_ia32_am_sc(from));
585         if(is_ia32_am_sc_sign(from))
586                 set_ia32_am_sc_sign(to);
587         add_ia32_am_offs_int(to, get_ia32_am_offs_int(from));
588         set_ia32_frame_ent(to, get_ia32_frame_ent(from));
589         if (is_ia32_use_frame(from))
590                 set_ia32_use_frame(to);
591 }
592
593 /**
594  * Sets the uses_frame flag.
595  */
596 void set_ia32_use_frame(ir_node *node) {
597         ia32_attr_t *attr    = get_ia32_attr(node);
598         attr->data.use_frame = 1;
599 }
600
601 /**
602  * Clears the uses_frame flag.
603  */
604 void clear_ia32_use_frame(ir_node *node) {
605         ia32_attr_t *attr    = get_ia32_attr(node);
606         attr->data.use_frame = 0;
607 }
608
609 /**
610  * Gets the uses_frame flag.
611  */
612 int is_ia32_use_frame(const ir_node *node) {
613         const ia32_attr_t *attr = get_ia32_attr_const(node);
614         return attr->data.use_frame;
615 }
616
617 /**
618  * Sets node to commutative.
619  */
620 void set_ia32_commutative(ir_node *node) {
621         ia32_attr_t *attr         = get_ia32_attr(node);
622         attr->data.is_commutative = 1;
623 }
624
625 /**
626  * Sets node to non-commutative.
627  */
628 void clear_ia32_commutative(ir_node *node) {
629         ia32_attr_t *attr         = get_ia32_attr(node);
630         attr->data.is_commutative = 0;
631 }
632
633 /**
634  * Checks if node is commutative.
635  */
636 int is_ia32_commutative(const ir_node *node) {
637         const ia32_attr_t *attr = get_ia32_attr_const(node);
638         return attr->data.is_commutative;
639 }
640
641 void set_ia32_need_stackent(ir_node *node) {
642         ia32_attr_t *attr     = get_ia32_attr(node);
643         attr->data.need_stackent = 1;
644 }
645
646 void clear_ia32_need_stackent(ir_node *node) {
647         ia32_attr_t *attr     = get_ia32_attr(node);
648         attr->data.need_stackent = 0;
649 }
650
651 int is_ia32_need_stackent(const ir_node *node) {
652         const ia32_attr_t *attr = get_ia32_attr_const(node);
653         return attr->data.need_stackent;
654 }
655
656 /**
657  * Gets the mode of the stored/loaded value (only set for Store/Load)
658  */
659 ir_mode *get_ia32_ls_mode(const ir_node *node) {
660         const ia32_attr_t *attr = get_ia32_attr_const(node);
661         return attr->ls_mode;
662 }
663
664 /**
665  * Sets the mode of the stored/loaded value (only set for Store/Load)
666  */
667 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
668         ia32_attr_t *attr = get_ia32_attr(node);
669         attr->ls_mode     = mode;
670 }
671
672 /**
673  * Gets the frame entity assigned to this node.
674  */
675 ir_entity *get_ia32_frame_ent(const ir_node *node) {
676         const ia32_attr_t *attr = get_ia32_attr_const(node);
677         return attr->frame_ent;
678 }
679
680 /**
681  * Sets the frame entity for this node.
682  */
683 void set_ia32_frame_ent(ir_node *node, ir_entity *ent) {
684         ia32_attr_t *attr = get_ia32_attr(node);
685         attr->frame_ent   = ent;
686         if(ent != NULL)
687                 set_ia32_use_frame(node);
688         else
689                 clear_ia32_use_frame(node);
690 }
691
692
693 /**
694  * Gets the instruction latency.
695  */
696 unsigned get_ia32_latency(const ir_node *node) {
697         const ir_op *op               = get_irn_op(node);
698         const ia32_op_attr_t *op_attr = (ia32_op_attr_t*) get_op_attr(op);
699         return op_attr->latency;
700 }
701
702 /**
703  * Returns the argument register requirements of an ia32 node.
704  */
705 const arch_register_req_t **get_ia32_in_req_all(const ir_node *node) {
706         const ia32_attr_t *attr = get_ia32_attr_const(node);
707         return attr->in_req;
708 }
709
710 /**
711  * Sets the argument register requirements of an ia32 node.
712  */
713 void set_ia32_in_req_all(ir_node *node, const arch_register_req_t **reqs) {
714         ia32_attr_t *attr = get_ia32_attr(node);
715         attr->in_req      = reqs;
716 }
717
718 /**
719  * Returns the result register requirements of an ia32 node.
720  */
721 const arch_register_req_t **get_ia32_out_req_all(const ir_node *node) {
722         const ia32_attr_t *attr = get_ia32_attr_const(node);
723         return attr->out_req;
724 }
725
726 /**
727  * Sets the result register requirements of an ia32 node.
728  */
729 void set_ia32_out_req_all(ir_node *node, const arch_register_req_t **reqs) {
730         ia32_attr_t *attr = get_ia32_attr(node);
731         attr->out_req     = reqs;
732 }
733
734 /**
735  * Returns the argument register requirement at position pos of an ia32 node.
736  */
737 const arch_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
738         const ia32_attr_t *attr = get_ia32_attr_const(node);
739         if(attr->in_req == NULL)
740                 return arch_no_register_req;
741
742         return attr->in_req[pos];
743 }
744
745 /**
746  * Returns the result register requirement at position pos of an ia32 node.
747  */
748 const arch_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
749         const ia32_attr_t *attr = get_ia32_attr_const(node);
750         if(attr->out_req == NULL)
751                 return arch_no_register_req;
752
753         return attr->out_req[pos];
754 }
755
756 /**
757  * Sets the OUT register requirements at position pos.
758  */
759 void set_ia32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
760         ia32_attr_t *attr  = get_ia32_attr(node);
761         attr->out_req[pos] = req;
762 }
763
764 /**
765  * Sets the IN register requirements at position pos.
766  */
767 void set_ia32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
768         ia32_attr_t *attr = get_ia32_attr(node);
769         attr->in_req[pos] = req;
770 }
771
772 /**
773  * Returns the register flag of an ia32 node.
774  */
775 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
776         const ia32_attr_t *attr = get_ia32_attr_const(node);
777         return attr->data.flags;
778 }
779
780 /**
781  * Sets the register flag of an ia32 node.
782  */
783 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
784         ia32_attr_t *attr = get_ia32_attr(node);
785         attr->data.flags  = flags;
786 }
787
788 void add_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
789         ia32_attr_t *attr  = get_ia32_attr(node);
790         attr->data.flags  |= flags;
791 }
792
793 /**
794  * Returns the result register slots of an ia32 node.
795  */
796 const arch_register_t **get_ia32_slots(const ir_node *node) {
797         const ia32_attr_t *attr = get_ia32_attr_const(node);
798         return attr->slots;
799 }
800
801 /**
802  * Returns the number of results.
803  */
804 int get_ia32_n_res(const ir_node *node) {
805         const ia32_attr_t *attr = get_ia32_attr_const(node);
806         return ARR_LEN(attr->slots);
807 }
808
809 /**
810  * Returns the condition code of a node.
811  */
812 long get_ia32_condcode(const ir_node *node)
813 {
814         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
815         return attr->pn_code;
816 }
817
818 /**
819  * Sets the condition code of a node
820  */
821 void set_ia32_condcode(ir_node *node, long code)
822 {
823         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
824         attr->pn_code = code;
825 }
826
827 /**
828  * Returns the condition code of a node.
829  */
830 unsigned get_ia32_copyb_size(const ir_node *node)
831 {
832         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
833         return attr->size;
834 }
835
836 /**
837  * Sets the flags for the n'th out.
838  */
839 void set_ia32_out_flags(ir_node *node, arch_irn_flags_t flags, int pos) {
840         ia32_attr_t *attr = get_ia32_attr(node);
841         assert(pos < ARR_LEN(attr->out_flags) && "Invalid OUT position.");
842         attr->out_flags[pos] = flags;
843 }
844
845 /**
846  * Gets the flags for the n'th out.
847  */
848 arch_irn_flags_t get_ia32_out_flags(const ir_node *node, int pos) {
849         const ia32_attr_t *attr = get_ia32_attr_const(node);
850         assert(pos < ARR_LEN(attr->out_flags) && "Invalid OUT position.");
851         return attr->out_flags[pos];
852 }
853
854 /**
855  * Get the list of available execution units.
856  */
857 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node) {
858         const ia32_attr_t *attr = get_ia32_attr_const(node);
859         return attr->exec_units;
860 }
861
862 /**
863  * Get the exception label attribute.
864  */
865 unsigned get_ia32_exc_label(const ir_node *node) {
866         const ia32_attr_t *attr = get_ia32_attr_const(node);
867         return attr->data.has_except_label;
868 }
869
870 /**
871  * Set the exception label attribute.
872  */
873 void set_ia32_exc_label(ir_node *node, unsigned flag) {
874         ia32_attr_t *attr = get_ia32_attr(node);
875         attr->data.has_except_label = flag;
876 }
877
878 /**
879  * Return the exception label id.
880  */
881 ir_label_t get_ia32_exc_label_id(const ir_node *node) {
882         const ia32_attr_t *attr = get_ia32_attr_const(node);
883
884         assert(attr->data.has_except_label);
885         return attr->exc_label;
886 }
887
888 /**
889  * Assign the exception label id.
890  */
891 void set_ia32_exc_label_id(ir_node *node, ir_label_t id) {
892         ia32_attr_t *attr = get_ia32_attr(node);
893
894         assert(attr->data.has_except_label);
895         attr->exc_label = id;
896 }
897
898 #ifndef NDEBUG
899
900 /**
901  * Returns the name of the original ir node.
902  */
903 const char *get_ia32_orig_node(const ir_node *node) {
904         const ia32_attr_t *attr = get_ia32_attr_const(node);
905         return attr->orig_node;
906 }
907
908 /**
909  * Sets the name of the original ir node.
910  */
911 void set_ia32_orig_node(ir_node *node, const char *name) {
912         ia32_attr_t *attr = get_ia32_attr(node);
913         attr->orig_node   = name;
914 }
915
916 #endif /* NDEBUG */
917
918 /******************************************************************************************************
919  *                      _       _         _   _           __                  _   _
920  *                     (_)     | |       | | | |         / _|                | | (_)
921  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
922  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
923  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
924  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
925  *     | |
926  *     |_|
927  ******************************************************************************************************/
928
929 /**
930  * Returns whether or not the node is an AddrModeS node.
931  */
932 int is_ia32_AddrModeS(const ir_node *node) {
933         const ia32_attr_t *attr = get_ia32_attr_const(node);
934         return (attr->data.tp == ia32_AddrModeS);
935 }
936
937 /**
938  * Returns whether or not the node is an AddrModeD node.
939  */
940 int is_ia32_AddrModeD(const ir_node *node) {
941         const ia32_attr_t *attr = get_ia32_attr_const(node);
942         return (attr->data.tp == ia32_AddrModeD);
943 }
944
945 /**
946  * Checks if node is a Load or xLoad/vfLoad.
947  */
948 int is_ia32_Ld(const ir_node *node) {
949         int op = get_ia32_irn_opcode(node);
950         return op == iro_ia32_Load ||
951                op == iro_ia32_xLoad ||
952                op == iro_ia32_vfld ||
953                op == iro_ia32_fld;
954 }
955
956 /**
957  * Checks if node is a Store or xStore/vfStore.
958  */
959 int is_ia32_St(const ir_node *node) {
960         int op = get_ia32_irn_opcode(node);
961         return op == iro_ia32_Store ||
962                op == iro_ia32_Store8Bit ||
963                op == iro_ia32_xStore ||
964                op == iro_ia32_vfst ||
965                op == iro_ia32_fst ||
966                op == iro_ia32_fstp;
967 }
968
969 /**
970  * Returns the name of the OUT register at position pos.
971  */
972 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
973         const ia32_attr_t *attr = get_ia32_attr_const(node);
974
975         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
976         assert(attr->slots[pos]  && "No register assigned");
977
978         return arch_register_get_name(attr->slots[pos]);
979 }
980
981 /**
982  * Returns the index of the OUT register at position pos within its register class.
983  */
984 int get_ia32_out_regnr(const ir_node *node, int pos) {
985         const ia32_attr_t *attr = get_ia32_attr_const(node);
986
987         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
988         assert(attr->slots[pos]  && "No register assigned");
989
990         return arch_register_get_index(attr->slots[pos]);
991 }
992
993 void ia32_swap_left_right(ir_node *node)
994 {
995         ia32_attr_t *attr  = get_ia32_attr(node);
996         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
997         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
998
999         assert(is_ia32_commutative(node));
1000         attr->data.ins_permuted = !attr->data.ins_permuted;
1001         set_irn_n(node, n_ia32_binary_left,  right);
1002         set_irn_n(node, n_ia32_binary_right, left);
1003 }
1004
1005 /**
1006  * Returns the OUT register at position pos.
1007  */
1008 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1009         const ia32_attr_t *attr = get_ia32_attr_const(node);
1010
1011         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
1012         assert(attr->slots[pos]  && "No register assigned");
1013
1014         return attr->slots[pos];
1015 }
1016
1017 /**
1018  * Initializes the nodes attributes.
1019  */
1020 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
1021                           const arch_register_req_t **in_reqs,
1022                           const arch_register_req_t **out_reqs,
1023                           const be_execution_unit_t ***execution_units,
1024                           int n_res)
1025 {
1026         ir_graph        *irg  = get_irn_irg(node);
1027         struct obstack  *obst = get_irg_obstack(irg);
1028         ia32_attr_t     *attr = get_ia32_attr(node);
1029
1030         set_ia32_flags(node, flags);
1031         set_ia32_in_req_all(node, in_reqs);
1032         set_ia32_out_req_all(node, out_reqs);
1033
1034         attr->exec_units  = execution_units;
1035 #ifndef NDEBUG
1036         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
1037 #endif
1038
1039         attr->out_flags = NEW_ARR_D(int, obst, n_res);
1040         memset(attr->out_flags, 0, n_res * sizeof(attr->out_flags[0]));
1041
1042         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
1043         /* void* cast to suppress an incorrect warning on MSVC */
1044         memset((void*)attr->slots, 0, n_res * sizeof(attr->slots[0]));
1045 }
1046
1047 void
1048 init_ia32_x87_attributes(ir_node *res)
1049 {
1050 #ifndef NDEBUG
1051         ia32_attr_t *attr  = get_ia32_attr(res);
1052         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
1053 #else
1054         (void) res;
1055 #endif
1056         ia32_current_cg->do_x87_sim = 1;
1057 }
1058
1059 void
1060 init_ia32_asm_attributes(ir_node *res)
1061 {
1062 #ifndef NDEBUG
1063         ia32_attr_t *attr  = get_ia32_attr(res);
1064         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
1065 #else
1066         (void) res;
1067 #endif
1068 }
1069
1070 void
1071 init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
1072                                int symconst_sign, long offset)
1073 {
1074         ia32_immediate_attr_t *attr = get_irn_generic_attr(res);
1075
1076 #ifndef NDEBUG
1077         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
1078 #endif
1079         attr->symconst = symconst;
1080         attr->sc_sign  = symconst_sign;
1081         attr->offset   = offset;
1082 }
1083
1084 void
1085 init_ia32_copyb_attributes(ir_node *res, unsigned size) {
1086         ia32_copyb_attr_t *attr = get_irn_generic_attr(res);
1087
1088 #ifndef NDEBUG
1089         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
1090 #endif
1091         attr->size = size;
1092 }
1093
1094 void
1095 init_ia32_condcode_attributes(ir_node *res, long pnc) {
1096         ia32_condcode_attr_t *attr = get_irn_generic_attr(res);
1097
1098 #ifndef NDEBUG
1099         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
1100 #endif
1101         attr->pn_code = pnc;
1102 }
1103
1104 ir_node *get_ia32_result_proj(const ir_node *node)
1105 {
1106         const ir_edge_t *edge;
1107
1108         foreach_out_edge(node, edge) {
1109                 ir_node *proj = get_edge_src_irn(edge);
1110                 if(get_Proj_proj(proj) == 0) {
1111                         return proj;
1112                 }
1113         }
1114         return NULL;
1115 }
1116
1117 /***************************************************************************************
1118  *                  _                            _                   _
1119  *                 | |                          | |                 | |
1120  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1121  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1122  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1123  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1124  *
1125  ***************************************************************************************/
1126
1127 /* default compare operation to compare attributes */
1128 int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b) {
1129         if (a->data.tp != b->data.tp)
1130                 return 1;
1131
1132         if (a->data.am_scale != b->data.am_scale
1133             || a->data.am_sc_sign != b->data.am_sc_sign
1134             || a->am_offs != b->am_offs
1135             || a->am_sc != b->am_sc
1136             || a->ls_mode != b->ls_mode)
1137                 return 1;
1138
1139         /* nodes with not yet assigned entities shouldn't be CSEd (important for
1140          * unsigned int -> double conversions */
1141         if(a->data.use_frame && a->frame_ent == NULL)
1142                 return 1;
1143         if(b->data.use_frame && b->frame_ent == NULL)
1144                 return 1;
1145
1146         if (a->data.use_frame != b->data.use_frame
1147             || a->frame_ent != b->frame_ent)
1148                 return 1;
1149
1150         if (a->data.tp != b->data.tp)
1151                 return 1;
1152
1153         if (a->data.has_except_label != b->data.has_except_label)
1154                 return 1;
1155
1156         if (a->data.ins_permuted != b->data.ins_permuted
1157                         || a->data.cmp_unsigned != b->data.cmp_unsigned)
1158                 return 1;
1159
1160         return 0;
1161 }
1162
1163 /** Compare nodes attributes for all "normal" nodes. */
1164 static
1165 int ia32_compare_nodes_attr(ir_node *a, ir_node *b)
1166 {
1167         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
1168         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
1169
1170         return ia32_compare_attr(attr_a, attr_b);
1171 }
1172
1173 /** Compare node attributes for nodes with condition code. */
1174 static
1175 int ia32_compare_condcode_attr(ir_node *a, ir_node *b)
1176 {
1177         const ia32_condcode_attr_t *attr_a;
1178         const ia32_condcode_attr_t *attr_b;
1179
1180         if (ia32_compare_nodes_attr(a, b))
1181                 return 1;
1182
1183         attr_a = get_ia32_condcode_attr_const(a);
1184         attr_b = get_ia32_condcode_attr_const(b);
1185
1186         if(attr_a->pn_code != attr_b->pn_code)
1187                 return 1;
1188
1189         return 0;
1190 }
1191
1192 /** Compare node attributes for CopyB nodes. */
1193 static
1194 int ia32_compare_copyb_attr(ir_node *a, ir_node *b)
1195 {
1196         const ia32_copyb_attr_t *attr_a;
1197         const ia32_copyb_attr_t *attr_b;
1198
1199         if (ia32_compare_nodes_attr(a, b))
1200                 return 1;
1201
1202         attr_a = get_ia32_copyb_attr_const(a);
1203         attr_b = get_ia32_copyb_attr_const(b);
1204
1205         if(attr_a->size != attr_b->size)
1206                 return 1;
1207
1208         return 0;
1209 }
1210
1211
1212 /** Compare ASM node attributes. */
1213 static
1214 int ia32_compare_asm_attr(ir_node *a, ir_node *b)
1215 {
1216         const ia32_asm_attr_t *attr_a;
1217         const ia32_asm_attr_t *attr_b;
1218
1219         if(ia32_compare_nodes_attr(a, b))
1220                 return 1;
1221
1222         attr_a = get_ia32_asm_attr_const(a);
1223         attr_b = get_ia32_asm_attr_const(b);
1224
1225         if(attr_a->asm_text != attr_b->asm_text)
1226                 return 1;
1227
1228         return 0;
1229 }
1230
1231 /** Compare node attributes for Immediates. */
1232 static
1233 int ia32_compare_immediate_attr(ir_node *a, ir_node *b)
1234 {
1235         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1236         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1237
1238         if(attr_a->symconst != attr_b->symconst ||
1239            attr_a->sc_sign != attr_b->sc_sign ||
1240            attr_a->offset != attr_b->offset)
1241                 return 1;
1242
1243         return 0;
1244 }
1245
1246 /** Compare node attributes for x87 nodes. */
1247 static
1248 int ia32_compare_x87_attr(ir_node *a, ir_node *b)
1249 {
1250         return ia32_compare_nodes_attr(a, b);
1251 }
1252
1253
1254 /* copies the ia32 attributes */
1255 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node)
1256 {
1257         ir_graph          *irg      = get_irn_irg(new_node);
1258         struct obstack    *obst     = get_irg_obstack(irg);
1259         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1260         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1261
1262         /* copy the attributes */
1263         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1264
1265         /* copy out flags */
1266         attr_new->out_flags =
1267                 DUP_ARR_D(int, obst, attr_old->out_flags);
1268         /* copy register assignments */
1269         attr_new->slots =
1270                 DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
1271 }
1272
1273 /* Include the generated constructor functions */
1274 #include "gen_ia32_new_nodes.c.inl"