Skip to content

Commit

Permalink
Make get_builtin_type and get_builtin_const macros
Browse files Browse the repository at this point in the history
That allows us to get rid of util.rs too ^-^
  • Loading branch information
VonTum committed Jan 30, 2025
1 parent ca04e50 commit 55f58b9
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 92 deletions.
24 changes: 14 additions & 10 deletions src/compiler_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::path::PathBuf;
use std::str::FromStr;

use crate::config::EarlyExitUpTo;
use crate::linker::{get_builtin_const, get_builtin_type, AFTER_INITIAL_PARSE_CP};
use crate::linker::AFTER_INITIAL_PARSE_CP;
use crate::prelude::*;

use sus_proc_macro::{get_builtin_const, get_builtin_type};
use tree_sitter::Parser;

use crate::{
Expand Down Expand Up @@ -43,15 +44,18 @@ impl Linker {
let stl_path = PathBuf::from_str(STD_LIB_PATH).expect("Standard library directory is not a valid path?");
self.add_all_files_in_directory(&stl_path, info_mngr);

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_const("true")].link_info.name, "true");
assert_eq!(self.constants[get_builtin_const("false")].link_info.name, "false");
assert_eq!(self.constants[get_builtin_const("__crash_compiler")].link_info.name, "__crash_compiler");
assert_eq!(self.constants[get_builtin_const("assert")].link_info.name, "assert");
assert_eq!(self.constants[get_builtin_const("sizeof")].link_info.name, "sizeof");
assert_eq!(self.constants[get_builtin_const("clog2")].link_info.name, "clog2");
// Sanity check for the names the compiler knows internally.
// They are defined in stl/core.sus
// Critically, stl/core.sus MUST be the first file to be loaded into the linker. Otherwise the IDs don't point to the correct objects
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_const!("true")].link_info.name, "true");
assert_eq!(self.constants[get_builtin_const!("false")].link_info.name, "false");
assert_eq!(self.constants[get_builtin_const!("__crash_compiler")].link_info.name, "__crash_compiler");
assert_eq!(self.constants[get_builtin_const!("assert")].link_info.name, "assert");
assert_eq!(self.constants[get_builtin_const!("sizeof")].link_info.name, "sizeof");
assert_eq!(self.constants[get_builtin_const!("clog2")].link_info.name, "clog2");
}

