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 23:53:39 +02:00
|
|
|
use crate::ast::{FunctionSignature, 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)]
|
|
|
|
#[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
|
|
|
|
2023-08-03 19:08:20 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum IRValueType {
|
|
|
|
I32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IRValueType {
|
|
|
|
unsafe fn get_llvm_type(&self, module: &mut IRModule) -> LLVMTypeRef {
|
|
|
|
match *self {
|
|
|
|
Self::I32 => LLVMInt32TypeInContext(module.context),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:53:39 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2023-08-02 17:19:30 +02:00
|
|
|
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 23:53:39 +02:00
|
|
|
|
|
|
|
pub fn mul(&mut self, lhs: IRValue, rhs: IRValue) -> Result<IRValue, Error> {
|
|
|
|
unsafe {
|
|
|
|
if lhs.0 == rhs.0 {
|
|
|
|
Ok(IRValue(
|
|
|
|
lhs.0,
|
|
|
|
LLVMBuildMul(self.module.builder, lhs.1, rhs.1, cstr!("tmpadd")),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Err(Error::TypeMismatch(lhs.0, rhs.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn function_call(&mut self, callee: &FunctionSignature) -> Result<IRValue, Error> {
|
|
|
|
unsafe {
|
|
|
|
let function = LLVMGetNamedFunction(
|
|
|
|
self.module.module,
|
|
|
|
into_cstring(callee.name.clone()).as_ptr(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let ret_t = LLVMInt32TypeInContext(self.module.context);
|
|
|
|
let mut argts = [];
|
|
|
|
let mut args = [];
|
|
|
|
|
|
|
|
let fun_t = LLVMFunctionType(ret_t, argts.as_mut_ptr(), argts.len() as u32, 0);
|
|
|
|
|
|
|
|
let call = LLVMBuildCall2(
|
|
|
|
self.module.builder,
|
|
|
|
fun_t,
|
|
|
|
function,
|
|
|
|
args.as_mut_ptr(),
|
|
|
|
args.len() as u32,
|
|
|
|
into_cstring(&callee.name).as_ptr(),
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(IRValue(IRValueType::I32, call))
|
|
|
|
}
|
|
|
|
}
|
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),
|
|
|
|
}
|