all LLVM C++ API contained to one file
This commit is contained in:
139
src/zig_llvm.cpp
139
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user