Skip to content

Commit

Permalink
fix type check issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Sep 11, 2023
1 parent c312a44 commit 8a991ca
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn type_check_not(

let t = engines.te().get(operand_expr.return_type);
match t {
TypeInfo::B256 | TypeInfo::UnsignedInteger(_) => Ok((
TypeInfo::B256 | TypeInfo::UnsignedInteger(_) | TypeInfo::Numeric => Ok((
ty::TyIntrinsicFunctionKind {
kind,
arguments: vec![operand_expr],
Expand Down Expand Up @@ -1008,7 +1008,7 @@ fn type_check_bitwise_binary_op(

let t = engines.te().get(lhs.return_type);
match t {
TypeInfo::B256 | TypeInfo::UnsignedInteger(_) => Ok((
TypeInfo::B256 | TypeInfo::UnsignedInteger(_) | TypeInfo::Numeric => Ok((
ty::TyIntrinsicFunctionKind {
kind,
arguments: vec![lhs, rhs],
Expand Down Expand Up @@ -1081,7 +1081,7 @@ fn type_check_shift_binary_op(

let t = engines.te().get(lhs.return_type);
match t {
TypeInfo::B256 | TypeInfo::UnsignedInteger(_) => Ok((
TypeInfo::B256 | TypeInfo::UnsignedInteger(_) | TypeInfo::Numeric => Ok((
ty::TyIntrinsicFunctionKind {
kind,
arguments: vec![lhs, rhs],
Expand Down
62 changes: 43 additions & 19 deletions sway-ir/src/optimize/misc_demotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ fn wide_binary_op_demotion(context: &mut Context, function: Function) -> Result<
op: B::Add | B::Sub | B::Mul | B::Div | B::Mod | B::And | B::Or | B::Xor,
arg1,
arg2,
} = instr_val.get_instruction(context)? else {
} = instr_val.get_instruction(context)?
else {
return None;
};

Expand Down Expand Up @@ -573,19 +574,30 @@ fn wide_cmp_demotion(context: &mut Context, function: Function) -> Result<bool,
let candidates = function
.instruction_iter(context)
.filter_map(|(block, instr_val)| {
let Instruction::Cmp(Predicate::Equal | Predicate::LessThan | Predicate::GreaterThan, arg1, arg2) = instr_val.get_instruction(context)? else {
let Instruction::Cmp(
Predicate::Equal | Predicate::LessThan | Predicate::GreaterThan,
arg1,
arg2,
) = instr_val.get_instruction(context)?
else {
return None;
};

let arg1_type = arg1
.get_type(context);
let arg2_type = arg2
.get_type(context);
let arg1_type = arg1.get_type(context);
let arg2_type = arg2.get_type(context);

match (arg1_type, arg2_type) {
(Some(arg1_type), Some(arg2_type)) if arg1_type.is_uint(context) && arg2_type.is_uint(context) => Some((block, instr_val)),
(Some(arg1_type), Some(arg2_type)) if arg1_type.is_b256(context) && arg2_type.is_b256(context) => Some((block, instr_val)),
_ => None
(Some(arg1_type), Some(arg2_type))
if arg1_type.is_uint_of(context, 256) && arg2_type.is_uint_of(context, 256) =>
{
Some((block, instr_val))
}
(Some(arg1_type), Some(arg2_type))
if arg1_type.is_b256(context) && arg2_type.is_b256(context) =>
{
Some((block, instr_val))
}
_ => None,
}
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -726,12 +738,15 @@ fn wide_unary_op_demotion(context: &mut Context, function: Function) -> Result<b
let Instruction::UnaryOp {
op: UnaryOpKind::Not,
arg,
} = instr_val.get_instruction(context)? else {
} = instr_val.get_instruction(context)?
else {
return None;
};

match arg.get_type(context) {
Some(t) if t.is_uint(context) || t.is_b256(context) => Some((block, instr_val)),
Some(t) if t.is_uint_of(context, 256) || t.is_b256(context) => {
Some((block, instr_val))
}
_ => None,
}
})
Expand Down Expand Up @@ -848,21 +863,30 @@ fn wide_shift_op_demotion(context: &mut Context, function: Function) -> Result<b
.instruction_iter(context)
.filter_map(|(block, instr_val)| {
let instr = instr_val.get_instruction(context)?;
let Instruction::BinaryOp { op: BinaryOpKind::Lsh | BinaryOpKind::Rsh , arg1, arg2 } = instr else {
let Instruction::BinaryOp {
op: BinaryOpKind::Lsh | BinaryOpKind::Rsh,
arg1,
arg2,
} = instr
else {
return None;
};

let arg1_type = arg1
.get_type(context);
let arg2_type = arg2
.get_type(context);
let arg1_type = arg1.get_type(context);
let arg2_type = arg2.get_type(context);

match (arg1_type, arg2_type) {
(Some(arg1_type), Some(arg2_type))
if arg1_type.is_uint(context) && arg2_type.is_uint64(context) => Some((block, instr_val)),
if arg1_type.is_uint_of(context, 256) && arg2_type.is_uint64(context) =>
{
Some((block, instr_val))
}
(Some(arg1_type), Some(arg2_type))
if arg1_type.is_b256(context) && arg2_type.is_uint64(context) => Some((block, instr_val)),
_ => None
if arg1_type.is_b256(context) && arg2_type.is_uint64(context) =>
{
Some((block, instr_val))
}
_ => None,
}
})
.collect::<Vec<_>>();
Expand Down
3 changes: 1 addition & 2 deletions sway-ir/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> {
| BinaryOpKind::Or
| BinaryOpKind::Xor
| BinaryOpKind::Mod => {
dbg!(1);
if !arg1_ty.is_ptr(self.context)
|| !arg2_ty.is_ptr(self.context)
|| !result_ty.is_ptr(self.context)
Expand Down Expand Up @@ -472,7 +471,7 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> {
}
BinaryOpKind::And | BinaryOpKind::Or | BinaryOpKind::Xor => {
if !arg1_ty.eq(self.context, &arg2_ty)
|| (!arg1_ty.is_uint(self.context) && !arg1_ty.is_b256(self.context))
|| !(arg1_ty.is_uint(self.context) || arg1_ty.is_b256(self.context))
{
return Err(IrError::VerifyBinaryOpIncorrectArgType);
}
Expand Down
8 changes: 0 additions & 8 deletions sway-types/src/b256.rs

This file was deleted.

1 change: 0 additions & 1 deletion sway-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{io, iter, slice};

pub mod constants;

pub mod b256;
pub mod ident;
pub mod u256;
pub use ident::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"typeArguments": null
},
"name": "C0",
"offset": 8524
"offset": 7308
},
{
"configurableType": {
Expand All @@ -16,7 +16,7 @@
"typeArguments": null
},
"name": "C1",
"offset": 8532
"offset": 7316
},
{
"configurableType": {
Expand All @@ -25,7 +25,7 @@
"typeArguments": null
},
"name": "C2",
"offset": 8548
"offset": 7332
},
{
"configurableType": {
Expand All @@ -34,7 +34,7 @@
"typeArguments": []
},
"name": "C3",
"offset": 8580
"offset": 7364
},
{
"configurableType": {
Expand All @@ -43,7 +43,7 @@
"typeArguments": []
},
"name": "C4",
"offset": 8596
"offset": 7380
},
{
"configurableType": {
Expand All @@ -52,7 +52,7 @@
"typeArguments": []
},
"name": "C5",
"offset": 8612
"offset": 7396
},
{
"configurableType": {
Expand All @@ -61,7 +61,7 @@
"typeArguments": null
},
"name": "C6",
"offset": 8628
"offset": 7412
},
{
"configurableType": {
Expand All @@ -70,7 +70,7 @@
"typeArguments": null
},
"name": "C7",
"offset": 8644
"offset": 7428
},
{
"configurableType": {
Expand All @@ -79,7 +79,7 @@
"typeArguments": null
},
"name": "C9",
"offset": 8692
"offset": 7476
}
],
"functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0xb870f1a5893135ee3d0d688f57577837050bb720452b7afe67caca22e6984127);
let addr = abi(BasicStorage, 0x3069f32c2759615911b885900468c1044ff4e4545de12921d82f49f7158cf50c);
let key = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down

0 comments on commit 8a991ca

Please sign in to comment.