forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.cpp
4242 lines (3515 loc) · 147 KB
/
Core.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
//===-- Core.cpp ----------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the common infrastructure (including the C bindings)
// for libLLVMCore.a, which implements the LLVM intermediate representation.
//
//===----------------------------------------------------------------------===//
#include "llvm-c/Core.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <system_error>
using namespace llvm;
#define DEBUG_TYPE "ir"
void llvm::initializeCore(PassRegistry &Registry) {
initializeDominatorTreeWrapperPassPass(Registry);
initializePrintModulePassWrapperPass(Registry);
initializePrintFunctionPassWrapperPass(Registry);
initializeSafepointIRVerifierPass(Registry);
initializeVerifierLegacyPassPass(Registry);
}
void LLVMInitializeCore(LLVMPassRegistryRef R) {
initializeCore(*unwrap(R));
}
void LLVMShutdown() {
llvm_shutdown();
}
/*===-- Error handling ----------------------------------------------------===*/
char *LLVMCreateMessage(const char *Message) {
return strdup(Message);
}
void LLVMDisposeMessage(char *Message) {
free(Message);
}
/*===-- Operations on contexts --------------------------------------------===*/
static ManagedStatic<LLVMContext> GlobalContext;
LLVMContextRef LLVMContextCreate() {
return wrap(new LLVMContext());
}
LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
LLVMDiagnosticHandler Handler,
void *DiagnosticContext) {
unwrap(C)->setDiagnosticHandlerCallBack(
LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
Handler),
DiagnosticContext);
}
LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
unwrap(C)->getDiagnosticHandlerCallBack());
}
void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
return unwrap(C)->getDiagnosticContext();
}
void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
void *OpaqueHandle) {
auto YieldCallback =
LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
}
LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
return unwrap(C)->shouldDiscardValueNames();
}
void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
unwrap(C)->setDiscardValueNames(Discard);
}
void LLVMContextDispose(LLVMContextRef C) {
delete unwrap(C);
}
unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
unsigned SLen) {
return unwrap(C)->getMDKindID(StringRef(Name, SLen));
}
unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
}
unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
return Attribute::getAttrKindFromName(StringRef(Name, SLen));
}
unsigned LLVMGetLastEnumAttributeKind(void) {
return Attribute::AttrKind::EndAttrKinds;
}
LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
uint64_t Val) {
auto &Ctx = *unwrap(C);
auto AttrKind = (Attribute::AttrKind)KindID;
if (AttrKind == Attribute::AttrKind::ByVal) {
// After r362128, byval attributes need to have a type attribute. Provide a
// NULL one until a proper API is added for this.
return wrap(Attribute::getWithByValType(Ctx, nullptr));
}
if (AttrKind == Attribute::AttrKind::StructRet) {
// Same as byval.
return wrap(Attribute::getWithStructRetType(Ctx, nullptr));
}
return wrap(Attribute::get(Ctx, AttrKind, Val));
}
unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
return unwrap(A).getKindAsEnum();
}
uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
auto Attr = unwrap(A);
if (Attr.isEnumAttribute())
return 0;
return Attr.getValueAsInt();
}
LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
LLVMTypeRef type_ref) {
auto &Ctx = *unwrap(C);
auto AttrKind = (Attribute::AttrKind)KindID;
return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
}
LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
auto Attr = unwrap(A);
return wrap(Attr.getValueAsType());
}
LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
const char *K, unsigned KLength,
const char *V, unsigned VLength) {
return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
StringRef(V, VLength)));
}
const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
unsigned *Length) {
auto S = unwrap(A).getKindAsString();
*Length = S.size();
return S.data();
}
const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
unsigned *Length) {
auto S = unwrap(A).getValueAsString();
*Length = S.size();
return S.data();
}
LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
auto Attr = unwrap(A);
return Attr.isEnumAttribute() || Attr.isIntAttribute();
}
LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
return unwrap(A).isStringAttribute();
}
LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {
return unwrap(A).isTypeAttribute();
}
char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
std::string MsgStorage;
raw_string_ostream Stream(MsgStorage);
DiagnosticPrinterRawOStream DP(Stream);
unwrap(DI)->print(DP);
Stream.flush();
return LLVMCreateMessage(MsgStorage.c_str());
}
LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
LLVMDiagnosticSeverity severity;
switch(unwrap(DI)->getSeverity()) {
default:
severity = LLVMDSError;
break;
case DS_Warning:
severity = LLVMDSWarning;
break;
case DS_Remark:
severity = LLVMDSRemark;
break;
case DS_Note:
severity = LLVMDSNote;
break;
}
return severity;
}
/*===-- Operations on modules ---------------------------------------------===*/
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
return wrap(new Module(ModuleID, *GlobalContext));
}
LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
LLVMContextRef C) {
return wrap(new Module(ModuleID, *unwrap(C)));
}
void LLVMDisposeModule(LLVMModuleRef M) {
delete unwrap(M);
}
const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
auto &Str = unwrap(M)->getModuleIdentifier();
*Len = Str.length();
return Str.c_str();
}
void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
}
const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
auto &Str = unwrap(M)->getSourceFileName();
*Len = Str.length();
return Str.c_str();
}
void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
unwrap(M)->setSourceFileName(StringRef(Name, Len));
}
/*--.. Data layout .........................................................--*/
const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
return unwrap(M)->getDataLayoutStr().c_str();
}
const char *LLVMGetDataLayout(LLVMModuleRef M) {
return LLVMGetDataLayoutStr(M);
}
void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
unwrap(M)->setDataLayout(DataLayoutStr);
}
/*--.. Target triple .......................................................--*/
const char * LLVMGetTarget(LLVMModuleRef M) {
return unwrap(M)->getTargetTriple().c_str();
}
void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
unwrap(M)->setTargetTriple(Triple);
}
/*--.. Module flags ........................................................--*/
struct LLVMOpaqueModuleFlagEntry {
LLVMModuleFlagBehavior Behavior;
const char *Key;
size_t KeyLen;
LLVMMetadataRef Metadata;
};
static Module::ModFlagBehavior
map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
switch (Behavior) {
case LLVMModuleFlagBehaviorError:
return Module::ModFlagBehavior::Error;
case LLVMModuleFlagBehaviorWarning:
return Module::ModFlagBehavior::Warning;
case LLVMModuleFlagBehaviorRequire:
return Module::ModFlagBehavior::Require;
case LLVMModuleFlagBehaviorOverride:
return Module::ModFlagBehavior::Override;
case LLVMModuleFlagBehaviorAppend:
return Module::ModFlagBehavior::Append;
case LLVMModuleFlagBehaviorAppendUnique:
return Module::ModFlagBehavior::AppendUnique;
}
llvm_unreachable("Unknown LLVMModuleFlagBehavior");
}
static LLVMModuleFlagBehavior
map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
switch (Behavior) {
case Module::ModFlagBehavior::Error:
return LLVMModuleFlagBehaviorError;
case Module::ModFlagBehavior::Warning:
return LLVMModuleFlagBehaviorWarning;
case Module::ModFlagBehavior::Require:
return LLVMModuleFlagBehaviorRequire;
case Module::ModFlagBehavior::Override:
return LLVMModuleFlagBehaviorOverride;
case Module::ModFlagBehavior::Append:
return LLVMModuleFlagBehaviorAppend;
case Module::ModFlagBehavior::AppendUnique:
return LLVMModuleFlagBehaviorAppendUnique;
default:
llvm_unreachable("Unhandled Flag Behavior");
}
}
LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
SmallVector<Module::ModuleFlagEntry, 8> MFEs;
unwrap(M)->getModuleFlagsMetadata(MFEs);
LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
for (unsigned i = 0; i < MFEs.size(); ++i) {
const auto &ModuleFlag = MFEs[i];
Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
Result[i].Key = ModuleFlag.Key->getString().data();
Result[i].KeyLen = ModuleFlag.Key->getString().size();
Result[i].Metadata = wrap(ModuleFlag.Val);
}
*Len = MFEs.size();
return Result;
}
void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
free(Entries);
}
LLVMModuleFlagBehavior
LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
unsigned Index) {
LLVMOpaqueModuleFlagEntry MFE =
static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
return MFE.Behavior;
}
const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
unsigned Index, size_t *Len) {
LLVMOpaqueModuleFlagEntry MFE =
static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
*Len = MFE.KeyLen;
return MFE.Key;
}
LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
unsigned Index) {
LLVMOpaqueModuleFlagEntry MFE =
static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
return MFE.Metadata;
}
LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
const char *Key, size_t KeyLen) {
return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
}
void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
const char *Key, size_t KeyLen,
LLVMMetadataRef Val) {
unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
{Key, KeyLen}, unwrap(Val));
}
/*--.. Printing modules ....................................................--*/
void LLVMDumpModule(LLVMModuleRef M) {
unwrap(M)->print(errs(), nullptr,
/*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
}
LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
char **ErrorMessage) {
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
if (EC) {
*ErrorMessage = strdup(EC.message().c_str());
return true;
}
unwrap(M)->print(dest, nullptr);
dest.close();
if (dest.has_error()) {
std::string E = "Error printing to file: " + dest.error().message();
*ErrorMessage = strdup(E.c_str());
return true;
}
return false;
}
char *LLVMPrintModuleToString(LLVMModuleRef M) {
std::string buf;
raw_string_ostream os(buf);
unwrap(M)->print(os, nullptr);
os.flush();
return strdup(buf.c_str());
}
/*--.. Operations on inline assembler ......................................--*/
void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
}
void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
unwrap(M)->setModuleInlineAsm(StringRef(Asm));
}
void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
}
const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
auto &Str = unwrap(M)->getModuleInlineAsm();
*Len = Str.length();
return Str.c_str();
}
LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, char *AsmString,
size_t AsmStringSize, char *Constraints,
size_t ConstraintsSize, LLVMBool HasSideEffects,
LLVMBool IsAlignStack,
LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
InlineAsm::AsmDialect AD;
switch (Dialect) {
case LLVMInlineAsmDialectATT:
AD = InlineAsm::AD_ATT;
break;
case LLVMInlineAsmDialectIntel:
AD = InlineAsm::AD_Intel;
break;
}
return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
StringRef(AsmString, AsmStringSize),
StringRef(Constraints, ConstraintsSize),
HasSideEffects, IsAlignStack, AD, CanThrow));
}
/*--.. Operations on module contexts ......................................--*/
LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
return wrap(&unwrap(M)->getContext());
}
/*===-- Operations on types -----------------------------------------------===*/
/*--.. Operations on all types (mostly) ....................................--*/
LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
switch (unwrap(Ty)->getTypeID()) {
case Type::VoidTyID:
return LLVMVoidTypeKind;
case Type::HalfTyID:
return LLVMHalfTypeKind;
case Type::BFloatTyID:
return LLVMBFloatTypeKind;
case Type::FloatTyID:
return LLVMFloatTypeKind;
case Type::DoubleTyID:
return LLVMDoubleTypeKind;
case Type::X86_FP80TyID:
return LLVMX86_FP80TypeKind;
case Type::FP128TyID:
return LLVMFP128TypeKind;
case Type::PPC_FP128TyID:
return LLVMPPC_FP128TypeKind;
case Type::LabelTyID:
return LLVMLabelTypeKind;
case Type::MetadataTyID:
return LLVMMetadataTypeKind;
case Type::IntegerTyID:
return LLVMIntegerTypeKind;
case Type::FunctionTyID:
return LLVMFunctionTypeKind;
case Type::StructTyID:
return LLVMStructTypeKind;
case Type::ArrayTyID:
return LLVMArrayTypeKind;
case Type::PointerTyID:
return LLVMPointerTypeKind;
case Type::FixedVectorTyID:
return LLVMVectorTypeKind;
case Type::X86_MMXTyID:
return LLVMX86_MMXTypeKind;
case Type::X86_AMXTyID:
return LLVMX86_AMXTypeKind;
case Type::TokenTyID:
return LLVMTokenTypeKind;
case Type::ScalableVectorTyID:
return LLVMScalableVectorTypeKind;
}
llvm_unreachable("Unhandled TypeID.");
}
LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
{
return unwrap(Ty)->isSized();
}
LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
return wrap(&unwrap(Ty)->getContext());
}
void LLVMDumpType(LLVMTypeRef Ty) {
return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
}
char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
std::string buf;
raw_string_ostream os(buf);
if (unwrap(Ty))
unwrap(Ty)->print(os);
else
os << "Printing <null> Type";
os.flush();
return strdup(buf.c_str());
}
/*--.. Operations on integer types .........................................--*/
LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
}
LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
}
LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
}
LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
}
LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
}
LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
}
LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
return wrap(IntegerType::get(*unwrap(C), NumBits));
}
LLVMTypeRef LLVMInt1Type(void) {
return LLVMInt1TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMInt8Type(void) {
return LLVMInt8TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMInt16Type(void) {
return LLVMInt16TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMInt32Type(void) {
return LLVMInt32TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMInt64Type(void) {
return LLVMInt64TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMInt128Type(void) {
return LLVMInt128TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMIntType(unsigned NumBits) {
return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
}
unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
return unwrap<IntegerType>(IntegerTy)->getBitWidth();
}
/*--.. Operations on real types ............................................--*/
LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
}
LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C));
}
LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
}
LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
}
LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
}
LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
}
LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
}
LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
}
LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {
return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C));
}
LLVMTypeRef LLVMHalfType(void) {
return LLVMHalfTypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMBFloatType(void) {
return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMFloatType(void) {
return LLVMFloatTypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMDoubleType(void) {
return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMX86FP80Type(void) {
return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMFP128Type(void) {
return LLVMFP128TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMPPCFP128Type(void) {
return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMX86MMXType(void) {
return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMX86AMXType(void) {
return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
}
/*--.. Operations on function types ........................................--*/
LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
LLVMTypeRef *ParamTypes, unsigned ParamCount,
LLVMBool IsVarArg) {
ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
}
LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
return unwrap<FunctionType>(FunctionTy)->isVarArg();
}
LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
}
unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
return unwrap<FunctionType>(FunctionTy)->getNumParams();
}
void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
for (Type *T : Ty->params())
*Dest++ = wrap(T);
}
/*--.. Operations on struct types ..........................................--*/
LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
unsigned ElementCount, LLVMBool Packed) {
ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
}
LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
unsigned ElementCount, LLVMBool Packed) {
return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
ElementCount, Packed);
}
LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
{
return wrap(StructType::create(*unwrap(C), Name));
}
const char *LLVMGetStructName(LLVMTypeRef Ty)
{
StructType *Type = unwrap<StructType>(Ty);
if (!Type->hasName())
return nullptr;
return Type->getName().data();
}
void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
unsigned ElementCount, LLVMBool Packed) {
ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
}
unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
return unwrap<StructType>(StructTy)->getNumElements();
}
void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
StructType *Ty = unwrap<StructType>(StructTy);
for (Type *T : Ty->elements())
*Dest++ = wrap(T);
}
LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
StructType *Ty = unwrap<StructType>(StructTy);
return wrap(Ty->getTypeAtIndex(i));
}
LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
return unwrap<StructType>(StructTy)->isPacked();
}
LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
return unwrap<StructType>(StructTy)->isOpaque();
}
LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
return unwrap<StructType>(StructTy)->isLiteral();
}
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
}
LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
return wrap(StructType::getTypeByName(*unwrap(C), Name));
}
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
int i = 0;
for (auto *T : unwrap(Tp)->subtypes()) {
Arr[i] = wrap(T);
i++;
}
}
LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
}
LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
}
LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
}
LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
unsigned ElementCount) {
return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
}
LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
auto *Ty = unwrap<Type>(WrappedTy);
if (auto *PTy = dyn_cast<PointerType>(Ty))
return wrap(PTy->getPointerElementType());
if (auto *ATy = dyn_cast<ArrayType>(Ty))
return wrap(ATy->getElementType());
return wrap(cast<VectorType>(Ty)->getElementType());
}
unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
return unwrap(Tp)->getNumContainedTypes();
}
unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
return unwrap<ArrayType>(ArrayTy)->getNumElements();
}
unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
return unwrap<PointerType>(PointerTy)->getAddressSpace();
}
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
}
/*--.. Operations on other types ...........................................--*/
LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
return wrap(Type::getVoidTy(*unwrap(C)));
}
LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
return wrap(Type::getLabelTy(*unwrap(C)));
}
LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
return wrap(Type::getTokenTy(*unwrap(C)));
}
LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
return wrap(Type::getMetadataTy(*unwrap(C)));
}
LLVMTypeRef LLVMVoidType(void) {
return LLVMVoidTypeInContext(LLVMGetGlobalContext());
}
LLVMTypeRef LLVMLabelType(void) {
return LLVMLabelTypeInContext(LLVMGetGlobalContext());
}
/*===-- Operations on values ----------------------------------------------===*/
/*--.. Operations on all values ............................................--*/
LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
return wrap(unwrap(Val)->getType());
}
LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
switch(unwrap(Val)->getValueID()) {
#define LLVM_C_API 1
#define HANDLE_VALUE(Name) \
case Value::Name##Val: \
return LLVM##Name##ValueKind;
#include "llvm/IR/Value.def"
default:
return LLVMInstructionValueKind;
}
}
const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
auto *V = unwrap(Val);
*Length = V->getName().size();
return V->getName().data();
}
void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
unwrap(Val)->setName(StringRef(Name, NameLen));
}
const char *LLVMGetValueName(LLVMValueRef Val) {
return unwrap(Val)->getName().data();
}
void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
unwrap(Val)->setName(Name);
}
void LLVMDumpValue(LLVMValueRef Val) {
unwrap(Val)->print(errs(), /*IsForDebug=*/true);
}
char* LLVMPrintValueToString(LLVMValueRef Val) {
std::string buf;
raw_string_ostream os(buf);
if (unwrap(Val))
unwrap(Val)->print(os);
else
os << "Printing <null> Value";
os.flush();
return strdup(buf.c_str());
}
void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
}
int LLVMHasMetadata(LLVMValueRef Inst) {
return unwrap<Instruction>(Inst)->hasMetadata();
}
LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
auto *I = unwrap<Instruction>(Inst);
assert(I && "Expected instruction");
if (auto *MD = I->getMetadata(KindID))
return wrap(MetadataAsValue::get(I->getContext(), MD));
return nullptr;
}
// MetadataAsValue uses a canonical format which strips the actual MDNode for
// MDNode with just a single constant value, storing just a ConstantAsMetadata
// This undoes this canonicalization, reconstructing the MDNode.
static MDNode *extractMDNode(MetadataAsValue *MAV) {
Metadata *MD = MAV->getMetadata();
assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
"Expected a metadata node or a canonicalized constant");
if (MDNode *N = dyn_cast<MDNode>(MD))
return N;
return MDNode::get(MAV->getContext(), MD);
}
void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
}
struct LLVMOpaqueValueMetadataEntry {
unsigned Kind;
LLVMMetadataRef Metadata;
};
using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
static LLVMValueMetadataEntry *
llvm_getMetadata(size_t *NumEntries,
llvm::function_ref<void(MetadataEntries &)> AccessMD) {
SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
AccessMD(MVEs);
LLVMOpaqueValueMetadataEntry *Result =
static_cast<LLVMOpaqueValueMetadataEntry *>(
safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
for (unsigned i = 0; i < MVEs.size(); ++i) {
const auto &ModuleFlag = MVEs[i];
Result[i].Kind = ModuleFlag.first;
Result[i].Metadata = wrap(ModuleFlag.second);
}
*NumEntries = MVEs.size();
return Result;
}
LLVMValueMetadataEntry *
LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
size_t *NumEntries) {
return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
Entries.clear();
unwrap<Instruction>(Value)->getAllMetadata(Entries);
});
}
/*--.. Conversion functions ................................................--*/
#define LLVM_DEFINE_VALUE_CAST(name) \
LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
}
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
if (isa<MDNode>(MD->getMetadata()) ||
isa<ValueAsMetadata>(MD->getMetadata()))
return Val;
return nullptr;
}
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
if (isa<MDString>(MD->getMetadata()))
return Val;
return nullptr;
}
/*--.. Operations on Uses ..................................................--*/
LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Value *V = unwrap(Val);
Value::use_iterator I = V->use_begin();
if (I == V->use_end())
return nullptr;
return wrap(&*I);