mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-27 20:30:09 +01:00
Creates object file from the module. (#2955)
This pr creates the object file for the carbon code that returns either 0 or 1. The command to convert the object code to binary is `clang <object_file_name> -o <binary_name>` --------- Co-authored-by: Farzana Ahmed Siddique <fasiddique@google.com> Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Farzana Ahmed Siddique
Richard Smith
parent
123662f5b1
commit
037196f69f
@@ -5,21 +5,17 @@
|
||||
#include "toolchain/codegen/codegen.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
|
||||
#include "llvm/IR/LegacyPassManager.h"
|
||||
#include "llvm/MC/TargetRegistry.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
#include "llvm/Support/TargetSelect.h"
|
||||
#include "llvm/Target/TargetMachine.h"
|
||||
#include "llvm/Target/TargetOptions.h"
|
||||
#include "llvm/TargetParser/Host.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto PrintAssemblyFromModule(llvm::Module& module,
|
||||
llvm::StringRef target_triple,
|
||||
llvm::raw_pwrite_stream& error_stream,
|
||||
llvm::raw_pwrite_stream& output_stream) -> bool {
|
||||
auto CodeGen::CreateTargetMachine() -> std::unique_ptr<llvm::TargetMachine> {
|
||||
// Initialize the target registry etc.
|
||||
llvm::InitializeAllTargetInfos();
|
||||
llvm::InitializeAllTargets();
|
||||
@@ -37,8 +33,8 @@ auto PrintAssemblyFromModule(llvm::Module& module,
|
||||
const auto* target = llvm::TargetRegistry::lookupTarget(triple, error);
|
||||
|
||||
if (!target) {
|
||||
error_stream << "ERROR: " << error << "\n";
|
||||
return false;
|
||||
error_stream_ << "ERROR: Invalid -target_triple: " << error << "\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
constexpr llvm::StringLiteral CPU = "generic";
|
||||
@@ -46,26 +42,48 @@ auto PrintAssemblyFromModule(llvm::Module& module,
|
||||
|
||||
llvm::TargetOptions target_opts;
|
||||
std::optional<llvm::Reloc::Model> reloc_model;
|
||||
auto* target_machine = target->createTargetMachine(
|
||||
target_triple, CPU, Features, target_opts, reloc_model);
|
||||
module.setDataLayout(target_machine->createDataLayout());
|
||||
module.setTargetTriple(target_triple);
|
||||
std::unique_ptr<llvm::TargetMachine> target_machine(
|
||||
target->createTargetMachine(target_triple, CPU, Features, target_opts,
|
||||
reloc_model));
|
||||
return target_machine;
|
||||
}
|
||||
|
||||
auto CodeGen::EmitCode(llvm::raw_pwrite_stream& dest,
|
||||
llvm::TargetMachine* target_machine,
|
||||
llvm::CodeGenFileType file_type) -> bool {
|
||||
Module.setDataLayout(target_machine->createDataLayout());
|
||||
Module.setTargetTriple(target_triple);
|
||||
|
||||
// Using the legacy PM to generate the assembly since the new PM
|
||||
// does not work with this yet.
|
||||
// TODO: make the new PM work with the codegen pipeline.
|
||||
|
||||
llvm::legacy::PassManager pass;
|
||||
auto file_type = llvm::CGFT_AssemblyFile;
|
||||
|
||||
if (target_machine->addPassesToEmitFile(pass, output_stream, nullptr,
|
||||
file_type)) {
|
||||
error_stream << "Nothing to write to object file\n";
|
||||
if (target_machine->addPassesToEmitFile(pass, dest, nullptr, file_type)) {
|
||||
error_stream_ << "Error: Nothing to write to object file\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
pass.run(module);
|
||||
delete target_machine;
|
||||
pass.run(Module);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto CodeGen::PrintAssembly() -> bool {
|
||||
auto target_machine = CreateTargetMachine();
|
||||
if (target_machine == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return EmitCode(output_stream_, target_machine.get(),
|
||||
llvm::CodeGenFileType::CGFT_AssemblyFile);
|
||||
}
|
||||
|
||||
auto CodeGen::GenerateObjectCode() -> bool {
|
||||
auto target_machine = CreateTargetMachine();
|
||||
if (target_machine == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return EmitCode(output_stream_, target_machine.get(),
|
||||
llvm::CodeGenFileType::CGFT_ObjectFile);
|
||||
}
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user