Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 138 additions & 1 deletion src/codegen/encoding/soroban_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn soroban_decode_arg(
},
Type::Uint(64) => decode_u64(wrapper_cfg, vartab, arg),

Type::Address(_) | Type::String => arg.clone(),
Type::Address(_) | Type::String | Type::DynamicBytes => arg.clone(),

Type::Enum(enum_no) => {
let decoded = soroban_decode_arg(arg, wrapper_cfg, vartab, ns, Some(Type::Uint(32)));
Expand Down Expand Up @@ -209,6 +209,75 @@ pub fn soroban_decode_arg(
}
}

Type::Bytes(n) => {
let n_expr = Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(32),
value: BigInt::from(n as u64),
};

let buf_var = vartab.temp_name("bytes_buf", &Type::DynamicBytes);
wrapper_cfg.add(
vartab,
Instr::Set {
loc: Loc::Codegen,
res: buf_var,
expr: Expression::AllocDynamicBytes {
loc: Loc::Codegen,
ty: Type::DynamicBytes,
size: Box::new(n_expr.clone()),
initializer: None,
},
},
);
let buf = Expression::Variable {
loc: Loc::Codegen,
ty: Type::DynamicBytes,
var_no: buf_var,
};

let dest_ptr = Expression::VectorData {
pointer: Box::new(buf.clone()),
};
let dest_encoded = zext_shift_add(Loc::Codegen, dest_ptr, 32, 4);
let src_off_encoded = zext_shift_add(
Loc::Codegen,
Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(32),
value: BigInt::from(0u64),
},
32,
4,
);
let len_encoded = zext_shift_add(Loc::Codegen, n_expr, 32, 4);

let unused = vartab.temp_name("bytes_copy_ret", &Type::Uint(64));
wrapper_cfg.add(
vartab,
Instr::Call {
res: vec![unused],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::BytesCopyToLinearMemory.name().to_string(),
},
args: vec![arg, src_off_encoded, dest_encoded, len_encoded],
},
);

Expression::Load {
loc: Loc::Codegen,
ty: Type::Bytes(n),
expr: Box::new(Expression::Cast {
loc: Loc::Codegen,
ty: Type::Ref(Box::new(Type::DynamicBytes)),
expr: Box::new(Expression::VectorData {
pointer: Box::new(buf),
}),
}),
}
}

_ => unimplemented!("unimplemented ty {:#?} in soroban decoder", ty),
}
}
Expand Down Expand Up @@ -721,6 +790,74 @@ pub fn soroban_encode_arg(
res: obj,
expr: encode_vector(item.clone(), cfg, vartab),
},
Type::DynamicBytes => Instr::Set {
loc: Loc::Codegen,
res: obj,
expr: item.clone(),
},

Type::Bytes(n) => {
let n_val = BigInt::from(n as u64);

let n_expr = Expression::NumberLiteral {
loc: item.loc(),
ty: Type::Uint(32),
value: n_val.clone(),
};

let buf_var = vartab.temp_name("bytes_spill", &Type::DynamicBytes);
cfg.add(
vartab,
Instr::Set {
loc: item.loc(),
res: buf_var,
expr: Expression::AllocDynamicBytes {
loc: item.loc(),
ty: Type::DynamicBytes,
size: Box::new(n_expr.clone()),
initializer: None,
},
},
);
let buf = Expression::Variable {
loc: item.loc(),
ty: Type::DynamicBytes,
var_no: buf_var,
};

cfg.add(
vartab,
Instr::Store {
dest: Expression::Cast {
loc: item.loc(),
ty: Type::Ref(Box::new(Type::DynamicBytes)),
expr: Box::new(Expression::VectorData {
pointer: Box::new(buf.clone()),
}),
},
data: item.clone(),
},
);

let encoded_ptr = zext_shift_add(
item.loc(),
Expression::VectorData {
pointer: Box::new(buf),
},
32,
4,
);
let encoded_len = zext_shift_add(item.loc(), n_expr, 32, 4);

Instr::Call {
res: vec![obj],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::BytesNewFromLinearMemory.name().to_string(),
},
args: vec![encoded_ptr, encoded_len],
}
}

_ => todo!("Type not yet supported in soroban encoder: {:?}", item.ty()),
};
Expand Down
43 changes: 43 additions & 0 deletions src/codegen/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,10 @@ fn expr_builtin(
args: arguments,
}
}
ast::Builtin::Sha256 | ast::Builtin::Keccak256 if ns.target == Target::Soroban => {
soroban_compute_hash(args, &builtin, cfg, contract_no, func, ns, vartab, opt, loc)
}

_ => {
let arguments: Vec<Expression> = args
.iter()
Expand Down Expand Up @@ -2965,6 +2969,45 @@ fn alloc_dynamic_array(
}
}

