Skip to content

Commit e53e8e7

Browse files
committed
Make size_t/ptrdiff_t match pointer size on 8/16 bit architectures
1 parent cf32bac commit e53e8e7

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

dmd/mtype.d

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,38 @@ version (IN_LLVM)
892892
twstring = twchar.immutableOf().arrayOf();
893893
tdstring = tdchar.immutableOf().arrayOf();
894894

895+
version (IN_LLVM)
896+
{
897+
switch (target.ptrsize)
898+
{
899+
case 1:
900+
tsize_t = basic[Tuns8];
901+
tptrdiff_t = basic[Tint8];
902+
break;
903+
case 2:
904+
tsize_t = basic[Tuns16];
905+
tptrdiff_t = basic[Tint16];
906+
break;
907+
case 4:
908+
tsize_t = basic[Tuns32];
909+
tptrdiff_t = basic[Tint32];
910+
break;
911+
case 8:
912+
tsize_t = basic[Tuns64];
913+
tptrdiff_t = basic[Tint64];
914+
break;
915+
default:
916+
assert(0, "Unsupported target pointer size");
917+
}
918+
}
919+
else
920+
{
895921
const isLP64 = target.isLP64;
896922

897923
tsize_t = basic[isLP64 ? Tuns64 : Tuns32];
898924
tptrdiff_t = basic[isLP64 ? Tint64 : Tint32];
925+
}
926+
899927
thash_t = tsize_t;
900928
}
901929

runtime/druntime/src/object.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,12 +2765,12 @@ class Exception : Throwable
27652765
* This constructor does not automatically throw the newly-created
27662766
* Exception; the $(D throw) expression should be used for that purpose.
27672767
*/
2768-
@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
2768+
@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = cast(size_t) __LINE__, Throwable nextInChain = null)
27692769
{
27702770
super(msg, file, line, nextInChain);
27712771
}
27722772

2773-
@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
2773+
@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = cast(size_t) __LINE__)
27742774
{
27752775
super(msg, file, line, nextInChain);
27762776
}
@@ -2797,7 +2797,7 @@ class Exception : Throwable
27972797
{
27982798
auto e = new Exception("msg");
27992799
assert(e.file == __FILE__);
2800-
assert(e.line == __LINE__ - 2);
2800+
assert(e.line == cast(size_t) (__LINE__ - 2));
28012801
assert(e.nextInChain is null);
28022802
assert(e.msg == "msg");
28032803
}

tests/compilable/size_t_16bit.d

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// A minimal test wrt. size_t on a 16-bit architecture.
2+
3+
// REQUIRES: target_AVR
4+
// RUN: %ldc -mtriple=avr -betterC -c %s
5+
6+
static assert(size_t.sizeof == 2);
7+
static assert(ptrdiff_t.sizeof == 2);
8+
9+
// test bounds checks
10+
int foo(int[] arr)
11+
{
12+
return arr[1];
13+
}

0 commit comments

Comments
 (0)