Skip to content

Commit

Permalink
Pull to_cgop out into a pass
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jul 26, 2010
1 parent 726e4a4 commit 1892a69
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
40 changes: 18 additions & 22 deletions Body.pm
Expand Up @@ -9,14 +9,13 @@ use CgOp ();
use Moose;

has name => (isa => 'Str', is => 'rw', default => "anon");
has uid => (isa => 'Int', is => 'ro', default => sub { ++(state $i) });
has do => (isa => 'Op', is => 'rw');
has outer => (isa => 'Body', is => 'rw', init_arg => undef);
has setting => (is => 'rw');
has code => (isa => 'CodeGen', is => 'ro', init_arg => undef,
lazy => 1, builder => 'gen_code');
has signature => (isa => 'Maybe[Sig]', is => 'ro');
has mainline => (isa => 'Bool', is => 'ro', lazy => 1,
builder => 'is_mainline');
builder => 'is_mainline');
# currently used types are phaser, loop, cond, class, mainline, bare, sub
# also '' for incorrectly contextualized {p,x,}block, blast
has type => (isa => 'Str', is => 'rw');
Expand All @@ -25,6 +24,7 @@ use CgOp ();
# my $x inside, floats out; mostly for blasts; set by context so must be rw
has transparent => (isa => 'Bool', is => 'rw', default => 0);
has decls => (isa => 'ArrayRef[Decl]', is => 'rw');
has cgoptree => (isa => 'CgOp', is => 'rw');

sub is_mainline {
my $self = shift;
Expand Down Expand Up @@ -56,7 +56,6 @@ use CgOp ();
if $self->type eq 'mainline';
unshift @x, Decl::PackageLink->new(name => '$?CURPKG')
if $self->type =~ /mainline|class|package|grammar|module|role|slang|knowhow/;
#print STDERR YAML::XS::Dump(\@x);
push @y, map { $_->outer_decls } @x
if $self->type ne 'mainline';
$self->decls(\@x);
Expand All @@ -65,34 +64,24 @@ use CgOp ();
@y;
}

sub gen_code {
sub to_cgop {
my ($self) = @_;
my @enter;
push @enter, map { $_->enter_code($self) } @{ $self->decls };
push @enter, $self->signature->binder if $self->signature;
# TODO: Bind a return value here to catch non-ro sub use
CodeGen->new(name => $self->name, body => $self,
lex2type => +{ %{ $self->lexical } },
ops => CgOp::prog($self->enter_code,
$self->cgoptree(CgOp::prog(@enter,
CgOp::return($self->do->code($self))));
}

sub enter_code {
my ($self) = @_;
my @p;
push @p, map { $_->enter_code($self) } @{ $self->decls };
push @p, $self->signature->binder if $self->signature;
CgOp::prog(@p);
map { $_->preinit_code($self) } @{ $self->decls };
}

sub write {
my ($self) = @_;
$self->code->write;
CodeGen->new(lex2types => $self->lexical, csname => $self->csname,
body => $self, ops => $self->cgoptree)->write;
$_->write($self) for (@{ $self->decls });
}

sub preinit_code {
my ($self) = @_;
CgOp::prog(map { $_->preinit_code($self) } @{ $self->decls });
}

sub lex_level {
my ($self, $var) = @_;

Expand Down Expand Up @@ -120,6 +109,13 @@ use CgOp ();
\%h;
}

sub csname {
my ($self) = @_;
my @name = split /\W+/, $self->name;
shift @name if @name && $name[0] eq '';
join("", (map { ucfirst $_ } @name), "_", $self->uid, "C");
}

# In order to support proper COMMON semantics on package variables
# we have only one operation here - autovivifying lookup.
#
Expand Down
5 changes: 3 additions & 2 deletions CgOp.pm
Expand Up @@ -523,12 +523,13 @@ use warnings;
zyg => \@args);
}

# must only be called during to_cgop phase!
sub protosub {
my ($body, @extra) = @_;
prog(
CgOp::Primitive->new(op => [ 'open_protopad', $body ]),
$body->preinit_code,
CgOp::Primitive->new(op => [ 'close_sub', $body->code ]));
$body->to_cgop,
CgOp::Primitive->new(op => [ 'close_sub', $body ]));
}

