-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcompiler.cpp
More file actions
847 lines (755 loc) · 26.9 KB
/
Copy pathcompiler.cpp
File metadata and controls
847 lines (755 loc) · 26.9 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include "compiler.h"
#include "runtime.h"
static ByteCode::Enum op1(String const& func) {
if(func == Strings::add) return ByteCode::pos;
if(func == Strings::sub) return ByteCode::neg;
if(func == Strings::abs) return ByteCode::abs;
if(func == Strings::sign) return ByteCode::sign;
if(func == Strings::sqrt) return ByteCode::sqrt;
if(func == Strings::floor) return ByteCode::floor;
if(func == Strings::ceiling) return ByteCode::ceiling;
if(func == Strings::trunc) return ByteCode::trunc;
if(func == Strings::exp) return ByteCode::exp;
if(func == Strings::log) return ByteCode::log;
if(func == Strings::cos) return ByteCode::cos;
if(func == Strings::sin) return ByteCode::sin;
if(func == Strings::tan) return ByteCode::tan;
if(func == Strings::acos) return ByteCode::acos;
if(func == Strings::asin) return ByteCode::asin;
if(func == Strings::atan) return ByteCode::atan;
if(func == Strings::isna) return ByteCode::isna;
if(func == Strings::isnan) return ByteCode::isnan;
if(func == Strings::isfinite) return ByteCode::isfinite;
if(func == Strings::isinfinite) return ByteCode::isinfinite;
if(func == Strings::lnot) return ByteCode::lnot;
if(func == Strings::sum) return ByteCode::sum;
if(func == Strings::prod) return ByteCode::prod;
if(func == Strings::mean) return ByteCode::mean;
if(func == Strings::min) return ByteCode::min;
if(func == Strings::max) return ByteCode::max;
if(func == Strings::any) return ByteCode::any;
if(func == Strings::all) return ByteCode::all;
if(func == Strings::cumsum) return ByteCode::cumsum;
if(func == Strings::cumprod) return ByteCode::cumprod;
if(func == Strings::cummin) return ByteCode::cummin;
if(func == Strings::cummax) return ByteCode::cummax;
if(func == Strings::type) return ByteCode::type;
if(func == Strings::length) return ByteCode::length;
if(func == Strings::strip) return ByteCode::strip;
if(func == Strings::random) return ByteCode::random;
throw RuntimeError(std::string("unexpected symbol '") + func + "' used as a unary operator");
}
static ByteCode::Enum op2(String const& func) {
if(func == Strings::add) return ByteCode::add;
if(func == Strings::sub) return ByteCode::sub;
if(func == Strings::mul) return ByteCode::mul;
if(func == Strings::div) return ByteCode::div;
if(func == Strings::idiv) return ByteCode::idiv;
if(func == Strings::mod) return ByteCode::mod;
if(func == Strings::pow) return ByteCode::pow;
if(func == Strings::atan2) return ByteCode::atan2;
if(func == Strings::hypot) return ByteCode::hypot;
if(func == Strings::lt) return ByteCode::lt;
if(func == Strings::gt) return ByteCode::gt;
if(func == Strings::eq) return ByteCode::eq;
if(func == Strings::neq) return ByteCode::neq;
if(func == Strings::ge) return ByteCode::ge;
if(func == Strings::le) return ByteCode::le;
if(func == Strings::lor) return ByteCode::lor;
if(func == Strings::land) return ByteCode::land;
if(func == Strings::pmin) return ByteCode::pmin;
if(func == Strings::pmax) return ByteCode::pmax;
if(func == Strings::cm2) return ByteCode::cm2;
if(func == Strings::bracket) return ByteCode::subset;
if(func == Strings::bb) return ByteCode::subset2;
if(func == Strings::vector) return ByteCode::vector;
if(func == Strings::round) return ByteCode::round;
if(func == Strings::signif) return ByteCode::signif;
if(func == Strings::attrget) return ByteCode::attrget;
throw RuntimeError(std::string("unexpected symbol '") + func + "' used as a binary operator");
}
static ByteCode::Enum op3(String const& func) {
if(func == Strings::bracketAssign) return ByteCode::iassign;
if(func == Strings::bbAssign) return ByteCode::eassign;
if(func == Strings::split) return ByteCode::split;
if(func == Strings::ifelse) return ByteCode::ifelse;
if(func == Strings::seq) return ByteCode::seq;
if(func == Strings::index) return ByteCode::index;
if(func == Strings::attrset) return ByteCode::attrset;
throw RuntimeError(std::string("unexpected symbol '") + func + "' used as a trinary operator");
}
int64_t Compiler::emit(ByteCode::Enum bc, Operand a, Operand b, Operand c) {
ir.push_back(IRNode(bc, a, b, c));
return ir.size()-1;
}
void Compiler::resolveLoopExits(int64_t start, int64_t end, int64_t nextTarget, int64_t breakTarget) {
for(int64_t i = start; i < end; i++) {
if(ir[i].bc == ByteCode::jmp && ir[i].a.i == 0) {
if(ir[i].b.i == 1) {
ir[i].a.i = nextTarget-i;
} else if(ir[i].b.i == 2) {
ir[i].a.i = breakTarget-i;
}
}
}
}
Compiler::Operand Compiler::compileConstant(Value const& expr, Prototype* code) {
std::map<Value, int64_t>::const_iterator i = constants.find(expr);
int64_t index = 0;
if(i == constants.end()) {
index = code->constants.size();
code->constants.push_back(expr);
constants[expr] = index;
} else {
index = i->second;
}
return Operand(CONSTANT, index);
}
static int64_t isDotDot(String s) {
if(s != 0 && s[0] == '.' && s[1] == '.') {
int64_t v = 0;
int64_t i = 2;
// maximum 64-bit integer has 19 digits, but really who's going to pass
// that many var args?
while(i < (19+2) && s[i] >= '0' && s[i] <= '9') {
v = v*10 + (s[i] - '0');
i++;
}
if(i < (19+2) && s[i] == 0) return v;
}
return -1;
}
Compiler::Operand Compiler::compileSymbol(Value const& symbol, Prototype* code) {
String s = SymbolStr(symbol);
int64_t dd = isDotDot(s);
if(dd > 0) {
Operand t = allocRegister();
emit(ByteCode::dotdot, dd-1, 0, t);
return t;
}
else {
Operand sym = compileConstant(Character::c(s), code);
Operand t = allocRegister();
emit(ByteCode::get, sym, 0, t);
return t;
}
}
Compiler::Operand Compiler::placeInRegister(Operand r) {
if(r.loc != REGISTER && r.loc != INVALID) {
kill(r);
Operand t = allocRegister();
emit(ByteCode::fastmov, r, 0, t);
return t;
}
return r;
}
Compiler::Operand Compiler::forceInRegister(Operand r) {
if(r.loc != INVALID) {
kill(r);
Operand t = allocRegister();
emit(ByteCode::mov, r, 0, t);
return t;
}
return r;
}
CompiledCall Compiler::makeCall(List const& call, Character const& names) {
// compute compiled call...precompiles promise code and some necessary values
int64_t dotIndex = call.length()-1;
PairList arguments;
for(int64_t i = 1; i < call.length(); i++) {
Pair p;
if(names.length() > 0) p.n = names[i]; else p.n = Strings::empty;
if(isSymbol(call[i]) && SymbolStr(call[i]) == Strings::dots) {
p.v = call[i];
dotIndex = i-1;
} else if(isCall(call[i]) || isSymbol(call[i])) {
Promise::Init(p.v, NULL, Compiler::compilePromise(thread, call[i]), false);
} else {
p.v = call[i];
}
arguments.push_back(p);
}
return CompiledCall(call, arguments, dotIndex, names.length() > 0);
}
// a standard call, not an op
Compiler::Operand Compiler::compileFunctionCall(List const& call, Character const& names, Prototype* code) {
Operand function = compile(call[0], code);
CompiledCall a = makeCall(call, names);
code->calls.push_back(a);
kill(function);
Operand result = allocRegister();
if(!a.named && a.dotIndex >= (int64_t)a.arguments.size())
emit(ByteCode::fastcall, function, code->calls.size()-1, result);
else
emit(ByteCode::call, function, code->calls.size()-1, result);
return result;
}
Compiler::Operand Compiler::compileInternalFunctionCall(List const& call, Prototype* code) {
String func = SymbolStr(call[0]);
std::map<String, int64_t>::const_iterator itr = state.internalFunctionIndex.find(func);
int64_t function = -1;
if(itr == state.internalFunctionIndex.end()) {
// this should eventually be an error, but so many are unimplemented right now that we'll just make it a warning
_warning(thread, std::string("Unimplemented internal function ") + state.externStr(func));
}
else {
function = itr->second;
// check parameter count
if(state.internalFunctions[function].params != call.length()-1)
_error(std::string("Incorrect number of arguments to internal function ") + state.externStr(func));
}
// compile parameters directly...reserve registers for them.
Operand liveIn = top();
int64_t reg = liveIn.i-1;
for(int64_t i = 1; i < call.length(); i++) {
Operand r = placeInRegister(compile(call[i], code));
assert(r.i == reg+1);
reg = r.i;
}
// kill all parameters
kill(reg); // only necessary to silence unused warning
kill(liveIn);
Operand result = allocRegister();
emit(ByteCode::internal, function, result, result);
return result;
}
Compiler::Operand Compiler::compileCall(List const& call, Character const& names, Prototype* code) {
int64_t length = call.length();
if(length == 0) {
throw CompileError("invalid empty call");
}
if(!isSymbol(call[0]) && !call[0].isCharacter1())
return compileFunctionCall(call, names, code);
String func = SymbolStr(call[0]);
// list is the only built in function that handles ... or named parameters
// we only handle the list(...) case through an op for now
if(func == Strings::list && call.length() == 2
&& isSymbol(call[1]) && SymbolStr(call[1]) == Strings::dots)
{
Operand result = allocRegister();
Operand counter = placeInRegister(compile(Integer::c(0), code));
Operand storage = allocRegister();
kill(storage); kill(counter);
emit(ByteCode::dotslist, counter, storage, result);
return result;
}
// These functions can't be called directly if the arguments are named or if
// there is a ... in the args list
bool complicated = false;
for(int64_t i = 0; i < length; i++) {
if(isSymbol(call[i]) && SymbolStr(call[i]) == Strings::dots)
complicated = true;
}
if(complicated)
return compileFunctionCall(call, names, code);
// switch statement supports named args
if(func == Strings::switchSym)
{
if(call.length() == 0) _error("'EXPR' is missing");
Operand c = compile(call[1], code);
int64_t n = call.length()-2;
int64_t branch = emit(ByteCode::branch, kill(c), n, 0);
for(int64_t i = 2; i < call.length(); i++) {
Character ni = Character::c(names.length() > i ? names[i] : Strings::empty);
emit(ByteCode::branch, compileConstant(ni, code), 0, 0);
}
std::vector<int64_t> jmps;
Operand result = placeInRegister(compileConstant(Null::Singleton(), code));
jmps.push_back(emit(ByteCode::jmp, (int64_t)0, (int64_t)0, (int64_t)0));
for(int64_t i = 1; i <= n; i++) {
ir[branch+i].c = (int64_t)ir.size()-branch;
if(!call[i+1].isNil()) {
kill(result);
Operand r = placeInRegister(compile(call[i+1], code));
if(r != result)
throw CompileError(std::string("switch statement doesn't put all its results in the same register"));
if(i < n)
jmps.push_back(emit(ByteCode::jmp, (int64_t)0, (int64_t)0, (int64_t)0));
result = r;
} else if(i == n) {
kill(result);
Operand r = placeInRegister(compileConstant(Null::Singleton(), code));
if(r != result)
throw CompileError(std::string("switch statement doesn't put all its results in the same register"));
result = r;
}
}
for(int64_t i = 0; i < (int64_t)jmps.size(); i++) {
ir[jmps[i]].a = (int64_t)ir.size()-jmps[i];
ir[jmps[i]].a = (int64_t)ir.size()-jmps[i];
}
return result;
}
for(int64_t i = 0; i < length; i++) {
if(names.length() > i && names[i] != Strings::empty)
complicated = true;
}
//if(complicated)
// return compileFunctionCall(call, names, code);
if(func == Strings::internal)
{
if(!call[1].isList() || !isCall(call[1]))
throw CompileError(std::string(".Internal has invalid arguments (") + Type::toString(call[1].type()) + ")");
return compileInternalFunctionCall((List const&)call[1], code);
}
else if(func == Strings::assign ||
func == Strings::eqassign ||
func == Strings::assign2)
{
Value dest = call[1];
Operand rhs = compile(call[2], code);
// Handle simple assignment
if(!isCall(dest)) {
Operand target =
func == Strings::assign2
? compileConstant(Character::c(SymbolStr(dest)), code)
: Operand(MEMORY, SymbolStr(dest));
emit(func == Strings::assign2 ? ByteCode::assign2 : ByteCode::assign,
target, 0, rhs);
}
// Handle complex LHS assignment instructions...
// semantics here are kind of tricky. Consider compiling:
// dim(a)[1] <- x
// This is progressively turned `inside out`:
// 1) dim(a) <- `[<-`(dim(a), 1, x)
// 2) a <- `dim<-`(a, `[<-`(dim(a), 1, x))
else {
Operand tmp = Operand(MEMORY, Strings::assignTmp);
emit( ByteCode::assign, tmp, 0, rhs );
Value value = CreateSymbol(Strings::assignTmp);
while(isCall(dest)) {
List const& c = (List const&)dest;
List n(c.length()+1);
for(int64_t i = 0; i < c.length(); i++) { n[i] = c[i]; }
String as = state.internStr(state.externStr(SymbolStr(c[0])) + "<-");
n[0] = CreateSymbol(as);
n[c.length()] = value;
Character nnames(c.length()+1);
if(!hasNames(c) && (as == Strings::bracketAssign || as == Strings::bbAssign) && c.length() == 3) {
for(int64_t i = 0; i < c.length()+1; i++) { nnames[i] = Strings::empty; }
}
else {
if(hasNames(c)) {
Value names = getNames(c);
for(int64_t i = 0; i < c.length(); i++) { nnames[i] = ((Character const&)names)[i]; }
} else {
for(int64_t i = 0; i < c.length(); i++) { nnames[i] = Strings::empty; }
}
nnames[c.length()] = Strings::value;
}
value = CreateCall(n, nnames);
dest = c[1];
}
Operand target =
func == Strings::assign2
? compileConstant(Character::c(SymbolStr(dest)), code)
: Operand(MEMORY, SymbolStr(dest));
Operand source = compile(value, code);
emit(func == Strings::assign2 ? ByteCode::assign2 : ByteCode::assign,
target, 0, source);
kill( source );
Operand rm = allocRegister();
emit( ByteCode::rm, tmp, 0, rm );
kill( rm );
}
return rhs;
}
else if(func == Strings::rm && call.length() == 2)
{
Operand symbol = Operand(MEMORY, SymbolStr(call[1]));
Operand rm = allocRegister();
emit( ByteCode::rm, symbol, 0, rm );
return rm;
}
else if(func == Strings::function)
{
//compile the default parameters
assert(call[1].isList());
List const& c = (List const&)call[1];
Character names = hasNames(c) ?
(Character const&)getNames(c) : Character(0);
PairList parameters;
for(int64_t i = 0; i < c.length(); i++) {
Pair p;
if(names.length() > 0) p.n = names[i]; else p.n = Strings::empty;
if(!c[i].isNil()) {
Promise::Init(p.v, NULL, compilePromise(thread, c[i]), true);
}
else {
p.v = c[i];
}
parameters.push_back(p);
}
//compile the source for the body
Prototype* functionCode = Compiler::compileFunctionBody(thread, call[2]);
// Populate function info
functionCode->parameters = parameters;
functionCode->parametersSize = parameters.size();
functionCode->string = SymbolStr(call[3]);
functionCode->dotIndex = parameters.size();
for(int64_t i = 0; i < (int64_t)parameters.size(); i++)
if(parameters[i].n == Strings::dots) functionCode->dotIndex = i;
Value function;
Function::Init(function, functionCode, 0);
Operand funcOp = compileConstant(function, code);
Operand reg = allocRegister();
emit(ByteCode::function, funcOp, 0, reg);
return reg;
}
else if(func == Strings::returnSym)
{
Operand result;
if(call.length() == 1) {
result = compileConstant(Null::Singleton(), code);
} else if(call.length() == 2)
result = placeInRegister(compile(call[1], code));
else
throw CompileError("Too many parameters to return. Wouldn't multiple return values be nice?\n");
if(scope != FUNCTION)
throw CompileError("Attempting to return from top-level expression or from non-function. Riposte doesn't support return inside promises currently, and may never do so");
emit(ByteCode::ret, result, 0, 0);
return result;
}
else if(func == Strings::forSym)
{
Operand loop_variable = Operand(MEMORY, SymbolStr(call[1]));
Operand loop_vector = forceInRegister(compile(call[2], code));
Operand loop_counter = allocRegister(); // save space for loop counter
Operand loop_limit = allocRegister(); // save space for the loop limit
emit(ByteCode::forbegin, loop_variable, loop_vector, loop_counter);
emit(ByteCode::jmp, 0, 0, 0);
loopDepth++;
int64_t beginbody = ir.size();
Operand body = compile(call[3], code);
int64_t endbody = ir.size();
resolveLoopExits(beginbody, endbody, endbody, endbody+2);
loopDepth--;
emit(ByteCode::forend, loop_variable, loop_vector, loop_counter);
emit(ByteCode::jmp, beginbody-endbody, 0, 0);
ir[beginbody-1].a.i = endbody-beginbody+4;
kill(body); kill(loop_limit); kill(loop_variable);
kill(loop_counter); kill(loop_vector);
return compileConstant(Null::Singleton(), code);
}
else if(func == Strings::whileSym)
{
Operand head_condition = compile(call[1], code);
emit(ByteCode::jc, 1, 0, kill(head_condition));
loopDepth++;
int64_t beginbody = ir.size();
Operand body = compile(call[2], code);
kill(body);
int64_t tail = ir.size();
Operand tail_condition = compile(call[1], code);
int64_t endbody = ir.size();
emit(ByteCode::jc, beginbody-endbody, 1, kill(tail_condition));
resolveLoopExits(beginbody, endbody, tail, endbody+1);
ir[beginbody-1].b = endbody-beginbody+2;
loopDepth--;
return compileConstant(Null::Singleton(), code);
}
else if(func == Strings::repeatSym)
{
loopDepth++;
int64_t beginbody = ir.size();
Operand body = compile(call[1], code);
int64_t endbody = ir.size();
resolveLoopExits(beginbody, endbody, endbody, endbody+1);
loopDepth--;
emit(ByteCode::jmp, beginbody-endbody, 0, 0);
kill(body);
return compileConstant(Null::Singleton(), code);
}
else if(func == Strings::nextSym)
{
if(loopDepth == 0) throw CompileError("next used outside of loop");
emit(ByteCode::jmp, 0, 1, 0);
return Operand(INVALID, (int64_t)0);
}
else if(func == Strings::breakSym)
{
if(loopDepth == 0) throw CompileError("break used outside of loop");
emit(ByteCode::jmp, 0, 2, 0);
return Operand(INVALID, (int64_t)0);
}
else if(func == Strings::ifSym)
{
Operand resultT, resultF;
if(call.length() != 3 && call.length() != 4)
throw CompileError("invalid if statement");
// resultT and resultF need to be in the same register so if statements can be used as expressions.
// if either one is not in a register move into register. They should be in the same register at
// the end. They could also be in the same MEMORY location at the end, but that's harder to code
// gen for...
Operand cond = compile(call[1], code);
emit(ByteCode::jc, 1, 0, kill(cond));
int64_t begin1 = ir.size(), begin2 = 0;
resultT = placeInRegister(compile(call[2], code));
emit(ByteCode::jmp, (int64_t)0, (int64_t)0, (int64_t)0);
begin2 = ir.size();
kill(resultT);
resultF = placeInRegister(
call.length() == 4 ? compile(call[3], code) :
compileConstant(Null::Singleton(), code) );
assert(resultT == resultF || resultT.loc == INVALID || resultF.loc == INVALID);
int64_t end = ir.size();
ir[begin2-1].a = end-begin2+1;
ir[begin1-1].b = begin2-begin1+1;
return resultT.loc == INVALID ? resultF : resultT;
}
else if(func == Strings::lor2 && call.length() == 3)
{
Operand r0 = placeInRegister(compileConstant(Logical::True(), code));
Operand left = compile(call[1], code);
emit(ByteCode::jc, 0, 1, kill(left));
int64_t j1 = ir.size()-1;
Operand right = compile(call[2], code);
emit(ByteCode::jc, 2, 1, kill(right));
kill(r0);
Operand r1 = placeInRegister(compileConstant(Logical::False(), code));
ir[j1].a = (int64_t)ir.size()-j1;
assert(r0 == r1 || r0.loc == INVALID || r1.loc == INVALID);
return r1;
}
else if(func == Strings::land2 && call.length() == 3)
{
Operand r0 = placeInRegister(compileConstant(Logical::False(), code));
Operand left = compile(call[1], code);
emit(ByteCode::jc, 1, 0, kill(left));
int64_t j1 = ir.size()-1;
Operand right = compile(call[2], code);
emit(ByteCode::jc, 1, 2, kill(right));
kill(r0);
Operand r1 = placeInRegister(compileConstant(Logical::True(), code));
ir[j1].b = (int64_t)ir.size()-j1;
assert(r0 == r1 || r0.loc == INVALID || r1.loc == INVALID);
return r1;
}
else if(func == Strings::brace)
{
int64_t length = call.length();
if(length <= 1) {
return compileConstant(Null::Singleton(), code);
} else {
Operand result;
for(int64_t i = 1; i < length; i++) {
// memory results need to be forced to handle things like:
// function(x,y) { x; y }
// if x is a promise, it must be forced
if(result.loc == MEMORY) {
result = placeInRegister(result);
}
kill(result);
result = compile(call[i], code);
}
return result;
}
}
else if(func == Strings::paren)
{
return compile(call[1], code);
}
else if(func == Strings::list)
{
Operand f, r;
for(int64_t i = 1; i < call.length(); i++) {
Operand t = forceInRegister(compile(call[i], code));
if(f.loc == INVALID) { f = t; r = t; }
else { assert(t.i == r.i+1); r = t; }
}
if(call.length() > 1)
kill(f);
Operand result = allocRegister();
emit(ByteCode::list, f, Operand((int64_t)call.length()-1), result);
return result;
}
// Trinary operators
else if((func == Strings::bracketAssign ||
func == Strings::bbAssign ||
func == Strings::split ||
func == Strings::ifelse ||
func == Strings::seq ||
func == Strings::index ||
func == Strings::attrset) &&
call.length() == 4) {
Operand c = placeInRegister(compile(call[1], code));
Operand b = compile(call[2], code);
Operand a = compile(call[3], code);
kill(a); kill(b); kill(c);
Operand result = allocRegister();
assert(c == result);
emit(op3(func), a, b, result);
return result;
}
// Binary operators
else if((func == Strings::add ||
func == Strings::sub ||
func == Strings::mul ||
func == Strings::div ||
func == Strings::idiv ||
func == Strings::pow ||
func == Strings::mod ||
func == Strings::atan2 ||
func == Strings::hypot ||
func == Strings::eq ||
func == Strings::neq ||
func == Strings::lt ||
func == Strings::gt ||
func == Strings::ge ||
func == Strings::le ||
func == Strings::pmin ||
func == Strings::pmax ||
func == Strings::cm2 ||
func == Strings::lor ||
func == Strings::land ||
func == Strings::bracket ||
func == Strings::bb ||
func == Strings::vector ||
func == Strings::round ||
func == Strings::signif ||
func == Strings::attrget) &&
call.length() == 3)
{
Operand a = compile(call[1], code);
Operand b = compile(call[2], code);
kill(b); kill(a);
Operand result = allocRegister();
emit(op2(func), a, b, result);
return result;
}
// Unary operators
else if((func == Strings::add ||
func == Strings::sub ||
func == Strings::lnot ||
func == Strings::abs ||
func == Strings::sign ||
func == Strings::sqrt ||
func == Strings::floor ||
func == Strings::ceiling ||
func == Strings::trunc ||
func == Strings::exp ||
func == Strings::log ||
func == Strings::cos ||
func == Strings::sin ||
func == Strings::tan ||
func == Strings::acos ||
func == Strings::asin ||
func == Strings::atan ||
func == Strings::isna ||
func == Strings::isnan ||
func == Strings::isfinite ||
func == Strings::isinfinite ||
func == Strings::sum ||
func == Strings::prod ||
func == Strings::mean ||
func == Strings::min ||
func == Strings::max ||
func == Strings::any ||
func == Strings::all ||
func == Strings::cumsum ||
func == Strings::cumprod ||
func == Strings::cummin ||
func == Strings::cummax ||
func == Strings::type ||
func == Strings::length ||
func == Strings::strip ||
func == Strings::random) &&
call.length() == 2)
{
// if there isn't exactly one parameter, we should call the library version...
Operand a = compile(call[1], code);
kill(a);
Operand result = allocRegister();
emit(op1(func), a, 0, result);
return result;
}
else if(func == Strings::missing)
{
if(call.length() != 2) _error("missing requires one argument");
if(!isSymbol(call[1]) && !call[1].isCharacter1()) _error("wrong parameter to missing");
Operand s = compileConstant(call[1], code);
Operand result = allocRegister();
emit(ByteCode::missing, s, 0, result);
return result;
}
else if(func == Strings::quote)
{
if(call.length() != 2) _error("quote requires one argument");
return compileConstant(call[1], code);
}
return compileFunctionCall(call, names, code);
}
Compiler::Operand Compiler::compileExpression(List const& values, Prototype* code) {
Operand result;
if(values.length() == 0) result = compileConstant(Null::Singleton(), code);
for(int64_t i = 0; i < values.length(); i++) {
// memory results need to be forced to handle things like:
// function(x,y) { x; y }
// if x is a promise, it must be forced
if(result.loc == MEMORY) {
result = placeInRegister(result);
}
kill(result);
result = compile(values[i], code);
}
return result;
}
Compiler::Operand Compiler::compile(Value const& expr, Prototype* code) {
if(isSymbol(expr)) {
return compileSymbol(expr, code);
}
if(isExpression(expr)) {
assert(expr.isList());
return compileExpression((List const&)expr, code);
}
else if(isCall(expr)) {
assert(expr.isList());
return compileCall((List const&)expr,
hasNames((List const&)expr) ?
(Character const&)getNames((List const&)expr) :
Character(0),
code);
}
else {
return compileConstant(expr, code);
}
}
void Compiler::dumpCode() const {
for(size_t i = 0; i < ir.size(); i++) {
std::cout << ByteCode::toString(ir[i].bc) << "\t" << ir[i].a.toString() << "\t" << ir[i].b.toString() << "\t" << ir[i].c.toString() << std::endl;
}
}
// generate actual code from IR as follows...
// MEMORY and INTEGER operands unchanged
// CONSTANT operands placed in lower N registers (starting at 0)
// REGISTER operands placed in above those
// all register ops encoded with negative integer.
// INVALID operands just go to 0 since they will never be used
int64_t Compiler::encodeOperand(Operand op, int64_t n) const {
if(op.loc == MEMORY || op.loc == INTEGER) return op.i;
else if(op.loc == CONSTANT) return (op.i+1);
else if(op.loc == REGISTER) return -(op.i);
else return 0;
}
Prototype* Compiler::compile(Value const& expr) {
Prototype* code = new Prototype();
assert(((int64_t)code) % 16 == 0); // our type packing assumes that this is true
Operand result = compile(expr, code);
// insert appropriate termination statement at end of code
if(scope == FUNCTION)
emit(ByteCode::ret, result, 0, 0);
else if(scope == PROMISE)
emit(ByteCode::retp, result, 0, 0);
else { // TOPLEVEL
emit(ByteCode::rets, result, 0, 0);
emit(ByteCode::done, 0, 0, 0);
}
//std::reverse(code->constants.begin(), code->constants.end());
code->expression = expr;
code->registers = code->constants.size() + max_n;
int64_t n = code->constants.size();
for(size_t i = 0; i < ir.size(); i++) {
code->bc.push_back(Instruction(ir[i].bc, encodeOperand(ir[i].a, n), encodeOperand(ir[i].b, n), encodeOperand(ir[i].c, n)));
}
return code;
}