Skip to content

Commit 2404293

Browse files
authored
Merge pull request #66 from pc2/28-ranged-int-2
Ranged Integers
2 parents 2fdf2b2 + 46beec4 commit 2404293

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+14630
-4299
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
// "sus_lsp.executable_path": "${workspaceFolder}/target/release/sus_compiler",
55
"sus_lsp.tcp_port": 25000,
66
"sus_lsp.args": [
7-
"--lsp"
7+
"--lsp",
8+
// Tracking down slow compilations
9+
"--kill-timeout", "2000ms"
810
//, "--upto", "typecheck"
911
],
1012
"editor.lineHeight": 0,

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ lsp-server = {version = "0.7.1", optional = true}
5151
lsp-types = {version = "0.94.0", optional = true}
5252
serde_json = {version = "1.0.97", optional = true}
5353
serde = {version = "1.0.156", optional = true}
54+
humantime = "2.2.0"
5455

5556
[build-dependencies]
5657
dirs-next = "2.0.0"

bad_syntax.sus

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// This is there to check for ICEs
33

44

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

1010
// test
1111
module contains_submodule {
12-
interface contains_submodule : int a, int b, int c -> int r
12+
interface contains_submodule : int#(MIN: 0, MAX: 1) a, int b, int c -> int r
1313
int tmp = multiply(a, b)
1414
reg r = tmp + c
1515
}

