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
|
//! Defines [`Pointer`] which is used to improve the quality of the generated clif ir for pointer
//! operations.
use crate::prelude::*;
use rustc_target::abi::Align;
use cranelift_codegen::ir::immediates::Offset32;
/// A pointer pointing either to a certain address, a certain stack slot or nothing.
#[derive(Copy, Clone, Debug)]
pub(crate) struct Pointer {
base: PointerBase,
offset: Offset32,
}
#[derive(Copy, Clone, Debug)]
pub(crate) enum PointerBase {
Addr(Value),
Stack(StackSlot),
Dangling(Align),
}
impl Pointer {
pub(crate) fn new(addr: Value) -> Self {
Pointer {
base: PointerBase::Addr(addr),
offset: Offset32::new(0),
}
}
pub(crate) fn stack_slot(stack_slot: StackSlot) -> Self {
Pointer {
base: PointerBase::Stack(stack_slot),
offset: Offset32::new(0),
}
}
pub(crate) fn const_addr<'a, 'tcx>(
fx: &mut FunctionCx<'a, 'tcx, impl Module>,
addr: i64,
) -> Self {
let addr = fx.bcx.ins().iconst(fx.pointer_type, addr);
Pointer {
base: PointerBase::Addr(addr),
offset: Offset32::new(0),
}
}
pub(crate) fn dangling(align: Align) -> Self {
Pointer {
base: PointerBase::Dangling(align),
offset: Offset32::new(0),
}
}
#[cfg(debug_assertions)]
pub(crate) fn base_and_offset(self) -> (PointerBase, Offset32) {
(self.base, self.offset)
}
pub(crate) fn get_addr<'a, 'tcx>(self, fx: &mut FunctionCx<'a, 'tcx, impl Module>) -> Value {
match self.base {
PointerBase::Addr(base_addr) => {
let offset: i64 = self.offset.into();
if offset == 0 {
base_addr
} else {
fx.bcx.ins().iadd_imm(base_addr, offset)
}
}
PointerBase::Stack(stack_slot) => {
fx.bcx
.ins()
.stack_addr(fx.pointer_type, stack_slot, self.offset)
}
PointerBase::Dangling(align) => fx
.bcx
.ins()
.iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()),
}
}
pub(crate) fn offset<'a, 'tcx>(
self,
fx: &mut FunctionCx<'a, 'tcx, impl Module>,
extra_offset: Offset32,
) -> Self {
self.offset_i64(fx, extra_offset.into())
}
pub(crate) fn offset_i64<'a, 'tcx>(
self,
fx: &mut FunctionCx<'a, 'tcx, impl Module>,
extra_offset: i64,
) -> Self {
if let Some(new_offset) = self.offset.try_add_i64(extra_offset) {
Pointer {
base: self.base,
offset: new_offset,
}
} else {
let base_offset: i64 = self.offset.into();
if let Some(new_offset) = base_offset.checked_add(extra_offset) {
let base_addr = match self.base {
PointerBase::Addr(addr) => addr,
PointerBase::Stack(stack_slot) => {
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0)
}
PointerBase::Dangling(align) => fx
.bcx
.ins()
.iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()),
};
let addr = fx.bcx.ins().iadd_imm(base_addr, new_offset);
Pointer {
base: PointerBase::Addr(addr),
offset: Offset32::new(0),
}
} else {
panic!(
"self.offset ({}) + extra_offset ({}) not representable in i64",
base_offset, extra_offset
);
}
}
}
pub(crate) fn offset_value<'a, 'tcx>(
self,
fx: &mut FunctionCx<'a, 'tcx, impl Module>,
extra_offset: Value,
) -> Self {
match self.base {
PointerBase::Addr(addr) => Pointer {
base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
offset: self.offset,
},
PointerBase::Stack(stack_slot) => {
let base_addr = fx
.bcx
.ins()
.stack_addr(fx.pointer_type, stack_slot, self.offset);
Pointer {
base: PointerBase::Addr(fx.bcx.ins().iadd(base_addr, extra_offset)),
offset: Offset32::new(0),
}
}
PointerBase::Dangling(align) => {
let addr = fx
.bcx
.ins()
.iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap());
Pointer {
base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
offset: self.offset,
}
}
}
}
pub(crate) fn load<'a, 'tcx>(
self,
fx: &mut FunctionCx<'a, 'tcx, impl Module>,
ty: Type,
flags: MemFlags,
) -> Value {
match self.base {
PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset),
PointerBase::Stack(stack_slot) => {
if ty == types::I128 || ty.is_vector() {
// WORKAROUND for stack_load.i128 and stack_load.iXxY not being implemented
let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0);
fx.bcx.ins().load(ty, flags, base_addr, self.offset)
} else {
fx.bcx.ins().stack_load(ty, stack_slot, self.offset)
}
}
PointerBase::Dangling(_align) => unreachable!(),
}
}
pub(crate) fn store<'a, 'tcx>(
self,
fx: &mut FunctionCx<'a, 'tcx, impl Module>,
value: Value,
flags: MemFlags,
) {
match self.base {
PointerBase::Addr(base_addr) => {
fx.bcx.ins().store(flags, value, base_addr, self.offset);
}
PointerBase::Stack(stack_slot) => {
let val_ty = fx.bcx.func.dfg.value_type(value);
if val_ty == types::I128 || val_ty.is_vector() {
// WORKAROUND for stack_store.i128 and stack_store.iXxY not being implemented
let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0);
fx.bcx.ins().store(flags, value, base_addr, self.offset);
} else {
fx.bcx.ins().stack_store(value, stack_slot, self.offset);
}
}
PointerBase::Dangling(_align) => unreachable!(),
}
}
}
|