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 ia32_default_req_none = {\n";
64 $tmp .= " arch_register_req_type_none,\n";
69 push(@obst_req, $tmp);
71 push(@obst_header_all, "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n");
72 push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_none;\n");
74 push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
76 # generate register type and class variable, init function and default requirements
77 foreach my $class_name (keys(%reg_classes)) {
78 my @class = @{ $reg_classes{"$class_name"} };
79 my $old_classname = $class_name;
81 $class_name = $arch."_".$class_name;
82 $numregs = "N_".$class_name."_REGS";
83 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
85 push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
86 push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
88 push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
89 push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
91 # there is a default NORMAL requirement for each class
92 $tmp = "/* Default NORMAL register requirements for class $class_name */\n";
93 $tmp .= "const arch_register_req_t ia32_default_req_$class_name = {\n";
94 $tmp .= " arch_register_req_type_normal,\n";
95 $tmp .= " $class_ptr,\n";
99 push(@obst_req, $tmp);
101 push(@obst_header_all, "\nextern const arch_register_req_t ia32_default_req_$class_name;\n");
104 push(@obst_reginit, " /* Init of all registers in class '$class_name' */\n\n");
106 # For each class we build for each of it's member registers a limit function
107 # which limits the class to this particular register. We also build the
108 # corresponding requirement structs.
109 # We need those functions to set register requirements on demand in transformation
110 # esp. for Call and RegParams where we can mix int and float parameters.
112 my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
114 # push the function prototype
115 $tmp = "int $limit_func_name(const ir_node *irn, int pos, bitset_t *bs)";
116 push(@obst_defreq_head, $tmp.";\n");
118 # push the function definition
120 $tmp .= " bs = bitset_clear_all(bs);\n";
121 $tmp .= " bitset_set(bs, REG_".uc($_->{"name"}).");\n"; # REGISTER to index assignment is done some lines down
122 $tmp .= " return 1;\n";
124 push(@obst_limit_func, $tmp);
126 # push the default requirement struct
127 $tmp = "const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}." = {\n";
128 $tmp .= " arch_register_req_type_limited,\n";
129 $tmp .= " $class_ptr,\n";
130 $tmp .= " $limit_func_name,\n";
133 push(@obst_req, $tmp);
135 push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}.";\n");
137 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
138 push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n");
139 push(@obst_reginit, " ".$class_name."_regs[$idx].name = \"".$_->{"name"}."\";\n");
140 push(@obst_reginit, " ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
141 push(@obst_reginit, " ".$class_name."_regs[$idx].index = $idx;\n");
142 push(@obst_reginit, " ".$class_name."_regs[$idx].type = ".$rt[$_->{"type"}].";\n\n");
149 push(@obst_header_all, "\n/* node specific requirements */\n");
151 # generate node-register constraints
152 foreach my $op (keys(%nodes)) {
153 my %n = %{ $nodes{"$op"} };
155 next if (!exists($n{"reg_req"}));
159 push(@obst_req, "/* IN requirements for '$op' */\n");
161 # We can have IN requirements like "out_d1"
162 # and for those we need the associated register class
163 # That's why we have to check all out requirements first
164 # and remember their classes.
166 if (exists($n{"reg_req"}{"out"})) {
167 my @out = @{ $n{"reg_req"}{"out"} };
169 for (my $idx = 0; $idx <= $#out; $idx++) {
172 if ($out[$idx] eq "none") {
175 elsif (is_reg_class($out[$idx])) {
178 elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) {
179 $class = "UNKNOWN_CLASS";
182 my @regs = split(/ /, $out[$idx]);
183 $class = get_reg_class($regs[0]);
184 if (!defined $class) {
185 die("Could not get register class for '$op' pos $idx ... exiting.\n");
189 push(@outidx_class, $class);
194 # we need to remember the classes of the IN constraints for
195 # OUT constraints like "in_s1"
198 # check for argument requirements
199 if (exists($n{"reg_req"}{"in"})) {
200 my @in = @{ $n{"reg_req"}{"in"} };
202 for (my $idx = 0; $idx <= $#in; $idx++) {
205 my $tmp2 = "const arch_register_req_t _".$op."_reg_req_in_$idx = ";
207 $tmp = "const arch_register_req_t *".$op."_reg_req_in_$idx = ";
209 push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_in_$idx;\n");
211 if ($in[$idx] eq "none") {
212 push(@inidx_class, "none");
213 $tmp .= "&ia32_default_req_none;\n";
215 elsif (is_reg_class($in[$idx])) {
216 push(@inidx_class, $in[$idx]);
217 $tmp .= "&ia32_default_req_".$arch."_".$in[$idx].";\n";
219 elsif ($in[$idx] =~ /^(!)?out_d(\d+)/) { # this is a "should be (un)equal to register at out_X"
220 $tmp .= "&_".$op."_reg_req_in_$idx;\n";
222 $tmp2 .= " arch_register_req_type_should_be_".($1 ? "different" : "same").",\n";
223 $tmp2 .= " &$arch\_reg_classes[CLASS_$arch\_".$outidx_class[$2 - 1]."],\n";
224 $tmp2 .= " NULL,\n -$2\n};\n";
229 $class = build_subset_class_func($op, $idx, 1, $in[$idx]);
230 if (!defined $class) {
231 die("Could not build subset for IN requirements '$op' pos $idx ... exiting.\n");
233 push(@inidx_class, $class);
234 $tmp .= "&_".$op."_reg_req_in_$idx;\n";
235 $tmp2 .= " {\n arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n limit_reg_".$op."_in_".$idx.",\n 0\n};\n";
240 push(@obst_req, $tmp."\n");
244 push(@obst_req, "/* OUT requirements for '$op' */\n");
246 # check for result requirements
247 if (exists($n{"reg_req"}{"out"})) {
248 my @out = @{ $n{"reg_req"}{"out"} };
250 for (my $idx = 0; $idx <= $#out; $idx++) {
253 my $tmp2 = "const arch_register_req_t _".$op."_reg_req_out_$idx = ";
255 $tmp = "const arch_register_req_t *".$op."_reg_req_out_$idx = ";
257 push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_out_$idx;\n");
259 if ($out[$idx] eq "none") {
260 $tmp .= "&ia32_default_req_none;\n";
262 elsif (is_reg_class($out[$idx])) {
263 $tmp .= "&ia32_default_req_".$arch."_".$out[$idx].";\n";
265 elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) { # this is a "should be (un)equal to register at in_X"
266 $tmp .= "&_".$op."_reg_req_out_$idx;\n";
268 $tmp2 .= " arch_register_req_type_should_be_".($1 ? "different" : "same").",\n";
269 $tmp2 .= " &$arch\_reg_classes[CLASS_$arch\_".$inidx_class[$2 - 1]."],\n";
270 $tmp2 .= " NULL,\n ".($2 - 1)."\n};\n";
275 $class = build_subset_class_func($op, $idx, 0, $out[$idx]);
276 if (!defined $class) {
277 die("Could not build subset for OUT requirements '$op' pos $idx ... exiting.\n");
279 $tmp .= "&_".$op."_reg_req_out_$idx;\n";
280 $tmp2 .= " {\n arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n limit_reg_".$op."_out_".$idx.",\n 0\n};\n";
285 push(@obst_req, $tmp."\n");
292 # generate header _t (internal usage) file
293 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
295 my $creation_time = localtime(time());
300 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
301 #define _GEN_$tmp\_REGALLOC_IF_T_H_
304 * Generated register classes from spec.
306 * DO NOT EDIT THIS FILE, your changes will be lost.
307 * Edit $specfile instead.
308 * created by: $0 $specfile $target_dir
309 * date: $creation_time
312 #include "../bearch.h"
316 print OUT @obst_regdef, "\n";
318 print OUT @obst_classdef, "\n";
320 print OUT @obst_regtypes, "\n";
322 print OUT @obst_defreq_head, "\n";
324 print OUT "void ".$arch."_register_init(void);\n\n";
326 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
330 # generate header (external usage) file
331 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
333 $creation_time = localtime(time());
336 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
337 #define _GEN_$tmp\_REGALLOC_IF_H_
340 * Contains additional external requirements defs for external includes.
342 * DO NOT EDIT THIS FILE, your changes will be lost.
343 * Edit $specfile instead.
344 * created by: $0 $specfile $target_dir
345 * date: $creation_time
348 #include "gen_$arch\_regalloc_if_t.h"
352 print OUT @obst_header_all;
354 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
360 # generate c inline file
361 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
363 $creation_time = localtime(time());
367 * The generated interface for the register allocator.
368 * Contains register classes and types and register constraints
369 * for all nodes where constraints were given in spec.
371 * DO NOT EDIT THIS FILE, your changes will be lost.
372 * Edit $specfile instead.
373 * created by: $0 $specfile $target_dir
374 * date: $creation_time
377 #include "gen_$arch\_regalloc_if_t.h"
381 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n ".join(",\n ", @obst_regclasses)."\n};\n\n";
383 print OUT "void ".$arch."_register_init(void) {\n";
384 print OUT @obst_reginit;
387 print OUT @obst_limit_func;
395 # Determines whether $name is a specified register class or not.
396 # @return 1 if name is register class, 0 otherwise
400 return 1 if exists($reg_classes{"$name"});
405 # Returns the register class for a given register.
406 # @return class or undef
410 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
415 # Returns the index of a given register within it's register class.
416 # @return index or undef
420 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
425 # Generates the function for a given $op and a given IN-index
426 # which returns a subset of possible register from a register class
427 # @return classname from which the subset is derived or undef
429 sub build_subset_class_func {
434 # build function header
438 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
439 push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
441 my @regs = split(/ /, shift);
443 # set/unset registers
447 if (substr($_, 0, 1) eq "!") {
448 if (defined($neg) && $neg == 0) {
449 # we have seen a positiv constraint as first one but this one is negative
450 # this doesn't make sense
451 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
455 if (!defined($neg)) {
456 push(@obst_limit_func, " bs = bitset_set_all(bs); /* allow all register (negative constraints given) */\n");
459 $_ = substr($_, 1); # skip '!'
463 if (defined($neg) && $neg == 1) {
464 # we have seen a negative constraint as first one but this one is positive
465 # this doesn't make sense
466 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
470 if (!defined($neg)) {
471 push(@obst_limit_func, " bs = bitset_clear_all(bs); /* disallow all register (positive constraints given) */\n");
476 # check if register belongs to one of the given classes
477 $temp = get_reg_class($_);
478 if (!defined($temp)) {
479 print STDERR "Unknown register '$_'!\n";
487 elsif ($class ne $temp) {
488 # all registers must belong to the same class
489 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
494 push(@obst_limit_func, " bitset_clear(bs, ".get_reg_index($_)."); /* disallow $_ */\n");
497 push(@obst_limit_func, " bitset_set(bs, ".get_reg_index($_)."); /* allow $_ */\n");
501 push(@obst_limit_func, "\n return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
502 push(@obst_limit_func, "}\n\n");