Skip to content

Commit

Permalink
Fix bug in size of type data
Browse files Browse the repository at this point in the history
  • Loading branch information
k0aki committed Sep 30, 2023
1 parent 871354d commit eb561de
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions crates/interpreter/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Frame {

let addr = self.alloca_region.len();

let size = types::byte_size_of_ty(ctx, ty);
let size = types::size_of_ty_data(ctx, ty);
self.alloca_region.resize(addr + size, 0);
self.local_values[v] = EvalValue::from_usize(addr);
}
Expand All @@ -60,7 +60,7 @@ impl Frame {
let addr = addr.to_u256().as_usize();
debug_assert!(addr < self.alloca_region.len());

let size = types::byte_size_of_ty(ctx, ty);
let size = types::size_of_ty_data(ctx, ty);
let literal_b = &self.alloca_region[addr..addr + size];
let Some(data) = EvalValue::deserialize(ctx, ty, literal_b) else {
return;
Expand All @@ -70,7 +70,7 @@ impl Frame {

pub fn str(&mut self, ctx: &ModuleCtx, addr: I256, data: I256, ty: Type) {
let addr = addr.to_u256().as_usize();
let size = types::byte_size_of_ty(ctx, ty);
let size = types::size_of_ty_data(ctx, ty);
let reg_value = EvalValue::from_i256(data);
reg_value.serialize(ctx, ty, &mut self.alloca_region[addr..size]);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/interpreter/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ use sonatina_ir::{
Type, I256,
};

pub fn byte_size_of_ty(ctx: &ModuleCtx, ty: Type) -> usize {
pub fn size_of_ty_data(ctx: &ModuleCtx, ty: Type) -> usize {
match ty {
Type::I1 => mem::size_of::<bool>(),
Type::I8 => mem::size_of::<i8>(),
Type::I16 => mem::size_of::<i16>(),
Type::I32 => mem::size_of::<i32>(),
Type::I64 => mem::size_of::<i64>(),
Type::I128 => mem::size_of::<i128>(),
Type::I256 => mem::size_of::<I256>(),
Type::I256 => 32,
Type::Compound(cmpd_ty) => {
use CompoundTypeData::*;
let cmpd_ty_data = ctx.with_ty_store(|s| s.resolve_compound(cmpd_ty).clone());
match cmpd_ty_data {
Array { len, elem } => len * byte_size_of_ty(ctx, elem),
Array { len, elem } => len * size_of_ty_data(ctx, elem),
Ptr(_) => mem::size_of::<usize>(),
Struct(data) => data.fields.iter().fold(0usize, |acc, field_ty| {
acc + byte_size_of_ty(ctx, *field_ty)
acc + size_of_ty_data(ctx, *field_ty)
}),
}
}
Expand Down Expand Up @@ -54,12 +54,12 @@ pub fn gep(
let cmpd_ty_data = ctx.with_ty_store(|s| s.resolve_compound(cmpd_ty.unwrap()).clone());
match cmpd_ty_data {
CompoundTypeData::Array { elem, .. } => {
offset += index * byte_size_of_ty(ctx, elem);
offset += index * size_of_ty_data(ctx, elem);
cmpd_ty = to_cmpd_ty(elem);
}
CompoundTypeData::Struct(data) => {
for ty in &data.fields[..index] {
offset += byte_size_of_ty(ctx, *ty);
offset += size_of_ty_data(ctx, *ty);
}
cmpd_ty = to_cmpd_ty(data.fields[index]);
}
Expand Down

0 comments on commit eb561de

Please sign in to comment.