Skip to content

Commit

Permalink
Add count_insns feature to VM
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Oct 19, 2023
1 parent 6553e9c commit d4c20e7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions ncc/count_insns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUST_BACKTRACE=1 cargo run -- $* && cd ../vm && cargo run --features count_insns ../ncc/out.asm && cd ../vm
3 changes: 3 additions & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ edition = "2021"
sdl2 = "0.35.2"
libc = "0.2"

[features]
count_insns = []

[profile.dev]
debug = true
opt-level = 1
Expand Down
9 changes: 9 additions & 0 deletions vm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mod vm;
mod sys;
mod asm;
mod utils;

extern crate sdl2;
extern crate libc;
Expand All @@ -17,6 +18,7 @@ use std::sync::{Arc, Mutex};
use crate::vm::{VM, Value, MemBlock, ExitReason};
use crate::asm::{Assembler};
use crate::sys::{SysState};
use crate::utils::{thousands_sep};

/// Command-line options
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -155,5 +157,12 @@ fn main()
let mut mutex = SysState::get_mutex(vm);
let ret_val = run_program(&mut mutex);

#[cfg(feature = "count_insns")]
{
let mut vm = mutex.lock().unwrap();
let insn_count = vm.get_insn_count();
println!("insns executed: {}", thousands_sep(insn_count));
}

exit(ret_val.as_i32());
}
18 changes: 18 additions & 0 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Produce a string with comma separator for thousands for an integer
pub fn thousands_sep(n: u64) -> String
{
let num_chars: Vec<char> = n.to_string().chars().rev().collect();

let mut chars_sep = Vec::new();

for idx in 0..num_chars.len() {
chars_sep.push(num_chars[idx]);
if (idx % 3) == 2 && idx < num_chars.len() - 1 {
chars_sep.push(',');
}
}

let num_str: String = chars_sep.into_iter().rev().collect();

num_str
}
17 changes: 17 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ pub struct VM

// List of stack frames (activation records)
frames: Vec<StackFrame>,

// Count of executed instructions
#[cfg(feature = "count_insns")]
insn_count: u64,
}

impl VM
Expand All @@ -545,9 +549,17 @@ impl VM
heap,
stack: Vec::default(),
frames: Vec::default(),
#[cfg(feature = "count_insns")]
insn_count: 0,
}
}

#[cfg(feature = "count_insns")]
pub fn get_insn_count(&self) -> u64
{
self.insn_count
}

pub fn stack_size(&self) -> usize
{
self.stack.len()
Expand Down Expand Up @@ -676,6 +688,11 @@ impl VM
// For each instruction to execute
loop
{
#[cfg(feature = "count_insns")]
{
self.insn_count += 1;
}

if pc >= self.code.len() {
panic!("pc outside bounds of code space")
}
Expand Down

0 comments on commit d4c20e7

Please sign in to comment.