openvm_custom_insn/
lib.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use proc_macro2::{Span, TokenStream};
use syn::{
    parse::{Parse, ParseStream},
    Ident, Token,
};

enum AsmArg {
    In(TokenStream),
    Out(TokenStream),
    InOut(TokenStream),
    ConstExpr(TokenStream),
    ConstLit(syn::LitStr),
}

struct CustomInsnR {
    pub rd: AsmArg,
    pub rs1: AsmArg,
    pub rs2: AsmArg,
    pub opcode: TokenStream,
    pub funct3: TokenStream,
    pub funct7: TokenStream,
}

struct CustomInsnI {
    pub rd: AsmArg,
    pub rs1: AsmArg,
    pub imm: AsmArg,
    pub opcode: TokenStream,
    pub funct3: TokenStream,
}

/// Returns `(rd, rs1, opcode, funct3)`.
#[allow(clippy::type_complexity)]
fn parse_common_fields(
    input: ParseStream,
) -> syn::Result<(
    Option<AsmArg>,
    Option<AsmArg>,
    Option<TokenStream>,
    Option<TokenStream>,
)> {
    let mut rd = None;
    let mut rs1 = None;
    let mut opcode = None;
    let mut funct3 = None;

    while !input.is_empty() {
        let key: Ident = input.parse()?;
        input.parse::<Token![=]>()?;

        let value = if key == "opcode" || key == "funct3" {
            let mut tokens = TokenStream::new();
            while !input.is_empty() && !input.peek(Token![,]) {
                tokens.extend(TokenStream::from(input.parse::<proc_macro2::TokenTree>()?));
            }
            match key.to_string().as_str() {
                "opcode" => opcode = Some(tokens),
                "funct3" => funct3 = Some(tokens),
                _ => unreachable!(),
            }
            None
        } else if key == "rd" || key == "rs1" {
            Some(parse_asm_arg(input)?)
        } else {
            while !input.is_empty() && !input.peek(Token![,]) {
                input.parse::<proc_macro2::TokenTree>()?;
            }
            None
        };

        match key.to_string().as_str() {
            "rd" => rd = value,
            "rs1" => rs1 = value,
            "opcode" | "funct3" => (),
            // Skip other fields instead of returning an error
            _ => {
                if !input.is_empty() {
                    input.parse::<Token![,]>()?;
                }
                continue;
            }
        }

        if !input.is_empty() {
            input.parse::<Token![,]>()?;
        }
    }

    Ok((rd, rs1, opcode, funct3))
}

// Helper function to parse AsmArg
fn parse_asm_arg(input: ParseStream) -> syn::Result<AsmArg> {
    let lookahead = input.lookahead1();
    if lookahead.peek(kw::In) {
        input.parse::<kw::In>()?;
        let mut tokens = TokenStream::new();
        while !input.is_empty() && !input.peek(Token![,]) {
            tokens.extend(TokenStream::from(input.parse::<proc_macro2::TokenTree>()?));
        }
        Ok(AsmArg::In(tokens))
    } else if lookahead.peek(kw::Out) {
        // ... similar for Out
        input.parse::<kw::Out>()?;
        let mut tokens = TokenStream::new();
        while !input.is_empty() && !input.peek(Token![,]) {
            tokens.extend(TokenStream::from(input.parse::<proc_macro2::TokenTree>()?));
        }
        Ok(AsmArg::Out(tokens))
    } else if lookahead.peek(kw::InOut) {
        // ... similar for InOut
        input.parse::<kw::InOut>()?;
        let mut tokens = TokenStream::new();
        while !input.is_empty() && !input.peek(Token![,]) {
            tokens.extend(TokenStream::from(input.parse::<proc_macro2::TokenTree>()?));
        }
        Ok(AsmArg::InOut(tokens))
    } else if lookahead.peek(kw::Const) {
        input.parse::<kw::Const>()?;
        if input.peek(syn::LitStr) {
            Ok(AsmArg::ConstLit(input.parse()?))
        } else {
            let mut tokens = TokenStream::new();
            while !input.is_empty() && !input.peek(Token![,]) {
                tokens.extend(TokenStream::from(input.parse::<proc_macro2::TokenTree>()?));
            }
            Ok(AsmArg::ConstExpr(tokens))
        }
    } else {
        Err(lookahead.error())
    }
}

impl Parse for CustomInsnR {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let input_fork = input.fork();
        let (rd, rs1, opcode, funct3) = parse_common_fields(input)?;

