aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-09 18:31:49 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-09 18:31:49 +0800
commit379ff50dcc7ccf977379aee68fa9d10f69065d23 (patch)
treee0fac3952d46d0bbcaea32bcfc6087c95fff2992 /backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp
parent495c675771f43cfa12ab7a426a538be72fec1805 (diff)
downloadrcc-project-379ff50dcc7ccf977379aee68fa9d10f69065d23.tar.gz
rcc-project-379ff50dcc7ccf977379aee68fa9d10f69065d23.tar.xz
[MC] Implement RV64GC assembly emission
Diffstat (limited to 'backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp')
-rw-r--r--backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp b/backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp
new file mode 100644
index 0000000..7661c61
--- /dev/null
+++ b/backend/lib/Target/RISCV/RISCVAssemblyPrinter.cpp
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "rcc/Target/RISCV/RISCVAssemblyPrinter.h"
+
+#include "rcc/CodeGen/AsmPrinter.h"
+#include "rcc/CodeGen/CompilationUnit.h"
+#include "rcc/CodeGen/MachineFunction.h"
+#include "rcc/CodeGen/MachineMCContext.h"
+#include "rcc/MC/MCContext.h"
+#include "rcc/MC/MCStreamer.h"
+#include "rcc/Target/RISCV/RISCVMCTargetLowering.h"
+
+#include <string>
+
+namespace rcc::riscv {
+namespace {
+
+[[nodiscard]] AsmPrinterOptions printerOptions(AssemblyEmissionOptions Options) noexcept {
+ return AsmPrinterOptions{
+ .EmitFunctionDirectives = Options.EmitFunctionDirectives,
+ .EmitCFI = Options.EmitCFI,
+ .EmitBlockLabels = Options.EmitBlockLabels,
+ .EmitInstructionSymbols = true,
+ .VerifyInput = Options.VerifyInput,
+ };
+}
+
+} // namespace
+
+Expected<std::string> emitAssembly(const CompilationUnit &Unit, const TargetDescription &Target,
+ AssemblyEmissionOptions Options) {
+ mc::MCContext MCContext;
+ MachineMCContext MachineContext(MCContext);
+ RISCVMCTargetLowering Lowering(Target);
+ mc::AsmTextStreamer Streamer(Lowering.instructionPrinter());
+ AsmPrinter Printer(Target, Lowering, MachineContext, Streamer, printerOptions(Options));
+ auto Emitted = Printer.emitCompilationUnit(Unit);
+ if (!Emitted)
+ return std::unexpected(Emitted.error());
+ return Streamer.takeOutput();
+}
+
+Expected<std::string> emitAssembly(const MachineFunction &Function, const TargetDescription &Target,
+ AssemblyEmissionOptions Options) {
+ mc::MCContext MCContext;
+ MachineMCContext MachineContext(MCContext);
+ RISCVMCTargetLowering Lowering(Target);
+ mc::AsmTextStreamer Streamer(Lowering.instructionPrinter());
+ AsmPrinter Printer(Target, Lowering, MachineContext, Streamer, printerOptions(Options));
+
+ auto Header = Lowering.emitFileHeader(MachineContext, Streamer);
+ if (!Header)
+ return std::unexpected(Header.error());
+ auto Emitted = Printer.emitFunction(Function);
+ if (!Emitted)
+ return std::unexpected(Emitted.error());
+ auto Trailer = Lowering.emitFileTrailer(MachineContext, Streamer);
+ if (!Trailer)
+ return std::unexpected(Trailer.error());
+ auto Finished = Streamer.finish();
+ if (!Finished)
+ return std::unexpected(Finished.error());
+ return Streamer.takeOutput();
+}
+
+} // namespace rcc::riscv