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