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.inl";
32 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
35 my @rt = ("arch_register_type_none",
36 "arch_register_type_write_invariant",
37 "arch_register_type_caller_saved",
38 "arch_register_type_callee_saved",
39 "arch_register_type_ignore");
42 my @obst_regtypes; # stack for the register type variables
43 my @obst_regclasses; # stack for the register class variables
44 my @obst_classdef; # stack to assign a number to a certain class
45 my @obst_reginit; # stack for the register type inits
46 my @obst_req; # stack for the register requirements
47 my @obst_limit_func; # stack for functions to return a subset of a register class
54 # generate register type and class variable and init function
55 foreach my $class_name (keys(%reg_classes)) {
56 my @class = @{ $reg_classes{"$class_name"} };
57 my $old_classname = $class_name;
59 $class_name = $arch."_".$class_name;
60 $numregs = "N_".$class_name."_REGS";
62 push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
63 push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
65 push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
66 push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
69 push(@obst_reginit, " /* Init of all registers in class '$class_name' */\n\n");
71 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
72 push(@obst_reginit, " ".$class_name."_regs[$idx].name = \"".$_->{"name"}."\";\n");
73 push(@obst_reginit, " ".$class_name."_regs[$idx].reg_class = &$arch\_reg_classes[CLASS_$class_name];\n");
74 push(@obst_reginit, " ".$class_name."_regs[$idx].index = $idx;\n");
75 push(@obst_reginit, " ".$class_name."_regs[$idx].type = ".$rt[$_->{"type"}].";\n\n");
82 # generate node-register constraints
83 foreach my $op (keys(%nodes)) {
84 my %n = %{ $nodes{"$op"} };
86 next if (!exists($n{"reg_req"}));
90 push(@obst_req, "/* IN requirements for '$op' */\n");
94 # check for argument requirements
95 if (exists($n{"reg_req"}{"in"})) {
96 my @in = @{ $n{"reg_req"}{"in"} };
98 for (my $idx = 0; $idx <= $#in; $idx++) {
101 push(@obst_req, "static const arch_register_req_t ".$op."_reg_req_in_$idx = {\n");
103 if ($in[$idx] eq "none") {
104 push(@inidx_class, "none");
105 push(@obst_req, " arch_register_req_type_none,\n NULL,\n { NULL }");
107 elsif (is_reg_class($in[$idx])) {
108 push(@inidx_class, $in[$idx]);
109 push(@obst_req, " arch_register_req_type_normal,\n &$arch\_reg_classes[CLASS_$arch\_".$in[$idx]."],\n { NULL }");
112 $class = build_subset_class_func($op, $idx, 1, $in[$idx]);
113 if (!defined $class) {
114 die("Could not build subset for IN requirements '$op' pos $idx ... exiting.\n");
116 push(@inidx_class, $class);
117 push(@obst_req, " arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n { limit_reg_".$op."_in_".$idx." }");
120 push(@obst_req, "\n};\n\n");
124 push(@obst_req, "/* OUT requirements for '$op' */\n");
126 # check for result requirements
127 if (exists($n{"reg_req"}{"out"})) {
128 my @out = @{ $n{"reg_req"}{"out"} };
130 for (my $idx = 0; $idx <= $#out; $idx++) {
133 push(@obst_req, "static const arch_register_req_t ".$op."_reg_req_out_$idx = {\n");
135 if ($out[$idx] eq "none") {
136 push(@obst_req, " arch_register_req_type_none,\n NULL,\n { NULL }");
138 elsif (is_reg_class($out[$idx])) {
139 push(@obst_req, " arch_register_req_type_normal,\n &$arch\_reg_classes[CLASS_$arch\_".$out[$idx]."],\n { NULL }");
141 elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) { # this is a "should be (un)equal to register at in_X"
142 push(@obst_req, " arch_register_req_type_".($1 ? "un" : "")."equal,\n");
143 push(@obst_req, " &$arch\_reg_classes[CLASS_$arch\_".$inidx_class[$2 - 1]."],\n");
144 push(@obst_req, " { ".($2 - 1)." }");
147 $class = build_subset_class_func($op, $idx, 0, $out[$idx]);
148 if (!defined $class) {
149 die("Could not build subset for OUT requirements '$op' pos $idx ... exiting.\n");
151 push(@obst_req, " arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n { limit_reg_".$op."_out_".$idx." }");
154 push(@obst_req, "\n};\n\n");
161 # generate header file
162 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
164 my $creation_time = localtime(time());
169 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
170 #define _GEN_$tmp\_REGALLOC_IF_H_
173 * Generated register classes from spec
175 * DO NOT EDIT THIS FILE, your changes will be lost.
176 * Edit $specfile instead.
177 * created by: $0 $specfile $target_dir
178 * date: $creation_time
183 print OUT @obst_regtypes;
185 print OUT "void ".$arch."_register_init(void);\n\n";
187 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
191 # generate c inline file
192 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
194 $creation_time = localtime(time());
198 * The generated interface for the register allocator.
199 * Contains register classes and types and register constraints
200 * for all nodes where constraints were given in spec.
202 * DO NOT EDIT THIS FILE, your changes will be lost.
203 * Edit $specfile instead.
204 * created by: $0 $specfile $target_dir
205 * date: $creation_time
208 #include "gen_$arch\_regalloc_if.h"
212 print OUT @obst_classdef, "\n";
214 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n ".join(",\n ", @obst_regclasses)."\n};\n\n";
216 print OUT "void ".$arch."_register_init(void) {\n";
217 print OUT @obst_reginit;
220 print OUT @obst_limit_func;
228 # Determines whether $name is a specified register class or not.
229 # @return 1 if name is register class, 0 otherwise
233 return 1 if exists($reg_classes{"$name"});
238 # Returns the register class for a given register.
239 # @return class or undef
243 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
248 # Returns the index of a given register within it's register class.
249 # @return index or undef
253 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
258 # Generates the function for a given $op and a given IN-index
259 # which returns a subset of possible register from a register class
260 # @return classname from which the subset is derived or undef
262 sub build_subset_class_func {
267 # build function header
271 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
272 push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
274 my @regs = split(/ /, shift);
276 # set/unset registers
280 if (substr($_, 0, 1) eq "!") {
281 if (defined($neg) && $neg == 0) {
282 # we have seen a positiv constraint as first one but this one is negative
283 # this doesn't make sense
284 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
288 if (!defined($neg)) {
289 push(@obst_limit_func, " bs = bitset_set_all(bs); /* allow all register (negative constraints given) */\n");
292 $_ = substr($_, 1); # skip '!'
296 if (defined($neg) && $neg == 1) {
297 # we have seen a negative constraint as first one but this one is positive
298 # this doesn't make sense
299 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
303 if (!defined($neg)) {
304 push(@obst_limit_func, " bs = bitset_clear_all(bs); /* disallow all register (positive constraints given) */\n");
309 # check if register belongs to one of the given classes
310 $temp = get_reg_class($_);
311 if (!defined($temp)) {
312 print STDERR "Unknown register '$_'!\n";
320 elsif ($class ne $temp) {
321 # all registers must belong to the same class
322 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
327 push(@obst_limit_func, " bitset_clear(bs, ".get_reg_index($_)."); /* disallow $_ */\n");
330 push(@obst_limit_func, " bitset_set(bs, ".get_reg_index($_)."); /* allow $_ */\n");
334 push(@obst_limit_func, "\n return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
335 push(@obst_limit_func, "}\n\n");