snarkvm_synthesizer_program/logic/instruction/operand/mod.rs
1// Copyright (c) 2019-2025 Provable Inc.
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 /// The operand is the program checksum.
48 /// If no program ID is specified, the checksum is for the current program.
49 /// If a program ID is specified, the checksum is for an external program.
50 Checksum(Option<ProgramID<N>>),
51 /// The operand is the program edition.
52 /// If no program ID is specified, the edition is for the current program.
53 /// If a program ID is specified, the edition is for an external program.
54 Edition(Option<ProgramID<N>>),
55 /// The operand is the program owner.
56 /// If no program ID is specified, the owner is for the current program.
57 /// If a program ID is specified, the owner is for an external program.
58 ProgramOwner(Option<ProgramID<N>>),
59}
60
61impl<N: Network> From<Literal<N>> for Operand<N> {
62 /// Initializes a new operand from a literal.
63 #[inline]
64 fn from(literal: Literal<N>) -> Self {
65 Operand::Literal(literal)
66 }
67}
68
69impl<N: Network> From<&Literal<N>> for Operand<N> {
70 /// Initializes a new operand from a reference to a literal.
71 #[inline]
72 fn from(literal: &Literal<N>) -> Self {
73 Operand::Literal(literal.clone())
74 }
75}
76
77impl<N: Network> From<Register<N>> for Operand<N> {
78 /// Initializes a new operand from a register.
79 #[inline]
80 fn from(register: Register<N>) -> Self {
81 Operand::Register(register)
82 }
83}
84
85impl<N: Network> From<&Register<N>> for Operand<N> {
86 /// Initializes a new operand from a reference to a register.
87 #[inline]
88 fn from(register: &Register<N>) -> Self {
89 Operand::Register(register.clone())
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96 use console::network::MainnetV0;
97
98 type CurrentNetwork = MainnetV0;
99
100 #[test]
101 fn test_operand_from_literal() -> Result<()> {
102 let literal = Literal::from_str("1field")?;
103 let expected = Operand::<CurrentNetwork>::Literal(literal.clone());
104
105 let operand = Operand::<CurrentNetwork>::from(literal);
106 assert_eq!(expected, operand);
107 Ok(())
108 }
109
110 #[test]
111 fn test_operand_from_register() -> Result<()> {
112 let register = Register::from_str("r0")?;
113 let expected = Operand::<CurrentNetwork>::Register(register.clone());
114
115 let operand = Operand::<CurrentNetwork>::from(register);
116 assert_eq!(expected, operand);
117 Ok(())
118 }
119
120 #[test]
121 fn test_operand_from_register_member() -> Result<()> {
122 let register = Register::from_str("r0.owner")?;
123 let expected = Operand::<CurrentNetwork>::Register(register.clone());
124
125 let operand = Operand::<CurrentNetwork>::from(register);
126 assert_eq!(expected, operand);
127 Ok(())
128 }
129}