runmat_vm/bytecode/
compile.rs1#[cfg(feature = "native-accel")]
2use crate::accel::graph::build_accel_graph;
3#[cfg(feature = "native-accel")]
4use crate::accel::stack_layout::annotate_fusion_groups_with_stack_layout;
5use crate::bytecode::{Bytecode, UserFunction};
6use crate::compiler::{CompileError, Compiler};
7use runmat_hir::HirProgram;
8use std::collections::HashMap;
9
10pub fn compile(
11 prog: &HirProgram,
12 existing_functions: &HashMap<String, UserFunction>,
13) -> Result<Bytecode, CompileError> {
14 let mut c = Compiler::new(prog);
15 c.functions = existing_functions.clone();
16 c.compile_program(prog)?;
17 #[cfg(feature = "native-accel")]
18 let accel_graph = build_accel_graph(&c.instructions, &c.var_types);
19 #[cfg(feature = "native-accel")]
20 let mut fusion_groups = accel_graph.detect_fusion_groups();
21 #[cfg(feature = "native-accel")]
22 annotate_fusion_groups_with_stack_layout(&c.instructions, &accel_graph, &mut fusion_groups);
23 Ok(Bytecode {
24 instructions: c.instructions,
25 instr_spans: c.instr_spans,
26 call_arg_spans: c.call_arg_spans,
27 source_id: None,
28 var_count: c.var_count,
29 functions: c.functions,
30 var_types: c.var_types,
31 var_names: HashMap::new(),
32 #[cfg(feature = "native-accel")]
33 accel_graph: Some(accel_graph),
34 #[cfg(feature = "native-accel")]
35 fusion_groups,
36 })
37}