ark_ff_asm/context/
data_structures.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
use std::fmt;

#[derive(Clone)]
pub enum AssemblyVar {
    Memory(String),
    Variable(String),
    Fixed(String),
}

impl AssemblyVar {
    pub fn memory_access(&self, offset: usize) -> Option<AssemblyVar> {
        match self {
            Self::Variable(a) | Self::Fixed(a) => Some(Self::Memory(format!("{}({})", offset, a))),
            _ => None,
        }
    }

    pub fn memory_accesses(&self, range: usize) -> Vec<AssemblyVar> {
        (0..range)
            .map(|i| {
                let offset = i * 8;
                self.memory_access(offset).unwrap()
            })
            .collect()
    }
}

impl fmt::Display for AssemblyVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::Variable(a) | Self::Fixed(a) | Self::Memory(a) => write!(f, "{}", a),
        }
    }
}

impl<'a> From<Declaration<'a>> for AssemblyVar {
    fn from(other: Declaration<'a>) -> Self {
        Self::Variable(format!("{{{}}}", other.name))
    }
}

impl<'a> From<Register<'a>> for AssemblyVar {
    fn from(other: Register<'a>) -> Self {
        Self::Fixed(format!("%{}", other.0))
    }
}

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Register<'a>(pub &'a str);
impl fmt::Display for Register<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "\"{}\"", self.0)
    }
}

#[derive(Copy, Clone)]
pub struct Declaration<'a> {
    /// Name of the assembly template variable declared by `self`.
    pub name: &'a str,
    /// Rust expression whose value is declared in `self`.
    pub expr: &'a str,
}

impl fmt::Display for Declaration<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "{} = in(reg) {},", self.name, self.expr)
    }
}