Skip to content

Commit

Permalink
Initial work for custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Aug 10, 2024
1 parent 0f8e062 commit 29aab87
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 81 deletions.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ lsp = ["lsp-server", "lsp-types", "serde_json", "serde"]

[profile.release]
debug = true

[profile.smol]
debug = false
strip = true
opt-level = 'z'
codegen-units = 1
lto = true
panic = 'abort'
inherits = "release"
6 changes: 2 additions & 4 deletions bitSerialMatrixMultiply.sus
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ module BitSerialMatrixMultiplyState {
for int Y in 0..HEIGHT {
BitSerialRow::<SIZE = WIDTH, WEIGHTS = MATRIX[Y];> row

if feed {
result_vector[Y] = result_vector[Y] * 2 + row(vector_bits)
}

if start {
result_vector[Y] = 0
} else if feed {
result_vector[Y] = result_vector[Y] * 2 + row(vector_bits)
}
}
}
Expand Down
Binary file modified philosophy/images/bitSerialMatMul.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified philosophy/images/bitSerialRow.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/dev_aid/lsp/tree_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ impl<'linker, Visitor: FnMut(Span, LocationInfo<'linker>), Pruner: Fn(Span) -> b
self.walk_module(md_id);
}
NameElem::Type(_) => {
todo!()
println!("TODO: tree_walk::walk_file of NameElem::Type")
}
NameElem::Constant(_) => {
todo!()
Expand Down
16 changes: 11 additions & 5 deletions src/flattening/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1580,11 +1580,17 @@ pub fn flatten_all_modules(linker: &mut Linker) {
// Skip because we know this from initialization.
cursor.field(field!("object_type"));

let Some(NameElem::Module(module_uuid)) = associated_value_iter.next() else {
unreachable!()
};

flatten(linker_ptr, *module_uuid, cursor);
match associated_value_iter.next().expect("Iterator cannot be exhausted") {
NameElem::Module(module_uuid) => {
flatten(linker_ptr, *module_uuid, cursor);
}
NameElem::Type(type_uuid) => {
println!("TODO Type flattening")
}
NameElem::Constant(const_uuid) => {
println!("TODO Constant flattening")
}
}
});
});
span_debugger.defuse();
Expand Down
159 changes: 94 additions & 65 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ use crate::typing::template::{
use super::parser::Cursor;
use super::*;

struct ModuleInitializationContext<'linker> {
struct InitializationContext<'linker> {
template_inputs: TemplateInputs,

// module-only stuff
ports: FlatAlloc<Port, PortIDMarker>,
interfaces: FlatAlloc<Interface, InterfaceIDMarker>,
domains: FlatAlloc<String, DomainIDMarker>,
template_inputs: TemplateInputs,

// struct-only stuff
fields: FlatAlloc<StructField, FieldIDMarker>,

file_text: &'linker FileText,
}

