openvm_transpiler/
lib.rs

1//! A transpiler from custom RISC-V ELFs to OpenVM executable binaries.
2
3use elf::Elf;
4use openvm_instructions::{exe::VmExe, program::Program};
5pub use openvm_platform;
6use openvm_stark_backend::p3_field::PrimeField32;
7use transpiler::{Transpiler, TranspilerError};
8
9use crate::util::elf_memory_image_to_openvm_memory_image;
10
11pub mod elf;
12pub mod transpiler;
13pub mod util;
14
15mod extension;
16pub use extension::{TranspilerExtension, TranspilerOutput};
17
18pub trait FromElf {
19    type ElfContext;
20    fn from_elf(elf: Elf, ctx: Self::ElfContext) -> Result<Self, TranspilerError>
21    where
22        Self: Sized;
23}
24
25impl<F: PrimeField32> FromElf for VmExe<F> {
26    type ElfContext = Transpiler<F>;
27    fn from_elf(elf: Elf, transpiler: Self::ElfContext) -> Result<Self, TranspilerError> {
28        let instructions = transpiler.transpile(&elf.instructions)?;
29        let program = Program::new_without_debug_infos_with_option(&instructions, elf.pc_base);
30        let mut init_memory = elf_memory_image_to_openvm_memory_image(elf.memory_image);
31        transpiler.modify_initial_memory(&mut init_memory)?;
32
33        Ok(VmExe {
34            program,
35            pc_start: elf.pc_start,
36            init_memory,
37            fn_bounds: elf.fn_bounds,
38        })
39    }
40}