Carp/core/StdInt.carp
Scott Olsen 5d530b7491
feat: register MAX and MIN macros for stdint types (#1412)
* feat: register MAX and MIN macros for stdint types

Adds MAX and MIN for each INT<N> type and MAX for each UINT<N> type.
These are macros defined by stdint.h and are sometimes useful for bounds
determinations and conversions and such.

* feat: make MAX and MIN interfaces

Since several numeric types define maximum and minimum values, it makes
sense for these to be defined as interfaces. This commit also makes
existing definitions of MAX and MIN for Carp's numeric types implement
the interfaces.

* fix: respect let binding shadowing in memory management (#1413)

* fix: respect let binding shadowing in memory management

Previously, we didn't account for shadowing in let bindings in our
memory management routines. This led to rare situations in which
multiple deleters might be added for a single variable name, for
example:

```clojure
(defn n [xs]
  (let [xs [1 2 3]
        n &xs]
    n))
```

The borrow checker would fail on this code since it would assign `xs`
two deleters, one for the untyped argument and another for the let
binding.

Instead, we now perform *exclusive* ownership transfer for the duration
of the let scope--when a shadow is introduced, the previous deleters for
that variable name are dropped until the end of the let scope, since we
evaluate all instances of the shadowed name to the more local binding.
At the end of the let scope, the original deleter is restored.

Fixes issue #597

* refactor: improved dead reference error for let

Since let scopes resolve to their bodies, we can report the body of the
let as the xobj producing an error when a dead reference is returned.

* test: update error message for dead refs in let

* test: add regression test for issue #597

Ensure we don't regress and fail to manage memory when let bindings
shadow function argument names.

* fix: respect symbol modes on interface concretization (#1415)

* fix: respect symbol modes on interface concretization

When concretizing interfaces (finding the appropriate implementation at
a call site) we previously set the lookup mode of all such resolved
symbols to CarpLand AFunction. This incorrectly overwrites the lookup
mode of Externally registered types, causing them to emit incorrect C
when the user specifies an override.

We now preserve whatever lookup mode is assigned to the implementation
the concretization resolves the interface to. This not only fixes the
external override emission issue, but should be more correct in general.

fixes #1414

* test: add regression test for issue #1414
2022-04-13 09:32:46 +02:00

404 lines
12 KiB
Plaintext

(system-include "carp_stdint.h")
(register-type Uint8)
(register-type Uint16)
(register-type Uint32)
(register-type Uint64)
(register-type Int8)
(register-type Int16)
(register-type Int32)
(register-type Int64)
(doc Int8 "is a thin wrapper around the `int8_t` C data type, a signed 8-bit
integer.")
(defmodule Int8
(register = (λ [Int8 Int8] Bool))
(register > (λ [Int8 Int8] Bool))
(register < (λ [Int8 Int8] Bool))
(register + (λ [Int8 Int8] Int8))
(register - (λ [Int8 Int8] Int8))
(register * (λ [Int8 Int8] Int8))
(register / (λ [Int8 Int8] Int8))
(register bit-shift-left (λ [Int8 Int8] Int8))
(register bit-shift-right (λ [Int8 Int8] Int8))
(register bit-or (λ [Int8 Int8] Int8))
(register bit-and (λ [Int8 Int8] Int8))
(register bit-not (λ [Int8] Int8))
(register bit-xor (λ [Int8 Int8] Int8))
(register to-long (λ [Int8] Long))
(register from-long (λ [Long] Int8))
(register copy (Fn [&Int8] Int8))
(register MAX Int8 "CARP_INT8_MAX")
(implements MAX MAX)
(register MIN Int8 "CARP_INT8_MIN")
(implements MIN MIN)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Int8)))
(implements + Int8.+)
(implements - Int8.-)
(implements * Int8.*)
(implements / Int8./)
(implements < Int8.<)
(implements > Int8.>)
(implements = Int8.=)
(implements copy Int8.copy)
(implements zero Int8.zero)
(implements bit-shift-left Int8.bit-shift-left)
(implements bit-shift-right Int8.bit-shift-right)
(implements bit-and Int8.bit-and)
(implements bit-or Int8.bit-or)
(implements bit-xor Int8.bit-xor)
(implements bit-not Int8.bit-not)
)
(defmodule Int8Extra
(defn = [a b] (Int8.= @a @b))
(implements = Int8Extra.=)
)
(doc Int16 "is a thin wrapper around the `int16_t` C data type, a signed 16-bit
integer.")
(defmodule Int16
(register = (λ [Int16 Int16] Bool))
(register > (λ [Int16 Int16] Bool))
(register < (λ [Int16 Int16] Bool))
(register + (λ [Int16 Int16] Int16))
(register - (λ [Int16 Int16] Int16))
(register * (λ [Int16 Int16] Int16))
(register / (λ [Int16 Int16] Int16))
(register bit-shift-left (λ [Int16 Int16] Int16))
(register bit-shift-right (λ [Int16 Int16] Int16))
(register bit-or (λ [Int16 Int16] Int16))
(register bit-and (λ [Int16 Int16] Int16))
(register bit-not (λ [Int16] Int16))
(register bit-xor (λ [Int16 Int16] Int16))
(register to-long (λ [Int16] Long))
(register from-long (λ [Long] Int16))
(register copy (Fn [&Int16] Int16))
(register MAX Int16 "CARP_INT16_MAX")
(implements MAX MAX)
(register MIN Int16 "CARP_INT16_MIN")
(implements MIN MIN)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Int16)))
(implements + Int16.+)
(implements - Int16.-)
(implements * Int16.*)
(implements / Int16./)
(implements < Int16.<)
(implements > Int16.>)
(implements = Int16.=)
(implements copy Int16.copy)
(implements zero Int16.zero)
(implements bit-shift-left Int16.bit-shift-left)
(implements bit-shift-right Int16.bit-shift-right)
(implements bit-and Int16.bit-and)
(implements bit-or Int16.bit-or)
(implements bit-xor Int16.bit-xor)
(implements bit-not Int16.bit-not)
)
(defmodule Int16Extra
(defn = [a b] (Int16.= @a @b))
(implements = Int16Extra.=)
)
(doc Int32 "is a thin wrapper around the `int32_t` C data type, a signed 32-bit
integer.")
(defmodule Int32
(register = (λ [Int32 Int32] Bool))
(register > (λ [Int32 Int32] Bool))
(register < (λ [Int32 Int32] Bool))
(register + (λ [Int32 Int32] Int32))
(register - (λ [Int32 Int32] Int32))
(register * (λ [Int32 Int32] Int32))
(register / (λ [Int32 Int32] Int32))
(register bit-shift-left (λ [Int32 Int32] Int32))
(register bit-shift-right (λ [Int32 Int32] Int32))
(register bit-or (λ [Int32 Int32] Int32))
(register bit-and (λ [Int32 Int32] Int32))
(register bit-not (λ [Int32] Int32))
(register bit-xor (λ [Int32 Int32] Int32))
(register to-long (λ [Int32] Long))
(register from-long (λ [Long] Int32))
(register copy (Fn [&Int32] Int32))
(register MAX Int32 "CARP_INT32_MAX")
(implements MAX MAX)
(register MIN Int32 "CARP_INT32_MIN")
(implements MIN MIN)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Int32)))
(implements + Int32.+)
(implements - Int32.-)
(implements * Int32.*)
(implements / Int32./)
(implements < Int32.<)
(implements > Int32.>)
(implements = Int32.=)
(implements copy Int32.copy)
(implements zero Int32.zero)
(implements bit-shift-left Int32.bit-shift-left)
(implements bit-shift-right Int32.bit-shift-right)
(implements bit-and Int32.bit-and)
(implements bit-or Int32.bit-or)
(implements bit-xor Int32.bit-xor)
(implements bit-not Int32.bit-not)
)
(defmodule Int32Extra
(defn = [a b] (Int32.= @a @b))
(implements = Int32Extra.=)
)
(doc Int64 "is a thin wrapper around the `int64_t` C data type, a signed 64-bit
integer.")
(defmodule Int64
(register = (λ [Int64 Int64] Bool))
(register > (λ [Int64 Int64] Bool))
(register < (λ [Int64 Int64] Bool))
(register + (λ [Int64 Int64] Int64))
(register - (λ [Int64 Int64] Int64))
(register * (λ [Int64 Int64] Int64))
(register / (λ [Int64 Int64] Int64))
(register bit-shift-left (λ [Int64 Int64] Int64))
(register bit-shift-right (λ [Int64 Int64] Int64))
(register bit-or (λ [Int64 Int64] Int64))
(register bit-and (λ [Int64 Int64] Int64))
(register bit-not (λ [Int64] Int64))
(register bit-xor (λ [Int64 Int64] Int64))
(register to-long (λ [Int64] Long))
(register from-long (λ [Long] Int64))
(register copy (Fn [&Int64] Int64))
(register MAX Int64 "CARP_INT64_MAX")
(implements MAX MAX)
(register MIN Int64 "CARP_INT64_MIN")
(implements MIN MIN)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Int64)))
(implements + Int64.+)
(implements - Int64.-)
(implements * Int64.*)
(implements / Int64./)
(implements < Int64.<)
(implements > Int64.>)
(implements = Int64.=)
(implements copy Int64.copy)
(implements zero Int64.zero)
(implements bit-shift-left Int64.bit-shift-left)
(implements bit-shift-right Int64.bit-shift-right)
(implements bit-and Int64.bit-and)
(implements bit-or Int64.bit-or)
(implements bit-xor Int64.bit-xor)
(implements bit-not Int64.bit-not)
)
(defmodule Int64Extra
(defn = [a b] (Int64.= @a @b))
(implements = Int64Extra.=)
)
(doc Uint8 "is a thin wrapper around the `uint8_t` C data type, an unsigned
8-bit integer.")
(defmodule Uint8
(register = (λ [Uint8 Uint8] Bool))
(register > (λ [Uint8 Uint8] Bool))
(register < (λ [Uint8 Uint8] Bool))
(register + (λ [Uint8 Uint8] Uint8))
(register - (λ [Uint8 Uint8] Uint8))
(register * (λ [Uint8 Uint8] Uint8))
(register / (λ [Uint8 Uint8] Uint8))
(register bit-shift-left (λ [Uint8 Uint8] Uint8))
(register bit-shift-right (λ [Uint8 Uint8] Uint8))
(register bit-or (λ [Uint8 Uint8] Uint8))
(register bit-and (λ [Uint8 Uint8] Uint8))
(register bit-not (λ [Uint8] Uint8))
(register bit-xor (λ [Uint8 Uint8] Uint8))
(register to-long (λ [Uint8] Long))
(register from-long (λ [Long] Uint8))
(register copy (Fn [&Uint8] Uint8))
(register MAX Uint8 "CARP_UINT8_MAX")
(implements MAX MAX)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Uint8)))
(implements + Uint8.+)
(implements - Uint8.-)
(implements * Uint8.*)
(implements / Uint8./)
(implements < Uint8.<)
(implements > Uint8.>)
(implements = Uint8.=)
(implements copy Uint8.copy)
(implements zero Uint8.zero)
(implements bit-shift-left Uint8.bit-shift-left)
(implements bit-shift-right Uint8.bit-shift-right)
(implements bit-and Uint8.bit-and)
(implements bit-or Uint8.bit-or)
(implements bit-xor Uint8.bit-xor)
(implements bit-not Uint8.bit-not)
)
(defmodule Uint8Extra
(defn = [a b] (Uint8.= @a @b))
(implements = Uint8Extra.=)
)
(doc Uint16 "is a thin wrapper around the `uint16_t` C data type, an unsigned
16-bit integer.")
(defmodule Uint16
(register = (λ [Uint16 Uint16] Bool))
(register > (λ [Uint16 Uint16] Bool))
(register < (λ [Uint16 Uint16] Bool))
(register + (λ [Uint16 Uint16] Uint16))
(register - (λ [Uint16 Uint16] Uint16))
(register * (λ [Uint16 Uint16] Uint16))
(register / (λ [Uint16 Uint16] Uint16))
(register bit-shift-left (λ [Uint16 Uint16] Uint16))
(register bit-shift-right (λ [Uint16 Uint16] Uint16))
(register bit-or (λ [Uint16 Uint16] Uint16))
(register bit-and (λ [Uint16 Uint16] Uint16))
(register bit-not (λ [Uint16] Uint16))
(register bit-xor (λ [Uint16 Uint16] Uint16))
(register to-long (λ [Uint16] Long))
(register from-long (λ [Long] Uint16))
(register copy (Fn [&Uint16] Uint16))
(register MAX Uint16 "CARP_UINT16_MAX")
(implements MAX MAX)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Uint16)))
(implements + Uint16.+)
(implements - Uint16.-)
(implements * Uint16.*)
(implements / Uint16./)
(implements < Uint16.<)
(implements > Uint16.>)
(implements = Uint16.=)
(implements copy Uint16.copy)
(implements zero Uint16.zero)
(implements bit-shift-left Uint16.bit-shift-left)
(implements bit-shift-right Uint16.bit-shift-right)
(implements bit-and Uint16.bit-and)
(implements bit-or Uint16.bit-or)
(implements bit-xor Uint16.bit-xor)
(implements bit-not Uint16.bit-not)
)
(defmodule Uint16Extra
(defn = [a b] (Uint16.= @a @b))
(implements = Uint16Extra.=)
)
(doc Uint32 "is a thin wrapper around the `uint32_t` C data type, an unsigned
32-bit integer.")
(defmodule Uint32
(register = (λ [Uint32 Uint32] Bool))
(register > (λ [Uint32 Uint32] Bool))
(register < (λ [Uint32 Uint32] Bool))
(register + (λ [Uint32 Uint32] Uint32))
(register - (λ [Uint32 Uint32] Uint32))
(register * (λ [Uint32 Uint32] Uint32))
(register / (λ [Uint32 Uint32] Uint32))
(register bit-shift-left (λ [Uint32 Uint32] Uint32))
(register bit-shift-right (λ [Uint32 Uint32] Uint32))
(register bit-or (λ [Uint32 Uint32] Uint32))
(register bit-and (λ [Uint32 Uint32] Uint32))
(register bit-not (λ [Uint32] Uint32))
(register bit-xor (λ [Uint32 Uint32] Uint32))
(register to-long (λ [Uint32] Long))
(register from-long (λ [Long] Uint32))
(register copy (Fn [&Uint32] Uint32))
(register MAX Uint32 "CARP_UINT32_MAX")
(implements MAX MAX)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Uint32)))
(implements + Uint32.+)
(implements - Uint32.-)
(implements * Uint32.*)
(implements / Uint32./)
(implements < Uint32.<)
(implements > Uint32.>)
(implements = Uint32.=)
(implements copy Uint32.copy)
(implements zero Uint32.zero)
(implements bit-shift-left Uint32.bit-shift-left)
(implements bit-shift-right Uint32.bit-shift-right)
(implements bit-and Uint32.bit-and)
(implements bit-or Uint32.bit-or)
(implements bit-xor Uint32.bit-xor)
(implements bit-not Uint32.bit-not)
)
(defmodule Uint32Extra
(defn = [a b] (Uint32.= @a @b))
(implements = Uint32Extra.=)
)
(doc Uint64 "is a thin wrapper around the `uint64_t` C data type, an unsigned
64-bit integer.")
(defmodule Uint64
(register = (λ [Uint64 Uint64] Bool))
(register > (λ [Uint64 Uint64] Bool))
(register < (λ [Uint64 Uint64] Bool))
(register + (λ [Uint64 Uint64] Uint64))
(register - (λ [Uint64 Uint64] Uint64))
(register * (λ [Uint64 Uint64] Uint64))
(register / (λ [Uint64 Uint64] Uint64))
(register bit-shift-left (λ [Uint64 Uint64] Uint64))
(register bit-shift-right (λ [Uint64 Uint64] Uint64))
(register bit-or (λ [Uint64 Uint64] Uint64))
(register bit-and (λ [Uint64 Uint64] Uint64))
(register bit-not (λ [Uint64] Uint64))
(register bit-xor (λ [Uint64 Uint64] Uint64))
(register to-long (λ [Uint64] Long))
(register from-long (λ [Long] Uint64))
(register copy (Fn [&Uint64] Uint64))
(register MAX Uint64 "CARP_UINT64_MAX")
(implements MAX MAX)
(defn zero [] (from-long 0l))
(register from-bytes (Fn [&(Array Byte)] (Array Uint64)))
(implements + Uint64.+)
(implements - Uint64.-)
(implements * Uint64.*)
(implements / Uint64./)
(implements < Uint64.<)
(implements > Uint64.>)
(implements = Uint64.=)
(implements copy Uint64.copy)
(implements zero Uint64.zero)
(implements bit-shift-left Uint64.bit-shift-left)
(implements bit-shift-right Uint64.bit-shift-right)
(implements bit-and Uint64.bit-and)
(implements bit-or Uint64.bit-or)
(implements bit-xor Uint64.bit-xor)
(implements bit-not Uint64.bit-not)
)
(defmodule Uint64Extra
(defn = [a b] (Uint64.= @a @b))
(implements = Uint64Extra.=)
)