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:
Farzana Ahmed Siddique
2023-07-07 22:16:41 +00:00
committed by GitHub
co-authored by Farzana Ahmed Siddique Richard Smith
parent 123662f5b1
commit 037196f69f
8 changed files with 142 additions and 29 deletions
+40 -3
View File
@@ -118,6 +118,7 @@ enum class DumpMode {
SemanticsIR,
LLVMIR,
Assembly,
ObjectCode,
Unknown
};
@@ -134,10 +135,11 @@ auto Driver::RunDumpSubcommand(DiagnosticConsumer& consumer,
.Case("semantics-ir", DumpMode::SemanticsIR)
.Case("llvm-ir", DumpMode::LLVMIR)
.Case("assembly", DumpMode::Assembly)
.Case("objcode", DumpMode::ObjectCode)
.Default(DumpMode::Unknown);
if (dump_mode == DumpMode::Unknown) {
error_stream_ << "ERROR: Dump mode should be one of tokens, parse-tree, "
"semantics-ir, llvm-ir, or assembly.\n";
"semantics-ir, llvm-ir, assembly, or objcode.\n";
return false;
}
args = args.drop_front();
@@ -163,6 +165,26 @@ auto Driver::RunDumpSubcommand(DiagnosticConsumer& consumer,
args = args.drop_front();
}
llvm::StringRef output_file;
if (dump_mode == DumpMode::ObjectCode) {
while (!args.empty() && (args.front().starts_with("--target_triple=") ||
args.front().starts_with("--output_file="))) {
if (args.front().starts_with("--target_triple=")) {
target_triple = args.front().split("=").second;
args = args.drop_front();
}
if (args.front().starts_with("--output_file=")) {
output_file = args.front().split("=").second;
args = args.drop_front();
}
}
if (output_file.empty()) {
error_stream_ << "ERROR: Must provide an output file.\n";
return false;
}
}
if (args.empty()) {
error_stream_ << "ERROR: No input file specified.\n";
return false;
@@ -248,8 +270,23 @@ auto Driver::RunDumpSubcommand(DiagnosticConsumer& consumer,
if (dump_mode == DumpMode::Assembly) {
consumer.Flush();
has_errors |= !Carbon::PrintAssemblyFromModule(
*module, target_triple, error_stream_, output_stream_);
CodeGen codegen(*module, target_triple, error_stream_, output_stream_);
has_errors |= !codegen.PrintAssembly();
return !has_errors;
}
if (dump_mode == DumpMode::ObjectCode) {
std::error_code ec;
llvm::raw_fd_ostream dest(output_file, ec, llvm::sys::fs::OF_None);
if (ec) {
error_stream_ << "Error: Could not open file: " << ec.message() << "\n";
return false;
}
CodeGen codegen(*module, target_triple, error_stream_, dest);
has_errors |= !codegen.GenerateObjectCode();
if (!has_errors) {
output_stream_ << "Success: Object file is generated!\n";
}
return !has_errors;
}