/
CodeGen.pm
498 lines (421 loc) · 14.4 KB
/
CodeGen.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
use strict;
use warnings;
use 5.010;
use CgOpToCLROp;
use CLRTypes;
{
package CodeGen;
use Moose;
has ops => (is => 'ro', required => 1);
has csname => (isa => 'Str', is => 'ro');
has minlets => (isa => 'Int', is => 'ro', default => 0);
has usednamed => (isa => 'Bool', is => 'ro', default => 0);
has numlabels => (isa => 'Int', is => 'rw', default => 1);
has numips => (isa => 'Int', is => 'rw', default => 1);
has lineinfo => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
has resulttype=>(isa => 'Maybe[Str]', is => 'rw', default => '');
has labelname => (isa => 'HashRef', is => 'ro', default => sub { +{} });
has buffer => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
has unreach => (isa => 'Bool', is => 'rw', default => 0);
has outcap => (isa => 'Bool', is => 'rw', default => 0);
has letstack => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
has letused => (isa => 'HashRef', is => 'ro', default => sub { +{} });
has consttab => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
has linestack => (isa => 'ArrayRef', is => 'ro', default => sub { [0] });
has ehspans => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
has ehlabels => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
# These are the sub-primitives. Their very inteface exposes volatile
# details of the codegen.
sub qm {
my $out = $_[0];
$out =~ s/"/""/g;
"@\"$out\"";
}
sub _odds {
my @o; for (my $i = 1; $i < @_; $i+=2 ) { push @o, $_[$i] } @o
}
sub _emit {
my ($self, $line) = @_;
push @{ $self->buffer }, " $line;\n";
}
sub _cpscall {
my ($self, $rt, $expr) = @_;
my $n = $self->ip;
$self->_emit("th.ip = $n");
$self->_emit("return $expr");
$self->lineinfo->[$n] = $self->linestack->[-1];
push @{ $self->buffer }, "case $n:\n";
die "Broken call $expr" if !defined($rt);
$self->resulttype($rt);
}
# These functions are usable from user code, but still depend on volatiles.
sub result {
my ($self) = @_;
$self->cast($self->resulttype, "object", "th.resultSlot");
}
sub set_result {
my ($self, $ty, $it) = @_;
$self->resulttype($ty);
$self->_emit("th.resultSlot = $it");
}
sub _lexn {
my ($which) = @_;
($which < 4) ? ("th.lex$which") : ("th.lexn[" . ($which - 4) . "]");
}
sub push_line {
my ($self, $line) = @_;
push @{ $self->linestack }, $line;
}
sub pop_line {
my ($self) = @_;
pop @{ $self->linestack };
}
sub lexnsize {
my ($self) = @_;
my $i = 0;
for (keys %{ $self->letused }) { $i++ if $_ >= 6 }
$i;
}
sub push_let {
my ($self, $which, $ty, $v) = @_;
my $ls = $self->letstack;
my $lu = $self->letused;
my $i;
for ($i = 0; ; $i++) {
last if !$lu->{$i} &&
($i >= 2 || $ty eq 'Int32' || $ty eq 'System.Int32');
}
my $ph = ($i < 2) ? "th.lexi$i" : _lexn($i-2);
my $pht = ($i < 2) ? $ty : "object";
push @$ls, [ $which, $ph, $pht, $ty, $i ];
$lu->{$i} = 1;
$self->_emit("$ph = $v");
}
sub drop_let {
my ($self, $which) = @_;
die "Let consistency error" if $which ne $self->letstack->[-1][0];
my $r = pop @{ $self->letstack };
$self->letused->{ $r->[4] } = 0;
}
sub peek_let {
my ($self, $which) = @_;
my $i = @{ $self->letstack } - 1;
while ($i >= 0 && $self->letstack->[$i][0] ne $which) { $i-- }
my $k = $self->letstack->[$i];
($k->[2] eq $k->[3]) ? (@$k[2,1]) : $self->cast(@$k[3,2,1]);
}
sub poke_let {
my ($self, $which, $ty, $v) = @_;
my $i = @{ $self->letstack } - 1;
while ($i >= 0 && $self->letstack->[$i][0] ne $which) { $i-- }
$self->_emit($self->letstack->[$i][1] . " = $v");
}
sub const {
my ($self, $type, $val) = @_;
my $knum = @{ $self->consttab };
my $name = "K_" . $self->csname . "_$knum";
push @{ $self->consttab }, "$type $name = $val";
$type, $name;
}
sub label {
my ($self) = @_;
my $n = $self->numlabels;
$self->numlabels($n + 1);
return $n;
}
sub ip {
my ($self) = @_;
my $n = $self->numips;
$self->numips($n + 1);
return $n;
}
sub labelhere {
my ($self, $n) = @_;
my $ip = $self->labelname->{$n} = $self->ip;
push @{ $self->buffer }, " goto case $ip;\n" unless $self->unreach;
push @{ $self->buffer }, "case $ip:\n";
$self->lineinfo->[$ip] = $self->linestack->[-1];
$self->unreach(0);
}
sub goto {
my ($self, $n) = @_;
push @{ $self->buffer }, " goto case \@\@L$n;\n";
$self->unreach(1);
}
sub cgoto {
my ($self, $n, $ty, $top) = @_;
push @{ $self->buffer }, " if ($top) { goto case \@\@L$n; }\n";
}
sub ncgoto {
my ($self, $n, $ty, $top) = @_;
push @{ $self->buffer }, " if (!$top) { goto case \@\@L$n; }\n";
}
sub ehspan {
my ($self, $type, $lab, $lidl, $ls, $le, $lg) = @_;
my $lid = -1;
if (defined $lab) {
$lid = @{ $self->ehlabels };
$self->labelname->{$lidl} = $lid if defined $lidl;
}
push @{ $self->ehspans }, "\@\@L$ls", "\@\@L$le", $type,
"\@\@L$lg", $lid;
}
sub hintget {
my ($self, $type, $data, $name) = @_;
$self->cast($type, "object", "$data.hints[" . qm($name) . "]");
}
sub rtpadget {
my ($self, $type, $order, $name) = @_;
$self->cast($type, "object", "th" . (".outer" x $order) . ".lex[" . qm($name) . "]");
}
sub rtpadput {
my ($self, $order, $name, $ty, $val) = @_;
$self->_emit("th" . (".outer" x $order) . ".lex[" . qm($name) . "] = $val");
}
sub rtpadgeti {
my ($self, $type, $order, $name) = @_;
my $tag = $name < 4 ? "lex$name" : ("lexn[" . ($name - 4) . "]");
$self->cast($type, "object", "th" . (".outer" x $order) . ".$tag");
}
sub rtpadputi {
my ($self, $order, $name, $type, $val) = @_;
my $tag = $name < 4 ? "lex$name" : ("lexn[" . ($name - 4) . "]");
$self->_emit("th" . (".outer" x $order) . ".$tag = $val");
}
sub callframe {
my ($self) = @_;
"Frame", "th";
}
sub drop {
my ($self, $ty, $val) = @_;
$self->_emit($val);
}
sub pos {
my ($self, $vty, $val) = @_;
'Variable', "th.pos[$val]";
}
sub _prepcall {
my ($self, $sig, @args) = @_;
my $quick = 1;
for (@$sig) { $quick &&= ($_ eq '') }
my ($inv, @vals) = _odds(@args);
if ($quick) {
return $inv, "new Variable[] { " . join(", ", @vals) . "}", "null";
} else {
# TODO: optimize harder
$self->outcap(1);
$self->_emit("_inv = $inv");
$self->_emit("_pos = new List<Variable>()");
$self->_emit("_nam = new Dictionary<string,Variable>()");
for (my $ix = 0; $ix < @$sig; $ix++) {
if ($sig->[$ix] eq '') {
$self->_emit("_pos.Add($vals[$ix])");
} elsif (substr($sig->[$ix],0,1) eq ':') {
my $n = qm(substr($sig->[$ix],1));
$self->_emit("_nam[$n] = $vals[$ix]");
} elsif ($sig->[$ix] eq 'flatpos') {
$self->_emit("_pos.AddRange($vals[$ix])");
} elsif ($sig->[$ix] eq 'flatnam') {
$self->_emit("Kernel.AddMany(_nam, $vals[$ix])");
} else {
die "weird sig bit $sig->[$ix]";
}
}
return "_inv", "_pos.ToArray()", "_nam";
}
}
sub call_method {
my ($self, $name, $sig, @args) = @_;
my ($inv, $pos, $nam) = $self->_prepcall($sig, @args);
$self->_cpscall('Variable', "$inv.InvokeMethod(th, ".qm($name).", $pos, $nam)");
}
sub call_sub {
my ($self, $sig, @args) = @_;
my ($inv, $pos, $nam) = $self->_prepcall($sig, @args);
$self->_cpscall('Variable', "$inv.Invoke(th, $pos, $nam)");
}
sub tail_call_sub {
my ($self, $sig, @args) = @_;
my ($inv, $pos, $nam) = $self->_prepcall($sig, @args);
$self->_emit("return $inv.Invoke(th.caller, $pos, $nam)");
$self->unreach(1);
}
sub clr_bool {
my ($self, $v) = @_;
'System.Boolean', $v ? 'true' : 'false';
}
sub clr_new {
my ($self, $class, @args) = @_;
$class, "new $class(" . join(", ", _odds @args) . ")";
}
sub clr_new_arr {
my ($self, $class, @args) = @_;
$class . "[]", "new $class []{" . join(", ", _odds @args) . "}";
}
sub clr_new_zarr {
my ($self, $class, $nity, $nitems) = @_;
$class . "[]", "(new $class [$nitems])";
}
sub clr_string {
my ($self, $text) = @_;
'System.String', qm($text);
}
sub clr_char {
my ($self, $val) = @_;
'Char', "((char)" . ord($val) . ")";
}
sub clr_int {
my ($self, $val) = @_;
'Int32', $val;
}
sub clr_double {
my ($self, $val) = @_;
'Double', "((Double)$val)";
}
sub labelid {
my ($self, $lbl) = @_;
'Int32', "\@\@L$lbl";
}
sub clr_arith {
my ($self, $op, $ty1, $a1, $ty2, $a2) = @_;
if ($ty1 ne $ty2) {
die "Overloaded operations not yet supported";
}
$ty1, "($a1 $op $a2)";
}
sub clr_compare {
my ($self, $op, $ty1, $a1, $ty2, $a2) = @_;
'Boolean', "($a1 $op $a2)";
}
sub clr_field_get {
my ($self, $f, $oty, $obj) = @_;
my ($nm, $cl, $ty) = CLRTypes->info('f', $oty, $f);
$ty, "($obj.$nm)";
}
sub clr_field_set {
my ($self, $f, $oty, $obj, $vty, $val) = @_;
$self->_emit("$obj.$f = $val");
}
sub clr_sfield_get {
my ($self, $f) = @_;
my ($nm, $cl, $ty) = CLRTypes->info('f', $f);
$ty, "$nm";
}
sub clr_sfield_set {
my ($self, $f, $vty, $val) = @_;
$self->_emit(CLRTypes->strip($f) . " = $val");
}
sub clr_index_get {
my ($self, $ixty, $ix, $oty, $obj) = @_;
my ($nm, $cl, $ty) = CLRTypes->info('i', $oty, 'Item');
$ty, "($obj" . "[$ix])";
}
sub clr_index_set {
my ($self, $ixty, $ix, $oty, $obj, $vty, $val) = @_;
$self->_emit("$obj" . "[$ix] = ($val)");
}
sub cast {
my ($self, $type, $vty, $val) = @_;
$type, "(($type)$val)";
}
sub clr_call_direct {
my ($self, $name, @args) = @_;
my ($nm, $cl, $rt) = CLRTypes->info('cm', $name);
if ($cl eq 'c') {
$self->_cpscall($rt,
"$nm(" . join(", ", "th", _odds @args) . ")");
} elsif ($rt ne 'Void') {
return ($rt, "$nm(" . join(", ", _odds @args) . ")");
} else {
$self->_emit("$nm(" . join(", ", _odds @args) . ")");
}
}
sub clr_call_virt {
my ($self, $name, $ity, $inv, @args) = @_;
my ($nm, $cl, $rt) = CLRTypes->info('cm', $ity, $name);
if ($cl eq 'c') {
$self->_cpscall($rt,
"$inv.$nm(" . join(", ", "th", _odds @args) . ")");
} elsif ($rt ne 'Void') {
return ($rt, "$inv.$nm(" . join(", ", _odds @args) . ")");
} else {
$self->_emit("$inv.$nm(" . join(", ", _odds @args) . ")");
}
}
sub rxbprim {
my ($self, $name, @args) = @_;
$self->_emit("if (!th.rx.$name(" . join(", ", _odds @args) . ")) goto case \@\@Lbacktrack");
}
sub rxpushb {
my ($self, $tag, $label) = @_;
push @{ $self->buffer }, " th.rx.PushBacktrack(\@\@L$label);\n";
}
sub return {
my ($self, $nv, $rty, $rval) = @_;
return if $self->unreach;
if ($nv) {
$self->_emit("th.caller.resultSlot = $rval");
}
$self->_emit("return th.caller");
$self->unreach(1);
}
sub push_null {
my ($self, $ty) = @_;
# (Int32)null isn't liked much
$ty, $ty eq 'Int32' ? "0" : "null";
}
###
sub subinfo_ctor_args {
my ($self, $outersi, $ltm) = @_;
for (@{ $self->buffer }, @{ $self->consttab }, @{ $self->ehspans }) {
s/\@\@L(\w+)/$self->labelname->{$1}/eg;
}
(CgOp::clr_string("$::UNITNAME " . $self->csname),
CgOp::rawnewarr('int', map { CgOp::int($_//0) } @{ $self->lineinfo }),
CgOp::rawsget($::UNITNAME . '.' . $self->csname . ':f,DynBlockDelegate'),
$outersi, $ltm,
CgOp::rawnewarr('int', map { CgOp::int($_) } @{ $self->ehspans }),
(@{ $self->ehlabels } ? CgOp::rawnewarr('str',
map { CgOp::clr_string($_) } @{ $self->ehlabels }) :
CgOp::null('clr:string[]')));
}
sub csharp {
my ($self) = @_;
my $t = '';
my $name = $self->csname;
$t .= " " x 4 . "public static Frame $name(Frame th) {\n";
if ($self->outcap) {
$t .= " " x 8 . "IP6 _inv; List<Variable> _pos; Dictionary<string,Variable> _nam;\n";
}
$t .= " " x 8 . "switch (th.ip) {\n";
$t .= " " x 12 . "case 0:\n";
if ($self->lexnsize > 0) {
$t .= " " x 16 . "th.lexn = new object[" . $self->lexnsize . "];\n";
}
if ($self->usednamed) {
$t .= " " x 16 . "if (th.lex == null) th.lex = new Dictionary<string,object>();\n";
}
for (@{ $self->buffer }, @{ $self->consttab }, @{ $self->ehspans }) {
s/\@\@L(\w+)/$self->labelname->{$1}/eg;
}
$t .= " " x 12 . $_ for @{ $self->buffer };
$t .= " " x 12 . "default:\n";
$t .= " " x 16 . "return Kernel.Die(th, \"Invalid IP\");\n";
$t .= " " x 8 . "}\n";
$t .= " " x 4 . "}\n";
for (@{ $self->consttab }) {
$t .= " " x 4 . "private static $_;\n";
}
$t;
}
sub BUILD {
my $self = shift;
for (my $i = 0; $i < $self->minlets; $i++) {
$self->letused->{$i + 2} = 1;
}
CgOpToCLROp::codegen($self, $self->ops);
}
__PACKAGE__->meta->make_immutable;
no Moose;
}
1;