Get pointers working

This commit is contained in:
Sofia 2025-07-21 14:43:24 +03:00
parent 9db508bd9c
commit ed9c8638d5
5 changed files with 63 additions and 26 deletions

View File

@ -395,6 +395,8 @@ impl Builder {
Instr::Alloca(_) => Ok(()),
Instr::Load(ptr, load_ty) => {
let ptr_ty = ptr.get_type(&self)?;
dbg!(&self);
dbg!(&ptr_ty, &load_ty);
if let Type::Ptr(ptr_ty_inner) = ptr_ty {
if *ptr_ty_inner == load_ty {
Ok(())

View File

@ -68,6 +68,10 @@ pub struct CompileOutput {
impl CompiledModule {
pub fn output(&self) -> CompileOutput {
unsafe {
let llvm_ir = from_cstring(LLVMPrintModuleToString(self.module_ref))
.expect("Unable to print LLVM IR to string");
println!("{}", llvm_ir);
LLVM_InitializeAllTargets();
LLVM_InitializeAllTargetInfos();
LLVM_InitializeAllTargetMCs();

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, mem};
use std::{array, collections::HashMap, mem};
use reid_lib::{
builder::{InstructionValue, TypeValue},
@ -736,7 +736,7 @@ impl mir::Expression {
}
}
mir::ExprKind::Indexed(expression, val_t, idx_expr) => {
let StackValue(kind, array_ty) = expression
let StackValue(kind, ty) = expression
.codegen(scope, &state.load(false))
.expect("array returned none!");
let idx = idx_expr
@ -749,23 +749,49 @@ impl mir::Expression {
.build("array.zero", Instr::Constant(ConstValue::U32(0)))
.unwrap();
let ptr = scope
let TypeKind::CodegenPtr(inner) = ty else {
panic!();
};
let (ptr, contained_ty) = if let TypeKind::UserPtr(further_inner) = *inner.clone() {
dbg!(&further_inner, &val_t);
let loaded = scope
.block
.build(
"array.load",
Instr::Load(
kind.instr(),
inner.get_type(scope.type_values, scope.types),
),
)
.unwrap();
(
scope
.block
.build(format!("array.gep"), Instr::GetElemPtr(loaded, vec![idx]))
.unwrap()
.maybe_location(&mut scope.block, location),
*further_inner,
)
} else {
let TypeKind::Array(elem_ty, _) = *inner else {
panic!();
};
(
scope
.block
.build(
format!("array.gep"),
Instr::GetElemPtr(kind.instr(), vec![first, idx]),
)
.unwrap()
.maybe_location(&mut scope.block, location);
let TypeKind::CodegenPtr(inner) = array_ty else {
panic!();
};
let TypeKind::Array(elem_ty, _) = *inner else {
panic!();
.maybe_location(&mut scope.block, location),
val_t.clone(),
)
};
if state.should_load {
dbg!(&contained_ty);
Some(StackValue(
kind.derive(
scope
@ -774,16 +800,19 @@ impl mir::Expression {
"array.load",
Instr::Load(
ptr,
val_t.get_type(scope.type_values, scope.types),
contained_ty.get_type(scope.type_values, scope.types),
),
)
.unwrap()
.maybe_location(&mut scope.block, location),
),
*elem_ty,
contained_ty,
))
} else {
Some(StackValue(kind.derive(ptr), TypeKind::CodegenPtr(elem_ty)))
Some(StackValue(
kind.derive(ptr),
TypeKind::CodegenPtr(Box::new(contained_ty)),
))
}
}
mir::ExprKind::Array(expressions) => {
@ -1175,9 +1204,9 @@ impl TypeKind {
let type_val = type_vals.get(n).unwrap().clone();
Type::CustomType(type_val)
}
TypeKind::UserPtr(type_kind) => {
Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs)))
}
TypeKind::UserPtr(type_kind) => Type::Ptr(Box::new(Type::Ptr(Box::new(
type_kind.get_type(type_vals, typedefs),
)))),
TypeKind::CodegenPtr(type_kind) => {
Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs)))
}
@ -1228,7 +1257,7 @@ impl TypeKind {
),
size_bits: self.size_of(),
}),
TypeKind::CodegenPtr(inner) | TypeKind::Borrow(inner, _) => {
TypeKind::CodegenPtr(inner) | TypeKind::UserPtr(inner) | TypeKind::Borrow(inner, _) => {
DebugTypeData::Pointer(DebugPointerType {
name,
pointee: inner.get_debug_type_hard(

View File

@ -103,10 +103,10 @@ impl<'map> Pass for LinkerPass<'map> {
modules.insert(module.name.clone(), Rc::new(RefCell::new((module, tokens))));
}
modules.insert(
"std".to_owned(),
Rc::new(RefCell::new(compile_std(&mut self.module_map)?)),
);
// modules.insert(
// "std".to_owned(),
// Rc::new(RefCell::new(compile_std(&mut self.module_map)?)),
// );
let mut modules_to_process: Vec<Rc<RefCell<(Module, Vec<FullToken>)>>> =
modules.values().cloned().collect();

View File

@ -4,7 +4,9 @@
extern fn malloc(size: u64) -> *u8;
fn main() -> u8 {
let ptr = malloc(4);
let mut ptr = malloc(4);
ptr[0] = 5;
return ptr[0];
}