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