Skip to content

Commit

Permalink
Read expr tree IR ops order
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw committed Jul 28, 2015
1 parent 4a4cd3c commit 4c14a58
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions tools/tiler-table-generator.pl
Expand Up @@ -13,8 +13,26 @@
# indirectly create rulenr -> terminal
# table contains head -> rulenr

my @rules;
# Read the expression tree node types in correct order
my @expr_ops;
{
open my $expr_h, '<', 'src/jit/expr.h' or die "Could not open expression definition file";
while (<$expr_h>) {
last if m/^#define MVM_JIT_IR_OPS\(_\) \\/;
}
while(!eof($expr_h)) {
my $line = <$expr_h>;
chomp $line;
last unless $line =~ m/\\$/;
next unless $line =~ m/_\((\w+), \d+, \d+, \w+\)/;
my $op = lc(substr($line, $-[1], $+[1]-$-[1]));
push @expr_ops, $op;
}
close $expr_h;
}

# Collect rules from the grammar
my @rules;
sub add_rule {
my ($fragment, $terminal, $cost, $depth) = @_;
my $list = [];
Expand Down Expand Up @@ -63,6 +81,30 @@ sub add_rule {
}


#
# Generate rulesets conservatively.
#
# The code generator tries to map tree elements to generation rules.
# In many cases, a given tree element can be mapped to many rules at
# the same time (the ruleset). Such rulesets represent the states in
# the matching DFA. State transitions are effected by means of a
# three-dimensional table of [ head x ruleset x ruleset ], which is
# why we want to keep the number of rulesets as small as possible.
#
# The smallest number possible is of course only those rulesets that
# can be generated by the DFA. Thus, we first need to generate the
# DFA table to find the rulesets required for the DFA table. To solve
# this circular problem, observe that the rulesets generated by a head
# can only depend on the terminals that the children of the head refer
# to. And that it cannot matter *which* ruleset generates these terminals,
# just as long as *all* terminals are generated by a ruleset. Hence,
# we can use topological sort to find the 'generatable' subset of heads,
# and use these to generate the rulesets by head.
#
# I think this can be optimized further by selecting only a single ruleset
# per terminal candidate ruleset.
#

my @order;
my %candidates;
my @rulesets;
Expand Down Expand Up @@ -158,15 +200,15 @@ sub min_cost {
my $lc1 = min_cost($rs1, $c1);
for my $rs2 (@$cand2) {
my $lc2 = min_cost($rs2, $c2);
$table{$head}{$rs1}{$rs2} = [$rule_nr, $lc1, $lc2] if $term eq 'reg';
$table{$head}{$rs1}{$rs2} = [$rule_nr, $lc1, $lc2] if ($term eq 'reg' || $term eq 'void');
$trans{$head,$rs1,$rs2}->{$rule_nr} = 1;
}
}
} else {
# unary
for my $rs1 (@$cand1) {
my $lc1 = min_cost($rs1, $c1);
$table{$head}{$rs1} = [$rule_nr, $lc1] if $term eq 'reg';
$table{$head}{$rs1} = [$rule_nr, $lc1] if ($term eq 'reg' || $term eq 'void');
$trans{$head,$rs1}->{$rule_nr} = 1;
}
}
Expand Down

0 comments on commit 4c14a58

Please sign in to comment.