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