sub letn {
Expand Down
15 changes: 3 additions & 12 deletions CodeGen.pm
Expand Up @@ -105,8 +105,7 @@ use 5.010;
}
}

has name => (isa => 'Str', is => 'ro');
has uid => (isa => 'Int', is => 'ro', default => sub { ++(state $i) });
has csname => (isa => 'Str', is => 'ro');
has entry => (isa => 'Bool', is => 'ro', default => 0);
has depth => (isa => 'Int', is => 'rw', default => 0);
has maxdepth => (isa => 'Int', is => 'rw', default => 0);
Expand Down Expand Up @@ -499,13 +498,13 @@ use 5.010;
}

sub close_sub {
my ($self, $bodycg) = @_;
my ($self, $body) = @_;
$self->pop_let('protopad');
pop @{ $self->bodies };
$self->peek_let('protopad');
my ($pp, $op) = $self->_popn(2);
$self->_push('IP6', "Kernel.MakeSub(new DynBlockDelegate(" .
$bodycg->csname . "), $pp, $op)");
$body->csname . "), $pp, $op)");
}

sub proto_var {
Expand Down Expand Up @@ -546,14 +545,6 @@ use 5.010;

###

sub csname {
my ($self) = @_;
return $self->name if $self->entry;
my @name = split /\W+/, $self->name;
shift @name if @name && $name[0] eq '';
join("", (map { ucfirst $_ } @name), "_", $self->uid, "C");
}

sub write {
my ($self) = @_;
my $name = $self->csname;
Expand Down
1 change: 1 addition & 0 deletions CompilerDriver.pm
Expand Up @@ -56,6 +56,7 @@ sub compile {
$::SETTING_RESUME = undef;

$ast->lift_decls;
$ast->to_cgop;

my $basename = $::UNITNAME;
$basename =~ s/::/\//g;
Expand Down
11 changes: 7 additions & 4 deletions Unit.pm
Expand Up @@ -11,9 +11,8 @@ use 5.010;
package Unit;
use Moose;
has mainline => (isa => 'Body', is => 'ro', required => 1);
has mainboot => (isa => 'CgOp', is => 'rw');
has name => (isa => 'Str', is => 'ro', required => 1);
has code => (isa => 'CodeGen', is => 'ro', init_arg => undef, lazy => 1,
builder => 'gen_code');
has setting => (is => 'ro');

has is_setting => (isa => 'Bool', is => 'ro');
Expand All @@ -23,12 +22,16 @@ use 5.010;
$_[0]->mainline->lift_decls;
}

sub to_cgop {
$_[0]->mainboot($_[0]->mainline->to_cgop);
}

sub gen_code {
my ($self) = @_;
$self->mainline->setting($self->setting) if $self->setting;
CodeGen->know_module($self->csname($self->setting_name));
CodeGen->know_module($self->csname);
CodeGen->new(name => 'BOOT', entry => 1,
CodeGen->new(csname => 'BOOT',
ops => CgOp::letn('pkg', CgOp::rawsget('Kernel.Global'),
CgOp::rawscall($self->csname($self->setting_name) . '.Initialize'),
CgOp::letn('protopad',
Expand All @@ -55,7 +58,7 @@ use 5.010;
print ::NIECZA_OUT <<EOH;
public class @{[ $self->csname ]} {
EOH
$self->code->write;
$self->gen_code->write;
$self->mainline->write;
if ($self->is_setting) {
print ::NIECZA_OUT <<EOSB ;
Expand Down

0 comments on commit 1892a69

Please sign in to comment.