Skip to content

Commit

Permalink
some assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
brockelmore committed Jul 17, 2024
1 parent e02fc09 commit 5a647e7
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 132 deletions.
2 changes: 1 addition & 1 deletion crates/graph/src/nodes/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ConcreteNode {
/// Creates a version of this concrete that is max size
pub fn max_size(&self, analyzer: &mut impl AnalyzerBackend) -> Result<Self, GraphError> {
let c = self.underlying(analyzer)?.max_size();
Ok(analyzer.add_node(Node::Concrete(c)).into())
Ok(analyzer.add_node(c).into())
}

/// Gets the internal type of the dynamic that backs this. Panics if this is not a dynamic concrete
Expand Down
39 changes: 37 additions & 2 deletions crates/graph/src/nodes/context/underlying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
AnalyzerBackend, ContextEdge, Edge, Node,
};

use shared::GraphError;
use shared::{GraphError, NodeIdx};

use solang_parser::pt::Loc;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -408,7 +408,11 @@ impl Context {
)));
}

let parent_fn = subctx_kind.parent_ctx().associated_fn(analyzer)?;
let parent_fn = if let Some(cont) = subctx_kind.continuation_of() {
cont.associated_fn(analyzer)?
} else {
subctx_kind.parent_ctx().associated_fn(analyzer)?
};

