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