openvm_native_guest_macro/
parse_compiler_output.rs

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
use openvm_instructions::instruction::Instruction;
use openvm_native_transpiler::deserialize_defined_instructions;
use p3_field::{Field, PrimeField32};

use crate::parse_kernel::ParsedKernel;

#[derive(Debug)]
pub struct CompiledKernelArgument {
    pub name: String,
    pub rust_type: String,
    pub edsl_type: String,
    pub fp: usize,
}

#[derive(Debug)]
pub struct CompiledKernel<F: Field> {
    pub function_name: String,
    pub arguments: Vec<CompiledKernelArgument>,
    pub body: Vec<Instruction<F>>,
    pub rust_return_type: String,
    pub edsl_return_type: String,
    pub return_fp: usize,
}

pub fn parse_compiled_kernel<F: PrimeField32>(
    parsed_kernel: ParsedKernel,
    compiler_output: String,
) -> CompiledKernel<F> {
    let words: Vec<u32> = compiler_output
        .lines()
        .map(|line| line.parse().unwrap())
        .collect();
    let mut index = 0;

    let arguments = parsed_kernel
        .arguments
        .into_iter()
        .map(|argument| {
            let name = argument.name;
            let rust_type = argument.rust_type;
            let edsl_type = argument.edsl_type;
            let fp = words[index] as usize;
            index += 1;
            CompiledKernelArgument {
                name,
                rust_type,
                edsl_type,
                fp,
            }
        })
        .collect::<Vec<_>>();
    let return_fp = words[index] as usize;
    index += 1;

    let instructions = deserialize_defined_instructions(&words[index..]);

    CompiledKernel {
        function_name: parsed_kernel.function_name,
        arguments,
        body: instructions,
        rust_return_type: parsed_kernel.rust_return_type,
        edsl_return_type: parsed_kernel.edsl_return_type,
        return_fp,
    }
}