forked from M-Labs/nac3
1
0
Fork 0

core/model: add var_alloc & array_var_alloc

This commit is contained in:
lyken 2024-07-17 15:02:15 +08:00
parent 2525369760
commit db9e9586b5
1 changed files with 28 additions and 2 deletions

View File

@ -1,10 +1,10 @@
use inkwell::{ use inkwell::{
context::Context, context::Context,
types::{AnyTypeEnum, BasicTypeEnum}, types::{AnyTypeEnum, BasicTypeEnum},
values::{AnyValue, AnyValueEnum, BasicValueEnum}, values::{AnyValue, AnyValueEnum, BasicValueEnum, PointerValue},
}; };
use crate::codegen::CodeGenContext; use crate::codegen::{CodeGenContext, CodeGenerator};
use super::{slice::ArraySlice, Int, Pointer}; use super::{slice::ArraySlice, Int, Pointer};
@ -89,4 +89,30 @@ pub trait Model<'ctx>: Clone + Copy + CanCheckLLVMType<'ctx> + Sized {
}, },
} }
} }
/// Do [`CodeGenerator::gen_var_alloc`] with the LLVM type of this [`Model<'ctx>`].
fn var_alloc<G: CodeGenerator + ?Sized>(
&self,
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
name: Option<&str>,
) -> Result<Pointer<'ctx, Self>, String> {
let value = generator.gen_var_alloc(ctx, self.get_llvm_type(ctx.ctx), name)?;
Ok(Pointer { element: *self, value })
}
/// Do [`CodeGenerator::gen_array_var_alloc`] with the LLVM type of this [`Model<'ctx>`].
fn array_var_alloc<G: CodeGenerator + ?Sized>(
&self,
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
size: Int<'ctx>,
name: Option<&'ctx str>,
) -> Result<Pointer<'ctx, Self>, String> {
let slice =
generator.gen_array_var_alloc(ctx, self.get_llvm_type(ctx.ctx), size.0, name)?;
let ptr = PointerValue::from(slice); // TODO: Remove ArraySliceValue
Ok(Pointer { element: *self, value: ptr })
}
} }