50ebc66471d2118f6d787c8500b84109b2dd202f
[libfirm] / ir / be / scripts / generate_regalloc_if.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2008 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 %cpu;
37
38 # include spec file
39
40 my $return;
41
42 use strict "subs";
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;
47 }
48 use strict "subs";
49
50 my $target_c   = $target_dir."/gen_".$arch."_regalloc_if.c";
51 my $target_h   = $target_dir."/gen_".$arch."_regalloc_if.h";
52
53 # helper function
54 sub translate_reg_type {
55         my $t = shift;
56
57         if ($t == 0) {
58                 return "arch_register_type_none";
59         }
60         else {
61                 my @types;
62
63                 if ($t & 1) {
64                         push(@types, "arch_register_type_caller_save");
65                 }
66
67                 if ($t & 2) {
68                         push(@types, "arch_register_type_callee_save");
69                 }
70
71                 if ($t & 4) {
72                         push(@types, "arch_register_type_ignore");
73                 }
74
75                 if ($t & 8) {
76                         push(@types, "arch_register_type_joker");
77                 }
78
79                 if ($t & 16) {
80                         push(@types, "arch_register_type_virtual");
81                 }
82
83                 if ($t & 32) {
84                         push(@types, "arch_register_type_state");
85                 }
86
87                 return join(" | ", @types);
88         }
89 }
90
91 # stacks for output
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
97 my $regdef2;
98 my $regcounts;
99 my $reginit;      # stack for the register type inits
100 my $single_constraints_decls;
101 my $single_constraints;
102
103 my $class_ptr;
104 my $class_idx = 0;
105
106 my %regclass2len = ();
107 my %reg2class = ();
108
109 $classdef .= "enum reg_classes {\n";
110
111 my $class_mode;
112
113 foreach my $class_name (keys(%reg_classes)) {
114         my @class = @{ $reg_classes{"$class_name"} };
115
116         my $idx = 0;
117         foreach (@class) {
118                 if (defined($_->{name})) {
119                         $reg2class{$_->{name}} = {
120                                 "class" => $class_name,
121                                 "index" => $idx
122                         };
123                 }
124                 $idx++;
125         }
126         $regclass2len{$class_name} = $idx;
127 }
128
129 sub get_limited_array {
130         my $reg      = shift;
131         my $regclass = $reg2class{"$reg"}{"class"};
132         my $ucname   = uc($reg);
133         my $result   = "{ ";
134
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);
139         my $first            = 1;
140         for (my $i = 0; $i < $arraylen; ++$i) {
141                 if ($first) {
142                         $first = 0;
143                 } else {
144                         $result .= ", ";
145                 }
146
147                 my $index = $reg2class{"$reg"}{"index"};
148                 if ($index >= $i*32 && $index < ($i+1)*32) {
149                         if ($i > 0) {
150                                 $result .= "(1 << (REG_${classuc}_${ucname} % 32))";
151                         } else {
152                                 $result .= "(1 << REG_${classuc}_${ucname})";
153                         }
154                 } else {
155                         $result .= "0";
156                 }
157         }
158         $result .= " }";
159 }
160
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;
165
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 = "";
172
173         if(defined($class_flags)) {
174                 my $first = 1;
175                 foreach my $flag (split(/\|/, $class_flags)) {
176                         if(!$first) {
177                                 $flags_prepared .= "|";
178                         } else {
179                                 $first = 0;
180                         }
181                         $flags_prepared .= "arch_register_class_flag_$flag";
182                 }
183         } else {
184                 $flags_prepared = "0";
185         }
186
187         $single_constraints_decls .= <<EOF;
188 static const arch_register_req_t ${arch}_class_reg_req_${old_classname};
189 EOF
190
191         $single_constraints .= <<EOF;
192 static const arch_register_req_t ${arch}_class_reg_req_${old_classname} = {
193         arch_register_req_type_normal,
194         &${arch}_reg_classes[CLASS_${arch}_${old_classname}],
195         NULL,
196         0,
197         0,
198         1
199 };
200 EOF
201
202         $classdef .= "\tCLASS_$class_name = $class_idx,\n";
203         my $numregs = @class;
204         my $first_reg = "&${arch}_registers[REG_". uc($class[0]->{"name"}) . "]";
205         push(@regclasses, "{ $class_idx, \"$class_name\", $numregs, NULL, $first_reg, $flags_prepared, &${arch}_class_reg_req_${old_classname} }");
206
207         my $idx = 0;
208         $reginit .= "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n";
209         my $lastreg;
210         foreach (@class) {
211                 my $name   = $_->{"name"};
212                 my $ucname = uc($name);
213                 my $type   = "arch_register_type_none";
214                 $type = translate_reg_type($_->{"type"}) if (exists($_->{"type"}));
215                 # realname is name if not set by user
216                 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
217                 my $realname = $_->{realname};
218                 my $classuc = uc($old_classname);
219
220                 $regdef  .= "\tREG_${ucname},\n";
221                 $regdef2 .= "\tREG_${classuc}_${ucname} = $idx,\n";
222
223                 $regtypes_def .= <<EOF;
224         {
225                 "${realname}",
226                 ${class_ptr},
227                 REG_${classuc}_${ucname},
228                 REG_${ucname},
229                 ${type},
230                 &${arch}_single_reg_req_${old_classname}_${name}
231         },
232 EOF
233
234                 my $limitedarray = get_limited_array($name);
235                 $single_constraints .= <<EOF;
236 static const unsigned ${arch}_limited_${old_classname}_${name} [] = ${limitedarray};
237 static const arch_register_req_t ${arch}_single_reg_req_${old_classname}_${name} = {
238         arch_register_req_type_limited,
239         ${class_ptr},
240         ${arch}_limited_${old_classname}_${name},
241         0,
242         0,
243         1
244 };
245 EOF
246
247                 $lastreg = $ucname;
248                 $idx++;
249         }
250         $regcounts .= "\tN_${class_name}_REGS = $numregs,\n";
251
252         $class_idx++;
253 }
254
255 my $archuc = uc($arch);
256
257 $classdef .= "\tN_${archuc}_CLASSES = ".scalar(keys(%reg_classes))."\n";
258 $classdef .= "};\n\n";
259
260 # generate header (external usage) file
261 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
262
263 my $creation_time = localtime(time());
264
265 print OUT<<EOF;
266 /**
267  * \@file
268  * \@brief Contains additional external requirements defs for external includes.
269  * \@note   DO NOT EDIT THIS FILE, your changes will be lost.
270  *         Edit $specfile instead.
271  *         created by: $0 $specfile $target_dir
272  * \@date   $creation_time
273  */
274 #ifndef FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
275 #define FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
276
277 #include "../bearch.h"
278 #include "${arch}_nodes_attr.h"
279
280 enum reg_indices {
281 ${regdef}
282         N_${archuc}_REGISTERS
283 };
284 enum {
285 ${regdef2}
286 };
287
288 enum {
289 ${regcounts}
290 };
291 ${classdef}
292
293 extern const arch_register_t ${arch}_registers[N_${archuc}_REGISTERS];
294
295 extern arch_register_class_t ${arch}_reg_classes[N_${archuc}_CLASSES];
296
297 void ${arch}_register_init(void);
298 unsigned ${arch}_get_n_regs(void);
299
300 #endif
301 EOF
302 close(OUT);
303
304
305 # generate c file
306 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
307
308 $creation_time = localtime(time());
309
310 print OUT<<EOF;
311 /**
312  * \@file
313  * \@brief  The generated interface for the register allocator.
314  *          Contains register classes and types and register constraints
315  *          for all nodes where constraints were given in spec.
316  * \@note    DO NOT EDIT THIS FILE, your changes will be lost.
317  *          Edit $specfile instead.
318  *          created by: $0 $specfile $target_dir
319  * \$date    $creation_time
320  */
321 #include "config.h"
322
323 #include "gen_${arch}_regalloc_if.h"
324 #include "gen_${arch}_machine.h"
325 #include "bearch_${arch}_t.h"
326 #include "irmode.h"
327
328 ${single_constraints_decls}
329 EOF
330
331 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @regclasses)."\n};\n\n";
332
333 print OUT<<EOF;
334 ${single_constraints}
335
336 const arch_register_t ${arch}_registers[] = {
337 ${regtypes_def}
338 };
339
340 void ${arch}_register_init(void)
341 {
342 ${reginit}
343 }
344 EOF
345 close(OUT);
346
347 ###
348 # Gets the variable name for the execution unit assigned to this register.
349 ###
350 sub get_execunit_variable_name {
351         my $unit    = shift;
352         my $name    = "NULL";
353         my $uc_arch = uc($arch);
354
355         if ($unit) {
356                 my $found = 0;
357 SRCH:   foreach my $cur_type (keys(%cpu)) {
358                         foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
359                                 if ($unit eq $cur_unit) {
360                                         my $tp_name   = "$arch\_execution_units_$cur_type";
361                                         my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
362                                         $name  = "&".$tp_name."[".$unit_name."]";
363                                         $found = 1;
364                                         last SRCH;
365                                 }
366                         }
367                 }
368
369                 if (! $found) {
370                         print STDERR "Invalid execution unit $unit specified!\n";
371                 }
372         }
373
374         return $name;
375 }