17384159c3a7829621d25c22d263518139cdc12b
[libfirm] / ir / be / scripts / generate_regalloc_if.pl
1 #!/usr/bin/perl -w
2
3 # This script generates C code which emits assembler code for the
4 # assembler ir nodes. It takes a "emit" key from the node specification
5 # and substitutes lines starting with . with a corresponding fprintf().
6 # Creation: 2005/11/14
7 # $Id$
8
9 use strict;
10 use Data::Dumper;
11
12 my $specfile   = $ARGV[0];
13 my $target_dir = $ARGV[1];
14
15 our $arch;
16 our %reg_classes;
17 our %nodes;
18
19 # include spec file
20
21 my $return;
22
23 no strict "subs";
24 unless ($return = do $specfile) {
25   warn "couldn't parse $specfile: $@" if $@;
26   warn "couldn't do $specfile: $!"    unless defined $return;
27   warn "couldn't run $specfile"       unless $return;
28 }
29 use strict "subs";
30
31 my $target_c   = $target_dir."/gen_".$arch."_regalloc_if.c";
32 my $target_h   = $target_dir."/gen_".$arch."_regalloc_if.h";
33 my $target_h_t = $target_dir."/gen_".$arch."_regalloc_if_t.h";
34
35 # helper function
36 my @rt = ("arch_register_type_none",
37           "arch_register_type_write_invariant",
38           "arch_register_type_caller_saved",
39           "arch_register_type_callee_saved",
40           "arch_register_type_ignore");
41
42 # stacks for output
43 my @obst_regtypes;     # stack for the register type variables
44 my @obst_regclasses;   # stack for the register class variables
45 my @obst_classdef;     # stack to define a name for a class index
46 my @obst_regdef;       # stack to define a name for a register index
47 my @obst_reginit;      # stack for the register type inits
48 my @obst_req;          # stack for the register requirements
49 my @obst_limit_func;   # stack for functions to return a subset of a register class
50 my @obst_defreq_head;  # stack for prototypes of default requirement function
51 my @obst_header_all;   # stack for some extern struct defs needed for bearch_$arch include
52
53 my $numregs;
54 my $class_ptr;
55 my $class_idx = 0;
56
57 my $tmp;
58
59 my %reg2class;
60
61 # there is a default NONE requirement
62 $tmp = "/* Default NONE register requirements */\n";
63 $tmp .= "const arch_register_req_t ia32_default_req_none = {\n";
64 $tmp .= "  arch_register_req_type_none,\n";
65 $tmp .= "  NULL,\n";
66 $tmp .= "  { NULL }\n";
67 $tmp .= "};\n\n";
68 push(@obst_req, $tmp);
69
70 push(@obst_header_all, "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n");
71 push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_none;\n");
72
73 push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
74
75 # generate register type and class variable, init function and default requirements
76 foreach my $class_name (keys(%reg_classes)) {
77   my @class         = @{ $reg_classes{"$class_name"} };
78   my $old_classname = $class_name;
79
80   $class_name = $arch."_".$class_name;
81   $numregs    = "N_".$class_name."_REGS";
82   $class_ptr  = "&".$arch."_reg_classes[CLASS_".$class_name."]";
83
84   push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
85   push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
86
87   push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
88   push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
89
90   # there is a default NORMAL requirement for each class
91   $tmp  = "/* Default NORMAL register requirements for class $class_name */\n";
92   $tmp .= "const arch_register_req_t ia32_default_req_$class_name = {\n";
93   $tmp .= "  arch_register_req_type_normal,\n";
94   $tmp .= "  $class_ptr,\n";
95   $tmp .= "  { NULL }\n";
96   $tmp .= "};\n\n";
97   push(@obst_req, $tmp);
98
99   push(@obst_header_all, "\nextern const arch_register_req_t ia32_default_req_$class_name;\n");
100
101   my $idx = 0;
102   push(@obst_reginit, "  /* Init of all registers in class '$class_name' */\n\n");
103   foreach (@class) {
104     # For each class we build for each of it's member registers a limit function
105     # which limits the class to this particular register. We also build the
106     # corresponding requirement structs.
107     # We need those functions to set register requirements on demand in transformation
108     # esp. for Call and RegParams where we can mix int and float parameters.
109
110     my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
111
112     # push the function prototype
113     $tmp = "int $limit_func_name(const ir_node *irn, int pos, bitset_t *bs)";
114     push(@obst_defreq_head, $tmp.";\n");
115
116     # push the function definition
117     $tmp .= " {\n";
118     $tmp .= "    bs = bitset_clear_all(bs);\n";
119     $tmp .= "    bitset_set(bs, REG_".uc($_->{"name"}).");\n";  # REGISTER to index assignment is done some lines down
120     $tmp .= "    return 1;\n";
121     $tmp .= "}\n\n";
122     push(@obst_limit_func, $tmp);
123
124     # push the default requirement struct
125     $tmp  = "const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}." = {\n";
126     $tmp .= "  arch_register_req_type_limited,\n";
127     $tmp .= "  $class_ptr,\n";
128     $tmp .= "  { $limit_func_name }\n";
129     $tmp .= "};\n\n";
130     push(@obst_req, $tmp);
131
132     push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}.";\n");
133
134     $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
135     push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n");
136     push(@obst_reginit, "  ".$class_name."_regs[$idx].name      = \"".$_->{"name"}."\";\n");
137     push(@obst_reginit, "  ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
138     push(@obst_reginit, "  ".$class_name."_regs[$idx].index     = $idx;\n");
139     push(@obst_reginit, "  ".$class_name."_regs[$idx].type      = ".$rt[$_->{"type"}].";\n\n");
140     $idx++;
141   }
142
143   $class_idx++;
144 }
145
146 push(@obst_header_all, "\n/* node specific requirements */\n");
147
148 # generate node-register constraints
149 foreach my $op (keys(%nodes)) {
150   my %n = %{ $nodes{"$op"} };
151
152   next if (!exists($n{"reg_req"}));
153
154   $op = $arch."_".$op;
155
156   push(@obst_req, "/* IN requirements for '$op' */\n");
157
158   # we need to remember the classes of the IN constraints for
159   # OUT constraints like "in_s1"
160   my @inidx_class;
161
162   # check for argument requirements
163   if (exists($n{"reg_req"}{"in"})) {
164     my @in = @{ $n{"reg_req"}{"in"} };
165
166     for (my $idx = 0; $idx <= $#in; $idx++) {
167       my $class = undef;
168
169       my $tmp2 = "const arch_register_req_t _".$op."_reg_req_in_$idx = ";
170
171       $tmp = "const arch_register_req_t *".$op."_reg_req_in_$idx = ";
172
173       push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_in_$idx;\n");
174
175       if ($in[$idx] eq "none") {
176         push(@inidx_class, "none");
177         $tmp .= "&ia32_default_req_none;\n";
178       }
179       elsif (is_reg_class($in[$idx])) {
180         push(@inidx_class, $in[$idx]);
181         $tmp .= "&ia32_default_req_".$arch."_".$in[$idx].";\n";
182       }
183       else {
184         $class = build_subset_class_func($op, $idx, 1, $in[$idx]);
185         if (!defined $class) {
186           die("Could not build subset for IN requirements '$op' pos $idx ... exiting.\n");
187         }
188         push(@inidx_class, $class);
189         $tmp  .= "&_".$op."_reg_req_in_$idx;\n";
190         $tmp2 .= " {\n  arch_register_req_type_limited,\n  &$arch\_reg_classes[CLASS_$arch\_".$class."],\n  { limit_reg_".$op."_in_".$idx." }\n};\n";
191
192         $tmp   = $tmp2.$tmp;
193       }
194
195       push(@obst_req, $tmp."\n");
196     }
197   }
198
199   push(@obst_req, "/* OUT requirements for '$op' */\n");
200
201   # check for result requirements
202   if (exists($n{"reg_req"}{"out"})) {
203     my @out = @{ $n{"reg_req"}{"out"} };
204
205     for (my $idx = 0; $idx <= $#out; $idx++) {
206       my $class = undef;
207
208       my $tmp2 = "const arch_register_req_t _".$op."_reg_req_out_$idx = ";
209
210       $tmp = "const arch_register_req_t *".$op."_reg_req_out_$idx = ";
211
212       push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_out_$idx;\n");
213
214       if ($out[$idx] eq "none") {
215         $tmp .= "&ia32_default_req_none;\n";
216       }
217       elsif (is_reg_class($out[$idx])) {
218         $tmp .= "&ia32_default_req_".$arch."_".$out[$idx].";\n";
219       }
220       elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) { # this is a "should be (un)equal to register at in_X"
221         $tmp  .= "&_".$op."_reg_req_out_$idx;\n";
222         $tmp2 .= " {\n";
223         $tmp2 .= "  arch_register_req_type_".($1 ? "un" : "")."equal,\n";
224         $tmp2 .= "  &$arch\_reg_classes[CLASS_$arch\_".$inidx_class[$2 - 1]."],\n";
225         $tmp2 .= "  { ".($2 - 1)." }\n};\n";
226
227         $tmp   = $tmp2.$tmp
228       }
229       else {
230         $class = build_subset_class_func($op, $idx, 0, $out[$idx]);
231         if (!defined $class) {
232           die("Could not build subset for OUT requirements '$op' pos $idx ... exiting.\n");
233         }
234         $tmp  .= "&_".$op."_reg_req_out_$idx;\n";
235         $tmp2 .= " {\n  arch_register_req_type_limited,\n  &$arch\_reg_classes[CLASS_$arch\_".$class."],\n  { limit_reg_".$op."_out_".$idx." }\n};\n";
236
237         $tmp   = $tmp2.$tmp
238       }
239
240       push(@obst_req, $tmp."\n");
241     }
242   }
243 }
244
245
246
247 # generate header _t (internal usage) file
248 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
249
250 my $creation_time = localtime(time());
251
252 $tmp = uc($arch);
253
254 print OUT<<EOF;
255 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
256 #define _GEN_$tmp\_REGALLOC_IF_T_H_
257
258 /**
259  * Generated register classes from spec.
260  *
261  * DO NOT EDIT THIS FILE, your changes will be lost.
262  * Edit $specfile instead.
263  * created by: $0 $specfile $target_dir
264  * date:       $creation_time
265  */
266
267 #include "../bearch.h"
268
269 EOF
270
271 print OUT @obst_regdef, "\n";
272
273 print OUT @obst_classdef, "\n";
274
275 print OUT @obst_regtypes, "\n";
276
277 print OUT @obst_defreq_head, "\n";
278
279 print OUT "void ".$arch."_register_init(void);\n\n";
280
281 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
282
283
284
285 # generate header (external usage) file
286 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
287
288 $creation_time = localtime(time());
289
290 print OUT<<EOF;
291 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
292 #define _GEN_$tmp\_REGALLOC_IF_H_
293
294 /**
295  * Contains additional external requirements defs for external includes.
296  *
297  * DO NOT EDIT THIS FILE, your changes will be lost.
298  * Edit $specfile instead.
299  * created by: $0 $specfile $target_dir
300  * date:       $creation_time
301  */
302
303 #include "gen_$arch\_regalloc_if_t.h"
304
305 EOF
306
307 print OUT @obst_header_all;
308
309 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
310
311 close(OUT);
312
313
314
315 # generate c inline file
316 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
317
318 $creation_time = localtime(time());
319
320 print OUT<<EOF;
321 /**
322  * The generated interface for the register allocator.
323  * Contains register classes and types and register constraints
324  * for all nodes where constraints were given in spec.
325  *
326  * DO NOT EDIT THIS FILE, your changes will be lost.
327  * Edit $specfile instead.
328  * created by: $0 $specfile $target_dir
329  * date:       $creation_time
330  */
331
332 #include "gen_$arch\_regalloc_if_t.h"
333
334 EOF
335
336 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n  ".join(",\n  ", @obst_regclasses)."\n};\n\n";
337
338 print OUT "void ".$arch."_register_init(void) {\n";
339 print OUT @obst_reginit;
340 print OUT "}\n\n";
341
342 print OUT @obst_limit_func;
343 print OUT @obst_req;
344
345 close(OUT);
346
347
348
349 ###
350 # Determines whether $name is a specified register class or not.
351 # @return 1 if name is register class, 0 otherwise
352 ###
353 sub is_reg_class {
354   my $name = shift;
355   return 1 if exists($reg_classes{"$name"});
356   return 0;
357 }
358
359 ###
360 # Returns the register class for a given register.
361 # @return class or undef
362 ###
363 sub get_reg_class {
364   my $reg = shift;
365   return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
366   return undef;
367 }
368
369 ###
370 # Returns the index of a given register within it's register class.
371 # @return index or undef
372 ###
373 sub get_reg_index {
374   my $reg = shift;
375   return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
376   return undef;
377 }
378
379 ###
380 # Generates the function for a given $op and a given IN-index
381 # which returns a subset of possible register from a register class
382 # @return classname from which the subset is derived or undef
383 ###
384 sub build_subset_class_func {
385   my $neg   = undef;
386   my $class = "";
387   my $temp;
388
389   # build function header
390   my $op  = shift;
391   my $idx = shift;
392   my $in  = shift;
393   push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
394   push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
395
396   my @regs = split(/ /, shift);
397
398   # set/unset registers
399   foreach (@regs) {
400     # check for negate
401
402     if (substr($_, 0, 1) eq "!") {
403       if (defined($neg) && $neg == 0) {
404         # we have seen a positiv constraint as first one but this one is negative
405         # this doesn't make sense
406         print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
407         return undef;
408       }
409
410       if (!defined($neg)) {
411         push(@obst_limit_func, "  bs = bitset_set_all(bs);     /* allow all register (negative constraints given) */\n");
412       }
413
414       $_   = substr($_, 1); # skip '!'
415       $neg = 1;
416     }
417     else {
418       if (defined($neg) && $neg == 1) {
419         # we have seen a negative constraint as first one but this one is positive
420         # this doesn't make sense
421         print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
422         return undef;
423       }
424
425       if (!defined($neg)) {
426         push(@obst_limit_func, "  bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */\n");
427       }
428       $neg = 0;
429     }
430
431     # check if register belongs to one of the given classes
432     $temp = get_reg_class($_);
433     if (!defined($temp)) {
434       print STDERR "Unknown register '$_'!\n";
435       return undef;
436     }
437
438     # set class
439     if (!$class) {
440       $class = $temp;
441     }
442     elsif ($class ne $temp) {
443       # all registers must belong to the same class
444       print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
445       return undef;
446     }
447
448     if ($neg == 1) {
449       push(@obst_limit_func, "  bitset_clear(bs, ".get_reg_index($_).");         /* disallow $_ */\n");
450     }
451     else {
452       push(@obst_limit_func, "  bitset_set(bs, ".get_reg_index($_).");           /* allow $_ */\n");
453     }
454   }
455
456   push(@obst_limit_func, "\n  return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
457   push(@obst_limit_func, "}\n\n");
458
459   return $class;
460 }