11use ckb_vm_definitions:: instructions:: { self as insts} ;
22use ckb_vm_definitions:: registers:: { RA , ZERO } ;
33
4+ use crate :: elf:: CFI ;
45use crate :: error:: OutOfBoundKind ;
56use crate :: instructions:: {
67 Instruction , InstructionFactory , Itype , R4type , R5type , Register , Rtype , Utype , a, b, cfi,
@@ -14,7 +15,7 @@ const RISCV_PAGESIZE_MASK: u64 = RISCV_PAGESIZE as u64 - 1;
1415const INSTRUCTION_CACHE_SIZE : usize = 4096 ;
1516
1617pub trait InstDecoder {
17- fn new < R : Register > ( isa : u8 , version : u32 ) -> Self ;
18+ fn new < R : Register > ( isa : u8 , version : u32 , cfi : CFI ) -> Self ;
1819 fn decode < M : Memory > ( & mut self , memory : & mut M , pc : u64 ) -> Result < Instruction , Error > ;
1920 fn reset_instructions_cache ( & mut self ) -> Result < ( ) , Error > ;
2021}
@@ -23,6 +24,7 @@ pub struct DefaultDecoder {
2324 factories : Vec < InstructionFactory > ,
2425 mop : bool ,
2526 version : u32 ,
27+ cfi : CFI ,
2628 // use a cache of instructions to avoid decoding the same instruction twice, pc is the key and the instruction is the value
2729 instructions_cache : [ ( u64 , u64 ) ; INSTRUCTION_CACHE_SIZE ] ,
2830}
@@ -34,6 +36,7 @@ impl DefaultDecoder {
3436 factories : vec ! [ ] ,
3537 mop,
3638 version,
39+ cfi : CFI :: default ( ) ,
3740 instructions_cache : [ ( u64:: MAX , 0 ) ; INSTRUCTION_CACHE_SIZE ] ,
3841 }
3942 }
@@ -118,7 +121,7 @@ impl DefaultDecoder {
118121 }
119122 let instruction_bits = self . decode_bits ( memory, pc) ?;
120123 for factory in & self . factories {
121- if let Some ( instruction) = factory ( instruction_bits, self . version ) {
124+ if let Some ( instruction) = factory ( instruction_bits, self . version , self . cfi ) {
122125 self . instructions_cache [ instruction_cache_key] = ( pc, instruction) ;
123126 return Ok ( instruction) ;
124127 }
@@ -859,8 +862,9 @@ impl DefaultDecoder {
859862}
860863
861864impl InstDecoder for DefaultDecoder {
862- fn new < R : Register > ( isa : u8 , version : u32 ) -> Self {
865+ fn new < R : Register > ( isa : u8 , version : u32 , cfi : CFI ) -> Self {
863866 let mut decoder = Self :: empty ( isa & ISA_MOP != 0 , version) ;
867+ decoder. cfi = cfi;
864868 if isa & ISA_CFI != 0 {
865869 decoder. add_instruction_factory ( cfi:: factory :: < R > ) ;
866870 }
0 commit comments