added used_x87 flag, so the x87 simulator runs only if fp
[libfirm] / ir / be / scripts / generate_regalloc_if.pl
1 #!/usr/bin/perl -w
2
3 # This script generates C code which emits assembler code for the
4 # assembler ir nodes. It takes a "emit" key from the node specification
5 # and substitutes lines starting with . with a corresponding fprintf().
6 # Creation: 2005/11/14
7 # $Id$
8
9 use strict;
10 use Data::Dumper;
11
12 my $specfile   = $ARGV[0];
13 my $target_dir = $ARGV[1];
14
15 our $arch;
16 our %reg_classes;
17 our %nodes;
18
19 # include spec file
20
21 my $return;
22
23 no strict "subs";
24 unless ($return = do $specfile) {
25         warn "couldn't parse $specfile: $@" if $@;
26         warn "couldn't do $specfile: $!"    unless defined $return;
27         warn "couldn't run $specfile"       unless $return;
28 }
29 use strict "subs";
30
31 my $target_c   = $target_dir."/gen_".$arch."_regalloc_if.c";
32 my $target_h   = $target_dir."/gen_".$arch."_regalloc_if.h";
33 my $target_h_t = $target_dir."/gen_".$arch."_regalloc_if_t.h";
34
35 # helper function
36 sub translate_reg_type {
37         my $t = shift;
38
39         if ($t == 0) {
40                 return "arch_register_type_none";
41         }
42         else {
43                 my @types;
44
45                 if ($t & 1) {
46                         push(@types, "arch_register_type_caller_save");
47                 }
48
49                 if ($t & 2) {
50                         push(@types, "arch_register_type_callee_save");
51                 }
52
53                 if ($t & 4) {
54                         push(@types, "arch_register_type_ignore");
55                 }
56
57                 return join(" | ", @types);
58         }
59 }
60
61 # stacks for output
62 my @obst_regtypes_def; # stack for the register type variables definitions
63 my @obst_regtypes_decl;# stack for the register type variables declarations
64 my @obst_regclasses;   # stack for the register class variables
65 my @obst_classdef;     # stack to define a name for a class index
66 my @obst_regdef;       # stack to define a name for a register index
67 my @obst_reginit;      # stack for the register type inits
68 my @obst_req;          # stack for the register requirements
69 my @obst_limit_func;   # stack for functions to return a subset of a register class
70 my @obst_defreq_head;  # stack for prototypes of default requirement function
71 my @obst_header_all;   # stack for some extern struct defs needed for bearch_$arch include
72 my @obst_requirement_def;  # stack for requirement name defines
73
74 my $numregs;
75 my $class_ptr;
76 my $class_idx = 0;
77
78 my $tmp;
79
80 my %reg2class;
81
82 # there is a default NONE requirement
83 $tmp = "/* Default NONE register requirements */\n";
84 $tmp .= "const $arch\_register_req_t $arch\_default_req_none = {\n";
85 $tmp .= "  {\n";
86 $tmp .= "    arch_register_req_type_none,  /* register type  */\n";
87 $tmp .= "    NULL,                         /* register class */\n";
88 $tmp .= "    NULL,                         /* limit function */\n";
89 $tmp .= "    NULL,                         /* limit environment */\n";
90 $tmp .= "    NULL,                         /* node for same */\n";
91 $tmp .= "    NULL                          /* node for different */\n";
92 $tmp .= "  },\n";
93 $tmp .= "  0,                              /* same pos */\n";
94 $tmp .= "  0                               /* different pos */\n";
95 $tmp .= "};\n\n";
96 push(@obst_req, $tmp);
97 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_none;\n");
98
99 push(@obst_classdef, "enum reg_classes {\n");
100
101 my $class_mode;
102
103 # generate register type and class variable, init function and default requirements
104 foreach my $class_name (keys(%reg_classes)) {
105         my @class         = @{ $reg_classes{"$class_name"} };
106         my $old_classname = $class_name;
107
108         $class_name = $arch."_".$class_name;
109         $numregs    = "N_".$class_name."_REGS";
110         $class_ptr  = "&".$arch."_reg_classes[CLASS_".$class_name."]";
111         $class_mode = pop(@class)->{"mode"};
112
113         push(@obst_regtypes_decl, "extern arch_register_t ".$class_name."_regs[$numregs];\n");
114         push(@obst_regtypes_def, "arch_register_t ".$class_name."_regs[$numregs];\n");
115
116         push(@obst_classdef, "  CLASS_$class_name = $class_idx,\n");
117         push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
118
119         # there is a default NORMAL requirement for each class
120         $tmp  = "/* Default NORMAL register requirements for class $class_name */\n";
121         $tmp .= "const $arch\_register_req_t $arch\_default_req_$class_name = {\n";
122         $tmp .= "  {\n";
123         $tmp .= "    arch_register_req_type_normal,\n";
124         $tmp .= "    $class_ptr,\n";
125         $tmp .= "    NULL,                    /* limit function */ \n";
126         $tmp .= "    NULL,                    /* limit environment */\n";
127         $tmp .= "    NULL,                    /* node for same */\n";
128         $tmp .= "    NULL                     /* node for different */\n";
129         $tmp .= "  },\n";
130         $tmp .= "  0,                         /* same pos */\n";
131         $tmp .= "  0                          /* different pos */\n";
132         $tmp .= "};\n\n";
133         push(@obst_req, $tmp);
134         push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_$class_name;\n");
135
136         my $idx = 0;
137         push(@obst_reginit, "  /* Init of all registers in class '$class_name' */\n\n");
138         push(@obst_reginit, "  /* set largest possible mode for '$class_name' */\n");
139         push(@obst_reginit, "  $arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
140         push(@obst_regdef, "enum reg_".$class_name."_values {\n");
141         foreach (@class) {
142                 # For each class we build for each of it's member registers a limit function
143                 # which limits the class to this particular register. We also build the
144                 # corresponding requirement structs.
145                 # We need those functions to set register requirements on demand in transformation
146                 # esp. for Call and RegParams where we can mix int and float parameters.
147
148                 my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
149
150                 # push the function prototype
151                 $tmp = "void $limit_func_name(void *_unused, bitset_t *bs)";
152                 push(@obst_defreq_head, $tmp.";\n");
153
154                 # push the function definition
155                 $tmp .= " {\n";
156                 $tmp .= "    bs = bitset_clear_all(bs);\n";
157                 $tmp .= "    bitset_set(bs, REG_".uc($_->{"name"}).");\n";  # REGISTER to index assignment is done some lines down
158                 $tmp .= "}\n\n";
159                 push(@obst_limit_func, $tmp);
160
161                 # push the default requirement struct
162                 $tmp  = "const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}." = {\n";
163                 $tmp .= "  {\n";
164                 $tmp .= "    arch_register_req_type_limited,\n";
165                 $tmp .= "    $class_ptr,\n";
166                 $tmp .= "    $limit_func_name,\n";
167                 $tmp .= "    NULL,                     /* limit environment */\n";
168                 $tmp .= "    NULL,                     /* node for same */\n";
169                 $tmp .= "    NULL                      /* node for different */\n";
170                 $tmp .= "  },\n";
171                 $tmp .= "  0,                          /* same pos */\n";
172                 $tmp .= "  0                           /* different pos */\n";
173                 $tmp .= "};\n\n";
174                 push(@obst_req, $tmp);
175                 push(@obst_header_all,"extern const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}.";\n");
176
177                 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
178                 push(@obst_regdef, "  REG_".uc($_->{"name"})." = $idx,\n");
179                 push(@obst_reginit, "  ".$class_name."_regs[$idx].name      = \"".$_->{"name"}."\";\n");
180                 push(@obst_reginit, "  ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
181                 push(@obst_reginit, "  ".$class_name."_regs[$idx].index     = $idx;\n");
182                 push(@obst_reginit, "  ".$class_name."_regs[$idx].type      = ".translate_reg_type($_->{"type"}).";\n");
183                 push(@obst_reginit, "\n");
184                 $idx++;
185         }
186         push(@obst_regdef, "  $numregs = $idx\n");
187         push(@obst_regdef, "};\n\n");
188
189         $class_idx++;
190 }
191
192 push(@obst_classdef, "  N_CLASSES = ".scalar(keys(%reg_classes))."\n");
193 push(@obst_classdef, "};\n\n");
194
195 # generate node-register constraints
196 foreach my $op (keys(%nodes)) {
197         my %n = %{ $nodes{"$op"} };
198
199         next if (!exists($n{"reg_req"}));
200
201         $op = $arch."_".$op;
202
203         push(@obst_req, "/* IN requirements for '$op' */\n");
204         # check for argument requirements
205         if (exists($n{"reg_req"}{"in"})) {
206                 generate_requirements(\%n, $op, "in");
207         }
208
209         push(@obst_req, "/* OUT requirements for '$op' */\n");
210         # check for result requirements
211         if (exists($n{"reg_req"}{"out"})) {
212                 generate_requirements(\%n, $op, "out");
213         }
214 }
215
216
217
218 # generate header _t (internal usage) file
219 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
220
221 my $creation_time = localtime(time());
222
223 $tmp = uc($arch);
224
225 print OUT<<EOF;
226 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
227 #define _GEN_$tmp\_REGALLOC_IF_T_H_
228
229 /**
230  * Generated register classes from spec.
231  *
232  * DO NOT EDIT THIS FILE, your changes will be lost.
233  * Edit $specfile instead.
234  * created by: $0 $specfile $target_dir
235  * date:       $creation_time
236  */
237
238 EOF
239
240 print OUT @obst_requirement_def;
241
242 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
243
244
245
246 # generate header (external usage) file
247 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
248
249 $creation_time = localtime(time());
250
251 print OUT<<EOF;
252 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
253 #define _GEN_$tmp\_REGALLOC_IF_H_
254
255 /**
256  * Contains additional external requirements defs for external includes.
257  *
258  * DO NOT EDIT THIS FILE, your changes will be lost.
259  * Edit $specfile instead.
260  * created by: $0 $specfile $target_dir
261  * date:       $creation_time
262  */
263
264 #include "../bearch.h"
265 #include "$arch\_nodes_attr.h"
266
267 EOF
268
269 print OUT @obst_regdef, "\n";
270
271 print OUT @obst_classdef, "\n";
272
273 print OUT @obst_regtypes_decl, "\n";
274
275 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
276
277 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
278
279 print OUT @obst_header_all, "\n";
280
281 print OUT @obst_defreq_head, "\n";
282
283 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
284
285 close(OUT);
286
287
288
289 # generate c inline file
290 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
291
292 $creation_time = localtime(time());
293
294 print OUT<<EOF;
295 /**
296  * The generated interface for the register allocator.
297  * Contains register classes and types and register constraints
298  * for all nodes where constraints were given in spec.
299  *
300  * DO NOT EDIT THIS FILE, your changes will be lost.
301  * Edit $specfile instead.
302  * created by: $0 $specfile $target_dir
303  * date:       $creation_time
304  */
305
306 #include "gen_$arch\_regalloc_if.h"
307 #include "bearch_$arch\_t.h"   /* we need this to put the caller saved registers into the isa set */
308 #include "$arch\_map_regs.h"
309 #include "irmode.h"
310
311 EOF
312
313 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n  ".join(",\n  ", @obst_regclasses)."\n};\n\n";
314
315 print OUT @obst_regtypes_def, "\n";
316
317 print OUT "void ".$arch."_register_init(void *isa_ptr) {\n";
318 print OUT @obst_reginit;
319 print OUT "}\n\n";
320
321 print OUT @obst_limit_func;
322
323 print OUT @obst_req;
324
325 close(OUT);
326
327 ###
328 # Remember the register class for each index in the given requirements.
329 # We need this information for requirements like "in_sX" or "out_dX"
330 # @return array of classes corresponding to the requirement for each index
331 ###
332 sub build_inout_idx_class {
333         my $n     = shift;
334         my $op    = shift;
335         my $inout = shift;
336         my @idx_class;
337
338         if (exists($n->{"reg_req"}{"$inout"})) {
339                 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
340
341                 for (my $idx = 0; $idx <= $#reqs; $idx++) {
342                         my $class = undef;
343
344                         if ($reqs[$idx] eq "none") {
345                                 $class = "none";
346                         }
347                         elsif (is_reg_class($reqs[$idx])) {
348                                 $class = $reqs[$idx];
349                         }
350                         else {
351                                 my @regs = split(/ /, $reqs[$idx]);
352 GET_CLASS:              foreach my $reg (@regs) {
353                                         if ($reg =~ /!?(in|out)\_r\d+/) {
354                                                 $class = "UNKNOWN_CLASS";
355                                         }
356                                         else {
357                                                 $class = get_reg_class($reg);
358                                                 if (!defined $class) {
359                                                         die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
360                                                 }
361                                                 else {
362                                                         last GET_CLASS;
363                                                 } # !defined class
364                                         } # if (reg =~ ...
365                                 } # foreach
366                         } # if
367
368                         push(@idx_class, $class);
369                 } # for
370         } # if
371
372         return @idx_class;
373 }
374
375 ###
376 # Generates the requirements for the given description
377 ###
378 sub generate_requirements {
379         my $n     = shift;
380         my $op    = shift;
381         my $inout = shift;
382
383         # get classes for the complementary direction
384         my $outin     = ($inout eq "in") ? "out" : "in";
385
386         my @reqs = @{ $n->{"reg_req"}{"$inout"} };
387
388         for (my $idx = 0; $idx <= $#reqs; $idx++) {
389                 my $class = undef;
390
391                 my $tmp2 = "const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx = ";
392                 my $tmp  = "#define ".$op."_reg_req_$inout\_$idx ";
393
394                 if ($reqs[$idx] eq "none") {
395                         $tmp .= "&$arch\_default_req_none\n";
396                 }
397                 elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
398                         if (is_reg_class($1)) {
399                                 $tmp  .=  "&_".$op."_reg_req_$inout\_$idx\n";
400                                 $tmp2 .= "{\n";
401                                 $tmp2 .= "  {\n";
402                                 $tmp2 .= "    arch_register_req_type_should_be_different_from_all,\n";
403                                 $tmp2 .= "    &$arch\_reg_classes[CLASS_$arch\_".$1."],\n";
404                                 $tmp2 .= "    NULL,        /* limit function */\n";
405                                 $tmp2 .= "    NULL,        /* limit environment */\n";
406                                 $tmp2 .= "    NULL,        /* same node */\n";
407                                 $tmp2 .= "    NULL         /* different node */\n";
408                                 $tmp2 .= "  },\n";
409                                 $tmp2 .= "  0,\n";
410                                 $tmp2 .= "  0\n";
411                                 $tmp2 .= "};\n";
412
413                                 push(@obst_req, $tmp2."\n");
414                                 push(@obst_header_all, "extern const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx;\n");
415                         }
416                         else {
417                                 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
418                         }
419                 }
420                 elsif (is_reg_class($reqs[$idx])) {
421                         $tmp .= "&$arch\_default_req_".$arch."_".$reqs[$idx]."\n";
422                 }
423                 else {
424                         my @req_type_mask;
425                         my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
426
427                         if (!defined($class)) {
428                                 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
429                         }
430
431                         if ($has_limit) {
432                                 push(@req_type_mask, "arch_register_req_type_limited");
433                         }
434                         if (defined($same_pos)) {
435                                 push(@req_type_mask, "arch_register_req_type_should_be_same");
436                         }
437                         if (defined($different_pos)) {
438                                 push(@req_type_mask, "arch_register_req_type_should_be_different");
439                         }
440
441                         $tmp  .= "&_".$op."_reg_req_$inout\_$idx\n";
442                         $tmp2 .= "{\n";
443                         $tmp2 .= "  {\n";
444                         $tmp2 .= "    ".join(" | ", @req_type_mask).",\n";
445                         $tmp2 .= "    &$arch\_reg_classes[CLASS_$arch\_".$class."],\n";
446                         $tmp2 .= "    ".($has_limit ? "limit_reg_".$op."_$inout\_".$idx : "NULL").",\n";
447                         $tmp2 .= "    NULL,        /* limit environment */\n";
448                         $tmp2 .= "    NULL,        /* same node */\n";
449                         $tmp2 .= "    NULL         /* different node */\n";
450                         $tmp2 .= "  },\n";
451                         $tmp2 .= "  ".(defined($same_pos) ? $same_pos : "0").",\n";
452                         $tmp2 .= "  ".(defined($different_pos) ? $different_pos : "0")."\n";
453                         $tmp2 .= "};\n";
454
455                         push(@obst_req, $tmp2."\n");
456                         push(@obst_header_all, "extern const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx;\n");
457                 }
458
459                 push(@obst_requirement_def, $tmp);
460         }
461
462 }
463
464 ###
465 # Determines whether $name is a specified register class or not.
466 # @return 1 if name is register class, 0 otherwise
467 ###
468 sub is_reg_class {
469         my $name = shift;
470         return 1 if exists($reg_classes{"$name"});
471         return 0;
472 }
473
474 ###
475 # Returns the register class for a given register.
476 # @return class or undef
477 ###
478 sub get_reg_class {
479         my $reg = shift;
480         $reg = substr($reg, 1) if ($reg =~ /!.*/);
481         return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
482         return undef;
483 }
484
485 ###
486 # Returns the index of a given register within it's register class.
487 # @return index or undef
488 ###
489 sub get_reg_index {
490         my $reg = shift;
491         return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
492         return undef;
493 }
494
495 ###
496 # Generates the function for a given $op and a given IN-index
497 # which returns a subset of possible register from a register class
498 # @return classname from which the subset is derived or undef and
499 #         pos which corresponds to in/out reference position or undef
500 ###
501 sub build_subset_class_func {
502         my $neg           = undef;
503         my $class         = undef;
504         my $has_limit     = 0;
505         my $same_pos      = undef;
506         my $different_pos = undef;
507         my $temp;
508         my @temp_obst;
509
510
511         # build function header
512         my $n   = shift;
513         my $op  = shift;
514         my $idx = shift;
515         my $in  = shift;
516
517         my $outin = $in ? "out" : "in";
518         my @regs  = split(/ /, shift);
519
520         my @idx_class = build_inout_idx_class($n, $op, $outin);
521
522         # set/unset registers
523 CHECK_REQS: foreach (@regs) {
524                 if (/(!)?$outin\_r(\d+)/) {
525                         if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
526                                 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
527                                 return (undef, undef, undef, undef);
528                         }
529
530                         if ($1) {
531                                 $different_pos = $in ? -$2 : $2 - 1;
532                         }
533                         else {
534                                 $same_pos = $in ? -$2 : $2 - 1;
535                         }
536
537                         $class = $idx_class[$2 - 1];
538                         next CHECK_REQS;
539                 }
540
541                 # check for negate
542                 if (substr($_, 0, 1) eq "!") {
543                         if (defined($neg) && $neg == 0) {
544                                 # we have seen a positiv constraint as first one but this one is negative
545                                 # this doesn't make sense
546                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
547                                 return (undef, undef, undef, undef);
548                         }
549
550                         if (!defined($neg)) {
551                                 $has_limit = 1;
552                                 push(@temp_obst, "  bs = bitset_set_all(bs);     /* allow all register (negative constraints given) */\n");
553                         }
554
555                         $_   = substr($_, 1); # skip '!'
556                         $neg = 1;
557                 }
558                 else {
559                         if (defined($neg) && $neg == 1) {
560                                 # we have seen a negative constraint as first one but this one is positive
561                                 # this doesn't make sense
562                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
563                                 return (undef, undef, undef, undef);
564                         }
565
566                         if (!defined($neg)) {
567                                 $has_limit = 1;
568                                 push(@temp_obst, "  bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */\n");
569                         }
570                         $neg = 0;
571                 }
572
573                 # check if register belongs to one of the given classes
574                 $temp = get_reg_class($_);
575                 if (!defined($temp)) {
576                         print STDERR "Unknown register '$_'!\n";
577                         return (undef, undef, undef, undef);
578                 }
579
580                 # set class
581                 if (!defined($class)) {
582                         $class = $temp;
583                 }
584                 elsif ($class ne $temp) {
585                         # all registers must belong to the same class
586                         print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
587                         return (undef, undef, undef, undef);
588                 }
589
590                 if ($neg == 1) {
591                         $has_limit = 1;
592                         push(@temp_obst, "  bitset_clear(bs, ".get_reg_index($_).");         /* disallow $_ */\n");
593                 }
594                 else {
595                         $has_limit = 1;
596                         push(@temp_obst, "  bitset_set(bs, ".get_reg_index($_).");           /* allow $_ */\n");
597                 }
598         }
599
600         my @cur_class = @{ $reg_classes{"$class"} };
601         for (my $idx = 0; $idx <= $#cur_class; $idx++) {
602                 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
603                         push(@temp_obst, "  bitset_clear(bs, ".get_reg_index($cur_class[$idx]{"name"}).");");
604                         push(@temp_obst, "         /* disallow ignore reg ".$cur_class[$idx]{"name"}." */\n");
605                 }
606         }
607
608         if ($has_limit == 1) {
609                 push(@obst_header_all, "void limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(void *_unused, bitset_t *bs);\n");
610
611                 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
612                 push(@obst_limit_func, "void limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(void *_unused, bitset_t *bs) {\n");
613                 push(@obst_limit_func, @temp_obst);
614                 push(@obst_limit_func, "}\n\n");
615         }
616
617         return ($class, $has_limit, $same_pos, $different_pos);
618 }