Merge pull request #85 from birth-software/move-llvm-cpp-out-of-bootstrap

Move LLVM inteface code out of bootstrap
This commit is contained in:
David 2024-02-18 09:15:43 -06:00 committed by GitHub
commit 1a2e1c8161
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 64 additions and 60 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
zig-cache/
zig-out/
nat/
llvm*/

View File

@ -102,10 +102,11 @@ pub fn build(b: *std.Build) !void {
const llvm_lib_dir = try std.mem.concat(b.allocator, u8, &.{ llvm_path, "/lib" });
compiler.addIncludePath(std.Build.LazyPath.relative(llvm_include_dir));
const cpp_files = .{
"bootstrap/backend/llvm.cpp",
"bootstrap/frontend/clang/main.cpp",
"bootstrap/frontend/clang/cc1.cpp",
"bootstrap/frontend/clang/cc1as.cpp",
"src/llvm/llvm.cpp",
"src/llvm/lld.cpp",
"src/llvm/clang_main.cpp",
"src/llvm/clang_cc1.cpp",
"src/llvm/clang_cc1as.cpp",
};
compiler.addCSourceFiles(.{
.files = &cpp_files,

58
src/llvm/lld.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "lld/Common/CommonLinkerContext.h"
using namespace llvm;
enum class Format {
elf = 0,
macho = 1,
coff = 2,
};
namespace lld {
namespace coff {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
namespace elf {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
namespace wasm {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
namespace macho {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
}
extern "C" void stream_to_string(raw_string_ostream& stream, const char** message_ptr, size_t* message_len);
extern "C" bool NativityLLDLink(Format format, const char** argument_ptr, size_t argument_count, const char** stdout_ptr, size_t* stdout_len, const char** stderr_ptr, size_t* stderr_len)
{
auto arguments = ArrayRef<const char*>(argument_ptr, argument_count);
std::string stdout_string;
raw_string_ostream stdout_stream(stdout_string);
std::string stderr_string;
raw_string_ostream stderr_stream(stderr_string);
bool success = false;
switch (format) {
case Format::elf:
success = lld::elf::link(arguments, stdout_stream, stderr_stream, true, false);
break;
case Format::coff:
success = lld::coff::link(arguments, stdout_stream, stderr_stream, true, false);
case Format::macho:
success = lld::macho::link(arguments, stdout_stream, stderr_stream, true, false);
default:
break;
}
stream_to_string(stdout_stream, stdout_ptr, stdout_len);
stream_to_string(stderr_stream, stderr_ptr, stderr_len);
return success;
}

View File

@ -14,7 +14,6 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/FileSystem.h"
#include "lld/Common/CommonLinkerContext.h"
using namespace llvm;
@ -667,7 +666,7 @@ extern "C" Constant* NativityLLVMBuilderCreateGlobalStringPointer(IRBuilder<>& b
return constant;
}
void stream_to_string(raw_string_ostream& stream, const char** message_ptr, size_t* message_len)
extern "C" void stream_to_string(raw_string_ostream& stream, const char** message_ptr, size_t* message_len)
{
stream.flush();
@ -850,25 +849,6 @@ extern "C" void NativityLLVMCallSetCallingConvention(CallBase& call_instruction,
call_instruction.setCallingConv(calling_convention);
}
namespace lld {
namespace coff {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
namespace elf {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
namespace wasm {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
namespace macho {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
}
}
extern "C" void NativityLLVMInitializeCodeGeneration()
{
InitializeAllTargetInfos();
@ -942,40 +922,6 @@ extern "C" bool NativityLLVMModuleAddPassesToEmitFile(Module& module, TargetMach
return true;
}
enum class Format {
elf = 0,
macho = 1,
coff = 2,
};
extern "C" bool NativityLLDLink(Format format, const char** argument_ptr, size_t argument_count, const char** stdout_ptr, size_t* stdout_len, const char** stderr_ptr, size_t* stderr_len)
{
auto arguments = ArrayRef<const char*>(argument_ptr, argument_count);
std::string stdout_string;
raw_string_ostream stdout_stream(stdout_string);
std::string stderr_string;
raw_string_ostream stderr_stream(stderr_string);
bool success = false;
switch (format) {
case Format::elf:
success = lld::elf::link(arguments, stdout_stream, stderr_stream, true, false);
break;
case Format::coff:
success = lld::coff::link(arguments, stdout_stream, stderr_stream, true, false);
case Format::macho:
success = lld::macho::link(arguments, stdout_stream, stderr_stream, true, false);
default:
break;
}
stream_to_string(stdout_stream, stdout_ptr, stdout_len);
stream_to_string(stderr_stream, stderr_ptr, stderr_len);
return success;
}
extern "C" bool NativityLLVMCompareTypes(Type* a, Type* b)
{
if (auto* int_a = dyn_cast<IntegerType>(a)) {