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