snarkvm_synthesizer_program/logic/command/
branch.rs1use crate::{Opcode, Operand};
17use console::{network::prelude::*, program::Identifier};
18
19pub type BranchEq<N> = Branch<N, { Variant::BranchEq as u8 }>;
21pub type BranchNeq<N> = Branch<N, { Variant::BranchNeq as u8 }>;
23
24enum Variant {
25 BranchEq,
26 BranchNeq,
27}
28
29#[derive(Clone, PartialEq, Eq, Hash)]
31pub struct Branch<N: Network, const VARIANT: u8> {
32 operands: [Operand<N>; 2],
34 position: Identifier<N>,
36}
37
38impl<N: Network, const VARIANT: u8> Branch<N, VARIANT> {
39 #[inline]
41 pub const fn opcode() -> Opcode {
42 match VARIANT {
43 0 => Opcode::Command("branch.eq"),
44 1 => Opcode::Command("branch.neq"),
45 _ => panic!("Invalid 'branch' instruction opcode"),
46 }
47 }
48
49 #[inline]
51 pub const fn operands(&self) -> &[Operand<N>] {
52 &self.operands
53 }
54
55 #[inline]
57 pub const fn first(&self) -> &Operand<N> {
58 &self.operands[0]
59 }
60
61 #[inline]
63 pub const fn second(&self) -> &Operand<N> {
64 &self.operands[1]
65 }
66
67 #[inline]
69 pub const fn position(&self) -> &Identifier<N> {
70 &self.position
71 }
72}
73
74impl<N: Network, const VARIANT: u8> Parser for Branch<N, VARIANT> {
75 #[inline]
77 fn parse(string: &str) -> ParserResult<Self> {
78 let (string, _) = Sanitizer::parse(string)?;
80 let (string, _) = tag(*Self::opcode())(string)?;
82 let (string, _) = Sanitizer::parse_whitespaces(string)?;
84
85 let (string, first) = Operand::parse(string)?;
87 let (string, _) = Sanitizer::parse_whitespaces(string)?;
89
90 let (string, second) = Operand::parse(string)?;
92 let (string, _) = Sanitizer::parse_whitespaces(string)?;
94
95 let (string, _) = tag("to")(string)?;
97 let (string, _) = Sanitizer::parse_whitespaces(string)?;
99 let (string, position) = Identifier::parse(string)?;
101
102 let (string, _) = Sanitizer::parse_whitespaces(string)?;
104 let (string, _) = tag(";")(string)?;
106
107 Ok((string, Self { operands: [first, second], position }))
108 }
109}
110
111impl<N: Network, const VARIANT: u8> FromStr for Branch<N, VARIANT> {
112 type Err = Error;
113
114 #[inline]
116 fn from_str(string: &str) -> Result<Self> {
117 match Self::parse(string) {
118 Ok((remainder, object)) => {
119 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
121 Ok(object)
123 }
124 Err(error) => bail!("Failed to parse string. {error}"),
125 }
126 }
127}
128
129impl<N: Network, const VARIANT: u8> Debug for Branch<N, VARIANT> {
130 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
132 Display::fmt(self, f)
133 }
134}
135
136impl<N: Network, const VARIANT: u8> Display for Branch<N, VARIANT> {
137 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
139 write!(f, "{} {} {} to {};", Self::opcode(), self.first(), self.second(), self.position)
141 }
142}
143
144impl<N: Network, const VARIANT: u8> FromBytes for Branch<N, VARIANT> {
145 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
147 let first = Operand::read_le(&mut reader)?;
149 let second = Operand::read_le(&mut reader)?;
151 let position = Identifier::read_le(&mut reader)?;
153
154 Ok(Self { operands: [first, second], position })
156 }
157}
158
159impl<N: Network, const VARIANT: u8> ToBytes for Branch<N, VARIANT> {
160 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
162 self.first().write_le(&mut writer)?;
164 self.second().write_le(&mut writer)?;
166 self.position.write_le(&mut writer)
168 }
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174 use console::{
175 network::MainnetV0,
176 program::{Identifier, Register},
177 };
178
179 type CurrentNetwork = MainnetV0;
180
181 #[test]
182 fn test_parse() {
183 let (string, branch) = BranchEq::<CurrentNetwork>::parse("branch.eq r0 r1 to exit;").unwrap();
184 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
185 assert_eq!(branch.first(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
186 assert_eq!(branch.second(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
187 assert_eq!(branch.position, Identifier::from_str("exit").unwrap(), "The position is incorrect");
188
189 let (string, branch) = BranchNeq::<CurrentNetwork>::parse("branch.neq r3 r4 to start;").unwrap();
190 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
191 assert_eq!(branch.first(), &Operand::Register(Register::Locator(3)), "The first operand is incorrect");
192 assert_eq!(branch.second(), &Operand::Register(Register::Locator(4)), "The second operand is incorrect");
193 assert_eq!(branch.position, Identifier::from_str("start").unwrap(), "The position is incorrect");
194 }
195}