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