5e8c80243bd37c7fe844414911e3063e95d7ebec
[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 #include "gen_ia32_new_nodes.h"
37
38 #ifdef obstack_chunk_alloc
39 # undef obstack_chunk_alloc
40 # define obstack_chunk_alloc xmalloc
41 #else
42 # define obstack_chunk_alloc xmalloc
43 # define obstack_chunk_free free
44 #endif
45
46 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
47
48 /***********************************************************************************
49  *      _                                   _       _             __
50  *     | |                                 (_)     | |           / _|
51  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
52  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
53  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
54  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
55  *                       | |
56  *                       |_|
57  ***********************************************************************************/
58
59 /**
60  * Returns the name of a SymConst.
61  * @param symc  the SymConst
62  * @return name of the SymConst
63  */
64 const char *get_sc_name(ir_node *symc) {
65         if (get_irn_opcode(symc) != iro_SymConst)
66                 return "NONE";
67
68         switch (get_SymConst_kind(symc)) {
69                 case symconst_addr_name:
70                         return get_id_str(get_SymConst_name(symc));
71
72                 case symconst_addr_ent:
73                         return get_entity_ld_name(get_SymConst_entity(symc));
74
75                 default:
76                         assert(0 && "Unsupported SymConst");
77         }
78
79         return NULL;
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 (get_ia32_cnst(n)) {
188                                 char *pref = "";
189
190                                 if (get_ia32_sc(n)) {
191                                         pref = "SymC ";
192                                 }
193
194                                 fprintf(F, "[%s%s]", pref, get_ia32_cnst(n));
195                         }
196
197                         if (! is_ia32_Lea(n)) {
198                                 if (is_ia32_AddrModeS(n)) {
199                                         fprintf(F, "[AM S] ");
200                                 }
201                                 else if (is_ia32_AddrModeD(n)) {
202                                         fprintf(F, "[AM D] ");
203                                 }
204                         }
205
206                         break;
207
208                 case dump_node_info_txt:
209                         n_res = get_ia32_n_res(n);
210                         fprintf(F, "=== IA32 attr begin ===\n");
211
212                         /* dump IN requirements */
213                         if (get_irn_arity(n) > 0) {
214                                 reqs = get_ia32_in_req_all(n);
215                                 dump_reg_req(F, n, reqs, 0);
216                         }
217
218                         /* dump OUT requirements */
219                         if (n_res > 0) {
220                                 reqs = get_ia32_out_req_all(n);
221                                 dump_reg_req(F, n, reqs, 1);
222                         }
223
224                         /* dump assigned registers */
225                         slots = get_ia32_slots(n);
226                         if (slots && n_res > 0) {
227                                 for (i = 0; i < n_res; i++) {
228                                         fprintf(F, "reg #%d = %s\n", i, slots[i] ? slots[i]->name : "n/a");
229                                 }
230                                 fprintf(F, "\n");
231                         }
232
233                         /* dump op type */
234                         fprintf(F, "op = ");
235                         switch (get_ia32_op_type(n)) {
236                                 case ia32_Normal:
237                                         fprintf(F, "Normal");
238                                         break;
239                                 case ia32_Const:
240                                         fprintf(F, "Const");
241                                         break;
242                                 case ia32_SymConst:
243                                         fprintf(F, "SymConst");
244                                         break;
245                                 case ia32_AddrModeD:
246                                         fprintf(F, "AM Dest (Load+Store)");
247                                         break;
248                                 case ia32_AddrModeS:
249                                         fprintf(F, "AM Source (Load)");
250                                         break;
251                                 default:
252                                         fprintf(F, "unknown (%d)", get_ia32_op_type(n));
253                                         break;
254                         }
255                         fprintf(F, "\n");
256
257
258                         /* dump supported am */
259                         fprintf(F, "AM support = ");
260                         switch (get_ia32_am_support(n)) {
261                                 case ia32_am_None:
262                                         fprintf(F, "none");
263                                         break;
264                                 case ia32_am_Source:
265                                         fprintf(F, "source only (Load)");
266                                         break;
267                                 case ia32_am_Dest:
268                                         fprintf(F, "dest only (Load+Store)");
269                                         break;
270                                 case ia32_am_Full:
271                                         fprintf(F, "full");
272                                         break;
273                                 default:
274                                         fprintf(F, "unknown (%d)", get_ia32_am_support(n));
275                                         break;
276                         }
277                         fprintf(F, "\n");
278
279                         /* dump am flavour */
280                         fprintf(F, "AM flavour =");
281                         am_flav = get_ia32_am_flavour(n);
282                         if (am_flav == ia32_am_N) {
283                                 fprintf(F, " none");
284                         }
285                         else {
286                                 if (am_flav & ia32_O) {
287                                         fprintf(F, " O");
288                                 }
289                                 if (am_flav & ia32_B) {
290                                         fprintf(F, " B");
291                                 }
292                                 if (am_flav & ia32_I) {
293                                         fprintf(F, " I");
294                                 }
295                                 if (am_flav & ia32_S) {
296                                         fprintf(F, " S");
297                                 }
298                         }
299                         fprintf(F, " (%d)\n", am_flav);
300
301                         /* dump AM offset */
302                         fprintf(F, "AM offset = ");
303                         if (get_ia32_am_offs(n)) {
304                                 fprintf(F, "%s", get_ia32_am_offs(n));
305                         }
306                         else {
307                                 fprintf(F, "n/a");
308                         }
309                         fprintf(F, "\n");
310
311                         /* dump AM scale */
312                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
313
314                         /* dump pn code */
315                         fprintf(F, "pn_code = %ld\n", get_ia32_pncode(n));
316
317                         /* dump n_res */
318                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
319
320                         /* dump use_frame */
321                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
322
323                         /* commutative */
324                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
325
326                         /* dump flags */
327                         fprintf(F, "flags =");
328                         flags = get_ia32_flags(n);
329                         if (flags == arch_irn_flags_none) {
330                                 fprintf(F, " none");
331                         }
332                         else {
333                                 if (flags & arch_irn_flags_dont_spill) {
334                                         fprintf(F, " unspillable");
335                                 }
336                                 if (flags & arch_irn_flags_rematerializable) {
337                                         fprintf(F, " remat");
338                                 }
339                                 if (flags & arch_irn_flags_ignore) {
340                                         fprintf(F, " ignore");
341                                 }
342                         }
343                         fprintf(F, " (%d)\n", flags);
344
345                         /* dump frame entity */
346                         fprintf(F, "frame entity = ");
347                         if (get_ia32_frame_ent(n)) {
348                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
349                         }
350                         else {
351                                 fprintf(F, "n/a");
352                         }
353                         fprintf(F, "\n");
354
355 #ifndef NDEBUG
356                         /* dump original ir node name */
357                         fprintf(F, "orig node = ");
358                         if (get_ia32_orig_node(n)) {
359                                 fprintf(F, "%s", get_ia32_orig_node(n));
360                         }
361                         else {
362                                 fprintf(F, "n/a");
363                         }
364                         fprintf(F, "\n");
365 #endif /* NDEBUG */
366
367                         fprintf(F, "=== IA32 attr end ===\n");
368                         /* end of: case dump_node_info_txt */
369                         break;
370         }
371
372         return bad;
373 }
374
375
376
377 /***************************************************************************************************
378  *        _   _                   _       __        _                    _   _               _
379  *       | | | |                 | |     / /       | |                  | | | |             | |
380  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
381  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
382  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
383  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
384  *                                        __/ |
385  *                                       |___/
386  ***************************************************************************************************/
387
388  static char *copy_str(const char *src) {
389          size_t l = strlen(src) + 1;
390          char *dst = xmalloc(l);
391          strncpy(dst, src, l);
392          dst[l - 1] = '\0';
393          return dst;
394  }
395
396  static char *set_cnst_from_tv(char *cnst, tarval *tv) {
397          int l = 64;
398          if (cnst) {
399                  free(cnst);
400          }
401
402          cnst = xmalloc(l);
403          assert(tarval_snprintf(cnst, l, tv));
404          cnst[l - 1] = 0;
405          return cnst;
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 supported addrmode of an ia32 node
435  */
436 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
437         ia32_attr_t *attr = get_ia32_attr(node);
438         return attr->data.am_support;
439 }
440
441 /**
442  * Sets the supported addrmode of an ia32 node
443  */
444 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
445         ia32_attr_t *attr = get_ia32_attr(node);
446         attr->data.am_support  = am_tp;
447 }
448
449 /**
450  * Gets the addrmode flavour of an ia32 node
451  */
452 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
453         ia32_attr_t *attr = get_ia32_attr(node);
454         return attr->data.am_flavour;
455 }
456
457 /**
458  * Sets the addrmode flavour of an ia32 node
459  */
460 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
461         ia32_attr_t *attr = get_ia32_attr(node);
462         attr->data.am_flavour  = am_flavour;
463 }
464
465 /**
466  * Joins all offsets to one string with adds.
467  */
468 char *get_ia32_am_offs(const ir_node *node) {
469         ia32_attr_t *attr = get_ia32_attr(node);
470         char        *res  = NULL;
471         int          size;
472
473         if (! attr->am_offs) {
474                 return NULL;
475         }
476
477         size = obstack_object_size(attr->am_offs);
478         if (size > 0) {
479                 res    = xmalloc(size + 2);
480                 res[0] = attr->data.offs_sign ? '-' : '+';
481                 memcpy(&res[1], obstack_base(attr->am_offs), size);
482                 res[size + 1] = '\0';
483         }
484         return res;
485 }
486
487 /**
488  * Add an offset for addrmode.
489  */
490 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
491         ia32_attr_t *attr = get_ia32_attr(node);
492
493         if (! offset)
494                 return;
495
496         /* offset could already have an explicit sign */
497         /* -> supersede op if necessary               */
498         if (offset[0] == '-' || offset[0] == '+') {
499                 if (offset[0] == '-') {
500                         op = (op == '-') ? '+' : '-';
501                 }
502
503                 /* skip explicit sign */
504                 offset++;
505         }
506
507         if (! attr->am_offs) {
508                 /* obstack is not initialized */
509                 attr->am_offs = xcalloc(1, sizeof(*(attr->am_offs)));
510                 obstack_init(attr->am_offs);
511
512                 attr->data.offs_sign = (op == '-') ? 1 : 0;
513         }
514         else {
515                 /* If obstack is initialized, connect the new offset with op */
516                 obstack_printf(attr->am_offs, "%c", op);
517         }
518
519         obstack_printf(attr->am_offs, "%s", offset);
520 }
521
522 /**
523  * Add an offset for addrmode.
524  */
525 void add_ia32_am_offs(ir_node *node, char *offset) {
526         extend_ia32_am_offs(node, offset, '+');
527 }
528
529 /**
530  * Sub an offset for addrmode.
531  */
532 void sub_ia32_am_offs(ir_node *node, char *offset) {
533         extend_ia32_am_offs(node, offset, '-');
534 }
535
536 /**
537  * Gets the addr mode const.
538  */
539 int get_ia32_am_scale(const ir_node *node) {
540         ia32_attr_t *attr = get_ia32_attr(node);
541         return attr->data.am_scale;
542 }
543
544 /**
545  * Sets the index register scale for addrmode.
546  */
547 void set_ia32_am_scale(ir_node *node, int scale) {
548         ia32_attr_t *attr   = get_ia32_attr(node);
549         attr->data.am_scale = scale;
550 }
551
552 /**
553  * Return the tarval of an immediate operation or NULL in case of SymConst
554  */
555 tarval *get_ia32_Immop_tarval(const ir_node *node) {
556         ia32_attr_t *attr = get_ia32_attr(node);
557     return attr->tv;
558 }
559
560 /**
561  * Sets the attributes of an immediate operation to the specified tarval
562  */
563 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
564         ia32_attr_t *attr = get_ia32_attr(node);
565         attr->tv          = tv;
566         attr->cnst        = set_cnst_from_tv(attr->cnst, attr->tv);
567 }
568
569 /**
570  * Return the sc attribute.
571  */
572 const char *get_ia32_sc(const ir_node *node) {
573         ia32_attr_t *attr = get_ia32_attr(node);
574         return attr->sc;
575 }
576
577 /**
578  * Sets the sc attribute.
579  */
580 void set_ia32_sc(ir_node *node, const char *sc) {
581         ia32_attr_t *attr = get_ia32_attr(node);
582         attr->sc          = copy_str(sc);
583
584         if (attr->cnst) {
585                 free(attr->cnst);
586         }
587         attr->cnst = attr->sc;
588 }
589
590 /**
591  * Gets the string representation of the internal const (tv or symconst)
592  */
593 char *get_ia32_cnst(const ir_node *node) {
594         ia32_attr_t *attr = get_ia32_attr(node);
595         return attr->cnst;
596 }
597
598 /**
599  * Sets the string representation of the internal const.
600  */
601 void set_ia32_cnst(ir_node *node, char *cnst) {
602         ia32_attr_t *attr = get_ia32_attr(node);
603         attr->cnst = cnst;
604 }
605
606 /**
607  * Sets the uses_frame flag.
608  */
609 void set_ia32_use_frame(ir_node *node) {
610         ia32_attr_t *attr    = get_ia32_attr(node);
611         attr->data.use_frame = 1;
612 }
613
614 /**
615  * Clears the uses_frame flag.
616  */
617 void clear_ia32_use_frame(ir_node *node) {
618         ia32_attr_t *attr    = get_ia32_attr(node);
619         attr->data.use_frame = 0;
620 }
621
622 /**
623  * Gets the uses_frame flag.
624  */
625 int is_ia32_use_frame(const ir_node *node) {
626         ia32_attr_t *attr = get_ia32_attr(node);
627         return attr->data.use_frame;
628 }
629
630 /**
631  * Sets node to commutative.
632  */
633 void set_ia32_commutative(ir_node *node) {
634         ia32_attr_t *attr         = get_ia32_attr(node);
635         attr->data.is_commutative = 1;
636 }
637
638 /**
639  * Sets node to non-commutative.
640  */
641 void clear_ia32_commutative(ir_node *node) {
642         ia32_attr_t *attr         = get_ia32_attr(node);
643         attr->data.is_commutative = 0;
644 }
645
646 /**
647  * Checks if node is commutative.
648  */
649 int is_ia32_commutative(const ir_node *node) {
650         ia32_attr_t *attr = get_ia32_attr(node);
651         return attr->data.is_commutative;
652 }
653
654 /**
655  * Gets the mode of the stored/loaded value (only set for Store/Load)
656  */
657 ir_mode *get_ia32_ls_mode(const ir_node *node) {
658         ia32_attr_t *attr = get_ia32_attr(node);
659         return attr->ls_mode;
660 }
661
662 /**
663  * Sets the mode of the stored/loaded value (only set for Store/Load)
664  */
665 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
666         ia32_attr_t *attr = get_ia32_attr(node);
667         attr->ls_mode     = mode;
668 }
669
670 /**
671  * Gets the mode of the result.
672  */
673 ir_mode *get_ia32_res_mode(const ir_node *node) {
674         ia32_attr_t *attr = get_ia32_attr(node);
675         return attr->res_mode;
676 }
677
678 /**
679  * Sets the mode of the result.
680  */
681 void set_ia32_res_mode(ir_node *node, ir_mode *mode) {
682         ia32_attr_t *attr = get_ia32_attr(node);
683         attr->res_mode    = mode;
684 }
685
686 /**
687  * Gets the frame entity assigned to this node;
688  */
689 entity *get_ia32_frame_ent(const ir_node *node) {
690         ia32_attr_t *attr = get_ia32_attr(node);
691         return attr->frame_ent;
692 }
693
694 /**
695  * Sets the frame entity for this node;
696  */
697 void set_ia32_frame_ent(ir_node *node, entity *ent) {
698         ia32_attr_t *attr = get_ia32_attr(node);
699         attr->frame_ent   = ent;
700 }
701
702 /**
703  * Returns the argument register requirements of an ia32 node.
704  */
705 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
706         ia32_attr_t *attr = get_ia32_attr(node);
707         return attr->in_req;
708 }
709
710 /**
711  * Sets the argument register requirements of an ia32 node.
712  */
713 void set_ia32_in_req_all(ir_node *node, const ia32_register_req_t **reqs) {
714         ia32_attr_t *attr = get_ia32_attr(node);
715         attr->in_req      = reqs;
716 }
717
718 /**
719  * Returns the result register requirements of an ia32 node.
720  */
721 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
722         ia32_attr_t *attr = get_ia32_attr(node);
723         return attr->out_req;
724 }
725
726 /**
727  * Sets the result register requirements of an ia32 node.
728  */
729 void set_ia32_out_req_all(ir_node *node, const ia32_register_req_t **reqs) {
730         ia32_attr_t *attr = get_ia32_attr(node);
731         attr->out_req     = reqs;
732 }
733
734 /**
735  * Returns the argument register requirement at position pos of an ia32 node.
736  */
737 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
738         ia32_attr_t *attr = get_ia32_attr(node);
739         return attr->in_req[pos];
740 }
741
742 /**
743  * Returns the result register requirement at position pos of an ia32 node.
744  */
745 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
746         ia32_attr_t *attr = get_ia32_attr(node);
747         return attr->out_req[pos];
748 }
749
750 /**
751  * Sets the OUT register requirements at position pos.
752  */
753 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
754         ia32_attr_t *attr  = get_ia32_attr(node);
755         attr->out_req[pos] = req;
756 }
757
758 /**
759  * Sets the IN register requirements at position pos.
760  */
761 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
762         ia32_attr_t *attr = get_ia32_attr(node);
763         attr->in_req[pos] = req;
764 }
765
766 /**
767  * Returns the register flag of an ia32 node.
768  */
769 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
770         ia32_attr_t *attr = get_ia32_attr(node);
771         return attr->data.flags;
772 }
773
774 /**
775  * Sets the register flag of an ia32 node.
776  */
777 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
778         ia32_attr_t *attr = get_ia32_attr(node);
779         attr->data.flags  = flags;
780 }
781
782 /**
783  * Returns the result register slots of an ia32 node.
784  */
785 const arch_register_t **get_ia32_slots(const ir_node *node) {
786         ia32_attr_t *attr = get_ia32_attr(node);
787         return attr->slots;
788 }
789
790 /**
791  * Sets the number of results.
792  */
793 void set_ia32_n_res(ir_node *node, int n_res) {
794         ia32_attr_t *attr = get_ia32_attr(node);
795         attr->data.n_res  = n_res;
796 }
797
798 /**
799  * Returns the number of results.
800  */
801 int get_ia32_n_res(const ir_node *node) {
802         ia32_attr_t *attr = get_ia32_attr(node);
803         return attr->data.n_res;
804 }
805
806 /**
807  * Returns the flavour of an ia32 node,
808  */
809 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
810         ia32_attr_t *attr = get_ia32_attr(node);
811         return attr->data.op_flav;
812 }
813
814 /**
815  * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
816  */
817 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
818         ia32_attr_t *attr  = get_ia32_attr(node);
819         attr->data.op_flav = op_flav;
820 }
821
822 /**
823  * Returns the projnum code.
824  */
825 long get_ia32_pncode(const ir_node *node) {
826         ia32_attr_t *attr = get_ia32_attr(node);
827         return attr->pn_code;
828 }
829
830 /**
831  * Sets the projnum code
832  */
833 void set_ia32_pncode(ir_node *node, long code) {
834         ia32_attr_t *attr = get_ia32_attr(node);
835         attr->pn_code     = code;
836 }
837
838 #ifndef NDEBUG
839
840 /**
841  * Returns the name of the original ir node.
842  */
843 const char *get_ia32_orig_node(const ir_node *node) {
844         ia32_attr_t *attr = get_ia32_attr(node);
845         return attr->orig_node;
846 }
847
848 /**
849  * Sets the name of the original ir node.
850  */
851 void set_ia32_orig_node(ir_node *node, const char *name) {
852         ia32_attr_t *attr = get_ia32_attr(node);
853         attr->orig_node   = name;
854 }
855
856 #endif /* NDEBUG */
857
858 /******************************************************************************************************
859  *                      _       _         _   _           __                  _   _
860  *                     (_)     | |       | | | |         / _|                | | (_)
861  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
862  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
863  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
864  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
865  *     | |
866  *     |_|
867  ******************************************************************************************************/
868
869 /**
870  * Gets the type of an ia32_Const.
871  */
872 unsigned get_ia32_Const_type(const ir_node *node) {
873         ia32_attr_t *attr = get_ia32_attr(node);
874
875         assert(is_ia32_Cnst(node) && "Need ia32_Const to get type");
876
877         return attr->data.tp;
878 }
879
880 /**
881  * Sets the type of an ia32_Const.
882  */
883 void set_ia32_Const_type(ir_node *node, int type) {
884         ia32_attr_t *attr = get_ia32_attr(node);
885
886         assert(is_ia32_Cnst(node) && "Need ia32_Const to set type");
887         assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
888
889         attr->data.tp = type;
890 }
891
892 /**
893  * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
894  */
895 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
896         ia32_attr_t *na = get_ia32_attr(node);
897         ia32_attr_t *ca = get_ia32_attr(cnst);
898
899         assert(is_ia32_Cnst(cnst) && "Need ia32_Const to set Immop attr");
900
901         na->tv = ca->tv;
902
903         if (ca->sc) {
904                 na->sc   = copy_str(ca->sc);
905                 na->cnst = na->sc;
906         }
907         else {
908                 na->cnst = set_cnst_from_tv(na->cnst, na->tv);
909                 na->sc   = NULL;
910         }
911 }
912
913 /**
914  * Copy the attributes from Immop to an Immop
915  */
916 void copy_ia32_Immop_attr(ir_node *node, ir_node *src)
917 {
918         ia32_attr_t *na = get_ia32_attr(node);
919         ia32_attr_t *ca = get_ia32_attr(src);
920
921         assert(get_ia32_cnst(src) != NULL);
922         na->tv = ca->tv;
923
924         if (ca->sc) {
925                 na->sc   = copy_str(ca->sc);
926                 na->cnst = na->sc;
927         }
928         else {
929                 na->cnst = set_cnst_from_tv(na->cnst, na->tv);
930                 na->sc   = NULL;
931         }
932 }
933
934 /**
935  * Copy the attributes from a Const to an ia32_Const
936  */
937 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
938         ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
939
940         assert(is_ia32_Cnst(ia32_cnst) && "Need ia32_Const to set Const attr");
941
942         switch (get_irn_opcode(cnst)) {
943                 case iro_Const:
944                         attr->data.tp = ia32_Const;
945                         attr->tv      = get_Const_tarval(cnst);
946                         attr->cnst    = set_cnst_from_tv(attr->cnst, attr->tv);
947                         break;
948                 case iro_SymConst:
949                         attr->data.tp = ia32_SymConst;
950                         attr->tv      = NULL;
951                         attr->sc      = copy_str(get_sc_name(cnst));
952                         attr->cnst    = attr->sc;
953                         break;
954                 case iro_Unknown:
955                         assert(0 && "Unknown Const NYI");
956                         break;
957                 default:
958                         assert(0 && "Cannot create ia32_Const for this opcode");
959         }
960 }
961
962 /**
963  * Sets the AddrMode(S|D) attribute
964  */
965 void set_ia32_AddrMode(ir_node *node, char direction) {
966         ia32_attr_t *attr = get_ia32_attr(node);
967
968         switch (direction) {
969                 case 'D':
970                         attr->data.tp = ia32_AddrModeD;
971                         break;
972                 case 'S':
973                         attr->data.tp = ia32_AddrModeS;
974                         break;
975                 default:
976                         assert(0 && "wrong AM type");
977         }
978 }
979
980 /**
981  * Returns whether or not the node is an AddrModeS node.
982  */
983 int is_ia32_AddrModeS(const ir_node *node) {
984         ia32_attr_t *attr = get_ia32_attr(node);
985         return (attr->data.tp == ia32_AddrModeS);
986 }
987
988 /**
989  * Returns whether or not the node is an AddrModeD node.
990  */
991 int is_ia32_AddrModeD(const ir_node *node) {
992         ia32_attr_t *attr = get_ia32_attr(node);
993         return (attr->data.tp == ia32_AddrModeD);
994 }
995
996 /**
997  * Checks if node is a Load or fLoad.
998  */
999 int is_ia32_Ld(const ir_node *node) {
1000         return is_ia32_Load(node) || is_ia32_fLoad(node);
1001 }
1002
1003 /**
1004  * Checks if node is a Store or fStore.
1005  */
1006 int is_ia32_St(const ir_node *node) {
1007         return is_ia32_Store(node) || is_ia32_fStore(node);
1008 }
1009
1010 /**
1011  * Checks if node is a Const or fConst.
1012  */
1013 int is_ia32_Cnst(const ir_node *node) {
1014         return is_ia32_Const(node) || is_ia32_fConst(node);
1015 }
1016
1017 /**
1018  * Returns the name of the OUT register at position pos.
1019  */
1020 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
1021         ia32_attr_t *attr = get_ia32_attr(node);
1022
1023         assert(is_ia32_irn(node) && "Not an ia32 node.");
1024         assert(pos < attr->data.n_res && "Invalid OUT position.");
1025         assert(attr->slots[pos]  && "No register assigned");
1026
1027         return arch_register_get_name(attr->slots[pos]);
1028 }
1029
1030 /**
1031  * Returns the index of the OUT register at position pos within its register class.
1032  */
1033 int get_ia32_out_regnr(const ir_node *node, int pos) {
1034         ia32_attr_t *attr = get_ia32_attr(node);
1035
1036         assert(is_ia32_irn(node) && "Not an ia32 node.");
1037         assert(pos < attr->data.n_res && "Invalid OUT position.");
1038         assert(attr->slots[pos]  && "No register assigned");
1039
1040         return arch_register_get_index(attr->slots[pos]);
1041 }
1042
1043 /**
1044  * Returns the OUT register at position pos.
1045  */
1046 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1047         ia32_attr_t *attr = get_ia32_attr(node);
1048
1049         assert(is_ia32_irn(node) && "Not an ia32 node.");
1050         assert(pos < attr->data.n_res && "Invalid OUT position.");
1051         assert(attr->slots[pos]  && "No register assigned");
1052
1053         return attr->slots[pos];
1054 }
1055
1056 /**
1057  * Allocates num register slots for node.
1058  */
1059 void alloc_ia32_reg_slots(ir_node *node, int num) {
1060         ia32_attr_t *attr = get_ia32_attr(node);
1061
1062         if (num) {
1063                 attr->slots = xcalloc(num, sizeof(attr->slots[0]));
1064         }
1065         else {
1066                 attr->slots = NULL;
1067         }
1068
1069         attr->data.n_res = num;
1070 }
1071
1072 /**
1073  * Initializes the nodes attributes.
1074  */
1075 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags, const ia32_register_req_t **in_reqs,
1076                                                   const ia32_register_req_t **out_reqs, int n_res)
1077 {
1078         set_ia32_flags(node, flags);
1079         set_ia32_in_req_all(node, in_reqs);
1080         set_ia32_out_req_all(node, out_reqs);
1081         alloc_ia32_reg_slots(node, n_res);
1082 }
1083
1084 /***************************************************************************************
1085  *                  _                            _                   _
1086  *                 | |                          | |                 | |
1087  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1088  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1089  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1090  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1091  *
1092  ***************************************************************************************/
1093
1094 /* default compare operation to compare immediate ops */
1095 int ia32_compare_immop_attr(ia32_attr_t *a, ia32_attr_t *b) {
1096         if (a->data.tp == b->data.tp) {
1097                 if (! (a->cnst && b->cnst))
1098                         return 1;
1099
1100                 return strcmp(a->cnst, b->cnst);
1101         }
1102
1103         return 1;
1104 }
1105
1106 /* copies the ia32 attributes */
1107 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node) {
1108         ia32_attr_t    *attr_old = get_ia32_attr(old_node);
1109         ia32_attr_t    *attr_new = get_ia32_attr(new_node);
1110         int             n_res    = get_ia32_n_res(old_node);
1111
1112         /* copy the attributes */
1113         memcpy(attr_new, attr_old, sizeof(*attr_new));
1114
1115         /* copy the register slots */
1116         attr_new->slots = NEW_ARR_D(arch_register_t *, get_irg_obstack(get_irn_irg(new_node)), n_res);
1117         memcpy((void *)attr_new->slots, (void *)attr_old->slots, sizeof(attr_new->slots[0]) * n_res);
1118 }
1119
1120 void ia32_register_copy_attr_func(void) {
1121         unsigned i, f = get_ia32_opcode_first(), l = get_ia32_opcode_last();
1122
1123         for (i = f; i < l; i++) {
1124                 ir_op *op = get_irp_opcode(i);
1125                 op->ops.copy_attr = ia32_copy_attr;
1126         }
1127 }
1128
1129 static void ia32_register_additional_opcodes(int n) {
1130         /* we don't need any additional opcodes */
1131 }
1132
1133 /* Include the generated constructor functions */
1134 #include "gen_ia32_new_nodes.c.inl"