zkevm-assembly 0.150.17

The zkEVM common utilities
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use zkevm_opcode_defs::ImmMemHandlerFlags;
use zkevm_opcode_defs::RegOrImmFlags;

use super::*;

use crate::assembly::mnemonic::all_from_tag_until_1_noconsume;

use crate::assembly::mnemonic::all_until_1_noconsume_inclusive;

use crate::assembly::operand::GenericOperand;
use nom::error::ParseError;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum OperandType {
    Definition(zkevm_opcode_defs::Operand),
    Label,
}

pub(crate) fn marker_full_operand() -> OperandType {
    OperandType::Definition(zkevm_opcode_defs::Operand::Full(
        ImmMemHandlerFlags::UseRegOnly,
    ))
}

pub(crate) fn marker_register_operand() -> OperandType {
    OperandType::Definition(zkevm_opcode_defs::Operand::RegOnly)
}

pub(crate) fn marker_non_mem_operand() -> OperandType {
    OperandType::Definition(zkevm_opcode_defs::Operand::RegOrImm(
        RegOrImmFlags::UseRegOnly,
    ))
}

#[track_caller]
pub(crate) fn parse_opcode_and_rest<'a>(input: &'a str) -> IResult<&'a str, (&'a str, &'a str)> {
    // only split opcode from any potential arguments

    let mut parser = nom::sequence::tuple::<_, _, nom::error::Error<_>, _>((
        all_until1(nom::character::complete::space1),
        nom::character::complete::space0,
        nom::combinator::rest,
    ));

    if let Ok((_, result)) = parser(input) {
        let opcode = result.0;
        let arguments = result.2;
        return Ok(("", (opcode, arguments)));
    }

    // edge case of opcode without arguments
    let mut parser = nom::sequence::tuple::<_, _, nom::error::Error<_>, _>((
        all_until1(nom::combinator::eof),
        nom::combinator::rest,
    ));
    let (_, result) = parser(input)?;
    let opcode = result.0;
    let arguments = "";

    Ok(("", (opcode, arguments)))
}

#[track_caller]
pub(crate) fn split_arguments<'a>(input: &'a str) -> IResult<&'a str, Vec<&'a str>> {
    // only split opcode from any potential arguments

    if input.is_empty() {
        return Ok((input, vec![]));
    }

    let mut rest = input;
    let mut results = Vec::with_capacity(4);

    for i in 0..4 {
        if i != 3 {
            let mut parser = nom::sequence::tuple::<_, _, nom::error::Error<_>, _>((
                nom::character::complete::space0,
                all_until1(nom::branch::alt((
                    nom::bytes::complete::tag(","),
                    nom::combinator::eof,
                ))),
                nom::character::complete::space0,
                nom::combinator::rest,
            ));

            if let Ok(result) = parser.parse(rest) {
                let result = result.1;
                let body = result.1;
                let tail = result.3;
                results.push(body);
                rest = tail;
            } else if rest.is_empty() {
                return Ok((rest, results));
            } else {
                return Err(nom::Err::Error(nom::error::Error::from_error_kind(
                    input,
                    nom::error::ErrorKind::TooLarge,
                )));
            }
        } else {
            let mut parser = nom::sequence::tuple::<_, _, nom::error::Error<_>, _>((
                nom::character::complete::space0,
                all_until1(nom::combinator::eof),
            ));

            if let Ok(result) = parser.parse(rest) {
                let result = result.1;
                let body = result.1;
                results.push(body);
            } else if rest.is_empty() {
                return Ok((rest, results));
            } else {
                return Err(nom::Err::Error(nom::error::Error::from_error_kind(
                    input,
                    nom::error::ErrorKind::TooLarge,
                )));
            }
        }
    }

    Ok((rest, results))
}

