6ad370e10d50e08d638a075e2fd84e2117b0a1e0
[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 .= "  0\n";
68 $tmp .= "};\n\n";
69 push(@obst_req, $tmp);
70
71 push(@obst_header_all, "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n");
72 push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_none;\n");
73
74 push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
75
76 # generate register type and class variable, init function and default requirements
77 foreach my $class_name (keys(%reg_classes)) {
78   my @class         = @{ $reg_classes{"$class_name"} };
79   my $old_classname = $class_name;
80
81   $class_name = $arch."_".$class_name;
82   $numregs    = "N_".$class_name."_REGS";
83   $class_ptr  = "&".$arch."_reg_classes[CLASS_".$class_name."]";
84
85   push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
86   push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
87
88   push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
89   push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }");
90
91   # there is a default NORMAL requirement for each class
92   $tmp  = "/* Default NORMAL register requirements for class $class_name */\n";
93   $tmp .= "const arch_register_req_t ia32_default_req_$class_name = {\n";
94   $tmp .= "  arch_register_req_type_normal,\n";
95   $tmp .= "  $class_ptr,\n";
96   $tmp .= "  NULL,\n";
97   $tmp .= "  0\n";
98   $tmp .= "};\n\n";
99   push(@obst_req, $tmp);
100
101   push(@obst_header_all, "\nextern const arch_register_req_t ia32_default_req_$class_name;\n");
102
103   my $idx = 0;
104   push(@obst_reginit, "  /* Init of all registers in class '$class_name' */\n\n");
105   foreach (@class) {
106     # For each class we build for each of it's member registers a limit function
107     # which limits the class to this particular register. We also build the
108     # corresponding requirement structs.
109     # We need those functions to set register requirements on demand in transformation
110     # esp. for Call and RegParams where we can mix int and float parameters.
111
112     my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"};
113
114     # push the function prototype
115     $tmp = "int $limit_func_name(const ir_node *irn, int pos, bitset_t *bs)";
116     push(@obst_defreq_head, $tmp.";\n");
117
118     # push the function definition
119     $tmp .= " {\n";
120     $tmp .= "    bs = bitset_clear_all(bs);\n";
121     $tmp .= "    bitset_set(bs, REG_".uc($_->{"name"}).");\n";  # REGISTER to index assignment is done some lines down
122     $tmp .= "    return 1;\n";
123     $tmp .= "}\n\n";
124     push(@obst_limit_func, $tmp);
125
126     # push the default requirement struct
127     $tmp  = "const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}." = {\n";
128     $tmp .= "  arch_register_req_type_limited,\n";
129     $tmp .= "  $class_ptr,\n";
130     $tmp .= "  $limit_func_name,\n";
131         $tmp .= "  0\n";
132     $tmp .= "};\n\n";
133     push(@obst_req, $tmp);
134
135     push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}.";\n");
136
137     $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
138     push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n");
139     push(@obst_reginit, "  ".$class_name."_regs[$idx].name      = \"".$_->{"name"}."\";\n");
140     push(@obst_reginit, "  ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
141     push(@obst_reginit, "  ".$class_name."_regs[$idx].index     = $idx;\n");
142     push(@obst_reginit, "  ".$class_name."_regs[$idx].type      = ".$rt[$_->{"type"}].";\n\n");
143     $idx++;
144   }
145
146   $class_idx++;
147 }
148
149 push(@obst_header_all, "\n/* node specific requirements */\n");
150
151 # generate node-register constraints
152 foreach my $op (keys(%nodes)) {
153   my %n = %{ $nodes{"$op"} };
154
155   next if (!exists($n{"reg_req"}));
156
157   $op = $arch."_".$op;
158
159   push(@obst_req, "/* IN requirements for '$op' */\n");
160
161   # We can have IN requirements like "out_d1"
162   # and for those we need the associated register class
163   # That's why we have to check all out requirements first
164   # and remember their classes.
165   my @outidx_class;
166   if (exists($n{"reg_req"}{"out"})) {
167     my @out = @{ $n{"reg_req"}{"out"} };
168
169     for (my $idx = 0; $idx <= $#out; $idx++) {
170       my $class = undef;
171
172       if ($out[$idx] eq "none") {
173                 $class = "none";
174       }
175       elsif (is_reg_class($out[$idx])) {
176                 $class = $out[$idx];
177       }
178       elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) {
179                 $class = "UNKNOWN_CLASS";
180       }
181       else {
182                 my @regs = split(/ /, $out[$idx]);
183         $class = get_reg_class($regs[0]);
184         if (!defined $class) {
185           die("Could not get register class  for '$op' pos $idx ... exiting.\n");
186         }
187       }
188
189       push(@outidx_class, $class);
190     }
191   }
192
193
194   # we need to remember the classes of the IN constraints for
195   # OUT constraints like "in_s1"
196   my @inidx_class;
197
198   # check for argument requirements
199   if (exists($n{"reg_req"}{"in"})) {
200     my @in = @{ $n{"reg_req"}{"in"} };
201
202     for (my $idx = 0; $idx <= $#in; $idx++) {
203       my $class = undef;
204
205       my $tmp2 = "const arch_register_req_t _".$op."_reg_req_in_$idx = ";
206
207       $tmp = "const arch_register_req_t *".$op."_reg_req_in_$idx = ";
208
209       push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_in_$idx;\n");
210
211       if ($in[$idx] eq "none") {
212         push(@inidx_class, "none");
213         $tmp .= "&ia32_default_req_none;\n";
214       }
215       elsif (is_reg_class($in[$idx])) {
216         push(@inidx_class, $in[$idx]);
217         $tmp .= "&ia32_default_req_".$arch."_".$in[$idx].";\n";
218       }
219           elsif ($in[$idx] =~ /^(!)?out_d(\d+)/) { # this is a "should be (un)equal to register at out_X"
220         $tmp  .= "&_".$op."_reg_req_in_$idx;\n";
221         $tmp2 .= " {\n";
222         $tmp2 .= "  arch_register_req_type_should_be_".($1 ? "different" : "same").",\n";
223         $tmp2 .= "  &$arch\_reg_classes[CLASS_$arch\_".$outidx_class[$2 - 1]."],\n";
224         $tmp2 .= "  NULL,\n  -$2\n};\n";
225
226         $tmp   = $tmp2.$tmp
227       }
228       else {
229         $class = build_subset_class_func($op, $idx, 1, $in[$idx]);
230         if (!defined $class) {
231           die("Could not build subset for IN requirements '$op' pos $idx ... exiting.\n");
232         }
233         push(@inidx_class, $class);
234         $tmp  .= "&_".$op."_reg_req_in_$idx;\n";
235         $tmp2 .= " {\n  arch_register_req_type_limited,\n  &$arch\_reg_classes[CLASS_$arch\_".$class."],\n  limit_reg_".$op."_in_".$idx.",\n  0\n};\n";
236
237         $tmp   = $tmp2.$tmp;
238       }
239
240       push(@obst_req, $tmp."\n");
241     }
242   }
243
244   push(@obst_req, "/* OUT requirements for '$op' */\n");
245
246   # check for result requirements
247   if (exists($n{"reg_req"}{"out"})) {
248     my @out = @{ $n{"reg_req"}{"out"} };
249
250     for (my $idx = 0; $idx <= $#out; $idx++) {
251       my $class = undef;
252
253       my $tmp2 = "const arch_register_req_t _".$op."_reg_req_out_$idx = ";
254
255       $tmp = "const arch_register_req_t *".$op."_reg_req_out_$idx = ";
256
257       push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_out_$idx;\n");
258
259       if ($out[$idx] eq "none") {
260         $tmp .= "&ia32_default_req_none;\n";
261       }
262       elsif (is_reg_class($out[$idx])) {
263         $tmp .= "&ia32_default_req_".$arch."_".$out[$idx].";\n";
264       }
265       elsif ($out[$idx] =~ /^(!)?in_s(\d+)/) { # this is a "should be (un)equal to register at in_X"
266         $tmp  .= "&_".$op."_reg_req_out_$idx;\n";
267         $tmp2 .= " {\n";
268         $tmp2 .= "  arch_register_req_type_should_be_".($1 ? "different" : "same").",\n";
269         $tmp2 .= "  &$arch\_reg_classes[CLASS_$arch\_".$inidx_class[$2 - 1]."],\n";
270         $tmp2 .= "  NULL,\n  ".($2 - 1)."\n};\n";
271
272         $tmp   = $tmp2.$tmp
273       }
274       else {
275         $class = build_subset_class_func($op, $idx, 0, $out[$idx]);
276         if (!defined $class) {
277           die("Could not build subset for OUT requirements '$op' pos $idx ... exiting.\n");
278         }
279         $tmp  .= "&_".$op."_reg_req_out_$idx;\n";
280         $tmp2 .= " {\n  arch_register_req_type_limited,\n  &$arch\_reg_classes[CLASS_$arch\_".$class."],\n  limit_reg_".$op."_out_".$idx.",\n  0\n};\n";
281
282         $tmp   = $tmp2.$tmp
283       }
284
285       push(@obst_req, $tmp."\n");
286     }
287   }
288 }
289
290
291
292 # generate header _t (internal usage) file
293 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
294
295 my $creation_time = localtime(time());
296
297 $tmp = uc($arch);
298
299 print OUT<<EOF;
300 #ifndef _GEN_$tmp\_REGALLOC_IF_T_H_
301 #define _GEN_$tmp\_REGALLOC_IF_T_H_
302
303 /**
304  * Generated register classes from spec.
305  *
306  * 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
312 #include "../bearch.h"
313
314 EOF
315
316 print OUT @obst_regdef, "\n";
317
318 print OUT @obst_classdef, "\n";
319
320 print OUT @obst_regtypes, "\n";
321
322 print OUT @obst_defreq_head, "\n";
323
324 print OUT "void ".$arch."_register_init(void);\n\n";
325
326 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
327
328
329
330 # generate header (external usage) file
331 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
332
333 $creation_time = localtime(time());
334
335 print OUT<<EOF;
336 #ifndef _GEN_$tmp\_REGALLOC_IF_H_
337 #define _GEN_$tmp\_REGALLOC_IF_H_
338
339 /**
340  * Contains additional external requirements defs for external includes.
341  *
342  * DO NOT EDIT THIS FILE, your changes will be lost.
343  * Edit $specfile instead.
344  * created by: $0 $specfile $target_dir
345  * date:       $creation_time
346  */
347
348 #include "gen_$arch\_regalloc_if_t.h"
349
350 EOF
351
352 print OUT @obst_header_all;
353
354 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
355
356 close(OUT);
357
358
359
360 # generate c inline file
361 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
362
363 $creation_time = localtime(time());
364
365 print OUT<<EOF;
366 /**
367  * The generated interface for the register allocator.
368  * Contains register classes and types and register constraints
369  * for all nodes where constraints were given in spec.
370  *
371  * DO NOT EDIT THIS FILE, your changes will be lost.
372  * Edit $specfile instead.
373  * created by: $0 $specfile $target_dir
374  * date:       $creation_time
375  */
376
377 #include "gen_$arch\_regalloc_if_t.h"
378
379 EOF
380
381 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n  ".join(",\n  ", @obst_regclasses)."\n};\n\n";
382
383 print OUT "void ".$arch."_register_init(void) {\n";
384 print OUT @obst_reginit;
385 print OUT "}\n\n";
386
387 print OUT @obst_limit_func;
388 print OUT @obst_req;
389
390 close(OUT);
391
392
393
394 ###
395 # Determines whether $name is a specified register class or not.
396 # @return 1 if name is register class, 0 otherwise
397 ###
398 sub is_reg_class {
399   my $name = shift;
400   return 1 if exists($reg_classes{"$name"});
401   return 0;
402 }
403
404 ###
405 # Returns the register class for a given register.
406 # @return class or undef
407 ###
408 sub get_reg_class {
409   my $reg = shift;
410   return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
411   return undef;
412 }
413
414 ###
415 # Returns the index of a given register within it's register class.
416 # @return index or undef
417 ###
418 sub get_reg_index {
419   my $reg = shift;
420   return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
421   return undef;
422 }
423
424 ###
425 # Generates the function for a given $op and a given IN-index
426 # which returns a subset of possible register from a register class
427 # @return classname from which the subset is derived or undef
428 ###
429 sub build_subset_class_func {
430   my $neg   = undef;
431   my $class = "";
432   my $temp;
433
434   # build function header
435   my $op  = shift;
436   my $idx = shift;
437   my $in  = shift;
438   push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
439   push(@obst_limit_func, "int limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(const ir_node *irn, int pos, bitset_t *bs) {\n");
440
441   my @regs = split(/ /, shift);
442
443   # set/unset registers
444   foreach (@regs) {
445     # check for negate
446
447     if (substr($_, 0, 1) eq "!") {
448       if (defined($neg) && $neg == 0) {
449         # we have seen a positiv constraint as first one but this one is negative
450         # this doesn't make sense
451         print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
452         return undef;
453       }
454
455       if (!defined($neg)) {
456         push(@obst_limit_func, "  bs = bitset_set_all(bs);     /* allow all register (negative constraints given) */\n");
457       }
458
459       $_   = substr($_, 1); # skip '!'
460       $neg = 1;
461     }
462     else {
463       if (defined($neg) && $neg == 1) {
464         # we have seen a negative constraint as first one but this one is positive
465         # this doesn't make sense
466         print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
467         return undef;
468       }
469
470       if (!defined($neg)) {
471         push(@obst_limit_func, "  bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */\n");
472       }
473       $neg = 0;
474     }
475
476     # check if register belongs to one of the given classes
477     $temp = get_reg_class($_);
478     if (!defined($temp)) {
479       print STDERR "Unknown register '$_'!\n";
480       return undef;
481     }
482
483     # set class
484     if (!$class) {
485       $class = $temp;
486     }
487     elsif ($class ne $temp) {
488       # all registers must belong to the same class
489       print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
490       return undef;
491     }
492
493     if ($neg == 1) {
494       push(@obst_limit_func, "  bitset_clear(bs, ".get_reg_index($_).");         /* disallow $_ */\n");
495     }
496     else {
497       push(@obst_limit_func, "  bitset_set(bs, ".get_reg_index($_).");           /* allow $_ */\n");
498     }
499   }
500
501   push(@obst_limit_func, "\n  return ".($neg ? scalar(@{ $reg_classes{"$class"} }) - scalar(@regs) : scalar(@regs)).";\n");
502   push(@obst_limit_func, "}\n\n");
503
504   return $class;
505 }