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];
42 unless ($return = do $specfile) {
43 die "Fatal error: couldn't parse $specfile: $@" if $@;
44 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
45 die "Fatal error: couldn't run $specfile" unless $return;
49 my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c";
50 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
55 my $flags = shift || "none";
56 return join(" | ", map { "$prefix$_" } split(/\s*\|\s*/, $flags));
59 sub translate_reg_type {
63 return "arch_register_type_none";
69 push(@types, "arch_register_type_ignore");
73 push(@types, "arch_register_type_virtual");
77 push(@types, "arch_register_type_state");
80 return join(" | ", @types);
85 my $regtypes_def; # stack for the register type variables definitions
86 my $regtypes_decl;# stack for the register type variables declarations
87 my @regclasses; # stack for the register class variables
88 my $classdef; # stack to define a name for a class index
89 my $regdef; # stack to define a name for a register index
92 my $reginit; # stack for the register type inits
93 my $single_constraints_decls;
94 my $single_constraints;
99 my %regclass2len = ();
102 $classdef .= "enum reg_classes {\n";
106 foreach my $class_name (keys(%reg_classes)) {
107 my @class = @{ $reg_classes{"$class_name"} };
111 if (defined($_->{name})) {
112 $reg2class{$_->{name}} = {
113 "class" => $class_name,
119 $regclass2len{$class_name} = $idx;
122 sub get_limited_array {
124 my $regclass = $reg2class{"$reg"}{"class"};
125 my $ucname = uc($reg);
128 my $limitedbitsetlen = $regclass2len{$regclass};
129 my $arraylen = ($limitedbitsetlen+31) / 32;
130 my $firstreg = uc($reg_classes{$regclass}[0]->{"name"});
131 my $classuc = uc($regclass);
133 for (my $i = 0; $i < $arraylen; ++$i) {
140 my $index = $reg2class{"$reg"}{"index"};
141 if ($index >= $i*32 && $index < ($i+1)*32) {
143 $result .= "(1 << (REG_${classuc}_${ucname} % 32))";
145 $result .= "(1 << REG_${classuc}_${ucname})";
154 # generate register type and class variable, init function and default requirements
155 foreach my $class_name (keys(%reg_classes)) {
156 my @class = @{ $reg_classes{"$class_name"} };
157 my $old_classname = $class_name;
159 $class_name = $arch."_".$class_name;
160 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
161 my $flags = pop(@class);
162 $class_mode = $flags->{"mode"};
163 my $flags_prepared = map_flags("arch_register_class_flag_", $flags->{"flags"});
165 $single_constraints .= <<EOF;
166 static const arch_register_req_t ${arch}_class_reg_req_${old_classname} = {
167 arch_register_req_type_normal,
168 &${arch}_reg_classes[CLASS_${arch}_${old_classname}],
176 $classdef .= "\tCLASS_$class_name = $class_idx,\n";
177 my $numregs = @class;
178 my $first_reg = "&${arch}_registers[REG_". uc($class[0]->{"name"}) . "]";
179 push(@regclasses, "{ $class_idx, \"$class_name\", $numregs, NULL, $first_reg, $flags_prepared, &${arch}_class_reg_req_${old_classname} }");
182 $reginit .= "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n";
185 my $name = $_->{"name"};
186 my $ucname = uc($name);
187 my $type = "arch_register_type_none";
188 $type = translate_reg_type($_->{"type"}) if (exists($_->{"type"}));
189 # realname is name if not set by user
190 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
191 my $realname = $_->{realname};
192 my $classuc = uc($old_classname);
194 $regdef .= "\tREG_${ucname},\n";
195 $regdef2 .= "\tREG_${classuc}_${ucname} = $idx,\n";
196 my $dwarf_number = 0;
197 if (defined($_->{dwarf})) {
198 $dwarf_number = $_->{dwarf};
201 $regtypes_def .= <<EOF;
205 REG_${classuc}_${ucname},
208 &${arch}_single_reg_req_${old_classname}_${name},
213 my $limitedarray = get_limited_array($name);
214 $single_constraints .= <<EOF;
215 static const unsigned ${arch}_limited_${old_classname}_${name} [] = ${limitedarray};
216 static const arch_register_req_t ${arch}_single_reg_req_${old_classname}_${name} = {
217 arch_register_req_type_limited,
219 ${arch}_limited_${old_classname}_${name},
229 $regcounts .= "\tN_${class_name}_REGS = $numregs,\n";
234 my $archuc = uc($arch);
236 $classdef .= "\tN_${archuc}_CLASSES = ".scalar(keys(%reg_classes))."\n";
237 $classdef .= "};\n\n";
239 # generate header (external usage) file
240 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
242 my $creation_time = localtime(time());
247 * \@brief Contains additional external requirements defs for external includes.
248 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
249 * Edit $specfile instead.
250 * created by: $0 $specfile $target_dir
251 * \@date $creation_time
253 #ifndef FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
254 #define FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
257 #include "${arch}_nodes_attr.h"
259 /** global register indices for ${arch} registers */
262 N_${archuc}_REGISTERS
264 /** local register indices for ${arch} registers */
269 /** number of registers in ${arch} register classes. */
275 extern const arch_register_t ${arch}_registers[N_${archuc}_REGISTERS];
277 extern arch_register_class_t ${arch}_reg_classes[N_${archuc}_CLASSES];
279 void ${arch}_register_init(void);
287 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
289 $creation_time = localtime(time());
294 * \@brief The generated interface for the register allocator.
295 * Contains register classes and types and register constraints
296 * for all nodes where constraints were given in spec.
297 * \@note 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
304 #include "gen_${arch}_regalloc_if.h"
305 #include "bearch_${arch}_t.h"
308 ${single_constraints}
311 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @regclasses)."\n};\n\n";
315 /** The array of all registers in the ${arch} architecture, sorted by its global index.*/
316 const arch_register_t ${arch}_registers[] = {
321 * Initializes ${arch} register classes.
323 void ${arch}_register_init(void)