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