1 /*
2  * Copyright (c) 2015 Andrew Kelley
3  *
4  * This file is part of zig, which is MIT licensed.
5  * See http://opensource.org/licenses/MIT
6  */
7 
8 
9 /*
10  * The point of this file is to contain all the LLVM C++ API interaction so that:
11  * 1. The compile time of other files is kept under control.
12  * 2. Provide a C interface to the LLVM functions we need for self-hosting purposes.
13  * 3. Prevent C++ from infecting the rest of the project.
14  */
15 
16 #include "zig_llvm.h"
17 
18 #if __GNUC__ >= 9
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Winit-list-lifetime"
21 #endif
22 
23 #include <llvm/Analysis/AliasAnalysis.h>
24 #include <llvm/Analysis/TargetLibraryInfo.h>
25 #include <llvm/Analysis/TargetTransformInfo.h>
26 #include <llvm/Bitcode/BitcodeWriter.h>
27 #include <llvm/IR/DIBuilder.h>
28 #include <llvm/IR/DiagnosticInfo.h>
29 #include <llvm/IR/IRBuilder.h>
30 #include <llvm/IR/InlineAsm.h>
31 #include <llvm/IR/Instructions.h>
32 #include <llvm/IR/LegacyPassManager.h>
33 #include <llvm/IR/Module.h>
34 #include <llvm/IR/PassManager.h>
35 #include <llvm/IR/Verifier.h>
36 #include <llvm/InitializePasses.h>
37 #include <llvm/MC/SubtargetFeature.h>
38 #include <llvm/Passes/PassBuilder.h>
39 #include <llvm/Passes/StandardInstrumentations.h>
40 #include <llvm/Object/Archive.h>
41 #include <llvm/Object/ArchiveWriter.h>
42 #include <llvm/Object/COFF.h>
43 #include <llvm/Object/COFFImportFile.h>
44 #include <llvm/Object/COFFModuleDefinition.h>
45 #include <llvm/PassRegistry.h>
46 #include <llvm/Support/CommandLine.h>
47 #include <llvm/Support/Host.h>
48 #include <llvm/Support/FileSystem.h>
49 #include <llvm/Support/Process.h>
50 #include <llvm/Support/TargetParser.h>
51 #include <llvm/Support/TimeProfiler.h>
52 #include <llvm/Support/Timer.h>
53 #include <llvm/Support/raw_ostream.h>
54 #include <llvm/Support/TargetRegistry.h>
55 #include <llvm/Target/TargetMachine.h>
56 #include <llvm/Target/CodeGenCWrappers.h>
57 #include <llvm/Transforms/IPO.h>
58 #include <llvm/Transforms/IPO/AlwaysInliner.h>
59 #include <llvm/Transforms/IPO/PassManagerBuilder.h>
60 #include <llvm/Transforms/Instrumentation/ThreadSanitizer.h>
61 #include <llvm/Transforms/Scalar.h>
62 #include <llvm/Transforms/Utils.h>
63 #include <llvm/Transforms/Utils/AddDiscriminators.h>
64 #include <llvm/Transforms/Utils/CanonicalizeAliases.h>
65 #include <llvm/Transforms/Utils/NameAnonGlobals.h>
66 
67 #include <lld/Common/Driver.h>
68 
69 #if __GNUC__ >= 9
70 #pragma GCC diagnostic pop
71 #endif
72 
73 #include <new>
74 
75 #include <stdlib.h>
76 
77 using namespace llvm;
78 
ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R)79 void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R) {
80     initializeLoopStrengthReducePass(*unwrap(R));
81 }
82 
ZigLLVMInitializeLowerIntrinsicsPass(LLVMPassRegistryRef R)83 void ZigLLVMInitializeLowerIntrinsicsPass(LLVMPassRegistryRef R) {
84     initializeLowerIntrinsicsPass(*unwrap(R));
85 }
86 
ZigLLVMGetHostCPUName(void)87 char *ZigLLVMGetHostCPUName(void) {
88     return strdup((const char *)sys::getHostCPUName().bytes_begin());
89 }
90 
ZigLLVMGetNativeFeatures(void)91 char *ZigLLVMGetNativeFeatures(void) {
92     SubtargetFeatures features;
93 
94     StringMap<bool> host_features;
95     if (sys::getHostCPUFeatures(host_features)) {
96         for (auto &F : host_features)
97             features.AddFeature(F.first(), F.second);
98     }
99 
100     return strdup((const char *)StringRef(features.getString()).bytes_begin());
101 }
102 
103 #ifndef NDEBUG
104 static const bool assertions_on = true;
105 #else
106 static const bool assertions_on = false;
107 #endif
108 
ZigLLVMCreateTargetMachine(LLVMTargetRef T,const char * Triple,const char * CPU,const char * Features,LLVMCodeGenOptLevel Level,LLVMRelocMode Reloc,LLVMCodeModel CodeModel,bool function_sections,ZigLLVMABIType float_abi,const char * abi_name)109 LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
110     const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
111     LLVMCodeModel CodeModel, bool function_sections, ZigLLVMABIType float_abi, const char *abi_name)
112 {
113     Optional<Reloc::Model> RM;
114     switch (Reloc){
115         case LLVMRelocStatic:
116             RM = Reloc::Static;
117             break;
118         case LLVMRelocPIC:
119             RM = Reloc::PIC_;
120             break;
121         case LLVMRelocDynamicNoPic:
122             RM = Reloc::DynamicNoPIC;
123             break;
124         case LLVMRelocROPI:
125             RM = Reloc::ROPI;
126             break;
127         case LLVMRelocRWPI:
128             RM = Reloc::RWPI;
129             break;
130         case LLVMRelocROPI_RWPI:
131             RM = Reloc::ROPI_RWPI;
132             break;
133         default:
134             break;
135     }
136 
137     bool JIT;
138     Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
139 
140     CodeGenOpt::Level OL;
141     switch (Level) {
142         case LLVMCodeGenLevelNone:
143             OL = CodeGenOpt::None;
144             break;
145         case LLVMCodeGenLevelLess:
146             OL = CodeGenOpt::Less;
147             break;
148         case LLVMCodeGenLevelAggressive:
149             OL = CodeGenOpt::Aggressive;
150             break;
151         default:
152             OL = CodeGenOpt::Default;
153             break;
154     }
155 
156     TargetOptions opt;
157 
158     opt.FunctionSections = function_sections;
159     switch (float_abi) {
160         case ZigLLVMABITypeDefault:
161             opt.FloatABIType = FloatABI::Default;
162             break;
163         case ZigLLVMABITypeSoft:
164             opt.FloatABIType = FloatABI::Soft;
165             break;
166         case ZigLLVMABITypeHard:
167             opt.FloatABIType = FloatABI::Hard;
168             break;
169     }
170 
171     if (abi_name != nullptr) {
172         opt.MCOptions.ABIName = abi_name;
173     }
174 
175     TargetMachine *TM = reinterpret_cast<Target*>(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
176             OL, JIT);
177     return reinterpret_cast<LLVMTargetMachineRef>(TM);
178 }
179 
ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD)180 unsigned ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD) {
181     return unwrap(TD)->getStackAlignment().value();
182 }
183 
ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD)184 unsigned ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD) {
185     return unwrap(TD)->getProgramAddressSpace();
186 }
187 
188 namespace {
189 // LLVM's time profiler can provide a hierarchy view of the time spent
190 // in each component. It generates JSON report in Chrome's "Trace Event"
191 // format. So the report can be easily visualized by the Chrome browser.
192 struct TimeTracerRAII {
193   // Granularity in ms
194   unsigned TimeTraceGranularity;
195   StringRef TimeTraceFile, OutputFilename;
196   bool EnableTimeTrace;
197 
TimeTracerRAII__anon973559780111::TimeTracerRAII198   TimeTracerRAII(StringRef ProgramName, StringRef OF)
199     : TimeTraceGranularity(500U),
200       TimeTraceFile(std::getenv("ZIG_LLVM_TIME_TRACE_FILE")),
201       OutputFilename(OF),
202       EnableTimeTrace(!TimeTraceFile.empty()) {
203     if (EnableTimeTrace) {
204       if (const char *G = std::getenv("ZIG_LLVM_TIME_TRACE_GRANULARITY"))
205         TimeTraceGranularity = (unsigned)std::atoi(G);
206 
207       llvm::timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
208     }
209   }
210 
~TimeTracerRAII__anon973559780111::TimeTracerRAII211   ~TimeTracerRAII() {
212     if (EnableTimeTrace) {
213       if (auto E = llvm::timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
214         handleAllErrors(std::move(E), [&](const StringError &SE) {
215           errs() << SE.getMessage() << "\n";
216         });
217         return;
218       }
219       timeTraceProfilerCleanup();
220     }
221   }
222 };
223 } // end anonymous namespace
224 
ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref,LLVMModuleRef module_ref,char ** error_message,bool is_debug,bool is_small,bool time_report,bool tsan,bool lto,const char * asm_filename,const char * bin_filename,const char * llvm_ir_filename,const char * bitcode_filename)225 bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
226         char **error_message, bool is_debug,
227         bool is_small, bool time_report, bool tsan, bool lto,
228         const char *asm_filename, const char *bin_filename,
229         const char *llvm_ir_filename, const char *bitcode_filename)
230 {
231     TimePassesIsEnabled = time_report;
232 
233     raw_fd_ostream *dest_asm_ptr = nullptr;
234     raw_fd_ostream *dest_bin_ptr = nullptr;
235     raw_fd_ostream *dest_bitcode_ptr = nullptr;
236 
237     if (asm_filename) {
238         std::error_code EC;
239         dest_asm_ptr = new(std::nothrow) raw_fd_ostream(asm_filename, EC, sys::fs::OF_None);
240         if (EC) {
241             *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
242             return true;
243         }
244     }
245     if (bin_filename) {
246         std::error_code EC;
247         dest_bin_ptr = new(std::nothrow) raw_fd_ostream(bin_filename, EC, sys::fs::OF_None);
248         if (EC) {
249             *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
250             return true;
251         }
252     }
253     if (bitcode_filename) {
254         std::error_code EC;
255         dest_bitcode_ptr = new(std::nothrow) raw_fd_ostream(bitcode_filename, EC, sys::fs::OF_None);
256         if (EC) {
257             *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
258             return true;
259         }
260     }
261 
262     std::unique_ptr<raw_fd_ostream> dest_asm(dest_asm_ptr),
263                                     dest_bin(dest_bin_ptr),
264                                     dest_bitcode(dest_bitcode_ptr);
265 
266 
267     auto PID = sys::Process::getProcessId();
268     std::string ProcName = "zig-";
269     ProcName += std::to_string(PID);
270     TimeTracerRAII TimeTracer(ProcName,
271                               bin_filename? bin_filename : asm_filename);
272 
273     TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);
274     target_machine.setO0WantsFastISel(true);
275 
276     Module &module = *unwrap(module_ref);
277 
278     // Pipeline configurations
279     PipelineTuningOptions pipeline_opts;
280     pipeline_opts.LoopUnrolling = !is_debug;
281     pipeline_opts.SLPVectorization = !is_debug;
282     pipeline_opts.LoopVectorization = !is_debug;
283     pipeline_opts.LoopInterleaving = !is_debug;
284     pipeline_opts.MergeFunctions = !is_debug;
285 
286     // Instrumentations
287     PassInstrumentationCallbacks instr_callbacks;
288     StandardInstrumentations std_instrumentations(false);
289     std_instrumentations.registerCallbacks(instr_callbacks);
290 
291     PassBuilder pass_builder(&target_machine, pipeline_opts,
292                              None, &instr_callbacks);
293     using OptimizationLevel = typename PassBuilder::OptimizationLevel;
294 
295     LoopAnalysisManager loop_am;
296     FunctionAnalysisManager function_am;
297     CGSCCAnalysisManager cgscc_am;
298     ModuleAnalysisManager module_am;
299 
300     // Register the AA manager first so that our version is the one used
301     function_am.registerPass([&] {
302       return pass_builder.buildDefaultAAPipeline();
303     });
304 
305     Triple target_triple(module.getTargetTriple());
306     auto tlii = std::make_unique<TargetLibraryInfoImpl>(target_triple);
307     function_am.registerPass([&] { return TargetLibraryAnalysis(*tlii); });
308 
309     // Initialize the AnalysisManagers
310     pass_builder.registerModuleAnalyses(module_am);
311     pass_builder.registerCGSCCAnalyses(cgscc_am);
312     pass_builder.registerFunctionAnalyses(function_am);
313     pass_builder.registerLoopAnalyses(loop_am);
314     pass_builder.crossRegisterProxies(loop_am, function_am,
315                                       cgscc_am, module_am);
316 
317     // IR verification
318     if (assertions_on) {
319       // Verify the input
320       pass_builder.registerPipelineStartEPCallback(
321         [](ModulePassManager &module_pm, OptimizationLevel OL) {
322           module_pm.addPass(VerifierPass());
323         });
324       // Verify the output
325       pass_builder.registerOptimizerLastEPCallback(
326         [](ModulePassManager &module_pm, OptimizationLevel OL) {
327           module_pm.addPass(VerifierPass());
328         });
329     }
330 
331     // Passes specific for release build
332     if (!is_debug) {
333       pass_builder.registerPipelineStartEPCallback(
334         [](ModulePassManager &module_pm, OptimizationLevel OL) {
335           module_pm.addPass(
336             createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
337         });
338     }
339 
340     // Thread sanitizer
341     if (tsan) {
342       pass_builder.registerOptimizerLastEPCallback(
343         [](ModulePassManager &module_pm, OptimizationLevel level) {
344           module_pm.addPass(ThreadSanitizerPass());
345         });
346     }
347 
348     ModulePassManager module_pm;
349     OptimizationLevel opt_level;
350     // Setting up the optimization level
351     if (is_debug)
352       opt_level = OptimizationLevel::O0;
353     else if (is_small)
354       opt_level = OptimizationLevel::Oz;
355     else
356       opt_level = OptimizationLevel::O3;
357 
358     // Initialize the PassManager
359     if (opt_level == OptimizationLevel::O0) {
360       module_pm = pass_builder.buildO0DefaultPipeline(opt_level, lto);
361     } else if (lto) {
362       module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level);
363     } else {
364       module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level);
365     }
366 
367     // Unfortunately we don't have new PM for code generation
368     legacy::PassManager codegen_pm;
369     codegen_pm.add(
370       createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis()));
371 
372     if (dest_bin && !lto) {
373         if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CGFT_ObjectFile)) {
374             *error_message = strdup("TargetMachine can't emit an object file");
375             return true;
376         }
377     }
378     if (dest_asm) {
379         if (target_machine.addPassesToEmitFile(codegen_pm, *dest_asm, nullptr, CGFT_AssemblyFile)) {
380             *error_message = strdup("TargetMachine can't emit an assembly file");
381             return true;
382         }
383     }
384 
385     // Optimization phase
386     module_pm.run(module, module_am);
387 
388     // Code generation phase
389     codegen_pm.run(module);
390 
391     if (llvm_ir_filename) {
392         if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) {
393             return true;
394         }
395     }
396 
397     if (dest_bin && lto) {
398         WriteBitcodeToFile(module, *dest_bin);
399     }
400     if (dest_bitcode) {
401         WriteBitcodeToFile(module, *dest_bitcode);
402     }
403 
404     if (time_report) {
405         TimerGroup::printAll(errs());
406     }
407 
408     return false;
409 }
410 
ZigLLVMTokenTypeInContext(LLVMContextRef context_ref)411 ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) {
412   return wrap(Type::getTokenTy(*unwrap(context_ref)));
413 }
414 
ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M,const char * Name,LLVMTypeRef FunctionTy,unsigned AddressSpace)415 LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy, unsigned AddressSpace) {
416     Function* func = Function::Create(unwrap<FunctionType>(FunctionTy), GlobalValue::ExternalLinkage, AddressSpace, Name, unwrap(M));
417     return wrap(func);
418 }
419 
ZigLLVMBuildCall(LLVMBuilderRef B,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,ZigLLVM_CallingConv CC,ZigLLVM_CallAttr attr,const char * Name)420 LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
421         unsigned NumArgs, ZigLLVM_CallingConv CC, ZigLLVM_CallAttr attr, const char *Name)
422 {
423     Value *V = unwrap(Fn);
424     FunctionType *FnT = cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
425     CallInst *call_inst = CallInst::Create(FnT, V, makeArrayRef(unwrap(Args), NumArgs), Name);
426     call_inst->setCallingConv(static_cast<CallingConv::ID>(CC));
427     switch (attr) {
428         case ZigLLVM_CallAttrAuto:
429             break;
430         case ZigLLVM_CallAttrNeverTail:
431             call_inst->setTailCallKind(CallInst::TCK_NoTail);
432             break;
433         case ZigLLVM_CallAttrNeverInline:
434             call_inst->addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
435             break;
436         case ZigLLVM_CallAttrAlwaysTail:
437             call_inst->setTailCallKind(CallInst::TCK_MustTail);
438             break;
439         case ZigLLVM_CallAttrAlwaysInline:
440             call_inst->addAttribute(AttributeList::FunctionIndex, Attribute::AlwaysInline);
441             break;
442     }
443     return wrap(unwrap(B)->Insert(call_inst));
444 }
445 
ZigLLVMBuildMemCpy(LLVMBuilderRef B,LLVMValueRef Dst,unsigned DstAlign,LLVMValueRef Src,unsigned SrcAlign,LLVMValueRef Size,bool isVolatile)446 LLVMValueRef ZigLLVMBuildMemCpy(LLVMBuilderRef B, LLVMValueRef Dst, unsigned DstAlign,
447         LLVMValueRef Src, unsigned SrcAlign, LLVMValueRef Size, bool isVolatile)
448 {
449     CallInst *call_inst = unwrap(B)->CreateMemCpy(unwrap(Dst),
450         MaybeAlign(DstAlign), unwrap(Src), MaybeAlign(SrcAlign), unwrap(Size), isVolatile);
451     return wrap(call_inst);
452 }
453 
ZigLLVMBuildMemSet(LLVMBuilderRef B,LLVMValueRef Ptr,LLVMValueRef Val,LLVMValueRef Size,unsigned Align,bool isVolatile)454 LLVMValueRef ZigLLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr, LLVMValueRef Val, LLVMValueRef Size,
455         unsigned Align, bool isVolatile)
456 {
457     CallInst *call_inst = unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Size),
458             MaybeAlign(Align), isVolatile);
459     return wrap(call_inst);
460 }
461 
ZigLLVMBuildMaxNum(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)462 LLVMValueRef ZigLLVMBuildMaxNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
463     CallInst *call_inst = unwrap(B)->CreateMaxNum(unwrap(LHS), unwrap(RHS), name);
464     return wrap(call_inst);
465 }
466 
ZigLLVMBuildMinNum(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)467 LLVMValueRef ZigLLVMBuildMinNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
468     CallInst *call_inst = unwrap(B)->CreateMinNum(unwrap(LHS), unwrap(RHS), name);
469     return wrap(call_inst);
470 }
471 
ZigLLVMBuildUMax(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)472 LLVMValueRef ZigLLVMBuildUMax(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
473     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::umax, unwrap(LHS), unwrap(RHS), nullptr, name);
474     return wrap(call_inst);
475 }
476 
ZigLLVMBuildUMin(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)477 LLVMValueRef ZigLLVMBuildUMin(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
478     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::umin, unwrap(LHS), unwrap(RHS), nullptr, name);
479     return wrap(call_inst);
480 }
481 
ZigLLVMBuildSMax(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)482 LLVMValueRef ZigLLVMBuildSMax(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
483     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::smax, unwrap(LHS), unwrap(RHS), nullptr, name);
484     return wrap(call_inst);
485 }
486 
ZigLLVMBuildSMin(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)487 LLVMValueRef ZigLLVMBuildSMin(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
488     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::smin, unwrap(LHS), unwrap(RHS), nullptr, name);
489     return wrap(call_inst);
490 }
491 
ZigLLVMBuildSAddSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)492 LLVMValueRef ZigLLVMBuildSAddSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
493     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::sadd_sat, unwrap(LHS), unwrap(RHS), nullptr, name);
494     return wrap(call_inst);
495 }
496 
ZigLLVMBuildUAddSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)497 LLVMValueRef ZigLLVMBuildUAddSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
498     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::uadd_sat, unwrap(LHS), unwrap(RHS), nullptr, name);
499     return wrap(call_inst);
500 }
501 
ZigLLVMBuildSSubSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)502 LLVMValueRef ZigLLVMBuildSSubSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
503     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::ssub_sat, unwrap(LHS), unwrap(RHS), nullptr, name);
504     return wrap(call_inst);
505 }
506 
ZigLLVMBuildUSubSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)507 LLVMValueRef ZigLLVMBuildUSubSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
508     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::usub_sat, unwrap(LHS), unwrap(RHS), nullptr, name);
509     return wrap(call_inst);
510 }
511 
ZigLLVMBuildSMulFixSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)512 LLVMValueRef ZigLLVMBuildSMulFixSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
513     llvm::Type* types[1] = {
514         unwrap(LHS)->getType(),
515     };
516     // pass scale = 0 as third argument
517     llvm::Value* values[3] = {unwrap(LHS), unwrap(RHS), unwrap(B)->getInt32(0)};
518 
519     CallInst *call_inst = unwrap(B)->CreateIntrinsic(Intrinsic::smul_fix_sat, types, values, nullptr, name);
520     return wrap(call_inst);
521 }
522 
ZigLLVMBuildUMulFixSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)523 LLVMValueRef ZigLLVMBuildUMulFixSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
524     llvm::Type* types[1] = {
525         unwrap(LHS)->getType(),
526     };
527     // pass scale = 0 as third argument
528     llvm::Value* values[3] = {unwrap(LHS), unwrap(RHS), unwrap(B)->getInt32(0)};
529 
530     CallInst *call_inst = unwrap(B)->CreateIntrinsic(Intrinsic::umul_fix_sat, types, values, nullptr, name);
531     return wrap(call_inst);
532 }
533 
ZigLLVMBuildSShlSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)534 LLVMValueRef ZigLLVMBuildSShlSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
535     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::sshl_sat, unwrap(LHS), unwrap(RHS), nullptr, name);
536     return wrap(call_inst);
537 }
538 
ZigLLVMBuildUShlSat(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)539 LLVMValueRef ZigLLVMBuildUShlSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name) {
540     CallInst *call_inst = unwrap(B)->CreateBinaryIntrinsic(Intrinsic::ushl_sat, unwrap(LHS), unwrap(RHS), nullptr, name);
541     return wrap(call_inst);
542 }
543 
ZigLLVMFnSetSubprogram(LLVMValueRef fn,ZigLLVMDISubprogram * subprogram)544 void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
545     assert( isa<Function>(unwrap(fn)) );
546     Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));
547     unwrapped_function->setSubprogram(reinterpret_cast<DISubprogram*>(subprogram));
548 }
549 
550 
ZigLLVMCreateDebugPointerType(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIType * pointee_type,uint64_t size_in_bits,uint64_t align_in_bits,const char * name)551 ZigLLVMDIType *ZigLLVMCreateDebugPointerType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIType *pointee_type,
552         uint64_t size_in_bits, uint64_t align_in_bits, const char *name)
553 {
554     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createPointerType(
555             reinterpret_cast<DIType*>(pointee_type), size_in_bits, align_in_bits, Optional<unsigned>(), name);
556     return reinterpret_cast<ZigLLVMDIType*>(di_type);
557 }
558 
ZigLLVMCreateDebugBasicType(ZigLLVMDIBuilder * dibuilder,const char * name,uint64_t size_in_bits,unsigned encoding)559 ZigLLVMDIType *ZigLLVMCreateDebugBasicType(ZigLLVMDIBuilder *dibuilder, const char *name,
560         uint64_t size_in_bits, unsigned encoding)
561 {
562     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createBasicType(
563             name, size_in_bits, encoding);
564     return reinterpret_cast<ZigLLVMDIType*>(di_type);
565 }
566 
ZigLLVMDIBuilderCreateVectorType(struct ZigLLVMDIBuilder * dibuilder,uint64_t SizeInBits,uint32_t AlignInBits,struct ZigLLVMDIType * Ty,uint32_t elem_count)567 struct ZigLLVMDIType *ZigLLVMDIBuilderCreateVectorType(struct ZigLLVMDIBuilder *dibuilder,
568         uint64_t SizeInBits, uint32_t AlignInBits, struct ZigLLVMDIType *Ty, uint32_t elem_count)
569 {
570     SmallVector<Metadata *, 1> subrange;
571     subrange.push_back(reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateSubrange(0, elem_count));
572     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createVectorType(
573             SizeInBits,
574             AlignInBits,
575             reinterpret_cast<DIType*>(Ty),
576             reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(subrange));
577     return reinterpret_cast<ZigLLVMDIType*>(di_type);
578 }
579 
ZigLLVMCreateDebugArrayType(ZigLLVMDIBuilder * dibuilder,uint64_t size_in_bits,uint64_t align_in_bits,ZigLLVMDIType * elem_type,int elem_count)580 ZigLLVMDIType *ZigLLVMCreateDebugArrayType(ZigLLVMDIBuilder *dibuilder, uint64_t size_in_bits,
581         uint64_t align_in_bits, ZigLLVMDIType *elem_type, int elem_count)
582 {
583     SmallVector<Metadata *, 1> subrange;
584     subrange.push_back(reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateSubrange(0, elem_count));
585     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createArrayType(
586             size_in_bits, align_in_bits,
587             reinterpret_cast<DIType*>(elem_type),
588             reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(subrange));
589     return reinterpret_cast<ZigLLVMDIType*>(di_type);
590 }
591 
ZigLLVMCreateDebugEnumerator(ZigLLVMDIBuilder * dibuilder,const char * name,int64_t val)592 ZigLLVMDIEnumerator *ZigLLVMCreateDebugEnumerator(ZigLLVMDIBuilder *dibuilder, const char *name, int64_t val) {
593     DIEnumerator *di_enumerator = reinterpret_cast<DIBuilder*>(dibuilder)->createEnumerator(name, val);
594     return reinterpret_cast<ZigLLVMDIEnumerator*>(di_enumerator);
595 }
596 
ZigLLVMCreateDebugEnumerationType(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIScope * scope,const char * name,ZigLLVMDIFile * file,unsigned line_number,uint64_t size_in_bits,uint64_t align_in_bits,ZigLLVMDIEnumerator ** enumerator_array,int enumerator_array_len,ZigLLVMDIType * underlying_type,const char * unique_id)597 ZigLLVMDIType *ZigLLVMCreateDebugEnumerationType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
598         const char *name, ZigLLVMDIFile *file, unsigned line_number, uint64_t size_in_bits,
599         uint64_t align_in_bits, ZigLLVMDIEnumerator **enumerator_array, int enumerator_array_len,
600         ZigLLVMDIType *underlying_type, const char *unique_id)
601 {
602     SmallVector<Metadata *, 8> fields;
603     for (int i = 0; i < enumerator_array_len; i += 1) {
604         DIEnumerator *dienumerator = reinterpret_cast<DIEnumerator*>(enumerator_array[i]);
605         fields.push_back(dienumerator);
606     }
607     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createEnumerationType(
608             reinterpret_cast<DIScope*>(scope),
609             name,
610             reinterpret_cast<DIFile*>(file),
611             line_number, size_in_bits, align_in_bits,
612             reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields),
613             reinterpret_cast<DIType*>(underlying_type),
614             unique_id);
615     return reinterpret_cast<ZigLLVMDIType*>(di_type);
616 }
617 
ZigLLVMCreateDebugMemberType(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIScope * scope,const char * name,ZigLLVMDIFile * file,unsigned line,uint64_t size_in_bits,uint64_t align_in_bits,uint64_t offset_in_bits,unsigned flags,ZigLLVMDIType * type)618 ZigLLVMDIType *ZigLLVMCreateDebugMemberType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
619         const char *name, ZigLLVMDIFile *file, unsigned line, uint64_t size_in_bits,
620         uint64_t align_in_bits, uint64_t offset_in_bits, unsigned flags, ZigLLVMDIType *type)
621 {
622     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createMemberType(
623             reinterpret_cast<DIScope*>(scope),
624             name,
625             reinterpret_cast<DIFile*>(file),
626             line, size_in_bits, align_in_bits, offset_in_bits,
627             static_cast<DINode::DIFlags>(flags),
628             reinterpret_cast<DIType*>(type));
629     return reinterpret_cast<ZigLLVMDIType*>(di_type);
630 }
631 
ZigLLVMCreateDebugUnionType(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIScope * scope,const char * name,ZigLLVMDIFile * file,unsigned line_number,uint64_t size_in_bits,uint64_t align_in_bits,unsigned flags,ZigLLVMDIType ** types_array,int types_array_len,unsigned run_time_lang,const char * unique_id)632 ZigLLVMDIType *ZigLLVMCreateDebugUnionType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
633         const char *name, ZigLLVMDIFile *file, unsigned line_number, uint64_t size_in_bits,
634         uint64_t align_in_bits, unsigned flags, ZigLLVMDIType **types_array, int types_array_len,
635         unsigned run_time_lang, const char *unique_id)
636 {
637     SmallVector<Metadata *, 8> fields;
638     for (int i = 0; i < types_array_len; i += 1) {
639         DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
640         fields.push_back(ditype);
641     }
642     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createUnionType(
643             reinterpret_cast<DIScope*>(scope),
644             name,
645             reinterpret_cast<DIFile*>(file),
646             line_number, size_in_bits, align_in_bits,
647             static_cast<DINode::DIFlags>(flags),
648             reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields),
649             run_time_lang, unique_id);
650     return reinterpret_cast<ZigLLVMDIType*>(di_type);
651 }
652 
ZigLLVMCreateDebugStructType(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIScope * scope,const char * name,ZigLLVMDIFile * file,unsigned line_number,uint64_t size_in_bits,uint64_t align_in_bits,unsigned flags,ZigLLVMDIType * derived_from,ZigLLVMDIType ** types_array,int types_array_len,unsigned run_time_lang,ZigLLVMDIType * vtable_holder,const char * unique_id)653 ZigLLVMDIType *ZigLLVMCreateDebugStructType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
654         const char *name, ZigLLVMDIFile *file, unsigned line_number, uint64_t size_in_bits,
655         uint64_t align_in_bits, unsigned flags, ZigLLVMDIType *derived_from,
656         ZigLLVMDIType **types_array, int types_array_len, unsigned run_time_lang, ZigLLVMDIType *vtable_holder,
657         const char *unique_id)
658 {
659     SmallVector<Metadata *, 8> fields;
660     for (int i = 0; i < types_array_len; i += 1) {
661         DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
662         fields.push_back(ditype);
663     }
664     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createStructType(
665             reinterpret_cast<DIScope*>(scope),
666             name,
667             reinterpret_cast<DIFile*>(file),
668             line_number, size_in_bits, align_in_bits,
669             static_cast<DINode::DIFlags>(flags),
670             reinterpret_cast<DIType*>(derived_from),
671             reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields),
672             run_time_lang,
673             reinterpret_cast<DIType*>(vtable_holder),
674             unique_id);
675     return reinterpret_cast<ZigLLVMDIType*>(di_type);
676 }
677 
ZigLLVMCreateReplaceableCompositeType(ZigLLVMDIBuilder * dibuilder,unsigned tag,const char * name,ZigLLVMDIScope * scope,ZigLLVMDIFile * file,unsigned line)678 ZigLLVMDIType *ZigLLVMCreateReplaceableCompositeType(ZigLLVMDIBuilder *dibuilder, unsigned tag,
679         const char *name, ZigLLVMDIScope *scope, ZigLLVMDIFile *file, unsigned line)
680 {
681     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createReplaceableCompositeType(
682             tag, name,
683             reinterpret_cast<DIScope*>(scope),
684             reinterpret_cast<DIFile*>(file),
685             line);
686     return reinterpret_cast<ZigLLVMDIType*>(di_type);
687 }
688 
ZigLLVMCreateDebugForwardDeclType(ZigLLVMDIBuilder * dibuilder,unsigned tag,const char * name,ZigLLVMDIScope * scope,ZigLLVMDIFile * file,unsigned line)689 ZigLLVMDIType *ZigLLVMCreateDebugForwardDeclType(ZigLLVMDIBuilder *dibuilder, unsigned tag,
690         const char *name, ZigLLVMDIScope *scope, ZigLLVMDIFile *file, unsigned line)
691 {
692     DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createForwardDecl(
693             tag, name,
694             reinterpret_cast<DIScope*>(scope),
695             reinterpret_cast<DIFile*>(file),
696             line);
697     return reinterpret_cast<ZigLLVMDIType*>(di_type);
698 }
699 
ZigLLVMReplaceTemporary(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIType * type,ZigLLVMDIType * replacement)700 void ZigLLVMReplaceTemporary(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIType *type,
701         ZigLLVMDIType *replacement)
702 {
703     reinterpret_cast<DIBuilder*>(dibuilder)->replaceTemporary(
704             TempDIType(reinterpret_cast<DIType*>(type)),
705             reinterpret_cast<DIType*>(replacement));
706 }
707 
ZigLLVMReplaceDebugArrays(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIType * type,ZigLLVMDIType ** types_array,int types_array_len)708 void ZigLLVMReplaceDebugArrays(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIType *type,
709         ZigLLVMDIType **types_array, int types_array_len)
710 {
711     SmallVector<Metadata *, 8> fields;
712     for (int i = 0; i < types_array_len; i += 1) {
713         DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
714         fields.push_back(ditype);
715     }
716     DICompositeType *composite_type = (DICompositeType*)reinterpret_cast<DIType*>(type);
717     reinterpret_cast<DIBuilder*>(dibuilder)->replaceArrays(
718             composite_type,
719             reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields));
720 }
721 
ZigLLVMCreateSubroutineType(ZigLLVMDIBuilder * dibuilder_wrapped,ZigLLVMDIType ** types_array,int types_array_len,unsigned flags)722 ZigLLVMDIType *ZigLLVMCreateSubroutineType(ZigLLVMDIBuilder *dibuilder_wrapped,
723         ZigLLVMDIType **types_array, int types_array_len, unsigned flags)
724 {
725     SmallVector<Metadata *, 8> types;
726     for (int i = 0; i < types_array_len; i += 1) {
727         DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
728         types.push_back(ditype);
729     }
730     DIBuilder *dibuilder = reinterpret_cast<DIBuilder*>(dibuilder_wrapped);
731     DISubroutineType *subroutine_type = dibuilder->createSubroutineType(
732             dibuilder->getOrCreateTypeArray(types),
733             static_cast<DINode::DIFlags>(flags));
734     DIType *ditype = subroutine_type;
735     return reinterpret_cast<ZigLLVMDIType*>(ditype);
736 }
737 
ZigLLVMEncoding_DW_ATE_unsigned(void)738 unsigned ZigLLVMEncoding_DW_ATE_unsigned(void) {
739     return dwarf::DW_ATE_unsigned;
740 }
741 
ZigLLVMEncoding_DW_ATE_signed(void)742 unsigned ZigLLVMEncoding_DW_ATE_signed(void) {
743     return dwarf::DW_ATE_signed;
744 }
745 
ZigLLVMEncoding_DW_ATE_float(void)746 unsigned ZigLLVMEncoding_DW_ATE_float(void) {
747     return dwarf::DW_ATE_float;
748 }
749 
ZigLLVMEncoding_DW_ATE_boolean(void)750 unsigned ZigLLVMEncoding_DW_ATE_boolean(void) {
751     return dwarf::DW_ATE_boolean;
752 }
753 
ZigLLVMEncoding_DW_ATE_unsigned_char(void)754 unsigned ZigLLVMEncoding_DW_ATE_unsigned_char(void) {
755     return dwarf::DW_ATE_unsigned_char;
756 }
757 
ZigLLVMEncoding_DW_ATE_signed_char(void)758 unsigned ZigLLVMEncoding_DW_ATE_signed_char(void) {
759     return dwarf::DW_ATE_signed_char;
760 }
761 
ZigLLVMLang_DW_LANG_C99(void)762 unsigned ZigLLVMLang_DW_LANG_C99(void) {
763     return dwarf::DW_LANG_C99;
764 }
765 
ZigLLVMTag_DW_variable(void)766 unsigned ZigLLVMTag_DW_variable(void) {
767     return dwarf::DW_TAG_variable;
768 }
769 
ZigLLVMTag_DW_structure_type(void)770 unsigned ZigLLVMTag_DW_structure_type(void) {
771     return dwarf::DW_TAG_structure_type;
772 }
773 
ZigLLVMTag_DW_enumeration_type(void)774 unsigned ZigLLVMTag_DW_enumeration_type(void) {
775     return dwarf::DW_TAG_enumeration_type;
776 }
777 
ZigLLVMTag_DW_union_type(void)778 unsigned ZigLLVMTag_DW_union_type(void) {
779     return dwarf::DW_TAG_union_type;
780 }
781 
ZigLLVMCreateDIBuilder(LLVMModuleRef module,bool allow_unresolved)782 ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {
783     DIBuilder *di_builder = new(std::nothrow) DIBuilder(*unwrap(module), allow_unresolved);
784     if (di_builder == nullptr)
785         return nullptr;
786     return reinterpret_cast<ZigLLVMDIBuilder *>(di_builder);
787 }
788 
ZigLLVMDisposeDIBuilder(ZigLLVMDIBuilder * dbuilder)789 void ZigLLVMDisposeDIBuilder(ZigLLVMDIBuilder *dbuilder) {
790     DIBuilder *di_builder = reinterpret_cast<DIBuilder *>(dbuilder);
791     delete di_builder;
792 }
793 
ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder,int line,int column,ZigLLVMDIScope * scope)794 void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, ZigLLVMDIScope *scope) {
795     DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
796     DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, column, di_scope, nullptr, false);
797     unwrap(builder)->SetCurrentDebugLocation(debug_loc);
798 }
799 
ZigLLVMClearCurrentDebugLocation(LLVMBuilderRef builder)800 void ZigLLVMClearCurrentDebugLocation(LLVMBuilderRef builder) {
801     unwrap(builder)->SetCurrentDebugLocation(DebugLoc());
802 }
803 
804 
ZigLLVMCreateLexicalBlock(ZigLLVMDIBuilder * dbuilder,ZigLLVMDIScope * scope,ZigLLVMDIFile * file,unsigned line,unsigned col)805 ZigLLVMDILexicalBlock *ZigLLVMCreateLexicalBlock(ZigLLVMDIBuilder *dbuilder, ZigLLVMDIScope *scope,
806         ZigLLVMDIFile *file, unsigned line, unsigned col)
807 {
808     DILexicalBlock *result = reinterpret_cast<DIBuilder*>(dbuilder)->createLexicalBlock(
809             reinterpret_cast<DIScope*>(scope),
810             reinterpret_cast<DIFile*>(file),
811             line,
812             col);
813     return reinterpret_cast<ZigLLVMDILexicalBlock*>(result);
814 }
815 
ZigLLVMCreateAutoVariable(ZigLLVMDIBuilder * dbuilder,ZigLLVMDIScope * scope,const char * name,ZigLLVMDIFile * file,unsigned line_no,ZigLLVMDIType * type,bool always_preserve,unsigned flags)816 ZigLLVMDILocalVariable *ZigLLVMCreateAutoVariable(ZigLLVMDIBuilder *dbuilder,
817         ZigLLVMDIScope *scope, const char *name, ZigLLVMDIFile *file, unsigned line_no,
818         ZigLLVMDIType *type, bool always_preserve, unsigned flags)
819 {
820     DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createAutoVariable(
821             reinterpret_cast<DIScope*>(scope),
822             name,
823             reinterpret_cast<DIFile*>(file),
824             line_no,
825             reinterpret_cast<DIType*>(type),
826             always_preserve,
827             static_cast<DINode::DIFlags>(flags));
828     return reinterpret_cast<ZigLLVMDILocalVariable*>(result);
829 }
830 
ZigLLVMCreateGlobalVariable(ZigLLVMDIBuilder * dbuilder,ZigLLVMDIScope * scope,const char * name,const char * linkage_name,ZigLLVMDIFile * file,unsigned line_no,ZigLLVMDIType * di_type,bool is_local_to_unit)831 ZigLLVMDIGlobalVariable *ZigLLVMCreateGlobalVariable(ZigLLVMDIBuilder *dbuilder,
832     ZigLLVMDIScope *scope, const char *name, const char *linkage_name, ZigLLVMDIFile *file,
833     unsigned line_no, ZigLLVMDIType *di_type, bool is_local_to_unit)
834 {
835     DIGlobalVariableExpression *result = reinterpret_cast<DIBuilder*>(dbuilder)->createGlobalVariableExpression(
836         reinterpret_cast<DIScope*>(scope),
837         name,
838         linkage_name,
839         reinterpret_cast<DIFile*>(file),
840         line_no,
841         reinterpret_cast<DIType*>(di_type),
842         is_local_to_unit);
843     return reinterpret_cast<ZigLLVMDIGlobalVariable*>(result);
844 }
845 
ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder * dbuilder,ZigLLVMDIScope * scope,const char * name,ZigLLVMDIFile * file,unsigned line_no,ZigLLVMDIType * type,bool always_preserve,unsigned flags,unsigned arg_no)846 ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder *dbuilder,
847         ZigLLVMDIScope *scope, const char *name, ZigLLVMDIFile *file, unsigned line_no,
848         ZigLLVMDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)
849 {
850     assert(arg_no != 0);
851     DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createParameterVariable(
852             reinterpret_cast<DIScope*>(scope),
853             name,
854             arg_no,
855             reinterpret_cast<DIFile*>(file),
856             line_no,
857             reinterpret_cast<DIType*>(type),
858             always_preserve,
859             static_cast<DINode::DIFlags>(flags));
860     return reinterpret_cast<ZigLLVMDILocalVariable*>(result);
861 }
862 
ZigLLVMLexicalBlockToScope(ZigLLVMDILexicalBlock * lexical_block)863 ZigLLVMDIScope *ZigLLVMLexicalBlockToScope(ZigLLVMDILexicalBlock *lexical_block) {
864     DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);
865     return reinterpret_cast<ZigLLVMDIScope*>(scope);
866 }
867 
ZigLLVMCompileUnitToScope(ZigLLVMDICompileUnit * compile_unit)868 ZigLLVMDIScope *ZigLLVMCompileUnitToScope(ZigLLVMDICompileUnit *compile_unit) {
869     DIScope *scope = reinterpret_cast<DICompileUnit*>(compile_unit);
870     return reinterpret_cast<ZigLLVMDIScope*>(scope);
871 }
872 
ZigLLVMFileToScope(ZigLLVMDIFile * difile)873 ZigLLVMDIScope *ZigLLVMFileToScope(ZigLLVMDIFile *difile) {
874     DIScope *scope = reinterpret_cast<DIFile*>(difile);
875     return reinterpret_cast<ZigLLVMDIScope*>(scope);
876 }
877 
ZigLLVMSubprogramToScope(ZigLLVMDISubprogram * subprogram)878 ZigLLVMDIScope *ZigLLVMSubprogramToScope(ZigLLVMDISubprogram *subprogram) {
879     DIScope *scope = reinterpret_cast<DISubprogram*>(subprogram);
880     return reinterpret_cast<ZigLLVMDIScope*>(scope);
881 }
882 
ZigLLVMTypeToScope(ZigLLVMDIType * type)883 ZigLLVMDIScope *ZigLLVMTypeToScope(ZigLLVMDIType *type) {
884     DIScope *scope = reinterpret_cast<DIType*>(type);
885     return reinterpret_cast<ZigLLVMDIScope*>(scope);
886 }
887 
ZigLLVMCreateCompileUnit(ZigLLVMDIBuilder * dibuilder,unsigned lang,ZigLLVMDIFile * difile,const char * producer,bool is_optimized,const char * flags,unsigned runtime_version,const char * split_name,uint64_t dwo_id,bool emit_debug_info)888 ZigLLVMDICompileUnit *ZigLLVMCreateCompileUnit(ZigLLVMDIBuilder *dibuilder,
889         unsigned lang, ZigLLVMDIFile *difile, const char *producer,
890         bool is_optimized, const char *flags, unsigned runtime_version, const char *split_name,
891         uint64_t dwo_id, bool emit_debug_info)
892 {
893     DICompileUnit *result = reinterpret_cast<DIBuilder*>(dibuilder)->createCompileUnit(
894             lang,
895             reinterpret_cast<DIFile*>(difile),
896             producer, is_optimized, flags, runtime_version, split_name,
897             (emit_debug_info ? DICompileUnit::DebugEmissionKind::FullDebug : DICompileUnit::DebugEmissionKind::NoDebug),
898             dwo_id);
899     return reinterpret_cast<ZigLLVMDICompileUnit*>(result);
900 }
901 
902 
ZigLLVMCreateFile(ZigLLVMDIBuilder * dibuilder,const char * filename,const char * directory)903 ZigLLVMDIFile *ZigLLVMCreateFile(ZigLLVMDIBuilder *dibuilder, const char *filename, const char *directory) {
904     DIFile *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFile(filename, directory);
905     return reinterpret_cast<ZigLLVMDIFile*>(result);
906 }
907 
ZigLLVMCreateFunction(ZigLLVMDIBuilder * dibuilder,ZigLLVMDIScope * scope,const char * name,const char * linkage_name,ZigLLVMDIFile * file,unsigned lineno,ZigLLVMDIType * fn_di_type,bool is_local_to_unit,bool is_definition,unsigned scope_line,unsigned flags,bool is_optimized,ZigLLVMDISubprogram * decl_subprogram)908 ZigLLVMDISubprogram *ZigLLVMCreateFunction(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
909         const char *name, const char *linkage_name, ZigLLVMDIFile *file, unsigned lineno,
910         ZigLLVMDIType *fn_di_type, bool is_local_to_unit, bool is_definition, unsigned scope_line,
911         unsigned flags, bool is_optimized, ZigLLVMDISubprogram *decl_subprogram)
912 {
913     DISubroutineType *di_sub_type = static_cast<DISubroutineType*>(reinterpret_cast<DIType*>(fn_di_type));
914     DISubprogram *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFunction(
915             reinterpret_cast<DIScope*>(scope),
916             name, linkage_name,
917             reinterpret_cast<DIFile*>(file),
918             lineno,
919             di_sub_type,
920             scope_line,
921             static_cast<DINode::DIFlags>(flags),
922             DISubprogram::toSPFlags(is_local_to_unit, is_definition, is_optimized),
923             nullptr,
924             reinterpret_cast<DISubprogram *>(decl_subprogram),
925             nullptr);
926     return reinterpret_cast<ZigLLVMDISubprogram*>(result);
927 }
928 
ZigLLVMDIBuilderFinalize(ZigLLVMDIBuilder * dibuilder)929 void ZigLLVMDIBuilderFinalize(ZigLLVMDIBuilder *dibuilder) {
930     reinterpret_cast<DIBuilder*>(dibuilder)->finalize();
931 }
932 
ZigLLVMInsertDeclareAtEnd(ZigLLVMDIBuilder * dibuilder,LLVMValueRef storage,ZigLLVMDILocalVariable * var_info,ZigLLVMDILocation * debug_loc,LLVMBasicBlockRef basic_block_ref)933 LLVMValueRef ZigLLVMInsertDeclareAtEnd(ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
934         ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref)
935 {
936     Instruction *result = reinterpret_cast<DIBuilder*>(dibuilder)->insertDeclare(
937             unwrap(storage),
938             reinterpret_cast<DILocalVariable *>(var_info),
939             reinterpret_cast<DIBuilder*>(dibuilder)->createExpression(),
940             reinterpret_cast<DILocation*>(debug_loc),
941             static_cast<BasicBlock*>(unwrap(basic_block_ref)));
942     return wrap(result);
943 }
944 
ZigLLVMInsertDeclare(ZigLLVMDIBuilder * dibuilder,LLVMValueRef storage,ZigLLVMDILocalVariable * var_info,ZigLLVMDILocation * debug_loc,LLVMValueRef insert_before_instr)945 LLVMValueRef ZigLLVMInsertDeclare(ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
946         ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr)
947 {
948     Instruction *result = reinterpret_cast<DIBuilder*>(dibuilder)->insertDeclare(
949             unwrap(storage),
950             reinterpret_cast<DILocalVariable *>(var_info),
951             reinterpret_cast<DIBuilder*>(dibuilder)->createExpression(),
952             reinterpret_cast<DILocation*>(debug_loc),
953             static_cast<Instruction*>(unwrap(insert_before_instr)));
954     return wrap(result);
955 }
956 
ZigLLVMGetDebugLoc(unsigned line,unsigned col,ZigLLVMDIScope * scope)957 ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigned col, ZigLLVMDIScope *scope) {
958     DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
959     DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, col, di_scope, nullptr, false);
960     return reinterpret_cast<ZigLLVMDILocation*>(debug_loc.get());
961 }
962 
ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped,bool on_state)963 void ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state) {
964     if (on_state) {
965         FastMathFlags fmf;
966         fmf.setFast();
967         unwrap(builder_wrapped)->setFastMathFlags(fmf);
968     } else {
969         unwrap(builder_wrapped)->clearFastMathFlags();
970     }
971 }
972 
ZigLLVMAddByValAttr(LLVMValueRef fn_ref,unsigned ArgNo,LLVMTypeRef type_val)973 void ZigLLVMAddByValAttr(LLVMValueRef fn_ref, unsigned ArgNo, LLVMTypeRef type_val) {
974     Function *func = unwrap<Function>(fn_ref);
975     const AttributeList attr_set = func->getAttributes();
976     AttrBuilder attr_builder;
977     Type *llvm_type = unwrap<Type>(type_val);
978     attr_builder.addByValAttr(llvm_type);
979     const AttributeList new_attr_set = attr_set.addAttributes(func->getContext(), ArgNo + 1, attr_builder);
980     func->setAttributes(new_attr_set);
981 }
982 
ZigLLVMAddSretAttr(LLVMValueRef fn_ref,unsigned ArgNo,LLVMTypeRef type_val)983 void ZigLLVMAddSretAttr(LLVMValueRef fn_ref, unsigned ArgNo, LLVMTypeRef type_val) {
984     Function *func = unwrap<Function>(fn_ref);
985     const AttributeList attr_set = func->getAttributes();
986     AttrBuilder attr_builder;
987     Type *llvm_type = unwrap<Type>(type_val);
988     attr_builder.addStructRetAttr(llvm_type);
989     const AttributeList new_attr_set = attr_set.addAttributes(func->getContext(), ArgNo + 1, attr_builder);
990     func->setAttributes(new_attr_set);
991 }
992 
ZigLLVMAddFunctionAttr(LLVMValueRef fn_ref,const char * attr_name,const char * attr_value)993 void ZigLLVMAddFunctionAttr(LLVMValueRef fn_ref, const char *attr_name, const char *attr_value) {
994     Function *func = unwrap<Function>(fn_ref);
995     const AttributeList attr_set = func->getAttributes();
996     AttrBuilder attr_builder;
997     if (attr_value) {
998         attr_builder.addAttribute(attr_name, attr_value);
999     } else {
1000         attr_builder.addAttribute(attr_name);
1001     }
1002     const AttributeList new_attr_set = attr_set.addAttributes(func->getContext(),
1003             AttributeList::FunctionIndex, attr_builder);
1004     func->setAttributes(new_attr_set);
1005 }
1006 
ZigLLVMAddFunctionAttrCold(LLVMValueRef fn_ref)1007 void ZigLLVMAddFunctionAttrCold(LLVMValueRef fn_ref) {
1008     Function *func = unwrap<Function>(fn_ref);
1009     const AttributeList attr_set = func->getAttributes();
1010     const AttributeList new_attr_set = attr_set.addAttribute(func->getContext(), AttributeList::FunctionIndex,
1011             Attribute::Cold);
1012     func->setAttributes(new_attr_set);
1013 }
1014 
ZigLLVMParseCommandLineOptions(size_t argc,const char * const * argv)1015 void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
1016     cl::ParseCommandLineOptions(argc, argv);
1017 }
1018 
ZigLLVMGetArchTypeName(ZigLLVM_ArchType arch)1019 const char *ZigLLVMGetArchTypeName(ZigLLVM_ArchType arch) {
1020     return (const char*)Triple::getArchTypeName((Triple::ArchType)arch).bytes_begin();
1021 }
1022 
ZigLLVMGetVendorTypeName(ZigLLVM_VendorType vendor)1023 const char *ZigLLVMGetVendorTypeName(ZigLLVM_VendorType vendor) {
1024     return (const char*)Triple::getVendorTypeName((Triple::VendorType)vendor).bytes_begin();
1025 }
1026 
ZigLLVMGetOSTypeName(ZigLLVM_OSType os)1027 const char *ZigLLVMGetOSTypeName(ZigLLVM_OSType os) {
1028     const char* name = (const char*)Triple::getOSTypeName((Triple::OSType)os).bytes_begin();
1029     if (strcmp(name, "macosx") == 0) return "macos";
1030     return name;
1031 }
1032 
ZigLLVMGetEnvironmentTypeName(ZigLLVM_EnvironmentType env_type)1033 const char *ZigLLVMGetEnvironmentTypeName(ZigLLVM_EnvironmentType env_type) {
1034     return (const char*)Triple::getEnvironmentTypeName((Triple::EnvironmentType)env_type).bytes_begin();
1035 }
1036 
ZigLLVMGetNativeTarget(ZigLLVM_ArchType * arch_type,ZigLLVM_VendorType * vendor_type,ZigLLVM_OSType * os_type,ZigLLVM_EnvironmentType * environ_type,ZigLLVM_ObjectFormatType * oformat)1037 void ZigLLVMGetNativeTarget(ZigLLVM_ArchType *arch_type,
1038         ZigLLVM_VendorType *vendor_type, ZigLLVM_OSType *os_type, ZigLLVM_EnvironmentType *environ_type,
1039         ZigLLVM_ObjectFormatType *oformat)
1040 {
1041     char *native_triple = LLVMGetDefaultTargetTriple();
1042     Triple triple(Triple::normalize(native_triple));
1043 
1044     *arch_type = (ZigLLVM_ArchType)triple.getArch();
1045     *vendor_type = (ZigLLVM_VendorType)triple.getVendor();
1046     *os_type = (ZigLLVM_OSType)triple.getOS();
1047     *environ_type = (ZigLLVM_EnvironmentType)triple.getEnvironment();
1048     *oformat = (ZigLLVM_ObjectFormatType)triple.getObjectFormat();
1049 
1050     free(native_triple);
1051 }
1052 
ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module)1053 void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module) {
1054     unwrap(module)->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION);
1055     unwrap(module)->addModuleFlag(Module::Warning, "Dwarf Version", 4);
1056 }
1057 
ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module)1058 void ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module) {
1059     unwrap(module)->addModuleFlag(Module::Warning, "CodeView", 1);
1060 }
1061 
ZigLLVMSetModulePICLevel(LLVMModuleRef module)1062 void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {
1063     unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);
1064 }
1065 
ZigLLVMSetModulePIELevel(LLVMModuleRef module)1066 void ZigLLVMSetModulePIELevel(LLVMModuleRef module) {
1067     unwrap(module)->setPIELevel(PIELevel::Level::Large);
1068 }
1069 
ZigLLVMSetModuleCodeModel(LLVMModuleRef module,LLVMCodeModel code_model)1070 void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
1071     bool JIT;
1072     unwrap(module)->setCodeModel(*unwrap(code_model, JIT));
1073     assert(!JIT);
1074 }
1075 
ZigLLVMBuildNSWShl(LLVMBuilderRef builder,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)1076 LLVMValueRef ZigLLVMBuildNSWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
1077         const char *name)
1078 {
1079     return wrap(unwrap(builder)->CreateShl(unwrap(LHS), unwrap(RHS), name, false, true));
1080 }
1081 
ZigLLVMBuildNUWShl(LLVMBuilderRef builder,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)1082 LLVMValueRef ZigLLVMBuildNUWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
1083         const char *name)
1084 {
1085     return wrap(unwrap(builder)->CreateShl(unwrap(LHS), unwrap(RHS), name, true, false));
1086 }
1087 
ZigLLVMBuildLShrExact(LLVMBuilderRef builder,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)1088 LLVMValueRef ZigLLVMBuildLShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
1089         const char *name)
1090 {
1091     return wrap(unwrap(builder)->CreateLShr(unwrap(LHS), unwrap(RHS), name, true));
1092 }
1093 
ZigLLVMBuildAShrExact(LLVMBuilderRef builder,LLVMValueRef LHS,LLVMValueRef RHS,const char * name)1094 LLVMValueRef ZigLLVMBuildAShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
1095         const char *name)
1096 {
1097     return wrap(unwrap(builder)->CreateAShr(unwrap(LHS), unwrap(RHS), name, true));
1098 }
1099 
ZigLLVMSetTailCall(LLVMValueRef Call)1100 void ZigLLVMSetTailCall(LLVMValueRef Call) {
1101     unwrap<CallInst>(Call)->setTailCallKind(CallInst::TCK_MustTail);
1102 }
1103 
ZigLLVMSetCallSret(LLVMValueRef Call,LLVMTypeRef return_type)1104 void ZigLLVMSetCallSret(LLVMValueRef Call, LLVMTypeRef return_type) {
1105     const AttributeList attr_set = unwrap<CallInst>(Call)->getAttributes();
1106     AttrBuilder attr_builder;
1107     Type *llvm_type = unwrap<Type>(return_type);
1108     attr_builder.addStructRetAttr(llvm_type);
1109     const AttributeList new_attr_set = attr_set.addAttributes(unwrap<CallInst>(Call)->getContext(), 1, attr_builder);
1110     unwrap<CallInst>(Call)->setAttributes(new_attr_set);
1111 }
1112 
ZigLLVMFunctionSetPrefixData(LLVMValueRef function,LLVMValueRef data)1113 void ZigLLVMFunctionSetPrefixData(LLVMValueRef function, LLVMValueRef data) {
1114     unwrap<Function>(function)->setPrefixData(unwrap<Constant>(data));
1115 }
1116 
ZigLLVMFunctionSetCallingConv(LLVMValueRef function,ZigLLVM_CallingConv cc)1117 void ZigLLVMFunctionSetCallingConv(LLVMValueRef function, ZigLLVM_CallingConv cc) {
1118     unwrap<Function>(function)->setCallingConv(static_cast<CallingConv::ID>(cc));
1119 }
1120 
1121 class MyOStream: public raw_ostream {
1122     public:
MyOStream(void (* _append_diagnostic)(void *,const char *,size_t),void * _context)1123         MyOStream(void (*_append_diagnostic)(void *, const char *, size_t), void *_context) :
1124             raw_ostream(true), append_diagnostic(_append_diagnostic), context(_context), pos(0) {
1125 
1126         }
write_impl(const char * ptr,size_t len)1127         void write_impl(const char *ptr, size_t len) override {
1128             append_diagnostic(context, ptr, len);
1129             pos += len;
1130         }
current_pos() const1131         uint64_t current_pos() const override {
1132             return pos;
1133         }
1134         void (*append_diagnostic)(void *, const char *, size_t);
1135         void *context;
1136         size_t pos;
1137 };
1138 
ZigLLVMWriteImportLibrary(const char * def_path,const ZigLLVM_ArchType arch,const char * output_lib_path,bool kill_at)1139 bool ZigLLVMWriteImportLibrary(const char *def_path, const ZigLLVM_ArchType arch,
1140                                const char *output_lib_path, bool kill_at)
1141 {
1142     COFF::MachineTypes machine = COFF::IMAGE_FILE_MACHINE_UNKNOWN;
1143 
1144     switch (arch) {
1145         case ZigLLVM_x86:
1146             machine = COFF::IMAGE_FILE_MACHINE_I386;
1147             break;
1148         case ZigLLVM_x86_64:
1149             machine = COFF::IMAGE_FILE_MACHINE_AMD64;
1150             break;
1151         case ZigLLVM_arm:
1152         case ZigLLVM_armeb:
1153         case ZigLLVM_thumb:
1154         case ZigLLVM_thumbeb:
1155             machine = COFF::IMAGE_FILE_MACHINE_ARMNT;
1156             break;
1157         case ZigLLVM_aarch64:
1158         case ZigLLVM_aarch64_be:
1159             machine = COFF::IMAGE_FILE_MACHINE_ARM64;
1160             break;
1161         default:
1162             break;
1163     }
1164 
1165     if (machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
1166         return true;
1167     }
1168 
1169     auto bufOrErr = MemoryBuffer::getFile(def_path);
1170     if (!bufOrErr) {
1171         return false;
1172     }
1173 
1174     MemoryBuffer& buf = *bufOrErr.get();
1175     Expected<object::COFFModuleDefinition> def =
1176         object::parseCOFFModuleDefinition(buf, machine, /* MingwDef */ true);
1177 
1178     if (!def) {
1179         return true;
1180     }
1181 
1182     // The exports-juggling code below is ripped from LLVM's DllToolDriver.cpp
1183 
1184     // If ExtName is set (if the "ExtName = Name" syntax was used), overwrite
1185     // Name with ExtName and clear ExtName. When only creating an import
1186     // library and not linking, the internal name is irrelevant. This avoids
1187     // cases where writeImportLibrary tries to transplant decoration from
1188     // symbol decoration onto ExtName.
1189     for (object::COFFShortExport& E : def->Exports) {
1190         if (!E.ExtName.empty()) {
1191             E.Name = E.ExtName;
1192             E.ExtName.clear();
1193         }
1194     }
1195 
1196     if (machine == COFF::IMAGE_FILE_MACHINE_I386 && kill_at) {
1197         for (object::COFFShortExport& E : def->Exports) {
1198             if (!E.AliasTarget.empty() || (!E.Name.empty() && E.Name[0] == '?'))
1199                 continue;
1200             E.SymbolName = E.Name;
1201             // Trim off the trailing decoration. Symbols will always have a
1202             // starting prefix here (either _ for cdecl/stdcall, @ for fastcall
1203             // or ? for C++ functions). Vectorcall functions won't have any
1204             // fixed prefix, but the function base name will still be at least
1205             // one char.
1206             E.Name = E.Name.substr(0, E.Name.find('@', 1));
1207             // By making sure E.SymbolName != E.Name for decorated symbols,
1208             // writeImportLibrary writes these symbols with the type
1209             // IMPORT_NAME_UNDECORATE.
1210         }
1211     }
1212 
1213     return static_cast<bool>(
1214         object::writeImportLibrary(def->OutputFile, output_lib_path,
1215                                    def->Exports, machine, /* MinGW */ true));
1216 }
1217 
ZigLLVMWriteArchive(const char * archive_name,const char ** file_names,size_t file_name_count,ZigLLVM_OSType os_type)1218 bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count,
1219         ZigLLVM_OSType os_type)
1220 {
1221     object::Archive::Kind kind;
1222     switch (os_type) {
1223         case ZigLLVM_Win32:
1224             // For some reason llvm-lib passes K_GNU on windows.
1225             // See lib/ToolDrivers/llvm-lib/LibDriver.cpp:168 in libDriverMain
1226             kind = object::Archive::K_GNU;
1227             break;
1228         case ZigLLVM_Linux:
1229             kind = object::Archive::K_GNU;
1230             break;
1231         case ZigLLVM_MacOSX:
1232         case ZigLLVM_Darwin:
1233         case ZigLLVM_IOS:
1234             kind = object::Archive::K_DARWIN;
1235             break;
1236         case ZigLLVM_OpenBSD:
1237         case ZigLLVM_FreeBSD:
1238             kind = object::Archive::K_BSD;
1239             break;
1240         default:
1241             kind = object::Archive::K_GNU;
1242     }
1243     SmallVector<NewArchiveMember, 4> new_members;
1244     for (size_t i = 0; i < file_name_count; i += 1) {
1245         Expected<NewArchiveMember> new_member = NewArchiveMember::getFile(file_names[i], true);
1246         Error err = new_member.takeError();
1247         if (err) return true;
1248         new_members.push_back(std::move(*new_member));
1249     }
1250     Error err = writeArchive(archive_name, new_members, true, kind, true, false, nullptr);
1251     if (err) return true;
1252     return false;
1253 }
1254 
ZigLLDLinkCOFF(int argc,const char ** argv,bool can_exit_early)1255 int ZigLLDLinkCOFF(int argc, const char **argv, bool can_exit_early) {
1256     std::vector<const char *> args(argv, argv + argc);
1257     return lld::coff::link(args, can_exit_early, llvm::outs(), llvm::errs());
1258 }
1259 
ZigLLDLinkELF(int argc,const char ** argv,bool can_exit_early)1260 int ZigLLDLinkELF(int argc, const char **argv, bool can_exit_early) {
1261     std::vector<const char *> args(argv, argv + argc);
1262     return lld::elf::link(args, can_exit_early, llvm::outs(), llvm::errs());
1263 }
1264 
ZigLLDLinkWasm(int argc,const char ** argv,bool can_exit_early)1265 int ZigLLDLinkWasm(int argc, const char **argv, bool can_exit_early) {
1266     std::vector<const char *> args(argv, argv + argc);
1267     return lld::wasm::link(args, can_exit_early, llvm::outs(), llvm::errs());
1268 }
1269 
wrap(Attribute Attr)1270 inline LLVMAttributeRef wrap(Attribute Attr) {
1271     return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
1272 }
1273 
unwrap(LLVMAttributeRef Attr)1274 inline Attribute unwrap(LLVMAttributeRef Attr) {
1275     return Attribute::fromRawPointer(Attr);
1276 }
1277 
ZigLLVMBuildAndReduce(LLVMBuilderRef B,LLVMValueRef Val)1278 LLVMValueRef ZigLLVMBuildAndReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1279     return wrap(unwrap(B)->CreateAndReduce(unwrap(Val)));
1280 }
1281 
ZigLLVMBuildOrReduce(LLVMBuilderRef B,LLVMValueRef Val)1282 LLVMValueRef ZigLLVMBuildOrReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1283     return wrap(unwrap(B)->CreateOrReduce(unwrap(Val)));
1284 }
1285 
ZigLLVMBuildXorReduce(LLVMBuilderRef B,LLVMValueRef Val)1286 LLVMValueRef ZigLLVMBuildXorReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1287     return wrap(unwrap(B)->CreateXorReduce(unwrap(Val)));
1288 }
1289 
ZigLLVMBuildIntMaxReduce(LLVMBuilderRef B,LLVMValueRef Val,bool is_signed)1290 LLVMValueRef ZigLLVMBuildIntMaxReduce(LLVMBuilderRef B, LLVMValueRef Val, bool is_signed) {
1291     return wrap(unwrap(B)->CreateIntMaxReduce(unwrap(Val), is_signed));
1292 }
1293 
ZigLLVMBuildIntMinReduce(LLVMBuilderRef B,LLVMValueRef Val,bool is_signed)1294 LLVMValueRef ZigLLVMBuildIntMinReduce(LLVMBuilderRef B, LLVMValueRef Val, bool is_signed) {
1295     return wrap(unwrap(B)->CreateIntMinReduce(unwrap(Val), is_signed));
1296 }
1297 
ZigLLVMBuildFPMaxReduce(LLVMBuilderRef B,LLVMValueRef Val)1298 LLVMValueRef ZigLLVMBuildFPMaxReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1299     return wrap(unwrap(B)->CreateFPMaxReduce(unwrap(Val)));
1300 }
1301 
ZigLLVMBuildFPMinReduce(LLVMBuilderRef B,LLVMValueRef Val)1302 LLVMValueRef ZigLLVMBuildFPMinReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1303     return wrap(unwrap(B)->CreateFPMinReduce(unwrap(Val)));
1304 }
1305 
ZigLLVMBuildAddReduce(LLVMBuilderRef B,LLVMValueRef Val)1306 LLVMValueRef ZigLLVMBuildAddReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1307     return wrap(unwrap(B)->CreateAddReduce(unwrap(Val)));
1308 }
1309 
ZigLLVMBuildMulReduce(LLVMBuilderRef B,LLVMValueRef Val)1310 LLVMValueRef ZigLLVMBuildMulReduce(LLVMBuilderRef B, LLVMValueRef Val) {
1311     return wrap(unwrap(B)->CreateMulReduce(unwrap(Val)));
1312 }
1313 
ZigLLVMBuildFPAddReduce(LLVMBuilderRef B,LLVMValueRef Acc,LLVMValueRef Val)1314 LLVMValueRef ZigLLVMBuildFPAddReduce(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Val) {
1315     return wrap(unwrap(B)->CreateFAddReduce(unwrap(Acc), unwrap(Val)));
1316 }
1317 
ZigLLVMBuildFPMulReduce(LLVMBuilderRef B,LLVMValueRef Acc,LLVMValueRef Val)1318 LLVMValueRef ZigLLVMBuildFPMulReduce(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Val) {
1319     return wrap(unwrap(B)->CreateFMulReduce(unwrap(Acc), unwrap(Val)));
1320 }
1321 
1322 static_assert((Triple::ArchType)ZigLLVM_UnknownArch == Triple::UnknownArch, "");
1323 static_assert((Triple::ArchType)ZigLLVM_arm == Triple::arm, "");
1324 static_assert((Triple::ArchType)ZigLLVM_armeb == Triple::armeb, "");
1325 static_assert((Triple::ArchType)ZigLLVM_aarch64 == Triple::aarch64, "");
1326 static_assert((Triple::ArchType)ZigLLVM_aarch64_be == Triple::aarch64_be, "");
1327 static_assert((Triple::ArchType)ZigLLVM_aarch64_32 == Triple::aarch64_32, "");
1328 static_assert((Triple::ArchType)ZigLLVM_arc == Triple::arc, "");
1329 static_assert((Triple::ArchType)ZigLLVM_avr == Triple::avr, "");
1330 static_assert((Triple::ArchType)ZigLLVM_bpfel == Triple::bpfel, "");
1331 static_assert((Triple::ArchType)ZigLLVM_bpfeb == Triple::bpfeb, "");
1332 static_assert((Triple::ArchType)ZigLLVM_csky == Triple::csky, "");
1333 static_assert((Triple::ArchType)ZigLLVM_hexagon == Triple::hexagon, "");
1334 static_assert((Triple::ArchType)ZigLLVM_m68k == Triple::m68k, "");
1335 static_assert((Triple::ArchType)ZigLLVM_mips == Triple::mips, "");
1336 static_assert((Triple::ArchType)ZigLLVM_mipsel == Triple::mipsel, "");
1337 static_assert((Triple::ArchType)ZigLLVM_mips64 == Triple::mips64, "");
1338 static_assert((Triple::ArchType)ZigLLVM_mips64el == Triple::mips64el, "");
1339 static_assert((Triple::ArchType)ZigLLVM_msp430 == Triple::msp430, "");
1340 static_assert((Triple::ArchType)ZigLLVM_ppc == Triple::ppc, "");
1341 static_assert((Triple::ArchType)ZigLLVM_ppcle == Triple::ppcle, "");
1342 static_assert((Triple::ArchType)ZigLLVM_ppc64 == Triple::ppc64, "");
1343 static_assert((Triple::ArchType)ZigLLVM_ppc64le == Triple::ppc64le, "");
1344 static_assert((Triple::ArchType)ZigLLVM_r600 == Triple::r600, "");
1345 static_assert((Triple::ArchType)ZigLLVM_amdgcn == Triple::amdgcn, "");
1346 static_assert((Triple::ArchType)ZigLLVM_riscv32 == Triple::riscv32, "");
1347 static_assert((Triple::ArchType)ZigLLVM_riscv64 == Triple::riscv64, "");
1348 static_assert((Triple::ArchType)ZigLLVM_sparc == Triple::sparc, "");
1349 static_assert((Triple::ArchType)ZigLLVM_sparcv9 == Triple::sparcv9, "");
1350 static_assert((Triple::ArchType)ZigLLVM_sparcel == Triple::sparcel, "");
1351 static_assert((Triple::ArchType)ZigLLVM_systemz == Triple::systemz, "");
1352 static_assert((Triple::ArchType)ZigLLVM_tce == Triple::tce, "");
1353 static_assert((Triple::ArchType)ZigLLVM_tcele == Triple::tcele, "");
1354 static_assert((Triple::ArchType)ZigLLVM_thumb == Triple::thumb, "");
1355 static_assert((Triple::ArchType)ZigLLVM_thumbeb == Triple::thumbeb, "");
1356 static_assert((Triple::ArchType)ZigLLVM_x86 == Triple::x86, "");
1357 static_assert((Triple::ArchType)ZigLLVM_x86_64 == Triple::x86_64, "");
1358 static_assert((Triple::ArchType)ZigLLVM_xcore == Triple::xcore, "");
1359 static_assert((Triple::ArchType)ZigLLVM_nvptx == Triple::nvptx, "");
1360 static_assert((Triple::ArchType)ZigLLVM_nvptx64 == Triple::nvptx64, "");
1361 static_assert((Triple::ArchType)ZigLLVM_le32 == Triple::le32, "");
1362 static_assert((Triple::ArchType)ZigLLVM_le64 == Triple::le64, "");
1363 static_assert((Triple::ArchType)ZigLLVM_amdil == Triple::amdil, "");
1364 static_assert((Triple::ArchType)ZigLLVM_amdil64 == Triple::amdil64, "");
1365 static_assert((Triple::ArchType)ZigLLVM_hsail == Triple::hsail, "");
1366 static_assert((Triple::ArchType)ZigLLVM_hsail64 == Triple::hsail64, "");
1367 static_assert((Triple::ArchType)ZigLLVM_spir == Triple::spir, "");
1368 static_assert((Triple::ArchType)ZigLLVM_spir64 == Triple::spir64, "");
1369 static_assert((Triple::ArchType)ZigLLVM_kalimba == Triple::kalimba, "");
1370 static_assert((Triple::ArchType)ZigLLVM_shave == Triple::shave, "");
1371 static_assert((Triple::ArchType)ZigLLVM_lanai == Triple::lanai, "");
1372 static_assert((Triple::ArchType)ZigLLVM_wasm32 == Triple::wasm32, "");
1373 static_assert((Triple::ArchType)ZigLLVM_wasm64 == Triple::wasm64, "");
1374 static_assert((Triple::ArchType)ZigLLVM_renderscript32 == Triple::renderscript32, "");
1375 static_assert((Triple::ArchType)ZigLLVM_renderscript64 == Triple::renderscript64, "");
1376 static_assert((Triple::ArchType)ZigLLVM_ve == Triple::ve, "");
1377 static_assert((Triple::ArchType)ZigLLVM_LastArchType == Triple::LastArchType, "");
1378 
1379 static_assert((Triple::VendorType)ZigLLVM_UnknownVendor == Triple::UnknownVendor, "");
1380 static_assert((Triple::VendorType)ZigLLVM_Apple == Triple::Apple, "");
1381 static_assert((Triple::VendorType)ZigLLVM_PC == Triple::PC, "");
1382 static_assert((Triple::VendorType)ZigLLVM_SCEI == Triple::SCEI, "");
1383 static_assert((Triple::VendorType)ZigLLVM_Freescale == Triple::Freescale, "");
1384 static_assert((Triple::VendorType)ZigLLVM_IBM == Triple::IBM, "");
1385 static_assert((Triple::VendorType)ZigLLVM_ImaginationTechnologies == Triple::ImaginationTechnologies, "");
1386 static_assert((Triple::VendorType)ZigLLVM_MipsTechnologies == Triple::MipsTechnologies, "");
1387 static_assert((Triple::VendorType)ZigLLVM_NVIDIA == Triple::NVIDIA, "");
1388 static_assert((Triple::VendorType)ZigLLVM_CSR == Triple::CSR, "");
1389 static_assert((Triple::VendorType)ZigLLVM_Myriad == Triple::Myriad, "");
1390 static_assert((Triple::VendorType)ZigLLVM_AMD == Triple::AMD, "");
1391 static_assert((Triple::VendorType)ZigLLVM_Mesa == Triple::Mesa, "");
1392 static_assert((Triple::VendorType)ZigLLVM_SUSE == Triple::SUSE, "");
1393 static_assert((Triple::VendorType)ZigLLVM_OpenEmbedded == Triple::OpenEmbedded, "");
1394 static_assert((Triple::VendorType)ZigLLVM_LastVendorType == Triple::LastVendorType, "");
1395 
1396 static_assert((Triple::OSType)ZigLLVM_UnknownOS == Triple::UnknownOS, "");
1397 static_assert((Triple::OSType)ZigLLVM_Ananas == Triple::Ananas, "");
1398 static_assert((Triple::OSType)ZigLLVM_CloudABI == Triple::CloudABI, "");
1399 static_assert((Triple::OSType)ZigLLVM_Darwin == Triple::Darwin, "");
1400 static_assert((Triple::OSType)ZigLLVM_DragonFly == Triple::DragonFly, "");
1401 static_assert((Triple::OSType)ZigLLVM_FreeBSD == Triple::FreeBSD, "");
1402 static_assert((Triple::OSType)ZigLLVM_Fuchsia == Triple::Fuchsia, "");
1403 static_assert((Triple::OSType)ZigLLVM_IOS == Triple::IOS, "");
1404 // Commented out to work around a Debian/Ubuntu bug.
1405 // See https://github.com/ziglang/zig/issues/2076
1406 //static_assert((Triple::OSType)ZigLLVM_KFreeBSD == Triple::KFreeBSD, "");
1407 static_assert((Triple::OSType)ZigLLVM_Linux == Triple::Linux, "");
1408 static_assert((Triple::OSType)ZigLLVM_Lv2 == Triple::Lv2, "");
1409 static_assert((Triple::OSType)ZigLLVM_MacOSX == Triple::MacOSX, "");
1410 static_assert((Triple::OSType)ZigLLVM_NetBSD == Triple::NetBSD, "");
1411 static_assert((Triple::OSType)ZigLLVM_OpenBSD == Triple::OpenBSD, "");
1412 static_assert((Triple::OSType)ZigLLVM_Solaris == Triple::Solaris, "");
1413 static_assert((Triple::OSType)ZigLLVM_Win32 == Triple::Win32, "");
1414 static_assert((Triple::OSType)ZigLLVM_ZOS == Triple::ZOS, "");
1415 static_assert((Triple::OSType)ZigLLVM_Haiku == Triple::Haiku, "");
1416 static_assert((Triple::OSType)ZigLLVM_Minix == Triple::Minix, "");
1417 static_assert((Triple::OSType)ZigLLVM_RTEMS == Triple::RTEMS, "");
1418 static_assert((Triple::OSType)ZigLLVM_NaCl == Triple::NaCl, "");
1419 static_assert((Triple::OSType)ZigLLVM_AIX == Triple::AIX, "");
1420 static_assert((Triple::OSType)ZigLLVM_CUDA == Triple::CUDA, "");
1421 static_assert((Triple::OSType)ZigLLVM_NVCL == Triple::NVCL, "");
1422 static_assert((Triple::OSType)ZigLLVM_AMDHSA == Triple::AMDHSA, "");
1423 static_assert((Triple::OSType)ZigLLVM_PS4 == Triple::PS4, "");
1424 static_assert((Triple::OSType)ZigLLVM_ELFIAMCU == Triple::ELFIAMCU, "");
1425 static_assert((Triple::OSType)ZigLLVM_TvOS == Triple::TvOS, "");
1426 static_assert((Triple::OSType)ZigLLVM_WatchOS == Triple::WatchOS, "");
1427 static_assert((Triple::OSType)ZigLLVM_Mesa3D == Triple::Mesa3D, "");
1428 static_assert((Triple::OSType)ZigLLVM_Contiki == Triple::Contiki, "");
1429 static_assert((Triple::OSType)ZigLLVM_AMDPAL == Triple::AMDPAL, "");
1430 static_assert((Triple::OSType)ZigLLVM_HermitCore == Triple::HermitCore, "");
1431 static_assert((Triple::OSType)ZigLLVM_Hurd == Triple::Hurd, "");
1432 static_assert((Triple::OSType)ZigLLVM_WASI == Triple::WASI, "");
1433 static_assert((Triple::OSType)ZigLLVM_Emscripten == Triple::Emscripten, "");
1434 static_assert((Triple::OSType)ZigLLVM_LastOSType == Triple::LastOSType, "");
1435 
1436 static_assert((Triple::EnvironmentType)ZigLLVM_UnknownEnvironment == Triple::UnknownEnvironment, "");
1437 static_assert((Triple::EnvironmentType)ZigLLVM_GNU == Triple::GNU, "");
1438 static_assert((Triple::EnvironmentType)ZigLLVM_GNUABIN32 == Triple::GNUABIN32, "");
1439 static_assert((Triple::EnvironmentType)ZigLLVM_GNUABI64 == Triple::GNUABI64, "");
1440 static_assert((Triple::EnvironmentType)ZigLLVM_GNUEABI == Triple::GNUEABI, "");
1441 static_assert((Triple::EnvironmentType)ZigLLVM_GNUEABIHF == Triple::GNUEABIHF, "");
1442 static_assert((Triple::EnvironmentType)ZigLLVM_GNUX32 == Triple::GNUX32, "");
1443 static_assert((Triple::EnvironmentType)ZigLLVM_GNUILP32 == Triple::GNUILP32, "");
1444 static_assert((Triple::EnvironmentType)ZigLLVM_CODE16 == Triple::CODE16, "");
1445 static_assert((Triple::EnvironmentType)ZigLLVM_EABI == Triple::EABI, "");
1446 static_assert((Triple::EnvironmentType)ZigLLVM_EABIHF == Triple::EABIHF, "");
1447 static_assert((Triple::EnvironmentType)ZigLLVM_Android == Triple::Android, "");
1448 static_assert((Triple::EnvironmentType)ZigLLVM_Musl == Triple::Musl, "");
1449 static_assert((Triple::EnvironmentType)ZigLLVM_MuslEABI == Triple::MuslEABI, "");
1450 static_assert((Triple::EnvironmentType)ZigLLVM_MuslEABIHF == Triple::MuslEABIHF, "");
1451 static_assert((Triple::EnvironmentType)ZigLLVM_MuslX32 == Triple::MuslX32, "");
1452 static_assert((Triple::EnvironmentType)ZigLLVM_MSVC == Triple::MSVC, "");
1453 static_assert((Triple::EnvironmentType)ZigLLVM_Itanium == Triple::Itanium, "");
1454 static_assert((Triple::EnvironmentType)ZigLLVM_Cygnus == Triple::Cygnus, "");
1455 static_assert((Triple::EnvironmentType)ZigLLVM_CoreCLR == Triple::CoreCLR, "");
1456 static_assert((Triple::EnvironmentType)ZigLLVM_Simulator == Triple::Simulator, "");
1457 static_assert((Triple::EnvironmentType)ZigLLVM_MacABI == Triple::MacABI, "");
1458 static_assert((Triple::EnvironmentType)ZigLLVM_LastEnvironmentType == Triple::LastEnvironmentType, "");
1459 
1460 static_assert((Triple::ObjectFormatType)ZigLLVM_UnknownObjectFormat == Triple::UnknownObjectFormat, "");
1461 static_assert((Triple::ObjectFormatType)ZigLLVM_COFF == Triple::COFF, "");
1462 static_assert((Triple::ObjectFormatType)ZigLLVM_ELF == Triple::ELF, "");
1463 static_assert((Triple::ObjectFormatType)ZigLLVM_GOFF == Triple::GOFF, "");
1464 static_assert((Triple::ObjectFormatType)ZigLLVM_MachO == Triple::MachO, "");
1465 static_assert((Triple::ObjectFormatType)ZigLLVM_Wasm == Triple::Wasm, "");
1466 static_assert((Triple::ObjectFormatType)ZigLLVM_XCOFF == Triple::XCOFF, "");
1467 
1468 static_assert((CallingConv::ID)ZigLLVM_C == llvm::CallingConv::C, "");
1469 static_assert((CallingConv::ID)ZigLLVM_Fast == llvm::CallingConv::Fast, "");
1470 static_assert((CallingConv::ID)ZigLLVM_Cold == llvm::CallingConv::Cold, "");
1471 static_assert((CallingConv::ID)ZigLLVM_GHC == llvm::CallingConv::GHC, "");
1472 static_assert((CallingConv::ID)ZigLLVM_HiPE == llvm::CallingConv::HiPE, "");
1473 static_assert((CallingConv::ID)ZigLLVM_WebKit_JS == llvm::CallingConv::WebKit_JS, "");
1474 static_assert((CallingConv::ID)ZigLLVM_AnyReg == llvm::CallingConv::AnyReg, "");
1475 static_assert((CallingConv::ID)ZigLLVM_PreserveMost == llvm::CallingConv::PreserveMost, "");
1476 static_assert((CallingConv::ID)ZigLLVM_PreserveAll == llvm::CallingConv::PreserveAll, "");
1477 static_assert((CallingConv::ID)ZigLLVM_Swift == llvm::CallingConv::Swift, "");
1478 static_assert((CallingConv::ID)ZigLLVM_CXX_FAST_TLS == llvm::CallingConv::CXX_FAST_TLS, "");
1479 static_assert((CallingConv::ID)ZigLLVM_FirstTargetCC == llvm::CallingConv::FirstTargetCC, "");
1480 static_assert((CallingConv::ID)ZigLLVM_X86_StdCall == llvm::CallingConv::X86_StdCall, "");
1481 static_assert((CallingConv::ID)ZigLLVM_X86_FastCall == llvm::CallingConv::X86_FastCall, "");
1482 static_assert((CallingConv::ID)ZigLLVM_ARM_APCS == llvm::CallingConv::ARM_APCS, "");
1483 static_assert((CallingConv::ID)ZigLLVM_ARM_AAPCS == llvm::CallingConv::ARM_AAPCS, "");
1484 static_assert((CallingConv::ID)ZigLLVM_ARM_AAPCS_VFP == llvm::CallingConv::ARM_AAPCS_VFP, "");
1485 static_assert((CallingConv::ID)ZigLLVM_MSP430_INTR == llvm::CallingConv::MSP430_INTR, "");
1486 static_assert((CallingConv::ID)ZigLLVM_X86_ThisCall == llvm::CallingConv::X86_ThisCall, "");
1487 static_assert((CallingConv::ID)ZigLLVM_PTX_Kernel == llvm::CallingConv::PTX_Kernel, "");
1488 static_assert((CallingConv::ID)ZigLLVM_PTX_Device == llvm::CallingConv::PTX_Device, "");
1489 static_assert((CallingConv::ID)ZigLLVM_SPIR_FUNC == llvm::CallingConv::SPIR_FUNC, "");
1490 static_assert((CallingConv::ID)ZigLLVM_SPIR_KERNEL == llvm::CallingConv::SPIR_KERNEL, "");
1491 static_assert((CallingConv::ID)ZigLLVM_Intel_OCL_BI == llvm::CallingConv::Intel_OCL_BI, "");
1492 static_assert((CallingConv::ID)ZigLLVM_X86_64_SysV == llvm::CallingConv::X86_64_SysV, "");
1493 static_assert((CallingConv::ID)ZigLLVM_Win64 == llvm::CallingConv::Win64, "");
1494 static_assert((CallingConv::ID)ZigLLVM_X86_VectorCall == llvm::CallingConv::X86_VectorCall, "");
1495 static_assert((CallingConv::ID)ZigLLVM_HHVM == llvm::CallingConv::HHVM, "");
1496 static_assert((CallingConv::ID)ZigLLVM_HHVM_C == llvm::CallingConv::HHVM_C, "");
1497 static_assert((CallingConv::ID)ZigLLVM_X86_INTR == llvm::CallingConv::X86_INTR, "");
1498 static_assert((CallingConv::ID)ZigLLVM_AVR_INTR == llvm::CallingConv::AVR_INTR, "");
1499 static_assert((CallingConv::ID)ZigLLVM_AVR_SIGNAL == llvm::CallingConv::AVR_SIGNAL, "");
1500 static_assert((CallingConv::ID)ZigLLVM_AVR_BUILTIN == llvm::CallingConv::AVR_BUILTIN, "");
1501 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_VS == llvm::CallingConv::AMDGPU_VS, "");
1502 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_GS == llvm::CallingConv::AMDGPU_GS, "");
1503 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_PS == llvm::CallingConv::AMDGPU_PS, "");
1504 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_CS == llvm::CallingConv::AMDGPU_CS, "");
1505 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_KERNEL == llvm::CallingConv::AMDGPU_KERNEL, "");
1506 static_assert((CallingConv::ID)ZigLLVM_X86_RegCall == llvm::CallingConv::X86_RegCall, "");
1507 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_HS == llvm::CallingConv::AMDGPU_HS, "");
1508 static_assert((CallingConv::ID)ZigLLVM_MSP430_BUILTIN == llvm::CallingConv::MSP430_BUILTIN, "");
1509 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_LS == llvm::CallingConv::AMDGPU_LS, "");
1510 static_assert((CallingConv::ID)ZigLLVM_AMDGPU_ES == llvm::CallingConv::AMDGPU_ES, "");
1511 static_assert((CallingConv::ID)ZigLLVM_AArch64_VectorCall == llvm::CallingConv::AArch64_VectorCall, "");
1512 static_assert((CallingConv::ID)ZigLLVM_MaxID == llvm::CallingConv::MaxID, "");
1513