Skip to content

Ranged Integers #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
88210fe
Added delayed constraint
IBims1NicerTobi Feb 11, 2025
cec2fce
Removed clones
IBims1NicerTobi Feb 11, 2025
2ac9db2
Finished BinaryOpTypecheckConstraint
IBims1NicerTobi Feb 18, 2025
77cc284
Fixed operator sizes
IBims1NicerTobi Feb 18, 2025
d1fb001
Removed INT_CONCRETE_TYPE
IBims1NicerTobi Feb 18, 2025
84c9684
Add `FlatAlloc::try_map`
VonTum Feb 19, 2025
6b671d8
Removed constructor
IBims1NicerTobi Feb 19, 2025
940b99f
Fixed review
IBims1NicerTobi Feb 25, 2025
109a867
Fixed regression test
IBims1NicerTobi Feb 25, 2025
1d34f31
Fixed error message formatting
IBims1NicerTobi Mar 4, 2025
f497ba5
You should add the field names
IBims1NicerTobi Mar 11, 2025
f1d2f42
Fixed template argument unification
IBims1NicerTobi Mar 19, 2025
f652430
Added template parameter to code constants
IBims1NicerTobi Mar 19, 2025
e30ed87
Fixed stl
IBims1NicerTobi Mar 19, 2025
1daefb7
Fixed std for sized ints
IBims1NicerTobi Mar 19, 2025
7414bc9
Fixed off by one error
IBims1NicerTobi Mar 19, 2025
184373b
Added lower bound
IBims1NicerTobi Mar 20, 2025
fa13e00
Use return value of fully_substitute
IBims1NicerTobi Mar 23, 2025
5467ce0
Added UnaryOpTypecheckConstraint
IBims1NicerTobi Mar 30, 2025
e7c3190
Fixed interfaces in core.sus
IBims1NicerTobi Mar 30, 2025
9a88093
Fixed syntax
IBims1NicerTobi Mar 30, 2025
1cc5a99
Merge master into 28-ranged-int-2
IBims1NicerTobi Mar 31, 2025
d3b43f6
Resolved missed conflict
IBims1NicerTobi Mar 31, 2025
8a8005a
Updated sizeof_named to return the correct number of bits for the ran…
IBims1NicerTobi Apr 3, 2025
dced0f8
Made get_bounds a member function to ConcreteType
IBims1NicerTobi Apr 8, 2025
ffb7750
Merge branch 'master' into 28-ranged-int-2
VonTum Apr 30, 2025
f874ad8
Merge branch 'master' into 28-ranged-int-2
VonTum May 11, 2025
e2e587c
Fix map_to_array
VonTum May 11, 2025
b0bc19a
Merge branch 'master' into 28-ranged-int-2
VonTum May 12, 2025
ddbf1b5
Share TemplateKind among template-involved types
VonTum May 12, 2025
eec48fd
Use AbstractType for ConcreteType creation
VonTum May 12, 2025
b74e2c0
Refactor: make TemplateKind use more pervasive
VonTum May 15, 2025
4e594cd
Fix bad Type Display for unification failures
VonTum May 15, 2025
5b32747
Big change to pivot to SetUnifier for Values
VonTum May 17, 2025
d6bd460
Create FinalizationContext
VonTum May 19, 2025
bf9e214
Make error ordering even more deterministic for CI
VonTum May 19, 2025
9a7b07a
Partial work to revamp Concrete Typecheck
VonTum May 25, 2025
8e8a706
Oh god, a compiling version with the new Typechecker!
VonTum May 26, 2025
a46f9ec
Minor to_string fixes
VonTum May 26, 2025
f5e8c17
Delete DelayedConstraintsList
VonTum May 26, 2025
952dc21
Add out of time killer to track down LSP slowdowns
VonTum May 26, 2025
340446d
Small fixes, improve error placement for Multiplexers
VonTum May 26, 2025
03e5502
Added __debug_breakpoint!() macro :tada:
VonTum May 26, 2025
24d7af9
Add __debug_span!()
VonTum May 26, 2025
bcff314
Fix array boundscheck also on reading
VonTum May 26, 2025
0c1ce44
Fix int type not transmitted by RealWire::Select
VonTum May 27, 2025
7de395e
Replace assert!(found_name != expected_name) check with assert!(found
VonTum May 27, 2025
4638596
Fix timeouts due to erroneously growing Module domains in LSP use
VonTum May 27, 2025
4e3fc28
Smooth over implicit_clk_domain
VonTum May 28, 2025
51fed3e
Fix the name of local domains in parent modules
VonTum May 28, 2025
ba0a145
Some rewriting of test.sus
VonTum May 28, 2025
5a8d89a
Fix Latency Counting rejecting input-only or output-only modules
VonTum May 28, 2025
46beec4
Update STL to use sized ints
VonTum May 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// "sus_lsp.executable_path": "${workspaceFolder}/target/release/sus_compiler",
"sus_lsp.tcp_port": 25000,
"sus_lsp.args": [
"--lsp"
"--lsp",
// Tracking down slow compilations
"--kill-timeout", "2000ms"
//, "--upto", "typecheck"
],
"editor.lineHeight": 0,
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lsp-server = {version = "0.7.1", optional = true}
lsp-types = {version = "0.94.0", optional = true}
serde_json = {version = "1.0.97", optional = true}
serde = {version = "1.0.156", optional = true}
humantime = "2.2.0"

[build-dependencies]
dirs-next = "2.0.0"
Expand Down
6 changes: 3 additions & 3 deletions bad_syntax.sus
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// This is there to check for ICEs


module multiply {
interface multiply : int a, int b -> int r
module multiply#(int MIN1, int MAX1, int MIN2, int MAX2) {
interface multiply : int#(MIN: MIN1, MAX: MAX1) a, int#(MIN: MIN2, MAX: MAX2) b -> int r
r = a * b
}

// test
module contains_submodule {
interface contains_submodule : int a, int b, int c -> int r
interface contains_submodule : int#(MIN: 0, MAX: 1) a, int b, int c -> int r
int tmp = multiply(a, b)
reg r = tmp + c
}
Expand Down
70 changes: 54 additions & 16 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ impl<IndexMarker> UUIDAllocator<IndexMarker> {
self.cur.0 += 1;
allocated_id
}
pub fn peek(&self) -> UUID<IndexMarker> {
self.cur
}
pub fn to_flat_alloc<T: Default>(&self) -> FlatAlloc<T, IndexMarker> {
let mut result = FlatAlloc::with_capacity(self.cur.0);
for _ in 0..self.cur.0 {
Expand Down Expand Up @@ -303,6 +300,10 @@ impl<T, IndexMarker> ArenaAllocator<T, IndexMarker> {
PhantomData,
)
}
pub fn free_reservation(&mut self, UUID(uuid, _): UUID<IndexMarker>) {
assert!(self.data[uuid].is_none());
self.free_slots.push(uuid);
}
pub fn revert_to_reservation(&mut self, UUID(uuid, _): UUID<IndexMarker>) {
assert!(self.data[uuid].is_some());
self.data[uuid] = None;
Expand Down Expand Up @@ -353,6 +354,15 @@ impl<T, IndexMarker> ArenaAllocator<T, IndexMarker> {
.find(|(id, v)| predicate(*id, v))
.map(|(id, _)| id)
}
#[track_caller]
pub fn get2_mut(
&mut self,
UUID(uuid_a, _): UUID<IndexMarker>,
UUID(uuid_b, _): UUID<IndexMarker>,
) -> Option<(&mut T, &mut T)> {
get2_mut(&mut self.data, uuid_a, uuid_b)
.map(|(a, b)| (a.as_mut().unwrap(), b.as_mut().unwrap()))
}
}

impl<T, IndexMarker> Index<UUID<IndexMarker>> for ArenaAllocator<T, IndexMarker> {
Expand Down Expand Up @@ -614,8 +624,6 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
_ph: PhantomData,
}
}
#[cfg(test)]
// Only for testing, so only enabled with test flag
pub fn from_vec(data: Vec<T>) -> Self {
Self {
data,
Expand Down Expand Up @@ -678,17 +686,6 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
_ph: PhantomData,
}
}
pub fn cast_to_array<const N: usize>(&self) -> &[T; N] {
assert!(self.len() == N);
self.data.as_slice().try_into().unwrap()
}
pub fn map_to_array<O, const N: usize>(
&self,
mut f: impl FnMut(UUID<IndexMarker>, &T) -> O,
) -> [O; N] {
assert!(self.len() == N);
std::array::from_fn(|i| f(UUID::from_hidden_value(i), &self.data[i]))
}
pub fn try_map<OT, ErrT>(
&self,
mut f: impl FnMut((UUID<IndexMarker>, &T)) -> Result<OT, ErrT>,
Expand All @@ -702,6 +699,47 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
_ph: PhantomData,
})
}
pub fn try_map2<T2, OT, ET>(
&self,
second: &FlatAlloc<T2, IndexMarker>,
mut f: impl FnMut((UUID<IndexMarker>, &T, &T2)) -> Result<OT, ET>,
) -> Result<FlatAlloc<OT, IndexMarker>, ET> {
let mut data = Vec::with_capacity(self.len());
for v in zip_eq(self.iter(), second.iter()) {
data.push(f(v)?);
}
Ok(FlatAlloc {
data,
_ph: PhantomData,
})
}
pub fn try_map3<T2, T3, OT, ET>(
&self,
second: &FlatAlloc<T2, IndexMarker>,
third: &FlatAlloc<T3, IndexMarker>,
mut f: impl FnMut((UUID<IndexMarker>, &T, &T2, &T3)) -> Result<OT, ET>,
) -> Result<FlatAlloc<OT, IndexMarker>, ET> {
let mut data = Vec::with_capacity(self.len());
for v in zip_eq3(self.iter(), second.iter(), third.iter()) {
data.push(f(v)?);
}
Ok(FlatAlloc {
data,
_ph: PhantomData,
})
}
pub fn cast_to_array<const N: usize>(&self) -> [&T; N] {
assert_eq!(self.len(), N);
std::array::from_fn(|i| &self.data[i])
}
pub fn cast_to_array_mut<const N: usize>(&mut self) -> [&mut T; N] {
assert_eq!(self.len(), N);
std::array::from_fn(|i| {
let ptr: *mut T = &mut self.data[i];
// SAFETY: Of course, the data[i] accesses are all disjoint
unsafe { &mut *ptr }
})
}
pub fn find(
&self,
mut predicate: impl FnMut(UUID<IndexMarker>, &T) -> bool,
Expand Down
50 changes: 50 additions & 0 deletions src/append_only_vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::cell::UnsafeCell;

/// An append-only Vector. The contents cannot be looked at, unless the vector is explicitly consumed. This allows us to present a const-ref [Self::push], which has nice ergonomics
#[derive(Debug)]
pub struct AppendOnlyVec<T> {
v: UnsafeCell<Vec<T>>,
}

impl<T> Default for AppendOnlyVec<T> {
fn default() -> Self {
Self {
v: UnsafeCell::new(Vec::new()),
}
}
}

impl<T> From<Vec<T>> for AppendOnlyVec<T> {
fn from(existing_vec: Vec<T>) -> Self {
Self {
v: UnsafeCell::new(existing_vec),
}
}
}

impl<T> AppendOnlyVec<T> {
pub fn push(&self, data: T) {
// SAFETY: AppendOnlyVec is made such that references to the content can only be made from exclusive references. Hence, no reference can be taken and then invalidated by a push
unsafe {
(*self.v.get()).push(data);
}
}
pub fn new() -> Self {
Self::default()
}
}

impl<T> From<AppendOnlyVec<T>> for Vec<T> {
fn from(val: AppendOnlyVec<T>) -> Self {
val.v.into_inner()
}
}

impl<T> IntoIterator for AppendOnlyVec<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;

fn into_iter(self) -> Self::IntoIter {
self.v.into_inner().into_iter()
}
}
35 changes: 18 additions & 17 deletions src/codegen/system_verilog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::flattening::{DeclarationKind, Instruction, Module, Port};
use crate::instantiation::{
InstantiatedModule, MultiplexerSource, RealWire, RealWireDataSource, RealWirePathElem,
};
use crate::typing::template::TVec;
use crate::typing::concrete_type::ConcreteTemplateArg;
use crate::typing::template::{TVec, TemplateKind};
use crate::{typing::concrete_type::ConcreteType, value::Value};

