ignore early clobber in ia32 asm parser (as the common backend should have handled...
[libfirm] / ir / be / ia32 / ia32_common_transform.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       This file implements the common parts of IR transformation from
23  *              firm into ia32-Firm.
24  * @author      Matthias Braun, Sebastian Buchwald
25  * @version     $Id: ia32_common_transform.c 21012 2008-08-06 13:35:17Z beck $
26  */
27 #include "config.h"
28
29 #include "error.h"
30 #include "ircons.h"
31 #include "irprintf.h"
32 #include "typerep.h"
33
34 #include "../betranshlp.h"
35 #include "../beirg_t.h"
36
37 #include "ia32_architecture.h"
38 #include "ia32_common_transform.h"
39 #include "ia32_new_nodes.h"
40
41 #include "gen_ia32_new_nodes.h"
42 #include "gen_ia32_regalloc_if.h"
43
44 /** hold the current code generator during transformation */
45 ia32_code_gen_t *env_cg = NULL;
46
47 heights_t *heights = NULL;
48
49 static const arch_register_req_t no_register_req = {
50         arch_register_req_type_none,
51         NULL,                         /* regclass */
52         NULL,                         /* limit bitset */
53         0,                            /* same pos */
54         0                             /* different pos */
55 };
56
57 static int check_immediate_constraint(long val, char immediate_constraint_type)
58 {
59         switch (immediate_constraint_type) {
60                 case 0:
61                 case 'i': return 1;
62
63                 case 'I': return    0 <= val && val <=  31;
64                 case 'J': return    0 <= val && val <=  63;
65                 case 'K': return -128 <= val && val <= 127;
66                 case 'L': return val == 0xff || val == 0xffff;
67                 case 'M': return    0 <= val && val <=   3;
68                 case 'N': return    0 <= val && val <= 255;
69                 case 'O': return    0 <= val && val <= 127;
70
71                 default: panic("Invalid immediate constraint found");
72         }
73 }
74
75 /**
76  * creates a unique ident by adding a number to a tag
77  *
78  * @param tag   the tag string, must contain a %d if a number
79  *              should be added
80  */
81 static ident *unique_id(const char *tag)
82 {
83         static unsigned id = 0;
84         char str[256];
85
86         snprintf(str, sizeof(str), tag, ++id);
87         return new_id_from_str(str);
88 }
89
90 /**
91  * Get a primitive type for a mode.
92  */
93 static ir_type *ia32_get_prim_type(pmap *types, ir_mode *mode)
94 {
95         pmap_entry *e = pmap_find(types, mode);
96         ir_type *res;
97
98         if (! e) {
99                 char buf[64];
100                 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
101                 res = new_type_primitive(new_id_from_str(buf), mode);
102                 set_type_alignment_bytes(res, 16);
103                 pmap_insert(types, mode, res);
104         }
105         else
106                 res = e->value;
107         return res;
108 }
109
110 ir_entity *create_float_const_entity(ir_node *cnst)
111 {
112         ia32_isa_t *isa = env_cg->isa;
113         tarval *key     = get_Const_tarval(cnst);
114         pmap_entry *e   = pmap_find(isa->tv_ent, key);
115         ir_entity *res;
116         ir_graph *rem;
117
118         if (e == NULL) {
119                 tarval  *tv   = key;
120                 ir_mode *mode = get_tarval_mode(tv);
121                 ir_type *tp;
122
123                 if (! ia32_cg_config.use_sse2) {
124                         /* try to reduce the mode to produce smaller sized entities */
125                         if (mode != mode_F) {
126                                 if (tarval_ieee754_can_conv_lossless(tv, mode_F)) {
127                                         mode = mode_F;
128                                         tv = tarval_convert_to(tv, mode);
129                                 } else if (mode != mode_D) {
130                                         if (tarval_ieee754_can_conv_lossless(tv, mode_D)) {
131                                                 mode = mode_D;
132                                                 tv = tarval_convert_to(tv, mode);
133                                         }
134                                 }
135                         }
136                 }
137
138                 if (mode == get_irn_mode(cnst)) {
139                         /* mode was not changed */
140                         tp = get_Const_type(cnst);
141                         if (tp == firm_unknown_type)
142                                 tp = ia32_get_prim_type(isa->types, mode);
143                 } else
144                         tp = ia32_get_prim_type(isa->types, mode);
145
146                 res = new_entity(get_glob_type(), unique_id(".LC%u"), tp);
147
148                 set_entity_ld_ident(res, get_entity_ident(res));
149                 set_entity_visibility(res, visibility_local);
150                 set_entity_variability(res, variability_constant);
151                 set_entity_allocation(res, allocation_static);
152
153                  /* we create a new entity here: It's initialization must resist on the
154                     const code irg */
155                 rem = current_ir_graph;
156                 current_ir_graph = get_const_code_irg();
157                 set_atomic_ent_value(res, new_Const_type(tv, tp));
158                 current_ir_graph = rem;
159
160                 pmap_insert(isa->tv_ent, key, res);
161         } else {
162                 res = e->value;
163         }
164
165         return res;
166 }
167
168 ir_node *create_Immediate(ir_entity *symconst, int symconst_sign, long val)
169 {
170         ir_graph *irg         = current_ir_graph;
171         ir_node  *start_block = get_irg_start_block(irg);
172         ir_node  *immediate   = new_bd_ia32_Immediate(NULL, start_block, symconst,
173                         symconst_sign, val);
174         arch_set_irn_register(immediate, &ia32_gp_regs[REG_GP_NOREG]);
175
176         return immediate;
177 }
178
179 const arch_register_t *ia32_get_clobber_register(const char *clobber)
180 {
181         const arch_register_t       *reg = NULL;
182         int                          c;
183         size_t                       r;
184         const arch_register_class_t *cls;
185
186         /* TODO: construct a hashmap instead of doing linear search for clobber
187          * register */
188         for(c = 0; c < N_CLASSES; ++c) {
189                 cls = & ia32_reg_classes[c];
190                 for(r = 0; r < cls->n_regs; ++r) {
191                         const arch_register_t *temp_reg = arch_register_for_index(cls, r);
192                         if(strcmp(temp_reg->name, clobber) == 0
193                                         || (c == CLASS_ia32_gp && strcmp(temp_reg->name+1, clobber) == 0)) {
194                                 reg = temp_reg;
195                                 break;
196                         }
197                 }
198                 if(reg != NULL)
199                         break;
200         }
201
202         return reg;
203 }
204
205 int ia32_mode_needs_gp_reg(ir_mode *mode) {
206         if(mode == mode_fpcw)
207                 return 0;
208         if(get_mode_size_bits(mode) > 32)
209                 return 0;
210         return mode_is_int(mode) || mode_is_reference(mode) || mode == mode_b;
211 }
212
213 static void parse_asm_constraints(constraint_t *constraint, const char *c,
214                            int is_output)
215 {
216         char                         immediate_type     = '\0';
217         unsigned                     limited            = 0;
218         const arch_register_class_t *cls                = NULL;
219         int                          memory_possible       = 0;
220         int                          all_registers_allowed = 0;
221         int                          p;
222         int                          same_as = -1;
223
224         memset(constraint, 0, sizeof(constraint[0]));
225         constraint->same_as = -1;
226
227         if(*c == 0) {
228                 /* a memory constraint: no need to do anything in backend about it
229                  * (the dependencies are already respected by the memory edge of
230                  * the node) */
231                 return;
232         }
233
234         /* TODO: improve error messages with node and source info. (As users can
235          * easily hit these) */
236         while(*c != 0) {
237                 switch(*c) {
238                 case ' ':
239                 case '\t':
240                 case '\n':
241                         break;
242
243                 /* Skip out/in-out marker */
244                 case '=': break;
245                 case '+': break;
246
247                 case '&': break;
248
249                 case '*':
250                         ++c;
251                         break;
252                 case '#':
253                         while(*c != 0 && *c != ',')
254                                 ++c;
255                         break;
256
257                 case 'a':
258                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
259                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
260                         limited |= 1 << REG_EAX;
261                         break;
262                 case 'b':
263                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
264                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
265                         limited |= 1 << REG_EBX;
266                         break;
267                 case 'c':
268                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
269                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
270                         limited |= 1 << REG_ECX;
271                         break;
272                 case 'd':
273                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
274                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
275                         limited |= 1 << REG_EDX;
276                         break;
277                 case 'D':
278                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
279                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
280                         limited |= 1 << REG_EDI;
281                         break;
282                 case 'S':
283                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
284                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
285                         limited |= 1 << REG_ESI;
286                         break;
287                 case 'Q':
288                 case 'q':
289                         /* q means lower part of the regs only, this makes no
290                          * difference to Q for us (we only assign whole registers) */
291                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
292                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
293                         limited |= 1 << REG_EAX | 1 << REG_EBX | 1 << REG_ECX |
294                                    1 << REG_EDX;
295                         break;
296                 case 'A':
297                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
298                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
299                         limited |= 1 << REG_EAX | 1 << REG_EDX;
300                         break;
301                 case 'l':
302                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
303                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
304                         limited |= 1 << REG_EAX | 1 << REG_EBX | 1 << REG_ECX |
305                                    1 << REG_EDX | 1 << REG_ESI | 1 << REG_EDI |
306                                    1 << REG_EBP;
307                         break;
308
309                 case 'R':
310                 case 'r':
311                 case 'p':
312                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
313                                 panic("multiple register classes not supported");
314                         cls                   = &ia32_reg_classes[CLASS_ia32_gp];
315                         all_registers_allowed = 1;
316                         break;
317
318                 case 'f':
319                 case 't':
320                 case 'u':
321                         /* TODO: mark values so the x87 simulator knows about t and u */
322                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_vfp])
323                                 panic("multiple register classes not supported");
324                         cls                   = &ia32_reg_classes[CLASS_ia32_vfp];
325                         all_registers_allowed = 1;
326                         break;
327
328                 case 'Y':
329                 case 'x':
330                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_xmm])
331                                 panic("multiple register classes not supproted");
332                         cls                   = &ia32_reg_classes[CLASS_ia32_xmm];
333                         all_registers_allowed = 1;
334                         break;
335
336                 case 'I':
337                 case 'J':
338                 case 'K':
339                 case 'L':
340                 case 'M':
341                 case 'N':
342                 case 'O':
343                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
344                                 panic("multiple register classes not supported");
345                         if (immediate_type != '\0')
346                                 panic("multiple immediate types not supported");
347                         cls            = &ia32_reg_classes[CLASS_ia32_gp];
348                         immediate_type = *c;
349                         break;
350                 case 'n':
351                 case 'i':
352                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
353                                 panic("multiple register classes not supported");
354                         if (immediate_type != '\0')
355                                 panic("multiple immediate types not supported");
356                         cls            = &ia32_reg_classes[CLASS_ia32_gp];
357                         immediate_type = 'i';
358                         break;
359
360                 case 'X':
361                 case 'g':
362                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
363                                 panic("multiple register classes not supported");
364                         if (immediate_type != '\0')
365                                 panic("multiple immediate types not supported");
366                         immediate_type        = 'i';
367                         cls                   = &ia32_reg_classes[CLASS_ia32_gp];
368                         all_registers_allowed = 1;
369                         memory_possible       = 1;
370                         break;
371
372                 case '0':
373                 case '1':
374                 case '2':
375                 case '3':
376                 case '4':
377                 case '5':
378                 case '6':
379                 case '7':
380                 case '8':
381                 case '9':
382                         if (is_output)
383                                 panic("can only specify same constraint on input");
384
385                         sscanf(c, "%d%n", &same_as, &p);
386                         if(same_as >= 0) {
387                                 c += p;
388                                 continue;
389                         }
390                         break;
391
392                 case 'm':
393                 case 'o':
394                 case 'V':
395                         /* memory constraint no need to do anything in backend about it
396                          * (the dependencies are already respected by the memory edge of
397                          * the node) */
398                         memory_possible = 1;
399                         break;
400
401                 case 'E': /* no float consts yet */
402                 case 'F': /* no float consts yet */
403                 case 's': /* makes no sense on x86 */
404                 case '<': /* no autodecrement on x86 */
405                 case '>': /* no autoincrement on x86 */
406                 case 'C': /* sse constant not supported yet */
407                 case 'G': /* 80387 constant not supported yet */
408                 case 'y': /* we don't support mmx registers yet */
409                 case 'Z': /* not available in 32 bit mode */
410                 case 'e': /* not available in 32 bit mode */
411                         panic("unsupported asm constraint '%c' found in (%+F)",
412                               *c, current_ir_graph);
413                         break;
414                 default:
415                         panic("unknown asm constraint '%c' found in (%+F)", *c,
416                               current_ir_graph);
417                         break;
418                 }
419                 ++c;
420         }
421
422         if(same_as >= 0) {
423                 if (cls != NULL)
424                         panic("same as and register constraint not supported");
425                 if (immediate_type != '\0')
426                         panic("same as and immediate constraint not supported");
427         }
428
429         if (cls == NULL && same_as < 0) {
430                 if (!memory_possible)
431                         panic("no constraint specified for assembler input");
432         }
433
434         constraint->same_as               = same_as;
435         constraint->cls                   = cls;
436         constraint->allowed_registers     = limited;
437         constraint->all_registers_allowed = all_registers_allowed;
438         constraint->memory_possible       = memory_possible;
439         constraint->immediate_type        = immediate_type;
440 }
441
442 ir_node *gen_ASM(ir_node *node)
443 {
444         ir_node                    *block = NULL;
445         ir_node                    *new_block = NULL;
446         dbg_info                   *dbgi      = get_irn_dbg_info(node);
447         int                         i, arity;
448         int                         out_idx;
449         ir_node                   **in;
450         ir_node                    *new_node;
451         int                         out_arity;
452         int                         n_out_constraints;
453         int                         n_clobbers;
454         const arch_register_req_t **out_reg_reqs;
455         const arch_register_req_t **in_reg_reqs;
456         ia32_asm_reg_t             *register_map;
457         unsigned                    reg_map_size = 0;
458         struct obstack             *obst;
459         const ir_asm_constraint    *in_constraints;
460         const ir_asm_constraint    *out_constraints;
461         ident                     **clobbers;
462         int                         clobbers_flags = 0;
463         unsigned                    clobber_bits[N_CLASSES];
464
465         memset(&clobber_bits, 0, sizeof(clobber_bits));
466
467         switch (be_transformer) {
468         case TRANSFORMER_DEFAULT:
469                 block     = get_nodes_block(node);
470                 new_block = be_transform_node(block);
471                 break;
472
473 #ifdef FIRM_GRGEN_BE
474         case TRANSFORMER_PBQP:
475         case TRANSFORMER_RAND:
476                 new_block = get_nodes_block(node);
477                 break;
478 #endif
479
480         default:
481                 panic("invalid transformer");
482         }
483
484         /* workaround for lots of buggy code out there as most people think volatile
485          * asm is enough for everything and forget the flags (linux kernel, etc.)
486          */
487         if (get_irn_pinned(node) == op_pin_state_pinned) {
488                 clobbers_flags = 1;
489         }
490
491         arity = get_irn_arity(node);
492         in    = ALLOCANZ(ir_node*, arity);
493
494         clobbers   = get_ASM_clobbers(node);
495         n_clobbers = 0;
496         for (i = 0; i < get_ASM_n_clobbers(node); ++i) {
497                 const arch_register_req_t *req;
498                 const char                *c = get_id_str(clobbers[i]);
499
500                 if (strcmp(c, "memory") == 0)
501                         continue;
502                 if (strcmp(c, "cc") == 0) {
503                         clobbers_flags = 1;
504                         continue;
505                 }
506
507                 req = parse_clobber(c);
508                 clobber_bits[req->cls->index] |= *req->limited;
509
510                 n_clobbers++;
511         }
512         n_out_constraints = get_ASM_n_output_constraints(node);
513         out_arity         = n_out_constraints + n_clobbers;
514
515         in_constraints  = get_ASM_input_constraints(node);
516         out_constraints = get_ASM_output_constraints(node);
517
518         /* determine size of register_map */
519         for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
520                 const ir_asm_constraint *constraint = &out_constraints[out_idx];
521                 if (constraint->pos > reg_map_size)
522                         reg_map_size = constraint->pos;
523         }
524         for (i = 0; i < arity; ++i) {
525                 const ir_asm_constraint   *constraint = &in_constraints[i];
526                 if(constraint->pos > reg_map_size)
527                         reg_map_size = constraint->pos;
528         }
529         ++reg_map_size;
530
531         obst         = get_irg_obstack(current_ir_graph);
532         register_map = NEW_ARR_D(ia32_asm_reg_t, obst, reg_map_size);
533         memset(register_map, 0, reg_map_size * sizeof(register_map[0]));
534
535         /* construct output constraints */
536         out_reg_reqs = obstack_alloc(obst, out_arity * sizeof(out_reg_reqs[0]));
537
538         for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
539                 const ir_asm_constraint   *constraint = &out_constraints[out_idx];
540                 const char                *c       = get_id_str(constraint->constraint);
541                 unsigned                   pos        = constraint->pos;
542                 constraint_t               parsed_constraint;
543                 const arch_register_req_t *req;
544
545                 parse_asm_constraints(&parsed_constraint, c, 1);
546                 req = make_register_req(&parsed_constraint, n_out_constraints,
547                                         out_reg_reqs, out_idx);
548                 out_reg_reqs[out_idx] = req;
549
550                 register_map[pos].use_input = 0;
551                 register_map[pos].valid     = 1;
552                 register_map[pos].memory    = 0;
553                 register_map[pos].inout_pos = out_idx;
554                 register_map[pos].mode      = constraint->mode;
555         }
556
557         /* inputs + input constraints */
558         in_reg_reqs = obstack_alloc(obst, arity * sizeof(in_reg_reqs[0]));
559         for (i = 0; i < arity; ++i) {
560                 ir_node                   *pred         = get_irn_n(node, i);
561                 const ir_asm_constraint   *constraint   = &in_constraints[i];
562                 ident                     *constr_id    = constraint->constraint;
563                 const char                *c            = get_id_str(constr_id);
564                 unsigned                   pos          = constraint->pos;
565                 int                        is_memory_op = 0;
566                 ir_node                   *input        = NULL;
567                 unsigned                   r_clobber_bits;
568                 constraint_t               parsed_constraint;
569                 const arch_register_req_t *req;
570
571                 parse_asm_constraints(&parsed_constraint, c, 0);
572                 if (parsed_constraint.cls != NULL) {
573                         r_clobber_bits = clobber_bits[parsed_constraint.cls->index];
574                         if (r_clobber_bits != 0) {
575                                 if (parsed_constraint.all_registers_allowed) {
576                                         parsed_constraint.all_registers_allowed = 0;
577                                         be_abi_set_non_ignore_regs(env_cg->birg->abi,
578                                                         parsed_constraint.cls,
579                                                         &parsed_constraint.allowed_registers);
580                                 }
581                                 parsed_constraint.allowed_registers &= ~r_clobber_bits;
582                         }
583                 }
584
585                 req = make_register_req(&parsed_constraint, n_out_constraints,
586                                         out_reg_reqs, i);
587                 in_reg_reqs[i] = req;
588
589                 if (parsed_constraint.immediate_type != '\0') {
590                         char imm_type = parsed_constraint.immediate_type;
591                         input = try_create_Immediate(pred, imm_type);
592                 }
593
594                 if (input == NULL) {
595                         ir_node *pred = NULL;
596                         switch (be_transformer) {
597                         case TRANSFORMER_DEFAULT:
598                                 pred  = get_irn_n(node, i);
599                                 input = be_transform_node(pred);
600                                 break;
601
602 #ifdef FIRM_GRGEN_BE
603                         case TRANSFORMER_PBQP:
604                         case TRANSFORMER_RAND:
605                                 input = get_irn_n(node, i);
606                                 break;
607 #endif
608
609                         default: panic("invalid transformer");
610                         }
611
612                         if (parsed_constraint.cls == NULL
613                                         && parsed_constraint.same_as < 0) {
614                                 is_memory_op = 1;
615                         } else if(parsed_constraint.memory_possible) {
616                                 /* TODO: match Load or Load/Store if memory possible is set */
617                         }
618                 }
619                 in[i] = input;
620
621                 register_map[pos].use_input = 1;
622                 register_map[pos].valid     = 1;
623                 register_map[pos].memory    = is_memory_op;
624                 register_map[pos].inout_pos = i;
625                 register_map[pos].mode      = constraint->mode;
626         }
627
628         /* parse clobbers */
629         for (i = 0; i < get_ASM_n_clobbers(node); ++i) {
630                 const char                *c = get_id_str(clobbers[i]);
631                 const arch_register_req_t *req;
632
633                 if (strcmp(c, "memory") == 0 || strcmp(c, "cc") == 0)
634                         continue;
635
636                 req = parse_clobber(c);
637                 out_reg_reqs[out_idx] = req;
638                 ++out_idx;
639         }
640
641         new_node = new_bd_ia32_Asm(dbgi, new_block, arity, in, out_arity,
642                                    get_ASM_text(node), register_map);
643
644         if (arity == 0)
645                 be_dep_on_frame(new_node);
646
647         set_ia32_out_req_all(new_node, out_reg_reqs);
648         set_ia32_in_req_all(new_node, in_reg_reqs);
649
650         SET_IA32_ORIG_NODE(new_node, node);
651
652         return new_node;
653 }
654
655 ir_node *gen_CopyB(ir_node *node) {
656         ir_node  *block    = NULL;
657         ir_node  *src      = NULL;
658         ir_node  *new_src  = NULL;
659         ir_node  *dst      = NULL;
660         ir_node  *new_dst  = NULL;
661         ir_node  *mem      = NULL;
662         ir_node  *new_mem  = NULL;
663         ir_node  *res      = NULL;
664         dbg_info *dbgi     = get_irn_dbg_info(node);
665         int      size      = get_type_size_bytes(get_CopyB_type(node));
666         int      rem;
667
668         switch (be_transformer) {
669                 case TRANSFORMER_DEFAULT:
670                         block    = be_transform_node(get_nodes_block(node));
671                         src      = get_CopyB_src(node);
672                         new_src  = be_transform_node(src);
673                         dst      = get_CopyB_dst(node);
674                         new_dst  = be_transform_node(dst);
675                         mem      = get_CopyB_mem(node);
676                         new_mem  = be_transform_node(mem);
677                         break;
678
679 #ifdef FIRM_GRGEN_BE
680                 case TRANSFORMER_PBQP:
681                 case TRANSFORMER_RAND:
682                         block    = get_nodes_block(node);
683                         new_src  = get_CopyB_src(node);
684                         new_dst  = get_CopyB_dst(node);
685                         new_mem  = get_CopyB_mem(node);
686                         break;
687 #endif
688
689                 default: panic("invalid transformer");
690         }
691
692         /* If we have to copy more than 32 bytes, we use REP MOVSx and */
693         /* then we need the size explicitly in ECX.                    */
694         if (size >= 32 * 4) {
695                 rem = size & 0x3; /* size % 4 */
696                 size >>= 2;
697
698                 res = new_bd_ia32_Const(dbgi, block, NULL, 0, size);
699                 be_dep_on_frame(res);
700
701                 res = new_bd_ia32_CopyB(dbgi, block, new_dst, new_src, res, new_mem, rem);
702         } else {
703                 if(size == 0) {
704                         ir_fprintf(stderr, "Optimization warning copyb %+F with size <4\n",
705                                    node);
706                 }
707                 res = new_bd_ia32_CopyB_i(dbgi, block, new_dst, new_src, new_mem, size);
708         }
709
710         SET_IA32_ORIG_NODE(res, node);
711
712         return res;
713 }
714
715 ir_node *gen_Proj_tls(ir_node *node) {
716         ir_node  *block = NULL;
717         dbg_info *dbgi  = NULL;
718         ir_node  *res   = NULL;
719
720         switch (be_transformer) {
721                 case TRANSFORMER_DEFAULT:
722                         block = be_transform_node(get_nodes_block(node));
723                         break;
724
725 #ifdef FIRM_GRGEN_BE
726                 case TRANSFORMER_PBQP:
727                 case TRANSFORMER_RAND:
728                         block = get_nodes_block(node);
729                         break;
730 #endif
731
732                 default: panic("invalid transformer");
733         }
734
735         res = new_bd_ia32_LdTls(dbgi, block, mode_Iu);
736
737         return res;
738 }
739
740 ir_node *gen_Unknown(ir_node *node)
741 {
742         ir_mode *mode = get_irn_mode(node);
743
744         if (mode_is_float(mode)) {
745                 if (ia32_cg_config.use_sse2) {
746                         return ia32_new_Unknown_xmm(env_cg);
747                 } else {
748                         /* Unknown nodes are buggy in x87 simulator, use zero for now... */
749                         ir_graph *irg   = current_ir_graph;
750                         dbg_info *dbgi  = get_irn_dbg_info(node);
751                         ir_node  *block = get_irg_start_block(irg);
752                         ir_node  *ret   = new_bd_ia32_vfldz(dbgi, block);
753
754                         be_dep_on_frame(ret);
755                         return ret;
756                 }
757         } else if (ia32_mode_needs_gp_reg(mode)) {
758                 return ia32_new_Unknown_gp(env_cg);
759         } else {
760                 panic("unsupported Unknown-Mode");
761         }
762         return NULL;
763 }
764
765 const arch_register_req_t *make_register_req(const constraint_t *constraint,
766                 int n_outs, const arch_register_req_t **out_reqs, int pos)
767 {
768         struct obstack      *obst    = get_irg_obstack(current_ir_graph);
769         int                  same_as = constraint->same_as;
770         arch_register_req_t *req;
771
772         if (same_as >= 0) {
773                 const arch_register_req_t *other_constr;
774
775                 if (same_as >= n_outs)
776                         panic("invalid output number in same_as constraint");
777
778                 other_constr     = out_reqs[same_as];
779
780                 req              = obstack_alloc(obst, sizeof(req[0]));
781                 *req             = *other_constr;
782                 req->type       |= arch_register_req_type_should_be_same;
783                 req->other_same  = 1U << pos;
784
785                 /* switch constraints. This is because in firm we have same_as
786                  * constraints on the output constraints while in the gcc asm syntax
787                  * they are specified on the input constraints */
788                 out_reqs[same_as] = req;
789                 return other_constr;
790         }
791
792         /* pure memory ops */
793         if (constraint->cls == NULL) {
794                 return &no_register_req;
795         }
796
797         if (constraint->allowed_registers != 0
798                         && !constraint->all_registers_allowed) {
799                 unsigned *limited_ptr;
800
801                 req         = obstack_alloc(obst, sizeof(req[0]) + sizeof(unsigned));
802                 memset(req, 0, sizeof(req[0]));
803                 limited_ptr = (unsigned*) (req+1);
804
805                 req->type    = arch_register_req_type_limited;
806                 *limited_ptr = constraint->allowed_registers;
807                 req->limited = limited_ptr;
808         } else {
809                 req       = obstack_alloc(obst, sizeof(req[0]));
810                 memset(req, 0, sizeof(req[0]));
811                 req->type = arch_register_req_type_normal;
812         }
813         req->cls = constraint->cls;
814
815         return req;
816 }
817
818 const arch_register_req_t *parse_clobber(const char *clobber)
819 {
820         struct obstack        *obst = get_irg_obstack(current_ir_graph);
821         const arch_register_t *reg  = ia32_get_clobber_register(clobber);
822         arch_register_req_t   *req;
823         unsigned              *limited;
824
825         if(reg == NULL) {
826                 panic("Register '%s' mentioned in asm clobber is unknown", clobber);
827         }
828
829         assert(reg->index < 32);
830
831         limited  = obstack_alloc(obst, sizeof(limited[0]));
832         *limited = 1 << reg->index;
833
834         req          = obstack_alloc(obst, sizeof(req[0]));
835         memset(req, 0, sizeof(req[0]));
836         req->type    = arch_register_req_type_limited;
837         req->cls     = arch_register_get_class(reg);
838         req->limited = limited;
839
840         return req;
841 }
842
843
844 int prevents_AM(ir_node *const block, ir_node *const am_candidate,
845                        ir_node *const other)
846 {
847         if (get_nodes_block(other) != block)
848                 return 0;
849
850         if (is_Sync(other)) {
851                 int i;
852
853                 for (i = get_Sync_n_preds(other) - 1; i >= 0; --i) {
854                         ir_node *const pred = get_Sync_pred(other, i);
855
856                         if (get_nodes_block(pred) != block)
857                                 continue;
858
859                         /* Do not block ourselves from getting eaten */
860                         if (is_Proj(pred) && get_Proj_pred(pred) == am_candidate)
861                                 continue;
862
863                         if (!heights_reachable_in_block(heights, pred, am_candidate))
864                                 continue;
865
866                         return 1;
867                 }
868
869                 return 0;
870         } else {
871                 /* Do not block ourselves from getting eaten */
872                 if (is_Proj(other) && get_Proj_pred(other) == am_candidate)
873                         return 0;
874
875                 if (!heights_reachable_in_block(heights, other, am_candidate))
876                         return 0;
877
878                 return 1;
879         }
880 }
881
882 ir_node *try_create_Immediate(ir_node *node, char immediate_constraint_type)
883 {
884         int          minus         = 0;
885         tarval      *offset        = NULL;
886         int          offset_sign   = 0;
887         long         val = 0;
888         ir_entity   *symconst_ent  = NULL;
889         int          symconst_sign = 0;
890         ir_mode     *mode;
891         ir_node     *cnst          = NULL;
892         ir_node     *symconst      = NULL;
893         ir_node     *new_node;
894
895         mode = get_irn_mode(node);
896         if(!mode_is_int(mode) && !mode_is_reference(mode)) {
897                 return NULL;
898         }
899
900         if(is_Minus(node)) {
901                 minus = 1;
902                 node  = get_Minus_op(node);
903         }
904
905         if(is_Const(node)) {
906                 cnst        = node;
907                 symconst    = NULL;
908                 offset_sign = minus;
909         } else if(is_SymConst(node)) {
910                 cnst          = NULL;
911                 symconst      = node;
912                 symconst_sign = minus;
913         } else if(is_Add(node)) {
914                 ir_node *left  = get_Add_left(node);
915                 ir_node *right = get_Add_right(node);
916                 if(is_Const(left) && is_SymConst(right)) {
917                         cnst          = left;
918                         symconst      = right;
919                         symconst_sign = minus;
920                         offset_sign   = minus;
921                 } else if(is_SymConst(left) && is_Const(right)) {
922                         cnst          = right;
923                         symconst      = left;
924                         symconst_sign = minus;
925                         offset_sign   = minus;
926                 }
927         } else if(is_Sub(node)) {
928                 ir_node *left  = get_Sub_left(node);
929                 ir_node *right = get_Sub_right(node);
930                 if(is_Const(left) && is_SymConst(right)) {
931                         cnst          = left;
932                         symconst      = right;
933                         symconst_sign = !minus;
934                         offset_sign   = minus;
935                 } else if(is_SymConst(left) && is_Const(right)) {
936                         cnst          = right;
937                         symconst      = left;
938                         symconst_sign = minus;
939                         offset_sign   = !minus;
940                 }
941         } else {
942                 return NULL;
943         }
944
945         if(cnst != NULL) {
946                 offset = get_Const_tarval(cnst);
947                 if(tarval_is_long(offset)) {
948                         val = get_tarval_long(offset);
949                 } else {
950                         ir_fprintf(stderr, "Optimisation Warning: tarval from %+F is not a "
951                                    "long?\n", cnst);
952                         return NULL;
953                 }
954
955                 if(!check_immediate_constraint(val, immediate_constraint_type))
956                         return NULL;
957         }
958         if(symconst != NULL) {
959                 if(immediate_constraint_type != 0) {
960                         /* we need full 32bits for symconsts */
961                         return NULL;
962                 }
963
964                 /* unfortunately the assembler/linker doesn't support -symconst */
965                 if(symconst_sign)
966                         return NULL;
967
968                 if(get_SymConst_kind(symconst) != symconst_addr_ent)
969                         return NULL;
970                 symconst_ent = get_SymConst_entity(symconst);
971         }
972         if(cnst == NULL && symconst == NULL)
973                 return NULL;
974
975         if(offset_sign && offset != NULL) {
976                 offset = tarval_neg(offset);
977         }
978
979         new_node = create_Immediate(symconst_ent, symconst_sign, val);
980
981         return new_node;
982 }