Codegen: Given a carbon file prints the assembly to the stdout (#2944)

Co-authored-by: Farzana Ahmed Siddique <fasiddique@google.com>
This commit is contained in:
Farzana Ahmed Siddique
2023-06-23 15:23:39 -07:00
committed by GitHub
co-authored by Farzana Ahmed Siddique
parent b908c6e274
commit aad4ed2083
7 changed files with 148 additions and 1 deletions
+18 -1
View File
@@ -11,6 +11,7 @@
#include "llvm/ADT/StringSwitch.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/Format.h"
#include "toolchain/codegen/codegen.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/sorting_diagnostic_consumer.h"
#include "toolchain/lexer/tokenized_buffer.h"
@@ -116,6 +117,7 @@ enum class DumpMode {
ParseTree,
SemanticsIR,
LLVMIR,
Assembly,
Unknown
};
@@ -131,10 +133,11 @@ auto Driver::RunDumpSubcommand(DiagnosticConsumer& consumer,
.Case("parse-tree", DumpMode::ParseTree)
.Case("semantics-ir", DumpMode::SemanticsIR)
.Case("llvm-ir", DumpMode::LLVMIR)
.Case("assembly", DumpMode::Assembly)
.Default(DumpMode::Unknown);
if (dump_mode == DumpMode::Unknown) {
error_stream_ << "ERROR: Dump mode should be one of tokens, parse-tree, "
"semantics-ir, or llvm-ir.\n";
"semantics-ir, llvm-ir, or assembly.\n";
return false;
}
args = args.drop_front();
@@ -153,6 +156,13 @@ auto Driver::RunDumpSubcommand(DiagnosticConsumer& consumer,
semantics_ir_include_builtins = true;
}
llvm::StringRef target_triple;
if (dump_mode == DumpMode::Assembly && !args.empty() &&
args.front().starts_with("--target_triple=")) {
target_triple = args.front().split("=").second;
args = args.drop_front();
}
if (args.empty()) {
error_stream_ << "ERROR: No input file specified.\n";
return false;
@@ -236,6 +246,13 @@ auto Driver::RunDumpSubcommand(DiagnosticConsumer& consumer,
/*IsForDebug=*/true);
}
if (dump_mode == DumpMode::Assembly) {
consumer.Flush();
has_errors |= !Carbon::PrintAssemblyFromModule(
*module, target_triple, error_stream_, output_stream_);
return !has_errors;
}
llvm_unreachable("should handle all dump modes");
}