fixed requirement generation
[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 my @rt = ("arch_register_type_none",
37           "arch_register_type_write_invariant",
38           "arch_register_type_caller_saved",
39           "arch_register_type_callee_saved",
40           "arch_register_type_ignore");
41
42 # stacks for output
43 my @obst_regtypes;     # stack for the register type variables
44 my @obst_regclasses;   # stack for the register class variables
45 my @obst_classdef;     # stack to define a name for a class index
46 my @obst_regdef;       # stack to define a name for a register index
47 my @obst_reginit;      # stack for the register type inits
48 my @obst_req;          # stack for the register requirements
49 my @obst_limit_func;   # stack for functions to return a subset of a register class
50 my @obst_defreq_head;  # stack for prototypes of default requirement function
51 my @obst_header_all;   # stack for some extern struct defs needed for bearch_$arch include
52
53 my $numregs;
54 my $class_ptr;
55 my $class_idx = 0;
56
57 my $tmp;
58
59 my %reg2class;
60
61 # there is a default NONE requirement
62 $tmp = "/* Default NONE register requirements */\n";
63 $tmp .= "const $arch\_register_req_t $arch\_default_req_none = {\n";
64 $tmp .= "  {\n";
65 $tmp .= "    arch_register_req_type_none,\n";
66 $tmp .= "    NULL,\n";
67 $tmp .= "    NULL,\n";
68 $tmp .= "    NULL\n";
69 $tmp .= "  },\n";
70 $tmp .= "  0\n";
71 $tmp .= "};\n\n";
72 push(@obst_req, $tmp);
73
74 push(@obst_header_all, "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n");
75 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_none;\n");
76
77 push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
78
79 # generate register type and class variable, init function and default requirements
80 foreach my $class_name (keys(%reg_classes)) {
81         my @class         = @{ $reg_classes{"$class_name"} };
82         my $old_classname = $class_name;
83
84         $class_name = $arch."_".$class_name;
85         $numregs    = "N_".$class_name."_REGS";
86         $class_ptr  = "&".$arch."_reg_classes[CLASS_".$class_name."]";
87
88         push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
89         push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
90
91         push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
92         push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
93
94         # there is a default NORMAL requirement for each class
95         $tmp  = "/* Default NORMAL register requirements for class $class_name */\n";
96         $tmp .= "const $arch\_register_req_t $arch\_default_req_$class_name = {\n";
97         $tmp .= "  {\n";
98         $tmp .= "    arch_register_req_type_normal,\n";
99         $tmp .= "    $class_ptr,\n";
100         $tmp .= "    NULL,\n";
101         $tmp .= "    NULL\n";
102         $tmp .= "  },\n";
103         $tmp .= "  0\n";
104         $tmp .= "};\n\n";
105         push(@obst_req, $tmp);
106
107         push(@obst_header_all, "\nextern const $arch\_register_req_t $arch\_default_req_$class_name;\n");
108
109         my $idx = 0;
110         push(@obst_reginit, "  /* Init of all registers in class '$class_name' */\n\n");
111         foreach (@class) {
112                 # For each class we build for each of it's member registers a limit function
113                 # which limits the class to this particular register. We also build the
114                 # corresponding requirement structs.
115                 # We need those functions to set register requirements on demand in transformation
116                 # esp. for Call and RegParams where we can mix int and float parameters.
117
118                 my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
119
120                 # push the function prototype
121                 $tmp = "int $limit_func_name(const ir_node *irn, int pos, bitset_t *bs)";
122                 push(@obst_defreq_head, $tmp.";\n");
123
124                 # push the function definition
125                 $tmp .= " {\n";
126                 $tmp .= "    bs = bitset_clear_all(bs);\n";
127                 $tmp .= "    bitset_set(bs, REG_".uc($_->{"name"}).");\n";  # REGISTER to index assignment is done some lines down
128                 $tmp .= "    return 1;\n";
129                 $tmp .= "}\n\n";
130                 push(@obst_limit_func, $tmp);
131
132                 # push the default requirement struct
133                 $tmp  = "const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}." = {\n";
134                 $tmp .= "  {\n";
135                 $tmp .= "    arch_register_req_type_limited,\n";
136                 $tmp .= "    $class_ptr,\n";
137                 $tmp .= "    $limit_func_name,\n";
138                 $tmp .= "    NULL\n";
139                 $tmp .= "  },\n";
140                 $tmp .= "  0\n";
141                 $tmp .= "};\n\n";
142                 push(@obst_req, $tmp);
143
144                 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}.";\n");
145
146                 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
147                 push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n");
148                 push(@obst_reginit, "  ".$class_name."_regs[$idx].name      = \"".$_->{"name"}."\";\n");
149                 push(@obst_reginit, "  ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
150                 push(@obst_reginit, "  ".$class_name."_regs[$idx].index     = $idx;\n");
151                 push(@obst_reginit, "  ".$class_name."_regs[$idx].type      = ".$rt[$_->{"type"}].";\n\n");
152                 $idx++;
153         }
154
155         $class_idx++;
156 }
157
158 push(@obst_header_all, "\n/* node specific requirements */\n");
159
160 # generate node-register constraints
161 foreach my $op (keys(%nodes)) {
162         my %n = %{ $nodes{"$op"} };
163
164         next if (!exists($n{"reg_req"}));
165
166         $op = $arch."_".$op;
167
168         push(@obst_req, "/* IN requirements for '$op' */\n");
169         # check for argument requirements
170         if (exists($n{"reg_req"}{"in"})) {
171                 generate_requirements(\%n, $op, "in");
172         }
173
174         push(@obst_req, "/* OUT requirements for '$op' */\n");
175         # check for result requirements
176         if (exists($n{"reg_req"}{"out"})) {
177                 generate_requirements(\%n, $op, "out");
178         }
179 }
180
181
182
183 # generate header _t (internal usage) file
184 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
185
186 my $creation_time = localtime(time());
187
188 $tmp = uc($arch);
189
190 print OUT<<EOF;
191 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
192 #define _GEN_$tmp\_REGALLOC_IF_T_H_
193
194 /**
195  * Generated register classes from spec.
196  *
197  * DO NOT EDIT THIS FILE, your changes will be lost.
198  * Edit $specfile instead.
199  * created by: $0 $specfile $target_dir
200  * date:       $creation_time
201  */
202
203 #include "../bearch.h"
204 #include "$arch\_nodes_attr.h"
205
206 EOF
207
208 print OUT @obst_regdef, "\n";
209
210 print OUT @obst_classdef, "\n";
211
212 print OUT @obst_regtypes, "\n";
213
214 print OUT @obst_defreq_head, "\n";
215
216 print OUT "void ".$arch."_register_init(void);\n\n";
217
218 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
219
220
221
222 # generate header (external usage) file
223 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
224
225 $creation_time = localtime(time());
226
227 print OUT<<EOF;
228 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
229 #define _GEN_$tmp\_REGALLOC_IF_H_
230
231 /**
232  * Contains additional external requirements defs for external includes.
233  *
234  * DO NOT EDIT THIS FILE, your changes will be lost.
235  * Edit $specfile instead.
236  * created by: $0 $specfile $target_dir
237  * date:       $creation_time
238  */
239
240 #include "gen_$arch\_regalloc_if_t.h"
241
242 EOF
243
244 print OUT @obst_header_all;
245
246 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
247
248 close(OUT);
249
250
251
252 # generate c inline file
253 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
254
255 $creation_time = localtime(time());
256
257 print OUT<<EOF;
258 /**
259  * The generated interface for the register allocator.
260  * Contains register classes and types and register constraints
261  * for all nodes where constraints were given in spec.
262  *
263  * DO NOT EDIT THIS FILE, your changes will be lost.
264  * Edit $specfile instead.
265  * created by: $0 $specfile $target_dir
266  * date:       $creation_time
267  */
268
269 #include "gen_$arch\_regalloc_if_t.h"
270
271 EOF
272
273 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n  ".join(",\n  ", @obst_regclasses)."\n};\n\n";
274
275 print OUT "void ".$arch."_register_init(void) {\n";
276 print OUT @obst_reginit;
277 print OUT "}\n\n";
278
279 print OUT @obst_limit_func;
280 print OUT @obst_req;
281
282 close(OUT);
283
284 ###
285 # Remember the register class for each index in the given requirements.
286 # We need this information for requirements like "in_sX" or "out_dX"
287 # @return array of classes corresponding to the requirement for each index
288 ###
289 sub build_inout_idx_class {
290         my $n     = shift;
291         my $op    = shift;
292         my $inout = shift;
293         my @idx_class;
294
295         if (exists($n->{"reg_req"}{"$inout"})) {
296                 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
297
298                 for (my $idx = 0; $idx <= $#reqs; $idx++) {
299                         my $class = undef;
300
301                         if ($reqs[$idx] eq "none") {
302                                 $class = "none";
303                         }
304                         elsif (is_reg_class($reqs[$idx])) {
305                                 $class = $reqs[$idx];
306                         }
307                         else {
308                                 my @regs = split(/ /, $reqs[$idx]);
309 GET_CLASS:              foreach my $reg (@regs) {
310                                         if ($reg =~ /!?(in|out)\_r\d+/) {
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                 my $tmp  = "const $arch\_register_req_t *".$op."_reg_req_$inout\_$idx = ";
350
351                 push(@obst_header_all, "extern const $arch\_register_req_t *".$op."_reg_req_$inout\_$idx;\n");
352
353                 if ($reqs[$idx] eq "none") {
354                         $tmp .= "&$arch\_default_req_none;\n";
355                 }
356                 elsif (is_reg_class($reqs[$idx])) {
357                         $tmp .= "&$arch\_default_req_".$arch."_".$reqs[$idx].";\n";
358                 }
359                 else {
360                         my @req_type_mask;
361                         my ($class, $has_limit, $pos, $same) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
362                         if (!defined($class)) {
363                                 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
364                         }
365                         if ($has_limit) {
366                                 push(@req_type_mask, "arch_register_req_type_limited");
367                         }
368                         if (defined($pos)) {
369                                 push(@req_type_mask, "arch_register_req_type_should_be_".($same ? "same" : "different"));
370                         }
371                         $tmp  .= "&_".$op."_reg_req_$inout\_$idx;\n";
372                         $tmp2 .= " {\n";
373                         $tmp2 .= "  {\n";
374                         $tmp2 .= "    ".join(" | ", @req_type_mask).",\n";
375                         $tmp2 .= "    &$arch\_reg_classes[CLASS_$arch\_".$class."],\n";
376                         $tmp2 .= "    ".($has_limit ? "limit_reg_".$op."_$inout\_".$idx : "NULL").",\n";
377                         $tmp2 .= "    NULL\n";
378                         $tmp2 .= "  },\n";
379                         $tmp2 .= "  ".(defined($pos) ? $pos : "0")."\n};\n";
380
381                         $tmp   = $tmp2.$tmp;
382                 }
383
384                 push(@obst_req, $tmp."\n");
385         }
386
387 }
388
389 ###
390 # Determines whether $name is a specified register class or not.
391 # @return 1 if name is register class, 0 otherwise
392 ###
393 sub is_reg_class {
394         my $name = shift;
395         return 1 if exists($reg_classes{"$name"});
396         return 0;
397 }
398
399 ###
400 # Returns the register class for a given register.
401 # @return class or undef
402 ###
403 sub get_reg_class {
404         my $reg = shift;
405         return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
406         return undef;
407 }
408
409 ###
410 # Returns the index of a given register within it's register class.
411 # @return index or undef
412 ###
413 sub get_reg_index {
414         my $reg = shift;
415         return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
416         return undef;
417 }
418
419 ###
420 # Generates the function for a given $op and a given IN-index
421 # which returns a subset of possible register from a register class
422 # @return classname from which the subset is derived or undef and
423 #         pos which corresponds to in/out reference position or undef
424 ###
425 sub build_subset_class_func {
426         my $neg   = undef;
427         my $class = undef;
428         my $temp;
429         my $has_limit = 0;
430
431         # build function header
432         my $n    = shift;
433         my $op   = shift;
434         my $idx  = shift;
435         my $in   = shift;
436         my $pos  = undef;
437         my $same = 1;
438
439         my @temp_obst;
440
441         my $outin = $in ? "out" : "in";
442         my @regs  = split(/ /, shift);
443
444         my @idx_class = build_inout_idx_class($n, $op, $outin);
445
446         # set/unset registers
447 CHECK_REQS: foreach (@regs) {
448                 if (/(!)?$outin\_r(\d+)/) {
449                         if (defined($pos)) {
450                                 print STDERR "Multiple in/out references in one requirement not allowed.\n";
451                                 return (undef, undef, undef, undef);
452                         }
453                         $same  = 0 if ($1);
454                         $class = $idx_class[$2 - 1];
455                         $pos   = $in ? -$2 : $2 - 1;
456                         next CHECK_REQS;
457                 }
458
459                 # check for negate
460                 if (substr($_, 0, 1) eq "!") {
461                         if (defined($neg) && $neg == 0) {
462                                 # we have seen a positiv constraint as first one but this one is negative
463                                 # this doesn't make sense
464                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
465                                 return (undef, undef, undef, undef);
466                         }
467
468                         if (!defined($neg)) {
469                                 $has_limit = 1;
470                                 push(@temp_obst, "  bs = bitset_set_all(bs);     /* allow all register (negative constraints given) */\n");
471                         }
472
473                         $_   = substr($_, 1); # skip '!'
474                         $neg = 1;
475                 }
476                 else {
477                         if (defined($neg) && $neg == 1) {
478                                 # we have seen a negative constraint as first one but this one is positive
479                                 # this doesn't make sense
480                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
481                                 return (undef, undef, undef, undef);
482                         }
483
484                         if (!defined($neg)) {
485                                 $has_limit = 1;
486                                 push(@temp_obst, "  bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */\n");
487                         }
488                         $neg = 0;
489                 }
490
491                 # check if register belongs to one of the given classes
492                 $temp = get_reg_class($_);
493                 if (!defined($temp)) {
494                         print STDERR "Unknown register '$_'!\n";
495                         return (undef, undef, undef, undef);
496                 }
497
498                 # set class
499                 if (!defined($class)) {
500                         $class = $temp;
501                 }
502                 elsif ($class ne $temp) {
503                         # all registers must belong to the same class
504                         print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
505                         return (undef, undef, undef, undef);
506                 }
507
508                 if ($neg == 1) {
509                         $has_limit = 1;
510                         push(@temp_obst, "  bitset_clear(bs, ".get_reg_index($_).");         /* disallow $_ */\n");
511                 }
512                 else {
513                         $has_limit = 1;
514                         push(@temp_obst, "  bitset_set(bs, ".get_reg_index($_).");           /* allow $_ */\n");
515                 }
516         }
517
518         if ($has_limit == 1) {
519                 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
520                 push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
521                 push(@obst_limit_func, @temp_obst);
522                 push(@obst_limit_func, "\n  return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
523                 push(@obst_limit_func, "}\n\n");
524         }
525
526         return ($class, $has_limit, $pos, $same);
527 }