huge refactor, can now define structs out of order

This commit is contained in:
2025-04-11 01:57:10 -04:00
parent f6a6761262
commit 31c16a263b
24 changed files with 765 additions and 566 deletions

View File

@@ -1,46 +1,51 @@
use std::fmt::Debug;
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct StructID(pub usize);
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct VarID(pub usize);
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct FnID(pub usize);
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct DataID(pub usize);
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct FieldID(pub usize);
use std::{fmt::Debug, marker::PhantomData};
// I had an idea for why these were different... now I don't
pub type Size = u32;
pub type Len = u32;
impl Debug for VarID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "var{}", self.0)
pub struct ID<T>(pub usize, PhantomData<T>);
impl<T> ID<T> {
pub fn new(i: usize) -> Self {
Self(i, PhantomData)
}
}
impl Debug for StructID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ty{}", self.0)
pub trait Named {
const NAME: &str;
}
impl<T> From<usize> for ID<T> {
fn from(value: usize) -> Self {
Self(value, PhantomData)
}
}
impl Debug for FnID {
impl<K: Named> Debug for ID<K> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "fn{}", self.0)
write!(f, "{}{}", K::NAME, self.0)
}
}
impl Debug for DataID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "data{}", self.0)
impl<T> PartialEq for ID<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Debug for FieldID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "field{}", self.0)
impl<T> Eq for ID<T> {}
impl<T> std::hash::Hash for ID<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl<T> Clone for ID<T> {
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
}
}
impl<T> Copy for ID<T> {}

View File

