use crate::{wasm::ToLSBytes, Error, Function, Result};
impl Function {
pub fn _local_get(&mut self, local_index: u32) -> Result<()> {
let local_index = local_index as usize;
if (self.is_main || self.abi.is_some()) && local_index < self.ty.params().len() {
self._local_get_calldata(local_index)
} else {
self._local_get_var(local_index)
}
}
pub fn _local_set(&mut self, local_index: u32) -> Result<()> {
self.masm.push(&self.env.alloc(local_index))?;
self.masm._mstore()?;
Ok(())
}
pub fn _local_tee(&mut self, index: u32) -> Result<()> {
self.masm._dup1()?;
self._local_set(index)?;
Ok(())
}
pub fn _global_get(&mut self, _: u32) -> Result<()> {
todo!()
}
pub fn _global_set(&mut self, _: u32) -> Result<()> {
todo!()
}
fn _local_get_calldata(&mut self, local_index: usize) -> Result<()> {
let mut offset = self.locals.offset_of(local_index)?;
if self.abi.is_some() {
offset = (4 + local_index * 32).to_ls_bytes().to_vec().into();
}
self.masm.push(&offset)?;
self.masm._calldataload()?;
Ok(())
}
fn _local_get_var(&mut self, local_index: usize) -> Result<()> {
tracing::debug!("Local get variable: {local_index}");
if local_index + 1 > self.locals.len() {
return Err(Error::InvalidLocalIndex(local_index));
}
self.masm.push(&self.env.alloc(local_index as u32))?;
self.masm._mload()?;
Ok(())
}
}