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