From 30ed0ae8c3d4a2544b3fc2fe1874ec59a7c54b22 Mon Sep 17 00:00:00 2001 From: hellerve Date: Thu, 23 Apr 2020 10:42:35 +0200 Subject: [PATCH] all: various long fixes --- core/carp_binary.h | 6 +++--- core/carp_long.h | 8 +++----- core/carp_memory.h | 2 +- core/carp_safe_int.h | 6 +++--- core/carp_stdint.h | 34 +++++++++++++++++----------------- core/core.h | 6 ------ run_carp_tests.ps1 | 1 - src/StartingEnv.hs | 12 ++++++------ 8 files changed, 33 insertions(+), 42 deletions(-) diff --git a/core/carp_binary.h b/core/carp_binary.h index 85eeb345..76d37ed7 100644 --- a/core/carp_binary.h +++ b/core/carp_binary.h @@ -10,9 +10,9 @@ uint32_t Binary_to_MINUS_int32(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) { uint64_t Binary_to_MINUS_int64(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) { - return (uint64_t)b1 | ((uint64_t)b2 << 8) | ((uint64_t)b3 << 16) | ((uint64_t)b4 << 24) | - ((uint64_t)b5 << 32) | ((uint64_t)b6 << 40) | ((uint64_t)b7 << 48) | - ((uint64_t)b8 << 56); + return (uint64_t)b1 | ((uint64_t)b2 << 8) | ((uint64_t)b3 << 16) | + ((uint64_t)b4 << 24) | ((uint64_t)b5 << 32) | ((uint64_t)b6 << 40) | + ((uint64_t)b7 << 48) | ((uint64_t)b8 << 56); } uint8_t Binary_int16_MINUS_to_MINUS_byte(uint16_t *x) { diff --git a/core/carp_long.h b/core/carp_long.h index 7e8faa3f..7da4281c 100644 --- a/core/carp_long.h +++ b/core/carp_long.h @@ -10,17 +10,15 @@ Long Long__MUL_(Long x, Long y) { Long Long__DIV_(Long x, Long y) { return x / y; } -#ifndef _WIN32 bool Long_safe_MINUS_add(Long x, Long y, Long* res) { - return __builtin_saddll_overflow(x, y, res); + return __builtin_add_overflow(x, y, res); } bool Long_safe_MINUS_sub(Long x, Long y, Long* res) { - return __builtin_ssubll_overflow(x, y, res); + return __builtin_sub_overflow(x, y, res); } bool Long_safe_MINUS_mul(Long x, Long y, Long* res) { - return __builtin_smulll_overflow(x, y, res); + return __builtin_mul_overflow(x, y, res); } -#endif bool Long__EQ_(Long x, Long y) { return x == y; } diff --git a/core/carp_memory.h b/core/carp_memory.h index 366a9038..5e964377 100644 --- a/core/carp_memory.h +++ b/core/carp_memory.h @@ -6,7 +6,7 @@ bool log_memory_balance = false; void *logged_malloc(size_t size) { void *ptr = malloc(size); if (log_memory_balance) { - printf("MALLOC: %p (%ld bytes)\n", ptr, size); + printf("MALLOC: %p (%zu bytes)\n", ptr, size); } malloc_balance_counter++; return ptr; diff --git a/core/carp_safe_int.h b/core/carp_safe_int.h index 63c0b062..7fa836f3 100644 --- a/core/carp_safe_int.h +++ b/core/carp_safe_int.h @@ -1,9 +1,9 @@ bool Int_safe_MINUS_add(int x, int y, int* res) { - return __builtin_sadd_overflow(x, y, res); + return __builtin_add_overflow(x, y, res); } bool Int_safe_MINUS_sub(int x, int y, int* res) { - return __builtin_ssub_overflow(x, y, res); + return __builtin_sub_overflow(x, y, res); } bool Int_safe_MINUS_mul(int x, int y, int* res) { - return __builtin_smul_overflow(x, y, res); + return __builtin_mul_overflow(x, y, res); } diff --git a/core/carp_stdint.h b/core/carp_stdint.h index 95d9d16a..6d088c06 100644 --- a/core/carp_stdint.h +++ b/core/carp_stdint.h @@ -52,10 +52,10 @@ String Uint8_str(Uint8 x) { snprintf(buffer, size, "Uint8(%" PRIu8 ")", x); return buffer; } -Uint8 Uint8_from_MINUS_long(long x) { +Uint8 Uint8_from_MINUS_long(Long x) { return (Uint8)x; } -long Uint8_to_MINUS_long(Uint8 x) { +Long Uint8_to_MINUS_long(Uint8 x) { return (long)x; } Uint8 Uint8_copy(Uint8* x) { @@ -119,10 +119,10 @@ String Uint16_str(Uint16 x) { snprintf(buffer, size, "Uint16(%" PRIu16 ")", x); return buffer; } -Uint16 Uint16_from_MINUS_long(long x) { +Uint16 Uint16_from_MINUS_long(Long x) { return (Uint16)x; } -long Uint16_to_MINUS_long(Uint16 x) { +Long Uint16_to_MINUS_long(Uint16 x) { return (long)x; } Uint16 Uint16_copy(Uint16* x) { @@ -186,10 +186,10 @@ String Uint32_str(Uint32 x) { snprintf(buffer, size, "Uint32(%" PRIu32 ")", x); return buffer; } -Uint32 Uint32_from_MINUS_long(long x) { +Uint32 Uint32_from_MINUS_long(Long x) { return (Uint32)x; } -long Uint32_to_MINUS_long(Uint32 x) { +Long Uint32_to_MINUS_long(Uint32 x) { return (long)x; } Uint32 Uint32_copy(Uint32* x) { @@ -253,10 +253,10 @@ String Uint64_str(Uint64 x) { snprintf(buffer, size, "Uint64(%" PRIu64 ")", x); return buffer; } -Uint64 Uint64_from_MINUS_long(long x) { +Uint64 Uint64_from_MINUS_long(Long x) { return (Uint64)x; } -long Uint64_to_MINUS_long(Uint64 x) { +Long Uint64_to_MINUS_long(Uint64 x) { return (long)x; } Uint64 Uint64_copy(Uint64* x) { @@ -320,10 +320,10 @@ String Int8_str(Int8 x) { snprintf(buffer, size, "Int8(%" PRId8 ")", x); return buffer; } -Int8 Int8_from_MINUS_long(long x) { +Int8 Int8_from_MINUS_long(Long x) { return (Int8)x; } -long Int8_to_MINUS_long(Int8 x) { +Long Int8_to_MINUS_long(Int8 x) { return (long)x; } Int8 Int8_copy(Int8* x) { @@ -387,10 +387,10 @@ String Int16_str(Int16 x) { snprintf(buffer, size, "Int16(%" PRId16 ")", x); return buffer; } -Int16 Int16_from_MINUS_long(long x) { +Int16 Int16_from_MINUS_long(Long x) { return (Int16)x; } -long Int16_to_MINUS_long(Int16 x) { +Long Int16_to_MINUS_long(Int16 x) { return (long)x; } Int16 Int16_copy(Int16* x) { @@ -454,10 +454,10 @@ String Int32_str(Int32 x) { snprintf(buffer, size, "Int32(%" PRId32 ")", x); return buffer; } -Int32 Int32_from_MINUS_long(long x) { +Int32 Int32_from_MINUS_long(Long x) { return (Int32)x; } -long Int32_to_MINUS_long(Int32 x) { +Long Int32_to_MINUS_long(Int32 x) { return (long)x; } Int32 Int32_copy(Int32* x) { @@ -521,11 +521,11 @@ String Int64_str(Int64 x) { snprintf(buffer, size, "Int64(%" PRId64 ")", x); return buffer; } -Int64 Int64_from_MINUS_long(long x) { +Int64 Int64_from_MINUS_long(Long x) { return (Int64)x; } -long Int64_to_MINUS_long(Int64 x) { - return (long)x; +Long Int64_to_MINUS_long(Int64 x) { + return (Long)x; } Int64 Int64_copy(Int64* x) { return *x; diff --git a/core/core.h b/core/core.h index d37771a6..93bf81d7 100644 --- a/core/core.h +++ b/core/core.h @@ -17,13 +17,7 @@ typedef int64_t Long; #define CHK_INDEX(i, n) #else -#if defined(WIN32) || defined(_WIN32) || \ - defined(__WIN32) && !defined(__CYGWIN__) -// The %zd format flag doesn't seem to work on Windows? -#define CHK_INDEX_FORMAT_STRING ":%u: bad index: %ld < %ld\n" -#else #define CHK_INDEX_FORMAT_STRING ":%u: bad index: %zd < %zd\n" -#endif #define CHK_INDEX(i, n) \ do { \ diff --git a/run_carp_tests.ps1 b/run_carp_tests.ps1 index 5f4d776d..8fff7168 100644 --- a/run_carp_tests.ps1 +++ b/run_carp_tests.ps1 @@ -32,7 +32,6 @@ exitOnError { stack exec carp "--" ./examples/check_malloc.carp -b } # Generate docs exitOnError { stack exec carp "--" ./docs/core/generate_core_docs.carp } -exitOnError { stack exec carp "--" ./docs/sdl/generate_sdl_docs.carp } echo "ALL TESTS DONE." diff --git a/src/StartingEnv.hs b/src/StartingEnv.hs index a65b12b0..f65767f4 100644 --- a/src/StartingEnv.hs +++ b/src/StartingEnv.hs @@ -113,7 +113,7 @@ templatePointerAdd = defineTemplate (SymPath ["Pointer"] "add") (FuncTy [PointerTy (VarTy "p"), LongTy] (PointerTy (VarTy "p")) StaticLifetimeTy) "adds a long integer value to a pointer." - (toTemplate "$p* $NAME ($p *p, long x)") + (toTemplate "$p* $NAME ($p *p, Long x)") (toTemplate $ unlines ["$DECL {" ," return p + x;" ,"}"]) @@ -123,7 +123,7 @@ templatePointerSub = defineTemplate (SymPath ["Pointer"] "sub") (FuncTy [PointerTy (VarTy "p"), LongTy] (PointerTy (VarTy "p")) StaticLifetimeTy) "subtracts a long integer value from a pointer." - (toTemplate "$p* $NAME ($p *p, long x)") + (toTemplate "$p* $NAME ($p *p, Long x)") (toTemplate $ unlines ["$DECL {" ," return p - x;" ,"}"]) @@ -133,7 +133,7 @@ templatePointerWidth = defineTemplate (SymPath ["Pointer"] "width") (FuncTy [PointerTy (VarTy "p")] LongTy StaticLifetimeTy) "gets the byte size of a pointer." - (toTemplate "long $NAME ($p *p)") + (toTemplate "Long $NAME ($p *p)") (toTemplate $ unlines ["$DECL {" ," return sizeof(*p);" ,"}"]) @@ -143,9 +143,9 @@ templatePointerToLong = defineTemplate (SymPath ["Pointer"] "to-long") (FuncTy [PointerTy (VarTy "p")] LongTy StaticLifetimeTy) "converts a pointer to a long integer." - (toTemplate "long $NAME ($p *p)") + (toTemplate "Long $NAME ($p *p)") (toTemplate $ unlines ["$DECL {" - ," return (long)p;" + ," return (Long)p;" ,"}"]) (const []) @@ -153,7 +153,7 @@ templatePointerFromLong = defineTemplate (SymPath ["Pointer"] "from-long") (FuncTy [LongTy] (PointerTy (VarTy "p")) StaticLifetimeTy) "converts a long integer to a pointer." - (toTemplate "$p* $NAME (long p)") + (toTemplate "$p* $NAME (Long p)") (toTemplate $ unlines ["$DECL {" ," return ($p*)p;" ,"}"])