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