Compare commits

..

No commits in common. "312a7772037298b60a88c289e301e48beb742c8a" and "515c031f195833411dca621751c473e8e1f9acf4" have entirely different histories.

6 changed files with 26 additions and 187 deletions

View File

@ -3,8 +3,6 @@
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use llvm_sys::core::LLVMBuildArrayAlloca;
use crate::{ use crate::{
BlockData, ConstValue, FunctionData, Instr, InstructionData, ModuleData, TerminatorKind, Type, BlockData, ConstValue, FunctionData, Instr, InstructionData, ModuleData, TerminatorKind, Type,
util::match_types, util::match_types,
@ -279,28 +277,6 @@ impl Builder {
Err(()) Err(())
} }
} }
Extract(arr, idx) => {
let arr_ty = arr.get_type(&self)?;
if let Type::Array(_, len) = arr_ty {
if len > idx { Ok(()) } else { Err(()) }
} else {
Err(())
}
}
ArrayAlloca(_, _) => Ok(()),
Insert(arr, idx, val) => {
let arr_ty = arr.get_type(&self)?;
let val_ty = val.get_type(&self)?;
if let Type::Array(elem_ty, len) = arr_ty {
if val_ty == *elem_ty && len > idx {
Ok(())
} else {
Err(())
}
} else {
Err(())
}
}
} }
} }
} }
@ -362,15 +338,6 @@ impl InstructionValue {
Alloca(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))), Alloca(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))),
Load(_, ty) => Ok(ty.clone()), Load(_, ty) => Ok(ty.clone()),
Store(_, value) => value.get_type(builder), Store(_, value) => value.get_type(builder),
Extract(arr, _) => match arr.get_type(builder) {
Ok(Type::Array(elem_t, _)) => Ok(*elem_t),
Ok(_) => Err(()),
Err(_) => Err(()),
},
ArrayAlloca(ty, len_value) => {
Ok(Type::Array(Box::new(ty.clone()), len_value.clone()))
}
Insert(_, _, val) => val.get_type(builder),
} }
} }
} }
@ -391,15 +358,6 @@ impl ConstValue {
ConstValue::U64(_) => U64, ConstValue::U64(_) => U64,
ConstValue::U128(_) => U128, ConstValue::U128(_) => U128,
ConstValue::Bool(_) => Bool, ConstValue::Bool(_) => Bool,
// ConstValue::Array(arr) => Array(
// Box::new(
// arr.iter()
// .map(|a| a.get_type(builder).unwrap())
// .next()
// .unwrap_or(Void),
// ),
// arr.len() as u32,
// ),
} }
} }
} }
@ -420,7 +378,6 @@ impl Type {
Type::Bool => true, Type::Bool => true,
Type::Void => false, Type::Void => false,
Type::Ptr(_) => false, Type::Ptr(_) => false,
Type::Array(_, _) => false,
} }
} }
@ -439,7 +396,6 @@ impl Type {
Type::Bool => false, Type::Bool => false,
Type::Void => false, Type::Void => false,
Type::Ptr(_) => false, Type::Ptr(_) => false,
Type::Array(_, _) => false,
} }
} }
} }

View File

