schala/src/llvm_wrap.rs

139 lines
4.4 KiB
Rust
Raw Normal View History

#![allow(non_snake_case)]
#![allow(dead_code)]
2016-12-26 23:29:24 -08:00
extern crate llvm_sys;
use self::llvm_sys::prelude::*;
use self::llvm_sys::core;
use std::ptr;
use std::ffi::CString;
pub fn create_context() -> LLVMContextRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMContextCreate() }
2016-12-26 23:29:24 -08:00
}
pub fn module_create_with_name(name: &str) -> LLVMModuleRef {
unsafe {
let n = name.as_ptr() as *const _;
core::LLVMModuleCreateWithName(n)
}
}
pub fn CreateBuilderInContext(context: LLVMContextRef) -> LLVMBuilderRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMCreateBuilderInContext(context) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn AppendBasicBlockInContext(context: LLVMContextRef,
function: LLVMValueRef,
name: &str)
-> LLVMBasicBlockRef {
2016-12-26 23:29:24 -08:00
let c_name = CString::new(name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMAppendBasicBlockInContext(context, function, c_name.as_ptr()) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn AddFunction(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> LLVMValueRef {
2016-12-26 23:29:24 -08:00
let c_name = CString::new(name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMAddFunction(module, c_name.as_ptr(), function_type) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
// NOTE this is incomplete
pub fn FunctionType(return_type: LLVMTypeRef,
param_types: &[LLVMTypeRef],
is_var_rag: bool)
-> LLVMTypeRef {
2016-12-26 23:29:24 -08:00
let len = param_types.len();
unsafe {
2016-12-29 02:02:45 -08:00
core::LLVMFunctionType(return_type,
ptr::null_mut(),
len as u32,
if is_var_rag { 1 } else { 0 })
2016-12-26 23:29:24 -08:00
}
}
pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMVoidTypeInContext(context) }
2016-12-26 23:29:24 -08:00
}
pub fn DisposeBuilder(builder: LLVMBuilderRef) {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMDisposeBuilder(builder) }
2016-12-26 23:29:24 -08:00
}
pub fn DisposeModule(module: LLVMModuleRef) {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMDisposeModule(module) }
2016-12-26 23:29:24 -08:00
}
pub fn ContextDispose(context: LLVMContextRef) {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMContextDispose(context) }
2016-12-26 23:29:24 -08:00
}
pub fn PositionBuilderAtEnd(builder: LLVMBuilderRef, basic_block: LLVMBasicBlockRef) {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMPositionBuilderAtEnd(builder, basic_block) }
2016-12-26 23:29:24 -08:00
}
pub fn BuildRet(builder: LLVMBuilderRef, val: LLVMValueRef) -> LLVMValueRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildRet(builder, val) }
2016-12-26 23:29:24 -08:00
}
pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildRetVoid(builder) }
2016-12-26 23:29:24 -08:00
}
pub fn DumpModule(module: LLVMModuleRef) {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMDumpModule(module) }
2016-12-26 23:29:24 -08:00
}
pub fn Int64TypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMInt64TypeInContext(context) }
2016-12-26 23:29:24 -08:00
}
pub fn ConstInt(int_type: LLVMTypeRef, n: u64, sign_extend: bool) -> LLVMValueRef {
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMConstInt(int_type, n, if sign_extend { 1 } else { 0 }) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn BuildAdd(builder: LLVMBuilderRef,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
reg_name: &str)
-> LLVMValueRef {
2016-12-26 23:29:24 -08:00
let name = CString::new(reg_name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildAdd(builder, lhs, rhs, name.as_ptr()) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn BuildSub(builder: LLVMBuilderRef,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
reg_name: &str)
-> LLVMValueRef {
2016-12-26 23:29:24 -08:00
let name = CString::new(reg_name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildSub(builder, lhs, rhs, name.as_ptr()) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn BuildMul(builder: LLVMBuilderRef,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
reg_name: &str)
-> LLVMValueRef {
2016-12-26 23:29:24 -08:00
let name = CString::new(reg_name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildMul(builder, lhs, rhs, name.as_ptr()) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn BuildUDiv(builder: LLVMBuilderRef,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
reg_name: &str)
-> LLVMValueRef {
2016-12-26 23:29:24 -08:00
let name = CString::new(reg_name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildUDiv(builder, lhs, rhs, name.as_ptr()) }
2016-12-26 23:29:24 -08:00
}
2016-12-29 02:02:45 -08:00
pub fn BuildSRem(builder: LLVMBuilderRef,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
reg_name: &str)
-> LLVMValueRef {
2016-12-26 23:29:24 -08:00
let name = CString::new(reg_name).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMBuildSRem(builder, lhs, rhs, name.as_ptr()) }
2016-12-26 23:29:24 -08:00
}
pub fn PrintModuleToFile(module: LLVMModuleRef, filename: &str) -> LLVMBool {
let out_file = CString::new(filename).unwrap();
2016-12-29 02:02:45 -08:00
unsafe { core::LLVMPrintModuleToFile(module, out_file.as_ptr(), ptr::null_mut()) }
}