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