reid-llvm/src/llvm_ir.rs

164 lines
4.4 KiB
Rust
Raw Normal View History

2023-08-02 17:48:56 +02:00
use std::ffi::{CStr, CString};
2023-08-02 17:19:30 +02:00
use std::mem;
use llvm_sys::{core::*, prelude::*, LLVMBuilder, LLVMContext, LLVMModule};
2023-08-02 17:43:47 +02:00
use crate::ast::Literal;
2023-08-02 17:19:30 +02:00
macro_rules! cstr {
($string:expr) => {
core::ffi::CStr::from_bytes_with_nul_unchecked(concat!($string, "\0").as_bytes()).as_ptr()
};
}
#[derive(Clone, Debug, PartialEq, Eq)]
2023-08-02 17:43:47 +02:00
pub enum IRValueType {
2023-08-02 17:19:30 +02:00
I32,
}
2023-08-02 17:43:47 +02:00
impl IRValueType {
2023-08-02 17:19:30 +02:00
unsafe fn get_llvm_type(&self, codegen: &mut IRModule) -> LLVMTypeRef {
match *self {
Self::I32 => LLVMInt32TypeInContext(codegen.context),
}
}
}
#[derive(Clone, Debug)]
#[must_use = "value contains raw pointer and must be inserted somewhere"]
2023-08-02 17:43:47 +02:00
pub struct IRValue(IRValueType, LLVMValueRef);
2023-08-02 17:19:30 +02:00
fn into_cstring<T: Into<String>>(value: T) -> CString {
let string = value.into();
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
}
pub struct IRModule {
context: *mut LLVMContext,
module: *mut LLVMModule,
builder: *mut LLVMBuilder,
}
impl IRModule {
2023-08-02 17:43:47 +02:00
pub fn new<T: Into<String>>(name: T) -> IRModule {
2023-08-02 17:19:30 +02:00
unsafe {
// Set up a context, module and builder in that context.
let context = LLVMContextCreate();
2023-08-02 17:43:47 +02:00
let module = LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), context);
2023-08-02 17:19:30 +02:00
let builder = LLVMCreateBuilderInContext(context);
IRModule {
context,
module,
builder,
}
}
}
pub fn create_block(&mut self) -> IRBlock {
IRBlock::create("entry", self)
}
2023-08-02 17:43:47 +02:00
pub fn create_func<T: Into<String>>(
&mut self,
name: T,
return_type: IRValueType,
) -> IRFunction {
2023-08-02 17:19:30 +02:00
unsafe {
let mut argts = [];
let func_type = LLVMFunctionType(
return_type.get_llvm_type(self),
argts.as_mut_ptr(),
argts.len() as u32,
0,
);
let anon_func = LLVMAddFunction(self.module, into_cstring(name).as_ptr(), func_type);
IRFunction {
2023-08-02 17:43:47 +02:00
value: IRValue(return_type, anon_func),
2023-08-02 17:19:30 +02:00
}
}
}
2023-08-02 17:43:47 +02:00
2023-08-02 17:48:56 +02:00
pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> {
unsafe { CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() }
2023-08-02 17:43:47 +02:00
}
2023-08-02 17:19:30 +02:00
}
impl Drop for IRModule {
fn drop(&mut self) {
// Clean up. Values created in the context mostly get cleaned up there.
unsafe {
LLVMDisposeBuilder(self.builder);
LLVMDisposeModule(self.module);
LLVMContextDispose(self.context);
}
}
}
pub struct IRFunction {
2023-08-02 17:43:47 +02:00
value: IRValue,
2023-08-02 17:19:30 +02:00
}
impl IRFunction {
2023-08-02 17:43:47 +02:00
pub fn add_definition(self, ret: IRValue, block: IRBlock) {
2023-08-02 17:19:30 +02:00
unsafe {
LLVMAppendExistingBasicBlock(self.value.1, block.blockref);
LLVMBuildRet(block.module.builder, ret.1);
}
}
}
pub struct IRBlock<'a> {
module: &'a mut IRModule,
blockref: LLVMBasicBlockRef,
}
impl<'a> IRBlock<'a> {
fn create<T: Into<String>>(name: T, codegen: &'a mut IRModule) -> IRBlock<'a> {
unsafe {
let blockref =
LLVMCreateBasicBlockInContext(codegen.context, into_cstring(name).as_ptr());
LLVMPositionBuilderAtEnd(codegen.builder, blockref);
IRBlock {
module: codegen,
blockref,
}
}
}
2023-08-02 17:43:47 +02:00
pub fn get_const(&mut self, literal_type: &Literal) -> IRValue {
2023-08-02 17:19:30 +02:00
unsafe {
match *literal_type {
2023-08-02 17:43:47 +02:00
Literal::I32(v) => IRValue(
IRValueType::I32,
2023-08-02 17:19:30 +02:00
LLVMConstInt(
LLVMInt32TypeInContext(self.module.context),
mem::transmute(v as i64),
1,
),
),
}
}
}
2023-08-02 19:17:06 +02:00
pub fn add(&mut self, lhs: IRValue, rhs: IRValue) -> Result<IRValue, Error> {
2023-08-02 17:19:30 +02:00
unsafe {
if lhs.0 == rhs.0 {
2023-08-02 17:43:47 +02:00
Ok(IRValue(
2023-08-02 17:19:30 +02:00
lhs.0,
LLVMBuildAdd(self.module.builder, lhs.1, rhs.1, cstr!("tmpadd")),
))
} else {
2023-08-02 19:17:06 +02:00
Err(Error::TypeMismatch(lhs.0, rhs.0))
2023-08-02 17:19:30 +02:00
}
}
}
}
2023-08-02 19:17:06 +02:00
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Type Mismatch: {0:?} {1:?}")]
TypeMismatch(IRValueType, IRValueType),
}