Skip to content

Commit

Permalink
Finally got basic working instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Nov 27, 2023
1 parent f0330be commit b7e3064
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 165 deletions.
90 changes: 85 additions & 5 deletions multiply_add.sus
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module packer : T[256] data /* v _ _ _ */ -> T[64] out { /* v v v v */
}


module multiply_add : i32 a, i32 b, i32 c -> i32 result, double double_result {
module multiply_add_old : i32 a, i32 b, i32 c -> i32 result, double double_result {
i32 tmp = a * b;
reg result = tmp + c;
reg double_result = cvt_to_double(result);
Expand Down Expand Up @@ -200,14 +200,35 @@ module b: HandShake hs -> {



module multiply_add :
int a,
int b,
int c
-> int total {

reg int tmp = a * b;
total = tmp + c;
}

//timeline (v, true -> /) .. (v, false -> v)*
module blur2 :
int data,
bool first
-> int blurred {

state int prev;

if !first {
blurred = data + prev;
}
prev = data;
}


module Tree_Multiply : int[4] values -> int total {
int a = values[0] * values[1];
reg int a = values[0] * values[1];
int b = values[2] * values[3];
reg total = a * b;
total = a * b;
}


Expand All @@ -221,8 +242,7 @@ module Accumulator : int term, bool done -> int total {
int new_tot = tot + term;
if done {
total = new_tot;
tot = 0;
//finish; // packet is hereby finished.
tot = 0; // Must restore initial conditions
} else {
tot = new_tot;
}
Expand Down Expand Up @@ -266,3 +286,63 @@ module Unpack4 : int[4] packed -> int out_stream {
//finish; // packet is hereby finished.
}
}




//timeline (bs -> /, true) | (bs -> v, false)
module first_bit_idx_6 : bool[6] bits -> int first, bool all_zeros {
if bits[0] {
first = 0;
all_zeros = false;
} else if bits[1] {
first = 1;
all_zeros = false;
} else if bits[2] {
first = 2;
all_zeros = false;
} else if bits[3] {
first = 3;
all_zeros = false;
} else if bits[4] {
first = 4;
all_zeros = false;
} else if bits[5] {
first = 5;
all_zeros = false;
} else {
all_zeros = true;
}
}

module first_bit_idx_24 : bool[24] bits -> int first {
int[4] offsets;
bool[4] was_nonzero;

for i in 0..4 {
offsets[i], was_nonzero[i] = first_bit_idx_6(bits[i*6+:6]);
}
}

module permute : bool[128] mbf, int selected_permutation -> bool[128] permuted_mbf {
// TODO
}

//timeline (X, [false;24], true -> /, false) | (X, vs, true -> X, true) .. (/, /, false -> X, true)*
module permute24 : bool[128] mbf, bool[24] valid_permutes, bool start -> bool[128] permuted_out, bool permuted_out_valid {
state bool[128] stored_mbf;
state bool[24] stored_valid_permutes = 000000000000000000000000;


bool[24] permutes_to_keep;
permutes_to_keep[0] = false;
for i in 1..24 {
permutes_to_keep[i] = permutes_to_keep[i-1] | stored_valid_permutes[i-1];
}

int current_permutation_idx = first_bit_idx_24(permutes_to_keep);

stored_valid_permutes = stored_valid_permutes & permutes_to_keep;

permuted_out = permute(stored_mbf, current_permutation_idx);
}
6 changes: 2 additions & 4 deletions resetNormalizer.sus
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

module hello_from_the_other_side : int a -> int result {
if true {
result = a;
}
module hello_from_the_other_side : int[3] a -> int result {
result = a[2];
}
37 changes: 24 additions & 13 deletions src/arena_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{ops::{IndexMut, Index}, marker::PhantomData, iter::Enumerate, fmt};

use crate::block_vector::{BlockVec, BlockVecIterMut, BlockVecIter};

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct UUID<IndexMarker : UUIDMarker>(usize, PhantomData<IndexMarker>);

Expand All @@ -24,8 +26,10 @@ impl<IndexMarker : UUIDMarker> Default for UUID<IndexMarker> {
}
}

const INVALID_UUID_VALUE : usize = usize::MAX;

impl<IndexMarker : UUIDMarker> UUID<IndexMarker> {
pub const INVALID : Self = UUID(usize::MAX, PhantomData);
pub const INVALID : Self = UUID(INVALID_UUID_VALUE, PhantomData);

pub const fn from_hidden_value(v : usize) -> Self {
UUID(v, PhantomData)
Expand Down Expand Up @@ -93,13 +97,15 @@ impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for ArenaAllocator<T,
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
assert!(self.data[uuid].is_some());
self.data[uuid].as_ref().unwrap()
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for ArenaAllocator<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
assert!(self.data[uuid].is_some());
self.data[uuid].as_mut().unwrap()
}
Expand Down Expand Up @@ -202,12 +208,14 @@ impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for ArenaVector<T, In
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
self.data[uuid].as_ref().unwrap()
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for ArenaVector<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
self.data[uuid].as_mut().unwrap()
}
}
Expand All @@ -234,47 +242,50 @@ impl<'a, T, IndexMarker : UUIDMarker> IntoIterator for &'a mut ArenaVector<T, In

#[derive(Debug)]
pub struct ListAllocator<T, IndexMarker : UUIDMarker> {
data : Vec<T>,
data : BlockVec<T>,
_ph : PhantomData<IndexMarker>
}

impl<T, IndexMarker : UUIDMarker> ListAllocator<T, IndexMarker> {
pub fn new() -> Self {
Self{data : Vec::new(), _ph : PhantomData}
Self{data : BlockVec::new(), _ph : PhantomData}
}
pub fn alloc(&mut self, v : T) -> UUID<IndexMarker> {
let uuid = UUID(self.data.len(), PhantomData);
self.data.push(v);
uuid
// Allocation is const because it doesn't invalidate existing references
pub fn alloc(&self, v : T) -> UUID<IndexMarker> {
UUID(self.data.alloc(v), PhantomData)
}
pub fn iter<'a>(&'a self) -> ListAllocIterator<'a, T, IndexMarker> {
self.into_iter()
}
pub fn iter_mut<'a>(&'a mut self) -> ListAllocIteratorMut<'a, T, IndexMarker> {
self.into_iter()
}
pub fn map<OutT, F : FnMut(UUID<IndexMarker>, &T) -> OutT>(&self, mut f : F) -> ListAllocator<OutT, IndexMarker> {
let data = self.iter().map(|(a, v)| f(a, v)).collect();
ListAllocator{data, _ph : PhantomData}
}
}

impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for ListAllocator<T, IndexMarker> {
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
&self.data[uuid]
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for ListAllocator<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
&mut self.data[uuid]
}
}

impl<T, IndexMarker : UUIDMarker> FromIterator<T> for ListAllocator<T, IndexMarker> {
fn from_iter<Iter: IntoIterator<Item = T>>(iter: Iter) -> Self {
Self { data: BlockVec::from_iter(iter), _ph: PhantomData }
}
}

pub struct ListAllocIterator<'a, T, IndexMarker> {
it: Enumerate<std::slice::Iter<'a, T>>,
it: Enumerate<BlockVecIter<'a, T>>,
_ph : PhantomData<IndexMarker>
}

Expand All @@ -294,7 +305,7 @@ impl<'a, T, IndexMarker : UUIDMarker> Iterator for ListAllocIterator<'a, T, Inde
}

pub struct ListAllocIteratorMut<'a, T, IndexMarker> {
it: Enumerate<std::slice::IterMut<'a, T>>,
it: Enumerate<BlockVecIterMut<'a, T>>,
_ph : PhantomData<IndexMarker>
}

Expand Down
29 changes: 19 additions & 10 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@

use num::bigint::BigUint;

use crate::{tokenizer::{TokenTypeIdx, get_token_type_name}, linker::{NamedUUID, FileUUID}, flattening::{FlattenedModule, FlatIDMarker, FlatID, FlattenedInterface}, arena_alloc::ListAllocator};
use crate::{tokenizer::{TokenTypeIdx, get_token_type_name}, linker::{NamedUUID, FileUUID}, flattening::{FlattenedModule, FlattenedInterface}, arena_alloc::{ListAllocator, UUIDMarker, UUID}, instantiation::InstantiationList};
use core::ops::Range;
use std::fmt::Display;