        // Parse rs2 and funct7 from the forked input
        let mut rs2 = None;
        let mut funct7 = None;
        while !input_fork.is_empty() {
            let key: Ident = input_fork.parse()?;
            input_fork.parse::<Token![=]>()?;

            if key == "rs2" {
                rs2 = Some(parse_asm_arg(&input_fork)?);
            } else if key == "funct7" {
                let mut tokens = TokenStream::new();
                while !input_fork.is_empty() && !input_fork.peek(Token![,]) {
                    tokens.extend(TokenStream::from(
                        input_fork.parse::<proc_macro2::TokenTree>()?,
                    ));
                }
                funct7 = Some(tokens);
            } else {
                // Skip other fields
                while !input_fork.is_empty() && !input_fork.peek(Token![,]) {
                    input_fork.parse::<proc_macro2::TokenTree>()?;
                }
            }

            if !input_fork.is_empty() {
                input_fork.parse::<Token![,]>()?;
            }
        }

        let opcode = opcode.ok_or_else(|| syn::Error::new(input.span(), "missing opcode field"))?;
        let funct3 = funct3.ok_or_else(|| syn::Error::new(input.span(), "missing funct3 field"))?;
        let funct7 = funct7.ok_or_else(|| syn::Error::new(input.span(), "missing funct7 field"))?;
        let rd = rd.ok_or_else(|| syn::Error::new(input.span(), "missing rd field"))?;
        let rs1 = rs1.ok_or_else(|| syn::Error::new(input.span(), "missing rs1 field"))?;
        let rs2 = rs2.ok_or_else(|| syn::Error::new(input.span(), "missing rs2 field"))?;

        Ok(CustomInsnR {
            rd,
            rs1,
            rs2,
            opcode,
            funct3,
            funct7,
        })
    }
}

impl Parse for CustomInsnI {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let input_fork = input.fork();
        let (rd, rs1, opcode, funct3) = parse_common_fields(input)?;

        // Parse imm from the forked input
        let mut imm = None;
        while !input_fork.is_empty() {
            let key: Ident = input_fork.parse()?;
            input_fork.parse::<Token![=]>()?;

            if key == "imm" {
                let value = parse_asm_arg(&input_fork)?;
                match value {
                    AsmArg::ConstLit(lit) => imm = Some(AsmArg::ConstLit(lit)),
                    AsmArg::ConstExpr(expr) => imm = Some(AsmArg::ConstExpr(expr)),
                    _ => return Err(syn::Error::new(key.span(), "imm must be a Const")),
                }
            } else {
                // Skip other fields
                while !input_fork.is_empty() && !input_fork.peek(Token![,]) {
                    input_fork.parse::<proc_macro2::TokenTree>()?;
                }
            }

            if !input_fork.is_empty() {
                input_fork.parse::<Token![,]>()?;
            }
        }

        let opcode = opcode.ok_or_else(|| syn::Error::new(input.span(), "missing opcode field"))?;
        let funct3 = funct3.ok_or_else(|| syn::Error::new(input.span(), "missing funct3 field"))?;
        let rd = rd.ok_or_else(|| syn::Error::new(input.span(), "missing rd field"))?;
        let rs1 = rs1.ok_or_else(|| syn::Error::new(input.span(), "missing rs1 field"))?;
        let imm = imm.ok_or_else(|| syn::Error::new(input.span(), "missing imm field"))?;

        Ok(CustomInsnI {
            rd,
            rs1,
            imm,
            opcode,
            funct3,
        })
    }
}