let modifier_state = if let Some(mstate) = modifier_state {
Some(mstate)
Expand Down Expand Up @@ -466,6 +470,37 @@ impl Context {
Ok(ctx_node)
}

pub fn add_fork_subctxs(
analyzer: &mut impl AnalyzerBackend,
parent_ctx: ContextNode,
loc: Loc,
) -> Result<(ContextNode, ContextNode), GraphError> {
let true_subctx_kind = SubContextKind::new_fork(parent_ctx, true);
let true_subctx = Context::add_subctx(true_subctx_kind, loc, analyzer, None)?;

let false_subctx_kind = SubContextKind::new_fork(parent_ctx, false);
let false_subctx = Context::add_subctx(false_subctx_kind, loc, analyzer, None)?;

parent_ctx.set_child_fork(true_subctx, false_subctx, analyzer)?;
let ctx_fork = analyzer.add_node(Node::ContextFork);
analyzer.add_edge(
ctx_fork,
parent_ctx,
Edge::Context(ContextEdge::ContextFork),
);
analyzer.add_edge(
NodeIdx::from(true_subctx.0),
ctx_fork,
Edge::Context(ContextEdge::Subcontext),
);
analyzer.add_edge(
NodeIdx::from(false_subctx.0),
ctx_fork,
Edge::Context(ContextEdge::Subcontext),
);
Ok((true_subctx, false_subctx))
}

pub fn new_loop_subctx(
parent_ctx: ContextNode,
loc: Loc,
Expand Down
21 changes: 15 additions & 6 deletions crates/graph/src/nodes/context/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ impl ContextNode {
}

/// Debug print the stack
pub fn debug_expr_stack(&self, analyzer: &impl GraphBackend) -> Result<(), GraphError> {
pub fn debug_expr_stack_str(&self, analyzer: &impl GraphBackend) -> Result<String, GraphError> {
let underlying_mut = self.underlying(analyzer)?;
underlying_mut
.expr_ret_stack
.iter()
.enumerate()
.for_each(|(i, elem)| println!("{i}. {}", elem.debug_str(analyzer)));
Ok(format!(
"[\n\t{}\n]",
underlying_mut
.expr_ret_stack
.iter()
.enumerate()
.map(|(i, elem)| format!("{i}. {}", elem.debug_str(analyzer)))
.collect::<Vec<_>>()
.join("\n\t")
))
}

pub fn debug_expr_stack(&self, analyzer: &impl GraphBackend) -> Result<(), GraphError> {
println!("{}", self.debug_expr_stack_str(analyzer)?);
Ok(())
}

Expand Down
26 changes: 7 additions & 19 deletions crates/graph/src/nodes/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Msg {
"data" => {
if let Some(d) = self.data.clone() {
let c = Concrete::from(d);
(analyzer.add_node(Node::Concrete(c)), "msg.data".to_string())
(analyzer.add_node(c), "msg.data".to_string())
} else {
let b = Builtin::DynamicBytes;
let node = analyzer.builtin_or_add(b);
Expand All @@ -87,10 +87,7 @@ impl Msg {
"sender" => {
if let Some(d) = self.sender {
let c = Concrete::from(d);
(
analyzer.add_node(Node::Concrete(c)),
"msg.sender".to_string(),
)
(analyzer.add_node(c), "msg.sender".to_string())
} else {
let node = analyzer.builtin_or_add(Builtin::Address);
let mut var = ContextVar::new_from_builtin(loc, node.into(), analyzer)?;
Expand All @@ -104,7 +101,7 @@ impl Msg {
"sig" => {
if let Some(d) = self.sig {
let c = Concrete::from(d);
(analyzer.add_node(Node::Concrete(c)), "msg.sig".to_string())
(analyzer.add_node(c), "msg.sig".to_string())
} else {
let node = analyzer.builtin_or_add(Builtin::Bytes(4));
let mut var = ContextVar::new_from_builtin(loc, node.into(), analyzer)?;
Expand All @@ -118,10 +115,7 @@ impl Msg {
"value" => {
if let Some(d) = self.value {
let c = Concrete::from(d);
(
analyzer.add_node(Node::Concrete(c)),
"msg.value".to_string(),
)
(analyzer.add_node(c), "msg.value".to_string())
} else {
let node = analyzer.builtin_or_add(Builtin::Uint(256));
let mut var = ContextVar::new_from_builtin(loc, node.into(), analyzer)?;
Expand All @@ -135,10 +129,7 @@ impl Msg {
"origin" => {
if let Some(d) = self.origin {
let c = Concrete::from(d);
(
analyzer.add_node(Node::Concrete(c)),
"tx.origin".to_string(),
)
(analyzer.add_node(c), "tx.origin".to_string())
} else {
let node = analyzer.builtin_or_add(Builtin::Address);
let mut var = ContextVar::new_from_builtin(loc, node.into(), analyzer)?;
Expand All @@ -152,10 +143,7 @@ impl Msg {
"gasprice" => {
if let Some(d) = self.gasprice {
let c = Concrete::from(d);
(
analyzer.add_node(Node::Concrete(c)),
"tx.gasprice".to_string(),
)
(analyzer.add_node(c), "tx.gasprice".to_string())
} else {
let node = analyzer.builtin_or_add(Builtin::Uint(64));
let mut var = ContextVar::new_from_builtin(loc, node.into(), analyzer)?;
Expand All @@ -169,7 +157,7 @@ impl Msg {
"gaslimit" => {
if let Some(d) = self.gaslimit {
let c = Concrete::from(d);
(analyzer.add_node(Node::Concrete(c)), "".to_string())
(analyzer.add_node(c), "".to_string())
} else {
let node = analyzer.builtin_or_add(Builtin::Uint(64));
let mut var = ContextVar::new_from_builtin(loc, node.into(), analyzer)?;
Expand Down
16 changes: 6 additions & 10 deletions crates/graph/src/nodes/var_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,14 @@ impl VarNode {
self.underlying(analyzer)?.name.as_ref().unwrap().name
);
let init = analyzer.parse_expr(arena, &expr, Some(parent));
let underlying = self.underlying(analyzer)?;
let target_ty = VarType::try_from_idx(analyzer, underlying.ty).unwrap();
let initer_ty = ContextVarNode::from(init).ty(analyzer)?.clone();

let underlying = self.underlying(analyzer)?.clone();
let mut set = false;
if let Some(ty) = VarType::try_from_idx(analyzer, underlying.ty) {
if let Some(initer) = VarType::try_from_idx(analyzer, init) {
if let Some(initer) = initer.try_cast(&ty, analyzer)? {
if let Some(conc_idx) = initer.builtin_to_concrete_idx(analyzer, arena)? {
set = true;
self.underlying_mut(analyzer)?.initializer = Some(conc_idx);
}
}
}
if let Some(initer) = initer_ty.try_cast(&target_ty, analyzer)? {
set = true;
self.underlying_mut(analyzer)?.initializer = Some(initer.ty_idx());
}

if !set {
Expand Down
20 changes: 10 additions & 10 deletions crates/graph/src/var_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl VarType {
let c = from_c.underlying(analyzer)?.clone();
let b = to_bn.underlying(analyzer)?;
if let Some(casted) = c.cast(b.clone()) {
let node = analyzer.add_node(Node::Concrete(casted));
let node = analyzer.add_node(casted);
Ok(Some(Self::Concrete(node.into())))
} else {
Ok(None)
Expand All @@ -306,7 +306,7 @@ impl VarType {
let c = from_c.underlying(analyzer)?.clone();
let to_c = to_c.underlying(analyzer)?;
if let Some(casted) = c.cast_from(to_c) {
let node = analyzer.add_node(Node::Concrete(casted));
let node = analyzer.add_node(casted);
Ok(Some(Self::Concrete(node.into())))
} else {
Ok(None)
Expand Down Expand Up @@ -334,7 +334,7 @@ impl VarType {
let Some(conc) = min.val.cast(builtin.clone()) else {
return Ok(None);
};
let conc_idx = analyzer.add_node(Node::Concrete(conc));
let conc_idx = analyzer.add_node(conc);
Ok(Some(conc_idx))
} else {
Ok(None)
Expand Down Expand Up @@ -380,7 +380,7 @@ impl VarType {
let c = from_c.underlying(analyzer)?.clone();
let b = to_bn.underlying(analyzer)?;
if let Some(casted) = c.literal_cast(b.clone()) {
let node = analyzer.add_node(Node::Concrete(casted));
let node = analyzer.add_node(casted);
Ok(Some(Self::Concrete(node.into())))
} else {
Ok(None)
Expand All @@ -390,7 +390,7 @@ impl VarType {
let c = from_c.underlying(analyzer)?.clone();
let to_c = to_c.underlying(analyzer)?;
if let Some(casted) = c.literal_cast_from(to_c) {
let node = analyzer.add_node(Node::Concrete(casted));
let node = analyzer.add_node(casted);
Ok(Some(Self::Concrete(node.into())))
} else {
Ok(None)
Expand Down Expand Up @@ -588,7 +588,7 @@ impl VarType {
// let mut h = H256::default();
// h.0[0] = val.0[idx.low_u32() as usize];
// let ret_val = Concrete::Bytes(1, h);
// let node = analyzer.add_node(Node::Concrete(ret_val));
// let node = analyzer.add_node(ret_val);
// return Ok(Some(node));
// }
// }
Expand Down Expand Up @@ -616,7 +616,7 @@ impl VarType {
// if let Some(idx) = val.0.node_idx() {
// return Ok(idx.into());
// } else if let Some(c) = val.0.concrete() {
// let cnode = analyzer.add_node(Node::Concrete(c));
// let cnode = analyzer.add_node(c);
// return Ok(cnode.into());
// }
// }
Expand All @@ -640,7 +640,7 @@ impl VarType {
// let mut h = H256::default();
// h.0[0] = val.0[idx.low_u32() as usize];
// let ret_val = Concrete::Bytes(1, h);
// let node = analyzer.add_node(Node::Concrete(ret_val));
// let node = analyzer.add_node(ret_val);
// return Ok(Some(node));
// }
// }
Expand All @@ -649,7 +649,7 @@ impl VarType {
// let mut h = H256::default();
// h.0[0] = elems[idx.low_u32() as usize];
// let ret_val = Concrete::Bytes(1, h);
// let node = analyzer.add_node(Node::Concrete(ret_val));
// let node = analyzer.add_node(ret_val);
// return Ok(Some(node));
// }
// }
Expand All @@ -658,7 +658,7 @@ impl VarType {
// let mut h = H256::default();
// h.0[0] = st.as_bytes()[idx.low_u32() as usize];
// let ret_val = Concrete::Bytes(1, h);
// let node = analyzer.add_node(Node::Concrete(ret_val));
// let node = analyzer.add_node(ret_val);
// return Ok(Some(node));
// }
// }
Expand Down
2 changes: 1 addition & 1 deletion crates/pyrometer/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl Analyzer {
expr: &Expression,
parent: Option<NodeIdx>,
) -> Option<ExprRet> {
tracing::trace!("Parsing required compile-time evaluation");
tracing::trace!("Parsing required compile-time evaluation: {expr:?}, {parent:?}");

let ctx = if let Some(parent) = parent {
let pf = Function {
Expand Down
1 change: 0 additions & 1 deletion crates/pyrometer/src/analyzer_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@ impl AnalyzerLike for Analyzer {
type ExprFlag = ExprFlag;

fn push_expr(&mut self, flat: FlatExpr) {
tracing::trace!("pushing expression: {flat:?}");
self.flattened.push(flat);
}

Expand Down
Loading

0 comments on commit 5a647e7

Please sign in to comment.