Skip to content

Commit

Permalink
Merge branch 'master' into 46-implement-display-using-proxy-types
Browse files Browse the repository at this point in the history
  • Loading branch information
IBims1NicerTobi authored Jan 12, 2025
2 parents 9f05312 + 205cf21 commit 5306c09
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 131 deletions.
4 changes: 2 additions & 2 deletions src/codegen/system_verilog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ impl<'g, 'out, Stream: std::fmt::Write> CodeGenerationContext<'g, 'out, Stream>
ConcreteTemplateArg::Type(..) => {
unreachable!("No extern module type arguments. Should have been caught by Lint")
}
ConcreteTemplateArg::Value(typed_value, _) => {
typed_value.value.inline_constant_to_string()
ConcreteTemplateArg::Value(value, _) => {
value.inline_constant_to_string()
}
ConcreteTemplateArg::NotProvided => unreachable!("All args are known at codegen"),
};
Expand Down
2 changes: 1 addition & 1 deletion src/dev_aid/lsp/hover_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'l> HoverCollector<'l> {
if is_generative {
let value_str = match &inst.generation_state[id] {
SubModuleOrWire::SubModule(_) | SubModuleOrWire::Wire(_) => unreachable!(),
SubModuleOrWire::CompileTimeValue(v) => format!(" = {}", v.value),
SubModuleOrWire::CompileTimeValue(v) => format!(" = {}", v),
SubModuleOrWire::Unnasigned => format!("never assigned to"),
};
self.monospace(value_str);
Expand Down
2 changes: 1 addition & 1 deletion src/instantiation/concrete_typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn try_to_attach_value_to_template_arg(template_wire_referernce: FlatID, found_v
let ConcreteType::Value(v) = found_value else {return}; // We don't have a value to assign
if let Some(template_id) = can_expression_be_value_inferred(submodule_link_info, template_wire_referernce) {
if let ConcreteTemplateArg::NotProvided = &template_args[template_id] {
template_args[template_id] = ConcreteTemplateArg::Value(TypedValue::from_value(v.clone()), HowDoWeKnowTheTemplateArg::Inferred)
template_args[template_id] = ConcreteTemplateArg::Value(v.clone(), HowDoWeKnowTheTemplateArg::Inferred)
}
}
}
Expand Down
61 changes: 29 additions & 32 deletions src/instantiation/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use std::ops::{Deref, Index, IndexMut};

use crate::linker::{get_builtin_type, IsExtern};
use crate::linker::IsExtern;
use crate::prelude::*;
use crate::typing::concrete_type::BOOL_CONCRETE_TYPE;
use crate::typing::template::{GlobalReference, HowDoWeKnowTheTemplateArg};

use num::BigInt;

use crate::flattening::*;
use crate::value::{compute_binary_op, compute_unary_op, TypedValue, Value};
use crate::value::{compute_binary_op, compute_unary_op, Value};
use crate::util::add_to_small_set;

use crate::typing::{
Expand Down Expand Up @@ -76,12 +77,12 @@ impl<'fl> GenerationState<'fl> {
*target = to_write;
Ok(())
}
fn get_generation_value(&self, v: FlatID) -> ExecutionResult<&TypedValue> {
fn get_generation_value(&self, v: FlatID) -> ExecutionResult<&Value> {
if let SubModuleOrWire::CompileTimeValue(vv) = &self.generation_state[v] {
if let Value::Unset | Value::Error = &vv.value {
if let Value::Unset | Value::Error = vv {
Err((
self.span_of(v),
format!("This variable is set but it's {:?}!", vv.value),
format!("This variable is set but it's {:?}!", vv),
))
} else {
Ok(vv)
Expand Down Expand Up @@ -129,18 +130,13 @@ impl<'fl> IndexMut<FlatID> for GenerationState<'fl> {
}
}

fn array_access(tv: &TypedValue, idx: &BigInt, span: BracketSpan) -> ExecutionResult<TypedValue> {
let typ = tv.typ.down_array().clone();

let Value::Array(arr) = &tv.value else {
fn array_access<'v>(arr_val: &'v Value, idx: &BigInt, span: BracketSpan) -> ExecutionResult<&'v Value> {
let Value::Array(arr) = arr_val else {
caught_by_typecheck!("Value must be an array")
};

if let Some(elem) = usize::try_from(idx).ok().and_then(|idx| arr.get(idx)) {
Ok(TypedValue {
typ,
value: elem.clone(),
})
Ok(elem)
} else {
Err((
span.outer_span(),
Expand Down Expand Up @@ -188,13 +184,13 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
}

/// TODO make builtins that depend on parameters
fn get_named_constant_value(&self, cst_ref: &GlobalReference<ConstantUUID>) -> TypedValue {
fn get_named_constant_value(&self, cst_ref: &GlobalReference<ConstantUUID>) -> Value {
let linker_cst = &self.linker.constants[cst_ref.id];

if linker_cst.link_info.is_extern == IsExtern::Builtin {
match linker_cst.link_info.name.as_str() {
"true" => TypedValue{value: Value::Bool(true), typ: ConcreteType::Named(get_builtin_type("bool"))},
"false" => TypedValue{value: Value::Bool(false), typ: ConcreteType::Named(get_builtin_type("bool"))},
"true" => Value::Bool(true),
"false" => Value::Bool(false),
"__crash_compiler" => {
cst_ref.get_total_span().debug();
panic!("__crash_compiler Intentional ICE. This is for debugging the compiler and LSP.")
Expand Down Expand Up @@ -320,19 +316,19 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
else {
unreachable!()
};
let mut new_val = std::mem::replace(&mut v_writable.value, Value::Error);
let mut new_val = std::mem::replace(v_writable, Value::Error);
self.generation_state.write_gen_variable(
&mut new_val,
&target_wire_ref.path,
found_v.value,
found_v,
)?;

let SubModuleOrWire::CompileTimeValue(v_writable) =
&mut self.generation_state[target_decl]
else {
unreachable!()
};
v_writable.value = new_val;
*v_writable = new_val;
}
RealWireRefRoot::Constant(_cst) => {
caught_by_typecheck!("Cannot assign to constants");
Expand All @@ -356,7 +352,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
self.generation_state.write_gen_variable(
initial_value,
&target_wire_ref.path,
found_v.value,
found_v,
)?;
}
}
Expand All @@ -365,8 +361,8 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
fn compute_compile_time_wireref(
&self,
wire_ref: &WireReference,
) -> ExecutionResult<TypedValue> {
let mut work_on_value: TypedValue = match &wire_ref.root {
) -> ExecutionResult<Value> {
let mut work_on_value: Value = match &wire_ref.root {
&WireReferenceRoot::LocalDecl(decl_id, _span) => {
self.generation_state.get_generation_value(decl_id)?.clone()
}
Expand All @@ -383,16 +379,16 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
&WireReferencePathElement::ArrayAccess { idx, bracket_span } => {
let idx = self.generation_state.get_generation_integer(idx)?;

array_access(&work_on_value, idx, bracket_span)?
array_access(&work_on_value, idx, bracket_span)?.clone()
}
}
}

Ok(work_on_value)
}
fn compute_compile_time(&mut self, expression: &Expression) -> ExecutionResult<TypedValue> {
fn compute_compile_time(&mut self, expression: &Expression) -> ExecutionResult<Value> {
Ok(match &expression.source {
ExpressionSource::WireRef(wire_ref) => self.compute_compile_time_wireref(wire_ref)?,
ExpressionSource::WireRef(wire_ref) => self.compute_compile_time_wireref(wire_ref)?.clone(),
&ExpressionSource::UnaryOp { op, right } => {
let right_val = self.generation_state.get_generation_value(right)?;
compute_unary_op(op, right_val)
Expand All @@ -404,7 +400,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
match op {
BinaryOperator::Divide | BinaryOperator::Modulo => {
use num::Zero;
if right_val.value.unwrap_integer().is_zero() {
if right_val.unwrap_integer().is_zero() {
return Err((
expression.span,
format!(
Expand All @@ -419,20 +415,21 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {

compute_binary_op(left_val, op, right_val)
}
ExpressionSource::Constant(value) => TypedValue::from_value(value.clone()),
ExpressionSource::Constant(value) => value.clone(),
})
}

fn alloc_wire_for_const(
&mut self,
value: TypedValue,
value: Value,
original_instruction: FlatID,
domain: DomainID,
) -> WireID {
self.wires.alloc(RealWire {
source: RealWireDataSource::Constant { value: value.value },
typ : value.get_type_best_effort(&mut self.type_substitutor),
source: RealWireDataSource::Constant { value },
original_instruction,
domain,
typ: value.typ,
name: self.unique_name_producer.get_unique_name(""),
specified_latency: CALCULATE_LATENCY_LATER,
absolute_latency: CALCULATE_LATENCY_LATER,
Expand Down Expand Up @@ -591,7 +588,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
self.template_args[template_id].unwrap_value().clone()
} else {
// Empty initial value
typ.get_initial_typed_val()
typ.get_initial_val()
};
SubModuleOrWire::CompileTimeValue(value)
} else {
Expand Down Expand Up @@ -788,7 +785,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
else {
unreachable!()
};
*v = TypedValue::make_integer(current_val.clone());
*v = Value::Integer(current_val.clone());
current_val += 1;
self.instantiate_code_block(stm.loop_body)?;
}
Expand Down
8 changes: 4 additions & 4 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
config,
errors::{CompileError, ErrorStore},
to_string::pretty_print_concrete_instance,
value::{TypedValue, Value},
value::Value,
};

use crate::typing::{
Expand Down Expand Up @@ -155,7 +155,7 @@ pub struct InstantiatedModule {
pub enum SubModuleOrWire {
SubModule(SubModuleID),
Wire(WireID),
CompileTimeValue(TypedValue),
CompileTimeValue(Value),
// Variable doesn't exist yet
Unnasigned,
}
Expand All @@ -169,7 +169,7 @@ impl SubModuleOrWire {
*result
}
#[track_caller]
pub fn unwrap_generation_value(&self) -> &TypedValue {
pub fn unwrap_generation_value(&self) -> &Value {
let Self::CompileTimeValue(result) = self else {
unreachable!("SubModuleOrWire::unwrap_generation_value failed! Is {self:?} instead")
};
Expand All @@ -192,7 +192,7 @@ pub enum RealWireRefRoot {
preamble: Vec<RealWirePathElem>,
},
Generative(FlatID),
Constant(TypedValue),
Constant(Value),
}

#[derive(Debug)]
Expand Down
8 changes: 4 additions & 4 deletions src/typing/template.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use crate::{prelude::*, value::Value};

use super::{abstract_type::AbstractType, concrete_type::ConcreteType};
use crate::{flattening::WrittenType, value::TypedValue};
use crate::flattening::WrittenType;

#[derive(Debug)]
pub struct GlobalReference<ID> {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl HowDoWeKnowTheTemplateArg {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConcreteTemplateArg {
Type(ConcreteType, HowDoWeKnowTheTemplateArg),
Value(TypedValue, HowDoWeKnowTheTemplateArg),
Value(Value, HowDoWeKnowTheTemplateArg),
NotProvided,
}

Expand All @@ -121,7 +121,7 @@ impl ConcreteTemplateArg {
t
}
#[track_caller]
pub fn unwrap_value(&self) -> &TypedValue {
pub fn unwrap_value(&self) -> &Value {
let Self::Value(v, _) = self else {unreachable!()};
v
}
Expand Down
Loading

0 comments on commit 5306c09

Please sign in to comment.