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