X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fscripts%2Fgenerate_regalloc_if.pl;h=a4488347bbc63a1f89c51629a952d381e6b9f727;hb=4f6cfa6408330763342990125d6b3d729b874ffb;hp=67e8907a94effb8c7877c85ff43e2b8bbc9d353c;hpb=983cfe92d98c7227dca6e39e3c74a261ab9a9e19;p=libfirm diff --git a/ir/be/scripts/generate_regalloc_if.pl b/ir/be/scripts/generate_regalloc_if.pl index 67e8907a9..a4488347b 100755 --- a/ir/be/scripts/generate_regalloc_if.pl +++ b/ir/be/scripts/generate_regalloc_if.pl @@ -1,460 +1,297 @@ #!/usr/bin/perl -w -# This script generates C code which emits assembler code for the -# assembler ir nodes. It takes a "emit" key from the node specification -# and substitutes lines starting with . with a corresponding fprintf(). +# +# Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. +# +# This file is part of libFirm. +# +# This file may be distributed and/or modified under the terms of the +# GNU General Public License version 2 as published by the Free Software +# Foundation and appearing in the file LICENSE.GPL included in the +# packaging of this file. +# +# Licensees holding valid libFirm Professional Edition licenses may use +# this file in accordance with the libFirm Commercial License. +# Agreement provided with the Software. +# +# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE. +# + +# This script generates C code which creates ands sets up functions and +# data structures for the register allocator. # Creation: 2005/11/14 -# $Id$ use strict; use Data::Dumper; +use integer; my $specfile = $ARGV[0]; my $target_dir = $ARGV[1]; our $arch; our %reg_classes; -our %nodes; # include spec file my $return; -no strict "subs"; +use strict "subs"; unless ($return = do $specfile) { - warn "couldn't parse $specfile: $@" if $@; - warn "couldn't do $specfile: $!" unless defined $return; - warn "couldn't run $specfile" unless $return; + die "Fatal error: couldn't parse $specfile: $@" if $@; + die "Fatal error: couldn't do $specfile: $!" unless defined $return; + die "Fatal error: couldn't run $specfile" unless $return; } use strict "subs"; my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c"; my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h"; -my $target_h_t = $target_dir."/gen_".$arch."_regalloc_if_t.h"; # helper function -my @rt = ("arch_register_type_none", - "arch_register_type_write_invariant", - "arch_register_type_caller_saved", - "arch_register_type_callee_saved", - "arch_register_type_ignore"); +sub map_flags { + my $prefix = shift; + my $flags = shift || "none"; + return join(" | ", map { "$prefix$_" } split(/\s*\|\s*/, $flags)); +} # stacks for output -my @obst_regtypes; # stack for the register type variables -my @obst_regclasses; # stack for the register class variables -my @obst_classdef; # stack to define a name for a class index -my @obst_regdef; # stack to define a name for a register index -my @obst_reginit; # stack for the register type inits -my @obst_req; # stack for the register requirements -my @obst_limit_func; # stack for functions to return a subset of a register class -my @obst_defreq_head; # stack for prototypes of default requirement function -my @obst_header_all; # stack for some extern struct defs needed for bearch_$arch include - -my $numregs; +my $regtypes_def; # stack for the register type variables definitions +my @regclasses; # stack for the register class variables +my $classdef; # stack to define a name for a class index +my $regdef; # stack to define a name for a register index +my $regdef2; +my $regcounts; +my $reginit; # stack for the register type inits +my $single_constraints; + my $class_ptr; my $class_idx = 0; -my $tmp; - -my %reg2class; - -# there is a default NONE requirement -$tmp = "/* Default NONE register requirements */\n"; -$tmp .= "const arch_register_req_t ia32_default_req_none = {\n"; -$tmp .= " arch_register_req_type_none,\n"; -$tmp .= " NULL,\n"; -$tmp .= " { NULL }\n"; -$tmp .= "};\n\n"; -push(@obst_req, $tmp); +my %regclass2len = (); +my %reg2class = (); -push(@obst_header_all, "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n"); -push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_none;\n"); +$classdef .= "enum reg_classes {\n"; -push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n"); - -# generate register type and class variable, init function and default requirements foreach my $class_name (keys(%reg_classes)) { - my @class = @{ $reg_classes{"$class_name"} }; - my $old_classname = $class_name; - - $class_name = $arch."_".$class_name; - $numregs = "N_".$class_name."_REGS"; - $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]"; - - push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n"); - push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n"); - - push(@obst_classdef, "#define CLASS_$class_name $class_idx\n"); - push(@obst_regclasses, "{ \"$class_name\", $numregs, ".$class_name."_regs }"); - - # there is a default NORMAL requirement for each class - $tmp = "/* Default NORMAL register requirements for class $class_name */\n"; - $tmp .= "const arch_register_req_t ia32_default_req_$class_name = {\n"; - $tmp .= " arch_register_req_type_normal,\n"; - $tmp .= " $class_ptr,\n"; - $tmp .= " { NULL }\n"; - $tmp .= "};\n\n"; - push(@obst_req, $tmp); - - push(@obst_header_all, "\nextern const arch_register_req_t ia32_default_req_$class_name;\n"); - - my $idx = 0; - push(@obst_reginit, " /* Init of all registers in class '$class_name' */\n\n"); - foreach (@class) { - # For each class we build for each of it's member registers a limit function - # which limits the class to this particular register. We also build the - # corresponding requirement structs. - # We need those functions to set register requirements on demand in transformation - # esp. for Call and RegParams where we can mix int and float parameters. - - my $limit_func_name = $arch."_limit_".$class_name."_".$_->{"name"}; - - # push the function prototype - $tmp = "int $limit_func_name(const ir_node *irn, int pos, bitset_t *bs)"; - push(@obst_defreq_head, $tmp.";\n"); - - # push the function definition - $tmp .= " {\n"; - $tmp .= " bs = bitset_clear_all(bs);\n"; - $tmp .= " bitset_set(bs, REG_".uc($_->{"name"}).");\n"; # REGISTER to index assignment is done some lines down - $tmp .= " return 1;\n"; - $tmp .= "}\n\n"; - push(@obst_limit_func, $tmp); - - # push the default requirement struct - $tmp = "const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}." = {\n"; - $tmp .= " arch_register_req_type_limited,\n"; - $tmp .= " $class_ptr,\n"; - $tmp .= " { $limit_func_name }\n"; - $tmp .= "};\n\n"; - push(@obst_req, $tmp); - - push(@obst_header_all, "extern const arch_register_req_t ia32_default_req_$class_name\_".$_->{"name"}.";\n"); - - $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use - push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n"); - push(@obst_reginit, " ".$class_name."_regs[$idx].name = \"".$_->{"name"}."\";\n"); - push(@obst_reginit, " ".$class_name."_regs[$idx].reg_class = $class_ptr;\n"); - push(@obst_reginit, " ".$class_name."_regs[$idx].index = $idx;\n"); - push(@obst_reginit, " ".$class_name."_regs[$idx].type = ".$rt[$_->{"type"}].";\n\n"); - $idx++; - } - - $class_idx++; + my @class = @{ $reg_classes{"$class_name"} }; + + my $idx = 0; + foreach (@class) { + if (defined($_->{name})) { + $reg2class{$_->{name}} = { + "class" => $class_name, + "index" => $idx + }; + } + $idx++; + } + $regclass2len{$class_name} = $idx; } -push(@obst_header_all, "\n/* node specific requirements */\n"); - -# generate node-register constraints -foreach my $op (keys(%nodes)) { - my %n = %{ $nodes{"$op"} }; - - next if (!exists($n{"reg_req"})); - - $op = $arch."_".$op; - - push(@obst_req, "/* IN requirements for '$op' */\n"); - - # we need to remember the classes of the IN constraints for - # OUT constraints like "in_s1" - my @inidx_class; - - # check for argument requirements - if (exists($n{"reg_req"}{"in"})) { - my @in = @{ $n{"reg_req"}{"in"} }; - - for (my $idx = 0; $idx <= $#in; $idx++) { - my $class = undef; - - my $tmp2 = "const arch_register_req_t _".$op."_reg_req_in_$idx = "; - - $tmp = "const arch_register_req_t *".$op."_reg_req_in_$idx = "; - - push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_in_$idx;\n"); - - if ($in[$idx] eq "none") { - push(@inidx_class, "none"); - $tmp .= "&ia32_default_req_none;\n"; - } - elsif (is_reg_class($in[$idx])) { - push(@inidx_class, $in[$idx]); - $tmp .= "&ia32_default_req_".$arch."_".$in[$idx].";\n"; - } - else { - $class = build_subset_class_func($op, $idx, 1, $in[$idx]); - if (!defined $class) { - die("Could not build subset for IN requirements '$op' pos $idx ... exiting.\n"); - } - push(@inidx_class, $class); - $tmp .= "&_".$op."_reg_req_in_$idx;\n"; - $tmp2 .= " {\n arch_register_req_type_limited,\n &$arch\_reg_classes[CLASS_$arch\_".$class."],\n { limit_reg_".$op."_in_".$idx." }\n};\n"; - - $tmp = $tmp2.$tmp; - } - - push(@obst_req, $tmp."\n"); - } - } - - push(@obst_req, "/* OUT requirements for '$op' */\n"); - - # check for result requirements - if (exists($n{"reg_req"}{"out"})) { - my @out = @{ $n{"reg_req"}{"out"} }; - - for (my $idx = 0; $idx <= $#out; $idx++) { - my $class = undef; - - my $tmp2 = "const arch_register_req_t _".$op."_reg_req_out_$idx = "; - - $tmp = "const arch_register_req_t *".$op."_reg_req_out_$idx = "; +sub get_limited_array { + my $reg = shift; + my $regclass = $reg2class{"$reg"}{"class"}; + my $ucname = uc($reg); + my $result = "{ "; + + my $limitedbitsetlen = $regclass2len{$regclass}; + my $arraylen = ($limitedbitsetlen+31) / 32; + my $firstreg = uc($reg_classes{$regclass}[0]->{"name"}); + my $classuc = uc($regclass); + my $first = 1; + for (my $i = 0; $i < $arraylen; ++$i) { + if ($first) { + $first = 0; + } else { + $result .= ", "; + } + + my $index = $reg2class{"$reg"}{"index"}; + if ($index >= $i*32 && $index < ($i+1)*32) { + if ($i > 0) { + $result .= "(1 << (REG_${classuc}_${ucname} % 32))"; + } else { + $result .= "(1 << REG_${classuc}_${ucname})"; + } + } else { + $result .= "0"; + } + } + $result .= " }"; +} - push(@obst_header_all, "extern const arch_register_req_t *".$op."_reg_req_out_$idx;\n"); +# generate register type and class variable, init function and default requirements +foreach my $class_name (keys(%reg_classes)) { + my @class = @{ $reg_classes{"$class_name"} }; + my $old_classname = $class_name; + + $class_name = $arch."_".$class_name; + $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]"; + my $flags = pop(@class); + my $class_mode = $flags->{"mode"}; + my $flags_prepared = map_flags("arch_register_class_flag_", $flags->{"flags"}); + + $single_constraints .= <{"name"}) . "]"; + push(@regclasses, "{ $class_idx, \"$class_name\", $numregs, NULL, $first_reg, $flags_prepared, &${arch}_class_reg_req_${old_classname} }"); + + my $idx = 0; + $reginit .= "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n"; + my $lastreg; + foreach (@class) { + my $name = $_->{"name"}; + my $ucname = uc($name); + my $type = map_flags("arch_register_type_", $_->{"type"}); + # realname is name if not set by user + $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"})); + my $realname = $_->{realname}; + my $classuc = uc($old_classname); + + $regdef .= "\tREG_${ucname},\n"; + $regdef2 .= "\tREG_${classuc}_${ucname} = $idx,\n"; + my $dwarf_number = 0; + if (defined($_->{dwarf})) { + $dwarf_number = $_->{dwarf}; + } + + $regtypes_def .= <$target_h_t") || die("Could not open $target_h_t, reason: $!\n"); +# generate header (external usage) file +open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n"); my $creation_time = localtime(time()); -$tmp = uc($arch); - print OUT<$target_h") || die("Could not open $target_h, reason: $!\n"); +# generate c file +open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n"); $creation_time = localtime(time()); print OUT<$target_c") || die("Could not open $target_c, reason: $!\n"); +print OUT<