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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
use ckb_vm_definitions::instructions::{self as insts};
use ckb_vm_definitions::registers::SP;
use super::i::nop;
use super::register::Register;
use super::utils::{jalr, ld, lw, rd, x, xs};
use super::{blank_instruction, set_instruction_length_2, Instruction, Itype, Rtype, Stype, Utype};
// Notice the location of rs2 in RVC encoding is different from full encoding
#[inline(always)]
fn c_rs2(instruction_bits: u32) -> usize {
x(instruction_bits, 2, 5, 0) as usize
}
// This function extract 3 bits from least_bit to form a register number,
// here since we are only using 3 bits, we can only reference the most popular
// used registers x8 - x15. In other words, a number of 0 extracted here means
// x8, 1 means x9, etc.
#[inline(always)]
fn compact_register_number(instruction_bits: u32, least_bit: usize) -> usize {
x(instruction_bits, least_bit, 3, 0) as usize + 8
}
// [12] => imm[5]
// [6:2] => imm[4:0]
fn immediate(instruction_bits: u32) -> i32 {
(x(instruction_bits, 2, 5, 0) | xs(instruction_bits, 12, 1, 5)) as i32
}
// [12] => imm[5]
// [6:2] => imm[4:0]
fn uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 2, 5, 0) | x(instruction_bits, 12, 1, 5)
}
// [12:2] => imm[11|4|9:8|10|6|7|3:1|5]
fn j_immediate(instruction_bits: u32) -> i32 {
(x(instruction_bits, 3, 3, 1)
| x(instruction_bits, 11, 1, 4)
| x(instruction_bits, 2, 1, 5)
| x(instruction_bits, 7, 1, 6)
| x(instruction_bits, 6, 1, 7)
| x(instruction_bits, 9, 2, 8)
| x(instruction_bits, 8, 1, 10)
| xs(instruction_bits, 12, 1, 11)) as i32
}
// [12:10] => uimm[5:3]
// [6:5] => uimm[7:6]
fn fld_uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 10, 3, 3) | x(instruction_bits, 5, 2, 6)
}
// [10:12] => uimm[5:3]
// [5:6] => uimm[2|6]
fn sw_uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 6, 1, 2) | x(instruction_bits, 10, 3, 3) | x(instruction_bits, 5, 1, 6)
}
// [12] => uimm[5]
// [6:2] => uimm[4:2|7:6]
fn lwsp_uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 4, 3, 2) | x(instruction_bits, 12, 1, 5) | x(instruction_bits, 2, 2, 6)
}
// [12] => uimm[5]
// [6:2] => uimm[4:3|8:6]
fn fldsp_uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 5, 2, 3) | x(instruction_bits, 12, 1, 5) | x(instruction_bits, 2, 3, 6)
}
// [12:7] => uimm[5:3|8:6]
fn fsdsp_uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 10, 3, 3) | x(instruction_bits, 7, 3, 6)
}
// [12:7] => uimm[5:2|7:6]
fn swsp_uimmediate(instruction_bits: u32) -> u32 {
x(instruction_bits, 9, 4, 2) | x(instruction_bits, 7, 2, 6)
}
// [12:10] => imm[8|4:3]
// [6:2] => imm[7:6|2:1|5]
fn b_immediate(instruction_bits: u32) -> i32 {
(x(instruction_bits, 3, 2, 1)
| x(instruction_bits, 10, 2, 3)
| x(instruction_bits, 2, 1, 5)
| x(instruction_bits, 5, 2, 6)
| xs(instruction_bits, 12, 1, 8)) as i32
}
#[allow(clippy::cognitive_complexity)]
pub fn factory<R: Register>(instruction_bits: u32, version: u32) -> Option<Instruction> {
let bit_length = R::BITS;
if bit_length != 32 && bit_length != 64 {
return None;
}
let rv32 = bit_length == 32;
let rv64 = bit_length == 64;
match instruction_bits & 0b_111_00000000000_11 {
// == Quadrant 0
0b_000_00000000000_00 => {
let nzuimm = x(instruction_bits, 6, 1, 2)
| x(instruction_bits, 5, 1, 3)
| x(instruction_bits, 11, 2, 4)
| x(instruction_bits, 7, 4, 6);
if nzuimm != 0 {
// C.ADDI4SPN
Some(
Itype::new_u(
insts::OP_ADDI,
compact_register_number(instruction_bits, 2),
SP,
nzuimm,
)
.0,
)
} else {
// Illegal instruction
None
}
}
0b_010_00000000000_00 => Some(
// C.LW
Itype::new_u(
lw(version),
compact_register_number(instruction_bits, 2),
compact_register_number(instruction_bits, 7),
sw_uimmediate(instruction_bits),
)
.0,
),
0b_011_00000000000_00 => {
if rv32 {
None
} else {
// C.LD
Some(
Itype::new_u(
ld(version),
compact_register_number(instruction_bits, 2),
compact_register_number(instruction_bits, 7),
fld_uimmediate(instruction_bits),
)
.0,
)
}
}
// Reserved
0b_100_00000000000_00 => None,
0b_110_00000000000_00 => Some(
// C.SW
Stype::new_u(
insts::OP_SW,
sw_uimmediate(instruction_bits),
compact_register_number(instruction_bits, 7),
compact_register_number(instruction_bits, 2),
)
.0,
),
0b_111_00000000000_00 => {
if rv32 {
None
} else {
// C.SD
Some(
Stype::new_u(
insts::OP_SD,
fld_uimmediate(instruction_bits),
compact_register_number(instruction_bits, 7),
compact_register_number(instruction_bits, 2),
)
.0,
)
}
}
// == Quadrant 1
0b_000_00000000000_01 => {
let nzimm = immediate(instruction_bits);
let rd = rd(instruction_bits);
if rd != 0 {
// C.ADDI
if nzimm != 0 {
Some(Itype::new_s(insts::OP_ADDI, rd, rd, nzimm).0)
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
} else {
// C.NOP
#[allow(clippy::if_same_then_else)]
if nzimm == 0 {
Some(nop())
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
}
}
0b_001_00000000000_01 => {
if rv32 {
// C.JAL
Some(Utype::new_s(insts::OP_JAL, 1, j_immediate(instruction_bits)).0)
} else {
let rd = rd(instruction_bits);
if rd != 0 {
// C.ADDIW
Some(Itype::new_s(insts::OP_ADDIW, rd, rd, immediate(instruction_bits)).0)
} else {
None
}
}
}
0b_010_00000000000_01 => {
let rd = rd(instruction_bits);
if rd != 0 {
// C.LI
Some(Itype::new_s(insts::OP_ADDI, rd, 0, immediate(instruction_bits)).0)
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
}
0b_011_00000000000_01 => {
let imm = immediate(instruction_bits) << 12;
if imm != 0 {
let rd = rd(instruction_bits);
if rd == SP {
// C.ADDI16SP
Some(
Itype::new_s(
insts::OP_ADDI,
SP,
SP,
(x(instruction_bits, 6, 1, 4)
| x(instruction_bits, 2, 1, 5)
| x(instruction_bits, 5, 1, 6)
| x(instruction_bits, 3, 2, 7)
| xs(instruction_bits, 12, 1, 9))
as i32,
)
.0,
)
} else {
// C.LUI
if rd != 0 {
Some(Utype::new_s(insts::OP_LUI, rd, imm).0)
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
}
} else {
None
}
}
0b_100_00000000000_01 => {
let rd = compact_register_number(instruction_bits, 7);
match instruction_bits & 0b_1_11_000_11000_00 {
// C.SRLI64
0b_0_00_000_00000_00 if instruction_bits & 0b_111_00 == 0 => Some(nop()),
// C.SRAI64
0b_0_01_000_00000_00 if instruction_bits & 0b_111_00 == 0 => Some(nop()),
// C.SUB
0b_0_11_000_00000_00 => Some(
Rtype::new(
insts::OP_SUB,
rd,
rd,
compact_register_number(instruction_bits, 2),
)
.0,
),
// C.XOR
0b_0_11_000_01000_00 => Some(
Rtype::new(
insts::OP_XOR,
rd,
rd,
compact_register_number(instruction_bits, 2),
)
.0,
),
// C.OR
0b_0_11_000_10000_00 => Some(
Rtype::new(
insts::OP_OR,
rd,
rd,
compact_register_number(instruction_bits, 2),
)
.0,
),
// C.AND
0b_0_11_000_11000_00 => Some(
Rtype::new(
insts::OP_AND,
rd,
rd,
compact_register_number(instruction_bits, 2),
)
.0,
),
// C.SUBW
0b_1_11_000_00000_00 if rv64 => Some(
Rtype::new(
insts::OP_SUBW,
rd,
rd,
compact_register_number(instruction_bits, 2),
)
.0,
),
// C.ADDW
0b_1_11_000_01000_00 if rv64 => Some(
Rtype::new(
insts::OP_ADDW,
rd,
rd,
compact_register_number(instruction_bits, 2),
)
.0,
),
// Reserved
0b_1_11_000_10000_00 => None,
// Reserved
0b_1_11_000_11000_00 => None,
_ => {
let uimm = uimmediate(instruction_bits);
match (instruction_bits & 0b_11_000_00000_00, uimm) {
// Invalid instruction
(0b_00_000_00000_00, 0) => None,
// C.SRLI
(0b_00_000_00000_00, uimm) => Some(
Itype::new_u(insts::OP_SRLI, rd, rd, uimm & u32::from(R::SHIFT_MASK)).0,
),
// Invalid instruction
(0b_01_000_00000_00, 0) => None,
// C.SRAI
(0b_01_000_00000_00, uimm) => Some(
Itype::new_u(insts::OP_SRAI, rd, rd, uimm & u32::from(R::SHIFT_MASK)).0,
),
// C.ANDI
(0b_10_000_00000_00, _) => Some(
Itype::new_s(insts::OP_ANDI, rd, rd, immediate(instruction_bits)).0,
),
_ => None,
}
}
}
}
// C.J
0b_101_00000000000_01 => {
Some(Utype::new_s(insts::OP_JAL, 0, j_immediate(instruction_bits)).0)
}
// C.BEQZ
0b_110_00000000000_01 => Some(
Stype::new_s(
insts::OP_BEQ,
b_immediate(instruction_bits),
compact_register_number(instruction_bits, 7),
0,
)
.0,
),
// C.BNEZ
0b_111_00000000000_01 => Some(
Stype::new_s(
insts::OP_BNE,
b_immediate(instruction_bits),
compact_register_number(instruction_bits, 7),
0,
)
.0,
),
// == Quadrant 2
0b_000_00000000000_10 => {
let uimm = uimmediate(instruction_bits);
let rd = rd(instruction_bits);
if rd != 0 && uimm != 0 {
// C.SLLI
Some(Itype::new_u(insts::OP_SLLI, rd, rd, uimm & u32::from(R::SHIFT_MASK)).0)
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
}
0b_010_00000000000_10 => {
let rd = rd(instruction_bits);
if rd != 0 {
// C.LWSP
Some(Itype::new_u(lw(version), rd, SP, lwsp_uimmediate(instruction_bits)).0)
} else {
// Reserved
None
}
}
0b_011_00000000000_10 => {
if rv32 {
None
} else {
let rd = rd(instruction_bits);
if rd != 0 {
// C.LDSP
Some(Itype::new_u(ld(version), rd, SP, fldsp_uimmediate(instruction_bits)).0)
} else {
// Reserved
None
}
}
}
0b_100_00000000000_10 => {
match instruction_bits & 0b_1_00000_00000_00 {
0b_0_00000_00000_00 => {
let rd = rd(instruction_bits);
let rs2 = c_rs2(instruction_bits);
if rs2 == 0 {
if rd != 0 {
// C.JR
Some(Itype::new_s(jalr(version), 0, rd, 0).0)
} else {
// Reserved
None
}
} else {
if rd != 0 {
// C.MV
Some(Rtype::new(insts::OP_ADD, rd, 0, rs2).0)
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
}
}
0b_1_00000_00000_00 => {
let rd = rd(instruction_bits);
let rs2 = c_rs2(instruction_bits);
match (rd, rs2) {
// C.EBREAK
(0, 0) => Some(blank_instruction(insts::OP_EBREAK)),
// C.JALR
(rs1, 0) => Some(Itype::new_s(jalr(version), 1, rs1, 0).0),
// C.ADD
(rd, rs2) => {
if rd != 0 {
Some(Rtype::new(insts::OP_ADD, rd, rd, rs2).0)
} else if version >= 1 {
// HINTs
Some(nop())
} else {
None
}
}
}
}
_ => unreachable!(),
}
}
0b_110_00000000000_10 => Some(
// C.SWSP
Stype::new_u(
insts::OP_SW,
swsp_uimmediate(instruction_bits),
SP,
c_rs2(instruction_bits),
)
.0,
),
0b_111_00000000000_10 => {
if rv32 {
None
} else {
// C.SDSP
Some(
Stype::new_u(
insts::OP_SD,
fsdsp_uimmediate(instruction_bits),
2,
c_rs2(instruction_bits),
)
.0,
)
}
}
_ => None,
}
.map(set_instruction_length_2)
}