use crate::polkavm::context::code_type::CodeType;
use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context;
use crate::polkavm::evm::immutable::Store;
pub fn r#return<'ctx>(
context: &mut Context<'ctx>,
offset: inkwell::values::IntValue<'ctx>,
length: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<()> {
match context.code_type() {
None => anyhow::bail!("Return is not available if the contract part is undefined"),
Some(CodeType::Deploy) => {
context.build_call(
<Store as RuntimeFunction>::declaration(context),
Default::default(),
"store_immutable_data",
);
}
Some(CodeType::Runtime) => {}
}
context.build_exit(
context.integer_const(crate::polkavm::XLEN, 0),
offset,
length,
)
}
pub fn revert<'ctx>(
context: &mut Context<'ctx>,
offset: inkwell::values::IntValue<'ctx>,
length: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<()> {
context.build_exit(
context.integer_const(crate::polkavm::XLEN, 1),
offset,
length,
)
}
pub fn stop(context: &mut Context) -> anyhow::Result<()> {
r#return(context, context.word_const(0), context.word_const(0))
}
pub fn invalid(context: &mut Context) -> anyhow::Result<()> {
let invalid_block = context.append_basic_block("explicit_invalid");
context.build_unconditional_branch(invalid_block);
context.set_basic_block(invalid_block);
context.build_runtime_call(revive_runtime_api::polkavm_imports::INVALID, &[]);
context.build_unreachable();
context.set_basic_block(context.append_basic_block("dead_code"));
Ok(())
}
pub fn selfdestruct<'ctx>(
context: &mut Context<'ctx>,
address: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<()> {
let address_pointer = context.build_address_argument_store(address)?;
context.build_runtime_call(
revive_runtime_api::polkavm_imports::TERMINATE,
&[address_pointer.to_int(context).into()],
);
Ok(())
}