src/alloc.rs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ impl<IndexMarker> UUIDAllocator<IndexMarker> {
9191
self.cur.0 += 1;
9292
allocated_id
9393
}
94-
pub fn peek(&self) -> UUID<IndexMarker> {
95-
self.cur
96-
}
9794
pub fn to_flat_alloc<T: Default>(&self) -> FlatAlloc<T, IndexMarker> {
9895
let mut result = FlatAlloc::with_capacity(self.cur.0);
9996
for _ in 0..self.cur.0 {
@@ -303,6 +300,10 @@ impl<T, IndexMarker> ArenaAllocator<T, IndexMarker> {
303300
PhantomData,
304301
)
305302
}
303+
pub fn free_reservation(&mut self, UUID(uuid, _): UUID<IndexMarker>) {
304+
assert!(self.data[uuid].is_none());
305+
self.free_slots.push(uuid);
306+
}
306307
pub fn revert_to_reservation(&mut self, UUID(uuid, _): UUID<IndexMarker>) {
307308
assert!(self.data[uuid].is_some());
308309
self.data[uuid] = None;
@@ -353,6 +354,15 @@ impl<T, IndexMarker> ArenaAllocator<T, IndexMarker> {
353354
.find(|(id, v)| predicate(*id, v))
354355
.map(|(id, _)| id)
355356
}
357+
#[track_caller]
358+
pub fn get2_mut(
359+
&mut self,
360+
UUID(uuid_a, _): UUID<IndexMarker>,
361+
UUID(uuid_b, _): UUID<IndexMarker>,
362+
) -> Option<(&mut T, &mut T)> {
363+
get2_mut(&mut self.data, uuid_a, uuid_b)
364+
.map(|(a, b)| (a.as_mut().unwrap(), b.as_mut().unwrap()))
365+
}
356366
}
357367

358368
impl<T, IndexMarker> Index<UUID<IndexMarker>> for ArenaAllocator<T, IndexMarker> {
@@ -614,8 +624,6 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
614624
_ph: PhantomData,
615625
}
616626
}
617-
#[cfg(test)]
618-
// Only for testing, so only enabled with test flag
619627
pub fn from_vec(data: Vec<T>) -> Self {
620628
Self {
621629
data,
@@ -678,17 +686,6 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
678686
_ph: PhantomData,
679687
}
680688
}
681-
pub fn cast_to_array<const N: usize>(&self) -> &[T; N] {
682-
assert!(self.len() == N);
683-
self.data.as_slice().try_into().unwrap()
684-
}
685-
pub fn map_to_array<O, const N: usize>(
686-
&self,
687-
mut f: impl FnMut(UUID<IndexMarker>, &T) -> O,
688-
) -> [O; N] {
689-
assert!(self.len() == N);
690-
std::array::from_fn(|i| f(UUID::from_hidden_value(i), &self.data[i]))
691-
}
692689
pub fn try_map<OT, ErrT>(
693690
&self,
694691
mut f: impl FnMut((UUID<IndexMarker>, &T)) -> Result<OT, ErrT>,
@@ -702,6 +699,47 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
702699
_ph: PhantomData,
703700
})
704701
}
702+
pub fn try_map2<T2, OT, ET>(
703+
&self,
704+
second: &FlatAlloc<T2, IndexMarker>,
705+
mut f: impl FnMut((UUID<IndexMarker>, &T, &T2)) -> Result<OT, ET>,
706+
) -> Result<FlatAlloc<OT, IndexMarker>, ET> {
707+
let mut data = Vec::with_capacity(self.len());
708+
for v in zip_eq(self.iter(), second.iter()) {
709+
data.push(f(v)?);
710+
}
711+
Ok(FlatAlloc {
712+
data,
713+
_ph: PhantomData,
714+
})
715+
}
716+
pub fn try_map3<T2, T3, OT, ET>(
717+
&self,
718+
second: &FlatAlloc<T2, IndexMarker>,
719+
third: &FlatAlloc<T3, IndexMarker>,
720+
mut f: impl FnMut((UUID<IndexMarker>, &T, &T2, &T3)) -> Result<OT, ET>,
721+
) -> Result<FlatAlloc<OT, IndexMarker>, ET> {
722+
let mut data = Vec::with_capacity(self.len());
723+
for v in zip_eq3(self.iter(), second.iter(), third.iter()) {
724+
data.push(f(v)?);
725+
}
726+
Ok(FlatAlloc {
727+
data,
728+
_ph: PhantomData,
729+
})
730+
}
731+
pub fn cast_to_array<const N: usize>(&self) -> [&T; N] {
732+
assert_eq!(self.len(), N);
733+
std::array::from_fn(|i| &self.data[i])
734+
}
735+
pub fn cast_to_array_mut<const N: usize>(&mut self) -> [&mut T; N] {
736+
assert_eq!(self.len(), N);
737+
std::array::from_fn(|i| {
738+
let ptr: *mut T = &mut self.data[i];
739+
// SAFETY: Of course, the data[i] accesses are all disjoint
740+
unsafe { &mut *ptr }
741+
})
742+
}
705743
pub fn find(
706744
&self,
707745
mut predicate: impl FnMut(UUID<IndexMarker>, &T) -> bool,

src/append_only_vec.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::cell::UnsafeCell;
2+
3+
/// 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
4+
#[derive(Debug)]
5+
pub struct AppendOnlyVec<T> {
6+
v: UnsafeCell<Vec<T>>,
7+
}
8+
9+
impl<T> Default for AppendOnlyVec<T> {
10+
fn default() -> Self {
11+
Self {
12+
v: UnsafeCell::new(Vec::new()),
13+
}
14+
}
15+
}
16+
17+
impl<T> From<Vec<T>> for AppendOnlyVec<T> {
18+
fn from(existing_vec: Vec<T>) -> Self {
19+
Self {
20+
v: UnsafeCell::new(existing_vec),
21+
}
22+
}
23+
}
24+
25+
impl<T> AppendOnlyVec<T> {
26+
pub fn push(&self, data: T) {
27+
// 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
28+
unsafe {
29+
(*self.v.get()).push(data);
30+
}
31+
}
32+
pub fn new() -> Self {
33+
Self::default()
34+
}
35+
}
36+
37+
impl<T> From<AppendOnlyVec<T>> for Vec<T> {
38+
fn from(val: AppendOnlyVec<T>) -> Self {
39+
val.v.into_inner()
40+
}
41+
}
42+
43+
impl<T> IntoIterator for AppendOnlyVec<T> {
44+
type Item = T;
45+
type IntoIter = std::vec::IntoIter<T>;
46+
47+
fn into_iter(self) -> Self::IntoIter {
48+
self.v.into_inner().into_iter()
49+
}
50+
}

src/codegen/system_verilog.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::flattening::{DeclarationKind, Instruction, Module, Port};
99
use crate::instantiation::{
1010
InstantiatedModule, MultiplexerSource, RealWire, RealWireDataSource, RealWirePathElem,
1111
};
12-
use crate::typing::template::TVec;
12+
use crate::typing::concrete_type::ConcreteTemplateArg;
13+
use crate::typing::template::{TVec, TemplateKind};
1314
use crate::{typing::concrete_type::ConcreteType, value::Value};
1415

1516
use super::shared::*;
@@ -43,7 +44,7 @@ fn typ_to_declaration(mut typ: &ConcreteType, var_name: &str) -> String {
4344
let mut array_string = String::new();
4445
while let ConcreteType::Array(arr) = typ {
4546
let (content_typ, size) = arr.deref();
46-
let sz = size.unwrap_value().unwrap_integer();
47+
let sz = size.unwrap_integer();
4748
write!(array_string, "[{}:0]", sz - 1).unwrap();
4849
typ = content_typ;
4950
}
@@ -57,7 +58,6 @@ fn typ_to_declaration(mut typ: &ConcreteType, var_name: &str) -> String {
5758
}
5859
}
5960
ConcreteType::Array(_) => unreachable!("All arrays have been used up already"),
60-
ConcreteType::Value(_) | ConcreteType::Unknown(_) => unreachable!(),
6161
}
6262
}
6363

