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
31 my $specfile = $ARGV[0];
32 my $target_dir = $ARGV[1];
43 unless ($return = do $specfile) {
44 die "Fatal error: couldn't parse $specfile: $@" if $@;
45 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
46 die "Fatal error: couldn't run $specfile" unless $return;
50 my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c";
51 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
54 sub translate_reg_type {
58 return "arch_register_type_none";
64 push(@types, "arch_register_type_caller_save");
68 push(@types, "arch_register_type_callee_save");
72 push(@types, "arch_register_type_ignore");
76 push(@types, "arch_register_type_joker");
80 push(@types, "arch_register_type_virtual");
84 push(@types, "arch_register_type_state");
87 return join(" | ", @types);
92 my $regtypes_def; # stack for the register type variables definitions
93 my $regtypes_decl;# stack for the register type variables declarations
94 my @regclasses; # stack for the register class variables
95 my $classdef; # stack to define a name for a class index
96 my $regdef; # stack to define a name for a register index
99 my $reginit; # stack for the register type inits
100 my $single_constraints_decls;
101 my $single_constraints;
106 my %regclass2len = ();
109 $classdef .= "enum reg_classes {\n";
113 foreach my $class_name (keys(%reg_classes)) {
114 my @class = @{ $reg_classes{"$class_name"} };
118 if (defined($_->{name})) {
119 $reg2class{$_->{name}} = {
120 "class" => $class_name,
126 $regclass2len{$class_name} = $idx;
129 sub get_limited_array {
131 my $regclass = $reg2class{"$reg"}{"class"};
132 my $ucname = uc($reg);
135 my $limitedbitsetlen = $regclass2len{$regclass};
136 my $arraylen = ($limitedbitsetlen+31) / 32;
137 my $firstreg = uc($reg_classes{$regclass}[0]->{"name"});
138 my $classuc = uc($regclass);
140 for (my $i = 0; $i < $arraylen; ++$i) {
147 my $index = $reg2class{"$reg"}{"index"};
148 if ($index >= $i*32 && $index < ($i+1)*32) {
150 $result .= "(1 << (REG_${classuc}_${ucname} % 32))";
152 $result .= "(1 << REG_${classuc}_${ucname})";
161 # generate register type and class variable, init function and default requirements
162 foreach my $class_name (keys(%reg_classes)) {
163 my @class = @{ $reg_classes{"$class_name"} };
164 my $old_classname = $class_name;
166 $class_name = $arch."_".$class_name;
167 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
168 my $flags = pop(@class);
169 $class_mode = $flags->{"mode"};
170 my $class_flags = $flags->{"flags"};
171 my $flags_prepared = "";
173 if(defined($class_flags)) {
175 foreach my $flag (split(/\|/, $class_flags)) {
177 $flags_prepared .= "|";
181 $flags_prepared .= "arch_register_class_flag_$flag";
184 $flags_prepared = "arch_register_class_flag_none";
187 $single_constraints .= <<EOF;
188 static const arch_register_req_t ${arch}_class_reg_req_${old_classname} = {
189 arch_register_req_type_normal,
190 &${arch}_reg_classes[CLASS_${arch}_${old_classname}],
198 $classdef .= "\tCLASS_$class_name = $class_idx,\n";
199 my $numregs = @class;
200 my $first_reg = "&${arch}_registers[REG_". uc($class[0]->{"name"}) . "]";
201 push(@regclasses, "{ $class_idx, \"$class_name\", $numregs, NULL, $first_reg, $flags_prepared, &${arch}_class_reg_req_${old_classname} }");
204 $reginit .= "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n";
207 my $name = $_->{"name"};
208 my $ucname = uc($name);
209 my $type = "arch_register_type_none";
210 $type = translate_reg_type($_->{"type"}) if (exists($_->{"type"}));
211 # realname is name if not set by user
212 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
213 my $realname = $_->{realname};
214 my $classuc = uc($old_classname);
216 $regdef .= "\tREG_${ucname},\n";
217 $regdef2 .= "\tREG_${classuc}_${ucname} = $idx,\n";
219 $regtypes_def .= <<EOF;
223 REG_${classuc}_${ucname},
226 &${arch}_single_reg_req_${old_classname}_${name}
230 my $limitedarray = get_limited_array($name);
231 $single_constraints .= <<EOF;
232 static const unsigned ${arch}_limited_${old_classname}_${name} [] = ${limitedarray};
233 static const arch_register_req_t ${arch}_single_reg_req_${old_classname}_${name} = {
234 arch_register_req_type_limited,
236 ${arch}_limited_${old_classname}_${name},
246 $regcounts .= "\tN_${class_name}_REGS = $numregs,\n";
251 my $archuc = uc($arch);
253 $classdef .= "\tN_${archuc}_CLASSES = ".scalar(keys(%reg_classes))."\n";
254 $classdef .= "};\n\n";
256 # generate header (external usage) file
257 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
259 my $creation_time = localtime(time());
264 * \@brief Contains additional external requirements defs for external includes.
265 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
266 * Edit $specfile instead.
267 * created by: $0 $specfile $target_dir
268 * \@date $creation_time
270 #ifndef FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
271 #define FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
273 #include "../bearch.h"
274 #include "${arch}_nodes_attr.h"
276 /** global register indices for ${arch} registers */
279 N_${archuc}_REGISTERS
281 /** local register indices for ${arch} registers */
286 /** number of registers in ${arch} register classes. */
292 extern const arch_register_t ${arch}_registers[N_${archuc}_REGISTERS];
294 extern arch_register_class_t ${arch}_reg_classes[N_${archuc}_CLASSES];
296 void ${arch}_register_init(void);
297 unsigned ${arch}_get_n_regs(void);
305 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
307 $creation_time = localtime(time());
312 * \@brief The generated interface for the register allocator.
313 * Contains register classes and types and register constraints
314 * for all nodes where constraints were given in spec.
315 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
316 * Edit $specfile instead.
317 * created by: $0 $specfile $target_dir
318 * \$date $creation_time
322 #include "gen_${arch}_regalloc_if.h"
323 #include "gen_${arch}_machine.h"
324 #include "bearch_${arch}_t.h"
327 ${single_constraints}
330 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @regclasses)."\n};\n\n";
334 /** The array of all registers in the ${arch} architecture, sorted by its global index.*/
335 const arch_register_t ${arch}_registers[] = {
340 * Initializes ${arch} register classes.
342 void ${arch}_register_init(void)
350 # Gets the variable name for the execution unit assigned to this register.
352 sub get_execunit_variable_name {
355 my $uc_arch = uc($arch);
359 SRCH: foreach my $cur_type (keys(%cpu)) {
360 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
361 if ($unit eq $cur_unit) {
362 my $tp_name = "$arch\_execution_units_$cur_type";
363 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
364 $name = "&".$tp_name."[".$unit_name."]";
372 print STDERR "Invalid execution unit $unit specified!\n";