-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayout.idr
More file actions
57 lines (48 loc) · 1.86 KB
/
Copy pathLayout.idr
File metadata and controls
57 lines (48 loc) · 1.86 KB
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
-- SPDX-License-Identifier: MPL-2.0
-- Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
||| Memory Layout Proofs — ABI Foundation.
|||
||| This module provides the formal proofs required to ensure that
||| Idris data structures are binary-compatible with the C/Zig layer.
module VALENCE_SHELL.ABI.Layout
import VALENCE_SHELL.ABI.Types
import Data.Vect
import Data.So
%default total
--------------------------------------------------------------------------------
-- Utilities
--------------------------------------------------------------------------------
||| CALCULATOR: Determines the padding required to reach the next alignment boundary.
public export
paddingFor : (offset : Nat) -> (alignment : Nat) -> Nat
paddingFor offset alignment =
if offset `mod` alignment == 0
then 0
else alignment - (offset `mod` alignment)
||| NORMALIZER: Rounds a size up to the nearest multiple of `alignment`.
public export
alignUp : (size : Nat) -> (alignment : Nat) -> Nat
alignUp size alignment =
size + paddingFor size alignment
--------------------------------------------------------------------------------
-- Struct Layout Logic
--------------------------------------------------------------------------------
||| FIELD METADATA: Tracks the physical footprint of a single record field.
public export
record Field where
constructor MkField
name : String
offset : Nat
size : Nat
alignment : Nat
||| LAYOUT SPECIFICATION: A collection of fields with formal safety proofs.
public export
record StructLayout where
constructor MkStructLayout
fields : Vect n Field
totalSize : Nat
alignment : Nat
-- INVARIANT: The total size must be at least the sum of all field sizes.
{auto 0 sizeCorrect : So (totalSize >= sum (map (\f => f.size) fields))}
-- INVARIANT: The total size must be a multiple of the alignment.
{auto 0 aligned : Divides alignment totalSize}