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