alloca is now declared in xmalloc.h
[libfirm] / ir / be / ia32 / ia32_new_nodes.c
1 /**
2  * This file implements the creation of the achitecture specific firm opcodes
3  * and the coresponding node constructors for the ia32 assembler irg.
4  * @author Christian Wuerdig
5  * $Id$
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "irprog_t.h"
14 #include "irgraph_t.h"
15 #include "irnode_t.h"
16 #include "irmode_t.h"
17 #include "ircons_t.h"
18 #include "iropt_t.h"
19 #include "irop.h"
20 #include "firm_common_t.h"
21 #include "irvrfy_t.h"
22 #include "irprintf.h"
23 #include "iredges.h"
24 #include "error.h"
25 #include "raw_bitset.h"
26 #include "xmalloc.h"
27
28 #include "../bearch.h"
29
30 #include "ia32_nodes_attr.h"
31 #include "ia32_new_nodes.h"
32 #include "gen_ia32_regalloc_if.h"
33 #include "gen_ia32_machine.h"
34
35 /**
36  * returns true if a node has x87 registers
37  */
38 int ia32_has_x87_register(const ir_node *n) {
39         assert(is_ia32_irn(n) && "Need ia32 node.");
40         return is_irn_machine_user(n, 0);
41 }
42
43 /***********************************************************************************
44  *      _                                   _       _             __
45  *     | |                                 (_)     | |           / _|
46  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
47  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
48  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
49  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
50  *                       | |
51  *                       |_|
52  ***********************************************************************************/
53
54 /**
55  * Dumps the register requirements for either in or out.
56  */
57 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
58                          int inout) {
59         char *dir = inout ? "out" : "in";
60         int   max = inout ? get_ia32_n_res(n) : get_irn_arity(n);
61         char  buf[1024];
62         int   i;
63
64         memset(buf, 0, sizeof(buf));
65
66         if (reqs) {
67                 for (i = 0; i < max; i++) {
68                         fprintf(F, "%sreq #%d =", dir, i);
69
70                         if (reqs[i]->type == arch_register_req_type_none) {
71                                 fprintf(F, " n/a");
72                         }
73
74                         if (reqs[i]->type & arch_register_req_type_normal) {
75                                 fprintf(F, " %s", reqs[i]->cls->name);
76                         }
77
78                         if (reqs[i]->type & arch_register_req_type_limited) {
79                                 fprintf(F, " %s",
80                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
81                         }
82
83                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
84                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
85                         }
86
87                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
88                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
89                         }
90
91                         fprintf(F, "\n");
92                 }
93
94                 fprintf(F, "\n");
95         }
96         else {
97                 fprintf(F, "%sreq = N/A\n", dir);
98         }
99 }
100
101 /**
102  * Dumper interface for dumping ia32 nodes in vcg.
103  * @param n        the node to dump
104  * @param F        the output file
105  * @param reason   indicates which kind of information should be dumped
106  * @return 0 on success or != 0 on failure
107  */
108 static int ia32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
109         ir_mode     *mode = NULL;
110         int          bad  = 0;
111         int          i, n_res, am_flav, flags;
112         const arch_register_req_t **reqs;
113         const arch_register_t     **slots;
114
115         switch (reason) {
116                 case dump_node_opcode_txt:
117                         fprintf(F, "%s", get_irn_opname(n));
118                         break;
119
120                 case dump_node_mode_txt:
121                         mode = get_irn_mode(n);
122
123                         if (is_ia32_Ld(n) || is_ia32_St(n)) {
124                                 mode = get_ia32_ls_mode(n);
125                         }
126
127                         fprintf(F, "[%s]", mode ? get_mode_name(mode) : "?NOMODE?");
128                         break;
129
130                 case dump_node_nodeattr_txt:
131                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
132                                 if(is_ia32_ImmSymConst(n)) {
133                                         ir_entity *ent = get_ia32_Immop_symconst(n);
134                                         ident *id = get_entity_ld_ident(ent);
135                                         fprintf(F, "[SymC %s]", get_id_str(id));
136                                 } else {
137                                         char buf[128];
138                                         tarval *tv = get_ia32_Immop_tarval(n);
139
140                                         tarval_snprintf(buf, sizeof(buf), tv);
141                                         fprintf(F, "[%s]", buf);
142                                 }
143                         }
144
145                         if (! is_ia32_Lea(n)) {
146                                 if (is_ia32_AddrModeS(n)) {
147                                         fprintf(F, "[AM S] ");
148                                 }
149                                 else if (is_ia32_AddrModeD(n)) {
150                                         fprintf(F, "[AM D] ");
151                                 }
152                         }
153
154                         break;
155
156                 case dump_node_info_txt:
157                         n_res = get_ia32_n_res(n);
158                         fprintf(F, "=== IA32 attr begin ===\n");
159
160                         /* dump IN requirements */
161                         if (get_irn_arity(n) > 0) {
162                                 reqs = get_ia32_in_req_all(n);
163                                 dump_reg_req(F, n, reqs, 0);
164                         }
165
166                         /* dump OUT requirements */
167                         if (n_res > 0) {
168                                 reqs = get_ia32_out_req_all(n);
169                                 dump_reg_req(F, n, reqs, 1);
170                         }
171
172                         /* dump assigned registers */
173                         slots = get_ia32_slots(n);
174                         if (slots && n_res > 0) {
175                                 for (i = 0; i < n_res; i++) {
176                                         const arch_register_t *reg;
177
178                                         /* retrieve "real" x87 register */
179                                         if (ia32_has_x87_register(n))
180                                                 reg = get_ia32_attr(n)->x87[i + 2];
181                                         else
182                                                 reg = slots[i];
183
184                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
185                                 }
186                                 fprintf(F, "\n");
187                         }
188
189                         /* dump op type */
190                         fprintf(F, "op = ");
191                         switch (get_ia32_op_type(n)) {
192                                 case ia32_Normal:
193                                         fprintf(F, "Normal");
194                                         break;
195                                 case ia32_AddrModeD:
196                                         fprintf(F, "AM Dest (Load+Store)");
197                                         break;
198                                 case ia32_AddrModeS:
199                                         fprintf(F, "AM Source (Load)");
200                                         break;
201                                 default:
202                                         fprintf(F, "unknown (%d)", get_ia32_op_type(n));
203                                         break;
204                         }
205                         fprintf(F, "\n");
206
207                         /* dump immop type */
208                         fprintf(F, "immediate = ");
209                         switch (get_ia32_immop_type(n)) {
210                                 case ia32_ImmNone:
211                                         fprintf(F, "None");
212                                         break;
213                                 case ia32_ImmConst:
214                                         fprintf(F, "Const");
215                                         break;
216                                 case ia32_ImmSymConst:
217                                         fprintf(F, "SymConst");
218                                         break;
219                                 default:
220                                         fprintf(F, "unknown (%d)", get_ia32_immop_type(n));
221                                         break;
222                         }
223                         fprintf(F, "\n");
224
225                         /* dump supported am */
226                         fprintf(F, "AM support = ");
227                         switch (get_ia32_am_support(n)) {
228                                 case ia32_am_None:
229                                         fprintf(F, "none");
230                                         break;
231                                 case ia32_am_Source:
232                                         fprintf(F, "source only (Load)");
233                                         break;
234                                 case ia32_am_Dest:
235                                         fprintf(F, "dest only (Load+Store)");
236                                         break;
237                                 case ia32_am_Full:
238                                         fprintf(F, "full");
239                                         break;
240                                 default:
241                                         fprintf(F, "unknown (%d)", get_ia32_am_support(n));
242                                         break;
243                         }
244                         fprintf(F, "\n");
245
246                         /* dump am flavour */
247                         fprintf(F, "AM flavour =");
248                         am_flav = get_ia32_am_flavour(n);
249                         if (am_flav == ia32_am_N) {
250                                 fprintf(F, " none");
251                         }
252                         else {
253                                 if (am_flav & ia32_O) {
254                                         fprintf(F, " O");
255                                 }
256                                 if (am_flav & ia32_B) {
257                                         fprintf(F, " B");
258                                 }
259                                 if (am_flav & ia32_I) {
260                                         fprintf(F, " I");
261                                 }
262                                 if (am_flav & ia32_S) {
263                                         fprintf(F, " S");
264                                 }
265                         }
266                         fprintf(F, " (%d)\n", am_flav);
267
268                         /* dump AM offset */
269                         if(get_ia32_am_offs_int(n) != 0) {
270                                 fprintf(F, "AM offset = %d\n", get_ia32_am_offs_int(n));
271                         }
272
273                         /* dump AM symconst */
274                         if(get_ia32_am_sc(n) != NULL) {
275                                 ir_entity *ent = get_ia32_am_sc(n);
276                                 ident *id = get_entity_ld_ident(ent);
277                                 fprintf(F, "AM symconst = %s\n", get_id_str(id));
278                         }
279
280                         /* dump AM scale */
281                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
282
283                         /* dump pn code */
284                         if(get_ia32_pncode(n) & ia32_pn_Cmp_Unsigned) {
285                                 fprintf(F, "pn_code = %d (%s, unsigned)\n", get_ia32_pncode(n),
286                                         get_pnc_string(get_ia32_pncode(n) & ~ia32_pn_Cmp_Unsigned));
287                         } else {
288                                 fprintf(F, "pn_code = %d (%s)\n", get_ia32_pncode(n),
289                                         get_pnc_string(get_ia32_pncode(n)));
290                         }
291
292                         /* dump n_res */
293                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
294
295                         /* dump use_frame */
296                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
297
298                         /* commutative */
299                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
300
301                         /* emit cl */
302                         fprintf(F, "emit cl instead of ecx = %d\n", is_ia32_emit_cl(n));
303
304                         /* got lea */
305                         fprintf(F, "got loea = %d\n", is_ia32_got_lea(n));
306
307                         /* need stackent */
308                         fprintf(F, "need stackent = %d\n", is_ia32_need_stackent(n));
309
310                         /* dump latency */
311                         fprintf(F, "latency = %d\n", get_ia32_latency(n));
312
313                         /* dump flags */
314                         fprintf(F, "flags =");
315                         flags = get_ia32_flags(n);
316                         if (flags == arch_irn_flags_none) {
317                                 fprintf(F, " none");
318                         }
319                         else {
320                                 if (flags & arch_irn_flags_dont_spill) {
321                                         fprintf(F, " unspillable");
322                                 }
323                                 if (flags & arch_irn_flags_rematerializable) {
324                                         fprintf(F, " remat");
325                                 }
326                                 if (flags & arch_irn_flags_ignore) {
327                                         fprintf(F, " ignore");
328                                 }
329                                 if (flags & arch_irn_flags_modify_sp) {
330                                         fprintf(F, " modify_sp");
331                                 }
332                         }
333                         fprintf(F, " (%d)\n", flags);
334
335                         /* dump frame entity */
336                         fprintf(F, "frame entity = ");
337                         if (get_ia32_frame_ent(n)) {
338                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
339                         }
340                         else {
341                                 fprintf(F, "n/a");
342                         }
343                         fprintf(F, "\n");
344
345                         /* dump modes */
346                         fprintf(F, "ls_mode = ");
347                         if (get_ia32_ls_mode(n)) {
348                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
349                         }
350                         else {
351                                 fprintf(F, "n/a");
352                         }
353                         fprintf(F, "\n");
354
355 #ifndef NDEBUG
356                         /* dump original ir node name */
357                         fprintf(F, "orig node = ");
358                         if (get_ia32_orig_node(n)) {
359                                 fprintf(F, "%s", get_ia32_orig_node(n));
360                         }
361                         else {
362                                 fprintf(F, "n/a");
363                         }
364                         fprintf(F, "\n");
365 #endif /* NDEBUG */
366
367                         fprintf(F, "=== IA32 attr end ===\n");
368                         /* end of: case dump_node_info_txt */
369                         break;
370         }
371
372         return bad;
373 }
374
375
376
377 /***************************************************************************************************
378  *        _   _                   _       __        _                    _   _               _
379  *       | | | |                 | |     / /       | |                  | | | |             | |
380  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
381  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
382  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
383  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
384  *                                        __/ |
385  *                                       |___/
386  ***************************************************************************************************/
387
388 /**
389  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
390  * Firm was made by people hating const :-(
391  */
392 ia32_attr_t *get_ia32_attr(const ir_node *node) {
393         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
394         return (ia32_attr_t *)get_irn_generic_attr((ir_node *)node);
395 }
396
397 /**
398  * Gets the type of an ia32 node.
399  */
400 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
401         ia32_attr_t *attr = get_ia32_attr(node);
402         return attr->data.tp;
403 }
404
405 /**
406  * Sets the type of an ia32 node.
407  */
408 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
409         ia32_attr_t *attr = get_ia32_attr(node);
410         attr->data.tp     = tp;
411 }
412
413 /**
414  * Gets the immediate op type of an ia32 node.
415  */
416 ia32_immop_type_t get_ia32_immop_type(const ir_node *node) {
417         ia32_attr_t *attr = get_ia32_attr(node);
418         return attr->data.imm_tp;
419 }
420
421 /**
422  * Gets the supported addrmode of an ia32 node
423  */
424 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
425         ia32_attr_t *attr = get_ia32_attr(node);
426         return attr->data.am_support;
427 }
428
429 /**
430  * Sets the supported addrmode of an ia32 node
431  */
432 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
433         ia32_attr_t *attr     = get_ia32_attr(node);
434         attr->data.am_support = am_tp;
435 }
436
437 /**
438  * Gets the addrmode flavour of an ia32 node
439  */
440 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
441         ia32_attr_t *attr = get_ia32_attr(node);
442         return attr->data.am_flavour;
443 }
444
445 /**
446  * Sets the addrmode flavour of an ia32 node
447  */
448 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
449         ia32_attr_t *attr     = get_ia32_attr(node);
450         attr->data.am_flavour = am_flavour;
451 }
452
453 /**
454  * Gets the addressmode offset as int.
455  */
456 int get_ia32_am_offs_int(const ir_node *node) {
457         ia32_attr_t *attr = get_ia32_attr(node);
458         return attr->am_offs;
459 }
460
461 /**
462  * Sets the addressmode offset from an int.
463  */
464 void set_ia32_am_offs_int(ir_node *node, int offset) {
465         ia32_attr_t *attr = get_ia32_attr(node);
466         attr->am_offs = offset;
467 }
468
469 void add_ia32_am_offs_int(ir_node *node, int offset) {
470         ia32_attr_t *attr = get_ia32_attr(node);
471         attr->am_offs += offset;
472 }
473
474 /**
475  * Returns the symconst entity associated to addrmode.
476  */
477 ir_entity *get_ia32_am_sc(const ir_node *node) {
478         ia32_attr_t *attr = get_ia32_attr(node);
479         return attr->am_sc;
480 }
481
482 /**
483  * Sets the symconst entity associated to addrmode.
484  */
485 void set_ia32_am_sc(ir_node *node, ir_entity *entity) {
486         ia32_attr_t *attr = get_ia32_attr(node);
487         attr->am_sc       = entity;
488 }
489
490 /**
491  * Sets the sign bit for address mode symconst.
492  */
493 void set_ia32_am_sc_sign(ir_node *node) {
494         ia32_attr_t *attr     = get_ia32_attr(node);
495         attr->data.am_sc_sign = 1;
496 }
497
498 /**
499  * Clears the sign bit for address mode symconst.
500  */
501 void clear_ia32_am_sc_sign(ir_node *node) {
502         ia32_attr_t *attr     = get_ia32_attr(node);
503         attr->data.am_sc_sign = 0;
504 }
505
506 /**
507  * Returns the sign bit for address mode symconst.
508  */
509 int is_ia32_am_sc_sign(const ir_node *node) {
510         ia32_attr_t *attr = get_ia32_attr(node);
511         return attr->data.am_sc_sign;
512 }
513
514 /**
515  * Gets the addr mode const.
516  */
517 int get_ia32_am_scale(const ir_node *node) {
518         ia32_attr_t *attr = get_ia32_attr(node);
519         return attr->data.am_scale;
520 }
521
522 /**
523  * Sets the index register scale for addrmode.
524  */
525 void set_ia32_am_scale(ir_node *node, int scale) {
526         ia32_attr_t *attr   = get_ia32_attr(node);
527         attr->data.am_scale = scale;
528 }
529
530 /**
531  * Return the tarval of an immediate operation or NULL in case of SymConst
532  */
533 tarval *get_ia32_Immop_tarval(const ir_node *node) {
534         ia32_attr_t *attr = get_ia32_attr(node);
535         assert(attr->data.imm_tp == ia32_ImmConst);
536     return attr->cnst_val.tv;
537 }
538
539 /**
540  * Sets the attributes of an immediate operation to the specified tarval
541  */
542 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
543         ia32_attr_t *attr = get_ia32_attr(node);
544         attr->data.imm_tp = ia32_ImmConst;
545         attr->cnst_val.tv = tv;
546 }
547
548 void set_ia32_Immop_symconst(ir_node *node, ir_entity *entity) {
549         ia32_attr_t *attr = get_ia32_attr(node);
550         attr->data.imm_tp = ia32_ImmSymConst;
551         attr->cnst_val.sc = entity;
552 }
553
554 ir_entity *get_ia32_Immop_symconst(const ir_node *node) {
555         ia32_attr_t *attr = get_ia32_attr(node);
556         assert(attr->data.imm_tp == ia32_ImmSymConst);
557         return attr->cnst_val.sc;
558 }
559
560 /**
561  * Sets the uses_frame flag.
562  */
563 void set_ia32_use_frame(ir_node *node) {
564         ia32_attr_t *attr    = get_ia32_attr(node);
565         attr->data.use_frame = 1;
566 }
567
568 /**
569  * Clears the uses_frame flag.
570  */
571 void clear_ia32_use_frame(ir_node *node) {
572         ia32_attr_t *attr    = get_ia32_attr(node);
573         attr->data.use_frame = 0;
574 }
575
576 /**
577  * Gets the uses_frame flag.
578  */
579 int is_ia32_use_frame(const ir_node *node) {
580         ia32_attr_t *attr = get_ia32_attr(node);
581         return attr->data.use_frame;
582 }
583
584 /**
585  * Sets node to commutative.
586  */
587 void set_ia32_commutative(ir_node *node) {
588         ia32_attr_t *attr         = get_ia32_attr(node);
589         attr->data.is_commutative = 1;
590 }
591
592 /**
593  * Sets node to non-commutative.
594  */
595 void clear_ia32_commutative(ir_node *node) {
596         ia32_attr_t *attr         = get_ia32_attr(node);
597         attr->data.is_commutative = 0;
598 }
599
600 /**
601  * Checks if node is commutative.
602  */
603 int is_ia32_commutative(const ir_node *node) {
604         ia32_attr_t *attr = get_ia32_attr(node);
605         return attr->data.is_commutative;
606 }
607
608 /**
609  * Sets node emit_cl.
610  */
611 void set_ia32_emit_cl(ir_node *node) {
612         ia32_attr_t *attr  = get_ia32_attr(node);
613         attr->data.emit_cl = 1;
614 }
615
616 /**
617  * Clears node emit_cl.
618  */
619 void clear_ia32_emit_cl(ir_node *node) {
620         ia32_attr_t *attr  = get_ia32_attr(node);
621         attr->data.emit_cl = 0;
622 }
623
624 /**
625  * Checks if node needs %cl.
626  */
627 int is_ia32_emit_cl(const ir_node *node) {
628         ia32_attr_t *attr = get_ia32_attr(node);
629         return attr->data.emit_cl;
630 }
631
632 /**
633  * Sets node got_lea.
634  */
635 void set_ia32_got_lea(ir_node *node) {
636         ia32_attr_t *attr  = get_ia32_attr(node);
637         attr->data.got_lea = 1;
638 }
639
640 /**
641  * Clears node got_lea.
642  */
643 void clear_ia32_got_lea(ir_node *node) {
644         ia32_attr_t *attr  = get_ia32_attr(node);
645         attr->data.got_lea = 0;
646 }
647
648 /**
649  * Checks if node got lea.
650  */
651 int is_ia32_got_lea(const ir_node *node) {
652         ia32_attr_t *attr = get_ia32_attr(node);
653         return attr->data.got_lea;
654 }
655
656 void set_ia32_need_stackent(ir_node *node) {
657         ia32_attr_t *attr     = get_ia32_attr(node);
658         attr->data.need_stackent = 1;
659 }
660
661 void clear_ia32_need_stackent(ir_node *node) {
662         ia32_attr_t *attr     = get_ia32_attr(node);
663         attr->data.need_stackent = 0;
664 }
665
666 int is_ia32_need_stackent(const ir_node *node) {
667         ia32_attr_t *attr = get_ia32_attr(node);
668         return attr->data.need_stackent;
669 }
670
671 /**
672  * Gets the mode of the stored/loaded value (only set for Store/Load)
673  */
674 ir_mode *get_ia32_ls_mode(const ir_node *node) {
675         ia32_attr_t *attr = get_ia32_attr(node);
676         return attr->ls_mode;
677 }
678
679 /**
680  * Sets the mode of the stored/loaded value (only set for Store/Load)
681  */
682 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
683         ia32_attr_t *attr = get_ia32_attr(node);
684         attr->ls_mode     = mode;
685 }
686
687 /**
688  * Gets the frame entity assigned to this node.
689  */
690 ir_entity *get_ia32_frame_ent(const ir_node *node) {
691         ia32_attr_t *attr = get_ia32_attr(node);
692         return attr->frame_ent;
693 }
694
695 /**
696  * Sets the frame entity for this node.
697  */
698 void set_ia32_frame_ent(ir_node *node, ir_entity *ent) {
699         ia32_attr_t *attr = get_ia32_attr(node);
700         attr->frame_ent   = ent;
701         if(ent != NULL)
702                 set_ia32_use_frame(node);
703         else
704                 clear_ia32_use_frame(node);
705 }
706
707
708 /**
709  * Gets the instruction latency.
710  */
711 unsigned get_ia32_latency(const ir_node *node) {
712         ia32_attr_t *attr = get_ia32_attr(node);
713         return attr->latency;
714 }
715
716 /**
717 * Sets the instruction latency.
718 */
719 void set_ia32_latency(ir_node *node, unsigned latency) {
720         ia32_attr_t *attr = get_ia32_attr(node);
721         attr->latency     = latency;
722 }
723
724 /**
725  * Returns the argument register requirements of an ia32 node.
726  */
727 const arch_register_req_t **get_ia32_in_req_all(const ir_node *node) {
728         ia32_attr_t *attr = get_ia32_attr(node);
729         return attr->in_req;
730 }
731
732 /**
733  * Sets the argument register requirements of an ia32 node.
734  */
735 void set_ia32_in_req_all(ir_node *node, const arch_register_req_t **reqs) {
736         ia32_attr_t *attr = get_ia32_attr(node);
737         attr->in_req      = reqs;
738 }
739
740 /**
741  * Returns the result register requirements of an ia32 node.
742  */
743 const arch_register_req_t **get_ia32_out_req_all(const ir_node *node) {
744         ia32_attr_t *attr = get_ia32_attr(node);
745         return attr->out_req;
746 }
747
748 /**
749  * Sets the result register requirements of an ia32 node.
750  */
751 void set_ia32_out_req_all(ir_node *node, const arch_register_req_t **reqs) {
752         ia32_attr_t *attr = get_ia32_attr(node);
753         attr->out_req     = reqs;
754 }
755
756 /**
757  * Returns the argument register requirement at position pos of an ia32 node.
758  */
759 const arch_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
760         ia32_attr_t *attr = get_ia32_attr(node);
761         if(attr->in_req == NULL)
762                 return arch_no_register_req;
763
764         return attr->in_req[pos];
765 }
766
767 /**
768  * Returns the result register requirement at position pos of an ia32 node.
769  */
770 const arch_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
771         ia32_attr_t *attr = get_ia32_attr(node);
772         if(attr->out_req == NULL)
773                 return arch_no_register_req;
774
775         return attr->out_req[pos];
776 }
777
778 /**
779  * Sets the OUT register requirements at position pos.
780  */
781 void set_ia32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
782         ia32_attr_t *attr  = get_ia32_attr(node);
783         attr->out_req[pos] = req;
784 }
785
786 /**
787  * Sets the IN register requirements at position pos.
788  */
789 void set_ia32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
790         ia32_attr_t *attr = get_ia32_attr(node);
791         attr->in_req[pos] = req;
792 }
793
794 /**
795  * Returns the register flag of an ia32 node.
796  */
797 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
798         ia32_attr_t *attr = get_ia32_attr(node);
799         return attr->data.flags;
800 }
801
802 /**
803  * Sets the register flag of an ia32 node.
804  */
805 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
806         ia32_attr_t *attr = get_ia32_attr(node);
807         attr->data.flags  = flags;
808 }
809
810 /**
811  * Returns the result register slots of an ia32 node.
812  */
813 const arch_register_t **get_ia32_slots(const ir_node *node) {
814         ia32_attr_t *attr = get_ia32_attr(node);
815         return attr->slots;
816 }
817
818 /**
819  * Sets the number of results.
820  */
821 void set_ia32_n_res(ir_node *node, int n_res) {
822         ia32_attr_t *attr = get_ia32_attr(node);
823         attr->data.n_res  = n_res;
824 }
825
826 /**
827  * Returns the number of results.
828  */
829 int get_ia32_n_res(const ir_node *node) {
830         ia32_attr_t *attr = get_ia32_attr(node);
831         return attr->data.n_res;
832 }
833
834 /**
835  * Returns the flavour of an ia32 node,
836  */
837 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
838         ia32_attr_t *attr = get_ia32_attr(node);
839         return attr->data.op_flav;
840 }
841
842 /**
843  * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
844  */
845 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
846         ia32_attr_t *attr  = get_ia32_attr(node);
847         attr->data.op_flav = op_flav;
848 }
849
850 /**
851  * Returns the projnum code.
852  */
853 pn_Cmp get_ia32_pncode(const ir_node *node) {
854         ia32_attr_t *attr = get_ia32_attr(node);
855         return attr->pn_code;
856 }
857
858 /**
859  * Sets the projnum code
860  */
861 void set_ia32_pncode(ir_node *node, pn_Cmp code) {
862         ia32_attr_t *attr = get_ia32_attr(node);
863         attr->pn_code     = code;
864 }
865
866 /**
867  * Sets the flags for the n'th out.
868  */
869 void set_ia32_out_flags(ir_node *node, arch_irn_flags_t flags, int pos) {
870         ia32_attr_t *attr = get_ia32_attr(node);
871         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
872         attr->out_flags[pos] = flags;
873 }
874
875 /**
876  * Gets the flags for the n'th out.
877  */
878 arch_irn_flags_t get_ia32_out_flags(const ir_node *node, int pos) {
879         ia32_attr_t *attr = get_ia32_attr(node);
880         return pos < (int)attr->data.n_res ? attr->out_flags[pos] : arch_irn_flags_none;
881 }
882
883 /**
884  * Get the list of available execution units.
885  */
886 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node) {
887         ia32_attr_t *attr = get_ia32_attr(node);
888         return attr->exec_units;
889 }
890
891 #ifndef NDEBUG
892
893 /**
894  * Returns the name of the original ir node.
895  */
896 const char *get_ia32_orig_node(const ir_node *node) {
897         ia32_attr_t *attr = get_ia32_attr(node);
898         return attr->orig_node;
899 }
900
901 /**
902  * Sets the name of the original ir node.
903  */
904 void set_ia32_orig_node(ir_node *node, const char *name) {
905         ia32_attr_t *attr = get_ia32_attr(node);
906         attr->orig_node   = name;
907 }
908
909 #endif /* NDEBUG */
910
911 /******************************************************************************************************
912  *                      _       _         _   _           __                  _   _
913  *                     (_)     | |       | | | |         / _|                | | (_)
914  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
915  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
916  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
917  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
918  *     | |
919  *     |_|
920  ******************************************************************************************************/
921
922 /**
923  * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
924  */
925 void copy_ia32_Immop_attr(ir_node *node, ir_node *from) {
926         ia32_immop_type_t immop_type = get_ia32_immop_type(from);
927
928         if(immop_type == ia32_ImmConst) {
929                 set_ia32_Immop_tarval(node, get_ia32_Immop_tarval(from));
930         } else if(immop_type == ia32_ImmSymConst) {
931                 set_ia32_Immop_symconst(node, get_ia32_Immop_symconst(from));
932         } else {
933                 ia32_attr_t *attr = get_ia32_attr(node);
934                 assert(immop_type == ia32_ImmNone);
935                 attr->data.imm_tp = ia32_ImmNone;
936         }
937 }
938
939 /**
940  * Copy the attributes from a Firm Const/SymConst to an ia32_Const
941  */
942 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
943         assert(is_ia32_Cnst(ia32_cnst) && "Need ia32_Const to set Const attr");
944
945         switch (get_irn_opcode(cnst)) {
946                 case iro_Const:
947                         set_ia32_Const_tarval(ia32_cnst, get_Const_tarval(cnst));
948                         break;
949                 case iro_SymConst:
950                         assert(get_SymConst_kind(cnst) == symconst_addr_ent);
951                         set_ia32_Immop_symconst(ia32_cnst, get_SymConst_entity(cnst));
952                         break;
953                 case iro_Unknown:
954                         assert(0 && "Unknown Const NYI");
955                         break;
956                 default:
957                         assert(0 && "Cannot create ia32_Const for this opcode");
958         }
959 }
960
961 void set_ia32_Const_tarval(ir_node *ia32_cnst, tarval *tv) {
962         if(mode_is_reference(get_tarval_mode(tv))) {
963                 if(tarval_is_null(tv)) {
964                         tv = get_tarval_null(mode_Iu);
965                 } else {
966                         panic("Can't convert reference tarval to mode_Iu at %+F", ia32_cnst);
967                 }
968         } else {
969                 tv = tarval_convert_to(tv, mode_Iu);
970         }
971
972         assert(tv != get_tarval_bad() && tv != get_tarval_undefined()
973                         && tv != NULL);
974         set_ia32_Immop_tarval(ia32_cnst, tv);
975 }
976
977
978 /**
979  * Sets the AddrMode(S|D) attribute
980  */
981 void set_ia32_AddrMode(ir_node *node, char direction) {
982         ia32_attr_t *attr = get_ia32_attr(node);
983
984         switch (direction) {
985                 case 'D':
986                         attr->data.tp = ia32_AddrModeD;
987                         break;
988                 case 'S':
989                         attr->data.tp = ia32_AddrModeS;
990                         break;
991                 default:
992                         assert(0 && "wrong AM type");
993         }
994 }
995
996 /**
997  * Returns whether or not the node is an immediate operation with Const.
998  */
999 int is_ia32_ImmConst(const ir_node *node) {
1000         ia32_attr_t *attr = get_ia32_attr(node);
1001         return (attr->data.imm_tp == ia32_ImmConst);
1002 }
1003
1004 /**
1005  * Returns whether or not the node is an immediate operation with SymConst.
1006  */
1007 int is_ia32_ImmSymConst(const ir_node *node) {
1008         ia32_attr_t *attr = get_ia32_attr(node);
1009         return (attr->data.imm_tp == ia32_ImmSymConst);
1010 }
1011
1012 /**
1013  * Returns whether or not the node is an AddrModeS node.
1014  */
1015 int is_ia32_AddrModeS(const ir_node *node) {
1016         ia32_attr_t *attr = get_ia32_attr(node);
1017         return (attr->data.tp == ia32_AddrModeS);
1018 }
1019
1020 /**
1021  * Returns whether or not the node is an AddrModeD node.
1022  */
1023 int is_ia32_AddrModeD(const ir_node *node) {
1024         ia32_attr_t *attr = get_ia32_attr(node);
1025         return (attr->data.tp == ia32_AddrModeD);
1026 }
1027
1028 /**
1029  * Checks if node is a Load or xLoad/vfLoad.
1030  */
1031 int is_ia32_Ld(const ir_node *node) {
1032         int op = get_ia32_irn_opcode(node);
1033         return op == iro_ia32_Load || op == iro_ia32_xLoad || op == iro_ia32_vfld || op == iro_ia32_fld;
1034 }
1035
1036 /**
1037  * Checks if node is a Store or xStore/vfStore.
1038  */
1039 int is_ia32_St(const ir_node *node) {
1040         int op = get_ia32_irn_opcode(node);
1041         return op == iro_ia32_Store || op == iro_ia32_xStore || op == iro_ia32_vfst || op == iro_ia32_fst || op == iro_ia32_fstp;
1042 }
1043
1044 /**
1045  * Checks if node is a Const or xConst/vfConst.
1046  */
1047 int is_ia32_Cnst(const ir_node *node) {
1048         int op = get_ia32_irn_opcode(node);
1049         return op == iro_ia32_Const || op == iro_ia32_xConst || op == iro_ia32_vfConst;
1050 }
1051
1052 /**
1053  * Returns the name of the OUT register at position pos.
1054  */
1055 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
1056         ia32_attr_t *attr = get_ia32_attr(node);
1057
1058         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1059         assert(attr->slots[pos]  && "No register assigned");
1060
1061         return arch_register_get_name(attr->slots[pos]);
1062 }
1063
1064 /**
1065  * Returns the index of the OUT register at position pos within its register class.
1066  */
1067 int get_ia32_out_regnr(const ir_node *node, int pos) {
1068         ia32_attr_t *attr = get_ia32_attr(node);
1069
1070         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1071         assert(attr->slots[pos]  && "No register assigned");
1072
1073         return arch_register_get_index(attr->slots[pos]);
1074 }
1075
1076 /**
1077  * Returns the OUT register at position pos.
1078  */
1079 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1080         ia32_attr_t *attr = get_ia32_attr(node);
1081
1082         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1083         assert(attr->slots[pos]  && "No register assigned");
1084
1085         return attr->slots[pos];
1086 }
1087
1088 /**
1089  * Initializes the nodes attributes.
1090  */
1091 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
1092                           const arch_register_req_t **in_reqs,
1093                           const arch_register_req_t **out_reqs,
1094                           const be_execution_unit_t ***execution_units,
1095                           int n_res, unsigned latency)
1096 {
1097         ia32_attr_t *attr = get_ia32_attr(node);
1098
1099         set_ia32_flags(node, flags);
1100         set_ia32_in_req_all(node, in_reqs);
1101         set_ia32_out_req_all(node, out_reqs);
1102         set_ia32_latency(node, latency);
1103         set_ia32_n_res(node, n_res);
1104
1105         attr->exec_units = execution_units;
1106
1107         attr->out_flags = NEW_ARR_D(int, get_irg_obstack(get_irn_irg(node)), n_res);
1108         memset(attr->out_flags, 0, n_res * sizeof(attr->out_flags[0]));
1109
1110         memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
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(ia32_attr_t *a, ia32_attr_t *b) {
1138         if (a->data.tp != b->data.tp
1139                         || a->data.imm_tp != b->data.imm_tp)
1140                 return 1;
1141
1142         if (a->data.imm_tp == ia32_ImmConst
1143                         && a->cnst_val.tv != b->cnst_val.tv)
1144                 return 1;
1145
1146         if (a->data.imm_tp == ia32_ImmSymConst
1147                         && a->cnst_val.sc != b->cnst_val.sc)
1148                 return 1;
1149
1150         if (a->data.am_flavour != b->data.am_flavour
1151                 || a->data.am_scale != b->data.am_scale
1152                 || a->data.offs_sign != b->data.offs_sign
1153                 || a->data.am_sc_sign != b->data.am_sc_sign
1154                 || a->am_offs != b->am_offs
1155                 || a->am_sc != b->am_sc
1156                         || a->ls_mode != b->ls_mode)
1157                 return 1;
1158
1159         if (a->data.use_frame != b->data.use_frame
1160                 || a->data.use_frame != b->data.use_frame
1161                 || a->frame_ent != b->frame_ent)
1162                 return 1;
1163
1164         if(a->pn_code != b->pn_code)
1165                 return 1;
1166
1167         if (a->data.tp != b->data.tp
1168                 || a->data.op_flav != b->data.op_flav)
1169                 return 1;
1170
1171         return 0;
1172 }
1173
1174 /* copies the ia32 attributes */
1175 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node) {
1176         ia32_attr_t *attr_old = get_ia32_attr(old_node);
1177         ia32_attr_t *attr_new = get_ia32_attr(new_node);
1178
1179         /* copy the attributes */
1180         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1181
1182         /* copy out flags */
1183         attr_new->out_flags =
1184                 DUP_ARR_D(int, get_irg_obstack(get_irn_irg(new_node)), attr_old->out_flags);
1185 }
1186
1187 /**
1188  * Registers the ia32_copy_attr function for all ia32 opcodes.
1189  */
1190 void ia32_register_copy_attr_func(void) {
1191         unsigned i, f = get_ia32_opcode_first(), l = get_ia32_opcode_last();
1192
1193         for (i = f; i < l; i++) {
1194                 ir_op *op = get_irp_opcode(i);
1195                 op->ops.copy_attr = ia32_copy_attr;
1196         }
1197 }
1198
1199 /* Include the generated constructor functions */
1200 #include "gen_ia32_new_nodes.c.inl"