Skip to content

Commit 739289e

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

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
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

dmd/typesem.d

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,18 @@ extern(C++) Type typeSemantic(Type type, const ref Loc loc, Scope* sc)
10711071
}
10721072
else
10731073
{
1074+
version (IN_LLVM)
1075+
{
1076+
// Kludge for 8/16 bit targets and `__LINE__` default arguments of type int:
1077+
// insert hidden cast if the parameter type is size_t.
1078+
// Without this, importing object.d fails (Exception constructors taking size_t line numbers).
1079+
if (e.isLineInitExp() && target.ptrsize < 4 && fparam.type.equivalent(Type.tsize_t))
1080+
{
1081+
if (!e.type)
1082+
e.type = Type.tint32;
1083+
e = e.castTo(sc, fparam.type);
1084+
}
1085+
}
10741086
e = inferType(e, fparam.type);
10751087
Initializer iz = new ExpInitializer(e.loc, e);
10761088
iz = iz.initializerSemantic(sc, fparam.type, INITnointerpret);

tests/codegen/size_t_16bit.d

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// A minimal test wrt. size_t on a 16-bit architecture.
2+
3+
// REQUIRES: target_AVR
4+
// RUN: %ldc -mtriple=avr -betterC -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
5+
// RUN: %ldc -mtriple=avr -betterC -c %s
6+
7+
static assert(size_t.sizeof == 2);
8+
static assert(ptrdiff_t.sizeof == 2);
9+
10+
int testBoundsCheck(int[] arr)
11+
{
12+
return arr[1];
13+
}
14+
15+
// __LINE__ expressions are of type int
16+
void takeIntLine(int line = __LINE__) {}
17+
// special case for 8/16 bit targets: __LINE__ magically cast to size_t
18+
void takeSizeTLine(size_t line = __LINE__) {}
19+
20+
void testDefaultLineArgs()
21+
{
22+
// CHECK: call {{.*}}takeIntLine{{.*}}(i32 [[@LINE+1]])
23+
takeIntLine();
24+
// CHECK: call {{.*}}takeSizeTLine{{.*}}(i16 zeroext [[@LINE+1]])
25+
takeSizeTLine();
26+
}

0 commit comments

Comments
 (0)