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