midenc_codegen_masm/compiler/
masm.rs1use miden_assembly::Library as CompiledLibrary;
2use midenc_hir::Symbol;
3use midenc_session::{diagnostics::Report, Emit, OutputMode, OutputType, Session};
4
5use crate::{Library, MastArtifact, Module, Program};
6
7pub enum MasmArtifact {
11    Executable(Box<Program>),
13    Library(Box<Library>),
15}
16
17impl MasmArtifact {
18    pub fn assemble(&self, session: &Session) -> Result<MastArtifact, Report> {
19        match self {
20            Self::Executable(program) => program.assemble(session).map(MastArtifact::Executable),
21            Self::Library(library) => library.assemble(session).map(MastArtifact::Library),
22        }
23    }
24
25    pub fn modules(&self) -> impl Iterator<Item = &Module> + '_ {
27        match self {
28            Self::Executable(ref program) => program.library().modules(),
29            Self::Library(ref lib) => lib.modules(),
30        }
31    }
32
33    pub fn insert(&mut self, module: Box<Module>) {
34        match self {
35            Self::Executable(ref mut program) => program.insert(module),
36            Self::Library(ref mut lib) => lib.insert(module),
37        }
38    }
39
40    pub fn link_library(&mut self, lib: CompiledLibrary) {
41        match self {
42            Self::Executable(ref mut program) => program.link_library(lib),
43            Self::Library(ref mut library) => library.link_library(lib),
44        }
45    }
46
47    pub fn unwrap_executable(self) -> Box<Program> {
48        match self {
49            Self::Executable(program) => program,
50            Self::Library(_) => panic!("tried to unwrap a mast library as an executable"),
51        }
52    }
53}
54
55impl Emit for MasmArtifact {
56    fn name(&self) -> Option<Symbol> {
57        None
58    }
59
60    fn output_type(&self, _mode: OutputMode) -> OutputType {
61        OutputType::Masm
62    }
63
64    fn write_to<W: std::io::Write>(
65        &self,
66        writer: W,
67        mode: OutputMode,
68        session: &Session,
69    ) -> std::io::Result<()> {
70        match self {
71            Self::Executable(ref prog) => prog.write_to(writer, mode, session),
72            Self::Library(ref lib) => lib.write_to(writer, mode, session),
73        }
74    }
75}