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