// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // The setup beside an x86-64 pane, where the listing's read-only data is written. A function sent to the pane from // a compiled program brings the data it addresses; one table of source locations is half a megabyte, and written // as `bytes 0x70 0x6b ...` it made a setup larger than the site will open from a link. A long block is quoted text // with byte escapes now, and this holds the reader and the writer to each other. import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { parseAsm, parseSetup, runAsmProgram } from '../dist/services/asm.js'; import { forSimulator, simulatorSetup, LONG_BLOCK } from '../dist/mojo/att.js'; import { listingFunctions } from '../dist/services/godbolt.js'; const ROOT = new URL('..', import.meta.url).pathname; const bytesOf = (m) => (m.text != null ? [...m.text].map((c) => c.charCodeAt(0) & 0xff) : m.bytes); test('quoted text in a setup takes byte escapes, and a comment sign inside it is text', () => { const s = parseSetup('memory 0x1000 "a\\x00b\\0c\\xff\\"q\\\\ # not a comment ; nor this // nor this" # a comment\nstdin "x\\n\\ty" ; another\nrdi = 0x1000 // a third'); assert.deepEqual(bytesOf(s.memory[0]), [...'a\0b\0cÿ"q\\ # not a comment ; nor this // nor this'].map((c) => c.charCodeAt(0))); assert.equal(s.stdin, 'x\n\ty'); assert.equal(s.registers.rdi, 0x1000n); // text that never closes, or closes early, is an error on its line and not a guess for (const bad of ['memory 0x1000 "abc', 'memory 0x1000 "a"b"', 'memory 0x1000 "abc\\"']) assert.throws(() => parseSetup(bad), /setup line 1: expected text in double quotes/, bad); // a block of every byte value survives being written long and read back (the longest a region may be is 1 MiB) const all = Array.from({ length: 4096 }, (_, i) => (i * 7 + (i >> 8)) & 0xff); const quoted = `"${all.map((b) => (b === 0x22 ? '\\"' : b === 0x5c ? '\\\\' : b >= 32 && b < 127 ? String.fromCharCode(b) : `\\x${b.toString(16).padStart(2, '0')}`)).join('')}"`; assert.deepEqual(bytesOf(parseSetup(`memory 0x2000 ${quoted}`).memory[0]), all); }); test('a function sent to the pane from demo\'s compiled listing brings its data in a setup a link can hold', () => { const status = JSON.parse(fs.readFileSync(path.join(ROOT, 'library/mojo/golden/status.json'), 'utf8')); const item = status.items.find((x) => x.id === 'local/demo'); const golden = JSON.parse(fs.readFileSync(path.join(ROOT, 'library/mojo/golden', `${item.key}.json`), 'utf8')); const fns = listingFunctions(golden.result.asm); let largest = 0; for (const name of ['cell', 'Grid::__init__', 'align', 'main']) { const fn = fns.find((f) => f.kind === 'user' && f.name.startsWith(name)); assert.ok(fn, `demo has no function ${name}`); const { source, data } = forSimulator(fns, fn); const setup = simulatorSetup(fn, data); const parsed = parseSetup(setup); const held = parsed.memory.reduce((n, m) => n + bytesOf(m).length, 0); largest = Math.max(largest, held); // about a character a byte for the long blocks: the whole setup stays under twice the bytes it places, plus its short lines assert.ok(setup.length < 2 * held + 20000, `${name}: a setup of ${setup.length} characters for ${held} bytes of data`); assert.ok(setup.length < 1024 * 1024, `${name}: a setup of ${setup.length} characters is more than half of what a link may hold`); // every block lands where the code addresses it, with no two overlapping const spans = parsed.memory.map((m) => [m.addr, m.addr + bytesOf(m).length]).sort((a, b) => a[0] - b[0]); for (let i = 1; i < spans.length; i++) assert.ok(spans[i][0] >= spans[i - 1][1], `${name}: two blocks overlap at 0x${spans[i][0].toString(16)}`); // and the pane still assembles and runs with it (a large function stops on the model's small stack, which is an answer) const program = parseAsm(source); assert.ok(program.ok, `${name}: ${JSON.stringify(program.diagnostics?.slice(0, 2))}`); const tr = runAsmProgram(program, { setup }); assert.ok(tr.count >= 1, `${name}: no step`); } assert.ok(largest > 400000, `the large table is still part of the test (${largest} bytes)`); assert.ok(LONG_BLOCK >= 16 && LONG_BLOCK <= 4096); });