Skip to content

Commit

Permalink
Refactor initialization procedure
Browse files Browse the repository at this point in the history
This lays the groundwork for modules and custom settings, and brings our
handling of CORE.setting much closer to STD.
  • Loading branch information
sorear committed Jul 23, 2010
1 parent dc56884 commit a7a25a2
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 103 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Expand Up @@ -2,10 +2,10 @@ Program.exe
Program.cs
Kernel.dll
Kernel.dll.so
Setting.dll
Setting.dll.so
Setting.cs
setting_ast.store
CORE.dll
CORE.dll.so
CORE.cs
CORE_ast.store
*.swp
*.pmc
syml
Expand Down
6 changes: 2 additions & 4 deletions setting → CORE.setting
@@ -1,8 +1,6 @@
# vim: ft=perl6

# XXX fakes out STD.pm6 into doing more or less the right thing
# need to understand this better
my class CORE { ... }
my module CORE;

# We have to do this directly due to the circularity saw. Same
# reason forces uncontainerized .NET values.
Expand Down Expand Up @@ -275,7 +273,7 @@ sub say($str) { Q:CgOp {
} }
sub exit() { Q:CgOp {
(prog [rawscall Environment.Exit (int 0) ]
(prog [rawscall System.Environment.Exit (int 0) ]
[null Variable]
)
} }
Expand Down
18 changes: 16 additions & 2 deletions CodeGen.pm
Expand Up @@ -46,6 +46,7 @@ use 5.010;
'Frame' =>
{ pos => [f => 'LValue[]'] },

'Kernel.Global' => [f => 'Variable'],
'Kernel.PackageLookup' => [c => 'Variable'],
'Kernel.SlurpyHelper' => [c => 'List<Variable>'],
'Kernel.Bind' => [c => 'Void'],
Expand All @@ -56,7 +57,7 @@ use 5.010;
'Kernel.NewRWListVar' => [m => 'Variable'],
'Console.WriteLine' => [m => 'Void'],
'Console.Error.WriteLine' => [m => 'Void'],
'Environment.Exit' => [m => 'Void'],
'System.Environment.Exit' => [m => 'Void'],
'String.Concat' => [m => 'String'],
'Kernel.AnyP' => [f => 'IP6'],
'Kernel.SubMO' => [f => 'DynMetaObject'],
Expand All @@ -67,6 +68,19 @@ use 5.010;
'Kernel.UnboxAny' => [m => 'object'],
);
sub know_module {
my ($class, $mname) = @_;
# for settings
$typedata{ $mname . '.Environment' } = [ f => 'Frame' ];
$typedata{ $mname . '.Installer' } = [ f => 'IP6' ];
# for importable modules
$typedata{ $mname . '.Type' } = [ f => 'Variable' ];
$typedata{ $mname . '.Stash' } = [ f => 'Variable' ];
# for all
$typedata{ $mname . '.Initialize' } = [ m => 'Void' ];
}
__PACKAGE__->know_module('NULL');
sub _typedata {
my ($self, $types, @path) = @_;
my $cursor = \%typedata;
Expand Down Expand Up @@ -570,7 +584,7 @@ use 5.010;
my $name = $self->csname;
my $vis = ($self->entry ? 'public' : 'private');
print ::NIECZA_OUT " " x 4, "$vis static Frame $name(Frame th) {\n";
print ::NIECZA_OUT " " x 8, "if (Kernel.TraceCont) { Console.WriteLine(\"Entering $name @ \" + th.ip); }\n";
print ::NIECZA_OUT " " x 8, "if (Kernel.TraceCont) { Console.WriteLine(\"Entering $::UNITNAME : $name @ \" + th.ip); }\n";
if ($self->maxdepth) {
print ::NIECZA_OUT " " x 8, "object " . join(", ", map { "s$_" }
0 .. ($self->maxdepth - 1)) . ";\n";
Expand Down
54 changes: 7 additions & 47 deletions CompilerDriver.pm
Expand Up @@ -4,7 +4,7 @@ use warnings;
use 5.010;

use Sub::Exporter -setup => {
exports => [ qw(header bootstrap setting mainline ast) ]
exports => [ qw(header setting mainline ast) ]
};

open ::NIECZA_OUT, ">&", \*STDOUT;
Expand Down Expand Up @@ -35,19 +35,19 @@ EOH
sub setting {
local $::SETTING_RESUME;
local $::YOU_WERE_HERE;
local $::UNITNAME = 'Setting';
local $::UNITNAME = 'CORE';
$STD::ALL = {};
my $setting_ast = Niecza::Grammar->parsefile("setting", setting => 'NULL',
actions => 'Niecza::Actions')->{_ast};
my $setting_ast = Niecza::Grammar->parsefile("CORE.setting",
setting => 'NULL', actions => 'Niecza::Actions')->{_ast};

$setting_ast->write;
store $::SETTING_RESUME, 'setting_ast.store';
store $::SETTING_RESUME, 'CORE_ast.store';
}

sub mainline {
my $code = shift;
local $::UNITNAME = 'Mainline';
local $::SETTING_RESUME = retrieve 'setting_ast.store';
local $::UNITNAME = '';
local $::SETTING_RESUME = retrieve 'CORE_ast.store';
$STD::ALL = {};
Niecza::Grammar->parse($code, actions => 'Niecza::Actions')->{_ast}->write;
}
Expand All @@ -62,44 +62,4 @@ sub ast {
print YAML::XS::Dump($a);
}

sub bootstrap {
print ::NIECZA_OUT <<EOF;
public class EntryPoint {
public static Frame START(Frame th) {
Frame t;
Dictionary<string,Variable> global = new Dictionary<string,Variable>();
switch (th.ip) {
case 0:
t = new Frame(th, th, new DynBlockDelegate(Setting.BOOT));
t.pos = new LValue[2] { Kernel.NewROScalar(th).lv,
Kernel.NewROScalar(new CLRImportObject(global)).lv };
th.ip = 1;
return t;
case 1:
th.ip = 2;
return ((Variable)th.resultSlot).lv.container.Fetch(th);
case 2:
th.ip = 3;
return ((IP6)th.resultSlot).Invoke(th, new LValue[0] {}, null);
case 3:
return null;
default:
throw new Exception("IP corruption");
}
}
public static void Main() {
Kernel.MainlineContinuation = new DynBlockDelegate(Mainline.BOOT);
Kernel.TraceCont = (Environment.GetEnvironmentVariable("NIECZA_TRACE") != null);
Frame root_f = new Frame(null, null,
new DynBlockDelegate(START));
Frame current = root_f;
while (current != null) {
current = current.Continue();
}
}
}
EOF
}

1;
32 changes: 12 additions & 20 deletions Decl.pm
Expand Up @@ -106,9 +106,10 @@ use CgOp;
use Moose;
extends 'Decl';

has slot => (isa => 'Str', is => 'ro', required => 1);
has list => (isa => 'Bool', is => 'ro', default => 0);
has shared => (isa => 'Bool', is => 'ro', default => 0);
has slot => (isa => 'Str', is => 'ro', required => 1);
has list => (isa => 'Bool', is => 'ro', default => 0);
has shared => (isa => 'Bool', is => 'ro', default => 0);
has zeroinit => (isa => 'Bool', is => 'ro', default => 0);

sub used_slots {
$_[0]->slot, 'Variable';
Expand All @@ -117,7 +118,9 @@ use CgOp;
sub preinit_code {
my ($self, $body) = @_;

if ($self->list) {
if ($self->zeroinit) {
CgOp::proto_var($self->slot, CgOp::newrwscalar(CgOp::null('IP6')));
} elsif ($self->list) {
CgOp::proto_var($self->slot,
CgOp::newrwlistvar(CgOp::fetch(CgOp::scopedlex('Any'))));
} else {
Expand Down Expand Up @@ -242,32 +245,21 @@ use CgOp;
}

{
package Decl::RunMainline;
package Decl::SaveEnv;
use Moose;
extends 'Decl';

sub used_slots { '!mainline', 'Variable' }
has unitname => (isa => 'Str', is => 'ro', required => 1);

sub preinit_code {
my ($self, $body) = @_;

# XXX ought not to have side effects here.
$::SETTING_RESUME = $body;
my $n = $self->unitname;
$n =~ s/::/./g;

CgOp::proto_var('!mainline',
CgOp::subcall(
CgOp::rawscall('Kernel.MakeSub',
CgOp::rawsget('Kernel.MainlineContinuation'),
CgOp::null('Frame'), CgOp::null('Frame')),
CgOp::newscalar(CgOp::letvar('protopad')),
CgOp::letvar('pkg')));
}

sub enter_code {
my ($self, $body) = @_;
$body->mainline ?
CgOp::share_lex('!mainline') :
CgOp::clone_lex('!mainline');
CgOp::rawsset($n . '.Environment', CgOp::letvar('protopad'));
}

__PACKAGE__->meta->make_immutable;
Expand Down
33 changes: 33 additions & 0 deletions Kernel.cs
Expand Up @@ -643,6 +643,19 @@ public class Kernel {
return th;
}

public static void RunLoop(DynBlockDelegate boot) {
Kernel.TraceCont = (Environment.GetEnvironmentVariable("NIECZA_TRACE") != null);
Frame root_f = new Frame(null, null, boot);
Frame current = root_f;
while (current != null) {
current = current.Continue();
}
}

// XXX should be per-unit
public static Variable Global =
NewROScalar(new CLRImportObject(new Dictionary<string,Variable>()));

static Kernel() {
SubMO = new DynMetaObject("Sub");
SubMO.OnInvoke = new DynMetaObject.InvokeHandler(SubInvoke);
Expand All @@ -659,3 +672,23 @@ public class Kernel {
}
}
}

// The root setting
public class NULL {
public static Niecza.Frame Environment = null;
public static Niecza.IP6 Installer = Niecza.Kernel.MakeSub(
new Niecza.DynBlockDelegate(MAIN), null, null);

private static Niecza.Frame MAIN(Niecza.Frame th) {
switch (th.ip) {
case 0:
th.ip = 1;
return Niecza.Kernel.Fetch(th, new Niecza.Variable(false,
Niecza.Variable.Context.Scalar, th.pos[0]));
default:
return ((Niecza.IP6)th.resultSlot).Invoke(th.caller,
new Niecza.LValue[0] {}, null);
}
}
public static void Initialize() {}
}
18 changes: 9 additions & 9 deletions Makefile
Expand Up @@ -4,26 +4,26 @@ STDENV=PERL5LIB=$(STDBASE) PERL6LIB=$(STDBASE):$(STDBASE)/lib
COMPILER=Body.pm CgOp.pm CodeGen.pm CompilerDriver.pm Decl.pm Op.pm RxOp.pm\
Sig.pm Unit.pm Niecza/Actions.pm Niecza/Grammar.pmc .STD_build_stamp

all: Setting.dll
all: CORE.dll
git rev-parse HEAD | cut -c1-7 > VERSION

test: $(COMPILER) test.pl Setting.dll
perl -MFile::Slurp -MCompilerDriver=:all -e 'header; mainline(scalar read_file("test.pl")); bootstrap' > Program.cs
gmcs /r:Kernel.dll /r:Setting.dll Program.cs
test: $(COMPILER) test.pl CORE.dll
perl -MFile::Slurp -MCompilerDriver=:all -e 'header; mainline(scalar read_file("test.pl"))' > Program.cs
gmcs /r:Kernel.dll /r:CORE.dll Program.cs
prove -e 'mono --debug=casts' Program.exe

.DELETE_ON_ERROR:

Setting.cs: $(COMPILER) setting
perl -MCompilerDriver=:all -e 'header; setting' > Setting.cs
CORE.cs: $(COMPILER) CORE.setting
perl -MCompilerDriver=:all -e 'header; setting' > CORE.cs

Kernel.dll: Kernel.cs
gmcs /target:library /out:Kernel.dll Kernel.cs
mono --aot Kernel.dll

Setting.dll: Kernel.dll Setting.cs
gmcs /target:library /out:Setting.dll /r:Kernel.dll Setting.cs
mono --aot Setting.dll
CORE.dll: Kernel.dll CORE.cs
gmcs /target:library /out:CORE.dll /r:Kernel.dll CORE.cs
mono --aot CORE.dll

Niecza/Grammar.pmc: Niecza/Grammar.pm6 .STD_build_stamp
STD5PREFIX=$(STDBASE)/ $(STDENV) $(STDBASE)/viv -5 -o Niecza/Grammar.pmc Niecza/Grammar.pm6
Expand Down
30 changes: 26 additions & 4 deletions Niecza/Actions.pm
Expand Up @@ -723,7 +723,7 @@ sub blockoid { my ($cl, $M) = @_;
# XXX horrible cheat, but my data structures aren't up to the task of
# $::UNIT being a class body &c.
if ($M->Str eq '{YOU_ARE_HERE}') {
$M->{_ast} = Op::YouAreHere->new;
$M->{_ast} = Op::YouAreHere->new(unitname => $::UNITNAME);
} else {
$M->{_ast} = $M->{statementlist}{_ast};
}
Expand Down Expand Up @@ -1184,11 +1184,33 @@ sub statement_prefix__S_START { my ($cl, $M) = @_;
}

sub comp_unit { my ($cl, $M) = @_;
my $body = $cl->sl_to_block('mainline', $M->{statementlist}{_ast},
subname => 'mainline');
my $body;
my $sl = $M->{statementlist}{_ast};

if (!$::YOU_WERE_HERE && $::UNITNAME) {
$sl = Op::StatementList->new(children => [ $sl,
Op::YouAreHere->new(save_only => 1, unitname => $::UNITNAME)]);
}

$body = $cl->sl_to_block('mainline', $sl, subname => 'mainline');
if ($::YOU_WERE_HERE) {
$body = Body->new(
type => 'mainline',
name => 'install',
signature => Sig->new(params => [
Sig::Parameter->new(target => Sig::Target->new(
slot => '!mainline', zeroinit => 1))]),
do => Op::CallSub->new(
invocant => Op::CgOp->new(op => CgOp::newscalar(
CgOp::rawsget($::SETTINGNAME . ".Installer"))),
positionals => [Op::SubDef->new(
var => $cl->gensym, body => $body)]));
}

$M->{_ast} = Unit->new(mainline => $body, name => $::UNITNAME,
$::SETTING_RESUME ? (setting => $::SETTING_RESUME) : ());
($::SETTING_RESUME ? (setting => $::SETTING_RESUME) : ()),
is_setting => (!!$::YOU_WERE_HERE),
setting_name => $::SETTINGNAME);
}

1;
8 changes: 6 additions & 2 deletions Op.pm
Expand Up @@ -89,11 +89,15 @@ use CgOp;
use Moose;
extends 'Op';

sub local_decls { Decl::RunMainline->new }
has unitname => (isa => 'Str', is => 'ro', required => 1);
has save_only => (isa => 'Bool', is => 'ro', default => 0);

sub local_decls { Decl::SaveEnv->new(unitname => $_[0]->unitname) }

sub code {
my ($self, $body) = @_;
CgOp::subcall(CgOp::fetch(CgOp::scopedlex('!mainline')));
$self->save_only ? CgOp::null('Variable') :
CgOp::subcall(CgOp::fetch(CgOp::scopedlex('!mainline')));
}

__PACKAGE__->meta->make_immutable;
Expand Down
4 changes: 3 additions & 1 deletion Sig.pm
Expand Up @@ -8,11 +8,13 @@ use 5.010;

has slot => (is => 'ro', isa => 'Maybe[Str]', required => 1);
has list => (is => 'ro', isa => 'Bool', default => 0);
has zeroinit => (is => 'ro', isa => 'Bool', default => 0);

sub local_decls {
my $self = shift;
if ($self->slot) {
Decl::SimpleVar->new(slot => $self->slot, list => $self->list)
Decl::SimpleVar->new(slot => $self->slot, list => $self->list,
zeroinit => $self->zeroinit)
} else {
()
}
Expand Down

0 comments on commit a7a25a2

Please sign in to comment.