#[track_caller]
fn parse_opcode_and_modifiers<'a>(
    input: &'a str,
) -> Result<(&'a str, HashSet<&'a str>), InstructionReadError> {
    let mut modifiers = HashSet::new();
    // now try to parse the opcode body into the reference opcode and potentially some modifiers
    let mut parser_with_modifiers = nom::sequence::tuple::<_, _, nom::error::Error<_>, _>((
        // body
        all_until_1_noconsume_inclusive(nom::bytes::complete::tag(".")),
        // and now potentially many of modifiers except the last one
        // because there is no space at the end
        nom::multi::many0(all_from_tag_until_1_noconsume(
            ".",
            nom::branch::alt((nom::bytes::complete::tag("."), nom::combinator::eof)),
        )),
        nom::combinator::eof,
    ));

    if let Ok((_, result)) = parser_with_modifiers.parse(input) {
        // parse modifiers
        let opcode = result.0;
        let mods = result.1;
        for m in mods.into_iter() {
            let m = if let Some(m) = m.strip_suffix('.') {
                m
            } else {
                assert!(!m.contains('.'));

                m
            };

            let is_fresh = modifiers.insert(m);
            if !is_fresh {
                return Err(InstructionReadError::DuplicateModifier(m.to_owned()));
            }
        }

        Ok((opcode, modifiers))
    } else {
        let mut parsers_without_modifiers =
            nom::sequence::tuple::<_, _, nom::error::Error<_>, _>((
                // may be body
                all_until1(nom::combinator::eof),
                nom::combinator::eof,
            ));

        let (_, result) = parsers_without_modifiers
            .parse(input)
            .map_err(|_| InstructionReadError::UnknownMnemonic(input.to_owned()))?;
        let opcode = result.0;

        Ok((opcode, modifiers))
    }
}

#[track_caller]
pub(crate) fn parse_code_element_inner<'a>(
    input: &'a str,
) -> Result<(&'a str, HashSet<&'a str>, Vec<&'a str>), InstructionReadError> {
    let (_, (body, arguments)) = parse_opcode_and_rest(input)
        .map_err(|_| InstructionReadError::UnknownMnemonic(input.to_owned()))?;
    let (_, arguments) = split_arguments(arguments)
        .map_err(|_| InstructionReadError::UnknownMnemonic(input.to_owned()))?;
    let (body, modifiers) = parse_opcode_and_modifiers(body)?;

    Ok((body, modifiers, arguments))
}

