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