@ -349,66 +349,6 @@ impl InstructionHolder {
module.values.get(&val).unwrap().value_ref, module.values.get(&val).unwrap().value_ref,
module.values.get(&ptr).unwrap().value_ref, module.values.get(&ptr).unwrap().value_ref,
), ),
Extract(arr, idx) => {
let t = arr.get_type(module.builder).unwrap();
let Type::Array(elem_t, _) = t else { panic!() };
let indices = &mut [ConstValue::I32(*idx as i32).as_llvm(module.context_ref)];
let ptr = LLVMBuildGEP2(
module.builder_ref,
elem_t.as_llvm(module.context_ref),
module.values.get(arr).unwrap().value_ref,
indices.as_mut_ptr(),
1,
c"insert_gep".as_ptr(),
);
LLVMBuildLoad2(
module.builder_ref,
elem_t.as_llvm(module.context_ref),
ptr,
c"load".as_ptr(),
)
}
ArrayAlloca(ty, len) => {
let array_len = ConstValue::U16(*len as u16).as_llvm(module.context_ref);
let array_ty = Type::Array(Box::new(ty.clone()), *len);
dbg!(
&ty.as_llvm(module.context_ref),
&array_ty.as_llvm(module.context_ref)
);
LLVMBuildArrayAlloca(
module.builder_ref,
ty.as_llvm(module.context_ref),
array_len,
c"array_alloca".as_ptr(),
)
}
Insert(arr, idx, val) => {
let indices = &mut [ConstValue::I32(*idx as i32).as_llvm(module.context_ref)];
let ptr = LLVMBuildGEP2(
module.builder_ref,
val.get_type(module.builder)
.unwrap()
.as_llvm(module.context_ref),
module.values.get(arr).unwrap().value_ref,
indices.as_mut_ptr(),
1,
c"insert_gep".as_ptr(),
);
LLVMBuildStore(
module.builder_ref,
module.values.get(val).unwrap().value_ref,
ptr,
)
// LLVMBuildInsertValue(
// module.builder_ref,
// ptr,
// module.values.get(val).unwrap().value_ref,
// *idx,
// c"insert".as_ptr(),
// )
}
} }
}; };
LLVMValue { LLVMValue {
@ -475,18 +415,18 @@ impl ConstValue {
fn as_llvm(&self, context: LLVMContextRef) -> LLVMValueRef { fn as_llvm(&self, context: LLVMContextRef) -> LLVMValueRef {
unsafe { unsafe {
let t = self.get_type().as_llvm(context); let t = self.get_type().as_llvm(context);
match self { match *self {
ConstValue::Bool(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::Bool(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::I8(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::I8(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::I16(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::I16(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::I32(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::I32(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::I64(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::I64(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::I128(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::I128(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::U8(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U8(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::U16(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U16(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::U32(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U32(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::U64(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U64(val) => LLVMConstInt(t, val as u64, 1),
ConstValue::U128(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U128(val) => LLVMConstInt(t, val as u64, 1),
} }
} }
} }
@ -505,7 +445,6 @@ impl Type {
Bool => LLVMInt1TypeInContext(context), Bool => LLVMInt1TypeInContext(context),
Void => LLVMVoidType(), Void => LLVMVoidType(),
Ptr(ty) => LLVMPointerType(ty.as_llvm(context), 0), Ptr(ty) => LLVMPointerType(ty.as_llvm(context), 0),
Array(elem_t, _) => LLVMPointerType(elem_t.as_llvm(context), 0),
} }
} }
} }

View File

@ -88,23 +88,18 @@ impl Debug for InstructionValue {
impl Debug for Instr { impl Debug for Instr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Instr::Param(nth) => fmt_call(f, &"Param", &nth), Self::Param(nth) => fmt_call(f, &"Param", &nth),
Instr::Constant(c) => c.fmt(f), Self::Constant(c) => c.fmt(f),
Instr::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs), Self::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs),
Instr::Sub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs), Self::Sub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs),
Instr::Mult(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs), Self::Mult(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs),
Instr::And(lhs, rhs) => fmt_binop(f, lhs, &"&&", rhs), Self::And(lhs, rhs) => fmt_binop(f, lhs, &"&&", rhs),
Instr::Phi(val) => fmt_call(f, &"Phi", &val), Self::Phi(val) => fmt_call(f, &"Phi", &val),
Instr::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs), Self::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs),
Instr::FunctionCall(fun, params) => fmt_call(f, fun, params), Self::FunctionCall(fun, params) => fmt_call(f, fun, params),
Instr::Alloca(name, ty) => write!(f, "alloca<{:?}>({})", ty, name), Instr::Alloca(name, ty) => write!(f, "alloca<{:?}>({})", ty, name),
Instr::Load(val, ty) => write!(f, "load<{:?}>({:?})", ty, val), Instr::Load(val, ty) => write!(f, "load<{:?}>({:?})", ty, val),
Instr::Store(ptr, val) => write!(f, "store({:?} = {:?})", ptr, val), Instr::Store(ptr, val) => write!(f, "store({:?} = {:?})", ptr, val),
Instr::Extract(instruction_value, idx) => fmt_index(f, instruction_value, idx),
Instr::ArrayAlloca(ty, instruction_value) => {
write!(f, "array_alloca<{:?}>({:?})", ty, instruction_value)
}
Instr::Insert(arr, idx, val) => write!(f, "{:?}[{}] = {:?}", arr, idx, val),
} }
} }
} }
@ -133,17 +128,6 @@ fn fmt_call(
f.write_char(')') f.write_char(')')
} }
fn fmt_index(
f: &mut std::fmt::Formatter<'_>,
fun: &impl std::fmt::Debug,
params: &impl std::fmt::Debug,
) -> std::fmt::Result {
fun.fmt(f)?;
f.write_char('[')?;
params.fmt(f)?;
f.write_char(']')
}
impl Debug for CmpPredicate { impl Debug for CmpPredicate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {

View File

@ -177,9 +177,6 @@ pub enum Instr {
Alloca(String, Type), Alloca(String, Type),
Load(InstructionValue, Type), Load(InstructionValue, Type),
Store(InstructionValue, InstructionValue), Store(InstructionValue, InstructionValue),
ArrayAlloca(Type, u32),
Insert(InstructionValue, u32, InstructionValue),
Extract(InstructionValue, u32),
/// Integer Comparison /// Integer Comparison
ICmp(CmpPredicate, InstructionValue, InstructionValue), ICmp(CmpPredicate, InstructionValue, InstructionValue),
@ -202,7 +199,6 @@ pub enum Type {
Bool, Bool,
Void, Void,
Ptr(Box<Type>), Ptr(Box<Type>),
Array(Box<Type>, u32),
} }
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]

View File

@ -1,7 +1,7 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn array() -> [u16; 4] { fn array() -> [u16; 4] {
return [10, 15, 7, 9]; return [1, 2, 3, 4];
} }
fn main() -> u16 { fn main() -> u16 {
@ -11,5 +11,5 @@ fn main() -> u16 {
// list[1] = 5; // list[1] = 5;
return list[3]; return list[0];
} }

View File

@ -334,44 +334,8 @@ impl mir::Expression {
None None
} }
} }
mir::ExprKind::Index(expression, idx) => { mir::ExprKind::Index(expression, _) => todo!("codegen for index expression"),
let expr = expression.codegen(scope)?; mir::ExprKind::Array(expressions) => todo!("codegen for array expression"),
Some(
scope
.block
.build(Instr::Extract(expr, *idx as u32))
.unwrap(),
)
}
mir::ExprKind::Array(expressions) => {
let instr_list = expressions
.iter()
.map(|e| e.codegen(scope).unwrap())
.collect::<Vec<_>>();
let instr_t = expressions
.iter()
.map(|e| e.return_type().unwrap().1)
.next()
.unwrap_or(TypeKind::Void);
dbg!(&instr_t);
let array = scope
.block
.build(Instr::ArrayAlloca(
instr_t.get_type(),
instr_list.len() as u32,
))
.unwrap();
for (i, instr) in instr_list.iter().enumerate() {
scope
.block
.build(Instr::Insert(array, i as u32, *instr))
.unwrap();
}
Some(array)
}
} }
} }
} }
@ -450,9 +414,9 @@ impl TypeKind {
TypeKind::U64 => Type::U64, TypeKind::U64 => Type::U64,
TypeKind::U128 => Type::U128, TypeKind::U128 => Type::U128,
TypeKind::Bool => Type::Bool, TypeKind::Bool => Type::Bool,
TypeKind::Array(elem_t, len) => Type::Array(Box::new(elem_t.get_type()), *len as u32),
TypeKind::Void => panic!("Void not a supported type"), TypeKind::Void => panic!("Void not a supported type"),
TypeKind::Vague(_) => panic!("Tried to compile a vague type!"), TypeKind::Vague(_) => panic!("Tried to compile a vague type!"),
TypeKind::Array(_, _) => todo!("codegen for array type"),
} }
} }
} }