forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use libc::c_void; | ||
use llvm_sys::execution_engine::{LLVMCreateGenericValueOfPointer, LLVMDisposeGenericValue, LLVMGenericValueIntWidth, LLVMGenericValueRef, LLVMGenericValueToInt, LLVMGenericValueToFloat, LLVMGenericValueToPointer}; | ||
|
||
use types::{AsTypeRef, FloatType}; | ||
|
||
// SubTypes: GenericValue<IntValue, FloatValue, or PointerValue> | ||
pub struct GenericValue { | ||
generic_value: LLVMGenericValueRef, | ||
} | ||
|
||
impl GenericValue { | ||
pub(crate) fn new(generic_value: LLVMGenericValueRef) -> Self { | ||
assert!(!generic_value.is_null()); | ||
|
||
GenericValue { | ||
generic_value, | ||
} | ||
} | ||
|
||
// SubType: GenericValue<IntValue> only | ||
pub fn int_width(&self) -> u32 { | ||
unsafe { | ||
LLVMGenericValueIntWidth(self.generic_value) | ||
} | ||
} | ||
|
||
// SubType: create_generic_value() -> GenericValue<PointerValue> | ||
// REVIEW: How safe is this really? | ||
pub unsafe fn create_generic_value_of_pointer<T>(value: &mut T) -> Self { | ||
let value = unsafe { | ||
LLVMCreateGenericValueOfPointer(value as *mut _ as *mut c_void) | ||
}; | ||
|
||
GenericValue::new(value) | ||
} | ||
|
||
// SubType: impl only for GenericValue<IntValue> | ||
pub fn into_int(self, is_signed: bool) -> u64 { | ||
unsafe { | ||
LLVMGenericValueToInt(self.generic_value, is_signed as i32) | ||
} | ||
} | ||
|
||
// SubType: impl only for GenericValue<FloatValue> | ||
pub fn into_float(self, float_type: &FloatType) -> f64 { | ||
unsafe { | ||
LLVMGenericValueToFloat(float_type.as_type_ref(), self.generic_value) | ||
} | ||
} | ||
|
||
// SubType: impl only for GenericValue<PointerValue, T> | ||
// REVIEW: How safe is this really? | ||
pub unsafe fn into_pointer<T>(self) -> *mut T { | ||
unsafe { | ||
LLVMGenericValueToPointer(self.generic_value) as *mut T | ||
} | ||
} | ||
} | ||
|
||
impl Drop for GenericValue { | ||
fn drop(&mut self) { | ||
unsafe { | ||
LLVMDisposeGenericValue(self.generic_value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters