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