forked from jckarter/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.cpp
4905 lines (4290 loc) · 159 KB
/
codegen.cpp
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "clay.hpp"
#include "evaluator.hpp"
#include "codegen.hpp"
#include "externals.hpp"
#include "loader.hpp"
#include "operators.hpp"
#include "lambdas.hpp"
#include "analyzer.hpp"
#include "invoketables.hpp"
#include "literals.hpp"
#include "desugar.hpp"
#include "constructors.hpp"
#include "parser.hpp"
#include "env.hpp"
#include "objects.hpp"
#include "error.hpp"
#include "int128.hpp"
#include "codegen_op.hpp"
#include "codegen.hpp"
#pragma clang diagnostic ignored "-Wcovered-switch-default"
namespace clay {
llvm::Module *llvmModule = NULL;
llvm::DIBuilder *llvmDIBuilder = NULL;
llvm::ExecutionEngine *llvmEngine;
const llvm::DataLayout *llvmDataLayout;
static vector<CValuePtr> initializedGlobals;
static CodegenContext *constructorsCtx = NULL;
static CodegenContext *destructorsCtx = NULL;
static bool isMsvcTarget() {
llvm::Triple target(llvmModule->getTargetTriple());
return (target.getOS() == llvm::Triple::Win32);
}
llvm::PointerType *exceptionReturnType() {
return llvmPointerType(int8Type);
}
llvm::Value *noExceptionReturnValue() {
return llvm::ConstantPointerNull::get(exceptionReturnType());
}
void codegenValueInit(CValuePtr dest, CodegenContext* ctx);
void codegenValueDestroy(CValuePtr dest, CodegenContext* ctx);
void codegenStackEntryDestroy(ValueStackEntry const &entry,
CodegenContext* ctx,
bool exception);
void codegenValueCopy(CValuePtr dest, CValuePtr src, CodegenContext* ctx);
void codegenValueMove(CValuePtr dest, CValuePtr src, CodegenContext* ctx);
void codegenValueAssign(CValuePtr dest, CValuePtr src, CodegenContext* ctx);
void codegenValueMoveAssign(CValuePtr dest,
CValuePtr src,
CodegenContext* ctx);
llvm::Value *codegenToBoolFlag(CValuePtr a, CodegenContext* ctx);
size_t cgMarkStack(CodegenContext* ctx);
void cgDestroyStack(size_t marker, CodegenContext* ctx, bool exception);
void cgPopStack(size_t marker, CodegenContext* ctx);
void cgDestroyAndPopStack(size_t marker, CodegenContext* ctx, bool exception);
void cgPushStackValue(CValuePtr cv, CodegenContext* ctx);
void cgPushStackStatement(ValueStackEntryType type,
EnvPtr env,
StatementPtr statement,
CodegenContext* ctx);
CValuePtr codegenAllocValue(TypePtr t, CodegenContext* ctx);
CValuePtr codegenAllocNewValue(TypePtr t, CodegenContext* ctx);
CValuePtr codegenAllocValueForPValue(PVData const &pv, CodegenContext* ctx);
MultiCValuePtr codegenMultiArgsAsRef(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx);
MultiCValuePtr codegenArgExprAsRef(ExprPtr x,
EnvPtr env,
CodegenContext* ctx);
CValuePtr codegenForwardOneAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx);
MultiCValuePtr codegenForwardMultiAsRef(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx);
MultiCValuePtr codegenForwardExprAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx);
CValuePtr codegenOneAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx);
MultiCValuePtr codegenMultiAsRef(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx);
MultiCValuePtr codegenExprAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx);
void codegenOneInto(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
CValuePtr out);
void codegenMultiInto(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out,
size_t count);
void codegenExprInto(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenMulti(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out,
size_t count);
void codegenOne(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
CValuePtr out);
void codegenExpr(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenStaticObject(ObjectPtr x,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenGVarInstance(GVarInstancePtr x);
void codegenExternalVariable(ExternalVariablePtr x);
void codegenExternalProcedure(ExternalProcedurePtr x, bool codegenBody);
void codegenValueHolder(ValueHolderPtr x,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCompileTimeValue(EValuePtr ev,
CodegenContext* ctx,
MultiCValuePtr out);
llvm::Value *codegenSimpleConstant(EValuePtr ev);
void codegenIndexingExpr(ExprPtr indexable,
ExprListPtr args,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenAliasIndexing(GlobalAliasPtr x,
ExprListPtr args,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCallExpr(ExprPtr callable,
ExprListPtr args,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenDispatch(ObjectPtr obj,
MultiCValuePtr args,
MultiPValuePtr pvArgs,
llvm::ArrayRef<unsigned> dispatchIndices,
CodegenContext* ctx,
MultiCValuePtr out);
bool codegenShortcut(ObjectPtr callable,
MultiPValuePtr pvArgs,
ExprListPtr args,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCallPointer(CValuePtr x,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCallCCode(CCodePointerTypePtr type,
llvm::Value *llCallable,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCallCCodePointer(CValuePtr x,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCallCode(InvokeEntry* entry,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenLowlevelCall(llvm::Value *llCallable,
llvm::ArrayRef<llvm::Value *> args,
CodegenContext* ctx);
void codegenCallInline(InvokeEntry* entry,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCallByName(InvokeEntry* entry,
ExprPtr callable,
ExprListPtr args,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out);
void codegenCodeBody(InvokeEntry* entry);
void codegenCWrapper(InvokeEntry* entry, CallingConv cc);
bool codegenStatement(StatementPtr stmt,
EnvPtr env,
CodegenContext* ctx);
void codegenCollectLabels(llvm::ArrayRef<StatementPtr> statements,
unsigned startIndex,
CodegenContext* ctx);
EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx);
void codegenExprAssign(ExprPtr left,
CValuePtr cvRight,
PVData const &pvRight,
EnvPtr env,
CodegenContext* ctx);
void codegenMultiExprAssign(ExprListPtr left,
MultiCValuePtr mcvRight,
MultiPValuePtr mpvRight,
EnvPtr env,
CodegenContext* ctx);
//
// flags for inline/exceptions
//
static bool _inlineEnabled = true;
static bool _exceptionsEnabled = true;
bool inlineEnabled()
{
return _inlineEnabled;
}
void setInlineEnabled(bool enabled)
{
_inlineEnabled = enabled;
}
bool exceptionsEnabled()
{
return _exceptionsEnabled;
}
void setExceptionsEnabled(bool enabled)
{
_exceptionsEnabled = enabled;
}
//
// utility procs
//
llvm::BasicBlock *newBasicBlock(llvm::StringRef name, CodegenContext* ctx)
{
return llvm::BasicBlock::Create(llvm::getGlobalContext(),
name,
ctx->llvmFunc);
}
CValuePtr staticCValue(ObjectPtr obj, CodegenContext* ctx)
{
TypePtr t = staticType(obj);
if (ctx->valueForStatics == NULL)
ctx->valueForStatics = ctx->initBuilder->CreateAlloca(llvmType(t));
return new CValue(t, ctx->valueForStatics);
}
static CValuePtr derefValue(CValuePtr cvPtr, CodegenContext* ctx)
{
assert(cvPtr->type->typeKind == POINTER_TYPE);
PointerType *pt = (PointerType *)cvPtr->type.ptr();
llvm::Value *ptrValue = ctx->builder->CreateLoad(cvPtr->llValue);
return new CValue(pt->pointeeType, ptrValue);
}
static CValuePtr derefValueForPValue(CValuePtr cv, PVData const &pv, CodegenContext* ctx)
{
if (pv.isRValue) {
return cv;
} else {
assert(cv->type == pointerType(pv.type));
return derefValue(cv, ctx);
}
}
//
// codegen value ops
//
void codegenValueInit(CValuePtr dest, CodegenContext* ctx)
{
if (isPrimitiveAggregateType(dest->type))
return;
codegenCallValue(staticCValue(dest->type.ptr(), ctx),
new MultiCValue(),
ctx,
new MultiCValue(dest));
}
void codegenValueDestroy(CValuePtr dest, CodegenContext* ctx)
{
if (isPrimitiveAggregateType(dest->type))
return;
bool savedCheckExceptions = ctx->checkExceptions;
ctx->checkExceptions = false;
codegenCallValue(staticCValue(operator_destroy(), ctx),
new MultiCValue(dest),
ctx,
new MultiCValue());
ctx->checkExceptions = savedCheckExceptions;
}
void codegenStackEntryDestroy(ValueStackEntry const &entry,
CodegenContext* ctx,
bool exception)
{
switch (entry.type) {
case LOCAL_VALUE:
codegenValueDestroy(entry.value, ctx);
break;
case FINALLY_STATEMENT: {
bool savedCheckExceptions = ctx->checkExceptions;
ctx->checkExceptions = false;
codegenStatement(entry.statement, entry.statementEnv, ctx);
ctx->checkExceptions = savedCheckExceptions;
}
break;
case ONERROR_STATEMENT: {
if (exception) {
bool savedCheckExceptions = ctx->checkExceptions;
ctx->checkExceptions = false;
codegenStatement(entry.statement, entry.statementEnv, ctx);
ctx->checkExceptions = savedCheckExceptions;
}
}
break;
}
}
void codegenValueCopy(CValuePtr dest, CValuePtr src, CodegenContext* ctx)
{
if (isPrimitiveAggregateType(dest->type)
&& (dest->type == src->type)
&& (!isPrimitiveAggregateTooLarge(dest->type)))
{
if (dest->type->typeKind != STATIC_TYPE) {
llvm::Value *v = ctx->builder->CreateLoad(src->llValue);
ctx->builder->CreateStore(v, dest->llValue);
}
return;
}
codegenCallValue(staticCValue(operator_copy(), ctx),
new MultiCValue(src),
ctx,
new MultiCValue(dest));
}
void codegenValueMove(CValuePtr dest, CValuePtr src, CodegenContext* ctx)
{
if (isPrimitiveAggregateType(dest->type)
&& (dest->type == src->type)
&& (!isPrimitiveAggregateTooLarge(dest->type)))
{
if (dest->type->typeKind != STATIC_TYPE) {
llvm::Value *v = ctx->builder->CreateLoad(src->llValue);
ctx->builder->CreateStore(v, dest->llValue);
}
return;
}
codegenCallValue(staticCValue(operator_move(), ctx),
new MultiCValue(src),
ctx,
new MultiCValue(dest));
}
void codegenValueForward(CValuePtr dest, CValuePtr src, CodegenContext* ctx)
{
if (src->type == dest->type) {
codegenValueMove(dest, src, ctx);
}
else {
assert(dest->type == pointerType(src->type));
ctx->builder->CreateStore(src->llValue, dest->llValue);
}
}
static void codegenValueAssign(
PVData const &pdest,
CValuePtr dest,
PVData const &psrc,
CValuePtr src,
CodegenContext* ctx)
{
if (isPrimitiveAggregateType(dest->type)
&& !pdest.isRValue
&& (dest->type == src->type)
&& (!isPrimitiveAggregateTooLarge(dest->type)))
{
if (dest->type->typeKind != STATIC_TYPE) {
llvm::Value *v = ctx->builder->CreateLoad(src->llValue);
ctx->builder->CreateStore(v, dest->llValue);
}
return;
}
MultiCValuePtr args = new MultiCValue(dest);
args->add(src);
MultiPValuePtr pvArgs = new MultiPValue(pdest);
pvArgs->add(psrc);
codegenCallValue(staticCValue(operator_assign(), ctx),
args,
pvArgs,
ctx,
new MultiCValue());
}
llvm::Value *codegenToBoolFlag(CValuePtr a, CodegenContext* ctx)
{
if (a->type != boolType)
typeError(boolType, a->type);
llvm::Value *flag = ctx->builder->CreateLoad(a->llValue);
assert(flag->getType() == llvmIntType(1));
return flag;
}
//
// temps
//
static llvm::Value *allocTemp(llvm::Type *llType, CodegenContext* ctx)
{
llvm::Value *llv = NULL;
for (size_t i = ctx->discardedSlots.size(); i > 0; --i) {
if (ctx->discardedSlots[i-1].llType == llType) {
llv = ctx->discardedSlots[i-1].llValue;
ctx->discardedSlots.erase(ctx->discardedSlots.begin() + long(i)-1);
break;
}
}
if (!llv)
llv = ctx->initBuilder->CreateAlloca(llType);
ctx->allocatedSlots.push_back(StackSlot(llType, llv));
return llv;
}
static size_t markTemps(CodegenContext* ctx) {
return ctx->allocatedSlots.size();
}
static void clearTemps(size_t marker, CodegenContext* ctx) {
while (marker < ctx->allocatedSlots.size()) {
ctx->discardedSlots.push_back(ctx->allocatedSlots.back());
ctx->allocatedSlots.pop_back();
}
}
//
// codegen value stack
//
size_t cgMarkStack(CodegenContext* ctx)
{
return ctx->valueStack.size();
}
void cgDestroyStack(size_t marker, CodegenContext* ctx, bool exception)
{
size_t i = ctx->valueStack.size();
assert(marker <= i);
while (marker < i) {
--i;
codegenStackEntryDestroy(ctx->valueStack[i], ctx, exception);
}
}
void cgPopStack(size_t marker, CodegenContext* ctx)
{
assert(marker <= ctx->valueStack.size());
while (marker < ctx->valueStack.size())
ctx->valueStack.pop_back();
}
void cgDestroyAndPopStack(size_t marker, CodegenContext* ctx, bool exception)
{
assert(marker <= ctx->valueStack.size());
while (marker < ctx->valueStack.size()) {
ValueStackEntry entry = ctx->valueStack.back();
ctx->valueStack.pop_back();
codegenStackEntryDestroy(entry, ctx, exception);
}
}
void cgPushStackValue(CValuePtr cv, CodegenContext* ctx)
{
ctx->valueStack.push_back(ValueStackEntry(cv));
}
void cgPushStackStatement(ValueStackEntryType type,
EnvPtr env,
StatementPtr statement,
CodegenContext* ctx)
{
ctx->valueStack.push_back(ValueStackEntry(type, env, statement));
}
CValuePtr codegenAllocValue(TypePtr t, CodegenContext* ctx)
{
llvm::Value *llv = allocTemp(llvmType(t), ctx);
return new CValue(t, llv);
}
CValuePtr codegenAllocValueForPValue(PVData const &pv, CodegenContext* ctx)
{
if (pv.isRValue)
return codegenAllocValue(pv.type, ctx);
else
return codegenAllocValue(pointerType(pv.type), ctx);
}
CValuePtr codegenAllocNewValue(TypePtr t, CodegenContext* ctx)
{
llvm::Type *llt = llvmType(t);
llvm::Value *llv = ctx->initBuilder->CreateAlloca(llt);
return new CValue(t, llv);
}
//
// codegenMultiArgsAsRef, codegenArgExprAsRef
//
MultiCValuePtr codegenMultiArgsAsRef(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx)
{
MultiCValuePtr out = new MultiCValue();
for (size_t i = 0; i < exprs->size(); ++i) {
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiCValuePtr mcv = codegenArgExprAsRef(y->expr, env, ctx);
out->add(mcv);
}
else if (x->exprKind == PAREN) {
MultiCValuePtr mcv = codegenArgExprAsRef(x, env, ctx);
out->add(mcv);
}
else {
MultiCValuePtr mcv = codegenArgExprAsRef(x, env, ctx);
out->add(mcv);
}
}
return out;
}
MultiCValuePtr codegenArgExprAsRef(ExprPtr x,
EnvPtr env,
CodegenContext* ctx)
{
if (x->exprKind == DISPATCH_EXPR) {
DispatchExpr *y = (DispatchExpr *)x.ptr();
return codegenExprAsRef(y->expr, env, ctx);
}
return codegenExprAsRef(x, env, ctx);
}
//
// codegenForwardOneAsRef, codegenForwardMultiAsRef, codegenForwardExprAsRef
//
CValuePtr codegenForwardOneAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx)
{
MultiCValuePtr mcv = codegenForwardExprAsRef(expr, env, ctx);
LocationContext loc(expr->location);
ensureArity(mcv, 1);
return mcv->values[0];
}
MultiCValuePtr codegenForwardMultiAsRef(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx)
{
MultiCValuePtr out = new MultiCValue();
for (size_t i = 0; i < exprs->size(); ++i) {
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiCValuePtr mcv = codegenForwardExprAsRef(y->expr, env, ctx);
out->add(mcv);
}
else {
MultiCValuePtr mcv = codegenForwardExprAsRef(x, env, ctx);
out->add(mcv);
}
}
return out;
}
MultiCValuePtr codegenForwardExprAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx)
{
MultiPValuePtr mpv = safeAnalyzeExpr(expr, env);
MultiCValuePtr mcv = codegenExprAsRef(expr, env, ctx);
assert(mpv->size() == mcv->size());
MultiCValuePtr out = new MultiCValue();
for (unsigned i = 0; i < mcv->size(); ++i) {
CValuePtr cv = mcv->values[i];
if (mpv->values[i].isRValue) {
cv = new CValue(cv->type, cv->llValue, true);
}
out->add(cv);
}
return out;
}
//
// codegenOneAsRef, codegenMultiAsRef, codegenExprAsRef
//
CValuePtr codegenOneAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx)
{
MultiCValuePtr mcv = codegenExprAsRef(expr, env, ctx);
LocationContext loc(expr->location);
ensureArity(mcv, 1);
return mcv->values[0];
}
MultiCValuePtr codegenMultiAsRef(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx)
{
MultiCValuePtr out = new MultiCValue();
for (size_t i = 0; i < exprs->size(); ++i) {
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiCValuePtr mcv = codegenExprAsRef(y->expr, env, ctx);
out->add(mcv);
}
else if (x->exprKind == PAREN) {
MultiCValuePtr mcv = codegenExprAsRef(x, env, ctx);
out->add(mcv);
}
else {
CValuePtr cv = codegenOneAsRef(x, env, ctx);
out->add(cv);
}
}
return out;
}
static MultiCValuePtr codegenExprAsRef2(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx)
{
MultiPValuePtr mpv = safeAnalyzeExpr(expr, env);
MultiCValuePtr mcv = new MultiCValue();
for (unsigned i = 0; i < mpv->size(); ++i) {
PVData const &pv = mpv->values[i];
mcv->add(codegenAllocValueForPValue(pv, ctx));
}
codegenExpr(expr, env, ctx, mcv);
MultiCValuePtr out = new MultiCValue();
for (unsigned i = 0; i < mpv->size(); ++i) {
if (mpv->values[i].isRValue) {
out->add(mcv->values[i]);
cgPushStackValue(mcv->values[i], ctx);
}
else {
out->add(derefValue(mcv->values[i], ctx));
}
}
return out;
}
static MultiCValuePtr codegenStaticObjectAsRef(ObjectPtr x,
ExprPtr expr,
EnvPtr env,
CodegenContext* ctx)
{
switch (x->objKind) {
case CVALUE : {
CValuePtr y = (CValue *)x.ptr();
if (y->forwardedRValue)
y = new CValue(y->type, y->llValue);
return new MultiCValue(y);
}
case MULTI_CVALUE : {
MultiCValue *y = (MultiCValue *)x.ptr();
MultiCValuePtr out = new MultiCValue();
for (size_t i = 0; i < y->size(); ++i) {
CValue *cv = y->values[i].ptr();
if (cv->forwardedRValue)
out->add(new CValue(cv->type, cv->llValue));
else
out->add(cv);
}
return out;
}
default :
return codegenExprAsRef2(expr, env, ctx);
}
}
MultiCValuePtr codegenExprAsRef(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx)
{
LocationContext loc(expr->location);
switch (expr->exprKind) {
case NAME_REF : {
NameRef *x = (NameRef *)expr.ptr();
ObjectPtr y = safeLookupEnv(env, x->name);
if (y->objKind == EXPRESSION) {
ExprPtr z = (Expr *)y.ptr();
return codegenExprAsRef(z, env, ctx);
}
else if (y->objKind == EXPR_LIST) {
ExprListPtr z = (ExprList *)y.ptr();
return codegenMultiAsRef(z, env, ctx);
}
return codegenStaticObjectAsRef(y, expr, env, ctx);
}
case OBJECT_EXPR : {
ObjectExpr *x = (ObjectExpr *)expr.ptr();
return codegenStaticObjectAsRef(x->obj, expr, env, ctx);
}
default :
break;
}
return codegenExprAsRef2(expr, env, ctx);
}
//
// codegenOneInto, codegenMultiInto, codegenExprInto
//
void codegenOneInto(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
CValuePtr out)
{
PVData pv = safeAnalyzeOne(expr, env);
size_t marker = cgMarkStack(ctx);
if (pv.isRValue) {
codegenOne(expr, env, ctx, out);
}
else {
CValuePtr cvPtr = codegenAllocValue(pointerType(pv.type), ctx);
codegenOne(expr, env, ctx, cvPtr);
codegenValueCopy(out, derefValue(cvPtr, ctx), ctx);
}
cgDestroyAndPopStack(marker, ctx, false);
}
void codegenMultiInto(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out,
size_t wantCount)
{
size_t marker = cgMarkStack(ctx);
size_t marker2 = marker;
size_t j = 0;
ExprPtr unpackExpr = implicitUnpackExpr(wantCount, exprs);
if (unpackExpr != NULL) {
MultiPValuePtr mpv = safeAnalyzeExpr(unpackExpr, env);
assert(j + mpv->size() <= out->size());
MultiCValuePtr out2 = new MultiCValue();
for (size_t k = 0; k < mpv->size(); ++k)
out2->add(out->values[j + k]);
codegenExprInto(unpackExpr, env, ctx, out2);
j += mpv->size();
} else for (size_t i = 0; i < exprs->size(); ++i) {
size_t prevJ = j;
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiPValuePtr mpv = safeAnalyzeExpr(y->expr, env);
assert(j + mpv->size() <= out->size());
MultiCValuePtr out2 = new MultiCValue();
for (size_t k = 0; k < mpv->size(); ++k)
out2->add(out->values[j + k]);
codegenExprInto(y->expr, env, ctx, out2);
j += mpv->size();
}
else if (x->exprKind == PAREN) {
MultiPValuePtr mpv = safeAnalyzeExpr(x, env);
assert(j + mpv->size() <= out->size());
MultiCValuePtr out2 = new MultiCValue();
for (size_t k = 0; k < mpv->size(); ++k)
out2->add(out->values[j + k]);
codegenExprInto(x, env, ctx, out2);
j += mpv->size();
}
else {
MultiPValuePtr mpv = safeAnalyzeExpr(x, env);
if (mpv->size() != 1)
arityError(x, 1, mpv->size());
assert(j < out->size());
codegenOneInto(x, env, ctx, out->values[j]);
++j;
}
cgDestroyAndPopStack(marker2, ctx, false);
for (size_t k = prevJ; k < j; ++k)
cgPushStackValue(out->values[k], ctx);
marker2 = cgMarkStack(ctx);
}
assert(j == out->size());
cgPopStack(marker, ctx);
}
void codegenExprInto(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out)
{
MultiPValuePtr mpv = safeAnalyzeExpr(expr, env);
assert(out->size() == mpv->size());
size_t marker = cgMarkStack(ctx);
MultiCValuePtr mcv = new MultiCValue();
for (unsigned i = 0; i < mpv->size(); ++i) {
PVData const &pv = mpv->values[i];
if (pv.isRValue) {
mcv->add(out->values[i]);
}
else {
CValuePtr cvPtr = codegenAllocValue(pointerType(pv.type), ctx);
mcv->add(cvPtr);
}
}
codegenExpr(expr, env, ctx, mcv);
size_t marker2 = cgMarkStack(ctx);
for (unsigned i = 0; i < mpv->size(); ++i) {
if (!mpv->values[i].isRValue) {
CValuePtr cv = derefValue(mcv->values[i], ctx);
codegenValueCopy(out->values[i], cv, ctx);
}
cgPushStackValue(out->values[i], ctx);
}
cgPopStack(marker2, ctx);
cgDestroyAndPopStack(marker, ctx, false);
}
//
// codegenMulti, codegenOne, codegenExpr
//
void codegenMulti(ExprListPtr exprs,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out,
size_t wantCount)
{
size_t marker = cgMarkStack(ctx);
size_t marker2 = marker;
unsigned j = 0;
ExprPtr unpackExpr = implicitUnpackExpr(wantCount, exprs);
if (unpackExpr != NULL) {
MultiPValuePtr mpv = safeAnalyzeExpr(unpackExpr, env);
assert(j + mpv->size() <= out->size());
MultiCValuePtr out2 = new MultiCValue();
for (unsigned k = 0; k < mpv->size(); ++k)
out2->add(out->values[j + k]);
codegenExpr(unpackExpr, env, ctx, out2);
j += unsigned(mpv->size());
} else for (size_t i = 0; i < exprs->size(); ++i) {
unsigned prevJ = j;
ExprPtr x = exprs->exprs[i];
if (x->exprKind == UNPACK) {
Unpack *y = (Unpack *)x.ptr();
MultiPValuePtr mpv = safeAnalyzeExpr(y->expr, env);
assert(j + mpv->size() <= out->size());
MultiCValuePtr out2 = new MultiCValue();
for (unsigned k = 0; k < mpv->size(); ++k)
out2->add(out->values[j + k]);
codegenExpr(y->expr, env, ctx, out2);
j += unsigned(mpv->size());
}
else if (x->exprKind == PAREN) {
MultiPValuePtr mpv = safeAnalyzeExpr(x, env);
assert(j + mpv->size() <= out->size());
MultiCValuePtr out2 = new MultiCValue();
for (unsigned k = 0; k < mpv->size(); ++k)
out2->add(out->values[j + k]);
codegenExpr(x, env, ctx, out2);
j += unsigned(mpv->size());
}
else {
MultiPValuePtr mpv = safeAnalyzeExpr(x, env);
if (mpv->size() != 1)
arityError(x, 1, mpv->size());
assert(j < out->size());
codegenOne(x, env, ctx, out->values[j]);
++j;
}
cgDestroyAndPopStack(marker2, ctx, false);
for (unsigned k = prevJ; k < j; ++k)
cgPushStackValue(out->values[k], ctx);
marker2 = cgMarkStack(ctx);
}
assert(j == out->size());
cgPopStack(marker, ctx);
}
void codegenOne(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
CValuePtr out)
{
codegenExpr(expr, env, ctx, new MultiCValue(out));
}
void codegenExpr(ExprPtr expr,
EnvPtr env,
CodegenContext* ctx,
MultiCValuePtr out)
{
LocationContext loc(expr->location);
switch (expr->exprKind) {
case BOOL_LITERAL : {
BoolLiteral *x = (BoolLiteral *)expr.ptr();
ValueHolderPtr y = boolToValueHolder(x->value);
codegenValueHolder(y, ctx, out);
break;
}
case INT_LITERAL : {
IntLiteral *x = (IntLiteral *)expr.ptr();
ValueHolderPtr y = parseIntLiteral(safeLookupModule(env), x);
codegenValueHolder(y, ctx, out);
break;
}
case FLOAT_LITERAL : {
FloatLiteral *x = (FloatLiteral *)expr.ptr();
ValueHolderPtr y = parseFloatLiteral(safeLookupModule(env), x);
codegenValueHolder(y, ctx, out);
break;
}
case CHAR_LITERAL : {
CharLiteral *x = (CharLiteral *)expr.ptr();
if (!x->desugared)
x->desugared = desugarCharLiteral(x->value);
codegenExpr(x->desugared, env, ctx, out);
break;
}
case STRING_LITERAL :
break;
case FILE_EXPR :
case ARG_EXPR :
break;
case LINE_EXPR : {
Location location = safeLookupCallByNameLocation(env);
unsigned line, column, tabColumn;
getLineCol(location, line, column, tabColumn);
ValueHolderPtr vh = sizeTToValueHolder(line+1);
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case COLUMN_EXPR : {
Location location = safeLookupCallByNameLocation(env);