// Token span. Indices are INCLUSIVE
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
pub struct Span(pub usize, pub usize);


#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash)]
pub struct DeclIDMarker;
impl UUIDMarker for DeclIDMarker {const DISPLAY_NAME : &'static str = "obj_";}
pub type DeclID = UUID<DeclIDMarker>;


impl Span {
pub fn to_range<T : Clone>(&self, tokens : &[Range<T>]) -> Range<T> {
let min = tokens[self.0].start.clone();
Expand Down Expand Up @@ -43,7 +50,7 @@ impl From<usize> for Span {

#[derive(Debug, Clone, Copy)]
pub enum LocalOrGlobal {
Local(FlatID),
Local(DeclID),
Global(usize)
}

Expand Down Expand Up @@ -87,7 +94,7 @@ pub enum Expression {
Constant(Value),
UnaryOp(Box<(Operator, usize/*Operator token */, SpanExpression)>),
BinOp(Box<(SpanExpression, Operator, usize/*Operator token */, SpanExpression)>),
Array(Box<(SpanExpression, SpanExpression)>), // first[second]
Array(Box<(SpanExpression, SpanExpression, Span/*Brackets */)>), // first[second]
FuncCall(Vec<SpanExpression>) // first(second, third, ...)
}

Expand All @@ -103,8 +110,8 @@ pub type SpanStatement = (Statement, Span);

#[derive(Debug)]
pub enum AssignableExpression {
Named{local_idx : FlatID},
ArrayIndex(Box<(SpanAssignableExpression, SpanExpression)>)
Named{local_idx : DeclID},
ArrayIndex(Box<(SpanAssignableExpression, SpanExpression, Span/* Brackets */)>)
}

#[derive(Debug)]
Expand All @@ -120,7 +127,7 @@ pub struct CodeBlock {

#[derive(Debug)]
pub enum Statement {
Declaration(FlatID),
Declaration(DeclID),
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : SpanExpression}, // num_regs v = expr;
If{condition : SpanExpression, then : CodeBlock, els : Option<CodeBlock>},
Block(CodeBlock),
Expand All @@ -141,11 +148,13 @@ pub struct LinkInfo {
pub struct Module {
pub link_info : LinkInfo,

pub declarations : ListAllocator<SignalDeclaration, FlatIDMarker>,
pub declarations : ListAllocator<SignalDeclaration, DeclIDMarker>,
pub code : CodeBlock,

pub interface : FlattenedInterface,
pub flattened : FlattenedModule
pub flattened : FlattenedModule,

pub instantiations : InstantiationList
}

#[derive(Debug,Clone,Copy)]
Expand Down Expand Up @@ -184,7 +193,7 @@ impl IterIdentifiers for SpanExpression {
}
}
Expression::Array(b) => {
let (array, idx) = &**b;
let (array, idx, bracket_span) = &**b;
array.for_each_value(func);
idx.for_each_value(func);
}
Expand All @@ -201,7 +210,7 @@ impl IterIdentifiers for SpanAssignableExpression {
func(LocalOrGlobal::Local(*id), span.0);
}
AssignableExpression::ArrayIndex(b) => {
let (array, idx) = &**b;
let (array, idx, bracket_span) = &**b;
array.for_each_value(func);
idx.for_each_value(func);
}
Expand Down
Loading

0 comments on commit b7e3064

Please sign in to comment.