Skip to content

Commit

Permalink
Started analyzer stuff and unified the allocation instructions into o…
Browse files Browse the repository at this point in the history
…ne and made a formal variable declaration/memory allocation instruction.
  • Loading branch information
jmeaster30 committed Oct 2, 2023
1 parent 97103d1 commit 99ef9c9
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 170 deletions.
2 changes: 1 addition & 1 deletion get-coverage.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cargo llvm-cov --html
firefox target/llvm-cov/html/index.html
#firefox target/llvm-cov/html/index.html
4 changes: 3 additions & 1 deletion src/hydro/analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mod analysisgraph;
pub mod possiblevalue;
pub mod possiblevalue;
pub mod analyze;
mod analysiscontext;
3 changes: 3 additions & 0 deletions src/hydro/analyzer/analysiscontext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct AnalysisContext {

}
12 changes: 10 additions & 2 deletions src/hydro/analyzer/analysisgraph.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use std::collections::HashMap;
use crate::hydro::analyzer::possiblevalue::PossibleValue;

pub struct AnalysisInstructionPointer {
pub module: String,
pub function: String,
pub instruction_number: u32,
}

pub struct AnalysisNode {
stack: Vec<PossibleValue>
instruction: AnalysisInstructionPointer,
previous_instructions: Vec<AnalysisInstructionPointer>,
dependent_instructions: Vec<AnalysisInstructionPointer>,
stack_size: i32,
}

