-
Notifications
You must be signed in to change notification settings - Fork 34
/
codegen_op.cpp
2404 lines (2144 loc) · 79.1 KB
/
codegen_op.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 "types.hpp"
#include "error.hpp"
#include "loader.hpp"
#include "codegen.hpp"
#include "objects.hpp"
#include "evaluator.hpp"
#include "analyzer.hpp"
#include "operators.hpp"
#include "constructors.hpp"
#include "externals.hpp"
#include "env.hpp"
#include "codegen_op.hpp"
namespace clay {
static llvm::StringMap<llvm::Constant*> stringTableConstants;
//
// valueToStatic, valueToStaticSizeTOrInt
// valueToType, valueToNumericType, valueToIntegerType,
// valueToPointerLikeType, valueToTupleType, valueToRecordType,
// valueToVariantType, valueToEnumType, valueToIdentifier
//
static ObjectPtr valueToStatic(CValuePtr cv)
{
if (cv->type->typeKind != STATIC_TYPE)
return NULL;
StaticType *st = (StaticType *)cv->type.ptr();
return st->obj;
}
static ObjectPtr valueToStatic(MultiCValuePtr args, unsigned index)
{
ObjectPtr obj = valueToStatic(args->values[index]);
if (!obj)
argumentError(index, "expecting a static value");
return obj;
}
static size_t valueToStaticSizeTOrInt(MultiCValuePtr args, unsigned index)
{
ObjectPtr obj = valueToStatic(args->values[index]);
if (!obj || (obj->objKind != VALUE_HOLDER))
argumentError(index, "expecting a static SizeT or Int value");
ValueHolder *vh = (ValueHolder *)obj.ptr();
if (vh->type == cSizeTType) {
return valueHolderToSizeT(vh);
}
else if (vh->type == cIntType) {
return size_t(vh->as<int>());
}
else {
argumentError(index, "expecting a static SizeT or Int value");
return 0;
}
}
static TypePtr valueToType(MultiCValuePtr args, unsigned index)
{
ObjectPtr obj = valueToStatic(args->values[index]);
if (!obj || (obj->objKind != TYPE))
argumentError(index, "expecting a type");
return (Type *)obj.ptr();
}
static TypePtr valueToNumericType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
switch (t->typeKind) {
case INTEGER_TYPE :
case FLOAT_TYPE :
return t;
default :
argumentTypeError(index, "numeric type", t);
return NULL;
}
}
static IntegerTypePtr valueToIntegerType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (t->typeKind != INTEGER_TYPE)
argumentTypeError(index, "integer type", t);
return (IntegerType *)t.ptr();
}
static TypePtr valueToPointerLikeType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (!isPointerOrCodePointerType(t))
argumentTypeError(index, "pointer or code-pointer type", t);
return t;
}
static TupleTypePtr valueToTupleType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (t->typeKind != TUPLE_TYPE)
argumentTypeError(index, "tuple type", t);
return (TupleType *)t.ptr();
}
static UnionTypePtr valueToUnionType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (t->typeKind != UNION_TYPE)
argumentTypeError(index, "union type", t);
return (UnionType *)t.ptr();
}
static RecordTypePtr valueToRecordType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (t->typeKind != RECORD_TYPE)
argumentTypeError(index, "record type", t);
return (RecordType *)t.ptr();
}
static VariantTypePtr valueToVariantType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (t->typeKind != VARIANT_TYPE)
argumentTypeError(index, "variant type", t);
return (VariantType *)t.ptr();
}
static EnumTypePtr valueToEnumType(MultiCValuePtr args, unsigned index)
{
TypePtr t = valueToType(args, index);
if (t->typeKind != ENUM_TYPE)
argumentTypeError(index, "enum type", t);
initializeEnumType((EnumType*)t.ptr());
return (EnumType *)t.ptr();
}
static IdentifierPtr valueToIdentifier(MultiCValuePtr args, unsigned index)
{
ObjectPtr obj = valueToStatic(args->values[index]);
if (!obj || (obj->objKind != IDENTIFIER))
argumentError(index, "expecting identifier value");
return (Identifier *)obj.ptr();
}
static llvm::StringRef valueToStringRef(MultiCValuePtr args, unsigned index)
{
return valueToIdentifier(args, index)->str;
}
//
// numericValue, integerValue, pointerValue,
// pointerLikeValue, cCodePointerValue,
// arrayValue, tupleValue, recordValue, variantValue, enumValue
//
static llvm::Value *numericValue(MultiCValuePtr args,
unsigned index,
TypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != type)
argumentTypeError(index, type, cv->type);
}
else {
switch (cv->type->typeKind) {
case INTEGER_TYPE :
case FLOAT_TYPE :
break;
default :
argumentTypeError(index, "numeric type", cv->type);
}
type = cv->type;
}
return ctx->builder->CreateLoad(cv->llValue);
}
static llvm::Value *floatValue(MultiCValuePtr args,
unsigned index,
FloatTypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type.ptr() != type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
switch (cv->type->typeKind) {
case FLOAT_TYPE :
break;
default :
argumentTypeError(index, "float type", cv->type);
}
type = (FloatType*)cv->type.ptr();
}
return ctx->builder->CreateLoad(cv->llValue);
}
static llvm::Value *integerOrPointerLikeValue(MultiCValuePtr args,
unsigned index,
TypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != type)
argumentTypeError(index, type, cv->type);
}
else {
switch (cv->type->typeKind) {
case INTEGER_TYPE :
case POINTER_TYPE :
case CODE_POINTER_TYPE :
case CCODE_POINTER_TYPE :
break;
default :
argumentTypeError(index, "integer, pointer, or code pointer type", cv->type);
}
type = cv->type;
}
return ctx->builder->CreateLoad(cv->llValue);
}
static void checkIntegerValue(MultiCValuePtr args,
unsigned index,
IntegerTypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != INTEGER_TYPE)
argumentTypeError(index, "integer type", cv->type);
type = (IntegerType *)cv->type.ptr();
}
}
static llvm::Value *integerValue(MultiCValuePtr args,
unsigned index,
IntegerTypePtr &type,
CodegenContext* ctx)
{
checkIntegerValue(args, index, type, ctx);
CValuePtr cv = args->values[index];
return ctx->builder->CreateLoad(cv->llValue);
}
static llvm::Value *pointerValue(MultiCValuePtr args,
unsigned index,
PointerTypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != (Type *)type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != POINTER_TYPE)
argumentTypeError(index, "pointer type", cv->type);
type = (PointerType *)cv->type.ptr();
llvmType(type->pointeeType); // force the pointee type to be refined
}
return ctx->builder->CreateLoad(cv->llValue);
}
static llvm::Value *pointerLikeValue(MultiCValuePtr args,
unsigned index,
TypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != type)
argumentTypeError(index, type, cv->type);
}
else {
if (!isPointerOrCodePointerType(cv->type))
argumentTypeError(index,
"pointer or code-pointer type",
cv->type);
type = cv->type;
}
return ctx->builder->CreateLoad(cv->llValue);
}
static llvm::Value* cCodePointerValue(MultiCValuePtr args,
unsigned index,
CCodePointerTypePtr &type)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != (Type *)type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != CCODE_POINTER_TYPE)
argumentTypeError(index, "c code pointer type", cv->type);
type = (CCodePointerType *)cv->type.ptr();
}
return cv->llValue;
}
static llvm::Value *arrayValue(MultiCValuePtr args,
unsigned index,
ArrayTypePtr &type)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != (Type *)type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != ARRAY_TYPE)
argumentTypeError(index, "array type", cv->type);
type = (ArrayType *)cv->type.ptr();
}
return cv->llValue;
}
static llvm::Value *tupleValue(MultiCValuePtr args,
unsigned index,
TupleTypePtr &type)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != (Type *)type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != TUPLE_TYPE)
argumentTypeError(index, "tuple type", cv->type);
type = (TupleType *)cv->type.ptr();
}
return cv->llValue;
}
static llvm::Value *recordValue(MultiCValuePtr args,
unsigned index,
RecordTypePtr &type)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != (Type *)type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != RECORD_TYPE)
argumentTypeError(index, "record type", cv->type);
type = (RecordType *)cv->type.ptr();
}
return cv->llValue;
}
static llvm::Value *enumValue(MultiCValuePtr args,
unsigned index,
EnumTypePtr &type,
CodegenContext* ctx)
{
CValuePtr cv = args->values[index];
if (type.ptr()) {
if (cv->type != (Type *)type.ptr())
argumentTypeError(index, type.ptr(), cv->type);
}
else {
if (cv->type->typeKind != ENUM_TYPE)
argumentTypeError(index, "enum type", cv->type);
type = (EnumType *)cv->type.ptr();
}
return ctx->builder->CreateLoad(cv->llValue);
}
llvm::AtomicOrdering atomicOrderValue(MultiCValuePtr args, unsigned index)
{
CValuePtr cv = args->values[index];
ObjectPtr obj = unwrapStaticType(cv->type);
if (obj != NULL && obj->objKind == PRIM_OP) {
PrimOp *prim = (PrimOp*)obj.ptr();
switch (prim->primOpCode) {
case PRIM_OrderUnordered:
return llvm::Unordered;
case PRIM_OrderMonotonic:
return llvm::Monotonic;
case PRIM_OrderAcquire:
return llvm::Acquire;
case PRIM_OrderRelease:
return llvm::Release;
case PRIM_OrderAcqRel:
return llvm::AcquireRelease;
case PRIM_OrderSeqCst:
return llvm::SequentiallyConsistent;
}
}
argumentTypeError(index, "atomic ordering", cv->type);
return llvm::Unordered;
}
llvm::AtomicRMWInst::BinOp atomicRMWOpValue(MultiCValuePtr args, unsigned index)
{
CValuePtr cv = args->values[index];
ObjectPtr obj = unwrapStaticType(cv->type);
if (obj != NULL && obj->objKind == PRIM_OP) {
PrimOp *prim = (PrimOp*)obj.ptr();
switch (prim->primOpCode) {
case PRIM_RMWXchg:
return llvm::AtomicRMWInst::Xchg;
case PRIM_RMWAdd:
return llvm::AtomicRMWInst::Add;
case PRIM_RMWSubtract:
return llvm::AtomicRMWInst::Sub;
case PRIM_RMWAnd:
return llvm::AtomicRMWInst::And;
case PRIM_RMWNAnd:
return llvm::AtomicRMWInst::Nand;
case PRIM_RMWOr:
return llvm::AtomicRMWInst::Or;
case PRIM_RMWXor:
return llvm::AtomicRMWInst::Xor;
case PRIM_RMWMin:
return llvm::AtomicRMWInst::Min;
case PRIM_RMWMax:
return llvm::AtomicRMWInst::Max;
case PRIM_RMWUMin:
return llvm::AtomicRMWInst::UMin;
case PRIM_RMWUMax:
return llvm::AtomicRMWInst::UMax;
}
}
argumentTypeError(index, "atomic rmw operation", cv->type);
return llvm::AtomicRMWInst::Xchg;
}
//
// codegenPrimOp
//
static llvm::Constant *codegenStringTableConstant(llvm::StringRef s)
{
llvm::StringMap<llvm::Constant*>::const_iterator oldConstant = stringTableConstants.find(s);
if (oldConstant != stringTableConstants.end())
return oldConstant->second;
llvm::Constant *sizeInitializer =
llvm::ConstantInt::get(llvmType(cSizeTType), s.size(), false);
llvm::Constant *stringInitializer =
llvm::ConstantDataArray::getString(llvm::getGlobalContext(), s, true);
llvm::Constant *structEntries[] = {sizeInitializer, stringInitializer};
llvm::Constant *initializer =
llvm::ConstantStruct::getAnon(llvm::getGlobalContext(),
structEntries,
false);
llvm::SmallString<128> buf;
llvm::raw_svector_ostream symbolName(buf);
symbolName << "\"" << s << "\" clay";
llvm::GlobalVariable *gvar = new llvm::GlobalVariable(
*llvmModule, initializer->getType(), true,
llvm::GlobalVariable::PrivateLinkage,
initializer, symbolName.str());
llvm::Constant *idxs[] = {
llvm::ConstantInt::get(llvmType(int32Type), 0),
llvm::ConstantInt::get(llvmType(int32Type), 0),
};
llvm::Constant *theConstant = llvm::ConstantExpr::getGetElementPtr(gvar, idxs);
stringTableConstants[s] = theConstant;
return theConstant;
}
//
// codegenCWrapper
//
static void codegenCWrapper(InvokeEntry* entry, CallingConv cc)
{
assert(!entry->llvmCWrappers[cc]);
ExternalTargetPtr target = getExternalTarget();
string callableName = getCodeName(entry);
TypePtr returnType;
if (entry->returnTypes.empty()) {
returnType = NULL;
}
else {
assert(entry->returnTypes.size() == 1);
assert(!entry->returnIsRef[0]);
returnType = entry->returnTypes[0];
}
ExternalFunction *extFunc = target->getExternalFunction(
cc, returnType, entry->argsKey, entry->argsKey.size(), false);
llvm::FunctionType *llFuncType = extFunc->getLLVMFunctionType();
std::string ccName;
switch (cc) {
case CC_DEFAULT:
ccName = "cdecl ";
break;
case CC_STDCALL:
ccName = "stdcall ";
break;
case CC_FASTCALL:
ccName = "fastcall ";
break;
case CC_THISCALL:
ccName = "thiscall ";
break;
default:
assert(false);
}
llvm::Function *llCWrapper =
llvm::Function::Create(llFuncType,
llvm::Function::InternalLinkage,
ccName + callableName,
llvmModule);
for (vector< pair<unsigned, llvm::Attributes> >::const_iterator
attr = extFunc->attrs.begin();
attr != extFunc->attrs.end();
++attr)
llCWrapper->addAttribute(attr->first, attr->second);
llCWrapper->setCallingConv(extFunc->llConv);
entry->llvmCWrappers[cc] = llCWrapper;
CodegenContext ctx(llCWrapper);
llvm::BasicBlock *llInitBlock =
llvm::BasicBlock::Create(llvm::getGlobalContext(),
"init",
llCWrapper);
llvm::BasicBlock *llBlock =
llvm::BasicBlock::Create(llvm::getGlobalContext(),
"code",
llCWrapper);
ctx.initBuilder.reset(new llvm::IRBuilder<>(llInitBlock));
ctx.builder.reset(new llvm::IRBuilder<>(llBlock));
ctx.exceptionValue = ctx.initBuilder->CreateAlloca(exceptionReturnType(), NULL, "exception");
vector<llvm::Value *> innerArgs;
vector<CReturn> returns;
llvm::Function::arg_iterator ai = llCWrapper->arg_begin();
extFunc->allocReturnValue(extFunc->retInfo, ai, returns, &ctx);
for (size_t i = 0; i < entry->argsKey.size(); ++i) {
CValuePtr cv = extFunc->allocArgumentValue(
extFunc->argInfos[i], "x", ai, &ctx);
innerArgs.push_back(cv->llValue);
}
for (vector<CReturn>::const_iterator ret = returns.begin();
ret != returns.end();
++ret)
innerArgs.push_back(ret->value->llValue);
// XXX check exception
ctx.builder->CreateCall(entry->llvmFunc, llvm::makeArrayRef(innerArgs));
extFunc->returnStatement(extFunc->retInfo, returns, &ctx);
ctx.initBuilder->CreateBr(llBlock);
}
//
// codegenCallCCode, codegenCallCCodePointer
//
static void codegenCallCCode(CCodePointerTypePtr t,
llvm::Value *llCallable,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out)
{
ExternalTargetPtr target = getExternalTarget();
if (!t->hasVarArgs)
ensureArity(args, t->argTypes.size());
else if (args->size() < t->argTypes.size())
arityError2(t->argTypes.size(), args->size());
vector<TypePtr> argTypes;
for (size_t i = 0; i < args->size(); ++i) {
CValuePtr cv = args->values[i];
argTypes.push_back(cv->type);
}
ExternalFunction *extFunc = target->getExternalFunction(
t->callingConv, t->returnType, argTypes,
t->argTypes.size(), t->hasVarArgs);
vector<llvm::Value*> llArgs;
extFunc->loadStructRetArgument(extFunc->retInfo, llArgs, ctx, out);
for (unsigned i = 0; i < t->argTypes.size(); ++i) {
CValuePtr cv = args->values[i];
if (cv->type != t->argTypes[i])
argumentTypeError(i, t->argTypes[i], cv->type);
extFunc->loadArgument(extFunc->argInfos[i], cv, llArgs, ctx, false);
}
if (t->hasVarArgs) {
for (size_t i = t->argTypes.size(); i < args->size(); ++i) {
CValuePtr cv = args->values[i];
extFunc->loadArgument(extFunc->argInfos[i], cv, llArgs,
ctx, true);
}
}
llvm::Value *llCastCallable = t->callingConv == CC_LLVM
? llCallable
: ctx->builder->CreateBitCast(llCallable, t->getCallType());
llvm::CallInst *callInst =
ctx->builder->CreateCall(llCastCallable, llvm::makeArrayRef(llArgs));
llvm::CallingConv::ID callingConv = extFunc->llConv;
callInst->setCallingConv(callingConv);
for (vector< pair<unsigned, llvm::Attributes> >::iterator
attr = extFunc->attrs.begin();
attr != extFunc->attrs.end();
++attr)
callInst->addAttribute(attr->first, attr->second);
llvm::Value *llRet = callInst;
extFunc->storeReturnValue(extFunc->retInfo, llRet, ctx, out);
}
static void codegenCallCCodePointer(CValuePtr x,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out)
{
llvm::Value *llCallable = ctx->builder->CreateLoad(x->llValue);
assert(x->type->typeKind == CCODE_POINTER_TYPE);
CCodePointerType *t = (CCodePointerType *)x->type.ptr();
codegenCallCCode(t, llCallable, args, ctx, out);
}
void codegenPrimOp(PrimOpPtr x,
MultiCValuePtr args,
CodegenContext* ctx,
MultiCValuePtr out)
{
switch (x->primOpCode) {
case PRIM_TypeP : {
ensureArity(args, 1);
ObjectPtr obj = valueToStatic(args->values[0]);
bool isType = (obj.ptr() && (obj->objKind == TYPE));
ValueHolderPtr vh = boolToValueHolder(isType);
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_TypeSize : {
ensureArity(args, 1);
TypePtr t = valueToType(args, 0);
ValueHolderPtr vh = sizeTToValueHolder(typeSize(t));
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_TypeAlignment : {
ensureArity(args, 1);
TypePtr t = valueToType(args, 0);
ValueHolderPtr vh = sizeTToValueHolder(typeAlignment(t));
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_OperatorP : {
ensureArity(args, 1);
ObjectPtr obj = valueToStatic(args->values[0]);
bool isOperator = false;
if (!!obj.ptr() && (obj->objKind==PROCEDURE || obj->objKind==GLOBAL_ALIAS)) {
TopLevelItem *item = (TopLevelItem *)obj.ptr();
isOperator = item->name->isOperator;
}
ValueHolderPtr vh = boolToValueHolder(isOperator);
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_SymbolP : {
ensureArity(args, 1);
ObjectPtr obj = valueToStatic(args->values[0]);
bool isSymbol = false;
if (obj.ptr() != NULL) {
switch (obj->objKind) {
case TYPE :
case RECORD_DECL :
case VARIANT_DECL :
case PROCEDURE :
case INTRINSIC :
case GLOBAL_ALIAS:
isSymbol = true;
break;
default :
break;
}
}
ValueHolderPtr vh = boolToValueHolder(isSymbol);
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_StaticCallDefinedP : {
if (args->size() < 1)
arityError2(1, args->size());
ObjectPtr callable = valueToStatic(args->values[0]);
if (!callable) {
argumentError(0, "static callable expected");
}
switch (callable->objKind) {
case TYPE :
case RECORD_DECL :
case VARIANT_DECL :
case PROCEDURE :
case INTRINSIC :
case GLOBAL_ALIAS:
break;
default :
argumentError(0, "invalid callable");
}
vector<PVData> argsPVData;
for (unsigned i = 1; i < args->size(); ++i) {
TypePtr t = valueToType(args, i);
argsPVData.push_back(PVData(t, false));
}
CompileContextPusher pusher(callable, argsPVData);
bool isDefined = analyzeIsDefined(callable, argsPVData);
ValueHolderPtr vh = boolToValueHolder(isDefined);
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_StaticCallOutputTypes :
break;
case PRIM_StaticMonoP : {
ensureArity(args, 1);
bool isMono;
ObjectPtr callable = valueToStatic(args->values[0]);
if (callable != NULL && callable->objKind == PROCEDURE) {
Procedure *p = (Procedure*)callable.ptr();
isMono = p->mono.monoState == Procedure_MonoOverload;
} else
isMono = false;
ValueHolderPtr vh = boolToValueHolder(isMono);
codegenStaticObject(vh.ptr(), ctx, out);
break;
}
case PRIM_StaticMonoInputTypes :
break;
case PRIM_bitcopy : {
ensureArity(args, 2);
CValuePtr cv0 = args->values[0];
CValuePtr cv1 = args->values[1];
if (!isPrimitiveType(cv0->type))
argumentTypeError(0, "primitive type", cv0->type);
if (cv0->type != cv1->type)
argumentTypeError(1, cv0->type, cv1->type);
llvm::Value *v = ctx->builder->CreateLoad(cv1->llValue);
ctx->builder->CreateStore(v, cv0->llValue);
assert(out->size() == 0);
break;
}
case PRIM_boolNot : {
ensureArity(args, 1);
CValuePtr cv = args->values[0];
if (cv->type != boolType)
argumentTypeError(0, boolType, cv->type);
assert(out->size() == 1);
llvm::Value *v = ctx->builder->CreateLoad(cv->llValue);
llvm::Value *zero = llvm::ConstantInt::get(llvmType(boolType), 0);
llvm::Value *flag = ctx->builder->CreateICmpEQ(v, zero);
CValuePtr out0 = out->values[0];
assert(out0->type == boolType);
ctx->builder->CreateStore(flag, out0->llValue);
break;
}
case PRIM_integerEqualsP :
case PRIM_integerLesserP : {
ensureArity(args, 2);
TypePtr t;
llvm::Value *v0 = integerOrPointerLikeValue(args, 0, t, ctx);
llvm::Value *v1 = integerOrPointerLikeValue(args, 1, t, ctx);
llvm::CmpInst::Predicate pred;
switch (x->primOpCode) {
case PRIM_integerEqualsP:
pred = llvm::CmpInst::ICMP_EQ;
break;
case PRIM_integerLesserP: {
bool isSigned;
switch (t->typeKind) {
case INTEGER_TYPE: {
IntegerType *it = (IntegerType*)t.ptr();
isSigned = it->isSigned;
break;
}
default:
isSigned = false;
break;
}
pred = isSigned
? llvm::CmpInst::ICMP_SLT
: llvm::CmpInst::ICMP_ULT;
break;
}
default:
assert(false);
}
llvm::Value *flag = ctx->builder->CreateICmp(pred, v0, v1);
assert(out->size() == 1);
CValuePtr out0 = out->values[0];
assert(out0->type == boolType);
ctx->builder->CreateStore(flag, out0->llValue);
break;
}
case PRIM_floatOrderedEqualsP :
case PRIM_floatOrderedLesserP :
case PRIM_floatOrderedLesserEqualsP :
case PRIM_floatOrderedGreaterP :
case PRIM_floatOrderedGreaterEqualsP :
case PRIM_floatOrderedNotEqualsP :
case PRIM_floatOrderedP :
case PRIM_floatUnorderedEqualsP :
case PRIM_floatUnorderedLesserP :
case PRIM_floatUnorderedLesserEqualsP :
case PRIM_floatUnorderedGreaterP :
case PRIM_floatUnorderedGreaterEqualsP :
case PRIM_floatUnorderedNotEqualsP :
case PRIM_floatUnorderedP : {
ensureArity(args, 2);
FloatTypePtr t;
llvm::Value *v0 = floatValue(args, 0, t, ctx);
llvm::Value *v1 = floatValue(args, 1, t, ctx);
llvm::CmpInst::Predicate pred;
switch (x->primOpCode) {
case PRIM_floatOrderedEqualsP :
pred = llvm::CmpInst::FCMP_OEQ;
break;
case PRIM_floatOrderedLesserP :
pred = llvm::CmpInst::FCMP_OLT;
break;
case PRIM_floatOrderedLesserEqualsP :
pred = llvm::CmpInst::FCMP_OLE;
break;
case PRIM_floatOrderedGreaterP :
pred = llvm::CmpInst::FCMP_OGT;
break;
case PRIM_floatOrderedGreaterEqualsP :
pred = llvm::CmpInst::FCMP_OGE;
break;
case PRIM_floatOrderedNotEqualsP :
pred = llvm::CmpInst::FCMP_ONE;
break;
case PRIM_floatOrderedP :
pred = llvm::CmpInst::FCMP_ORD;
break;
case PRIM_floatUnorderedEqualsP :
pred = llvm::CmpInst::FCMP_UEQ;
break;
case PRIM_floatUnorderedLesserP :
pred = llvm::CmpInst::FCMP_ULT;
break;
case PRIM_floatUnorderedLesserEqualsP :
pred = llvm::CmpInst::FCMP_ULE;
break;
case PRIM_floatUnorderedGreaterP :
pred = llvm::CmpInst::FCMP_UGT;
break;
case PRIM_floatUnorderedGreaterEqualsP :
pred = llvm::CmpInst::FCMP_UGE;
break;
case PRIM_floatUnorderedNotEqualsP :
pred = llvm::CmpInst::FCMP_UNE;
break;
case PRIM_floatUnorderedP :
pred = llvm::CmpInst::FCMP_UNO;
break;
default:
assert(false);
}
llvm::Value *flag = ctx->builder->CreateFCmp(pred, v0, v1);
assert(out->size() == 1);
CValuePtr out0 = out->values[0];
assert(out0->type == boolType);
ctx->builder->CreateStore(flag, out0->llValue);
break;
}
case PRIM_numericAdd : {
ensureArity(args, 2);
TypePtr t;
llvm::Value *v0 = numericValue(args, 0, t, ctx);
llvm::Value *v1 = numericValue(args, 1, t, ctx);
llvm::Value *result;
switch (t->typeKind) {
case INTEGER_TYPE :
result = ctx->builder->CreateAdd(v0, v1);
break;
case FLOAT_TYPE :
result = ctx->builder->CreateFAdd(v0, v1);
break;
default :
assert(false);
}
assert(out->size() == 1);
CValuePtr out0 = out->values[0];
assert(out0->type == t);
ctx->builder->CreateStore(result, out0->llValue);
break;
}
case PRIM_numericSubtract : {
ensureArity(args, 2);
TypePtr t;
llvm::Value *v0 = numericValue(args, 0, t, ctx);
llvm::Value *v1 = numericValue(args, 1, t, ctx);
llvm::Value *result;
switch (t->typeKind) {
case INTEGER_TYPE :
result = ctx->builder->CreateSub(v0, v1);
break;
case FLOAT_TYPE :
result = ctx->builder->CreateFSub(v0, v1);
break;
default :
assert(false);
}
assert(out->size() == 1);
CValuePtr out0 = out->values[0];
assert(out0->type == t);
ctx->builder->CreateStore(result, out0->llValue);
break;
}
case PRIM_numericMultiply : {
ensureArity(args, 2);
TypePtr t;
llvm::Value *v0 = numericValue(args, 0, t, ctx);
llvm::Value *v1 = numericValue(args, 1, t, ctx);
llvm::Value *result;
switch (t->typeKind) {
case INTEGER_TYPE :
result = ctx->builder->CreateMul(v0, v1);
break;
case FLOAT_TYPE :
result = ctx->builder->CreateFMul(v0, v1);
break;
default :
assert(false);
}
assert(out->size() == 1);
CValuePtr out0 = out->values[0];
assert(out0->type == t);
ctx->builder->CreateStore(result, out0->llValue);
break;
}
case PRIM_floatDivide : {
ensureArity(args, 2);
FloatTypePtr t;
llvm::Value *v0 = floatValue(args, 0, t, ctx);
llvm::Value *v1 = floatValue(args, 1, t, ctx);
llvm::Value *result;
result = ctx->builder->CreateFDiv(v0, v1);
assert(out->size() == 1);