Skip to content

Commit

Permalink
Cleaned up code, ready to merge
Browse files Browse the repository at this point in the history
!
  • Loading branch information
Swathi Narayan committed Jul 19, 2024
1 parent bdb4bbb commit 7280c35
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 74 deletions.
59 changes: 2 additions & 57 deletions crates/forge_analyzer/src/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![allow(dead_code, unused)]

use std::hash::Hash;
use std::{borrow::Borrow, fmt, mem};

use crate::utils::{calls_method, eq_prop_name};
use forge_file_resolver::{FileResolver, ForgeResolver};
use forge_utils::{create_newtype, FxHashMap};
use std::collections::{HashMap, HashSet};
use swc_core::common::pass::define;

use itertools::Itertools;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -187,6 +184,7 @@ pub fn run_resolver(
module.visit_with(&mut import_collector);
}

// check for required after Definitions pass
// This loop runs through the different import modules and corresponding definitions.
let defs = Definitions::new(
environment
Expand Down Expand Up @@ -230,61 +228,8 @@ pub fn run_resolver(
};
module.visit_with(&mut collector);
}
environment
}

// This function is a helper function to run_resolver() 's SSA Form Loop.
// Input: rvalue and hashmap of VarIds that have been updated in the SSA Form Loop.
// Output: new rvalue that has the newest VarId in its operands.
pub fn update_rvalue(rvalue: &Rvalue, updated_vars: &HashMap<VarId, VarId>) -> Rvalue {
let mut new_rvalue = rvalue.clone();
match rvalue {
Rvalue::Unary(unop, Operand::Var(variable)) => {
let op_var_id = variable.as_var_id().unwrap();
if updated_vars.contains_key(&op_var_id) {
let updated_var_id = *updated_vars.get(&op_var_id).unwrap();
let new_operand = Operand::Var(Variable::new(updated_var_id));
new_rvalue = Rvalue::Unary(*unop, new_operand);
}
}
Rvalue::Bin(binop, operand1, operand2) => {
let mut new_operand_1 = operand1.clone();
let mut new_operand_2 = operand2.clone();
if let Operand::Var(variable) = operand1 {
let op_var_id = variable.as_var_id().unwrap();
if updated_vars.contains_key(&op_var_id) {
let updated_var_id = *updated_vars.get(&op_var_id).unwrap();
new_operand_1 = Operand::Var(Variable::new(updated_var_id));
}
}
if let Operand::Var(variable) = operand2 {
let op_var_id = variable.as_var_id().unwrap();
if updated_vars.contains_key(&op_var_id) {
let updated_var_id = *updated_vars.get(&op_var_id).unwrap();
new_operand_2 = Operand::Var(Variable::new(updated_var_id));
}
}
new_rvalue = Rvalue::Bin(*binop, new_operand_1, new_operand_2);
}
Rvalue::Read(Operand::Var(variable)) => {
let op_var_id = variable.as_var_id().unwrap();
if updated_vars.contains_key(&op_var_id) {
let updated_var_id = *updated_vars.get(&op_var_id).unwrap();
let new_operand = Operand::Var(Variable::new(updated_var_id));
new_rvalue = Rvalue::Read(new_operand);
}
}
Rvalue::Call(Operand::Var(variable), vector) => {
let op_var_id = variable.as_var_id().unwrap();
if updated_vars.contains_key(&op_var_id) {
let updated_var_id = *updated_vars.get(&op_var_id).unwrap();
let new_operand = Operand::Var(Variable::new(updated_var_id));
new_rvalue = Rvalue::Call(new_operand, vector.clone());
}
}
_ => {}
}
new_rvalue
environment
}

/// this struct is a bit of a hack, because we also use it for
Expand Down
7 changes: 3 additions & 4 deletions crates/forge_analyzer/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,13 @@ impl<'cx, C: Runner<'cx>> Interp<'cx, C> {
)
}
// push consts into vec if both are consts
(Value::Const(const_value1), Value::Const(const_value2)) => {
self.add_value_with_projection(
(Value::Const(const_value1), Value::Const(const_value2)) => self
.add_value_with_projection(
defid_block,
varid,
Value::Phi(vec![const_value1, const_value2]),
projections,
);
}
),
(Value::Object(exist_var), Value::Object(new_var)) => {
// store projection values that are transferred
let mut projections_transferred = vec![];
Expand Down
11 changes: 1 addition & 10 deletions crates/forge_analyzer/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ use std::slice;

use forge_utils::create_newtype;
use forge_utils::FxHashMap;
use itertools::Itertools;
use smallvec::smallvec;
use smallvec::smallvec_inline;
use smallvec::SmallVec;
use swc_core::common::SyntaxContext;
use swc_core::ecma::ast;
use swc_core::ecma::ast::BinaryOp;
use swc_core::ecma::ast::Bool;
use swc_core::ecma::ast::JSXText;
use swc_core::ecma::ast::Lit;
use swc_core::ecma::ast::Null;
Expand Down Expand Up @@ -140,7 +137,7 @@ pub struct Body {
pub blocks: TiVec<BasicBlockId, BasicBlock>,
pub vars: TiVec<VarId, VarKind>,
pub values: FxHashMap<DefId, Value>,
pub ident_to_local: FxHashMap<Id, VarId>,
ident_to_local: FxHashMap<Id, VarId>,
pub def_id_to_vars: FxHashMap<DefId, VarId>,
pub class_instantiations: HashMap<DefId, DefId>,
predecessors: OnceCell<TiVec<BasicBlockId, SmallVec<[BasicBlockId; 2]>>>,
Expand Down Expand Up @@ -344,12 +341,6 @@ impl Body {
self.vars.push_and_get_key(kind)
}

#[inline]
pub(crate) fn add_insts(&mut self, new_insts: Vec<Inst>, bb: BasicBlockId) {
let block = self.blocks.get_mut(bb).unwrap();
block.insts = new_insts;
}

#[inline]
pub(crate) fn get_defid_from_var(&self, varid: VarId) -> Option<DefId> {
match self.vars.get(varid)? {
Expand Down
8 changes: 5 additions & 3 deletions test-apps/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"validate": "tsc --noEmit --project ./tsconfig.json"
},
"devDependencies": {
"@forge/api": "^3.8.0",
"@forge/ui": "^1.11.1",
"@types/node": "^20.14.10",
"@types/node": "20.11.20",
"@types/react": "18.2.57",
"typescript": "5.3.3"
},
"dependencies": {
"@forge/api": "3.2.0",
"@forge/ui": "1.11.0"
}
}

0 comments on commit 7280c35

Please sign in to comment.