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