#[track_caller]
pub(crate) fn parse_canonical_operands_sequence<'a>(
    arguments: Vec<&'a str>,
    sources: &[OperandType],
    destinations: &[OperandType],
) -> Result<Vec<FullOperand>, InstructionReadError> {
    if arguments.len() != sources.len() + destinations.len() {
        return Err(InstructionReadError::InvalidArgumentCount {
            expected: sources.len() + destinations.len(),
            found: arguments.len(),
        });
    }

    let mut results = Vec::with_capacity(arguments.len());

    let mut it = arguments.into_iter();
    let mut idx = 0;

    for src in sources.iter() {
        let input = it.next().unwrap();
        match src {
            OperandType::Definition(def) => {
                match def {
                    zkevm_opcode_defs::Operand::Full(_) => {
                        use crate::assembly::parse::addressing::parse_full_operand;
                        let (_, operand) = parse_full_operand(input).map_err(|_| {
                            InstructionReadError::InvalidGenericOperand(input.to_owned())
                        })?;
                        results.push(operand);
                    }
                    zkevm_opcode_defs::Operand::RegOnly => {
                        let (_, (register, imm)) = parse_absolute_addressing_single(input)
                            .map_err(|_| {
                                InstructionReadError::InvalidAbsoluteLikeAddress(input.to_owned())
                            })?;

                        use crate::assembly::parse::addressing::parse_absolute_addressing_single;
                        if imm != 0 {
                            return Err(InstructionReadError::InvalidOperandImmInRegLocation(
                                input.to_owned(),
                            ));
                        }

                        let operand = FullOperand::Register(register);
                        results.push(operand);
                    }
                    zkevm_opcode_defs::Operand::RegOrImm(_) => {
                        use crate::assembly::parse::addressing::parse_full_operand;
                        let (_, operand) = parse_full_operand(input).map_err(|_| {
                            InstructionReadError::InvalidGenericOperand(input.to_owned())
                        })?;
                        let _as_reg_imm = operand.clone().as_non_memory_operand(idx)?;
                        results.push(operand);
                    }
                }
                // we parse an operand that can be reg-only, or full one
            }
            OperandType::Label => {
                // we expect to find a label
                use crate::assembly::parse::constant_operand::parse_constant_operand;

                let (_, label) = parse_constant_operand(input).map_err(|_| {
                    InstructionReadError::InvalidLabeledConstantOperand(input.to_owned())
                })?;
                results.push(label);
            }
        }

        idx += 1;
    }

    for dst in destinations.iter() {
        let input = it.next().unwrap();
        match dst {
            OperandType::Definition(def) => {
                match def {
                    zkevm_opcode_defs::Operand::Full(_) => {
                        use crate::assembly::parse::addressing::parse_full_operand;
                        let (_, operand) = parse_full_operand(input).map_err(|_| {
                            InstructionReadError::InvalidGenericOperand(input.to_owned())
                        })?;
                        match &operand {
                            FullOperand::Full(GenericOperand { r#type: t, .. }) => {
                                if !t.is_allowed_for_dst() {
                                    dbg!(t);
                                    return Err(InstructionReadError::InvalidArgument {
                                        index: idx,
                                        expected: "operand that can be destination",
                                        found: input.to_owned(),
                                    });
                                }
                            }
                            _ => {}
                        }
                        results.push(operand);
                    }
                    zkevm_opcode_defs::Operand::RegOnly => {
                        let (_, (register, imm)) = parse_absolute_addressing_single(input)
                            .map_err(|_| {
                                InstructionReadError::InvalidAbsoluteLikeAddress(input.to_owned())
                            })?;

                        use crate::assembly::parse::addressing::parse_absolute_addressing_single;
                        if imm != 0 {
                            return Err(InstructionReadError::InvalidOperandImmInRegLocation(
                                input.to_owned(),
                            ));
                        }

                        let operand = FullOperand::Register(register);
                        results.push(operand);
                    }
                    zkevm_opcode_defs::Operand::RegOrImm(_) => {
                        unreachable!()
                    }
                }
                // we parse an operand that can be reg-only, or full one
            }
            OperandType::Label => {
                panic!("Label can not be in dst position");
            }
        }

        idx += 1;
    }

    Ok(results)
}

use crate::assembly::mnemonic::*;
use lazy_static::lazy_static;

lazy_static! {
    pub(crate) static ref ALL_CANONICALIZATION_TRANSFORMERS: Vec<Box<dyn Fn(&str) -> IResult<&str, String> + 'static + Send + Sync>> =
        vec![Box::from(parse_set_flags_combinator),];
    pub(crate) static ref ALL_MNEMONIC_TRANSFORMERS: Vec<Box<dyn Fn(&str) -> IResult<&str, String> + 'static + Send + Sync>> = {
        vec![
            Box::from(parse_nop_combinator),
            Box::from(parse_mov_combinator),
            Box::from(parse_xor_combinator),
            Box::from(parse_and_combinator),
            Box::from(parse_or_combinator),
            Box::from(parse_shl_combinator),
            Box::from(parse_shr_combinator),
            Box::from(parse_rol_combinator),
            Box::from(parse_ror_combinator),
            // Box::from(parse_advance_sp_combinator),
            Box::from(parse_shorthand_ret),
            Box::from(parse_shorthand_revert),
            Box::from(parse_shorthand_panic),
            Box::from(parse_invoke_combinator),
            Box::from(parse_push_combinator),
            Box::from(parse_pop_combinator),
            Box::from(parse_sread_combinator),
            Box::from(parse_sload_combinator),
            Box::from(parse_sstore_combinator),
            Box::from(parse_tread_combinator),
            Box::from(parse_tload_combinator),
            Box::from(parse_tstore_combinator),
            Box::from(parse_decom_combinator),
            Box::from(parse_event_combinator),
            Box::from(parse_to_l1_combinator),
            Box::from(parse_gas_left_combinator),
            Box::from(parse_set_gas_per_pubdatagas_left_combinator),
            Box::from(parse_precompile_combinator),
            Box::from(parse_increase_sp_shorthard),
            Box::from(parse_decrease_sp_shorthard),
            Box::from(parse_shorthand_near_call),
            Box::from(parse_shorthand_exceptionless_near_call),
            Box::from(parse_uma_heap_read_combinator),
            Box::from(parse_uma_aux_heap_read_combinator),
            Box::from(parse_uma_heap_write_combinator),
            Box::from(parse_uma_aux_heap_write_combinator),
            Box::from(parse_uma_fat_ptr_read_combinator),
            Box::from(parse_uma_heap_read_increment_combinator),
            Box::from(parse_uma_aux_heap_read_increment_combinator),
            Box::from(parse_uma_heap_write_increment_combinator),
            Box::from(parse_uma_aux_heap_write_increment_combinator),
            Box::from(parse_uma_fat_ptr_read_increment_combinator),
        ]
    };
}

