Skip to content

Commit

Permalink
Generalize EVMLA functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Feb 7, 2024
1 parent 052fbd7 commit 6b47b36
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 293 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use crate::context::code_type::CodeType;
/// Is only relevant to the EVM legacy assembly.
///
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BlockKey {
pub struct Key {
/// The block code type.
pub code_type: CodeType,
/// The block tag.
pub tag: num::BigUint,
}

impl BlockKey {
impl Key {
///
/// A shortcut constructor.
///
Expand All @@ -26,7 +26,7 @@ impl BlockKey {
}
}

impl std::fmt::Display for BlockKey {
impl std::fmt::Display for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//!

pub mod evmla_data;
pub mod key;

use self::evmla_data::EVMLAData;

Expand Down
45 changes: 45 additions & 0 deletions src/context/function/evmla_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//!
//! The LLVM function EVM legacy assembly data.
//!

use std::collections::BTreeMap;

use crate::context::function::block::key::Key as BlockKey;
use crate::context::function::block::Block;

///
/// The LLVM function EVM legacy assembly data.
///
/// Describes some data that is only relevant to the EVM legacy assembly.
///
#[derive(Debug)]
pub struct EVMLAData<'ctx> {
/// The ordinary blocks with numeric tags.
/// Is only used by the Solidity EVM compiler.
pub blocks: BTreeMap<BlockKey, Vec<Block<'ctx>>>,
/// The function stack size.
pub stack_size: usize,
}

impl<'ctx> EVMLAData<'ctx> {
///
/// A shortcut constructor.
///
pub fn new(stack_size: usize) -> Self {
Self {
blocks: BTreeMap::new(),
stack_size,
}
}

///
/// Inserts a function block.
///
pub fn insert_block(&mut self, key: BlockKey, block: Block<'ctx>) {
if let Some(blocks) = self.blocks.get_mut(&key) {
blocks.push(block);
} else {
self.blocks.insert(key, vec![block]);
}
}
}
2 changes: 2 additions & 0 deletions src/context/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
//! The common LLVM function entities.
//!

pub mod block;
pub mod declaration;
pub mod evmla_data;
pub mod r#return;
8 changes: 4 additions & 4 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//!

pub mod attribute;
pub mod block_key;
pub mod code_type;
pub mod function;
pub mod r#loop;
Expand All @@ -23,6 +22,7 @@ use self::pointer::Pointer;
use self::r#loop::Loop;
use self::traits::address_space::IAddressSpace;
use self::traits::evmla_data::IEVMLAData;
use self::traits::evmla_function::IEVMLAFunction;

///
/// The LLVM module context trait.
Expand All @@ -42,7 +42,7 @@ pub trait IContext<'ctx> {
///
/// The function type.
///
type Function;
type Function: IEVMLAFunction<'ctx>;

///
/// The Solidity extra data type.
Expand Down Expand Up @@ -80,12 +80,12 @@ pub trait IContext<'ctx> {
fn module(&self) -> &inkwell::module::Module<'ctx>;

///
/// Sets the current code type (deploy or runtime).
/// Sets the code type.
///
fn set_code_type(&mut self, code_type: CodeType);

///
/// Returns the current code type (deploy or runtime).
/// Returns the code type.
///
fn code_type(&self) -> Option<CodeType>;

Expand Down
4 changes: 2 additions & 2 deletions src/context/traits/evmla_data.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//!
//! The LLVM IR EVMLA function trait.
//! The LLVM IR EVMLA data trait.
//!

use crate::context::value::Value;

///
/// The LLVM IR EVMLA function trait.
/// The LLVM IR EVMLA data trait.
///
pub trait IEVMLAData<'ctx> {
///
Expand Down
18 changes: 18 additions & 0 deletions src/context/traits/evmla_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//!
//! The LLVM IR EVMLA function trait.
//!

use crate::context::function::block::key::Key as BlockKey;
use crate::context::function::block::Block;

///
/// The LLVM IR EVMLA function trait.
///
pub trait IEVMLAFunction<'ctx> {
///
/// Returns the block with the specified tag and initial stack pattern.
///
/// If there is only one block, it is returned unconditionally.
///
fn find_block(&self, key: &BlockKey, stack_hash: &md5::Digest) -> anyhow::Result<Block<'ctx>>;
}
1 change: 1 addition & 0 deletions src/context/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

pub mod address_space;
pub mod evmla_data;
pub mod evmla_function;
86 changes: 0 additions & 86 deletions src/eravm/context/function/evmla_data.rs

This file was deleted.

52 changes: 45 additions & 7 deletions src/eravm/context/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//! The LLVM IR generator function.
//!

pub mod block;
pub mod evmla_data;
pub mod intrinsics;
pub mod llvm_runtime;
pub mod runtime;
Expand All @@ -13,14 +11,17 @@ pub mod yul_data;
use std::collections::HashMap;

use crate::context::attribute::Attribute;
use crate::context::function::block::key::Key as BlockKey;
use crate::context::function::block::Block;
use crate::context::function::declaration::Declaration as FunctionDeclaration;
use crate::context::function::evmla_data::EVMLAData as FunctionEVMLAData;
use crate::context::function::r#return::Return as FunctionReturn;
use crate::context::pointer::Pointer;
use crate::context::traits::evmla_function::IEVMLAFunction;
use crate::eravm::context::address_space::AddressSpace;
use crate::optimizer::settings::size_level::SizeLevel;
use crate::optimizer::Optimizer;

use self::evmla_data::EVMLAData;
use self::runtime::Runtime;
use self::vyper_data::VyperData;
use self::yul_data::YulData;
Expand Down Expand Up @@ -50,7 +51,7 @@ pub struct Function<'ctx> {
/// The Yul compiler data.
yul_data: Option<YulData>,
/// The EVM legacy assembly compiler data.
evmla_data: Option<EVMLAData<'ctx>>,
evmla_data: Option<FunctionEVMLAData<'ctx>>,
/// The Vyper data.
vyper_data: Option<VyperData>,
}
Expand Down Expand Up @@ -339,7 +340,7 @@ impl<'ctx> Function<'ctx> {
///
/// Sets the EVM legacy assembly data.
///
pub fn set_evmla_data(&mut self, data: EVMLAData<'ctx>) {
pub fn set_evmla_data(&mut self, data: FunctionEVMLAData<'ctx>) {
self.evmla_data = Some(data);
}

Expand All @@ -349,7 +350,7 @@ impl<'ctx> Function<'ctx> {
/// # Panics
/// If the EVM data has not been initialized.
///
pub fn evmla(&self) -> &EVMLAData<'ctx> {
pub fn evmla(&self) -> &FunctionEVMLAData<'ctx> {
self.evmla_data
.as_ref()
.expect("The EVM data must have been initialized")
Expand All @@ -361,7 +362,7 @@ impl<'ctx> Function<'ctx> {
/// # Panics
/// If the EVM data has not been initialized.
///
pub fn evmla_mut(&mut self) -> &mut EVMLAData<'ctx> {
pub fn evmla_mut(&mut self) -> &mut FunctionEVMLAData<'ctx> {
self.evmla_data
.as_mut()
.expect("The EVM data must have been initialized")
Expand Down Expand Up @@ -429,3 +430,40 @@ impl<'ctx> Function<'ctx> {
.expect("The Yul data must have been initialized")
}
}

impl<'ctx> IEVMLAFunction<'ctx> for Function<'ctx> {
fn find_block(&self, key: &BlockKey, stack_hash: &md5::Digest) -> anyhow::Result<Block<'ctx>> {
let evmla_data = self.evmla();

if evmla_data
.blocks
.get(key)
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))?
.len()
== 1
{
return evmla_data
.blocks
.get(key)
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))?
.first()
.cloned()
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key));
}

evmla_data
.blocks
.get(key)
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))?
.iter()
.find(|block| {
block
.evm()
.stack_hashes
.iter()
.any(|hash| hash == stack_hash)
})
.cloned()
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))
}
}
23 changes: 0 additions & 23 deletions src/evm/context/function/block/evmla_data.rs

This file was deleted.

Loading

0 comments on commit 6b47b36

Please sign in to comment.