From ac35308d30bc5dc555d6e7740587b56b1a66c5e8 Mon Sep 17 00:00:00 2001 From: Lennart Van Hirtum Date: Thu, 7 Nov 2024 20:19:54 +0100 Subject: [PATCH] Delete ConcreteType::Error & evaluate constants in execute.rs --- src/codegen_fallback.rs | 2 +- src/compiler_top.rs | 8 +------- src/flattening/initialization.rs | 4 +--- src/instantiation/execute.rs | 21 ++++++++++++++++++--- src/linker/mod.rs | 25 +++---------------------- src/to_string.rs | 1 - src/typing/concrete_type.rs | 4 +--- src/value.rs | 8 +------- 8 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/codegen_fallback.rs b/src/codegen_fallback.rs index 2f31b76..ac21654 100644 --- a/src/codegen_fallback.rs +++ b/src/codegen_fallback.rs @@ -54,7 +54,7 @@ fn typ_to_declaration(mut typ: &ConcreteType, var_name: &str) -> String { } } ConcreteType::Array(_) => unreachable!("All arrays have been used up already"), - ConcreteType::Value(_) | ConcreteType::Unknown | ConcreteType::Error => unreachable!(), + ConcreteType::Value(_) | ConcreteType::Unknown => unreachable!(), } } diff --git a/src/compiler_top.rs b/src/compiler_top.rs index 081f74e..302f592 100644 --- a/src/compiler_top.rs +++ b/src/compiler_top.rs @@ -3,9 +3,8 @@ use std::path::PathBuf; use std::str::FromStr; use crate::config::EarlyExitUpTo; -use crate::linker::{get_builtin_constant, get_builtin_type, AFTER_INITIAL_PARSE_CP}; +use crate::linker::{get_builtin_type, AFTER_INITIAL_PARSE_CP}; use crate::prelude::*; -use crate::value::{TypedValue, Value}; use tree_sitter::Parser; @@ -43,11 +42,6 @@ impl Linker { assert_eq!(self.types[get_builtin_type("int")].link_info.name, "int"); assert_eq!(self.types[get_builtin_type("bool")].link_info.name, "bool"); - - assert_eq!(self.constants[get_builtin_constant("true")].link_info.name, "true"); - assert_eq!(self.constants[get_builtin_constant("false")].link_info.name, "false"); - self.constants[get_builtin_constant("true")].val = TypedValue::from_value(Value::Bool(true)); - self.constants[get_builtin_constant("false")].val = TypedValue::from_value(Value::Bool(false)); } pub fn add_all_files_in_directory(&mut self, directory : &PathBuf, info_mngr : &mut ExtraInfoManager) { diff --git a/src/flattening/initialization.rs b/src/flattening/initialization.rs index ccc70b8..99298e2 100644 --- a/src/flattening/initialization.rs +++ b/src/flattening/initialization.rs @@ -6,7 +6,6 @@ use crate::linker::{IsExtern, NamedConstant, AFTER_INITIAL_PARSE_CP}; use crate::prelude::*; use crate::linker::{FileBuilder, LinkInfo, ResolvedGlobals}; -use crate::value::TypedValue; use crate::{file_position::FileText, flattening::Module, instantiation::InstantiationList}; use crate::typing::template::{ @@ -336,8 +335,7 @@ fn initialize_global_object(builder: &mut FileBuilder, parsing_errors: ErrorColl GlobalObjectKind::Const => { builder.add_const(NamedConstant { link_info, - output_decl: FlatID::PLACEHOLDER, - val: TypedValue::make_placeholder(), + output_decl: FlatID::PLACEHOLDER }); } } diff --git a/src/instantiation/execute.rs b/src/instantiation/execute.rs index d729120..fcddfd3 100644 --- a/src/instantiation/execute.rs +++ b/src/instantiation/execute.rs @@ -6,7 +6,9 @@ use std::ops::{Deref, Index, IndexMut}; +use crate::linker::{get_builtin_type, IsExtern}; use crate::prelude::*; +use crate::typing::template::GlobalReference; use num::BigInt; @@ -191,6 +193,20 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> { } } + fn get_named_constant_value(&self, cst_ref: &GlobalReference) -> TypedValue { + 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"))}, + other => unreachable!("{other} is not a known builtin constant") + } + } else { + todo!("Custom Constants"); + } + } + // Points to the wire in the hardware that corresponds to the root of this. fn determine_wire_ref_root(&mut self, wire_ref_root: &WireReferenceRoot) -> RealWireRefRoot { match wire_ref_root { @@ -204,8 +220,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> { SubModuleOrWire::Unnasigned => unreachable!(), }, WireReferenceRoot::NamedConstant(cst) => { - let cst = &self.linker.constants[cst.id]; - RealWireRefRoot::Constant(cst.val.clone()) + RealWireRefRoot::Constant(self.get_named_constant_value(cst)) } WireReferenceRoot::SubModulePort(port) => { return self.instantiate_port_wire_ref_root( @@ -358,7 +373,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> { self.generation_state.get_generation_value(decl_id)?.clone() } WireReferenceRoot::NamedConstant(cst) => { - self.linker.constants[cst.id].get_value().clone() + self.get_named_constant_value(cst) } &WireReferenceRoot::SubModulePort(_) => { todo!("Don't yet support compile time functions") diff --git a/src/linker/mod.rs b/src/linker/mod.rs index bd90457..79ea4b6 100644 --- a/src/linker/mod.rs +++ b/src/linker/mod.rs @@ -16,8 +16,7 @@ use crate::{ alloc::ArenaAllocator, file_position::FileText, flattening::Module, - util::{const_str_position, const_str_position_in_tuples}, - value::{TypedValue, Value}, + util::const_str_position, }; use crate::errors::{CompileError, ErrorInfo, ErrorLevel, ErrorStore}; @@ -30,9 +29,6 @@ use self::checkpoint::CheckPoint; const BUILTIN_TYPES: [&'static str; 2] = ["bool", "int"]; -const BUILTIN_CONSTANTS: [(&'static str, Value); 2] = - [("true", Value::Bool(true)), ("false", Value::Bool(false))]; - pub const fn get_builtin_type(name: &'static str) -> TypeUUID { if let Some(is_type) = const_str_position(name, &BUILTIN_TYPES) { TypeUUID::from_hidden_value(is_type) @@ -41,14 +37,6 @@ pub const fn get_builtin_type(name: &'static str) -> TypeUUID { } } -pub const fn get_builtin_constant(name: &'static str) -> ConstantUUID { - if let Some(is_constant) = const_str_position_in_tuples(name, &BUILTIN_CONSTANTS) { - ConstantUUID::from_hidden_value(is_constant) - } else { - unreachable!() - } -} - #[derive(Debug, Clone)] pub struct Documentation { pub gathered: Box<[Span]>, @@ -69,7 +57,7 @@ impl Documentation { } } -#[derive(Debug)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum IsExtern { Normal, Extern, @@ -149,14 +137,7 @@ pub struct LinkingErrorLocation { #[derive(Debug)] pub struct NamedConstant { pub link_info: LinkInfo, - pub output_decl: FlatID, - pub val: TypedValue -} - -impl NamedConstant { - pub fn get_value(&self) -> &TypedValue { - &self.val - } + pub output_decl: FlatID } pub struct FileData { diff --git a/src/to_string.rs b/src/to_string.rs index 6c088b8..841f623 100644 --- a/src/to_string.rs +++ b/src/to_string.rs @@ -94,7 +94,6 @@ impl ConcreteType { } ConcreteType::Value(v) => format!("{{concrete_type_{v}}}"), ConcreteType::Unknown => format!("{{concrete_type_unknown}}"), - ConcreteType::Error => format!("{{concrete_type_error}}"), } } } diff --git a/src/typing/concrete_type.rs b/src/typing/concrete_type.rs index 7f4630e..efc63ea 100644 --- a/src/typing/concrete_type.rs +++ b/src/typing/concrete_type.rs @@ -17,8 +17,7 @@ pub enum ConcreteType { Named(TypeUUID), Value(Value), Array(Box<(ConcreteType, ConcreteType)>), - Unknown, - Error, + Unknown } /// Panics on Type Errors that should have been caught by [AbstractType] @@ -104,7 +103,6 @@ impl ConcreteType { && target_arr_size.type_compare(found_arr_size) } (ConcreteType::Value(lv), ConcreteType::Value(rv)) => lv == rv, - (ConcreteType::Error, _) | (_, ConcreteType::Error) => true, // Just assume correct, because the other side has an error (ConcreteType::Unknown, _) | (_, ConcreteType::Unknown) => { todo!("Type Unification {self:?} {found:?}") } diff --git a/src/value.rs b/src/value.rs index bc0aca6..3e71a2b 100644 --- a/src/value.rs +++ b/src/value.rs @@ -178,12 +178,6 @@ impl TypedValue { value: Value::Integer(i), } } - pub fn make_placeholder() -> Self { - Self { - typ: ConcreteType::Error, - value: Value::Error - } - } /// panics if the value can't be typed. pub fn from_value(value: Value) -> Self { Self { @@ -217,7 +211,7 @@ impl ConcreteType { } Value::Array(arr.into_boxed_slice()) } - ConcreteType::Value(_) | ConcreteType::Unknown | ConcreteType::Error => unreachable!(), + ConcreteType::Value(_) | ConcreteType::Unknown => unreachable!(), } }