Skip to content

Commit

Permalink
Merge pull request #21 from ejrgilbert/task/refactor_probe_to_mode
Browse files Browse the repository at this point in the history
Refactor probe -> mode
  • Loading branch information
ejrgilbert authored May 23, 2024
2 parents 7297504 + 38d44dd commit 95ee474
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 68 deletions.
32 changes: 16 additions & 16 deletions src/behavior/builder_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ impl BehaviorTreeBuilder<'_> {
if let Some(provider) = self.ast.get_mut(&self.curr_provider_name) {
if let Some(package) = provider.get_mut(&self.curr_package_name) {
if let Some(event) = package.get_mut(&self.curr_event_name) {
if let Some(probes) = event.get_mut(&probe.name) {
if let Some(probes) = event.get_mut(&probe.mode) {
probes.push((*probe).clone());
} else {
event.insert(probe.name.clone(), vec![(*probe).clone()]);
event.insert(probe.mode.clone(), vec![(*probe).clone()]);
}
}

Expand Down Expand Up @@ -170,16 +170,16 @@ impl BehaviorTreeBuilder<'_> {
self.tree.sequence(self.err);
}

self.visit_probe_ty(event, "before");
self.visit_probe_ty(event, "alt");
self.visit_probe_ty(event, "after");
self.visit_probe_mode(event, "before");
self.visit_probe_mode(event, "alt");
self.visit_probe_mode(event, "after");

if event.probe_map.len() > 1 {
self.tree.exit_sequence(self.err);
}
}

fn visit_probe_ty(&mut self, event: &Event, ty: &str) {
fn visit_probe_mode(&mut self, event: &Event, ty: &str) {
if let Some(probes) = event.probe_map.get(ty) {
if let Some(probe) = probes.get(0) {
// just grab the first one and emit behavior (the behavior includes a loop
Expand All @@ -198,8 +198,8 @@ impl BehaviorTreeBuilder<'_> {
}, self.err)
.sequence(self.err)
.fallback(self.err)
.decorator(DecoratorType::IsProbeType {
probe_type: "alt".to_string()
.decorator(DecoratorType::IsProbeMode {
probe_mode: "alt".to_string()
}, self.err)
.remove_orig(self.err)
.exit_decorator(self.err)
Expand All @@ -217,21 +217,21 @@ impl BehaviorTreeBuilder<'_> {
.exit_decorator(self.err)
.fallback(self.err)
// before behavior
.decorator(DecoratorType::IsProbeType {
probe_type: "before".to_string()
.decorator(DecoratorType::IsProbeMode {
probe_mode: "before".to_string()
}, self.err);

self.emit_bytecode_probe_before_body(probe);
self.tree.exit_decorator(self.err)
// alt behavior
.decorator(DecoratorType::IsProbeType {
probe_type: "alt".to_string()
.decorator(DecoratorType::IsProbeMode {
probe_mode: "alt".to_string()
}, self.err);
self.emit_bytecode_probe_alt_body(probe);
self.tree.exit_decorator(self.err)
// after behavior
.decorator(DecoratorType::IsProbeType {
probe_type: "after".to_string()
.decorator(DecoratorType::IsProbeMode {
probe_mode: "after".to_string()
}, self.err);
self.emit_bytecode_probe_after_body(probe);
self.tree.exit_decorator(self.err)
Expand Down Expand Up @@ -395,12 +395,12 @@ impl WhammVisitor<()> for BehaviorTreeBuilder<'_> {

fn visit_probe(&mut self, probe: &Probe) -> () {
trace!("Entering: BehaviorTreeBuilder::visit_probe");
self.context_name += &format!(":{}", probe.name.clone());
self.context_name += &format!(":{}", probe.mode.clone());
self.add_probe_to_ast(probe);

self.tree.action_with_child(ActionWithChildType::EnterProbe {
context: self.context_name.clone(),
probe_name: probe.name.clone(),
probe_mode: probe.mode.clone(),
global_names: probe.globals.keys().cloned().collect(),
}, self.err);

Expand Down
10 changes: 5 additions & 5 deletions src/behavior/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ pub enum Node {

#[derive(Debug)]
pub enum DecoratorType {
IsProbeType {
probe_type: String
IsProbeMode {
probe_mode: String
},
HasAltCall,
PredIs {
Expand Down Expand Up @@ -486,7 +486,7 @@ pub enum ActionWithChildType {
},
EnterProbe {
context: String,
probe_name: String,
probe_mode: String,
global_names: Vec<String>
}
}
Expand Down Expand Up @@ -525,7 +525,7 @@ pub trait BehaviorVisitor<T> {
fn visit_decorator(&mut self, node: &Node) -> T {
if let Node::Decorator { ty, ..} = node {
match ty {
DecoratorType::IsProbeType {..} => self.visit_is_probe_type(node),
DecoratorType::IsProbeMode {..} => self.visit_is_probe_mode(node),
DecoratorType::HasAltCall {..} => self.visit_has_alt_call(node),
DecoratorType::PredIs {..} => self.visit_pred_is(node)
}
Expand Down Expand Up @@ -566,7 +566,7 @@ pub trait BehaviorVisitor<T> {
}

// Decorator nodes
fn visit_is_probe_type(&mut self, node: &Node) -> T;
fn visit_is_probe_mode(&mut self, node: &Node) -> T;
fn visit_has_alt_call(&mut self, node: &Node) -> T;
fn visit_pred_is(&mut self, node: &Node) -> T;

Expand Down
10 changes: 5 additions & 5 deletions src/behavior/visualize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ impl BehaviorVisitor<()> for Visualizer<'_> {
}
}

fn visit_is_probe_type(&mut self, node: &TreeNode) -> () {
fn visit_is_probe_mode(&mut self, node: &TreeNode) -> () {
if let TreeNode::Decorator { id, ty, parent, child } = node {
if let DecoratorType::IsProbeType {probe_type} = ty {
self.emit_decorator_node(id, &format!("IsProbeType_{}", probe_type.replace(":", "_")));
if let DecoratorType::IsProbeMode {probe_mode} = ty {
self.emit_decorator_node(id, &format!("IsProbeMode_{}", probe_mode.replace(":", "_")));
self.emit_edge(parent, id);

if let Some(node) = self.tree.get_node(child.clone()) {
Expand Down Expand Up @@ -253,8 +253,8 @@ impl BehaviorVisitor<()> for Visualizer<'_> {

fn visit_enter_probe(&mut self, node: &TreeNode) -> () {
if let TreeNode::ActionWithChild { id, ty, parent, child } = node {
if let ActionWithChildType::EnterProbe { probe_name, .. } = ty {
self.emit_special_action_node(id, &format!("EnterProbe_{}", probe_name.replace(":", "_")));
if let ActionWithChildType::EnterProbe { probe_mode, .. } = ty {
self.emit_special_action_node(id, &format!("EnterProbe_{}", probe_mode.replace(":", "_")));
self.emit_edge(parent, id);

if let Some(node) = self.tree.get_node(child.clone()) {
Expand Down
2 changes: 1 addition & 1 deletion src/generator/init_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl WhammVisitorMut<bool> for InitGenerator<'_> {
_ => {}
}
// let mut is_success = self.emitter.emit_probe(probe);
self.context_name += &format!(":{}", probe.name.clone());
self.context_name += &format!(":{}", probe.mode.clone());
let mut is_success = true;

// visit fns
Expand Down
32 changes: 16 additions & 16 deletions src/generator/instr_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct InstrGenerator<'a, 'b> {
pub curr_provider_name: String,
pub curr_package_name: String,
pub curr_event_name: String,
pub curr_probe_name: String,
pub curr_probe_mode: String,
pub curr_probe: Option<Probe>
}
impl InstrGenerator<'_, '_> {
Expand Down Expand Up @@ -57,8 +57,8 @@ impl InstrGenerator<'_, '_> {
self.curr_package_name = package.to_string();
if let Some(event) = spec_split.next() {
self.curr_event_name = event.to_string();
if let Some(probe) = spec_split.next() {
self.curr_probe_name = probe.to_string()
if let Some(mode) = spec_split.next() {
self.curr_probe_mode = mode.to_string()
}
}
}
Expand Down Expand Up @@ -155,11 +155,11 @@ impl BehaviorVisitor<bool> for InstrGenerator<'_, '_> {
false
}

fn visit_is_probe_type(&mut self, node: &Node) -> bool {
fn visit_is_probe_mode(&mut self, node: &Node) -> bool {
let mut is_success = true;
if let Node::Decorator { ty, child, .. } = node {
if let DecoratorType::IsProbeType {probe_type} = ty {
if self.curr_probe_name == *probe_type {
if let DecoratorType::IsProbeMode {probe_mode} = ty {
if self.curr_probe_mode == *probe_mode {
if let Some(node) = self.tree.get_node(child.clone()) {
is_success &= self.visit_node(node);
}
Expand Down Expand Up @@ -355,12 +355,12 @@ impl BehaviorVisitor<bool> for InstrGenerator<'_, '_> {
fn visit_enter_probe(&mut self, node: &Node) -> bool {
let mut is_success = true;
if let Node::ActionWithChild { ty, child, .. } = node {
if let ActionWithChildType::EnterProbe { probe_name, global_names, .. } = ty {
if let ActionWithChildType::EnterProbe { probe_mode, global_names, .. } = ty {
// enter probe's scope
if !self.emitter.enter_named_scope(probe_name) {
self.err.unexpected_error(true, Some(format!("{UNEXPECTED_ERR_MSG} Could not find the specified scope by name: `{}`", probe_name)), None);
if !self.emitter.enter_named_scope(probe_mode) {
self.err.unexpected_error(true, Some(format!("{UNEXPECTED_ERR_MSG} Could not find the specified scope by name: `{}`", probe_mode)), None);
}
self.curr_probe_name = probe_name.clone();
self.curr_probe_mode = probe_mode.clone();

// define this probe's compiler variables
for global in global_names {
Expand All @@ -369,15 +369,15 @@ impl BehaviorVisitor<bool> for InstrGenerator<'_, '_> {
Ok(res) => is_success &= res,
}
}
if probe_name == "before" || probe_name == "after" {
if probe_mode == "before" || probe_mode == "after" {
// Perform 'before' and 'after' probe logic
// Must pull the probe by index due to Rust calling constraints...
let probe_list_len = get_probes_from_ast(&self.ast, &self.curr_provider_name, &self.curr_package_name,
&self.curr_event_name, probe_name).len();
&self.curr_event_name, probe_mode).len();
for i in Vec::from_iter(0..probe_list_len).iter() {

if let Some(probe) = get_probe_at_idx(&self.ast, &self.curr_provider_name, &self.curr_package_name,
&self.curr_event_name, probe_name, i) {
&self.curr_event_name, probe_mode, i) {
// make a clone of the current probe per instruction traversal
// this will reset the clone pred/body for each instruction!
let mut probe_cloned = probe.clone();
Expand All @@ -394,12 +394,12 @@ impl BehaviorVisitor<bool> for InstrGenerator<'_, '_> {
is_success &= self.visit_node(node);
}
}
} else if probe_name == "alt" {
} else if probe_mode == "alt" {
// Perform 'alt' probe logic
let probe_list = get_probes_from_ast(&self.ast, &self.curr_provider_name, &self.curr_package_name,
&self.curr_event_name, probe_name);
&self.curr_event_name, probe_mode);
if probe_list.len() > 1 {
warn!("There is more than one probe for probe type '{}'. So only emitting first probe, ignoring rest.", probe_name)
warn!("There is more than one probe for probe type '{}'. So only emitting first probe, ignoring rest.", probe_mode)
}
// make a clone of the first probe per instruction traversal
// this will reset the clone pred/body for each instruction!
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn run_instr(app_wasm_path: String, whammy_path: String, output_wasm_path: Strin
curr_provider_name: "".to_string(),
curr_package_name: "".to_string(),
curr_event_name: "".to_string(),
curr_probe_name: "".to_string(),
curr_probe_mode: "".to_string(),
curr_probe: None,
};
instr.run(&behavior_tree);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/print_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl WhammVisitor<String> for AsStrVisitor {
fn visit_probe(&mut self, probe: &Probe) -> String {
let mut s = "".to_string();

s += &format!("{} `{}` probe {{{}", self.get_indent(), probe.name, NL);
s += &format!("{} `{}` probe {{{}", self.get_indent(), probe.mode, NL);
self.increase_indent();

// print fns
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ wasm::call:alt /
let probe = event.probe_map.get("alt").unwrap().get(0).unwrap();
assert_eq!(0, probe.globals.len());
assert_eq!(0, probe.fns.len());
assert_eq!("alt", probe.name);
assert_eq!("alt", probe.mode);

// probe predicate
assert!(probe.predicate.is_some());
Expand Down
32 changes: 16 additions & 16 deletions src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ impl Whamm {
"table_copy".to_string()
),
];
let wasm_bytecode_probe_types = vec![
let wasm_bytecode_probe_modes = vec![
(
ProvidedFunctionality {
name: "before".to_string(),
Expand Down Expand Up @@ -804,7 +804,7 @@ impl Whamm {

// Build out the wasm_bytecode_map
for (info, name) in wasm_bytecode_events {
wasm_bytecode_map.insert(name, (info.clone(), wasm_bytecode_probe_types.clone()));
wasm_bytecode_map.insert(name, (info.clone(), wasm_bytecode_probe_modes.clone()));
}

self.provided_probes.insert("wasm".to_string(), (
Expand Down Expand Up @@ -1217,7 +1217,7 @@ impl Whammy {
return Ok(());
}

/// Iterates over all of the matched providers, packages, events, and probe names
/// Iterates over all of the matched providers, packages, events, and probe mode names
/// to add a copy of the user-defined Probe for each of them.
pub fn add_probe(&mut self, provided_probes: &ProvidedProbes,
probe_spec: &ProbeSpec, predicate: Option<Expr>, body: Option<Vec<Statement>>) -> Result<(), WhammError> {
Expand Down Expand Up @@ -1545,7 +1545,7 @@ impl Event {

#[derive(Clone, Debug)]
pub struct Probe {
pub name: String,
pub mode: String,
pub loc: Option<Location>,
pub fns: Vec<(ProvidedFunctionality, Fn)>, // Comp-provided
pub globals: HashMap<String, (ProvidedFunctionality, Global)>, // Comp-provided
Expand All @@ -1554,11 +1554,11 @@ pub struct Probe {
pub body: Option<Vec<Statement>>
}
impl Probe {
pub fn new(name: String, loc: Option<Location>, predicate: Option<Expr>, body: Option<Vec<Statement>>) -> Self {
let fns = Probe::get_provided_fns(&name);
let globals = Probe::get_provided_globals(&name);
pub fn new(mode: String, loc: Option<Location>, predicate: Option<Expr>, body: Option<Vec<Statement>>) -> Self {
let fns = Probe::get_provided_fns(&mode);
let globals = Probe::get_provided_globals(&mode);
Probe {
name,
mode,
loc,
fns,
globals,
Expand All @@ -1568,23 +1568,23 @@ impl Probe {
}
}

fn get_provided_fns(_name: &String) -> Vec<(ProvidedFunctionality, Fn)> {
fn get_provided_fns(_mode: &String) -> Vec<(ProvidedFunctionality, Fn)> {
vec![]
}

fn get_provided_globals(_name: &String) -> HashMap<String, (ProvidedFunctionality, Global)> {
fn get_provided_globals(_mode: &String) -> HashMap<String, (ProvidedFunctionality, Global)> {
HashMap::new()
}

/// Get the Probe names that match the passed glob pattern
pub fn get_matches(provided_probes: &ProvidedProbes, provider: &str, package: &str, event: &str, probe_patt: &str) -> Vec<(ProvidedFunctionality, String)> {
let glob = Pattern::new(&probe_patt.to_lowercase()).unwrap();
/// Get the Probe modes that match the passed glob pattern
pub fn get_matches(provided_probes: &ProvidedProbes, provider: &str, package: &str, event: &str, mode_patt: &str) -> Vec<(ProvidedFunctionality, String)> {
let glob = Pattern::new(&mode_patt.to_lowercase()).unwrap();

let mut matches = vec![];

for (info, p_name) in provided_probes.get(provider).unwrap().1.get(package).unwrap().1.get(event).unwrap().1.iter() {
if glob.matches(&p_name.to_lowercase()) {
matches.push((info.clone(), p_name.clone()));
for (info, m_name) in provided_probes.get(provider).unwrap().1.get(package).unwrap().1.get(event).unwrap().1.iter() {
if glob.matches(&m_name.to_lowercase()) {
matches.push((info.clone(), m_name.clone()));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/verifier/builder_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,20 @@ impl SymbolTableBuilder<'_> {
}

fn add_probe(&mut self, probe: &Probe) {
if self.table.lookup(&probe.name).is_some() {
if self.table.lookup(&probe.mode).is_some() {
// This should never be the case since it's controlled by the compiler!
self.err.unexpected_error(true, Some(UNEXPECTED_ERR_MSG.to_string()), None);
}

// create record
let probe_rec = Record::Probe {
name: probe.name.clone(),
mode: probe.mode.clone(),
fns: vec![],
globals: vec![],
};

// Add probe to scope
let id = self.table.put(probe.name.clone(), probe_rec);
let id = self.table.put(probe.mode.clone(), probe_rec);

// Add probe to current event record
match self.table.get_record_mut(&self.curr_event.unwrap()) {
Expand All @@ -210,7 +210,7 @@ impl SymbolTableBuilder<'_> {
self.curr_probe = Some(id.clone());

// set scope name and type
self.table.set_curr_scope_info(probe.name.clone(), ScopeType::Probe);
self.table.set_curr_scope_info(probe.mode.clone(), ScopeType::Probe);
}

fn add_fn(&mut self, f: &Fn) {
Expand Down
2 changes: 1 addition & 1 deletion src/verifier/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ pub enum Record {
probes: Vec<usize>
},
Probe {
name: String,
mode: String,
fns: Vec<usize>,
globals: Vec<usize>
},
Expand Down
Loading

0 comments on commit 95ee474

Please sign in to comment.