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