diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2015-11-30 14:43:09 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2015-11-30 14:43:23 -0700 |
| commit | b77c423f773cdf05c95b293f44852ec5a39fc4ed (patch) | |
| tree | e37dcaa0a6f8f48b67736800dc00b73f773cb86e /src/zig_llvm.cpp | |
| parent | f36255b670f2bcec368a9ce4622af194b5b71d70 (diff) | |
| download | zig-b77c423f773cdf05c95b293f44852ec5a39fc4ed.tar.gz zig-b77c423f773cdf05c95b293f44852ec5a39fc4ed.zip | |
all LLVM C++ API contained to one file
Diffstat (limited to 'src/zig_llvm.cpp')
| -rw-r--r-- | src/zig_llvm.cpp | 139 |
1 files changed, 136 insertions, 3 deletions
diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 42b6f5c0f8..81104914ab 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -19,6 +19,7 @@ #include <llvm/MC/SubtargetFeature.h> #include <llvm/Support/raw_ostream.h> #include <llvm/Support/FileSystem.h> +#include <llvm/Support/TargetParser.h> #include <llvm/Target/TargetMachine.h> #include <llvm/IR/LegacyPassManager.h> #include <llvm/IR/Module.h> @@ -148,6 +149,22 @@ LLVMZigDIType *LLVMZigCreateDebugBasicType(LLVMZigDIBuilder *dibuilder, const ch return reinterpret_cast<LLVMZigDIType*>(di_type); } +LLVMZigDISubroutineType *LLVMZigCreateSubroutineType(LLVMZigDIBuilder *dibuilder_wrapped, + LLVMZigDIFile *file, LLVMZigDIType **types_array, int types_array_len, unsigned flags) +{ + SmallVector<Metadata *, 8> types; + for (int i = 0; i < types_array_len; i += 1) { + DIType *ditype = reinterpret_cast<DIType*>(types_array[i]); + types.push_back(ditype); + } + DIBuilder *dibuilder = reinterpret_cast<DIBuilder*>(dibuilder_wrapped); + DISubroutineType *subroutine_type = dibuilder->createSubroutineType( + reinterpret_cast<DIFile*>(file), + dibuilder->getOrCreateTypeArray(types), + flags); + return reinterpret_cast<LLVMZigDISubroutineType*>(subroutine_type); +} + unsigned LLVMZigEncoding_DW_ATE_unsigned(void) { return dwarf::DW_ATE_unsigned; } @@ -161,12 +178,12 @@ unsigned LLVMZigLang_DW_LANG_C99(void) { } LLVMZigDIBuilder *LLVMZigCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) { - DIBuilder *di_builder = new DIBuilder(*llvm::unwrap(module), allow_unresolved); + DIBuilder *di_builder = new DIBuilder(*unwrap(module), allow_unresolved); return reinterpret_cast<LLVMZigDIBuilder *>(di_builder); } void LLVMZigSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, LLVMZigDIScope *scope) { - unwrap(builder)->SetCurrentDebugLocation(llvm::DebugLoc::get( + unwrap(builder)->SetCurrentDebugLocation(DebugLoc::get( line, column, reinterpret_cast<DIScope*>(scope))); } @@ -223,7 +240,7 @@ LLVMZigDISubprogram *LLVMZigCreateFunction(LLVMZigDIBuilder *dibuilder, LLVMZigD LLVMZigDISubroutineType *ty, bool is_local_to_unit, bool is_definition, unsigned scope_line, unsigned flags, bool is_optimized, LLVMValueRef function) { - llvm::Function *unwrapped_function = reinterpret_cast<llvm::Function*>(unwrap(function)); + Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(function)); DISubprogram *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFunction( reinterpret_cast<DIScope*>(scope), name, linkage_name, @@ -237,3 +254,119 @@ LLVMZigDISubprogram *LLVMZigCreateFunction(LLVMZigDIBuilder *dibuilder, LLVMZigD void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder) { reinterpret_cast<DIBuilder*>(dibuilder)->finalize(); } + +enum FloatAbi { + FloatAbiHard, + FloatAbiSoft, + FloatAbiSoftFp, +}; + +static int get_arm_sub_arch_version(const Triple &triple) { + return ARMTargetParser::parseArchVersion(triple.getArchName()); +} + +static FloatAbi get_float_abi(const Triple &triple) { + switch (triple.getOS()) { + case Triple::Darwin: + case Triple::MacOSX: + case Triple::IOS: + if (get_arm_sub_arch_version(triple) == 6 || + get_arm_sub_arch_version(triple) == 7) + { + return FloatAbiSoftFp; + } else { + return FloatAbiSoft; + } + case Triple::Win32: + return FloatAbiHard; + case Triple::FreeBSD: + switch (triple.getEnvironment()) { + case Triple::GNUEABIHF: + return FloatAbiHard; + default: + return FloatAbiSoft; + } + default: + switch (triple.getEnvironment()) { + case Triple::GNUEABIHF: + return FloatAbiHard; + case Triple::GNUEABI: + return FloatAbiSoftFp; + case Triple::EABIHF: + return FloatAbiHard; + case Triple::EABI: + return FloatAbiSoftFp; + case Triple::Android: + if (get_arm_sub_arch_version(triple) == 7) { + return FloatAbiSoftFp; + } else { + return FloatAbiSoft; + } + default: + return FloatAbiSoft; + } + } +} + +Buf *get_dynamic_linker(LLVMTargetMachineRef target_machine_ref) { + TargetMachine *target_machine = reinterpret_cast<TargetMachine*>(target_machine_ref); + const Triple &triple = target_machine->getTargetTriple(); + + const Triple::ArchType arch = triple.getArch(); + + if (triple.getEnvironment() == Triple::Android) { + if (triple.isArch64Bit()) { + return buf_create_from_str("/system/bin/linker64"); + } else { + return buf_create_from_str("/system/bin/linker"); + } + } else if (arch == Triple::x86 || + arch == Triple::sparc || + arch == Triple::sparcel) + { + return buf_create_from_str("/lib/ld-linux.so.2"); + } else if (arch == Triple::aarch64) { + return buf_create_from_str("/lib/ld-linux-aarch64.so.1"); + } else if (arch == Triple::aarch64_be) { + return buf_create_from_str("/lib/ld-linux-aarch64_be.so.1"); + } else if (arch == Triple::arm || arch == Triple::thumb) { + if (triple.getEnvironment() == Triple::GNUEABIHF || + get_float_abi(triple) == FloatAbiHard) + { + return buf_create_from_str("/lib/ld-linux-armhf.so.3"); + } else { + return buf_create_from_str("/lib/ld-linux.so.3"); + } + } else if (arch == Triple::armeb || arch == Triple::thumbeb) { + if (triple.getEnvironment() == Triple::GNUEABIHF || + get_float_abi(triple) == FloatAbiHard) + { + return buf_create_from_str("/lib/ld-linux-armhf.so.3"); + } else { + return buf_create_from_str("/lib/ld-linux.so.3"); + } + } else if (arch == Triple::mips || arch == Triple::mipsel || + arch == Triple::mips64 || arch == Triple::mips64el) + { + // when you want to solve this TODO, grep clang codebase for + // getLinuxDynamicLinker + zig_panic("TODO figure out MIPS dynamic linker name"); + } else if (arch == Triple::ppc) { + return buf_create_from_str("/lib/ld.so.1"); + } else if (arch == Triple::ppc64) { + return buf_create_from_str("/lib64/ld64.so.2"); + } else if (arch == Triple::ppc64le) { + return buf_create_from_str("/lib64/ld64.so.2"); + } else if (arch == Triple::systemz) { + return buf_create_from_str("/lib64/ld64.so.1"); + } else if (arch == Triple::sparcv9) { + return buf_create_from_str("/lib64/ld-linux.so.2"); + } else if (arch == Triple::x86_64 && + triple.getEnvironment() == Triple::GNUX32) + { + return buf_create_from_str("/libx32/ld-linux-x32.so.2"); + } else { + return buf_create_from_str("/lib64/ld-linux-x86-64.so.2"); + } +} + |
