Fix warnings

This commit is contained in:
Sofia 2025-07-08 18:47:46 +03:00
parent fb876e3ef5
commit 2e99ec3a80
9 changed files with 47 additions and 118 deletions

View File

@ -1,4 +1,4 @@
use std::{cell::RefCell, marker::PhantomData, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use crate::{ use crate::{
BlockData, ConstValue, FunctionData, InstructionData, InstructionKind, ModuleData, BlockData, ConstValue, FunctionData, InstructionData, InstructionKind, ModuleData,
@ -140,6 +140,7 @@ impl Builder {
} }
} }
#[allow(dead_code)]
pub(crate) unsafe fn module_data(&self, value: &ModuleValue) -> ModuleData { pub(crate) unsafe fn module_data(&self, value: &ModuleValue) -> ModuleData {
unsafe { self.modules.borrow().get_unchecked(value.0).data.clone() } unsafe { self.modules.borrow().get_unchecked(value.0).data.clone() }
} }
@ -156,6 +157,7 @@ impl Builder {
} }
} }
#[allow(dead_code)]
pub(crate) unsafe fn block_data(&self, value: &BlockValue) -> BlockData { pub(crate) unsafe fn block_data(&self, value: &BlockValue) -> BlockData {
unsafe { unsafe {
self.modules self.modules
@ -235,7 +237,6 @@ impl Builder {
impl InstructionValue { impl InstructionValue {
pub fn get_type(&self, builder: &Builder) -> Result<Type, ()> { pub fn get_type(&self, builder: &Builder) -> Result<Type, ()> {
use InstructionKind::*; use InstructionKind::*;
use Type::*;
unsafe { unsafe {
match &builder.instr_data(self).kind { match &builder.instr_data(self).kind {
Param(nth) => builder Param(nth) => builder
@ -247,7 +248,7 @@ impl InstructionValue {
Constant(c) => Ok(c.get_type()), Constant(c) => Ok(c.get_type()),
Add(lhs, rhs) => match_types(lhs, rhs, &builder), Add(lhs, rhs) => match_types(lhs, rhs, &builder),
Sub(lhs, rhs) => match_types(lhs, rhs, &builder), Sub(lhs, rhs) => match_types(lhs, rhs, &builder),
ICmp(pred, lhs, rhs) => Ok(Type::Bool), ICmp(_, _, _) => Ok(Type::Bool),
FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret),
Phi(values) => values.first().ok_or(()).and_then(|v| v.get_type(&builder)), Phi(values) => values.first().ok_or(()).and_then(|v| v.get_type(&builder)),
} }

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, ffi::CString, hash::Hash, process::Termination, ptr::null_mut}; use std::{collections::HashMap, ffi::CString, ptr::null_mut};
use llvm_sys::{ use llvm_sys::{
LLVMIntPredicate, LLVMIntPredicate,
@ -18,7 +18,7 @@ use llvm_sys::{
use crate::util::{ErrorMessageHolder, from_cstring, into_cstring}; use crate::util::{ErrorMessageHolder, from_cstring, into_cstring};
use super::{ use super::{
ConstValue, Context, Function, IntPredicate, Module, TerminatorKind, Type, ConstValue, Context, IntPredicate, TerminatorKind, Type,
builder::{ builder::{
BlockHolder, BlockValue, Builder, FunctionHolder, FunctionValue, InstructionHolder, BlockHolder, BlockValue, Builder, FunctionHolder, FunctionValue, InstructionHolder,
InstructionValue, ModuleHolder, InstructionValue, ModuleHolder,
@ -54,6 +54,7 @@ pub struct LLVMModule<'a> {
builder: &'a Builder, builder: &'a Builder,
context_ref: LLVMContextRef, context_ref: LLVMContextRef,
builder_ref: LLVMBuilderRef, builder_ref: LLVMBuilderRef,
#[allow(dead_code)]
module_ref: LLVMModuleRef, module_ref: LLVMModuleRef,
functions: HashMap<FunctionValue, LLVMFunction>, functions: HashMap<FunctionValue, LLVMFunction>,
blocks: HashMap<BlockValue, LLVMBasicBlockRef>, blocks: HashMap<BlockValue, LLVMBasicBlockRef>,
@ -67,7 +68,7 @@ pub struct LLVMFunction {
} }
pub struct LLVMValue { pub struct LLVMValue {
ty: Type, _ty: Type,
value_ref: LLVMValueRef, value_ref: LLVMValueRef,
} }
@ -244,9 +245,9 @@ impl InstructionHolder {
&self, &self,
module: &LLVMModule, module: &LLVMModule,
function: &LLVMFunction, function: &LLVMFunction,
block: LLVMBasicBlockRef, _block: LLVMBasicBlockRef,
) -> LLVMValue { ) -> LLVMValue {
let ty = self.value.get_type(module.builder).unwrap(); let _ty = self.value.get_type(module.builder).unwrap();
let val = unsafe { let val = unsafe {
use super::InstructionKind::*; use super::InstructionKind::*;
match &self.data.kind { match &self.data.kind {
@ -267,7 +268,7 @@ impl InstructionHolder {
let rhs_val = module.values.get(&rhs).unwrap().value_ref; let rhs_val = module.values.get(&rhs).unwrap().value_ref;
LLVMBuildICmp( LLVMBuildICmp(
module.builder_ref, module.builder_ref,
pred.as_llvm(ty.signed()), pred.as_llvm(_ty.signed()),
lhs_val, lhs_val,
rhs_val, rhs_val,
c"icmp".as_ptr(), c"icmp".as_ptr(),
@ -298,7 +299,7 @@ impl InstructionHolder {
} }
let phi = LLVMBuildPhi( let phi = LLVMBuildPhi(
module.builder_ref, module.builder_ref,
ty.as_llvm(module.context_ref), _ty.as_llvm(module.context_ref),
c"phi".as_ptr(), c"phi".as_ptr(),
); );
LLVMAddIncoming( LLVMAddIncoming(
@ -311,7 +312,10 @@ impl InstructionHolder {
} }
} }
}; };
LLVMValue { ty, value_ref: val } LLVMValue {
_ty,
value_ref: val,
}
} }
} }
@ -319,10 +323,10 @@ impl TerminatorKind {
fn compile( fn compile(
&self, &self,
module: &LLVMModule, module: &LLVMModule,
function: &LLVMFunction, _function: &LLVMFunction,
block: LLVMBasicBlockRef, _block: LLVMBasicBlockRef,
) -> LLVMValue { ) -> LLVMValue {
let ty = self.get_type(module.builder).unwrap(); let _ty = self.get_type(module.builder).unwrap();
let val = unsafe { let val = unsafe {
match self { match self {
TerminatorKind::Ret(val) => { TerminatorKind::Ret(val) => {
@ -341,7 +345,10 @@ impl TerminatorKind {
} }
} }
}; };
LLVMValue { ty, value_ref: val } LLVMValue {
_ty,
value_ref: val,
}
} }
} }

View File

@ -178,57 +178,3 @@ pub enum TerminatorKind {
Branch(BlockValue), Branch(BlockValue),
CondBr(InstructionValue, BlockValue, BlockValue), CondBr(InstructionValue, BlockValue, BlockValue),
} }
fn test() {
use ConstValue::*;
use InstructionKind::*;
let context = Context::new();
let mut module = context.module("test");
let mut main = module.function("main", Type::I32, Vec::new());
let mut m_entry = main.block("entry");
let mut fibonacci = module.function("fibonacci", Type::I32, vec![Type::I32]);
let arg = m_entry.build(Constant(I32(5))).unwrap();
m_entry
.build(FunctionCall(fibonacci.value, vec![arg]))
.unwrap();
let mut f_entry = fibonacci.block("entry");
let num_3 = f_entry.build(Constant(I32(3))).unwrap();
let param_n = f_entry.build(Param(0)).unwrap();
let cond = f_entry
.build(ICmp(IntPredicate::LessThan, param_n, num_3))
.unwrap();
let mut then_b = fibonacci.block("then");
let mut else_b = fibonacci.block("else");
f_entry
.terminate(TerminatorKind::CondBr(cond, then_b.value, else_b.value))
.unwrap();
let ret_const = then_b.build(Constant(I32(1))).unwrap();
then_b.terminate(TerminatorKind::Ret(ret_const)).unwrap();
let const_1 = else_b.build(Constant(I32(1))).unwrap();
let const_2 = else_b.build(Constant(I32(2))).unwrap();
let param_1 = else_b.build(Sub(param_n, const_1)).unwrap();
let param_2 = else_b.build(Sub(param_n, const_2)).unwrap();
let call_1 = else_b
.build(FunctionCall(fibonacci.value, vec![param_1]))
.unwrap();
let call_2 = else_b
.build(FunctionCall(fibonacci.value, vec![param_2]))
.unwrap();
let add = else_b.build(Add(call_1, call_2)).unwrap();
else_b.terminate(TerminatorKind::Ret(add)).unwrap();
dbg!(context);
}

View File

@ -53,10 +53,19 @@ impl BinaryOperator {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRange); pub struct FunctionCallExpression(
pub String,
pub Vec<Expression>,
#[allow(dead_code)] pub TokenRange,
);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct IfExpression(pub Expression, pub Block, pub Option<Block>, pub TokenRange); pub struct IfExpression(
pub Expression,
pub Block,
pub Option<Block>,
#[allow(dead_code)] pub TokenRange,
);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LetStatement(pub String, pub Option<Type>, pub Expression, pub TokenRange); pub struct LetStatement(pub String, pub Option<Type>, pub Expression, pub TokenRange);
@ -72,6 +81,7 @@ pub struct FunctionSignature {
pub name: String, pub name: String,
pub args: Vec<(String, Type)>, pub args: Vec<(String, Type)>,
pub return_type: Option<Type>, pub return_type: Option<Type>,
#[allow(dead_code)]
pub range: TokenRange, pub range: TokenRange,
} }
@ -91,7 +101,7 @@ pub struct Block(
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum BlockLevelStatement { pub enum BlockLevelStatement {
Let(LetStatement), Let(LetStatement),
Import(ImportStatement), Import { _i: ImportStatement },
Expression(Expression), Expression(Expression),
Return(ReturnType, Expression), Return(ReturnType, Expression),
} }

View File

@ -308,7 +308,9 @@ impl Parse for BlockLevelStatement {
use BlockLevelStatement as Stmt; use BlockLevelStatement as Stmt;
Ok(match stream.peek() { Ok(match stream.peek() {
Some(Token::LetKeyword) => Stmt::Let(stream.parse()?), Some(Token::LetKeyword) => Stmt::Let(stream.parse()?),
Some(Token::ImportKeyword) => Stmt::Import(stream.parse()?), Some(Token::ImportKeyword) => Stmt::Import {
_i: stream.parse()?,
},
Some(Token::ReturnKeyword) => { Some(Token::ReturnKeyword) => {
stream.next(); stream.next();
let exp = stream.parse()?; let exp = stream.parse()?;

View File

@ -1,45 +1,8 @@
use std::collections::HashMap;
use crate::{ use crate::{
ast::{self, TypeKind}, ast::{self},
mir::{self, StmtKind, VariableReference}, mir::{self, StmtKind, VariableReference},
}; };
#[derive(Clone)]
pub enum InferredType {
FromVariable(String),
FunctionReturn(String),
Static(mir::TypeKind),
OneOf(Vec<InferredType>),
Void,
Unknown,
}
impl InferredType {
fn collapse(&self) -> mir::TypeKind {
match self {
InferredType::FromVariable(_) => mir::TypeKind::Vague(mir::VagueType::Unknown),
InferredType::FunctionReturn(_) => mir::TypeKind::Vague(mir::VagueType::Unknown),
InferredType::Static(type_kind) => type_kind.clone(),
InferredType::OneOf(inferred_types) => {
let list: Vec<mir::TypeKind> =
inferred_types.iter().map(|t| t.collapse()).collect();
if let Some(first) = list.first() {
if list.iter().all(|i| i == first) {
first.clone().into()
} else {
mir::TypeKind::Vague(mir::VagueType::Unknown)
}
} else {
mir::TypeKind::Void
}
}
InferredType::Void => mir::TypeKind::Void,
InferredType::Unknown => mir::TypeKind::Vague(mir::VagueType::Unknown),
}
}
}
impl ast::Module { impl ast::Module {
pub fn process(&self) -> mir::Module { pub fn process(&self) -> mir::Module {
let mut imports = Vec::new(); let mut imports = Vec::new();
@ -103,7 +66,7 @@ impl ast::Block {
), ),
s_let.3, s_let.3,
), ),
ast::BlockLevelStatement::Import(_) => todo!(), ast::BlockLevelStatement::Import { _i } => todo!(),
ast::BlockLevelStatement::Expression(e) => (StmtKind::Expression(e.process()), e.1), ast::BlockLevelStatement::Expression(e) => (StmtKind::Expression(e.process()), e.1),
ast::BlockLevelStatement::Return(_, e) => (StmtKind::Expression(e.process()), e.1), ast::BlockLevelStatement::Return(_, e) => (StmtKind::Expression(e.process()), e.1),
}; };

View File

@ -1,9 +1,8 @@
use std::{collections::HashMap, mem, ops::Deref}; use std::{collections::HashMap, mem};
use reid_lib::{ use reid_lib::{
builder::{FunctionValue, InstructionValue}, builder::InstructionValue, Block, ConstValue, Context, Function, InstructionKind, IntPredicate,
Block, ConstValue, Context, Function, InstructionKind, IntPredicate, Module, TerminatorKind, Module, TerminatorKind, Type,
Type,
}; };
use crate::mir::{self, types::ReturnType, TypeKind, VariableReference}; use crate::mir::{self, types::ReturnType, TypeKind, VariableReference};
@ -40,7 +39,7 @@ impl mir::Module {
let mut entry = function.block("entry"); let mut entry = function.block("entry");
let mut stack_values = HashMap::new(); let mut stack_values = HashMap::new();
for (i, (p_name, p_type)) in mir_function.parameters.iter().enumerate() { for (i, (p_name, _)) in mir_function.parameters.iter().enumerate() {
stack_values.insert( stack_values.insert(
p_name.clone(), p_name.clone(),
entry.build(InstructionKind::Param(i)).unwrap(), entry.build(InstructionKind::Param(i)).unwrap(),

View File

@ -1,7 +1,7 @@
use std::{collections::HashMap, convert::Infallible, iter}; use std::{collections::HashMap, convert::Infallible, iter};
/// This module contains code relevant to doing a type checking pass on the MIR. /// This module contains code relevant to doing a type checking pass on the MIR.
use crate::{ast::Type, mir::*, util::try_all}; use crate::{mir::*, util::try_all};
use TypeKind::*; use TypeKind::*;
use VagueType::*; use VagueType::*;

View File

@ -76,6 +76,7 @@ impl<'a, 'b> TokenStream<'a, 'b> {
/// Parse the next item with Parse-trait, also mapping it with the given /// Parse the next item with Parse-trait, also mapping it with the given
/// function. The token-stream is only consumed, if the inner function /// function. The token-stream is only consumed, if the inner function
/// retuns an Ok. /// retuns an Ok.
#[allow(dead_code)]
pub fn parse_map<T: Parse + std::fmt::Debug, F, O>(&mut self, inner: F) -> Result<O, Error> pub fn parse_map<T: Parse + std::fmt::Debug, F, O>(&mut self, inner: F) -> Result<O, Error>
where where
F: Fn(T) -> Result<O, Error>, F: Fn(T) -> Result<O, Error>,