snarkvm_synthesizer_program/logic/instruction/operand/
mod.rs

1// Copyright 2024-2025 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod bytes;
17mod parse;
18
19use console::{
20    network::prelude::*,
21    program::{Literal, ProgramID, Register},
22    types::Group,
23};
24
25/// The `Operand` enum represents the options for an operand in an instruction.
26/// This enum is designed to for instructions such as `add {Register} {Literal} into {Register}`.
27#[derive(Clone, PartialEq, Eq, Hash)]
28pub enum Operand<N: Network> {
29    /// The operand is a literal.
30    Literal(Literal<N>),
31    /// The operand is a register.
32    Register(Register<N>),
33    /// The operand is the program ID.
34    ProgramID(ProgramID<N>),
35    /// The operand is the signer address.
36    /// Note: This variant is only accessible in the `function` scope.
37    Signer,
38    /// The operand is the caller address.
39    /// Note: This variant is only accessible in the `function` scope.
40    Caller,
41    /// The operand is the block height.
42    /// Note: This variant is only accessible in the `finalize` scope.
43    BlockHeight,
44    /// The operand is the network ID.
45    /// Note: This variant is only accessible in the `finalize` scope.
46    NetworkID,
47}
48
49impl<N: Network> From<Literal<N>> for Operand<N> {
50    /// Initializes a new operand from a literal.
51    #[inline]
52    fn from(literal: Literal<N>) -> Self {
53        Operand::Literal(literal)
54    }
55}
56
57impl<N: Network> From<&Literal<N>> for Operand<N> {
58    /// Initializes a new operand from a reference to a literal.
59    #[inline]
60    fn from(literal: &Literal<N>) -> Self {
61        Operand::Literal(literal.clone())
62    }
63}
64
65impl<N: Network> From<Register<N>> for Operand<N> {
66    /// Initializes a new operand from a register.
67    #[inline]
68    fn from(register: Register<N>) -> Self {
69        Operand::Register(register)
70    }
71}
72
73impl<N: Network> From<&Register<N>> for Operand<N> {
74    /// Initializes a new operand from a reference to a register.
75    #[inline]
76    fn from(register: &Register<N>) -> Self {
77        Operand::Register(register.clone())
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84    use console::network::MainnetV0;
85
86    type CurrentNetwork = MainnetV0;
87
88    #[test]
89    fn test_operand_from_literal() -> Result<()> {
90        let literal = Literal::from_str("1field")?;
91        let expected = Operand::<CurrentNetwork>::Literal(literal.clone());
92
93        let operand = Operand::<CurrentNetwork>::from(literal);
94        assert_eq!(expected, operand);
95        Ok(())
96    }
97
98    #[test]
99    fn test_operand_from_register() -> Result<()> {
100        let register = Register::from_str("r0")?;
101        let expected = Operand::<CurrentNetwork>::Register(register.clone());
102
103        let operand = Operand::<CurrentNetwork>::from(register);
104        assert_eq!(expected, operand);
105        Ok(())
106    }
107
108    #[test]
109    fn test_operand_from_register_member() -> Result<()> {
110        let register = Register::from_str("r0.owner")?;
111        let expected = Operand::<CurrentNetwork>::Register(register.clone());
112
113        let operand = Operand::<CurrentNetwork>::from(register);
114        assert_eq!(expected, operand);
115        Ok(())
116    }
117}