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