Files
arti e0cf441118 m6: apply the final-review fixes
- spec 4.2/7.4: the caller-allocated hidden return slot (no sret
  pointer), with the 1987 _mkbig/_callbig oracle recorded as binding;
  spec 8 annotates debug info as M7.
- README: qualify the complete-surface headline, name the sub-byte
  stride in the M7 ownership lists, and flag the default -O2 over-scale.
- shim: %c honours width and the '-' flag; a bare %c is unchanged.
- runtime gate: add %c width output and call rts.lib _isascii, checking
  it in the GSPLNK map (587 bytes, 13 markers).
- aggregate gate: return a packed bitfield struct by value and check
  its fields, sign extension and raw word (49 markers).
- tms-clang: default TMS_CLANG/TMS_OPT to build/llvm-clean-build/bin
  and fail clearly when a tool is missing; update the README example.
2026-11-14 09:46:52 +00:00

352 lines
8.9 KiB
C

/* M6 aggregate gate driver.
*
* Calls the hand-written ABI functions in m6-aggregate.ll through the TI
* aggregate rules and writes result words to the marker array defined by the
* entry assembly. The .ll callees are *not* compiled by Clang, so the driver
* pins the caller side against independently written lowering; the C
* functions below then exercise the compiler's own callee side (and its
* caller side calling itself).
*
* The ABI under test (1987 `_mkbig`/`_callbig`):
* - the caller reserves an alignTo(size, 2)-byte slot at the top of its
* frame before saving STK, pushes the arguments, calls;
* - the callee copies the object bytes to saved_STK-size (the object sits
* at the top of the rounded slot) and ends with LOADSTK + RETS 2;
* - the caller copies the bytes out and releases the slot;
* - struct/union arguments are still byval copies in the argument area;
* - no sret pointer argument and no A8 aggregate return.
*
* Marker word n lives at aggregate_markers[n], i.e. 0x20 bits above the
* previous one; scripts/m6/aggregate-check.lua reads them by bit address from
* the linker map.
*/
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
typedef unsigned long long u64;
struct six {
int a;
short b;
};
struct five {
int a;
char b;
};
/* A packed bitfield struct returned by value: the Task 5 (bitfields) / Task 6
* (aggregate return) seam. The 12 field bits fit one 16-bit word, so the
* hidden slot is 2 bytes and the callee stores the packed bits directly. */
struct bits {
unsigned int a : 3;
unsigned int b : 5;
int c : 4;
};
struct big {
long a, b, c, d, e;
};
struct pair {
long a, b;
};
struct inner {
long a;
short b;
};
struct outer {
struct inner i;
long c;
};
union uni {
long l;
char c[6];
};
struct mixed {
long a;
long long b;
double d;
float f;
char c;
};
struct six_guard {
struct six s;
unsigned int guard;
};
struct five_guard {
struct five f;
unsigned char guard;
};
/* Layout contract: TI packs records, so a 5-byte object is possible and the
* 6-byte slot rounds it up by one byte. */
_Static_assert(sizeof(struct six) == 6, "six");
_Static_assert(__builtin_offsetof(struct six, b) == 4, "six.b");
_Static_assert(sizeof(struct bits) == 2, "bits");
_Static_assert(sizeof(struct five) == 5, "five");
_Static_assert(__builtin_offsetof(struct five, b) == 4, "five.b");
_Static_assert(sizeof(struct big) == 20, "big");
_Static_assert(sizeof(struct pair) == 8, "pair");
_Static_assert(sizeof(struct inner) == 6, "inner");
_Static_assert(sizeof(struct outer) == 10, "outer");
_Static_assert(__builtin_offsetof(struct outer, c) == 6, "outer.c");
_Static_assert(sizeof(union uni) == 6, "uni");
_Static_assert(sizeof(struct mixed) == 25, "mixed");
_Static_assert(__builtin_offsetof(struct mixed, b) == 4, "mixed.b");
_Static_assert(__builtin_offsetof(struct mixed, d) == 12, "mixed.d");
_Static_assert(__builtin_offsetof(struct mixed, f) == 20, "mixed.f");
_Static_assert(__builtin_offsetof(struct mixed, c) == 24, "mixed.c");
extern volatile u32 aggregate_markers[];
/* Callees defined in m6-aggregate.ll. */
extern struct six mk_six(int, int);
extern struct five mk_five(int, int);
extern struct big mk_big(int);
extern struct pair mk_pair(int, int);
extern struct outer mk_outer(int);
extern union uni mk_uni(int);
extern int sum_six(struct six, int);
extern int sum_big(struct big);
extern int sum_uni(union uni);
/* Keep raw object copies in .bss so the exact bytes can be inspected. */
static volatile struct six six_raw;
static volatile struct five five_raw;
static volatile struct big big_raw;
static volatile union uni uni_raw;
static volatile struct mixed mixed_raw;
static volatile struct six_guard six_guard_slot;
static volatile struct five_guard five_guard_slot;
static volatile struct bits bits_raw;
static void mark(u32 n, u32 v) { aggregate_markers[n] = v; }
/* --- C-side aggregate callees (the compiler's callee lowering) ----------- */
static __attribute__((noinline)) struct six c_mksix(int a, int b)
{
struct six s;
s.a = a;
s.b = (short)b;
return s;
}
static __attribute__((noinline)) int c_usesix(int x)
{
struct six s = c_mksix(x, 7);
return s.a * 3 + s.b + x;
}
/* The bitfield-struct return: fields come from x so the callee cannot fold
* the result, and the caller reads them back through the returned object.
* c uses the low bits only: a (x >> 8) & mask source would exercise an
* unrelated generic load-narrowing path on this bit-addressed target. */
static __attribute__((noinline)) struct bits c_mkbits(int x)
{
struct bits v;
v.a = x & 7;
v.b = (x >> 3) & 31;
v.c = -(x & 7);
return v;
}
static __attribute__((noinline)) struct big c_mkbig(int x)
{
struct big b;
b.a = x;
b.b = x + 1;
b.c = x + 2;
b.d = x + 3;
b.e = x + 4;
return b;
}
static __attribute__((noinline)) int c_usebig(int x)
{
struct big b = c_mkbig(x);
return b.a + b.e;
}
static __attribute__((noinline)) struct outer c_mkouter(int x)
{
struct outer o;
o.i.a = x;
o.i.b = 2;
o.c = x + 3;
return o;
}
static __attribute__((noinline)) int c_useouter(int x)
{
struct outer o = c_mkouter(x);
return o.i.a + o.i.b + o.c;
}
static __attribute__((noinline)) union uni c_mkuni(int x)
{
union uni u;
u.l = x;
return u;
}
static __attribute__((noinline)) int c_useuni(int x)
{
union uni u = c_mkuni(x);
return u.l;
}
static __attribute__((noinline)) struct mixed c_mkmixed(int x)
{
struct mixed m;
m.a = x;
m.b = x;
m.d = 1.5;
m.f = 2.5f;
m.c = 7;
return m;
}
static __attribute__((noinline)) int c_usemixed(int x)
{
struct mixed m = c_mkmixed(x);
return (long)m.a + (long)m.b + (long)m.d + (long)m.f + (long)m.c;
}
static __attribute__((noinline)) int c_sumpair(struct pair p) { return p.a + p.b; }
static __attribute__((noinline)) int c_sumsix(struct six s) { return s.a + s.b; }
static __attribute__((noinline)) int c_sumuni(union uni u) { return u.l + u.c[5]; }
void aggregate_run(void)
{
struct six s6;
struct five f5;
struct big b5;
struct pair p2;
struct outer o;
union uni u;
volatile u32 *w;
volatile u8 *pb;
/* The .ll 6-byte struct return and its raw memory image. */
s6 = mk_six(0x11223344, 0x5566);
mark(0, (u32)s6.a);
mark(1, (u16)s6.b);
six_raw = s6;
w = (volatile u32 *)&six_raw;
mark(2, w[0]);
pb = (volatile u8 *)&six_raw;
mark(3, pb[4] | ((u32)pb[5] << 8));
/* A 6-byte object must not overwrite the next object. */
six_guard_slot.guard = 0x600D600D;
six_guard_slot.s = mk_six(0x11223344, 0x5566);
mark(4, six_guard_slot.guard);
/* The 5-byte object in a 6-byte slot; the guard proves the copy is exact
* (the slot's pad byte is not written into the caller's object). */
f5 = mk_five(0xAABBCCDD, 0x5A);
mark(5, (u32)f5.a);
mark(6, (u8)f5.b);
five_raw = f5;
w = (volatile u32 *)&five_raw;
mark(7, w[0]);
pb = (volatile u8 *)&five_raw;
mark(8, pb[4]);
five_guard_slot.guard = 0xA5;
five_guard_slot.f = mk_five(0x01020304, 0x7E);
mark(9, five_guard_slot.guard);
/* The 5-word / 160-bit case. */
b5 = mk_big(0x10);
mark(10, (u32)b5.a);
mark(11, (u32)b5.b);
mark(12, (u32)b5.c);
mark(13, (u32)b5.d);
mark(14, (u32)b5.e);
big_raw = b5;
w = (volatile u32 *)&big_raw;
mark(15, w[0]);
mark(16, w[1]);
mark(17, w[2]);
mark(18, w[3]);
mark(19, w[4]);
/* The 2-word case (1987 `_mkpair`). */
p2 = mk_pair(0xAAAA5555, 0x12345678);
mark(20, (u32)p2.a);
mark(21, (u32)p2.b);
/* Nested struct: flattened fields at 0, 4 and 6. */
o = mk_outer(0x100);
mark(22, (u32)o.i.a);
mark(23, (u32)o.i.b);
mark(24, (u32)o.c);
/* Union return and its raw low word. */
u = mk_uni(0xDEADBEEF);
mark(25, (u32)u.l);
uni_raw = u;
w = (volatile u32 *)&uni_raw;
mark(26, w[0]);
/* Struct/union by value as arguments: the callee receives a private copy
* in the argument area (the driver is the caller here). */
mark(27, (u32)sum_six(s6, 100));
mark(28, (u32)sum_big(b5));
mark(29, (u32)sum_uni(mk_uni(0x12345678)));
/* C-defined callees (the compiler's own callee lowering), called from C. */
mark(30, (u32)c_usesix(50));
mark(31, (u32)c_usebig(10));
mark(32, (u32)c_useouter(0x200));
mark(33, (u32)c_useuni(0x0BADF00D));
p2.a = 5;
p2.b = 6;
mark(34, (u32)c_sumpair(p2));
s6.a = 0x10;
s6.b = 0x20;
mark(35, (u32)c_sumsix(s6));
u.l = 0x11223344;
u.c[5] = 0x7E;
mark(36, (u32)c_sumuni(u));
/* The aggregate can mix i64, double and float members. */
mark(37, (u32)c_usemixed(100));
mixed_raw = c_mkmixed(100);
w = (volatile u32 *)&mixed_raw;
mark(38, w[0]);
mark(39, w[1]);
mark(40, w[2]);
mark(41, w[3]);
mark(42, w[4]);
mark(43, w[5]);
pb = (volatile u8 *)&mixed_raw;
mark(44, pb[24]);
/* Packed bitfield struct returned by value (Task 5/6 seam): fields, the
* signed field and the raw packed word. c_mkbits(0x1A5) is a=5, b=20,
* c=-5 in one 16-bit word. */
{
struct bits v = c_mkbits(0x1A5);
volatile u8 *bb;
mark(45, (u32)v.a | ((u32)v.b << 8) |
((u32)(v.c & 0xF) << 16));
mark(46, (u32)v.c);
bits_raw = v;
bb = (volatile u8 *)&bits_raw;
mark(47, (u32)bb[0] | ((u32)bb[1] << 8));
}
}