2016-12-27 01:14:52 -08:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
#![allow(dead_code)]
|
2016-12-26 23:29:24 -08:00
|
|
|
extern crate llvm_sys;
|
|
|
|
|
2017-01-14 01:23:14 -08:00
|
|
|
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;
|
|
|
|
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
|
|
|
}
|
2016-12-27 01:14:52 -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()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn AddIncoming(phi: LLVMValueRef, incoming_values: *mut LLVMValueRef, incoming_blocks: *mut LLVMBasicBlockRef,
|
|
|
|
count: u32) {
|
|
|
|
|
|
|
|
unsafe { core::LLVMAddIncoming(phi, incoming_values, incoming_blocks, count) }
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:23:14 -08:00
|
|
|
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()) }
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:23:14 -08:00
|
|
|
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-14 01:23:14 -08:00
|
|
|
}
|
|
|
|
|
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()) }
|
|
|
|
}
|
|
|
|
|
2016-12-27 01:14:52 -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()) }
|
2016-12-27 01:14:52 -08:00
|
|
|
}
|