diff --git a/codegen/src/jump/pc.rs b/codegen/src/jump/pc.rs index b86a22715..d164befec 100644 --- a/codegen/src/jump/pc.rs +++ b/codegen/src/jump/pc.rs @@ -1,7 +1,7 @@ //! Program counter handlers. use crate::{ - jump::{relocate, Jump, JumpTable}, + jump::{Jump, JumpTable}, Error, Result, BUFFER_LIMIT, }; @@ -10,8 +10,8 @@ impl JumpTable { pub fn shift_pc(&mut self, start: u16, offset: u16) -> Result<()> { tracing::trace!("shift pc from 0x{start:x} with offset={offset}"); self.shift_label_pc(start, offset)?; - self.shift_label_target(start, 0, offset)?; - self.shift_func_target(start, 0, offset)?; + self.shift_label_target(start, offset)?; + self.shift_func_target(start, offset)?; Ok(()) } @@ -35,9 +35,8 @@ impl JumpTable { total_offset }, )?; - // let offset = self.target_offset(&target, total_offset)?; - self.shift_target(pc, total_offset, offset)?; + self.shift_target(pc, offset)?; total_offset += offset; Ok(()) }) @@ -48,10 +47,10 @@ impl JumpTable { /// 1. shift code section. /// 2. shift label targets. /// 3. shift function targets. - pub fn shift_target(&mut self, ptr: u16, total_offset: u16, target_offset: u16) -> Result<()> { - self.code.shift(target_offset); - self.shift_label_target(ptr, total_offset, target_offset)?; - self.shift_func_target(ptr, total_offset, target_offset) + pub fn shift_target(&mut self, ptr: u16, offset: u16) -> Result<()> { + self.code.shift(offset); + self.shift_label_target(ptr, offset)?; + self.shift_func_target(ptr, offset) } /// Shift program counter for labels. @@ -82,7 +81,7 @@ impl JumpTable { } /// Shift program counter for functions. - pub fn shift_func_target(&mut self, ptr: u16, total_offset: u16, offset: u16) -> Result<()> { + pub fn shift_func_target(&mut self, ptr: u16, offset: u16) -> Result<()> { if self.func.is_empty() { tracing::trace!("No functions to shift."); return Ok(()); @@ -93,14 +92,14 @@ impl JumpTable { if *target > ptr { tracing::trace!( - "[{total_offset}] shift Func({index}) target with offset={offset}: 0x{target:x}(0x{ptr:x}) -> 0x{:x}", + "shift Func({index}) target with offset={offset}: 0x{target:x}(0x{ptr:x}) -> 0x{:x}", next_target ); *target = next_target; } else { tracing::trace!( - "[{total_offset}] shift Func({index}) target with offset=0: 0x{target:x}(0x{ptr:x}) -> 0x{target:x}" + "shift Func({index}) target with offset=0: 0x{target:x}(0x{ptr:x}) -> 0x{target:x}" ); } @@ -109,7 +108,7 @@ impl JumpTable { } /// Shift target program counter for labels. - pub fn shift_label_target(&mut self, ptr: u16, total_offset: u16, offset: u16) -> Result<()> { + pub fn shift_label_target(&mut self, ptr: u16, offset: u16) -> Result<()> { if self.jump.is_empty() { tracing::trace!("No labels to shift."); return Ok(()); @@ -121,14 +120,14 @@ impl JumpTable { if *label > ptr { tracing::trace!( - "[{total_offset}] shift Label(0x{pc:x}) target with offset={offset}: 0x{label:x}(0x{ptr:x}) -> 0x{:x}", + "shift Label(0x{pc:x}) target with offset={offset}: 0x{label:x}(0x{ptr:x}) -> 0x{:x}", next_label, ); *label = next_label; } else { tracing::trace!( - "[{total_offset}] shift Label(0x{pc:x}) target with offset=0: 0x{label:x}(0x{ptr:x}) -> 0x{label:x}" + "shift Label(0x{pc:x}) target with offset=0: 0x{label:x}(0x{ptr:x}) -> 0x{label:x}" ); } } diff --git a/codegen/src/jump/relocate.rs b/codegen/src/jump/relocate.rs index 4ed06fc42..f232add2f 100644 --- a/codegen/src/jump/relocate.rs +++ b/codegen/src/jump/relocate.rs @@ -37,35 +37,35 @@ impl JumpTable { } } -/// Get the offset of the program counter for relocation. -pub fn offset(pc: u16) -> Result { - let mut offset = 0; - - // Update the target program counter - { - // The maximum size of the PC is 2 bytes, whatever PUSH1 or PUSH32 - // takes 1 more byte. - offset += 1; - - // Update the program counter for the edge cases. - // - // Start from 0xff, the lowest significant bytes of the target - // program counter will take 2 bytes instead of 1 byte. - // - // | PC | PC BYTES | TARGET PC | - // |------|----------|-----------| - // | 0xfe | 1 | 0xff | - // | 0xff | 2 | 0x0101 | - offset += if pc > 0xfe { 2 } else { 1 } - } - - // Check PC range. - if pc + offset > BUFFER_LIMIT as u16 { - return Err(Error::InvalidPC((pc + offset) as usize)); - } - - Ok(offset) -} +// /// Get the offset of the program counter for relocation. +// pub fn offset(pc: u16) -> Result { +// let mut offset = 0; +// +// // Update the target program counter +// { +// // The maximum size of the PC is 2 bytes, whatever PUSH1 or PUSH32 +// // takes 1 more byte. +// offset += 1; +// +// // Update the program counter for the edge cases. +// // +// // Start from 0xff, the lowest significant bytes of the target +// // program counter will take 2 bytes instead of 1 byte. +// // +// // | PC | PC BYTES | TARGET PC | +// // |------|----------|-----------| +// // | 0xfe | 1 | 0xff | +// // | 0xff | 2 | 0x0101 | +// offset += if pc > 0xfe { 2 } else { 1 } +// } +// +// // Check PC range. +// if pc + offset > BUFFER_LIMIT as u16 { +// return Err(Error::InvalidPC((pc + offset) as usize)); +// } +// +// Ok(offset) +// } /// Relocate program counter to buffer. fn pc(buffer: &mut Buffer, original_pc: u16, target_pc: u16) -> Result { diff --git a/codegen/src/visitor/call.rs b/codegen/src/visitor/call.rs index b50b5f9e4..3d0a9ef8a 100644 --- a/codegen/src/visitor/call.rs +++ b/codegen/src/visitor/call.rs @@ -42,7 +42,7 @@ impl Function { .into()); } - tracing::trace!("call internal function: index={index}"); + tracing::debug!("call internal function: index={index}"); let (params, results) = self.env.funcs.get(&index).unwrap_or(&(0, 0)); // TODO: adapat the case that the params is larger than 0xff (#247) @@ -53,7 +53,7 @@ impl Function { // [ .., // , // params[SWAP], params[PUSH, SLOT, MSTORE], - // PUSH, PC, JUMP, + // {(PUSH, PC), JUMP, JUMPDEST} // ] // <- selfparams[PUSH, OFFSET, CALLDATALOAD] // diff --git a/compiler/src/parser.rs b/compiler/src/parser.rs index 7e4c05682..a8a2418a7 100644 --- a/compiler/src/parser.rs +++ b/compiler/src/parser.rs @@ -47,7 +47,8 @@ impl<'p> Parser<'p> { let locals = fun.body.get_locals_reader()?.get_count(); let params = sig.params().len(); tracing::trace!( - "computing slots for function {idx}, locals: {locals}, params: {params}, reserved: {slots}" + "computing slots for function {idx}, locals: {locals}, params: {params}, reserved: {slots}, external: {}", + self.env.is_external(fun.index()) ); self.env.slots.insert(fun.index(), slots); @@ -56,6 +57,8 @@ impl<'p> Parser<'p> { .insert(fun.index(), (params as u32, sig.results().len() as u32)); slots += locals; + + // process prarams for internal functions only if !self.env.is_external(fun.index()) && !self.env.is_main(fun.index()) { slots += params as u32; }