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 6, 2023
1 parent cf32bac commit 940e02e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 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
12 changes: 12 additions & 0 deletions dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,18 @@ extern(C++) Type typeSemantic(Type type, const ref Loc loc, Scope* sc)
}
else
{
version (IN_LLVM)
{
// Kludge for 8/16 bit targets and `__LINE__` default arguments of type int:
// insert hidden cast if the parameter type is size_t.
// Without this, importing object.d fails (Exception constructors taking size_t line numbers).
if (e.isLineInitExp() && target.ptrsize < 4 && fparam.type.equivalent(Type.tsize_t))
{
if (!e.type)
e.type = Type.tint32;
e = e.castTo(sc, fparam.type);
}
}
e = inferType(e, fparam.type);
Initializer iz = new ExpInitializer(e.loc, e);
iz = iz.initializerSemantic(sc, fparam.type, INITnointerpret);
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 940e02e

Please sign in to comment.