Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: Make size_t/ptrdiff_t match pointer size on 8/16 bit architectures #4466

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions tests/codegen/size_t_16bit.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// A minimal test wrt. size_t on a 16-bit architecture.

// REQUIRES: target_AVR
// RUN: %ldc -mtriple=avr -betterC -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
// RUN: %ldc -mtriple=avr -betterC -c %s

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

int testBoundsCheck(int[] arr)
{
return arr[1];
}

// __LINE__ expressions are of type int
void takeIntLine(int line = __LINE__) {}
// special case for 8/16 bit targets: __LINE__ magically cast to size_t
void takeSizeTLine(size_t line = __LINE__) {}

void testDefaultLineArgs()
{
// CHECK: call {{.*}}takeIntLine{{.*}}(i32 [[@LINE+1]])
takeIntLine();
// CHECK: call {{.*}}takeSizeTLine{{.*}}(i16 zeroext [[@LINE+1]])
takeSizeTLine();
}