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