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