Carp/core/Long.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

85 lines
2.4 KiB
Plaintext

(system-include "carp_long.h")
(doc Long "is a bigger integral data type (its size is 64 bits). Its suffix is
`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))
(register / (λ [Long Long] Long))
(register < (λ [Long Long] Bool))
(register > (λ [Long Long] Bool))
(register = (λ [Long Long] Bool))
(register neg (λ [Long] Long))
(register mod (λ [Long Long] Long))
(register seed (λ [Long] ()))
(register bit-shift-left (λ [Long Long] Long))
(register bit-shift-right (λ [Long Long] Long))
(register bit-and (λ [Long Long] Long))
(register bit-or (λ [Long Long] Long))
(register bit-xor (λ [Long Long] Long))
(register bit-not (λ [Long] Long))
(register inc (λ [Long] Long))
(register dec (λ [Long] Long))
(register to-int (λ [Long] Int))
(register from-int (λ [Int] Long))
(todo copy "Should not be needed when refs to value types are auto-converted
to non-refs.")
(register copy (λ [&Long] Long))
(implements + Long.+)
(implements - Long.-)
(implements * Long.*)
(implements / Long./)
(implements < Long.<)
(implements > Long.>)
(implements = Long.=)
(implements copy Long.copy)
(implements inc Long.inc)
(implements dec Long.dec)
(implements neg Long.neg)
(implements mod Long.mod)
(implements to-int Long.to-int)
(implements from-int Long.from-int)
(implements bit-shift-left Long.bit-shift-left)
(implements bit-shift-right Long.bit-shift-right)
(implements bit-and Long.bit-and)
(implements bit-or Long.bit-or)
(implements bit-xor Long.bit-xor)
(implements bit-not Long.bit-not)
(posix-only ; this seems to generate invalid code on some windows machines
(register safe-add (λ [Long Long (Ref Long)] Bool))
(register safe-sub (λ [Long Long (Ref Long)] Bool))
(register safe-mul (λ [Long Long (Ref Long)] Bool))
)
(register abs (λ [Long] Long))
(defn even? [a] (= (mod a 2l) 0l))
(defn odd? [a] (not (even? a)))
(defn zero [] 0l)
(implements abs Long.abs)
(implements zero Long.zero)
)
(defmodule LongRef
(defn = [a b]
(Long.= @a @b))
(implements = LongRef.=)
(defn < [a b]
(Long.< @a @b))
(implements < LongRef.<)
(defn > [a b]
(Long.> @a @b))
(implements > LongRef.>)
)