Skip to content

Commit

Permalink
Get br_table part of branch monitor working (#153)
Browse files Browse the repository at this point in the history
* Get br_table part of branch monitor working

* Fix commented-out br_table test content
  • Loading branch information
ejrgilbert authored Oct 4, 2024
1 parent 3d0a0bb commit b1a61f9
Show file tree
Hide file tree
Showing 19 changed files with 462 additions and 75 deletions.
8 changes: 8 additions & 0 deletions src/emitter/rewriting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,14 @@ fn emit_value<'a, T: Opcode<'a> + MacroOpcode<'a> + AddLocal>(
}
is_success &= true;
}
Value::U32U32Map { .. } => err.unexpected_error(
false,
Some(format!(
"{err_msg} \
`emit_value` shouldn't be called with a U32U32Map type...should already be handled!"
)),
None,
),
}
is_success
}
Expand Down
9 changes: 1 addition & 8 deletions src/emitter/rewriting/module_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,7 @@ impl<'a, 'b, 'c, 'd, 'e, 'f> ModuleEmitter<'a, 'b, 'c, 'd, 'e, 'f> {
self.mem_tracker.curr_mem_offset += val.len();
true
}
Value::U32 { .. }
| Value::I32 { .. }
| Value::F32 { .. }
| Value::U64 { .. }
| Value::I64 { .. }
| Value::F64 { .. }
| Value::Tuple { .. }
| Value::Boolean { .. } => {
_ => {
err.unexpected_error(
true,
Some(format!(
Expand Down
5 changes: 5 additions & 0 deletions src/emitter/rewriting/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub struct ProbeSpec {
pub struct LocInfo<'a> {
/// static information to be saved in symbol table
pub static_data: HashMap<String, Option<Value>>,
/// dynamic information to be emitted at the probe location
pub dynamic_data: HashMap<String, Option<Value>>,
/// dynamic information corresponding to the operands of this location
pub(crate) args: Vec<Arg>,
pub num_alt_probes: usize,
Expand Down Expand Up @@ -158,6 +160,9 @@ impl<'a> LocInfo<'a> {
// handle static_data
self.static_data.extend(other.static_data.to_owned());

// handle dynamic_data
self.dynamic_data.extend(other.dynamic_data.to_owned());

// handle args
if !self.args.is_empty() {
if !other.args.is_empty() {
Expand Down
16 changes: 15 additions & 1 deletion src/emitter/rewriting/rules/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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, $docs:expr)*) => {
($($op:ident, $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 @@ -249,6 +249,9 @@ impl Event for OpcodeEvent {
val: targets.default(),
}),
);

let mut target_map = HashMap::new();

for (i, target) in targets.targets().enumerate() {
if let Ok(target) = target {
loc_info.static_data.insert(
Expand All @@ -258,8 +261,19 @@ impl Event for OpcodeEvent {
val: target,
}),
);
target_map.insert(i as u32, target);
}
}
loc_info.dynamic_data.insert(
"targets".to_string(),
Some(Value::U32U32Map {
ty: DataType::Map {
key_ty: Box::new(DataType::U32),
val_ty: Box::new(DataType::U32),
},
val: Box::new(target_map),
}),
);
loc_info.add_probes(self.probe_spec(), &self.probes);
}
}
Expand Down
258 changes: 258 additions & 0 deletions src/emitter/rewriting/visiting_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::emitter::rewriting::rules::{Arg, LocInfo, ProbeSpec, Provider, WhammP
use crate::emitter::rewriting::{block_type_to_wasm, emit_expr, whamm_type_to_wasm_global};
use crate::emitter::rewriting::{emit_stmt, print_report_all, Emitter};
use crate::libraries::core::maps::map_adapter::MapLibAdapter;
use std::collections::HashMap;

use crate::generator::types::ExprFolder;
use crate::libraries::core::io::io_adapter::IOAdapter;
Expand Down Expand Up @@ -134,6 +135,259 @@ impl<'a, 'b, 'c, 'd, 'e, 'f, 'g> VisitingEmitter<'a, 'b, 'c, 'd, 'e, 'f, 'g> {
}
}

pub(crate) fn emit_dynamic_compiler_data(
&mut self,
data: &HashMap<String, Option<Value>>,
err: &mut ErrorGen,
) -> bool {
let mut is_success = true;
for (name, val) in data.iter() {
let var_id = Expr::VarId {
definition: Definition::CompilerDynamic,
name: name.clone(),
loc: None,
};
let mut block: Vec<Statement> = match val {
Some(Value::U32 { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::U32,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::U32 {
ty: DataType::U32,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::I32 { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::I32,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::I32 {
ty: DataType::I32,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::F32 { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::F32,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::F32 {
ty: DataType::F32,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::U64 { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::U64,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::U64 {
ty: DataType::U64,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::I64 { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::I64,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::I64 {
ty: DataType::I64,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::F64 { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::F64,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::F64 {
ty: DataType::F64,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::Boolean { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::Boolean,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::Boolean {
ty: DataType::Boolean,
val: *val,
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::Str { val, .. }) => {
// create a declaration
let decl = Statement::Decl {
ty: DataType::Str,
var_id: var_id.clone(),
loc: None,
};
// create an assignment
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::Str {
ty: DataType::Str,
val: val.clone(),
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::Tuple { vals, ty }) => {
// create a declaration
let decl = Statement::Decl {
ty: ty.clone(),
var_id: var_id.clone(),
loc: None,
};
// create assignments
let assign = Statement::Assign {
var_id: var_id.clone(),
expr: Expr::Primitive {
val: Value::Tuple {
ty: ty.clone(),
vals: vals.clone(),
},
loc: None,
},
loc: None,
};
vec![decl, assign]
}
Some(Value::U32U32Map { val, ty }) => {
// create a declaration
let decl = Statement::Decl {
ty: ty.clone(),
var_id: var_id.clone(),
loc: None,
};
// create assignments
let mut stmts = vec![decl];
for (key, val) in val.iter() {
stmts.push(Statement::SetMap {
map: var_id.clone(),
key: Expr::Primitive {
val: Value::U32 {
ty: DataType::U32,
val: *key,
},
loc: None,
},
val: Expr::Primitive {
val: Value::U32 {
ty: DataType::U32,
val: *val,
},
loc: None,
},
loc: None,
});
}
stmts
}
None => {
vec![]
} // skip
};
for stmt in block.iter_mut() {
is_success &= emit_stmt(
stmt,
&mut self.app_iter,
self.table,
self.mem_tracker,
self.map_lib_adapter,
self.report_var_metadata,
UNEXPECTED_ERR_MSG,
err,
);
}
}
is_success
}

pub(crate) fn save_args(&mut self, args: &[Arg]) -> bool {
// No opcodes should have been emitted in the module yet!
// So, we can just save off the first * items in the stack as the args
Expand Down Expand Up @@ -278,6 +532,10 @@ impl<'a, 'b, 'c, 'd, 'e, 'f, 'g> VisitingEmitter<'a, 'b, 'c, 'd, 'e, 'f, 'g> {
});

// reset dynamic_data
loc_info.dynamic_data.iter().for_each(|(symbol_name, ..)| {
self.table.remove_record(symbol_name);
});

for i in 0..loc_info.args.len() {
let arg_name = format!("arg{}", i);
self.table.remove_record(&arg_name);
Expand Down
3 changes: 2 additions & 1 deletion src/generator/init_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ impl WhammVisitorMut<bool> for InitGenerator<'_, '_, '_, '_, '_, '_, '_, '_> {
| Value::U64 { .. }
| Value::I64 { .. }
| Value::F64 { .. }
| Value::Boolean { .. } => {
| Value::Boolean { .. }
| Value::U32U32Map { .. } => {
// ignore, will not have a string to emit
true
}
Expand Down
Loading

0 comments on commit b1a61f9

Please sign in to comment.