1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod masm;
mod mast;

use midenc_hir::{
    self as hir,
    pass::{RewritePass, RewriteSet},
};
use midenc_session::{diagnostics::Report, Session};

pub use self::{masm::MasmArtifact, mast::MastArtifact};
use crate::{intrinsics, ConvertHirToMasm, Program};

pub type CompilerResult<T> = Result<T, Report>;

/// [MasmCompiler] is a compiler from Miden IR to MASM IR, an intermediate representation
/// of Miden Assembly which is used within the Miden compiler framework for various purposes,
/// and can be emitted directly to textual Miden Assembly.
///
/// The [MasmCompiler] is designed to compile a [midenc_hir::Program]
///
/// can be used to take a linked [midenc_hir::Program] and
/// compile it to MASM IR, an intermediate representation of Miden Assembly
/// used within the compiler.
pub struct MasmCompiler<'a> {
    session: &'a Session,
    analyses: hir::pass::AnalysisManager,
}
impl<'a> MasmCompiler<'a> {
    pub fn new(session: &'a Session) -> Self {
        Self {
            session,
            analyses: hir::pass::AnalysisManager::new(),
        }
    }

    /// Compile an [hir::Program] that has been linked and is ready to be compiled.
    pub fn compile(&mut self, mut input: Box<hir::Program>) -> CompilerResult<MasmArtifact> {
        use midenc_hir::pass::ConversionPass;

        let mut rewrites = default_rewrites([], self.session);

        let modules = input.modules_mut().take();
        for mut module in modules.into_iter() {
            rewrites.apply(&mut module, &mut self.analyses, self.session)?;
            input.modules_mut().insert(module);
        }

        let mut convert_to_masm = ConvertHirToMasm::<hir::Program>::default();
        let mut artifact = convert_to_masm.convert(input, &mut self.analyses, self.session)?;

        // Ensure intrinsics modules are linked
        artifact.insert(Box::new(
            intrinsics::load("intrinsics::mem", &self.session.source_manager)
                .expect("undefined intrinsics module"),
        ));
        artifact.insert(Box::new(
            intrinsics::load("intrinsics::i32", &self.session.source_manager)
                .expect("undefined intrinsics module"),
        ));
        artifact.insert(Box::new(
            intrinsics::load("intrinsics::i64", &self.session.source_manager)
                .expect("undefined intrinsics module"),
        ));

        Ok(artifact)
    }

    /// Compile a single [hir::Module] as a program.
    ///
    /// It is assumed that the given module has been validated, and that all necessary
    /// rewrites have been applied. If one of these invariants is not upheld, compilation
    /// may fail.
    pub fn compile_module(&mut self, input: Box<hir::Module>) -> CompilerResult<Box<Program>> {
        assert!(input.entrypoint().is_some(), "cannot compile a program without an entrypoint");

        let program =
            hir::ProgramBuilder::new(&self.session.diagnostics).with_module(input)?.link()?;

        match self.compile(program)? {
            MasmArtifact::Executable(program) => Ok(program),
            _ => unreachable!("expected compiler to produce an executable, got a library"),
        }
    }

    /// Compile a set of [hir::Module] as a program.
    ///
    /// It is assumed that the given modules have been validated, and that all necessary
    /// rewrites have been applied. If one of these invariants is not upheld, compilation
    /// may fail.
    pub fn compile_modules<I: IntoIterator<Item = Box<hir::Module>>>(
        &mut self,
        input: I,
    ) -> CompilerResult<Box<Program>> {
        let mut builder = hir::ProgramBuilder::new(&self.session.diagnostics);
        for module in input.into_iter() {
            builder.add_module(module)?;
        }

        let program = builder.link()?;

        assert!(program.has_entrypoint(), "cannot compile a program without an entrypoint");

        match self.compile(program)? {
            MasmArtifact::Executable(program) => Ok(program),
            _ => unreachable!("expected compiler to produce an executable, got a library"),
        }
    }
}

pub fn default_rewrites<P>(registered: P, session: &Session) -> RewriteSet<hir::Module>
where
    P: IntoIterator<Item = Box<dyn RewritePass<Entity = hir::Module>>>,
    <P as IntoIterator>::IntoIter: ExactSizeIterator,
{
    use midenc_hir::pass::ModuleRewritePassAdapter;

    let registered = registered.into_iter();

    // If no rewrites were explicitly enabled, and conversion to Miden Assembly is,
    // then we must ensure that the basic transformation passes are applied.
    //
    // Otherwise, assume that the intent was to skip those rewrites and do not add them
    let mut rewrites = RewriteSet::default();
    if registered.len() == 0 {
        if session.should_codegen() {
            let fn_rewrites = default_function_rewrites(session);
            for rewrite in fn_rewrites {
                rewrites.push(ModuleRewritePassAdapter::new(rewrite));
            }
        }
    } else {
        rewrites.extend(registered);
    }

    rewrites
}

pub fn default_function_rewrites(session: &Session) -> RewriteSet<hir::Function> {
    use midenc_hir_transform as transforms;

    // If no rewrites were explicitly enabled, and conversion to Miden Assembly is,
    // then we must ensure that the basic transformation passes are applied.
    //
    // Otherwise, assume that the intent was to skip those rewrites and do not add them
    let mut rewrites = RewriteSet::default();
    if session.should_codegen() {
        rewrites.push(transforms::SplitCriticalEdges);
        rewrites.push(transforms::Treeify);
        rewrites.push(transforms::InlineBlocks);
        rewrites.push(transforms::ApplySpills);
    }

    rewrites
}