This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnqp-jvm-cc.nqp
268 lines (236 loc) · 7.93 KB
/
nqp-jvm-cc.nqp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use QASTJASTCompiler;
use JASTNodes;
use helper;
# Backend class for the JVM.
class HLL::Backend::JVM {
method apply_transcodings($s, $transcode) {
$s
}
method config() {
nqp::hash()
}
method force_gc() {
nqp::die("Cannot force GC on JVM backend yet");
}
method name() {
'jvm'
}
method nqpevent($spec?) {
# Doesn't do anything just yet
}
method run_profiled($what) {
nqp::printfh(nqp::getstderr(),
"Attach a profiler (e.g. JVisualVM) and press enter");
nqp::readlinefh(nqp::getstdin());
$what();
}
method run_traced($level, $what) {
nqp::die("No tracing support");
}
method version_string() {
"JVM"
}
method stages() {
'jast classfile jvm'
}
method is_precomp_stage($stage) {
# Currently, everything is pre-comp since we're a cross-compiler.
1
}
method is_textual_stage($stage) {
0
}
method classname($source, *%adverbs) {
unless %*COMPILING<%?OPTIONS><javaclass> {
%*COMPILING<%?OPTIONS><javaclass> := nqp::sha1(nqp::sha1($source) ~ nqp::time_n());
}
$source
}
method jast($qast, *%adverbs) {
QAST::CompilerJAST.jast($qast, :classname(%*COMPILING<%?OPTIONS><javaclass>));
}
method classfile($jast, *%adverbs) {
my $name := %*COMPILING<%?OPTIONS><javaclass>;
my $dump := $jast.dump();
spurt($name ~ '.dump', $dump);
run('java',
'-cp ' ~ pathlist('bin', '3rdparty/asm/asm-4.1.jar'),
'org/perl6/nqp/jast2bc/JASTToJVMBytecode',
$name ~ '.dump', $name ~ '.class');
unlink($name ~ '.dump');
my $fh := open($name ~ '.class', :r, :bin);
my $class := $fh.readall();
$fh.close();
$class
}
method jvm($class, *%adverbs) {
my $name := %*COMPILING<%?OPTIONS><javaclass>;
-> {
run('java',
'-cp ' ~ pathlist('.', 'nqp-runtime.jar', '3rdparty/asm/asm-4.1.jar'),
$name);
unlink($name ~ '.class');
}
}
method is_compunit($cuish) {
!pir::isa__IPs($cuish, 'String')
}
}
sub MAIN(*@ARGS) {
# Get original compiler, then re-register it as a cross compiler.
my $nqpcomp-orig := nqp::getcomp('nqp');
my $nqpcomp-cc := nqp::clone($nqpcomp-orig);
$nqpcomp-cc.language('nqp-cc');
# Add --javaclass command line option for specifying the name of the Java class
# to generate.
my @clo := $nqpcomp-cc.commandline_options();
@clo.push('javaclass=s');
$nqpcomp-cc.backend(HLL::Backend::JVM);
$nqpcomp-cc.addstage('classname', :after<start>);
$nqpcomp-cc.command_line(@ARGS, :stable-sc(1),
:setting('NQPCOREJVM'), :custom-regex-lib('QRegexJVM'),
:encoding('utf8'), :transcode('ascii iso-8859-1'));
}
# Set up various NQP-specific ops.
my $ops := QAST::CompilerJAST.operations();
my $RT_OBJ := 0;
my $RT_INT := 1;
my $RT_NUM := 2;
my $RT_STR := 3;
my $TYPE_TC := 'Lorg/perl6/nqp/runtime/ThreadContext;';
my $TYPE_OPS := 'Lorg/perl6/nqp/runtime/Ops;';
my $TYPE_SMO := 'Lorg/perl6/nqp/sixmodel/SixModelObject;';
my $TYPE_STR := 'Ljava/lang/String;';
$ops.add_hll_op('nqp', 'preinc', -> $qastcomp, $op {
my $var := $op[0];
unless nqp::istype($var, QAST::Var) {
nqp::die("Pre-increment can only work on a variable");
}
$qastcomp.as_jast(QAST::Op.new(
:op('bind'),
$var,
QAST::Op.new(
:op('add_n'),
$var,
QAST::IVal.new( :value(1) )
)));
});
$ops.add_hll_op('nqp', 'predec', -> $qastcomp, $op {
my $var := $op[0];
unless nqp::istype($var, QAST::Var) {
nqp::die("Pre-decrement can only work on a variable");
}
$qastcomp.as_jast(QAST::Op.new(
:op('bind'),
$var,
QAST::Op.new(
:op('sub_n'),
$var,
QAST::IVal.new( :value(1) )
)));
});
$ops.add_hll_op('nqp', 'postinc', -> $qastcomp, $op {
my $var := $op[0];
my $tmp := QAST::Op.unique('tmp');
unless nqp::istype($var, QAST::Var) {
nqp::die("Post-increment can only work on a variable");
}
$qastcomp.as_jast(QAST::Stmt.new(
:resultchild(0),
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name($tmp), :scope('local'), :decl('var'), :returns($var.returns) ),
$var
),
QAST::Op.new(
:op('bind'),
$var,
QAST::Op.new(
:op('add_n'),
QAST::Var.new( :name($tmp), :scope('local'), :returns($var.returns) ),
QAST::IVal.new( :value(1) )
)
)));
});
$ops.add_hll_op('nqp', 'postdec', -> $qastcomp, $op {
my $var := $op[0];
my $tmp := QAST::Op.unique('tmp');
unless nqp::istype($var, QAST::Var) {
nqp::die("Post-decrement can only work on a variable");
}
$qastcomp.as_jast(QAST::Stmt.new(
:resultchild(0),
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name($tmp), :scope('local'), :decl('var') ),
$var
),
QAST::Op.new(
:op('bind'),
$var,
QAST::Op.new(
:op('sub_n'),
QAST::Var.new( :name($tmp), :scope('local') ),
QAST::IVal.new( :value(1) )
)
)));
});
$ops.add_hll_op('nqp', 'numify', -> $qastcomp, $op {
$qastcomp.as_jast($op[0], :want($RT_NUM))
});
$ops.add_hll_op('nqp', 'stringify', -> $qastcomp, $op {
$qastcomp.as_jast($op[0], :want($RT_STR))
});
$ops.add_hll_op('nqp', 'falsey', -> $qastcomp, $op {
# Compile expression to falsify.
my $il := JAST::InstructionList.new();
my $res := $qastcomp.as_jast($op[0]);
$il.append($res.jast);
$*STACK.obtain($il, $res);
# Now go by type.
if $res.type == $RT_OBJ {
$il.append(JAST::Instruction.new( :op('aload_1') ));
$il.append(JAST::Instruction.new( :op('invokestatic'),
$TYPE_OPS, 'isfalse', 'Long', $TYPE_SMO, $TYPE_TC ));
}
elsif $res.type == $RT_STR {
$il.append(JAST::Instruction.new( :op('invokestatic'),
$TYPE_OPS, 'isfalse_s', 'Long', $TYPE_STR ));
}
else {
my $false := JAST::Label.new( :name($op.unique('not_false')) );
my $done := JAST::Label.new( :name($op.unique('not_done')) );
$il.append(JAST::PushIVal.new( :value(0) ));
$il.append(JAST::Instruction.new( :op($res.type == $RT_INT ?? 'lcmp' !! 'dcmpl') ));
$il.append(JAST::Instruction.new( :op('ifne'), $false ));
$il.append(JAST::PushIVal.new( :value(1) ));
$il.append(JAST::Instruction.new( :op('goto'), $done ));
$il.append($false);
$il.append(JAST::PushIVal.new( :value(0) ));
$il.append($done);
}
$qastcomp.result($il, $RT_INT)
});
# NQP object unbox, which also must somewhat handle coercion.
QAST::OperationsJAST.add_hll_unbox('nqp', $RT_INT, -> $qastcomp {
my $il := JAST::InstructionList.new();
$il.append(JAST::Instruction.new( :op('aload_1') ));
$il.append(JAST::Instruction.new( :op('invokestatic'), $TYPE_OPS,
'smart_numify', 'Double', $TYPE_SMO, $TYPE_TC ));
$il.append(JAST::Instruction.new( :op('d2l') ));
$il
});
QAST::OperationsJAST.add_hll_unbox('nqp', $RT_NUM, -> $qastcomp {
my $il := JAST::InstructionList.new();
$il.append(JAST::Instruction.new( :op('aload_1') ));
$il.append(JAST::Instruction.new( :op('invokestatic'), $TYPE_OPS,
'smart_numify', 'Double', $TYPE_SMO, $TYPE_TC ));
$il
});
QAST::OperationsJAST.add_hll_unbox('nqp', $RT_STR, -> $qastcomp {
my $il := JAST::InstructionList.new();
$il.append(JAST::Instruction.new( :op('aload_1') ));
$il.append(JAST::Instruction.new( :op('invokestatic'), $TYPE_OPS,
'smart_stringify', $TYPE_STR, $TYPE_SMO, $TYPE_TC ));
$il
});