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