Skip to content

Commit

Permalink
Task/check if maps (#155)
Browse files Browse the repository at this point in the history
* merge conflicts with master

* fmt

* remove commented-out code
  • Loading branch information
ejrgilbert authored Oct 4, 2024
1 parent b1a61f9 commit da22ccd
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 86 deletions.
6 changes: 4 additions & 2 deletions src/generator/init_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ impl InitGenerator<'_, '_, '_, '_, '_, '_, '_, '_> {
}
fn on_startup(&mut self) {
self.create_start();
self.create_global_map_init();
self.create_print_map_meta();
if self.emitter.map_lib_adapter.is_used {
self.create_global_map_init();
self.create_print_map_meta();
}
self.create_print_global_meta();
}
fn create_start(&mut self) {
Expand Down
88 changes: 5 additions & 83 deletions src/generator/instr_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ impl<'b> InstrGenerator<'_, 'b, '_, '_, '_, '_, '_, '_> {

is_success &= self.save_args();
//after saving args, we run the check if we need to initialize global maps
// TODO -- only inject this IF NEEDED (not all scripts need global init)
self.emit_global_map_init();
// only inject this IF NEEDED (not all scripts need global init)
if self.emitter.map_lib_adapter.is_used {
self.emit_global_map_init();
}
self.configure_probe_mode();

// Now we know we're going to insert the probe, let's define
Expand Down Expand Up @@ -469,91 +471,11 @@ impl<'b> InstrGenerator<'_, 'b, '_, '_, '_, '_, '_, '_> {
//now set the new key value for the new maps
map_meta_str.insert(*key, val);
}
let mut is_success = self.setup_global_map_init();
is_success &= self.setup_print_global_meta(&var_meta_str);
let mut is_success = self.setup_print_global_meta(&var_meta_str);
is_success &= self.setup_print_map_meta(&map_meta_str);
is_success
}

fn setup_global_map_init(&mut self) -> bool {
//first, we need to create the maps in global_map_init - where all the other maps are initialized
let global_map_init_id = match self
.emitter
.app_iter
.module
.functions
.get_local_fid_by_name("global_map_init")
{
Some(start_id) => start_id,
None => {
self.err.add_error(ErrorGen::get_unexpected_error(
true,
Some(format!(
"{UNEXPECTED_ERR_MSG} \
No start function found in the module!"
)),
None,
));
return false;
}
};

// set up the global_map_init function for insertions
let mut global_map_init = match self
.emitter
.app_iter
.module
.functions
.get_fn_modifier(global_map_init_id)
{
Some(func) => func,
None => {
self.err.add_error(ErrorGen::get_unexpected_error(
true,
Some(format!(
"{UNEXPECTED_ERR_MSG} \
No 'global_map_init' function found in the module!"
)),
None,
));
return false;
}
};
//now set up the actual module editing
global_map_init.before_at(Location::Module {
func_idx: global_map_init_id, // not used
instr_idx: 0,
});

// set up the output header!
let header = Metadata::get_csv_header();
//first, emit the string to data section
// todo(maps) factor this logic out to a function call!
let data_id = self.emitter.app_iter.module.data.len();
let header_bytes = header.as_bytes().to_owned();
let data_segment = DataSegment {
data: header_bytes,
kind: DataSegmentKind::Active {
memory_index: self.emitter.mem_tracker.mem_id,
offset_expr: InitExpr::Value(OrcaValue::I32(
self.emitter.mem_tracker.curr_mem_offset as i32,
)),
},
};
self.emitter.app_iter.module.data.push(data_segment);

// save the memory addresses/lens, so they can be used as appropriate
self.emitter.mem_tracker.emitted_strings.insert(
header.clone(),
StringAddr {
data_id: data_id as u32,
mem_offset: self.emitter.mem_tracker.curr_mem_offset,
len: header.len(),
},
);
true
}

/// set up the print_global_meta function for insertions
fn setup_print_global_meta(&mut self, var_meta_str: &HashMap<u32, String>) -> bool {
// get the function
Expand Down
6 changes: 5 additions & 1 deletion src/libraries/core/io/io_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const PUTI: &str = "puti";

// //this is the code that knows which functions to call in lib.rs based on what is in the AST -> will be in emitter folder eventually
pub struct IOAdapter {
pub is_used: bool,
// func_name -> fid
funcs: HashMap<String, u32>,
}
Expand All @@ -31,7 +32,10 @@ impl IOAdapter {
pub fn new() -> Self {
let funcs = HashMap::from([(PUTC.to_string(), 0), (PUTI.to_string(), 0)]);
//Reserve map 0 for the var metadata map and map 1 for the map metadata map
IOAdapter { funcs }
IOAdapter {
is_used: false,
funcs,
}
}

pub fn putsln<'a, T: Opcode<'a> + MacroOpcode<'a> + AddLocal>(
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/core/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ impl LibPackage for IOPackage {
fn add_fid_to_adapter(&mut self, fname: &str, fid: u32) {
self.adapter.add_fid(fname, fid);
}
fn set_adapter_usage(&mut self, is_used: bool) {
self.adapter.is_used = is_used;
}
}
impl WhammVisitor<bool> for IOPackage {
fn visit_whamm(&mut self, whamm: &Whamm) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/core/maps/map_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::collections::HashMap;
const PRINT_MAP: &str = "print_map";

pub struct MapLibAdapter {
pub is_used: bool,
// func_name -> fid
funcs: HashMap<String, u32>,
map_count: u32,
Expand Down Expand Up @@ -65,6 +66,7 @@ impl MapLibAdapter {
("print_map".to_string(), 0),
]);
MapLibAdapter {
is_used: false,
funcs,
map_count: 0,
init_bool_location: 0,
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/core/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ impl LibPackage for MapLibPackage {
fn add_fid_to_adapter(&mut self, fname: &str, fid: u32) {
self.adapter.add_fid(fname, fid);
}
fn set_adapter_usage(&mut self, is_used: bool) {
self.adapter.is_used = is_used;
}
}
impl WhammVisitor<bool> for MapLibPackage {
fn visit_whamm(&mut self, whamm: &Whamm) -> bool {
Expand Down
1 change: 1 addition & 0 deletions src/libraries/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub trait LibPackage: WhammVisitor<bool> {
fn is_used(&self) -> bool;
fn get_fn_names(&self) -> Vec<String>;
fn add_fid_to_adapter(&mut self, fname: &str, fid: u32);
fn set_adapter_usage(&mut self, is_used: bool);
}
pub trait LibAdapter {
fn get_funcs(&self) -> &HashMap<String, u32>;
Expand Down
1 change: 1 addition & 0 deletions src/libraries/linking/import_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn link_core_lib(
) {
for package in packages.iter_mut() {
package.visit_whamm(ast);
package.set_adapter_usage(package.is_used());
if package.is_used() {
// Read core library Wasm into Orca module
let buff = std::fs::read(core_wasm_path).unwrap();
Expand Down

0 comments on commit da22ccd

Please sign in to comment.