use super::shared::*;
Expand Down Expand Up @@ -43,7 +44,7 @@ fn typ_to_declaration(mut typ: &ConcreteType, var_name: &str) -> String {
let mut array_string = String::new();
while let ConcreteType::Array(arr) = typ {
let (content_typ, size) = arr.deref();
let sz = size.unwrap_value().unwrap_integer();
let sz = size.unwrap_integer();
write!(array_string, "[{}:0]", sz - 1).unwrap();
typ = content_typ;
}
Expand All @@ -57,7 +58,6 @@ 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(_) => unreachable!(),
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'g> CodeGenerationContext<'g> {
idx += 1;
let (new_typ, sz) = arr_box.deref();
typ = new_typ;
let sz = sz.unwrap_value().unwrap_integer();
let sz = sz.unwrap_integer();
write!(
for_stack,
"for({for_should_declare_var}{var_name} = 0; {var_name} < {sz}; {var_name} = {var_name} + 1) "
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<'g> CodeGenerationContext<'g> {
writeln!(self.program_text, "{wire_or_reg}{wire_decl};").unwrap();

let mut path = String::new();
for n in 0_usize..*rank {
for n in 0..rank.len() {
path.push_str(&format!("[_i{n}]"));
writeln!(self.program_text, "foreach ({wire_name}{path}) begin").unwrap();
}
Expand All @@ -361,7 +361,7 @@ impl<'g> CodeGenerationContext<'g> {
)
.unwrap();

for _n in 0_usize..*rank {
for _n in rank {
writeln!(self.program_text, "end").unwrap();
}
}
Expand All @@ -387,7 +387,7 @@ impl<'g> CodeGenerationContext<'g> {

let mut path = String::new();

for n in 0_usize..*rank {
for n in 0..rank.len() {
path.push_str(&format!("[_i{n}]"));
writeln!(self.program_text, "foreach ({wire_name}{path}) begin").unwrap();
}
Expand All @@ -401,7 +401,7 @@ impl<'g> CodeGenerationContext<'g> {
)
.unwrap();

for _n in 0_usize..*rank {
for _n in rank {
writeln!(self.program_text, "end").unwrap();
}
}
Expand Down Expand Up @@ -488,19 +488,20 @@ impl<'g> CodeGenerationContext<'g> {
fn write_template_args(
&mut self,
link_info: &LinkInfo,
concrete_template_args: &TVec<ConcreteType>,
concrete_template_args: &TVec<ConcreteTemplateArg>,
) {
self.program_text.write_str(&link_info.name).unwrap();
self.program_text.write_str(" #(").unwrap();
let mut first = true;
concrete_template_args.iter().for_each(|(arg_id, arg)| {
let arg_name = &link_info.template_parameters[arg_id].name;
let arg_value = match arg {
ConcreteType::Named(..) | ConcreteType::Array(..) => {
unreachable!("No extern module type arguments. Should have been caught by Lint")
TemplateKind::Type(_) => {
unreachable!(
"No extern module type arguments. Should have been caught by Lint"
);
}
ConcreteType::Value(value) => value.inline_constant_to_string(),
ConcreteType::Unknown(_) => unreachable!("All args are known at codegen"),
TemplateKind::Value(value) => value.inline_constant_to_string(),
};
if first {
self.program_text.write_char(',').unwrap();
Expand Down Expand Up @@ -593,8 +594,8 @@ impl<'g> CodeGenerationContext<'g> {
self.program_text.write_str("\tassign out = in;\n").unwrap();
}
"IntToBits" => {
let [num_bits] = self.instance.global_ref.template_args.cast_to_array();
let num_bits: usize = num_bits.unwrap_value().unwrap_int();
let [num_bits] = self.instance.global_ref.template_args.cast_to_int_array();
let num_bits: usize = num_bits.try_into().unwrap();

let _value_port = self
.md
Expand All @@ -607,8 +608,8 @@ impl<'g> CodeGenerationContext<'g> {
}
}
"BitsToInt" => {
let [num_bits] = self.instance.global_ref.template_args.cast_to_array();
let num_bits: usize = num_bits.unwrap_value().unwrap_int();
let [num_bits] = self.instance.global_ref.template_args.cast_to_int_array();
let num_bits: usize = num_bits.try_into().unwrap();

let _bits_port = self
.md
Expand Down
3 changes: 1 addition & 2 deletions src/codegen/vhdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn typ_to_declaration(mut typ: &ConcreteType) -> String {
let mut array_string = String::new();
while let ConcreteType::Array(arr) = typ {
let (content_typ, size) = arr.deref();
let sz = size.unwrap_value().unwrap_integer();
let sz = size.unwrap_integer();
write!(array_string, "array (0 to {}) of", sz - 1).unwrap();
typ = content_typ;
}
Expand All @@ -56,7 +56,6 @@ fn typ_to_declaration(mut typ: &ConcreteType) -> String {
}
}
ConcreteType::Array(_) => unreachable!("All arrays have been used up already"),
ConcreteType::Value(_) | ConcreteType::Unknown(_) => unreachable!(),
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/compiler_top.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str::FromStr;

use crate::config::EarlyExitUpTo;
Expand Down Expand Up @@ -236,10 +235,10 @@ impl Linker {
if md.link_info.template_parameters.is_empty() {
let _inst = self.instantiator.instantiate(
self,
Rc::new(ConcreteGlobalReference {
ConcreteGlobalReference {
id,
template_args: FlatAlloc::new(),
}),
},
);
}
}
Expand Down
Loading
Loading