cleanup, optimize. use Arc for reference sharing

floats
Ondřej Hruška 4 years ago
parent d5de189af6
commit 3599cb46c4
Signed by untrusted user: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 6
      crsn/src/asm/data/mod.rs
  2. 5
      crsn/src/asm/data/rd.rs
  3. 4
      crsn/src/asm/data/wr.rs
  4. 2
      crsn/src/asm/instr/op.rs
  5. 4
      crsn/src/asm/mod.rs
  6. 2
      crsn/src/asm/parse/arg_parser.rs
  7. 2
      crsn/src/asm/parse/parse_data.rs
  8. 2
      crsn/src/builtin/defs.rs
  9. 6
      crsn/src/builtin/exec.rs
  10. 1
      crsn/src/builtin/parse.rs
  11. 14
      crsn/src/module/mod.rs
  12. 6
      crsn/src/runtime/program.rs
  13. 26
      crsn/src/runtime/run_thread.rs
  14. 9
      crsn/src/runtime/run_thread/info.rs
  15. 2
      crsn/src/runtime/run_thread/state.rs
  16. 2
      crsn_stacks/src/defs.rs
  17. 7
      crsn_stacks/src/exec.rs
  18. 8
      crsn_stacks/src/parse.rs
  19. 14
      launcher/src/main.rs

@ -3,7 +3,10 @@ use std::fmt::{Debug, Display, Formatter};
use std::fmt; use std::fmt;
pub use mask::Mask; pub use mask::Mask;
pub use rd::Rd;
pub use rd::RdObj;
pub use reg::Register; pub use reg::Register;
pub use wr::Wr;
use crate::asm::data::literal::{as_signed, Value}; use crate::asm::data::literal::{as_signed, Value};
@ -14,11 +17,8 @@ pub mod reg;
pub mod mask; pub mod mask;
mod rd; mod rd;
pub use rd::Rd;
pub use rd::RdObj;
mod wr; mod wr;
pub use wr::Wr;
/// Data source disposition /// Data source disposition
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]

@ -1,8 +1,9 @@
use crate::asm::data::{RdData, Mask, DataDisp, Register};
use crate::asm::data::literal::Value;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::fmt; use std::fmt;
use crate::asm::data::{DataDisp, Mask, RdData, Register};
use crate::asm::data::literal::Value;
/// Data source argument (read-only) /// Data source argument (read-only)
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Clone, Copy, Eq, PartialEq)]
pub struct Rd(pub RdData, pub Mask); pub struct Rd(pub RdData, pub Mask);

@ -1,8 +1,8 @@
use crate::asm::data::{RdData, Mask, DataDisp, WrData, Rd};
use crate::asm::data::literal::Value;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::fmt; use std::fmt;
use crate::asm::data::{DataDisp, Mask, Rd, WrData};
/// Data destination argument (read-write) /// Data destination argument (read-write)
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Clone, Copy, Eq, PartialEq)]
pub struct Wr(WrData, Mask); pub struct Wr(WrData, Mask);

@ -3,7 +3,7 @@ use std::fmt::Debug;
use crate::builtin::defs::BuiltinOp; use crate::builtin::defs::BuiltinOp;
use crate::module::{EvalRes, OpTrait}; use crate::module::{EvalRes, OpTrait};
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;
use crate::runtime::run_thread::{state::RunState, info::ThreadInfo}; use crate::runtime::run_thread::{info::ThreadInfo, state::RunState};
/// A higher level simple opration /// A higher level simple opration
#[derive(Debug)] #[derive(Debug)]

