Skip to content

Commit

Permalink
Merge pull request #2972 from AleoHQ/feat/stubs-disassembler
Browse files Browse the repository at this point in the history
[Draft] Disassembler from Aleo instructions to Leo AST
  • Loading branch information
d0cd authored Dec 1, 2023
2 parents 88b85f8 + c94c2c3 commit ea0fce0
Show file tree
Hide file tree
Showing 24 changed files with 710 additions and 29 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ members = [
"docs/grammar",
"errors",
"leo/package",
"tests/test-framework"
"tests/test-framework",
"utils/disassembler"
]

[workspace.dependencies.snarkvm]
Expand Down
3 changes: 3 additions & 0 deletions compiler/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ license = "GPL-3.0"
edition = "2021"
rust-version = "1.69"

[dependencies.snarkvm]
workspace = true

[dependencies.leo-errors]
path = "../../errors"
version = "=1.10.0"
Expand Down
7 changes: 7 additions & 0 deletions compiler/ast/src/common/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use leo_errors::Result;
use leo_span::{Span, Symbol};
use snarkvm::console::program::Identifier as IdentifierCore;

use crate::{simple_node_impl, Node, NodeID};
use serde::{
Expand All @@ -28,6 +29,7 @@ use serde::{
Serialize,
Serializer,
};
use snarkvm::prelude::Network;
use std::{
collections::BTreeMap,
fmt,
Expand Down Expand Up @@ -152,3 +154,8 @@ impl<'de> Deserialize<'de> for Identifier {
deserializer.deserialize_str(IdentifierVisitor)
}
}
impl<N: Network> From<&IdentifierCore<N>> for Identifier {
fn from(id: &IdentifierCore<N>) -> Self {
Self { name: Symbol::intern(&id.to_string()), span: Default::default(), id: Default::default() }
}
}
12 changes: 12 additions & 0 deletions compiler/ast/src/mapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{Identifier, Node, NodeID, Type};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use snarkvm::prelude::{Mapping as MappingCore, Network};
use std::fmt;

/// A mapping declaration, e.g `mapping balances: address => u128`.
Expand All @@ -36,6 +37,17 @@ pub struct Mapping {
pub id: NodeID,
}

impl<N: Network> From<&MappingCore<N>> for Mapping {
fn from(mapping: &MappingCore<N>) -> Self {
Self {
identifier: Identifier::from(mapping.name()),
key_type: Type::from(mapping.key().plaintext_type()),
value_type: Type::from(mapping.value().plaintext_type()),
span: Default::default(),
id: Default::default(),
}
}
}
impl fmt::Display for Mapping {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "mapping {}: {} => {}", self.identifier, self.key_type, self.value_type)
Expand Down
7 changes: 7 additions & 0 deletions compiler/ast/src/program/program_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::Identifier;

use core::fmt;
use serde::{de, de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
use snarkvm::{console::program::ProgramID, prelude::Network};
use std::collections::BTreeMap;

/// An identifier for a program that is eventually deployed to the network.
Expand Down Expand Up @@ -92,3 +93,9 @@ impl<'de> Deserialize<'de> for ProgramId {
deserializer.deserialize_str(ProgramIdVisitor)
}
}

impl<N: Network> From<&ProgramID<N>> for ProgramId {
fn from(program: &ProgramID<N>) -> Self {
Self { name: Identifier::from(program.name()), network: Identifier::from(program.network()) }
}
}
73 changes: 70 additions & 3 deletions compiler/ast/src/struct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
pub mod member;
pub use member::*;

use crate::{Identifier, Node, NodeID};
use crate::{Identifier, Mode, Node, NodeID, Type};
use leo_span::{Span, Symbol};

use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::fmt;

use snarkvm::{
console::program::{RecordType, StructType},
prelude::{
EntryType::{Constant, Private, Public},
Network,
},
};

/// A struct type definition, e.g., `struct Foo { my_field: Bar }`.
/// In some languages these are called `struct`s.
///
Expand Down Expand Up @@ -70,10 +79,68 @@ impl fmt::Display for Struct {
f.write_str(if self.is_record { "record" } else { "struct" })?;
writeln!(f, " {} {{ ", self.identifier)?;
for field in self.members.iter() {
writeln!(f, " {field}")?;
writeln!(f, " {field}")?;
}
write!(f, "}}")
write!(f, " }}")
}
}

crate::simple_node_impl!(Struct);

impl<N: Network> From<&StructType<N>> for Struct {
fn from(input: &StructType<N>) -> Self {
Self {
identifier: Identifier::from(input.name()),
members: input
.members()
.iter()
.map(|(id, type_)| Member {
mode: Mode::None,
identifier: Identifier::from(id),
type_: Type::from(type_),
span: Default::default(),
id: Default::default(),
})
.collect(),
is_record: false,
span: Default::default(),
id: Default::default(),
}
}
}

impl<N: Network> From<&RecordType<N>> for Struct {
fn from(input: &RecordType<N>) -> Self {
Self {
identifier: Identifier::from(input.name()),
members: [
vec![Member {
mode: if input.owner().is_private() { Mode::Public } else { Mode::Private },
identifier: Identifier::new(Symbol::intern("owner"), Default::default()),
type_: Type::Address,
span: Default::default(),
id: Default::default(),
}],
input
.entries()
.iter()
.map(|(id, entry)| Member {
mode: if input.owner().is_public() { Mode::Public } else { Mode::Private },
identifier: Identifier::from(id),
type_: match entry {
Public(t) => Type::from(t),
Private(t) => Type::from(t),
Constant(t) => Type::from(t),
},
span: Default::default(),
id: Default::default(),
})
.collect_vec(),
]
.concat(),
is_record: true,
span: Default::default(),
id: Default::default(),
}
}
}
39 changes: 37 additions & 2 deletions compiler/ast/src/stub/finalize_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{Identifier, Input, Node, NodeID, Output, TupleType, Type};
use crate::{Finalize, FunctionInput, Identifier, Input, Mode, Node, NodeID, Output, TupleType, Type};

use leo_span::Span;
use leo_span::{Span, Symbol};

use core::fmt;
use serde::{Deserialize, Serialize};
use snarkvm::{
prelude::{
FinalizeType::{Future, Plaintext},
Network,
},
synthesizer::program::{CommandTrait, FinalizeCore},
};

/// A finalize stub.
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -51,6 +58,34 @@ impl FinalizeStub {
}
}

impl<N: Network, Command: CommandTrait<N>> From<&FinalizeCore<N, Command>> for FinalizeStub {
fn from(finalize: &FinalizeCore<N, Command>) -> Self {
let mut inputs = Vec::new();

finalize.inputs().iter().enumerate().for_each(|(index, input)| {
let arg_name = Identifier::new(Symbol::intern(&format!("a{}", index + 1)), Default::default());
match input.finalize_type() {
Plaintext(val) => inputs.push(Input::Internal(FunctionInput {
identifier: arg_name,
mode: Mode::None,
type_: Type::from(val),
span: Default::default(),
id: Default::default(),
})),
Future(_) => {} // Don't need to worry about nested futures
}
});

Self::new(Identifier::from(finalize.name()), inputs, Vec::new(), Default::default(), Default::default())
}
}

impl From<Finalize> for FinalizeStub {
fn from(finalize: Finalize) -> Self {
Self::new(finalize.identifier, finalize.input, finalize.output, Default::default(), Default::default())
}
}

impl fmt::Display for FinalizeStub {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let parameters = self.input.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(",");
Expand Down
Loading

0 comments on commit ea0fce0

Please sign in to comment.