2022-06-20 20:32:29 -07:00
|
|
|
use ethereum_types::U256;
|
|
|
|
|
use plonky2_util::ceil_div_usize;
|
|
|
|
|
|
2022-07-23 11:16:45 +02:00
|
|
|
use crate::cpu::kernel::prover_input::ProverInputFn;
|
|
|
|
|
|
2022-06-20 20:32:29 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct File {
|
|
|
|
|
pub(crate) body: Vec<Item>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-25 23:10:08 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2022-06-20 20:32:29 -07:00
|
|
|
pub(crate) enum Item {
|
2022-07-07 08:59:53 -07:00
|
|
|
/// Defines a new macro: name, params, body.
|
|
|
|
|
MacroDef(String, Vec<String>, Vec<Item>),
|
|
|
|
|
/// Calls a macro: name, args.
|
|
|
|
|
MacroCall(String, Vec<PushTarget>),
|
2022-07-14 14:55:17 -07:00
|
|
|
/// Repetition, like `%rep` in NASM.
|
|
|
|
|
Repeat(Literal, Vec<Item>),
|
2022-07-19 15:28:34 -07:00
|
|
|
/// A directive to manipulate the stack according to a specified pattern.
|
|
|
|
|
/// The first list gives names to items on the top of the stack.
|
|
|
|
|
/// The second list specifies replacement items.
|
|
|
|
|
/// Example: `(a, b, c) -> (c, 5, 0x20, @SOME_CONST, a)`.
|
|
|
|
|
StackManipulation(Vec<String>, Vec<StackReplacement>),
|
2022-06-20 20:32:29 -07:00
|
|
|
/// Declares a global label.
|
|
|
|
|
GlobalLabelDeclaration(String),
|
|
|
|
|
/// Declares a label that is local to the current file.
|
|
|
|
|
LocalLabelDeclaration(String),
|
|
|
|
|
/// A `PUSH` operation.
|
|
|
|
|
Push(PushTarget),
|
2022-07-23 11:16:45 +02:00
|
|
|
/// A `ProverInput` operation.
|
|
|
|
|
ProverInput(ProverInputFn),
|
2022-06-20 20:32:29 -07:00
|
|
|
/// Any opcode besides a PUSH opcode.
|
|
|
|
|
StandardOp(String),
|
|
|
|
|
/// Literal hex data; should contain an even number of hex chars.
|
|
|
|
|
Bytes(Vec<Literal>),
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 15:28:34 -07:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub(crate) enum StackReplacement {
|
2022-07-30 21:51:55 -07:00
|
|
|
/// Can be either a named item or a label.
|
|
|
|
|
Identifier(String),
|
2022-07-19 15:28:34 -07:00
|
|
|
Literal(Literal),
|
|
|
|
|
MacroVar(String),
|
|
|
|
|
Constant(String),
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 20:32:29 -07:00
|
|
|
/// The target of a `PUSH` operation.
|
2022-07-30 21:51:55 -07:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
2022-06-20 20:32:29 -07:00
|
|
|
pub(crate) enum PushTarget {
|
|
|
|
|
Literal(Literal),
|
|
|
|
|
Label(String),
|
2022-07-07 08:59:53 -07:00
|
|
|
MacroVar(String),
|
2022-07-08 08:56:46 -07:00
|
|
|
Constant(String),
|
2022-06-20 20:32:29 -07:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 15:28:34 -07:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
2022-06-20 20:32:29 -07:00
|
|
|
pub(crate) enum Literal {
|
|
|
|
|
Decimal(String),
|
|
|
|
|
Hex(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Literal {
|
|
|
|
|
pub(crate) fn to_trimmed_be_bytes(&self) -> Vec<u8> {
|
|
|
|
|
let u256 = self.to_u256();
|
2022-07-01 09:29:56 -07:00
|
|
|
let num_bytes = ceil_div_usize(u256.bits(), 8).max(1);
|
2022-06-20 20:32:29 -07:00
|
|
|
// `byte` is little-endian, so we manually reverse it.
|
|
|
|
|
(0..num_bytes).rev().map(|i| u256.byte(i)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn to_u256(&self) -> U256 {
|
|
|
|
|
let (src, radix) = match self {
|
|
|
|
|
Literal::Decimal(s) => (s, 10),
|
|
|
|
|
Literal::Hex(s) => (s, 16),
|
|
|
|
|
};
|
|
|
|
|
U256::from_str_radix(src, radix)
|
|
|
|
|
.unwrap_or_else(|_| panic!("Not a valid u256 literal: {:?}", self))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn to_u8(&self) -> u8 {
|
|
|
|
|
let (src, radix) = match self {
|
|
|
|
|
Literal::Decimal(s) => (s, 10),
|
|
|
|
|
Literal::Hex(s) => (s, 16),
|
|
|
|
|
};
|
|
|
|
|
u8::from_str_radix(src, radix)
|
|
|
|
|
.unwrap_or_else(|_| panic!("Not a valid u8 literal: {:?}", self))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use crate::cpu::kernel::ast::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn literal_to_be_bytes() {
|
2022-07-01 09:29:56 -07:00
|
|
|
assert_eq!(
|
|
|
|
|
Literal::Decimal("0".into()).to_trimmed_be_bytes(),
|
|
|
|
|
vec![0x00]
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-20 20:32:29 -07:00
|
|
|
assert_eq!(
|
|
|
|
|
Literal::Decimal("768".into()).to_trimmed_be_bytes(),
|
|
|
|
|
vec![0x03, 0x00]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Literal::Hex("a1b2".into()).to_trimmed_be_bytes(),
|
|
|
|
|
vec![0xa1, 0xb2]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Literal::Hex("1b2".into()).to_trimmed_be_bytes(),
|
|
|
|
|
vec![0x1, 0xb2]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|