openvm_transpiler/
lib.rs

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