1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
mod array_value;
#[deny(missing_docs)]
mod basic_value_use;
#[deny(missing_docs)]
mod call_site_value;
mod enums;
mod float_value;
mod fn_value;
mod generic_value;
mod global_value;
mod instruction_value;
mod int_value;
mod metadata_value;
mod phi_value;
mod ptr_value;
mod struct_value;
mod traits;
mod vec_value;
pub use crate::values::array_value::ArrayValue;
pub use crate::values::basic_value_use::BasicValueUse;
pub use crate::values::call_site_value::CallSiteValue;
pub use crate::values::enums::{AnyValueEnum, AggregateValueEnum, BasicValueEnum, BasicMetadataValueEnum};
pub use crate::values::float_value::FloatValue;
pub use crate::values::fn_value::FunctionValue;
pub use crate::values::generic_value::GenericValue;
pub use crate::values::global_value::GlobalValue;
#[llvm_versions(7.0..=latest)]
pub use crate::values::global_value::UnnamedAddress;
pub use crate::values::instruction_value::{InstructionValue, InstructionOpcode};
pub use crate::values::int_value::IntValue;
pub use crate::values::metadata_value::{MetadataValue, FIRST_CUSTOM_METADATA_KIND_ID};
pub use crate::values::phi_value::PhiValue;
pub use crate::values::ptr_value::PointerValue;
pub use crate::values::struct_value::StructValue;
pub use crate::values::traits::{AnyValue, AggregateValue, BasicValue, IntMathValue, FloatMathValue, PointerMathValue};
pub use crate::values::vec_value::VectorValue;
pub(crate) use crate::values::traits::AsValueRef;
use llvm_sys::core::{LLVMIsConstant, LLVMIsNull, LLVMIsUndef, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDumpValue, LLVMIsAInstruction, LLVMGetMetadata, LLVMHasMetadata, LLVMSetMetadata, LLVMReplaceAllUsesWith, LLVMGetFirstUse};
use llvm_sys::prelude::{LLVMValueRef, LLVMTypeRef};
use std::ffi::CStr;
use std::fmt;
use crate::support::LLVMString;
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
struct Value {
value: LLVMValueRef,
}
impl Value {
pub(crate) fn new(value: LLVMValueRef) -> Value {
debug_assert!(!value.is_null(), "This should never happen since containing struct should check null ptrs");
Value {
value: value
}
}
fn is_instruction(&self) -> bool {
unsafe {
!LLVMIsAInstruction(self.value).is_null()
}
}
fn as_instruction(&self) -> Option<InstructionValue> {
if !self.is_instruction() {
return None;
}
Some(InstructionValue::new(self.value))
}
fn is_null(&self) -> bool {
unsafe {
LLVMIsNull(self.value) == 1
}
}
fn is_const(&self) -> bool {
unsafe {
LLVMIsConstant(self.value) == 1
}
}
fn set_name(&self, name: &str) {
#[cfg(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9",
feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0"))]
{
use llvm_sys::core::LLVMSetValueName;
use std::ffi::CString;
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
unsafe {
LLVMSetValueName(self.value, c_string.as_ptr());
}
}
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9",
feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0")))]
{
use llvm_sys::core::LLVMSetValueName2;
unsafe {
LLVMSetValueName2(self.value, name.as_ptr() as *const i8, name.len())
}
}
}
fn get_name(&self) -> &CStr {
#[cfg(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9",
feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0"))]
let ptr = unsafe {
use llvm_sys::core::LLVMGetValueName;
LLVMGetValueName(self.value)
};
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9",
feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0")))]
let ptr = unsafe {
use llvm_sys::core::LLVMGetValueName2;
let mut len = 0;
LLVMGetValueName2(self.value, &mut len)
};
unsafe {
CStr::from_ptr(ptr)
}
}
fn is_undef(&self) -> bool {
unsafe {
LLVMIsUndef(self.value) == 1
}
}
fn get_type(&self) -> LLVMTypeRef {
unsafe {
LLVMTypeOf(self.value)
}
}
fn print_to_string(&self) -> LLVMString {
let c_string = unsafe {
LLVMPrintValueToString(self.value)
};
LLVMString::new(c_string)
}
fn print_to_stderr(&self) {
unsafe {
LLVMDumpValue(self.value)
}
}
fn has_metadata(&self) -> bool {
unsafe {
LLVMHasMetadata(self.value) == 1
}
}
fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
let metadata_value = unsafe {
LLVMGetMetadata(self.value, kind_id)
};
if metadata_value.is_null() {
return None;
}
Some(MetadataValue::new(metadata_value))
}
fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
unsafe {
LLVMSetMetadata(self.value, kind_id, metadata.as_value_ref())
}
}
fn replace_all_uses_with(&self, other: LLVMValueRef) {
unsafe {
LLVMReplaceAllUsesWith(self.value, other)
}
}
pub fn get_first_use(&self) -> Option<BasicValueUse> {
let use_ = unsafe {
LLVMGetFirstUse(self.value)
};
if use_.is_null() {
return None;
}
Some(BasicValueUse::new(use_))
}
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let llvm_value = self.print_to_string();
let llvm_type = unsafe {
CStr::from_ptr(LLVMPrintTypeToString(LLVMTypeOf(self.value)))
};
let name = self.get_name();
let is_const = self.is_const();
let is_null = self.is_null();
let is_undef = self.is_undef();
f.debug_struct("Value")
.field("name", &name)
.field("address", &self.value)
.field("is_const", &is_const)
.field("is_null", &is_null)
.field("is_undef", &is_undef)
.field("llvm_value", &llvm_value)
.field("llvm_type", &llvm_type)
.finish()
}
}