Skip to content

Commit

Permalink
Add limits.h (#746)
Browse files Browse the repository at this point in the history
* Add limits.h

* Add integration test

* Update Yoakke to support i64 literals

* Compatibility with GCC
  • Loading branch information
kant2002 authored Dec 4, 2024
1 parent aea40bf commit c9d8cc9
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 7 deletions.
48 changes: 48 additions & 0 deletions Cesium.CodeGen/Ir/Expressions/Constants/IntegerConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,54 @@ private static bool TryParse(string text, out long value)
textSpan = textSpan[..^1];
}

if (textSpan.EndsWith("i64", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^3];
}

if (textSpan.EndsWith("i32", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^3];
}

if (textSpan.EndsWith("i16", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^3];
}

if (textSpan.EndsWith("i8", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^2];
}

if (textSpan.EndsWith("ui64", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^4];
}

if (textSpan.EndsWith("ui32", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^4];
}

if (textSpan.EndsWith("ui16", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^4];
}

if (textSpan.EndsWith("ui8", StringComparison.InvariantCultureIgnoreCase))
{
i64Parse = true;
textSpan = textSpan[..^3];
}

if (textSpan.EndsWith("u", StringComparison.InvariantCultureIgnoreCase))
{
unsignedParse = true;
Expand Down
6 changes: 6 additions & 0 deletions Cesium.Compiler/stdlib/limits.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#pragma once
#define CHAR_BIT 8
#define CHAR_MIN (-128)
#define CHAR_MAX 127
#define SCHAR_MIN (-128)
#define SCHAR_MAX 127
#define UCHAR_MAX 0xff

#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX SCHAR_MAX

#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffff
Expand Down
18 changes: 18 additions & 0 deletions Cesium.Compiler/stdlib/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@
#define UINT_FAST32_MAX UINT32_MAX
#define UINT_FAST64_MAX UINT64_MAX

#define INTPTR_MIN INT64_MIN
#define INTPTR_MAX INT64_MAX
#define UINTPTR_MAX UINT64_MAX

#define PTRDIFF_MIN INTPTR_MIN
#define PTRDIFF_MAX INTPTR_MAX

#define SIZE_MAX 0xffffffffffffffffui64

#define SIG_ATOMIC_MIN INT32_MIN
#define SIG_ATOMIC_MAX INT32_MAX

#define WCHAR_MIN 0x0000
#define WCHAR_MAX 0xffff

#define WINT_MIN 0x0000
#define WINT_MAX 0xffff

typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
Expand Down
46 changes: 46 additions & 0 deletions Cesium.IntegrationTests/stdlib/types/limits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <limits.h>
#include <stdint.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
printf("CHAR_BIT = %d\n", CHAR_BIT);
// GCC use different value then MSVC
//printf("MB_LEN_MAX = %d\n\n", MB_LEN_MAX);

printf("CHAR_MIN = %+d\n", CHAR_MIN);
printf("CHAR_MAX = %+d\n", CHAR_MAX);
printf("SCHAR_MIN = %+d\n", SCHAR_MIN);
printf("SCHAR_MAX = %+d\n", SCHAR_MAX);
printf("UCHAR_MAX = %u\n\n", UCHAR_MAX);

printf("SHRT_MIN = %+d\n", SHRT_MIN);
printf("SHRT_MAX = %+d\n", SHRT_MAX);
printf("USHRT_MAX = %u\n\n", USHRT_MAX);

printf("INT_MIN = %+d\n", INT_MIN);
printf("INT_MAX = %+d\n", INT_MAX);
printf("UINT_MAX = %u\n\n", UINT_MAX);

// We cannot test this across all platforms.
//printf("LONG_MIN = %+ld\n", LONG_MIN);
//printf("LONG_MAX = %+ld\n", LONG_MAX);
//printf("ULONG_MAX = %lu\n\n", ULONG_MAX);

//printf("LLONG_MIN = %+lld\n", LLONG_MIN);
//printf("LLONG_MAX = %+lld\n", LLONG_MAX);
//printf("ULLONG_MAX = %llu\n\n", ULLONG_MAX);

// Format t and j is not supported.
// printf("PTRDIFF_MIN = %td\n", PTRDIFF_MIN);
// printf("PTRDIFF_MAX = %+td\n", PTRDIFF_MAX);
// printf("SIZE_MAX = %zu\n", SIZE_MAX);
// printf("SIG_ATOMIC_MIN = %+jd\n", (intmax_t)SIG_ATOMIC_MIN);
// printf("SIG_ATOMIC_MAX = %+jd\n", (intmax_t)SIG_ATOMIC_MAX);
// printf("WCHAR_MIN = %+jd\n", (intmax_t)WCHAR_MIN);
// printf("WCHAR_MAX = %+jd\n", (intmax_t)WCHAR_MAX);
// printf("WINT_MIN = %jd\n", (intmax_t)WINT_MIN);
// printf("WINT_MAX = %jd\n", (intmax_t)WINT_MAX);

return 42;
}
2 changes: 2 additions & 0 deletions Cesium.Runtime.Tests/StdIoFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public void FPrintFHex(long input, string format, string expectedResult)
[Theory]
[InlineData(-1L, "%li", 2, "-1")]
[InlineData(-1L, "%lu", 20, "18446744073709551615")]
[InlineData(-9223372036854775808L, "%li", 20, "-9223372036854775808")]
[InlineData(2147483647L, "%ld", 10, "2147483647")]
[InlineData(-1L, "%llu", 20, "18446744073709551615")]
[InlineData(-1L, "%ull", 20, "18446744073709551615")]
[InlineData(-1L, "%zu", 20, "18446744073709551615")]
Expand Down
14 changes: 7 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.assert" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Yoakke.SynKit.C.Syntax" Version="2024.11.19-2.59.23-nightly" />
<PackageVersion Include="Yoakke.SynKit.Lexer" Version="2024.11.19-2.59.23-nightly" />
<PackageVersion Include="Yoakke.SynKit.Lexer.Generator" Version="2024.11.19-2.59.23-nightly" />
<PackageVersion Include="Yoakke.SynKit.Parser" Version="2024.11.19-2.59.23-nightly" />
<PackageVersion Include="Yoakke.SynKit.Parser.Generator" Version="2024.11.19-2.59.23-nightly" />
<PackageVersion Include="Yoakke.SynKit.Text" Version="2024.11.19-2.59.23-nightly" />
<PackageVersion Include="Yoakke.SynKit.C.Syntax" Version="2024.12.1-13.31.31-nightly" />
<PackageVersion Include="Yoakke.SynKit.Lexer" Version="2024.12.1-13.31.31-nightly" />
<PackageVersion Include="Yoakke.SynKit.Lexer.Generator" Version="2024.12.1-13.31.31-nightly" />
<PackageVersion Include="Yoakke.SynKit.Parser" Version="2024.12.1-13.31.31-nightly" />
<PackageVersion Include="Yoakke.SynKit.Parser.Generator" Version="2024.12.1-13.31.31-nightly" />
<PackageVersion Include="Yoakke.SynKit.Text" Version="2024.12.1-13.31.31-nightly" />
<PackageVersion Include="Nuke.Common" Version="9.0.2" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.12.6" />
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
</ItemGroup>
</Project>
</Project>

0 comments on commit c9d8cc9

Please sign in to comment.