schala/schala-lib/src/llvm_wrap.rs

280 lines
8.8 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::{LLVMIntPredicate, LLVMRealPredicate};
2016-12-26 23:29:24 -08:00
use self::llvm_sys::prelude::*;
use self::llvm_sys::core;
use std::ptr;
2017-01-15 07:23:53 -08:00
use std::ffi::{CString, CStr};
use std::os::raw::c_char;
2016-12-26 23:29:24 -08:00
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
pub fn FunctionType(return_type: LLVMTypeRef,
2017-01-14 17:19:11 -08:00
mut param_types: Vec<LLVMTypeRef>,
2016-12-29 02:02:45 -08:00
is_var_rag: bool)
-> LLVMTypeRef {
2016-12-26 23:29:24 -08:00
let len = param_types.len();
unsafe {
2017-01-14 17:19:11 -08:00
let pointer = param_types.as_mut_ptr();
2016-12-29 02:02:45 -08:00
core::LLVMFunctionType(return_type,
2017-01-14 17:19:11 -08:00
pointer,
2016-12-29 02:02:45 -08:00
len as u32,
if is_var_rag { 1 } else { 0 })
2016-12-26 23:29:24 -08:00
}
}
2017-01-14 17:19:11 -08:00
pub fn GetNamedFunction(module: LLVMModuleRef,
name: &str) -> Option<LLVMValueRef> {
let c_name = CString::new(name).unwrap();
let ret = unsafe { core::LLVMGetNamedFunction(module, c_name.as_ptr()) };
if ret.is_null() {
None
} else {
Some(ret)
}
}
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
}
2017-01-10 03:33:02 -08:00
pub fn BuildCondBr(builder: LLVMBuilderRef,
if_expr: LLVMValueRef,
then_expr: LLVMBasicBlockRef,
else_expr: LLVMBasicBlockRef) -> LLVMValueRef {
unsafe { core::LLVMBuildCondBr(builder, if_expr, then_expr, else_expr) }
}
2017-01-10 18:07:16 -08:00
pub fn BuildBr(builder: LLVMBuilderRef,
dest: LLVMBasicBlockRef) -> LLVMValueRef {
unsafe { core::LLVMBuildBr(builder, dest) }
}
2017-01-11 20:25:40 -08:00
pub fn GetInsertBlock(builder: LLVMBuilderRef) -> LLVMBasicBlockRef {
unsafe { core::LLVMGetInsertBlock(builder) }
}
pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMValueRef {
let name = CString::new(name).unwrap();
unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) }
}
2017-01-15 07:23:53 -08:00
pub fn SetValueName(value: LLVMValueRef, name: &str) {
let name = CString::new(name).unwrap();
unsafe {
core::LLVMSetValueName(value, name.as_ptr())
}
}
pub fn GetValueName(value: LLVMValueRef) -> String {
unsafe {
let name_ptr: *const c_char = core::LLVMGetValueName(value);
CStr::from_ptr(name_ptr).to_string_lossy().into_owned()
}
}
pub fn GetParams(function: LLVMValueRef) -> Vec<LLVMValueRef> {
let size = CountParams(function);
unsafe {
let mut container = Vec::with_capacity(size);
container.set_len(size);
core::LLVMGetParams(function, container.as_mut_ptr());
container
2017-01-15 07:23:53 -08:00
}
}
pub fn CountParams(function: LLVMValueRef) -> usize {
unsafe { core::LLVMCountParams(function) as usize }
}
pub fn BuildFCmp(builder: LLVMBuilderRef,
op: LLVMRealPredicate,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
name: &str) -> LLVMValueRef {
let name = CString::new(name).unwrap();
unsafe { core::LLVMBuildFCmp(builder, op, lhs, rhs, name.as_ptr()) }
}
2017-01-14 02:52:52 -08:00
pub fn BuildZExt(builder: LLVMBuilderRef,
val: LLVMValueRef,
dest_type: LLVMTypeRef,
name: &str) -> LLVMValueRef {
let name = CString::new(name).unwrap();
unsafe { core::LLVMBuildZExt(builder, val, dest_type, name.as_ptr()) }
}
pub fn BuildUIToFP(builder: LLVMBuilderRef,
val: LLVMValueRef,
dest_type: LLVMTypeRef,
name: &str) -> LLVMValueRef {
let name = CString::new(name).unwrap();
2017-01-14 02:52:52 -08:00
unsafe { core::LLVMBuildUIToFP(builder, val, dest_type, name.as_ptr()) }
}
2017-01-10 03:33:02 -08:00
pub fn BuildICmp(builder: LLVMBuilderRef,
op: LLVMIntPredicate,
lhs: LLVMValueRef,
rhs: LLVMValueRef,
name: &str) -> LLVMValueRef {
let name = CString::new(name).unwrap();
unsafe { core::LLVMBuildICmp(builder, op, lhs, rhs, name.as_ptr()) }
}
2017-01-17 10:41:50 -08:00
pub fn GetBasicBlockParent(block: LLVMBasicBlockRef) -> LLVMValueRef {
unsafe { core::LLVMGetBasicBlockParent(block) }
}
2017-01-15 23:43:24 -08:00
pub fn GetBasicBlocks(function: LLVMValueRef) -> Vec<LLVMBasicBlockRef> {
let size = CountBasicBlocks(function);
unsafe {
let mut container = Vec::with_capacity(size);
container.set_len(size);
core::LLVMGetBasicBlocks(function, container.as_mut_ptr());
container
2017-01-15 23:43:24 -08:00
}
}
pub fn CountBasicBlocks(function: LLVMValueRef) -> usize {
unsafe { core::LLVMCountBasicBlocks(function) as usize }
}
2017-01-16 02:38:58 -08:00
pub fn PrintModuleToString(module: LLVMModuleRef) -> String {
unsafe {
let str_ptr: *const c_char = core::LLVMPrintModuleToString(module);
CStr::from_ptr(str_ptr).to_string_lossy().into_owned()
}
}
2017-01-17 19:48:25 -08:00
pub fn AddIncoming(phi_node: LLVMValueRef, mut incoming_values: Vec<LLVMValueRef>,
mut incoming_blocks: Vec<LLVMBasicBlockRef>) {
2017-01-17 10:41:50 -08:00
2017-01-17 19:48:25 -08:00
let count = incoming_blocks.len() as u32;
if incoming_values.len() as u32 != count {
2017-01-17 10:41:50 -08:00
panic!("Bad invocation of AddIncoming");
}
2017-01-17 19:48:25 -08:00
unsafe {
let vals = incoming_values.as_mut_ptr();
let blocks = incoming_blocks.as_mut_ptr();
core::LLVMAddIncoming(phi_node, vals, blocks, count)
2017-01-17 10:41:50 -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()) }
}