fn soroban_compute_hash(
args: &[ast::Expression],
builtin: &ast::Builtin,
cfg: &mut ControlFlowGraph,
contract_no: usize,
func: Option<&Function>,
ns: &Namespace,
vartab: &mut Vartable,
opt: &Options,
loc: &pt::Loc,
) -> Expression {
let host_fn_name = match builtin {
ast::Builtin::Sha256 => HostFunctions::ComputeSha256.name().to_string(),
ast::Builtin::Keccak256 => HostFunctions::ComputeKeccak256.name().to_string(),
_ => unreachable!("soroban_compute_hash only lowers sha256 and keccak256"),
};

let raw_input = expression(&args[0], cfg, contract_no, func, ns, vartab, opt);
let input = soroban_encode_arg(raw_input, cfg, vartab, ns);

let digest_var = vartab.temp_name("digest_obj", &Type::Uint(64));
cfg.add(
vartab,
Instr::Call {
res: vec![digest_var],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction { name: host_fn_name },
args: vec![input],
},
);
let digest = Expression::Variable {
loc: *loc,
ty: Type::Uint(64),
var_no: digest_var,
};

soroban_decode_arg(digest, cfg, vartab, ns, Some(Type::Bytes(32)))
}

fn add(
loc: &pt::Loc,
ty: &Type,
Expand Down
8 changes: 8 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ pub enum HostFunctions {
BytesNewFromLinearMemory,
BytesLen,
BytesCopyToLinearMemory,
BytesGet,
BytesPut,
ComputeSha256,
ComputeKeccak256,
}

impl HostFunctions {
Expand Down Expand Up @@ -192,10 +196,14 @@ impl HostFunctions {
HostFunctions::BytesNewFromLinearMemory => "b.3",
HostFunctions::BytesLen => "b.8",
HostFunctions::BytesCopyToLinearMemory => "b.1",
HostFunctions::BytesGet => "b.6",
HostFunctions::BytesPut => "b.5",
HostFunctions::VecLen => "v.3",
HostFunctions::VecPopBack => "v.7",
HostFunctions::VecGet => "v.1",
HostFunctions::VecPut => "v.0",
HostFunctions::ComputeSha256 => "c._",
HostFunctions::ComputeKeccak256 => "c.1",
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/emit/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,8 @@ impl<'a> Binary<'a> {

fn var_ty_uses_pointer_storage(&self, ty: &Type) -> bool {
match ty.deref_memory() {
Type::Struct(_)
| Type::Array(..)
| Type::DynamicBytes
| Type::ExternalFunction { .. } => true,
Type::String => self.ns.target != Target::Soroban,
Type::Struct(_) | Type::Array(..) | Type::ExternalFunction { .. } => true,
Type::String | Type::DynamicBytes => self.ns.target != Target::Soroban,
_ => false,
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/emit/soroban/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ impl HostFunctions {
.context
.i64_type()
.fn_type(&[ty.into(), ty.into(), ty.into(), ty.into()], false),
HostFunctions::BytesGet => bin
.context
.i64_type()
.fn_type(&[ty.into(), ty.into()], false),
HostFunctions::BytesPut => bin
.context
.i64_type()
.fn_type(&[ty.into(), ty.into(), ty.into()], false),
HostFunctions::ComputeSha256 => bin.context.i64_type().fn_type(&[ty.into()], false),
HostFunctions::ComputeKeccak256 => bin.context.i64_type().fn_type(&[ty.into()], false),
}
}
}
Expand Down Expand Up @@ -338,7 +348,9 @@ impl SorobanTarget {
ast::Type::Uint(256) => ScSpecTypeDef::U256,
ast::Type::Bool => ScSpecTypeDef::Bool,
ast::Type::Address(_) => ScSpecTypeDef::Address,
ast::Type::Bytes(_) => ScSpecTypeDef::Bytes,
ast::Type::Bytes(_) | ast::Type::DynamicBytes => {
ScSpecTypeDef::Bytes
}
ast::Type::String => ScSpecTypeDef::String,
ast::Type::Array(ty, _) => {
let element = Self::vec_spec_type(ty.as_ref());
Expand Down Expand Up @@ -377,7 +389,7 @@ impl SorobanTarget {
ast::Type::Int(_) => ScSpecTypeDef::I32,
ast::Type::Bool => ScSpecTypeDef::Bool,
ast::Type::Address(_) => ScSpecTypeDef::Address,
ast::Type::Bytes(_) => ScSpecTypeDef::Bytes,
ast::Type::Bytes(_) | ast::Type::DynamicBytes => ScSpecTypeDef::Bytes,
ast::Type::String => ScSpecTypeDef::String,
ast::Type::Void => ScSpecTypeDef::Void,
ast::Type::Struct(_) => ScSpecTypeDef::Void, // TODO: Map struct types.
Expand Down Expand Up @@ -466,8 +478,13 @@ impl SorobanTarget {
HostFunctions::GetCurrentContractAddress,
HostFunctions::BytesNewFromLinearMemory,
HostFunctions::BytesCopyToLinearMemory,
HostFunctions::BytesLen,
HostFunctions::BytesGet,
HostFunctions::BytesPut,
HostFunctions::VecLen,
HostFunctions::VecPopBack,
HostFunctions::ComputeSha256,
HostFunctions::ComputeKeccak256,
];

for func in &host_functions {
Expand Down
Loading