// The x86-64 model machine: for every problem that has assembly, on every machine, the hardware model runs, the // machine presets reach it, and the pipeline, micro-op, out-of-order and encoding data hold together. The app's // board, which decides what the course views are given, is tested in pplus-web. import test from 'node:test'; import assert from 'node:assert/strict'; import { stageOccupancy, STAGES } from '../dist/engines/asm86/pipeline.js'; import { coreAt } from '../dist/engines/asm86/ooo.js'; import { decompose } from '../dist/engines/asm86/uops.js'; import fs from 'node:fs'; import { stripHeader, buildListing } from '../scripts/mojo/listings.mjs'; import { forSimulator, simulatorSetup } from '../dist/mojo/att.js'; import { listingFunctions } from '../dist/services/godbolt.js'; import { hardwareFor, resolveMachine } from '../dist/engines/asm86/machine.js'; import { viewNames } from '../dist/engines/asm86/views.js'; import { SERVICES } from '../dist/services/index.js'; import { MACHINE_PRESETS, parseMachine, machineName, machineSpec, machineLabel } from '../dist/common/machine.js'; import { Workspace } from '../dist/common/workspace.js'; import { PROBLEMS, problemById, workForProblem } from '../dist/common/problems.js'; /** Every catalog problem with an x86-64 pane, run as the Hardware area runs it. */ const WITH_ASM = PROBLEMS.filter((p) => (p.sources?.asm ?? '').trim()); const traceOf = (p, machine) => { const st = workForProblem(p); if (machine) st.machine = machine; st.lang = 'asm'; const s = new Workspace(st); const tr = s.trace(); assert.ok(tr && tr.lang === 'asm', `${p.id}: the x86-64 pane runs`); return { session: s, tr, raw: tr.raw.trace }; }; test('the hardware model runs on every problem with assembly, on both machines, with and without virtual memory', () => { for (const p of WITH_ASM) for (const machine of ['teaching', 'realistic', 'teaching+paging', 'realistic+paging']) { const { raw, tr } = traceOf(p, machine); const hw = hardwareFor(raw); assert.doesNotThrow(() => { hw.memory(); hw.pipeline(); hw.core(); }, `${p.id} on ${machine}`); const perf = tr.raw.hardware().perf(); assert.equal(perf.instructions, raw.stats.instructions, `${p.id} on ${machine}: the counters read the same run`); for (const t of [0, 1, Math.floor(raw.count / 2), raw.count]) assert.ok(hw.memory().stateAt(t).h, `${p.id}: caches at step ${t}`); } }); test('a machine is a preset with virtual memory on or off, and the run carries it to the hardware model', () => { assert.deepEqual(MACHINE_PRESETS.slice(0, 2), ['teaching', 'realistic']); for (const preset of MACHINE_PRESETS) for (const paging of [false, true]) { const name = machineName({ preset, paging }); assert.deepEqual(parseMachine(name), { preset, paging }, name); assert.equal(resolveMachine(machineSpec(name)).paging.enabled, paging, `${name}: paging`); assert.match(machineLabel(name), new RegExp(`^${preset}`)); } assert.deepEqual(parseMachine(undefined), { preset: 'teaching', paging: false }); assert.deepEqual(parseMachine('warp'), { preset: 'teaching', paging: false }, 'an unknown machine is the teaching one'); const p = PROBLEMS.find((x) => x.id === 'asm-two-pointers'); const { session, tr, raw } = traceOf(p, 'realistic+paging'); assert.equal(tr.raw.machine, 'realistic+paging'); const hw = hardwareFor(raw); assert.equal(hw.config.caches.l1d.line, 64, 'the views see the realistic machine'); assert.ok(hw.config.paging.enabled, 'and its virtual memory'); assert.ok(hw.memory().steps.some((r) => r.translations.length), 'addresses are translated'); session.state.machine = 'teaching'; assert.notEqual(session.trace(), tr, 'a new machine is a new run'); assert.equal(hardwareFor(session.trace().raw.trace).config.paging.enabled, false); }); test('time: the pipeline, micro-op, out-of-order and encoding data the Pipeline and Whole chip presets draw', () => { for (const p of WITH_ASM) for (const machine of ['teaching', 'realistic']) { const { raw } = traceOf(p, machine); const hw = hardwareFor(raw); const pipe = hw.pipeline(); const core = hw.core(); assert.equal(pipe.rows.length >= raw.count, true, `${p.id}: a pipeline row per instruction run (and any wrong-path fetches)`); for (const r of pipe.rows.slice(0, 200)) { for (const st of STAGES) assert.equal(stageOccupancy(pipe, r.E[st])[st] != null, true, `${p.id}: ${st} is occupied when a row enters it`); } for (let c = 0; c <= core.cycles; c += Math.max(1, Math.floor(core.cycles / 50))) { const at = coreAt(core, c); assert.ok(at.rob.length <= hw.config.core.rob, `${p.id} cycle ${c}: the reorder buffer never holds more than it has room for`); } for (const ins of raw.program.instrs) { const u = decompose(ins); assert.ok(Array.isArray(u) && u.length >= 1, `${p.id}: ${ins.text} splits into micro-ops`); } // the encoding view and the listing's bytes need the program laid out in memory if (raw.layout?.ok) for (const ins of raw.program.instrs) assert.ok(raw.layout.len[ins.index] >= 1 && raw.layout.len[ins.index] <= 15, `${p.id}: ${ins.text} is 1 to 15 bytes`); } assert.ok(WITH_ASM.some((p) => traceOf(p).raw.layout?.ok), 'the problems encode'); });