Add some doxygen docu.
[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 = "arch_register_class_flag_none";
185         }
186
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}],
191         NULL,
192         0,
193         0,
194         1
195 };
196 EOF
197
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} }");
202
203         my $idx = 0;
204         $reginit .= "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n";
205         my $lastreg;
206         foreach (@class) {
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);
215
216                 $regdef  .= "\tREG_${ucname},\n";
217                 $regdef2 .= "\tREG_${classuc}_${ucname} = $idx,\n";
218
219                 $regtypes_def .= <<EOF;
220         {
221                 "${realname}",
222                 ${class_ptr},
223                 REG_${classuc}_${ucname},
224                 REG_${ucname},
225                 ${type},
226                 &${arch}_single_reg_req_${old_classname}_${name}
227         },
228 EOF
229
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,
235         ${class_ptr},
236         ${arch}_limited_${old_classname}_${name},
237         0,
238         0,
239         1
240 };
241 EOF
242
243                 $lastreg = $ucname;
244                 $idx++;
245         }
246         $regcounts .= "\tN_${class_name}_REGS = $numregs,\n";
247
248         $class_idx++;
249 }
250
251 my $archuc = uc($arch);
252
253 $classdef .= "\tN_${archuc}_CLASSES = ".scalar(keys(%reg_classes))."\n";
254 $classdef .= "};\n\n";
255
256 # generate header (external usage) file
257 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
258
259 my $creation_time = localtime(time());
260
261 print OUT<<EOF;
262 /**
263  * \@file
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
269  */
270 #ifndef FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
271 #define FIRM_BE_${archuc}_GEN_${archuc}_REGALLOC_IF_H
272
273 #include "../bearch.h"
274 #include "${arch}_nodes_attr.h"
275
276 /** global register indices for ${arch} registers */
277 enum reg_indices {
278 ${regdef}
279         N_${archuc}_REGISTERS
280 };
281 /** local register indices for ${arch} registers */
282 enum {
283 ${regdef2}
284 };
285
286 /** number of registers in ${arch} register classes. */
287 enum {
288 ${regcounts}
289 };
290 ${classdef}
291
292 extern const arch_register_t ${arch}_registers[N_${archuc}_REGISTERS];
293
294 extern arch_register_class_t ${arch}_reg_classes[N_${archuc}_CLASSES];
295
296 void ${arch}_register_init(void);
297 unsigned ${arch}_get_n_regs(void);
298
299 #endif
300 EOF
301 close(OUT);
302
303
304 # generate c file
305 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
306
307 $creation_time = localtime(time());
308
309 print OUT<<EOF;
310 /**
311  * \@file
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
319  */
320 #include "config.h"
321
322 #include "gen_${arch}_regalloc_if.h"
323 #include "gen_${arch}_machine.h"
324 #include "bearch_${arch}_t.h"
325 #include "irmode.h"
326
327 ${single_constraints}
328 EOF
329
330 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @regclasses)."\n};\n\n";
331
332 print OUT<<EOF;
333
334 /** The array of all registers in the ${arch} architecture, sorted by its global index.*/
335 const arch_register_t ${arch}_registers[] = {
336 ${regtypes_def}
337 };
338
339 /**
340  * Initializes ${arch} register classes.
341  */
342 void ${arch}_register_init(void)
343 {
344 ${reginit}
345 }
346 EOF
347 close(OUT);
348
349 ###
350 # Gets the variable name for the execution unit assigned to this register.
351 ###
352 sub get_execunit_variable_name {
353         my $unit    = shift;
354         my $name    = "NULL";
355         my $uc_arch = uc($arch);
356
357         if ($unit) {
358                 my $found = 0;
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."]";
365                                         $found = 1;
366                                         last SRCH;
367                                 }
368                         }
369                 }
370
371                 if (! $found) {
372                         print STDERR "Invalid execution unit $unit specified!\n";
373                 }
374         }
375
376         return $name;
377 }