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";
66 $tmp .= " { NULL }\n";
68 push(@obst_req, $tmp);
70 push(@obst_header_all, "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n");
71 push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_none;\n");
73 push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
75 # generate register type and class variable, init function and default requirements
76 foreach my $class_name (keys(%reg_classes)) {
77 my @class = @{ $reg_classes{"$class_name"} };
78 my $old_classname = $class_name;
80 $class_name = $arch."_".$class_name;
81 $numregs = "N_".$class_name."_REGS";
82 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
84 push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
85 push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
87 push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
88 push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
90 # there is a default NORMAL requirement for each class
91 $tmp = "/* Default NORMAL register requirements for class $class_name */\n";
92 $tmp .= "const arch_register_req_t ia32_default_req_$class_name = {\n";
93 $tmp .= " arch_register_req_type_normal,\n";
94 $tmp .= " $class_ptr,\n";
95 $tmp .= " { NULL }\n";
97 push(@obst_req, $tmp);
99 push(@obst_header_all, "\nextern const arch_register_req_t ia32_default_req_$class_name;\n");
102 push(@obst_reginit, " /* Init of all registers in class '$class_name' */\n\n");
104 # For each class we build for each of it's member registers a limit function
105 # which limits the class to this particular register. We also build the
106 # corresponding requirement structs.
107 # We need those functions to set register requirements on demand in transformation
108 # esp. for Call and RegParams where we can mix int and float parameters.
110 my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
112 # push the function prototype
113 $tmp = "int $limit_func_name(const ir_node *irn, int pos, bitset_t *bs)";
114 push(@obst_defreq_head, $tmp.";\n");
116 # push the function definition
118 $tmp .= " bs = bitset_clear_all(bs);\n";
119 $tmp .= " bitset_set(bs, REG_".uc($_->{"name"}).");\n"; # REGISTER to index assignment is done some lines down
120 $tmp .= " return 1;\n";
122 push(@obst_limit_func, $tmp);
124 # push the default requirement struct
125 $tmp = "const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}." = {\n";
126 $tmp .= " arch_register_req_type_limited,\n";
127 $tmp .= " $class_ptr,\n";
128 $tmp .= " { $limit_func_name }\n";
130 push(@obst_req, $tmp);
132 push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}.";\n");
134 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
135 push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n");
136 push(@obst_reginit, " ".$class_name."_regs[$idx].name = \"".$_->{"name"}."\";\n");
137 push(@obst_reginit, " ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
138 push(@obst_reginit, " ".$class_name."_regs[$idx].index = $idx;\n");
139 push(@obst_reginit, " ".$class_name."_regs[$idx].type = ".$rt[$_->{"type"}].";\n\n");
146 push(@obst_header_all, "\n/* node specific requirements */\n");
148 # generate node-register constraints
149 foreach my $op (keys(%nodes)) {
150 my %n = %{ $nodes{"$op"} };
152 next if (!exists($n{"reg_req"}));
156 push(@obst_req, "/* IN requirements for '$op' */\n");
158 # we need to remember the classes of the IN constraints for
159 # OUT constraints like "in_s1"
162 # check for argument requirements
163 if (exists($n{"reg_req"}{"in"})) {
164 my @in = @{ $n{"reg_req"}{"in"} };
166 for (my $idx = 0; $idx <= $#in; $idx++) {
169 my $tmp2 = "const arch_register_req_t _".$op."_reg_req_in_$idx = ";
171 $tmp = "const arch_register_req_t *".$op."_reg_req_in_$idx = ";
173 push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_in_$idx;\n");
175 if ($in[$idx] eq "none") {
176 push(@inidx_class, "none");
177 $tmp .= "&ia32_default_req_none;\n";
179 elsif (is_reg_class($in[$idx])) {
180 push(@inidx_class, $in[$idx]);
181 $tmp .= "&ia32_default_req_".$arch."_".$in[$idx].";\n";
184 $class = build_subset_class_func($op, $idx, 1, $in[$idx]);
185 if (!defined $class) {
186 die("Could not build subset for IN requirements '$op' pos $idx ... exiting.\n");
188 push(@inidx_class, $class);
189 $tmp .= "&_".$op."_reg_req_in_$idx;\n";
190 $tmp2 .= " {\n arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n { limit_reg_".$op."_in_".$idx." }\n};\n";
195 push(@obst_req, $tmp."\n");
199 push(@obst_req, "/* OUT requirements for '$op' */\n");
201 # check for result requirements
202 if (exists($n{"reg_req"}{"out"})) {
203 my @out = @{ $n{"reg_req"}{"out"} };
205 for (my $idx = 0; $idx <= $#out; $idx++) {
208 my $tmp2 = "const arch_register_req_t _".$op."_reg_req_out_$idx = ";
210 $tmp = "const arch_register_req_t *".$op."_reg_req_out_$idx = ";
212 push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_out_$idx;\n");
214 if ($out[$idx] eq "none") {
215 $tmp .= "&ia32_default_req_none;\n";
217 elsif (is_reg_class($out[$idx])) {
218 $tmp .= "&ia32_default_req_".$arch."_".$out[$idx].";\n";
220 elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) { # this is a "should be (un)equal to register at in_X"
221 $tmp .= "&_".$op."_reg_req_out_$idx;\n";
223 $tmp2 .= " arch_register_req_type_should_be_".($1 ? "different" : "same").",\n";
224 $tmp2 .= " &$arch\_reg_classes[CLASS_$arch\_".$inidx_class[$2 - 1]."],\n";
225 $tmp2 .= " { ".($2 - 1)." }\n};\n";
230 $class = build_subset_class_func($op, $idx, 0, $out[$idx]);
231 if (!defined $class) {
232 die("Could not build subset for OUT requirements '$op' pos $idx ... exiting.\n");
234 $tmp .= "&_".$op."_reg_req_out_$idx;\n";
235 $tmp2 .= " {\n arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n { limit_reg_".$op."_out_".$idx." }\n};\n";
240 push(@obst_req, $tmp."\n");
247 # generate header _t (internal usage) file
248 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
250 my $creation_time = localtime(time());
255 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
256 #define _GEN_$tmp\_REGALLOC_IF_T_H_
259 * Generated register classes from spec.
261 * DO NOT EDIT THIS FILE, your changes will be lost.
262 * Edit $specfile instead.
263 * created by: $0 $specfile $target_dir
264 * date: $creation_time
267 #include "../bearch.h"
271 print OUT @obst_regdef, "\n";
273 print OUT @obst_classdef, "\n";
275 print OUT @obst_regtypes, "\n";
277 print OUT @obst_defreq_head, "\n";
279 print OUT "void ".$arch."_register_init(void);\n\n";
281 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
285 # generate header (external usage) file
286 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
288 $creation_time = localtime(time());
291 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
292 #define _GEN_$tmp\_REGALLOC_IF_H_
295 * Contains additional external requirements defs for external includes.
297 * DO NOT EDIT THIS FILE, your changes will be lost.
298 * Edit $specfile instead.
299 * created by: $0 $specfile $target_dir
300 * date: $creation_time
303 #include "gen_$arch\_regalloc_if_t.h"
307 print OUT @obst_header_all;
309 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
315 # generate c inline file
316 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
318 $creation_time = localtime(time());
322 * The generated interface for the register allocator.
323 * Contains register classes and types and register constraints
324 * for all nodes where constraints were given in spec.
326 * DO NOT EDIT THIS FILE, your changes will be lost.
327 * Edit $specfile instead.
328 * created by: $0 $specfile $target_dir
329 * date: $creation_time
332 #include "gen_$arch\_regalloc_if_t.h"
336 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n ".join(",\n ", @obst_regclasses)."\n};\n\n";
338 print OUT "void ".$arch."_register_init(void) {\n";
339 print OUT @obst_reginit;
342 print OUT @obst_limit_func;
350 # Determines whether $name is a specified register class or not.
351 # @return 1 if name is register class, 0 otherwise
355 return 1 if exists($reg_classes{"$name"});
360 # Returns the register class for a given register.
361 # @return class or undef
365 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
370 # Returns the index of a given register within it's register class.
371 # @return index or undef
375 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
380 # Generates the function for a given $op and a given IN-index
381 # which returns a subset of possible register from a register class
382 # @return classname from which the subset is derived or undef
384 sub build_subset_class_func {
389 # build function header
393 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
394 push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
396 my @regs = split(/ /, shift);
398 # set/unset registers
402 if (substr($_, 0, 1) eq "!") {
403 if (defined($neg) && $neg == 0) {
404 # we have seen a positiv constraint as first one but this one is negative
405 # this doesn't make sense
406 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
410 if (!defined($neg)) {
411 push(@obst_limit_func, " bs = bitset_set_all(bs); /* allow all register (negative constraints given) */\n");
414 $_ = substr($_, 1); # skip '!'
418 if (defined($neg) && $neg == 1) {
419 # we have seen a negative constraint as first one but this one is positive
420 # this doesn't make sense
421 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
425 if (!defined($neg)) {
426 push(@obst_limit_func, " bs = bitset_clear_all(bs); /* disallow all register (positive constraints given) */\n");
431 # check if register belongs to one of the given classes
432 $temp = get_reg_class($_);
433 if (!defined($temp)) {
434 print STDERR "Unknown register '$_'!\n";
442 elsif ($class ne $temp) {
443 # all registers must belong to the same class
444 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
449 push(@obst_limit_func, " bitset_clear(bs, ".get_reg_index($_)."); /* disallow $_ */\n");
452 push(@obst_limit_func, " bitset_set(bs, ".get_reg_index($_)."); /* allow $_ */\n");
456 push(@obst_limit_func, "\n return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
457 push(@obst_limit_func, "}\n\n");