Skip to content

Commit

Permalink
Expose the preinit/run split to the entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan O'Rear committed Jul 11, 2010
1 parent 32d2ad5 commit 8605d1d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
21 changes: 12 additions & 9 deletions CodeGen.pm
Expand Up @@ -45,6 +45,7 @@ use 5.010;

has name => (isa => 'Str', is => 'ro');
has uid => (isa => 'Int', is => 'ro', default => sub { ++(state $i) });
has entry => (isa => 'Bool', is => 'ro', default => 0);
has depth => (isa => 'Int', is => 'rw', default => 0);
has maxdepth => (isa => 'Int', is => 'rw', default => 0);
has savedepth => (isa => 'Int', is => 'rw', default => 0);
Expand Down Expand Up @@ -503,6 +504,7 @@ 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");
Expand All @@ -511,19 +513,20 @@ use 5.010;
sub write {
my ($self) = @_;
my $name = $self->csname;
print " " x 8, "private static Frame $name(Frame th) {\n";
print " " x 12, "Console.WriteLine(\"Entering $name @ \" + th.ip);\n";
my $vis = ($self->entry ? 'public' : 'private');
print " " x 4, "$vis static Frame $name(Frame th) {\n";
print " " x 8, "Console.WriteLine(\"Entering $name @ \" + th.ip);\n";
if ($self->maxdepth) {
print " " x 12, "object " . join(", ", map { "s$_" }
print " " x 8, "object " . join(", ", map { "s$_" }
0 .. ($self->maxdepth - 1)) . ";\n";
}
print " " x 12, "switch (th.ip) {\n";
print " " x 16, "case 0:\n";
print " " x 16, $_ for @{ $self->buffer };
print " " x 16, "default:\n";
print " " x 20, "throw new Exception(\"Invalid IP\");\n";
print " " x 12, "}\n";
print " " x 8, "switch (th.ip) {\n";
print " " x 12, "case 0:\n";
print " " x 12, $_ for @{ $self->buffer };
print " " x 12, "default:\n";
print " " x 16, "throw new Exception(\"Invalid IP\");\n";
print " " x 8, "}\n";
print " " x 4, "}\n";
}

__PACKAGE__->meta->make_immutable;
Expand Down
36 changes: 35 additions & 1 deletion CompilerDriver.pm
Expand Up @@ -10,4 +10,38 @@ use Op ();
use Niecza::Grammar ();
use Niecza::Actions ();

Niecza::Grammar->parsefile("setting", setting => 'NULL', actions => 'Niecza::Actions')->{_ast}->write;
{
local $::UNITNAME = 'Mainline';
Niecza::Grammar->parsefile("setting", setting => 'NULL', actions => 'Niecza::Actions')->{_ast}->write;
}

print <<EOF;
public class EntryPoint {
public static Frame START(Frame th) {
Frame t;
switch (th.ip) {
case 0:
t = new Frame(th, th, new DynBlockDelegate(Mainline.BOOT));
t.pos = new LValue[1] { Kernel.NewROLValue(th) };
th.ip = 1;
return t;
case 1:
th.ip = 2;
return ((IP6)th.resultSlot).Invoke(th, new LValue[0] {}, null);
case 2:
return null;
default:
throw new Exception("IP corruption");
}
}
public static void Main() {
Frame root_f = new Frame(null, null,
new DynBlockDelegate(START));
Frame current = root_f;
while (current != null) {
current = current.Continue();
}
}
}
EOF
2 changes: 1 addition & 1 deletion Niecza/Actions.pm
Expand Up @@ -778,7 +778,7 @@ sub comp_unit { my ($cl, $M) = @_;
my $body = $cl->sl_to_block($M->{statementlist}{_ast},
subname => 'mainline');

$M->{_ast} = Unit->new(mainline => $body);
$M->{_ast} = Unit->new(mainline => $body, name => $::UNITNAME);
}

1;
32 changes: 15 additions & 17 deletions Unit.pm
Expand Up @@ -2,29 +2,35 @@ use strict;
use warnings;
use 5.010;

# A Unit generates a CLR class with a BOOT member
# All used Units except for the setting go into Niecza.Kernel.Units
# The main program generates a main class, which sets up Units and runs the
# setting
# BOOT subs take one argument, the outer protopad
{
package Unit;
use Moose;
has mainline => (isa => 'Body', is => 'ro', required => 1);

has codegen => (isa => 'CodeGen', is => 'rw');
has name => (isa => 'Str', is => 'ro', required => 1);
has codegen => (isa => 'CodeGen', is => 'rw');

sub code {
my ($self) = @_;
my $cg = $self->codegen;
if ($cg) {
return $cg;
} else {
$self->codegen($cg = CodeGen->new(name => 'boot'));
$self->codegen($cg = CodeGen->new(name => 'BOOT', entry => 1));
$cg->new_aux('protopad', 'Frame');
$cg->new_aux('how', 'Variable');
$cg->push_null('Frame');
$cg->pos(0);
$cg->fetch;
$cg->cast('Frame');
$cg->push_aux('protopad');
$cg->open_protopad;
$self->mainline->do_preinit($cg);
$cg->close_sub($self->mainline->code);
$cg->call_sub(0,0);
$cg->return;
$cg->return(1);
return $cg;
}
}
Expand All @@ -35,20 +41,12 @@ use 5.010;
print <<EOH;
using System;
using System.Collections.Generic;
namespace Niecza {
public class MainClass {
public static void Main() {
Frame root_f = new Frame(null, null,
new DynBlockDelegate(@{[ $self->code->csname ]}));
Frame current = root_f;
while (current != null) {
current = current.Continue();
}
}
using Niecza;
public class @{[ $self->name ]} {
EOH
$self->code->write;
$self->mainline->write;
print " }\n}\n"
print "}\n"
}

__PACKAGE__->meta->make_immutable;
Expand Down

0 comments on commit 8605d1d

Please sign in to comment.