@ -10,8 +10,8 @@ pub mod parse;
pub mod patches; pub mod patches;
/// Parse a program from string and assemble a low level instruction sequence from it. /// Parse a program from string and assemble a low level instruction sequence from it.
pub fn assemble(source: &str, parsers: &'static [Box<dyn CrsnExtension>]) -> Result<Arc<Program>, error::Error> { pub fn assemble(source: &str, parsers: Arc<Vec<Box<dyn CrsnExtension>>>) -> Result<Arc<Program>, error::Error> {
let ops = parse::parse(source, parsers)?; let ops = parse::parse(source, &parsers)?;
trace!("--- Compiled program ---"); trace!("--- Compiled program ---");
for (n, op) in ops.iter().enumerate() { for (n, op) in ops.iter().enumerate() {

@ -1,6 +1,6 @@
use sexp::Sexp; use sexp::Sexp;
use crate::asm::data::{Rd, Wr, RdObj, RdData, Mask}; use crate::asm::data::{Mask, Rd, RdData, RdObj, Wr};
use crate::asm::parse::parse_data::{parse_rd, parse_wr}; use crate::asm::parse::parse_data::{parse_rd, parse_wr};
use crate::asm::parse::sexp_expect::expect_string_atom; use crate::asm::parse::sexp_expect::expect_string_atom;

@ -2,7 +2,7 @@ use std::convert::TryFrom;
use sexp::{Atom, Sexp}; use sexp::{Atom, Sexp};
use crate::asm::data::{DataDisp, WrData, Rd, reg, RdData, Wr}; use crate::asm::data::{DataDisp, Rd, RdData, reg, Wr, WrData};
use crate::asm::data::literal::Label; use crate::asm::data::literal::Label;
use crate::asm::error::Error; use crate::asm::error::Error;
use crate::asm::parse::sexp_expect::expect_string_atom; use crate::asm::parse::sexp_expect::expect_string_atom;

@ -1,4 +1,4 @@
use crate::asm::data::{Rd, Wr, RdObj}; use crate::asm::data::{Rd, RdObj, Wr};
use crate::asm::data::literal::{DebugMsg, Label, RoutineName}; use crate::asm::data::literal::{DebugMsg, Label, RoutineName};
use crate::asm::instr::{Cond, Op}; use crate::asm::instr::{Cond, Op};

@ -1,11 +1,11 @@
use crate::asm::data::{Rd, RdData};
use crate::asm::data::literal::Addr; use crate::asm::data::literal::Addr;
use crate::asm::instr::Cond;
use crate::builtin::defs::BuiltinOp; use crate::builtin::defs::BuiltinOp;
use crate::module::{EvalRes, OpTrait}; use crate::module::{EvalRes, OpTrait};
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;
use crate::runtime::frame::StackFrame; use crate::runtime::frame::StackFrame;
use crate::runtime::run_thread::{state::RunState, ThreadInfo}; use crate::runtime::run_thread::{state::RunState, ThreadInfo};
use crate::asm::data::{Rd, RdData};
use crate::asm::instr::Cond;
impl OpTrait for BuiltinOp { impl OpTrait for BuiltinOp {
fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> { fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> {
@ -142,7 +142,7 @@ impl OpTrait for BuiltinOp {
trace!("Drop object: {:#x}", x); trace!("Drop object: {:#x}", x);
let mut dropped = false; let mut dropped = false;
for ex in info.program.extensions { for ex in info.program.extensions.iter() {
if ex.drop_obj(info, state, x)?.is_some() { if ex.drop_obj(info, state, x)?.is_some() {
dropped = true; dropped = true;
} }

@ -9,7 +9,6 @@ use crate::asm::parse::parse_data::{parse_label, parse_rd};
use crate::asm::parse::sexp_expect::expect_string_atom; use crate::asm::parse::sexp_expect::expect_string_atom;
use crate::builtin::defs::BuiltinOp; use crate::builtin::defs::BuiltinOp;
use crate::module::{CrsnExtension, ParseOpRes}; use crate::module::{CrsnExtension, ParseOpRes};
use crate::asm::data::RdObj;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BuiltinOps { pub struct BuiltinOps {

@ -2,14 +2,14 @@ use std::fmt::Debug;
pub use eval_res::EvalRes; pub use eval_res::EvalRes;
use crate::asm::data::literal::Value;
use crate::asm::data::Mask;
use crate::asm::error::Error; use crate::asm::error::Error;
use crate::asm::instr::Op; use crate::asm::instr::Op;
use crate::asm::parse::arg_parser::ArgParser; use crate::asm::parse::arg_parser::ArgParser;
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;
use crate::runtime::run_thread::state::RunState; use crate::runtime::run_thread::state::RunState;
use crate::runtime::run_thread::ThreadInfo; use crate::runtime::run_thread::ThreadInfo;
use crate::asm::data::literal::Value;
use crate::asm::data::Mask;
mod eval_res; mod eval_res;
@ -40,7 +40,7 @@ pub trait CrsnExtension: Debug + Send + Sync + 'static {
fn drop_obj(&self, fn drop_obj(&self,
#[allow(unused)] ti: &ThreadInfo, #[allow(unused)] ti: &ThreadInfo,
#[allow(unused)] state: &mut RunState, #[allow(unused)] state: &mut RunState,
#[allow(unused)] handle : Value) -> Result<Option<()>, Fault> #[allow(unused)] handle: Value) -> Result<Option<()>, Fault>
{ {
// Default impl - we do not support dropping this object // Default impl - we do not support dropping this object
Ok(None) Ok(None)
@ -50,8 +50,8 @@ pub trait CrsnExtension: Debug + Send + Sync + 'static {
fn read_obj(&self, fn read_obj(&self,
#[allow(unused)] ti: &ThreadInfo, #[allow(unused)] ti: &ThreadInfo,
#[allow(unused)] state: &mut RunState, #[allow(unused)] state: &mut RunState,
#[allow(unused)] handle : Value, #[allow(unused)] handle: Value,
#[allow(unused)] mask : Mask #[allow(unused)] mask: Mask,
) -> Result<Option<Value>, Fault> { ) -> Result<Option<Value>, Fault> {
// Default impl - we do not support reading this object // Default impl - we do not support reading this object
Ok(None) Ok(None)
@ -61,8 +61,8 @@ pub trait CrsnExtension: Debug + Send + Sync + 'static {
fn write_obj(&self, fn write_obj(&self,
#[allow(unused)] ti: &ThreadInfo, #[allow(unused)] ti: &ThreadInfo,
#[allow(unused)] state: &mut RunState, #[allow(unused)] state: &mut RunState,
#[allow(unused)] handle : Value, #[allow(unused)] handle: Value,
#[allow(unused)] mask : Mask #[allow(unused)] mask: Mask,
) -> Result<Option<()>, Fault> { ) -> Result<Option<()>, Fault> {
// Default impl - we do not support writing this object // Default impl - we do not support writing this object
Ok(None) Ok(None)

@ -4,20 +4,20 @@ use std::sync::Arc;
use crate::asm::data::literal::{Addr, Label, RoutineName}; use crate::asm::data::literal::{Addr, Label, RoutineName};
use crate::asm::instr::Op; use crate::asm::instr::Op;
use crate::builtin::defs::BuiltinOp; use crate::builtin::defs::BuiltinOp;
use crate::runtime::fault::Fault;
use crate::module::CrsnExtension; use crate::module::CrsnExtension;
use crate::runtime::fault::Fault;
#[derive(Debug)] #[derive(Debug)]
pub struct Program { pub struct Program {
pub ops: Vec<Op>, pub ops: Vec<Op>,
pub extensions: &'static [Box<dyn CrsnExtension>], pub extensions: Arc<Vec<Box<dyn CrsnExtension>>>,
routines: HashMap<RoutineName, Addr>, routines: HashMap<RoutineName, Addr>,
far_labels: HashMap<Label, Addr>, far_labels: HashMap<Label, Addr>,
barriers: Vec<Addr>, barriers: Vec<Addr>,
} }
impl Program { impl Program {
pub fn new(ops: Vec<Op>, extensions: &'static [Box<dyn CrsnExtension>]) -> Arc<Self> { pub fn new(ops: Vec<Op>, extensions: Arc<Vec<Box<dyn CrsnExtension>>>) -> Arc<Self> {
let mut p = Self { let mut p = Self {
ops, ops,
extensions, extensions,

@ -1,14 +1,16 @@
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::AtomicU64;
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::time::Duration; use std::time::Duration;
use crate::asm::data::literal::{Addr, Value}; pub use info::ThreadInfo;
pub use state::RunState;
use crate::asm::data::literal::Addr;
use crate::module::EvalRes; use crate::module::EvalRes;
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;
use crate::runtime::frame::StackFrame; use crate::runtime::frame::StackFrame;
use crate::runtime::program::Program; use crate::runtime::program::Program;
use crate::runtime::run_thread::info::{UNIQ_BASE};
#[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)] #[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)]
pub struct ThreadToken(pub u32); pub struct ThreadToken(pub u32);
@ -20,27 +22,17 @@ pub struct RunThread {
pub mod info; pub mod info;
pub mod state; pub mod state;
pub use info::ThreadInfo;
pub use state::RunState;
pub fn new_uniq() -> &'static AtomicU64 { pub fn new_uniq() -> Arc<AtomicU64> {
let uniq = AtomicU64::new(info::UNIQ_BASE); Arc::new(AtomicU64::new(info::UNIQ_BASE))
Box::leak(Box::new(uniq))
} }
impl RunThread { impl RunThread {
pub fn new(id: ThreadToken, uniq: Option<&'static AtomicU64>, program: Arc<Program>, pc: Addr, args: &[u64]) -> Self { pub fn new(id: ThreadToken, uniq: Option<Arc<AtomicU64>>, program: Arc<Program>, pc: Addr, args: &[u64]) -> Self {
let uniq = if let Some(u) = uniq {
u
} else {
let u = AtomicU64::new(UNIQ_BASE);
Box::leak(Box::new(u)) as &AtomicU64
};
Self { Self {
info: ThreadInfo { info: ThreadInfo {
id, id,
uniq, uniq: uniq.unwrap_or_else(new_uniq),
program, program,
cycle_time: Duration::default(), cycle_time: Duration::default(),
}, },

@ -1,15 +1,16 @@
use crate::runtime::run_thread::ThreadToken;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc; use std::sync::Arc;
use crate::runtime::program::Program; use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration; use std::time::Duration;
use crate::asm::data::literal::Value; use crate::asm::data::literal::Value;
use crate::runtime::program::Program;
use crate::runtime::run_thread::ThreadToken;
pub struct ThreadInfo { pub struct ThreadInfo {
/// Thread ID /// Thread ID
pub id: ThreadToken, pub id: ThreadToken,
/// Thread ID /// Thread ID
pub(crate) uniq: &'static AtomicU64, pub(crate) uniq: Arc<AtomicU64>,
/// Program to run /// Program to run
pub program: Arc<Program>, pub program: Arc<Program>,
/// Program to run /// Program to run

@ -1,7 +1,7 @@
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use std::collections::HashMap; use std::collections::HashMap;
use crate::asm::data::{WrData, Rd, Register, RdData, Wr, RdObj}; use crate::asm::data::{Rd, RdData, RdObj, Register, Wr, WrData};
use crate::asm::data::literal::{Addr, Value}; use crate::asm::data::literal::{Addr, Value};
use crate::asm::instr::Cond; use crate::asm::instr::Cond;
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;

@ -1,4 +1,4 @@
use crsn::asm::data::{Rd, Wr, RdObj}; use crsn::asm::data::{Rd, RdObj, Wr};
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum StackOp { pub enum StackOp {

@ -1,13 +1,12 @@
use std::collections::{VecDeque, HashMap}; use std::collections::{HashMap, VecDeque};
use crsn::asm::data::literal::Value; use crsn::asm::data::literal::Value;
use crsn::asm::instr::Cond; use crsn::asm::instr::Cond;
use crsn::module::{EvalRes, OpTrait, CrsnExtension}; use crsn::module::{CrsnExtension, EvalRes, OpTrait};
use crsn::runtime::fault::Fault; use crsn::runtime::fault::Fault;
use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; use crsn::runtime::run_thread::{state::RunState, ThreadInfo};
use crate::defs::StackOp; use crate::defs::StackOp;
use crate::StackOps;
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct Stacks { struct Stacks {
@ -68,7 +67,7 @@ impl OpTrait for StackOp {
// //
} }
pub(crate) fn drop_stack(state: &mut RunState, handle : Value) -> Result<Option<()>, Fault> { pub(crate) fn drop_stack(state: &mut RunState, handle: Value) -> Result<Option<()>, Fault> {
let stacks: &mut Stacks = state.ext_mut(); let stacks: &mut Stacks = state.ext_mut();
Ok(stacks.store.remove(&handle).map(|_| ())) Ok(stacks.store.remove(&handle).map(|_| ()))
} }

@ -1,12 +1,12 @@
use crsn::asm::data::literal::Value;
use crsn::asm::error::Error; use crsn::asm::error::Error;
use crsn::asm::instr::Op; use crsn::asm::instr::Op;
use crsn::asm::parse::arg_parser::ArgParser; use crsn::asm::parse::arg_parser::ArgParser;
use crsn::module::{CrsnExtension, ParseOpRes}; use crsn::module::{CrsnExtension, ParseOpRes};
use crsn::runtime::fault::Fault;
use crsn::runtime::run_thread::{RunState, ThreadInfo};
use crate::defs::StackOp; use crate::defs::StackOp;
use crsn::runtime::run_thread::{ThreadInfo, RunState};
use crsn::asm::data::literal::Value;
use crsn::runtime::fault::Fault;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StackOps { pub struct StackOps {
@ -54,7 +54,7 @@ impl CrsnExtension for StackOps {
})))) }))))
} }
fn drop_obj(&self, _ti: &ThreadInfo, state: &mut RunState, handle : Value) -> Result<Option<()>, Fault> fn drop_obj(&self, _ti: &ThreadInfo, state: &mut RunState, handle: Value) -> Result<Option<()>, Fault>
{ {
crate::exec::drop_stack(state, handle) crate::exec::drop_stack(state, handle)
} }

@ -1,16 +1,16 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
use std::time::Duration;
use std::sync::Arc;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use crsn::asm::data::literal::Addr; use crsn::asm::data::literal::Addr;
use crsn::runtime::run_thread::{RunThread, ThreadToken}; use crsn::runtime::run_thread::{RunThread, ThreadToken};
use crsn::runtime::run_thread;
use crsn_arith::ArithOps; use crsn_arith::ArithOps;
use crsn_stacks::StackOps; use crsn_stacks::StackOps;
use std::sync::atomic::AtomicU64;
use crsn::runtime::run_thread;
fn main() { fn main() {
SimpleLogger::new().init().unwrap(); SimpleLogger::new().init().unwrap();
@ -91,16 +91,16 @@ fn main() {
) )
"; ";
let parsers = Box::leak(Box::new(vec![ let parsers = Arc::new(vec![
ArithOps::new(), ArithOps::new(),
StackOps::new(), StackOps::new(),
])); ]);
let parsed = crsn::asm::assemble(program, parsers.as_slice()).unwrap(); let parsed = crsn::asm::assemble(program, parsers).unwrap();
let uniq = run_thread::new_uniq(); let uniq = run_thread::new_uniq();
let mut thread1 = RunThread::new(ThreadToken(0), Some(uniq), parsed.clone(), Addr(0), &[]); let thread1 = RunThread::new(ThreadToken(0), Some(uniq.clone()), parsed.clone(), Addr(0), &[]);
//thread1.set_speed(Duration::from_millis(250)); //thread1.set_speed(Duration::from_millis(250));
let _thread2 = RunThread::new(ThreadToken(1), Some(uniq), parsed.clone(), Addr(0), &[]); let _thread2 = RunThread::new(ThreadToken(1), Some(uniq), parsed.clone(), Addr(0), &[]);

Loading…
Cancel
Save