Skip to content

Commit

Permalink
Added more int, float, and pointer const methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Aug 30, 2017
1 parent f6f367f commit 227391e
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ impl Builder {

// REVIEW: How does LLVM treat out of bound index? Maybe we should return an Option?
// or is that only in bounds GEP
// REVIEW: Should this be AggregatePointerValue?
pub fn build_extract_value(&self, value: &AggregateValue, index: u32, name: &str) -> BasicValueEnum {
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");

Expand All @@ -707,6 +708,7 @@ impl Builder {
BasicValueEnum::new(value)
}

// REVIEW: Should this be AggregatePointerValue instead of just PointerValue?
pub fn build_insert_value(&self, value: &BasicValue, ptr: &PointerValue, index: u32, name: &str) -> InstructionValue {
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");

Expand Down
16 changes: 14 additions & 2 deletions src/types/float_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use llvm_sys::core::{LLVMConstReal, LLVMConstNull, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type};
use llvm_sys::core::{LLVMConstReal, LLVMConstNull, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type, LLVMConstRealOfString};
use llvm_sys::prelude::LLVMTypeRef;

use std::ffi::CStr;
use std::ffi::{CString, CStr};

use context::ContextRef;
use types::traits::AsTypeRef;
Expand Down Expand Up @@ -42,6 +42,18 @@ impl FloatType {
FloatValue::new(value)
}

// REVIEW: What happens when string is invalid? Nullptr?
// REVIEW: Difference of LLVMConstRealOfStringAndSize?
pub fn const_float_from_string(&self, string: &str) -> FloatValue {
let c_string = CString::new(string).expect("Conversion to CString failed unexpectedly");

let value = unsafe {
LLVMConstRealOfString(self.as_type_ref(), c_string.as_ptr())
};

FloatValue::new(value)
}

pub fn const_null_ptr(&self) -> PointerValue {
self.float_type.const_null_ptr()
}
Expand Down
16 changes: 14 additions & 2 deletions src/types/int_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstNull, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth};
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstNull, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth, LLVMConstIntOfString};
use llvm_sys::prelude::LLVMTypeRef;

use std::ffi::CStr;
use std::ffi::{CString, CStr};

use context::ContextRef;
use types::traits::AsTypeRef;
Expand Down Expand Up @@ -85,6 +85,18 @@ impl IntType {
IntValue::new(value)
}

// REVIEW: What happens when string is invalid? Nullptr?
// REVIEW: Difference of LLVMConstIntOfStringAndSize?
pub fn const_int_from_string(&self, string: &str, radix: u8) -> IntValue {
let c_string = CString::new(string).expect("Conversion to CString failed unexpectedly");

let value = unsafe {
LLVMConstIntOfString(self.as_type_ref(), c_string.as_ptr(), radix)
};

IntValue::new(value)
}

pub fn const_all_ones(&self) -> IntValue {
let value = unsafe {
LLVMConstAllOnes(self.as_type_ref())
Expand Down
39 changes: 35 additions & 4 deletions src/values/float_value.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use llvm_sys::core::{LLVMConstFNeg, LLVMConstFAdd, LLVMConstFSub, LLVMConstFMul, LLVMConstFDiv, LLVMConstFRem, LLVMConstFPCast};
use llvm_sys::core::{LLVMConstFNeg, LLVMConstFAdd, LLVMConstFSub, LLVMConstFMul, LLVMConstFDiv, LLVMConstFRem, LLVMConstFPCast, LLVMConstFPToUI, LLVMConstFPToSI, LLVMConstFPTrunc, LLVMConstFPExt};
use llvm_sys::prelude::LLVMValueRef;

use std::ffi::CStr;

use types::FloatType;
use types::AsTypeRef;
use types::{AsTypeRef, FloatType, IntType};
use values::traits::AsValueRef;
use values::{InstructionValue, Value, MetadataValue};
use values::{InstructionValue, IntValue, Value, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
pub struct FloatValue {
Expand Down Expand Up @@ -114,6 +113,38 @@ impl FloatValue {
FloatValue::new(value)
}

pub fn const_to_unsigned_int(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstFPToUI(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

pub fn const_to_signed_int(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstFPToSI(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

pub fn const_truncate(&self, float_type: &FloatType) -> FloatValue {
let value = unsafe {
LLVMConstFPTrunc(self.as_value_ref(), float_type.as_type_ref())
};

FloatValue::new(value)
}

pub fn const_extend(&self, float_type: &FloatType) -> FloatValue {
let value = unsafe {
LLVMConstFPExt(self.as_value_ref(), float_type.as_type_ref())
};

FloatValue::new(value)
}

pub fn has_metadata(&self) -> bool {
self.float_value.has_metadata()
}
Expand Down
62 changes: 61 additions & 1 deletion src/values/int_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use llvm_sys::core::{LLVMConstNot, LLVMConstNeg, LLVMConstNSWNeg, LLVMConstNUWNeg, LLVMConstAdd, LLVMConstNSWAdd, LLVMConstNUWAdd, LLVMConstSub, LLVMConstNSWSub, LLVMConstNUWSub, LLVMConstMul, LLVMConstNSWMul, LLVMConstNUWMul, LLVMConstUDiv, LLVMConstSDiv, LLVMConstSRem, LLVMConstURem, LLVMConstIntCast, LLVMConstXor, LLVMConstOr, LLVMConstAnd, LLVMConstExactSDiv, LLVMConstShl, LLVMConstLShr, LLVMConstAShr, LLVMConstUIToFP, LLVMConstSIToFP, LLVMConstIntToPtr};
use llvm_sys::core::{LLVMConstNot, LLVMConstNeg, LLVMConstNSWNeg, LLVMConstNUWNeg, LLVMConstAdd, LLVMConstNSWAdd, LLVMConstNUWAdd, LLVMConstSub, LLVMConstNSWSub, LLVMConstNUWSub, LLVMConstMul, LLVMConstNSWMul, LLVMConstNUWMul, LLVMConstUDiv, LLVMConstSDiv, LLVMConstSRem, LLVMConstURem, LLVMConstIntCast, LLVMConstXor, LLVMConstOr, LLVMConstAnd, LLVMConstExactSDiv, LLVMConstShl, LLVMConstLShr, LLVMConstAShr, LLVMConstUIToFP, LLVMConstSIToFP, LLVMConstIntToPtr, LLVMConstTrunc, LLVMConstSExt, LLVMConstZExt, LLVMConstTruncOrBitCast, LLVMConstSExtOrBitCast, LLVMConstZExtOrBitCast, LLVMConstBitCast};
use llvm_sys::prelude::LLVMValueRef;

use std::ffi::CStr;
Expand Down Expand Up @@ -284,6 +284,66 @@ impl IntValue {
PointerValue::new(value)
}

pub fn const_truncate(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstTrunc(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

// TODO: More descriptive name
pub fn const_s_extend(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstSExt(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

// TODO: More descriptive name
pub fn const_z_ext(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstZExt(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

pub fn const_truncate_or_bit_cast(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstTruncOrBitCast(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

// TODO: More descriptive name
pub fn const_s_extend_or_bit_cast(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstSExtOrBitCast(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

// TODO: More descriptive name
pub fn const_z_ext_or_bit_cast(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstZExtOrBitCast(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

pub fn const_bit_cast(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstBitCast(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

pub fn has_metadata(&self) -> bool {
self.int_value.has_metadata()
}
Expand Down
55 changes: 51 additions & 4 deletions src/values/ptr_value.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use llvm_sys::core::{LLVMConstGEP, LLVMConstInBoundsGEP, LLVMConstPtrToInt, LLVMConstPointerCast, LLVMConstAddrSpaceCast};
use llvm_sys::prelude::LLVMValueRef;

use std::ffi::CStr;

use types::PointerType;
use values::traits::AsValueRef;
use values::{InstructionValue, Value, MetadataValue};
use types::{AsTypeRef, IntType, PointerType};
use values::{AsValueRef, InstructionValue, IntValue, Value, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
pub struct PointerValue {
ptr_value: Value
ptr_value: Value,
}

impl PointerValue {
Expand Down Expand Up @@ -63,6 +63,53 @@ impl PointerValue {
pub fn set_metadata(&self, metadata: &MetadataValue, kind_id: u32) {
self.ptr_value.set_metadata(metadata, kind_id)
}

// REVIEW: Should this be on array value too?
pub fn const_gep(&self, ordered_indexes: &[&IntValue]) -> PointerValue {
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
.map(|val| val.as_value_ref())
.collect();
let value = unsafe {
LLVMConstGEP(self.as_value_ref(), index_values.as_mut_ptr(), index_values.len() as u32)
};

PointerValue::new(value)
}

pub fn const_in_bounds_gep(&self, ordered_indexes: &[&IntValue]) -> PointerValue {
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
.map(|val| val.as_value_ref())
.collect();
let value = unsafe {
LLVMConstInBoundsGEP(self.as_value_ref(), index_values.as_mut_ptr(), index_values.len() as u32)
};

PointerValue::new(value)
}

pub fn const_to_int(&self, int_type: &IntType) -> IntValue {
let value = unsafe {
LLVMConstPtrToInt(self.as_value_ref(), int_type.as_type_ref())
};

IntValue::new(value)
}

pub fn const_cast(&self, ptr_type: &PointerType) -> PointerValue {
let value = unsafe {
LLVMConstPointerCast(self.as_value_ref(), ptr_type.as_type_ref())
};

PointerValue::new(value)
}

pub fn const_address_space_cast(&self, ptr_type: &PointerType) -> PointerValue {
let value = unsafe {
LLVMConstAddrSpaceCast(self.as_value_ref(), ptr_type.as_type_ref())
};

PointerValue::new(value)
}
}

impl AsValueRef for PointerValue {
Expand Down

0 comments on commit 227391e

Please sign in to comment.