forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irtype.rs
432 lines (377 loc) · 15.7 KB
/
irtype.rs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Each of the valid `Value` types.
//!
//! These generally mimic the Sway types with a couple of exceptions:
//! - [`Type::Unit`] is still a discrete type rather than an empty tuple. This may change in the
//! future.
//! - [`Type::Union`] is a sum type which resembles a C union. Each member of the union uses the
//! same storage and the size of the union is the size of the largest member.
//!
//! [`Aggregate`] is an abstract collection of [`Type`]s used for structs, unions and arrays,
//! though see below for future improvements around splitting arrays into a different construct.
use crate::{context::Context, pretty::DebugWithContext, Constant, ConstantValue, Value};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, DebugWithContext)]
pub struct Type(pub generational_arena::Index);
#[derive(Debug, Clone, DebugWithContext, Hash, PartialEq, Eq)]
pub enum TypeContent {
Unit,
Bool,
Uint(u8), // XXX u256 is not unreasonable and can't fit in a `u8`.
B256,
String(u64),
Array(Type, u64),
Union(Vec<Type>),
Struct(Vec<Type>),
Slice,
Pointer(Type),
}
impl Type {
fn get_or_create_unique_type(context: &mut Context, t: TypeContent) -> Type {
// Trying to avoiding cloning t unless we're creating a new type.
#[allow(clippy::map_entry)]
if !context.type_map.contains_key(&t) {
let new_type = Type(context.types.insert(t.clone()));
context.type_map.insert(t, new_type);
new_type
} else {
context.type_map.get(&t).copied().unwrap()
}
}
/// Get Type if it already exists.
pub fn get_type(context: &Context, t: &TypeContent) -> Option<Type> {
context.type_map.get(t).copied()
}
pub fn create_basic_types(context: &mut Context) {
Self::get_or_create_unique_type(context, TypeContent::Unit);
Self::get_or_create_unique_type(context, TypeContent::Bool);
Self::get_or_create_unique_type(context, TypeContent::Uint(8));
Self::get_or_create_unique_type(context, TypeContent::Uint(32));
Self::get_or_create_unique_type(context, TypeContent::Uint(64));
Self::get_or_create_unique_type(context, TypeContent::B256);
Self::get_or_create_unique_type(context, TypeContent::Slice);
}
/// Get the content for this Type.
pub fn get_content<'a>(&self, context: &'a Context) -> &'a TypeContent {
&context.types[self.0]
}
/// Get unit type
pub fn get_unit(context: &Context) -> Type {
Self::get_type(context, &TypeContent::Unit).expect("create_basic_types not called")
}
/// Get bool type
pub fn get_bool(context: &Context) -> Type {
Self::get_type(context, &TypeContent::Bool).expect("create_basic_types not called")
}
/// New unsigned integer type
pub fn new_uint(context: &mut Context, width: u8) -> Type {
Self::get_or_create_unique_type(context, TypeContent::Uint(width))
}
/// New u8 type
pub fn get_uint8(context: &Context) -> Type {
Self::get_type(context, &TypeContent::Uint(8)).expect("create_basic_types not called")
}
/// New u32 type
pub fn get_uint32(context: &Context) -> Type {
Self::get_type(context, &TypeContent::Uint(32)).expect("create_basic_types not called")
}
/// New u64 type
pub fn get_uint64(context: &Context) -> Type {
Self::get_type(context, &TypeContent::Uint(64)).expect("create_basic_types not called")
}
/// Get unsigned integer type
pub fn get_uint(context: &Context, width: u8) -> Option<Type> {
Self::get_type(context, &TypeContent::Uint(width))
}
/// Get B256 type
pub fn get_b256(context: &Context) -> Type {
Self::get_type(context, &TypeContent::B256).expect("create_basic_types not called")
}
/// Get string type
pub fn new_string(context: &mut Context, len: u64) -> Type {
Self::get_or_create_unique_type(context, TypeContent::String(len))
}
/// Get array type
pub fn new_array(context: &mut Context, elm_ty: Type, len: u64) -> Type {
Self::get_or_create_unique_type(context, TypeContent::Array(elm_ty, len))
}
/// Get union type
pub fn new_union(context: &mut Context, fields: Vec<Type>) -> Type {
Self::get_or_create_unique_type(context, TypeContent::Union(fields))
}
/// Get struct type
pub fn new_struct(context: &mut Context, fields: Vec<Type>) -> Type {
Self::get_or_create_unique_type(context, TypeContent::Struct(fields))
}
/// New pointer type
pub fn new_ptr(context: &mut Context, to_ty: Type) -> Type {
Self::get_or_create_unique_type(context, TypeContent::Pointer(to_ty))
}
/// Get slice type
pub fn get_slice(context: &mut Context) -> Type {
Self::get_type(context, &TypeContent::Slice).expect("create_basic_types not called")
}
/// Return a string representation of type, used for printing.
pub fn as_string(&self, context: &Context) -> String {
let sep_types_str = |agg_content: &Vec<Type>, sep: &str| {
agg_content
.iter()
.map(|ty| ty.as_string(context))
.collect::<Vec<_>>()
.join(sep)
};
match self.get_content(context) {
TypeContent::Unit => "()".into(),
TypeContent::Bool => "bool".into(),
TypeContent::Uint(nbits) => format!("u{nbits}"),
TypeContent::B256 => "b256".into(),
TypeContent::String(n) => format!("string<{n}>"),
TypeContent::Array(ty, cnt) => {
format!("[{}; {}]", ty.as_string(context), cnt)
}
TypeContent::Union(agg) => {
format!("( {} )", sep_types_str(agg, " | "))
}
TypeContent::Struct(agg) => {
format!("{{ {} }}", sep_types_str(agg, ", "))
}
TypeContent::Slice => "slice".into(),
TypeContent::Pointer(ty) => format!("ptr {}", ty.as_string(context)),
}
}
/// Compare a type to this one for equivalence.
/// `PartialEq` does not take into account the special case for Unions below.
pub fn eq(&self, context: &Context, other: &Type) -> bool {
match (self.get_content(context), other.get_content(context)) {
(TypeContent::Unit, TypeContent::Unit) => true,
(TypeContent::Bool, TypeContent::Bool) => true,
(TypeContent::Uint(l), TypeContent::Uint(r)) => l == r,
(TypeContent::B256, TypeContent::B256) => true,
(TypeContent::String(l), TypeContent::String(r)) => l == r,
(TypeContent::Array(l, llen), TypeContent::Array(r, rlen)) => {
llen == rlen && l.eq(context, r)
}
(TypeContent::Struct(l), TypeContent::Struct(r))
| (TypeContent::Union(l), TypeContent::Union(r)) => {
l.len() == r.len() && l.iter().zip(r.iter()).all(|(l, r)| l.eq(context, r))
}
// Unions are special. We say unions are equivalent to any of their variant types.
(_, TypeContent::Union(_)) => other.eq(context, self),
(TypeContent::Union(l), _) => l.iter().any(|field_ty| other.eq(context, field_ty)),
(TypeContent::Slice, TypeContent::Slice) => true,
(TypeContent::Pointer(l), TypeContent::Pointer(r)) => l.eq(context, r),
_ => false,
}
}
/// Is bool type
pub fn is_bool(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Bool)
}
/// Is unit type
pub fn is_unit(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Unit)
}
/// Is unsigned integer type
pub fn is_uint(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Uint(_))
}
/// Is u8 type
pub fn is_uint8(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Uint(8))
}
/// Is u32 type
pub fn is_uint32(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Uint(32))
}
/// Is u64 type
pub fn is_uint64(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Uint(64))
}
/// Is unsigned integer type of specific width
pub fn is_uint_of(&self, context: &Context, width: u8) -> bool {
matches!(*self.get_content(context), TypeContent::Uint(width_) if width == width_)
}
/// Is B256 type
pub fn is_b256(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::B256)
}
/// Is string type
pub fn is_string(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::String(_))
}
/// Is array type
pub fn is_array(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Array(..))
}
/// Is union type
pub fn is_union(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Union(_))
}
/// Is struct type
pub fn is_struct(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Struct(_))
}
/// Is aggregate type: struct, union or array.
pub fn is_aggregate(&self, context: &Context) -> bool {
self.is_struct(context) || self.is_union(context) || self.is_array(context)
}
/// Returns true if this is a slice type.
pub fn is_slice(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Slice)
}
/// Returns true if this is a pointer type.
pub fn is_ptr(&self, context: &Context) -> bool {
matches!(*self.get_content(context), TypeContent::Pointer(_))
}
/// Get pointed to type iff self is a Pointer.
pub fn get_pointee_type(&self, context: &Context) -> Option<Type> {
if let TypeContent::Pointer(to_ty) = self.get_content(context) {
Some(*to_ty)
} else {
None
}
}
/// Get width of an integer type.
pub fn get_uint_width(&self, context: &Context) -> Option<u8> {
if let TypeContent::Uint(width) = self.get_content(context) {
Some(*width)
} else {
None
}
}
/// What's the type of the struct/array value indexed by indices.
pub fn get_indexed_type(&self, context: &Context, indices: &[u64]) -> Option<Type> {
if indices.is_empty() {
return None;
}
indices.iter().fold(Some(*self), |ty, idx| {
ty.and_then(|ty| {
ty.get_field_type(context, *idx)
.or_else(|| ty.get_array_elem_type(context))
})
})
}
/// What's the offset of the indexed element?
/// It may not always be possible to determine statically.
pub fn get_indexed_offset(&self, context: &Context, indices: &[Value]) -> Option<u64> {
indices
.iter()
.fold(Some((*self, 0)), |ty, idx| {
let Some(Constant {
value: ConstantValue::Uint(idx),
ty: _,
}) = idx.get_constant(context) else { return None; };
ty.and_then(|(ty, accum_offset)| {
if ty.is_struct(context) {
// Sum up all sizes of all previous fields.
let prev_idxs_offset = (0..(*idx)).try_fold(0, |accum, pre_idx| {
ty.get_field_type(context, pre_idx)
.map(|field_ty| field_ty.size_in_bytes(context) + accum)
})?;
ty.get_field_type(context, *idx)
.map(|field_ty| (field_ty, accum_offset + prev_idxs_offset))
} else if ty.is_union(context) {
ty.get_field_type(context, *idx)
.map(|field_ty| (field_ty, accum_offset))
} else {
assert!(
ty.is_array(context),
"Expected aggregate type when indexing using GEP. Got {}",
ty.as_string(context)
);
// size_of_element * idx will be the offset of idx.
ty.get_array_elem_type(context).map(|elm_ty| {
let prev_idxs_offset = ty
.get_array_elem_type(context)
.unwrap()
.size_in_bytes(context)
* idx;
(elm_ty, accum_offset + prev_idxs_offset)
})
}
})
})
.map(|pair| pair.1)
}
pub fn get_field_type(&self, context: &Context, idx: u64) -> Option<Type> {
if let TypeContent::Struct(fields) | TypeContent::Union(fields) = self.get_content(context)
{
fields.get(idx as usize).cloned()
} else {
// Trying to index a non-aggregate.
None
}
}
/// Get the type of the array element, if applicable.
pub fn get_array_elem_type(&self, context: &Context) -> Option<Type> {
if let TypeContent::Array(ty, _) = *self.get_content(context) {
Some(ty)
} else {
None
}
}
/// Get the length of the array , if applicable.
pub fn get_array_len(&self, context: &Context) -> Option<u64> {
if let TypeContent::Array(_, n) = *self.get_content(context) {
Some(n)
} else {
None
}
}
/// Get the length of a string
pub fn get_string_len(&self, context: &Context) -> Option<u64> {
if let TypeContent::String(n) = *self.get_content(context) {
Some(n)
} else {
None
}
}
/// Get the type of each field of a struct Type. Empty vector otherwise.
pub fn get_field_types(&self, context: &Context) -> Vec<Type> {
match self.get_content(context) {
TypeContent::Struct(fields) | TypeContent::Union(fields) => fields.clone(),
_ => vec![],
}
}
pub fn size_in_bytes(&self, context: &Context) -> u64 {
match self.get_content(context) {
TypeContent::Unit
| TypeContent::Bool
| TypeContent::Uint(_)
| TypeContent::Pointer(_) => 8,
TypeContent::Slice => 16,
TypeContent::B256 => 32,
TypeContent::String(n) => super::size_bytes_round_up_to_word_alignment!(*n),
TypeContent::Array(el_ty, cnt) => cnt * el_ty.size_in_bytes(context),
TypeContent::Struct(field_tys) => {
// Sum up all the field sizes.
field_tys
.iter()
.map(|field_ty| field_ty.size_in_bytes(context))
.sum()
}
TypeContent::Union(field_tys) => {
// Find the max size for field sizes.
field_tys
.iter()
.map(|field_ty| field_ty.size_in_bytes(context))
.max()
.unwrap_or(0)
}
}
}
}
// This is a mouthful...
#[macro_export]
macro_rules! size_bytes_round_up_to_word_alignment {
($bytes_expr: expr) => {
($bytes_expr + 7) - (($bytes_expr + 7) % 8)
};
}
/// A helper to check if an Option<Type> value is of a particular Type.
pub trait TypeOption {
fn is(&self, pred: fn(&Type, &Context) -> bool, context: &Context) -> bool;
}
impl TypeOption for Option<Type> {
fn is(&self, pred: fn(&Type, &Context) -> bool, context: &Context) -> bool {
self.filter(|ty| pred(ty, context)).is_some()
}
}