pub struct AnalysisGraph {
Expand Down
5 changes: 5 additions & 0 deletions src/hydro/analyzer/analyze.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::hydro::analyzer::analysiscontext::AnalysisContext;

pub trait Analyzable {
fn analyze(&self, context: &mut AnalysisContext);
}
28 changes: 26 additions & 2 deletions src/hydro/analyzer/possiblevalue.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hydro::value::Value;
use crate::hydro::value::{Type, Value};

#[derive(Debug)]
pub struct PossibleValue {
Expand Down Expand Up @@ -154,10 +154,34 @@ impl PossibleValue {

pub fn complement(value: Self) -> Self {
Self {
ranges: Vec::new()
ranges: Self::complement_internal(value.ranges),
}
}

fn complement_internal(ranges: Vec<(bool, Value, Value, bool)>) -> Vec<(bool, Value, Value, bool)> {
let mut result = Vec::new();
let mut sorted_ranges = ranges.clone();
sorted_ranges.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());

for i in 0..(sorted_ranges.len() + 1) {
if i == 0 {
let range = &sorted_ranges[i];
let left = Type::min(range.1.type_of());
result.push((true, left, range.1.clone(), !range.0));
} else if i < sorted_ranges.len() {
let range = &sorted_ranges[i];
let previous = &sorted_ranges[i - 1];
result.push((!previous.3, previous.2.clone(), range.1.clone(), !range.0));
} else {
let prev = &sorted_ranges[i - 1];
let right = Type::max(prev.2.type_of());
result.push((!prev.3, prev.2.clone(), right, true));
}
}

Self::union_internal(result)
}

pub fn contains(&self, value: Value) -> bool {
for range in &self.ranges {
if Self::inside(range.clone(), value.clone()) {
Expand Down
30 changes: 3 additions & 27 deletions src/hydro/debuggable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl Instruction {
Instruction::Store(x) => x.debug(module, context, debug_context),
Instruction::ArrayIndex(x) => x.debug(module, context, debug_context),
Instruction::LayoutIndex(x) => x.debug(module, context, debug_context),
Instruction::AllocArray(x) => x.debug(module, context, debug_context),
Instruction::AllocLayout(x) => x.debug(module, context, debug_context),
Instruction::Allocate(x) => x.debug(module, context, debug_context),
}
}
}
Expand Down Expand Up @@ -886,37 +885,14 @@ impl Debuggable for LayoutIndex {
}
}

impl Debuggable for AllocArray {
impl Debuggable for Allocate {
fn debug(
&self,
module: &Module,
context: &mut ExecutionContext,
debug_context: &mut DebugContext,
) -> Result<bool, Exception> {
let metric_name = "allocarray".to_string();
debug_context.start_core_metric(
context.current_module.clone(),
context.current_function.clone(),
metric_name.clone(),
);
let result = self.execute(module, context);
debug_context.stop_core_metric(
context.current_module.clone(),
context.current_function.clone(),
metric_name,
);
return result;
}
}

impl Debuggable for AllocLayout {
fn debug(
&self,
module: &Module,
context: &mut ExecutionContext,
debug_context: &mut DebugContext,
) -> Result<bool, Exception> {
let metric_name = "alloclayout".to_string();
let metric_name = "allocate".to_string();
debug_context.start_core_metric(
context.current_module.clone(),
context.current_function.clone(),
Expand Down
58 changes: 4 additions & 54 deletions src/hydro/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ impl Instruction {
Instruction::Return(x) => x.execute(module, context),
Instruction::Load(x) => x.execute(module, context),
Instruction::Store(x) => x.execute(module, context),
Instruction::AllocArray(x) => x.execute(module, context),
Instruction::AllocLayout(x) => x.execute(module, context),
Instruction::Allocate(x) => x.execute(module, context),
Instruction::ArrayIndex(x) => x.execute(module, context),
Instruction::LayoutIndex(x) => x.execute(module, context),
}
Expand Down Expand Up @@ -695,13 +694,7 @@ impl Executable for LayoutIndex {
}
}

impl Executable for AllocArray {
fn execute(&self, _module: &Module, _context: &mut ExecutionContext) -> Result<bool, Exception> {
todo!();
}
}

impl Executable for AllocLayout {
impl Executable for Allocate {
fn execute(&self, module: &Module, context: &mut ExecutionContext) -> Result<bool, Exception> {
if context.stack.len() < 1 {
return Err(Exception::new(
Expand All @@ -710,53 +703,10 @@ impl Executable for AllocLayout {
));
}

let layout_template = match self.module_name.clone() {
Some(template_module_name) => match module.modules.get(template_module_name.as_str()) {
Some(template_module) => match template_module
.layout_templates
.get(self.layout_template_name.as_str())
{
Some(template) => template,
None => {
return Err(Exception::new(
context.clone(),
format!(
"Layout '{}' not found in module '{}'",
self.layout_template_name, template_module_name
)
.as_str(),
))
}
},
None => {
return Err(Exception::new(
context.clone(),
format!("Module '{}' not found.", template_module_name).as_str(),
))
}
},
None => match module
.layout_templates
.get(self.layout_template_name.as_str())
{
Some(template) => template,
None => {
return Err(Exception::new(
context.clone(),
format!(
"Layout '{}' not found in module '{}'",
self.layout_template_name, module.name
)
.as_str(),
))
}
},
};

let allocated = layout_template.create_value();
let allocated = module.resolve_type(self.allocated_type.clone(), context)?.default();
let value_reference = context.stack.pop().unwrap();
match &value_reference {
Value::Reference(reference) => context.modify(reference, allocated)?,
Value::Reference(reference) => context.init(reference, allocated)?,
_ => {
return Err(Exception::new(
context.clone(),
Expand Down
35 changes: 31 additions & 4 deletions src/hydro/executioncontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ impl ExecutionContext {
}
}

pub fn init(&mut self, reference: &Reference, value: Value) -> Result<(), Exception> {
match reference {
Reference::ArrayIndex(_) => Err(Exception::new(
self.clone(),
"Initializing memory with non-variable reference doesn't make sense",
)),
Reference::LayoutIndex(_) => Err(Exception::new(
self.clone(),
"Initializing memory with non-variable reference doesn't make sense",
)),
Reference::Variable(variable_reference) => match self.variables.get(&variable_reference.name.clone()) {
Some(_) => Err(Exception::new(
self.clone(),
format!("Variable '{}' already exists :(", variable_reference.name.clone()).as_str(),
)),
None => {
self.variables.insert(variable_reference.name.clone(), value.clone());
Ok(())
}
}
}
}

pub fn modify(&mut self, reference: &Reference, value: Value) -> Result<(), Exception> {
match reference {
Reference::ArrayIndex(index_reference) => {
Expand Down Expand Up @@ -216,10 +239,14 @@ impl ExecutionContext {
}
}
}
Reference::Variable(variable_reference) => {
self
.variables
.insert(variable_reference.name.clone(), value.clone());
Reference::Variable(variable_reference) => match self.variables.get(&variable_reference.name.clone()) {
Some(_) => {
self.variables.insert(variable_reference.name.clone(), value.clone());
}
None => return Err(Exception::new(
self.clone(),
format!("Variable '{}' does not exist :(", variable_reference.name.clone()).as_str(),
))
}
}
Ok(())
Expand Down
18 changes: 3 additions & 15 deletions src/hydro/frontend/binaryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ impl Binaryable for Instruction {
Instruction::Store(x) => x.output(start_offset),
Instruction::ArrayIndex(x) => x.output(start_offset),
Instruction::LayoutIndex(x) => x.output(start_offset),
Instruction::AllocArray(x) => x.output(start_offset),
Instruction::AllocLayout(x) => x.output(start_offset),
Instruction::Allocate(x) => x.output(start_offset),
_ => panic!("Binaryable not implemented for supplied type"),
}
}
Expand Down Expand Up @@ -180,8 +179,7 @@ impl Binaryable for Instruction {
b's' => Instruction::Store(Store::input(index, input_bytes)),
b'i' => Instruction::ArrayIndex(ArrayIndex::input(index, input_bytes)),
b'm' => Instruction::LayoutIndex(LayoutIndex::input(index, input_bytes)),
b'[' => Instruction::AllocArray(AllocArray::input(index, input_bytes)),
b'{' => Instruction::AllocLayout(AllocLayout::input(index, input_bytes)),
b'[' => Instruction::Allocate(Allocate::input(index, input_bytes)),
_ => panic!("Binaryable not implemented for supplied type"),
}
}
Expand Down Expand Up @@ -497,17 +495,7 @@ impl Binaryable for LayoutIndex {
}
}

impl Binaryable for AllocArray {
fn output(&self, start_offset: usize) -> Vec<u8> {
todo!()
}

fn input(index: &mut usize, input_bytes: &Vec<u8>) -> Self {
todo!()
}
}

impl Binaryable for AllocLayout {
impl Binaryable for Allocate {
fn output(&self, start_offset: usize) -> Vec<u8> {
todo!()
}
Expand Down
Loading

0 comments on commit 99ef9c9

Please sign in to comment.