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
use llvm_sys::{
analysis::{LLVMVerifierFailureAction, LLVMVerifyFunction},
core::{
LLVMCountBasicBlocks, LLVMCountParams, LLVMDumpValue, LLVMGetParam, LLVMGetReturnType,
LLVMGetValueKind, LLVMGetValueName2, LLVMSetValueName2, LLVMTypeOf,
},
prelude::LLVMValueRef,
LLVMTypeKind, LLVMValueKind,
};
use std::ffi::CStr;
use std::marker::PhantomData;
use std::ops::Deref;
use super::Type;
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Value<'llvm>(LLVMValueRef, PhantomData<&'llvm ()>);
impl<'llvm> Value<'llvm> {
pub(super) fn new(value_ref: LLVMValueRef) -> Self {
assert!(!value_ref.is_null());
Value(value_ref, PhantomData)
}
#[inline]
pub(super) fn value_ref(&self) -> LLVMValueRef {
self.0
}
pub(super) fn kind(&self) -> LLVMValueKind {
unsafe { LLVMGetValueKind(self.value_ref()) }
}
pub fn dump(&self) {
unsafe { LLVMDumpValue(self.value_ref()) };
}
pub fn type_of(&self) -> Type<'llvm> {
let type_ref = unsafe { LLVMTypeOf(self.value_ref()) };
Type::new(type_ref)
}
pub fn set_name(&self, name: &str) {
unsafe { LLVMSetValueName2(self.value_ref(), name.as_ptr().cast(), name.len()) };
}
pub fn get_name(&self) -> &'llvm str {
let name = unsafe {
let mut len: libc::size_t = 0;
let name = LLVMGetValueName2(self.0, &mut len as _);
assert!(!name.is_null());
CStr::from_ptr(name)
};
name.to_str()
.expect("Expected valid UTF8 string from LLVM API")
}
pub fn is_f64(&self) -> bool {
self.type_of().kind() == LLVMTypeKind::LLVMDoubleTypeKind
}
pub fn is_int(&self) -> bool {
self.type_of().kind() == LLVMTypeKind::LLVMIntegerTypeKind
}
}
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct FnValue<'llvm>(Value<'llvm>);
impl<'llvm> Deref for FnValue<'llvm> {
type Target = Value<'llvm>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'llvm> FnValue<'llvm> {
pub(super) fn new(value_ref: LLVMValueRef) -> Self {
let value = Value::new(value_ref);
debug_assert_eq!(
value.kind(),
LLVMValueKind::LLVMFunctionValueKind,
"Expected a fn value when constructing FnValue!"
);
FnValue(value)
}
pub fn ret_type(&self) -> Type<'llvm> {
let type_ref = unsafe { LLVMGetReturnType(LLVMTypeOf(self.value_ref())) };
Type::new(type_ref)
}
pub fn args(&self) -> usize {
unsafe { LLVMCountParams(self.value_ref()) as usize }
}
pub fn arg(&self, idx: usize) -> Value<'llvm> {
assert!(idx < self.args());
let value_ref = unsafe { LLVMGetParam(self.value_ref(), idx as libc::c_uint) };
Value::new(value_ref)
}
pub fn basic_blocks(&self) -> usize {
unsafe { LLVMCountBasicBlocks(self.value_ref()) as usize }
}
pub fn verify(&self) -> bool {
unsafe {
LLVMVerifyFunction(
self.value_ref(),
LLVMVerifierFailureAction::LLVMPrintMessageAction,
) == 0
}
}
}