/
CompilerDriver.pm
191 lines (161 loc) · 5.15 KB
/
CompilerDriver.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package CompilerDriver;
use strict;
use warnings;
use 5.010;
use Sub::Exporter -setup => {
exports => [ qw(compile) ]
};
use Time::HiRes 'time';
use File::Basename;
use autodie ':all';
open ::NIECZA_OUT, ">&", \*STDOUT;
use lib 'STD_checkout';
BEGIN {
use File::Spec;
$CursorBase::SET_PERL6LIB = [ File::Spec->curdir ];
}
use Body ();
use Decl ();
use Unit ();
use Op ();
use Optimizer::Beta ();
use ResolveLex ();
use Storable;
use Niecza::Grammar ();
use Niecza::Actions ();
my $builddir;
{
my $libdir = dirname($INC{'CompilerDriver.pm'});
$builddir = File::Spec->catdir($libdir, "../obj");
}
File::Path::make_path($builddir);
sub build_file { File::Spec->catfile($builddir, $_[1]) }
sub metadata_for {
my ($cl, $unit) = @_;
$unit =~ s/::/./g;
Storable::retrieve(File::Spec->catfile($builddir, "$unit.store"))
}
{
package
CursorBase;
no warnings 'redefine';
sub sys_save_syml {
my ($self, $all) = @_;
$::niecza_mod_symbols = $all;
}
sub sys_get_perl6lib {
File::Spec->curdir
}
sub sys_load_modinfo {
my $self = shift;
my $module = shift;
$module =~ s/::/./g;
my ($symlfile) = File::Spec->catfile($builddir, "$module.store");
my ($modfile) = $self->sys_find_module($module, 0)
or return undef;
unless (-f $symlfile and -M $modfile > -M $symlfile) {
$self->sys_compile_module($module, $symlfile, $modfile);
}
return Storable::retrieve($symlfile)->{'syml'};
}
sub load_lex {
my $self = shift;
my $setting = shift;
my $settingx = $setting;
$settingx =~ s/::/./g;
if ($setting eq 'NULL') {
my $id = "MY:file<NULL.pad>:line(1):pos(0)";
my $core = Stash->new('!id' => [$id], '!file' => 'NULL.pad',
'!line' => 1);
return Stash->new('CORE' => $core, 'MY:file<NULL.pad>' => $core,
'SETTING' => $core, $id => $core);
}
my $astf = File::Spec->catfile($builddir, "$settingx.store");
if (-e $astf) {
return Storable::retrieve($astf)->{'syml'};
}
$self->sorry("Unable to load setting $setting.");
return $self->load_lex("NULL");
}
}
sub compile {
my %args = @_;
$args{lang} //= 'CORE';
local %::UNITDEPS;
local $::SETTING_RESUME;
local $::niecza_mod_symbols;
local $::YOU_WERE_HERE;
local $::UNITNAME = $args{main} ? '' : $args{file};
local $::SAFEMODE = $args{safe};
$::UNITNAME =~ s/\.(?:pm6?|setting)//;
$::UNITNAME =~ s|[\\/]|.|g;
$STD::ALL = {};
$::SETTING_RESUME = CompilerDriver->metadata_for($args{lang})->{setting}
unless $args{lang} eq 'NULL';
$::UNITDEPS{$args{lang}} = 1 if $args{lang} ne 'NULL';
my ($m, $a) = $args{file} ? ('parsefile', $args{file}) :
('parse', $args{code});
my $ast;
my $basename = $::UNITNAME || 'MAIN';
my $csfile = File::Spec->catfile($builddir, "$basename.cs");
my $outname = File::Spec->catfile($builddir,
$basename . ($args{main} ? ".exe" : ".dll"));
my @phases = (
[ 'parse', sub {
$ast = Niecza::Grammar->$m($a, setting => $args{lang},
actions => 'Niecza::Actions')->{_ast}; } ],
[ 'lift_decls', sub {
$::SETTING_RESUME = undef;
$ast->lift_decls; } ],
[ 'beta', sub { Optimizer::Beta::run($ast) } ],
[ 'extract_scopes', sub { $ast->extract_scopes } ],
[ 'to_cgop', sub { $ast->to_cgop } ],
[ 'resolve_lex', sub { ResolveLex::run($ast) } ],
[ 'to_anf', sub { $ast->to_anf } ],
[ 'writecs', sub {
open ::NIECZA_OUT, ">", $csfile;
binmode ::NIECZA_OUT, ":utf8";
print ::NIECZA_OUT <<EOH;
using System;
using System.Collections.Generic;
using Niecza;
EOH
$ast->write;
close ::NIECZA_OUT;
if ($::SETTING_RESUME || $::niecza_mod_symbols) {
my $blk = { setting => $::SETTING_RESUME,
syml => $::niecza_mod_symbols };
store $blk, File::Spec->catfile($builddir, "$basename.store");
}
$ast = undef;
} ],
[ 'gmcs', sub {
delete $::UNITDEPS{$basename};
my @args = ("gmcs",
($args{main} ? () : ("/target:library")),
"/lib:$builddir",
"/r:Kernel.dll", (map { "/r:$_.dll" } sort keys %::UNITDEPS),
"/out:$outname",
$csfile);
print STDERR "@args\n" if $args{stagetime};
system @args;
} ],
[ 'aot', sub {
system "mono", "--aot", $outname;
} ]);
for my $p (@phases) {
next if $p->[0] eq 'aot' && !$args{aot};
my $t1 = time if $args{stagetime};
$p->[1]->();
my $t2 = time if $args{stagetime};
printf "%-20s: %gs\n", "$basename " . $p->[0],
$t2 - $t1 if $args{stagetime};
if ($args{stopafter} && $args{stopafter} eq $p->[0]) {
if ($ast) {
print STDERR YAML::XS::Dump($ast);
}
return;
}
}
}
1;