Finish casting codegen

This commit is contained in:
Sofia 2025-07-22 14:12:31 +03:00
parent e73b939de0
commit c03a5188ea
4 changed files with 64 additions and 50 deletions

View File

@ -18,9 +18,9 @@ fn main() {
FunctionFlags::default(), FunctionFlags::default(),
); );
let arg = m_entry.build("const", Constant(I32(5))).unwrap(); let arg = m_entry.build_named("const", Constant(I32(5))).unwrap();
let fibonacci_call = m_entry let fibonacci_call = m_entry
.build("const", FunctionCall(fibonacci.value(), vec![arg])) .build_named("const", FunctionCall(fibonacci.value(), vec![arg]))
.unwrap(); .unwrap();
m_entry m_entry
.terminate(TerminatorKind::Ret(fibonacci_call)) .terminate(TerminatorKind::Ret(fibonacci_call))
@ -28,10 +28,10 @@ fn main() {
let mut f_entry = fibonacci.block("entry"); let mut f_entry = fibonacci.block("entry");
let num_3 = f_entry.build("const", Constant(I32(3))).unwrap(); let num_3 = f_entry.build_named("const", Constant(I32(3))).unwrap();
let param_n = f_entry.build("param", Param(0)).unwrap(); let param_n = f_entry.build_named("param", Param(0)).unwrap();
let cond = f_entry let cond = f_entry
.build("cmp", ICmp(CmpPredicate::LT, param_n, num_3)) .build_named("cmp", ICmp(CmpPredicate::LT, param_n, num_3))
.unwrap(); .unwrap();
let mut then_b = fibonacci.block("then"); let mut then_b = fibonacci.block("then");
@ -41,21 +41,21 @@ fn main() {
.terminate(TerminatorKind::CondBr(cond, then_b.value(), else_b.value())) .terminate(TerminatorKind::CondBr(cond, then_b.value(), else_b.value()))
.unwrap(); .unwrap();
let ret_const = then_b.build("const", Constant(I32(1))).unwrap(); let ret_const = then_b.build_named("const", Constant(I32(1))).unwrap();
then_b.terminate(TerminatorKind::Ret(ret_const)).unwrap(); then_b.terminate(TerminatorKind::Ret(ret_const)).unwrap();
let const_1 = else_b.build("const", Constant(I32(1))).unwrap(); let const_1 = else_b.build_named("const", Constant(I32(1))).unwrap();
let const_2 = else_b.build("const", Constant(I32(2))).unwrap(); let const_2 = else_b.build_named("const", Constant(I32(2))).unwrap();
let param_1 = else_b.build("sub", Sub(param_n, const_1)).unwrap(); let param_1 = else_b.build_named("sub", Sub(param_n, const_1)).unwrap();
let param_2 = else_b.build("sub", Sub(param_n, const_2)).unwrap(); let param_2 = else_b.build_named("sub", Sub(param_n, const_2)).unwrap();
let call_1 = else_b let call_1 = else_b
.build("fibonacci", FunctionCall(fibonacci.value(), vec![param_1])) .build_named("fibonacci", FunctionCall(fibonacci.value(), vec![param_1]))
.unwrap(); .unwrap();
let call_2 = else_b let call_2 = else_b
.build("fibonacci", FunctionCall(fibonacci.value(), vec![param_2])) .build_named("fibonacci", FunctionCall(fibonacci.value(), vec![param_2]))
.unwrap(); .unwrap();
let add = else_b.build("add", Add(call_1, call_2)).unwrap(); let add = else_b.build_named("add", Add(call_1, call_2)).unwrap();
else_b.terminate(TerminatorKind::Ret(add)).unwrap(); else_b.terminate(TerminatorKind::Ret(add)).unwrap();

View File

@ -250,7 +250,7 @@ impl Instr {
} }
impl<'builder> Block<'builder> { impl<'builder> Block<'builder> {
pub fn build<T: Into<String>>( pub fn build_named<T: Into<String>>(
&mut self, &mut self,
name: T, name: T,
instruction: Instr, instruction: Instr,
@ -268,7 +268,7 @@ impl<'builder> Block<'builder> {
} }
} }
pub fn build_anon(&mut self, instruction: Instr) -> Result<InstructionValue, ()> { pub fn build(&mut self, instruction: Instr) -> Result<InstructionValue, ()> {
unsafe { unsafe {
let name = instruction.default_name().to_owned(); let name = instruction.default_name().to_owned();
self.builder.add_instruction( self.builder.add_instruction(

View File

@ -353,16 +353,16 @@ impl mir::Module {
// Codegen actual parameters // Codegen actual parameters
let arg_name = format!("arg.{}", p_name); let arg_name = format!("arg.{}", p_name);
let param = entry let param = entry
.build(format!("{}.get", arg_name), Instr::Param(i)) .build_named(format!("{}.get", arg_name), Instr::Param(i))
.unwrap(); .unwrap();
let alloca = entry let alloca = entry
.build( .build_named(
&arg_name, &arg_name,
Instr::Alloca(p_ty.get_type(&type_values, &types)), Instr::Alloca(p_ty.get_type(&type_values, &types)),
) )
.unwrap(); .unwrap();
entry entry
.build(format!("{}.store", arg_name), Instr::Store(alloca, param)) .build_named(format!("{}.store", arg_name), Instr::Store(alloca, param))
.unwrap(); .unwrap();
stack_values.insert( stack_values.insert(
p_name.clone(), p_name.clone(),
@ -488,7 +488,7 @@ impl mir::Statement {
let alloca = scope let alloca = scope
.block .block
.build( .build_named(
name, name,
Instr::Alloca(value.1.get_type(scope.type_values, scope.types)), Instr::Alloca(value.1.get_type(scope.type_values, scope.types)),
) )
@ -497,7 +497,7 @@ impl mir::Statement {
scope scope
.block .block
.build( .build_named(
format!("{}.store", name), format!("{}.store", name),
Instr::Store(alloca, value.instr()), Instr::Store(alloca, value.instr()),
) )
@ -557,7 +557,7 @@ impl mir::Statement {
StackValueKind::Mutable(instr) => { StackValueKind::Mutable(instr) => {
scope scope
.block .block
.build(backing_var, Instr::Store(instr, rhs_value.instr())) .build_named(backing_var, Instr::Store(instr, rhs_value.instr()))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
} }
@ -600,7 +600,7 @@ impl mir::Expression {
v.0.derive( v.0.derive(
scope scope
.block .block
.build( .build_named(
format!("{}", varref.1), format!("{}", varref.1),
Instr::Load( Instr::Load(
v.0.instr(), v.0.instr(),
@ -647,7 +647,7 @@ impl mir::Expression {
_ => todo!(), _ => todo!(),
}; };
Some(StackValue( Some(StackValue(
StackValueKind::Immutable(scope.block.build_anon(instr).unwrap()), StackValueKind::Immutable(scope.block.build(instr).unwrap()),
TypeKind::U32, TypeKind::U32,
)) ))
} }
@ -671,7 +671,7 @@ impl mir::Expression {
let val = scope let val = scope
.block .block
.build( .build_named(
call.name.clone(), call.name.clone(),
Instr::FunctionCall(callee.ir.value(), params), Instr::FunctionCall(callee.ir.value(), params),
) )
@ -686,11 +686,11 @@ impl mir::Expression {
let ptr = if ret_type_kind != TypeKind::Void { let ptr = if ret_type_kind != TypeKind::Void {
let ptr = scope let ptr = scope
.block .block
.build(&call.name, Instr::Alloca(ret_type.clone())) .build_named(&call.name, Instr::Alloca(ret_type.clone()))
.unwrap(); .unwrap();
scope scope
.block .block
.build(format!("{}.store", call.name), Instr::Store(ptr, val)) .build_named(format!("{}.store", call.name), Instr::Store(ptr, val))
.unwrap(); .unwrap();
Some(ptr) Some(ptr)
@ -704,7 +704,7 @@ impl mir::Expression {
StackValueKind::Immutable( StackValueKind::Immutable(
scope scope
.block .block
.build(call.name.clone(), Instr::Load(ptr, ret_type)) .build_named(call.name.clone(), Instr::Load(ptr, ret_type))
.unwrap(), .unwrap(),
), ),
ret_type_kind, ret_type_kind,
@ -743,7 +743,7 @@ impl mir::Expression {
let first = scope let first = scope
.block .block
.build("array.zero", Instr::Constant(ConstValue::U32(0))) .build_named("array.zero", Instr::Constant(ConstValue::U32(0)))
.unwrap(); .unwrap();
let TypeKind::CodegenPtr(inner) = ty else { let TypeKind::CodegenPtr(inner) = ty else {
@ -754,7 +754,7 @@ impl mir::Expression {
dbg!(&further_inner, &val_t); dbg!(&further_inner, &val_t);
let loaded = scope let loaded = scope
.block .block
.build( .build_named(
"array.load", "array.load",
Instr::Load( Instr::Load(
kind.instr(), kind.instr(),
@ -765,7 +765,7 @@ impl mir::Expression {
( (
scope scope
.block .block
.build(format!("array.gep"), Instr::GetElemPtr(loaded, vec![idx])) .build_named(format!("array.gep"), Instr::GetElemPtr(loaded, vec![idx]))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location), .maybe_location(&mut scope.block, location),
*further_inner, *further_inner,
@ -777,7 +777,7 @@ impl mir::Expression {
( (
scope scope
.block .block
.build( .build_named(
format!("array.gep"), format!("array.gep"),
Instr::GetElemPtr(kind.instr(), vec![first, idx]), Instr::GetElemPtr(kind.instr(), vec![first, idx]),
) )
@ -793,7 +793,7 @@ impl mir::Expression {
kind.derive( kind.derive(
scope scope
.block .block
.build( .build_named(
"array.load", "array.load",
Instr::Load( Instr::Load(
ptr, ptr,
@ -837,7 +837,7 @@ impl mir::Expression {
let array = scope let array = scope
.block .block
.build(&array_name, Instr::Alloca(array_ty.clone())) .build_named(&array_name, Instr::Alloca(array_ty.clone()))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
@ -847,30 +847,30 @@ impl mir::Expression {
let index_expr = scope let index_expr = scope
.block .block
.build( .build_named(
index.to_string(), index.to_string(),
Instr::Constant(ConstValue::U32(index as u32)), Instr::Constant(ConstValue::U32(index as u32)),
) )
.unwrap(); .unwrap();
let first = scope let first = scope
.block .block
.build("zero", Instr::Constant(ConstValue::U32(0))) .build_named("zero", Instr::Constant(ConstValue::U32(0)))
.unwrap(); .unwrap();
let ptr = scope let ptr = scope
.block .block
.build(gep_n, Instr::GetElemPtr(array, vec![first, index_expr])) .build_named(gep_n, Instr::GetElemPtr(array, vec![first, index_expr]))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
scope scope
.block .block
.build(store_n, Instr::Store(ptr, *instr)) .build_named(store_n, Instr::Store(ptr, *instr))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
} }
let array_val = scope let array_val = scope
.block .block
.build(load_n, Instr::Load(array, array_ty)) .build_named(load_n, Instr::Load(array, array_ty))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
@ -897,7 +897,7 @@ impl mir::Expression {
let value = scope let value = scope
.block .block
.build( .build_named(
gep_n, gep_n,
Instr::GetStructElemPtr(struct_val.instr(), idx as u32), Instr::GetStructElemPtr(struct_val.instr(), idx as u32),
) )
@ -910,7 +910,7 @@ impl mir::Expression {
struct_val.0.derive( struct_val.0.derive(
scope scope
.block .block
.build( .build_named(
load_n, load_n,
Instr::Load( Instr::Load(
value, value,
@ -937,7 +937,7 @@ impl mir::Expression {
let struct_ptr = scope let struct_ptr = scope
.block .block
.build(name, Instr::Alloca(struct_ty.clone())) .build_named(name, Instr::Alloca(struct_ty.clone()))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
@ -947,13 +947,13 @@ impl mir::Expression {
let elem_ptr = scope let elem_ptr = scope
.block .block
.build(gep_n, Instr::GetStructElemPtr(struct_ptr, i as u32)) .build_named(gep_n, Instr::GetStructElemPtr(struct_ptr, i as u32))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
if let Some(val) = exp.codegen(scope, state) { if let Some(val) = exp.codegen(scope, state) {
scope scope
.block .block
.build(store_n, Instr::Store(elem_ptr, val.instr())) .build_named(store_n, Instr::Store(elem_ptr, val.instr()))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
} }
@ -961,7 +961,7 @@ impl mir::Expression {
let struct_val = scope let struct_val = scope
.block .block
.build(load_n, Instr::Load(struct_ptr, struct_ty)) .build_named(load_n, Instr::Load(struct_ptr, struct_ty))
.unwrap(); .unwrap();
Some(StackValue( Some(StackValue(
@ -993,7 +993,7 @@ impl mir::Expression {
let var_ptr_instr = scope let var_ptr_instr = scope
.block .block
.build( .build_named(
format!("{}.deref", varref.1), format!("{}.deref", varref.1),
Instr::Load( Instr::Load(
v.0.instr(), v.0.instr(),
@ -1009,7 +1009,7 @@ impl mir::Expression {
v.0.derive( v.0.derive(
scope scope
.block .block
.build( .build_named(
format!("{}.deref.inner", varref.1), format!("{}.deref.inner", varref.1),
Instr::Load( Instr::Load(
var_ptr_instr, var_ptr_instr,
@ -1028,7 +1028,21 @@ impl mir::Expression {
} }
}) })
} }
mir::ExprKind::CastTo(expression, type_kind) => todo!(), mir::ExprKind::CastTo(expression, type_kind) => {
let val = expression.codegen(scope, state)?;
let cast_instr = val
.1
.get_type(scope.type_values, scope.types)
.cast_instruction(
val.instr(),
&type_kind.get_type(scope.type_values, scope.types),
)
.unwrap();
Some(StackValue(
val.0.derive(scope.block.build(cast_instr).unwrap()),
type_kind.clone(),
))
}
}; };
if let Some(value) = &value { if let Some(value) = &value {
value.instr().maybe_location(&mut scope.block, location); value.instr().maybe_location(&mut scope.block, location);
@ -1109,7 +1123,7 @@ impl mir::IfExpression {
incoming.extend(else_res.clone()); incoming.extend(else_res.clone());
let instr = scope let instr = scope
.block .block
.build( .build_named(
"phi", "phi",
Instr::Phi(incoming.iter().map(|i| i.instr()).collect()), Instr::Phi(incoming.iter().map(|i| i.instr()).collect()),
) )
@ -1151,7 +1165,7 @@ impl mir::CmpOperator {
impl mir::Literal { impl mir::Literal {
fn as_const(&self, block: &mut Block) -> InstructionValue { fn as_const(&self, block: &mut Block) -> InstructionValue {
block block
.build(format!("{}", self), self.as_const_kind()) .build_named(format!("{}", self), self.as_const_kind())
.unwrap() .unwrap()
} }

View File

@ -1,6 +1,6 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn other() -> u16 { fn other() -> i16 {
return 6; return 6;
} }