Skip to content

Commit

Permalink
Add some features to enable implementing an opcode category monitor (#…
Browse files Browse the repository at this point in the history
…196)

* Add some features to enable implementing an opcode category monitor

* Remove rust exp file, until we need it

* fmt/clippy
  • Loading branch information
ejrgilbert authored Feb 21, 2025
1 parent 7875ecd commit cb769de
Show file tree
Hide file tree
Showing 19 changed files with 565 additions and 349 deletions.
9 changes: 8 additions & 1 deletion src/emitter/rewriting/rules/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct OpcodeEvent {
probes: HashMap<WhammModeKind, Vec<SimpleProbe>>,
}
macro_rules! define_opcode_event {
($($op:ident, $name:ident, $num_args:expr, $imms:expr, $globals:expr, $fns:expr, $supported_modes:expr, $req_map:expr, $docs:expr)*) => {
($($op:ident, $category:expr, $name:ident, $num_args:expr, $imms:expr, $globals:expr, $fns:expr, $supported_modes:expr, $req_map:expr, $docs:expr)*) => {
impl FromStr for OpcodeEvent {
fn from_str(name: &str) -> Self {
match name {
Expand Down Expand Up @@ -533,6 +533,13 @@ impl OpcodeEvent {
impl Event for OpcodeEvent {
fn get_loc_info(&self, app_wasm: &Module, instr: &Operator) -> Option<LocInfo> {
let mut loc_info = LocInfo::new();
// define the opcode category
loc_info.static_data.insert(
"category".to_string(),
Some(Value::Str {
val: self.kind.category().to_string(),
}),
);

match self.kind {
OpcodeEventKind::Unreachable { .. } => {
Expand Down
40 changes: 37 additions & 3 deletions src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::parser::types::{
BinOp, Block, DataType, Definition, Expr, Fn, Global, ProvidedFunction, Script, Statement,
UnOp, Value, Whamm, WhammVisitorMut,
};
use crate::verifier::types::Record;
use log::{debug, trace, warn};
use orca_wasm::ir::id::FunctionID;
use std::collections::HashMap;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub trait GeneratingVisitor: WhammVisitorMut<bool> {
fn enter_named_scope(&mut self, name: &str);
fn enter_scope(&mut self);
fn exit_scope(&mut self);
fn lookup_var_mut(&mut self, name: &str) -> Option<&mut Record>;
fn visit_stmts(&mut self, stmts: &mut [Statement]) -> bool {
let mut is_success = true;
stmts.iter_mut().for_each(|stmt| {
Expand Down Expand Up @@ -391,9 +393,41 @@ impl<T: GeneratingVisitor> WhammVisitorMut<bool> for T {
is_success
}
Expr::Primitive { val, .. } => self.visit_value(val),
Expr::VarId { .. } => {
// ignore, will not have a string to emit
true
Expr::VarId {
definition, name, ..
} => {
if matches!(definition, Definition::CompilerStatic) {
// (Hacky to fix the borrow issues with rust)
let mut val = {
if let Some(Record::Var {
value: Some(val), ..
}) = self.lookup_var_mut(name)
{
val.clone()
} else {
// ignore, nothing to emit here
return true;
}
};

if let Value::Str { .. } = val {
self.emit_string(&mut val);
}

// look back up to overwrite the value!
let Some(Record::Var {
value: Some(old_val),
..
}) = self.lookup_var_mut(name)
else {
panic!("Unable to find the definition for a static compiler variable!")
};
*old_val = val;
true
} else {
// ignore, will not have a string to emit
true
}
}
Expr::MapGet { map, key, .. } => {
let mut is_success = true;
Expand Down
4 changes: 4 additions & 0 deletions src/generator/rewriting/init_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::emitter::module_emitter::ModuleEmitter;
use crate::generator::GeneratingVisitor;
use crate::lang_features::report_vars::LocationData;
use crate::parser::types::{DataType, Fn, Value, Whamm, WhammVisitorMut};
use crate::verifier::types::Record;
use orca_wasm::ir::id::FunctionID;

/// Serves as the first phase of instrumenting a module by setting up
Expand Down Expand Up @@ -86,4 +87,7 @@ impl GeneratingVisitor for InitGenerator<'_, '_, '_, '_, '_, '_, '_, '_, '_> {
fn exit_scope(&mut self) {
self.emitter.exit_scope(self.err);
}
fn lookup_var_mut(&mut self, name: &str) -> Option<&mut Record> {
self.emitter.table.lookup_var_mut(name, &None, self.err)
}
}
3 changes: 3 additions & 0 deletions src/generator/wizard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,7 @@ impl GeneratingVisitor for WizardGenerator<'_, '_, '_, '_, '_, '_, '_, '_, '_, '
fn exit_scope(&mut self) {
self.emitter.exit_scope(self.err);
}
fn lookup_var_mut(&mut self, name: &str) -> Option<&mut Record> {
self.emitter.table.lookup_var_mut(name, &None, self.err)
}
}
8 changes: 5 additions & 3 deletions src/lang_features/libraries/core/maps/map_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ impl MapLibAdapter {
// insert map
("insert_i32_i32".to_string(), 0),
("insert_i32_string".to_string(), 0),
("insert_string_i32".to_string(), 0),
("insert_i32i32i32tuple_i32".to_string(), 0),
// get from map
("get_i32_i32".to_string(), 0),
("get_i32_string".to_string(), 0),
("get_string_i32".to_string(), 0),
("get_i32i32i32tuple_i32".to_string(), 0),
// printing maps
("print_map".to_string(), 0),
Expand Down Expand Up @@ -287,7 +289,7 @@ impl MapLibAdapter {
err.type_check_error(
true,
format!(
"MapLibAdapter.map_create_fname: Unsupported map type: {:?} -> {:?}",
"MapLibAdapter.map_create_fname: Unsupported map type: {:?} -> {:?}, need function with name '{fname}'",
key, val
),
&None,
Expand All @@ -306,7 +308,7 @@ impl MapLibAdapter {
err.type_check_error(
true,
format!(
"MapLibAdapter.map_insert_fname: Unsupported map type: {:?} -> {:?}",
"MapLibAdapter.map_insert_fname: Unsupported map type: {:?} -> {:?}, need function with name '{fname}'",
key, val
),
&None,
Expand All @@ -325,7 +327,7 @@ impl MapLibAdapter {
err.type_check_error(
true,
format!(
"MapLibAdapter.map_get_fname: Unsupported map type: {:?} -> {:?}",
"MapLibAdapter.map_get_fname: Unsupported map type: {:?} -> {:?}, need function with name '{fname}'",
key, val
),
&None,
Expand Down
Loading

0 comments on commit cb769de

Please sign in to comment.