• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "llvm-c/BitReader.h"
2 #include "llvm-c/Core.h"
3 #include "llvm-c/Object.h"
4 #include "llvm/ADT/ArrayRef.h"
5 #include "llvm/ADT/DenseSet.h"
6 #include "llvm/ADT/SmallVector.h"
7 #include "llvm/Analysis/Lint.h"
8 #include "llvm/Analysis/Passes.h"
9 #include "llvm/IR/IRBuilder.h"
10 #include "llvm/IR/InlineAsm.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Support/FormattedStream.h"
17 #include "llvm/Support/JSON.h"
18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/Memory.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/TargetSelect.h"
22 #include "llvm/Support/Timer.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Transforms/IPO.h"
27 #include "llvm/Transforms/Instrumentation.h"
28 #include "llvm/Transforms/Scalar.h"
29 #include "llvm/Transforms/Vectorize.h"
30 
31 #define LLVM_VERSION_GE(major, minor)                                          \
32   (LLVM_VERSION_MAJOR > (major) ||                                             \
33    LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR >= (minor))
34 
35 #define LLVM_VERSION_LT(major, minor) (!LLVM_VERSION_GE((major), (minor)))
36 
37 #include "llvm/IR/LegacyPassManager.h"
38 
39 #include "llvm/Bitcode/BitcodeReader.h"
40 #include "llvm/Bitcode/BitcodeWriter.h"
41 
42 #include "llvm/IR/DIBuilder.h"
43 #include "llvm/IR/DebugInfo.h"
44 #include "llvm/IR/IRPrintingPasses.h"
45 #include "llvm/Linker/Linker.h"
46 
47 #if LLVM_VERSION_GE(16, 0)
48 #include "llvm/TargetParser/Triple.h"
49 #else
50 #include "llvm/ADT/Triple.h"
51 #endif
52 
53 extern "C" void LLVMRustSetLastError(const char *);
54 
55 enum class LLVMRustResult { Success, Failure };
56 
57 enum LLVMRustAttribute {
58   AlwaysInline = 0,
59   ByVal = 1,
60   Cold = 2,
61   InlineHint = 3,
62   MinSize = 4,
63   Naked = 5,
64   NoAlias = 6,
65   NoCapture = 7,
66   NoInline = 8,
67   NonNull = 9,
68   NoRedZone = 10,
69   NoReturn = 11,
70   NoUnwind = 12,
71   OptimizeForSize = 13,
72   ReadOnly = 14,
73   SExt = 15,
74   StructRet = 16,
75   UWTable = 17,
76   ZExt = 18,
77   InReg = 19,
78   SanitizeThread = 20,
79   SanitizeAddress = 21,
80   SanitizeMemory = 22,
81   NonLazyBind = 23,
82   OptimizeNone = 24,
83   ReturnsTwice = 25,
84   ReadNone = 26,
85   SanitizeHWAddress = 28,
86   WillReturn = 29,
87   StackProtectReq = 30,
88   StackProtectStrong = 31,
89   StackProtect = 32,
90   NoUndef = 33,
91   SanitizeMemTag = 34,
92   NoCfCheck = 35,
93   ShadowCallStack = 36,
94   AllocSize = 37,
95 #if LLVM_VERSION_GE(15, 0)
96   AllocatedPointer = 38,
97   AllocAlign = 39,
98 #endif
99   SanitizeSafeStack = 40,
100 };
101 
102 typedef struct OpaqueRustString *RustStringRef;
103 typedef struct LLVMOpaqueTwine *LLVMTwineRef;
104 typedef struct LLVMOpaqueSMDiagnostic *LLVMSMDiagnosticRef;
105 
106 extern "C" void LLVMRustStringWriteImpl(RustStringRef Str, const char *Ptr,
107                                         size_t Size);
108 
109 class RawRustStringOstream : public llvm::raw_ostream {
110   RustStringRef Str;
111   uint64_t Pos;
112 
write_impl(const char * Ptr,size_t Size)113   void write_impl(const char *Ptr, size_t Size) override {
114     LLVMRustStringWriteImpl(Str, Ptr, Size);
115     Pos += Size;
116   }
117 
current_pos()118   uint64_t current_pos() const override { return Pos; }
119 
120 public:
RawRustStringOstream(RustStringRef Str)121   explicit RawRustStringOstream(RustStringRef Str) : Str(Str), Pos(0) {}
122 
~RawRustStringOstream()123   ~RawRustStringOstream() {
124     // LLVM requires this.
125     flush();
126   }
127 };
128