Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' of github.com:jnthn/6model
  • Loading branch information
diakopter committed Nov 21, 2010
2 parents 33cf495 + a7baae0 commit 0b3a6ad
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
56 changes: 55 additions & 1 deletion dotnet/compiler/NQPOptimizer.pm
Expand Up @@ -33,6 +33,11 @@ our multi optimizer(PAST::Block $block) {
$block.control('');
}

# If there's no inner blocks, we can demote lexicals to constants.
unless get_annotation($block, 'has_nested_blocks') {
demote_lexicals_to_locals($block);
}

# Remove this block from the block stack.
@*BLOCK_STACK.shift();
}
Expand Down Expand Up @@ -63,10 +68,13 @@ our multi optimizer(PAST::Op $op) {
}

our multi optimizer(PAST::Var $var) {
# May need to visit the viviself.
# May need to visit the viviself as well as children.
if $var.viviself ~~ PAST::Node {
optimizer($var.viviself);
}
for @($var) {
optimizer($_);
}
}

our multi optimizer(PAST::Val $stmts) {
Expand All @@ -91,3 +99,49 @@ sub annotate($node, $key, $value) {
sub get_annotation($node, $key) {
$node{'nqp_opt_' ~ $key}
}

# Demotes lexicals to local variables.
sub demote_lexicals_to_locals($block) {
my %*demoted;
my $*counter := 0;
for @($block) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker(PAST::Stmts $stmts) {
for @($stmts) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker(PAST::Var $var) {
if %*demoted{$var.name} {
$var.scope('register');
$var.name(%*demoted{$var.name});
}
elsif $var.isdecl && $var.scope eq 'lexical' {
# Make sure it's not a context var.
my $twigil := pir::substr($var.name, 1, 1);
if $twigil ne '*' && $twigil ne '?' {
# Good demotion candidate.
my $new_name := 'demoted_lexical_' ~ $*counter;
$*counter := $*counter + 1;
%*demoted{$var.name} := $new_name;
$var.name($new_name);
$var.scope('register');
}
}
if $var.viviself {
demote_lexicals_worker($var.viviself);
}
for @($var) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker(PAST::Op $op) {
for @($op) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker($any) {
# Nothing to do for this node type
}
3 changes: 3 additions & 0 deletions dotnet/compiler/PAST2DNSTCompiler.pm
Expand Up @@ -1046,6 +1046,9 @@ our multi sub dnst_for(PAST::Var $var) {
if $*BIND_CONTEXT {
$result.push($*BIND_VALUE);
}
elsif $var.viviself {
$result.push(dnst_for($var.viviself));
}
else {
$result.push('null');
}
Expand Down

0 comments on commit 0b3a6ad

Please sign in to comment.