1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Provides the [opcode_match!] macro.
use OpcodeGen;
use OpcodeMatches;
use TokenStream;
use parse_macro_input;
use crategenerate;
/// Generates a complex match statement
///
/// # Format
///
/// The basic format is like this:
///
/// ```rust
/// # use opcode_macros::opcode_match;
/// # let opcode = 0u8;
/// # mod namespace {
/// # pub const A_1: u8 = 0;
/// # pub const A_2: u8 = 0;
/// # pub const B_1: u8 = 0;
/// # pub const B_2: u8 = 0;
/// # }
/// # let result =
/// opcode_match! {
/// opcode as u8 in namespace,
/// [[A_1: a1, A_2: a2], [B_1: b1, B_2: b2]] => {
/// // Code
/// # 1
/// }
/// _ => {
/// // Code
/// # 0
/// }
/// }
/// # ;
/// # assert_eq!(result, 1);
/// ```
///
/// It generates something like this:
///
/// ```rust
/// # let opcode = 0u8;
/// # mod namespace {
/// # pub const A_1: u8 = 0;
/// # pub const A_2: u8 = 0;
/// # pub const B_1: u8 = 0;
/// # pub const B_2: u8 = 0;
/// # }
/// const A_1_B_1: u8 = namespace::A_1 | namespace::B_1;
/// const A_1_B_2: u8 = namespace::A_1 | namespace::B_2;
/// const A_2_B_1: u8 = namespace::A_2 | namespace::B_1;
/// const A_2_B_2: u8 = namespace::A_2 | namespace::B_2;
/// match opcode {
/// A_1_B_1 => { /* Code */ }
/// A_1_B_2 => { /* Code */ }
/// A_2_B_1 => { /* Code */ }
/// A_2_B_2 => { /* Code */ }
/// _ => { /* Code */ }
/// }
/// ```
///
/// ## Match Arm Headers
///
/// Match arm headers is something like `[[A_1: a1, A_2: a2], [B_1: b1, B_2: b2]]`.
///
/// For example, in eBPF opcodes, `BPF_ALU | BPF_K | BPF_ADD` is an opcode for
/// 32-bit addition with constants, while `BPF_ALU | BPF_K | BPF_SUB` is an opcode
/// for 32-bit subtraction with constants. To match against these opcodes,
/// we use the following code:
///
/// ```rust
/// # use opcode_macros::opcode_match;
/// # use ebpf_consts::*;
/// # let opcode = 0x04u8;
/// # let mut result = 0u64;
/// # let dst = 10u64;
/// # let imm = 10u64;
/// opcode_match! {
/// opcode as u8 in ebpf_consts,
/// [[BPF_ALU: _], [BPF_K: _],
/// [BPF_ADD: add, BPF_SUB: sub]] => {
/// result = dst.#"wrapping_{}"2(imm);
/// }
/// _ => {}
/// }
/// # assert_eq!(result, 20);
/// ```
///
/// We will talk about the templating rules later.
///
/// In the example above, you can also use some other variants:
/// - `[BPF_ADD: "add", BPF_SUB: "sub"]`
/// - `[BPF_ADD: [add], BPF_SUB: [sub]]`
/// - `[BPF_ADD: ["add"], BPF_SUB: ["sub"]]`
/// - `[BPF_ADD: ["add", "extra1"], BPF_SUB: ["sub", "extra2"]]`
///
/// If you want to substitutes parts of the code with symbols like "+",
/// you will need to quote the symbols like `[BPF_ADD: "+"]`.
///
/// ## Code Template
///
/// ### Substitution
///
/// This is not a real life example.
///
/// ```rust
/// # use opcode_macros::opcode_match;
/// # use ebpf_consts::*;
/// # use core::ops::Add;
/// # let opcode = 0u8;
/// opcode_match! {
/// opcode as u8 in ebpf_consts,
/// [
/// // Group 0
/// [BPF_K: ["const", 1, 2, "Constant Operation"]],
/// // Group 1
/// [BPF_ADD: ["add", 3, 4, "Addition Operation"]],
/// ] => {
/// // Use the first token in group 0 as a string
/// assert_eq!(#0, "const");
/// // Use the fourth token in group 0 as a string
/// assert_eq!(#:3:0, "Constant Operation");
/// assert_eq!(#:0:1, "add");
/// assert_eq!(#:3:1, "Addition Operation");
///
/// // Use raw tokens
/// assert_eq!(#:1:1, "3");
/// assert_eq!(#:1:=1, 3);
/// // 30.add(40) == 70, where #=1 is just #:0:=1
/// let value = 30isize;
/// assert_eq!(value.#=1(40), 70);
///
/// // With in-token substitution: add -> wrapping_add
/// assert_eq!(value.#"wrapping_{}"1(40), 70);
/// }
/// _ => panic!(),
/// }
/// ```
///
/// ### Conditional Blocks
///
/// ```rust
/// use opcode_macros::opcode_match;
/// use ebpf_consts::*;
///
/// # let opcode = BPF_X | BPF_ALU;
/// opcode_match! {
/// opcode as u8 in ebpf_consts,
/// [[BPF_X: x, BPF_K: k], [BPF_ALU: "alu", BPF_ALU64: "alu64"]] => {
/// #?((x))
/// println!("In V1 branch");
/// assert_eq!(#0, "x"); ##
///
/// #?((k))
/// println!("In V2 && {} branch", #1);
/// assert_eq!(#0, "k"); ##
///
/// #?((!"k"))
/// println!("In V2 && {} branch", #1);
/// assert_eq!(#0, "x"); ##
/// println!("Common");
/// }
/// _ => panic!(),
/// };
/// ```
///
/// The grammar is `#?((cond1, cond2)|(cond3|cond4)|...) CODE ##`,
/// making the code only injected if the opcode matches
/// `(cond1 && cond2) || (cond3 && cond4) || ...`.
/// A condition can be negated with an exclamation mark `!cond1`.
///
/// We don't allow nested conditions.
/// Generates opcode from bit field enums
///
/// # Example
///
/// ```rust
/// use opcode_macros::opcode_gen;
/// opcode_gen! {
/// for BPF_* in ebpf_consts as u8 {
/// [
/// [BPF_ALU, BPF_ALU64],
/// [BPF_K, BPF_X],
/// [BPF_ADD, BPF_SUB],
/// ]
/// [
/// [BPF_JMP, BPF_JMP32],
/// [BPF_K, BPF_X],
/// [BPF_JLE, BPF_JLT],
/// ]
/// }
/// }
/// # use ebpf_consts::*;
/// assert_eq!(BPF_ALU64_K_ADD, BPF_ALU64 | BPF_K | BPF_ADD);
/// assert_eq!(BPF_JMP_X_JLT, BPF_JMP | BPF_X | BPF_JLT);
/// ```