Skip to content

Commit

Permalink
feat: register MAX and MIN macros for stdint types (#1412)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
scolsen committed Apr 13, 2022
1 parent 5856f43 commit 5d530b7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/Double.carp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(implements pi Double.pi)
(def e 2.718281828459045)
(register MAX Double "CARP_DBL_MAX")
(implements MAX MAX)
(register = (Fn [Double Double] Bool))
(register < (Fn [Double Double] Bool))
(register > (Fn [Double Double] Bool))
Expand Down
1 change: 1 addition & 0 deletions core/Float.carp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ suffixed with `f`.")
(defmodule Float
(def pi 3.1415926536f)
(register MAX Float "CARP_FLT_MAX")
(implements MAX MAX)
(register neg (Fn [Float] Float))
(register + (Fn [Float Float] Float))
(register - (Fn [Float Float] Float))
Expand Down
2 changes: 2 additions & 0 deletions core/Int.carp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@

(defmodule Int
(register MAX Int "CARP_INT_MAX")
(implements MAX MAX)
(register MIN Int "CARP_INT_MIN")
(implements MIN MIN)
(register bit-shift-left (λ [Int Int] Int))
(register bit-shift-right (λ [Int Int] Int))
(register bit-and (λ [Int Int] Int))
Expand Down
2 changes: 2 additions & 0 deletions core/Interfaces.carp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
(definterface sqrt (λ [a] a))
(definterface tan (λ [a] a))
(definterface tanh (λ [a] a))
(definterface MAX a)
(definterface MIN a)

(definterface slice (Fn [&a Int Int] a))

Expand Down
2 changes: 2 additions & 0 deletions core/Long.carp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
`l`.")
(defmodule Long
(register MAX Long "LONG_MAX")
(implements MAX MAX)
(register MIN Long "LONG_MIN")
(implements MIN MIN)
(register + (λ [Long Long] Long))
(register - (λ [Long Long] Long))
(register * (λ [Long Long] Long))
Expand Down
24 changes: 24 additions & 0 deletions core/StdInt.carp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ integer.")
(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))

Expand Down Expand Up @@ -74,6 +78,10 @@ integer.")
(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))

Expand Down Expand Up @@ -120,6 +128,10 @@ integer.")
(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))

Expand Down Expand Up @@ -166,6 +178,10 @@ integer.")
(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))

Expand Down Expand Up @@ -212,6 +228,8 @@ integer.")
(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))

Expand Down Expand Up @@ -258,6 +276,8 @@ integer.")
(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))

Expand Down Expand Up @@ -304,6 +324,8 @@ integer.")
(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))

Expand Down Expand Up @@ -350,6 +372,8 @@ integer.")
(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))

Expand Down
14 changes: 14 additions & 0 deletions core/carp_stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ typedef int16_t Int16;
typedef int32_t Int32;
typedef int64_t Int64;

uint8_t CARP_UINT8_MAX = UINT8_MAX;
uint16_t CARP_UINT16_MAX = UINT16_MAX;
uint32_t CARP_UINT32_MAX = UINT32_MAX;
uint64_t CARP_UINT64_MAX = UINT64_MAX;

int8_t CARP_INT8_MAX = INT8_MAX;
int8_t CARP_INT8_MIN = INT8_MIN;
int16_t CARP_INT16_MAX = INT16_MAX;
int16_t CARP_INT16_MIN = INT16_MIN;
int32_t CARP_INT32_MAX = INT32_MAX;
int32_t CARP_INT32_MIN = INT32_MIN;
int64_t CARP_INT64_MAX = INT64_MAX;
int64_t CARP_INT64_MIN = INT64_MIN;

Uint8 Uint8__PLUS_(Uint8 x, Uint8 y) {
return x + y;
}
Expand Down

0 comments on commit 5d530b7

Please sign in to comment.