Add custom debug format for LLIR

This commit is contained in:
Sofia 2025-07-08 00:04:35 +03:00
parent 22737f022e
commit 95b3ffe8ef
3 changed files with 123 additions and 64 deletions

View File

@ -5,40 +5,40 @@ use crate::{
TerminatorKind, Type, util::match_types,
};
#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)]
pub struct ModuleValue(usize);
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
pub struct ModuleValue(pub(crate) usize);
#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)]
pub struct FunctionValue(ModuleValue, usize);
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
pub struct FunctionValue(pub(crate) ModuleValue, pub(crate) usize);
#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)]
pub struct BlockValue(FunctionValue, usize);
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
pub struct BlockValue(pub(crate) FunctionValue, pub(crate) usize);
#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)]
pub struct InstructionValue(pub(crate) BlockValue, usize);
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
pub struct InstructionValue(pub(crate) BlockValue, pub(crate) usize);
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct ModuleHolder {
pub(crate) value: ModuleValue,
pub(crate) data: ModuleData,
pub(crate) functions: Vec<FunctionHolder>,
}
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct FunctionHolder {
pub(crate) value: FunctionValue,
pub(crate) data: FunctionData,
pub(crate) blocks: Vec<BlockHolder>,
}
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct BlockHolder {
pub(crate) value: BlockValue,
pub(crate) data: BlockData,
pub(crate) instructions: Vec<InstructionHolder>,
}
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct InstructionHolder {
pub(crate) value: InstructionValue,
pub(crate) data: InstructionData,
@ -190,54 +190,6 @@ impl Builder {
self.modules.clone()
}
// pub(crate) fn get_functions(&self, module: ModuleValue) -> Vec<(FunctionValue, FunctionData)> {
// unsafe {
// self.modules
// .borrow()
// .get_unchecked(module.0)
// .2
// .iter()
// .map(|h| (h.0, h.1.clone()))
// .collect()
// }
// }
// pub(crate) fn get_blocks(&self, function: FunctionValue) -> Vec<(BlockValue, BlockData)> {
// unsafe {
// self.modules
// .borrow()
// .get_unchecked(function.0.0)
// .2
// .get_unchecked(function.1)
// .2
// .iter()
// .map(|h| (h.0, h.1.clone()))
// .collect()
// }
// }
// pub(crate) fn get_instructions(
// &self,
// block: BlockValue,
// ) -> (
// Vec<(InstructionValue, InstructionData)>,
// Option<TerminatorKind>,
// ) {
// unsafe {
// let modules = self.modules.borrow();
// let block = modules
// .get_unchecked(block.0.0.0)
// .2
// .get_unchecked(block.0.1)
// .2
// .get_unchecked(block.1);
// (
// block.2.iter().map(|h| (h.0, h.1.clone())).collect(),
// block.1.terminator.clone(),
// )
// }
// }
pub fn check_instruction(&self, instruction: &InstructionValue) -> Result<(), ()> {
use super::InstructionKind::*;
unsafe {

106
reid-llvm-lib/src/debug.rs Normal file
View File

@ -0,0 +1,106 @@
use std::fmt::Debug;
use crate::{InstructionData, InstructionKind, IntPredicate, TerminatorKind, builder::*};
impl Debug for ModuleHolder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(&format!("Module({})", self.data.name))
.field(&self.value)
.field(&self.functions)
.finish()
}
}
impl Debug for FunctionHolder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(&format!(
"{}({:?}) -> {:?}",
self.data.name, self.data.params, self.data.ret
))
.field(&self.blocks)
.finish()
}
}
impl Debug for BlockHolder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(&format!("Block({})", self.data.name))
.field(&self.value)
.field(&self.instructions)
.field(&self.data.terminator)
.finish()
}
}
impl Debug for InstructionHolder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?} = {:?}", &self.value, &self.data)
}
}
impl Debug for InstructionData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.kind.fmt(f)
}
}
impl Debug for ModuleValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "M[{}]", &self.0)
}
}
impl Debug for FunctionValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "F[{}, {}]", &self.0.0, &self.1,)
}
}
impl Debug for BlockValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "B[{}, {}, {}]", &self.0.0.0, &self.0.1, self.1)
}
}
impl Debug for InstructionValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"I<{}-{}-{}-{}>",
&self.0.0.0.0, &self.0.0.1, &self.0.1, self.1
)
}
}
impl Debug for InstructionKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Param(nth) => write!(f, "Param({})", &nth),
Self::Constant(c) => write!(f, "{:?}", &c),
Self::Add(lhs, rhs) => write!(f, "{:?} + {:?}", &lhs, &rhs),
Self::Sub(lhs, rhs) => write!(f, "{:?} + {:?}", &lhs, &rhs),
Self::Phi(val) => write!(f, "Phi: {:?}", &val),
Self::ICmp(cmp, lhs, rhs) => write!(f, "{:?} {:?} {:?}", &lhs, &cmp, &rhs),
Self::FunctionCall(fun, params) => write!(f, "{:?}({:?})", &fun, &params),
}
}
}
impl Debug for IntPredicate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::LessThan => write!(f, "<"),
Self::GreaterThan => write!(f, ">"),
}
}
}
impl Debug for TerminatorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Ret(val) => write!(f, "Ret {:?}", &val),
Self::Branch(val) => write!(f, "Br {:?}", &val),
Self::CondBr(cond, b1, b2) => write!(f, "CondBr {:?} ? {:?} : {:?}", &cond, &b1, &b2),
}
}
}

View File

@ -4,6 +4,7 @@ use builder::{BlockValue, Builder, FunctionValue, InstructionValue, ModuleValue}
pub mod builder;
pub mod compile;
mod debug;
mod util;
// pub struct InstructionValue(BlockValue, usize);
@ -130,18 +131,18 @@ impl<'builder> Block<'builder> {
}
}
#[derive(Debug, Clone, Hash)]
#[derive(Clone, Hash)]
pub struct InstructionData {
kind: InstructionKind,
}
#[derive(Debug, Clone, Copy, Hash)]
#[derive(Clone, Copy, Hash)]
pub enum IntPredicate {
LessThan,
GreaterThan,
}
#[derive(Debug, Clone, Hash)]
#[derive(Clone, Hash)]
pub enum InstructionKind {
Param(usize),
Constant(ConstValue),
@ -171,7 +172,7 @@ pub enum ConstValue {
U32(u32),
}
#[derive(Debug, Clone, Hash)]
#[derive(Clone, Hash)]
pub enum TerminatorKind {
Ret(InstructionValue),
Branch(BlockValue),