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