M4: SplitBit assembles SplitBit, and then assembles itself
> load Asm.sbx
> run cosmos.asm
wrote cosmos.bin: program 7036, data 2448, labels 475
> run Asm.asm
wrote Asm.sbx: program 7533, data 4099, labels 555
Both byte for byte identical to what the host assembler builds from the
same source. The machine now builds the operating system it is running on,
and builds the thing that built it.
THE CHECK THAT MATTERS MOST IS THE THIRD ONE. A binary that matches could
still have come from an assembler wrong in some way this particular source
happens not to exercise. So Tests/native.sh boots the CosmOS that CosmOS
built and has THAT assemble CosmOS again - and the second generation is
identical to the first, down to the cycle count. It is a fixed point: the
machinery has been through itself. After this the host is a convenience
rather than a necessity.
WHAT STOOD IN THE WAY was not the assembler. It loaded, faulted at 7,780
cycles, and the fault was in CosmOS: a loaded program is staged at 0x8000
before being blitted into place, so the whole FILE has to fit in the 32,768
bytes above it. The assembler's file was 33,983, and 22K of that was
zeroed scratch buffers - because #Reserve emits what it reserves.
None of that is initialised data. It is scratch, wanted only while the
assembler runs, and while it runs everything above its own data is free.
So the buffers are a MAP now rather than declarations - Assembler/scratch.asm
writes down six addresses and the file carries none of it. 33,983 bytes
became 11,648, and the assembler could load itself.
The map has a file of its own because the reader and the label table both
need addresses out of it while neither includes the other.
The sizes are cut to the largest thing it is asked to build, and that turns
out not to be the operating system: the assembler is 555 labels and 11,648
bytes of output against CosmOS's 475 and 9,564. The hardest thing this
assembles is itself.
Also: sizing it for CosmOS meant raising the label table, and raising the
label table is what pushed the file over the staging limit. The two facts
only met because the first one was tried.
Speed, measured rather than guessed: CosmOS takes 80,168,646 cycles, which
is eighty seconds of emulated time and under a second under --fast. Most of
it is a straight walk of 475 label names, several thousand times. Sorting
or bucketing that is easy and was deliberately not written before there was
something to measure.
make run-cosmos now puts every source file on the disk, so the whole thing
can be done rather than read about.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
This commit is contained in:
co-authored by
Claude Opus 5
parent
20989c3439
commit
fb335681d2
@@ -117,6 +117,56 @@ for app in $APPS; do
|
||||
check "$app byte for byte" cmp -s "$WORK/got-$app.sbx" "$WORK/ref-$app.sbx"
|
||||
done
|
||||
|
||||
# ---- Self hosting ----
|
||||
#
|
||||
# The two things that make this more than a demonstration: the machine assembling the
|
||||
# operating system it is running on, and the assembler assembling itself. Both have to come
|
||||
# out byte for byte identical to what the host tool builds from the same source.
|
||||
#
|
||||
# THE FIXED POINT IS THE THIRD CHECK AND THE ONE THAT MATTERS MOST. A binary that matches
|
||||
# could still have been built by an assembler that is wrong in some way the source happens
|
||||
# not to exercise; a binary that BUILDS ITSELF AGAIN and comes out the same has been through
|
||||
# its own machinery. The host is no longer necessary after this point - it is a convenience.
|
||||
|
||||
SOURCES="cosmos console sbfs services text"
|
||||
PARTS="Asm numbers source token classify labels vectors table"
|
||||
|
||||
"$TOOL" format "$WORK/self.img" 4096 8 >/dev/null
|
||||
for f in $SOURCES; do
|
||||
"$TOOL" put "$WORK/self.img" "$ROOT/Programs/CosmOS/Source/$f.asm" "$f.asm" >/dev/null
|
||||
done
|
||||
for f in $PARTS; do
|
||||
"$TOOL" put "$WORK/self.img" "$ROOT/Programs/CosmOS/Assembler/$f.asm" "$f.asm" >/dev/null
|
||||
done
|
||||
"$TOOL" put "$WORK/self.img" "$WORK/Asm.sbx" Asm.sbx >/dev/null
|
||||
|
||||
printf 'load Asm.sbx\nrun cosmos.asm\nrun Asm.asm\nexit\n' \
|
||||
| "$EMU" --fast --cycles 4000000000 -D "$WORK/self.img" "$WORK/cosmos.bin" \
|
||||
> "$WORK/self.txt" 2>&1
|
||||
|
||||
rm -f "$WORK/built-cosmos.bin" "$WORK/built-Asm.sbx"
|
||||
"$TOOL" get "$WORK/self.img" cosmos.bin "$WORK/built-cosmos.bin" >/dev/null 2>&1
|
||||
"$TOOL" get "$WORK/self.img" Asm.sbx "$WORK/built-Asm.sbx" >/dev/null 2>&1
|
||||
|
||||
REPORT="$(grep -o 'program 7036, data 2448, labels [0-9]*' "$WORK/self.txt" || true)"
|
||||
check "CosmOS builds CosmOS" cmp -s "$WORK/built-cosmos.bin" "$WORK/cosmos.bin"
|
||||
REPORT="$(grep -o 'program 7533, data [0-9]*, labels [0-9]*' "$WORK/self.txt" || true)"
|
||||
check "and builds itself" cmp -s "$WORK/built-Asm.sbx" "$WORK/Asm.sbx"
|
||||
|
||||
# ---- And the second generation is the first ----
|
||||
#
|
||||
# Boot the CosmOS that CosmOS built, and have that assemble CosmOS again.
|
||||
if [ -s "$WORK/built-cosmos.bin" ]; then
|
||||
"$TOOL" delete "$WORK/self.img" cosmos.bin >/dev/null 2>&1
|
||||
printf 'load Asm.sbx\nrun cosmos.asm\nexit\n' \
|
||||
| "$EMU" --fast --cycles 4000000000 -D "$WORK/self.img" "$WORK/built-cosmos.bin" \
|
||||
> "$WORK/second.txt" 2>&1
|
||||
rm -f "$WORK/second-cosmos.bin"
|
||||
"$TOOL" get "$WORK/self.img" cosmos.bin "$WORK/second-cosmos.bin" >/dev/null 2>&1
|
||||
fi
|
||||
REPORT="a fixed point"
|
||||
check "second generation" cmp -s "$WORK/second-cosmos.bin" "$WORK/built-cosmos.bin"
|
||||
|
||||
echo
|
||||
if [ "$FAIL" -eq 0 ]; then
|
||||
echo "All $PASS native assembler checks passed."
|
||||
@@ -125,5 +175,10 @@ else
|
||||
echo
|
||||
echo "The session was:"
|
||||
sed 's/^/ /' "$WORK/session.txt"
|
||||
if [ -f "$WORK/self.txt" ]; then
|
||||
echo
|
||||
echo "And the self hosting run:"
|
||||
sed 's/^/ /' "$WORK/self.txt"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user