// Helper function for handling register arguments in both proc macros
fn handle_reg_arg(
    template: &mut String,
    args: &mut Vec<proc_macro2::TokenStream>,
    arg: &AsmArg,
    reg_name: &str,
) {
    let reg_ident = syn::Ident::new(reg_name, Span::call_site());
    match arg {
        AsmArg::ConstLit(lit) => {
            template.push_str(", ");
            template.push_str(&lit.value());
        }
        AsmArg::In(tokens) => {
            template.push_str(", {");
            template.push_str(reg_name);
            template.push('}');
            args.push(quote::quote! { #reg_ident = in(reg) #tokens });
        }
        AsmArg::Out(tokens) => {
            template.push_str(", {");
            template.push_str(reg_name);
            template.push('}');
            args.push(quote::quote! { #reg_ident = out(reg) #tokens });
        }
        AsmArg::InOut(tokens) => {
            template.push_str(", {");
            template.push_str(reg_name);
            template.push('}');
            args.push(quote::quote! { #reg_ident = inout(reg) #tokens });
        }
        AsmArg::ConstExpr(tokens) => {
            template.push_str(", {");
            template.push_str(reg_name);
            template.push('}');
            args.push(quote::quote! { #reg_ident = const #tokens });
        }
    }
}

mod kw {
    syn::custom_keyword!(In);
    syn::custom_keyword!(Out);
    syn::custom_keyword!(InOut);
    syn::custom_keyword!(Const);
}

/// Custom RISC-V instruction macro for the zkVM.
///
/// This macro is used to define custom R-type RISC-V instructions for the zkVM.
/// Usage:
/// ```rust
/// custom_insn_r!(
///     opcode = OPCODE,
///     funct3 = FUNCT3,
///     funct7 = FUNCT7,
///     rd = InOut x0,
///     rs1 = In rs1,
///     rs2 = In rs2
/// );
/// ```
/// Here, `opcode`, `funct3`, and `funct7` are the opcode, funct3, and funct7 fields of the RISC-V instruction.
/// `rd`, `rs1`, and `rs2` are the destination register, source register 1, and source register 2 respectively.
/// The `In`, `Out`, `InOut`, and `Const` keywords are required to specify the type of the register arguments.
/// They translate to `in(reg)`, `out(reg)`, `inout(reg)`, and `const` respectively, and mean
/// - "read the value from this variable" before execution (`In`),
/// - "write the value to this variable" after execution (`Out`),
/// - "read the value from this variable, then write it back to the same variable" after execution (`InOut`), and
/// - "use this constant value" (`Const`).
#[proc_macro]
pub fn custom_insn_r(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let CustomInsnR {
        rd,
        rs1,
        rs2,
        opcode,
        funct3,
        funct7,
    } = syn::parse_macro_input!(input as CustomInsnR);

    let mut template = String::from(".insn r {opcode}, {funct3}, {funct7}");
    let mut args = vec![];

    // Helper function to handle register arguments
    handle_reg_arg(&mut template, &mut args, &rd, "rd");
    handle_reg_arg(&mut template, &mut args, &rs1, "rs1");
    handle_reg_arg(&mut template, &mut args, &rs2, "rs2");

    let expanded = quote::quote! {
        #[cfg(target_os = "zkvm")]
        unsafe {
            core::arch::asm!(
                #template,
                opcode = const #opcode,
                funct3 = const #funct3,
                funct7 = const #funct7,
                #(#args),*
            )
        }
    };

    expanded.into()
}

/// Custom RISC-V instruction macro for the zkVM.
///
/// This macro is used to define custom I-type RISC-V instructions for the zkVM.
/// Usage:
/// ```rust
/// custom_insn_r!(
///     opcode = OPCODE,
///     funct3 = FUNCT3,
///     rd = InOut x0,
///     rs1 = In rs1,
///     imm = Const 123
/// );
/// ```
/// Here, `opcode`, `funct3` are the opcode and funct3 fields of the RISC-V instruction.
/// `rd`, `rs1`, and `imm` are the destination register, source register 1, and immediate value respectively.
/// The `In`, `Out`, `InOut`, and `Const` keywords are required to specify the type of the register arguments.
/// They translate to `in(reg)`, `out(reg)`, `inout(reg)`, and `const` respectively, and mean
/// - "read the value from this variable" before execution (`In`),
/// - "write the value to this variable" after execution (`Out`),
/// - "read the value from this variable, then write it back to the same variable" after execution (`InOut`), and
/// - "use this constant value" (`Const`).
///
/// The `imm` argument is required to be a constant value.
#[proc_macro]
pub fn custom_insn_i(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let CustomInsnI {
        rd,
        rs1,
        imm,
        opcode,
        funct3,
    } = syn::parse_macro_input!(input as CustomInsnI);

    let mut template = String::from(".insn i {opcode}, {funct3}");
    let mut args = vec![];

    // Helper function to handle register arguments
    handle_reg_arg(&mut template, &mut args, &rd, "rd");
    handle_reg_arg(&mut template, &mut args, &rs1, "rs1");
    handle_reg_arg(&mut template, &mut args, &imm, "imm");

    let expanded = quote::quote! {
        #[cfg(target_os = "zkvm")]
        unsafe {
            core::arch::asm!(
                #template,
                opcode = const #opcode,
                funct3 = const #funct3,
                #(#args),*
            )
        }
    };

    expanded.into()
}