#[track_caller]
pub(crate) fn parse_code_element<'a>(input: &'a str) -> Result<Instruction, InstructionReadError> {
    // first we canonicalize
    let mut canonical = input.to_owned();

    for transformer in ALL_CANONICALIZATION_TRANSFORMERS.iter() {
        if let Ok((_, transformed)) = transformer(&canonical) {
            canonical = transformed;
        }
    }
    // then apply some short mnemonics into full forms
    for transformer in ALL_MNEMONIC_TRANSFORMERS.iter() {
        if let Ok((_, transformed)) = transformer(&canonical) {
            canonical = transformed;
            break;
        }
    }
    // now parse
    let (opcode, modifiers, arguments) = parse_code_element_inner(&canonical)?;

    Instruction::try_from_parts(opcode, modifiers, arguments)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_parse_trivial_opcode() {
        let opcode = parse_code_element("nop r0, r0, r0, r0").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_parse_add_opcode() {
        let opcode = parse_code_element("add! stack-=[r1 + 4], r5, stack=[42]").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_parse_event_shorthand() {
        let opcode = parse_code_element("event.first r6, r5").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_parse_imms_in_addressing() {
        let opcode = parse_code_element("add 4, r0, stack=[r2 + 1]").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_stack_adjustment_shorthand() {
        let opcode = parse_code_element("nop stack+=[5]").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_uma_and_increment() {
        let opcode = parse_code_element("ld.1.inc r2, r4, r2").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_parse_ptr_add_opcode() {
        let opcode = parse_code_element("ptr.add stack-=[r1 + 4], r5, stack=[42]").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_parse_uma_read_ptr_opcode() {
        let opcode = parse_code_element("ld r1, r2").unwrap();
        dbg!(opcode);
        let opcode = parse_code_element("ld.inc r1, r2, r3").unwrap();
        dbg!(opcode);
        let opcode = parse_code_element("st.1.inc r1, r2, r3").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_shorthand_ret_like() {
        let opcode = parse_code_element("ret").unwrap();
        dbg!(opcode);
        let opcode = parse_code_element("revert").unwrap();
        dbg!(opcode);
        let opcode = parse_code_element("panic").unwrap();
        dbg!(opcode);
    }

    #[test]
    fn test_parse_global_var() {
        let opcode = parse_code_element("add! stack[@var + 4], r5, stack=[42]").unwrap();
        dbg!(opcode);
    }
}