1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
5pub enum Intrinsic {
6 IsReferenceType,
7 SizeOfType,
8 SizeOfVal,
9 SizeOfStr,
10 IsStrArray,
11 AssertIsStrArray,
12 ToStrArray,
13 Eq,
14 Gt,
15 Lt,
16 Gtf,
17 AddrOf,
18 StateClear,
19 StateLoadWord,
20 StateStoreWord,
21 StateLoadQuad,
22 StateStoreQuad,
23 Log,
24 Add,
25 Sub,
26 Mul,
27 Div,
28 And,
29 Or,
30 Xor,
31 Lsh,
32 Rsh,
33 Mod,
34 Revert,
35 PtrAdd,
36 PtrSub,
37 Smo,
38 Not,
39 JmpMem,
40 ContractCall, ContractRet, EncodeBufferEmpty, EncodeBufferAppend, EncodeBufferAsRawSlice, Slice, ElemAt, Transmute, }
49
50impl fmt::Display for Intrinsic {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 let s = match self {
53 Intrinsic::IsReferenceType => "is_reference_type",
54 Intrinsic::IsStrArray => "is_str_type",
55 Intrinsic::SizeOfType => "size_of",
56 Intrinsic::SizeOfVal => "size_of_val",
57 Intrinsic::SizeOfStr => "size_of_str_array",
58 Intrinsic::AssertIsStrArray => "assert_is_str_array",
59 Intrinsic::ToStrArray => "to_str_array",
60 Intrinsic::Eq => "eq",
61 Intrinsic::Gt => "gt",
62 Intrinsic::Lt => "lt",
63 Intrinsic::Gtf => "gtf",
64 Intrinsic::AddrOf => "addr_of",
65 Intrinsic::StateClear => "state_clear",
66 Intrinsic::StateLoadWord => "state_load_word",
67 Intrinsic::StateStoreWord => "state_store_word",
68 Intrinsic::StateLoadQuad => "state_load_quad",
69 Intrinsic::StateStoreQuad => "state_store_quad",
70 Intrinsic::Log => "log",
71 Intrinsic::Add => "add",
72 Intrinsic::Sub => "sub",
73 Intrinsic::Mul => "mul",
74 Intrinsic::Div => "div",
75 Intrinsic::And => "and",
76 Intrinsic::Or => "or",
77 Intrinsic::Xor => "xor",
78 Intrinsic::Lsh => "lsh",
79 Intrinsic::Rsh => "rsh",
80 Intrinsic::Mod => "mod",
81 Intrinsic::Revert => "revert",
82 Intrinsic::PtrAdd => "ptr_add",
83 Intrinsic::PtrSub => "ptr_sub",
84 Intrinsic::Smo => "smo",
85 Intrinsic::Not => "not",
86 Intrinsic::JmpMem => "jmp_mem",
87 Intrinsic::ContractCall => "contract_call",
88 Intrinsic::ContractRet => "contract_ret",
89 Intrinsic::EncodeBufferEmpty => "encode_buffer_empty",
90 Intrinsic::EncodeBufferAppend => "encode_buffer_append",
91 Intrinsic::EncodeBufferAsRawSlice => "encode_buffer_as_raw_slice",
92 Intrinsic::Slice => "slice",
93 Intrinsic::ElemAt => "elem_at",
94 Intrinsic::Transmute => "transmute",
95 };
96 write!(f, "{s}")
97 }
98}
99
100impl Intrinsic {
101 pub fn try_from_str(raw: &str) -> Option<Intrinsic> {
102 use Intrinsic::*;
103 Some(match raw {
104 "__is_reference_type" => IsReferenceType,
105 "__is_str_array" => IsStrArray,
106 "__size_of" => SizeOfType,
107 "__size_of_val" => SizeOfVal,
108 "__size_of_str_array" => SizeOfStr,
109 "__assert_is_str_array" => AssertIsStrArray,
110 "__to_str_array" => ToStrArray,
111 "__eq" => Eq,
112 "__gt" => Gt,
113 "__lt" => Lt,
114 "__gtf" => Gtf,
115 "__addr_of" => AddrOf,
116 "__state_clear" => StateClear,
117 "__state_load_word" => StateLoadWord,
118 "__state_store_word" => StateStoreWord,
119 "__state_load_quad" => StateLoadQuad,
120 "__state_store_quad" => StateStoreQuad,
121 "__log" => Log,
122 "__add" => Add,
123 "__sub" => Sub,
124 "__mul" => Mul,
125 "__div" => Div,
126 "__and" => And,
127 "__or" => Or,
128 "__xor" => Xor,
129 "__lsh" => Lsh,
130 "__rsh" => Rsh,
131 "__mod" => Mod,
132 "__revert" => Revert,
133 "__ptr_add" => PtrAdd,
134 "__ptr_sub" => PtrSub,
135 "__smo" => Smo,
136 "__not" => Not,
137 "__jmp_mem" => JmpMem,
138 "__contract_call" => ContractCall,
139 "__contract_ret" => ContractRet,
140 "__encode_buffer_empty" => EncodeBufferEmpty,
141 "__encode_buffer_append" => EncodeBufferAppend,
142 "__encode_buffer_as_raw_slice" => EncodeBufferAsRawSlice,
143 "__slice" => Slice,
144 "__elem_at" => ElemAt,
145 "__transmute" => Transmute,
146 _ => return None,
147 })
148 }
149}