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