Files
pplus-core/tests/asm-coverage.test.mjs
alexm 401084bab3
Test / gate (push) Failing after 19m0s
Initial commit
2026-11-12 22:23:54 -05:00

70 lines
4.5 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// The assembly census (scripts/asm-census.mjs) as a ratchet: every function the
// real compiler produced for the user's own code assembles on the model machine,
// the library functions that assemble never get fewer, and the instructions
// outside the model's set are only the 256-bit AVX2 ones the model says it
// does not have. A new corpus item that brings a new instruction fails here,
// which is the point.
import test from 'node:test';
import assert from 'node:assert/strict';
import { census } from '../scripts/asm-census.mjs';
import { parseAsm, runAsmProgram } from '../dist/services/asm.js';
import { intelLine } from '../dist/mojo/att.js';
const LIBRARY_AT_LEAST = 438; // raise as the machine learns more
const OUTSIDE = new Set(['vextracti128', 'vinserti128', 'vpblendvb', 'vperm2i128', 'vpalignr', 'vptest']); // ymm: 256-bit AVX2
test('every user function in the corpus listings assembles on the model machine', () => {
const c = census();
assert.deepEqual(c.userFailures, []);
assert.equal(c.userOk, c.user);
assert.ok(c.user >= 7, `only ${c.user} user functions in the corpus`);
});
test('the library functions that assemble do not get fewer, and what is outside the set is only AVX2', () => {
const c = census();
assert.ok(c.libOk >= LIBRARY_AT_LEAST, `${c.libOk} library functions assemble, fewer than ${LIBRARY_AT_LEAST}`);
const unexpected = c.missing.map(([op]) => op).filter((op) => !OUTSIDE.has(op));
assert.deepEqual(unexpected, [], 'instructions the corpus uses that the model neither has nor lists as outside it');
});
test('the AT&T translator strips the size suffix from the BMI and atomic mnemonics and keeps lock', () => {
const map = new Map();
assert.equal(intelLine('andnq %rax, %r9, %rax', map), 'andn rax, r9, rax');
assert.equal(intelLine('mulxq %rcx, %rdx, %rdx', map), 'mulx rdx, rdx, rcx');
assert.equal(intelLine('lock xaddq %rax, (%rcx)', map), 'lock xadd qword ptr [rcx], rax');
assert.equal(intelLine('shlxq %rcx, %rbx, %r11', map), 'shlx r11, rbx, rcx');
assert.equal(intelLine('vpcmpeqb %xmm1, %xmm0, %xmm3', map), 'vpcmpeqb xmm3, xmm0, xmm1');
});
test('the instructions compiled Mojo uses compute what the manual says', () => {
const run = (src, setup = '') => { const p = parseAsm(src); assert.ok(p.ok, p.diagnostics.map((d) => d.message).join('; ')); return runAsmProgram(p, { setup, machine: 'teaching', maxSteps: 200 }); };
const st = run([
'mov rdx, 6', 'mov rcx, 7', 'mulx r8, r9, rcx',
'mov rax, 0xff00', 'mov rbx, 0x0ff0', 'andn r10, rax, rbx',
'mov rcx, 3', 'shlx r11, rbx, rcx', 'sarx r12, rax, rcx', 'bzhi r13, rbx, rcx',
'mov rsi, 12', 'blsr rdi, rsi', 'blsi r14, rsi', 'blsmsk r15, rsi', 'rorx rbx, rsi, 1',
'mov rax, 1', 'mov qword ptr [0x1000], 10', 'lock xadd qword ptr [0x1000], rax',
'mov rax, 11', 'mov rcx, 99', 'lock cmpxchg qword ptr [0x1000], rcx',
'bts rdx, 4', 'mov eax, 0x12345678', 'bswap eax',
].join('\n'), 'memory 0x1000 "........"');
const s = st.states[st.count];
assert.equal(st.error, null);
assert.equal(s.regs.r9, 42n); assert.equal(s.regs.r8, 0n);
assert.equal(s.regs.r10, 0xf0n); assert.equal(s.regs.r11, 0x7f80n); assert.equal(s.regs.r12, 0x1fe0n); assert.equal(s.regs.r13, 0n);
assert.equal(s.regs.rdi, 8n); assert.equal(s.regs.r14, 4n); assert.equal(s.regs.r15, 7n); assert.equal(s.regs.rbx, 6n);
assert.equal(s.regs.rdx, 22n); assert.equal(s.regs.rax, 0x78563412n); assert.equal(s.flags.ZF, true);
assert.equal(s.mem.find((m) => m.addr === 0x1000).bytes[0], 99, 'cmpxchg stored the new value');
// vectors: a broadcast compare finds every A in GATTACAGATTACA
const v = run(['vmovdqu xmm1, xmmword ptr [0x1000]', 'mov eax, 0x41', 'vmovd xmm4, eax', 'vpbroadcastb xmm4, xmm4', 'vpcmpeqb xmm3, xmm1, xmm4', 'vpmovmskb r8d, xmm3', 'vpextrb eax, xmm1, 1', 'vpshufd xmm5, xmm1, 0x1b'].join('\n'), 'memory 0x1000 "GATTACAGATTACA.."');
const vs = v.states[v.count];
assert.equal(vs.regs.r8, 0b10100101010010n); assert.equal(vs.regs.rax, 0x41n);
// ud2 stops the run and says why
const u = run('mov rax, 1\nud2');
assert.match(u.error.message, /invalid-opcode/);
// a stub for the standard library stops the run and names the function
const stub = run('call std_io_io_print\nstd_io_io_print:\n ud2 ; std_io_io_print: standard library, not on the model machine');
assert.match(stub.error.message, /std_io_io_print: standard library, not on the model machine/);
});