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(_)
285            | TypeDefKind::Future(_)
286            | TypeDefKind::Stream(_)
287            | TypeDefKind::ErrorContext => int_size_align(Int::U32),
288            // This shouldn't be used for anything since raw resources aren't part of the ABI -- just handles to
289            // them.
290            TypeDefKind::Resource => ElementInfo::new(
291                ArchitectureSize::new(usize::MAX, 0),
292                Alignment::Bytes(NonZeroUsize::new(usize::MAX).unwrap()),
293            ),
294            TypeDefKind::Unknown => unreachable!(),
295        }
296    }
297
298    pub fn size(&self, ty: &Type) -> ArchitectureSize {
299        match ty {
300            Type::Bool | Type::U8 | Type::S8 => ArchitectureSize::new(1, 0),
301            Type::U16 | Type::S16 => ArchitectureSize::new(2, 0),
302            Type::U32 | Type::S32 | Type::F32 | Type::Char => ArchitectureSize::new(4, 0),
303            Type::U64 | Type::S64 | Type::F64 => ArchitectureSize::new(8, 0),
304            Type::String => ArchitectureSize::new(0, 2),
305            Type::Id(id) => self.map[id.index()].size,
306        }
307    }
308
309    pub fn align(&self, ty: &Type) -> Alignment {
310        match ty {
311            Type::Bool | Type::U8 | Type::S8 => Alignment::Bytes(NonZeroUsize::new(1).unwrap()),
312            Type::U16 | Type::S16 => Alignment::Bytes(NonZeroUsize::new(2).unwrap()),
313            Type::U32 | Type::S32 | Type::F32 | Type::Char => {
314                Alignment::Bytes(NonZeroUsize::new(4).unwrap())
315            }
316            Type::U64 | Type::S64 | Type::F64 => Alignment::Bytes(NonZeroUsize::new(8).unwrap()),
317            Type::String => Alignment::Pointer,
318            Type::Id(id) => self.map[id.index()].align,
319        }
320    }
321
322    pub fn field_offsets<'a>(
323        &self,
324        types: impl IntoIterator<Item = &'a Type>,
325    ) -> Vec<(ArchitectureSize, &'a Type)> {
326        let mut cur = ArchitectureSize::default();
327        types
328            .into_iter()
329            .map(|ty| {
330                let ret = align_to_arch(cur, self.align(ty));
331                cur = ret + self.size(ty);
332                (ret, ty)
333            })
334            .collect()
335    }
336
337    pub fn payload_offset<'a>(
338        &self,
339        tag: Int,
340        cases: impl IntoIterator<Item = Option<&'a Type>>,
341    ) -> ArchitectureSize {
342        let mut max_align = Alignment::default();
343        for ty in cases {
344            if let Some(ty) = ty {
345                max_align = max_align.max(self.align(ty));
346            }
347        }
348        let tag_size = int_size_align(tag).size;
349        align_to_arch(tag_size, max_align)
350    }
351
352    pub fn record<'a>(&self, types: impl IntoIterator<Item = &'a Type>) -> ElementInfo {
353        let mut size = ArchitectureSize::default();
354        let mut align = Alignment::default();
355        for ty in types {
356            let field_size = self.size(ty);
357            let field_align = self.align(ty);
358            size = align_to_arch(size, field_align) + field_size;
359            align = align.max(field_align);
360        }
361        ElementInfo::new(align_to_arch(size, align), align)
362    }
363
364    pub fn params<'a>(&self, types: impl IntoIterator<Item = &'a Type>) -> ElementInfo {
365        self.record(types.into_iter())
366    }
367
368    fn variant<'a>(
369        &self,
370        tag: Int,
371        types: impl IntoIterator<Item = Option<&'a Type>>,
372    ) -> ElementInfo {
373        let ElementInfo {
374            size: discrim_size,
375            align: discrim_align,
376        } = int_size_align(tag);
377        let mut case_size = ArchitectureSize::default();
378        let mut case_align = Alignment::default();
379        for ty in types {
380            if let Some(ty) = ty {
381                case_size = case_size.max(&self.size(ty));
382                case_align = case_align.max(self.align(ty));
383            }
384        }
385        let align = discrim_align.max(case_align);
386        let discrim_aligned = align_to_arch(discrim_size, case_align);
387        let size_sum = discrim_aligned + case_size;
388        ElementInfo::new(align_to_arch(size_sum, align), align)
389    }
390}
391
392fn int_size_align(i: Int) -> ElementInfo {
393    match i {
394        Int::U8 => Alignment::Bytes(NonZeroUsize::new(1).unwrap()),
395        Int::U16 => Alignment::Bytes(NonZeroUsize::new(2).unwrap()),
396        Int::U32 => Alignment::Bytes(NonZeroUsize::new(4).unwrap()),
397        Int::U64 => Alignment::Bytes(NonZeroUsize::new(8).unwrap()),
398    }
399    .into()
400}
401
402/// Increase `val` to a multiple of `align`;
403/// `align` must be a power of two
404pub(crate) fn align_to(val: usize, align: usize) -> usize {
405    (val + align - 1) & !(align - 1)
406}
407
408/// Increase `val` to a multiple of `align`, with special handling for pointers;
409/// `align` must be a power of two or `Alignment::Pointer`
410pub fn align_to_arch(val: ArchitectureSize, align: Alignment) -> ArchitectureSize {
411    match align {
412        Alignment::Pointer => {
413            let new32 = align_to(val.bytes, 4);
414            if new32 != align_to(new32, 8) {
415                ArchitectureSize::new(new32 - 4, val.pointers + 1)
416            } else {
417                ArchitectureSize::new(new32, val.pointers)
418            }
419        }
420        Alignment::Bytes(align_bytes) => {
421            let align_bytes = align_bytes.get();
422            if align_bytes > 4 && (val.pointers & 1) != 0 {
423                let new_bytes = align_to(val.bytes, align_bytes);
424                if (new_bytes - val.bytes) >= 4 {
425                    // up to four extra bytes fit together with a the extra 32 bit pointer
426                    // and the 64 bit pointer is always 8 bytes (so no change in value)
427                    ArchitectureSize::new(new_bytes - 8, val.pointers + 1)
428                } else {
429                    // there is no room to combine, so the odd pointer aligns to 8 bytes
430                    ArchitectureSize::new(new_bytes + 8, val.pointers - 1)
431                }
432            } else {
433                ArchitectureSize::new(align_to(val.bytes, align_bytes), val.pointers)
434            }
435        }
436    }
437}
438
439#[cfg(test)]
440mod test {
441    use super::*;
442
443    #[test]
444    fn align() {
445        // u8 + ptr
446        assert_eq!(
447            align_to_arch(ArchitectureSize::new(1, 0), Alignment::Pointer),
448            ArchitectureSize::new(0, 1)
449        );
450        // u8 + u64
451        assert_eq!(
452            align_to_arch(
453                ArchitectureSize::new(1, 0),
454                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
455            ),
456            ArchitectureSize::new(8, 0)
457        );
458        // u8 + u32
459        assert_eq!(
460            align_to_arch(
461                ArchitectureSize::new(1, 0),
462                Alignment::Bytes(NonZeroUsize::new(4).unwrap())
463            ),
464            ArchitectureSize::new(4, 0)
465        );
466        // ptr + u64
467        assert_eq!(
468            align_to_arch(
469                ArchitectureSize::new(0, 1),
470                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
471            ),
472            ArchitectureSize::new(8, 0)
473        );
474        // u32 + ptr
475        assert_eq!(
476            align_to_arch(ArchitectureSize::new(4, 0), Alignment::Pointer),
477            ArchitectureSize::new(0, 1)
478        );
479        // u32, ptr + u64
480        assert_eq!(
481            align_to_arch(
482                ArchitectureSize::new(0, 2),
483                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
484            ),
485            ArchitectureSize::new(0, 2)
486        );
487        // ptr, u8 + u64
488        assert_eq!(
489            align_to_arch(
490                ArchitectureSize::new(1, 1),
491                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
492            ),
493            ArchitectureSize::new(0, 2)
494        );
495        // ptr, u8 + ptr
496        assert_eq!(
497            align_to_arch(ArchitectureSize::new(1, 1), Alignment::Pointer),
498            ArchitectureSize::new(0, 2)
499        );
500        // ptr, ptr, u8 + u64
501        assert_eq!(
502            align_to_arch(
503                ArchitectureSize::new(1, 2),
504                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
505            ),
506            ArchitectureSize::new(8, 2)
507        );
508        assert_eq!(
509            align_to_arch(
510                ArchitectureSize::new(30, 3),
511                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
512            ),
513            ArchitectureSize::new(40, 2)
514        );
515
516        assert_eq!(
517            ArchitectureSize::new(12, 0).max(&ArchitectureSize::new(0, 2)),
518            ArchitectureSize::new(8, 1)
519        );
520        assert_eq!(
521            ArchitectureSize::new(10, 0).max(&ArchitectureSize::new(0, 2)),
522            ArchitectureSize::new(8, 1)
523        );
524
525        assert_eq!(
526            align_to_arch(
527                ArchitectureSize::new(2, 0),
528                Alignment::Bytes(NonZeroUsize::new(8).unwrap())
529            ),
530            ArchitectureSize::new(8, 0)
531        );
532        assert_eq!(
533            align_to_arch(ArchitectureSize::new(2, 0), Alignment::Pointer),
534            ArchitectureSize::new(0, 1)
535        );
536    }
537
538    #[test]
539    fn resource_size() {
540        // keep it identical to the old behavior
541        let obj = SizeAlign::default();
542        let elem = obj.calculate(&TypeDef {
543            name: None,
544            kind: TypeDefKind::Resource,
545            owner: crate::TypeOwner::None,
546            docs: Default::default(),
547            stability: Default::default(),
548        });
549        assert_eq!(elem.size, ArchitectureSize::new(usize::MAX, 0));
550        assert_eq!(
551            elem.align,
552            Alignment::Bytes(NonZeroUsize::new(usize::MAX).unwrap())
553        );
554    }
555    #[test]
556    fn result_ptr_10() {
557        let mut obj = SizeAlign::default();
558        let mut resolve = Resolve::default();
559        let tuple = crate::Tuple {
560            types: vec![Type::U16, Type::U16, Type::U16, Type::U16, Type::U16],
561        };
562        let id = resolve.types.alloc(TypeDef {
563            name: None,
564            kind: TypeDefKind::Tuple(tuple),
565            owner: crate::TypeOwner::None,
566            docs: Default::default(),
567            stability: Default::default(),
568        });
569        obj.fill(&resolve);
570        let my_result = crate::Result_ {
571            ok: Some(Type::String),
572            err: Some(Type::Id(id)),
573        };
574        let elem = obj.calculate(&TypeDef {
575            name: None,
576            kind: TypeDefKind::Result(my_result),
577            owner: crate::TypeOwner::None,
578            docs: Default::default(),
579            stability: Default::default(),
580        });
581        assert_eq!(elem.size, ArchitectureSize::new(8, 2));
582        assert_eq!(elem.align, Alignment::Pointer);
583    }
584    #[test]
585    fn result_ptr_64bit() {
586        let obj = SizeAlign::default();
587        let my_record = crate::Record {
588            fields: vec![
589                crate::Field {
590                    name: String::new(),
591                    ty: Type::String,
592                    docs: Default::default(),
593                },
594                crate::Field {
595                    name: String::new(),
596                    ty: Type::U64,
597                    docs: Default::default(),
598                },
599            ],
600        };
601        let elem = obj.calculate(&TypeDef {
602            name: None,
603            kind: TypeDefKind::Record(my_record),
604            owner: crate::TypeOwner::None,
605            docs: Default::default(),
606            stability: Default::default(),
607        });
608        assert_eq!(elem.size, ArchitectureSize::new(8, 2));
609        assert_eq!(elem.align, Alignment::Bytes(NonZeroUsize::new(8).unwrap()));
610    }
611}