wit_parser/
sizealign.rs

1use std::{
2    cmp::Ordering,
3    num::NonZeroUsize,
4    ops::{Add, AddAssign},
5};
6
7use crate::{FlagsRepr, Int, Resolve, Type, TypeDef, TypeDefKind};
8
9/// Architecture specific alignment
10#[derive(Eq, PartialEq, Clone, Copy)]
11pub enum Alignment {
12    /// This represents 4 byte alignment on 32bit and 8 byte alignment on 64bit architectures
13    Pointer,
14    /// This alignment is architecture independent (derived from integer or float types)
15    Bytes(NonZeroUsize),
16}
17
18impl Default for Alignment {
19    fn default() -> Self {
20        Alignment::Bytes(NonZeroUsize::new(1).unwrap())
21    }
22}
23
24impl std::fmt::Debug for Alignment {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Alignment::Pointer => f.write_str("ptr"),
28            Alignment::Bytes(b) => f.write_fmt(format_args!("{}", b.get())),
29        }
30    }
31}
32
33impl PartialOrd for Alignment {
34    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
35        Some(self.cmp(other))
36    }
37}
38
39impl Ord for Alignment {
40    /// Needed for determining the max alignment of an object from its parts.
41    /// The ordering is: Bytes(1) < Bytes(2) < Bytes(4) < Pointer < Bytes(8)
42    /// as a Pointer is either four or eight byte aligned, depending on the architecture
43    fn cmp(&self, other: &Self) -> Ordering {
44        match (self, other) {
45            (Alignment::Pointer, Alignment::Pointer) => std::cmp::Ordering::Equal,
46            (Alignment::Pointer, Alignment::Bytes(b)) => {
47                if b.get() > 4 {
48                    std::cmp::Ordering::Less
49                } else {
50                    std::cmp::Ordering::Greater
51                }
52            }
53            (Alignment::Bytes(b), Alignment::Pointer) => {
54                if b.get() > 4 {
55                    std::cmp::Ordering::Greater
56                } else {
57                    std::cmp::Ordering::Less
58                }
59            }
60            (Alignment::Bytes(a), Alignment::Bytes(b)) => a.cmp(b),
61        }
62    }
63}
64
65impl Alignment {
66    /// for easy migration this gives you the value for wasm32
67    pub fn align_wasm32(&self) -> usize {
68        match self {
69            Alignment::Pointer => 4,
70            Alignment::Bytes(bytes) => bytes.get(),
71        }
72    }
73
74    pub fn align_wasm64(&self) -> usize {
75        match self {
76            Alignment::Pointer => 8,
77            Alignment::Bytes(bytes) => bytes.get(),
78        }
79    }
80
81    pub fn format(&self, ptrsize_expr: &str) -> String {
82        match self {
83            Alignment::Pointer => ptrsize_expr.into(),
84            Alignment::Bytes(bytes) => format!("{}", bytes.get()),
85        }
86    }
87}
88
89/// Architecture specific measurement of position,
90/// the combined amount in bytes is
91/// `bytes + pointers * core::mem::size_of::<*const u8>()`
92#[derive(Default, Clone, Copy, Eq, PartialEq)]
93pub struct ArchitectureSize {
94    /// architecture independent bytes
95    pub bytes: usize,
96    /// amount of pointer sized units to add
97    pub pointers: usize,
98}
99
100impl Add<ArchitectureSize> for ArchitectureSize {
101    type Output = ArchitectureSize;
102
103    fn add(self, rhs: ArchitectureSize) -> Self::Output {
104        ArchitectureSize::new(self.bytes + rhs.bytes, self.pointers + rhs.pointers)
105    }
106}
107
108impl AddAssign<ArchitectureSize> for ArchitectureSize {
109    fn add_assign(&mut self, rhs: ArchitectureSize) {
110        self.bytes += rhs.bytes;
111        self.pointers += rhs.pointers;
112    }
113}
114
115impl From<Alignment> for ArchitectureSize {
116    fn from(align: Alignment) -> Self {
117        match align {
118            Alignment::Bytes(bytes) => ArchitectureSize::new(bytes.get(), 0),
119            Alignment::Pointer => ArchitectureSize::new(0, 1),
120        }
121    }
122}
123
124impl std::fmt::Debug for ArchitectureSize {
125    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126        f.write_str(&self.format("ptrsz"))
127    }
128}
129
130impl ArchitectureSize {
131    pub fn new(bytes: usize, pointers: usize) -> Self {
132        Self { bytes, pointers }
133    }
134
135    pub fn max<B: std::borrow::Borrow<Self>>(&self, other: B) -> Self {
136        let other = other.borrow();
137        let self32 = self.size_wasm32();
138        let self64 = self.size_wasm64();
139        let other32 = other.size_wasm32();
140        let other64 = other.size_wasm64();
141        if self32 >= other32 && self64 >= other64 {
142            *self
143        } else if self32 <= other32 && self64 <= other64 {
144            *other
145        } else {
146            // we can assume a combination of bytes and pointers, so align to at least pointer size
147            let new32 = align_to(self32.max(other32), 4);
148            let new64 = align_to(self64.max(other64), 8);
149            ArchitectureSize::new(new32 + new32 - new64, (new64 - new32) / 4)
150        }
151    }
152
153    pub fn add_bytes(&self, b: usize) -> Self {
154        Self::new(self.bytes + b, self.pointers)
155    }
156
157    /// The effective offset/size is
158    /// `constant_bytes() + core::mem::size_of::<*const u8>() * pointers_to_add()`
159    pub fn constant_bytes(&self) -> usize {
160        self.bytes
161    }
162
163    pub fn pointers_to_add(&self) -> usize {
164        self.pointers
165    }
166
167    /// Shortcut for compatibility with previous versions
168    pub fn size_wasm32(&self) -> usize {
169        self.bytes + self.pointers * 4
170    }
171
172    pub fn size_wasm64(&self) -> usize {
173        self.bytes + self.pointers * 8
174    }
175
176    /// prefer this over >0
177    pub fn is_empty(&self) -> bool {
178        self.bytes == 0 && self.pointers == 0
179    }
180
181    // create a suitable expression in bytes from a pointer size argument
182    pub fn format(&self, ptrsize_expr: &str) -> String {
183        self.format_term(ptrsize_expr, false)
184    }
185
186    // create a suitable expression in bytes from a pointer size argument,
187    // extended API with optional brackets around the sum
188    pub fn format_term(&self, ptrsize_expr: &str, suppress_brackets: bool) -> String {
189        if self.pointers != 0 {
190            if self.bytes > 0 {
191                // both
192                if suppress_brackets {
193                    format!(
194                        "{}+{}*{ptrsize_expr}",
195                        self.constant_bytes(),
196                        self.pointers_to_add()
197                    )
198                } else {
199                    format!(
200                        "({}+{}*{ptrsize_expr})",
201                        self.constant_bytes(),
202                        self.pointers_to_add()
203                    )
204                }
205            } else if self.pointers == 1 {
206                // one pointer
207                ptrsize_expr.into()
208            } else {
209                // only pointer
210                if suppress_brackets {
211                    format!("{}*{ptrsize_expr}", self.pointers_to_add())
212                } else {
213                    format!("({}*{ptrsize_expr})", self.pointers_to_add())
214                }
215            }
216        } else {
217            // only bytes
218            format!("{}", self.constant_bytes())
219        }
220    }
221}
222
223/// Information per structure element
224#[derive(Default)]
225pub struct ElementInfo {
226    pub size: ArchitectureSize,
227    pub align: Alignment,
228}
229
230impl From<Alignment> for ElementInfo {
231    fn from(align: Alignment) -> Self {
232        ElementInfo {
233            size: align.into(),
234            align,
235        }
236    }
237}
238
239impl ElementInfo {
240    fn new(size: ArchitectureSize, align: Alignment) -> Self {
241        Self { size, align }
242    }
243}
244
245/// Collect size and alignment for sub-elements of a structure
246#[derive(Default)]
247pub struct SizeAlign {
248    map: Vec<ElementInfo>,
249}
250
251impl SizeAlign {
252    pub fn fill(&mut self, resolve: &Resolve) {
253        self.map = Vec::new();
254        for (_, ty) in resolve.types.iter() {
255            let pair = self.calculate(ty);
256            self.map.push(pair);
257        }
258    }
259
260    fn calculate(&self, ty: &TypeDef) -> ElementInfo {
261        match &ty.kind {
262            TypeDefKind::Type(t) => ElementInfo::new(self.size(t), self.align(t)),
263            TypeDefKind::List(_) => {
264                ElementInfo::new(ArchitectureSize::new(0, 2), Alignment::Pointer)
265            }
266            TypeDefKind::Record(r) => self.record(r.fields.iter().map(|f| &f.ty)),
267            TypeDefKind::Tuple(t) => self.record(t.types.iter()),
268            TypeDefKind::Flags(f) => match f.repr() {
269                FlagsRepr::U8 => int_size_align(Int::U8),
270                FlagsRepr::U16 => int_size_align(Int::U16),
271                FlagsRepr::U32(n) => ElementInfo::new(
272                    ArchitectureSize::new(n * 4, 0),
273                    Alignment::Bytes(NonZeroUsize::new(4).unwrap()),
274                ),
275            },
276            TypeDefKind::Variant(v) => self.variant(v.tag(), v.cases.iter().map(|c| c.ty.as_ref())),
277            TypeDefKind::Enum(e) => self.variant(e.tag(), []),
278            TypeDefKind::Option(t) => self.variant(Int::U8, [Some(t)]),
279            TypeDefKind::Result(r) => self.variant(Int::U8, [r.ok.as_ref(), r.err.as_ref()]),
280            // A resource is represented as an index.
281            // A future is represented as an index.
282            // A stream is represented as an index.
283            // An error is represented as an index.
284            TypeDefKind::Handle(_) | TypeDefKind::Future(_) | TypeDefKind::Stream(_) => {
285                int_size_align(Int::U32)
286            }
287            // This shouldn't be used for anything since raw resources aren't part of the ABI -- just handles to
288            // them.
289            TypeDefKind::Resource => ElementInfo::new(
290                ArchitectureSize::new(usize::MAX, 0),
291                Alignment::Bytes(NonZeroUsize::new(usize::MAX).unwrap()),
292            ),
293            TypeDefKind::Unknown => unreachable!(),
294        }
295    }
296
297    pub fn size(&self, ty: &Type) -> ArchitectureSize {
298        match ty {
299            Type::Bool | Type::U8 | Type::S8 => ArchitectureSize::new(1, 0),
300            Type::U16 | Type::S16 => ArchitectureSize::new(2, 0),
301            Type::U32 | Type::S32 | Type::F32 | Type::Char | Type::ErrorContext => {
302                ArchitectureSize::new(4, 0)
303            }
304            Type::U64 | Type::S64 | Type::F64 => ArchitectureSize::new(8, 0),
305            Type::String => ArchitectureSize::new(0, 2),
306            Type::Id(id) => self.map[id.index()].size,
307        }
308    }
309
310    pub fn align(&self, ty: &Type) -> Alignment {
311        match ty {
312            Type::Bool | Type::U8 | Type::S8 => Alignment::Bytes(NonZeroUsize::new(1).unwrap()),
313            Type::U16 | Type::S16 => Alignment::Bytes(NonZeroUsize::new(2).unwrap()),
314            Type::U32 | Type::S32 | Type::F32 | Type::Char | Type::ErrorContext => {
315                Alignment::Bytes(NonZeroUsize::new(4).unwrap())
316            }
317            Type::U64 | Type::S64 | Type::F64 => Alignment::Bytes(NonZeroUsize::new(8).unwrap()),
318            Type::String => Alignment::Pointer,
319            Type::Id(id) => self.map[id.index()].align,
320        }
321    }
322
323    pub fn field_offsets<'a>(
324        &self,
325        types: impl IntoIterator<Item = &'a Type>,
326    ) -> Vec<(ArchitectureSize, &'a Type)> {
327        let mut cur = ArchitectureSize::default();
328        types
329            .into_iter()
330            .map(|ty| {
331                let ret = align_to_arch(cur, self.align(ty));
332                cur = ret + self.size(ty);
333                (ret, ty)
334            })
335            .collect()
336    }
337
338    pub fn payload_offset<'a>(
339        &self,
340        tag: Int,
341        cases: impl IntoIterator<Item = Option<&'a Type>>,
342    ) -> ArchitectureSize {
343        let mut max_align = Alignment::default();
344        for ty in cases {
345            if let Some(ty) = ty {
346                max_align = max_align.max(self.align(ty));
347            }
348        }
349        let tag_size = int_size_align(tag).size;
350        align_to_arch(tag_size, max_align)
351    }
352
353    pub fn record<'a>(&self, types: impl IntoIterator<Item = &'a Type>) -> ElementInfo {
354        let mut size = ArchitectureSize::default();
355        let mut align = Alignment::default();
356        for ty in types {
357            let field_size = self.size(ty);
358            let field_align = self.align(ty);
359            size = align_to_arch(size, field_align) + field_size;
360            align = align.max(field_align);
361        }
362        ElementInfo::new(align_to_arch(size, align), align)
363    }
364
365    pub fn params<'a>(&self, types: impl IntoIterator<Item = &'a Type>) -> ElementInfo {
366        self.record(types.into_iter())
367    }
368
369    fn variant<'a>(
370        &self,
371        tag: Int,
372        types: impl IntoIterator<Item = Option<&'a Type>>,
373    ) -> ElementInfo {
374        let ElementInfo {
375            size: discrim_size,
376            align: discrim_align,
377        } = int_size_align(tag);
378        let mut case_size = ArchitectureSize::default();
379        let mut case_align = Alignment::default();
380        for ty in types {
381            if let Some(ty) = ty {
382                case_size = case_size.max(&self.size(ty));
383                case_align = case_align.max(self.align(ty));
384            }
385        }
386        let align = discrim_align.max(case_align);
387        let discrim_aligned = align_to_arch(discrim_size, case_align);
388        let size_sum = discrim_aligned + case_size;
389        ElementInfo::new(align_to_arch(size_sum, align), align)
390    }
391}
392
393fn int_size_align(i: Int) -> ElementInfo {
394    match i {
395        Int::U8 => Alignment::Bytes(NonZeroUsize::new(1).unwrap()),
396        Int::U16 => Alignment::Bytes(NonZeroUsize::new(2).unwrap()),
397        Int::U32 => Alignment::Bytes(NonZeroUsize::new(4).unwrap()),
398        Int::U64 => Alignment::Bytes(NonZeroUsize::new(8).unwrap()),
399    }
400    .into()
401}
402
403/// Increase `val` to a multiple of `align`;
404/// `align` must be a power of two
405pub(crate) fn align_to(val: usize, align: usize) -> usize {
406    (val + align - 1) & !(align - 1)
407}
408
409/// Increase `val` to a multiple of `align`, with special handling for pointers;
410/// `align` must be a power of two or `Alignment::Pointer`
411pub fn align_to_arch(val: ArchitectureSize, align: Alignment) -> ArchitectureSize {
412    match align {
413        Alignment::Pointer => {
414            let new32 = align_to(val.bytes, 4);
415            if new32 != align_to(new32, 8) {
416                ArchitectureSize::new(new32 - 4, val.pointers + 1)
417            } else {
418                ArchitectureSize::new(new32, val.pointers)
419            }
420        }
421        Alignment::Bytes(align_bytes) => {
422            let align_bytes = align_bytes.get();
423            if align_bytes > 4 && (val.pointers & 1) != 0 {
424                let new_bytes = align_to(val.bytes, align_bytes);
425                if (new_bytes - val.bytes) >= 4 {
426                    // up to four extra bytes fit together with a the extra 32 bit pointer
427                    // and the 64 bit pointer is always 8 bytes (so no change in value)
428                    ArchitectureSize::new(new_bytes - 8, val.pointers + 1)
429                } else {
430                    // there is no room to combine, so the odd pointer aligns to 8 bytes
431                    ArchitectureSize::new(new_bytes + 8, val.pointers - 1)
432                }
433            } else {
434                ArchitectureSize::new(align_to(val.bytes, align_bytes), val.pointers)
435            }
436        }
437    }
438}
439
440#[cfg(test)]
441mod test {
442    use super::*;
443
444    #[test]
445    fn align() {
446        // u8 + ptr
447        assert_eq!(
448            align_to_arch(ArchitectureSize::new(1, 0), Alignment::Pointer),
449            ArchitectureSize::new(0, 1)
450        );
451        // u8 + u64
452        assert_eq!(
453            align_to_arch(
454                ArchitectureSize::new(1, 0),
455                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
456            ),
457            ArchitectureSize::new(8, 0)
458        );
459        // u8 + u32
460        assert_eq!(
461            align_to_arch(
462                ArchitectureSize::new(1, 0),
463                Alignment::Bytes(NonZeroUsize::new(4).unwrap())
464            ),
465            ArchitectureSize::new(4, 0)
466        );
467        // ptr + u64
468        assert_eq!(
469            align_to_arch(
470                ArchitectureSize::new(0, 1),
471                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
472            ),
473            ArchitectureSize::new(8, 0)
474        );
475        // u32 + ptr
476        assert_eq!(
477            align_to_arch(ArchitectureSize::new(4, 0), Alignment::Pointer),
478            ArchitectureSize::new(0, 1)
479        );
480        // u32, ptr + u64
481        assert_eq!(
482            align_to_arch(
483                ArchitectureSize::new(0, 2),
484                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
485            ),
486            ArchitectureSize::new(0, 2)
487        );
488        // ptr, u8 + u64
489        assert_eq!(
490            align_to_arch(
491                ArchitectureSize::new(1, 1),
492                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
493            ),
494            ArchitectureSize::new(0, 2)
495        );
496        // ptr, u8 + ptr
497        assert_eq!(
498            align_to_arch(ArchitectureSize::new(1, 1), Alignment::Pointer),
499            ArchitectureSize::new(0, 2)
500        );
501        // ptr, ptr, u8 + u64
502        assert_eq!(
503            align_to_arch(
504                ArchitectureSize::new(1, 2),
505                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
506            ),
507            ArchitectureSize::new(8, 2)
508        );
509        assert_eq!(
510            align_to_arch(
511                ArchitectureSize::new(30, 3),
512                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
513            ),
514            ArchitectureSize::new(40, 2)
515        );
516
517        assert_eq!(
518            ArchitectureSize::new(12, 0).max(&ArchitectureSize::new(0, 2)),
519            ArchitectureSize::new(8, 1)
520        );
521        assert_eq!(
522            ArchitectureSize::new(10, 0).max(&ArchitectureSize::new(0, 2)),
523            ArchitectureSize::new(8, 1)
524        );
525
526        assert_eq!(
527            align_to_arch(
528                ArchitectureSize::new(2, 0),
529                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
530            ),
531            ArchitectureSize::new(8, 0)
532        );
533        assert_eq!(
534            align_to_arch(ArchitectureSize::new(2, 0), Alignment::Pointer),
535            ArchitectureSize::new(0, 1)
536        );
537    }
538
539    #[test]
540    fn resource_size() {
541        // keep it identical to the old behavior
542        let obj = SizeAlign::default();
543        let elem = obj.calculate(&TypeDef {
544            name: None,
545            kind: TypeDefKind::Resource,
546            owner: crate::TypeOwner::None,
547            docs: Default::default(),
548            stability: Default::default(),
549        });
550        assert_eq!(elem.size, ArchitectureSize::new(usize::MAX, 0));
551        assert_eq!(
552            elem.align,
553            Alignment::Bytes(NonZeroUsize::new(usize::MAX).unwrap())
554        );
555    }
556    #[test]
557    fn result_ptr_10() {
558        let mut obj = SizeAlign::default();
559        let mut resolve = Resolve::default();
560        let tuple = crate::Tuple {
561            types: vec![Type::U16, Type::U16, Type::U16, Type::U16, Type::U16],
562        };
563        let id = resolve.types.alloc(TypeDef {
564            name: None,
565            kind: TypeDefKind::Tuple(tuple),
566            owner: crate::TypeOwner::None,
567            docs: Default::default(),
568            stability: Default::default(),
569        });
570        obj.fill(&resolve);
571        let my_result = crate::Result_ {
572            ok: Some(Type::String),
573            err: Some(Type::Id(id)),
574        };
575        let elem = obj.calculate(&TypeDef {
576            name: None,
577            kind: TypeDefKind::Result(my_result),
578            owner: crate::TypeOwner::None,
579            docs: Default::default(),
580            stability: Default::default(),
581        });
582        assert_eq!(elem.size, ArchitectureSize::new(8, 2));
583        assert_eq!(elem.align, Alignment::Pointer);
584    }
585    #[test]
586    fn result_ptr_64bit() {
587        let obj = SizeAlign::default();
588        let my_record = crate::Record {
589            fields: vec![
590                crate::Field {
591                    name: String::new(),
592                    ty: Type::String,
593                    docs: Default::default(),
594                },
595                crate::Field {
596                    name: String::new(),
597                    ty: Type::U64,
598                    docs: Default::default(),
599                },
600            ],
601        };
602        let elem = obj.calculate(&TypeDef {
603            name: None,
604            kind: TypeDefKind::Record(my_record),
605            owner: crate::TypeOwner::None,
606            docs: Default::default(),
607            stability: Default::default(),
608        });
609        assert_eq!(elem.size, ArchitectureSize::new(8, 2));
610        assert_eq!(elem.align, Alignment::Bytes(NonZeroUsize::new(8).unwrap()));
611    }
612}