@@ -251,7 +251,7 @@ impl<'g> CodeGenerationContext<'g> {
251251
idx += 1;
252252
let (new_typ, sz) = arr_box.deref();
253253
typ = new_typ;
254-
let sz = sz.unwrap_value().unwrap_integer();
254+
let sz = sz.unwrap_integer();
255255
write!(
256256
for_stack,
257257
"for({for_should_declare_var}{var_name} = 0; {var_name} < {sz}; {var_name} = {var_name} + 1) "
@@ -348,7 +348,7 @@ impl<'g> CodeGenerationContext<'g> {
348348
writeln!(self.program_text, "{wire_or_reg}{wire_decl};").unwrap();
349349

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

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

388388
let mut path = String::new();
389389

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

404-
for _n in 0_usize..*rank {
404+
for _n in rank {
405405
writeln!(self.program_text, "end").unwrap();
406406
}
407407
}
@@ -488,19 +488,20 @@ impl<'g> CodeGenerationContext<'g> {
488488
fn write_template_args(
489489
&mut self,
490490
link_info: &LinkInfo,
491-
concrete_template_args: &TVec<ConcreteType>,
491+
concrete_template_args: &TVec<ConcreteTemplateArg>,
492492
) {
493493
self.program_text.write_str(&link_info.name).unwrap();
494494
self.program_text.write_str(" #(").unwrap();
495495
let mut first = true;
496496
concrete_template_args.iter().for_each(|(arg_id, arg)| {
497497
let arg_name = &link_info.template_parameters[arg_id].name;
498498
let arg_value = match arg {
499-
ConcreteType::Named(..) | ConcreteType::Array(..) => {
500-
unreachable!("No extern module type arguments. Should have been caught by Lint")
499+
TemplateKind::Type(_) => {
500+
unreachable!(
501+
"No extern module type arguments. Should have been caught by Lint"
502+
);
501503
}
502-
ConcreteType::Value(value) => value.inline_constant_to_string(),
503-
ConcreteType::Unknown(_) => unreachable!("All args are known at codegen"),
504+
TemplateKind::Value(value) => value.inline_constant_to_string(),
504505
};
505506
if first {
506507
self.program_text.write_char(',').unwrap();
@@ -593,8 +594,8 @@ impl<'g> CodeGenerationContext<'g> {
593594
self.program_text.write_str("\tassign out = in;\n").unwrap();
594595
}
595596
"IntToBits" => {
596-
let [num_bits] = self.instance.global_ref.template_args.cast_to_array();
597-
let num_bits: usize = num_bits.unwrap_value().unwrap_int();
597+
let [num_bits] = self.instance.global_ref.template_args.cast_to_int_array();
598+
let num_bits: usize = num_bits.try_into().unwrap();
598599

599600
let _value_port = self
600601
.md
@@ -607,8 +608,8 @@ impl<'g> CodeGenerationContext<'g> {
607608
}
608609
}
609610
"BitsToInt" => {
610-
let [num_bits] = self.instance.global_ref.template_args.cast_to_array();
611-
let num_bits: usize = num_bits.unwrap_value().unwrap_int();
611+
let [num_bits] = self.instance.global_ref.template_args.cast_to_int_array();
612+
let num_bits: usize = num_bits.try_into().unwrap();
612613

613614
let _bits_port = self
614615
.md

src/codegen/vhdl.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn typ_to_declaration(mut typ: &ConcreteType) -> String {
4242
let mut array_string = String::new();
4343
while let ConcreteType::Array(arr) = typ {
4444
let (content_typ, size) = arr.deref();
45-
let sz = size.unwrap_value().unwrap_integer();
45+
let sz = size.unwrap_integer();
4646
write!(array_string, "array (0 to {}) of", sz - 1).unwrap();
4747
typ = content_typ;
4848
}
@@ -56,7 +56,6 @@ fn typ_to_declaration(mut typ: &ConcreteType) -> String {
5656
}
5757
}
5858
ConcreteType::Array(_) => unreachable!("All arrays have been used up already"),
59-
ConcreteType::Value(_) | ConcreteType::Unknown(_) => unreachable!(),
6059
}
6160
}
6261

src/compiler_top.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::ffi::OsStr;
22
use std::path::{Path, PathBuf};
3-
use std::rc::Rc;
43
use std::str::FromStr;
54

65
use crate::config::EarlyExitUpTo;
@@ -236,10 +235,10 @@ impl Linker {
236235
if md.link_info.template_parameters.is_empty() {
237236
let _inst = self.instantiator.instantiate(
238237
self,
239-
Rc::new(ConcreteGlobalReference {
238+
ConcreteGlobalReference {
240239
id,
241240
template_args: FlatAlloc::new(),
242-
}),
241+
},
243242
);
244243
}
245244
}

0 commit comments

Comments
 (0)