pub fn add_all_files_in_directory<ExtraInfoManager : LinkerExtraFileInfoManager>(&mut self, directory : &PathBuf, info_mngr : &mut ExtraInfoManager) {
Expand Down
6 changes: 4 additions & 2 deletions src/flattening/lints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::linker::{get_builtin_const, IsExtern, LinkInfo, AFTER_LINTS_CP};
use sus_proc_macro::get_builtin_const;

use crate::linker::{IsExtern, LinkInfo, AFTER_LINTS_CP};
use crate::prelude::*;
use crate::typing::template::ParameterKind;

Expand Down Expand Up @@ -59,7 +61,7 @@ fn find_unused_variables(md: &Module, errors: &ErrorCollector) {
if let Instruction::Expression(expr) = instr {
if let ExpressionSource::WireRef(wr) = &expr.source {
if let WireReferenceRoot::NamedConstant(cst) = &wr.root {
if cst.id == get_builtin_const("assert") {
if cst.id == get_builtin_const!("assert") {
is_instance_used_map[assert_instr_id] = true;
wire_to_explore_queue.push(assert_instr_id);
}
Expand Down
7 changes: 6 additions & 1 deletion src/instantiation/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::typing::template::GlobalReference;
use num::BigInt;

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

use crate::typing::{
Expand Down Expand Up @@ -151,6 +150,12 @@ fn array_access<'v>(
}
}

fn add_to_small_set<T: Eq>(set_vec: &mut Vec<T>, elem: T) {
if !set_vec.contains(&elem) {
set_vec.push(elem);
}
}

/// Temporary intermediary struct
///
/// See [WireReferenceRoot]
Expand Down
22 changes: 1 addition & 21 deletions src/linker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use tree_sitter::Tree;
use crate::{
alloc::ArenaAllocator,
file_position::FileText,
flattening::Module,
util::const_str_position,
flattening::Module
};

use crate::errors::{CompileError, ErrorInfo, ErrorLevel, ErrorStore};
Expand All @@ -27,25 +26,6 @@ use crate::flattening::{StructType, TypingAllocator};

use self::checkpoint::CheckPoint;

const BUILTIN_TYPES: [&'static str; 2] = ["bool", "int"];
const BUILTIN_CONSTANTS: [&'static str; 6] = ["__crash_compiler", "true", "false", "assert", "sizeof", "clog2"];

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)
} else {
unreachable!()
}
}

pub const fn get_builtin_const(name: &'static str) -> ConstantUUID {
if let Some(is_type) = const_str_position(name, &BUILTIN_CONSTANTS) {
ConstantUUID::from_hidden_value(is_type)
} else {
unreachable!()
}
}

/// Documentation can be attached to [Module], [StructType], [NamedConstant], [crate::flattening::Declaration]
#[derive(Debug, Clone)]
pub struct Documentation {
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod alloc;
mod block_vector;
mod util;

mod config;
mod debug;
Expand Down
7 changes: 4 additions & 3 deletions src/typing/abstract_type.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use sus_proc_macro::get_builtin_type;

use crate::alloc::ArenaAllocator;
use crate::prelude::*;
use crate::value::Value;
Expand All @@ -6,7 +8,6 @@ use std::ops::Deref;
use super::template::{GlobalReference, Parameter, TVec};
use super::type_inference::{DomainVariableID, DomainVariableIDMarker, TypeSubstitutor, TypeVariableID, TypeVariableIDMarker, UnifyErrorReport};
use crate::flattening::{BinaryOperator, StructType, TypingAllocator, UnaryOperator, WrittenType};
use crate::linker::get_builtin_type;
use crate::to_string::map_to_type_names;

/// This contains only the information that can be type-checked before template instantiation.
Expand Down Expand Up @@ -36,8 +37,8 @@ pub enum AbstractType {
Unknown(TypeVariableID),
}

pub const BOOL_TYPE: AbstractType = AbstractType::Named(get_builtin_type("bool"));
pub const INT_TYPE: AbstractType = AbstractType::Named(get_builtin_type("int"));
pub const BOOL_TYPE: AbstractType = AbstractType::Named(get_builtin_type!("bool"));
pub const INT_TYPE: AbstractType = AbstractType::Named(get_builtin_type!("int"));

/// These represent (clock) domains. While clock domains are included under this umbrella, domains can use the same clock.
/// The use case for non-clock-domains is to separate Latency Counting domains. So different pipelines where it doesn't
Expand Down
10 changes: 5 additions & 5 deletions src/typing/concrete_type.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use num::BigInt;
use sus_proc_macro::get_builtin_type;

use crate::prelude::*;
use std::ops::Deref;

use crate::linker::get_builtin_type;
use crate::value::Value;

use super::template::TVec;

use super::type_inference::ConcreteTypeVariableID;

pub const BOOL_CONCRETE_TYPE: ConcreteType = ConcreteType::Named(ConcreteGlobalReference {
id: get_builtin_type("bool"),
id: get_builtin_type!("bool"),
template_args: FlatAlloc::new(),
});

pub const INT_CONCRETE_TYPE: ConcreteType = ConcreteType::Named(ConcreteGlobalReference {
id: get_builtin_type("int"),
id: get_builtin_type!("int"),
template_args: FlatAlloc::new(),
});

Expand Down Expand Up @@ -94,9 +94,9 @@ impl ConcreteType {

/// TODO #50 Ranged Int work should be integrated
pub fn sizeof_named(type_ref: &ConcreteGlobalReference<TypeUUID>) -> u64 {
if type_ref.id == get_builtin_type("int") {
if type_ref.id == get_builtin_type!("int") {
32 // TODO concrete int sizes
} else if type_ref.id == get_builtin_type("bool") {
} else if type_ref.id == get_builtin_type!("bool") {
1
} else {
println!("TODO Named Structs Size");
Expand Down
49 changes: 0 additions & 49 deletions src/util.rs

This file was deleted.

1 change: 1 addition & 0 deletions sus-proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rust-version = "1.78"
# Tree sitter
tree-sitter = "~0.24.7"
tree-sitter-sus = {version = "~0.2.0-devel", path = "../tree-sitter-sus"}
regex = "1.11.1"

# proc_macro utils
syn = "1.0"
Expand Down
52 changes: 52 additions & 0 deletions sus-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

use proc_macro::TokenStream;

use quote::{quote, quote_spanned};
use regex::Regex;
use syn::{parse_macro_input, LitStr};

#[proc_macro]
Expand Down Expand Up @@ -69,3 +71,53 @@ pub fn field(token_stream: TokenStream) -> TokenStream {
}
.into()
}

#[proc_macro]
pub fn get_builtin_type(token_stream: TokenStream) -> TokenStream {
let string_literal: LitStr = parse_macro_input!(token_stream);

let object_name = string_literal.value();

let core_file_text = std::fs::read_to_string("stl/core.sus").unwrap();

let re = Regex::new(r"__builtin__\s+struct\s+([a-zA-Z0-9_]+)\s*(?:#\(.*\))?\s*\{").unwrap();

for (idx, c) in re.captures_iter(&core_file_text).enumerate() {
let (_full, [found_name]) = c.extract();
if found_name == object_name {
return quote! {
crate::prelude::TypeUUID::from_hidden_value(#idx)
}.into();
}
}

quote_spanned!(
string_literal.span() =>
compile_error!("Unknown builtin type was not found in stl/core.sus")
).into()
}

#[proc_macro]
pub fn get_builtin_const(token_stream: TokenStream) -> TokenStream {
let string_literal: LitStr = parse_macro_input!(token_stream);

let object_name = string_literal.value();

let core_file_text = std::fs::read_to_string("stl/core.sus").unwrap();

let re = Regex::new(r"__builtin__\s+const\s+.+\s+([a-zA-Z0-9_]+)\s*(?:#\(.*\))?\s*\{").unwrap();

for (idx, c) in re.captures_iter(&core_file_text).enumerate() {
let (_full, [found_name]) = c.extract();
if found_name == object_name {
return quote! {
crate::prelude::ConstantUUID::from_hidden_value(#idx)
}.into();
}
}

quote_spanned!(
string_literal.span() =>
compile_error!("Unknown builtin const was not found in stl/core.sus")
).into()
}

0 comments on commit 55f58b9

Please sign in to comment.