Permalink
Browse files

Prototype the JSON-based to_nam

  • Loading branch information...
1 parent 4c0d984 commit add05ef90d377faad3205ce78ba5d37a39fe32c2 @sorear committed Dec 13, 2010
Showing with 130 additions and 77 deletions.
  1. +7 −3 src/CompilerDriver.pm
  2. +6 −13 src/Metamodel.pm
  3. +116 −60 src/NAMBackend.pm
  4. +1 −1 src/RxOp.pm
View
10 src/CompilerDriver.pm
@@ -216,7 +216,13 @@ sub compile {
[ 'parse', sub {
$ast = Niecza::Grammar->$m($a, setting => $lang,
actions => 'Niecza::Actions')->{_ast}; } ],
- [ 'begin', sub { $ast = $ast->begin } ],
+ [ 'begin', sub {
+ $ast = $ast->begin;
+ if (defined $name) {
+ $ast->filename($filename);
+ $ast->modtime($modtime);
+ }
+ } ],
[ 'beta', sub { Optimizer::Beta::run($ast) } ],
[ 'simpl', sub { Optimizer::Simplifier::run($ast) } ],
[ 'nam', sub { $ast = NAMBackend::run($ast) } ],
@@ -228,8 +234,6 @@ sub compile {
close $fh;
if (defined $name) {
$ast->syml($::niecza_mod_symbols);
- $ast->filename($filename);
- $ast->modtime($modtime);
store $ast, File::Spec->catfile($builddir, "$basename.store");
}
} ],
View
19 src/Metamodel.pm
@@ -235,8 +235,6 @@ our %units;
has exports => (is => 'rw', isa => 'ArrayRef[ArrayRef[Str]]');
- sub BUILD { push @{ $unit->packages }, $_[0] }
-
sub add_attribute {
my ($self, $name) = @_;
die "attribute $name defined in a lowly package";
@@ -390,7 +388,6 @@ our %units;
use Moose;
extends 'Metamodel::Module';
- has builder => (isa => 'ArrayRef', is => 'rw');
has attributes => (isa => 'ArrayRef[Str]', is => 'ro',
default => sub { [] });
has methods => (isa => 'ArrayRef', is => 'ro',
@@ -705,17 +702,13 @@ our %units;
has bottom_ref => (is => 'rw');
has xref => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
- has tdeps => (isa => 'HashRef[Metamodel::Unit]', is => 'ro',
+ has tdeps => (isa => 'HashRef[ArrayRef]', is => 'ro',
default => sub { +{} });
has filename => (isa => 'Str', is => 'rw');
has modtime => (isa => 'Num', is => 'rw');
has syml => (is => 'rw');
- # we like to delete staticsubs in the optimizer, so visiting them is
- # a tad harder
- has packages => (isa => 'ArrayRef[Metamodel::Package]', is => 'ro',
- default => sub { [] });
has next_anon_stash => (isa => 'Int', is => 'rw', default => 0);
sub is_true_setting { $_[0]->name eq 'SAFE' || $_[0]->name eq 'CORE' }
@@ -754,7 +747,9 @@ our %units;
sub visit_local_packages {
my ($self, $cb) = @_;
- $cb->($_) for @{ $self->packages };
+ for (@{ $self->xref }) {
+ $cb->($_) if defined($_) && $_->isa('Metamodel::Package');
+ }
}
sub visit_local_subs_postorder {
@@ -782,7 +777,6 @@ our %units;
for (keys %{ $u2->tdeps }) {
$units{$_} //= CompilerDriver::metadata_for($_);
$self->tdeps->{$_} //= $u2->tdeps->{$_};
- $u2->tdeps->{$_} = $self->tdeps->{$_}; # save a bit of memory
}
$self->ns->add_from($u2name);
$u2;
@@ -992,7 +986,7 @@ sub Op::Attribute::begin {
$ns->add_attribute($self->name);
my $nb = Metamodel::StaticSub->new(
unit => $unit,
- outer => $opensubs[-1],
+ outer => $opensubs[-1]->xref,
name => $self->name,
cur_pkg => $opensubs[-1]->cur_pkg,
returnable => 0,
@@ -1122,7 +1116,6 @@ sub Op::PackageDef::begin {
@{ $self->exports } ]);
if ($pclass eq 'Metamodel::ParametricRole') {
- $unit->deref($obj)->builder($body->xref);
$body->parametric_role_hack($obj);
$body->add_my_name('*params', noinit => 1);
$body->create_static_pad;
@@ -1151,7 +1144,7 @@ sub Op::Augment::begin {
my $ph = Metamodel::StaticSub->new(
unit => $unit,
- outer => $body,
+ outer => $body->xref,
cur_pkg => [ 'GLOBAL' ],
name => 'ANON',
is_phaser => 0,
View
176 src/NAMBackend.pm
@@ -10,86 +10,142 @@ our $unit;
sub run {
local $unit = shift;
- [ header(),
- stashlog(),
- objects() ]
+ $unit->visit_local_subs_postorder(\&nam_sub);
+
+ $unit->to_nam;
}
-sub header {
- [ $unit->mainline->xref,
- $unit->name,
- $unit->setting,
- $unit->bottom_ref,
- $unit->tdeps,
- $unit->modtime,
- $unit->filename ]
+sub nam_sub {
+ my $s = shift;
+ $s->{nam} = $s->code->cgop($s);
+ if ($s->parametric_role_hack) {
+ for (@{ $unit->deref($s->parametric_role_hack)->methods }) {
+ if (ref $_->[0]) {
+ $_->[0] = $_->[0]->cgop($s);
+ }
+ }
+ }
+ delete $s->{code};
}
-sub stashlog {
- $unit->ns->log;
+sub Metamodel::Unit::to_nam {
+ my $self = shift;
+ [
+ $self->mainline->xref,
+ $self->name,
+ $self->ns->log,
+ $self->setting,
+ $self->bottom_ref,
+ [ map { $_ && $_->to_nam } @{ $self->xref } ],
+ [ map { [$_, @{ $self->tdeps->{$_} }] } sort keys %{ $self->tdeps } ],
+ ]
}
-sub objects {
- map { defined($_) ? $_->nam_object : [] } @{ $unit->xref };
+sub Metamodel::StaticSub::to_nam {
+ my $self = shift;
+ my $flags = 0;
+ $flags |= 1 if $self->run_once;
+ $flags |= 2 if $self->spad_exists;
+ $flags |= 4 if $self->gather_hack;
+ $flags |= 8 if $self->strong_used;
+ $flags |= 16 if $self->returnable;
+ $flags |= 32 if $self->augmenting;
+ [
+ $self->name,
+ $self->{outer}, # get the raw xref
+ $flags,
+ [ map { $_->xref->[1] } @{ $self->zyg } ],
+ $self->parametric_role_hack,
+ $self->augment_hack,
+ $self->is_phaser,
+ $self->body_of,
+ $self->in_class,
+ $self->cur_pkg,
+ $self->class,
+ $self->ltm,
+ $self->exports,
+ ($self->signature && [ map { $_->to_nam } @{ $self->signature->params } ]),
+ [ map { [ $_, @{ $self->lexicals->{$_}->to_nam } ] }
+ sort keys %{ $self->lexicals } ],
+ $self->{nam}->to_nam,
+ ]
}
-sub Metamodel::StaticSub::nam_object {
- my ($self) = @_;
- [ $self->outer,
- $self->name,
- $self->run_once,
- $self->spad_exists,
- $self->signature,
- $self->lexicals,
- $self->code->cgop($self),
- [ map { $_->xref->[1] } @{ $self->zyg } ],
- $self->gather_hack,
- $self->parametric_role_hack,
- $self->augment_hack,
- $self->is_phaser // -1,
- $self->body_of,
- $self->in_class,
- $self->cur_pkg,
- $self->returnable,
- $self->augmenting,
- $self->class,
- $self->ltm,
- $self->exports ]
+sub Metamodel::Package::to_nam {
+ my $self = shift;
+ [
+ substr(lc(ref($self)),11),
+ $self->name,
+ @_
+ ]
}
-sub Metamodel::Package::nam_object {
+sub Metamodel::Class::to_nam {
my $self = shift;
- [ $self->name,
- $self->exports ]
+ $self->Metamodel::Package::to_nam(
+ $self->attributes,
+ [ map { $_->to_nam } @{ $self->methods } ],
+ $self->superclasses,
+ $self->linearized_mro,
+ );
}
-sub Metamodel::Class::nam_object {
+sub Metamodel::Role::to_nam {
my $self = shift;
- [ $self->name,
- $self->exports,
- $self->attributes,
- $self->methods,
- $self->superclasses,
- $self->linearized_mro ]
+ $self->Metamodel::Package::to_nam(
+ $self->attributes,
+ [ map { $_->to_nam } @{ $self->methods } ],
+ $self->superclasses,
+ );
}
-sub Metamodel::Role::nam_object {
+sub Metamodel::ParametricRole::to_nam {
my $self = shift;
- [ $self->name,
- $self->exports,
- $self->attributes,
- $self->methods,
- $self->superclasses ]
+ $self->Metamodel::Package::to_nam(
+ $self->attributes,
+ $self->methods,
+ $self->superclasses,
+ );
+}
+
+sub Metamodel::Method::to_nam {
+ [ $_[0]->name, ($_[0]->private ? 1 : 0), $_[0]->body ]
}
-sub Metamodel::ParametricRole::nam_object {
+sub Sig::Parameter::to_nam {
my $self = shift;
- [ $self->name,
- $self->exports,
- $self->builder,
- $self->attributes,
- $self->methods,
- $self->superclasses ]
+ my $flags = 0;
+ $flags |= 1 if $self->slurpy;
+ $flags |= 2 if $self->slurpycap;
+ $flags |= 4 if $self->rwtrans;
+ $flags |= 8 if $self->full_parcel;
+ $flags |= 16 if $self->optional;
+ $flags |= 32 if $self->positional;
+ $flags |= 64 if $self->readonly;
+ $flags |= 128 if $self->list;
+ $flags |= 256 if $self->hash;
+
+ [
+ $self->name,
+ $flags,
+ $self->slot,
+ $self->names,
+ $self->mdefault
+ ]
+}
+
+
+sub Metamodel::Lexical::Simple::to_nam {
+ ['simple', ($_[0]->noinit ? 4 : 0) + ($_[0]->list ? 2 : 0) +
+ ($_[0]->hash ? 1 : 0)]
+}
+sub Metamodel::Lexical::Common::to_nam { ['common', @{$_[0]->path}, $_[0]->name ] }
+sub Metamodel::Lexical::Alias::to_nam { ['alias', $_[0]->to] }
+sub Metamodel::Lexical::SubDef::to_nam { ['sub', @{ $_[0]->body->xref } ] }
+sub Metamodel::Lexical::Stash::to_nam { ['stash', @{ $_[0]->path } ] }
+
+sub CgOpNode::to_nam {
+ [ map { Scalar::Util::blessed($_) ? $_->to_nam : $_ } @{ $_[0] } ]
}
1;
View
2 src/RxOp.pm
@@ -1002,7 +1002,7 @@ use CgOp;
sub lad {
my ($self) = @_;
- [ 'CC', CgOp::cc_expr($self->cc) ];
+ [ 'CC', @{ $self->cc } ];
}
__PACKAGE__->meta->make_immutable;

0 comments on commit add05ef

Please sign in to comment.