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 block timestamp.
45 /// Note: This variant is only accessible in the `finalize` scope.
46 BlockTimestamp,
47 /// The operand is the network ID.
48 /// Note: This variant is only accessible in the `finalize` scope.
49 NetworkID,
50 /// The operand is the program checksum.
51 /// If no program ID is specified, the checksum is for the current program.
52 /// If a program ID is specified, the checksum is for an external program.
53 Checksum(Option<ProgramID<N>>),
54 /// The operand is the program edition.
55 /// If no program ID is specified, the edition is for the current program.
56 /// If a program ID is specified, the edition is for an external program.
57 Edition(Option<ProgramID<N>>),
58 /// The operand is the program owner.
59 /// If no program ID is specified, the owner is for the current program.
60 /// If a program ID is specified, the owner is for an external program.
61 ProgramOwner(Option<ProgramID<N>>),
62}
63
64impl<N: Network> From<Literal<N>> for Operand<N> {
65 /// Initializes a new operand from a literal.
66 #[inline]
67 fn from(literal: Literal<N>) -> Self {
68 Operand::Literal(literal)
69 }
70}
71
72impl<N: Network> From<&Literal<N>> for Operand<N> {
73 /// Initializes a new operand from a reference to a literal.
74 #[inline]
75 fn from(literal: &Literal<N>) -> Self {
76 Operand::Literal(literal.clone())
77 }
78}
79
80impl<N: Network> From<Register<N>> for Operand<N> {
81 /// Initializes a new operand from a register.
82 #[inline]
83 fn from(register: Register<N>) -> Self {
84 Operand::Register(register)
85 }
86}
87
88impl<N: Network> From<&Register<N>> for Operand<N> {
89 /// Initializes a new operand from a reference to a register.
90 #[inline]
91 fn from(register: &Register<N>) -> Self {
92 Operand::Register(register.clone())
93 }
94}
95
96impl<N: Network> Operand<N> {
97 /// Returns `true` if the operand contains a string type.
98 pub fn contains_string_type(&self) -> bool {
99 matches!(self, Self::Literal(Literal::String(_)))
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106 use console::network::MainnetV0;
107
108 type CurrentNetwork = MainnetV0;
109
110 #[test]
111 fn test_operand_from_literal() -> Result<()> {
112 let literal = Literal::from_str("1field")?;
113 let expected = Operand::<CurrentNetwork>::Literal(literal.clone());
114
115 let operand = Operand::<CurrentNetwork>::from(literal);
116 assert_eq!(expected, operand);
117 Ok(())
118 }
119
120 #[test]
121 fn test_operand_from_register() -> Result<()> {
122 let register = Register::from_str("r0")?;
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
130 #[test]
131 fn test_operand_from_register_member() -> Result<()> {
132 let register = Register::from_str("r0.owner")?;
133 let expected = Operand::<CurrentNetwork>::Register(register.clone());
134
135 let operand = Operand::<CurrentNetwork>::from(register);
136 assert_eq!(expected, operand);
137 Ok(())
138 }
139}