4 # Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
6 # This file is part of libFirm.
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # This script generates C code which creates ands sets up functions and
23 # data structures for the register allocator.
24 # Creation: 2005/11/14
30 my $specfile = $ARGV[0];
31 my $target_dir = $ARGV[1];
41 unless ($return = do $specfile) {
42 die "Fatal error: couldn't parse $specfile: $@" if $@;
43 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
44 die "Fatal error: couldn't run $specfile" unless $return;
48 my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c";
49 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
54 my $flags = shift || "none";
55 return join(" | ", map { "$prefix$_" } split(/\s*\|\s*/, $flags));
59 my $regtypes_def; # stack for the register type variables definitions
60 my @regclasses; # stack for the register class variables
61 my $classdef; # stack to define a name for a class index
62 my $regdef; # stack to define a name for a register index
65 my $reginit; # stack for the register type inits
66 my $single_constraints;
71 my %regclass2len = ();
74 $classdef .= "enum reg_classes {\n";
76 foreach my $class_name (keys(%reg_classes)) {
77 my @class = @{ $reg_classes{"$class_name"} };
81 if (defined($_->{name})) {
82 $reg2class{$_->{name}} = {
83 "class" => $class_name,
89 $regclass2len{$class_name} = $idx;
92 sub get_limited_array {
94 my $regclass = $reg2class{"$reg"}{"class"};
95 my $ucname = uc($reg);
98 my $limitedbitsetlen = $regclass2len{$regclass};
99 my $arraylen = ($limitedbitsetlen+31) / 32;
100 my $firstreg = uc($reg_classes{$regclass}[0]->{"name"});
101 my $classuc = uc($regclass);
103 for (my $i = 0; $i < $arraylen; ++$i) {
110 my $index = $reg2class{"$reg"}{"index"};
111 if ($index >= $i*32 && $index < ($i+1)*32) {
113 $result .= "(1 << (REG_${classuc}_${ucname} % 32))";
115 $result .= "(1 << REG_${classuc}_${ucname})";
124 # generate register type and class variable, init function and default requirements
125 foreach my $class_name (keys(%reg_classes)) {
126 my @class = @{ $reg_classes{"$class_name"} };
127 my $old_classname = $class_name;
129 $class_name = $arch."_".$class_name;
130 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
131 my $flags = pop(@class);
132 my $class_mode = $flags->{"mode"};
133 my $flags_prepared = map_flags("arch_register_class_flag_", $flags->{"flags"});
135 $single_constraints .= <<EOF;
136 static const arch_register_req_t ${arch}_class_reg_req_${old_classname} = {
137 arch_register_req_type_normal,
138 &${arch}_reg_classes[CLASS_${arch}_${old_classname}],
146 $classdef .= "\tCLASS_$class_name = $class_idx,\n";
147 my $numregs = @class;
148 my $first_reg = "&${arch}_registers[REG_". uc($class[0]->{"name"}) . "]";
149 push(@regclasses, "{ $class_idx, \"$class_name\", $numregs, NULL, $first_reg, $flags_prepared, &${arch}_class_reg_req_${old_classname} }");
152 $reginit .= "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n";
155 my $name = $_->{"name"};
156 my $ucname = uc($name);
157 my $type = map_flags("arch_register_type_", $_->{"type"});
158 # realname is name if not set by user
159 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
160 my $realname = $_->{realname};
161 my $classuc = uc($old_classname);
163 $regdef .= "\tREG_${ucname},\n";
164 $regdef2 .= "\tREG_${classuc}_${ucname} = $idx,\n";
165 my $dwarf_number = 0;
166 if (defined($_->{dwarf})) {
167 $dwarf_number = $_->{dwarf};
170 $regtypes_def .= <<EOF;
174 REG_${classuc}_${ucname},
177 &${arch}_single_reg_req_${old_classname}_${name},
182 my $limitedarray = get_limited_array($name);
183 $single_constraints .= <<EOF;
184 static const unsigned ${arch}_limited_${old_classname}_${name} [] = ${limitedarray};
185 static const arch_register_req_t ${arch}_single_reg_req_${old_classname}_${name} = {
186 arch_register_req_type_limited,
188 ${arch}_limited_${old_classname}_${name},
198 $regcounts .= "\tN_${class_name}_REGS = $numregs,\n";
203 my $archuc = uc($arch);
205 $classdef .= "\tN_${archuc}_CLASSES = ".scalar(keys(%reg_classes))."\n";
206 $classdef .= "};\n\n";
208 # generate header (external usage) file
209 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
211 my $creation_time = localtime(time());
216 * \@brief Contains additional external requirements defs for external includes.
217 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
218 * Edit $specfile instead.
219 * created by: $0 $specfile $target_dir
220 * \@date $creation_time
222 #ifndef FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
223 #define FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
226 #include "${arch}_nodes_attr.h"
228 /** global register indices for ${arch} registers */
231 N_${archuc}_REGISTERS
233 /** local register indices for ${arch} registers */
238 /** number of registers in ${arch} register classes. */
244 extern const arch_register_t ${arch}_registers[N_${archuc}_REGISTERS];
246 extern arch_register_class_t ${arch}_reg_classes[N_${archuc}_CLASSES];
248 void ${arch}_register_init(void);
256 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
258 $creation_time = localtime(time());
263 * \@brief The generated interface for the register allocator.
264 * Contains register classes and types and register constraints
265 * for all nodes where constraints were given in spec.
266 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
267 * Edit $specfile instead.
268 * created by: $0 $specfile $target_dir
269 * \$date $creation_time
273 #include "gen_${arch}_regalloc_if.h"
274 #include "bearch_${arch}_t.h"
277 ${single_constraints}
280 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @regclasses)."\n};\n\n";
284 /** The array of all registers in the ${arch} architecture, sorted by its global index.*/
285 const arch_register_t ${arch}_registers[] = {
290 * Initializes ${arch} register classes.
292 void ${arch}_register_init(void)