no need to generate the old-style requirements anymore
[libfirm] / ir / be / scripts / generate_regalloc_if.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
5 #
6 # This file is part of libFirm.
7 #
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.
12 #
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.
16 #
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
19 # PURPOSE.
20 #
21
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
25 # $Id$
26
27 use strict;
28 use Data::Dumper;
29 use integer;
30
31 my $specfile   = $ARGV[0];
32 my $target_dir = $ARGV[1];
33
34 our $arch;
35 our %reg_classes;
36 our %nodes;
37 our %cpu;
38 our %flags = ();
39
40 # include spec file
41
42 my $return;
43
44 use strict "subs";
45 unless ($return = do $specfile) {
46         die "Fatal error: couldn't parse $specfile: $@" if $@;
47         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
48         die "Fatal error: couldn't run $specfile"       unless $return;
49 }
50 use strict "subs";
51
52 my $target_c   = $target_dir."/gen_".$arch."_regalloc_if.c";
53 my $target_h   = $target_dir."/gen_".$arch."_regalloc_if.h";
54
55 # helper function
56 sub translate_reg_type {
57         my $t = shift;
58
59         if ($t == 0) {
60                 return "arch_register_type_none";
61         }
62         else {
63                 my @types;
64
65                 if ($t & 1) {
66                         push(@types, "arch_register_type_caller_save");
67                 }
68
69                 if ($t & 2) {
70                         push(@types, "arch_register_type_callee_save");
71                 }
72
73                 if ($t & 4) {
74                         push(@types, "arch_register_type_ignore");
75                 }
76
77                 if ($t & 8) {
78                         push(@types, "arch_register_type_joker");
79                 }
80
81                 if ($t & 16) {
82                         push(@types, "arch_register_type_virtual");
83                 }
84
85                 if ($t & 32) {
86                         push(@types, "arch_register_type_state");
87                 }
88
89                 return join(" | ", @types);
90         }
91 }
92
93 # stacks for output
94 my @obst_regtypes_def; # stack for the register type variables definitions
95 my @obst_regtypes_decl;# stack for the register type variables declarations
96 my @obst_regclasses;   # stack for the register class variables
97 my @obst_classdef;     # stack to define a name for a class index
98 my @obst_regdef;       # stack to define a name for a register index
99 my @obst_reginit;      # stack for the register type inits
100 my @obst_req;          # stack for the register requirements
101 my @obst_limit_func;   # stack for functions to return a subset of a register class
102 my @obst_header_all;   # stack for some extern struct defs needed for bearch_$arch include
103
104 my $numregs;
105 my $class_ptr;
106 my $class_idx = 0;
107
108 my $tmp;
109
110 my %reg2class;
111 my %regclass2len;
112
113 push(@obst_classdef, "enum reg_classes {\n");
114
115 my $class_mode;
116
117 # generate register type and class variable, init function and default requirements
118 foreach my $class_name (keys(%reg_classes)) {
119         my @class         = @{ $reg_classes{"$class_name"} };
120         my $old_classname = $class_name;
121
122         $class_name = $arch."_".$class_name;
123         $numregs    = "N_".$class_name."_REGS";
124         $class_ptr  = "&".$arch."_reg_classes[CLASS_".$class_name."]";
125         $class_mode = pop(@class)->{"mode"};
126
127         push(@obst_regtypes_decl, "extern const arch_register_t ${class_name}_regs[$numregs];\n");
128
129         push(@obst_classdef, "\tCLASS_$class_name = $class_idx,\n");
130         push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
131
132         my $idx = 0;
133         push(@obst_reginit, "\t/* set largest possible mode for '$class_name' */\n");
134         push(@obst_reginit, "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
135         push(@obst_regtypes_def, "const arch_register_t ${class_name}_regs[$numregs] = {\n");
136
137         push(@obst_regdef, "enum reg_${class_name}_indices {\n");
138         foreach (@class) {
139                 my $ucname = uc($_->{"name"});
140                 my $type = translate_reg_type($_->{"type"});
141                 # realname is name if not set by user
142                 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
143                 my $realname = $_->{realname};
144                 my $execunitvarname = get_execunit_variable_name($_->{"unit"});
145
146
147                 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
148                 push(@obst_regdef, "\tREG_${ucname},\n");
149
150                 push(@obst_regtypes_def, "\t{\n");
151                 push(@obst_regtypes_def, "\t\t\"$realname\",\n");
152                 push(@obst_regtypes_def, "\t\t$class_ptr,\n");
153                 push(@obst_regtypes_def, "\t\tREG_${ucname},\n");
154                 push(@obst_regtypes_def, "\t\t$type,\n");
155                 push(@obst_regtypes_def, "\t\t$execunitvarname\n");
156                 push(@obst_regtypes_def, "\t},\n");
157
158                 $idx++;
159         }
160         push(@obst_regtypes_def, "};\n");
161
162         $regclass2len{$old_classname} = $idx;
163         push(@obst_regdef, "\t$numregs = $idx\n");
164         push(@obst_regdef, "};\n\n");
165
166         $class_idx++;
167 }
168
169 push(@obst_regdef, "enum flag_indices {\n");
170 foreach my $flag (keys(%flags)) {
171         my %f = %{ $flags{$flag} };
172
173         push(@obst_regdef, "\tFLAG_$flag,\n");
174 }
175 push(@obst_regdef, "\tFLAG_LAST\n");
176 push(@obst_regdef, "};\n");
177 push(@obst_regtypes_decl, "extern arch_flag_t ${arch}_flags[];\n");
178
179 push(@obst_classdef, "\tN_CLASSES = ".scalar(keys(%reg_classes))."\n");
180 push(@obst_classdef, "};\n\n");
181
182 $tmp = uc($arch);
183
184 # generate header (external usage) file
185 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
186
187 my $creation_time = localtime(time());
188
189 print OUT<<EOF;
190 /**
191  * \@file
192  * \@brief Contains additional external requirements defs for external includes.
193  * \@note   DO NOT EDIT THIS FILE, your changes will be lost.
194  *         Edit $specfile instead.
195  *         created by: $0 $specfile $target_dir
196  * \@date   $creation_time
197  */
198 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_H
199 #define FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_H
200
201 #include "../bearch.h"
202 #include "${arch}_nodes_attr.h"
203
204 EOF
205
206 print OUT @obst_regdef, "\n";
207
208 print OUT @obst_classdef, "\n";
209
210 print OUT @obst_regtypes_decl, "\n";
211
212 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
213
214 print OUT "void ".$arch."_register_init(void);\n\n";
215
216 print OUT @obst_header_all, "\n";
217
218 print OUT "\n#endif\n";
219
220 close(OUT);
221
222
223
224 # generate c file
225 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
226
227 $creation_time = localtime(time());
228
229 print OUT<<EOF;
230 /**
231  * \@file
232  * \@brief  The generated interface for the register allocator.
233  *          Contains register classes and types and register constraints
234  *          for all nodes where constraints were given in spec.
235  * \@note    DO NOT EDIT THIS FILE, your changes will be lost.
236  *          Edit $specfile instead.
237  *          created by: $0 $specfile $target_dir
238  * \$date    $creation_time
239  */
240 #ifdef HAVE_CONFIG_H
241 #include "config.h"
242 #endif
243
244 #include "gen_${arch}_regalloc_if.h"
245 #include "gen_${arch}_machine.h"
246 #include "bearch_${arch}_t.h"
247 #include "${arch}_map_regs.h"
248 #include "irmode.h"
249
250 EOF
251
252 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
253
254 print OUT @obst_regtypes_def, "\n";
255
256 print OUT "void ${arch}_register_init(void) {\n";
257 print OUT @obst_reginit;
258 print OUT "}\n\n";
259
260 print OUT @obst_limit_func;
261
262 print OUT @obst_req;
263
264 close(OUT);
265
266 ###
267 # Gets the variable name for the execution unit assigned to this register.
268 ###
269 sub get_execunit_variable_name {
270         my $unit    = shift;
271         my $name    = "NULL";
272         my $uc_arch = uc($arch);
273
274         if ($unit) {
275                 my $found = 0;
276 SRCH:   foreach my $cur_type (keys(%cpu)) {
277                         foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
278                                 if ($unit eq $cur_unit) {
279                                         my $tp_name   = "$arch\_execution_units_$cur_type";
280                                         my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
281                                         $name  = "&".$tp_name."[".$unit_name."]";
282                                         $found = 1;
283                                         last SRCH;
284                                 }
285                         }
286                 }
287
288                 if (! $found) {
289                         print STDERR "Invalid execution unit $unit specified!\n";
290                 }
291         }
292
293         return $name;
294 }