/
Compiler.pm
595 lines (490 loc) · 17 KB
/
Compiler.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
module POST::Compiler;
=begin
Generate PBC file.
=end
=begin Labels
Labels are handled in two passes:
=item Generate todolist.
Todolist is hash of (position=>(name, op_start)), where C<position> is offset
in bytecode where label with C<name> used.
=item Populate labels.
Iterate over todolist and replace labels with offset of Sub start.
=end Labels
our $OPLIB;
our $DEBUG;
our $REGALLOC;
INIT {
$REGALLOC := POST::VanillaAllocator.new;
}
method pbc($post, %adverbs) {
#pir::trace(1);
$OPLIB := pir::new__psp('OpLib', "core_ops");
$DEBUG := 1 || %adverbs<debug>;
# Emitting context. Contains consts, etc.
my %context := self.create_context($post);
%context<pir_file> := $post;
# Iterate over Subs and put them into POST::File table.
# Used for discriminating find_sub_not_null vs "constant Subs" in
# PCC call handling.
self.enumerate_subs($post);
for @($post) -> $s {
self.to_pbc($s, %context);
}
%context<packfile>;
};
##########################################
# Emiting pbc
our multi method to_pbc($what, %context) {
self.panic($what.WHAT);
}
our multi method to_pbc(Undef $what, %context) {
# Do nothing.
}
our multi method to_pbc(POST::Sub $sub, %context) {
# Store current Sub in context to resolve symbols and constants.
%context<sub> := $sub;
# Allocate registers.
my @n_regs_used := $REGALLOC.process($sub);
self.debug('n_regs_used ' ~ @n_regs_used.join('-')) if $DEBUG;
self.dumper($sub, "sub") if $DEBUG;
my $bc := %context<bytecode>;
# Todo-list of Labels.
%context<labels_todo> := hash();
# Packfile poop his pants...
my $sb := pir::new__PS('StringBuilder');
$sb.push(~$sub.name);
my $subname := ~$sb;
self.debug("Emitting $subname") if $DEBUG;
%context<constants>.get_or_create_string($subname);
my $start_offset := +$bc;
self.debug("From $start_offset") if $DEBUG;
# Handle params
$sub<params> := list() unless $sub<params>;
$sub<params>.unshift(POST::Value.new(:name<self>)) if $sub.is_method;
# Bloody "main" sub...
if +$sub<params> {
self.build_pcc_call("get_params_pc", $sub<params>, %context);
}
# Emit ops.
for @($sub) {
self.to_pbc($_, %context);
}
# Default .return(). XXX We don't need it (probably)
$bc.push([
'set_returns',
0x000 # id of FIA
]);
$bc.push([
'returncc'
]);
my $end_offset := +$bc;
self.debug("To $end_offset") if $DEBUG;
# Fixup labels to set real offsets
self.fixup_labels($sub, %context<labels_todo>, %context<bytecode>);
# Now create Sub PMC using hash of values.
my %sub := hash(
:start_offs( $start_offset ),
:end_offs( $end_offset ),
:name( $subname ),
# PCT's Sub.subid creates it. So poke inside $sub
:subid( $sub<subid> // $subname ),
:ns_entry_name( $sub.nsentry // $subname ),
:vtable_index( -1 ), # It must be -1!!!
:HLL_id<0>,
:method_name( $sub.is_method ?? $subname !! ''),
:n_regs_used(@n_regs_used),
:pf_flags(self.create_sub_pf_flags($sub)),
:comp_flags(self.create_sub_comp_flags($sub)),
);
if pir::defined__ip($sub.namespace) {
my $nskey := $sub.namespace.to_pmc(%context)[0];
if pir::typeof__sp($nskey) eq 'Key' {
%sub<namespace_name> := $nskey;
}
}
# and store it in PackfileConstantTable
# We can have pre-allocated constant for this sub already.
# XXX Use .namespace for generating full name!
my $idx := $sub.constant_index;
if pir::defined__ip($idx) {
self.debug("Reusing old constant") if $DEBUG;
%context<constants>[$idx] := pir::new__PSP('Sub', %sub);
}
else {
self.debug("Allocate new constant") if $DEBUG;
$idx := %context<constants>.push(pir::new__PSP('Sub', %sub));
$sub.constant_index($idx);
}
}
our multi method to_pbc(POST::Op $op, %context) {
# Generate full name
my $fullname := $op.pirop;
self.debug("Short name $fullname") if $DEBUG;
for @($op) {
my $type := $_.type || self.get_register($_.name, %context).type;
$fullname := ~$fullname ~ '_' ~ ~$type;
}
self.debug("Fullname $fullname") if $DEBUG;
# Store op offset. It will be needed for calculating labels.
%context<opcode_offset> := +%context<bytecode>;
%context<bytecode>.push($OPLIB{$fullname});
for @($op) {
self.to_pbc($_, %context);
}
}
# Some PIR sugar produces nested Nodes.
our multi method to_pbc(POST::Node $node, %context) {
for @($node) {
self.to_pbc($_, %context);
}
}
our multi method to_pbc(POST::Label $l, %context) {
my $bc := %context<bytecode>;
if $l.declared {
my $pos := +$bc;
self.debug("Declare label '{ $l.name }' at $pos") if $DEBUG;
# Declaration of Label. Update offset in Sub.labels.
$l.position($pos);
# We can have "enclosed" ops. Process them now.
for @($l) {
self.to_pbc($_, %context);
}
}
else {
# Usage of Label. Put into todolist and reserve space.
my $pos := +$bc;
$bc.push(0);
%context<labels_todo>{$pos} := list($l.name, %context<opcode_offset>);
self.debug("Todo label '{ $l.name }' at $pos, { %context<opcode_offset> }") if $DEBUG;
}
}
our multi method to_pbc(POST::Call $call, %context) {
my $bc := %context<bytecode>;
my $calltype := $call.calltype;
my $is_tailcall := $calltype eq 'tailcall';
if $calltype eq 'call' || $calltype eq 'tailcall' {
if $call.invocant {
$call<params> := list() unless $call<params>;
$call<params>.unshift($call.invocant);
}
self.build_pcc_call("set_args_pc", $call<params>, %context);
if $call.invocant {
if $call.name.isa(POST::Constant) {
$bc.push($is_tailcall
?? $OPLIB<tailcallmethod_p_sc>
!! $OPLIB<callmethodcc_p_sc>);
self.to_pbc($call.invocant, %context);
self.to_pbc($call.name, %context);
}
else {
self.panic('NYI $P0.$S0()');
}
}
else {
my $SUB;
my $processed := 0;
if $call.name.isa(POST::Constant) {
# Constant string. E.g. "foo"()
# Avoid find_sub_not_null when Sub is constant.
my $full_name;
$full_name := %context<sub>.namespace.Str if %context<sub>.namespace;
$full_name := ~$full_name ~ ~$call<name><value>;
my $invocable_sub := %context<pir_file>.sub($full_name);
self.debug("invocable_sub $invocable_sub") if $DEBUG;
if $invocable_sub {
my $idx := $invocable_sub.constant_index;
unless pir::defined__ip($idx) {
# Allocate new space in constant table. We'll reuse it later.
$idx := %context<constants>.push(pir::new__ps("Integer"));
$invocable_sub.constant_index($idx);
}
$SUB := %context<sub>.symbol("!SUB");
$bc.push($OPLIB<set_p_pc>);
self.to_pbc($SUB, %context);
$bc.push($idx);
$processed := 1;
}
}
unless $processed {
if $call.name.isa(POST::Constant) {
$SUB := %context<sub>.symbol("!SUB");
$bc.push($OPLIB<find_sub_not_null_p_sc>);
self.to_pbc($SUB, %context);
self.to_pbc($call<name>, %context);
}
else {
self.debug("Name is " ~ $call<name>.WHAT) if $DEBUG;
$SUB := $call<name>;
}
}
my $o := $is_tailcall ?? "tailcall_p" !! "invokecc_p";
self.debug($o) if $DEBUG;
$bc.push($OPLIB{ $o });
self.to_pbc($SUB, %context);
}
unless $is_tailcall {
self.build_pcc_call("get_results_pc", $call<results>, %context);
}
}
elsif $calltype eq 'return' {
self.build_pcc_call("set_returns_pc", $call<params>, %context);
$bc.push($OPLIB<returncc>);
}
elsif $calltype eq 'results' {
# This is generated in eceptions handling
self.build_pcc_call("get_results_pc", $call<params>, %context);
}
else {
self.panic("NYI { $calltype }");
}
}
##########
# Generating parts of Op
our multi method to_op(POST::Key $key, %context) {
self.debug("Want key") if $DEBUG;
my $key_pmc := $key.to_pmc(%context)[0];
self.debug("Got key") if $DEBUG;
# XXX PackfileConstantTable can't Keys equivalense it. So just push it.
# XXX PCC clone_key...
#my $idx := %context<constants>.push($key_pmc);
my $constants := %context<constants>;
my $idx;
Q:PIR {
.local pmc key_pmc, constants, idx
.local int i0
find_lex constants, "$constants"
find_lex key_pmc, "$key_pmc"
i0 = elements constants
constants[i0] = key_pmc
find_lex idx, "$idx"
idx = i0
store_lex "$idx", idx
};
$idx;
}
our multi method to_op(POST::Constant $op, %context) {
my $idx;
my $type := $op.type;
if $type eq 'ic' || $type eq 'kic' {
$idx := $op.value;
}
elsif $type eq 'nc' {
$idx := %context<constants>.get_or_create_number($op.value);
}
else {
self.panic("NYI");
}
self.debug("Index $idx") if $DEBUG;
$idx;
}
our multi method to_op(POST::String $str, %context) {
my $idx;
my $type := $str.type;
if $type ne 'sc' {
self.panic("attempt to pass a non-sc value off as a string");
}
if $str.encoding eq 'fixed_8' && $str.charset eq 'ascii' {
$idx := %context<constants>.get_or_create_string($str.value);
}
else {
#create a ByteBuffer and convert it to a string with the given encoding/charset
my $bb := pir::new__ps('ByteBuffer');
my $str_val := $str.value;
Q:PIR{
.local pmc str_val, bb
.local string s
str_val = find_lex '$str_val'
bb = find_lex '$bb'
s = str_val
bb = s
};
$idx := %context<constants>.get_or_create_string($bb.get_string(
$str.charset,
$str.encoding,
));
}
$idx;
}
our multi method to_op(POST::Value $val, %context) {
# Redirect to real value. POST::Value is just reference.
my $orig := self.get_register($val.name, %context);
self.to_op($orig, %context);
}
our multi method to_op(POST::Register $reg, %context) {
$reg.regno;
}
# /Emiting pbc
##########################################
##########################################
# PCC related functions
our method build_pcc_call($opname, @args, %context) {
my $bc := %context<bytecode>;
my $signature := self.build_args_signature(@args, %context);
my $sig_idx := %context<constants>.get_or_create_pmc($signature);
self.debug("Sig: $sig_idx") if $DEBUG;
self.debug($opname) if $DEBUG;
# Push signature and all args.
my @op := $opname, $sig_idx;
for @args -> $arg {
# Handle :named params
if pir::isa__ips($arg.modifier, "Hash") {
my $name := $arg.modifier<named> // $arg.name;
@op.push(
%context<constants>.get_or_create_string($name)
);
}
@op.push(self.to_op($arg, %context));
}
$bc.push(@op);
}
our method build_args_signature(@args, %context) {
my @sig;
for @args -> $arg {
# build_single_arg can return 2 values, but @a.push can't handle it
my $s := self.build_single_arg($arg, %context);
if pir::isa__ips($s, 'Integer') {
@sig.push($s);
}
else {
for $s { @sig.push($_); }
}
}
# Copy @sig into $signature
my $elements := +@sig;
my $signature := Q:PIR{
%r = find_lex '$elements'
$I99 = %r
%r = find_lex '$signature'
%r = new ['FixedIntegerArray'], $I99
};
# TODO Update nqp-setting to support .kv
my $idx := 0;
for @sig -> $val {
$signature[$idx] := $val;
$idx++;
}
$signature;
}
our method build_single_arg($arg, %context) {
# Build call signature arg according to PDD03
# POST::Value doesn't have .type. Lookup in symbols.
my $type := $arg.type // self.get_register($arg.name, %context).type;
my $res;
# Register types.
if $type eq 'i' { $res := 0 }
elsif $type eq 's' { $res := 1 }
elsif $type eq 'p' { $res := 2 }
elsif $type eq 'n' { $res := 3 }
# Constants
elsif $type eq 'ic' { $res := 0 + 0x10 }
elsif $type eq 'sc' { $res := 1 + 0x10 }
elsif $type eq 'pc' { $res := 2 + 0x10 }
elsif $type eq 'nc' { $res := 3 + 0x10 }
else { self.panic("Unknown arg type '$type'") }
my $mod := $arg.modifier;
if $mod {
if pir::isa__ips($mod, "Hash") {
# named
# First is string constant with :named flag
$res := list(0x1 + 0x10 + 0x200, $res + 0x200)
}
elsif $mod eq 'slurpy' { $res := $res + 0x20 } # 5
elsif $mod eq 'flat' { $res := $res + 0x20 } # 5
elsif $mod eq 'optional' { $res := $res + 0x80 } # 7
elsif $mod eq 'opt_flag' { $res := $res + 0x100 } # 8
elsif $mod eq 'slurpy named' { $res := $res + 0x20 + 0x200 } # 5 + 9
else { self.panic("Unsupported modifier $mod"); }
}
$res;
}
# /PCC related functions
##########################################
our method create_context($past) {
my %context;
%context<packfile> := pir::new__PS("Packfile");
# Scaffolding
# Packfile will be created with fresh directory
my $pfdir := %context<packfile>.get_directory;
# We need some constants
%context<constants> := pir::new__PS('PackfileConstantTable');
# Add PackfileConstantTable into directory.
$pfdir<CONSTANTS_hello.pir> := %context<constants>;
# Generate bytecode
%context<bytecode> := pir::new__PS('PackfileRawSegment');
# Store bytecode
$pfdir<BYTECODE_hello.pir> := %context<bytecode>;
# TODO pbc_disassemble crashes without proper debug.
# Add a debug segment.
# %context<debug> := pir::new__PS('PackfileDebug');
# Store the debug segment in bytecode
#$pfdir<BYTECODE_hello.pir_DB> := %context<debug>;
%context;
}
# XXX This is required only for PAST->POST generated tree.
our method enumerate_subs(POST::File $post) {
for @($post) -> $sub {
# XXX Should we emit warning on duplicates?
$post.sub($sub.full_name, $sub) if $sub.isa(POST::Sub);
}
}
# Declare as multi to get "static" typecheck.
our multi method create_sub_pf_flags(POST::Sub $sub) {
# This constants aren't exposed. So keep reference here.
# SUB_FLAG_IS_OUTER = PObj_private1_FLAG == 0x01
# SUB_FLAG_PF_ANON = PObj_private3_FLAG == 0x08
# SUB_FLAG_PF_MAIN = PObj_private4_FLAG == 0x10
# SUB_FLAG_PF_LOAD = PObj_private5_FLAG == 0x20
# SUB_FLAG_PF_IMMEDIATE = PObj_private6_FLAG == 0x40
# SUB_FLAG_PF_POSTCOMP = PObj_private7_FLAG == 0x80
my $res := 0;
$res := $res + 0x01 * $sub.outer;
$res := $res + 0x08 * $sub.anon;
$res := $res + 0x10 * $sub.main;
$res := $res + 0x20 * $sub.load;
$res := $res + 0x40 * $sub.immediate;
$res := $res + 0x80 * $sub.postcomp;
self.debug("pf_flags $res") if $DEBUG;
$res;
}
our multi method create_sub_comp_flags(POST::Sub $sub) {
# SUB_COMP_FLAG_VTABLE = SUB_COMP_FLAG_BIT_1 == 0x02
# SUB_COMP_FLAG_METHOD = SUB_COMP_FLAG_BIT_2 == 0x04
# SUB_COMP_FLAG_PF_INIT = SUB_COMP_FLAG_BIT_10 == 0x400
# SUB_COMP_FLAG_NSENTRY = SUB_COMP_FLAG_BIT_11 == 0x800
my $res := 0;
$res := $res + 0x002 if $sub.vtable;
$res := $res + 0x004 if $sub.is_method;
$res := $res + 0x400 if $sub.is_init;
$res := $res + 0x800 if $sub.nsentry; # XXX Check when to set ns_entry_name in .to_pbc!
self.debug("comp_flags $res") if $DEBUG;
$res;
}
our method fixup_labels($sub, $labels_todo, $bc) {
self.debug("Fixup labels") if $DEBUG;
for $labels_todo -> $kv {
my $offset := $kv.key;
my @pair := $kv.value;
self.debug("Fixing '{ @pair[0] }' from op { @pair[1] } at { $offset }") if $DEBUG;
my $delta := $sub.label(@pair[0]).position - @pair[1];
$bc[$offset] := $delta;
}
}
# Get register from symbol table with validation
our method get_register($name, %context) {
my $reg := %context<sub>.symbol($name);
if !$reg {
self.panic("Register '{ $name }' not predeclared in '{ %context<sub>.name }'");
}
$reg;
}
method debug(*@args) {
if $DEBUG {
for @args {
pir::say($_);
}
}
}
INIT {
pir::load_bytecode('nqp-setting.pbc');
}
# vim: ft=perl6