impl<'linker> ModuleInitializationContext<'linker> {
fn gather_initial_module(&mut self, cursor: &mut Cursor) -> (Span, String) {
impl<'linker> InitializationContext<'linker> {
fn gather_initial_global_object(&mut self, cursor: &mut Cursor) -> (Span, String) {
let name_span = cursor.field_span(field!("name"), kind!("identifier"));
let name = self.file_text[name_span].to_owned();
self.domains.alloc(name.clone());
Expand Down Expand Up @@ -184,10 +190,8 @@ impl<'linker> ModuleInitializationContext<'linker> {
}

fn finish_gather_decl(&mut self, is_input: Option<bool>, whole_decl_span: Span, cursor: &mut Cursor) {
if is_input.is_none() {return}; // TODO early return now

// If generative input it's a template arg
let is_gen = if cursor.optional_field(field!("declaration_modifiers")) {
let is_generative = if cursor.optional_field(field!("declaration_modifiers")) {
cursor.kind() == kw!("gen")
} else {
false
Expand All @@ -198,17 +202,19 @@ impl<'linker> ModuleInitializationContext<'linker> {
let decl_span = Span::new_overarching(type_span, whole_decl_span.empty_span_at_end());
let name_span = cursor.field_span(field!("name"), kind!("identifier"));
let name = self.file_text[name_span].to_owned();
if is_gen {
self.template_inputs.alloc(TemplateInput {
name,
name_span,
kind: TemplateInputKind::Generative(GenerativeTemplateInputKind {
decl_span,
declaration_instruction: FlatID::PLACEHOLDER,
}),
});
} else {
if let Some(is_input) = is_input {

match (is_generative, is_input) {
(true, Some(true)) => {
self.template_inputs.alloc(TemplateInput {
name,
name_span,
kind: TemplateInputKind::Generative(GenerativeTemplateInputKind {
decl_span,
declaration_instruction: FlatID::PLACEHOLDER,
}),
});
}
(false, Some(is_input)) => {
self.ports.alloc(Port {
name,
name_span,
Expand All @@ -218,6 +224,15 @@ impl<'linker> ModuleInitializationContext<'linker> {
declaration_instruction: FlatID::PLACEHOLDER,
});
}
(false, None) => {
self.fields.alloc(StructField{
name: name.clone(),
name_span,
decl_span,
declaration_instruction: FlatID::PLACEHOLDER
});
}
_other => {}
}
}
}
Expand All @@ -231,73 +246,87 @@ pub fn gather_initial_file_data(mut builder: FileBuilder) {
let parsing_errors = ErrorCollector::new_empty(builder.file_id, builder.files);
cursor.report_all_decendant_errors(&parsing_errors);

let span = cursor.span();
cursor.go_down(kind!("global_object"), |cursor| {
let span = cursor.span();
let extern_kw = cursor.optional_field(field!("extern_marker")).then(|| cursor.kind());
cursor.field(field!("object_type"));
let global_obj_kind = match cursor.kind() {
kw!("module") => {
GlobalObjectKind::Module
}
kw!("function") => {
GlobalObjectKind::Functions
}
kw!("struct") => {
GlobalObjectKind::Struct
}
_other => cursor.could_not_match()
};

initialize_module(&mut builder, extern_kw, parsing_errors, span, cursor);
initialize_global_object(&mut builder, parsing_errors, span, cursor);
});
},
);
}

fn initialize_module(builder: &mut FileBuilder, extern_kw : Option<u16>, parsing_errors: ErrorCollector, span: Span, cursor: &mut Cursor) {
let mut ctx = ModuleInitializationContext {
fn initialize_global_object(builder: &mut FileBuilder, parsing_errors: ErrorCollector, span: Span, cursor: &mut Cursor) {
let is_extern = match cursor.optional_field(field!("extern_marker")).then(|| cursor.kind()) {
None => IsExtern::Normal,
Some(kw!("extern")) => IsExtern::Extern,
Some(kw!("__builtin__")) => IsExtern::Builtin,
Some(_) => cursor.could_not_match()
};

cursor.field(field!("object_type"));
let global_obj_kind = match cursor.kind() {
kw!("module") => {
GlobalObjectKind::Module
}
kw!("function") => {
GlobalObjectKind::Function
}
kw!("struct") => {
GlobalObjectKind::Struct
}
_other => cursor.could_not_match()
};

let mut ctx = InitializationContext {
ports: FlatAlloc::new(),
interfaces: FlatAlloc::new(),
domains: FlatAlloc::new(),
template_inputs: FlatAlloc::new(),
fields: FlatAlloc::new(),
file_text: &builder.file_data.file_text,
};

let (name_span, name) = ctx.gather_initial_module(cursor);
let (name_span, name) = ctx.gather_initial_global_object(cursor);

let resolved_globals = ResolvedGlobals::empty();
let errors = parsing_errors.into_storage();
let after_initial_parse_cp =
CheckPoint::checkpoint(&errors, &resolved_globals);

let is_extern = match extern_kw {
None => IsExtern::Normal,
Some(kw!("extern")) => IsExtern::Extern,
Some(kw!("__builtin__")) => IsExtern::Builtin,
Some(_) => cursor.could_not_match()
let link_info = LinkInfo {
documentation: cursor.extract_gathered_comments(),
file: builder.file_id,
name,
name_span,
span,
errors,
is_extern,
resolved_globals,
template_arguments: ctx.template_inputs,
after_initial_parse_cp,
after_flatten_cp: None,
};

let md = Module {
link_info: LinkInfo {
documentation: cursor.extract_gathered_comments(),
file: builder.file_id,
name,
name_span,
span,
errors,
is_extern,
resolved_globals,
template_arguments: ctx.template_inputs,
after_initial_parse_cp,
after_flatten_cp: None,
},
instructions: FlatAlloc::new(),
ports: ctx.ports,
domain_names: ctx.domains,
domains: FlatAlloc::new(),
interfaces: ctx.interfaces,
instantiations: InstantiationList::new(),
};
match global_obj_kind {
GlobalObjectKind::Module | GlobalObjectKind::Function => {
let md = Module {
link_info,
instructions: FlatAlloc::new(),
ports: ctx.ports,
domain_names: ctx.domains,
domains: FlatAlloc::new(),
interfaces: ctx.interfaces,
instantiations: InstantiationList::new(),
};

builder.add_module(md);
}
GlobalObjectKind::Struct => {
let typ = StructType {
link_info,
fields: ctx.fields
};

builder.add_module(md);
builder.add_type(typ);
}
}
}
23 changes: 22 additions & 1 deletion src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::typing::{
#[derive(Debug)]
pub enum GlobalObjectKind {
Module,
Functions,
Function,
Struct
}

Expand Down Expand Up @@ -133,6 +133,27 @@ impl Module {
}
}

#[derive(Debug)]
pub struct StructType {
/// Created in Stage 1: Initialization
pub link_info : LinkInfo,

/// Created in Stage 1: Initialization
///
/// [StructField::declaration_instruction] are set in Stage 2: Flattening
fields: FlatAlloc<StructField, FieldIDMarker>,
}

#[derive(Debug)]
pub struct StructField {
pub name: String,
pub name_span: Span,
pub decl_span: Span,
/// This is only set after flattening is done. Initially just [UUID::PLACEHOLDER]
pub declaration_instruction: FlatID,
}


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PortOrInterface {
Port(PortID),
Expand Down
36 changes: 31 additions & 5 deletions src/linker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::{

use crate::errors::{CompileError, ErrorInfo, ErrorLevel, ErrorStore};

use crate::flattening::StructType;

use crate::typing::{
abstract_type::{DomainType, FullType},
concrete_type::ConcreteType,
Expand Down Expand Up @@ -151,6 +153,7 @@ impl NamedConstant {
#[derive(Debug)]
pub enum NamedType {
Builtin(&'static str),
Struct(StructType)
}

impl Linkable for NamedConstant {
Expand All @@ -174,21 +177,37 @@ impl Linkable for NamedConstant {
}

impl Linkable for NamedType {
fn get_name(&self) -> &'static str {
fn get_name(&self) -> &str {
match self {
NamedType::Builtin(name) => name,
NamedType::Struct(s) => &s.link_info.name
}
}
fn get_linking_error_location(&self) -> LinkingErrorLocation {
LinkingErrorLocation {
named_type: "Builtin Type",
full_name: self.get_full_name(),
location: None,

match self {
NamedType::Builtin(_) => {
LinkingErrorLocation {
named_type: "Builtin Type",
full_name: self.get_full_name(),
location: None,
}
}
NamedType::Struct(typ) => {
LinkingErrorLocation {
named_type: "Struct",
full_name: typ.link_info.get_full_name(),
location: Some((typ.link_info.name_span, typ.link_info.file)),
}
}
}
}
fn get_link_info(&self) -> Option<&LinkInfo> {
match self {
NamedType::Builtin(_) => None,
NamedType::Struct(typ) => {
Some(&typ.link_info)
}
}
}
}
Expand Down Expand Up @@ -501,4 +520,11 @@ impl<'linker> FileBuilder<'linker> {
self.associated_values.push(new_module_uuid);
self.add_name(module_name, new_module_uuid);
}

pub fn add_type(&mut self, typ: StructType) {
let type_name = typ.link_info.name.clone();
let new_type_uuid = NameElem::Type(self.types.alloc(NamedType::Struct(typ)));
self.associated_values.push(new_type_uuid);
self.add_name(type_name, new_type_uuid);
}
}
Loading

0 comments on commit 29aab87

Please sign in to comment.