-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.rs
378 lines (334 loc) · 9.96 KB
/
decode.rs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use crate::processor::exceptions::IllegalInstructionException::UnknownOpcode;
use anyhow::Result;
use std::convert::TryInto;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Opcode {
Load,
Store,
LoadFP,
StoreFP,
OpImm,
Op,
OpImm32,
Op32,
AddUpperImmPC,
LoadUpperImm,
JumpAndLink,
JumpAndLinkRegister,
Branch,
Vector,
System,
MiscMem,
Custom2CHERI,
}
impl TryInto<Opcode> for u8 {
type Error = anyhow::Error;
fn try_into(self) -> Result<Opcode> {
Ok(match self {
0b00_000_11 => Opcode::Load,
0b01_000_11 => Opcode::Store,
0b00_001_11 => Opcode::LoadFP,
0b01_001_11 => Opcode::StoreFP,
0b00_011_11 => Opcode::MiscMem,
0b00_100_11 => Opcode::OpImm,
0b01_100_11 => Opcode::Op,
0b00_110_11 => Opcode::OpImm32,
0b01_110_11 => Opcode::Op32,
0b00_101_11 => Opcode::AddUpperImmPC,
0b01_101_11 => Opcode::LoadUpperImm,
0b11_001_11 => Opcode::JumpAndLinkRegister,
0b11_011_11 => Opcode::JumpAndLink,
0b11_000_11 => Opcode::Branch,
0b10_101_11 => Opcode::Vector,
0b11_100_11 => Opcode::System,
0b10_110_11 => Opcode::Custom2CHERI,
_ => bail!(UnknownOpcode(self)),
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct Imm {
// Source bits used to generate the immediate
data: u32,
// The bit width of the immediate
width: u32,
}
impl Imm {
/// Generates an immediate from data + width
pub fn new(data: u32, width: u32) -> Imm {
assert!(width <= 32);
// Check if provided data was too-wide
assert_eq!(bits!(data, 0:(width as usize - 1)), data);
Imm { data, width }
}
/// Return the immediate data as a u32
///
/// ```
/// use rsim::processor::decode::Imm;
/// let i = Imm::new(0xF0FF, 16);
/// assert_eq!(i.no_extend_u32(), 0xF0FF);
/// ```
#[inline]
pub fn no_extend_u32(&self) -> u32 {
self.data
}
/// Return the immediate data as a u64
///
/// ```
/// use rsim::processor::decode::Imm;
/// let i = Imm::new(0xF0FF, 16);
/// assert_eq!(i.no_extend_u64(), 0xF0FF);
/// ```
#[inline]
pub fn no_extend_u64(&self) -> u64 {
self.data as u64
}
/// Sign-extend the immediate value and return as a u32
///
/// ```
/// use rsim::processor::decode::Imm;
/// let i = Imm::new(0xF0FF, 16);
/// assert_eq!(i.sign_extend_u32(), 0xFFFF_F0FF);
/// assert_eq!(Imm::new(0x00FF_0000, 32).sign_extend_u32(), 0x00FF_0000);
/// ```
#[inline]
pub fn sign_extend_u32(&self) -> u32 {
// Chop the top bits off a full sign-extended u64
self.sign_extend_i32() as u32
}
/// Sign-extend the immediate value and return as a u64
///
/// ```
/// use rsim::processor::decode::Imm;
/// let i = Imm::new(0xF0FF, 16);
/// assert_eq!(i.sign_extend_u64(), 0xFFFF_FFFF_FFFF_F0FF);
/// ```
#[inline]
pub fn sign_extend_u64(&self) -> u64 {
self.sign_extend_i64() as u64
}
/// Sign-extend the immediate value and return as a i32
///
/// ```
/// use rsim::processor::decode::Imm;
/// let i = Imm::new(0xFFFF, 16);
/// assert_eq!(i.sign_extend_i32(), -1);
/// ```
#[inline]
pub fn sign_extend_i32(&self) -> i32 {
let sign = self.data & (1 << (self.width - 1));
if self.width == 32 || sign == 0 {
self.data as i32
} else {
(-1i32 << self.width) | self.data as i32
}
}
/// Sign-extend the immediate value and return as a i64
///
/// ```
/// use rsim::processor::decode::Imm;
/// let i = Imm::new(0xFFFF, 16);
/// assert_eq!(i.sign_extend_i64(), -1);
/// ```
#[inline]
pub fn sign_extend_i64(&self) -> i64 {
self.sign_extend_i32() as i64
}
}
/// TODO - Right now this does sign extension up to 32-bits.
/// These should really all be 64-bit, now that we could be decoding 32 or 64-bit instructions.
/// TODO - Make each of these a separate struct? Then we can combine variants in enums, e.g. type ROrIType = (RType, IType).
#[derive(Debug, Clone, Copy)]
pub enum InstructionBits {
RType {
rd: u8,
funct3: u8,
rs1: u8,
rs2: u8,
funct7: u8,
},
IType {
rd: u8,
funct3: u8,
rs1: u8,
imm: Imm,
},
ROrIType {
rd: u8,
funct3: u8,
rs1: u8,
// R-Type only
rs2: u8,
funct7: u8,
// I-Type only
imm: Imm,
},
SType {
funct3: u8,
rs1: u8,
rs2: u8,
imm: Imm,
},
UType {
rd: u8,
imm: Imm,
},
JType {
rd: u8,
imm: Imm,
},
BType {
funct3: u8,
rs1: u8,
rs2: u8,
imm: Imm,
},
VType {
funct3: u8,
rs1: u8,
rs2: u8,
rd: u8,
// Only for use in arithmetic
funct6: u8,
vm: bool,
// Only for use in configuration
zimm11: u16,
zimm10: u16,
},
FLdStType {
rd: u8,
width: u8,
rs1: u8,
rs2: u8,
// Float only
funct7: u8,
// Vector only
vm: bool,
mew: bool,
mop: u8,
nf: u8,
},
}
impl InstructionBits {
pub fn get_opcode(inst: u32) -> Result<Opcode> {
(bits!(inst, 0:6) as u8).try_into()
}
pub fn from_r(inst: u32) -> InstructionBits {
InstructionBits::RType {
rd: (bits!(inst, 7:11) as u8),
funct3: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
rs2: (bits!(inst, 20:24) as u8),
funct7: (bits!(inst, 25:31) as u8),
}
}
pub fn from_i(inst: u32) -> InstructionBits {
InstructionBits::IType {
rd: (bits!(inst, 7:11) as u8),
funct3: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
imm: Imm::new(bits!(inst, 20:31), 12),
}
}
pub fn from_r_or_i(inst: u32) -> InstructionBits {
InstructionBits::ROrIType {
rd: (bits!(inst, 7:11) as u8),
funct3: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
rs2: (bits!(inst, 20:24) as u8),
funct7: (bits!(inst, 25:31) as u8),
imm: Imm::new(bits!(inst, 20:31), 12),
}
}
pub fn from_s(inst: u32) -> InstructionBits {
let imm_bits: u32 = (bits!(inst, 7:11) as u32) | ((bits!(inst, 25:31) as u32) << 5);
InstructionBits::SType {
funct3: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
rs2: (bits!(inst, 20:24) as u8),
imm: Imm::new(imm_bits, 12),
}
}
pub fn from_u(inst: u32) -> InstructionBits {
let imm_bits = bits!(inst, 12:31);
InstructionBits::UType {
rd: (bits!(inst, 7:11) as u8),
imm: Imm::new(imm_bits << 12, 32),
}
}
pub fn from_j(inst: u32) -> InstructionBits {
let imm = (bits!(inst, 21:30) << 1)
| (bits!(inst, 20:20) << 11)
| (bits!(inst, 12:19) << 12)
| (bits!(inst, 31:31) << 20);
InstructionBits::JType {
rd: (bits!(inst, 7:11) as u8),
imm: Imm::new(imm, 21),
}
}
pub fn from_b(inst: u32) -> InstructionBits {
let imm = (bits!(inst, 8:11) << 1)
| (bits!(inst, 25:30) << 5)
| (bits!(inst, 7:7) << 11)
| (bits!(inst, 31:31) << 12);
InstructionBits::BType {
funct3: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
rs2: (bits!(inst, 20:24) as u8),
imm: Imm::new(imm, 13),
}
}
pub fn from_v(inst: u32) -> InstructionBits {
InstructionBits::VType {
rd: (bits!(inst, 7:11) as u8),
funct3: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
rs2: (bits!(inst, 20:24) as u8),
// arithmetic
funct6: (bits!(inst, 26:31) as u8),
vm: bits!(inst, 25:25) == 1,
// configuration
zimm11: bits!(inst, 20:30) as u16,
zimm10: bits!(inst, 20:29) as u16,
}
}
pub fn from_f_ld_st(inst: u32) -> InstructionBits {
InstructionBits::FLdStType {
rd: (bits!(inst, 7:11) as u8),
width: (bits!(inst, 12:14) as u8),
rs1: (bits!(inst, 15:19) as u8),
rs2: (bits!(inst, 20:24) as u8),
// Float only
funct7: (bits!(inst, 25:31) as u8),
// Vector only
vm: bits!(inst, 25:25) == 1,
mop: bits!(inst, 26:27) as u8,
mew: bits!(inst, 28:28) == 1,
nf: bits!(inst, 29:31) as u8,
}
}
}
pub fn decode(inst: u32) -> Result<(Opcode, InstructionBits)> {
let opcode = InstructionBits::get_opcode(inst)?;
use Opcode::*;
let instr = match opcode {
Load => InstructionBits::from_i(inst),
Store => InstructionBits::from_s(inst),
LoadFP => InstructionBits::from_f_ld_st(inst),
StoreFP => InstructionBits::from_f_ld_st(inst),
MiscMem => InstructionBits::from_i(inst),
OpImm => InstructionBits::from_i(inst),
Op => InstructionBits::from_r(inst),
OpImm32 => InstructionBits::from_i(inst),
Op32 => InstructionBits::from_r(inst),
AddUpperImmPC => InstructionBits::from_u(inst),
LoadUpperImm => InstructionBits::from_u(inst),
JumpAndLink => InstructionBits::from_j(inst),
JumpAndLinkRegister => InstructionBits::from_i(inst),
Branch => InstructionBits::from_b(inst),
Vector => InstructionBits::from_v(inst),
System => InstructionBits::from_i(inst),
Custom2CHERI => InstructionBits::from_r_or_i(inst),
};
Ok((opcode, instr))
}