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

143 lines
5.8 KiB
C

/* M6 runtime gate driver.
*
* Exercises the linked M6 runtime surface through the shared shim:
* - printf("%d %s %f %lld", ...) with a negative int, a string, a GSP double
* with a fraction, and a negative 64-bit value;
* - the M5 integer formats/flags/width/alternate forms;
* - i64/unsigned-i64 boundaries, %ld/%lu and a zero-padded %lld;
* - %f sign/space/plus/zero/width/precision, star width/precision, and the
* documented half-up rounding of %.0f / %.1f;
* - sprintf return values (C: the full untruncated length), NUL
* termination, and safe truncation at the shim's SPRINTF_CAP;
* - memcpy and memmove, including an overlapping backward copy;
* - a double add and multiply that must call flib.lib (FD$ADD/FD$MUL) and be
* converted back to an int by the compiler's fptosi path (FD$DTOI).
*
* The shim writes printf output to the CONF_* block (0x01B00020 length,
* 0x01B10000 bytes); runtime-check.lua compares those bytes exactly with the
* expected text. Marker word n is runtime_markers[n], 0x20 bits apart, in the
* .bss array defined by runtime-entry.asm.
*/
typedef unsigned int u32;
extern volatile u32 runtime_markers[];
extern int printf(const char *fmt, ...);
extern int sprintf(char *buf, const char *fmt, ...);
extern void *memcpy(void *dst, const void *src, unsigned int n);
extern void *memmove(void *dst, const void *src, unsigned int n);
extern int memcmp(const void *a, const void *b, unsigned int n);
extern unsigned int strlen(const char *s);
/* A real rts.lib scalar entry (isascii.obj; no data/.cinit dependency). The
* 1990 `_div`/`_ldiv` wrappers also live in rts.lib but return div_t/ldiv_t
* with the 1990 sret form, not M6's 1987 hidden slot, so calling them would
* not be valid M6 ABI evidence. */
extern int rts_isascii(int c) __asm__("isascii");
void runtime_run(void)
{
char sbuf[256]; /* the shim's sprintf cap (SPRINTF_CAP) */
char lng[301];
char m1[8] = "abcdefg";
char m2[8];
char m3[8] = "1234567";
volatile double a = 1.5; /* volatile: keep FD$ADD/FD$MUL at -O2 */
volatile double b = 2.5;
double d, e;
int i, n;
/* 1. The required mixed conversion: int, string, GSP double, i64. */
runtime_markers[0] = (u32)printf("%d %s %f %lld\n",
-42, "hi", 3.25,
-1234567890123LL);
/* 2. M5 flags/width/alternate plus the new %s precision and width. */
printf("[%5d][%05d][%-5d][%+d][% d][%x][%#x][%o][%#o][%8.3s]\n",
42, -42, 7, 5, 5, 255, 255, 8, 8, "abcdef");
/* 2b. %c honours width/left; a bare %c is unchanged. */
printf("c=[%3c][%-3c][%c]\n", 'A', 'B', 'C');
/* 3. i64/unsigned-i64 boundaries, 32-bit long, zero-padded i64. */
printf("%lld %lld %llu %llu %lld %lu [%020lld]\n",
9223372036854775807LL, (-9223372036854775807LL - 1),
0xFFFFFFFFFFFFFFFFULL, 0ULL, -1LL, 0xFFFFFFFFUL, -5LL);
/* 3b. %ld, 64-bit hex/octal, integer precision and a negative `*`
* width (left-justify with the magnitude of the width). */
printf("int=[%ld][%llx][%llo][%.5d][%*d]\n",
-123456789L, 0x123456789ABCDEF0ULL,
0x123456789ABCDEF0ULL, 42, -6, 42);
/* 4. %f sign/flags/width/precision; %.0f and %.1f round half-up. */
printf("%f %.2f %.0f %.1f %10.2f %-10.2f %+f\n",
-3.25, -3.25, 2.5, 1.25, 3.25, 3.25, 3.25);
/* 5. Zero, zero-padded, star width/precision, space and '#' flags;
* %.9f proves the documented 6-digit precision cap. */
printf("%f|%08.2f|%.2f|%*.*f|% f|%#.0f|%.9f\n",
0.0, 3.25, -0.0, 7, 2, 3.25, 3.25, 2.5, 3.25);
/* 5b. The documented i32-range cap: an out-of-range magnitude is clamped
* instead of taking FD$DTOI's error path. */
printf("cap=%f\n", 3000000000.0);
/* 5c. C precision edge cases: the octal '#' must not add a second
* leading zero when precision already supplies one, and zero with
* precision 0 produces no digits (except `%#.0o`/`%#o`, which keep
* one). */
printf("prec=[%#.3o][%.0d][%.0i][%.0u][%.0x][%.0X][%#.0o][%#o][%5.0d]\n",
8, 0, 0, 0, 0, 0, 0, 0, 0);
/* 5d. Rounding carry through the fraction into the integer part
* (9.9999999 -> 10.000000) and the negative magnitude clamp. */
printf("round=%f ncap=%f\n", 9.9999999, -3000000000.0);
/* sprintf into the caller buffer and print it; the return is the
* C number of characters written (no NUL). */
n = sprintf(sbuf, "%d|%s|%.1f", 7, "abc", 2.5);
runtime_markers[1] = (u32)n;
runtime_markers[11] = (u32)printf("S[%s] (%d)\n", sbuf, n);
/* sprintf return value at the 64-bit boundary (19 digits). */
runtime_markers[2] = (u32)sprintf(sbuf, "%lld",
1234567890123456789LL);
/* sprintf truncation: 300 characters into the 256-byte shim cap. */
for (i = 0; i < 300; i++)
lng[i] = 'a';
lng[300] = 0;
n = sprintf(sbuf, "%s", lng);
runtime_markers[3] = (u32)n;
runtime_markers[4] = strlen(sbuf);
runtime_markers[5] = (u32)(sbuf[255] == 0);
printf("T(%d,%u,%d)\n", n, strlen(sbuf), sbuf[255] == 0);
/* memcpy; then memmove in both directions (overlapping ranges). */
memcpy(m2, m1, 8);
runtime_markers[8] = (u32)(memcmp(m2, m1, 8) == 0);
printf("mem=%s %s\n", m2, m1);
memmove(m1 + 1, m1, 6);
runtime_markers[9] = (u32)(memcmp(m1, "aabcdef", 8) == 0);
printf("mov=%s\n", m1);
memmove(m3, m3 + 2, 4);
runtime_markers[10] = (u32)(memcmp(m3, "3456567", 8) == 0);
printf("mov2=%s\n", m3);
/* rts.lib is linked for real: _isascii is a scalar entry with the TI ABI
* (the 1990 _div/_ldiv wrappers are sret-form and are not called). */
i = (rts_isascii('A') != 0) && (rts_isascii(0xC8) == 0);
runtime_markers[12] = (u32)i;
printf("rts=%d\n", i);
/* flib.lib double arithmetic, formatted back through %f. The volatile
* operands keep the add/multiply from folding at -O2, so both levels
* link and call the real FD$ entries. */
d = a + b;
e = a * b;
runtime_markers[6] = (u32)(int)d;
runtime_markers[7] = (u32)(int)e;
printf("flib %f %f\n", d, e);
}