- Properly support AM for push and pop nodes
[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 ia32 assembler irg.
4  * @author Christian Wuerdig
5  * $Id$
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #ifdef HAVE_MALLOC_H
13 #include <malloc.h>
14 #endif
15
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19
20 #include <stdlib.h>
21
22 #include "irprog_t.h"
23 #include "irgraph_t.h"
24 #include "irnode_t.h"
25 #include "irmode_t.h"
26 #include "ircons_t.h"
27 #include "iropt_t.h"
28 #include "irop.h"
29 #include "firm_common_t.h"
30 #include "irvrfy_t.h"
31 #include "irprintf.h"
32
33 #include "../bearch.h"
34
35 #include "ia32_nodes_attr.h"
36 #include "ia32_new_nodes.h"
37 #include "gen_ia32_regalloc_if.h"
38
39 /**
40  * Returns the ident of a SymConst.
41  * @param symc  The SymConst
42  * @return The ident of the SymConst
43  */
44 static ident *get_sc_ident(ir_node *symc) {
45         entity  *ent;
46         ir_type *owner;
47         ident   *id;
48
49         switch (get_SymConst_kind(symc)) {
50                 case symconst_addr_name:
51                         return get_SymConst_name(symc);
52
53                 case symconst_addr_ent:
54                         ent   = get_SymConst_entity(symc);
55                         owner = get_entity_owner(ent);
56                         id    = get_entity_ld_ident(ent);
57                         if (owner == get_tls_type()) {
58                                 if (get_entity_visibility(ent) == visibility_external_allocated)
59                                         id = mangle(id, new_id_from_chars("@INDNTPOFF", 10));
60                                 else
61                                         id = mangle(id, new_id_from_chars("@NTPOFF", 7));
62                         }
63                         return id;
64
65                 default:
66                         assert(0 && "Unsupported SymConst");
67         }
68
69         return NULL;
70 }
71
72
73
74 /***********************************************************************************
75  *      _                                   _       _             __
76  *     | |                                 (_)     | |           / _|
77  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
78  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
79  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
80  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
81  *                       | |
82  *                       |_|
83  ***********************************************************************************/
84
85 /**
86  * Returns a string containing the names of all registers within the limited bitset
87  */
88 static char *get_limited_regs(const arch_register_req_t *req, char *buf, int max) {
89         bitset_t *bs   = bitset_alloca(req->cls->n_regs);
90         char     *p    = buf;
91         int       size = 0;
92         int       i, cnt;
93
94         req->limited(NULL, bs);
95
96         for (i = 0; i < req->cls->n_regs; i++) {
97                 if (bitset_is_set(bs, i)) {
98                         cnt = snprintf(p, max - size, " %s", req->cls->regs[i].name);
99                         if (cnt < 0) {
100                                 fprintf(stderr, "dumper problem, exiting\n");
101                                 exit(1);
102                         }
103
104                         p    += cnt;
105                         size += cnt;
106
107                         if (size >= max)
108                                 break;
109                 }
110         }
111
112         return buf;
113 }
114
115 /**
116  * Dumps the register requirements for either in or out.
117  */
118 static void dump_reg_req(FILE *F, ir_node *n, const ia32_register_req_t **reqs, int inout) {
119         char *dir = inout ? "out" : "in";
120         int   max = inout ? get_ia32_n_res(n) : get_irn_arity(n);
121         char *buf = alloca(1024);
122         int   i;
123
124         memset(buf, 0, 1024);
125
126         if (reqs) {
127                 for (i = 0; i < max; i++) {
128                         fprintf(F, "%sreq #%d =", dir, i);
129
130                         if (reqs[i]->req.type == arch_register_req_type_none) {
131                                 fprintf(F, " n/a");
132                         }
133
134                         if (reqs[i]->req.type & arch_register_req_type_normal) {
135                                 fprintf(F, " %s", reqs[i]->req.cls->name);
136                         }
137
138                         if (reqs[i]->req.type & arch_register_req_type_limited) {
139                                 fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
140                         }
141
142                         if (reqs[i]->req.type & arch_register_req_type_should_be_same) {
143                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->same_pos));
144                         }
145
146                         if (reqs[i]->req.type & arch_register_req_type_should_be_different) {
147                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->different_pos));
148                         }
149
150                         fprintf(F, "\n");
151                 }
152
153                 fprintf(F, "\n");
154         }
155         else {
156                 fprintf(F, "%sreq = N/A\n", dir);
157         }
158 }
159
160 /**
161  * Dumper interface for dumping ia32 nodes in vcg.
162  * @param n        the node to dump
163  * @param F        the output file
164  * @param reason   indicates which kind of information should be dumped
165  * @return 0 on success or != 0 on failure
166  */
167 static int ia32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
168         ir_mode     *mode = NULL;
169         int          bad  = 0;
170         int          i, n_res, am_flav, flags;
171         const ia32_register_req_t **reqs;
172         const arch_register_t     **slots;
173
174         switch (reason) {
175                 case dump_node_opcode_txt:
176                         fprintf(F, "%s", get_irn_opname(n));
177                         break;
178
179                 case dump_node_mode_txt:
180                         mode = get_irn_mode(n);
181
182                         if (is_ia32_Ld(n) || is_ia32_St(n)) {
183                                 mode = get_ia32_ls_mode(n);
184                         }
185
186                         fprintf(F, "[%s]", mode ? get_mode_name(mode) : "?NOMODE?");
187                         break;
188
189                 case dump_node_nodeattr_txt:
190                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n) || is_ia32_Cnst(n)) {
191                                 char       *pref = is_ia32_ImmSymConst(n) || (get_ia32_op_type(n) == ia32_SymConst) ? "SymC " : "";
192                                 const char *cnst = get_ia32_cnst(n);
193
194                                 fprintf(F, "[%s%s]", pref, cnst ? cnst : "NONE");
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                         /* dump immop type */
258                         fprintf(F, "immediate = ");
259                         switch (get_ia32_immop_type(n)) {
260                                 case ia32_ImmNone:
261                                         fprintf(F, "None");
262                                         break;
263                                 case ia32_ImmConst:
264                                         fprintf(F, "Const");
265                                         break;
266                                 case ia32_ImmSymConst:
267                                         fprintf(F, "SymConst");
268                                         break;
269                                 default:
270                                         fprintf(F, "unknown (%d)", get_ia32_immop_type(n));
271                                         break;
272                         }
273                         fprintf(F, "\n");
274
275                         /* dump supported am */
276                         fprintf(F, "AM support = ");
277                         switch (get_ia32_am_support(n)) {
278                                 case ia32_am_None:
279                                         fprintf(F, "none");
280                                         break;
281                                 case ia32_am_Source:
282                                         fprintf(F, "source only (Load)");
283                                         break;
284                                 case ia32_am_Dest:
285                                         fprintf(F, "dest only (Load+Store)");
286                                         break;
287                                 case ia32_am_Full:
288                                         fprintf(F, "full");
289                                         break;
290                                 default:
291                                         fprintf(F, "unknown (%d)", get_ia32_am_support(n));
292                                         break;
293                         }
294                         fprintf(F, "\n");
295
296                         /* dump am flavour */
297                         fprintf(F, "AM flavour =");
298                         am_flav = get_ia32_am_flavour(n);
299                         if (am_flav == ia32_am_N) {
300                                 fprintf(F, " none");
301                         }
302                         else {
303                                 if (am_flav & ia32_O) {
304                                         fprintf(F, " O");
305                                 }
306                                 if (am_flav & ia32_B) {
307                                         fprintf(F, " B");
308                                 }
309                                 if (am_flav & ia32_I) {
310                                         fprintf(F, " I");
311                                 }
312                                 if (am_flav & ia32_S) {
313                                         fprintf(F, " S");
314                                 }
315                         }
316                         fprintf(F, " (%d)\n", am_flav);
317
318                         /* dump AM offset */
319                         fprintf(F, "AM offset = ");
320                         if (get_ia32_am_offs(n)) {
321                                 fprintf(F, "%s", get_ia32_am_offs(n));
322                         }
323                         else {
324                                 fprintf(F, "n/a");
325                         }
326                         fprintf(F, "\n");
327
328                         /* dump AM scale */
329                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
330
331                         /* dump pn code */
332                         fprintf(F, "pn_code = %ld\n", get_ia32_pncode(n));
333
334                         /* dump n_res */
335                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
336
337                         /* dump use_frame */
338                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
339
340                         /* commutative */
341                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
342
343                         /* emit cl */
344                         fprintf(F, "emit cl instead of ecx = %d\n", is_ia32_emit_cl(n));
345
346                         /* got lea */
347                         fprintf(F, "got loea = %d\n", is_ia32_got_lea(n));
348
349                         /* got reload */
350                         fprintf(F, "got reload = %d\n", is_ia32_got_reload(n));
351
352                         /* dump latency */
353                         fprintf(F, "latency = %d\n", get_ia32_latency(n));
354
355                         /* dump flags */
356                         fprintf(F, "flags =");
357                         flags = get_ia32_flags(n);
358                         if (flags == arch_irn_flags_none) {
359                                 fprintf(F, " none");
360                         }
361                         else {
362                                 if (flags & arch_irn_flags_dont_spill) {
363                                         fprintf(F, " unspillable");
364                                 }
365                                 if (flags & arch_irn_flags_rematerializable) {
366                                         fprintf(F, " remat");
367                                 }
368                                 if (flags & arch_irn_flags_ignore) {
369                                         fprintf(F, " ignore");
370                                 }
371                                 if (flags & arch_irn_flags_modify_sp) {
372                                         fprintf(F, " modify_sp");
373                                 }
374                         }
375                         fprintf(F, " (%d)\n", flags);
376
377                         /* dump frame entity */
378                         fprintf(F, "frame entity = ");
379                         if (get_ia32_frame_ent(n)) {
380                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
381                         }
382                         else {
383                                 fprintf(F, "n/a");
384                         }
385                         fprintf(F, "\n");
386
387                         /* dump modes */
388                         fprintf(F, "ls_mode = ");
389                         if (get_ia32_ls_mode(n)) {
390                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
391                         }
392                         else {
393                                 fprintf(F, "n/a");
394                         }
395                         fprintf(F, "\n");
396
397                         fprintf(F, "res_mode = ");
398                         if (get_ia32_res_mode(n)) {
399                                 ir_fprintf(F, "%+F", get_ia32_res_mode(n));
400                         }
401                         else {
402                                 fprintf(F, "n/a");
403                         }
404                         fprintf(F, "\n");
405
406                         fprintf(F, "src_mode = ");
407                         if (get_ia32_src_mode(n)) {
408                                 ir_fprintf(F, "%+F", get_ia32_src_mode(n));
409                         }
410                         else {
411                                 fprintf(F, "n/a");
412                         }
413                         fprintf(F, "\n");
414
415                         fprintf(F, "tgt_mode = ");
416                         if (get_ia32_tgt_mode(n)) {
417                                 ir_fprintf(F, "%+F", get_ia32_tgt_mode(n));
418                         }
419                         else {
420                                 fprintf(F, "n/a");
421                         }
422                         fprintf(F, "\n");
423
424 #ifndef NDEBUG
425                         /* dump original ir node name */
426                         fprintf(F, "orig node = ");
427                         if (get_ia32_orig_node(n)) {
428                                 fprintf(F, "%s", get_ia32_orig_node(n));
429                         }
430                         else {
431                                 fprintf(F, "n/a");
432                         }
433                         fprintf(F, "\n");
434 #endif /* NDEBUG */
435
436                         fprintf(F, "=== IA32 attr end ===\n");
437                         /* end of: case dump_node_info_txt */
438                         break;
439         }
440
441         return bad;
442 }
443
444
445
446 /***************************************************************************************************
447  *        _   _                   _       __        _                    _   _               _
448  *       | | | |                 | |     / /       | |                  | | | |             | |
449  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
450  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
451  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
452  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
453  *                                        __/ |
454  *                                       |___/
455  ***************************************************************************************************/
456
457 /**
458  * Returns an ident for the given tarval tv.
459  */
460 static ident *get_ident_for_tv(tarval *tv) {
461         char buf[1024];
462         int len = tarval_snprintf(buf, sizeof(buf), tv);
463         assert(len);
464         return new_id_from_str(buf);
465 }
466
467 /**
468  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
469  * Firm was made by people hating const :-(
470  */
471 ia32_attr_t *get_ia32_attr(const ir_node *node) {
472         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
473         return (ia32_attr_t *)get_irn_generic_attr((ir_node *)node);
474 }
475
476 /**
477  * Gets the type of an ia32 node.
478  */
479 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
480         ia32_attr_t *attr = get_ia32_attr(node);
481         return attr->data.tp;
482 }
483
484 /**
485  * Sets the type of an ia32 node.
486  */
487 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
488         ia32_attr_t *attr = get_ia32_attr(node);
489         attr->data.tp     = tp;
490 }
491
492 /**
493  * Gets the immediate op type of an ia32 node.
494  */
495 ia32_immop_type_t get_ia32_immop_type(const ir_node *node) {
496         ia32_attr_t *attr = get_ia32_attr(node);
497         return attr->data.imm_tp;
498 }
499
500 /**
501  * Sets the immediate op type of an ia32 node.
502  */
503 void set_ia32_immop_type(ir_node *node, ia32_immop_type_t tp) {
504         ia32_attr_t *attr = get_ia32_attr(node);
505         attr->data.imm_tp = tp;
506 }
507
508 /**
509  * Gets the supported addrmode of an ia32 node
510  */
511 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
512         ia32_attr_t *attr = get_ia32_attr(node);
513         return attr->data.am_support;
514 }
515
516 /**
517  * Sets the supported addrmode of an ia32 node
518  */
519 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp) {
520         ia32_attr_t *attr     = get_ia32_attr(node);
521         attr->data.am_support = am_tp;
522 }
523
524 /**
525  * Gets the addrmode flavour of an ia32 node
526  */
527 ia32_am_flavour_t get_ia32_am_flavour(const ir_node *node) {
528         ia32_attr_t *attr = get_ia32_attr(node);
529         return attr->data.am_flavour;
530 }
531
532 /**
533  * Sets the addrmode flavour of an ia32 node
534  */
535 void set_ia32_am_flavour(ir_node *node, ia32_am_flavour_t am_flavour) {
536         ia32_attr_t *attr     = get_ia32_attr(node);
537         attr->data.am_flavour = am_flavour;
538 }
539
540 /**
541  * Joins all offsets to one string with adds.
542  */
543 char *get_ia32_am_offs(const ir_node *node) {
544         ia32_attr_t *attr = get_ia32_attr(node);
545         static char res[64];
546
547         snprintf(res, sizeof(res), "%+d", attr->am_offs);
548
549         return res;
550 }
551
552 /**
553  * Gets the addressmode offset as long.
554  */
555 int get_ia32_am_offs_int(const ir_node *node) {
556         ia32_attr_t *attr = get_ia32_attr(node);
557         return attr->am_offs;
558 }
559
560 /**
561  * Add an offset for addrmode.
562  */
563 static void extend_ia32_am_offs(ir_node *node, char *offset, char op) {
564         ia32_attr_t *attr = get_ia32_attr(node);
565         int res, o;
566
567         if (offset == NULL || offset[0] == '\0')
568                 return;
569
570         if (offset[0] == '-')
571                 res = sscanf(offset, "%d", &o);
572         else
573                 res = sscanf(offset, "%u", &o);
574         assert(res == 1);
575
576         if (op == '-')
577                 attr->am_offs -= o;
578         else if (op == '+')
579                 attr->am_offs += o;
580         else
581                 assert(0);
582 }
583
584 /**
585  * Add an offset for addrmode.
586  */
587 void add_ia32_am_offs(ir_node *node, const char *offset) {
588         extend_ia32_am_offs(node, (char *)offset, '+');
589 }
590
591 void add_ia32_am_offs_int(ir_node *node, int offset) {
592         ia32_attr_t *attr = get_ia32_attr(node);
593         attr->am_offs += offset;
594 }
595
596 /**
597  * Sub an offset for addrmode.
598  */
599 void sub_ia32_am_offs(ir_node *node, const char *offset) {
600         extend_ia32_am_offs(node, (char *)offset, '-');
601 }
602
603 /**
604  * Returns the symconst ident associated to addrmode.
605  */
606 ident *get_ia32_am_sc(const ir_node *node) {
607         ia32_attr_t *attr = get_ia32_attr(node);
608         return attr->am_sc;
609 }
610
611 /**
612  * Sets the symconst ident associated to addrmode.
613  */
614 void set_ia32_am_sc(ir_node *node, ident *sc) {
615         ia32_attr_t *attr = get_ia32_attr(node);
616         attr->am_sc       = sc;
617 }
618
619 /**
620  * Sets the sign bit for address mode symconst.
621  */
622 void set_ia32_am_sc_sign(ir_node *node) {
623         ia32_attr_t *attr     = get_ia32_attr(node);
624         attr->data.am_sc_sign = 1;
625 }
626
627 /**
628  * Clears the sign bit for address mode symconst.
629  */
630 void clear_ia32_am_sc_sign(ir_node *node) {
631         ia32_attr_t *attr     = get_ia32_attr(node);
632         attr->data.am_sc_sign = 0;
633 }
634
635 /**
636  * Returns the sign bit for address mode symconst.
637  */
638 int is_ia32_am_sc_sign(const ir_node *node) {
639         ia32_attr_t *attr = get_ia32_attr(node);
640         return attr->data.am_sc_sign;
641 }
642
643 /**
644  * Gets the addr mode const.
645  */
646 int get_ia32_am_scale(const ir_node *node) {
647         ia32_attr_t *attr = get_ia32_attr(node);
648         return attr->data.am_scale;
649 }
650
651 /**
652  * Sets the index register scale for addrmode.
653  */
654 void set_ia32_am_scale(ir_node *node, int scale) {
655         ia32_attr_t *attr   = get_ia32_attr(node);
656         attr->data.am_scale = scale;
657 }
658
659 /**
660  * Return the tarval of an immediate operation or NULL in case of SymConst
661  */
662 tarval *get_ia32_Immop_tarval(const ir_node *node) {
663         ia32_attr_t *attr = get_ia32_attr(node);
664     return attr->cnst_val.tv;
665 }
666
667 /**
668  * Sets the attributes of an immediate operation to the specified tarval
669  */
670 void set_ia32_Immop_tarval(ir_node *node, tarval *tv) {
671         ia32_attr_t *attr = get_ia32_attr(node);
672         attr->cnst_val.tv = tv;
673         attr->cnst        = get_ident_for_tv(tv);
674 }
675
676 /**
677  * Gets the string representation of the internal const (tv or symconst)
678  */
679 const char *get_ia32_cnst(const ir_node *node) {
680         ia32_attr_t *attr = get_ia32_attr(node);
681         if (! attr->cnst)
682                 return NULL;
683         return get_id_str(attr->cnst);
684 }
685
686 /**
687  * Sets the string representation of the internal const.
688  */
689 void set_ia32_cnst(ir_node *node, const char *cnst) {
690         ia32_attr_t *attr = get_ia32_attr(node);
691         attr->cnst        = new_id_from_str(cnst);
692 }
693
694 /**
695  * Gets the ident representation of the internal const (tv or symconst)
696  */
697 ident *get_ia32_id_cnst(const ir_node *node) {
698         ia32_attr_t *attr = get_ia32_attr(node);
699         return attr->cnst;
700 }
701
702 /**
703  * Sets the ident representation of the internal const.
704  */
705 void set_ia32_id_cnst(ir_node *node, ident *cnst) {
706         ia32_attr_t *attr = get_ia32_attr(node);
707         attr->cnst        = cnst;
708 }
709
710 /**
711  * Sets the uses_frame flag.
712  */
713 void set_ia32_use_frame(ir_node *node) {
714         ia32_attr_t *attr    = get_ia32_attr(node);
715         attr->data.use_frame = 1;
716 }
717
718 /**
719  * Clears the uses_frame flag.
720  */
721 void clear_ia32_use_frame(ir_node *node) {
722         ia32_attr_t *attr    = get_ia32_attr(node);
723         attr->data.use_frame = 0;
724 }
725
726 /**
727  * Gets the uses_frame flag.
728  */
729 int is_ia32_use_frame(const ir_node *node) {
730         ia32_attr_t *attr = get_ia32_attr(node);
731         return attr->data.use_frame;
732 }
733
734 /**
735  * Sets node to commutative.
736  */
737 void set_ia32_commutative(ir_node *node) {
738         ia32_attr_t *attr         = get_ia32_attr(node);
739         attr->data.is_commutative = 1;
740 }
741
742 /**
743  * Sets node to non-commutative.
744  */
745 void clear_ia32_commutative(ir_node *node) {
746         ia32_attr_t *attr         = get_ia32_attr(node);
747         attr->data.is_commutative = 0;
748 }
749
750 /**
751  * Checks if node is commutative.
752  */
753 int is_ia32_commutative(const ir_node *node) {
754         ia32_attr_t *attr = get_ia32_attr(node);
755         return attr->data.is_commutative;
756 }
757
758 /**
759  * Sets node emit_cl.
760  */
761 void set_ia32_emit_cl(ir_node *node) {
762         ia32_attr_t *attr  = get_ia32_attr(node);
763         attr->data.emit_cl = 1;
764 }
765
766 /**
767  * Clears node emit_cl.
768  */
769 void clear_ia32_emit_cl(ir_node *node) {
770         ia32_attr_t *attr  = get_ia32_attr(node);
771         attr->data.emit_cl = 0;
772 }
773
774 /**
775  * Checks if node needs %cl.
776  */
777 int is_ia32_emit_cl(const ir_node *node) {
778         ia32_attr_t *attr = get_ia32_attr(node);
779         return attr->data.emit_cl;
780 }
781
782 /**
783  * Sets node got_lea.
784  */
785 void set_ia32_got_lea(ir_node *node) {
786         ia32_attr_t *attr  = get_ia32_attr(node);
787         attr->data.got_lea = 1;
788 }
789
790 /**
791  * Clears node got_lea.
792  */
793 void clear_ia32_got_lea(ir_node *node) {
794         ia32_attr_t *attr  = get_ia32_attr(node);
795         attr->data.got_lea = 0;
796 }
797
798 /**
799  * Checks if node got lea.
800  */
801 int is_ia32_got_lea(const ir_node *node) {
802         ia32_attr_t *attr = get_ia32_attr(node);
803         return attr->data.got_lea;
804 }
805
806 /**
807  * Sets node got_reload.
808  */
809 void set_ia32_got_reload(ir_node *node) {
810         ia32_attr_t *attr     = get_ia32_attr(node);
811         attr->data.got_reload = 1;
812 }
813
814 /**
815  * Clears node got_reload.
816  */
817 void clear_ia32_got_reload(ir_node *node) {
818         ia32_attr_t *attr     = get_ia32_attr(node);
819         attr->data.got_reload = 0;
820 }
821
822 /**
823  * Checks if node got reload.
824  */
825 int is_ia32_got_reload(const ir_node *node) {
826         ia32_attr_t *attr = get_ia32_attr(node);
827         return attr->data.got_reload;
828 }
829
830 /**
831  * Gets the mode of the stored/loaded value (only set for Store/Load)
832  */
833 ir_mode *get_ia32_ls_mode(const ir_node *node) {
834         ia32_attr_t *attr = get_ia32_attr(node);
835         return attr->ls_mode;
836 }
837
838 /**
839  * Sets the mode of the stored/loaded value (only set for Store/Load)
840  */
841 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
842         ia32_attr_t *attr = get_ia32_attr(node);
843         attr->ls_mode     = mode;
844 }
845
846 /**
847  * Gets the mode of the result.
848  */
849 ir_mode *get_ia32_res_mode(const ir_node *node) {
850         ia32_attr_t *attr = get_ia32_attr(node);
851         return attr->res_mode;
852 }
853
854 /**
855  * Sets the mode of the result.
856  */
857 void set_ia32_res_mode(ir_node *node, ir_mode *mode) {
858         ia32_attr_t *attr = get_ia32_attr(node);
859         attr->res_mode    = mode;
860 }
861
862 /**
863  * Gets the source mode of conversion.
864  */
865 ir_mode *get_ia32_src_mode(const ir_node *node) {
866         ia32_attr_t *attr = get_ia32_attr(node);
867         return attr->src_mode;
868 }
869
870 /**
871  * Sets the source mode of conversion.
872  */
873 void set_ia32_src_mode(ir_node *node, ir_mode *mode) {
874         ia32_attr_t *attr = get_ia32_attr(node);
875         attr->src_mode    = mode;
876 }
877
878 /**
879  * Gets the target mode of conversion.
880  */
881 ir_mode *get_ia32_tgt_mode(const ir_node *node) {
882         ia32_attr_t *attr = get_ia32_attr(node);
883         return attr->tgt_mode;
884 }
885
886 /**
887  * Sets the target mode of conversion.
888  */
889 void set_ia32_tgt_mode(ir_node *node, ir_mode *mode) {
890         ia32_attr_t *attr = get_ia32_attr(node);
891         attr->tgt_mode    = mode;
892 }
893
894 /**
895  * Gets the frame entity assigned to this node.
896  */
897 entity *get_ia32_frame_ent(const ir_node *node) {
898         ia32_attr_t *attr = get_ia32_attr(node);
899         return attr->frame_ent;
900 }
901
902 /**
903  * Sets the frame entity for this node.
904  */
905 void set_ia32_frame_ent(ir_node *node, entity *ent) {
906         ia32_attr_t *attr = get_ia32_attr(node);
907         attr->frame_ent   = ent;
908         set_ia32_use_frame(node);
909 }
910
911
912 /**
913  * Gets the instruction latency.
914  */
915 unsigned get_ia32_latency(const ir_node *node) {
916         ia32_attr_t *attr = get_ia32_attr(node);
917         return attr->latency;
918 }
919
920 /**
921 * Sets the instruction latency.
922 */
923 void set_ia32_latency(ir_node *node, unsigned latency) {
924         ia32_attr_t *attr = get_ia32_attr(node);
925         attr->latency     = latency;
926 }
927
928 /**
929  * Returns the argument register requirements of an ia32 node.
930  */
931 const ia32_register_req_t **get_ia32_in_req_all(const ir_node *node) {
932         ia32_attr_t *attr = get_ia32_attr(node);
933         return attr->in_req;
934 }
935
936 /**
937  * Sets the argument register requirements of an ia32 node.
938  */
939 void set_ia32_in_req_all(ir_node *node, const ia32_register_req_t **reqs) {
940         ia32_attr_t *attr = get_ia32_attr(node);
941         attr->in_req      = reqs;
942 }
943
944 /**
945  * Returns the result register requirements of an ia32 node.
946  */
947 const ia32_register_req_t **get_ia32_out_req_all(const ir_node *node) {
948         ia32_attr_t *attr = get_ia32_attr(node);
949         return attr->out_req;
950 }
951
952 /**
953  * Sets the result register requirements of an ia32 node.
954  */
955 void set_ia32_out_req_all(ir_node *node, const ia32_register_req_t **reqs) {
956         ia32_attr_t *attr = get_ia32_attr(node);
957         attr->out_req     = reqs;
958 }
959
960 /**
961  * Returns the argument register requirement at position pos of an ia32 node.
962  */
963 const ia32_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
964         ia32_attr_t *attr = get_ia32_attr(node);
965         return attr->in_req[pos];
966 }
967
968 /**
969  * Returns the result register requirement at position pos of an ia32 node.
970  */
971 const ia32_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
972         ia32_attr_t *attr = get_ia32_attr(node);
973         return attr->out_req[pos];
974 }
975
976 /**
977  * Sets the OUT register requirements at position pos.
978  */
979 void set_ia32_req_out(ir_node *node, const ia32_register_req_t *req, int pos) {
980         ia32_attr_t *attr  = get_ia32_attr(node);
981         attr->out_req[pos] = req;
982 }
983
984 /**
985  * Sets the IN register requirements at position pos.
986  */
987 void set_ia32_req_in(ir_node *node, const ia32_register_req_t *req, int pos) {
988         ia32_attr_t *attr = get_ia32_attr(node);
989         attr->in_req[pos] = req;
990 }
991
992 /**
993  * Returns the register flag of an ia32 node.
994  */
995 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
996         ia32_attr_t *attr = get_ia32_attr(node);
997         return attr->data.flags;
998 }
999
1000 /**
1001  * Sets the register flag of an ia32 node.
1002  */
1003 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
1004         ia32_attr_t *attr = get_ia32_attr(node);
1005         attr->data.flags  = flags;
1006 }
1007
1008 /**
1009  * Returns the result register slots of an ia32 node.
1010  */
1011 const arch_register_t **get_ia32_slots(const ir_node *node) {
1012         ia32_attr_t *attr = get_ia32_attr(node);
1013         return attr->slots;
1014 }
1015
1016 /**
1017  * Sets the number of results.
1018  */
1019 void set_ia32_n_res(ir_node *node, int n_res) {
1020         ia32_attr_t *attr = get_ia32_attr(node);
1021         attr->data.n_res  = n_res;
1022 }
1023
1024 /**
1025  * Returns the number of results.
1026  */
1027 int get_ia32_n_res(const ir_node *node) {
1028         ia32_attr_t *attr = get_ia32_attr(node);
1029         return attr->data.n_res;
1030 }
1031
1032 /**
1033  * Returns the flavour of an ia32 node,
1034  */
1035 ia32_op_flavour_t get_ia32_flavour(const ir_node *node) {
1036         ia32_attr_t *attr = get_ia32_attr(node);
1037         return attr->data.op_flav;
1038 }
1039
1040 /**
1041  * Sets the flavour of an ia32 node to flavour_Div/Mod/DivMod/Mul/Mulh.
1042  */
1043 void set_ia32_flavour(ir_node *node, ia32_op_flavour_t op_flav) {
1044         ia32_attr_t *attr  = get_ia32_attr(node);
1045         attr->data.op_flav = op_flav;
1046 }
1047
1048 /**
1049  * Returns the projnum code.
1050  */
1051 long get_ia32_pncode(const ir_node *node) {
1052         ia32_attr_t *attr = get_ia32_attr(node);
1053         return attr->pn_code;
1054 }
1055
1056 /**
1057  * Sets the projnum code
1058  */
1059 void set_ia32_pncode(ir_node *node, long code) {
1060         ia32_attr_t *attr = get_ia32_attr(node);
1061         attr->pn_code     = code;
1062 }
1063
1064 #ifndef NDEBUG
1065
1066 /**
1067  * Returns the name of the original ir node.
1068  */
1069 const char *get_ia32_orig_node(const ir_node *node) {
1070         ia32_attr_t *attr = get_ia32_attr(node);
1071         return attr->orig_node;
1072 }
1073
1074 /**
1075  * Sets the name of the original ir node.
1076  */
1077 void set_ia32_orig_node(ir_node *node, const char *name) {
1078         ia32_attr_t *attr = get_ia32_attr(node);
1079         attr->orig_node   = name;
1080 }
1081
1082 #endif /* NDEBUG */
1083
1084 /******************************************************************************************************
1085  *                      _       _         _   _           __                  _   _
1086  *                     (_)     | |       | | | |         / _|                | | (_)
1087  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
1088  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
1089  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
1090  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
1091  *     | |
1092  *     |_|
1093  ******************************************************************************************************/
1094
1095 /**
1096  * Gets the type of an ia32_Const.
1097  */
1098 unsigned get_ia32_Const_type(const ir_node *node) {
1099         ia32_attr_t *attr = get_ia32_attr(node);
1100
1101         assert(is_ia32_Cnst(node) && "Need ia32_Const to get type");
1102
1103         return attr->data.tp;
1104 }
1105
1106 /**
1107  * Sets the type of an ia32_Const.
1108  */
1109 void set_ia32_Const_type(ir_node *node, int type) {
1110         ia32_attr_t *attr = get_ia32_attr(node);
1111
1112         assert(is_ia32_Cnst(node) && "Need ia32_Const to set type");
1113         assert((type == ia32_Const || type == ia32_SymConst) && "Unsupported ia32_Const type");
1114
1115         attr->data.tp = type;
1116 }
1117
1118 /**
1119  * Copy the attributes from an ia32_Const to an Immop (Add_i, Sub_i, ...) node
1120  */
1121 void set_ia32_Immop_attr(ir_node *node, ir_node *cnst) {
1122         ia32_attr_t *na = get_ia32_attr(node);
1123         ia32_attr_t *ca = get_ia32_attr(cnst);
1124
1125         switch(get_ia32_Const_type(cnst)) {
1126                 case ia32_Const:
1127                         na->cnst_val.tv = ca->cnst_val.tv;
1128                         na->cnst        = ca->cnst;
1129                         set_ia32_immop_type(node, ia32_ImmConst);
1130                         break;
1131                 case ia32_SymConst:
1132                         na->cnst_val.sc = ca->cnst_val.sc;
1133                         na->cnst        = na->cnst_val.sc;
1134                         set_ia32_immop_type(node, ia32_ImmSymConst);
1135                         break;
1136                 default:
1137                         assert(0 && "Need ia32_Const to set Immop attr");
1138         }
1139 }
1140
1141 /**
1142  * Copy the attributes from Immop to an Immop
1143  */
1144 void copy_ia32_Immop_attr(ir_node *dst, ir_node *src) {
1145         ia32_attr_t *da = get_ia32_attr(dst);
1146         ia32_attr_t *sa = get_ia32_attr(src);
1147
1148         switch(get_ia32_immop_type(src)) {
1149                 case ia32_ImmConst:
1150                         da->cnst_val.tv = sa->cnst_val.tv;
1151                         da->cnst        = sa->cnst;
1152                         set_ia32_immop_type(dst, ia32_ImmConst);
1153                         break;
1154                 case ia32_ImmSymConst:
1155                         da->cnst_val.sc = sa->cnst_val.sc;
1156                         da->cnst        = sa->cnst;
1157                         set_ia32_immop_type(dst, ia32_ImmSymConst);
1158                         break;
1159                 default:
1160                         assert(0 && "Need Immop to copy Immop attr");
1161         }
1162 }
1163
1164 /**
1165  * Copy the attributes from a Firm Const/SymConst to an ia32_Const
1166  */
1167 void set_ia32_Const_attr(ir_node *ia32_cnst, ir_node *cnst) {
1168         ia32_attr_t *attr = get_ia32_attr(ia32_cnst);
1169         ir_mode *mode;
1170
1171         assert(is_ia32_Cnst(ia32_cnst) && "Need ia32_Const to set Const attr");
1172
1173         switch (get_irn_opcode(cnst)) {
1174                 case iro_Const:
1175                         attr->data.tp     = ia32_Const;
1176                         attr->cnst_val.tv = get_Const_tarval(cnst);
1177                         mode = get_tarval_mode(attr->cnst_val.tv);
1178                         if (mode_is_reference(mode) &&
1179                             get_mode_null(mode) == attr->cnst_val.tv) {
1180                                 attr->cnst_val.tv = get_mode_null(mode_Iu);
1181                         }
1182                         attr->cnst        = get_ident_for_tv(attr->cnst_val.tv);
1183                         break;
1184                 case iro_SymConst:
1185                         attr->data.tp     = ia32_SymConst;
1186                         attr->cnst_val.sc = get_sc_ident(cnst);
1187                         attr->cnst        = attr->cnst_val.sc;
1188                         break;
1189                 case iro_Unknown:
1190                         assert(0 && "Unknown Const NYI");
1191                         break;
1192                 default:
1193                         assert(0 && "Cannot create ia32_Const for this opcode");
1194         }
1195 }
1196
1197 /**
1198  * Sets the AddrMode(S|D) attribute
1199  */
1200 void set_ia32_AddrMode(ir_node *node, char direction) {
1201         ia32_attr_t *attr = get_ia32_attr(node);
1202
1203         switch (direction) {
1204                 case 'D':
1205                         attr->data.tp = ia32_AddrModeD;
1206                         break;
1207                 case 'S':
1208                         attr->data.tp = ia32_AddrModeS;
1209                         break;
1210                 default:
1211                         assert(0 && "wrong AM type");
1212         }
1213 }
1214
1215 /**
1216  * Returns whether or not the node is an immediate operation with Const.
1217  */
1218 int is_ia32_ImmConst(const ir_node *node) {
1219         ia32_attr_t *attr = get_ia32_attr(node);
1220         return (attr->data.imm_tp == ia32_ImmConst);
1221 }
1222
1223 /**
1224  * Returns whether or not the node is an immediate operation with SymConst.
1225  */
1226 int is_ia32_ImmSymConst(const ir_node *node) {
1227         ia32_attr_t *attr = get_ia32_attr(node);
1228         return (attr->data.imm_tp == ia32_ImmSymConst);
1229 }
1230
1231 /**
1232  * Returns whether or not the node is an AddrModeS node.
1233  */
1234 int is_ia32_AddrModeS(const ir_node *node) {
1235         ia32_attr_t *attr = get_ia32_attr(node);
1236         return (attr->data.tp == ia32_AddrModeS);
1237 }
1238
1239 /**
1240  * Returns whether or not the node is an AddrModeD node.
1241  */
1242 int is_ia32_AddrModeD(const ir_node *node) {
1243         ia32_attr_t *attr = get_ia32_attr(node);
1244         return (attr->data.tp == ia32_AddrModeD);
1245 }
1246
1247 /**
1248  * Checks if node is a Load or xLoad/vfLoad.
1249  */
1250 int is_ia32_Ld(const ir_node *node) {
1251         int op = get_ia32_irn_opcode(node);
1252         return op == iro_ia32_Load || op == iro_ia32_xLoad || op == iro_ia32_vfld || op == iro_ia32_fld;
1253 }
1254
1255 /**
1256  * Checks if node is a Store or xStore/vfStore.
1257  */
1258 int is_ia32_St(const ir_node *node) {
1259         int op = get_ia32_irn_opcode(node);
1260         return op == iro_ia32_Store || op == iro_ia32_xStore || op == iro_ia32_vfst || op == iro_ia32_fst || op == iro_ia32_fstp;
1261 }
1262
1263 /**
1264  * Checks if node is a Const or xConst/vfConst.
1265  */
1266 int is_ia32_Cnst(const ir_node *node) {
1267         int op = get_ia32_irn_opcode(node);
1268         return op == iro_ia32_Const || op == iro_ia32_xConst || op == iro_ia32_vfConst;
1269 }
1270
1271 /**
1272  * Returns the name of the OUT register at position pos.
1273  */
1274 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
1275         ia32_attr_t *attr = get_ia32_attr(node);
1276
1277         assert(is_ia32_irn(node) && "Not an ia32 node.");
1278         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1279         assert(attr->slots[pos]  && "No register assigned");
1280
1281         return arch_register_get_name(attr->slots[pos]);
1282 }
1283
1284 /**
1285  * Returns the index of the OUT register at position pos within its register class.
1286  */
1287 int get_ia32_out_regnr(const ir_node *node, int pos) {
1288         ia32_attr_t *attr = get_ia32_attr(node);
1289
1290         assert(is_ia32_irn(node) && "Not an ia32 node.");
1291         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1292         assert(attr->slots[pos]  && "No register assigned");
1293
1294         return arch_register_get_index(attr->slots[pos]);
1295 }
1296
1297 /**
1298  * Returns the OUT register at position pos.
1299  */
1300 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1301         ia32_attr_t *attr = get_ia32_attr(node);
1302
1303         assert(is_ia32_irn(node) && "Not an ia32 node.");
1304         assert(pos < (int) attr->data.n_res && "Invalid OUT position.");
1305         assert(attr->slots[pos]  && "No register assigned");
1306
1307         return attr->slots[pos];
1308 }
1309
1310 /**
1311  * Initializes the nodes attributes.
1312  */
1313 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags, const ia32_register_req_t **in_reqs,
1314                                                   const ia32_register_req_t **out_reqs, int n_res, unsigned latency)
1315 {
1316         ia32_attr_t *attr = get_ia32_attr(node);
1317         set_ia32_flags(node, flags);
1318         set_ia32_in_req_all(node, in_reqs);
1319         set_ia32_out_req_all(node, out_reqs);
1320         set_ia32_latency(node, latency);
1321
1322         attr->data.n_res = n_res;
1323         memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
1324 }
1325
1326 /***************************************************************************************
1327  *                  _                            _                   _
1328  *                 | |                          | |                 | |
1329  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1330  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1331  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1332  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1333  *
1334  ***************************************************************************************/
1335
1336 /* default compare operation to compare immediate ops */
1337 int ia32_compare_immop_attr(ia32_attr_t *a, ia32_attr_t *b) {
1338         int equ = 0;
1339
1340         if (a->data.tp == b->data.tp) {
1341                 equ = (a->cnst == b->cnst);
1342                 equ = equ ? (a->data.use_frame == b->data.use_frame) : 0;
1343
1344                 if (equ && a->data.use_frame && b->data.use_frame)
1345                         equ = (a->frame_ent == b->frame_ent);
1346         }
1347
1348         return !equ;
1349 }
1350
1351 /* compare converts */
1352 int ia32_compare_conv_attr(ia32_attr_t *a, ia32_attr_t *b) {
1353         int equ = ! ia32_compare_immop_attr(a, b);
1354
1355         equ = equ ? (a->src_mode == b->src_mode) && (a->tgt_mode == b->tgt_mode) : equ;
1356
1357         return !equ;
1358 }
1359
1360 /* Copy attribute function not needed any more, but might be of use later. */
1361 #if 0
1362 /* copies the ia32 attributes */
1363 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node) {
1364         ia32_attr_t    *attr_old = get_ia32_attr(old_node);
1365         ia32_attr_t    *attr_new = get_ia32_attr(new_node);
1366         int             n_res    = get_ia32_n_res(old_node);
1367
1368         /* copy the attributes */
1369         memcpy(attr_new, attr_old, sizeof(*attr_new));
1370 }
1371
1372 /**
1373  * Registers the ia32_copy_attr function for all ia32 opcodes.
1374  */
1375 void ia32_register_copy_attr_func(void) {
1376         unsigned i, f = get_ia32_opcode_first(), l = get_ia32_opcode_last();
1377
1378         for (i = f; i < l; i++) {
1379                 ir_op *op = get_irp_opcode(i);
1380                 op->ops.copy_attr = ia32_copy_attr;
1381         }
1382 }
1383 #endif
1384
1385 /* Include the generated constructor functions */
1386 #include "gen_ia32_new_nodes.c.inl"