Add function flags and codegen extern functions

This commit is contained in:
Sofia 2025-07-14 18:26:57 +03:00
parent 376baa2c9a
commit 4eaa365674
4 changed files with 46 additions and 11 deletions

View File

@ -1,4 +1,4 @@
use reid_lib::{ConstValue, Context, Instr, CmpPredicate, TerminatorKind, Type}; use reid_lib::{CmpPredicate, ConstValue, Context, FunctionFlags, Instr, TerminatorKind, Type};
fn main() { fn main() {
use ConstValue::*; use ConstValue::*;
@ -8,10 +8,15 @@ fn main() {
let mut module = context.module("test"); let mut module = context.module("test");
let main = module.function("main", Type::I32, Vec::new()); let main = module.function("main", Type::I32, Vec::new(), FunctionFlags::default());
let mut m_entry = main.block("entry"); let mut m_entry = main.block("entry");
let fibonacci = module.function("fibonacci", Type::I32, vec![Type::I32]); let fibonacci = module.function(
"fibonacci",
Type::I32,
vec![Type::I32],
FunctionFlags::default(),
);
let arg = m_entry.build(Constant(I32(5))).unwrap(); let arg = m_entry.build(Constant(I32(5))).unwrap();
let fibonacci_call = m_entry let fibonacci_call = m_entry

View File

@ -49,7 +49,13 @@ pub struct Module<'ctx> {
} }
impl<'ctx> Module<'ctx> { impl<'ctx> Module<'ctx> {
pub fn function(&mut self, name: &str, ret: Type, params: Vec<Type>) -> Function<'ctx> { pub fn function(
&mut self,
name: &str,
ret: Type,
params: Vec<Type>,
flags: FunctionFlags,
) -> Function<'ctx> {
unsafe { unsafe {
Function { Function {
phantom: PhantomData, phantom: PhantomData,
@ -60,6 +66,7 @@ impl<'ctx> Module<'ctx> {
name: name.to_owned(), name: name.to_owned(),
ret, ret,
params, params,
flags,
}, },
), ),
} }
@ -76,6 +83,18 @@ pub struct FunctionData {
name: String, name: String,
ret: Type, ret: Type,
params: Vec<Type>, params: Vec<Type>,
flags: FunctionFlags,
}
#[derive(Debug, Clone, Copy, Hash)]
pub struct FunctionFlags {
pub is_extern: bool,
}
impl Default for FunctionFlags {
fn default() -> FunctionFlags {
FunctionFlags { is_extern: false }
}
} }
pub struct Function<'ctx> { pub struct Function<'ctx> {

View File

@ -1,8 +1,8 @@
use std::{collections::HashMap, mem}; use std::{collections::HashMap, mem};
use reid_lib::{ use reid_lib::{
builder::InstructionValue, Block, CmpPredicate, ConstValue, Context, Function, Instr, Module, builder::InstructionValue, Block, CmpPredicate, ConstValue, Context, Function, FunctionFlags,
TerminatorKind as Term, Type, Instr, Module, TerminatorKind as Term, Type,
}; };
use crate::mir::{self, types::ReturnType, IndexedVariableReference, NamedVariableRef, TypeKind}; use crate::mir::{self, types::ReturnType, IndexedVariableReference, NamedVariableRef, TypeKind};
@ -60,10 +60,21 @@ impl mir::Module {
.collect(); .collect();
let func = match &function.kind { let func = match &function.kind {
mir::FunctionDefinitionKind::Local(_, _) => { mir::FunctionDefinitionKind::Local(_, _) => module.function(
module.function(&function.name, function.return_type.get_type(), param_types) &function.name,
} function.return_type.get_type(),
mir::FunctionDefinitionKind::Extern => todo!(), param_types,
FunctionFlags::default(),
),
mir::FunctionDefinitionKind::Extern => module.function(
&function.name,
function.return_type.get_type(),
param_types,
FunctionFlags {
is_extern: true,
..FunctionFlags::default()
},
),
}; };
functions.insert(function.name.clone(), func); functions.insert(function.name.clone(), func);
} }

View File

@ -536,7 +536,7 @@ impl Literal {
(L::U64(_), TypeKind::U64) => self, (L::U64(_), TypeKind::U64) => self,
(L::U128(_), TypeKind::U128) => self, (L::U128(_), TypeKind::U128) => self,
(L::Bool(_), TypeKind::Bool) => self, (L::Bool(_), TypeKind::Bool) => self,
(L::String(val), TypeKind::StringPtr) => self, (L::String(_), TypeKind::StringPtr) => self,
// TODO make sure that v is actually able to fit in the // TODO make sure that v is actually able to fit in the
// requested type // requested type
(L::Vague(VagueL::Number(v)), TypeKind::I8) => L::I8(v as i8), (L::Vague(VagueL::Number(v)), TypeKind::I8) => L::I8(v as i8),