Add array and pointer-types to lib

This commit is contained in:
Sofia 2025-07-19 16:18:29 +03:00
parent d64cf750b1
commit 98169af415
4 changed files with 61 additions and 33 deletions

View File

@ -2,7 +2,10 @@
//! LLIR ([`Context`]) into LLVM IR. This module is the only one that interfaces //! LLIR ([`Context`]) into LLVM IR. This module is the only one that interfaces
//! with the LLVM API. //! with the LLVM API.
use std::{collections::HashMap, ptr::null_mut}; use std::{
collections::HashMap,
ptr::{null, null_mut},
};
use llvm_sys::{ use llvm_sys::{
LLVMIntPredicate, LLVMLinkage, LLVMValueKind, LLVMIntPredicate, LLVMLinkage, LLVMValueKind,
@ -475,6 +478,23 @@ impl DebugTypeHolder {
subprogram.flags.as_llvm(), subprogram.flags.as_llvm(),
) )
} }
DebugTypeData::Pointer(ptr) => LLVMDIBuilderCreatePointerType(
debug.builder,
*debug.types.get(&ptr.pointee).unwrap(),
ptr.size_bits,
0,
0,
into_cstring(ptr.name.clone()).as_ptr(),
ptr.name.len(),
),
DebugTypeData::Array(array) => LLVMDIBuilderCreateArrayType(
debug.builder,
array.size_bits,
array.align_bits,
*debug.types.get(&array.element_type).unwrap(),
Vec::new().as_mut_ptr(),
0,
),
} }
} }
} }

View File

@ -9,9 +9,9 @@ use crate::{
CmpPredicate, Instr, InstructionData, TerminatorKind, CmpPredicate, Instr, InstructionData, TerminatorKind,
builder::*, builder::*,
debug_information::{ debug_information::{
DebugBasicType, DebugLocation, DebugLocationValue, DebugMetadataHolder, DebugMetadataValue, DebugArrayType, DebugBasicType, DebugLocation, DebugLocationValue, DebugMetadataHolder,
DebugProgramValue, DebugScopeValue, DebugSubprogramType, DebugTypeData, DebugTypeHolder, DebugMetadataValue, DebugPointerType, DebugProgramValue, DebugScopeValue,
DebugTypeValue, DebugSubprogramType, DebugTypeData, DebugTypeHolder, DebugTypeValue,
}, },
}; };
@ -237,8 +237,12 @@ impl Debug for DebugTypeHolder {
impl Debug for DebugTypeData { impl Debug for DebugTypeData {
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 {
Self::Basic(basic) => Debug::fmt(basic, f), DebugTypeData::Basic(ty) => Debug::fmt(ty, f),
Self::Subprogram(subprogram) => Debug::fmt(subprogram, f), DebugTypeData::Subprogram(ty) => Debug::fmt(ty, f),
DebugTypeData::Basic(ty) => Debug::fmt(ty, f),
DebugTypeData::Subprogram(ty) => Debug::fmt(ty, f),
DebugTypeData::Pointer(ty) => Debug::fmt(ty, f),
DebugTypeData::Array(ty) => Debug::fmt(ty, f),
} }
} }
} }
@ -257,12 +261,30 @@ impl Debug for DebugBasicType {
impl Debug for DebugSubprogramType { impl Debug for DebugSubprogramType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Subprogram") f.debug_tuple("Subprogram")
.field(&self.params) .field(&self.parameters)
.field(&self.flags) .field(&self.flags)
.finish() .finish()
} }
} }
impl Debug for DebugPointerType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(&format!("Pointer<{:?}>({})", self.pointee, self.name))
.field(&self.size_bits)
.finish()
}
}
impl Debug for DebugArrayType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(&format!("Array<{:?}>", self.element_type))
.field("size_bits", &self.size_bits)
.field("align_bits", &self.align_bits)
.field("subscripts", &self.subscripts)
.finish()
}
}
impl Debug for DebugMetadataValue { impl Debug for DebugMetadataValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Meta[{}]", self.0) write!(f, "Meta[{}]", self.0)

View File

@ -257,7 +257,9 @@ pub struct DwarfFlags;
#[derive(Clone)] #[derive(Clone)]
pub enum DebugTypeData { pub enum DebugTypeData {
Basic(DebugBasicType), Basic(DebugBasicType),
Subprogram(DebugSubprogramTypeData), Subprogram(DebugSubprogramType),
Pointer(DebugPointerType),
Array(DebugArrayType),
} }
#[derive(Clone)] #[derive(Clone)]
@ -273,11 +275,10 @@ pub struct DebugBasicType {
#[derive(Clone)] #[derive(Clone)]
pub struct DebugArrayType { pub struct DebugArrayType {
pub length: u64, pub size_bits: u64,
/// Alignment
pub align_bits: u32, pub align_bits: u32,
pub array_type: DebugTypeValue, pub element_type: DebugTypeValue,
pub elements: Vec<DebugTypeValue>, pub subscripts: Vec<DebugTypeValue>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -285,25 +286,11 @@ pub struct DebugPointerType {
pub name: String, pub name: String,
pub pointee: DebugTypeValue, pub pointee: DebugTypeValue,
pub size_bits: u64, pub size_bits: u64,
pub align_bits: u64,
} }
#[derive(Clone)] #[derive(Clone)]
pub struct DebugStructType {
location: DebugLocationValue,
pub size_bits: u64,
pub align_bits: u64,
pub flags: DwarfFlags,
}
#[derive(Debug, Clone)]
pub struct DebugSubprogramTypeData {
pub parameters: Vec<DebugTypeValue>,
pub flags: DwarfFlags,
}
pub struct DebugSubprogramType { pub struct DebugSubprogramType {
pub params: Vec<DebugProgramValue>, pub parameters: Vec<DebugTypeValue>,
pub flags: DwarfFlags, pub flags: DwarfFlags,
} }

View File

@ -6,7 +6,7 @@ use reid_lib::{
debug_information::{ debug_information::{
DebugBasicType, DebugFileData, DebugInformation, DebugLocalVariable, DebugLocation, DebugBasicType, DebugFileData, DebugInformation, DebugLocalVariable, DebugLocation,
DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugProgramValue, DebugRecordKind, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugProgramValue, DebugRecordKind,
DebugScopeValue, DebugSubprogramData, DebugSubprogramOptionals, DebugSubprogramTypeData, DebugScopeValue, DebugSubprogramData, DebugSubprogramOptionals, DebugSubprogramType,
DebugTypeData, DebugTypeValue, DwarfEncoding, DwarfFlags, InstructionDebugRecordData, DebugTypeData, DebugTypeValue, DwarfEncoding, DwarfFlags, InstructionDebugRecordData,
}, },
Block, CmpPredicate, ConstValue, Context, CustomTypeKind, Function, FunctionFlags, Instr, Block, CmpPredicate, ConstValue, Context, CustomTypeKind, Function, FunctionFlags, Instr,
@ -307,11 +307,10 @@ impl mir::Module {
.return_type .return_type
.get_debug_type(&debug_types, &debug); .get_debug_type(&debug_types, &debug);
let debug_ty = let debug_ty = debug.debug_type(DebugTypeData::Subprogram(DebugSubprogramType {
debug.debug_type(DebugTypeData::Subprogram(DebugSubprogramTypeData { parameters: vec![*fn_param_ty],
parameters: vec![*fn_param_ty], flags: DwarfFlags,
flags: DwarfFlags, }));
}));
let subprogram = debug.subprogram(DebugSubprogramData { let subprogram = debug.subprogram(DebugSubprogramData {
name: mir_function.name.clone(), name: mir_function.name.clone(),