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 $arch\_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"
204 #include "$arch\_nodes_attr.h"
208 print OUT @obst_regdef, "\n";
210 print OUT @obst_classdef, "\n";
212 print OUT @obst_regtypes, "\n";
214 print OUT @obst_defreq_head, "\n";
216 print OUT "void ".$arch."_register_init(void);\n\n";
218 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
222 # generate header (external usage) file
223 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
225 $creation_time = localtime(time());
228 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
229 #define _GEN_$tmp\_REGALLOC_IF_H_
232 * Contains additional external requirements defs for external includes.
234 * DO NOT EDIT THIS FILE, your changes will be lost.
235 * Edit $specfile instead.
236 * created by: $0 $specfile $target_dir
237 * date: $creation_time
240 #include "gen_$arch\_regalloc_if_t.h"
244 print OUT @obst_header_all;
246 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
252 # generate c inline file
253 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
255 $creation_time = localtime(time());
259 * The generated interface for the register allocator.
260 * Contains register classes and types and register constraints
261 * for all nodes where constraints were given in spec.
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
269 #include "gen_$arch\_regalloc_if_t.h"
273 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n ".join(",\n ", @obst_regclasses)."\n};\n\n";
275 print OUT "void ".$arch."_register_init(void) {\n";
276 print OUT @obst_reginit;
279 print OUT @obst_limit_func;
285 # Remember the register class for each index in the given requirements.
286 # We need this information for requirements like "in_sX" or "out_dX"
287 # @return array of classes corresponding to the requirement for each index
289 sub build_inout_idx_class {
295 if (exists($n->{"reg_req"}{"$inout"})) {
296 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
298 for (my $idx = 0; $idx <= $#reqs; $idx++) {
301 if ($reqs[$idx] eq "none") {
304 elsif (is_reg_class($reqs[$idx])) {
305 $class = $reqs[$idx];
308 my @regs = split(/ /, $reqs[$idx]);
309 GET_CLASS: foreach my $reg (@regs) {
310 if ($reg =~ /!?(in|out)\_r\d+/) {
311 $class = "UNKNOWN_CLASS";
314 $class = get_reg_class($reg);
315 if (!defined $class) {
316 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
325 push(@idx_class, $class);
333 # Generates the requirements for the given description
335 sub generate_requirements {
340 # get classes for the complementary direction
341 my $outin = ($inout eq "in") ? "out" : "in";
343 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
345 for (my $idx = 0; $idx <= $#reqs; $idx++) {
348 my $tmp2 = "const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx = ";
349 my $tmp = "const $arch\_register_req_t *".$op."_reg_req_$inout\_$idx = ";
351 push(@obst_header_all, "extern const $arch\_register_req_t *".$op."_reg_req_$inout\_$idx;\n");
353 if ($reqs[$idx] eq "none") {
354 $tmp .= "&$arch\_default_req_none;\n";
356 elsif (is_reg_class($reqs[$idx])) {
357 $tmp .= "&$arch\_default_req_".$arch."_".$reqs[$idx].";\n";
361 my ($class, $has_limit, $pos, $same) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
362 if (!defined($class)) {
363 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
366 push(@req_type_mask, "arch_register_req_type_limited");
369 push(@req_type_mask, "arch_register_req_type_should_be_".($same ? "same" : "different"));
371 $tmp .= "&_".$op."_reg_req_$inout\_$idx;\n";
374 $tmp2 .= " ".join(" | ", @req_type_mask).",\n";
375 $tmp2 .= " &$arch\_reg_classes[CLASS_$arch\_".$class."],\n";
376 $tmp2 .= " ".($has_limit ? "limit_reg_".$op."_$inout\_".$idx : "NULL").",\n";
379 $tmp2 .= " ".(defined($pos) ? $pos : "0")."\n};\n";
384 push(@obst_req, $tmp."\n");
390 # Determines whether $name is a specified register class or not.
391 # @return 1 if name is register class, 0 otherwise
395 return 1 if exists($reg_classes{"$name"});
400 # Returns the register class for a given register.
401 # @return class or undef
405 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
410 # Returns the index of a given register within it's register class.
411 # @return index or undef
415 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
420 # Generates the function for a given $op and a given IN-index
421 # which returns a subset of possible register from a register class
422 # @return classname from which the subset is derived or undef and
423 # pos which corresponds to in/out reference position or undef
425 sub build_subset_class_func {
431 # build function header
441 my $outin = $in ? "out" : "in";
442 my @regs = split(/ /, shift);
444 my @idx_class = build_inout_idx_class($n, $op, $outin);
446 # set/unset registers
447 CHECK_REQS: foreach (@regs) {
448 if (/(!)?$outin\_r(\d+)/) {
450 print STDERR "Multiple in/out references in one requirement not allowed.\n";
451 return (undef, undef, undef, undef);
454 $class = $idx_class[$2 - 1];
455 $pos = $in ? -$2 : $2 - 1;
460 if (substr($_, 0, 1) eq "!") {
461 if (defined($neg) && $neg == 0) {
462 # we have seen a positiv constraint as first one but this one is negative
463 # this doesn't make sense
464 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
465 return (undef, undef, undef, undef);
468 if (!defined($neg)) {
470 push(@temp_obst, " bs = bitset_set_all(bs); /* allow all register (negative constraints given) */\n");
473 $_ = substr($_, 1); # skip '!'
477 if (defined($neg) && $neg == 1) {
478 # we have seen a negative constraint as first one but this one is positive
479 # this doesn't make sense
480 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
481 return (undef, undef, undef, undef);
484 if (!defined($neg)) {
486 push(@temp_obst, " bs = bitset_clear_all(bs); /* disallow all register (positive constraints given) */\n");
491 # check if register belongs to one of the given classes
492 $temp = get_reg_class($_);
493 if (!defined($temp)) {
494 print STDERR "Unknown register '$_'!\n";
495 return (undef, undef, undef, undef);
499 if (!defined($class)) {
502 elsif ($class ne $temp) {
503 # all registers must belong to the same class
504 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
505 return (undef, undef, undef, undef);
510 push(@temp_obst, " bitset_clear(bs, ".get_reg_index($_)."); /* disallow $_ */\n");
514 push(@temp_obst, " bitset_set(bs, ".get_reg_index($_)."); /* allow $_ */\n");
518 if ($has_limit == 1) {
519 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
520 push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
521 push(@obst_limit_func, @temp_obst);
522 push(@obst_limit_func, "\n return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
523 push(@obst_limit_func, "}\n\n");
526 return ($class, $has_limit, $pos, $same);