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 sub translate_reg_type {
40 return "arch_register_type_none";
46 push(@types, "arch_register_type_caller_save");
50 push(@types, "arch_register_type_callee_save");
54 push(@types, "arch_register_type_ignore");
57 return join(" | ", @types);
62 my @obst_regtypes_def; # stack for the register type variables definitions
63 my @obst_regtypes_decl;# stack for the register type variables declarations
64 my @obst_regclasses; # stack for the register class variables
65 my @obst_classdef; # stack to define a name for a class index
66 my @obst_regdef; # stack to define a name for a register index
67 my @obst_reginit; # stack for the register type inits
68 my @obst_req; # stack for the register requirements
69 my @obst_limit_func; # stack for functions to return a subset of a register class
70 my @obst_defreq_head; # stack for prototypes of default requirement function
71 my @obst_header_all; # stack for some extern struct defs needed for bearch_$arch include
72 my @obst_requirement_def; # stack for requirement name defines
82 # there is a default NONE requirement
83 $tmp = "/* Default NONE register requirements */\n";
84 $tmp .= "const $arch\_register_req_t $arch\_default_req_none = {\n";
86 $tmp .= " arch_register_req_type_none, /* register type */\n";
87 $tmp .= " NULL, /* register class */\n";
88 $tmp .= " NULL, /* limit function */\n";
89 $tmp .= " NULL, /* limit environment */\n";
90 $tmp .= " NULL, /* node for same */\n";
91 $tmp .= " NULL /* node for different */\n";
93 $tmp .= " 0, /* same pos */\n";
94 $tmp .= " 0 /* different pos */\n";
96 push(@obst_req, $tmp);
97 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_none;\n");
99 push(@obst_classdef, "enum reg_classes {\n");
103 # generate register type and class variable, init function and default requirements
104 foreach my $class_name (keys(%reg_classes)) {
105 my @class = @{ $reg_classes{"$class_name"} };
106 my $old_classname = $class_name;
108 $class_name = $arch."_".$class_name;
109 $numregs = "N_".$class_name."_REGS";
110 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
111 $class_mode = pop(@class)->{"mode"};
113 push(@obst_regtypes_decl, "extern arch_register_t ".$class_name."_regs[$numregs];\n");
114 push(@obst_regtypes_def, "arch_register_t ".$class_name."_regs[$numregs];\n");
116 push(@obst_classdef, " CLASS_$class_name = $class_idx,\n");
117 push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
119 # there is a default NORMAL requirement for each class
120 $tmp = "/* Default NORMAL register requirements for class $class_name */\n";
121 $tmp .= "const $arch\_register_req_t $arch\_default_req_$class_name = {\n";
123 $tmp .= " arch_register_req_type_normal,\n";
124 $tmp .= " $class_ptr,\n";
125 $tmp .= " NULL, /* limit function */ \n";
126 $tmp .= " NULL, /* limit environment */\n";
127 $tmp .= " NULL, /* node for same */\n";
128 $tmp .= " NULL /* node for different */\n";
130 $tmp .= " 0, /* same pos */\n";
131 $tmp .= " 0 /* different pos */\n";
133 push(@obst_req, $tmp);
134 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_$class_name;\n");
137 push(@obst_reginit, " /* Init of all registers in class '$class_name' */\n\n");
138 push(@obst_reginit, " /* set largest possible mode for '$class_name' */\n");
139 push(@obst_reginit, " $arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
140 push(@obst_regdef, "enum reg_".$class_name."_values {\n");
142 # For each class we build for each of it's member registers a limit function
143 # which limits the class to this particular register. We also build the
144 # corresponding requirement structs.
145 # We need those functions to set register requirements on demand in transformation
146 # esp. for Call and RegParams where we can mix int and float parameters.
148 my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
150 # push the function prototype
151 $tmp = "void $limit_func_name(void *_unused, bitset_t *bs)";
152 push(@obst_defreq_head, $tmp.";\n");
154 # push the function definition
156 $tmp .= " bs = bitset_clear_all(bs);\n";
157 $tmp .= " bitset_set(bs, REG_".uc($_->{"name"}).");\n"; # REGISTER to index assignment is done some lines down
159 push(@obst_limit_func, $tmp);
161 # push the default requirement struct
162 $tmp = "const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}." = {\n";
164 $tmp .= " arch_register_req_type_limited,\n";
165 $tmp .= " $class_ptr,\n";
166 $tmp .= " $limit_func_name,\n";
167 $tmp .= " NULL, /* limit environment */\n";
168 $tmp .= " NULL, /* node for same */\n";
169 $tmp .= " NULL /* node for different */\n";
171 $tmp .= " 0, /* same pos */\n";
172 $tmp .= " 0 /* different pos */\n";
174 push(@obst_req, $tmp);
175 push(@obst_header_all,"extern const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}.";\n");
177 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
178 push(@obst_regdef, " REG_".uc($_->{"name"})." = $idx,\n");
179 push(@obst_reginit, " ".$class_name."_regs[$idx].name = \"".$_->{"name"}."\";\n");
180 push(@obst_reginit, " ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
181 push(@obst_reginit, " ".$class_name."_regs[$idx].index = $idx;\n");
182 push(@obst_reginit, " ".$class_name."_regs[$idx].type = ".translate_reg_type($_->{"type"}).";\n");
183 push(@obst_reginit, "\n");
186 push(@obst_regdef, " $numregs = $idx\n");
187 push(@obst_regdef, "};\n\n");
192 push(@obst_classdef, " N_CLASSES = ".scalar(keys(%reg_classes))."\n");
193 push(@obst_classdef, "};\n\n");
195 # generate node-register constraints
196 foreach my $op (keys(%nodes)) {
197 my %n = %{ $nodes{"$op"} };
199 next if (!exists($n{"reg_req"}));
203 push(@obst_req, "/* IN requirements for '$op' */\n");
204 # check for argument requirements
205 if (exists($n{"reg_req"}{"in"})) {
206 generate_requirements(\%n, $op, "in");
209 push(@obst_req, "/* OUT requirements for '$op' */\n");
210 # check for result requirements
211 if (exists($n{"reg_req"}{"out"})) {
212 generate_requirements(\%n, $op, "out");
218 # generate header _t (internal usage) file
219 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
221 my $creation_time = localtime(time());
226 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
227 #define _GEN_$tmp\_REGALLOC_IF_T_H_
230 * Generated register classes from spec.
232 * DO NOT EDIT THIS FILE, your changes will be lost.
233 * Edit $specfile instead.
234 * created by: $0 $specfile $target_dir
235 * date: $creation_time
240 print OUT @obst_requirement_def;
242 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
246 # generate header (external usage) file
247 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
249 $creation_time = localtime(time());
252 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
253 #define _GEN_$tmp\_REGALLOC_IF_H_
256 * Contains additional external requirements defs for external includes.
258 * DO NOT EDIT THIS FILE, your changes will be lost.
259 * Edit $specfile instead.
260 * created by: $0 $specfile $target_dir
261 * date: $creation_time
264 #include "../bearch.h"
265 #include "$arch\_nodes_attr.h"
269 print OUT @obst_regdef, "\n";
271 print OUT @obst_classdef, "\n";
273 print OUT @obst_regtypes_decl, "\n";
275 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
277 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
279 print OUT @obst_header_all, "\n";
281 print OUT @obst_defreq_head, "\n";
283 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
289 # generate c inline file
290 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
292 $creation_time = localtime(time());
296 * The generated interface for the register allocator.
297 * Contains register classes and types and register constraints
298 * for all nodes where constraints were given in spec.
300 * DO NOT EDIT THIS FILE, your changes will be lost.
301 * Edit $specfile instead.
302 * created by: $0 $specfile $target_dir
303 * date: $creation_time
306 #include "gen_$arch\_regalloc_if.h"
307 #include "bearch_$arch\_t.h" /* we need this to put the caller saved registers into the isa set */
308 #include "$arch\_map_regs.h"
313 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n ".join(",\n ", @obst_regclasses)."\n};\n\n";
315 print OUT @obst_regtypes_def, "\n";
317 print OUT "void ".$arch."_register_init(void *isa_ptr) {\n";
318 print OUT @obst_reginit;
321 print OUT @obst_limit_func;
328 # Remember the register class for each index in the given requirements.
329 # We need this information for requirements like "in_sX" or "out_dX"
330 # @return array of classes corresponding to the requirement for each index
332 sub build_inout_idx_class {
338 if (exists($n->{"reg_req"}{"$inout"})) {
339 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
341 for (my $idx = 0; $idx <= $#reqs; $idx++) {
344 if ($reqs[$idx] eq "none") {
347 elsif (is_reg_class($reqs[$idx])) {
348 $class = $reqs[$idx];
351 my @regs = split(/ /, $reqs[$idx]);
352 GET_CLASS: foreach my $reg (@regs) {
353 if ($reg =~ /!?(in|out)\_r\d+/) {
354 $class = "UNKNOWN_CLASS";
357 $class = get_reg_class($reg);
358 if (!defined $class) {
359 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
368 push(@idx_class, $class);
376 # Generates the requirements for the given description
378 sub generate_requirements {
383 # get classes for the complementary direction
384 my $outin = ($inout eq "in") ? "out" : "in";
386 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
388 for (my $idx = 0; $idx <= $#reqs; $idx++) {
391 my $tmp2 = "const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx = ";
392 my $tmp = "#define ".$op."_reg_req_$inout\_$idx ";
394 if ($reqs[$idx] eq "none") {
395 $tmp .= "&$arch\_default_req_none\n";
397 elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
398 if (is_reg_class($1)) {
399 $tmp .= "&_".$op."_reg_req_$inout\_$idx\n";
402 $tmp2 .= " arch_register_req_type_should_be_different_from_all,\n";
403 $tmp2 .= " &$arch\_reg_classes[CLASS_$arch\_".$1."],\n";
404 $tmp2 .= " NULL, /* limit function */\n";
405 $tmp2 .= " NULL, /* limit environment */\n";
406 $tmp2 .= " NULL, /* same node */\n";
407 $tmp2 .= " NULL /* different node */\n";
413 push(@obst_req, $tmp2."\n");
414 push(@obst_header_all, "extern const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx;\n");
417 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
420 elsif (is_reg_class($reqs[$idx])) {
421 $tmp .= "&$arch\_default_req_".$arch."_".$reqs[$idx]."\n";
425 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
427 if (!defined($class)) {
428 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
432 push(@req_type_mask, "arch_register_req_type_limited");
434 if (defined($same_pos)) {
435 push(@req_type_mask, "arch_register_req_type_should_be_same");
437 if (defined($different_pos)) {
438 push(@req_type_mask, "arch_register_req_type_should_be_different");
441 $tmp .= "&_".$op."_reg_req_$inout\_$idx\n";
444 $tmp2 .= " ".join(" | ", @req_type_mask).",\n";
445 $tmp2 .= " &$arch\_reg_classes[CLASS_$arch\_".$class."],\n";
446 $tmp2 .= " ".($has_limit ? "limit_reg_".$op."_$inout\_".$idx : "NULL").",\n";
447 $tmp2 .= " NULL, /* limit environment */\n";
448 $tmp2 .= " NULL, /* same node */\n";
449 $tmp2 .= " NULL /* different node */\n";
451 $tmp2 .= " ".(defined($same_pos) ? $same_pos : "0").",\n";
452 $tmp2 .= " ".(defined($different_pos) ? $different_pos : "0")."\n";
455 push(@obst_req, $tmp2."\n");
456 push(@obst_header_all, "extern const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx;\n");
459 push(@obst_requirement_def, $tmp);
465 # Determines whether $name is a specified register class or not.
466 # @return 1 if name is register class, 0 otherwise
470 return 1 if exists($reg_classes{"$name"});
475 # Returns the register class for a given register.
476 # @return class or undef
480 $reg = substr($reg, 1) if ($reg =~ /!.*/);
481 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
486 # Returns the index of a given register within it's register class.
487 # @return index or undef
491 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
496 # Generates the function for a given $op and a given IN-index
497 # which returns a subset of possible register from a register class
498 # @return classname from which the subset is derived or undef and
499 # pos which corresponds to in/out reference position or undef
501 sub build_subset_class_func {
505 my $same_pos = undef;
506 my $different_pos = undef;
511 # build function header
517 my $outin = $in ? "out" : "in";
518 my @regs = split(/ /, shift);
520 my @idx_class = build_inout_idx_class($n, $op, $outin);
522 # set/unset registers
523 CHECK_REQS: foreach (@regs) {
524 if (/(!)?$outin\_r(\d+)/) {
525 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
526 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
527 return (undef, undef, undef, undef);
531 $different_pos = $in ? -$2 : $2 - 1;
534 $same_pos = $in ? -$2 : $2 - 1;
537 $class = $idx_class[$2 - 1];
542 if (substr($_, 0, 1) eq "!") {
543 if (defined($neg) && $neg == 0) {
544 # we have seen a positiv constraint as first one but this one is negative
545 # this doesn't make sense
546 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
547 return (undef, undef, undef, undef);
550 if (!defined($neg)) {
552 push(@temp_obst, " bs = bitset_set_all(bs); /* allow all register (negative constraints given) */\n");
555 $_ = substr($_, 1); # skip '!'
559 if (defined($neg) && $neg == 1) {
560 # we have seen a negative constraint as first one but this one is positive
561 # this doesn't make sense
562 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
563 return (undef, undef, undef, undef);
566 if (!defined($neg)) {
568 push(@temp_obst, " bs = bitset_clear_all(bs); /* disallow all register (positive constraints given) */\n");
573 # check if register belongs to one of the given classes
574 $temp = get_reg_class($_);
575 if (!defined($temp)) {
576 print STDERR "Unknown register '$_'!\n";
577 return (undef, undef, undef, undef);
581 if (!defined($class)) {
584 elsif ($class ne $temp) {
585 # all registers must belong to the same class
586 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
587 return (undef, undef, undef, undef);
592 push(@temp_obst, " bitset_clear(bs, ".get_reg_index($_)."); /* disallow $_ */\n");
596 push(@temp_obst, " bitset_set(bs, ".get_reg_index($_)."); /* allow $_ */\n");
600 my @cur_class = @{ $reg_classes{"$class"} };
601 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
602 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
603 push(@temp_obst, " bitset_clear(bs, ".get_reg_index($cur_class[$idx]{"name"}).");");
604 push(@temp_obst, " /* disallow ignore reg ".$cur_class[$idx]{"name"}." */\n");
608 if ($has_limit == 1) {
609 push(@obst_header_all, "void limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(void *_unused, bitset_t *bs);\n");
611 push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
612 push(@obst_limit_func, "void limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(void *_unused, bitset_t *bs) {\n");
613 push(@obst_limit_func, @temp_obst);
614 push(@obst_limit_func, "}\n\n");
617 return ($class, $has_limit, $same_pos, $different_pos);