@@ -5,7 +5,7 @@ use std::collections::HashMap;
#[derive(Debug)]
pub struct IRLFunction {
pub instructions: Vec<IRLInstruction>,
pub instructions: Vec<LInstruction>,
pub stack: HashMap<VarID, Size>,
pub subvar_map: HashMap<VarID, VarOffset>,
pub args: Vec<(VarID, Size)>,
@@ -14,7 +14,7 @@ pub struct IRLFunction {
}
#[derive(Debug)]
pub enum IRLInstruction {
pub enum LInstruction {
Mv {
dest: VarID,
dest_offset: Size,
@@ -59,7 +59,7 @@ pub enum IRLInstruction {
Mark(Symbol),
}
impl IRLInstruction {
impl LInstruction {
pub fn is_ret(&self) -> bool {
matches!(self, Self::Ret { .. })
}

View File

@@ -1,41 +1,38 @@
use std::collections::HashMap;
use crate::ir::{AsmBlockArgType, IRUFunction, IRUInstrInst, Size, SymbolSpace, VarOffset};
use crate::ir::{AsmBlockArgType, UInstrInst, Size, SymbolSpace, UFunc, VarOffset};
use super::{
IRLFunction, IRLInstruction, IRUInstruction, IRUProgram, Len, Symbol, SymbolSpaceBuilder, Type,
IRLFunction, LInstruction, Len, Symbol, SymbolSpaceBuilder, Type, UInstruction, UProgram,
VarID,
};
pub struct IRLProgram {
pub struct LProgram {
sym_space: SymbolSpace,
entry: Symbol,
}
// NOTE: there are THREE places here where I specify size (8)
impl IRLProgram {
pub fn create(p: &IRUProgram) -> Result<Self, String> {
let mut start = None;
for (i, f) in p.iter_fns() {
if f.name == "start" {
start = Some(i);
}
}
let start = start.ok_or("no start method found")?;
impl LProgram {
pub fn create(p: &UProgram) -> Result<Self, String> {
let start = p
.names
.lookup::<UFunc>("start")
.ok_or("no start method found")?;
let mut ssbuilder = SymbolSpaceBuilder::with_entries(&[start]);
let entry = ssbuilder.func(&start);
while let Some((sym, i)) = ssbuilder.pop_fn() {
let f = p.fns[i.0].as_ref().unwrap();
let mut fbuilder = IRLFunctionBuilder::new(p, &mut ssbuilder);
let mut fbuilder = LFunctionBuilder::new(p, &mut ssbuilder);
for i in &f.instructions {
fbuilder.insert_instr(i);
}
if fbuilder.instrs.last().is_none_or(|i| !i.is_ret()) {
fbuilder.instrs.push(IRLInstruction::Ret { src: None });
fbuilder.instrs.push(LInstruction::Ret { src: None });
}
let res = fbuilder.finish(f);
ssbuilder.write_fn(sym, res, Some(f.name.clone()));
ssbuilder.write_fn(sym, res, Some(p.names.get(i).to_string()));
}
let sym_space = ssbuilder.finish().expect("we failed the mission");
Ok(Self { sym_space, entry })
@@ -46,10 +43,10 @@ impl IRLProgram {
}
}
pub struct IRLFunctionBuilder<'a> {
program: &'a IRUProgram,
pub struct LFunctionBuilder<'a> {
program: &'a UProgram,
builder: &'a mut SymbolSpaceBuilder,
instrs: Vec<IRLInstruction>,
instrs: Vec<LInstruction>,
stack: HashMap<VarID, Size>,
subvar_map: HashMap<VarID, VarOffset>,
makes_call: bool,
@@ -62,8 +59,8 @@ pub struct LoopCtx {
bot: Symbol,
}
impl<'a> IRLFunctionBuilder<'a> {
pub fn new(program: &'a IRUProgram, builder: &'a mut SymbolSpaceBuilder) -> Self {
impl<'a> LFunctionBuilder<'a> {
pub fn new(program: &'a UProgram, builder: &'a mut SymbolSpaceBuilder) -> Self {
Self {
instrs: Vec::new(),
stack: HashMap::new(),
@@ -92,50 +89,56 @@ impl<'a> IRLFunctionBuilder<'a> {
self.subvar_map.insert(i, off);
}
}
pub fn insert_instr(&mut self, i: &IRUInstrInst) -> Option<Option<String>> {
pub fn insert_instr(&mut self, i: &UInstrInst) -> Option<Option<String>> {
match &i.i {
IRUInstruction::Mv { dest, src } => {
UInstruction::Mv { dest, src } => {
self.alloc_stack(dest.id)?;
self.map_subvar(src.id);
self.instrs.push(IRLInstruction::Mv {
self.instrs.push(LInstruction::Mv {
dest: dest.id,
dest_offset: 0,
src: src.id,
src_offset: 0,
});
}
IRUInstruction::Ref { dest, src } => {
UInstruction::Ref { dest, src } => {
self.alloc_stack(dest.id)?;
self.map_subvar(src.id);
self.instrs.push(IRLInstruction::Ref {
self.instrs.push(LInstruction::Ref {
dest: dest.id,
src: src.id,
});
}
IRUInstruction::LoadData { dest, src } => {
UInstruction::LoadData { dest, src } => {
self.alloc_stack(dest.id)?;
let data = &self.program.data[src.0];
let ddef = self.program.get_data(*src);
let sym = self.builder.ro_data(src, data, Some(ddef.label.clone()));
self.instrs.push(IRLInstruction::LoadData {
let data = self.program.expect(*src);
let sym = self.builder.ro_data(
src,
&data.content,
Some(self.program.names.get(dest.id).to_string()),
);
self.instrs.push(LInstruction::LoadData {
dest: dest.id,
offset: 0,
len: data.len() as Len,
len: data.content.len() as Len,
src: sym,
});
}
IRUInstruction::LoadSlice { dest, src } => {
UInstruction::LoadSlice { dest, src } => {
self.alloc_stack(dest.id)?;
let data = &self.program.data[src.0];
let def = self.program.get_data(*src);
let Type::Array(_, len) = &def.ty else {
let data = self.program.expect(*src);
let Type::Array(_, len) = &data.ty else {
return Some(Some(format!(
"tried to load {} as slice",
self.program.type_name(&def.ty)
self.program.type_name(&data.ty)
)));
};
let sym = self.builder.ro_data(src, data, Some(def.label.clone()));
self.instrs.push(IRLInstruction::LoadAddr {
let sym = self.builder.ro_data(
src,
&data.content,
Some(self.program.names.get(dest.id).to_string()),
);
self.instrs.push(LInstruction::LoadAddr {
dest: dest.id,
offset: 0,
src: sym,
@@ -144,23 +147,23 @@ impl<'a> IRLFunctionBuilder<'a> {
let sym = self
.builder
.anon_ro_data(&(*len as u64).to_le_bytes(), Some(format!("len: {}", len)));
self.instrs.push(IRLInstruction::LoadData {
self.instrs.push(LInstruction::LoadData {
dest: dest.id,
offset: 8,
len: 8,
src: sym,
});
}
IRUInstruction::LoadFn { dest, src } => {
UInstruction::LoadFn { dest, src } => {
self.alloc_stack(dest.id)?;
let sym = self.builder.func(src);
self.instrs.push(IRLInstruction::LoadAddr {
self.instrs.push(LInstruction::LoadAddr {
dest: dest.id,
offset: 0,
src: sym,
});
}
IRUInstruction::Call { dest, f, args } => {
UInstruction::Call { dest, f, args } => {
self.alloc_stack(dest.id);
self.makes_call = true;
let fid = &self.program.fn_map[&f.id];
@@ -171,7 +174,7 @@ impl<'a> IRLFunctionBuilder<'a> {
} else {
None
};
let call = IRLInstruction::Call {
let call = LInstruction::Call {
dest,
f: sym,
args: args
@@ -184,7 +187,7 @@ impl<'a> IRLFunctionBuilder<'a> {
};
self.instrs.push(call);
}
IRUInstruction::AsmBlock { instructions, args } => {
UInstruction::AsmBlock { instructions, args } => {
let mut inputs = Vec::new();
let mut outputs = Vec::new();
for a in args {
@@ -199,15 +202,15 @@ impl<'a> IRLFunctionBuilder<'a> {
}
}
}
self.instrs.push(IRLInstruction::AsmBlock {
self.instrs.push(LInstruction::AsmBlock {
instructions: instructions.clone(),
inputs,
outputs,
})
}
IRUInstruction::Ret { src } => {
UInstruction::Ret { src } => {
self.map_subvar(src.id);
self.instrs.push(IRLInstruction::Ret {
self.instrs.push(LInstruction::Ret {
src: if self.program.size_of_var(src.id).expect("unsized var") == 0 {
None
} else {
@@ -215,9 +218,9 @@ impl<'a> IRLFunctionBuilder<'a> {
},
})
}
IRUInstruction::Construct { dest, fields } => {
UInstruction::Construct { dest, fields } => {
self.alloc_stack(dest.id)?;
let ty = &self.program.get_var(dest.id).ty;
let ty = &self.program.expect(dest.id).ty;
let &Type::Struct { id, ref args } = ty else {
return Some(Some(format!(
"Failed to contruct type {}",
@@ -226,7 +229,7 @@ impl<'a> IRLFunctionBuilder<'a> {
};
for (&fid, var) in fields {
self.map_subvar(var.id);
self.instrs.push(IRLInstruction::Mv {
self.instrs.push(LInstruction::Mv {
dest: dest.id,
src: var.id,
dest_offset: self.program.field_offset(id, fid).expect("field offset"),
@@ -234,19 +237,19 @@ impl<'a> IRLFunctionBuilder<'a> {
})
}
}
IRUInstruction::If { cond, body } => {
UInstruction::If { cond, body } => {
self.map_subvar(cond.id);
let sym = self.builder.reserve();
self.instrs.push(IRLInstruction::Branch {
self.instrs.push(LInstruction::Branch {
to: *sym,
cond: cond.id,
});
for i in body {
self.insert_instr(i);
}
self.instrs.push(IRLInstruction::Mark(*sym));
self.instrs.push(LInstruction::Mark(*sym));
}
IRUInstruction::Loop { body } => {
UInstruction::Loop { body } => {
let top = self.builder.reserve();
let bot = self.builder.reserve();
let old = self.loopp;
@@ -254,21 +257,21 @@ impl<'a> IRLFunctionBuilder<'a> {
bot: *bot,
top: *top,
});
self.instrs.push(IRLInstruction::Mark(*top));
self.instrs.push(LInstruction::Mark(*top));
for i in body {
self.insert_instr(i);
}
self.instrs.push(IRLInstruction::Jump(*top));
self.instrs.push(IRLInstruction::Mark(*bot));
self.instrs.push(LInstruction::Jump(*top));
self.instrs.push(LInstruction::Mark(*bot));
self.loopp = old;
}
IRUInstruction::Break => {
self.instrs.push(IRLInstruction::Jump(
UInstruction::Break => {
self.instrs.push(LInstruction::Jump(
self.loopp.expect("Tried to break outside of loop").bot,
));
}
IRUInstruction::Continue => {
self.instrs.push(IRLInstruction::Jump(
UInstruction::Continue => {
self.instrs.push(LInstruction::Jump(
self.loopp.expect("Tried to break outside of loop").top,
));
}
@@ -276,7 +279,7 @@ impl<'a> IRLFunctionBuilder<'a> {
Some(None)
}
pub fn finish(self, f: &IRUFunction) -> IRLFunction {
pub fn finish(self, f: &UFunc) -> IRLFunction {
IRLFunction {
instructions: self.instrs,
makes_call: self.makes_call,
@@ -292,7 +295,7 @@ impl<'a> IRLFunctionBuilder<'a> {
}
}
impl std::ops::Deref for IRLProgram {
impl std::ops::Deref for LProgram {
type Target = SymbolSpace;
fn deref(&self) -> &Self::Target {

View File

@@ -1,82 +0,0 @@
use crate::{
common::FileSpan,
ir::{FieldID, Len, StructID, VarID},
};
use super::Type;
use std::{collections::HashMap, fmt::Debug};
#[derive(Clone)]
pub struct FnDef {
pub name: String,
pub args: Vec<VarDef>,
pub ret: Type,
pub origin: Origin,
}
#[derive(Clone)]
pub struct StructField {
pub name: String,
pub ty: Type,
}
#[derive(Clone)]
pub struct StructDef {
pub name: String,
pub fields: Vec<StructField>,
pub field_map: HashMap<String, FieldID>,
pub origin: Origin,
}
#[derive(Clone)]
pub struct VarDef {
pub name: String,
pub parent: Option<FieldRef>,
pub ty: Type,
pub origin: Origin,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
pub struct VarOffset {
pub id: VarID,
pub offset: Len,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
pub struct FieldRef {
pub var: VarID,
// this is technically redundant bc you can get it from the var...
// but it makes things a lot easier, and you'd have to recheck the fields anyways
pub struc: StructID,
pub field: FieldID,
}
#[derive(Clone)]
pub struct DataDef {
pub ty: Type,
pub origin: Origin,
pub label: String,
}
pub type Origin = FileSpan;
impl FnDef {
pub fn ty(&self) -> Type {
Type::Fn {
args: self.args.iter().map(|a| a.ty.clone()).collect(),
ret: Box::new(self.ret.clone()),
}
}
}
impl StructDef {
pub fn field(&self, id: FieldID) -> &StructField {
&self.fields[id.0]
}
pub fn get_field(&self, name: &str) -> Option<&StructField> {
self.field_map.get(name).map(|id| self.field(*id))
}
pub fn iter_fields(&self) -> impl Iterator<Item = (FieldID, &StructField)> {
self.fields.iter().enumerate().map(|(i, f)| (FieldID(i), f))
}
}

View File

@@ -1,9 +1,9 @@
use crate::common::{CompilerMsg, CompilerOutput, FileSpan};
use super::{IRUProgram, Type};
use super::{Type, UProgram};
impl CompilerOutput {
pub fn check_assign(&mut self, p: &IRUProgram, src: &Type, dest: &Type, span: FileSpan) {
pub fn check_assign(&mut self, p: &UProgram, src: &Type, dest: &Type, span: FileSpan) -> bool {
// TODO: spans
if src != dest {
self.err(CompilerMsg {
@@ -14,6 +14,9 @@ impl CompilerOutput {
),
spans: vec![span],
});
true
} else {
false
}
}
}

View File

@@ -1,7 +1,7 @@
use crate::{common::FileSpan, ir::VarID};
use std::fmt::Debug;
use super::IRUInstruction;
use super::UInstruction;
#[derive(Clone, Copy)]
pub struct VarInst {
@@ -9,8 +9,8 @@ pub struct VarInst {
pub span: FileSpan,
}
pub struct IRUInstrInst {
pub i: IRUInstruction,
pub struct UInstrInst {
pub i: UInstruction,
pub span: FileSpan,
}
@@ -20,7 +20,7 @@ impl Debug for VarInst {
}
}
impl Debug for IRUInstrInst {
impl Debug for UInstrInst {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.i)
}

View File

@@ -1,18 +1,9 @@
use std::{collections::HashMap, fmt::Write};
use super::{
arch::riscv64::RV64Instruction, inst::VarInst, DataID, FnID, IRUInstrInst, Type, VarID,
};
use super::{arch::riscv64::RV64Instruction, inst::VarInst, DataID, FnID, UInstrInst, UFunc};
use crate::{compiler::arch::riscv::Reg, ir::FieldID, util::Padder};
pub struct IRUFunction {
pub name: String,
pub args: Vec<VarID>,
pub ret: Type,
pub instructions: Vec<IRUInstrInst>,
}
pub enum IRUInstruction {
pub enum UInstruction {
Mv {
dest: VarInst,
src: VarInst,
@@ -51,10 +42,10 @@ pub enum IRUInstruction {
},
If {
cond: VarInst,
body: Vec<IRUInstrInst>,
body: Vec<UInstrInst>,
},
Loop {
body: Vec<IRUInstrInst>,
body: Vec<UInstrInst>,
},
Break,
Continue,
@@ -73,7 +64,7 @@ pub enum AsmBlockArgType {
Out,
}
impl std::fmt::Debug for IRUInstruction {
impl std::fmt::Debug for UInstruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Mv { dest, src } => write!(f, "{dest:?} <- {src:?}")?,
@@ -86,9 +77,7 @@ impl std::fmt::Debug for IRUInstruction {
f: func,
args,
} => write!(f, "{dest:?} <- {func:?}({args:?})")?,
Self::AsmBlock { args, instructions } => {
write!(f, "asm {args:?} {instructions:#?}")?
}
Self::AsmBlock { args, instructions } => write!(f, "asm {args:?} {instructions:#?}")?,
Self::Ret { src } => f.debug_struct("Ret").field("src", src).finish()?,
Self::Construct { dest, fields } => write!(f, "{dest:?} <- {fields:?}")?,
Self::If { cond, body } => {
@@ -126,9 +115,9 @@ impl std::fmt::Debug for IRUInstruction {
}
}
impl std::fmt::Debug for IRUFunction {
impl std::fmt::Debug for UFunc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{:?}", &self.name, self.args)?;
write!(f, "{:?}", self.args)?;
if !self.instructions.is_empty() {
f.write_str("{\n ")?;
let mut padder = Padder::new(f);

160
src/ir/upper/kind.rs Normal file
View File

@@ -0,0 +1,160 @@
use crate::{
common::FileSpan,
ir::{Len, Named, ID},
};
use super::{Type, UInstrInst, UProgram};
use std::{collections::HashMap, fmt::Debug};
pub const NAMED_KINDS: usize = 4;
pub struct UFunc {
pub args: Vec<VarID>,
pub ret: Type,
pub origin: Origin,
pub instructions: Vec<UInstrInst>,
}
#[derive(Clone)]
pub struct StructField {
pub name: String,
pub ty: Type,
}
#[derive(Clone)]
pub struct UStruct {
pub fields: Vec<StructField>,
pub field_map: HashMap<String, FieldID>,
pub origin: Origin,
}
#[derive(Clone)]
pub struct UVar {
pub parent: Option<FieldRef>,
pub ty: Type,
pub origin: Origin,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
pub struct VarOffset {
pub id: VarID,
pub offset: Len,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
pub struct FieldRef {
pub var: VarID,
// this is technically redundant bc you can get it from the var...
// but it makes things a lot easier, and you'd have to recheck the fields anyways
pub struc: StructID,
pub field: FieldID,
}
#[derive(Clone)]
pub struct UData {
pub ty: Type,
pub origin: Origin,
pub content: Vec<u8>,
}
pub type Origin = FileSpan;
impl UFunc {
pub fn ty(&self, program: &UProgram) -> Type {
Type::Fn {
args: self
.args
.iter()
.map(|a| program.expect(*a).ty.clone())
.collect(),
ret: Box::new(self.ret.clone()),
}
}
}
impl UStruct {
pub fn field(&self, id: FieldID) -> &StructField {
&self.fields[id.0]
}
pub fn get_field(&self, name: &str) -> Option<&StructField> {
self.field_map.get(name).map(|id| self.field(*id))
}
pub fn iter_fields(&self) -> impl Iterator<Item = (FieldID, &StructField)> {
self.fields
.iter()
.enumerate()
.map(|(i, f)| (FieldID::new(i), f))
}
}
pub type StructID = ID<UStruct>;
pub type VarID = ID<UVar>;
pub type DataID = ID<UData>;
pub type FieldID = ID<StructField>;
pub type FnID = ID<UFunc>;
impl Kind for UFunc {
const INDEX: usize = 0;
fn from_program_mut(program: &mut UProgram) -> &mut Vec<Option<Self>> {
&mut program.fns
}
fn from_program(program: &UProgram) -> &Vec<Option<Self>> {
&program.fns
}
}
impl Named for UFunc {
const NAME: &str = "func";
}
impl Kind for UVar {
const INDEX: usize = 1;
fn from_program_mut(program: &mut UProgram) -> &mut Vec<Option<Self>> {
&mut program.vars
}
fn from_program(program: &UProgram) -> &Vec<Option<Self>> {
&program.vars
}
}
impl Named for UVar {
const NAME: &str = "var";
}
impl Kind for UStruct {
const INDEX: usize = 2;
fn from_program_mut(program: &mut UProgram) -> &mut Vec<Option<Self>> {
&mut program.structs
}
fn from_program(program: &UProgram) -> &Vec<Option<Self>> {
&program.structs
}
}
impl Named for UStruct {
const NAME: &str = "struct";
}
impl Kind for UData {
const INDEX: usize = 3;
fn from_program_mut(program: &mut UProgram) -> &mut Vec<Option<Self>> {
&mut program.data
}
fn from_program(program: &UProgram) -> &Vec<Option<Self>> {
&program.data
}
}
impl Named for UData {
const NAME: &str = "data";
}
impl Named for StructField {
const NAME: &str = "field";
}
pub trait Kind {
const INDEX: usize;
fn from_program_mut(program: &mut UProgram) -> &mut Vec<Option<Self>>
where
Self: Sized;
fn from_program(program: &UProgram) -> &Vec<Option<Self>>
where
Self: Sized;
}

View File

@@ -1,5 +1,5 @@
mod def;
mod func;
mod kind;
mod instr;
mod ty;
mod program;
mod validate;
@@ -7,8 +7,8 @@ mod error;
mod inst;
use super::*;
pub use def::*;
pub use func::*;
pub use kind::*;
pub use instr::*;
pub use ty::*;
pub use program::*;
pub use inst::*;

View File

@@ -2,40 +2,67 @@ use std::{collections::HashMap, fmt::Debug};
use super::{inst::VarInst, *};
pub struct IRUProgram {
pub fn_defs: Vec<FnDef>,
pub var_defs: Vec<VarDef>,
pub struct_defs: Vec<StructDef>,
pub data_defs: Vec<DataDef>,
pub fns: Vec<Option<IRUFunction>>,
pub data: Vec<Vec<u8>>,
pub struct UProgram {
pub fns: Vec<Option<UFunc>>,
pub vars: Vec<Option<UVar>>,
pub structs: Vec<Option<UStruct>>,
pub data: Vec<Option<UData>>,
pub start: Option<FnID>,
pub names: NameMap,
// todo: these feel weird raw
pub fn_map: HashMap<VarID, FnID>,
pub inv_fn_map: Vec<VarID>,
pub temp: usize,
pub stack: Vec<HashMap<String, Idents>>,
pub name_stack: Vec<HashMap<String, Idents>>,
}
impl IRUProgram {
pub struct NameMap {
names: [Vec<String>; NAMED_KINDS],
inv_names: [HashMap<String, usize>; NAMED_KINDS],
}
impl NameMap {
pub fn new() -> Self {
Self {
names: core::array::from_fn(|_| Vec::new()),
inv_names: core::array::from_fn(|_| HashMap::new()),
}
}
pub fn get<K: Kind>(&self, id: ID<K>) -> &str {
&self.names[K::INDEX][id.0]
}
pub fn lookup<K: Kind>(&self, name: &str) -> Option<ID<K>> {
Some(ID::new(*self.inv_names[K::INDEX].get(name)?))
}
pub fn push<K: Kind>(&mut self, name: String) {
self.inv_names[K::INDEX].insert(name.clone(), self.names[K::INDEX].len());
self.names[K::INDEX].push(name);
}
}
impl UProgram {
pub fn new() -> Self {
Self {
fn_defs: Vec::new(),
var_defs: Vec::new(),
struct_defs: Vec::new(),
data_defs: Vec::new(),
data: Vec::new(),
fn_map: HashMap::new(),
fns: Vec::new(),
vars: Vec::new(),
structs: Vec::new(),
data: Vec::new(),
start: None,
names: NameMap::new(),
fn_map: HashMap::new(),
inv_fn_map: Vec::new(),
temp: 0,
stack: vec![HashMap::new()],
name_stack: vec![HashMap::new()],
}
}
pub fn push(&mut self) {
self.stack.push(HashMap::new());
self.name_stack.push(HashMap::new());
}
pub fn pop(&mut self) {
self.stack.pop();
self.name_stack.pop();
}
pub fn get(&self, name: &str) -> Option<Idents> {
for map in self.stack.iter().rev() {
pub fn get_idents(&self, name: &str) -> Option<Idents> {
for map in self.name_stack.iter().rev() {
let res = map.get(name);
if res.is_some() {
return res.cloned();
@@ -43,43 +70,29 @@ impl IRUProgram {
}
None
}
pub fn get_var(&self, id: VarID) -> &VarDef {
&self.var_defs[id.0]
pub fn get<K: Kind>(&self, id: ID<K>) -> Option<&K> {
K::from_program(self)[id.0].as_ref()
}
pub fn get_fn(&self, id: FnID) -> &FnDef {
&self.fn_defs[id.0]
pub fn get_mut<K: Kind>(&mut self, id: ID<K>) -> Option<&mut K> {
K::from_program_mut(self)[id.0].as_mut()
}
pub fn get_data(&self, id: DataID) -> &DataDef {
&self.data_defs[id.0]
pub fn expect<K: Kind + Named>(&self, id: ID<K>) -> &K {
self.get(id)
.unwrap_or_else(|| panic!("{id:?} not defined yet!"))
}
pub fn get_fn_var(&self, id: VarID) -> Option<&FnDef> {
Some(&self.fn_defs[self.fn_map.get(&id)?.0])
pub fn expect_mut<K: Kind + Named>(&mut self, id: ID<K>) -> &mut K {
self.get_mut(id)
.unwrap_or_else(|| panic!("{id:?} not defined yet!"))
}
pub fn get_struct(&self, id: StructID) -> &StructDef {
&self.struct_defs[id.0]
}
pub fn alias_fn(&mut self, name: &str, id: FnID) {
self.insert(name, Ident::Fn(id));
}
pub fn named_var(&mut self, def: VarDef) -> VarID {
// TODO: this is stupid
let id = self.def_var(def.clone());
self.name_var(&def, id);
id
}
pub fn name_var(&mut self, def: &VarDef, var: VarID) {
self.insert(&def.name, Ident::Var(var));
}
pub fn def_var(&mut self, var: VarDef) -> VarID {
let i = self.var_defs.len();
self.var_defs.push(var);
VarID(i)
pub fn get_fn_var(&self, id: VarID) -> Option<&UFunc> {
self.fns[self.fn_map.get(&id)?.0].as_ref()
}
pub fn size_of_type(&self, ty: &Type) -> Option<Size> {
// TODO: target matters
Some(match ty {
Type::Bits(b) => *b,
Type::Struct { id, args } => self.struct_defs[id.0]
Type::Struct { id, args } => self.structs[id.0]
.as_ref()?
.fields
.iter()
.try_fold(0, |sum, f| Some(sum + self.size_of_type(&f.ty)?))?,
@@ -92,9 +105,8 @@ impl IRUProgram {
Type::Unit => 0,
})
}
pub fn struct_layout() {}
pub fn size_of_var(&self, var: VarID) -> Option<Size> {
self.size_of_type(&self.var_defs[var.0].ty)
self.size_of_type(&self.get(var)?.ty)
}
pub fn temp_subvar(&mut self, origin: Origin, ty: Type, parent: FieldRef) -> VarInst {
self.temp_var_inner(origin, ty, Some(parent))
@@ -104,12 +116,10 @@ impl IRUProgram {
}
fn temp_var_inner(&mut self, origin: Origin, ty: Type, parent: Option<FieldRef>) -> VarInst {
let v = self.def_var(VarDef {
name: format!("temp{}", self.temp),
parent,
origin,
ty,
});
let v = self.def(
format!("temp{}", self.temp),
Some(UVar { parent, origin, ty }),
);
self.temp += 1;
VarInst {
id: v,
@@ -117,44 +127,29 @@ impl IRUProgram {
}
}
pub fn def_fn(&mut self, def: FnDef) -> FnID {
let i = self.fn_defs.len();
let id = FnID(i);
let var_def = VarDef {
name: def.name.clone(),
parent: None,
origin: def.origin,
ty: def.ty(),
};
let vid = self.def_var(var_def);
self.insert(&def.name, Ident::Var(vid));
self.fn_map.insert(vid, id);
self.insert(&def.name, Ident::Fn(id));
self.fn_defs.push(def);
self.fns.push(None);
pub fn write<K: Kind>(&mut self, id: ID<K>, k: K) {
K::from_program_mut(self)[id.0] = Some(k);
}
pub fn def<K: Kind>(&mut self, name: String, k: Option<K>) -> ID<K> {
self.names.push::<K>(name);
let vec = K::from_program_mut(self);
let id = ID::new(vec.len());
vec.push(k);
id
}
pub fn def_struct(&mut self, def: StructDef) -> StructID {
let i = self.struct_defs.len();
let id = StructID(i);
self.insert(&def.name, Ident::Type(id));
self.struct_defs.push(def);
pub fn def_searchable<K: Kind>(&mut self, name: String, k: Option<K>) -> ID<K> {
let id = self.def(name.clone(), k);
self.name_on_stack(id, name);
id
}
pub fn def_data(&mut self, def: DataDef, bytes: Vec<u8>) -> DataID {
let i = self.data.len();
self.data_defs.push(def);
self.data.push(bytes);
DataID(i)
}
pub fn type_name(&self, ty: &Type) -> String {
let mut str = String::new();
match ty {
Type::Struct { id: base, args } => {
str += &self.get_struct(*base).name;
str += self.names.get(*base);
if let Some(arg) = args.first() {
str = str + "<" + &self.type_name(arg);
}
@@ -188,81 +183,82 @@ impl IRUProgram {
}
str
}
fn insert(&mut self, name: &str, id: Ident) {
let idx = self.stack.len() - 1;
let last = &mut self.stack[idx];
if let Some(l) = last.get_mut(name) {
l.insert(id);
fn name_on_stack<K: Kind>(&mut self, id: ID<K>, name: String) {
let idx = self.name_stack.len() - 1;
let last = &mut self.name_stack[idx];
if let Some(l) = last.get_mut(&name) {
l.insert(id.into());
} else {
last.insert(name.to_string(), Idents::new(id));
last.insert(name, Idents::new(id.into()));
}
}
pub fn write_fn(&mut self, id: FnID, f: IRUFunction) {
self.fns[id.0] = Some(f);
}
pub fn iter_vars(&self) -> impl Iterator<Item = (VarID, &VarDef)> {
self.var_defs.iter().enumerate().map(|(i, v)| (VarID(i), v))
}
pub fn iter_fns(&self) -> impl Iterator<Item = (FnID, &IRUFunction)> {
self.fns
.iter()
.enumerate()
.flat_map(|(i, f)| Some((FnID(i), f.as_ref()?)))
}
pub fn var_offset(&self, var: VarID) -> Option<VarOffset> {
let mut current = VarOffset { id: var, offset: 0 };
while let Some(parent) = self.var_defs[current.id.0].parent {
while let Some(parent) = self.get(current.id)?.parent {
current.id = parent.var;
current.offset += self.field_offset(parent.struc, parent.field)?;
}
Some(current)
}
pub fn field_offset(&self, struct_id: StructID, field: FieldID) -> Option<Len> {
let struc = self.get_struct(struct_id);
let struc = self.get(struct_id)?;
let mut offset = 0;
for i in 0..field.0 {
offset += self.size_of_type(&struc.fields[i].ty)?;
}
Some(offset)
}
pub fn iter_vars(&self) -> impl Iterator<Item = (VarID, &UVar)> {
self.vars
.iter()
.flatten()
.enumerate()
.map(|(i, x)| (ID::new(i), x))
}
pub fn iter_fns(&self) -> impl Iterator<Item = (FnID, &UFunc)> {
self.fns
.iter()
.flatten()
.enumerate()
.map(|(i, x)| (ID::new(i), x))
}
}
#[derive(Debug, Clone, Copy)]
pub enum Ident {
Var(VarID),
Fn(FnID),
Type(StructID),
pub struct Ident {
id: usize,
kind: usize,
}
impl<K: Kind> From<ID<K>> for Ident {
fn from(id: ID<K>) -> Self {
Self {
id: id.0,
kind: K::INDEX,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Idents {
pub latest: Ident,
pub var: Option<VarID>,
pub func: Option<FnID>,
pub struc: Option<StructID>,
pub kinds: [Option<usize>; NAMED_KINDS],
}
impl Idents {
fn new(latest: Ident) -> Self {
let mut s = Self {
latest,
var: None,
func: None,
struc: None,
kinds: [None; NAMED_KINDS],
};
s.insert(latest);
s
}
fn insert(&mut self, i: Ident) {
self.latest = i;
match i {
Ident::Var(v) => {
self.var = Some(v);
}
Ident::Fn(f) => {
self.func = Some(f);
}
Ident::Type(t) => self.struc = Some(t),
}
self.kinds[i.kind] = Some(i.id);
}
pub fn get<K: Kind>(&self) -> Option<ID<K>> {
self.kinds[K::INDEX].map(|i| i.into())
}
}

View File

@@ -1,4 +1,6 @@
use super::{IRUProgram, Len, StructID};
use crate::common::CompilerOutput;
use super::{Len, StructID, UInstruction, UProgram, UVar};
#[derive(Clone, PartialEq)]
pub enum Type {
@@ -25,13 +27,121 @@ impl Type {
}
}
// should impl instead
pub fn resolve_types(ns: &IRUProgram) {
for (i, f) in ns.iter_fns() {
for inst in &f.instructions {
match &inst.i {
_ => todo!(),
impl UProgram {
pub fn resolve_types(&mut self) {
// I LOVE RUST
let mut vars = self.vars.clone();
for f in self.fns.iter().flatten() {
for i in &f.instructions {
self.resolve_instr_types(&mut vars, &i.i);
}
}
self.vars = vars;
}
pub fn resolve_instr_types(&self, vars: &mut Vec<Option<UVar>>, i: &UInstruction) {
match &i {
UInstruction::Call { dest, f, args } => {
let ret = self.get_fn_var(f.id).expect("bruh").ret.clone();
vars[dest.id.0].as_mut().expect("bruh").ty = ret;
}
UInstruction::Mv { dest, src } => {
let dest_ty = &vars[dest.id.0].as_ref().unwrap().ty;
let src_ty = &vars[src.id.0].as_ref().unwrap().ty;
if let Some(ty) = match_types(dest_ty, src_ty) {
vars[dest.id.0]
.as_mut()
.expect("PARTIAL BORROWING WOULD BE REALLY COOL")
.ty = ty.clone();
vars[src.id.0]
.as_mut()
.expect("PARTIAL BORROWING WOULD BE REALLY COOL")
.ty = ty;
}
}
UInstruction::Ref { dest, src } => {
// TODO
}
UInstruction::LoadData { dest, src } => {
// TODO
}
UInstruction::LoadSlice { dest, src } => {
// TODO
}
UInstruction::LoadFn { dest, src } => {
// TODO
}
UInstruction::AsmBlock { instructions, args } => {
// TODO
}
UInstruction::Ret { .. } => {}
UInstruction::Construct { dest, fields } => {
// TODO
}
UInstruction::If { cond, body } => {
for i in body {
self.resolve_instr_types(vars, &i.i);
}
}
UInstruction::Loop { body } => {
for i in body {
self.resolve_instr_types(vars, &i.i);
}
}
UInstruction::Break => {}
UInstruction::Continue => {}
}
}
}
pub fn match_types(dest: &Type, src: &Type) -> Option<Type> {
if dest == src {
return None;
}
match (dest, src) {
(Type::Error, x) | (x, Type::Error) => None,
(Type::Infer, x) | (x, Type::Infer) => Some(x.clone()),
(
Type::Struct {
id: dest_id,
args: dest_args,
},
Type::Struct {
id: src_id,
args: src_args,
},
) => {
if dest_id != src_id {
return None;
}
None
// TODO
// let mut args = Vec::new();
// for (darg, sarg) in dest_args.iter().zip(src_args) {
// }
// Some(Type::Struct { id: *dest_id, args })
}
(
Type::Fn {
args: dest_args,
ret: dest_ret,
},
Type::Fn {
args: src_args,
ret: src_ret,
},
) => {
// TODO
None
}
(Type::Ref(dest), Type::Ref(src)) => Some(match_types(dest, src)?),
(Type::Slice(dest), Type::Slice(src)) => Some(match_types(dest, src)?),
(Type::Array(dest, dlen), Type::Array(src, slen)) => {
if dlen != slen {
return None;
}
match_types(dest, src)
}
_ => None,
}
}

View File

@@ -1,26 +1,27 @@
// TODO: move this into ir, not parser
use super::{IRUInstrInst, IRUInstruction, IRUProgram, Type};
use super::{Type, UInstrInst, UInstruction, UProgram};
use crate::common::{CompilerMsg, CompilerOutput, FileSpan};
impl IRUProgram {
impl UProgram {
pub fn validate(&self) -> CompilerOutput {
let mut output = CompilerOutput::new();
for (f, fd) in self.fns.iter().flatten().zip(&self.fn_defs) {
self.validate_fn(
&f.instructions,
fd.origin,
&fd.ret,
&mut output,
true,
false,
);
for f in self.fns.iter().flatten() {
self.validate_fn(&f.instructions, f.origin, &f.ret, &mut output, true, false);
}
for (id, var) in self.iter_vars() {
if var.ty == Type::Error {
output.err(CompilerMsg {
msg: format!("Var {:?} is error type!", id),
spans: vec![var.origin],
});
}
}
output
}
pub fn validate_fn(
&self,
instructions: &[IRUInstrInst],
instructions: &[UInstrInst],
origin: FileSpan,
ret: &Type,
output: &mut CompilerOutput,
@@ -30,31 +31,31 @@ impl IRUProgram {
let mut no_ret = true;
for i in instructions {
match &i.i {
IRUInstruction::Mv { dest, src } => {
let dest = self.get_var(dest.id);
let src = self.get_var(src.id);
UInstruction::Mv { dest, src } => {
let dest = self.expect(dest.id);
let src = self.expect(src.id);
output.check_assign(self, &src.ty, &dest.ty, i.span);
}
IRUInstruction::Ref { dest, src } => {
UInstruction::Ref { dest, src } => {
// TODO
}
IRUInstruction::LoadData { dest, src } => {
let dest = self.get_var(dest.id);
let src = self.get_data(*src);
UInstruction::LoadData { dest, src } => {
let dest = self.expect(dest.id);
let src = self.expect(*src);
output.check_assign(self, &src.ty, &dest.ty, i.span);
}
IRUInstruction::LoadSlice { dest, src } => {
let dest = self.get_var(dest.id);
let src = self.get_data(*src);
UInstruction::LoadSlice { dest, src } => {
let dest = self.expect(dest.id);
let src = self.expect(*src);
let Type::Array(srcty, ..) = &src.ty else {
todo!()
};
output.check_assign(self, &Type::Slice(srcty.clone()), &dest.ty, i.span);
}
IRUInstruction::LoadFn { dest, src } => todo!(),
IRUInstruction::Call { dest, f, args } => {
let destty = &self.get_var(dest.id).ty;
let f = self.get_var(f.id);
UInstruction::LoadFn { dest, src } => todo!(),
UInstruction::Call { dest, f, args } => {
let destty = &self.expect(dest.id).ty;
let f = self.expect(f.id);
let Type::Fn { args: argtys, ret } = &f.ty else {
todo!()
};
@@ -65,21 +66,21 @@ impl IRUProgram {
spans: vec![dest.span],
});
}
for (argv, argt) in args.iter().zip(argtys) {
let dest = self.get_var(argv.id);
output.check_assign(self, argt, &dest.ty, argv.span);
for (dst_ty, src) in argtys.iter().zip(args) {
let src_var = self.expect(src.id);
output.check_assign(self, &src_var.ty, dst_ty, src.span);
}
}
IRUInstruction::AsmBlock { instructions, args } => {
UInstruction::AsmBlock { instructions, args } => {
// TODO
}
IRUInstruction::Ret { src } => {
let srcty = &self.get_var(src.id).ty;
UInstruction::Ret { src } => {
let srcty = &self.expect(src.id).ty;
output.check_assign(self, srcty, ret, src.span);
no_ret = false;
}
IRUInstruction::Construct { dest, fields } => {
let dest_def = self.get_var(dest.id);
UInstruction::Construct { dest, fields } => {
let dest_def = self.expect(dest.id);
let tyid = match &dest_def.ty {
Type::Struct { id, args } => *id,
_ => {
@@ -90,10 +91,10 @@ impl IRUProgram {
continue;
}
};
let def = self.get_struct(tyid);
let def = self.expect(tyid);
for (id, field) in def.iter_fields() {
if let Some(var) = fields.get(&id) {
let ety = &self.get_var(var.id).ty;
let ety = &self.expect(var.id).ty;
output.check_assign(self, &field.ty, ety, var.span);
} else {
output.err(CompilerMsg {
@@ -103,15 +104,15 @@ impl IRUProgram {
}
}
}
IRUInstruction::If { cond, body } => {
let cond = self.get_var(cond.id);
UInstruction::If { cond, body } => {
let cond = self.expect(cond.id);
output.check_assign(self, &cond.ty, &Type::Bits(64), i.span);
self.validate_fn(body, origin, ret, output, false, breakable);
}
IRUInstruction::Loop { body } => {
UInstruction::Loop { body } => {
self.validate_fn(body, origin, ret, output, false, true);
}
IRUInstruction::Break => {
UInstruction::Break => {
if !breakable {
output.err(CompilerMsg {
msg: "Can't break here (outside of loop)".to_string(),
@@ -120,7 +121,7 @@ impl IRUProgram {
}
// TODO
}
IRUInstruction::Continue => {
UInstruction::Continue => {
if !breakable {
output.err(CompilerMsg {
msg: "Can't continue here (outside of loop)".to_string(),