Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Fix BitExtract and get_slice_int losing length information
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckeogh committed May 8, 2024
1 parent b93336c commit dabc31b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion borealis/src/brig/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn codegen_bits() -> TokenStream {

impl Bits {
pub fn new(value: u128, length: u16) -> Self {
Self { value, length }
Self { value, length }.normalize()
}

pub fn value(&self) -> u128 {
Expand Down
13 changes: 5 additions & 8 deletions borealis/src/brig/functions_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,21 +323,18 @@ pub fn codegen_stmt(stmt: Statement) -> TokenStream {
if let Type::Bits = &*value.typ() {
let length = if let Type::Bits = &*length.typ() {
let length = get_ident(&length);
quote!(#length.value() as u32)
quote!(#length.value())
} else {
let length = get_ident(&length);
quote!(#length as u32)
quote!(u16::try_from(#length).unwrap())
};

let value = get_ident(&value);
let start = get_ident(&start);

quote! (
(
(#value >> #start) &
Bits::new(((1u128).checked_shl(#length).map(|x| x - 1).unwrap_or(!0)), #value.length())
)
)
quote! {
(Bits::new(((#value) >> (#start)).value(), #length))
}
} else {
let typ = codegen_type(value.typ());

Expand Down
4 changes: 4 additions & 0 deletions borealis/src/brig/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub fn create_manifest(
)),
)
})
.chain([(
PackageName::new("log".to_owned()).unwrap(),
InheritableDependency::Value(TomlDependency::Simple("0.4.21".to_owned())),
)])
.collect(),
),
dev_dependencies: None,
Expand Down
23 changes: 18 additions & 5 deletions borealis/src/rudder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,11 +1067,24 @@ impl<'ctx: 'fn_ctx, 'fn_ctx> BlockBuildContext<'ctx, 'fn_ctx> {
}

"bitvector_concat" => Some(self.generate_concat(args[0].clone(), args[1].clone())),
"get_slice_int" => Some(self.builder.build(StatementKind::BitExtract {
value: args[1].clone(),
start: args[2].clone(),
length: args[0].clone(),
})),
"get_slice_int" => {
let extract = self.builder.build(StatementKind::BitExtract {
value: args[1].clone(),
start: args[2].clone(),
length: args[0].clone(),
});

let value = self.builder.generate_cast(extract, Arc::new(Type::u128()));

let length = self
.builder
.generate_cast(args[0].clone(), Arc::new(Type::u16()));

Some(
self.builder
.build(StatementKind::CreateBits { value, length }),
)
}

"set_slice_bits" => {
//val set_slice_bits : (%i, %i, %bv, %i, %bv) -> %bv
Expand Down
2 changes: 1 addition & 1 deletion borealis/src/rudder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ impl Statement {
StatementKind::Panic(_) => Arc::new(Type::void()),
StatementKind::ReadPc => Arc::new(Type::u64()),
StatementKind::WritePc { .. } => Arc::new(Type::void()),
StatementKind::BitExtract { value, .. } => value.typ(),
StatementKind::BitExtract { value, .. } => value.typ(), // todo: this is a simplification, be more precise
StatementKind::BitInsert { original_value, .. } => original_value.typ(),
StatementKind::ReadElement { vector, .. } => {
let Type::Vector { element_type, .. } = &*vector.typ() else {
Expand Down

0 comments on commit dabc31b

Please sign in to comment.