Skip to main content

qcode/value/insn/
assert.rs

1use crate::value::LocalValueId;
2
3use super::mnemonic::{Args, MnemonicKind};
4use smallvec::smallvec;
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
7pub struct Assert {
8    pub condition: LocalValueId,
9}
10
11impl MnemonicKind for Assert {
12    fn opcode(&self) -> &'static str {
13        "assert"
14    }
15
16    fn args(&self) -> Args {
17        smallvec![self.condition]
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use wazabin_qcode_macro::qcode;
24
25    use crate::{context::Context, value::insn::Mnemonic};
26
27    #[test]
28    fn test_assert_macro_and_display() {
29        let mut ctx = Context::new();
30
31        qcode!(
32            ctx,
33            "
34            <block>
35                local i8 COND;
36                %cond = load(COND:1, COND);
37                assert %cond;
38                goto <0x1001>;
39            "
40        );
41
42        let block = crate::value::BasicBlock::from_id(&ctx, block);
43        let assert_insn = block
44            .iter()
45            .find(|i| matches!(i.mnemonic(), Mnemonic::Assert(_)))
46            .expect("assert instruction should be present");
47
48        assert_eq!(assert_insn.size(), 0);
49        assert_eq!(assert_insn.as_statement().to_string(), "assert i8 %cond;");
50    }
51}