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().
12 my $specfile = $ARGV[0];
13 my $target_dir = $ARGV[1];
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;
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";
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");
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
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";
65 $tmp .= " arch_register_req_type_none,\n";
72 push(@obst_req, $tmp);
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");
77 push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
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;
84 $class_name = $arch."_".$class_name;
85 $numregs = "N_".$class_name."_REGS";
86 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
88 push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
89 push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
91 push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
92 push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
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";
98 $tmp .= " arch_register_req_type_normal,\n";
99 $tmp .= " $class_ptr,\n";
105 push(@obst_req, $tmp);
107 push(@obst_header_all, "\nextern const arch_register_req_t ia32_default_req_$class_name;\n");
110 push(@obst_reginit, " /* Init of all registers in class '$class_name' */\n\n");
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.
118 my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
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");
124 # push the function definition
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";
130 push(@obst_limit_func, $tmp);
132 # push the default requirement struct
133 $tmp = "const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}." = {\n";
135 $tmp .= " arch_register_req_type_limited,\n";
136 $tmp .= " $class_ptr,\n";
137 $tmp .= " $limit_func_name,\n";
142 push(@obst_req, $tmp);
144 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}.";\n");
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");
158 push(@obst_header_all, "\n/* node specific requirements */\n");
160 # generate node-register constraints
161 foreach my $op (keys(%nodes)) {
162 my %n = %{ $nodes{"$op"} };
164 next if (!exists($n{"reg_req"}));
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");
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");
183 # generate header _t (internal usage) file
184 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
186 my $creation_time = localtime(time());
191 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
192 #define _GEN_$tmp\_REGALLOC_IF_T_H_
195 * Generated register classes from spec.
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
203 #include "../bearch.h"
207 print OUT @obst_regdef, "\n";
209 print OUT @obst_classdef, "\n";
211 print OUT @obst_regtypes, "\n";
213 print OUT @obst_defreq_head, "\n";
215 print OUT "void ".$arch."_register_init(void);\n\n";
217 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
221 # generate header (external usage) file
222 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
224 $creation_time = localtime(time());
227 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
228 #define _GEN_$tmp\_REGALLOC_IF_H_
231 * Contains additional external requirements defs for external includes.
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
239 #include "gen_$arch\_regalloc_if_t.h"
243 print OUT @obst_header_all;
245 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
251 # generate c inline file
252 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
254 $creation_time = localtime(time());
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.
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
268 #include "gen_$arch\_regalloc_if_t.h"
272 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n ".join(",\n ", @obst_regclasses)."\n};\n\n";
274 print OUT "void ".$arch."_register_init(void) {\n";
275 print OUT @obst_reginit;
278 print OUT @obst_limit_func;
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
288 sub build_inout_idx_class {
294 if (exists($n->{"reg_req"}{"$inout"})) {
295 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
297 for (my $idx = 0; $idx <= $#reqs; $idx++) {
300 if ($reqs[$idx] eq "none") {
303 elsif (is_reg_class($reqs[$idx])) {
304 $class = $reqs[$idx];
307 my @regs = split(/ /, $reqs[$idx]);
308 GET_CLASS: foreach my $reg (@regs) {
309 if ($reg =~ /!?(in|out)\_r\d+/) {
310 $class = "UNKNOWN_CLASS";
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");
324 push(@idx_class, $class);
332 # Generates the requirements for the given description
334 sub generate_requirements {
339 # get classes for the complementary direction
340 my $outin = ($inout eq "in") ? "out" : "in";
342 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
344 for (my $idx = 0; $idx <= $#reqs; $idx++) {
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 = ";
350 push(@obst_header_all, "extern const $arch\_register_req_t *".$op."_reg_req_$inout\_$idx;\n");
352 if ($reqs[$idx] eq "none") {
353 $tmp .= "&$arch\_default_req_none;\n";
355 elsif (is_reg_class($reqs[$idx])) {
356 $tmp .= "&$arch\_default_req_".$arch."_".$reqs[$idx].";\n";
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");
365 push(@req_type_mask, "arch_register_req_type_limited");
368 push(@req_type_mask, "arch_register_req_type_should_be_".($same ? "same" : "different"));
370 $tmp .= "&_".$op."_reg_req_$inout\_$idx;\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";
378 $tmp2 .= " ".(defined($pos) ? $pos : "0")."\n};\n";
383 push(@obst_req, $tmp."\n");
389 # Determines whether $name is a specified register class or not.
390 # @return 1 if name is register class, 0 otherwise
394 return 1 if exists($reg_classes{"$name"});
399 # Returns the register class for a given register.
400 # @return class or undef
404 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
409 # Returns the index of a given register within it's register class.
410 # @return index or undef
414 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
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
424 sub build_subset_class_func {
430 # build function header
440 my $outin = $in ? "out" : "in";
441 my @regs = split(/ /, shift);
443 my @idx_class = build_inout_idx_class($n, $op, $outin);
445 # set/unset registers
446 CHECK_REQS: foreach (@regs) {
447 if (/(!)?$outin\_r(\d+)/) {
449 print STDERR "Multiple in/out references in one requirement not allowed.\n";
450 return (undef, undef, undef, undef);
453 $class = $idx_class[$2 - 1];
454 $pos = $in ? -$2 : $2 - 1;
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);
467 if (!defined($neg)) {
469 push(@temp_obst, " bs = bitset_set_all(bs); /* allow all register (negative constraints given) */\n");
472 $_ = substr($_, 1); # skip '!'
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);
483 if (!defined($neg)) {
485 push(@temp_obst, " bs = bitset_clear_all(bs); /* disallow all register (positive constraints given) */\n");
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);
498 if (!defined($class)) {
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);
509 push(@temp_obst, " bitset_clear(bs, ".get_reg_index($_)."); /* disallow $_ */\n");
513 push(@temp_obst, " bitset_set(bs, ".get_reg_index($_)."); /* allow $_ */\n");
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");
525 return ($class, $has_limit, $pos, $same);