Skip to content

Commit

Permalink
Make size_t/ptrdiff_t match pointer size on 8/16 bit architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Aug 5, 2023
1 parent cf32bac commit e53e8e7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
28 changes: 28 additions & 0 deletions dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,38 @@ version (IN_LLVM)
twstring = twchar.immutableOf().arrayOf();
tdstring = tdchar.immutableOf().arrayOf();

version (IN_LLVM)
{
switch (target.ptrsize)
{
case 1:
tsize_t = basic[Tuns8];
tptrdiff_t = basic[Tint8];
break;
case 2:
tsize_t = basic[Tuns16];
tptrdiff_t = basic[Tint16];
break;
case 4:
tsize_t = basic[Tuns32];
tptrdiff_t = basic[Tint32];
break;
case 8:
tsize_t = basic[Tuns64];
tptrdiff_t = basic[Tint64];
break;
default:
assert(0, "Unsupported target pointer size");
}
}
else
{
const isLP64 = target.isLP64;

tsize_t = basic[isLP64 ? Tuns64 : Tuns32];
tptrdiff_t = basic[isLP64 ? Tint64 : Tint32];
}

thash_t = tsize_t;
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/druntime/src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -2765,12 +2765,12 @@ class Exception : Throwable
* This constructor does not automatically throw the newly-created
* Exception; the $(D throw) expression should be used for that purpose.
*/
@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = cast(size_t) __LINE__, Throwable nextInChain = null)
{
super(msg, file, line, nextInChain);
}

@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = cast(size_t) __LINE__)
{
super(msg, file, line, nextInChain);
}
Expand All @@ -2797,7 +2797,7 @@ class Exception : Throwable
{
auto e = new Exception("msg");
assert(e.file == __FILE__);
assert(e.line == __LINE__ - 2);
assert(e.line == cast(size_t) (__LINE__ - 2));
assert(e.nextInChain is null);
assert(e.msg == "msg");
}
Expand Down
13 changes: 13 additions & 0 deletions tests/compilable/size_t_16bit.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// A minimal test wrt. size_t on a 16-bit architecture.

// REQUIRES: target_AVR
// RUN: %ldc -mtriple=avr -betterC -c %s

static assert(size_t.sizeof == 2);
static assert(ptrdiff_t.sizeof == 2);

// test bounds checks
int foo(int[] arr)
{
return arr[1];
}

0 comments on commit e53e8e7

Please sign in to comment.