Skip to content

Commit

Permalink
feat: more debug locations
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 24, 2024
1 parent 1e7bb55 commit 3992a57
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 136 deletions.
16 changes: 9 additions & 7 deletions lib/edlang_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,25 @@ pub struct Ident {
pub struct Type {
pub name: Ident,
pub generics: Vec<Type>,
pub is_ref: Option<RefType>,
pub qualifiers: Vec<TypeQualifier>,
pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TypeQualifier {
Ref, // &
RefMut, // &mut
Ptr, // *const
PtrMut, // *mut
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FnParam {
pub name: Ident,
pub arg_type: Type,
pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RefType {
Not,
Mut,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Block {
pub body: Vec<Statement>,
Expand Down
118 changes: 81 additions & 37 deletions lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError>
.build_load(compile_basic_type(ctx, &local_ty), ptr, "deref")?
.into_pointer_value();
local_ty = match local_ty.kind {
ir::TypeKind::Ptr(inner) => *inner,
ir::TypeKind::Ptr(_, inner) => *inner,
ir::TypeKind::Ref(_, inner) => *inner,
_ => unreachable!(),
}
Expand Down Expand Up @@ -606,7 +606,7 @@ fn compile_bin_op<'ctx>(
let is_float = matches!(lhs_ty.kind, ir::TypeKind::Float(_));
let is_signed = matches!(lhs_ty.kind, ir::TypeKind::Int(_));

Ok(match op {
let (result, ty) = match op {
ir::BinOp::Add => {
let value = if is_float {
ctx.builder
Expand Down Expand Up @@ -931,7 +931,9 @@ fn compile_bin_op<'ctx>(
)
}
ir::BinOp::Offset => todo!(),
})
};

Ok((result, ty))
}

fn compile_rvalue<'ctx>(
Expand All @@ -941,41 +943,77 @@ fn compile_rvalue<'ctx>(
rvalue: &ir::RValue,
) -> Result<(BasicValueEnum<'ctx>, TypeInfo), BuilderError> {
Ok(match rvalue {
ir::RValue::Use(op) => compile_load_operand(ctx, fn_id, locals, op)?,
ir::RValue::Ref(_mutable, op) => match op {
ir::Operand::Copy(_) => todo!(),
ir::Operand::Move(place) => {
let mut ptr = *locals.get(&place.local).unwrap();
let mut local_ty = {
let body = ctx.ctx.program.functions.get(&fn_id).unwrap();
body.locals[place.local].ty.clone()
};

for proj in &place.projection {
match proj {
ir::PlaceElem::Deref => {
ptr = ctx
.builder
.build_load(compile_basic_type(ctx, &local_ty), ptr, "deref")?
.into_pointer_value();
local_ty = match local_ty.kind {
ir::TypeKind::Ptr(inner) => *inner,
ir::TypeKind::Ref(_, inner) => *inner,
_ => unreachable!(),
ir::RValue::Use(op, span) => {
ctx.set_debug_loc(
ctx.builder
.get_current_debug_location()
.unwrap()
.get_scope(),
*span,
);
compile_load_operand(ctx, fn_id, locals, op)?
}
ir::RValue::Ref(_mutable, op, span) => {
ctx.set_debug_loc(
ctx.builder
.get_current_debug_location()
.unwrap()
.get_scope(),
*span,
);
match op {
ir::Operand::Copy(_) => todo!(),
ir::Operand::Move(place) => {
let mut ptr = *locals.get(&place.local).unwrap();
let mut local_ty = {
let body = ctx.ctx.program.functions.get(&fn_id).unwrap();
body.locals[place.local].ty.clone()
};

for proj in &place.projection {
match proj {
ir::PlaceElem::Deref => {
ptr = ctx
.builder
.build_load(compile_basic_type(ctx, &local_ty), ptr, "deref")?
.into_pointer_value();
local_ty = match local_ty.kind {
ir::TypeKind::Ptr(_, inner) => *inner,
ir::TypeKind::Ref(_, inner) => *inner,
_ => unreachable!(),
}
}
ir::PlaceElem::Field { .. } => todo!(),
ir::PlaceElem::Index { .. } => todo!(),
}
ir::PlaceElem::Field { .. } => todo!(),
ir::PlaceElem::Index { .. } => todo!(),
}
}

(ptr.as_basic_value_enum(), local_ty)
(ptr.as_basic_value_enum(), local_ty)
}
ir::Operand::Constant(_) => todo!("references to constants not yet implemented"),
}
ir::Operand::Constant(_) => todo!("references to constants not yet implemented"),
},
ir::RValue::BinOp(op, lhs, rhs) => compile_bin_op(ctx, fn_id, locals, *op, lhs, rhs)?,
ir::RValue::LogicOp(_, _, _) => todo!(),
ir::RValue::UnOp(op, value) => compile_unary_op(ctx, fn_id, locals, *op, value)?,
}
ir::RValue::BinOp(op, lhs, rhs, span) => {
ctx.set_debug_loc(
ctx.builder
.get_current_debug_location()
.unwrap()
.get_scope(),
*span,
);
compile_bin_op(ctx, fn_id, locals, *op, lhs, rhs)?
}
ir::RValue::LogicOp(_, _, _, _span) => todo!(),
ir::RValue::UnOp(op, value, span) => {
ctx.set_debug_loc(
ctx.builder
.get_current_debug_location()
.unwrap()
.get_scope(),
*span,
);
compile_unary_op(ctx, fn_id, locals, *op, value)?
}
})
}

Expand Down Expand Up @@ -1017,7 +1055,7 @@ fn compile_load_place<'ctx>(
.build_load(compile_basic_type(ctx, &local_ty), ptr, "deref")?
.into_pointer_value();
local_ty = match local_ty.kind {
ir::TypeKind::Ptr(inner) => *inner,
ir::TypeKind::Ptr(_, inner) => *inner,
ir::TypeKind::Ref(_, inner) => *inner,
_ => unreachable!(),
}
Expand Down Expand Up @@ -1110,6 +1148,10 @@ fn compile_value<'ctx>(
.into_int_type()
.const_int((*x) as u64, false)
.as_basic_value_enum(),
ir::ConstValue::Isize(x) => ty
.into_int_type()
.const_int((*x) as u64, true)
.as_basic_value_enum(),
},
ValueTree::Branch(_) => todo!(),
})
Expand Down Expand Up @@ -1178,7 +1220,7 @@ fn compile_basic_type<'ctx>(
ir::TypeKind::FnDef(_def_id, _generic_args) => {
panic!()
}
ir::TypeKind::Ptr(_pointee) => ctx
ir::TypeKind::Ptr(_is_mut, _pointee) => ctx
.ctx
.context
.ptr_sized_int_type(&ctx.target_data, None)
Expand All @@ -1205,6 +1247,7 @@ fn compile_basic_type<'ctx>(
.struct_type(&fields, false)
.as_basic_type_enum()
}
ir::TypeKind::Str => todo!(),
}
}

Expand Down Expand Up @@ -1308,10 +1351,10 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
ir::TypeKind::FnDef(_def_id, _generic_args) => {
panic!()
}
ir::TypeKind::Ptr(pointee) => ctx
ir::TypeKind::Ptr(_is_mut, pointee) => ctx
.di_builder
.create_pointer_type(
&format!("ptr<{:?}>", pointee.kind),
&format!("*{:?}", pointee.kind),
compile_debug_type(ctx, pointee),
(ctx.target_data.get_pointer_byte_size(None) * 8).into(),
ctx.target_data.get_pointer_byte_size(None),
Expand Down Expand Up @@ -1357,5 +1400,6 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
)
.as_type()
}
ir::TypeKind::Str => todo!(),
}
}
18 changes: 11 additions & 7 deletions lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ pub enum TypeKind {
Uint(UintTy),
Float(FloatTy),
FnDef(DefId, Vec<TypeInfo>), // The vec are generic types, not arg types
Ptr(Box<TypeInfo>),
Str,
Ptr(bool, Box<TypeInfo>),
Ref(bool, Box<TypeInfo>),
Struct(DefId), // todo, add generics
}
Expand Down Expand Up @@ -259,9 +260,10 @@ impl TypeKind {
Self::Float(_) => todo!(),
TypeKind::Unit => unreachable!(),
TypeKind::FnDef(_, _) => unreachable!(),
TypeKind::Ptr(_pointee) => todo!(),
TypeKind::Ptr(_, _pointee) => todo!(),
TypeKind::Ref(_, inner) => inner.kind.get_falsy_value(),
TypeKind::Struct(_) => todo!(),
TypeKind::Str => todo!(),
}
}
}
Expand Down Expand Up @@ -329,6 +331,7 @@ impl ValueTree {
ConstValue::F32(_) => TypeKind::Float(FloatTy::F32),
ConstValue::F64(_) => TypeKind::Float(FloatTy::F64),
ConstValue::Char(_) => TypeKind::Char,
ConstValue::Isize(_) => TypeKind::Int(IntTy::Isize),
},
ValueTree::Branch(_) => todo!(),
}
Expand All @@ -337,11 +340,11 @@ impl ValueTree {

#[derive(Debug, Clone)]
pub enum RValue {
Use(Operand),
Ref(bool, Operand),
BinOp(BinOp, Operand, Operand),
LogicOp(LogicalOp, Operand, Operand),
UnOp(UnOp, Operand),
Use(Operand, Span),
Ref(bool, Operand, Span),
BinOp(BinOp, Operand, Operand, Span),
LogicOp(LogicalOp, Operand, Operand, Span),
UnOp(UnOp, Operand, Span),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -465,6 +468,7 @@ pub enum ConstValue {
I32(i32),
I64(i64),
I128(i128),
Isize(isize),
U8(u8),
U16(u16),
U32(u32),
Expand Down
Loading

0 comments on commit 3992a57

Please sign in to comment.