diff --git a/.claude/CLAUDE.md b/.claude/CLAUDE.md index 40ef29d..aede858 100644 --- a/.claude/CLAUDE.md +++ b/.claude/CLAUDE.md @@ -27,7 +27,7 @@ cargo test ## Key Design Decisions - Follows hyperpolymath ABI-FFI standard (Idris2 ABI, Zig FFI) -- MPL-2.0 license +- MPL-2.0 license (code) + CC-BY-SA-4.0 (docs); full texts in LICENSES/ - RSR (Rhodium Standard Repository) template - Author: Jonathan D.A. Jewell diff --git a/.github/workflows/rhodibot.yml b/.github/workflows/rhodibot.yml index a82f178..1f45960 100644 --- a/.github/workflows/rhodibot.yml +++ b/.github/workflows/rhodibot.yml @@ -4,7 +4,7 @@ # Reads root-hygiene rules and auto-fixes what it can: # - Delete banned files (AI.djot, duplicate CONTRIBUTING.adoc, stale snapshots) # - Rename misnamed files (AI.a2ml → 0-AI-MANIFEST.a2ml) -# - Fix SPDX headers (AGPL → PMPL in dotfiles) +# - Fix SPDX headers (AGPL → MPL-2.0 in dotfiles) # - Create missing required files (SECURITY.md, CONTRIBUTING.md) # - Report unfixable issues as PR comments # @@ -87,7 +87,7 @@ jobs: for dotfile in .gitignore .gitattributes .editorconfig; do if [ -f "$dotfile" ] && grep -q "AGPL-3.0" "$dotfile" 2>/dev/null; then sed -i 's/AGPL-3.0-or-later/MPL-2.0/g; s/AGPL-3.0/MPL-2.0/g' "$dotfile" - FIXES="$FIXES\n- Fixed SPDX header in \`$dotfile\` (AGPL → PMPL)" + FIXES="$FIXES\n- Fixed SPDX header in \`$dotfile\` (AGPL → MPL-2.0)" CHANGED=true fi done diff --git a/.gitignore b/.gitignore index 73f5db0..ae57802 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,8 @@ sync_report*.txt # Hypatia scan cache (local-only) .hypatia/ target/ + +# Idris2 build artefacts +**/build/ +*.ttc +*.ttm diff --git a/.machine_readable/compliance/reuse/dep5 b/.machine_readable/compliance/reuse/dep5 index 49aaed6..bead9ed 100644 --- a/.machine_readable/compliance/reuse/dep5 +++ b/.machine_readable/compliance/reuse/dep5 @@ -52,3 +52,10 @@ License: MPL-2.0 Files: cliff.toml Copyright: {{CURRENT_YEAR}} {{AUTHOR}} ({{OWNER}}) <{{AUTHOR_EMAIL}}> License: MPL-2.0 + +# Documentation prose is CC-BY-SA-4.0 (code/config is MPL-2.0). +# Last-match-wins in the Debian copyright format, so this overrides the +# `Files: *` default above for prose docs. +Files: *.adoc *.md docs/* docs/**/* +Copyright: {{CURRENT_YEAR}} {{AUTHOR}} ({{OWNER}}) <{{AUTHOR_EMAIL}}> +License: CC-BY-SA-4.0 diff --git a/.machine_readable/contractiles/must/Mustfile.a2ml b/.machine_readable/contractiles/must/Mustfile.a2ml index 615f5d0..22df24a 100644 --- a/.machine_readable/contractiles/must/Mustfile.a2ml +++ b/.machine_readable/contractiles/must/Mustfile.a2ml @@ -57,7 +57,7 @@ These are hard requirements — CI fails if any check fails. - severity: warning ### no-agpl -- description: No AGPL-3.0 references (superseded by PMPL) +- description: No AGPL-3.0 references (superseded by MPL-2.0) - run: "! grep -r 'AGPL-3.0' .gitignore .gitattributes .editorconfig 2>/dev/null | head -1 | grep -q ." - severity: critical diff --git a/.machine_readable/contractiles/trust/Trustfile.a2ml b/.machine_readable/contractiles/trust/Trustfile.a2ml index 59b95cf..4547ba3 100644 --- a/.machine_readable/contractiles/trust/Trustfile.a2ml +++ b/.machine_readable/contractiles/trust/Trustfile.a2ml @@ -33,8 +33,8 @@ is traceable. - severity: warning ### license-content -- description: LICENSE contains PMPL identifier -- run: grep -q 'PMPL' LICENSE +- description: LICENSE contains MPL-2.0 identifier +- run: grep -q 'MPL-2.0' LICENSE - severity: warning ### ci-pipeline-present diff --git a/Cargo.toml b/Cargo.toml index b1e012a..774aeb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" edition = "2024" authors = ["Jonathan D.A. Jewell "] description = "Generate high-performance C libraries from high-level descriptions using Nim metaprogramming" -license-file = "LICENSE" +license = "MPL-2.0" repository = "https://github.com/hyperpolymath/nimiser" keywords = ["nim", "metaprogramming", "code-generation", "c-libraries"] categories = ["command-line-utilities", "development-tools"] diff --git a/LICENSE b/LICENSE index 14e2f77..2a8b960 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,5 @@ +SPDX-License-Identifier: MPL-2.0 + Mozilla Public License Version 2.0 ================================== diff --git a/QUICKSTART-MAINTAINER.adoc b/QUICKSTART-MAINTAINER.adoc index 4a971d6..3f130d4 100644 --- a/QUICKSTART-MAINTAINER.adoc +++ b/QUICKSTART-MAINTAINER.adoc @@ -106,7 +106,7 @@ Or via OPSM: `opsm update {{PACKAGE_NAME}}` == Security Notes -* License: MPL-2.0 (Palimpsest License) +* License: MPL-2.0 (code) / CC-BY-SA-4.0 (docs) * All dependencies SHA-pinned * `panic-attacker` scan results: link:INSTALL-SECURITY-REPORT.adoc[] * OpenSSF Scorecard: see badge in README diff --git a/contractiles/trust/Trustfile.a2ml b/contractiles/trust/Trustfile.a2ml index 30782f6..6bc8fd2 100644 --- a/contractiles/trust/Trustfile.a2ml +++ b/contractiles/trust/Trustfile.a2ml @@ -16,7 +16,7 @@ Maximal trust by default — LLM may read, build, test, lint, format. ### license-content - description: LICENSE contains expected SPDX identifier -- run: grep -q 'SPDX\|License\|MIT\|Apache\|PMPL\|MPL' LICENSE +- run: grep -q 'SPDX\|MPL-2.0' LICENSE - severity: critical ### no-secrets-committed diff --git a/docs/RSR_OUTLINE.adoc b/docs/RSR_OUTLINE.adoc index 014b21c..8faf3bb 100644 --- a/docs/RSR_OUTLINE.adoc +++ b/docs/RSR_OUTLINE.adoc @@ -1,6 +1,6 @@ = RSR Template Repository -image:[Palimpsest-MPL-1.0,link="https://github.com/hyperpolymath/palimpsest-license"] image:[Palimpsest,link="https://github.com/hyperpolymath/palimpsest-license"] +image:https://img.shields.io/badge/license-MPL--2.0-blue[MPL-2.0,link="LICENSES/MPL-2.0.txt"] image:https://img.shields.io/badge/docs-CC--BY--SA--4.0-blue[CC-BY-SA-4.0,link="LICENSES/CC-BY-SA-4.0.txt"] :toc: :sectnums: @@ -78,7 +78,7 @@ just validate-rsr |Container build (Wolfi base, Podman) |`LICENSE` -|MPL-2.0 (Palimpsest MPL) +|MPL-2.0 (code) / CC-BY-SA-4.0 (docs) |`EXHIBIT-A-ETHICAL-USE.txt` |Ethical use guidelines (LICENSE Exhibit A) diff --git a/docs/STATE-VISUALIZER.adoc b/docs/STATE-VISUALIZER.adoc index 2af3297..4be8d44 100644 --- a/docs/STATE-VISUALIZER.adoc +++ b/docs/STATE-VISUALIZER.adoc @@ -87,7 +87,7 @@ CONTAINER ECOSYSTEM (Phase 2) REPO INFRASTRUCTURE .machine_readable/ ██████████ 100% STATE/META/ECOSYSTEM active - Governance & License ██████████ 100% PMPL & Ethical use verified + Governance & License ██████████ 100% MPL-2.0 & Ethical use verified Development Shells (Nix/Guix) ██████████ 100% Reproducible env stable ───────────────────────────────────────────────────────────────────────────── diff --git a/src/interface/abi/Foreign.idr b/src/interface/abi/Nimiser/ABI/Foreign.idr similarity index 96% rename from src/interface/abi/Foreign.idr rename to src/interface/abi/Nimiser/ABI/Foreign.idr index 4c12639..5caa83c 100644 --- a/src/interface/abi/Foreign.idr +++ b/src/interface/abi/Nimiser/ABI/Foreign.idr @@ -50,6 +50,15 @@ export free : Handle -> IO () free h = primIO (prim__free (handlePtr h)) +-------------------------------------------------------------------------------- +-- String Marshalling (declared early; used by the generation wrappers below) +-------------------------------------------------------------------------------- + +||| Convert C string to Idris String +export +%foreign "support:idris2_getString, libidris2_support" +prim__getString : Bits64 -> String + -------------------------------------------------------------------------------- -- Nim Compilation Pipeline -------------------------------------------------------------------------------- @@ -102,7 +111,9 @@ genTemplate h tmpl = do (if tmpl.exported then 1 else 0)) if ptr == 0 then pure (Left TemplateError) - else pure (Right (prim__getString ptr)) + else do + let str = prim__getString ptr + pure (Right str) ||| Generate Nim macro code from a macro definition ||| Returns a C string containing generated Nim source, or null on error. @@ -121,17 +132,14 @@ genMacro h mac = do (if mac.generatesExport then 1 else 0)) if ptr == 0 then pure (Left MacroError) - else pure (Right (prim__getString ptr)) + else do + let str = prim__getString ptr + pure (Right str) -------------------------------------------------------------------------------- -- String Operations -------------------------------------------------------------------------------- -||| Convert C string to Idris String -export -%foreign "support:idris2_getString, libidris2_support" -prim__getString : Bits64 -> String - ||| Free C string allocated by the Nim library export %foreign "C:nimiser_free_string, libnimiser" diff --git a/src/interface/abi/Layout.idr b/src/interface/abi/Nimiser/ABI/Layout.idr similarity index 73% rename from src/interface/abi/Layout.idr rename to src/interface/abi/Nimiser/ABI/Layout.idr index 7044587..ac68b85 100644 --- a/src/interface/abi/Layout.idr +++ b/src/interface/abi/Nimiser/ABI/Layout.idr @@ -20,6 +20,8 @@ module Nimiser.ABI.Layout import Nimiser.ABI.Types import Data.Vect import Data.So +import Data.Nat +import Decidable.Equality %default total @@ -33,24 +35,38 @@ paddingFor : (offset : Nat) -> (alignment : Nat) -> Nat paddingFor offset alignment = if offset `mod` alignment == 0 then 0 - else alignment - (offset `mod` alignment) + else minus alignment (offset `mod` alignment) ||| Proof that alignment divides aligned size public export data Divides : Nat -> Nat -> Type where DivideBy : (k : Nat) -> {n : Nat} -> {m : Nat} -> (m = k * n) -> Divides n m +||| Sound decision procedure: does n divide m? +||| For n = S j, compute q = div m (S j) and check m == q * (S j). +||| Division does not reduce definitionally, so the equality is checked, not +||| asserted. +public export +decDivides : (n : Nat) -> (m : Nat) -> Maybe (Divides n m) +decDivides Z _ = Nothing +decDivides (S j) m = + let q = div m (S j) in + case decEq m (q * (S j)) of + Yes prf => Just (DivideBy q prf) + No _ => Nothing + ||| Round up to next alignment boundary public export alignUp : (size : Nat) -> (alignment : Nat) -> Nat alignUp size alignment = size + paddingFor size alignment -||| Proof that alignUp produces aligned result +||| Decide whether alignUp produced an aligned result for a given alignment. +||| (Division does not reduce definitionally, so this is decided rather than +||| proven by `Refl`; a universally-quantified `Refl` proof would be unsound.) public export -alignUpCorrect : (size : Nat) -> (align : Nat) -> (align > 0) -> Divides align (alignUp size align) -alignUpCorrect size align prf = - DivideBy ((size + paddingFor size align) `div` align) Refl +alignUpCorrect : (size : Nat) -> (align : Nat) -> Maybe (Divides align (alignUp size align)) +alignUpCorrect size align = decDivides align (alignUp size align) -------------------------------------------------------------------------------- -- Struct Field Layout @@ -82,7 +98,7 @@ record StructLayout where ||| Calculate total struct size with padding public export -calcStructSize : Vect n Field -> Nat -> Nat +calcStructSize : Vect k Field -> Nat -> Nat calcStructSize [] align = 0 calcStructSize (f :: fs) align = let lastOffset = foldl (\acc, field => nextFieldOffset field) f.offset fs @@ -91,23 +107,28 @@ calcStructSize (f :: fs) align = ||| Proof that field offsets are correctly aligned public export -data FieldsAligned : Vect n Field -> Type where +data FieldsAligned : Vect k Field -> Type where NoFields : FieldsAligned [] ConsField : (f : Field) -> - (rest : Vect n Field) -> + (rest : Vect k Field) -> Divides f.alignment f.offset -> FieldsAligned rest -> FieldsAligned (f :: rest) ||| Verify a struct layout is valid public export -verifyLayout : (fields : Vect n Field) -> (align : Nat) -> Either String StructLayout +verifyLayout : (fields : Vect k Field) -> (align : Nat) -> Either String StructLayout verifyLayout fields align = - let size = calcStructSize fields align - in case decSo (size >= sum (map (\f => f.size) fields)) of - Yes prf => Right (MkStructLayout fields size align) - No _ => Left "Invalid struct size" + let size = calcStructSize fields align in + case choose (size >= sum (map (\f => f.size) fields)) of + Right _ => Left "Invalid struct size" + Left sizeOk => + case decDivides align size of + Nothing => Left "Total size is not a multiple of the alignment" + Just alignProof => + Right (MkStructLayout fields size align + {sizeCorrect = sizeOk} {aligned = alignProof}) -------------------------------------------------------------------------------- -- Nim Object Layout Rules @@ -148,7 +169,7 @@ nimFieldAlign p ty = min (nimFieldSize p ty) (ptrSize p `div` 8) ||| Convert a NimObject to a StructLayout using exportc rules ||| (C-compatible: declaration order, natural alignment, trailing padding) public export -nimObjectToLayout : Platform -> NimObject -> Either String StructLayout +nimObjectToLayout : Platform -> NimObjectDef -> Either String StructLayout nimObjectToLayout p obj = let fields = computeFields p 0 obj.fields in verifyLayout (fromList fields) (maxAlign p obj.fields) @@ -200,10 +221,25 @@ data CABICompliant : StructLayout -> Type where FieldsAligned layout.fields -> CABICompliant layout +||| Decide whether every field's offset is aligned to its alignment. +public export +decFieldsAligned : (fields : Vect k Field) -> Maybe (FieldsAligned fields) +decFieldsAligned [] = Just NoFields +decFieldsAligned (f :: fs) = + case decDivides f.alignment f.offset of + Nothing => Nothing + Just dv => + case decFieldsAligned fs of + Nothing => Nothing + Just rest => Just (ConsField f fs dv rest) + ||| Check if layout follows C ABI public export checkCABI : (layout : StructLayout) -> Either String (CABICompliant layout) -checkCABI layout = Right (CABIOk layout ?fieldsAlignedProof) +checkCABI layout = + case decFieldsAligned layout.fields of + Just prf => Right (CABIOk layout prf) + Nothing => Left "Struct fields are not correctly aligned for C ABI" -------------------------------------------------------------------------------- -- Nim-Specific Layout Examples @@ -224,6 +260,8 @@ nimBufferLayout = ] 16 -- Total size: 16 bytes 8 -- Alignment: 8 bytes (pointer) + {sizeCorrect = Oh} + {aligned = DivideBy 2 Refl} ||| Example: Nim object with default alignment ||| type NimResult {.exportc: "nimiser_result".} = object @@ -239,16 +277,25 @@ nimResultLayout = ] 16 -- Total size: 16 bytes 8 -- Alignment: 8 bytes + {sizeCorrect = Oh} + {aligned = DivideBy 2 Refl} ||| Proof that buffer layout is valid export -nimBufferLayoutValid : CABICompliant nimBufferLayout -nimBufferLayoutValid = CABIOk nimBufferLayout ?bufferFieldsAligned +nimBufferLayoutValid : CABICompliant Layout.nimBufferLayout +nimBufferLayoutValid = + CABIOk nimBufferLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 2 Refl) + (ConsField _ _ (DivideBy 3 Refl) NoFields))) ||| Proof that result layout is valid export -nimResultLayoutValid : CABICompliant nimResultLayout -nimResultLayoutValid = CABIOk nimResultLayout ?resultFieldsAligned +nimResultLayoutValid : CABICompliant Layout.nimResultLayout +nimResultLayoutValid = + CABIOk nimResultLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) NoFields)) -------------------------------------------------------------------------------- -- Offset Calculation @@ -262,7 +309,11 @@ fieldOffset layout name = Just idx => Just (finToNat idx ** index idx layout.fields) Nothing => Nothing -||| Proof that field offset is within struct bounds +||| Decide whether a field's offset is within struct bounds. +||| In general a field may exceed the struct, so the result is a Maybe witness. public export -offsetInBounds : (layout : StructLayout) -> (f : Field) -> So (f.offset + f.size <= layout.totalSize) -offsetInBounds layout f = ?offsetInBoundsProof +offsetInBounds : (layout : StructLayout) -> (f : Field) -> Maybe (So (f.offset + f.size <= layout.totalSize)) +offsetInBounds layout f = + case choose (f.offset + f.size <= layout.totalSize) of + Left ok => Just ok + Right _ => Nothing diff --git a/src/interface/abi/Nimiser/ABI/Proofs.idr b/src/interface/abi/Nimiser/ABI/Proofs.idr new file mode 100644 index 0000000..8667f60 --- /dev/null +++ b/src/interface/abi/Nimiser/ABI/Proofs.idr @@ -0,0 +1,63 @@ +-- SPDX-License-Identifier: MPL-2.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) +-- +||| Machine-checked theorems for the Nimiser ABI. +||| +||| Each concrete `StructLayout` value is proven `CABICompliant` by building +||| the `FieldsAligned` witness directly (one `DivideBy` per field, where the +||| field offset equals `k * alignment`). Multiplication reduces during +||| typechecking; division does not — so these witnesses are built by hand +||| rather than via `decFieldsAligned`. + +module Nimiser.ABI.Proofs + +import Nimiser.ABI.Types +import Nimiser.ABI.Layout +import Data.Vect + +%default total + +-------------------------------------------------------------------------------- +-- C-ABI compliance of the concrete struct layouts +-------------------------------------------------------------------------------- + +||| The `nimiser_buffer` layout is C-ABI compliant. +||| Fields: data @0 align 8 (0 = 0*8), len @8 align 4 (8 = 2*4), +||| cap @12 align 4 (12 = 3*4). +export +nimBufferCompliant : CABICompliant Layout.nimBufferLayout +nimBufferCompliant = + CABIOk nimBufferLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 2 Refl) + (ConsField _ _ (DivideBy 3 Refl) NoFields))) + +||| The `nimiser_result` layout is C-ABI compliant. +||| Fields: code @0 align 4 (0 = 0*4), message @8 align 8 (8 = 1*8). +export +nimResultCompliant : CABICompliant Layout.nimResultLayout +nimResultCompliant = + CABIOk nimResultLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) NoFields)) + +-------------------------------------------------------------------------------- +-- Result-code encoding facts +-------------------------------------------------------------------------------- + +||| The success result encodes to the C integer 0. +export +okIsZero : resultToInt Ok = 0 +okIsZero = Refl + +||| The null-pointer result encodes to the C integer 4 (a nontrivial, distinct +||| code in the middle of the enumeration). +export +nullPointerIsFour : resultToInt NullPointer = 4 +nullPointerIsFour = Refl + +||| The result encoding is injective on the two error codes we pin here: +||| distinct constructors map to distinct integers, witnessed structurally. +export +okDistinctFromError : Not (resultToInt Ok = resultToInt Error) +okDistinctFromError = \case Refl impossible diff --git a/src/interface/abi/Types.idr b/src/interface/abi/Nimiser/ABI/Types.idr similarity index 77% rename from src/interface/abi/Types.idr rename to src/interface/abi/Nimiser/ABI/Types.idr index 2e8d3d6..866c5c2 100644 --- a/src/interface/abi/Types.idr +++ b/src/interface/abi/Nimiser/ABI/Types.idr @@ -16,6 +16,7 @@ module Nimiser.ABI.Types import Data.Bits import Data.So import Data.Vect +import Decidable.Equality %default total @@ -31,10 +32,7 @@ data Platform = Linux | Windows | MacOS | BSD | WASM ||| This will be set during compilation based on target public export thisPlatform : Platform -thisPlatform = - %runElab do - -- Platform detection logic - pure Linux -- Default, override with compiler flags +thisPlatform = Linux -- Default, override with compiler flags -------------------------------------------------------------------------------- -- Nim Metaprogramming Types @@ -205,7 +203,62 @@ DecEq Result where decEq CompilationFailed CompilationFailed = Yes Refl decEq TemplateError TemplateError = Yes Refl decEq MacroError MacroError = Yes Refl - decEq _ _ = No absurd + decEq Ok Error = No (\case Refl impossible) + decEq Ok InvalidParam = No (\case Refl impossible) + decEq Ok OutOfMemory = No (\case Refl impossible) + decEq Ok NullPointer = No (\case Refl impossible) + decEq Ok CompilationFailed = No (\case Refl impossible) + decEq Ok TemplateError = No (\case Refl impossible) + decEq Ok MacroError = No (\case Refl impossible) + decEq Error Ok = No (\case Refl impossible) + decEq Error InvalidParam = No (\case Refl impossible) + decEq Error OutOfMemory = No (\case Refl impossible) + decEq Error NullPointer = No (\case Refl impossible) + decEq Error CompilationFailed = No (\case Refl impossible) + decEq Error TemplateError = No (\case Refl impossible) + decEq Error MacroError = No (\case Refl impossible) + decEq InvalidParam Ok = No (\case Refl impossible) + decEq InvalidParam Error = No (\case Refl impossible) + decEq InvalidParam OutOfMemory = No (\case Refl impossible) + decEq InvalidParam NullPointer = No (\case Refl impossible) + decEq InvalidParam CompilationFailed = No (\case Refl impossible) + decEq InvalidParam TemplateError = No (\case Refl impossible) + decEq InvalidParam MacroError = No (\case Refl impossible) + decEq OutOfMemory Ok = No (\case Refl impossible) + decEq OutOfMemory Error = No (\case Refl impossible) + decEq OutOfMemory InvalidParam = No (\case Refl impossible) + decEq OutOfMemory NullPointer = No (\case Refl impossible) + decEq OutOfMemory CompilationFailed = No (\case Refl impossible) + decEq OutOfMemory TemplateError = No (\case Refl impossible) + decEq OutOfMemory MacroError = No (\case Refl impossible) + decEq NullPointer Ok = No (\case Refl impossible) + decEq NullPointer Error = No (\case Refl impossible) + decEq NullPointer InvalidParam = No (\case Refl impossible) + decEq NullPointer OutOfMemory = No (\case Refl impossible) + decEq NullPointer CompilationFailed = No (\case Refl impossible) + decEq NullPointer TemplateError = No (\case Refl impossible) + decEq NullPointer MacroError = No (\case Refl impossible) + decEq CompilationFailed Ok = No (\case Refl impossible) + decEq CompilationFailed Error = No (\case Refl impossible) + decEq CompilationFailed InvalidParam = No (\case Refl impossible) + decEq CompilationFailed OutOfMemory = No (\case Refl impossible) + decEq CompilationFailed NullPointer = No (\case Refl impossible) + decEq CompilationFailed TemplateError = No (\case Refl impossible) + decEq CompilationFailed MacroError = No (\case Refl impossible) + decEq TemplateError Ok = No (\case Refl impossible) + decEq TemplateError Error = No (\case Refl impossible) + decEq TemplateError InvalidParam = No (\case Refl impossible) + decEq TemplateError OutOfMemory = No (\case Refl impossible) + decEq TemplateError NullPointer = No (\case Refl impossible) + decEq TemplateError CompilationFailed = No (\case Refl impossible) + decEq TemplateError MacroError = No (\case Refl impossible) + decEq MacroError Ok = No (\case Refl impossible) + decEq MacroError Error = No (\case Refl impossible) + decEq MacroError InvalidParam = No (\case Refl impossible) + decEq MacroError OutOfMemory = No (\case Refl impossible) + decEq MacroError NullPointer = No (\case Refl impossible) + decEq MacroError CompilationFailed = No (\case Refl impossible) + decEq MacroError TemplateError = No (\case Refl impossible) -------------------------------------------------------------------------------- -- Opaque Handles @@ -221,8 +274,10 @@ data Handle : Type where ||| Returns Nothing if pointer is null public export createHandle : Bits64 -> Maybe Handle -createHandle 0 = Nothing -createHandle ptr = Just (MkHandle ptr) +createHandle ptr = + case choose (ptr /= 0) of + Left ok => Just (MkHandle ptr {nonNull = ok}) + Right _ => Nothing ||| Extract pointer value from handle public export @@ -245,8 +300,8 @@ record NimField where ||| A Nim object definition that will be exported as a C struct public export -record NimObject where - constructor MkNimObject +record NimObjectDef where + constructor MkNimObjectDef objectName : String fields : List NimField isPacked : Bool -- {.packed.} on the whole object @@ -284,10 +339,15 @@ ptrSize MacOS = 64 ptrSize BSD = 64 ptrSize WASM = 32 -||| Pointer type for platform +||| Pointer-sized integer type for a platform. +||| 64-bit platforms use Bits64; WASM (32-bit) uses Bits32. public export CPtr : Platform -> Type -> Type -CPtr p _ = Bits (ptrSize p) +CPtr Linux _ = Bits64 +CPtr Windows _ = Bits64 +CPtr MacOS _ = Bits64 +CPtr BSD _ = Bits64 +CPtr WASM _ = Bits32 -------------------------------------------------------------------------------- -- Memory Layout Proofs @@ -306,8 +366,8 @@ data HasAlignment : Type -> Nat -> Type where ||| Size of C types (platform-specific) public export cSizeOf : (p : Platform) -> (t : Type) -> Nat -cSizeOf p (CInt _) = 4 -cSizeOf p (CSize _) = if ptrSize p == 64 then 8 else 4 +cSizeOf p Bits8 = 1 +cSizeOf p Bits16 = 2 cSizeOf p Bits32 = 4 cSizeOf p Bits64 = 8 cSizeOf p Double = 8 @@ -316,8 +376,8 @@ cSizeOf p _ = ptrSize p `div` 8 ||| Alignment of C types (platform-specific) public export cAlignOf : (p : Platform) -> (t : Type) -> Nat -cAlignOf p (CInt _) = 4 -cAlignOf p (CSize _) = if ptrSize p == 64 then 8 else 4 +cAlignOf p Bits8 = 1 +cAlignOf p Bits16 = 2 cAlignOf p Bits32 = 4 cAlignOf p Bits64 = 8 cAlignOf p Double = 8 diff --git a/src/interface/abi/nimiser-abi.ipkg b/src/interface/abi/nimiser-abi.ipkg new file mode 100644 index 0000000..3cdafb1 --- /dev/null +++ b/src/interface/abi/nimiser-abi.ipkg @@ -0,0 +1,4 @@ +-- SPDX-License-Identifier: MPL-2.0 +package nimiser-abi +sourcedir = "." +modules = Nimiser.ABI.Types, Nimiser.ABI.Layout, Nimiser.ABI.Foreign, Nimiser.ABI.Proofs