boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

commit ccb69f1c9fcd1e3b5222653b3ba3255ba05efc79
parent ffe72f13e339e8411d89985ad324f01a6483a4e6
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Fri, 24 Apr 2026 15:58:07 -0700

Restructure build into scripts/, vendor seeds, unify ARCH paths

* Move build helpers into scripts/: bootstrap.sh, build-p1.sh,
  build-p1pp.sh, build-native-tools.sh, lint.sh, run-tests.sh, plus
  Containerfile.busybox. Drop the per-suite m1pp/test.sh, m1pp/build*.sh
  and tests/p1/test.sh runners.
* Vendor stage0-posix seed sources into vendor/seed/<arch>/. The chain
  no longer needs a sibling live-bootstrap checkout; populate-upstream.sh
  is gone.
* Switch every podman invocation to one per-arch boot2-busybox:<arch>
  image (busybox:musl base, built from scripts/Containerfile.busybox).
  Drop the alpine pin and the obsolete amd64 PF_X->PF_R hex0-seed patch.
* Single unified test runner scripts/run-tests.sh with --suite/--arch;
  m1pp and p1 suites both fan out across all three arches.
* Every program (m1pp, pokem, hello) and every test path drives off a
  single ARCH= (default aarch64); the lone aarch64 specialisation left
  is the host-side tools-native target.
* Rename p1->P1, m1pp->M1pp, tests/{p1,m1pp}->tests/{P1,M1pp},
  p1pp.P1pp->P1pp.P1pp, m1pp.{c,P1}->M1pp.{c,P1}; py generators move
  into P1/gen/. Repo rebrand lispcc->boot2.
* Drop tests/{hello,demo}.M1 (pre-rename register names); the make hello
  showcase now points at tests/M1pp/00-hello.M1.

Verified: 60 m1pp + 15 p1 tests pass on aarch64/amd64/riscv64.

Diffstat:
DContainerfile.busybox | 7-------
Rm1pp/m1pp.P1 -> M1pp/M1pp.P1 | 0
Rm1pp/m1pp.c -> M1pp/M1pp.c | 0
MMakefile | 445+++++++++++++++++++++++++------------------------------------------------------
Rp1/P1-aarch64.M1pp -> P1/P1-aarch64.M1pp | 0
Rp1/P1-amd64.M1pp -> P1/P1-amd64.M1pp | 0
Rp1/P1-riscv64.M1pp -> P1/P1-riscv64.M1pp | 0
Rp1/P1.M1pp -> P1/P1.M1pp | 0
Rp1/p1pp.P1pp -> P1/P1pp.P1pp | 0
Rp1/gen/aarch64.py -> P1/gen/aarch64.py | 0
Rp1/gen/amd64.py -> P1/gen/amd64.py | 0
Rp1/gen/common.py -> P1/gen/common.py | 0
Rp1/llvm_disasm_aarch64.py -> P1/gen/llvm_disasm_aarch64.py | 0
Rp1/gen/p1_gen.py -> P1/gen/p1_gen.py | 0
Rp1/gen/riscv64.py -> P1/gen/riscv64.py | 0
MREADME.md | 8++++----
Dbootstrap.sh | 69---------------------------------------------------------------------
Mdocs/LISP-C.md | 2+-
Mdocs/LISP.md | 2+-
Mdocs/P1.md | 2+-
Dm1pp/build-native-tools.sh | 81-------------------------------------------------------------------------------
Dm1pp/build-p1.sh | 94-------------------------------------------------------------------------------
Dm1pp/build.sh | 137-------------------------------------------------------------------------------
Dm1pp/test.sh | 128-------------------------------------------------------------------------------
Dp1/aarch64.py | 413-------------------------------------------------------------------------------
Dp1/common.py | 66------------------------------------------------------------------
Dp1/p1_gen.py | 257-------------------------------------------------------------------------------
Dpopulate-upstream.sh | 52----------------------------------------------------
Mpost.md | 353+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Ascripts/Containerfile.busybox | 18++++++++++++++++++
Ascripts/bootstrap.sh | 39+++++++++++++++++++++++++++++++++++++++
Ascripts/build-native-tools.sh | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/build-p1.sh | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/build-p1pp.sh | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rlint.sh -> scripts/lint.sh | 0
Ascripts/run-tests.sh | 214+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rtests/m1pp/00-hello.M1 -> tests/M1pp/00-hello.M1 | 0
Rtests/m1pp/00-hello.expected -> tests/M1pp/00-hello.expected | 0
Rtests/m1pp/01-passthrough.M1pp -> tests/M1pp/01-passthrough.M1pp | 0
Rtests/m1pp/01-passthrough.expected -> tests/M1pp/01-passthrough.expected | 0
Rtests/m1pp/02-defs.M1pp -> tests/M1pp/02-defs.M1pp | 0
Rtests/m1pp/02-defs.expected -> tests/M1pp/02-defs.expected | 0
Rtests/m1pp/03-builtins.M1pp -> tests/M1pp/03-builtins.M1pp | 0
Rtests/m1pp/03-builtins.expected -> tests/M1pp/03-builtins.expected | 0
Rtests/m1pp/04-expr-ops.M1pp -> tests/M1pp/04-expr-ops.M1pp | 0
Rtests/m1pp/04-expr-ops.expected -> tests/M1pp/04-expr-ops.expected | 0
Rtests/m1pp/05-int-atoms.M1pp -> tests/M1pp/05-int-atoms.M1pp | 0
Rtests/m1pp/05-int-atoms.expected -> tests/M1pp/05-int-atoms.expected | 0
Rtests/m1pp/06-paste.M1pp -> tests/M1pp/06-paste.M1pp | 0
Rtests/m1pp/06-paste.expected -> tests/M1pp/06-paste.expected | 0
Rtests/m1pp/07-rescan.M1pp -> tests/M1pp/07-rescan.M1pp | 0
Rtests/m1pp/07-rescan.expected -> tests/M1pp/07-rescan.expected | 0
Rtests/m1pp/08-select.M1pp -> tests/M1pp/08-select.M1pp | 0
Rtests/m1pp/08-select.expected -> tests/M1pp/08-select.expected | 0
Rtests/m1pp/09-args.M1pp -> tests/M1pp/09-args.M1pp | 0
Rtests/m1pp/09-args.expected -> tests/M1pp/09-args.expected | 0
Rtests/m1pp/10-full-parity.M1pp -> tests/M1pp/10-full-parity.M1pp | 0
Rtests/m1pp/10-full-parity.expected -> tests/M1pp/10-full-parity.expected | 0
Rtests/m1pp/11-local-labels.M1pp -> tests/M1pp/11-local-labels.M1pp | 0
Rtests/m1pp/11-local-labels.expected -> tests/M1pp/11-local-labels.expected | 0
Rtests/m1pp/12-braced-args.M1pp -> tests/M1pp/12-braced-args.M1pp | 0
Rtests/m1pp/12-braced-args.expected -> tests/M1pp/12-braced-args.expected | 0
Rtests/m1pp/13-parenless-control.M1pp -> tests/M1pp/13-parenless-control.M1pp | 0
Rtests/m1pp/13-parenless-control.expected -> tests/M1pp/13-parenless-control.expected | 0
Rtests/m1pp/13-parenless.M1pp -> tests/M1pp/13-parenless.M1pp | 0
Rtests/m1pp/13-parenless.expected -> tests/M1pp/13-parenless.expected | 0
Rtests/m1pp/14-str-builtin.M1pp -> tests/M1pp/14-str-builtin.M1pp | 0
Rtests/m1pp/14-str-builtin.expected -> tests/M1pp/14-str-builtin.expected | 0
Rtests/m1pp/14-str-paste.M1pp -> tests/M1pp/14-str-paste.M1pp | 0
Rtests/m1pp/14-str-paste.expected -> tests/M1pp/14-str-paste.expected | 0
Rtests/m1pp/15-struct.M1pp -> tests/M1pp/15-struct.M1pp | 0
Rtests/m1pp/15-struct.expected -> tests/M1pp/15-struct.expected | 0
Rtests/m1pp/16-enum.M1pp -> tests/M1pp/16-enum.M1pp | 0
Rtests/m1pp/16-enum.expected -> tests/M1pp/16-enum.expected | 0
Rtests/m1pp/17-scopes.M1pp -> tests/M1pp/17-scopes.M1pp | 0
Rtests/m1pp/17-scopes.expected -> tests/M1pp/17-scopes.expected | 0
Rtests/m1pp/_04-strlen-badarg.M1pp -> tests/M1pp/_04-strlen-badarg.M1pp | 0
Rtests/m1pp/_12-braced-malformed.M1pp -> tests/M1pp/_12-braced-malformed.M1pp | 0
Rtests/m1pp/_14-str-malformed.M1pp -> tests/M1pp/_14-str-malformed.M1pp | 0
Rtests/p1/argc_exit.P1pp -> tests/P1/argc_exit.P1pp | 0
Rtests/p1/argc_exit.expected -> tests/P1/argc_exit.expected | 0
Rtests/p1/double.P1pp -> tests/P1/double.P1pp | 0
Rtests/p1/double.expected -> tests/P1/double.expected | 0
Rtests/p1/hello.P1pp -> tests/P1/hello.P1pp | 0
Rtests/p1/hello.expected -> tests/P1/hello.expected | 0
Rtests/p1/p1-aliasing.P1pp -> tests/P1/p1-aliasing.P1pp | 0
Rtests/p1/p1-aliasing.expected -> tests/P1/p1-aliasing.expected | 0
Rtests/p1/p1-call.P1pp -> tests/P1/p1-call.P1pp | 0
Rtests/p1/p1-call.expected -> tests/P1/p1-call.expected | 0
Dtests/demo.M1 | 303-------------------------------------------------------------------------------
Dtests/hello.M1 | 26--------------------------
Dtests/p1/test.sh | 114-------------------------------------------------------------------------------
Avendor/seed/LICENSE | 674+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/README.md | 26++++++++++++++++++++++++++
Avendor/seed/aarch64/ELF.hex2 | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/aarch64/M0.hex2 | 958+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/catm_AArch64.hex2 -> vendor/seed/aarch64/catm.hex2 | 0
Avendor/seed/aarch64/hex0-seed | 0
Avendor/seed/aarch64/hex0.hex0 | 258+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/aarch64/hex1.hex0 | 464+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/aarch64/hex2.hex1 | 694+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/amd64/ELF.hex2 | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/amd64/M0.hex2 | 845+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/amd64/catm.hex2 | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/amd64/hex0-seed | 0
Avendor/seed/amd64/hex0.hex0 | 152+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/amd64/hex1.hex0 | 315+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/amd64/hex2.hex1 | 587+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/riscv64/ELF.hex2 | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/riscv64/M0.hex2 | 1700+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/riscv64/catm.hex2 | 171+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/riscv64/hex0-seed | 0
Avendor/seed/riscv64/hex0.hex0 | 211+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/riscv64/hex1.hex0 | 647+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Avendor/seed/riscv64/hex2.hex1 | 903+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115 files changed, 9829 insertions(+), 2199 deletions(-)

diff --git a/Containerfile.busybox b/Containerfile.busybox @@ -1,7 +0,0 @@ -FROM gcr.io/distroless/static-debian12@sha256:7985579713fb1171e707d74659c67af3605642d1c9db305304c2998a99032615 AS busybox - -FROM gcr.io/distroless/static-debian12@sha256:20bc6c0bc4d625a22a8fde3e55f6515709b32055ef8fb9cfbddaa06d1760f838 -COPY --from=busybox /busybox/busybox /busybox/busybox -RUN ["/busybox/busybox", "sh", "-c", "for n in sh cp chmod; do /busybox/busybox ln -s busybox /busybox/$n; done"] -ENV PATH=/busybox:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -CMD ["/busybox/sh"] diff --git a/m1pp/m1pp.P1 b/M1pp/M1pp.P1 diff --git a/m1pp/m1pp.c b/M1pp/M1pp.c diff --git a/Makefile b/Makefile @@ -1,47 +1,31 @@ -# lispcc — P1 portable pseudo-ISA demo, built from seed0. +# boot2 — P1 portable pseudo-ISA + M1pp. # -# The toolchain (M0, hex2-0, catm) is bootstrapped per arch from a 400-byte -# hand-assembled hex0-seed via the stage0-posix chain: no C compiler is -# involved, not even the pre-M2-Planet cc_<arch>. See bootstrap.sh. +# Everything here drives `scripts/*.sh`; nothing builds or runs in podman +# outside the per-arch `boot2-busybox:<arch>` image (built from +# `scripts/Containerfile.busybox`). Bootstrap inputs are vendored in +# `vendor/seed/<arch>/`. # -# One podman image is used for everything: alpine at the target arch. The -# seed binaries are target-arch ELF, so the whole chain — toolchain build, -# assembly, linking, and running the final program — runs under the target -# Linux ABI. Foreign arches transparently use podman's binfmt + qemu-user. +# Common entrypoints: +# make all (m1pp + pokem for ARCH) +# make m1pp ARCH=amd64 build the m1pp expander for one arch +# make pokem build pokem for ARCH +# make hello build+run hello via the bootstrap chain +# make test every suite, every arch +# make test SUITE=m1pp m1pp suite, every arch +# make test SUITE=p1 ARCH=amd64 p1 suite, one arch +# make image build the per-arch container image +# make tools bootstrap M0/hex2-0/catm for ARCH +# make tools-native build host-native M1/hex2/m1pp for dev speed +# make clean rm -rf build/ # -# Usage: -# make Build hello for default ARCH=aarch64 -# make ARCH=amd64 Targeting amd64 -# make PROG=demo Build the broader-ISA demo program -# make run Run $(PROG) for $(ARCH) under alpine -# make run-all Build + run $(PROG) on all three arches -# make PROG=demo run-all Run demo on all three arches -# make clean Remove build/ artifacts - -# --- Configuration --------------------------------------------------------- - -PROG ?= hello -ARCH ?= aarch64 +# ARCH defaults to aarch64 and applies to every program/image/tools target. +# It also restricts test runs to one arch when set; without it, tests fan +# out across all three. -# Programs live in src/ (real: lisp) or tests/ (smoke: hello, demo). -# Resolve PROG.M1 against both so the user doesn't have to remember -# which bucket a program is in. -PROG_SRC := $(firstword $(wildcard src/$(PROG).M1 tests/$(PROG).M1)) -ifeq ($(PROG_SRC),) - $(error PROG '$(PROG)' not found — no src/$(PROG).M1 or tests/$(PROG).M1) -endif +ARCH ?= aarch64 -# Per-program runtime arguments. The lisp interpreter reads its script -# from argv[1] (docs/LISP.md step 8). Default to its smoke fixture so -# `make run-all` stays a smoke test. -# -# Lisp fixtures get src/prelude.scm cat'd in front of them (see the -# $(COMBINED_DIR) rule below) so map/filter/fold are in scope without -# the prelude living inside lisp.M1. -RUN_ARGS_lisp ?= $(COMBINED_DIR)/00-identity.scm -RUN_ARGS := $(RUN_ARGS_$(PROG)) +ALL_ARCHES := aarch64 amd64 riscv64 -# Map P1 ARCH -> Linux-platform tag for the container. PLATFORM_aarch64 := linux/arm64 PLATFORM_amd64 := linux/amd64 PLATFORM_riscv64 := linux/riscv64 @@ -50,292 +34,147 @@ ifeq ($(PLATFORM),) $(error ARCH '$(ARCH)' not supported — use aarch64, amd64, or riscv64) endif -# Default upstream checkout path. Only consumed by populate-upstream.sh, -# which runs on the host to populate build/upstream/ before the container -# ever starts. Podman itself never mounts this — the container only ever -# sees curdir, so all inputs must already live inside it. -UPSTREAM ?= $(abspath $(CURDIR)/../live-bootstrap) - -# Pinned to per-arch manifest digests (not the :latest tag, and not the index -# digest): podman's local store keys images by the digest you requested, so -# pulling the same index digest under three different --platforms collapses -# to a single entry — the last pull wins and the other arches run against a -# mismatched image (hence "image platform does not match" warnings). Giving -# each arch its own per-arch manifest digest keeps them as distinct entries -# that coexist in the store and survive across invocations. -# -# Current pins: alpine 3.23.4 per-arch manifests fetched 2026-04-21 from -# public.ecr.aws (index sha256:5b10f432…). Bump all three together when you -# want a newer base. -ALPINE_IMAGE := public.ecr.aws/docker/library/alpine -RUNTIME_IMAGE_aarch64 := $(ALPINE_IMAGE)@sha256:378c4c5418f7493bd500ad21ffb43818d0689daaad43e3261859fb417d1481a0 -RUNTIME_IMAGE_amd64 := $(ALPINE_IMAGE)@sha256:4d889c14e7d5a73929ab00be2ef8ff22437e7cbc545931e52554a7b00e123d8b -RUNTIME_IMAGE_riscv64 := $(ALPINE_IMAGE)@sha256:667d07bf2f6239f094f64b5682c8ffbe24c9f3139b1fb854f85caf931a3d7439 -RUNTIME_IMAGE := $(RUNTIME_IMAGE_$(ARCH)) - -OUT_DIR := build/$(ARCH) -TOOLS_DIR := $(OUT_DIR)/tools -COMBINED_DIR := $(OUT_DIR)/combined - -ifeq ($(PROG),m1m) - ifneq ($(ARCH),aarch64) - $(error PROG=m1m is currently staged for ARCH=aarch64 only) - endif - P1_DEFS := $(OUT_DIR)/p1_m1m_aarch64.M1 -else - P1_DEFS := $(OUT_DIR)/p1_$(ARCH).M1 -endif +IMAGE := boot2-busybox:$(ARCH) -# stage0-posix uses mixed-case arch dirs (AArch64, AMD64) that don't match -# our lowercase ARCH. Map them so build/upstream/ mirrors upstream layout. -ARCH_DIR_aarch64 := AArch64 -ARCH_DIR_amd64 := AMD64 -ARCH_DIR_riscv64 := riscv64 -ARCH_DIR := $(ARCH_DIR_$(ARCH)) - -# Host-populated mirror of the upstream files we consume. Everything -# bootstrap.sh needs (seeds, hex0/1/2 sources, catm, M0, ELF headers) -# lands here before any podman work begins. -UPSTREAM_DIR := build/upstream -UPSTREAM_STAMP := $(UPSTREAM_DIR)/.stamp - -# Single podman view: curdir mounted at /work. Toolchain build, assembly, -# link, and run all share this view. Keeping it narrow means nothing -# outside the repo is visible to the container. -# -# --pull=never: the image is fetched exactly once per arch by the -# $(IMAGE_STAMP) rule below. Every other podman invocation must find it -# already in the local store, otherwise fail loudly — so cross-arch pulls -# or a bumped digest show up as errors, not silent registry traffic. +OUT_DIR := build/$(ARCH) +TOOLS_DIR := $(OUT_DIR)/tools +IMAGE_STAMP := $(OUT_DIR)/.image + +# `--pull=never`: image is built once per arch by the IMAGE_STAMP rule +# and lives in the local store. No registry traffic at run time. PODMAN := podman run --rm --pull=never --platform $(PLATFORM) \ -v $(CURDIR):/work \ -w /work \ - $(RUNTIME_IMAGE) - -# Per-arch image stamp. One explicit `podman pull` registers the -# digest-pinned image in the local store; subsequent runs use --pull=never -# and never contact the registry. Bumping $(RUNTIME_IMAGE) requires -# `make clean` (or removing build/$(ARCH)/.image) to repull. -IMAGE_STAMP := $(OUT_DIR)/.image + $(IMAGE) # --- Targets --------------------------------------------------------------- -.PHONY: all toolchain populate-upstream run run-all test-lisp test-lisp-all test-m1pp test-p1 clean - -all: $(OUT_DIR)/$(PROG) +.PHONY: all m1pp pokem hello run test image tools tools-native \ + clean help $(ALL_ARCHES:%=image-%) $(ALL_ARCHES:%=tools-%) -toolchain: $(TOOLS_DIR)/M0 +all: m1pp pokem -populate-upstream: $(UPSTREAM_STAMP) +help: + @sed -n '/^# Common entrypoints:/,/^$$/p' Makefile | sed 's/^# *//' -$(OUT_DIR) $(TOOLS_DIR) $(COMBINED_DIR): - mkdir -p $@ - -# Prepend src/prelude.scm to every lisp fixture via plain cat, producing -# a standalone .scm the interpreter can eval with its single-argv -# contract. Host-side — no podman needed, it's just byte concatenation. -$(COMBINED_DIR)/%.scm: tests/lisp/%.scm src/prelude.scm | $(COMBINED_DIR) - cat src/prelude.scm $< > $@ +# --- Container image (per arch) ------------------------------------------- -$(IMAGE_STAMP): | $(OUT_DIR) - podman pull --platform $(PLATFORM) $(RUNTIME_IMAGE) - @touch $@ +image: $(IMAGE_STAMP) -# Mirror the upstream seed + hex0/1/2/catm/M0/ELF files we need from -# $(UPSTREAM) into build/upstream/. Host-side so the container mount stays -# minimal. The stamp doubles as an order marker and avoids re-copying on -# every toolchain build. -$(UPSTREAM_STAMP): populate-upstream.sh - sh populate-upstream.sh $(UPSTREAM) +$(IMAGE_STAMP): scripts/Containerfile.busybox | $(OUT_DIR) + podman build --platform $(PLATFORM) -t $(IMAGE) \ + -f scripts/Containerfile.busybox scripts/ @touch $@ -# Any file anyone asks for under build/upstream/ is produced by the stamp -# rule above. Empty recipe — the file is already on disk once the stamp -# exists, and the stamp's timestamp stands in for every file's freshness. -$(UPSTREAM_DIR)/%: $(UPSTREAM_STAMP) ; +$(OUT_DIR) $(TOOLS_DIR): + mkdir -p $@ -# Bootstrap M0, hex2-0, catm (and the throwaway hex0/hex1) from hex0-seed. -# One shot per arch — see bootstrap.sh for the phase-by-phase chain. +# --- Bootstrap toolchain (per arch) --------------------------------------- # -# Grouped target (&:) so all five outputs come from a single recipe run. -$(TOOLS_DIR)/M0 $(TOOLS_DIR)/hex2-0 $(TOOLS_DIR)/catm $(TOOLS_DIR)/hex0 $(TOOLS_DIR)/hex1 &: bootstrap.sh $(UPSTREAM_STAMP) | $(TOOLS_DIR) $(IMAGE_STAMP) - $(PODMAN) sh bootstrap.sh $(ARCH) /work/$(TOOLS_DIR) +# Produce M0/hex2-0/catm (and throwaway hex0/hex1) from the vendored +# hex0-seed by chaining inside the busybox container. One shot per arch. -# Assemble: lint, prune p1 DEFINEs to only those referenced by the program, -# then combine + feed to M0. -# -# Lint catches P1_*/SYS_* tokens with no matching DEFINE — M1 otherwise -# silently emits the literal token text and produces a SIGILL-on-run -# binary. Runs on the host (plain POSIX sh); no podman dependency. -# -# p1_$(ARCH).M1 enumerates every (op, reg-tuple) encoding — ~7,300 DEFINEs, -# of which only ~350 are referenced by a typical PROG_SRC. Dropping the dead -# ones before M0 sees them cuts M0's input bytes ~50% and its per-macro -# O(N) Set_Expression scan ~20x. Safe because p1 DEFINE bodies are pure hex -# with no cross-DEFINE references, so the used set is just the tokens that -# literally appear in PROG_SRC. -# -# M0/hex2-0 use unbuffered fgetc/fputc that do one read()/write() syscall per -# byte. Going through the virtiofs bind mount costs microseconds per syscall -# on Apple Silicon podman (~90s for a 340KB stream); staging everything in -# the container's own overlayfs /tmp collapses it to ~2s. So we cp inputs to -# /tmp up front, run the tools /tmp → /tmp, and cp outputs back — virtiofs -# only sees bulk read()/write() via cp, never per-byte. -# -# M0 takes a single positional input (no -f flag), so we catm the pruned p1 -# with PROG_SRC first. Intermediates stay in OUT_DIR so clean picks them up. -$(OUT_DIR)/$(PROG).hex2: $(PROG_SRC) $(P1_DEFS) lint.sh $(TOOLS_DIR)/M0 $(TOOLS_DIR)/catm | $(OUT_DIR) $(IMAGE_STAMP) - ./lint.sh $(P1_DEFS) $(PROG_SRC) - awk 'NR==FNR{for(i=1;i<=NF;i++)u[$$i]=1;next} /^DEFINE /{if($$2 in u)print;next} {print}' \ - $(PROG_SRC) $(P1_DEFS) > $(OUT_DIR)/p1_$(ARCH).pruned.M1 - $(PODMAN) sh -ec ' \ - cp $(OUT_DIR)/p1_$(ARCH).pruned.M1 /tmp/p1.M1 ; \ - cp $(PROG_SRC) /tmp/prog.M1 ; \ - $(TOOLS_DIR)/catm /tmp/combined.M1 /tmp/p1.M1 /tmp/prog.M1 ; \ - $(TOOLS_DIR)/M0 /tmp/combined.M1 /tmp/$(PROG).hex2 ; \ - cp /tmp/combined.M1 $(OUT_DIR)/$(PROG).combined.M1 ; \ - cp /tmp/$(PROG).hex2 $(OUT_DIR)/$(PROG).hex2' - -# Link: prepend the BSS-enabled ELF header and feed to hex2-0, then truncate -# the on-disk file to p_filesz bytes. The kernel zero-fills the gap up to -# p_memsz at load time. See src/elf/ELF-aarch64.hex2 for the scheme. +tools: $(TOOLS_DIR)/M0 + +$(TOOLS_DIR)/M0 $(TOOLS_DIR)/hex2-0 $(TOOLS_DIR)/catm $(TOOLS_DIR)/hex0 $(TOOLS_DIR)/hex1 &: \ + scripts/bootstrap.sh $(wildcard vendor/seed/$(ARCH)/*) | $(TOOLS_DIR) $(IMAGE_STAMP) + $(PODMAN) sh scripts/bootstrap.sh $(ARCH) $(TOOLS_DIR) + +# --- Native tools (host-side, dev-loop convenience) ----------------------- # -# hex2-0 is strictly positional too, so again catm first. hex2-0 hardcodes -# base address 0x00600000 (no --base-address flag), which is why the ELF -# header references `&ELF_base` symbolically rather than baking in a -# concrete VA — the header travels to whatever base the linker chose. +# Drives scripts/build-native-tools.sh; produces build/native-tools/{M1,hex2,m1pp} +# on the host. These are byte-equivalent substitutes for the bootstrap +# tools when invoked with the right flags (see scripts/build-p1.sh). + +tools-native: + sh scripts/build-native-tools.sh + +# --- P1v2 backend tables -------------------------------------------------- # -# Truncate: p_filesz lives at byte offset 0x60 (96) in the ELF64 header — -# within the first program header, immediately after p_paddr. We read it -# as a little-endian u32 (the high four bytes are zero for any sane size) -# and truncate /tmp/$(PROG) before cp'ing back. Everything past p_filesz -# in the raw hex2-0 output is zero bytes from the trailing BSS region. +# build/p1v2/<arch>/p1_<arch>.M1 is the M1-format DEFINE table for that +# arch, generated by P1/gen/p1_gen.py. Used by scripts/build-p1.sh (the .P1 +# pipeline; .P1pp uses the M1pp form under P1/P1-<arch>.M1pp instead). + +build/p1v2/aarch64/p1_aarch64.M1: $(wildcard P1/gen/*.py) + python3 P1/gen/p1_gen.py --arch aarch64 build/p1v2 + +build/p1v2/amd64/p1_amd64.M1: $(wildcard P1/gen/*.py) + python3 P1/gen/p1_gen.py --arch amd64 build/p1v2 + +build/p1v2/riscv64/p1_riscv64.M1: $(wildcard P1/gen/*.py) + python3 P1/gen/p1_gen.py --arch riscv64 build/p1v2 + +# --- Programs (per arch) -------------------------------------------------- + +m1pp: $(OUT_DIR)/m1pp +pokem: $(OUT_DIR)/pokem + +$(OUT_DIR)/m1pp: M1pp/M1pp.P1 build/p1v2/$(ARCH)/p1_$(ARCH).M1 \ + scripts/build-p1.sh scripts/lint.sh + sh scripts/build-p1.sh $(ARCH) M1pp/M1pp.P1 $@ + +$(OUT_DIR)/pokem: pokem/pokem.P1 build/p1v2/$(ARCH)/p1_$(ARCH).M1 \ + scripts/build-p1.sh scripts/lint.sh + sh scripts/build-p1.sh $(ARCH) pokem/pokem.P1 $@ + +# Bootstrap-chain showcase: build hello via the full M0/hex2-0 path inside +# the container. Demonstrates the seed -> tool -> ELF flow end to end. +HELLO_SRC := tests/M1pp/00-hello.M1 + +hello: $(OUT_DIR)/hello + +$(OUT_DIR)/hello: $(HELLO_SRC) build/p1v2/$(ARCH)/p1_$(ARCH).M1 \ + scripts/build-p1.sh scripts/lint.sh $(TOOLS_DIR)/M0 + M1PP_BOOTSTRAP_TOOLS=1 sh scripts/build-p1.sh $(ARCH) $(HELLO_SRC) $@ + +run: $(OUT_DIR)/hello | $(IMAGE_STAMP) + $(PODMAN) ./$(OUT_DIR)/hello + +# --- Tests ---------------------------------------------------------------- # -# All I/O stages through /tmp for the same unbuffered-fputc reason as M0. -ELF_HDR_SRC := src/elf/ELF-$(ARCH).hex2 - -$(OUT_DIR)/$(PROG): $(OUT_DIR)/$(PROG).hex2 $(ELF_HDR_SRC) $(TOOLS_DIR)/hex2-0 $(TOOLS_DIR)/catm | $(IMAGE_STAMP) - $(PODMAN) sh -ec ' \ - cp $(ELF_HDR_SRC) /tmp/elf.hex2 ; \ - cp $(OUT_DIR)/$(PROG).hex2 /tmp/prog.hex2 ; \ - $(TOOLS_DIR)/catm /tmp/linked.hex2 /tmp/elf.hex2 /tmp/prog.hex2 ; \ - $(TOOLS_DIR)/hex2-0 /tmp/linked.hex2 /tmp/$(PROG).raw ; \ - size=$$(od -An -tu4 -N4 -j96 /tmp/$(PROG).raw | tr -d " ") ; \ - head -c $$size /tmp/$(PROG).raw > /tmp/$(PROG) ; \ - chmod 0700 /tmp/$(PROG) ; \ - cp /tmp/linked.hex2 $(OUT_DIR)/$(PROG).linked.hex2 ; \ - cp /tmp/$(PROG) $(OUT_DIR)/$(PROG)' - -# $(RUN_ARGS) is listed as a prerequisite so that lisp's concat'd fixture -# is materialised before the interpreter runs. Empty for hello/demo and -# therefore a no-op there. -run: $(OUT_DIR)/$(PROG) $(RUN_ARGS) | $(IMAGE_STAMP) - $(PODMAN) ./$(OUT_DIR)/$(PROG) $(RUN_ARGS) - -# `-` prefix: continue past non-zero exit. demo.M1 exits with the computed -# result (5), which is a legitimate program outcome, not a make failure. -run-all: - -$(MAKE) --no-print-directory PROG=$(PROG) ARCH=aarch64 run - -$(MAKE) --no-print-directory PROG=$(PROG) ARCH=amd64 run - -$(MAKE) --no-print-directory PROG=$(PROG) ARCH=riscv64 run - -# Test harness (docs/LISP.md step 9). Each fixtures pair in tests/lisp/ is a -# <name>.scm program plus a <name>.expected file holding its exact -# stdout. test-lisp runs every fixture on $(ARCH); test-lisp-all runs -# the suite on all three arches — cross-arch consistency falls out by -# transitivity, since every arch must match the shared expectation. +# `make test` runs every suite on every arch. SUITE/ARCH narrow it. +# Test runner is scripts/run-tests.sh, parameterized by --suite/--arch. + +SUITE ?= + +# When ARCH is set on the command line, restrict tests to that arch. +ifeq ($(origin ARCH),file) + ARCH_FILTER := + TEST_ARCHES := $(ALL_ARCHES) +else + ARCH_FILTER := $(ARCH) + TEST_ARCHES := $(ARCH) +endif + +test: +ifeq ($(SUITE),) + @$(MAKE) --no-print-directory test SUITE=m1pp + @$(MAKE) --no-print-directory test SUITE=p1 +else + @$(MAKE) --no-print-directory \ + $(foreach a,$(TEST_ARCHES),image-$(a)) \ + $(foreach a,$(TEST_ARCHES),build/p1v2/$(a)/p1_$(a).M1) +ifeq ($(SUITE),m1pp) + sh scripts/run-tests.sh --suite=m1pp $(if $(ARCH_FILTER),--arch=$(ARCH_FILTER)) +else ifeq ($(SUITE),p1) + sh scripts/run-tests.sh --suite=p1 $(if $(ARCH_FILTER),--arch=$(ARCH_FILTER)) +else + @echo "unknown SUITE='$(SUITE)' (expected m1pp | p1)" >&2; exit 2 +endif +endif + +# --- Per-arch convenience targets ----------------------------------------- # -# Non-zero exits from the interpreter are fine (it exits with the -# final fixnum payload), so we ignore exit codes and only diff stdout. -LISP_TESTS := \ - tests/lisp/00-identity.scm \ - tests/lisp/07-tailcall.scm \ - tests/lisp/10-arith.scm \ - tests/lisp/11-compare.scm \ - tests/lisp/11-list.scm \ - tests/lisp/12-numpred.scm \ - tests/lisp/12-string.scm \ - tests/lisp/13-bitwise.scm \ - tests/lisp/13-vector.scm \ - tests/lisp/14-io.scm \ - tests/lisp/14-tagpred.scm \ - tests/lisp/15-pred.scm \ - tests/lisp/16-prelude.scm \ - tests/lisp/20-quote.scm \ - tests/lisp/21-neg-hex.scm \ - tests/lisp/22-char.scm \ - tests/lisp/23-vector.scm \ - tests/lisp/24-dotted.scm \ - tests/lisp/25-set.scm \ - tests/lisp/26-let.scm \ - tests/lisp/27-cond.scm \ - tests/lisp/28-quasi.scm \ - tests/lisp/29-innerdef.scm \ - tests/lisp/17-gc-cons-churn.scm \ - tests/lisp/18-gc-deep-list.scm \ - tests/lisp/19-gc-vector-churn.scm \ - tests/lisp/20-gc-closure-churn.scm \ - tests/lisp/21-gc-mixed.scm - -# Build the prelude-prefixed twin of every fixture and feed that to the -# interpreter; .expected still lives next to the original .scm. -LISP_TESTS_COMBINED := $(patsubst tests/lisp/%.scm,$(COMBINED_DIR)/%.scm,$(LISP_TESTS)) - -test-lisp: $(LISP_TESTS_COMBINED) | $(IMAGE_STAMP) - @$(MAKE) --no-print-directory PROG=lisp ARCH=$(ARCH) build/$(ARCH)/lisp - @pass=0; fail=0; \ - for scm in $(LISP_TESTS); do \ - expected="$${scm%.scm}.expected"; \ - combined="$(COMBINED_DIR)/$$(basename $$scm)"; \ - actual=$$($(PODMAN) ./build/$(ARCH)/lisp "$$combined" || true); \ - want=$$(cat "$$expected" 2>/dev/null || printf ''); \ - if [ "$$actual" = "$$want" ]; then \ - echo " PASS [$(ARCH)] $$scm"; \ - pass=$$((pass+1)); \ - else \ - echo " FAIL [$(ARCH)] $$scm"; \ - echo " expected: $$want"; \ - echo " actual: $$actual"; \ - fail=$$((fail+1)); \ - fi; \ - done; \ - echo "[$(ARCH)] $$pass passed, $$fail failed"; \ - [ $$fail -eq 0 ] - -test-lisp-all: - $(MAKE) --no-print-directory ARCH=aarch64 test-lisp - $(MAKE) --no-print-directory ARCH=amd64 test-lisp - $(MAKE) --no-print-directory ARCH=riscv64 test-lisp - -# m1pp port test suite (P1v2, aarch64-only). Drives m1pp/build.sh + run. -# Fixtures live in tests/m1pp/; see m1pp/test.sh for the runner contract. -test-m1pp: build/p1v2/aarch64/p1_aarch64.M1 $(TOOLS_DIR)/M0 | $(IMAGE_STAMP) - sh m1pp/test.sh - -# P1pp fixtures under tests/p1/ run through the M1pp-driven pipeline -# (p1/P1-<arch>.M1pp + p1/P1.M1pp + <name>.P1pp) for every backend arch. -# Cross-arch consistency falls out because every arch must match the -# same <name>.expected output. -test-p1: - sh tests/p1/test.sh - -# P1v2 DEFINE table for aarch64. Generated by p1/p1_gen.py from -# p1/aarch64.py. Used by the m1pp port (build/m1pp/) — distinct from the -# legacy build/aarch64/p1_aarch64.M1 used by PROG=hello/lisp/m1m. -build/p1v2/aarch64/p1_aarch64.M1: p1/p1_gen.py p1/aarch64.py p1/common.py - python3 p1/p1_gen.py --arch aarch64 build/p1v2 +# `make image-amd64` etc. — used by `make test` to materialise every arch's +# container/table before run-tests.sh starts looping. + +$(ALL_ARCHES:%=image-%): + @$(MAKE) --no-print-directory ARCH=$(@:image-%=%) image + +$(ALL_ARCHES:%=tools-%): + @$(MAKE) --no-print-directory ARCH=$(@:tools-%=%) tools + +# -------------------------------------------------------------------------- clean: rm -rf build/ - -# Generate all three per-arch DEFINE tables from src/p1_gen.py in a -# single shot. Grouped target (&:) because p1_gen.py writes all three -# files unconditionally. Output lands under build/<arch>/ (build/ is -# wiped by clean, so the build regenerates on any p1_gen.py edit with -# no staleness risk). -build/aarch64/p1_aarch64.M1 build/amd64/p1_amd64.M1 build/riscv64/p1_riscv64.M1 &: src/p1_gen.py - python3 src/p1_gen.py build - -build/aarch64/p1_m1m_aarch64.M1: build/aarch64/p1_aarch64.M1 src/m1m.M1 | build/aarch64 - awk 'NR==FNR{for(i=1;i<=NF;i++)u[$$i]=1;next} /^DEFINE /{if($$2 in u)print;next} {print}' \ - src/m1m.M1 build/aarch64/p1_aarch64.M1 > $@ diff --git a/p1/P1-aarch64.M1pp b/P1/P1-aarch64.M1pp diff --git a/p1/P1-amd64.M1pp b/P1/P1-amd64.M1pp diff --git a/p1/P1-riscv64.M1pp b/P1/P1-riscv64.M1pp diff --git a/p1/P1.M1pp b/P1/P1.M1pp diff --git a/p1/p1pp.P1pp b/P1/P1pp.P1pp diff --git a/p1/gen/aarch64.py b/P1/gen/aarch64.py diff --git a/p1/gen/amd64.py b/P1/gen/amd64.py diff --git a/p1/gen/common.py b/P1/gen/common.py diff --git a/p1/llvm_disasm_aarch64.py b/P1/gen/llvm_disasm_aarch64.py diff --git a/p1/gen/p1_gen.py b/P1/gen/p1_gen.py diff --git a/p1/gen/riscv64.py b/P1/gen/riscv64.py diff --git a/README.md b/README.md @@ -33,9 +33,9 @@ (define tcc (tcc1 tcc.c)) ;; compiler: tcc-compiled tcc ``` -* P1: docs/P1.md -* M1pp: docs/M1PP.md -* P1PP: docs/LIBP1PP.md, p1/P1.M1pp, p1/P1pp.P1pp -* Scheme plan: docs/LISP.md, docs/LISP-C.md +* P1: [docs/P1.md](docs/P1.md) +* M1pp: [docs/M1PP.md](docs/M1PP.md) +* P1PP: [docs/LIBP1PP.md](docs/LIBP1PP.md), [P1/P1.M1pp](P1/P1.M1pp), [P1/P1pp.P1pp](P1/P1pp.P1pp) +* Scheme plan: [docs/LISP.md](docs/LISP.md), [docs/LISP-C.md](docs/LISP-C.md) If you'd like to chat, email me at hi at ryansepassi.com diff --git a/bootstrap.sh b/bootstrap.sh @@ -1,69 +0,0 @@ -#!/bin/sh -# Bootstrap the seed0 toolchain we need to assemble + link P1 programs: -# hex0-seed -> hex0 -> hex1 -> hex2-0 -> catm -> M0. -# -# Runs inside a target-arch alpine container. All produced binaries are -# target-arch Linux ELF and are written to $OUT as: hex0 hex1 hex2-0 catm M0. -# -# The only non-source input is bootstrap-seeds/POSIX/<Arch>/hex0-seed (~400 -# bytes, hand-assembled, shipped by stage0-posix). Nothing above M0 is built, -# which is the whole point — no C compiler is involved, not even cc_<arch>. -# -# Inputs are read from build/upstream/, which populate-upstream.sh mirrors -# from live-bootstrap's stage0-posix on the host. The container mounts only -# curdir, so everything bootstrap.sh needs must already live inside it. -# -# Phase map (stage0-posix mescc-tools-{seed,mini}-kaem.kaem phases 0-3): -# 0) hex0-seed + hex0_<A>.hex0 -> hex0 -# 1) hex0 + hex1_<A>.hex0 -> hex1 -# 2) hex1 + hex2_<A>.hex1 -> hex2-0 -# 2b) hex2-0 + catm_<A>.hex2 -> catm -# 3a) catm : ELF header + M0_<A>.hex2 -> M0.hex2 -# 3b) hex2-0 : M0.hex2 -> M0 -# -# Usage: bootstrap.sh <arch> <out-dir> -# arch: aarch64 | amd64 | riscv64 -# out-dir: absolute path where tool binaries should land -set -eu - -ARCH=$1 -OUT=$2 - -# Map lispcc's lowercase ARCH to stage0-posix's dir name. -case "$ARCH" in - aarch64) A=AArch64 ;; - amd64) A=AMD64 ;; - riscv64) A=riscv64 ;; - *) echo "bootstrap.sh: unsupported arch '$ARCH'" >&2 ; exit 1 ;; -esac - -S=build/upstream -mkdir -p "$OUT" - -# qemu-user amd64 workaround: the shipped hex0-seed and the hex0 it produces -# both have a program header with p_flags=0x01 (PF_X only, no PF_R). A native -# Linux kernel treats x86-64's hardware coercion as making this loadable, but -# qemu-user's stricter ELF loader faults when fetching instructions from an -# unreadable segment. Copy the seed to a writable location and flip p_flags -# to 0x05 (PF_R|PF_X) before use. All later seed sources already use 0x07. -# -# This only affects foreign-arch builds on non-amd64 hosts; on a native amd64 -# host the patch is a no-op (binary would load fine either way). -SEED="$S"/bootstrap-seeds/POSIX/"$A"/hex0-seed -if [ "$ARCH" = amd64 ]; then - cp "$SEED" "$OUT"/hex0-seed - printf '\5' | dd of="$OUT"/hex0-seed bs=1 seek=68 count=1 conv=notrunc status=none - chmod +x "$OUT"/hex0-seed - SEED="$OUT"/hex0-seed -fi - -"$SEED" "$S"/"$A"/hex0_"$A".hex0 "$OUT"/hex0 -if [ "$ARCH" = amd64 ]; then - printf '\5' | dd of="$OUT"/hex0 bs=1 seek=68 count=1 conv=notrunc status=none -fi - -"$OUT"/hex0 "$S"/"$A"/hex1_"$A".hex0 "$OUT"/hex1 -"$OUT"/hex1 "$S"/"$A"/hex2_"$A".hex1 "$OUT"/hex2-0 -"$OUT"/hex2-0 "$S"/"$A"/catm_"$A".hex2 "$OUT"/catm -"$OUT"/catm "$OUT"/M0.hex2 "$S"/"$A"/ELF-"$ARCH".hex2 "$S"/"$A"/M0_"$A".hex2 -"$OUT"/hex2-0 "$OUT"/M0.hex2 "$OUT"/M0 diff --git a/docs/LISP-C.md b/docs/LISP-C.md @@ -794,7 +794,7 @@ before `p1_main`. ## File layout ``` -lispcc/ +boot2/ src/ scheme.P1 # M1PP source, consumes P1.M1pp macro library lisp/ diff --git a/docs/LISP.md b/docs/LISP.md @@ -1,4 +1,4 @@ -# Minimal Scheme subset (lispcc) +# Minimal Scheme subset (boot2) Working doc. Baseline is R7RS-small (2013); everything here is a delta against it. Intent: smallest surface that supports writing a self-hosted diff --git a/docs/P1.md b/docs/P1.md @@ -35,7 +35,7 @@ This document describes instructions using ordinary assembly notation such as Because of the toolchain constraints above, portable source does not encode most operands as textual instruction arguments. Instead, register choices, inline immediate values, and small fixed parameters are fused into opcode -names, following the generated-table style used by `src/p1_gen.py`. +names, following the generated-table style used by `p1/gen/p1_gen.py`. So the notation in this document is descriptive rather than literal: diff --git a/m1pp/build-native-tools.sh b/m1pp/build-native-tools.sh @@ -1,81 +0,0 @@ -#!/bin/sh -## build-native-tools.sh — compile mescc-tools M1 and hex2 natively for -## dev-loop speed. These are NOT in the bootstrap chain; they are a fast -## substitute for stage0 M0/hex2-0, used by m1pp/build.sh's default mode. -## -## Output is verified byte-exact with the bootstrap tools when the right -## flags are passed (see m1pp/build.sh). Using them saves ~150× wall time -## on P1 builds / tests by avoiding the per-byte syscall storm the -## stage0 tools incur under Apple's linux/arm64 VM. -## -## Source lookup (first match wins): -## 1. $MESCC_TOOLS_SRC (if set) -## 2. ../live-bootstrap/seed/stage0-posix/mescc-tools -## 3. ../mescc-tools (if M2libc is populated) - -set -eu - -REPO=$(cd "$(dirname "$0")/.." && pwd) -cd "$REPO" - -OUT=build/native-tools -mkdir -p "$OUT" - -find_src() { - if [ -n "${MESCC_TOOLS_SRC:-}" ]; then - if [ -f "$MESCC_TOOLS_SRC/M1-macro.c" ] && [ -f "$MESCC_TOOLS_SRC/M2libc/bootstrappable.c" ]; then - echo "$MESCC_TOOLS_SRC" - return 0 - fi - echo "build-native-tools.sh: MESCC_TOOLS_SRC=$MESCC_TOOLS_SRC is not a complete mescc-tools tree" >&2 - return 1 - fi - for d in \ - "$REPO/../live-bootstrap/seed/stage0-posix/mescc-tools" \ - "$REPO/../mescc-tools" - do - if [ -f "$d/M1-macro.c" ] && [ -f "$d/M2libc/bootstrappable.c" ]; then - echo "$d" - return 0 - fi - done - echo "build-native-tools.sh: no mescc-tools source found." >&2 - echo " set MESCC_TOOLS_SRC to a directory containing M1-macro.c and M2libc/," >&2 - echo " or fall back to the bootstrap path with M1PP_BOOTSTRAP_TOOLS=1." >&2 - return 1 -} - -SRC=$(find_src) - -: "${CC:=cc}" -CFLAGS="-O2 -std=c99 -D_GNU_SOURCE" - -## Only rebuild if sources are newer than the cached binary. -m1_fresh() { - [ -x "$OUT/M1" ] || return 1 - for s in "$SRC/M1-macro.c" "$SRC/stringify.c" "$SRC/M2libc/bootstrappable.c"; do - [ "$OUT/M1" -nt "$s" ] || return 1 - done - return 0 -} -hex2_fresh() { - [ -x "$OUT/hex2" ] || return 1 - for s in "$SRC/hex2.c" "$SRC/hex2_linker.c" "$SRC/hex2_word.c" "$SRC/M2libc/bootstrappable.c"; do - [ "$OUT/hex2" -nt "$s" ] || return 1 - done - return 0 -} - -if ! m1_fresh; then - echo " compiling $OUT/M1 from $SRC" - $CC $CFLAGS \ - "$SRC/M1-macro.c" "$SRC/stringify.c" "$SRC/M2libc/bootstrappable.c" \ - -o "$OUT/M1" -fi - -if ! hex2_fresh; then - echo " compiling $OUT/hex2 from $SRC" - $CC $CFLAGS \ - "$SRC/hex2.c" "$SRC/hex2_linker.c" "$SRC/hex2_word.c" "$SRC/M2libc/bootstrappable.c" \ - -o "$OUT/hex2" -fi diff --git a/m1pp/build-p1.sh b/m1pp/build-p1.sh @@ -1,94 +0,0 @@ -#!/bin/sh -## build-p1.sh -- build a P1v2 .P1pp source for the M1pp-based P1 pipeline. -## -## Pipeline: -## 1. catm p1/P1-<arch>.M1pp + p1/P1.M1pp + <source.P1pp> -> combined.M1pp -## 2. m1pp combined.M1pp -> expanded.M1 (macros -> raw hex + labels) -## 3. M1 expanded.M1 -> prog.hex2 (stringify literals, pass hex through) -## 4. catm ELF-<arch>.hex2 + prog.hex2 -> linked.hex2 -## 5. hex2 linked.hex2 -> raw ELF -## 6. chmod + trim to p_filesz, deposit at <output> -## -## Usage: m1pp/build-p1.sh <arch> <source.P1pp> <output> -## arch: aarch64 | riscv64 | amd64 -## -## The m1pp expander itself is architecture-neutral; we run the aarch64 -## binary under podman linux/arm64 for all target arches. M1/hex2 run -## natively on the host via build/native-tools/. - -set -eu - -if [ "$#" -ne 3 ]; then - echo "usage: $0 <arch> <source.P1pp> <output>" >&2 - exit 2 -fi - -ARCH=$1 -SRC=$2 -OUT=$3 - -case "$ARCH" in - aarch64) ELF_HDR=build/upstream/AArch64/ELF-aarch64.hex2 ;; - amd64) ELF_HDR=build/upstream/AMD64/ELF-amd64.hex2 ;; - riscv64) ELF_HDR=build/upstream/riscv64/ELF-riscv64.hex2 ;; - *) echo "build-p1.sh: unsupported arch '$ARCH'" >&2; exit 1 ;; -esac - -REPO=$(cd "$(dirname "$0")/.." && pwd) -cd "$REPO" - -FRONTEND=p1/P1.M1pp -BACKEND=p1/P1-$ARCH.M1pp - -for f in "$BACKEND" "$FRONTEND" "$ELF_HDR" "$SRC"; do - if [ ! -e "$f" ]; then - echo "build-p1.sh: missing input: $f" >&2 - exit 1 - fi -done - -## Host-compiled m1pp has 64K-token / 512K-text buffers, versus the ~4K / -## 32K cap in the aarch64 self-hosted m1pp.P1. The combined backend + -## frontend + source easily blows past the 4K-token cap, so use native. -NATIVE_M1PP=build/native-tools/m1pp -NATIVE_M1=build/native-tools/M1 -NATIVE_HEX2=build/native-tools/hex2 -if [ ! -x "$NATIVE_M1PP" ]; then - : "${CC:=cc}" - mkdir -p build/native-tools - $CC -O2 -std=c99 -o "$NATIVE_M1PP" m1pp/m1pp.c -fi -if [ ! -x "$NATIVE_M1" ] || [ ! -x "$NATIVE_HEX2" ]; then - sh m1pp/build-native-tools.sh -fi - -NAME=$(basename "$SRC" .P1pp) -WORK=build/p1v2-m1pp/$ARCH/$NAME.work -mkdir -p "$WORK" "$(dirname "$OUT")" - -COMBINED=$WORK/combined.M1pp -EXPANDED=$WORK/expanded.M1 -PROG_HEX2=$WORK/prog.hex2 -LINKED=$WORK/linked.hex2 -RAW=$WORK/prog.raw - -cat "$BACKEND" "$FRONTEND" "$SRC" > "$COMBINED" - -"$NATIVE_M1PP" "$COMBINED" "$EXPANDED" - -"$NATIVE_M1" --architecture "$ARCH" --little-endian \ - -f "$EXPANDED" -o "$PROG_HEX2" - -cat "$ELF_HDR" "$PROG_HEX2" > "$LINKED" - -"$NATIVE_HEX2" --architecture "$ARCH" --little-endian \ - --base-address 0x600000 \ - -f "$LINKED" -o "$RAW" - -## Trim trailing zero padding past p_filesz (lives at byte offset 96 as a -## little-endian u32 in the ELF64 program header). The hex2 output -## zero-fills to p_memsz; the kernel zero-fills the BSS gap at load time -## so we can chop anything past p_filesz on disk. -size=$(od -An -tu4 -N4 -j96 "$RAW" | tr -d ' ') -head -c "$size" "$RAW" > "$OUT" -chmod 0700 "$OUT" diff --git a/m1pp/build.sh b/m1pp/build.sh @@ -1,137 +0,0 @@ -#!/bin/sh -## build.sh — build a P1v2 .P1 source into a runnable aarch64 ELF binary. -## -## Mirrors the Makefile's PROG=m1m pipeline but targets the P1v2 DEFINE -## table at build/p1v2/aarch64/p1_aarch64.M1. -## -## Usage: m1pp/build.sh <source.P1> <output_binary> -## -## Pipeline (mirrors Makefile PROG=m1m): -## 1. lint — assert every P1v2 op token in source.P1 is defined -## 2. prune — strip DEFINEs the source doesn't reference -## 3. catm — pruned defs ++ source.P1 -> combined.M1 -## 4. M1 — combined.M1 -> .hex2 -## 5. catm — ELF header ++ .hex2 -> linked.hex2 -## 6. hex2 — linked.hex2 -> raw ELF -## 7. chmod 0700, deposit at <output_binary> -## -## Default mode uses native-compiled mescc-tools M1/hex2 on the host: -## ~150× faster than the bootstrap M0/hex2-0 on the linux/arm64 VM (the -## bootstrap tools do one syscall per byte; mescc-tools uses buffered -## stdio). Output is byte-exact — verified against the bootstrap chain -## with --little-endian on both tools. -## -## This is a dev-loop convenience. It does not alter the bootstrap chain -## itself: the stage0 M0/hex2-0 binaries under build/$ARCH/tools remain -## the source of truth. Set M1PP_BOOTSTRAP_TOOLS=1 to force the original -## container-based path (verification, or when mescc-tools is unavailable). -## -## Intermediates land in build/m1pp/<basename>.* for later inspection. - -set -eu - -if [ "$#" -ne 2 ]; then - echo "usage: $0 <source.P1> <output_binary>" >&2 - exit 2 -fi - -SRC=$1 -OUT=$2 - -REPO=$(cd "$(dirname "$0")/.." && pwd) -ARCH=aarch64 -PLATFORM=linux/arm64 -IMAGE=localhost/distroless-busybox:latest -CONTAINERFILE=Containerfile.busybox - -P1_DEFS=build/p1v2/$ARCH/p1_$ARCH.M1 -TOOLS=build/$ARCH/tools -ELF_HDR=build/upstream/AArch64/ELF-aarch64.hex2 -BASE_ADDR=0x600000 ## must match the load address encoded in $ELF_HDR - -NATIVE_TOOLS_DIR=build/native-tools -NATIVE_M1=$NATIVE_TOOLS_DIR/M1 -NATIVE_HEX2=$NATIVE_TOOLS_DIR/hex2 - -cd "$REPO" - -for f in "$P1_DEFS" "$ELF_HDR" lint.sh "$SRC"; do - if [ ! -e "$f" ]; then - echo "build.sh: missing dependency: $f" >&2 - exit 1 - fi -done - -NAME=$(basename "$SRC" .P1) -WORK=build/m1pp/$NAME.work -mkdir -p "$WORK" "$(dirname "$OUT")" - -PRUNED=$WORK/p1.pruned.M1 - -## Step 1: lint. -sh lint.sh "$P1_DEFS" "$SRC" - -## Step 2: prune. Same awk one-liner as Makefile — collect all whitespace- -## separated tokens from the source, keep only DEFINEs whose name appears. -awk 'NR==FNR{for(i=1;i<=NF;i++)u[$i]=1;next} /^DEFINE /{if($2 in u)print;next} {print}' \ - "$SRC" "$P1_DEFS" > "$PRUNED" - -## Steps 3-7: mode selection. -if [ "${M1PP_BOOTSTRAP_TOOLS:-0}" = 1 ]; then - ## Bootstrap mode: run stage0 M0/hex2-0 inside the container. Slow - ## (~110s on 1 MB m1pp.P1) but exercises the same code path the - ## seed/tcc-boot chain does. Stage through /tmp to dodge virtiofs. - for f in "$TOOLS/M0" "$TOOLS/hex2-0" "$TOOLS/catm"; do - if [ ! -e "$f" ]; then - echo "build.sh: missing bootstrap dependency: $f" >&2 - exit 1 - fi - done - if ! podman image exists "$IMAGE"; then - podman build -f "$CONTAINERFILE" -t "$IMAGE" . - fi - podman run --rm --pull=never --platform "$PLATFORM" \ - -v "$REPO":/work \ - -w /work \ - "$IMAGE" sh -ec " - set -eu - cp $PRUNED /tmp/p1.M1 - cp $SRC /tmp/prog.M1 - $TOOLS/catm /tmp/combined.M1 /tmp/p1.M1 /tmp/prog.M1 - $TOOLS/M0 /tmp/combined.M1 /tmp/prog.hex2 - - cp $ELF_HDR /tmp/elf.hex2 - $TOOLS/catm /tmp/linked.hex2 /tmp/elf.hex2 /tmp/prog.hex2 - $TOOLS/hex2-0 /tmp/linked.hex2 /tmp/prog - chmod 0700 /tmp/prog - - cp /tmp/combined.M1 $WORK/combined.M1 - cp /tmp/prog.hex2 $WORK/prog.hex2 - cp /tmp/linked.hex2 $WORK/linked.hex2 - cp /tmp/prog $WORK/prog - " -else - ## Native mode (default): host-compiled mescc-tools M1/hex2. The - ## bootstrap chain is not invoked. Flags are chosen to make the - ## emitted aarch64 ELF bit-exact with the bootstrap build: - ## M1: --little-endian (default is big-endian; --architecture - ## aarch64 does NOT override that — see M1-macro.c:786). - ## hex2: --little-endian and --base-address matches $ELF_HDR. - if [ ! -x "$NATIVE_M1" ] || [ ! -x "$NATIVE_HEX2" ]; then - sh m1pp/build-native-tools.sh - fi - - ## catm on the bootstrap side is just multi-file concat → shell cat - ## is equivalent. - cat "$PRUNED" "$SRC" > "$WORK/combined.M1" - "$NATIVE_M1" --architecture "$ARCH" --little-endian \ - -f "$WORK/combined.M1" -o "$WORK/prog.hex2" - cat "$ELF_HDR" "$WORK/prog.hex2" > "$WORK/linked.hex2" - "$NATIVE_HEX2" --architecture "$ARCH" --little-endian \ - --base-address "$BASE_ADDR" \ - -f "$WORK/linked.hex2" -o "$WORK/prog" - chmod 0700 "$WORK/prog" -fi - -cp "$WORK/prog" "$OUT" -chmod 0700 "$OUT" diff --git a/m1pp/test.sh b/m1pp/test.sh @@ -1,128 +0,0 @@ -#!/bin/sh -## test.sh — run the m1pp test suite under tests/m1pp/. -## -## Two fixture shapes, selected by file extension: -## -## <name>.M1 — standalone P1v2 program (build-pipeline smoke test). -## Built, run with no args; stdout diffed against -## <name>.expected. -## -## <name>.M1pp — input for the m1pp expander (parity test). -## Runner builds m1pp/m1pp.P1 once, runs the resulting -## binary as `m1pp <name>.M1pp <tmp>/out`, and diffs -## that output file against <name>.expected. -## -## Filenames starting with `_` are skipped — used for ad-hoc debugging. -## -## Usage: m1pp/test.sh [fixture-name ...] -## No args: run every non-`_` fixture under tests/m1pp/. - -set -eu - -REPO=$(cd "$(dirname "$0")/.." && pwd) -PLATFORM=linux/arm64 -## Share the tag that build.sh builds and runs under. build.sh creates it -## on first run from Containerfile.busybox — we assume it exists by the time -## a fixture is run (build_expander / per-fixture build.sh invocations -## produce it as a side effect). -IMAGE=localhost/distroless-busybox:latest - -cd "$REPO" - -EXPANDER_BIN=build/m1pp/m1pp -EXPANDER_SRC=m1pp/m1pp.P1 -EXPANDER_DEFS=build/p1v2/aarch64/p1_aarch64.M1 -EXPANDER_BUILT=0 - -expander_up_to_date() { - [ -x "$EXPANDER_BIN" ] || return 1 - [ "$EXPANDER_BIN" -nt "$EXPANDER_SRC" ] || return 1 - [ "$EXPANDER_BIN" -nt "$EXPANDER_DEFS" ] || return 1 - return 0 -} - -build_expander() { - if [ "$EXPANDER_BUILT" = 0 ]; then - if expander_up_to_date; then - echo " (m1pp up to date, skipping rebuild)" - else - sh m1pp/build.sh "$EXPANDER_SRC" "$EXPANDER_BIN" >/dev/null 2>&1 || { - echo "FATAL: failed to build $EXPANDER_SRC" >&2 - sh m1pp/build.sh "$EXPANDER_SRC" "$EXPANDER_BIN" 2>&1 | sed 's/^/ /' >&2 - exit 1 - } - fi - EXPANDER_BUILT=1 - fi -} - -if [ "$#" -gt 0 ]; then - NAMES="$*" -else - NAMES=$(ls tests/m1pp/ 2>/dev/null \ - | sed -n 's/^\([^_][^.]*\)\.\(M1\|M1pp\)$/\1/p' \ - | sort -u) -fi - -if [ -z "$NAMES" ]; then - echo "no fixtures to run" >&2 - exit 1 -fi - -pass=0 -fail=0 -for name in $NAMES; do - expected=tests/m1pp/$name.expected - m1_src=tests/m1pp/$name.M1 - m1pp_src=tests/m1pp/$name.M1pp - - if [ ! -e "$expected" ]; then - echo " SKIP $name (no .expected)" - continue - fi - - if [ -e "$m1pp_src" ]; then - build_expander - outfile=build/m1pp/$name.out - rm -f "$outfile" - podman run --rm --pull=never --platform "$PLATFORM" \ - -v "$REPO":/work -w /work "$IMAGE" \ - "./$EXPANDER_BIN" "$m1pp_src" "$outfile" >/dev/null 2>&1 || true - if [ -e "$outfile" ]; then - actual=$(cat "$outfile") - else - actual="" - fi - elif [ -e "$m1_src" ]; then - bin=build/m1pp/$name - sh m1pp/build.sh "$m1_src" "$bin" >/dev/null 2>&1 || { - echo " FAIL $name (build failed)" - sh m1pp/build.sh "$m1_src" "$bin" 2>&1 | sed 's/^/ /' - fail=$((fail + 1)) - continue - } - actual=$(podman run --rm --pull=never --platform "$PLATFORM" \ - -v "$REPO":/work -w /work "$IMAGE" \ - "./$bin" 2>&1 || true) - else - echo " SKIP $name (no .M1 or .M1pp)" - continue - fi - - expected_content=$(cat "$expected") - - if [ "$actual" = "$expected_content" ]; then - echo " PASS $name" - pass=$((pass + 1)) - else - echo " FAIL $name" - echo " --- expected ---" - printf '%s\n' "$expected_content" | sed 's/^/ /' - echo " --- actual ---" - printf '%s\n' "$actual" | sed 's/^/ /' - fail=$((fail + 1)) - fi -done - -echo "$pass passed, $fail failed" -[ "$fail" -eq 0 ] diff --git a/p1/aarch64.py b/p1/aarch64.py @@ -1,413 +0,0 @@ -from common import ( - AddI, - ArchDef, - BranchReg, - CondB, - CondBZ, - Enter, - La, - LaBr, - LdArg, - Li, - LogI, - Mem, - Mov, - Nullary, - Rrr, - ShiftI, - le32, - register_arch, - round_up, -) - - -NAT = { - 'a0': 0, - 'a1': 1, - 'a2': 2, - 'a3': 3, - 'x4': 4, - 'x5': 5, - 't0': 9, - 't1': 10, - 't2': 11, - 's0': 19, - 's1': 20, - 's2': 21, - 's3': 22, - 'sp': 31, - 'xzr': 31, - 'lr': 30, - 'br': 17, - 'scratch': 16, - 'x8': 8, - 'save0': 23, - 'save1': 24, - 'save2': 25, -} - - -RRR_BASE = { - 'ADD': 0x8B000000, - 'SUB': 0xCB000000, - 'AND': 0x8A000000, - 'OR': 0xAA000000, - 'XOR': 0xCA000000, - 'SHL': 0x9AC02000, - 'SHR': 0x9AC02400, - 'SAR': 0x9AC02800, - 'DIV': 0x9AC00C00, -} - - -SYSCALL_NUMBERS = { - 'SYS_READ': 63, - 'SYS_WRITE': 64, - 'SYS_CLOSE': 57, - 'SYS_OPENAT': 56, - 'SYS_EXIT': 93, - 'SYS_CLONE': 220, - 'SYS_EXECVE': 221, - 'SYS_WAITID': 95, -} - - -def aa_rrr(base, rd, ra, rb): - d = NAT[rd] - a = NAT[ra] - b = NAT[rb] - return le32(base | (b << 16) | (a << 5) | d) - - -def aa_add_imm(rd, ra, imm12, sub=False): - d = NAT[rd] - a = NAT[ra] - base = 0xD1000000 if sub else 0x91000000 - return le32(base | ((imm12 & 0xFFF) << 10) | (a << 5) | d) - - -def aa_mov_rr(dst, src): - if dst == 'sp': - return aa_add_imm('sp', src, 0, sub=False) - if src == 'sp': - return aa_add_imm(dst, 'sp', 0, sub=False) - d = NAT[dst] - s = NAT[src] - return le32(0xAA000000 | (s << 16) | (31 << 5) | d) - - -def aa_ubfm(rd, ra, immr, imms): - d = NAT[rd] - a = NAT[ra] - return le32(0xD3400000 | (immr << 16) | (imms << 10) | (a << 5) | d) - - -def aa_sbfm(rd, ra, immr, imms): - d = NAT[rd] - a = NAT[ra] - return le32(0x93400000 | (immr << 16) | (imms << 10) | (a << 5) | d) - - -def aa_movz(rd, imm16): - d = NAT[rd] - return le32(0xD2800000 | ((imm16 & 0xFFFF) << 5) | d) - - -def aa_movn(rd, imm16): - d = NAT[rd] - return le32(0x92800000 | ((imm16 & 0xFFFF) << 5) | d) - - -def aa_materialize_small_imm(rd, imm): - if imm >= 0: - return aa_movz(rd, imm) - return aa_movn(rd, (~imm) & 0xFFFF) - - -def aa_ldst_uimm12(base, rt, rn, off_bytes, size_log2): - imm12 = off_bytes >> size_log2 - t = NAT[rt] - n = NAT[rn] - return le32(base | (imm12 << 10) | (n << 5) | t) - - -def aa_ldst_unscaled(base, rt, rn, off): - imm9 = off & 0x1FF - t = NAT[rt] - n = NAT[rn] - return le32(base | (imm9 << 12) | (n << 5) | t) - - -def aa_mem(op, rt, rn, off): - bases = { - 'LD': (0xF9400000, 3, 0xF8400000), - 'ST': (0xF9000000, 3, 0xF8000000), - 'LB': (0x39400000, 0, 0x38400000), - 'SB': (0x39000000, 0, 0x38000000), - } - uimm_base, size_log2, unscaled_base = bases[op] - scale = 1 << size_log2 - if off >= 0 and off % scale == 0 and off < (4096 << size_log2): - return aa_ldst_uimm12(uimm_base, rt, rn, off, size_log2) - if -256 <= off <= 255: - return aa_ldst_unscaled(unscaled_base, rt, rn, off) - if -2048 <= off <= 2047: - if off >= 0: - addr = aa_add_imm('scratch', rn, off, sub=False) - else: - addr = aa_add_imm('scratch', rn, -off, sub=True) - return addr + aa_ldst_uimm12(uimm_base, rt, 'scratch', 0, size_log2) - raise ValueError(f'aarch64 offset out of range for {op}: {off}') - - -def aa_cmp_skip(op, ra, rb): - a = NAT[ra] - b = NAT[rb] - cmp_hex = le32(0xEB000000 | (b << 16) | (a << 5) | 31) - skip_cond = { - 'BEQ': 1, - 'BNE': 0, - 'BLT': 10, - 'BLTU': 2, - }[op] - return cmp_hex + le32(0x54000040 | skip_cond) - - -def aa_br(reg): - return le32(0xD61F0000 | (NAT[reg] << 5)) - - -def aa_blr(reg): - return le32(0xD63F0000 | (NAT[reg] << 5)) - - -def aa_ret(): - return le32(0xD65F03C0) - - -def aa_epilogue(): - # Frame teardown, shared by ERET, TAIL, TAILR. Loads lr and the - # saved caller sp from the hidden header at native_sp+0/+8, then - # unwinds sp. Does NOT transfer control; the caller appends an - # aa_ret / aa_br as appropriate. - return ( - aa_mem('LD', 'lr', 'sp', 0) - + aa_mem('LD', 'x8', 'sp', 8) - + aa_mov_rr('sp', 'x8') - ) - - -def aa_lit64_prefix(rd): - ## 64-bit literal-pool prefix for LI: ldr xN, [pc,#8]; b PC+12. - ## The 8 bytes that follow in source become the literal; b skips them. - d = NAT[rd] - ldr_lit = 0x58000040 | d - b_plus12 = 0x14000003 - return le32(ldr_lit) + le32(b_plus12) - - -def aa_lit32_prefix(rd): - ## 32-bit literal-pool prefix for LA / LA_BR: ldr wN, [pc,#8]; b PC+8. - ## ldr w zero-extends into the full 64-bit register, so a 4-byte literal - ## is enough for any address in the stage0 layout (base 0x00600000, - ## programs well under 4 GB). This lets source use `&label` directly - ## without padding to 8 bytes. - d = NAT[rd] - ldr_lit = 0x18000040 | d - b_plus8 = 0x14000002 - return le32(ldr_lit) + le32(b_plus8) - - -def encode_li(_arch, row): - return aa_lit64_prefix(row.rd) - - -def encode_la(_arch, row): - return aa_lit32_prefix(row.rd) - - -def encode_labr(_arch, _row): - return aa_lit32_prefix('br') - - -def encode_mov(_arch, row): - # Portable `sp` is the frame-local base, which is 16 bytes above - # native sp (the backend's 2-word hidden header sits at the low end - # of each frame allocation). So reading sp into a register yields - # native_sp + 16, not native_sp itself. - if row.rs == 'sp': - return aa_add_imm(row.rd, 'sp', 16, sub=False) - return aa_mov_rr(row.rd, row.rs) - - -def encode_rrr(_arch, row): - if row.op == 'MUL': - d = NAT[row.rd] - a = NAT[row.ra] - b = NAT[row.rb] - return le32(0x9B000000 | (b << 16) | (31 << 10) | (a << 5) | d) - if row.op == 'REM': - d = NAT[row.rd] - a = NAT[row.ra] - b = NAT[row.rb] - sc = NAT['scratch'] - sdiv = 0x9AC00C00 | (b << 16) | (a << 5) | sc - msub = 0x9B008000 | (b << 16) | (a << 10) | (sc << 5) | d - return le32(sdiv) + le32(msub) - return aa_rrr(RRR_BASE[row.op], row.rd, row.ra, row.rb) - - -def encode_addi(_arch, row): - if row.imm >= 0: - return aa_add_imm(row.rd, row.ra, row.imm, sub=False) - return aa_add_imm(row.rd, row.ra, -row.imm, sub=True) - - -def encode_logi(_arch, row): - seq = aa_materialize_small_imm('scratch', row.imm) - base = { - 'ANDI': 0x8A000000, - 'ORI': 0xAA000000, - }[row.op] - return seq + aa_rrr(base, row.rd, row.ra, 'scratch') - - -def encode_shifti(_arch, row): - if row.op == 'SHLI': - return aa_ubfm(row.rd, row.ra, (-row.imm) & 63, 63 - row.imm) - if row.op == 'SHRI': - return aa_ubfm(row.rd, row.ra, row.imm, 63) - return aa_sbfm(row.rd, row.ra, row.imm, 63) - - -def encode_mem(_arch, row): - # Portable sp points to the frame-local base; the 2-word hidden - # header sits at native_sp+0/+8 and is not portable-addressable. - # Shift sp-relative offsets past the header. - off = row.off + 16 if row.rn == 'sp' else row.off - return aa_mem(row.op, row.rt, row.rn, off) - - -def encode_ldarg(_arch, row): - return aa_mem('LD', 'scratch', 'sp', 8) + aa_mem('LD', row.rd, 'scratch', 16 + 8 * row.slot) - - -def encode_branch_reg(_arch, row): - if row.kind == 'BR': - return aa_br(row.rs) - if row.kind == 'CALLR': - return aa_blr(row.rs) - if row.kind == 'TAILR': - return aa_epilogue() + aa_br(row.rs) - raise ValueError(f'unknown branch-reg kind: {row.kind}') - - -def encode_condb(_arch, row): - return aa_cmp_skip(row.op, row.ra, row.rb) + aa_br('br') - - -def encode_condbz(_arch, row): - a = NAT[row.ra] - br_hex = aa_br('br') - if row.op == 'BEQZ': - return le32(0xB5000000 | (2 << 5) | a) + br_hex - if row.op == 'BNEZ': - return le32(0xB4000000 | (2 << 5) | a) + br_hex - cmp_zero = le32(0xEB1F001F | (a << 5)) - bge = le32(0x54000040 | 10) - return cmp_zero + bge + br_hex - - -def encode_enter(arch, row): - frame_bytes = round_up(arch.stack_align, 2 * arch.word_bytes + row.size) - return ( - aa_add_imm('sp', 'sp', frame_bytes, sub=True) - + aa_mem('ST', 'lr', 'sp', 0) - + aa_add_imm('x8', 'sp', frame_bytes, sub=False) - + aa_mem('ST', 'x8', 'sp', 8) - ) - - -def encode_nullary(_arch, row): - if row.kind == 'B': - return aa_br('br') - if row.kind == 'CALL': - return aa_blr('br') - if row.kind == 'RET': - return aa_ret() - if row.kind == 'ERET': - return aa_epilogue() + aa_ret() - if row.kind == 'TAIL': - return aa_epilogue() + aa_br('br') - if row.kind == 'SYSCALL': - return ''.join([ - aa_mov_rr('x8', 'a0'), - aa_mov_rr('save0', 'a1'), - aa_mov_rr('save1', 'a2'), - aa_mov_rr('save2', 'a3'), - aa_mov_rr('a0', 'save0'), - aa_mov_rr('a1', 'save1'), - aa_mov_rr('a2', 'save2'), - aa_mov_rr('a3', 't0'), - aa_mov_rr('x4', 's0'), - aa_mov_rr('x5', 's1'), - le32(0xD4000001), - aa_mov_rr('a1', 'save0'), - aa_mov_rr('a2', 'save1'), - aa_mov_rr('a3', 'save2'), - ]) - raise ValueError(f'unknown nullary kind: {row.kind}') - - -def aa_start_stub(): - # Backend-owned :_start stub per docs/P1.md §Program Entry. Captures - # argc from [sp] and argv pointer from sp+8, calls p1_main under the - # one-word direct-result convention (a0=argc, a1=argv), then issues a - # native Linux sys_exit with p1_main's return value. Mirrors the - # m1pp-path stub in p1/P1-aarch64.M1pp (`%p1_entry`). - # - # Raw hex outside `DEFINE` bodies must be single-quoted so bootstrap - # M0 treats it as a literal byte run rather than a token. - def q(hex_bytes): - return f"'{hex_bytes}'" - return [ - ':_start', - q(aa_mem('LD', 'a0', 'sp', 0)), - q(aa_add_imm('a1', 'sp', 8, sub=False)), - q(aa_lit32_prefix('br')), - '&p1_main', - q(aa_blr('br')), - q(aa_movz('x8', 93)), - q(le32(0xD4000001)), - ] - - -ENCODERS = { - Li: encode_li, - La: encode_la, - LaBr: encode_labr, - Mov: encode_mov, - Rrr: encode_rrr, - AddI: encode_addi, - LogI: encode_logi, - ShiftI: encode_shifti, - Mem: encode_mem, - LdArg: encode_ldarg, - Nullary: encode_nullary, - BranchReg: encode_branch_reg, - CondB: encode_condb, - CondBZ: encode_condbz, - Enter: encode_enter, -} - - -register_arch( - ArchDef( - name='aarch64', - word_bytes=8, - stack_align=16, - syscall_numbers=SYSCALL_NUMBERS, - encoders=ENCODERS, - start_stub=aa_start_stub, - ) -) diff --git a/p1/common.py b/p1/common.py @@ -1,66 +0,0 @@ -from collections import namedtuple - - -ArchDef = namedtuple( - 'ArchDef', - 'name word_bytes stack_align syscall_numbers encoders start_stub', -) - -Banner = namedtuple('Banner', 'text') -Literal = namedtuple('Literal', 'name hex_by_arch') -Nullary = namedtuple('Nullary', 'name kind') -Li = namedtuple('Li', 'name rd') -La = namedtuple('La', 'name rd') -LaBr = namedtuple('LaBr', 'name') -Mov = namedtuple('Mov', 'name rd rs') -Rrr = namedtuple('Rrr', 'name op rd ra rb') -AddI = namedtuple('AddI', 'name rd ra imm') -LogI = namedtuple('LogI', 'name op rd ra imm') -ShiftI = namedtuple('ShiftI', 'name op rd ra imm') -Mem = namedtuple('Mem', 'name op rt rn off') -LdArg = namedtuple('LdArg', 'name rd slot') -BranchReg = namedtuple('BranchReg', 'name kind rs') -CondB = namedtuple('CondB', 'name op ra rb') -CondBZ = namedtuple('CondBZ', 'name op ra') -Enter = namedtuple('Enter', 'name size') - - -ARCH_REGISTRY = {} - - -def register_arch(arch): - if arch.name in ARCH_REGISTRY: - raise RuntimeError(f'duplicate arch registration: {arch.name}') - ARCH_REGISTRY[arch.name] = arch - - -def get_arch(name): - return ARCH_REGISTRY[name] - - -def registered_arches(): - return tuple(sorted(ARCH_REGISTRY)) - - -def byte(n): - return f'{n & 0xFF:02X}' - - -def le32(n): - return (n & 0xFFFFFFFF).to_bytes(4, 'little').hex().upper() - - -def le64(n): - return (n & 0xFFFFFFFFFFFFFFFF).to_bytes(8, 'little').hex().upper() - - -def word_hex(word_bytes, n): - if word_bytes == 4: - return le32(n) - if word_bytes == 8: - return le64(n) - raise ValueError(f'unsupported word size: {word_bytes}') - - -def round_up(align, n): - return ((n + align - 1) // align) * align diff --git a/p1/p1_gen.py b/p1/p1_gen.py @@ -1,257 +0,0 @@ -#!/usr/bin/env python3 -"""Generate P1 v2 DEFINE tables. - -This is a fresh generator for docs/P1v2.md. The ISA surface is described by -plain namedtuple rows, and each backend registers a simple row-type -> encoder -mapping. The emitted immediate/offset domains are still curated tables rather -than the full theoretical spec space, so extending coverage is a one-line data -edit instead of an architecture rewrite. - -Usage: - python3 p1/p1_gen.py [--arch ARCH] [build-root] - python3 p1/p1_gen.py --check [--arch ARCH] [build-root] - python3 p1/p1_gen.py --list-archs -""" - -import os -import sys -from itertools import product - -from common import ( - AddI, - Banner, - BranchReg, - CondB, - CondBZ, - Enter, - La, - LaBr, - LdArg, - Li, - Literal, - LogI, - Mem, - Mov, - Nullary, - Rrr, - ShiftI, - get_arch, - registered_arches, - word_hex, -) - -import aarch64 # noqa: F401 - imported for arch registration side effects - - -P1_GPRS = ('a0', 'a1', 'a2', 'a3', 't0', 't1', 't2', 's0', 's1', 's2', 's3') -P1_BASES = P1_GPRS + ('sp',) - -RRR_OPS = ('ADD', 'SUB', 'AND', 'OR', 'XOR', 'SHL', 'SHR', 'SAR', 'MUL', 'DIV', 'REM') -LOGI_OPS = ('ANDI', 'ORI') -SHIFT_OPS = ('SHLI', 'SHRI', 'SARI') -MEM_OPS = ('LD', 'ST', 'LB', 'SB') -CONDB_OPS = ('BEQ', 'BNE', 'BLT', 'BLTU') -CONDBZ_OPS = ('BEQZ', 'BNEZ', 'BLTZ') - -ADDI_IMMS = ( - -2048, -1024, -256, -128, -64, -48, -32, -24, -16, -12, -8, -7, -6, - -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 15, 16, 24, 32, 40, - 48, 63, 64, 127, 128, 255, 256, 512, 1024, 2047, -) - -LOGI_IMMS = ( - -1, 0, 1, 2, 3, 4, 6, 7, 8, 15, 16, 31, 32, 63, 64, 127, 255, 511, 1023, - 2047, -) - -SHIFT_IMMS = tuple(range(64)) - -MEM_OFFS = ( - -256, -128, -64, -48, -32, -24, -16, -8, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, - 15, 16, 24, 32, 40, 48, 56, 64, 128, 255, -) - -LDARG_SLOTS = tuple(range(32)) -ENTER_SIZES = tuple(range(0, 129)) - - -HEADER = """## p1_{arch}.M1 — GENERATED by p1/p1_gen.py. Do not edit by hand. -## -## This table targets the P1 v2 ISA described in docs/P1v2.md. -## Row shapes are shared; per-arch lowering lives in p1/<arch>.py. -""" - - -def imm_suffix(imm): - return f'NEG{-imm}' if imm < 0 else str(imm) - - -def rows(arch): - out = [] - - out.append(Banner('Materialization')) - for rd in P1_GPRS: - out.append(Li(name=f'LI_{rd.upper()}', rd=rd)) - for rd in P1_GPRS: - out.append(La(name=f'LA_{rd.upper()}', rd=rd)) - out.append(LaBr(name='LA_BR')) - - out.append(Banner('Moves')) - for rd, rs in product(P1_GPRS, P1_GPRS): - out.append(Mov(name=f'MOV_{rd.upper()}_{rs.upper()}', rd=rd, rs=rs)) - for rd in P1_GPRS: - out.append(Mov(name=f'MOV_{rd.upper()}_SP', rd=rd, rs='sp')) - - out.append(Banner('Register Arithmetic')) - for op, rd, ra, rb in product(RRR_OPS, P1_GPRS, P1_GPRS, P1_GPRS): - out.append(Rrr(name=f'{op}_{rd.upper()}_{ra.upper()}_{rb.upper()}', - op=op, rd=rd, ra=ra, rb=rb)) - - out.append(Banner('Immediate Arithmetic')) - for rd, ra, imm in product(P1_GPRS, P1_GPRS, ADDI_IMMS): - out.append(AddI(name=f'ADDI_{rd.upper()}_{ra.upper()}_{imm_suffix(imm)}', - rd=rd, ra=ra, imm=imm)) - for op, rd, ra, imm in product(LOGI_OPS, P1_GPRS, P1_GPRS, LOGI_IMMS): - out.append(LogI(name=f'{op}_{rd.upper()}_{ra.upper()}_{imm_suffix(imm)}', - op=op, rd=rd, ra=ra, imm=imm)) - for op, rd, ra, imm in product(SHIFT_OPS, P1_GPRS, P1_GPRS, SHIFT_IMMS): - out.append(ShiftI(name=f'{op}_{rd.upper()}_{ra.upper()}_{imm}', - op=op, rd=rd, ra=ra, imm=imm)) - - out.append(Banner('Memory')) - for op, rt, rn, off in product(MEM_OPS, P1_GPRS, P1_BASES, MEM_OFFS): - out.append(Mem(name=f'{op}_{rt.upper()}_{rn.upper()}_{imm_suffix(off)}', - op=op, rt=rt, rn=rn, off=off)) - - out.append(Banner('ABI Access')) - for rd, slot in product(P1_GPRS, LDARG_SLOTS): - out.append(LdArg(name=f'LDARG_{rd.upper()}_{slot}', rd=rd, slot=slot)) - - out.append(Banner('Branches')) - out.append(Nullary(name='B', kind='B')) - for rs in P1_GPRS: - out.append(BranchReg(name=f'BR_{rs.upper()}', kind='BR', rs=rs)) - for op, ra, rb in product(CONDB_OPS, P1_GPRS, P1_GPRS): - out.append(CondB(name=f'{op}_{ra.upper()}_{rb.upper()}', op=op, ra=ra, rb=rb)) - for op, ra in product(CONDBZ_OPS, P1_GPRS): - out.append(CondBZ(name=f'{op}_{ra.upper()}', op=op, ra=ra)) - - out.append(Banner('Calls And Returns')) - out.append(Nullary(name='CALL', kind='CALL')) - out.append(Nullary(name='RET', kind='RET')) - out.append(Nullary(name='ERET', kind='ERET')) - out.append(Nullary(name='TAIL', kind='TAIL')) - for rs in P1_GPRS: - out.append(BranchReg(name=f'CALLR_{rs.upper()}', kind='CALLR', rs=rs)) - for rs in P1_GPRS: - out.append(BranchReg(name=f'TAILR_{rs.upper()}', kind='TAILR', rs=rs)) - - out.append(Banner('Frame Management')) - for size in ENTER_SIZES: - out.append(Enter(name=f'ENTER_{size}', size=size)) - - out.append(Banner('System')) - out.append(Nullary(name='SYSCALL', kind='SYSCALL')) - for name, number in sorted(arch.syscall_numbers.items()): - out.append(Literal(name=name, hex_by_arch={arch.name: word_hex(arch.word_bytes, number)})) - - return out - - -def lower_name(name): - low = name.lower() - head, sep, rest = low.partition('_') - if not sep: - return low - if '_' not in rest: - return low - return f'{head}_{rest.replace("_", ",")}' - - -def encode_row(arch, row): - if isinstance(row, Literal): - return row.hex_by_arch[arch.name] - encoder = arch.encoders[type(row)] - return encoder(arch, row) - - -def emit(arch_name): - arch = get_arch(arch_name) - out = [HEADER.format(arch=arch.name).rstrip(), ''] - seen = set() - for row in rows(arch): - if isinstance(row, Banner): - out.append('') - out.append(f'## ---- {row.text}') - continue - name = lower_name(row.name) - if name in seen: - raise RuntimeError(f'duplicate DEFINE: {name}') - seen.add(name) - out.append(f'DEFINE {name} {encode_row(arch, row)}') - out.append('') - out.append('## ---- Program Entry') - out.append('## Backend-owned :_start stub per docs/P1.md §Program Entry.') - out.append('## Calls p1_main under the one-word direct-result convention') - out.append("## (a0=argc, a1=argv) and sys_exits its return value.") - out.extend(arch.start_stub()) - out.append('') - return '\n'.join(out) - - -def parse_args(argv): - check = False - archs = [] - positional = [] - i = 0 - while i < len(argv): - arg = argv[i] - if arg == '--check': - check = True - elif arg == '--list-archs': - print('\n'.join(registered_arches())) - sys.exit(0) - elif arg == '--arch': - i += 1 - if i >= len(argv): - raise SystemExit('--arch requires a value') - archs.append(argv[i]) - else: - positional.append(arg) - i += 1 - build_root = positional[0] if positional else os.path.join('build', 'p1v2') - if not archs: - archs = list(registered_arches()) - return check, archs, build_root - - -def main(argv=None): - check, archs, build_root = parse_args(argv or sys.argv[1:]) - had_diff = False - - for arch_name in archs: - arch = get_arch(arch_name) - dest_dir = os.path.join(build_root, arch.name) - path = os.path.join(dest_dir, f'p1_{arch.name}.M1') - content = emit(arch.name) - if check: - try: - with open(path) as f: - existing = f.read() - except FileNotFoundError: - existing = '' - if existing != content: - sys.stderr.write(f'DIFF: {path}\n') - had_diff = True - continue - os.makedirs(dest_dir, exist_ok=True) - with open(path, 'w') as f: - f.write(content) - print(f'wrote {path} ({len(content)} bytes)') - - if check and had_diff: - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/populate-upstream.sh b/populate-upstream.sh @@ -1,52 +0,0 @@ -#!/bin/sh -# Copy the files bootstrap.sh needs from live-bootstrap's stage0-posix into -# build/upstream/, mirroring the upstream directory layout. Runs on the host: -# the podman invocations in the Makefile only mount curdir, so anything -# bootstrap.sh needs has to land inside curdir first. -# -# Inputs per arch (A in AArch64|AMD64|riscv64): -# bootstrap-seeds/POSIX/$A/hex0-seed -# $A/hex0_$A.hex0 -# $A/hex1_$A.hex0 -# $A/hex2_$A.hex1 -# $A/catm_$A.hex2 vendored in src/ for AArch64, mirrored from upstream elsewhere -# $A/M0_$A.hex2 -# $A/ELF-<arch>.hex2 used by both bootstrap.sh (M0.hex2 link) and -# the Makefile's final program link -# -# Usage: populate-upstream.sh [UPSTREAM] -# UPSTREAM: path to live-bootstrap checkout (default: ../live-bootstrap) -set -eu - -UPSTREAM=${1:-../live-bootstrap} -S="$UPSTREAM/seed/stage0-posix" -OUT=build/upstream - -if [ ! -d "$S" ]; then - echo "populate-upstream.sh: expected '$S' to exist" >&2 - exit 1 -fi - -for A in AArch64 AMD64 riscv64; do - case "$A" in - AArch64) arch=aarch64 ;; - AMD64) arch=amd64 ;; - riscv64) arch=riscv64 ;; - esac - - mkdir -p "$OUT/bootstrap-seeds/POSIX/$A" "$OUT/$A" - - cp "$S/bootstrap-seeds/POSIX/$A/hex0-seed" "$OUT/bootstrap-seeds/POSIX/$A/" - cp "$S/$A/hex0_$A.hex0" "$OUT/$A/" - cp "$S/$A/hex1_$A.hex0" "$OUT/$A/" - cp "$S/$A/hex2_$A.hex1" "$OUT/$A/" - if [ "$A" = AArch64 ]; then - cp src/catm_AArch64.hex2 "$OUT/$A/" - else - cp "$S/$A/catm_$A.hex2" "$OUT/$A/" - fi - cp "$S/$A/M0_$A.hex2" "$OUT/$A/" - cp "$S/$A/ELF-$arch.hex2" "$OUT/$A/" -done - -echo "populate-upstream: copied into $OUT from $UPSTREAM" diff --git a/post.md b/post.md @@ -19,26 +19,26 @@ For decades, though, we had lost the seed. We were just going around using whatever compiler was on hand, figuring that there always would be one on hand, and trusting that it would do what it said on its tin. People noticed it was a bit odd that we had lost the chain, that we couldn't compile the compiler if -we had lost the compiler, but not much was done about it. +we lost the compiler, but not much was done about it. In 1983, Ken Thompson told a little horror story that spooked folks, but still, it didn't really change much. Like many good horror stories, this one was about a little invisible demon. Someone had put a virus into the compiler that -replicated itself into all future compilers and would scrub itself out of +replicated itself into all future compilers but would scrub itself out of existence if you ever tried compiling a program that went looking for it. All of our compilers were infected and none of us knew. Shivers. (Search for "Trusting Trust" if you want to read the full story). ## Bottle universe -Nobody did anything about it. Until Jeremiah Orians and Jan Nieuwenhuizen -decided to do something about it, and in 2023, they delivered "the full -bootstrap". A few hundred bytes of binary seed that defined a little language -called "hex0", and then source files from there on until they could compile -Fabrice Bellard's Tiny C Compiler (TCC). Once you have TCC, you compile an old -version of GCC that was still written in C (GCC moved to C++), then compile a -more recent GCC, then you use that to compile everything else, including Linux. -Truly amazing work. +Nobody did anything about it until someone did. Our heroes are Jeremiah Orians +and Jan Nieuwenhuizen; in 2023, they delivered "THE FULL BOOTSTRAP" (tm). A few +hundred bytes of binary seed that defined a little language called "hex0", and +then source files from there on until they could compile Fabrice Bellard's Tiny +C Compiler (TCC). Once you have TCC, you compile an old version of GCC that was +still written in C (GCC has long since moved to C++, woe be unto us all), then +compile a more recent GCC, then you use that to compile everything else, +including Linux. Truly amazing work. Jeremiah's early chain is quite beautiful: @@ -70,9 +70,10 @@ assembler. Features DEFINE substitution of names to hex bytes, hex and ascii strings, 8/16/32-bit decimal and hex immediates (!, @, %). From this base, they build a "subset C" compiler and a mini shell called "kaem" -in M0, then use that to build up to a little userspace called "M2-Planet" and -the Mes Scheme interpreter and ecosystem. They then define another C compiler -in Scheme (MesCC), and use that to compile TCC. And then you're off to the races. +in M0 (per architecture), then use that to build up to a little userspace +called "M2-Planet" and the Mes Scheme interpreter and ecosystem. They then +define another C compiler in Scheme (MesCC), and use that to compile TCC. And +then you're off to the races. There's something elegant and simple and beautiful about this bottle universe they constructed: an assembler and linker, a Scheme, a C. Timeless. What more @@ -80,23 +81,39 @@ do you need really? ## An alternative path -Up through M0, it's about 2000 lines of code (2KLOC). But it gets goopy quick. -It's ~30KLOC to get from M0 to MesCC, and then ~20KLOC for TCC itself. And -the early bits defined in M1, like the subset C compiler, are all -arch-specific. +Up through M0, it's about 2000 lines of code (2KLOC) per architecture. But it +gets goopy quick. It's ~30KLOC to get from M0 to MesCC, and then ~20KLOC for +TCC itself. And the early bits defined in M1, like the subset C compiler, are +all arch-specific. For fun, learning, and tremendous profit, I thought I'd play around with an alternative path. One that hopefully will knock down that KLOC count and maybe tidy up this little bottle universe that exists at the bottom of the software sea. -Three moves. First, write a richer macro preprocessor called m1pp (ht Bjarne) -that makes low-level programming ergonomic enough to skip the subset-C step. -Second, get to portability sooner, by defining a pseudo-ISA called P1 in m1pp -that each arch maps to via small expansions. Third, use P1 to host a Scheme, -and use that Scheme to host a C compiler. - -Here's what I'm aiming for, in Schemey pseudocode: +Two moves. + +**First, portability.** M1 source is arch-specific: not just the bytes you emit +depend on whether you're targeting aarch64 or amd64 or riscv, but the source +code you write is dictated by arch-specific defines. Replace that with a +portable pseudo-ISA called P1. The interface is entirely portable, implemented +by arch-specific defines. Each target ships a standard backend — +`P1A-aarch64.M1`, `P1A-amd64.M1`, and so on — that DEFINEs each P1 mnemonic as +the right raw bytes for that arch. Catm the backend in at build time and the +same P1 source assembles anywhere. Think of it as portable assembly, well +before you get to the portable assembly that we call C, but targetable from +a simple textual expander instead of a full-blown compiler. + +**Second, power.** M1 is austere — DEFINE substitution of simple names and raw +bytes — and writing anything of size in it is miserable. So layer a macro +preprocessor on top and you get M1pp (ht Bjarne). Named-parameter macros, +compile-time integer expressions, conditional expansion, local labels, a few +structural shorthands. Rewrite the P1 files with M1pp macros to get the +arch-specific `P1A.M1pp` and the arch-agnostic `P1.M1pp`; the latter I'll call +P1pp, our richer portable target. Expressive enough to host a Lisp in a file +you might actually want to read. + +Here's what I'm aiming for, in Lispy pseudocode: ``` ;; Bootstrap from seed, same as before @@ -106,103 +123,49 @@ Here's what I'm aiming for, in Schemey pseudocode: (define catm (hex2 catm.hex2)) (define M0 (hex2 (catm ELF.hex2 M0.hex2))) -;; Compile+Link +;; Compile+Link for arch-specific M1 source (defn exe (M1-src) (hex2 (catm ELF.hex2 (M0 M1-src)))) -;; m1pp itself is written in M1 and built via the M1 path -(define m1pp (exe (catm P1.M1 m1pp.M1))) +;; P1 — portable pseudo-ISA at the M1 level. +;; P1A.M1 is the arch-specific backend +;; m1pp is itself a P1 program. +(define m1pp (exe (catm P1A.M1 m1pp.P1))) -;; Now P1 is defined as m1pp macros: -;; P1A.M1pp is the arch-specific backend, P1pp.M1pp is the portable interface -(defn p1exe (P1-src) (exe (m1pp (catm P1A.M1pp P1pp.M1pp P1-src)))) +;; P1pp — P1 rewritten with m1pp macros. Assemble any P1pp source via m1pp. +;; P1A.M1pp is the arch-specific backend, now rewritten to use M1pp +;; P1.M1pp is the arch-agnostic interface +;; P1pp.P1pp is "libp1pp", P1pp's standard library, niceties and utilities for +;; programming in P1pp +(defn ppexe (src) (exe (m1pp (catm P1A.M1pp P1.M1pp P1pp.P1pp src)))) ;; To Scheme! -(define scheme (p1exe scheme.P1)) +(define scheme (ppexe scheme1.P1pp)) ;; And finally C -(defn scc (C-src) (p1exe (scheme cc.scm C-src))) +(defn scc (C-src) (ppexe (scheme cc.scm C-src))) (define tcc0 (scc tcc.c)) ;; compiler: scheme cc.scm (define tcc1 (tcc0 tcc.c)) ;; compiler: scheme-compiled tcc (define tcc (tcc1 tcc.c)) ;; compiler: tcc-compiled tcc ``` -So the new bits we need: -- m1pp: P1.M1, m1pp.M1 -- P1: P1A.M1pp, P1pp.M1pp -- Scheme: scheme.P1 -- C: cc.scm - -Today, I present just m1pp and P1. Next will come a Scheme in P1, and finally -enough of a C compiler in Scheme to compile the tcc source. - -## m1pp - -m1pp is a small macro expander layered on top of M0, with an aim of making it -bearable enough to write assembly programs that you might just want to linger -for a moment way down here near the bottom of the sea. It adds three things M0 -doesn't have: macros with named parameters, integer expressions that evaluate -at expand time, and conditional expansion. - -Features: - -- **Macros**: `%macro NAME(a, b) ... %endm`, called with `%NAME(x, y)`. Macro - bodies can themselves contain macro calls, so expansion is recursive. -- **Integer expressions**: emit little-endian hex from a Lisp expression — - `!(expr)` for 8 bits, `@(expr)` for 16, `%(expr)` for 32, `$(expr)` for 64. - The punctuation mirrors M0's decimal/hex immediate widths (`!@%`) and adds - `$` for the 64-bit case. -- **Conditional expansion**: `%select(cond, then, else)` evaluates `cond` as - an integer and emits exactly one of the two branches. Non-zero is true. -- **Token concat**: `a ## b` pastes two tokens into one identifier. -- **Struct / enum shorthands**: `%struct NAME { f1 f2 }` synthesizes - `%NAME.f1` → 0, `%NAME.f2` → 8, `%NAME.SIZE` → 16. `%enum` does the same - with stride 1 and a `COUNT` terminator. Saves hand-counting offsets for - records and small tag sets. -- **Local labels**: inside a macro body, `:@loop` and `&@loop` pick up a - fresh per-expansion suffix, so a macro can define its own labels without - colliding with itself at a second call site. -- **Stringify**: `%str(foo)` turns a word token into the string literal - `"foo"`. - -Comments (`#` and `;`) and M0 `DEFINE`s pass through untouched. That's it — no -`%ifdef`, no string manipulation, no floating point. Enough to encode -instructions and define P1 mnemonics; nothing more. - -The expression syntax is, of course, Lisp-shaped: - -``` -atoms: decimal or 0x-prefixed integer literals -calls: (+ a b) (- a b) (* a b) (/ a b) (% a b) (<< a b) (>> a b) - (= a b) (!= a b) (< a b) (<= a b) (> a b) (>= a b) - (& a b) (| a b) (^ a b) (~ a) -``` - -Here's a real macro from the aarch64 P1 backend — the "add register, immediate" -instruction: +So the new bits are: -``` -%macro aa64_add_imm(rd, ra, imm12) -%((| 0x91000000 (<< (& imm12 0xFFF) 10) (<< %aa64_reg(ra) 5) %aa64_reg(rd))) -%endm -``` +- P1: `P1A.M1` +- m1pp: `m1pp.P1` +- P1pp: `P1A.M1pp`, `P1.M1pp`, `P1pp.P1pp` +- Scheme: `scheme1.P1pp` +- C: `cc.scm` -A call like `%aa64_add_imm(a0, a0, 8)` expands to a single Lisp expression -that ORs together the opcode, the 12-bit immediate shifted into place, the -source register field, and the destination register field. The outer `%(...)` -evaluates that expression and emits it as four little-endian hex bytes, which -M0 and hex2 then hand to the linker. The nested `%aa64_reg(...)` calls in the -body show that macros can call other macros during expansion — `aa64_reg` is a -tiny macro per register name that produces the aarch64-native register number -(e.g. `a0` → 0, `s0` → 19). - -That one pattern — Lisp expression in, hex bytes out — covers all per-arch -instruction encoding. +Today, I present P1, M1pp, and P1pp — the portability move and the power move. +Next will come a Scheme in P1pp, and finally enough of a C compiler in +Scheme to compile the tcc source. ## P1 -P1 is a portable pseudo-ISA, targetable at hex2-ready bytes via m1pp macros. -Source looks a lot like assembly, but the mnemonics are m1pp calls and each -arch picks its own encoding. +P1 is a portable pseudo-ISA. Source looks a lot like assembly; each mnemonic +is a macro, and each arch ships a backend that realizes those mnemonics as +raw bytes for that target. Flip the backend file you catm in, and the same +P1 source assembles for a different arch. Registers: @@ -212,14 +175,13 @@ Registers: - `sp` — stack pointer. That's a modest budget — twelve visible registers. The goal is small backends -and uniform source across a 32-bit RISC-V, an aarch64, and an x86-64. Anything -beyond this that a backend needs for encoding (scratch regs, link register, -zero register) is backend-private and exposed only through the -portable ops. `LA_BR` is the one exception on the source side: it loads a -label into a hidden *branch-target register* that the immediately following -direct control-flow op (`B`, `CALL`, `TAIL`, or any conditional branch) then -consumes. The register-indirect forms (`BR`, `CALLR`, `TAILR`) take their -target in an ordinary register instead. +and uniform source across architectures. Anything beyond this that a backend +needs for encoding (scratch regs, link register, zero register) is +backend-private and exposed only through the portable ops. `LA_BR` is the one +exception on the source side: it loads a label into a hidden *branch-target +register* that the immediately following direct control-flow op (`B`, `CALL`, +`TAIL`, or any conditional branch) then consumes. The register-indirect forms +(`BR`, `CALLR`, `TAILR`) take their target in an ordinary register instead. A word is register-sized — 32 bits on 32-bit targets, 64 bits on 64-bit targets. `LD`/`ST` move words; `LB`/`SB` move bytes. No 16/32-bit slots in @@ -266,11 +228,109 @@ The backend emits a small per-arch `:_start` stub that captures the native entry state, calls `p1_main`, and hands its return value to `sys_exit`. The portable side never sees the raw entry stack. -P1 ships as a pair of files you catm before any P1 source: +This shape is aimed at portability and simplicity of targeting from pure macro +expansion, sacrificing some performance, which seems a reasonable tradeoff +for the bootstrap. + +P1 comes in two flavors, one per toolchain level. The M1-level flavor is a +pair of files you catm before any P1 source: + +- `P1A.M1` — arch-specific backend, one DEFINE per mnemonic. + +The M1pp-level flavor — P1pp — is the same idea with richer macros, where the +arch-specific encodings are specified in P1A.M1pp via macro instead of +hardcoded like in P1A.M1: + +- `P1A.M1pp` — arch-specific backend. +- `P1.M1pp` — the portable interface. + +## M1pp -- `P1A.M1pp` — architecture-specific backend implementing the portable - interface. -- `P1pp.M1pp` — the portable interface that P1 programs program against. +P1-at-the-M1-level gets us portability, but M1 is austere — DEFINE +substitution and raw bytes. Fine for hand-encoding a handful of instructions; +painful for anything of size. m1pp is a small macro expander layered on top +of M0 that makes low-level programming bearable enough to linger down here +near the bottom of the sea. It adds three things M0 doesn't have: macros with +named parameters, integer expressions that evaluate at expand time, and +conditional expansion. + +Features: + +- **Macros**: `%macro NAME(a, b) ... %endm`, called with `%NAME(x, y)`. Macro + bodies can themselves contain macro calls, so expansion is recursive. +- **Integer expressions**: emit little-endian hex from a Lisp expression — + `!(expr)` for 8 bits, `@(expr)` for 16, `%(expr)` for 32, `$(expr)` for 64. + The punctuation mirrors M0's decimal/hex immediate widths (`!@%`) and adds + `$` for the 64-bit case. +- **Conditional expansion**: `%select(cond, then, else)` evaluates `cond` as + an integer and emits exactly one of the two branches. Non-zero is true. +- **Token concat**: `a ## b` pastes two tokens into one identifier. +- **Struct / enum shorthands**: `%struct NAME { f1 f2 }` synthesizes + `%NAME.f1` → 0, `%NAME.f2` → 8, `%NAME.SIZE` → 16. `%enum` does the same + with stride 1 and a `COUNT` terminator. Saves hand-counting offsets for + records and small tag sets. +- **Local labels**: inside a macro body, `:@loop` and `&@loop` pick up a + fresh per-expansion suffix, so a macro can define its own labels without + colliding with itself at a second call site. +- **Scopes**: `%scope NAME ... %endscope` pushes a lexical scope. Inside + it, a label written `::foo` emits as `:NAME__foo` (stack joined by `__` + when nested), so several callers can reuse short names without collision. + Resolution follows the caller's scope, not the macro's, so a generic + `%break` or `%continue` inside a scoped loop lands on the right label. +- **Stringify**: `%str(foo)` turns a word token into the string literal + `"foo"`. + +Comments (`#` and `;`) and M0 `DEFINE`s pass through untouched. + +The expression syntax is, of course, Lisp-shaped: + +``` +atoms: decimal or 0x-prefixed integer literals +calls: (+ a b) (- a b) (* a b) (/ a b) (% a b) (<< a b) (>> a b) + (= a b) (!= a b) (< a b) (<= a b) (> a b) (>= a b) + (& a b) (| a b) (^ a b) (~ a) + (strlen "astr") +``` + +Here's a real macro from the aarch64 P1pp backend — the "add register, immediate" +instruction: + +``` +%macro aa64_add_imm(rd, ra, imm12) +%((| 0x91000000 (<< (& imm12 0xFFF) 10) (<< %aa64_reg(ra) 5) %aa64_reg(rd))) +%endm +``` + +A call like `%aa64_add_imm(a0, a0, 8)` expands to a single Lisp expression +that ORs together the opcode, the 12-bit immediate shifted into place, the +source register field, and the destination register field. The outer `%(...)` +evaluates that expression and emits it as four little-endian hex bytes, which +M0 and hex2 then hand to the linker. The nested `%aa64_reg(...)` calls in the +body show that macros can call other macros during expansion — `aa64_reg` is a +tiny macro per register name that produces the aarch64-native register number +(e.g. `a0` → 0, `s0` → 19). + +That one pattern — Lisp expression in, hex bytes out — covers all per-arch +instruction encoding. + +The scope mechanism is what lets libp1pp's standard library define a +generic function macro `%fn`: + +``` +%fn(parse_number, 16, { + ... + LA_BR &::done + BEQZ t0 + ... + ::done +}) +``` + +The `16` is the frame-local byte count passed through to `%enter`. The +`::done` labels mangle to `parse_number__done`, so every function gets +its own short namespace with no hand-prefixing, and scoped loops inside +the body (`%loop_scoped`, `%while_scoped_nez`, etc.) stack further — a +`%break` anywhere inside the innermost loop finds its `_end`. ## Three programs @@ -281,7 +341,7 @@ exit status: ``` :p1_main - %ret() + %ret :ELF_end ``` @@ -290,13 +350,13 @@ Hello world is a single `sys_write` from a leaf function: ``` :p1_main - %li(a0) %sys_write() + %li(a0) %sys_write %li(a1) %1 %0 %la(a2) &msg %li(a3) %14 %0 - %syscall() + %syscall %li(a0) %0 %0 - %ret() + %ret :msg "Hello, World! @@ -308,7 +368,7 @@ Hello world is a single `sys_write` from a leaf function: A few things to notice. `%li(a0)` and `%la(a2)` don't take the immediate as a macro argument — the backend emits a load-from-literal-pool prefix and the bytes that follow in source become the literal. Here -`%sys_write()` expands to the 8-byte Linux syscall number for write +`%sys_write` expands to the 8-byte Linux syscall number for write (the aarch64 backend's choice); `&msg` is a 4-byte label reference (addresses fit in 32 bits in the stage0 image layout). The two `%N %0` pairs are M0 4-byte decimal immediates padded to 8 bytes. @@ -318,37 +378,46 @@ A function call, with a helper that doubles its argument: ``` :double %shli(a0, a0, 1) - %ret() + %ret :p1_main %enter(0) - %la_br() &double - %call() - %eret() + %call(&double) + %eret :ELF_end ``` -`LA_BR` loads the hidden branch-target register; the next control-flow -op consumes it. `double` is a leaf and needs no frame. `p1_main` is not -— it calls `double`, so it opens a standard frame with `%enter(0)` to -preserve the hidden return-address state across the call, and closes it -with `%eret()`, which tears down the frame and returns in one step. Run with `./double a b c` and the exit -status is `8` (argc=4, doubled). +`double` is a leaf and needs no frame. `p1_main` is not — it calls +`double`, so it opens a standard frame with `%enter(0)` to preserve the +hidden return-address state across the call, and closes it with +`%eret`, which tears down the frame and returns in one step. Run with +`./double a b c` and the exit status is `8` (argc=4, doubled). ## What it cost Concrete sizes for what's landed today: -- `m1pp.M1`: ~4KLOC — the M1 source of the macro expander itself. +- `m1pp.P1`: ~4KLOC — the P1 source of the macro expander itself. - `P1.M1pp`: ~150 LOC — the portable P1 interface. -- `P1-aarch64.M1pp`: ~460 LOC — one arch backend. - -The pitch of this rewrite — shrinking the pre-Scheme KLOC count — can't be -fully cashed in until scheme.P1 and cc.scm land; that's where the 30KLOC of -M2-Planet-and-Mes gets compared against. But the shape is already visible: -once m1pp is up, adding a new architecture is a few hundred lines of macro -definitions, not a new port of a subset-C compiler. The arch-specific surface -is the thin file, not the thick one. - -Next post: a Scheme interpreter written in P1. +- `P1-{aarch64,riscv64,amd64}.M1pp`: ~{480,430,650} LOC — arch backends. +- `p1pp.P1pp`: ~1KLOC — libp1pp, the standard library of control-flow + macros and utilities. + +(These are non-comment, non-blank LOC) + +We can't really see the full payoff until we have scheme1.P1pp and cc.scm of +course, but I think it's a meaningful improvement over per-arch subset C +compilers, and it's a neat little macro language to boot. Adding support for a +new arch at this level is just a few hundred lines of macro definitions, not a +new compiler. + +I'll be working on a tiny Scheme implementation in P1pp, and will likely edit +P1 and M1pp as I do. + +The dream is that we go from P1pp to Scheme to C via nice direct hops and no +external dependencies until TCC, and specifically, to a tiny single-binary +cross-compiling TCC-like C compiler. A micro Clang/LLVM, maybe 80% of the bang +for 1% of the buck. Projects like TCC, QBE, and Tilde all make me hopeful that +this is possible, and as ever, people like Jeremiah, Jan, and Fabrice are +inspirations. Here's to the crazy ones. diff --git a/scripts/Containerfile.busybox b/scripts/Containerfile.busybox @@ -0,0 +1,18 @@ +## Minimal per-arch image used to run target-arch ELF (stage0 tools and +## final P1 programs). Built per --platform; tag as boot2-busybox:<arch>. +## +## docker.io/library/busybox:musl ships per-arch manifests for amd64, arm64 +## (aarch64), and riscv64. Pinning by index digest collapses to a single +## entry in the local store under multiple --platform pulls (the last wins), +## so we let `podman build --platform` pick the right per-arch manifest from +## the index and rely on per-arch tags (boot2-busybox:<arch>) to keep the +## resulting images distinct in the local store. +## +## Pinned to the busybox:musl index manifest (a multi-arch index, not a +## per-arch image). Per-arch entries within this index, fetched 2026-04-24: +## amd64 sha256:298efc24641ff8a1a285abdc555a0ce5ab7c42eb085e1be099f824188e069604 +## arm64 sha256:458a2ae4cb09bf96f8e24f135474b1552039738ed16ee470320a9c05c2da2004 +## riscv64 sha256:657f5a49af9288dc98d2bf45343e45c57c3caf3946aa9df436d05da320a8c863 + +FROM docker.io/library/busybox@sha256:19b646668802469d968a05342a601e78da4322a414a7c09b1c9ee25165042138 +CMD ["/bin/sh"] diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh @@ -0,0 +1,39 @@ +#!/bin/sh +## bootstrap.sh — bring up M0/hex2-0/catm from a 400-byte hex0-seed. +## +## Runs inside a target-arch busybox container. All produced binaries are +## target-arch Linux ELF and land in $OUT as: hex0 hex1 hex2-0 catm M0. +## +## Phase map (stage0-posix mescc-tools-{seed,mini}-kaem.kaem phases 0-3): +## 0) hex0-seed + hex0.hex0 -> hex0 +## 1) hex0 + hex1.hex0 -> hex1 +## 2) hex1 + hex2.hex1 -> hex2-0 +## 2b) hex2-0 + catm.hex2 -> catm +## 3a) catm : ELF.hex2 + M0.hex2 -> M0.combined.hex2 +## 3b) hex2-0 : M0.combined.hex2 -> M0 +## +## Inputs are read from vendor/seed/<arch>/ (vendored upstream). +## +## Usage: bootstrap.sh <arch> <out-dir> +## arch: aarch64 | amd64 | riscv64 +## out-dir: path (relative to /work) where tool binaries should land + +set -eu + +ARCH=$1 +OUT=$2 + +case "$ARCH" in + aarch64|amd64|riscv64) ;; + *) echo "bootstrap.sh: unsupported arch '$ARCH'" >&2 ; exit 1 ;; +esac + +S=vendor/seed/$ARCH +mkdir -p "$OUT" + +"$S/hex0-seed" "$S/hex0.hex0" "$OUT/hex0" +"$OUT/hex0" "$S/hex1.hex0" "$OUT/hex1" +"$OUT/hex1" "$S/hex2.hex1" "$OUT/hex2-0" +"$OUT/hex2-0" "$S/catm.hex2" "$OUT/catm" +"$OUT/catm" "$OUT/M0.combined.hex2" "$S/ELF.hex2" "$S/M0.hex2" +"$OUT/hex2-0" "$OUT/M0.combined.hex2" "$OUT/M0" diff --git a/scripts/build-native-tools.sh b/scripts/build-native-tools.sh @@ -0,0 +1,91 @@ +#!/bin/sh +## build-native-tools.sh — host-compile mescc-tools M1 + hex2 for dev-loop +## speed. +## +## These are NOT in the bootstrap chain: they're a fast substitute for the +## stage0 M0/hex2-0 produced by scripts/bootstrap.sh. Output is byte-exact +## with the bootstrap tools when invoked with the right flags (see +## scripts/build-p1.sh / scripts/build-p1pp.sh). Running them on the host +## avoids the per-byte syscall storm the stage0 tools incur under Apple's +## linux/arm64 VM (~150x faster on a typical P1 build). +## +## Source lookup (first match wins): +## 1. $MESCC_TOOLS_SRC (if set) +## 2. ../live-bootstrap/seed/stage0-posix/mescc-tools +## 3. ../mescc-tools (if M2libc is populated) + +set -eu + +REPO=$(cd "$(dirname "$0")/.." && pwd) +cd "$REPO" + +OUT=build/native-tools +mkdir -p "$OUT" + +find_src() { + if [ -n "${MESCC_TOOLS_SRC:-}" ]; then + if [ -f "$MESCC_TOOLS_SRC/M1-macro.c" ] && [ -f "$MESCC_TOOLS_SRC/M2libc/bootstrappable.c" ]; then + echo "$MESCC_TOOLS_SRC" + return 0 + fi + echo "build-native-tools.sh: MESCC_TOOLS_SRC=$MESCC_TOOLS_SRC is not a complete mescc-tools tree" >&2 + return 1 + fi + for d in \ + "$REPO/../live-bootstrap/seed/stage0-posix/mescc-tools" \ + "$REPO/../mescc-tools" + do + if [ -f "$d/M1-macro.c" ] && [ -f "$d/M2libc/bootstrappable.c" ]; then + echo "$d" + return 0 + fi + done + echo "build-native-tools.sh: no mescc-tools source found." >&2 + echo " set MESCC_TOOLS_SRC to a directory containing M1-macro.c and M2libc/," >&2 + echo " or fall back to the bootstrap path with M1PP_BOOTSTRAP_TOOLS=1." >&2 + return 1 +} + +SRC=$(find_src) + +: "${CC:=cc}" +CFLAGS="-O2 -std=c99 -D_GNU_SOURCE" + +m1_fresh() { + [ -x "$OUT/M1" ] || return 1 + for s in "$SRC/M1-macro.c" "$SRC/stringify.c" "$SRC/M2libc/bootstrappable.c"; do + [ "$OUT/M1" -nt "$s" ] || return 1 + done + return 0 +} +hex2_fresh() { + [ -x "$OUT/hex2" ] || return 1 + for s in "$SRC/hex2.c" "$SRC/hex2_linker.c" "$SRC/hex2_word.c" "$SRC/M2libc/bootstrappable.c"; do + [ "$OUT/hex2" -nt "$s" ] || return 1 + done + return 0 +} +m1pp_fresh() { + [ -x "$OUT/m1pp" ] || return 1 + [ "$OUT/m1pp" -nt "$REPO/M1pp/M1pp.c" ] || return 1 + return 0 +} + +if ! m1_fresh; then + echo " compiling $OUT/M1 from $SRC" + $CC $CFLAGS \ + "$SRC/M1-macro.c" "$SRC/stringify.c" "$SRC/M2libc/bootstrappable.c" \ + -o "$OUT/M1" +fi + +if ! hex2_fresh; then + echo " compiling $OUT/hex2 from $SRC" + $CC $CFLAGS \ + "$SRC/hex2.c" "$SRC/hex2_linker.c" "$SRC/hex2_word.c" "$SRC/M2libc/bootstrappable.c" \ + -o "$OUT/hex2" +fi + +if ! m1pp_fresh; then + echo " compiling $OUT/m1pp from M1pp/M1pp.c" + $CC -O2 -std=c99 M1pp/M1pp.c -o "$OUT/m1pp" +fi diff --git a/scripts/build-p1.sh b/scripts/build-p1.sh @@ -0,0 +1,110 @@ +#!/bin/sh +## build-p1.sh — assemble a P1v2 source (.P1 or .M1) into a runnable ELF. +## +## Pipeline: +## 1. lint — assert every P1v2 op token in <src> is defined +## 2. prune — strip DEFINEs the source doesn't reference +## 3. cat — pruned defs ++ <src> -> combined.M1 +## 4. M1 — combined.M1 -> .hex2 +## 5. cat — ELF header ++ .hex2 -> linked.hex2 +## 6. hex2 — linked.hex2 -> raw ELF +## 7. trim p_filesz, chmod 0700 +## +## Modes (selected via M1PP_BOOTSTRAP_TOOLS env): +## default host-side native mescc-tools M1/hex2 (~150x faster +## than stage0 on Apple Silicon). Built on demand by +## scripts/build-native-tools.sh. +## =1 bootstrap M0/hex2-0 inside a busybox container, with +## /tmp staging to dodge per-byte virtiofs syscall storm. +## Output is byte-identical with the native mode. +## +## Usage: scripts/build-p1.sh <arch> <source.P1|.M1> <output_binary> + +set -eu + +if [ "$#" -ne 3 ]; then + echo "usage: $0 <arch> <source> <output>" >&2 + exit 2 +fi + +ARCH=$1 +SRC=$2 +OUT=$3 + +REPO=$(cd "$(dirname "$0")/.." && pwd) +cd "$REPO" + +case "$ARCH" in + aarch64) PLATFORM=linux/arm64 ;; + amd64) PLATFORM=linux/amd64 ;; + riscv64) PLATFORM=linux/riscv64 ;; + *) echo "build-p1.sh: unsupported arch '$ARCH'" >&2; exit 1 ;; +esac + +P1_DEFS=build/p1v2/$ARCH/p1_$ARCH.M1 +ELF_HDR=vendor/seed/$ARCH/ELF.hex2 +BASE_ADDR=0x600000 ## must match the load address encoded in $ELF_HDR +TOOLS=build/$ARCH/tools ## bootstrap M0/hex2-0/catm (when M1PP_BOOTSTRAP_TOOLS=1) + +NAME=$(basename "$SRC" | sed 's/\.[^.]*$//') +WORK=build/p1/$ARCH/$NAME.work +mkdir -p "$WORK" "$(dirname "$OUT")" + +for f in "$P1_DEFS" "$ELF_HDR" scripts/lint.sh "$SRC"; do + if [ ! -e "$f" ]; then + echo "build-p1.sh: missing dependency: $f" >&2 + exit 1 + fi +done + +PRUNED=$WORK/p1.pruned.M1 +COMBINED=$WORK/combined.M1 +PROG_HEX2=$WORK/prog.hex2 +LINKED=$WORK/linked.hex2 +RAW=$WORK/prog.raw + +sh scripts/lint.sh "$P1_DEFS" "$SRC" + +awk 'NR==FNR{for(i=1;i<=NF;i++)u[$i]=1;next} /^DEFINE /{if($2 in u)print;next} {print}' \ + "$SRC" "$P1_DEFS" > "$PRUNED" + +cat "$PRUNED" "$SRC" > "$COMBINED" + +if [ "${M1PP_BOOTSTRAP_TOOLS:-0}" = 1 ]; then + for f in "$TOOLS/M0" "$TOOLS/hex2-0" "$TOOLS/catm"; do + [ -e "$f" ] || { echo "build-p1.sh: missing bootstrap dependency: $f" >&2; exit 1; } + done + cat "$ELF_HDR" > "$WORK/elf.hex2" + podman run --rm --pull=never --platform "$PLATFORM" \ + -v "$REPO":/work -w /work \ + "boot2-busybox:$ARCH" sh -ec " + set -eu + cp $COMBINED /tmp/combined.M1 + $TOOLS/M0 /tmp/combined.M1 /tmp/prog.hex2 + cp $WORK/elf.hex2 /tmp/elf.hex2 + $TOOLS/catm /tmp/linked.hex2 /tmp/elf.hex2 /tmp/prog.hex2 + $TOOLS/hex2-0 /tmp/linked.hex2 /tmp/prog.raw + cp /tmp/prog.hex2 $PROG_HEX2 + cp /tmp/linked.hex2 $LINKED + cp /tmp/prog.raw $RAW + " +else + NATIVE_M1=build/native-tools/M1 + NATIVE_HEX2=build/native-tools/hex2 + if [ ! -x "$NATIVE_M1" ] || [ ! -x "$NATIVE_HEX2" ]; then + sh scripts/build-native-tools.sh + fi + "$NATIVE_M1" --architecture "$ARCH" --little-endian \ + -f "$COMBINED" -o "$PROG_HEX2" + cat "$ELF_HDR" "$PROG_HEX2" > "$LINKED" + "$NATIVE_HEX2" --architecture "$ARCH" --little-endian \ + --base-address "$BASE_ADDR" \ + -f "$LINKED" -o "$RAW" +fi + +## Trim trailing zero padding past p_filesz (lives at byte offset 96 as a +## little-endian u32 in the ELF64 program header). The kernel zero-fills +## the BSS gap up to p_memsz at load time. +size=$(od -An -tu4 -N4 -j96 "$RAW" | tr -d ' ') +head -c "$size" "$RAW" > "$OUT" +chmod 0700 "$OUT" diff --git a/scripts/build-p1pp.sh b/scripts/build-p1pp.sh @@ -0,0 +1,79 @@ +#!/bin/sh +## build-p1pp.sh — assemble a P1pp source (.P1pp) into a runnable ELF. +## +## Pipeline: +## 1. cat — P1/P1-<arch>.M1pp ++ P1/P1.M1pp ++ <source.P1pp> -> combined.M1pp +## 2. m1pp — combined.M1pp -> expanded.M1 (macros -> raw hex + labels) +## 3. M1 — expanded.M1 -> prog.hex2 (stringify literals, pass hex through) +## 4. cat — vendor/seed/<arch>/ELF.hex2 ++ prog.hex2 -> linked.hex2 +## 5. hex2 — linked.hex2 -> raw ELF +## 6. trim p_filesz, chmod 0700 +## +## Uses the native-compiled m1pp + mescc-tools M1/hex2 (host binaries built +## by scripts/build-native-tools.sh). The host-compiled m1pp has 64K-token / +## 512K-text buffers, versus ~4K / 32K in the aarch64 self-hosted m1pp.P1; +## the combined backend + frontend + source easily overflows the latter. +## +## Usage: scripts/build-p1pp.sh <arch> <source.P1pp> <output_binary> + +set -eu + +if [ "$#" -ne 3 ]; then + echo "usage: $0 <arch> <source.P1pp> <output>" >&2 + exit 2 +fi + +ARCH=$1 +SRC=$2 +OUT=$3 + +REPO=$(cd "$(dirname "$0")/.." && pwd) +cd "$REPO" + +case "$ARCH" in + aarch64|amd64|riscv64) ;; + *) echo "build-p1pp.sh: unsupported arch '$ARCH'" >&2; exit 1 ;; +esac + +ELF_HDR=vendor/seed/$ARCH/ELF.hex2 +BASE_ADDR=0x600000 +FRONTEND=P1/P1.M1pp +BACKEND=P1/P1-$ARCH.M1pp + +for f in "$BACKEND" "$FRONTEND" "$ELF_HDR" "$SRC"; do + [ -e "$f" ] || { echo "build-p1pp.sh: missing input: $f" >&2; exit 1; } +done + +NATIVE_M1PP=build/native-tools/m1pp +NATIVE_M1=build/native-tools/M1 +NATIVE_HEX2=build/native-tools/hex2 +if [ ! -x "$NATIVE_M1PP" ] || [ ! -x "$NATIVE_M1" ] || [ ! -x "$NATIVE_HEX2" ]; then + sh scripts/build-native-tools.sh +fi + +NAME=$(basename "$SRC" .P1pp) +WORK=build/p1pp/$ARCH/$NAME.work +mkdir -p "$WORK" "$(dirname "$OUT")" + +COMBINED=$WORK/combined.M1pp +EXPANDED=$WORK/expanded.M1 +PROG_HEX2=$WORK/prog.hex2 +LINKED=$WORK/linked.hex2 +RAW=$WORK/prog.raw + +cat "$BACKEND" "$FRONTEND" "$SRC" > "$COMBINED" + +"$NATIVE_M1PP" "$COMBINED" "$EXPANDED" + +"$NATIVE_M1" --architecture "$ARCH" --little-endian \ + -f "$EXPANDED" -o "$PROG_HEX2" + +cat "$ELF_HDR" "$PROG_HEX2" > "$LINKED" + +"$NATIVE_HEX2" --architecture "$ARCH" --little-endian \ + --base-address "$BASE_ADDR" \ + -f "$LINKED" -o "$RAW" + +size=$(od -An -tu4 -N4 -j96 "$RAW" | tr -d ' ') +head -c "$size" "$RAW" > "$OUT" +chmod 0700 "$OUT" diff --git a/lint.sh b/scripts/lint.sh diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh @@ -0,0 +1,214 @@ +#!/bin/sh +## run-tests.sh — unified test runner for the m1pp and p1 fixture suites. +## +## Each suite is a directory of `<name>.<ext>` fixtures with sibling +## `<name>.expected` files. The runner builds each fixture, runs it inside +## the matching busybox container (`boot2-busybox:<arch>`), and diffs +## actual against expected output. Filenames starting with `_` are skipped +## (parked, ad-hoc debugging). +## +## Suites: +## m1pp tests/M1pp/<name>.M1 — P1v2 program built via build-p1.sh, +## run in container, stdout diffed. +## tests/M1pp/<name>.M1pp — m1pp expander parity test: m1pp +## binary (built once) consumes the +## fixture and writes <out>; diffed. +## aarch64 only. +## p1 tests/P1/<name>.P1pp — P1pp program built via build-p1pp.sh +## for each requested arch, run in +## container, stdout diffed. Default: +## all three arches. +## +## Usage: scripts/run-tests.sh --suite <m1pp|p1> [--arch ARCH] [name ...] + +set -eu + +SUITE= +ARCH= +NAMES= + +while [ "$#" -gt 0 ]; do + case "$1" in + --suite) shift; SUITE=$1 ;; + --suite=*) SUITE=${1#--suite=} ;; + --arch) shift; ARCH=$1 ;; + --arch=*) ARCH=${1#--arch=} ;; + --) shift; while [ "$#" -gt 0 ]; do NAMES="$NAMES $1"; shift; done; break ;; + -*) echo "$0: unknown flag '$1'" >&2; exit 2 ;; + *) NAMES="$NAMES $1" ;; + esac + shift +done + +case "$SUITE" in + m1pp|p1) ;; + "") echo "$0: --suite required (m1pp | p1)" >&2; exit 2 ;; + *) echo "$0: unknown suite '$SUITE'" >&2; exit 2 ;; +esac + +REPO=$(cd "$(dirname "$0")/.." && pwd) +cd "$REPO" + +platform_of() { + case "$1" in + aarch64) echo linux/arm64 ;; + amd64) echo linux/amd64 ;; + riscv64) echo linux/riscv64 ;; + *) echo "$0: unknown arch '$1'" >&2; return 1 ;; + esac +} + +run_in_container() { + arch=$1; shift + podman run --rm --pull=never --platform "$(platform_of "$arch")" \ + -v "$REPO":/work -w /work \ + "boot2-busybox:$arch" "$@" +} + +discover() { + dir=$1; ext=$2 + ls "$dir" 2>/dev/null \ + | sed -n "s/^\\([^_][^.]*\\)\\.$ext\$/\\1/p" \ + | sort -u +} + +PASS=0 +FAIL=0 + +report() { + label=$1; status=$2 + case "$status" in + PASS) PASS=$((PASS + 1));; + FAIL) FAIL=$((FAIL + 1));; + esac + echo " $status $label" +} + +show_diff() { + expected=$1; actual=$2 + echo " --- expected ---" + printf '%s\n' "$expected" | sed 's/^/ /' + echo " --- actual ---" + printf '%s\n' "$actual" | sed 's/^/ /' +} + +## --- m1pp suite --------------------------------------------------------- + +m1pp_build_expander() { + arch=$1 + bin=build/$arch/m1pp + src=M1pp/M1pp.P1 + defs=build/p1v2/$arch/p1_$arch.M1 + if [ -x "$bin" ] && [ "$bin" -nt "$src" ] && [ "$bin" -nt "$defs" ]; then + return 0 + fi + sh scripts/build-p1.sh "$arch" "$src" "$bin" >/dev/null 2>&1 || { + echo "FATAL: failed to build m1pp expander for $arch" >&2 + sh scripts/build-p1.sh "$arch" "$src" "$bin" 2>&1 | sed 's/^/ /' >&2 + exit 1 + } +} + +run_m1pp_suite() { + if [ -z "$ARCH" ]; then + ARCHES="aarch64 amd64 riscv64" + else + ARCHES=$ARCH + fi + if [ -z "$NAMES" ]; then + m1=$(discover tests/M1pp M1) + m1pp=$(discover tests/M1pp M1pp) + NAMES=$(printf '%s\n%s\n' "$m1" "$m1pp" | sort -u | tr '\n' ' ') + fi + for name in $NAMES; do + expected=tests/M1pp/$name.expected + m1_src=tests/M1pp/$name.M1 + m1pp_src=tests/M1pp/$name.M1pp + + if [ ! -e "$expected" ]; then + echo " SKIP $name (no .expected)" + continue + fi + expected_content=$(cat "$expected") + + for arch in $ARCHES; do + label="[$arch] $name" + if [ -e "$m1pp_src" ]; then + m1pp_build_expander "$arch" + outfile=build/$arch/m1pp-out/$name + mkdir -p "$(dirname "$outfile")" + rm -f "$outfile" + run_in_container "$arch" "./build/$arch/m1pp" "$m1pp_src" "$outfile" \ + >/dev/null 2>&1 || true + actual=$([ -e "$outfile" ] && cat "$outfile" || echo "") + elif [ -e "$m1_src" ]; then + bin=build/$arch/m1pp-tests/$name + if ! sh scripts/build-p1.sh "$arch" "$m1_src" "$bin" >/dev/null 2>&1; then + report "$label" FAIL + sh scripts/build-p1.sh "$arch" "$m1_src" "$bin" 2>&1 | sed 's/^/ /' + continue + fi + actual=$(run_in_container "$arch" "./$bin" 2>&1 || true) + else + echo " SKIP $name (no .M1 or .M1pp)" + break + fi + + if [ "$actual" = "$expected_content" ]; then + report "$label" PASS + else + report "$label" FAIL + show_diff "$expected_content" "$actual" + fi + done + done +} + +## --- p1 suite ----------------------------------------------------------- + +run_p1_suite() { + if [ -z "$ARCH" ]; then + ARCHES="aarch64 amd64 riscv64" + else + ARCHES=$ARCH + fi + if [ -z "$NAMES" ]; then + NAMES=$(discover tests/P1 P1pp) + fi + for name in $NAMES; do + fixture=tests/P1/$name.P1pp + expected=tests/P1/$name.expected + if [ ! -e "$fixture" ]; then + echo " SKIP $name (no .P1pp)"; continue + fi + if [ ! -e "$expected" ]; then + echo " SKIP $name (no .expected)"; continue + fi + expected_content=$(cat "$expected") + + for arch in $ARCHES; do + label="[$arch] $name" + bin=build/$arch/p1-tests/$name + if ! sh scripts/build-p1pp.sh "$arch" "$fixture" "$bin" >/dev/null 2>&1; then + report "$label" FAIL + sh scripts/build-p1pp.sh "$arch" "$fixture" "$bin" 2>&1 | sed 's/^/ /' + continue + fi + actual=$(run_in_container "$arch" "./$bin" 2>&1 || true) + if [ "$actual" = "$expected_content" ]; then + report "$label" PASS + else + report "$label" FAIL + show_diff "$expected_content" "$actual" + fi + done + done +} + +case "$SUITE" in + m1pp) run_m1pp_suite ;; + p1) run_p1_suite ;; +esac + +echo "$PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ] diff --git a/tests/m1pp/00-hello.M1 b/tests/M1pp/00-hello.M1 diff --git a/tests/m1pp/00-hello.expected b/tests/M1pp/00-hello.expected diff --git a/tests/m1pp/01-passthrough.M1pp b/tests/M1pp/01-passthrough.M1pp diff --git a/tests/m1pp/01-passthrough.expected b/tests/M1pp/01-passthrough.expected diff --git a/tests/m1pp/02-defs.M1pp b/tests/M1pp/02-defs.M1pp diff --git a/tests/m1pp/02-defs.expected b/tests/M1pp/02-defs.expected diff --git a/tests/m1pp/03-builtins.M1pp b/tests/M1pp/03-builtins.M1pp diff --git a/tests/m1pp/03-builtins.expected b/tests/M1pp/03-builtins.expected diff --git a/tests/m1pp/04-expr-ops.M1pp b/tests/M1pp/04-expr-ops.M1pp diff --git a/tests/m1pp/04-expr-ops.expected b/tests/M1pp/04-expr-ops.expected diff --git a/tests/m1pp/05-int-atoms.M1pp b/tests/M1pp/05-int-atoms.M1pp diff --git a/tests/m1pp/05-int-atoms.expected b/tests/M1pp/05-int-atoms.expected diff --git a/tests/m1pp/06-paste.M1pp b/tests/M1pp/06-paste.M1pp diff --git a/tests/m1pp/06-paste.expected b/tests/M1pp/06-paste.expected diff --git a/tests/m1pp/07-rescan.M1pp b/tests/M1pp/07-rescan.M1pp diff --git a/tests/m1pp/07-rescan.expected b/tests/M1pp/07-rescan.expected diff --git a/tests/m1pp/08-select.M1pp b/tests/M1pp/08-select.M1pp diff --git a/tests/m1pp/08-select.expected b/tests/M1pp/08-select.expected diff --git a/tests/m1pp/09-args.M1pp b/tests/M1pp/09-args.M1pp diff --git a/tests/m1pp/09-args.expected b/tests/M1pp/09-args.expected diff --git a/tests/m1pp/10-full-parity.M1pp b/tests/M1pp/10-full-parity.M1pp diff --git a/tests/m1pp/10-full-parity.expected b/tests/M1pp/10-full-parity.expected diff --git a/tests/m1pp/11-local-labels.M1pp b/tests/M1pp/11-local-labels.M1pp diff --git a/tests/m1pp/11-local-labels.expected b/tests/M1pp/11-local-labels.expected diff --git a/tests/m1pp/12-braced-args.M1pp b/tests/M1pp/12-braced-args.M1pp diff --git a/tests/m1pp/12-braced-args.expected b/tests/M1pp/12-braced-args.expected diff --git a/tests/m1pp/13-parenless-control.M1pp b/tests/M1pp/13-parenless-control.M1pp diff --git a/tests/m1pp/13-parenless-control.expected b/tests/M1pp/13-parenless-control.expected diff --git a/tests/m1pp/13-parenless.M1pp b/tests/M1pp/13-parenless.M1pp diff --git a/tests/m1pp/13-parenless.expected b/tests/M1pp/13-parenless.expected diff --git a/tests/m1pp/14-str-builtin.M1pp b/tests/M1pp/14-str-builtin.M1pp diff --git a/tests/m1pp/14-str-builtin.expected b/tests/M1pp/14-str-builtin.expected diff --git a/tests/m1pp/14-str-paste.M1pp b/tests/M1pp/14-str-paste.M1pp diff --git a/tests/m1pp/14-str-paste.expected b/tests/M1pp/14-str-paste.expected diff --git a/tests/m1pp/15-struct.M1pp b/tests/M1pp/15-struct.M1pp diff --git a/tests/m1pp/15-struct.expected b/tests/M1pp/15-struct.expected diff --git a/tests/m1pp/16-enum.M1pp b/tests/M1pp/16-enum.M1pp diff --git a/tests/m1pp/16-enum.expected b/tests/M1pp/16-enum.expected diff --git a/tests/m1pp/17-scopes.M1pp b/tests/M1pp/17-scopes.M1pp diff --git a/tests/m1pp/17-scopes.expected b/tests/M1pp/17-scopes.expected diff --git a/tests/m1pp/_04-strlen-badarg.M1pp b/tests/M1pp/_04-strlen-badarg.M1pp diff --git a/tests/m1pp/_12-braced-malformed.M1pp b/tests/M1pp/_12-braced-malformed.M1pp diff --git a/tests/m1pp/_14-str-malformed.M1pp b/tests/M1pp/_14-str-malformed.M1pp diff --git a/tests/p1/argc_exit.P1pp b/tests/P1/argc_exit.P1pp diff --git a/tests/p1/argc_exit.expected b/tests/P1/argc_exit.expected diff --git a/tests/p1/double.P1pp b/tests/P1/double.P1pp diff --git a/tests/p1/double.expected b/tests/P1/double.expected diff --git a/tests/p1/hello.P1pp b/tests/P1/hello.P1pp diff --git a/tests/p1/hello.expected b/tests/P1/hello.expected diff --git a/tests/p1/p1-aliasing.P1pp b/tests/P1/p1-aliasing.P1pp diff --git a/tests/p1/p1-aliasing.expected b/tests/P1/p1-aliasing.expected diff --git a/tests/p1/p1-call.P1pp b/tests/P1/p1-call.P1pp diff --git a/tests/p1/p1-call.expected b/tests/P1/p1-call.expected diff --git a/tests/demo.M1 b/tests/demo.M1 @@ -1,303 +0,0 @@ -## P1 broader-ISA demo — portable across aarch64, amd64, riscv64. -## -## Exercises the P1 ISA in tranches (see P1.md §"Instruction set"). -## Each op is applied with non-identity operands so a mis-encoded op -## produces a detectably-wrong final r1. If every op in every tranche -## is correct, r1 ends at 5, stdout is "P1 = 5\n", exit is 5. -## -## Tranche 1: reg-reg-reg arith (11 ops). Each step's result is -## unique vs. neighbor ops on the same operands, e.g. -## ADD(5,3)=8 SUB(8,3)=5 XOR(5,3)=6 OR(6,3)=7 AND(7,5)=5 -## MUL(5,3)=15 DIV(15,3)=5 REM(5,7)=5 SHL(5,3)=40 SHR(40,3)=5. -## SAR vs SHR agree on positive values; SAR is separately tested -## on a negative value in r4 (SAR(-1,3)=-1, SHR would give huge). -## Tranche 2: immediate forms. ADDI tested with BOTH positive and -## negative imm12 (proves signed-immediate encoding). SHLI/SHRI -## with non-zero shift amount. ANDI/ORI with overlapping-bit -## masks so the result is distinguishable from ADDI/XORI. SARI -## tested on negative value via r4. -## (No SUBI/XORI/MULI in P1 — see PLAN.md §"Feature floor".) -## Tranche 3: LA + memory round-trip. LD/ST (64-bit) and LB/SB -## (8-bit zero-extended). 32-bit LW/SW dropped from the ISA — -## synthesize from LD + mask/shift if needed. Signed imm12 on -## LD/ST is exercised via a NEG8-offset round-trip through -## scratch_mid, mirroring the ADDI NEG1/NEG3 signed-imm test. -## Tranche 4: LI_BR-indirect branches. B, BEQ, BNE, BLT each tested -## in BOTH taken and fall-through directions so an inverted -## condition encoding doesn't pass. BLT additionally tested with -## a negative operand (-1 < 0 signed) to prove signed semantics. -## BLTU/BGEU/BGE dropped from the ISA — see P1.md. -## Tranche 5: CALL / RET / TAIL / PROLOGUE / EPILOGUE. Nested CALL -## stresses PROLOGUE lr-save; TAIL unwinds the frame. Stack -## balance is additionally verified by snapshotting sp via -## MOV_R6_SP before tranche 5 and comparing after — a TAIL that -## omits its epilogue (or whose epilogue clobbers the branch -## target) leaks fn_parent_tail's 16-byte frame and the delta -## folds into the accumulator. All three arches now run real -## sp-moving PROLOGUE/EPILOGUE, so the check bites everywhere. -## -## Run-and-verify: -## make PROG=demo ARCH=<arch> run && echo "exit=$?" -## expected stdout: "P1 = 5\n" expected exit: 5 - -:_start - ## Setup: r1 is the running accumulator; r2/r5 are arith partners. - ## r1 is seeded via the LI_R6 → MOV_R1_R6 roundtrip so the LI_R6 - ## encoding gets exercised — otherwise r6 is only written by - ## MOV_R6_R1 in the exit path. Clobbering r1 to 0 between the two - ## ops means a broken MOV_R1_R6 (wrong source reg) leaves r1=0 and - ## poisons the chain; a broken LI_R6 (wrong dest reg or wrong - ## literal-slot offset) puts the wrong value in r6 and MOV - ## propagates it. - li_r6 '05000000' # r6 = 5 via LI_R6 (discriminator) - li_r1 '00000000' # clobber r1 before the MOV - mov_r1,r6 # r1 = r6 = 5 - li_r2 '03000000' # r2 = 3 (arith partner & shift amount) - li_r5 '05000000' # r5 = 5 (AND partner) - - ## Tranche 1: reg-reg-reg arith. - add_r1,r1,r2 # 5 + 3 = 8 - sub_r1,r1,r2 # 8 - 3 = 5 - xor_r1,r1,r2 # 5 ^ 3 = 6 - or_r1,r1,r2 # 6 | 3 = 7 - and_r1,r1,r5 # 7 & 5 = 5 - mul_r1,r1,r2 # 5 * 3 = 15 - div_r1,r1,r2 # 15 / 3 = 5 - li_r5 '07000000' # r5 = 7 for REM - rem_r1,r1,r5 # 5 % 7 = 5 - shl_r1,r1,r2 # 5 << 3 = 40 - shr_r1,r1,r2 # 40 >> 3 = 5 - - ## SAR discriminator: on positive values SAR and SHR agree, so - ## put a negative value in r4 and check that SAR preserves the - ## sign bits. Then fold r4 into r1 so a misbehaving SAR poisons - ## the accumulator. - li_r4 '00000000' # r4 = 0 - addi_r4,r4,neg1 # r4 = 0 + (-1) = -1 (sign-extended 64-bit) - sar_r4,r4,r2 # r4 = -1 >> 3 = -1 (SHR would be 0x1FFF...FFFF) - add_r1,r1,r4 # r1 = 5 + (-1) = 4 - addi_r1,r1,1 # r1 = 4 + 1 = 5 - - ## Tranche 2: immediate forms. Chain 5 → 8 → 5 → 10 → 5 → 4 → 5 - ## plus a SARI discriminator via r4. - addi_r1,r1,3 # 5 + 3 = 8 - addi_r1,r1,neg3 # 8 + (-3) = 5 (signed imm12) - shli_r1,r1,1 # 5 << 1 = 10 (non-zero shift) - shri_r1,r1,1 # 10 >> 1 = 5 - andi_r1,r1,6 # 5 & 6 = 4 (0b101 & 0b110 = 0b100) - ori_r1,r1,1 # 4 | 1 = 5 (aarch64 bitmask-immediate - # requires valid-mask imm; 5 is invalid, 1 is - # valid. Tranche 1 OR_R1_R1_R2 (6|3=7) already - # discriminates OR from ADD/XOR on overlapping - # bits; ORI here just tests the imm-ORR encoding - # exists and doesn't crash, plus distinguishes - # from AND (4&1=0 would fail the chain).) - - li_r4 '00000000' - addi_r4,r4,neg1 # r4 = -1 - sari_r4,r4,1 # r4 = -1 >> 1 = -1 (SHRI would be 0x7FFF...FFFF) - add_r1,r1,r4 # r1 = 5 + (-1) = 4 - addi_r1,r1,1 # r1 = 5 - - ## Tranche 3: memory round-trip. For each width, store r1, - ## clobber r1 to 0, then reload. A broken ST/LD leaves r1 != 5. - la_r4 &scratch - - st_r1,r4,0 # [scratch+0..8] = r1 (= 5) - li_r1 '00000000' - ld_r1,r4,0 # r1 = [scratch+0..8] -> 5 - - ## Non-zero imm12 on the 64-bit forms. Distinct value 8 at +8 so - ## an LD_R4_8 that silently aliased to +0 would read 5 and fail the - ## SUB step; a broken ST_R4_8 leaves [+8]=0 (scratch init) which the - ## LD observes. The +0 round-trip above anchors the imm12=0 case. - li_r1 '08000000' # distinct from 5 - st_r1,r4,8 # [scratch+8..16] = 8 - li_r1 '00000000' - ld_r1,r4,8 # r1 = [scratch+8..16] -> 8 - sub_r1,r1,r2 # r1 = 8 - 3 = 5 (r2 still 3 from setup) - - sb_r1,r4,16 # [scratch+16] = r1 (low byte) - li_r1 '00000000' - lb_r1,r4,16 # r1 = zext [scratch+16] -> 5 - - ## Negative imm12 on the memory forms. LA to scratch_mid (= scratch+16) - ## then round-trip a distinct sentinel (13) via [r4 + -8] = scratch+8. - ## Proves the signed offset encoding sign-extends on LD/ST the same - ## way ADDI NEG1/NEG3 proves it on arith. A symmetric "both sides - ## miscompiled to the same wrong offset" bug could still false-pass, - ## but the common cases (sign bit dropped, imm zero-extended) blow - ## up via segfault or a mismatched round-trip value. - la_r4 &scratch_mid - li_r1 '0D000000' # r1 = 13 (distinct sentinel) - st_r1,r4,neg8 # [scratch+8] = 13 (overwrites +8 slot's old 8) - li_r1 '00000000' - ld_r1,r4,neg8 # r1 = [scratch+8] -> 13 - addi_r1,r1,neg3 # r1 = 13 - 3 = 10 - shri_r1,r1,1 # r1 = 10 >> 1 = 5 - - ## Tranche 4: branches. r2=0, r3=1 to start; each subtest resets - ## as needed. Taken-path subtests clobber r1 on fall-through; - ## fall-through subtests clobber r1 on incorrect branch. - li_r2 '00000000' # r2 = 0 - li_r3 '01000000' # r3 = 1 - - ## B — unconditional. Correct: jump to b4_1_ok, skipping clobber. - li_br &b4_1_ok - b - li_r1 '00000000' -:b4_1_ok - - ## BEQ taken: r3=0 so r2==r3. - li_r3 '00000000' - li_br &b4_2_ok - beq_r2,r3 # 0 == 0, taken - li_r1 '00000000' -:b4_2_ok - li_r3 '01000000' # restore r3 = 1 - - ## BEQ fall-through: 0 != 1, branch must NOT fire. If it - ## (incorrectly) fires we jump to b4_3_bad and clobber r1. - li_br &b4_3_bad - beq_r2,r3 # 0 == 1? no, fall through - li_br &b4_3_ok - b -:b4_3_bad - li_r1 '00000000' -:b4_3_ok - - ## BNE taken: 0 != 1. - li_br &b4_4_ok - bne_r2,r3 # 0 != 1, taken - li_r1 '00000000' -:b4_4_ok - - ## BNE fall-through: r3=0 so r2==r3; branch must NOT fire. - li_r3 '00000000' - li_br &b4_5_bad - bne_r2,r3 # 0 != 0? no, fall through - li_br &b4_5_ok - b -:b4_5_bad - li_r1 '00000000' -:b4_5_ok - li_r3 '01000000' # restore r3 = 1 - - ## BLT taken: 0 < 1 (signed). - li_br &b4_6_ok - blt_r2,r3 # 0 < 1, taken - li_r1 '00000000' -:b4_6_ok - - ## BLT fall-through: 1 < 0 is false. - li_r2 '01000000' # r2 = 1 - li_r3 '00000000' # r3 = 0 - li_br &b4_7_bad - blt_r2,r3 # 1 < 0? no, fall through - li_br &b4_7_ok - b -:b4_7_bad - li_r1 '00000000' -:b4_7_ok - - ## BLT signed discrimination: -1 < 0 must be taken. If BLT were - ## accidentally unsigned, -1 as 0xFFFF...FFFF > 0 and the branch - ## would not fire. - li_r2 '00000000' # r2 = 0 - li_r4 '00000000' - addi_r4,r4,neg1 # r4 = -1 (sign-extended) - li_br &b4_8_ok - blt_r4,r2 # -1 < 0 (signed)? yes, taken - li_r1 '00000000' -:b4_8_ok - - ## Tranche 5: CALL / RET / PROLOGUE / EPILOGUE / TAIL. - ## fn_identity does its own nested CALL to fn_inner — if PROLOGUE - ## doesn't spill lr correctly, the inner CALL clobbers the - ## return-to-_start address and we crash or hang. The function - ## bodies live inline below the subtests, guarded by a b over - ## them so we don't fall through after the last subtest. - ## - ## Stack-balance discriminator for TAIL: snapshot sp into r6 - ## (callee-saved) before any CALL. A correctly-paired call tree - ## nets to sp_after == sp_before. A TAIL that skips its epilogue - ## leaks fn_parent_tail's 16-byte frame — the delta is folded - ## into the accumulator below via SUB r1, r1, delta. - mov_r6,sp # r6 = sp snapshot (pre-tranche) - - li_br &fn_identity - call # nested-CALL test: returns r1 unchanged - - li_br &fn_parent_tail - call # TAIL test: fn_identity RETs to here - - li_br &b5_end - b # skip over the inlined function bodies - -:fn_inner - prologue - epilogue - ret - -:fn_identity - prologue - li_br &fn_inner - call - epilogue - ret - -:fn_parent_tail - prologue - li_br &fn_identity - tail - -:b5_end - mov_r2,sp # r2 = sp snapshot (post-tranche) - sub_r2,r2,r6 # r2 = sp_after - sp_before (0 if balanced) - sub_r1,r1,r2 # r1 -= delta; unchanged (= 5) iff balanced - - mov_r6,r1 # r6 = 5 (callee-saved, survives syscalls) - - ## write(1, &prefix, 5) — "P1 = " - li_r0 sys_write - li_r1 '01000000' - li_r2 &prefix - li_r3 '05000000' - syscall - - ## write(1, &digits + r6, 1) — the computed digit ('5') - li_r0 sys_write - li_r1 '01000000' - li_r2 &digits - add_r2,r2,r6 # r2 = &digits + 5 - li_r3 '01000000' - syscall - - ## write(1, &newline, 1) - li_r0 sys_write - li_r1 '01000000' - li_r2 &newline - li_r3 '01000000' - syscall - - ## exit(r6) — exit status = computed result - li_r0 sys_exit - mov_r1,r6 - syscall - -:prefix -"P1 = " -:digits -"0123456789" -:newline -" -" - -## 32 bytes reserved for tranche 3 memory round-trip. The LOAD segment -## is RWX (see ELF-<arch>.hex2 ph_flags=7) so we can store into this -## region at runtime. scratch_mid = scratch+16, the base address for -## the negative-imm12 LD/ST test ([scratch_mid + -8] = [scratch+8]). -:scratch '0000000000000000' '0000000000000000' -:scratch_mid '0000000000000000' '0000000000000000' - -:ELF_end diff --git a/tests/hello.M1 b/tests/hello.M1 @@ -1,26 +0,0 @@ -## P1 "hello, world" — portable pseudo-ISA source. -## -## Same program on every P1 target; only the backing defs file swaps -## (see p1_aarch64.M1, and eventually p1_amd64.M1 / p1_riscv64.M1). -## -## Linux calling convention on P1: -## SYSCALL num -> r0, args -> r1..r6, result -> r0 - -:_start - ## write(fd=1, buf=&msg, count=14) - li_r0 sys_write # r0 = syscall number (write) - li_r1 '01000000' # r1 = fd (stdout) - li_r2 &msg # r2 = buf - li_r3 '0E000000' # r3 = count (14) - syscall - - ## exit(0) - li_r0 sys_exit # r0 = syscall number (exit) - li_r1 '00000000' # r1 = status - syscall - -:msg -"Hello, world! -" - -:ELF_end diff --git a/tests/p1/test.sh b/tests/p1/test.sh @@ -1,114 +0,0 @@ -#!/bin/sh -## tests/p1/test.sh -- run the P1-language test suite. -## -## A P1 fixture is `<name>.P1pp`. For each fixture with a `<name>.expected` -## sibling, the runner builds the program for every backend arch via -## m1pp/build-p1.sh (which handles m1pp expansion, M1 stringification, and -## hex2 linking against the per-arch ELF header), runs it under the -## matching podman platform, and diffs stdout against the expectation. -## Cross-arch consistency falls out by transitivity. -## -## Filenames starting with `_` are skipped (parked). -## -## Usage: tests/p1/test.sh [--arch ARCH] [fixture-name ...] -## No args: every non-`_` fixture under tests/p1/ for every arch. -## --arch: restrict to a single arch (aarch64 | amd64 | riscv64). - -set -eu - -REPO=$(cd "$(dirname "$0")/../.." && pwd) -cd "$REPO" - -ALL_ARCHES="aarch64 amd64 riscv64" - -PLATFORM_aarch64=linux/arm64 -PLATFORM_amd64=linux/amd64 -PLATFORM_riscv64=linux/riscv64 - -IMAGE_aarch64=public.ecr.aws/docker/library/alpine@sha256:378c4c5418f7493bd500ad21ffb43818d0689daaad43e3261859fb417d1481a0 -IMAGE_amd64=public.ecr.aws/docker/library/alpine@sha256:4d889c14e7d5a73929ab00be2ef8ff22437e7cbc545931e52554a7b00e123d8b -IMAGE_riscv64=public.ecr.aws/docker/library/alpine@sha256:667d07bf2f6239f094f64b5682c8ffbe24c9f3139b1fb854f85caf931a3d7439 - -ARCHES="$ALL_ARCHES" -NAMES="" -while [ "$#" -gt 0 ]; do - case "$1" in - --arch) - shift - ARCHES="$1" - ;; - --arch=*) - ARCHES="${1#--arch=}" - ;; - *) - NAMES="$NAMES $1" - ;; - esac - shift -done - -if [ -z "$NAMES" ]; then - NAMES=$(ls tests/p1/ 2>/dev/null \ - | sed -n 's/^\([^_][^.]*\)\.P1pp$/\1/p' \ - | sort -u) -fi - -if [ -z "$NAMES" ]; then - echo "no fixtures to run" >&2 - exit 1 -fi - -pass=0 -fail=0 -for name in $NAMES; do - fixture=tests/p1/$name.P1pp - expected=tests/p1/$name.expected - - if [ ! -e "$fixture" ]; then - echo " SKIP $name (no .P1pp)" - continue - fi - if [ ! -e "$expected" ]; then - echo " SKIP $name (no .expected)" - continue - fi - - expected_content=$(cat "$expected") - - for arch in $ARCHES; do - eval platform=\$PLATFORM_$arch - eval image=\$IMAGE_$arch - if [ -z "$platform" ]; then - echo " SKIP [$arch] $name (unknown arch)" - continue - fi - - binary=build/p1-tests/$arch/$name - - if ! sh m1pp/build-p1.sh "$arch" "$fixture" "$binary" >/dev/null 2>&1; then - echo " FAIL [$arch] $name (build failed)" - sh m1pp/build-p1.sh "$arch" "$fixture" "$binary" 2>&1 | sed 's/^/ /' - fail=$((fail + 1)) - continue - fi - - actual=$(podman run --rm --pull=never --platform "$platform" \ - -v "$REPO":/work -w /work "$image" \ - "./$binary" 2>&1 || true) - - if [ "$actual" = "$expected_content" ]; then - echo " PASS [$arch] $name" - pass=$((pass + 1)) - else - echo " FAIL [$arch] $name" - echo " --- expected ---" - printf '%s\n' "$expected_content" | sed 's/^/ /' - echo " --- actual ---" - printf '%s\n' "$actual" | sed 's/^/ /' - fail=$((fail + 1)) - fi - done -done - -echo "$pass passed, $fail failed" -[ "$fail" -eq 0 ] diff --git a/vendor/seed/LICENSE b/vendor/seed/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<http://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<http://www.gnu.org/philosophy/why-not-lgpl.html>. diff --git a/vendor/seed/README.md b/vendor/seed/README.md @@ -0,0 +1,26 @@ +# vendor/seed + +Bootstrap seeds and stage0-posix sources, vendored from +[live-bootstrap](https://github.com/fosslinux/live-bootstrap) +(`seed/stage0-posix/`). + +Per arch (`aarch64`, `amd64`, `riscv64`): + +| file | upstream path | +| ----------- | ------------------------------------------------------- | +| `hex0-seed` | `bootstrap-seeds/POSIX/<A>/hex0-seed` | +| `hex0.hex0` | `<A>/hex0_<A>.hex0` | +| `hex1.hex0` | `<A>/hex1_<A>.hex0` | +| `hex2.hex1` | `<A>/hex2_<A>.hex1` | +| `catm.hex2` | `<A>/catm_<A>.hex2` (locally produced for `aarch64`, see note) | +| `M0.hex2` | `<A>/M0_<A>.hex2` | +| `ELF.hex2` | `<A>/ELF-<arch>.hex2` | + +`<A>` is the upstream's mixed-case directory: `AArch64`, `AMD64`, `riscv64`. + +`aarch64/catm.hex2` is locally produced — upstream ships AArch64's catm only +as `.hex1`, not `.hex2`, so the chain in `scripts/bootstrap.sh` (which feeds +`catm.hex2` into `hex2-0`) needs the `.hex2` form. The other two arches use +the upstream `.hex2` directly. + +License: GPL-3.0-or-later, see [`LICENSE`](LICENSE). diff --git a/vendor/seed/aarch64/ELF.hex2 b/vendor/seed/aarch64/ELF.hex2 @@ -0,0 +1,69 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org> +### Copyright (C) 2020 Sanne Wouda +### This file is part of stage0. +### +### M2-Planet is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### M2-Planet is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with stage0. If not, see <http://www.gnu.org/licenses/>. + +### ELF-aarch64.hex2: ARMv8 AArch64 ELF header in hex2 +### if you wish to use this header, you need to add :ELF_end to the end of your +### M1 or hex2 files. + +## ELF Header + +:ELF_base +7F 45 4C 46 # e_ident[EI_MAG0-3] ELF's magic number + +02 # e_ident[EI_CLASS] Indicating 64 bit +01 # e_ident[EI_DATA] Indicating little endianness +01 # e_ident[EI_VERSION] Indicating original elf + +03 # e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 # e_ident[EI_ABIVERSION] Set at 0 because noone cares + +00 00 00 00 00 00 00 # e_ident[EI_PAD] +02 00 # e_type Indicating Executable +B7 00 # e_machine Indicating AArch64 +01 00 00 00 # e_version Indicating original elf + +&_start 00 00 00 00 # e_entry Address of the entry point +%ELF_program_headers>ELF_base 00 00 00 00 # e_phoff Address of program header table +00 00 00 00 00 00 00 00 # e_shoff Address of section header table + +00 00 00 00 # e_flags +40 00 # e_ehsize Indicating our 64 Byte header + +38 00 # e_phentsize size of a program header table +01 00 # e_phnum number of entries in program table + +00 00 # e_shentsize size of a section header table +00 00 # e_shnum number of entries in section table + +00 00 # e_shstrndx index of the section names + +## Program Header +:ELF_program_headers +01 00 00 00 # ph_type: PT-LOAD = 1 +07 00 00 00 # ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 # ph_offset + +&ELF_base 00 00 00 00 # ph_vaddr +&ELF_base 00 00 00 00 # ph_physaddr + +%ELF_end>ELF_base 00 00 00 00 # ph_filesz +%ELF_end>ELF_base 00 00 00 00 # ph_memsz + +01 00 00 00 00 00 00 00 # ph_align + +:ELF_text diff --git a/vendor/seed/aarch64/M0.hex2 b/vendor/seed/aarch64/M0.hex2 @@ -0,0 +1,958 @@ +; Copyright (C) 2017 Jeremiah Orians +; Copyright (C) 2020 Sanne Wouda +; This file is part of stage0. +; +; stage0 is free software: you can redistribute it and/or modify +; it under the terms of the GNU General Public License as published by +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; stage0 is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with stage0. If not, see <http://www.gnu.org/licenses/>. + + + ; Register usage: + ; X0, X8, RSI, RDI => Temps + ; X12 => MALLOC + ; X13 => HEAD + ; X14 => Output_file + ; X15 => Input_file + + ; Struct format: (size 32) + ; NEXT => 0 + ; TYPE => 8 + ; TEXT => 16 + ; EXPRESSION => 24 + + ; Types + ; None => 0 + ; MACRO => 1 + ; STRING => 2 + +; Where the ELF Header is going to hit +; Simply jump to _start +; Our main function +:_start + e10b40f9 # LDR_X1_[SP,16] ; Get the actual input name + 600c8092 # SET_X0_TO_FCNTL_H_AT_FDCWD ; AT_FDCWD, relative to current working directory + 020080d2 # SET_X2_TO_0 ; prepare read_only + 080780d2 # SET_X8_TO_SYS_OPENAT ; The syscall number for openat(), aarch64 has no open() + 010000d4 # SYSCALL ; Now open that damn file + ef0300aa # SET_X15_FROM_X0 ; Preserve the file pointer we were given + + e10f40f9 # LDR_X1_[SP,24] ; Get the output name + 600c8092 # SET_X0_TO_FCNTL_H_AT_FDCWD ; AT_FDCWD, relative to current working directory + 224880d2 # SET_X2_TO_577 ; Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 033080d2 # SET_X3_TO_384 ; Prepare file as RWX for owner only (600 in octal) + 080780d2 # SET_X8_TO_SYS_OPENAT ; The syscall number for openat(), aarch64 has no open() + 010000d4 # SYSCALL ; Open file! + 1f0000f1 # CMP_X0_TO_0 ; Check for missing output + 4d000054 # SKIP_INST_LE ; Have real output + ^~_start_out 14 # ^~_start_out FBRANCH + 200080d2 # SET_X0_TO_1 ; Use stdout + +:_start_out + ee0300aa # SET_X14_FROM_X0 ; Preserve the file pointer we were given + c81a80d2 # SET_X8_TO_SYS_BRK ; the Syscall # for SYS_BRK + 000080d2 # SET_X0_TO_0 ; Get current brk + 010000d4 # SYSCALL ; Let the kernel do the work + ec0300aa # SET_X12_FROM_X0 ; Set our malloc pointer + + ^~Tokenize_Line 94 # ^~Tokenize_Line FCALL ; Get all lines + e0030daa # SET_X0_FROM_X13 ; prepare for Reverse_List + ^~Reverse_List 94 # ^~Reverse_List FCALL ; Correct order + ed0300aa # SET_X13_FROM_X0 ; Update HEAD + ^~Identify_Macros 94 # ^~Identify_Macros FCALL ; Find the DEFINEs + ^~Line_Macro 94 # ^~Line_Macro FCALL ; Apply the DEFINEs + ^~Process_String 94 # ^~Process_String FCALL ; Handle strings + ^~Eval_Immediates 94 # ^~Eval_Immediates FCALL ; Handle Numbers + ^~Preserve_Other 94 # ^~Preserve_Other FCALL ; Collect the remaining + ^~Print_Hex 94 # ^~Print_Hex FCALL ; Output our results + +:Done + ; program completed Successfully + 000080d2 # SET_X0_TO_0 ; All is well + a80b80d2 # SET_X8_TO_SYS_EXIT ; put the exit syscall number in eax + 010000d4 # SYSCALL ; Call it a good day + + +; Tokenize_Line Function +; Using input file X15 and Head X13 +; Creates a linked list of structs +; Uses X1 for in_set strings, X2 for Int C and X3 for Struct Token* p +:Tokenize_Line + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR +:restart + ^~fgetc 94 # ^~fgetc FCALL ; Read a char + 1f1000b1 # CMP_X0_TO_MINUS_4 ; Check for EOF + 41000054 # SKIP_INST_NE ; File is collected + ^~done 14 # ^~done FBRANCH + + 001c4092 # AND_X0_X0_0xFF ; We have to zero extend it to use it + e20300aa # SET_X2_FROM_X0 ; Protect C + + 41000018 # LOAD_W1_AHEAD ; Get pointer to ";;" + 02000014 # SKIP_32_DATA + &comments # &comments + ^~In_Set 94 # ^~In_Set FCALL ; Check for comments + 1f0400f1 # CMP_X0_TO_1 ; If comments + 41000054 # SKIP_INST_NE ; try again + ^~Purge_LineComment 14 # ^~Purge_LineComment FBRANCH + + e00302aa # SET_X0_FROM_X2 ; put C in place for check + 41000018 # LOAD_W1_AHEAD ; Get pointer to "\n\t " + 02000014 # SKIP_32_DATA + &terminators # &terminators + ^~In_Set 94 # ^~In_Set FCALL ; Check for terminators + 1f0400f1 # CMP_X0_TO_1 ; If terminator + 41000054 # SKIP_INST_NE ; try again + ^~restart 17 # ^~restart RBRANCH + + 000480d2 # SET_X0_TO_32 ; Malloc the struct P + ^~malloc 94 # ^~malloc FCALL ; Get pointer to P + e30300aa # SET_X3_FROM_X0 ; Protect P + 6d0000f9 # STR_X13_[X3] ; P->NEXT = HEAD + ed0303aa # SET_X13_FROM_X3 ; HEAD = P + + e00302aa # SET_X0_FROM_X2 ; put C in place for check + 41000018 # LOAD_W1_AHEAD ; Get pointer to "\"'" + 02000014 # SKIP_32_DATA + &string_char # &string_char + ^~In_Set 94 # ^~In_Set FCALL ; Check for string chars + 1f0400f1 # CMP_X0_TO_1 ; If string char + 41000054 # SKIP_INST_NE ; Get string + ^~Store_String 14 # ^~Store_String FBRANCH + + ^~Store_Atom 94 # ^~Store_Atom FCALL ; Get whole token + ^~restart 17 # ^~restart RBRANCH + +:done + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; fgetc function +; Receives FILE* in X15 +; Returns -4 (EOF) or char in X0 +:fgetc + e10f1ff8 # PUSH_X1 + e20f1ff8 # PUSH_X2 + 60008092 # SET_X0_TO_MINUS_4 ; Put EOF in x0 + e00f1ff8 # PUSH_X0 ; Assume bad (If nothing read, value will remain EOF) + e1030091 # SET_X1_FROM_SP ; Get stack addresss + e0030faa # SET_X0_FROM_X15 ; Where are we reading from + e80780d2 # SET_X8_TO_SYS_READ ; the syscall number for read + 220080d2 # SET_X2_TO_1 ; set the size of chars we want + + 010000d4 # SYSCALL ; call the Kernel + + e00741f8 # POP_X0 ; Get either char or EOF + e20741f8 # POP_X2 + e10741f8 # POP_X1 + c0035fd6 # RETURN + + +; Malloc isn't actually required if the program being built fits in the initial memory +; However, it doesn't take much to add it. +; Requires X12 to be initialized and X0 to have the number of desired bytes +:malloc + e10f1ff8 # PUSH_X1 + 00000c8b # ADD_X0_X0_X12 ; Request the number of desired bytes + c81a80d2 # SET_X8_TO_SYS_BRK ; the Syscall # for SYS_BRK + 010000d4 # SYSCALL ; call the Kernel + + e1030caa # SET_X1_FROM_X12 ; save Return pointer + ec0300aa # SET_X12_FROM_X0 ; Update pointer + e00301aa # SET_X0_FROM_X1 ; return pointer + e10741f8 # POP_X1 + c0035fd6 # RETURN + + +; Purge_LineComment function +; Reads chars until LF and jumps to restart +:Purge_LineComment + ^~fgetc 97 # ^~fgetc RCALL ; Get a char + 001c4092 # AND_X0_X0_0xFF ; Zero extend + 1f2800f1 # CMP_X0_TO_10 ; While not LF + 40000054 # SKIP_INST_EQ ; Keep reading + ^~Purge_LineComment 17 # ^~Purge_LineComment RBRANCH + ^~restart 17 # ^~restart RBRANCH + + +; Store_String Function +; Receives C in X2, HEAD in X3 and Input file in X14 +; Uses X1 for terminator, X2 for C and X3 for string +:Store_String + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + + 400080d2 # SET_X0_TO_2 ; Using TYPE STRING + 600400f9 # STR_X0_[X3,8] ; HEAD->TYPE = STRING + 004080d2 # SET_X0_TO_512 ; Malloc the string + ^~malloc 97 # ^~malloc RCALL ; Get pointer to P + 600800f9 # STR_X0_[X3,16] ; HEAD->TEXT = STRING + e10302aa # SET_X1_FROM_X2 ; Protect terminator + e30300aa # SET_X3_FROM_X0 ; Protect string pointer +:Store_String_Loop + 62140038 # STR_BYTE_W2_[X3]_1 + ^~fgetc 97 # ^~fgetc RCALL ; read next char + 001c4092 # AND_X0_X0_0xFF ; Zero extend it + e20300aa # SET_X2_FROM_X0 ; Update C + 5f0001eb # CMP_X2_X1 ; See if we hit terminator + 40000054 # SKIP_INST_EQ ; Otherwise keep looping + ^~Store_String_Loop 17 # ^~Store_String_Loop RBRANCH + + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00303aa # SET_X0_FROM_X3 ; Return HEAD + ^~restart 17 # ^~restart RBRANCH + + +; Store_Atom Function +; Receives C in X2, HEAD in X3 and Input file in X15 +; Uses X1 for in_set strings, X2 for C and X3 for string +:Store_Atom + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + + 002080d2 # SET_X0_TO_256 ; Malloc the string + ^~malloc 97 # ^~malloc RCALL ; Get pointer to P + 600800f9 # STR_X0_[X3,16] ; HEAD->TEXT = STRING + 41000018 # LOAD_W1_AHEAD ; Get pointer to "\n\t " + 02000014 # SKIP_32_DATA + &terminators # &terminators + e30300aa # SET_X3_FROM_X0 ; Protect string pointer +:Store_Atom_loop + 62140038 # STR_BYTE_W2_[X3]_1 ; write byte + ^~fgetc 97 # ^~fgetc RCALL ; read next char + 001c4092 # AND_X0_X0_0xFF ; Zero extend it + e20300aa # SET_X2_FROM_X0 ; Update C + ^~In_Set 94 # ^~In_Set FCALL ; Check for terminators + 1f0000f1 # CMP_X0_TO_0 ; Check for "\n\t " + 41000054 # SKIP_INST_NE ; Loop otherwise + ^~Store_Atom_loop 17 # ^~Store_Atom_loop RBRANCH + + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00303aa # SET_X0_FROM_X3 ; Return HEAD + c0035fd6 # RETURN + + +; In_Set function +; Receives Char C in X0 and CHAR* in X1 +; Returns 1 if true, zero if false in X0 +:In_Set + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 +:In_Set_loop + 22144038 # LDR_BYTE_W2_[X1]_1 ; Read char + + 1f0002eb # CMP_X0_X2 ; See if they match + 41000054 # SKIP_INST_NE ; return true + ^~In_Set_True 14 # ^~In_Set_True FBRANCH + + 5f0000f1 # CMP_X2_TO_0 ; Check for NULL + 41000054 # SKIP_INST_NE ; return false + ^~In_Set_False 14 # ^~In_Set_False FBRANCH + + ^~In_Set_loop 17 # ^~In_Set_loop RBRANCH ; Keep looping + +:In_Set_True + 200080d2 # SET_X0_TO_1 ; Set True + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + +:In_Set_False + 000080d2 # SET_X0_TO_0 ; Set FALSE + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + +; Char sets +:terminators + 0A 09 20 00 # "\n\t " + +:comments + 23 3B 00 # "#;" + +:string_char + 22 27 00 # '22 27 00' + + 00 00 # '00 00' ; .p2align 2 + +; Reverse_List function +; Receives List in X0 +; Returns the list reversed in X0 +:Reverse_List + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e10300aa # SET_X1_FROM_X0 ; Set HEAD + 000080d2 # SET_X0_TO_0 ; ROOT = NULL +:Reverse_List_Loop + 3f0000f1 # CMP_X1_TO_0 ; WHILE HEAD != NULL + 41000054 # SKIP_INST_NE ; Stop otherwise + ^~Reverse_List_Done 14 # ^~Reverse_List_Done FBRANCH + + 220040f9 # LDR_X2_[X1] ; NEXT = HEAD->NEXT + 200000f9 # STR_X0_[X1] ; HEAD->NEXT = ROOT + e00301aa # SET_X0_FROM_X1 ; ROOT = HEAD + e10302aa # SET_X1_FROM_X2 ; HEAD = NEXT + ^~Reverse_List_Loop 17 # ^~Reverse_List_Loop RBRANCH ; Keep Going + +:Reverse_List_Done + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; Identify_Macros function +; Receives List in X0 +; Updates the list in place; does not modify registers +; Uses X1 for DEFINE, X2 for I +:Identify_Macros + e00f1ff8 # PUSH_X0 ; Protect X0 + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + + 41000018 # LOAD_W1_AHEAD ; Setup define string + 02000014 # SKIP_32_DATA + &DEFINE_str # &DEFINE_str + e20300aa # SET_X2_FROM_X0 ; I = HEAD +:Identify_Macros_Loop + 400840f9 # LDR_X0_[X2,16] ; I->TEXT + ^~match 94 # ^~match FCALL ; IF "DEFINE" == I->TEXT + 1f0000f1 # CMP_X0_TO_0 ; Check if match + 40000054 # SKIP_INST_EQ ; Skip the work + ^~Identify_Macros_Next 14 # ^~Identify_Macros_Next FBRANCH + + ; Deal with MACRO + 200080d2 # SET_X0_TO_1 ; Using MACRO + 400400f9 # STR_X0_[X2,8] ; I->TYPE = MACRO + + 400040f9 # LDR_X0_[X2] ; I->NEXT + 000840f9 # LDR_X0_[X0,16] ; I->NEXT->TEXT + 400800f9 # STR_X0_[X2,16] ; I->TEXT = I->NEXT->TEXT + + 400040f9 # LDR_X0_[X2] ; I->NEXT + 000040f9 # LDR_X0_[X0] ; I->NEXT->NEXT + 000840f9 # LDR_X0_[X0,16] ; I->NEXT->NEXT->TEXT + 400c00f9 # STR_X0_[X2,24] ; I->EXPRESSION = I->NEXT->NEXT->TEXT + + 400040f9 # LDR_X0_[X2] ; I->NEXT + 000040f9 # LDR_X0_[X0] ; I->NEXT->NEXT + 000040f9 # LDR_X0_[X0] ; I->NEXT->NEXT->NEXT + 400000f9 # STR_X0_[X2] ; I->NEXT = I->NEXT->NEXT->NEXT + +:Identify_Macros_Next + 420040f9 # LDR_X2_[X2] ; I = I->NEXT + 5f0000f1 # CMP_X2_TO_0 ; Check for NULL + 40000054 # SKIP_INST_EQ ; Keep looping otherwise + ^~Identify_Macros_Loop 17 # ^~Identify_Macros_Loop RBRANCH + + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00741f8 # POP_X0 ; Restore X0 + c0035fd6 # RETURN + +:DEFINE_str + 44 45 46 49 4E 45 00 # "DEFINE" + + 00 # '00' ; .p2align 2 + +; match function +; Receives CHAR* in X0 and CHAR* in X1 +; Returns 0 (TRUE) or 1 (FALSE) in X0 +:match + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + + e20300aa # SET_X2_FROM_X0 ; S1 in place + e30301aa # SET_X3_FROM_X1 ; S2 in place +:match_Loop + 40144038 # LDR_BYTE_W0_[X2]_1 ; S1[0] + 61144038 # LDR_BYTE_W1_[X3]_1 ; S2[0] + 1f0001eb # CMP_X0_X1 ; See if they match + 40000054 # SKIP_INST_EQ ; If not + ^~match_False 14 # ^~match_False FBRANCH + + 1f0000f1 # CMP_X0_TO_0 ; If reached end of string + 41000054 # SKIP_INST_NE ; Perfect match + ^~match_Done 14 # ^~match_Done FBRANCH + ^~match_Loop 17 # ^~match_Loop RBRANCH ; Otherwise keep looping + +:match_False + 200080d2 # SET_X0_TO_1 ; Return false +:match_Done + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; Line_Macro function +; Receives List in X0 +; Updates the list in place; does not modify registers +; Uses X0 for I, X1 for I->TEXT, X2 for I->EXPRESSION +:Line_Macro + e00f1ff8 # PUSH_X0 ; Protect X0 + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR +:Line_Macro_Loop + 010440f9 # LDR_X1_[X0,8] ; I->TYPE + 3f0400f1 # CMP_X1_TO_1 ; IF MACRO == I->TYPE + 40000054 # SKIP_INST_EQ ; Otherwise move on + ^~Line_Macro_Next 14 # ^~Line_Macro_Next FBRANCH + + ; Is a macro apply + 010840f9 # LDR_X1_[X0,16] ; I->TEXT + 020c40f9 # LDR_X2_[X0,24] ; I->EXPRESSION + 000040f9 # LDR_X0_[X0] ; I->NEXT + ^~Set_Expression 94 # ^~Set_Expression FCALL ; Apply it + ^~Line_Macro_Loop 17 # ^~Line_Macro_Loop RBRANCH ; Move on to next + +:Line_Macro_Next + 000040f9 # LDR_X0_[X0] ; I->NEXT + 1f0000f1 # CMP_X0_TO_0 ; Check for NULL + 40000054 # SKIP_INST_EQ ; Keep going + ^~Line_Macro_Loop 17 # ^~Line_Macro_Loop RBRANCH + + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00741f8 # POP_X0 ; Restore X0 + c0035fd6 # RETURN + + +; Set_Expression function +; Receives List in X0, CHAR* in X1 and CHAR* in X2 +; Updates the list in place; does not modify registers +; Uses X1 for C, X2 for EXP and X3 for I +:Set_Expression + e00f1ff8 # PUSH_X0 ; Protect X0 + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + e30300aa # SET_X3_FROM_X0 ; Set I +:Set_Expression_Loop + 600440f9 # LDR_X0_[X3,8] ; I->TYPE + 1f0400f1 # CMP_X0_TO_1 ; IF MACRO == I->TYPE + 41000054 # SKIP_INST_NE ; Ignore and move on + ^~Set_Expression_Next 14 # ^~Set_Expression_Next FBRANCH + + 600840f9 # LDR_X0_[X3,16] ; I->TEXT + ^~match 97 # ^~match RCALL ; Check for match + 1f0000f1 # CMP_X0_TO_0 ; If match + 40000054 # SKIP_INST_EQ ; Otherwise next + ^~Set_Expression_Next 14 # ^~Set_Expression_Next FBRANCH + + ; We have a non-macro match + 620c00f9 # STR_X2_[X3,24] ; I->EXPRESSION = EXP + +:Set_Expression_Next + 630040f9 # LDR_X3_[X3] ; I = I->NEXT + 7f0000f1 # CMP_X3_TO_0 ; IF NULL == I + 40000054 # SKIP_INST_EQ ; Otherwise keep looping + ^~Set_Expression_Loop 17 # ^~Set_Expression_Loop RBRANCH + + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00741f8 # POP_X0 ; Restore X0 + c0035fd6 # RETURN + + +; Process_String function +; Receives List in X0 +; Update the list in place; does not modify registers +; Uses X1 for I->TEXT, X2 for I and X3 for S +:Process_String + e00f1ff8 # PUSH_X0 ; Protect X0 + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + + e20300aa # SET_X2_FROM_X0 ; I = HEAD +:Process_String_loop + 400440f9 # LDR_X0_[X2,8] ; I->TYPE + 1f0800f1 # CMP_X0_TO_2 ; IF STRING == I->TYPE + 40000054 # SKIP_INST_EQ ; Skip to next + ^~Process_String_Next 14 # ^~Process_String_Next FBRANCH + + 410840f9 # LDR_X1_[X2,16] ; I->TEXT + 20004039 # LDR_BYTE_W0_[X1] ; I->TEXT[0] + 1f9c00f1 # CMP_X0_TO_39 ; IF '\'' == I->TEXT[0] + 40000054 # SKIP_INST_EQ ; Deal with '\"' + ^~Process_String_Raw 14 # ^~Process_String_Raw FBRANCH + + ; Deal with '\'' + 21040091 # ADD_X1_X1_1 ; I->TEXT + 1 + 410c00f9 # STR_X1_[X2,24] ; I->EXPRESSION = I->TEXT + 1 + ^~Process_String_Next 14 # ^~Process_String_Next FBRANCH ; Move on to next + +:Process_String_Raw + e00301aa # SET_X0_FROM_X1 ; Get length of I->TEXT + ^~string_length 94 # ^~string_length FCALL ; Do it + 00fc42d3 # LSR_X0_X0_2 ; LENGTH = LENGTH >> 2 + 00040091 # ADD_X0_X0_1 ; LENGTH = LENGTH + 1 + 00f07dd3 # LSL_X0_X0_3 ; LENGTH = LENGTH << 3 + ^~malloc 97 # ^~malloc RCALL ; Get string + e30301aa # SET_X3_FROM_X1 ; S = I->TEXT + 63040091 # ADD_X3_X3_1 ; S = S + 1 + 400c00f9 # STR_X0_[X2,24] ; I->EXPRESSION = hexify + e10300aa # SET_X1_FROM_X0 ; Put hexify buffer in x1 + +:Process_String_Raw_Loop + 60144038 # LDR_BYTE_W0_[X3]_1 ; Read 1 chars + e00f1ff8 # PUSH_X0 + ^~hex8 94 # ^~hex8 FCALL ; write them all + e00741f8 # POP_X0 + 1f0000f1 # CMP_X0_TO_0 ; Check for NULL + 40000054 # SKIP_INST_EQ ; Keep looping + ^~Process_String_Raw_Loop 17 # ^~Process_String_Raw_Loop RBRANCH + +:Process_String_Next + 420040f9 # LDR_X2_[X2] ; I = I->NEXT + 5f0000f1 # CMP_X2_TO_0 ; IF NULL == I + 40000054 # SKIP_INST_EQ ; Otherwise keep looping + ^~Process_String_loop 17 # ^~Process_String_loop RBRANCH + + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00741f8 # POP_X0 ; Restore X0 + c0035fd6 # RETURN + + +; string_length function +; Receives CHAR* in X0 +; Returns INT in X0 +; Uses X0 for CH, X1 for S and X2 for INDEX +:string_length + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e10300aa # SET_X1_FROM_X0 ; Set S + 020080d2 # SET_X2_TO_0 ; INDEX = 0 +:string_length_loop + 20686238 # LDR_BYTE_W0_[X1,X2] ; S[INDEX] + 1f0000f1 # CMP_X0_TO_0 ; IF NULL == S[INDEX] + 41000054 # SKIP_INST_NE ; Stop + ^~string_length_done 14 # ^~string_length_done FBRANCH + + 42040091 # ADD_X2_X2_1 ; INDEX = INDEX + 1 + ^~string_length_loop 17 # ^~string_length_loop RBRANCH ; Keep going + +:string_length_done + e00302aa # SET_X0_FROM_X2 ; RETURN INDEX + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; Eval_Immediates function +; Receives List in X0 +; Updates the list in place; does not modify registers +; Uses X1 for I->TEXT[0], X2 for I->TEXT[1] and X3 for I +:Eval_Immediates + e00f1ff8 # PUSH_X0 ; Protect X0 + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + e30300aa # SET_X3_FROM_X0 ; I = HEAD +:Eval_Immediates_Loop + ; Check for MACRO + 600440f9 # LDR_X0_[X3,8] ; I->TYPE + 1f0400f1 # CMP_X0_TO_1 ; IF MACRO == I-TYPE + 41000054 # SKIP_INST_NE ; Skip to next + ^~Eval_Immediates_Next 14 # ^~Eval_Immediates_Next FBRANCH + + ; Check for NULL EXPRESSION + 600c40f9 # LDR_X0_[X3,24] ; I->EXPRESSION + 1f0000f1 # CMP_X0_TO_0 ; IF NULL == I->EXPRESSION + 40000054 # SKIP_INST_EQ ; Skip to next + ^~Eval_Immediates_Next 14 # ^~Eval_Immediates_Next FBRANCH + + ; Check if number + 600840f9 # LDR_X0_[X3,16] ; I->TEXT + 01144038 # LDR_BYTE_W1_[X0]_1 ; I->TEXT[0] + 02004039 # LDR_BYTE_W2_[X0] ; I->TEXT[1] + ^~numerate_string 94 # ^~numerate_string FCALL ; Convert string to INT + 1f0000f1 # CMP_X0_TO_0 ; IF 0 == numerate_number(I->TEXT + 1) + 40000054 # SKIP_INST_EQ ; Has a value + ^~Eval_Immediates_value 14 # ^~Eval_Immediates_value FBRANCH + + ; Last chance for Immediate + 5fc000f1 # CMP_X2_TO_48 ; If '0' == I->TEXT[1] + 40000054 # SKIP_INST_EQ ; Skip to next + ^~Eval_Immediates_Next 14 # ^~Eval_Immediates_Next FBRANCH + +:Eval_Immediates_value + ^~express_number 94 # ^~express_number FCALL ; Convert value to hex string + 600c00f9 # STR_X0_[X3,24] ; I->EXPRESSION = express_number(value, I-TEXT[0]) + +:Eval_Immediates_Next + 630040f9 # LDR_X3_[X3] ; I = I->NEXT + 7f0000f1 # CMP_X3_TO_0 ; IF NULL == I + 40000054 # SKIP_INST_EQ ; Otherwise keep looping + ^~Eval_Immediates_Loop 17 # ^~Eval_Immediates_Loop RBRANCH + + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00741f8 # POP_X0 ; Restore X0 + c0035fd6 # RETURN + + +; numerate_string function +; Receives CHAR* in X0 +; Returns value of CHAR* in X0 +; Uses X0 for VALUE, X1 for S, X2 for CH and X4 for NEGATIVE? +:numerate_string + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + e40f1ff8 # PUSH_X4 + e10300aa # SET_X1_FROM_X0 ; put S in correct place + 000080d2 # SET_X0_TO_0 ; Initialize to Zero +:numerate_string_loop + 22044039 # LDR_BYTE_W2_[X1,1] ; S[1] + 5fe001f1 # CMP_X2_TO_120 ; IF 'x' == S[1] + 41000054 # SKIP_INST_NE ; Deal with hex input + ^~numerate_hex 14 # ^~numerate_hex FBRANCH + + ; Assume decimal input + 040080d2 # SET_X4_TO_0 ; Assume no negation + 22004039 # LDR_BYTE_W2_[X1] ; S[0] + 5fb400f1 # CMP_X2_TO_45 ; IF '-' == S[0] + 40000054 # SKIP_INST_EQ ; Skip negation + ^~numerate_decimal 14 # ^~numerate_decimal FBRANCH + + 240080d2 # SET_X4_TO_1 ; Set FLAG + 21040091 # ADD_X1_X1_1 ; S = S + 1 + +:numerate_decimal + 22004039 # LDR_BYTE_W2_[X1] ; S[0] + 5f0000f1 # CMP_X2_TO_0 ; IF NULL == S[0] + 41000054 # SKIP_INST_NE ; We are done + ^~numerate_decimal_done 14 # ^~numerate_decimal_done FBRANCH + + e10f1ff8 # PUSH_X1 + 410180d2 # SET_X1_TO_10 + 007c019b # MUL_X0_X0_X1 ; VALUE = VALUE * 10 + e10741f8 # POP_X1 + + 42c000d1 # SUB_X2_X2_48 ; CH = CH - '0' + 5f2400f1 # CMP_X2_TO_9 ; Check for illegal + 4d000054 # SKIP_INST_LE ; If CH > '9' + ^~numerate_string_fail 14 # ^~numerate_string_fail FBRANCH + 5f0000f1 # CMP_X2_TO_0 ; Check for illegal + 4a000054 # SKIP_INST_GE ; IF CH < 0 + ^~numerate_string_fail 14 # ^~numerate_string_fail FBRANCH + 0000028b # ADD_X0_X0_X2 ; VALUE = VALUE + CH + 21040091 # ADD_X1_X1_1 ; S = S + 1 + ^~numerate_decimal 17 # ^~numerate_decimal RBRANCH ; Keep looping + +:numerate_decimal_done + 9f0400f1 # CMP_X4_TO_1 ; Check if need to negate + 40000054 # SKIP_INST_EQ ; Nope + ^~numerate_string_done 14 # ^~numerate_string_done FBRANCH + + e00300cb # NEG_X0_X0 ; VALUE = VALUE * -1 + ^~numerate_string_done 14 # ^~numerate_string_done FBRANCH ; Done + +:numerate_hex + 21080091 # ADD_X1_X1_2 ; S = S + 2 +:numerate_hex_loop + 22004039 # LDR_BYTE_W2_[X1] ; S[0] + 5f0000f1 # CMP_X2_TO_0 ; IF NULL == S[0] + 41000054 # SKIP_INST_NE ; We are done + ^~numerate_string_done 14 # ^~numerate_string_done FBRANCH + + 00ec7cd3 # LSL_X0_X0_4 ; VALUE = VALUE << 4 + 42c000d1 # SUB_X2_X2_48 ; CH = CH - '0' + 5f2800f1 # CMP_X2_TO_10 ; IF 10 >= CH + 4a000054 # SKIP_INST_GE ; NO + ^~numerate_hex_digit 14 # ^~numerate_hex_digit FBRANCH + 421c00d1 # SUB_X2_X2_7 ; Push A-F into range +:numerate_hex_digit + 5f3c00f1 # CMP_X2_TO_15 ; Check for illegal + 4d000054 # SKIP_INST_LE ; If CH > 'F' + ^~numerate_string_fail 14 # ^~numerate_string_fail FBRANCH + 5f0000f1 # CMP_X2_TO_0 ; Check for illegal + 4a000054 # SKIP_INST_GE ; IF CH < 0 + ^~numerate_string_fail 14 # ^~numerate_string_fail FBRANCH + 0000028b # ADD_X0_X0_X2 ; VALUE = VALUE + CH + 21040091 # ADD_X1_X1_1 ; S = S + 1 + ^~numerate_hex_loop 17 # ^~numerate_hex_loop RBRANCH ; Keep looping + +:numerate_string_fail + 000080d2 # SET_X0_TO_0 ; Return ZERO + +:numerate_string_done + e40741f8 # POP_X4 ; Restore X4 + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; express_number function +; Receives INT in X0 and CHAR in X1 +; Allocates a string and expresses the value in hex +; Returns string in X0 +; Uses X0 for VALUE, X1 for S and X2 for CH +:express_number + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 + fe0f1ff8 # PUSH_LR + + e20301aa # SET_X2_FROM_X1 ; Put CH in right place + e10300aa # SET_X1_FROM_X0 ; Protect VALUE + 5f9400f1 # CMP_X2_TO_37 ; IF '%' == CH + 40000054 # SKIP_INST_EQ ; Otherwise try @ + ^~express_number2 14 # ^~express_number2 FBRANCH + + 200180d2 # SET_X0_TO_9 ; We need 9bytes + ^~malloc 97 # ^~malloc RCALL ; Get S pointer + e30300aa # SET_X3_FROM_X0 ; Put S and VALUE in place + e00301aa # SET_X0_FROM_X1 + e10303aa # SET_X1_FROM_X3 + e10f1ff8 # PUSH_X1 ; Protect S + ^~hex32l 94 # ^~hex32l FCALL ; Store 32bits + ^~express_number_done 14 # ^~express_number_done FBRANCH ; done + +:express_number2 + 5f0001f1 # CMP_X2_TO_64 ; IF '@' == CH + 40000054 # SKIP_INST_EQ ; Othrewise try ! + ^~express_number1 14 # ^~express_number1 FBRANCH + + a00080d2 # SET_X0_TO_5 ; We need 5bytes + ^~malloc 97 # ^~malloc RCALL ; Get S pointer + e30300aa # SET_X3_FROM_X0 ; Put S and VALUE in place + e00301aa # SET_X0_FROM_X1 + e10303aa # SET_X1_FROM_X3 + e10f1ff8 # PUSH_X1 ; Protect S + ^~hex16l 94 # ^~hex16l FCALL ; Store 16bits + ^~express_number_done 14 # ^~express_number_done FBRANCH ; done + +:express_number1 + 600080d2 # SET_X0_TO_3 ; We need 3bytes + ^~malloc 97 # ^~malloc RCALL ; Get S pointer + e30300aa # SET_X3_FROM_X0 ; Put S and VALUE in place + e00301aa # SET_X0_FROM_X1 + e10303aa # SET_X1_FROM_X3 + e10f1ff8 # PUSH_X1 ; Protect S + ^~hex8 94 # ^~hex8 FCALL ; Store 8bit + +:express_number_done + e00741f8 # POP_X0 ; Restore S + fe0741f8 # POP_LR + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; HEX to ascii routine +; Receives INT in X0 and CHAR* in X1 +; Stores ascii of INT in CHAR* +; Returns only modifying X0 +:hex64l + fe0f1ff8 # PUSH_LR + e00f1ff8 # PUSH_X0 ; Protect top 32 + ^~hex32l 94 # ^~hex32l FCALL ; Store it + e00741f8 # POP_X0 ; do top 32 + 00fc60d3 # LSR_X0_X0_32 ; do bottom 32 first + ^~hex32l 94 # ^~hex32l FCALL + fe0741f8 # POP_LR + c0035fd6 # RETURN + +:hex32l + fe0f1ff8 # PUSH_LR + e00f1ff8 # PUSH_X0 ; Protect top 16 + ^~hex16l 94 # ^~hex16l FCALL ; Store it + e00741f8 # POP_X0 ; do top 16 + 00fc50d3 # LSR_X0_X0_16 ; do bottom 16 first + ^~hex16l 94 # ^~hex16l FCALL + fe0741f8 # POP_LR + c0035fd6 # RETURN + +:hex16l + fe0f1ff8 # PUSH_LR + e00f1ff8 # PUSH_X0 ; Protect top byte + ^~hex8 94 # ^~hex8 FCALL ; Store it + e00741f8 # POP_X0 ; do high byte + 00fc48d3 # LSR_X0_X0_8 ; do bottom byte first + ^~hex8 94 # ^~hex8 FCALL + fe0741f8 # POP_LR + c0035fd6 # RETURN + +:hex8 + fe0f1ff8 # PUSH_LR + e00f1ff8 # PUSH_X0 ; Protect bottom nibble + 00fc44d3 # LSR_X0_X0_4 ; do high nibble first + ^~hex4 94 # ^~hex4 FCALL ; Store it + e00741f8 # POP_X0 ; do low nibble + ^~hex4 94 # ^~hex4 FCALL + fe0741f8 # POP_LR + c0035fd6 # RETURN + +:hex4 + 000c4092 # AND_X0_X0_0xF ; isolate nibble + 00c00091 # ADD_X0_X0_48 ; convert to ascii + 1fe400f1 # CMP_X0_TO_57 ; valid digit? + 4c000054 # SKIP_INST_GT ; yes + ^~hex1 14 # ^~hex1 FBRANCH + 001c0091 # ADD_X0_X0_7 ; use alpha range +:hex1 + 20140038 # STR_BYTE_W0_[X1]_1 ; store result + c0035fd6 # RETURN + + +; Preserve_Other function +; Receives List in X0 +; Updates the list in place; does not modify registers +; Uses X0 for I, X1 for I->TEXT +:Preserve_Other + e00f1ff8 # PUSH_X0 ; Protect X0 + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + e30f1ff8 # PUSH_X3 ; Protect X3 +:Preserve_Other_Loop + 010c40f9 # LDR_X1_[X0,24] ; I->EXPRESSION + 3f0000f1 # CMP_X1_TO_0 ; IF NULL == I->EXPRESSION + 40000054 # SKIP_INST_EQ ; Otherwise next + ^~Preserve_Other_Next 14 # ^~Preserve_Other_Next FBRANCH + + ; Needs preserving + 010840f9 # LDR_X1_[X0,16] ; I->TEXT + 010c00f9 # STR_X1_[X0,24] ; I->EXPRESSION = I->TEXT + +:Preserve_Other_Next + 000040f9 # LDR_X0_[X0] ; I = I->NEXT + 1f0000f1 # CMP_X0_TO_0 ; IF NULL == I + 40000054 # SKIP_INST_EQ ; Otherwise keep looping + ^~Preserve_Other_Loop 17 # ^~Preserve_Other_Loop RBRANCH + + e30741f8 # POP_X3 ; Restore X3 + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + e00741f8 # POP_X0 ; Restore X0 + c0035fd6 # RETURN + + +; Print_Hex function +; Receives list in X0 +; walks the list and prints the I->EXPRESSION for all nodes followed by newline +; Uses X1 for I +:Print_Hex + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + fe0f1ff8 # PUSH_LR + e1030daa # SET_X1_FROM_X13 ; I = Head +:Print_Hex_Loop + 200440f9 # LDR_X0_[X1,8] ; I->TYPE + 1f0400f1 # CMP_X0_TO_1 ; IF MACRO == I->TYPE + 41000054 # SKIP_INST_NE ; Skip + ^~Print_Hex_Next 14 # ^~Print_Hex_Next FBRANCH + + 200c40f9 # LDR_X0_[X1,24] ; Using EXPRESSION + ^~File_Print 94 # ^~File_Print FCALL ; Print it + 400180d2 # SET_X0_TO_10 ; NEWLINE + ^~fputc 94 # ^~fputc FCALL ; Append it + +:Print_Hex_Next + 210040f9 # LDR_X1_[X1] ; Iterate to next Token + 3f0000f1 # CMP_X1_TO_0 ; Check for NULL + 40000054 # SKIP_INST_EQ ; Otherwise keep looping + ^~Print_Hex_Loop 17 # ^~Print_Hex_Loop RBRANCH + + fe0741f8 # POP_LR + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; File_Print function +; Receives CHAR* in X0 +; calls fputc for every non-null char +:File_Print + e10f1ff8 # PUSH_X1 ; Protect X1 + e20f1ff8 # PUSH_X2 ; Protect X2 + fe0f1ff8 # PUSH_LR + e10300aa # SET_X1_FROM_X0 ; Protect S + 1f0000f1 # CMP_X0_TO_0 ; Protect against nulls + 41000054 # SKIP_INST_NE ; Simply don't try to print them + ^~File_Print_Done 14 # ^~File_Print_Done FBRANCH +:File_Print_Loop + 20004039 # LDR_BYTE_W0_[X1] ; Read byte + 1f0000f1 # CMP_X0_TO_0 ; Check for NULL + 41000054 # SKIP_INST_NE ; Stop at NULL + ^~File_Print_Done 14 # ^~File_Print_Done FBRANCH + + ^~fputc 94 # ^~fputc FCALL ; write it + 21040091 # ADD_X1_X1_1 ; S = S + 1 + ^~File_Print_Loop 17 # ^~File_Print_Loop RBRANCH ; Keep going + +:File_Print_Done + fe0741f8 # POP_LR + e20741f8 # POP_X2 ; Restore X2 + e10741f8 # POP_X1 ; Restore X1 + c0035fd6 # RETURN + + +; fputc function +; receives CHAR in X0 and FILE* in X14 +; writes char and Returns +:fputc + e10f1ff8 # PUSH_X1 + e20f1ff8 # PUSH_X2 + + e00f1ff8 # PUSH_X0 ; We are writing x0 + e1030091 # SET_X1_FROM_SP ; Get stack address + e0030eaa # SET_X0_FROM_X14 ; Write to target file + 080880d2 # SET_X8_TO_SYS_WRITE ; the syscall number for write + 220080d2 # SET_X2_TO_1 ; set the size of chars we want + + 010000d4 # SYSCALL ; call the Kernel + + e00741f8 # POP_X0 ; Restore stack + e20741f8 # POP_X2 ; Restore X3 + e10741f8 # POP_X1 + c0035fd6 # RETURN + +:ELF_end diff --git a/src/catm_AArch64.hex2 b/vendor/seed/aarch64/catm.hex2 diff --git a/vendor/seed/aarch64/hex0-seed b/vendor/seed/aarch64/hex0-seed Binary files differ. diff --git a/vendor/seed/aarch64/hex0.hex0 b/vendor/seed/aarch64/hex0.hex0 @@ -0,0 +1,258 @@ +## Copyright (C) 2016 Jeremiah Orians +## Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org> +## Copyright (C) 2020 Sanne Wouda +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +## ELF Header + +#:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 0 because none cares +00 ## e_ident[EI_ABIVERSION] See above + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +B7 00 ## e_machine Indicating AArch64 +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## Flags +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +0E 02 00 00 00 00 00 00 ## p_filesz +0E 02 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +#:ELF_text + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function +#:_start + E10B40F9 ; LDR_X1_[SP,16] # Get the actual input name + 600C8092 ; SET_X0_TO_FCNTL_H_AT_FDCWD # AT_FDCWD, relative to current working directory + 020080D2 ; SET_X2_TO_0 # Prepare file as read only + 080780D2 ; SET_X8_TO_SYS_OPENAT # The syscall number for openat(), aarch64 has no open() + 010000d4 ; SYSCALL # Open file! + E90300AA ; SET_X9_FROM_X0 # Preserve the file pointer we were given + + E10F40F9 ; LDR_X1_[SP,24] # Get the output name + 600C8092 ; SET_X0_TO_FCNTL_H_AT_FDCWD # AT_FDCWD, relative to current working directory + 224880D2 ; SET_X2_TO_577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 033880D2 ; SET_X3_TO_448 # Prepare file as RWX for owner only (700 in octal) + 080780D2 ; SET_X8_TO_SYS_OPENAT # The syscall number for openat(), aarch64 has no open() + 010000D4 ; SYSCALL # Open file! + EA0300AA ; SET_X10_FROM_X0 # Preserve the file pointer we were given + + # Our flag for byte processing + 0F008092 ; SET_X15_TO_MINUS_1 + + # temp storage for the sum + 0E0080D2 ; SET_X14_TO_0 + +#:loop + # Read a byte + 470000 94 ; ^~Read_byte CALL + + # process byte + 120000 94 ; ^~hex CALL + + # Deal with -1 values + 1F0000F1 ; CMP_X0_TO_0 + 4A000054 ; SKIP_INST_GE + FCFFFF 17 ; ^~loop RBRANCH + + # deal with toggle + FF0100F1 ; CMP_X15_TO_0 + 4B000054 ; SKIP_INST_LT + 040000 14 ; ^~print BRANCH + + # process first byte of pair + EE0300AA ; SET_X14_FROM_X0 + 0F0080D2 ; SET_X15_TO_0 + + F6FFFF 17 ; ^~loop RBRANCH + +# process second byte of pair +#:print + # update the sum and store in output + 00100E8B ; ADD_X0_X0_X14_LSL4 + 41000018 ; LOAD_W1_AHEAD + 02000014 ; SKIP_32_DATA + 0C02 6000 ; $output DATA_OFFSET 40 00 + 20000039 ; STR_BYTE_W0_[X1] + + # flip the toggle + 0F008092 ; SET_X15_TO_MINUS_1 + + 2E0000 94 ; ^~write_byte CALL + + EEFFFF 17 ; ^~loop RBRANCH + +#:hex + # Purge Comment Lines (#) + 1F8C00F1 ; CMP_X0_TO_35 + 41000054 ; SKIP_INST_NE + 170000 14 ; ^~purge_comment BRANCH + + # Purge Comment Lines (;) + 1FEC00F1 ; CMP_X0_TO_59 + 41000054 ; SKIP_INST_NE + 140000 14 ; ^~purge_comment BRANCH + + # deal all ascii less than 0 + 1FC000F1 ; CMP_X0_TO_48 + 4A000054 ; SKIP_INST_GE + 1F0000 14 ; ^~ascii_other BRANCH + + # deal with 0-9 + 1FE800F1 ; CMP_X0_TO_58 + 4A000054 ; SKIP_INST_GE + 160000 14 ; ^~ascii_num BRANCH + + # deal with all ascii less than A + 1F0401F1 ; CMP_X0_TO_65 + 4A000054 ; SKIP_INST_GE + 190000 14 ; ^~ascii_other BRANCH + + # deal with A-F + 1F1C01F1 ; CMP_X0_TO_71 + 4A000054 ; SKIP_INST_GE + 140000 14 ; ^~ascii_high BRANCH + + # deal with all ascii less than a + 1F8401F1 ; CMP_X0_TO_97 + 4A000054 ; SKIP_INST_GE + 130000 14 ; ^~ascii_other BRANCH + + # deal with a-f + 1F9C01F1 ; CMP_X0_TO_103 + 4A000054 ; SKIP_INST_GE + 0C0000 14 ; ^~ascii_low BRANCH + + # The rest that remains needs to be ignored + 0F0000 14 ; ^~ascii_other BRANCH + +#:purge_comment + FE0F1FF8 ; PUSH_LR # push lr + +#:purge_comment_loop + # Read a byte + 1A0000 94 ; ^~Read_byte CALL + + # Loop if not LF + 1F2800F1 ; CMP_X0_TO_10 + 40000054 ; SKIP_INST_EQ + FDFFFF 17 ; ^~purge_comment_loop RBRANCH + + # Otherwise return -1 + 00008092 ; SET_X0_TO_MINUS_1 + + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + +#:ascii_num + 00C000D1 ; SUB_X0_48 + C0035FD6 ; RETURN + +#:ascii_low + 005C01D1 ; SUB_X0_87 + C0035FD6 ; RETURN + +#:ascii_high + 00DC00D1 ; SUB_X0_55 + C0035FD6 ; RETURN + +#:ascii_other + 00008092 ; SET_X0_TO_MINUS_1 + C0035FD6 ; RETURN + +#:Done + # program completed Successfully + 000080D2 ; SET_X0_TO_0 # All is well + A80B80D2 ; SET_X8_TO_SYS_EXIT # put the exit syscall number in x8 + 010000d4 ; SYSCALL + +#:write_byte + # Print our Hex + E0030AAA ; SET_X0_FROM_X10 # Where are we writing to + 41000018 ; LOAD_W1_AHEAD # What we are writing + 02000014 ; SKIP_32_DATA + 0C02 6000 ; $output DATA_OFFSET + 220080D2 ; SET_X2_TO_1 # set the size of chars we want + 080880D2 ; SET_X8_TO_SYS_WRITE # the syscall number for write + 010000d4 ; SYSCALL # call the Kernel + C0035FD6 ; RETURN + +#:Read_byte + # Attempt to read 1 byte from input file + E00309AA ; SET_X0_FROM_X9 # Where are we reading from + 41000018 ; LOAD_W1_AHEAD # Where to put it + 02000014 ; SKIP_32_DATA + 0D02 6000 ; $input DATA_OFFSET + 220080D2 ; SET_X2_TO_1 # set the size of chars we want + E80780D2 ; SET_X8_TO_SYS_READ # the syscall number for read + 010000D4 ; SYSCALL # call the Kernel + + 1F0000F1 ; CMP_X0_TO_0 # Got EOF call it done + 41000054 ; SKIP_INST_NE + ECFFFF 17 ; ^~Done RBRANCH + + # load byte + 40000098 ; LOAD_W0_AHEAD + 02000014 ; SKIP_32_DATA + 0D02 6000 ; $input DATA_OFFSET + 00004039 ; LDR_BYTE_W0_[X0] # load char, unsigned so zero-extended already + C0035FD6 ; RETURN + +#:ELF_data +# Where we are putting our output +#:output + # Reserve a byte + 00 + +# Where we get our input +#:input + # Reserve a byte + 00 + +#:ELF_end diff --git a/vendor/seed/aarch64/hex1.hex0 b/vendor/seed/aarch64/hex1.hex0 @@ -0,0 +1,464 @@ +## Copyright (C) 2017 Jeremiah Orians +## Copyright (C) 2020 Sanne Wouda +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +## ELF Header + +#:ELF_base + +7F 45 4C 46 # e_ident[EI_MAG0-3] ELF's magic number + +02 # e_ident[EI_CLASS] Indicating 64 bit +01 # e_ident[EI_DATA] Indicating little endianness +01 # e_ident[EI_VERSION] Indicating original elf + +03 # e_ident[EI_OSABI] Set at 0 because none cares +00 # e_ident[EI_ABIVERSION] See above + +00 00 00 00 00 00 00 # e_ident[EI_PAD] +02 00 # e_type Indicating Executable +B7 00 # e_machine Indicating AArch64 +01 00 00 00 # e_version Indicating original elf + +78 00 60 00 00 00 00 00 # e_entry Address of the entry point +40 00 00 00 00 00 00 00 # e_phoff Address of program header table +00 00 00 00 00 00 00 00 # e_shoff Address of section header table + +00 00 00 00 # e_flags +40 00 # e_ehsize Indicating our 64 Byte header + +38 00 # e_phentsize size of a program header table +01 00 # e_phnum number of entries in program table + +00 00 # e_shentsize size of a section header table +00 00 # e_shnum number of entries in section table + +00 00 # e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 # ph_type: PT-LOAD = 1 +07 00 00 00 # ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 # ph_offset +00 00 60 00 00 00 00 00 # ph_vaddr +00 00 60 00 00 00 00 00 # ph_physaddr +3D 04 00 00 00 00 00 00 # ph_filesz +3D 04 00 00 00 00 00 00 # ph_memsz +01 00 00 00 00 00 00 00 # ph_align + +#:ELF_text + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function +#:_start + E10B40F9 ; LDR_X1_[SP,16] # Get the actual input name + 600C8092 ; SET_X0_TO_FCNTL_H_AT_FDCWD # AT_FDCWD, relative to current working directory + 020080D2 ; SET_X2_TO_0 # Prepare file as read only + 080780D2 ; SET_X8_TO_SYS_OPENAT # The syscall number for openat(), aarch64 has no open() + 010000D4 ; SYSCALL # Open file! + E90300AA ; SET_X9_FROM_X0 # Preserve the file pointer we were given + + E10F40F9 ; LDR_X1_[SP,24] # Get the output name + 600C8092 ; SET_X0_TO_FCNTL_H_AT_FDCWD # AT_FDCWD, relative to current working directory + 224880D2 ; SET_X2_TO_577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 033880D2 ; SET_X3_TO_448 # Prepare file as RWX for owner only (700 in octal) + 080780D2 ; SET_X8_TO_SYS_OPENAT # The syscall number for openat(), aarch64 has no open() + 010000D4 ; SYSCALL # Open file! + EA0300AA ; SET_X10_FROM_X0 # Preserve the file pointer we were given + + 0F008092 ; SET_X15_TO_MINUS_1 # Our flag for byte processing + 0E0080D2 ; SET_X14_TO_0 # temp storage for the sum + 0D0CA0D2 ; SET_X13_TO_0x600000 # Our starting IP + 0D0000 94 ; ^~First_pass CALL # Process it + + # rewind input file + E00309AA ; SET_X0_FROM_X9 # Using our input file + 010080D2 ; SET_X1_TO_0 # Offset + 020080D2 ; SET_X2_TO_0 # Whence SEEK_SET + C80780D2 ; SET_X8_TO_SYS_LSEEK # lseek + 010000D4 ; SYSCALL + + 0F008092 ; SET_X15_TO_MINUS_1 # Our flag for byte processing + 0E0080D2 ; SET_X14_TO_0 # temp storage for the sum + 0D0CA0D2 ; SET_X13_TO_0x600000 # Our starting IP + 2E0000 94 ; ^~Second_pass CALL # Process it + + # program competed Successfully + 000080D2 ; SET_X0_TO_0 # All is well + A80B80D2 ; SET_X8_TO_SYS_EXIT # put the exit syscall number in x8 + 010000D4 ; SYSCALL + +#:First_pass + FE0F1FF8 ; PUSH_LR # push lr + +#:First_pass_loop + 880000 94 ; ^~Read_byte CALL + + # Deal with EOF + 1F1000B1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + 240000 14 ; ^~First_pass_done BRANCH + + # Check for : + 1FE800F1 ; CMP_X0_TO_58 + 40000054 ; SKIP_INST_EQ + 020000 14 ; ^~First_pass_0 BRANCH + + # Deal with label + A10000 94 ; ^~StoreLabel CALL + +#:First_pass_0 + # Check for $ + 1F9000F1 ; CMP_X0_TO_36 + 41000054 ; SKIP_INST_NE + 140000 14 ; ^~First_pass_abs16pointer BRANCH + + # Check for ~ + 1FF801F1 ; CMP_X0_TO_126 + 41000054 ; SKIP_INST_NE + 140000 14 ; ^~First_pass_rel24pointer BRANCH + + # Check for & + 1F9800F1 ; CMP_X0_TO_38 + 41000054 ; SKIP_INST_NE + 140000 14 ; ^~First_pass_abs32pointer BRANCH + + # Deal with everything else + 4A0000 94 ; ^~hex CALL # Process our char + + # Deal with EOF + 1F1000B1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + 130000 14 ; ^~First_pass_done BRANCH + + # deal with -1 values + 1F0000F1 ; CMP_X0_TO_0 + 4A000054 ; SKIP_INST_GE + E9FFFF 17 ; ^~First_pass_loop RBRANCH + + # deal with toggle + FF0100F1 ; CMP_X15_TO_0 + 41000054 ; SKIP_INST_NE + 020000 14 ; ^~First_pass_1 BRANCH + AD050091 ; ADD_X13_1 # Increment IP + +#:First_pass_1 + EF032FAA ; NOT_X15 # Flip toggle + E3FFFF 17 ; ^~First_pass_loop RBRANCH + +#:First_pass_abs16pointer + # Deal with 16-bit Pointer to label + 6A0000 94 ; ^~Read_byte CALL # Drop the char + AD090091 ; ADD_X13_2 # Increment IP + E0FFFF 17 ; ^~First_pass_loop RBRANCH # Loop again + +#:First_pass_rel24pointer + # Deal with 24-bit Pointer to label + 670000 94 ; ^~Read_byte CALL # Drop the char + AD0D0091 ; ADD_X13_3 # Increment IP + DDFFFF 17 ; ^~First_pass_loop RBRANCH # Loop again + +#:First_pass_abs32pointer + # Deal with 32-bit Pointer to label + 640000 94 ; ^~Read_byte CALL # Drop the char + AD110091 ; ADD_X13_4 # Increment IP + DAFFFF17 ; ^~First_pass_loop RBRANCH # Loop again + +#:First_pass_done + FE0741F8 ; POP_LR + C0035FD6 ; RETURN + +#:Second_pass + FE0F1FF8 ; PUSH_LR # push lr + +#:Second_pass_loop + 5E0000 94 ; ^~Read_byte CALL + + # Deal with EOF + 1F1000B1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + 2C0000 14 ; ^~Second_pass_done BRANCH + + # Simply drop the label + 1FE800F1 ; CMP_X0_TO_58 + 40000054 ; SKIP_INST_EQ + 030000 14 ; ^~Second_pass_0 BRANCH + 570000 94 ; ^~Read_byte CALL + F8FFFF 17 ; ^~Second_pass_loop RBRANCH + +#:Second_pass_0 + # Check for $ + 1F9000F1 ; CMP_X0_TO_36 + 41000054 ; SKIP_INST_NE + 140000 14 ; ^~Second_pass_abs16pointer BRANCH + + # Check for ~, assume preceded by ^ + 1FF801F1 ; CMP_X0_TO_126 + 41000054 ; SKIP_INST_NE + 130000 14 ; ^~Second_pass_rel24pointer BRANCH + + # Check for $ + 1F9800F1 ; CMP_X0_TO_38 + 41000054 ; SKIP_INST_NE + 120000 14 ; ^~Second_pass_abs32pointer BRANCH + + # Deal with everything else + 1F0000 94 ; ^~hex CALL # Process our char + + # Deal with EOF + 1F1000B1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + 1A0000 14 ; ^~Second_pass_done BRANCH + + # deal with -1 values + 1F0000F1 ; CMP_X0_TO_0 + 4A000054 ; SKIP_INST_GE + E8FFFF 17 ; ^~Second_pass_loop RBRANCH + + # deal with toggle + FF0100F1 ; CMP_X15_TO_0 + 41000054 ; SKIP_INST_NE + 0A0000 14 ; ^~print BRANCH + + # process first byte of pair + EE0300AA ; SET_X14_FROM_X0 + 0F0080D2 ; SET_X15_TO_0 + E2FFFF 17 ; ^~Second_pass_loop RBRANCH + +#:Second_pass_abs16pointer + 640000 94 ; ^~StoreAbs16Pointer CALL + E0FFFF 17 ; ^~Second_pass_loop RBRANCH + +#:Second_pass_rel24pointer + 6E0000 94 ; ^~StoreRel24Pointer CALL + DEFFFF 17 ; ^~Second_pass_loop RBRANCH + +#:Second_pass_abs32pointer + 7A0000 94 ; ^~StoreAbs32Pointer CALL + DCFFFF 17 ; ^~Second_pass_loop RBRANCH + +# process second byte of pair +#:print + # update the sum and store in output + 00100E8B ; ADD_X0_X0_X14_LSL_4 + 41000018 ; LOAD_W1_AHEAD + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 20000039 ; STR_BYTE_W0_[X1] + + # flip the toggle + 0F008092 ; SET_X15_TO_MINUS_1 + + # print our first Hex + 220080D2 ; SET_X2_TO_1 # set the size of chars we want + 430000 94 ; ^~print_chars CALL + + AD050091 ; ADD_X13_1 # Increment IP + D2FFFF 17 ; ^~Second_pass_loop RBRANCH + +#:Second_pass_done + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + +#:hex + # deal with EOF + 1F1000B1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + 2A0000 14 ; ^~EOF BRANCH + + # Purge Comment Lines (#) + 1F8C00F1 ; CMP_X0_TO_35 + 41000054 ; SKIP_INST_NE + 170000 14 ; ^~purge_comment BRANCH + + # Purge Comment Lines (;) + 1FEC00F1 ; CMP_X0_TO_59 + 41000054 ; SKIP_INST_NE + 140000 14 ; ^~purge_comment BRANCH + + # deal all ascii less than 0 + 1FC000F1 ; CMP_X0_TO_48 + 4A000054 ; SKIP_INST_GE + 1F0000 14 ; ^~ascii_other BRANCH + + # deal with 0-9 + 1FE800F1 ; CMP_X0_TO_58 + 4A000054 ; SKIP_INST_GE + 160000 14 ; ^~ascii_num BRANCH + + # deal with all ascii less than A + 1F0401F1 ; CMP_X0_TO_65 + 4A000054 ; SKIP_INST_GE + 190000 14 ; ^~ascii_other BRANCH + + # deal with A-F + 1F1C01F1 ; CMP_X0_TO_71 + 4A000054 ; SKIP_INST_GE + 140000 14 ; ^~ascii_high BRANCH + + # deal with all ascii less than a + 1F8401F1 ; CMP_X0_TO_97 + 4A000054 ; SKIP_INST_GE + 130000 14 ; ^~ascii_other BRANCH + + # deal with a-f + 1F9C01F1 ; CMP_X0_TO_103 + 4A000054 ; SKIP_INST_GE + 0C0000 14 ; ^~ascii_low BRANCH + + # The rest that remains needs to be ignored + 0F0000 14 ; ^~ascii_other BRANCH + +#:purge_comment + FE0F1FF8 ; PUSH_LR # push lr + +#:loop0 + # Read a byte + 100000 94 ; ^~Read_byte CALL + + # Loop if not LF + 1F2800F1 ; CMP_X0_TO_10 + 40000054 ; SKIP_INST_EQ + FDFFFF 17 ; ^~loop0 RBRANCH + + # Otherwise return -1 + 00008092 ; SET_X0_TO_MINUS_1 + + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + +#:ascii_num + 00C000D1 ; SUB_X0_48 + C0035FD6 ; RETURN + +#:ascii_low + 005C01D1 ; SUB_X0_87 + C0035FD6 ; RETURN + +#:ascii_high + 00DC00D1 ; SUB_X0_55 + C0035FD6 ; RETURN + +#:ascii_other + 00008092 ; SET_X0_TO_MINUS_1 + C0035FD6 ; RETURN + +#:EOF + C0035FD6 ; RETURN + +#:Read_byte + # Attempt to read 1 byte from input file + E00309AA ; SET_X0_FROM_X9 # Where are we reading from + 41000018 ; LOAD_W1_AHEAD # Where to put it + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 220080D2 ; SET_X2_TO_1 # set the size of chars we want + E80780D2 ; SET_X8_TO_SYS_READ # the syscall number for read + 010000D4 ; SYSCALL # call the Kernel + + 1F0000F1 ; CMP_X0_TO_0 + 41000054 ; SKIP_INST_NE + 060000 14 ; ^~Read_byte_1 BRANCH + + # load byte + 40000098 ; LOAD_W0_AHEAD + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 00004039 ; LDR_BYTE_W0_[X0] # load char, unsigned so zero-extended already + C0035FD6 ; RETURN + +# Deal with EOF +#:Read_byte_1 + 60008092 ; SET_X0_TO_MINUS_4 # Put EOF in x0 + C0035FD6 ; RETURN + +#:print_chars + E0030AAA ; SET_X0_FROM_X10 # Write to target file + 41000018 ; LOAD_W1_AHEAD # What we are writing + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 080880D2 ; SET_X8_TO_SYS_WRITE # the syscall number for write + 010000D4 ; SYSCALL # call the Kernel + C0035FD6 ; RETURN + +#:Get_table_target + FE0F1FF8 ; PUSH_LR # push lr + E7FFFF 97 ; ^~Read_byte RCALL # Get single char label + 41000018 ; LOAD_W1_AHEAD + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 200C008B ; ADD_X0_X1_X0_LSL_3 # Each label in table takes 8 bytes to store + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + +#:StoreLabel + FE0F1FF8 ; PUSH_LR + F7FFFF 97 ; ^~Get_table_target RCALL + 0D0000F9 ; STR_X13_[X0] + FE0741F8 ; POP_LR + C0035FD6 ; RETURN + +#:StoreAbs16Pointer + FE0F1FF8 ; PUSH_LR # push lr + F2FFFF 97 ; ^~Get_table_target RCALL # Get address of pointer + 000040F9 ; DEREF_X0 # Get pointer + 41000018 ; LOAD_W1_AHEAD + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 200000F9 ; STR_X0_[X1] # put value in output + AD090091 ; ADD_X13_2 # Increment IP + 420080D2 ; SET_X2_TO_2 # set the size of chars we want + E3FFFF 97 ; ^~print_chars RCALL + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + +#:StoreRel24Pointer + FE0F1FF8 ; PUSH_LR # push lr + E6FFFF 97 ; ^~Get_table_target RCALL # Get address of pointer + 000040F9 ; DEREF_X0 # Get pointer + 00000DCB ; SUB_X0_X0_X13 # target - ip + 00FC4293 ; ASR_X0_X0_2 # rel24 pointer is 4-byte aligned + 41000018 ; LOAD_W1_AHEAD + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 200000F9 ; STR_X0_[X1] # put value in output + AD0D0091 ; ADD_X13_3 # Increment IP + 620080D2 ; SET_X2_TO_3 # set the size of chars we want + D5FFFF 97 ; ^~print_chars RCALL + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + +#:StoreAbs32Pointer + FE0F1FF8 ; PUSH_LR # push lr + D8FFFF 97 ; ^~Get_table_target RCALL # Get address of pointer + 000040F9 ; DEREF_X0 # Get pointer + 41000018 ; LOAD_W1_AHEAD + 02000014 ; SKIP_32_DATA + 3C04 6000 ; &table + 200000F9 ; STR_X0_[X1] # put value in output + AD110091 ; ADD_X13_4 # Increment IP + 820080D2 ; SET_X2_TO_4 # set the size of chars we want + C9FFFF 97 ; ^~print_chars RCALL + FE0741F8 ; POP_LR # pop lr + C0035FD6 ; RETURN + + +#:ELF_data +#:table + 00 ; NULL8 + +#:ELF_end diff --git a/vendor/seed/aarch64/hex2.hex1 b/vendor/seed/aarch64/hex2.hex1 @@ -0,0 +1,694 @@ +## Copyright (C) 2017 Jeremiah Orians +## Copyright (C) 2020 Sanne Wouda +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +#:ELF_base + +7F 45 4C 46 # e_ident[EI_MAG0-3] ELF's magic number + +02 # e_ident[EI_CLASS] Indicating 64 bit +01 # e_ident[EI_DATA] Indicating little endianness +01 # e_ident[EI_VERSION] Indicating original elf + +03 # e_ident[EI_OSABI] Set at 0 because none cares +00 # e_ident[EI_ABIVERSION] See above + +00 00 00 00 00 00 00 # e_ident[EI_PAD] +02 00 # e_type Indicating Executable +B7 00 # e_machine Indicating AArch64 +01 00 00 00 # e_version Indicating original elf + +78 00 60 00 00 00 00 00 # e_entry Address of the entry point +40 00 00 00 00 00 00 00 # e_phoff Address of program header table +00 00 00 00 00 00 00 00 # e_shoff Address of section header table + +00 00 00 00 # e_flags +40 00 # e_ehsize Indicating our 64 Byte header + +38 00 # e_phentsize size of a program header table +01 00 # e_phnum number of entries in program table + +00 00 # e_shentsize size of a section header table +00 00 # e_shnum number of entries in section table + +00 00 # e_shstrndx index of the section names + +#:ELF_program_headers +#:ELF_program_header__text +01 00 00 00 # ph_type: PT-LOAD = 1 +07 00 00 00 # ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 # ph_offset +00 00 60 00 00 00 00 00 # ph_vaddr +00 00 60 00 00 00 00 00 # ph_physaddr +21 07 00 00 00 00 00 00 # ph_filesz +21 07 00 00 00 00 00 00 # ph_memsz +01 00 00 00 00 00 00 00 # ph_align + + # Register usage: + # X0, X1, X2, X3 => Temps + # X15 => Flag + # X14 => High bits + # X13 => IP + # X12 => MALLOC + # X11 => HEAD + + # Struct format: (size 24) + # NEXT => 0 + # TARGET => 8 + # NAME => 16 + +#:ELF_text + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function +#:_start + 000080d2 ; SET_X0_TO_0 # Get current pointer + ^~m 94 ; ^~malloc FCALL # Get current HEAP + ec0300aa ; SET_X12_FROM_X0 # Setup MALLOC + 00007091 ; ADD_X0_X0_12MB # Create space for temp [12MB] + ^~m 94 ; ^~malloc FCALL # Give ourselves 81920 bytes to work with + + e10b40f9 ; LDR_X1_[SP,16] # Get the actual input name + 600c8092 ; SET_X0_TO_FCNTL_H_AT_FDCWD # AT_FDCWD, relative to current working directory + 020080d2 ; SET_X2_TO_0 # Prepare file as read only + 080780d2 ; SET_X8_TO_SYS_OPENAT # The syscall number for openat(), aarch64 has no open() + 010000d4 ; SYSCALL # Open file! + e90300aa ; SET_X9_FROM_X0 # Preserve the file pointer we were given + + e10f40f9 ; LDR_X1_[SP,24] # Get the output name + 600c8092 ; SET_X0_TO_FCNTL_H_AT_FDCWD # AT_FDCWD, relative to current working directory + 224880d2 ; SET_X2_TO_577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 033880d2 ; SET_X3_TO_448 # Prepare file as RWX for owner only (700 in octal) + 080780d2 ; SET_X8_TO_SYS_OPENAT # The syscall number for openat(), aarch64 has no open() + 010000d4 ; SYSCALL # Open file! + 1f0000f1 ; CMP_X0_TO_0 # Check for missing output + 4d000054 ; SKIP_INST_LE # Have real output + ^~s 14 ; ^~_start_out FBRANCH + 200080d2 ; SET_X0_TO_1 # Use stdout + +:s + ea0300aa ; SET_X10_FROM_X0 # Preserve the file pointer we were given + + ^~C 94 ; ^~ClearScratch FCALL # Zero scratch + 0f008092 ; SET_X15_TO_MINUS_1 # Our flag for byte processing + 0e0080d2 ; SET_X14_TO_0 # temp storage for the sum + 0d0ca0d2 ; SET_X13_TO_0x00600000 # Our starting IP + 0b0080d2 ; SET_X11_TO_0 # HEAD = NULL + ^~F 94 ; ^~First_pass FCALL # Process it + + # rewind input file + e00309aa ; SET_X0_FROM_X9 # Using our input file + 010080d2 ; SET_X1_TO_0 # Offset + 020080d2 ; SET_X2_TO_0 # Whence SEEK_SET + c80780d2 ; SET_X8_TO_SYS_LSEEK # lseek + 010000d4 ; SYSCALL + + 0f008092 ; SET_X15_TO_MINUS_1 # Our flag for byte processing + 0e0080d2 ; SET_X14_TO_0 # temp storage for the sum + 0d0ca0d2 ; SET_X13_TO_0x00600000 # Our starting IP + ^~S 94 ; ^~Second_pass FCALL # Process it + + # program completed Successfully + 000080d2 ; SET_X0_TO_0 # All is well + a80b80d2 ; SET_X8_TO_SYS_EXIT # put the exit syscall number in x8 + 010000d4 ; SYSCALL + + +:F ; :First_pass + fe0f1ff8 ; PUSH_LR # push lr +:a ; :First_pass_loop + ^~R 94 ; ^~Read_byte FCALL + + # Deal with EOF + 1f1000b1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + ^~d 14 ; ^~First_pass_done FBRANCH + + # Check for : + 1fe800f1 ; CMP_X0_TO_58 + 40000054 ; SKIP_INST_EQ + ^~b 14 ; ^~First_pass_0 FBRANCH + + # Deal with label + e0030caa ; SET_X0_FROM_X12 # ENTRY + 8c610091 ; ADD_X12_X12_24 #^~ CALLOC + 0c0800f9 ; STR_X12_[X0,16] # ENTRY->NAME = TOKEN + 0d0400f9 ; STR_X13_[X0,8] # ENTRY->TARGET = IP + 0b0000f9 ; STR_X11_[X0] # ENTRY->NEXT = HEAD + eb0300aa ; SET_X11_FROM_X0 # HEAD = ENTRY + e1030caa ; SET_X1_FROM_X12 # Write Starting after struct + ^~J 94 ; ^~consume_token FCALL # Collect whole string + ec0301aa ; SET_X12_FROM_X1 # Update HEAP + ^~a 17 ; ^~First_pass_loop RBRANCH + +:b ; :First_pass_0 + # Check for ! + 1f8400f1 ; CMP_X0_TO_33 + 41000054 ; SKIP_INST_NE + ^~Z 14 ; ^~First_pass_pointer FBRANCH + + # Check for @ + 1f0001f1 ; CMP_X0_TO_64 + 41000054 ; SKIP_INST_NE + ^~Z 14 ; ^~First_pass_pointer FBRANCH + + # Check for $ + 1f9000f1 ; CMP_X0_TO_36 + 41000054 ; SKIP_INST_NE + ^~Z 14 ; ^~First_pass_pointer FBRANCH + + # Check for % + 1f9400f1 ; CMP_X0_TO_37 + 41000054 ; SKIP_INST_NE + ^~Z 14 ; ^~First_pass_pointer FBRANCH + + # Check for & + 1f9800f1 ; CMP_X0_TO_38 + 41000054 ; SKIP_INST_NE + ^~Z 14 ; ^~First_pass_pointer FBRANCH + + # Check for ~ + 1ff801f1 ; CMP_X0_TO_126 + 41000054 ; SKIP_INST_NE + ^~Z 14 ; ^~First_pass_pointer FBRANCH + + # Deal with everything else + ^~x 94 ; ^~hex FCALL # Process our char + + # Deal with EOF + 1f1000b1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + ^~d 14 ; ^~First_pass_done FBRANCH + + # deal with -1 values + 1f0000f1 ; CMP_X0_TO_0 + 4a000054 ; SKIP_INST_GE + ^~a 17 ; ^~First_pass_loop RBRANCH + + # deal with toggle + ff0100f1 ; CMP_X15_TO_0 + 41000054 ; SKIP_INST_NE + ^~c 14 ; ^~First_pass_1 FBRANCH + ad050091 ; ADD_X13_X13_1 # Increment IP + +:c ; :First_pass_1 + ef032faa ; NOT_X15_X15 + ^~a 17 ; ^~First_pass_loop RBRANCH + +:Z ; :First_pass_pointer + # Deal with Pointer to label + ^~e 94 ; ^~Update_Pointer FCALL # Increment IP + 41000018 ; LOAD_W1_AHEAD # Using scratch + 02000014 ; SKIP_32_DATA + &1 ; &scratch + ^~J 94 ; ^~consume_token FCALL # Read token + ^~C 94 ; ^~ClearScratch FCALL # Throw away token + 1ff800f1 ; CMP_X0_TO_62 # check for '>' + 40000054 ; SKIP_INST_EQ # Loop again + ^~a 17 ; ^~First_pass_loop RBRANCH + + # Deal with %label>label case + 41000018 ; LOAD_W1_AHEAD # Write to scratch + 02000014 ; SKIP_32_DATA + &1 ; &scratch + ^~J 94 ; ^~consume_token FCALL # get token + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~a 17 ; ^~First_pass_loop RBRANCH # Loop again + +:d ; :First_pass_done + fe0741f8 ; POP_LR # pop lr + c0035fd6 ; RETURN + +:e ; :Update_Pointer + # Check for ! + 1f8400f1 ; CMP_X0_TO_33 + 41000054 ; SKIP_INST_NE + ^~i 14 ; ^~Update_Pointer_1 FBRANCH + + # Check for @ + 1f0001f1 ; CMP_X0_TO_64 + 41000054 ; SKIP_INST_NE + ^~h 14 ; ^~Update_Pointer_2 FBRANCH + + # Check for $ + 1f9000f1 ; CMP_X0_TO_36 + 41000054 ; SKIP_INST_NE + ^~h 14 ; ^~Update_Pointer_2 FBRANCH + + # Check for ~ + 1ff801f1 ; CMP_X0_TO_126 + 41000054 ; SKIP_INST_NE + ^~g 14 ; ^~Update_Pointer_3 FBRANCH + + # Check for % + 1f9400f1 ; CMP_X0_TO_37 + 41000054 ; SKIP_INST_NE + ^~f 14 ; ^~Update_Pointer_4 FBRANCH + + # Check for & + 1f9800f1 ; CMP_X0_TO_38 + 41000054 ; SKIP_INST_NE + ^~f 14 ; ^~Update_Pointer_4 FBRANCH + + # deal with bad input + ^~Y 14 ; ^~fail FBRANCH + +:f ; :Update_Pointer_4 + ad050091 ; ADD_X13_X13_1 # Increment IP +:g ; :Update_Pointer_3 + ad050091 ; ADD_X13_X13_1 # Increment IP +:h ; :Update_Pointer_2 + ad050091 ; ADD_X13_X13_1 # Increment IP +:i ; Update_Pointer_1 + ad050091 ; ADD_X13_X13_1 # Increment IP + c0035fd6 ; RETURN + + +:S ; :Second_pass + fe0f1ff8 ; PUSH_LR # push lr + + # x7 marks whether we need to do a word-aligned pointer +:j ; :Second_pass_loop + 070080d2 ; SET_X7_TO_0 +:k ; :Second_pass_align + ^~R 94 ; ^~Read_byte FCALL + + # Deal with EOF + 1f1000b1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + ^~w 14 ; ^~Second_pass_done FBRANCH + + # Deal with ^ + 1f7801f1 ; CMP_X0_TO_94 + 41000054 ; SKIP_INST_NE + ^~l 14 ; ^~Second_pass_align_0 FBRANCH + + # Simply drop the label + 1fe800f1 ; CMP_X0_TO_58 + 40000054 ; SKIP_INST_EQ + ^~n 14 ; ^~Second_pass_0 FBRANCH + + 41000018 ; LOAD_W1_AHEAD # Using scratch + 02000014 ; SKIP_32_DATA + &1 ; &scratch + ^~J 94 ; ^~consume_token FCALL # Read token + ^~C 94 ; ^~ClearScratch FCALL # Throw away token + + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:l ; :Second_pass_align_0 + e70327aa ; NOT_X7_X7 # toggle alignment + ^~k 17 ; ^~Second_pass_align RBRANCH + +:n ; :Second_pass_0 + # Deal with % pointer + 1f9400f1 ; CMP_X0_TO_37 + 41000054 ; SKIP_INST_NE + ^~o 14 ; ^~StorePointer_rel4 FBRANCH + + # Deal with ~ pointer + 1ff801f1 ; CMP_X0_TO_126 + 41000054 ; SKIP_INST_NE + ^~q 14 ; ^~StorePointer_rel3 FBRANCH + + # Deal with @ pointer + 1f0001f1 ; CMP_X0_TO_64 + 41000054 ; SKIP_INST_NE + ^~r 14 ; ^~StorePointer_rel2 FBRANCH + + # Deal with ! pointer + 1f8400f1 ; CMP_X0_TO_33 + 41000054 ; SKIP_INST_NE + ^~t 14 ; ^~StorePointer_rel1 FBRANCH + + # Deal with & pointer + 1f9800f1 ; CMP_X0_TO_38 + 41000054 ; SKIP_INST_NE + ^~u 14 ; ^~StorePointer_abs4 FBRANCH + + # Deal with $ pointer + 1f9000f1 ; CMP_X0_TO_36 + 41000054 ; SKIP_INST_NE + ^~v 14 ; ^~StorePointer_abs2 FBRANCH + + # Deal with everything else + ^~x 94 ; ^~hex FCALL # Process our char + + # Deal with EOF + 1f1000b1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + ^~w 14 ; ^~Second_pass_done FBRANCH + + # deal with -1 values + 1f0000f1 ; CMP_X0_TO_0 + 4a000054 ; SKIP_INST_GE + ^~j 17 ; ^~Second_pass_loop RBRANCH + + # deal with toggle + ff0100f1 ; CMP_X15_TO_0 + 41000054 ; SKIP_INST_NE + ^~p 14 ; ^~print FBRANCH + + # process first byte of pair + ee0300aa ; SET_X14_FROM_X0 + 0f0080d2 ; SET_X15_TO_0 + ^~j 17 ; ^~Second_pass_loop RBRANCH + + # process second byte of pair +:p ; :print + # update the sum and store in output + 00100e8b ; ADD_X0_X0_X14_LSL_4 + + # Print our first Hex + 220080d2 ; SET_X2_TO_1 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + + # flip the toggle + ef032faa ; NOT_X15_X15 + + ad050091 ; ADD_X13_X13_1 # Increment IP + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:o ; :StorePointer_rel4 + ^~W 94 ; ^~StorePointer FCALL # Do Common + 000002cb ; SUB_X0_X0_X2 # target - ip + 470000b4 ; CBZ_X7_PAST_INST + 00fc4293 ; ASR_X0_X0_2 + 820080d2 ; SET_X2_TO_4 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:q ; :StorePointer_rel3 + ^~W 94 ; ^~StorePointer FCALL # Do Common + 000002cb ; SUB_X0_X0_X2 # target - ip + 470000b4 ; CBZ_X7_PAST_INST + 00fc4293 ; ASR_X0_X0_2 + 620080d2 ; SET_X2_TO_3 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:r ; :StorePointer_rel2 + ^~W 94 ; ^~StorePointer FCALL # Do Common + 000002cb ; SUB_X0_X0_X2 # target - ip + 470000b4 ; CBZ_X7_PAST_INST + 00fc4293 ; ASR_X0_X0_2 + 420080d2 ; SET_X2_TO_2 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:t ; :StorePointer_rel1 + ^~W 94 ; ^~StorePointer FCALL # Do Common + 000002cb ; SUB_X0_X0_X2 # target - ip + 470000b4 ; CBZ_X7_PAST_INST + 00fc4293 ; ASR_X0_X0_2 + 220080d2 ; SET_X2_TO_1 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:u ; :StorePointer_abs4 + ^~W 94 ; ^~StorePointer FCALL # Do Common + 820080d2 ; SET_X2_TO_4 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:v ; :StorePointer_abs2 + ^~W 94 ; ^~StorePointer FCALL # Do Common + 420080d2 ; SET_X2_TO_2 # set the size of chars we want + ^~I 94 ; ^~print_chars FCALL + ^~C 94 ; ^~ClearScratch FCALL # Clean up after ourselves + ^~j 17 ; ^~Second_pass_loop RBRANCH + +:w ; :Second_pass_done + fe0741f8 ; POP_LR # pop lr + c0035fd6 ; RETURN + +:x ; :hex + # deal with EOF + 1f1000b1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + ^~z 14 ; ^~EOF FBRANCH + # deal with line comments starting with # + 1f8c00f1 ; CMP_X0_TO_35 + 41000054 ; SKIP_INST_NE + ^~E 14 ; ^~ascii_comment FBRANCH + # deal with line comments starting with ; + 1fec00f1 ; CMP_X0_TO_59 + 41000054 ; SKIP_INST_NE + ^~E 14 ; ^~ascii_comment FBRANCH + # deal all ascii less than 0 + 1fc000f1 ; CMP_X0_TO_48 + 4a000054 ; SKIP_INST_GE + ^~y 14 ; ^~ascii_other FBRANCH + # deal with 0-9 + 1fe800f1 ; CMP_X0_TO_58 + 4a000054 ; SKIP_INST_GE + ^~A 14 ; ^~ascii_num FBRANCH + # deal with all ascii less than A + 1f0401f1 ; CMP_X0_TO_65 + 4a000054 ; SKIP_INST_GE + ^~y 14 ; ^~ascii_other FBRANCH + # deal with A-F + 1f1c01f1 ; CMP_X0_TO_71 + 4a000054 ; SKIP_INST_GE + ^~D 14 ; ^~ascii_high FBRANCH + #deal with all ascii less than a + 1f8401f1 ; CMP_X0_TO_97 + 4a000054 ; SKIP_INST_GE + ^~y 14 ; ^~ascii_other FBRANCH + #deal with a-f + 1f9c01f1 ; CMP_X0_TO_103 + 4a000054 ; SKIP_INST_GE + ^~B 14 ; ^~ascii_low FBRANCH + # The rest that remains needs to be ignored +:y ; :ascii_other + 00008092 ; SET_X0_TO_MINUS_1 + c0035fd6 ; RETURN + +:z ; :EOF + c0035fd6 ; RETURN +:A ; :ascii_num + 00c000d1 ; SUB_X0_X0_48 + c0035fd6 ; RETURN +:B ; :ascii_low + 005c01d1 ; SUB_X0_X0_87 + c0035fd6 ; RETURN +:D ; :ascii_high + 00dc00d1 ; SUB_X0_X0_55 + c0035fd6 ; RETURN +:E ; :ascii_comment + fe0f1ff8 ; PUSH_LR # push lr +:G ; :ascii_comment_loop + ^~R 94 ; ^~Read_byte FCALL + 1f3400f1 ; CMP_X0_TO_13 + 41000054 ; SKIP_INST_NE + ^~H 14 ; ^~ascii_comment_cr FBRANCH + 1f2800f1 ; CMP_X0_TO_10 + 40000054 ; SKIP_INST_EQ + ^~G 17 ; ^~ascii_comment_loop RBRANCH +:H ; :ascii_comment_cr + 00008092 ; SET_X0_TO_MINUS_1 + fe0741f8 ; POP_LR # pop lr + c0035fd6 ; RETURN + + +# Malloc isn't actually required if the program being built fits in the initial memory +# However, it doesn't take much to add it. +# Requires a value in X0 +:m + c81a80d2 ; SET_X8_TO_SYS_BRK # the Syscall # for SYS_BRK + 010000d4 ; SYSCALL # call the Kernel + c0035fd6 ; RETURN + + +:R ; :Read_byte + e10f1ff8 ; PUSH_X1 + 60008092 ; SET_X0_TO_MINUS_4 # Put EOF in x0 + e00f1ff8 ; PUSH_X0 + + # Attempt to read 1 byte from STDIN + e00309aa ; SET_X0_FROM_X9 # Where are we reading from + e1030091 ; SET_X1_FROM SP # Where to put it + 220080d2 ; SET_X2_TO_1 # set the size of chars we want + e80780d2 ; SET_X8_TO_SYS_READ # the syscall number for read + 010000d4 ; SYSCALL # call the Kernel + + e0078138 ; POP_X0SB + e10741f8 ; POP_X1 + c0035fd6 ; RETURN + +:I ; :print_chars + e00f1ff8 ; PUSH_X0 + e1030091 ; SET_X1_FROM_SP + e0030aaa ; SET_X0_FROM_X10 # Write to target file + 080880d2 ; SET_X8_TO_SYS_WRITE # the syscall number for write + 010000d4 ; SYSCALL # call the Kernel + e00741f8 ; POP_X0 + c0035fd6 ; RETURN + +# Receives pointer in x1 +# Writes out char and updates x1 +# leaves with x1 8-byte aligned +:J ; :consume_token + fe0f1ff8 ; PUSH_LR # push lr +:K ; :consume_token_loop + ^~R 97 ; ^~Read_byte RCALL # Consume_token + + 1f1000b1 ; CMP_X0_TO_MINUS_4 + 41000054 ; SKIP_INST_NE + ^~L 14 ; ^~consume_token_done FBRANCH + + # Check for \t + 1f2400f1 ; CMP_X0_TO_9 + 41000054 ; SKIP_INST_NE + ^~L 14 ; ^~consume_token_done FBRANCH + + # Check for \n + 1f2800f1 ; CMP_X0_TO_10 + 41000054 ; SKIP_INST_NE + ^~L 14 ; ^~consume_token_done FBRANCH + + # Check for ' ' + 1f8000f1 ; CMP_X0_TO_32 + 41000054 ; SKIP_INST_NE + ^~L 14 ; ^~consume_token_done FBRANCH + + # Check for '>' + 1ff800f1 ; CMP_X0_TO_62 + 41000054 ; SKIP_INST_NE + ^~L 14 ; ^~consume_token_done FBRANCH + + # Looks like we are still reading token + 20140038 ; STR_BYTE_W0_[X1]_1 # store char and update pointer + ^~K 17 ; ^~consume_token_loop RBRANCH # loop until done + +:L ; :consume_token_done + 030080d2 ; SET_X3_TO_0 +:M ; :consume_token_pad + 23140038 ; STR_BYTE_W3_[X1]_1 # write at least one zero-byte + 3f0840f2 ; TST_X1_7 # is x1 8-byte aligned? + 40000054 ; SKIP_INST_EQ + ^~M 17 ; ^~consume_token_pad RBRANCH + + fe0741f8 ; POP_LR # pop lr + c0035fd6 ; RETURN + +# finds label matching pointer in scratch +# clobbers x1, x3, x4, x5 +# returns address in x0 +:N ; :GetTarget + e5030baa ; SET_X5_FROM_X11 # Grab HEAD +:O ; :GetTarget_loop + 44000018 ; LOAD_W4_AHEAD # Reset scratch + 02000014 ; SKIP_32_DATA + &1 ; &scratch + a30840f9 ; LDR_X3_[X5,16] # I->NAME +:P ; :GetTarget_loop_0 + 60144038 ; LDR_BYTE_W0_[X3]_1 # I->NAME[0] + 81144038 ; LDR_BYTE_W1_[X4]_1 # scratch[0] + 1f0001eb ; CMP_X0_X1 # IF TOKEN == I->NAME + 40000054 ; SKIP_INST_EQ # Oops + ^~T 14 ; ^~GetTarget_miss FBRANCH + + 3f0000f1 ; CMP_X1_TO_0 + 40000054 ; SKIP_INST_EQ # Loop until + ^~P 17 ; ^~GetTarget_loop_0 RBRANCH + ^~U 14 ; ^~GetTarget_done FBRANCH # Match + + # Miss +:T ; :GetTarget_miss + a50040f9 ; DEREF_X5 # I = I->NEXT + bf0000f1 ; CMP_X5_TO_0 # IF NULL == I + 41000054 ; SKIP_INST_NE # Abort hard + ^~Y 14 ; ^~fail FBRANCH + + ^~O 17 ; ^~GetTarget_loop RBRANCH + +:U ; GetTarget_done + a00440f9 ; LDR_X0_[X5,8] # Get address + c0035fd6 ; RETURN + +# clobbers x3, x4 +:C ; ClearScratch + e00f1ff8 ; PUSH_X0 + 44000018 ; LOAD_W4_AHEAD # Where our table is + 02000014 ; SKIP_32_DATA + &1 ; &scratch + 000080d2 ; SET_X0_TO_0 # Using null + +:V ; :ClearScratch_loop + 83004039 ; LDR_BYTE_W3_[X4] # Get current value + 801c0038 ; STR_BYTE_W0_[X4,1]_WB # Because we want nuoll + 7f0000f1 ; CMP_X3_TO_0 # Check if we hit null + 40000054 ; SKIP_INST_EQ # Keep looping + ^~V 17 ; ^~ClearScratch_loop RBRANCH + + e00741f8 ; POP_X0 + c0035fd6 ; RETURN + + +# returns target in x0, base in x2 +:W ; :StorePointer + fe0f1ff8 ; PUSH_LR + e6030daa ; SET_X6_FROM_X13 + ^~e 97 ; ^~Update_Pointer RCALL # Increment IP + 41000018 ; LOAD_W1_AHEAD # Write to scratch + 02000014 ; SKIP_32_DATA + &1 ; &scratch + ^~J 97 ; ^~consume_token RCALL # get token + e00f1ff8 ; PUSH_X0 # Protect base_sep_p + ^~N 97 ; ^~GetTarget RCALL # Get address of pointer + ^~C 97 ; ^~ClearScratch RCALL # Clean up after ourselves + e20306aa ; SET_X2_FROM_X6 # base = IP + e10741f8 ; POP_X1 # Restore base_sep_p + 3ff800f1 ; CMP_X1_TO_62 # If base_sep_p == '>' + 40000054 ; SKIP_INST_EQ # If not + ^~X 14 ; ^~StorePointer_done FBRANCH + + # Deal with %label>label case + e00f1ff8 ; PUSH_X0 # We need to preserve main target + 41000018 ; LOAD_W1_AHEAD # Write to scratch + 02000014 ; SKIP_32_DATA + &1 ; &scratch + ^~J 97 ; ^~consume_token RCALL # get token + ^~N 97 ; ^~GetTarget RCALL # Get address of pointer + ^~C 97 ; ^~ClearScratch RCALL # Clean up after ourselves + e20300aa ; SET_X2_FROM_X0 # Use our new base + e00741f8 ; POP_X0 # Restore main target + +:X ; :StorePointer_done + fe0741f8 ; POP_LR # pop lr + c0035fd6 ; RETURN + + +:Y ; fail + # Some shit went wrong + 200080d2 ; SET_X0_TO_1 # All is wrong + a80b80d2 ; SET_X8_TO_SYS_EXIT # put the exit syscall number in eax + 010000d4 ; SYSCALL # Call it a good day + +#:ELF_data +:0 + 0000000000000000 ; NULL64 +:1 + 00 ; NULL8 + +#:ELF_end diff --git a/vendor/seed/amd64/ELF.hex2 b/vendor/seed/amd64/ELF.hex2 @@ -0,0 +1,68 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org> +### This file is part of M2-Planet. +### +### M2-Planet is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### M2-Planet is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with M2-Planet. If not, see <http://www.gnu.org/licenses/>. + +### elf64.hex2: 64 bit elf header in hex2 +### if you wish to use this header, you need to add :ELF_end to the end of your +### M1 or hex2 files. + +## ELF Header + +:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +3E 00 ## e_machine Indicating AMD64 +01 00 00 00 ## e_version Indicating original elf + +&_start 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +%ELF_program_headers>ELF_base 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +&ELF_base 00 00 00 00 ## p_vaddr +&ELF_base 00 00 00 00 ## p_physaddr + +%ELF_end>ELF_base 00 00 00 00 ## p_filesz +%ELF_end>ELF_base 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +:ELF_text diff --git a/vendor/seed/amd64/M0.hex2 b/vendor/seed/amd64/M0.hex2 @@ -0,0 +1,845 @@ +# SPDX-FileCopyrightText: 2019 Jeremiah Orians <jeremiah@pdp10.guru> +# SPDX-FileCopyrightText: 2023 Andrius Štikonas <andrius@stikonas.eu> +# +# SPDX-License-Identifier: GPL-3.0-or-later + + # Register usage: + # RAX, RSI, RDI => Temps + # R12 => MALLOC + # R13 => HEAD + # R14 => Output_file + # R15 => Input_file + + # Struct format: (size 32) + # NEXT => 0 + # TYPE => 8 + # TEXT => 16 + # EXPRESSION => 24 + + # Types + # None => 0 + # MACRO => 1 + # STRING => 2 + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function +:_start + 58 ; pop_rax # Get the number of arguments + 5F ; pop_rdi # Get the program name + 5F ; pop_rdi # Get the actual input name + 48C7C6 00000000 ; mov_rsi, %0 # prepare read_only + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 4989C7 ; mov_r15,rax # Preserve the file pointer we were given + + 5F ; pop_rdi # Get the actual output name + 48C7C6 41020000 ; mov_rsi, %577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 48C7C2 80010000 ; mov_rdx, %384 # Prepare file as RW for owner only (600 in octal) + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 483D 00000000 ; cmp_rax, %0 # Check for missing output + 7F !_start_out ; jg8 !_start_out # Have real input + 48C7C0 01000000 ; mov_rax, %1 # Use stdout + +:_start_out + 4989C6 ; mov_r14,rax # Preserve the file pointer we were given + + 48C7C0 0C000000 ; mov_rax, %12 # the Syscall # for SYS_BRK + 48C7C7 00000000 ; mov_rdi, %0 # Get current brk + 0F05 ; syscall # Let the kernel do the work + 4989C4 ; mov_r12,rax # Set our malloc pointer + + 4D31ED ; xor_r13,r13 # Set HEAD = NULL + E8 %Tokenize_Line ; call %Tokenize_Line # Get all lines + 4C89E8 ; mov_rax,r13 # prepare for Reverse_List + E8 %Reverse_List ; call %Reverse_List # Correct order + 4989C5 ; mov_r13,rax # Update HEAD + E8 %Identify_Macros ; call %Identify_Macros # Find the DEFINEs + E8 %Line_Macro ; call %Line_Macro # Apply the DEFINEs + E8 %Process_String ; call %Process_String # Handle strings + E8 %Eval_Immediates ; call %Eval_Immediates # Handle Numbers + E8 %Preserve_Other ; call %Preserve_Other # Collect the remaining + E8 %Print_Hex ; call %Print_Hex # Output our results + +:Done + # program completed Successfully + 48C7C7 00000000 ; mov_rdi, %0 # All is well + 48C7C0 3C000000 ; mov_rax, %0x3c # put the exit syscall number in eax + 0F05 ; syscall # Call it a good day + + +# Tokenize_Line Function +# Using input file R15 and Head R13 +# Creates a linked list of structs +# Uses RBX for in_set strings, RCX for Int C and RDX for Struct Token* p +:Tokenize_Line + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + +:restart + E8 %fgetc ; call %fgetc # Read a char + 483D FCFFFFFF ; cmp_rax, %-4 # Check for EOF + 74 !done ; je8 !done # File is collected + + 480FB6C0 ; movzx_rax,al # We have to zero extend it to use it + 4889C1 ; mov_rcx,rax # Protect C + + 488D1D %comments ; lea_rbx,[rip+DWORD] %comments # Get pointer to "#;" + E8 %In_Set ; call %In_Set # Check for comments + 483D 01000000 ; cmp_rax, %1 # If comments + 0F84 %Purge_LineComment ; je %Purge_LineComment # try again + + 4889C8 ; mov_rax,rcx # put C in place for check + 488D1D %terminators ; lea_rbx,[rip+DWORD] %terminators # Get pointer to "\n\t " + E8 %In_Set ; call %In_Set # Check for terminators + 483D 01000000 ; cmp_rax, %1 # If terminator + 74 !restart ; je8 !restart # try again + + 48C7C0 20000000 ; mov_rax, %32 # Malloc the struct P + E8 %malloc ; call %malloc # Get pointer to P + 4889C2 ; mov_rdx,rax # Protect P + 4C892A ; mov_[rdx],r13 # P->NEXT = HEAD + 4989D5 ; mov_r13,rdx # HEAD = P + + 4889C8 ; mov_rax,rcx # put C in place for check + 488D1D %string_char ; lea_rbx,[rip+DWORD] %string_char # Get pointer to "\"'" + E8 %In_Set ; call %In_Set # Check for string chars + 483D 01000000 ; cmp_rax, %1 # If string char + 0F84 %Store_String ; je %Store_String # Get string + + E8 %Store_Atom ; call %Store_Atom # Get whole token + EB !restart ; jmp8 !restart + +:done + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# fgetc function +# Receives FILE* in R15 +# Returns -4 (EOF) or char in RAX +:fgetc + 48C7C0 FCFFFFFF ; mov_rax, %-4 # Put EOF in rax + 50 ; push_rax # Assume bad (If nothing read, value will remain EOF)ill remain EOF) + 488D3424 ; lea_rsi,[rsp] # Get stack address + 4C89FF ; mov_rdi,r15 # Where are we reading from + 48C7C0 00000000 ; mov_rax, %0 # the syscall number for read + 52 ; push_rdx # Protect RDX + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + 4153 ; push_r11 # Protect r11 + 0F05 ; syscall # call the Kernel + 415B ; pop_r11 # Restore r11 + 5A ; pop_rdx # Restore RDX + 58 ; pop_rax # Get either char or EOF + C3 ; ret + + +# Malloc isn't actually required if the program being built fits in the initial memory +# However, it doesn't take much to add it. +# Requires R12 to be initialized and RAX to have the number of desired bytes +:malloc + 4C89E7 ; mov_rdi,r12 # Using the current pointer + 4801C7 ; add_rdi,rax # Request the number of desired bytes + 48C7C0 0C000000 ; mov_rax, %12 # the Syscall # for SYS_BRK + 51 ; push_rcx # Protect rcx + 4153 ; push_r11 # Protect r11 + 0F05 ; syscall # call the Kernel + 415B ; pop_r11 # Restore r11 + 59 ; pop_rcx # Restore rcx + 4C89E0 ; mov_rax,r12 # Return pointer + 4989FC ; mov_r12,rdi # Update pointer + C3 ; ret + + +# Purge_LineComment function +# Reads chars until LF and jumps to restart +:Purge_LineComment + E8 %fgetc ; call %fgetc # Get a char + 480FB6C0 ; movzx_rax,al # Zero extend + 4883F8 0A ; cmp_rax, %10 # While not LF + 75 !Purge_LineComment ; jne8 !Purge_LineComment # Keep reading + E9 %restart ; jmp %restart + + +# Store_String Function +# Receives C in RCX, HEAD in RDX and Input file in R14 +# Uses RBX for terminator, RCX for C and RDX for string +:Store_String + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + + 48C7C0 02000000 ; mov_rax, %2 # Using TYPE STRING + 488942 08 ; mov_[rdx+BYTE],rax !8 # HEAD->TYPE = STRINGE = STRING + 48C7C0 00010000 ; mov_rax, %256 # Malloc the string + E8 %malloc ; call %malloc # Get pointer to P + 488942 10 ; mov_[rdx+BYTE],rax !16 # HEAD->TEXT = STRINGXT = STRING + 4889CB ; mov_rbx,rcx # Protect terminator + 4889C2 ; mov_rdx,rax # Protect string pointer +:Store_String_Loop + 880A ; mov_[rdx],cl # write byte + E8 %fgetc ; call %fgetc # read next char + 480FB6C0 ; movzx_rax,al # Zero extend it + 4889C1 ; mov_rcx,rax # Update C + 4883C2 01 ; add_rdx, %1 # STRING = STRING + 1 + 4839D9 ; cmp_rcx,rbx # See if we hit terminator + 75 !Store_String_Loop ; jne8 !Store_String_Loop # Otherwise keep looping + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 4889D0 ; mov_rax,rdx # return HEAD + E9 %restart ; jmp %restart + + +# Store_Atom Function +# Receives C in RCX, HEAD in RDX and Input file in R15 +# Uses RBX for in_set strings, RCX for C and RDX for string +:Store_Atom + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + + 48C7C0 00010000 ; mov_rax, %256 # Malloc the string + E8 %malloc ; call %malloc # Get pointer to P + 488942 10 ; mov_[rdx+BYTE],rax !16 # HEAD->TEXT = STRING + 488D1D %terminators ; lea_rbx,[rip+DWORD] %terminators # Get pointer to "\n\t " + 4889C2 ; mov_rdx,rax # Protect string pointer +:Store_Atom_loop + 880A ; mov_[rdx],cl # write byte + E8 %fgetc ; call %fgetc # read next char + 480FB6C0 ; movzx_rax,al # Zero extend it + 4889C1 ; mov_rcx,rax # Update C + 4883C2 01 ; add_rdx, %1 # STRING = STRING + 1 + E8 %In_Set ; call %In_Set # Check for terminators + 4883F8 00 ; cmp_rax, %0 # Check for "\n\t " + 74 !Store_Atom_loop ; je8 !Store_Atom_loop # Loop otherwise + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 4889D0 ; mov_rax,rdx # return HEAD + C3 ; ret + + +# In_Set function +# Receives Char C in RAX and CHAR* in RBX +# Returns 1 if true, zero if false in RAX +:In_Set + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX +:In_Set_loop + 8A0B ; mov_cl,[rbx] # Read char + 480FB6C9 ; movzx_rcx,cl # Zero extend it + + 4839C8 ; cmp_rax,rcx # See if they match + 74 !In_Set_True ; je8 !In_Set_True # return true + + 4881F9 00000000 ; cmp_rcx, %0 # Check for NULL + 74 !In_Set_False ; je8 !In_Set_False # return false + + 4881C3 01000000 ; add_rbx, %1 # s = s + 1 + EB !In_Set_loop ; jmp8 !In_Set_loop # Keep looping + +:In_Set_True + 48C7C0 01000000 ; mov_rax, %1 # Set True + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + +:In_Set_False + 48C7C0 00000000 ; mov_rax, %0 # Set FALSE + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + +# Char sets +:terminators + 0A 09 20 00 # "\n\t \0" + +:comments + 23 3B 00 # "#;\0" + +:string_char + 22 27 00 # "\"'\0" + + +# Reverse_List function +# Receives List in RAX +# Returns the list reversed in RAX +:Reverse_List + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 4889C3 ; mov_rbx,rax # Set HEAD + 48C7C0 00000000 ; mov_rax, %0 # ROOT = NULL +:Reverse_List_Loop + 4883FB 00 ; cmp_rbx, %0 # WHILE HEAD != NULL + 74 !Reverse_List_Done ; je8 !Reverse_List_Done # Stop otherwise + + 488B0B ; mov_rcx,[rbx] # NEXT = HEAD->NEXT + 488903 ; mov_[rbx],rax # HEAD->NEXT = ROOT + 4889D8 ; mov_rax,rbx # ROOT = HEAD + 4889CB ; mov_rbx,rcx # HEAD = NEXT + EB !Reverse_List_Loop ; jmp8 !Reverse_List_Loop # Keep Going + +:Reverse_List_Done + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# Identify_Macros function +# Receives List in RAX +# Updates the list in place; does not modify registers +# Uses RBX for DEFINE, RCX for I +:Identify_Macros + 50 ; push_rax # Protect RAX + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 488D1D %DEFINE_str ; lea_rbx,[rip+DWORD] %DEFINE_str # Setup define string + 4889C1 ; mov_rcx,rax # I = HEAD +:Identify_Macros_Loop + 488B41 10 ; mov_rax,[rcx+BYTE] !16 # I->TEXT + E8 %match ; call %match # IF "DEFINE" == I->TEXT + 4883F8 00 ; cmp_rax, %0 # Check if match + 75 !Identify_Macros_Next ; jne8 !Identify_Macros_Next # Skip the work + + # Deal with MACRO + 48C7C0 01000000 ; mov_rax, %1 # Using MACRO + 488941 08 ; mov_[rcx+BYTE],rax !8 # I->TYPE = MACRO + + 488B01 ; mov_rax,[rcx] # I->NEXT + 488B40 10 ; mov_rax,[rax+BYTE] !16 # I->NEXT->TEXT + 488941 10 ; mov_[rcx+BYTE],rax !16 # I->TEXT = I->NEXT->TEXT + + 488B01 ; mov_rax,[rcx] # I->NEXT + 488B00 ; mov_rax,[rax] # I->NEXT->NEXT + 488B40 10 ; mov_rax,[rax+BYTE] !16 # I->NEXT->NEXT->TEXT + 488941 18 ; mov_[rcx+BYTE],rax !24 # I->EXPRESSION = I->NEXT->NEXT->TEXT + + 488B01 ; mov_rax,[rcx] # I->NEXT + 488B00 ; mov_rax,[rax] # I->NEXT->NEXT + 488B00 ; mov_rax,[rax] # I->NEXT->NEXT->NEXT + 488901 ; mov_[rcx],rax # I->NEXT = I->NEXT->NEXT->NEXT + +:Identify_Macros_Next + 488B09 ; mov_rcx,[rcx] # I = I->NEXT + 4883F9 00 ; cmp_rcx, %0 # Check for NULL + 75 !Identify_Macros_Loop ; jne8 !Identify_Macros_Loop # Keep looping otherwise + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 58 ; pop_rax # Restore RAX + C3 ; ret + +:DEFINE_str + 44 45 46 49 4E 45 00 # "DEFINE" + + +# match function +# Receives CHAR* in RAX and CHAR* in RBX +# Returns 0 (TRUE) or 1 (FALSE) in RAX +:match + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 4889C1 ; mov_rcx,rax # S1 in place + 4889DA ; mov_rdx,rbx # S2 in place +:match_Loop + 8A01 ; mov_al,[rcx] # S1[0] + 480FB6C0 ; movzx_rax,al # Make it useful + 8A1A ; mov_bl,[rdx] # S2[0] + 480FB6DB ; movzx_rbx,bl # Make it useful + 4839D8 ; cmp_rax,rbx # See if they match + 75 !match_False ; jne8 !match_False # If not + + 4883C1 01 ; add_rcx, %1 # S1 = S1 + 1 + 4883C2 01 ; add_rdx, %1 # S2 = S2 + 1 + 4883F8 00 ; cmp_rax, %0 # If reached end of string + 74 !match_Done ; je8 !match_Done # Perfect match + EB !match_Loop ; jmp8 !match_Loop # Otherwise keep looping + +:match_False + 48C7C0 01000000 ; mov_rax, %1 # Return false +:match_Done + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# Line_Macro function +# Receives List in RAX +# Updates the list in place; does not modify registers +# Uses RAX for I, RBX for I->TEXT, RCX for I->EXPRESSION +:Line_Macro + 50 ; push_rax # Protect RAX + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX +:Line_Macro_Loop + 488B58 08 ; mov_rbx,[rax+BYTE] !8 # I->TYPE + 4883FB 01 ; cmp_rbx, %1 # IF MACRO == I->TYPE + 75 !Line_Macro_Next ; jne8 !Line_Macro_Next # Otherwise move on + + # Is a macro apply + 488B58 10 ; mov_rbx,[rax+BYTE] !16 # I->TEXT + 488B48 18 ; mov_rcx,[rax+BYTE] !24 # I->EXPRESSION + 488B00 ; mov_rax,[rax] # I->NEXT + E8 %Set_Expression ; call %Set_Expression # Apply it + EB !Line_Macro_Loop ; jmp8 !Line_Macro_Loop # Move on to next + +:Line_Macro_Next + 488B00 ; mov_rax,[rax] # I->NEXT + 4883F8 00 ; cmp_rax, %0 # Check for NULL + 75 !Line_Macro_Loop ; jne8 !Line_Macro_Loop # Keep going + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 58 ; pop_rax # Restore RAX + C3 ; ret + + +# Set_Expression function +# Receives List in RAX, CHAR* in RBX and CHAR* in RCX +# Updates the list in place; does not modify registers +# Uses RBX for C, RCX for EXP and RDX for I +:Set_Expression + 50 ; push_rax # Protect RAX + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 4889C2 ; mov_rdx,rax # Set I +:Set_Expression_Loop + 488B42 08 ; mov_rax,[rdx+BYTE] !8 # I->TYPE + 4883F8 01 ; cmp_rax, %1 # IF MACRO == I->TYPE + 74 !Set_Expression_Next ; je8 !Set_Expression_Next # Ignore and move on + + 488B42 10 ; mov_rax,[rdx+BYTE] !16 # I->TEXT + E8 %match ; call %match # Check for match + 4883F8 00 ; cmp_rax, %0 # If match + 75 !Set_Expression_Next ; jne8 !Set_Expression_Next # Otherwise next + + # We have a non-macro match + 48894A 18 ; mov_[rdx+BYTE],rcx !24 # I->EXPRESSION = EXP + +:Set_Expression_Next + 488B12 ; mov_rdx,[rdx] # I = I->NEXT + 4883FA 00 ; cmp_rdx, %0 # IF NULL == I + 75 !Set_Expression_Loop ; jne8 !Set_Expression_Loop # Otherwise keep looping + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 58 ; pop_rax # Restore RAX + C3 ; ret + + +# Process_String function +# Receives List in RAX +# Update the list in place; does not modify registers +# Uses RBX for I->TEXT, RCX for I and RDX for S +:Process_String + 50 ; push_rax # Protect RAX + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 4889C1 ; mov_rcx,rax # I = HEAD +:Process_String_loop + 488B41 08 ; mov_rax,[rcx+BYTE] !8 # I->TYPE + 4883F8 02 ; cmp_rax, %2 # IF STRING == I->TYPE + 75 !Process_String_Next ; jne8 !Process_String_Next # Skip to next + + 488B59 10 ; mov_rbx,[rcx+BYTE] !16 # I->TEXT + 8A03 ; mov_al,[rbx] # I->TEXT[0] + 480FB6C0 ; movzx_rax,al # make it useful + 4883F8 27 ; cmp_rax, %39 # IF '\'' == I->TEXT[0] + 75 !Process_String_Raw ; jne8 !Process_String_Raw # Deal with '"' + + # Deal with '\'' + 4883C3 01 ; add_rbx, %1 # I->TEXT + 1 + 488959 18 ; mov_[rcx+BYTE],rbx !24 # I->EXPRESSION = I->TEXT + 1 + EB !Process_String_Next ; jmp8 !Process_String_Next # Move on to next + +:Process_String_Raw + 4889D8 ; mov_rax,rbx # Get length of I->TEXT + E8 %string_length ; call %string_length # Do it + 48C1E8 02 ; shr_rax, !2 # LENGTH = LENGTH >> 2 + 4883C0 01 ; add_rax, %1 # LENGTH = LENGTH + 1 + 48C1E0 03 ; shl_rax, !3 # LENGTH = LENGTH << 3 + E8 %malloc ; call %malloc # Get string + 4889DA ; mov_rdx,rbx # S = I->TEXT + 4883C2 01 ; add_rdx, %1 # S = S + 1 + 488941 18 ; mov_[rcx+BYTE],rax !24 # I->EXPRESSION = hexify + 4889C3 ; mov_rbx,rax # Put hexify buffer in rbx + +:Process_String_Raw_Loop + 8A02 ; mov_al,[rdx] # Read 1 chars + 480FB6C0 ; movzx_rax,al # Make it useful + 4883C2 01 ; add_rdx, %1 # S = S + 1 + 3C 00 ; cmp_al, !0 # Check for NULL + 9C ; pushf # Protect condition + E8 %hex8 ; call %hex8 # write them all + 9D ; popf # restore condition + 75 !Process_String_Raw_Loop ; jne8 !Process_String_Raw_Loop # Keep looping + +:Process_String_Next + 488B09 ; mov_rcx,[rcx] # I = I->NEXT + 4883F9 00 ; cmp_rcx, %0 # IF NULL == I + 75 !Process_String_loop ; jne8 !Process_String_loop # Otherwise keep looping + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 58 ; pop_rax # Restore RAX + C3 ; ret + + +# string_length function +# Receives CHAR* in RAX +# Returns INT in RAX +# Uses RAX for CH, RBX for S and RCX for INDEX +:string_length + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 4889C3 ; mov_rbx,rax # Set S + B9 00000000 ; mov_rcx, %0 # INDEX = 0 +:string_length_loop + 8A040B ; mov_al,[rbx+rcx] # S[0] + 480FB6C0 ; movzx_rax,al # make it useful + 4883F8 00 ; cmp_rax, %0 # IF NULL == S[0] + 74 !string_length_done ; je8 !string_length_done # Stop + + 4883C1 01 ; add_rcx, %1 # INDEX = INDEX + 1 + EB !string_length_loop ; jmp8 !string_length_loop # Keep going + +:string_length_done + 4889C8 ; mov_rax,rcx # RETURN INDEX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# Eval_Immediates function +# Receives List in RAX +# Updates the list in place; does not modify registers +# Uses RBX for I->TEXT[0], RCX for I->TEXT[1] and RDX for I +:Eval_Immediates + 50 ; push_rax # Protect RAX + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 4889C2 ; mov_rdx,rax # I = HEAD +:Eval_Immediates_Loop + # Check for MACRO + 488B42 08 ; mov_rax,[rdx+BYTE] !8 # I->TYPE + 4883F8 01 ; cmp_rax, %1 # IF MACRO == I->TYPE + 74 !Eval_Immediates_Next ; je8 !Eval_Immediates_Next # Skip to next + + # Check for NULL EXPRESSION + 488B42 18 ; mov_rax,[rdx+BYTE] !24 # I->EXPRESSION + 4883F8 00 ; cmp_rax, %0 # IF NULL == I->EXPRESSION + 75 !Eval_Immediates_Next ; jne8 !Eval_Immediates_Next # Skip to next + + # Check if number + 488B42 10 ; mov_rax,[rdx+BYTE] !16 # I->TEXT + 8A18 ; mov_bl,[rax] # I->TEXT[0] + 480FB6DB ; movzx_rbx,bl # Extend to use + 4883C0 01 ; add_rax, %1 # I->TEXT + 1 + 8A08 ; mov_cl,[rax] # I->TEXT[1] + 480FB6C9 ; movzx_rcx,cl # Extend to use + E8 %numerate_string ; call %numerate_string # Convert string to INT + 4883F8 00 ; cmp_rax, %0 # IF 0 == numerate_string(I->TEXT + 1) + 75 !Eval_Immediates_value ; jne8 !Eval_Immediates_value # Has a value + + # Last chance for Immediate + 4883F9 30 ; cmp_rcx, %48 # If '0' == I->TEXT[1] + 75 !Eval_Immediates_Next ; jne8 !Eval_Immediates_Next # Skip to next + +:Eval_Immediates_value + E8 %express_number ; call %express_number # Convert value to hex string + 488942 18 ; mov_[rdx+BYTE],rax !24 # I->EXPRESSION = express_number(value, I-TEXT[0]) + +:Eval_Immediates_Next + 488B12 ; mov_rdx,[rdx] # I = I->NEXT + 4883FA 00 ; cmp_rdx, %0 # IF NULL == I + 75 !Eval_Immediates_Loop ; jne8 !Eval_Immediates_Loop # Otherwise keep looping + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 58 ; pop_rax # Restore RAX + C3 ; ret + + +# numerate_string function +# Receives CHAR* in RAX +# Returns value of CHAR* in RAX +# Only supports negative decimals and Uppercase Hex (eg 5, -3 and 0xCC) +# Uses RAX for VALUE, RBX for S, RCX for CH and RSI for NEGATIVE? +:numerate_string + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 56 ; push_rsi # Protect RSI + 4889C3 ; mov_rbx,rax # put S in correct place + 48C7C0 00000000 ; mov_rax, %0 # Initialize to Zero +:numerate_string_loop + 8A4B 01 ; mov_cl,[rbx+BYTE] !1 # S[1] + 480FB6C9 ; movzx_rcx,cl # make it useful + 4883F9 78 ; cmp_rcx, %120 # IF 'x' == S[1] + 74 !numerate_hex ; je8 !numerate_hex # Deal with hex input + + # Assume decimal input + 48C7C6 00000000 ; mov_rsi, %0 # Assume no negation + 8A0B ; mov_cl,[rbx] # S[0] + 480FB6C9 ; movzx_rcx,cl # make it useful + 4883F9 2D ; cmp_rcx, %45 # IF '-' == S[0] + 75 !numerate_decimal ; jne8 !numerate_decimal # Skip negation + + 48C7C6 01000000 ; mov_rsi, %1 # Set FLAG + 4883C3 01 ; add_rbx, %1 # S = S + 1 + +:numerate_decimal + 8A0B ; mov_cl,[rbx] # S[0] + 480FB6C9 ; movzx_rcx,cl # make it useful + 4883F9 00 ; cmp_rcx, %0 # IF NULL == S[0] + 74 !numerate_decimal_done ; je8 !numerate_decimal_done # We are done + + 486BC0 0A ; imul_rax, !10 # VALUE = VALUE * 10 + 4883E9 30 ; sub_rcx, !48 # CH = CH - '0' + 4883F9 09 ; cmp_rcx, %9 # Check for illegal + 7F !numerate_string_fail ; jg8 !numerate_string_fail # If CH > '9' + 4883F9 00 ; cmp_rcx, %0 # Check for illegal + 7C !numerate_string_fail ; jl8 !numerate_string_fail # IF CH < 0 + 4801C8 ; add_rax,rcx # VALUE = VALUE + CH + 4883C3 01 ; add_rbx, %1 # S = S + 1 + EB !numerate_decimal ; jmp8 !numerate_decimal # Keep looping + +:numerate_decimal_done + 4883FE 01 ; cmp_rsi, %1 # Check if need to negate + 75 !numerate_string_done ; jne8 !numerate_string_done # Nope + + 486BC0 FF ; imul_rax, !-1 # VALUE = VALUE * -1 + EB !numerate_string_done ; jmp8 !numerate_string_done # Done + +:numerate_hex + 4883C3 02 ; add_rbx, %2 # S = S + 2 +:numerate_hex_loop + 8A0B ; mov_cl,[rbx] # S[0] + 480FB6C9 ; movzx_rcx,cl # make it useful + 4883F9 00 ; cmp_rcx, %0 # IF NULL == S[0] + 0F84 %numerate_string_done ; je8 !numerate_string_done # We are done + + 48C1E0 04 ; shl_rax, !4 # VALUE = VALUE << 4 + 4883E9 30 ; sub_rcx, !48 # CH = CH - '0' + 4883F9 0A ; cmp_rcx, %10 # IF 10 >= CH + 7C !numerate_hex_digit ; jl8 !numerate_hex_digit # NO + 4883E9 07 ; sub_rcx, !7 # Push A-F into range +:numerate_hex_digit + 4883F9 0F ; cmp_rcx, %15 # Check for illegal + 7F !numerate_string_fail ; jg8 !numerate_string_fail # If CH > 'F' + 4883F9 00 ; cmp_rcx, %0 # Check for illegal + 7C !numerate_string_fail ; jl8 !numerate_string_fail # IF CH < 0 + 4801C8 ; add_rax,rcx # VALUE = VALUE + CH + 4883C3 01 ; add_rbx, %1 # S = S + 1 + EB !numerate_hex_loop ; jmp8 !numerate_hex_loop # Keep looping + +:numerate_string_fail + 48C7C0 00000000 ; mov_rax, %0 # return ZERO + +:numerate_string_done + 5E ; pop_rsi # Restore RSI + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# express_number function +# Receives INT in RAX and CHAR in RBX +# Allocates a string and expresses the value in hex +# Returns string in RAX +# Uses RAX for VALUE, RBX for S and RCX for CH +:express_number + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX + 4889D9 ; mov_rcx,rbx # Put CH in right place + 4889C3 ; mov_rbx,rax # Protect VALUE + 4883F9 25 ; cmp_rcx, %37 # IF '%' == CH + 75 !express_number2 ; jne8 !express_number2 # Otherwise try @ + + 48C7C0 09000000 ; mov_rax, %9 # We need 9 bytes + E8 %malloc ; call %malloc # Get S pointer + 4893 ; xchg_rax,rbx # Put S and VALUE in place + 53 ; push_rbx # Protect S + E8 %hex32l ; call %hex32l # Store 32bits + EB !express_number_done ; jmp8 !express_number_done # done + +:express_number2 + 4883F9 40 ; cmp_rcx, %64 # IF '@' == CH + 75 !express_number1 ; jne8 !express_number1 # Othrewise try ! + + 48C7C0 05000000 ; mov_rax, %5 # We need 5 bytes + E8 %malloc ; call %malloc # Get S pointer + 4893 ; xchg_rax,rbx # Put S and VALUE in place + 53 ; push_rbx # Protect S + E8 %hex16l ; call %hex16l # Store 16bits + EB !express_number_done ; jmp8 !express_number_done # done + +:express_number1 + 48C7C0 03000000 ; mov_rax, %3 # We need 3 bytes + E8 %malloc ; call %malloc # Get S pointer + 4893 ; xchg_rax,rbx # Put S and VALUE in place + 53 ; push_rbx # Protect S + E8 %hex8 ; call %hex8 # Store 8bit + +:express_number_done + 58 ; pop_rax # Restore S + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# HEX to ascii routine +# Receives INT in RAX and CHAR* in RBX +# Stores ascii of INT in CHAR* +# Returns only modifying RAX +:hex64l + 50 ; push_rax # Protect top 32 + E8 %hex32l ; call %hex32l # Store it + 58 ; pop_rax # do top 32 + 48C1E8 20 ; shr_rax, !32 # do bottom 32 first +:hex32l + 50 ; push_rax # Protect top 16 + E8 %hex16l ; call %hex16l # Store it + 58 ; pop_rax # do top 16 + 48C1E8 10 ; shr_rax, !16 # do bottom 16 first +:hex16l + 50 ; push_rax # Protect top byte + E8 %hex8 ; call %hex8 # Store it + 58 ; pop_rax # do high byte + 48C1E8 08 ; shr_rax, !8 # do bottom byte first +:hex8 + 50 ; push_rax # Protect bottom nibble + 48C1E8 04 ; shr_rax, !4 # do high nibble first + E8 %hex4 ; call %hex4 # Store it + 58 ; pop_rax # do low nibble +:hex4 + 4883E0 0F ; and_rax, !0xF # isolate nibble + 04 30 ; add_al, !0x30 # convert to ascii (add '0') + 3C 39 ; cmp_al, !0x39 # valid digit? (compare to '9') + 76 !hex1 ; jbe8 !hex1 # yes + 04 07 ; add_al, !7 # use alpha range +:hex1 + 8803 ; mov_[ebx],al # store result + 4883C3 01 ; add_ebx, !1 # next position + C3 ; ret + + +# Preserve_Other function +# Receives List in RAX +# Updates the list in place; does not modify registers +# Uses RAX for I, RBX for I->TEXT +:Preserve_Other + 50 ; push_rax # Protect RAX + 53 ; push_rbx # Protect RBX + 51 ; push_rcx # Protect RCX + 52 ; push_rdx # Protect RDX +:Preserve_Other_Loop + 488B58 18 ; mov_rbx,[rax+BYTE] !24 # I->EXPRESSION + 4883FB 00 ; cmp_rbx, %0 # IF NULL == I->EXPRESSION + 75 !Preserve_Other_Next ; jne8 !Preserve_Other_Next # Otherwise next + + # Needs preserving + 488B58 10 ; mov_rbx,[rax+BYTE] !16 # I->TEXT + 488958 18 ; mov_[rax+BYTE],rbx !24 # I->EXPRESSION = I->TEXT + +:Preserve_Other_Next + 488B00 ; mov_rax,[rax] # I = I->NEXT + 4883F8 00 ; cmp_rax, %0 # IF NULL == I + 75 !Preserve_Other_Loop ; jne8 !Preserve_Other_Loop # Otherwise keep looping + + 5A ; pop_rdx # Restore RDX + 59 ; pop_rcx # Restore RCX + 5B ; pop_rbx # Restore RBX + 58 ; pop_rax # Restore RAX + C3 ; ret + + +# Print_Hex function +# Receives list in RAX +# walks the list and prints the I->EXPRESSION for all nodes followed by newline +# Uses RBX for I +:Print_Hex + 53 ; push_rbx # Protect RBX + 4C89EB ; mov_rbx,r13 # I = Head +:Print_Hex_Loop + 488B43 08 ; mov_rax,[rbx+BYTE] !8 # I->TYPE + 4883F8 01 ; cmp_rax, %1 # IF MACRO == I->TYPE + 74 !Print_Hex_Next ; je8 !Print_Hex_Next # Skip + + 488B43 18 ; mov_rax,[rbx+BYTE] !24 # Using EXPRESSION + E8 %File_Print ; call %File_Print # Print it + 48C7C0 0A000000 ; mov_rax, %10 # NEWLINE + E8 %fputc ; call %fputc # Append it + +:Print_Hex_Next + 488B1B ; mov_rbx,[rbx] # Iterate to next Token + 4883FB 00 ; cmp_rbx, %0 # Check for NULL + 75 !Print_Hex_Loop ; jne8 !Print_Hex_Loop # Otherwise keep looping + + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# File_Print function +# Receives CHAR* in RAX +# calls fputc for every non-null char +:File_Print + 53 ; push_rbx # Protect RBX + 4889C3 ; mov_rbx,rax # Protect S + 4883F8 00 ; cmp_rax, %0 # Protect against nulls + 74 !File_Print_Done ; je8 !File_Print_Done # Simply don't try to print them +:File_Print_Loop + 8A03 ; mov_al,[rbx] # Read byte + 480FB6C0 ; movzx_rax,al # zero extend + 4883F8 00 ; cmp_rax, %0 # Check for NULL + 74 !File_Print_Done ; je8 !File_Print_Done # Stop at NULL + + E8 %fputc ; call %fputc # write it + 4883C3 01 ; add_rbx, %1 # S = S + 1 + EB !File_Print_Loop ; jmp8 !File_Print_Loop # Keep going + +:File_Print_Done + 5B ; pop_rbx # Restore RBX + C3 ; ret + + +# fputc function +# receives CHAR in RAX and FILE* in R14 +# writes char and returns +:fputc + 50 ; push_rax # We are writing rax + 488D3424 ; lea_rsi,[rsp] # Get stack address + 4C89F7 ; mov_rdi,r14 # Write to target file + 48C7C0 01000000 ; mov_rax, %1 # the syscall number for write + 52 ; push_rdx # Protect RDX + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + 4153 ; push_r11 # Protect HEAD + 0F05 ; syscall # call the Kernel + 415B ; pop_r11 # Restore HEAD + 5A ; pop_rdx # Restore RDX + 58 ; pop_rax # Restore stack + C3 ; ret + +:ELF_end diff --git a/vendor/seed/amd64/catm.hex2 b/vendor/seed/amd64/catm.hex2 @@ -0,0 +1,108 @@ +# SPDX-FileCopyrightText: 2019 Jeremiah Orians <jeremiah@pdp10.guru> +# +# SPDX-License-Identifier: GPL-3.0-or-later + +## ELF Header + +:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +3E 00 ## e_machine Indicating AMD64 +01 00 00 00 ## e_version Indicating original elf + +&_start 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +%ELF_program_headers>ELF_base 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +&ELF_base 00 00 00 00 ## p_vaddr +&ELF_base 00 00 00 00 ## p_physaddr + +%ELF_end>ELF_base 00 00 00 00 ## p_filesz +%ELF_end>ELF_base 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +:ELF_text + +:_start + 58 ; pop_rax # Get the number of arguments + 5F ; pop_rdi # Get the program name + 5F ; pop_rdi # Get the actual output name + 48C7C6 41020000 ; mov_rsi, %577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 48C7C2 80010000 ; mov_rdx, %384 # Prepare file as RW for owner only (600 in octal) + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that file + 4989C7 ; mov_r15,rax # Preserve the file pointer we were given + + 48C7C0 0C000000 ; mov_rax, %12 # the Syscall # for SYS_BRK + 48C7C7 00000000 ; mov_rdi, %0 # Get current brk + 0F05 ; syscall # Let the kernel do the work + 4989C6 ; mov_r14,rax # Set our malloc pointer + + 48C7C0 0C000000 ; mov_rax, %12 # the Syscall # for SYS_BRK + 4C89F7 ; mov_r14,rax # Using current pointer + 4881C7 00001000 ; add_rdi, %0x100000 # Allocate 1MB + 0F05 ; syscall # Let the kernel do the work + +:core + 5F ; pop_rdi # Get the actual input name + 4883FF 00 ; cmp_rdi, !0 # Check for null string + 0F84 %done ; je %done # Hit null be done + + 48C7C6 00000000 ; mov_rsi, %0 # prepare read_only + 48C7C2 00000000 ; mov_rdx, %0 # prevent any interactions + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 4989C5 ; mov_r13,rax # Protect INPUT +:keep + 48C7C2 00001000 ; mov_rdx, %0x100000 # set the size of chars we want + 4C89F6 ; mov_rsi,r14 # Where to put it + 4C89EF ; mov_rdi,r13 # Where are we reading from + 48C7C0 00000000 ; mov_rax, %0 # the syscall number for read + 0F05 ; syscall # call the Kernel + 50 ; push_rax # Protect the number of bytes read + + 4889C2 ; mov_rdx,rax # Number of bytes to write + 4C89F6 ; mov_rsi,r14 # What we are writing + 4C89FF ; mov_rdi,r15 # Write to target file + 48C7C0 01000000 ; mov_rax, %1 # the syscall number for write + 0F05 ; syscall # call the Kernel + + 58 ; pop_rax # Get bytes read + 483D 00001000 ; cmp_rax, %0x100000 # Check if buffer was fully used + 0F84 %keep ; je %keep # Keep looping if was full + E9 %core ; jmp %core # Otherwise move to next file + +:done + # program completed Successfully + 48C7C7 00000000 ; mov_rdi, %0 # All is well + 48C7C0 3C000000 ; mov_rax, %0x3C # put the exit syscall number in eax + 0F05 ; syscall # Call it a good day + +:ELF_end diff --git a/vendor/seed/amd64/hex0-seed b/vendor/seed/amd64/hex0-seed Binary files differ. diff --git a/vendor/seed/amd64/hex0.hex0 b/vendor/seed/amd64/hex0.hex0 @@ -0,0 +1,152 @@ +# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru> +# SPDX-FileCopyrightText: 2023 Andrius Štikonas <andrius@stikonas.eu> +# +# SPDX-License-Identifier: GPL-3.0-or-later + +## ELF Header +#:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +3E 00 ## e_machine Indicating AMD64 +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 ## p_type +01 00 00 00 ## p_flags: PF-X = 1 +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +E5 00 00 00 00 00 00 00 ## p_filesz +E5 00 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +#:ELF_text + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function +#:_start (0x600078) + 58 ; pop_rax # Get the number of arguments + 5F ; pop_rdi # Get the program name + 5F ; pop_rdi # Get the actual input name + 31F6 ; xor_esi,esi # prepare read_only, rsi = 0 + 6A 02 ; push !2 # prepare syscall number + 58 ; pop_rax # the syscall number for open() + 99 ; cdq # Extra sure, rdx = 0 + 0F05 ; syscall # Now open that damn file + 5F ; pop_rdi # Get the actual output name + 50 ; push_rax # Preserve the file pointer we were given + 66BE 4102 ; mov_si, @577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 66BA C001 ; mov_dx, @448 # Prepare file as RWX for owner only (700 in octal) + 6A 02 ; push !2 # prepare syscall number + 58 ; pop_rax # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 93 ; xchg_ebx,eax # Preserve the file pointer we were given + 99 ; cdq # rdx = 0 since file descriptor is nonnegative + FFC2 ; inc_edx # rdx = 1 (count for read/write) + +#:loop_reset_all (0x600096) + 31ED ; xor_ebp,ebp # ebp = 0 (no prior hex val) + +# Comment tracking is done with ecx. +# ecx is decremented if we hit a +# comment (';' or '#') and reset +# if we hit a new-line. +#:loop_reset_comment (0x600098) + 52 ; push_rdx + 59 ; pop_rcx # Set no current comment +#:loop_add_comment (0x60009A) + FFC9 ; dec_ecx +#:loop (0x60009C) + + # Read a byte + 5F ; pop_rdi # Get infile + 54 ; push_rsp + 5E ; pop_rsi # Set buffer + # rdx is already set to 1. + 31C0 ; xor_eax,eax # Set read syscall in rax + 51 ; push_rcx # Save comment tracking + 0F05 ; syscall # Do the actual read + 59 ; pop_rcx # Restore comment tracking + 57 ; push_rdi # Re-save infile + 85C0 ; test_eax,eax # Check what we got + 75 06 ; jne !cont # No EOF + + # Exit successfully + B0 3C ; mov_al, !60 # Set exit syscall in rax + 31FF ; xor_edi,edi # Set return success (rdi = 0) + 0F05 ; syscall # Exit + +#:cont (0x6000B0) + 8A06 ; mov_al,[rsi] # Move prog byte in eax + + # New line check + 3C 0A ; cmp_al, !10 # Check new-line + 74 E2 ; je !loop_reset_comment # If new-line, end comment handling + + # In comment check + 85C9 ; test_ecx,ecx # Skip byte if we are in a comment + 75 E2 ; jne !loop + + # Start comment check + 3C 23 ; cmp_al, !35 # Start of '#' comment + 74 DC ; je !loop_add_comment + + 3C 3B ; cmp_al, !59 # Start of ';' comment + 74 D8 ; je !loop_add_comment + + # Start of hex str to int + 2C 30 ; sub_al, !48 # Subtract ascii '0' from al + 2C 0A ; sub_al, !10 # Check for value in '0'-'9' + 72 08 ; jb !write # We have hex value, write it + + 2C 07 ; sub_al, !7 # Subtract ('A'-'0') from al + 24 DF ; and_al, !0xDF # Remove lower case bit + 3C 07 ; cmp_al, !7 # Check for value 'A'-'F' + 73 CC ; jae !loop # We have hex value, write it + +#:write (0x6000D0) + C1E5 04 ; shl_ebp, !4 # Shift up existing hex digit + 04 0A ; add_al, !10 # Finish converting ascii to raw value + 01C5 ; add_ebp,eax # Combine the hex digits + + # Check if this is first digit in hex val + F7DB ; neg_ebx # Flip sign of r10 to indicate we got a digit + 7C C1 ; jl !loop # Negative -> first digit, get another one + + # We have both digits in low byte of ebp, good to write + 892E ; mov_[rsi],ebp # Move edge to buffer + 89DF ; mov_edi,ebx # Move outfile to rdi + B0 01 ; mov_al, !1 # Set write syscall in rax + 0F05 ; syscall # Do the write + EB B1 ; jmp !loop_reset_all # Start a fresh byte + +#:ELF_end (0x6000E5) diff --git a/vendor/seed/amd64/hex1.hex0 b/vendor/seed/amd64/hex1.hex0 @@ -0,0 +1,315 @@ +# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru> +# SPDX-FileCopyrightText: 2023 Andrius Štikonas <andrius@stikonas.eu> +# +# SPDX-License-Identifier: GPL-3.0-or-later + +## ELF Header +#:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +3E 00 ## e_machine Indicating AMD64 +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +6E 02 00 00 00 00 00 00 ## p_filesz +6E 02 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +#:ELF_text + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function + +# :_start (0x600078) + 58 ; pop_rax # Get the number of arguments + 5F ; pop_rdi # Get the program name$ + 5F ; pop_rdi # Get the actual input name + 48C7C6 00000000 ; mov_rsi, %0 # prepare read_only + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 4989C1 ; mov_r9,rax # Preserve the file pointer we were given + + 5F ; pop_rdi # Get the actual output name + 48C7C6 41020000 ; mov_rsi, %577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 48C7C2 C0010000 ; mov_rdx, %448 # Prepare file as RWX for owner only (700 in octal) + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 4989C2 ; mov_r10,rax # Preserve the file pointer we were given + + 49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing + 49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum + 49C7C5 00000000 ; mov_r13, %0 # Our starting IP + E8 39000000 ; call %First_pass # Process it + + # rewind input file + 4C89CF ; mov_rdi,r9 # Using our input file + 48C7C6 00000000 ; mov_rsi, %0 # Offset Zero + 48C7C2 00000000 ; mov_rdx, %0 # Whence Zero + 48C7C0 08000000 ; mov_rax, %8 # lseek + 0F05 ; syscall + + 49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing + 49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum + 49C7C5 00000000 ; mov_r13, %0 # Our starting IP + E8 69000000 ; call %Second_pass # Process it + + E9 E4000000 ; jmp %Done + +# :First_pass (0x6000FC) + E8 EF000000 ; call %Read_byte + + # Deal with EOF + 3C FC ; cmp_al, !-4 + 74 34 ; je8 !First_pass_done + + # Check for : + 3C 3A ; cmp_al, !0x3a + 75 05 ; jne8 !First_pass_0 + + # Deal with label + E8 32010000 ; call %StoreLabel + +# :First_pass_0 (0x60010E) + # Check for % + 3C 25 ; cmp_al, !0x25 + 74 1C ; je8 !First_pass_pointer + + # Deal with everything else + E8 23000000 ; call %hex # Process our char + + # Deal with EOF + 3C FC ; cmp_al, !-4 + 74 1E ; je8 !First_pass_done + + # deal with -1 values + 3C 00 ; cmp_al, !0 + 7C DD ; jl8 !First_pass + + # deal with toggle + 4983FF 00 ; cmp_r15, !0 + 74 04 ; je8 !First_pass_1 + 4983C5 01 ; add_r13, !1 # Increment IP + +# :First_pass_1 (0x600129) + 49F7D7 ; not_r15 + EB CE ; jmp8 !First_pass + +# :First_pass_pointer (0x60012E) + # Deal with Pointer to label + E8 BD000000 ; call %Read_byte # Drop the char + 4983C5 04 ; add_r13, !4 # Increment IP + EB C3 ; jmp8 !First_pass # Loop again + +# :First_pass_done (0x600139) + C3 ; ret + +# :hex (0x60013A) + # deal with EOF + 3C FC ; cmp_al, !-4 + 74 60 ; je8 !EOF + # deal with line comments starting with # + 3C 23 ; cmp_al, !0x23 + 74 69 ; je8 !ascii_comment + # deal with line comments starting with ; + 3C 3B ; cmp_al, !0x3b + 74 65 ; je8 !ascii_comment + # deal all ascii less than 0 + 3C 30 ; cmp_al, !0x30 + 7C 5E ; jl8 !ascii_other + # deal with 0-9 + 3C 3A ; cmp_al, !0x3a + 7C 51 ; jl8 !ascii_num + # deal with all ascii less than A + 3C 41 ; cmp_al, !0x41 + 7C 56 ; jl8 !ascii_other + # deal with A-F + 3C 47 ; cmp_al, !0x47 + 7C 4F ; jl8 !ascii_high + # deal with all ascii less than a + 3C 61 ; cmp_al, !0x61 + 7C 4E ; jl8 !ascii_other + # deal with a-f + 3C 67 ; cmp_al, !0x67 + 7C 44 ; jl8 !ascii_low + # The rest that remains needs to be ignored + EB 48 ; jmp8 !ascii_other + +# :Second_pass (0x600160) + E8 8B000000 ; call %Read_byte + + # Deal with EOF + 3C FC ; cmp_al, !-4 + 74 35 ; je8 !Second_pass_done + + # Simply drop the label + 3C 3A ; cmp_al, !0x3a + 75 07 ; jne8 !Second_pass_0 + + E8 7E000000 ; call %Read_byte + EB EC ; jmp8 !Second_pass + +# :Second_pass_0 (0x600174) + # Deal with % pointer + 3C 25 ; cmp_al, !0x25 + 75 07 ; jne8 !Second_pass_1 + + E8 CE000000 ; call %StorePointer + EB E1 ; jmp8 !Second_pass + +# :Second_pass_1 (0x60017F) + # Deal with everything else + E8 B6FFFFFF ; call %hex # Process our char + + # Deal with EOF + 3C FC ; cmp_al, !-4 + 74 16 ; je8 !Second_pass_done + + # deal with -1 values + 3C 00 ; cmp_al, !0 + 7C D4 ; jl8 !Second_pass + + # deal with toggle + 4983FF 00 ; cmp_r15, !0 + 74 29 ; je8 !print + + # process first byte of pair + 4989C6 ; mov_r14,rax + 49C7C7 00000000 ; mov_r15, %0 + EB C2 ; jmp8 !Second_pass + +# :Second_pass_done (0x60019E) +# :EOF + C3 ; ret +# :ascii_num (0x60019F) + 2C 30 ; sub_al, !0x30 + C3 ; ret +# :ascii_low (0x6001A2) + 2C 57 ; sub_al, !0x57 + C3 ; ret +# :ascii_high (0x6001A5) + 2C 37 ; sub_al, !0x37 + C3 ; ret +# :ascii_other (0x6001A8) + B0 FF ; mov_al, !-1 + C3 ; ret +# :ascii_comment (0x6001AB) + E8 40000000 ; call %Read_byte + 3C 0D ; cmp_al, !0xd + 74 04 ; je8 !ascii_comment_cr + 3C 0A ; cmp_al, !0xa + 75 F3 ; jne8 !ascii_comment +# :ascii_comment_cr (0x6001B8) + B0 FF ; mov_al, !-1 + C3 ; ret + +# process second byte of pair +# :print (0x6001BB) + # update the sum and store in output + 49C1E6 04 ; shl_r14, !4 + 4C01F0 ; add_rax,r14 + 8805 A6000000 ; mov_[rip+DWORD],al %table + + # flip the toggle + 49F7D7 ; not_r15 + + # Print our first Hex + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + E8 41000000 ; call %print_chars + + 4983C5 01 ; add_r13, !1 # Increment IP + E9 80FFFFFF ; jmp %Second_pass + +# :Done (0x6001E0) + # program completed Successfully + 48C7C7 00000000 ; mov_rdi, %0 # All is well + 48C7C0 3C000000 ; mov_rax, %0x3c # put the exit syscall number in rax + 0F05 ; syscall # Call it a good day + +# :Read_byte (0x6001F0) + # Attempt to read 1 byte from STDIN + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + 488D35 70000000 ; lea_rsi,[rip+DWORD] %table # Where to put it + 4C89CF ; mov_rdi,r9 # Where are we reading from + 31C0 ; xor_eax,eax # the syscall number for read + 0F05 ; syscall # call the Kernel + + 4885C0 ; test_rax,rax # check what we got + 74 0B ; je8 !Read_byte_1 # Got EOF call it done + + # load byte + 8A05 5E000000 ; mov_al,[rip+DWORD] %table # load char + 480FB6C0 ; movzx_rax,al # We have to zero extend it to use it + C3 ; ret + +# Deal with EOF +# :Read_byte_1 (0x600215) + B0 FC ; mov_al, !-4 # Put EOF in rax + C3 ; ret + +# :print_chars (0x600218) + 488D35 4F000000 ; lea_rsi,[rip+DWORD] %table # What we are writing + 4C89D7 ; mov_rdi,r10 # Write to target file + 48C7C0 01000000 ; mov_rax, %1 # the syscall number for write + 0F05 ; syscall # call the Kernel + C3 ; ret + +# :Get_table_target (0x60022C) + E8 BFFFFFFF ; call %Read_byte # Get single char label + 48C1E0 03 ; shl_rax, !3 # Each label in table takes 8 bytes to store + 488D0D 32000000 ; lea_rcx,[rip+DWORD] %table # Get table + 4801C8 ; add_rax,rcx # Calculate offset + C3 ; ret + +# :StoreLabel (0x600240) + E8 E7FFFFFF ; call %Get_table_target + 4C8928 ; mov_[rax],r13 # Write out pointer to table + 31C0 ; xor_eax,eax # wipe higher bits of rax, so that cmp al works + C3 ; ret + +# :StorePointer (0x60024B) + 4983C5 04 ; add_r13, !4 # Increment IP + E8 D8FFFFFF ; call %Get_table_target # Get address of pointer + 488B00 ; mov_rax,[rax] # Get pointer + 4C29E8 ; sub_rax,r13 # target - ip + 488905 0D000000 ; mov_[rip+DWORD],rax %table # put value in output + 48C7C2 04000000 ; mov_rdx, %4 # set the size of chars we want + E8 ABFFFFFF ; call %print_chars + C3 ; ret + +# :table (0x60026E) + +# :ELF_end diff --git a/vendor/seed/amd64/hex2.hex1 b/vendor/seed/amd64/hex2.hex1 @@ -0,0 +1,587 @@ +# SPDX-FileCopyrightText: 2016 Jeremiah Orians <jeremiah@pdp10.guru> +# SPDX-FileCopyrightText: 2017 Jan Nieuwenhuizen <janneke@gnu.org> +# +# SPDX-License-Identifier: GPL-3.0-or-later + +## ELF Header +# :ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +3E 00 ## e_machine Indicating AMD64 +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +# :ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +EF 05 00 00 00 00 00 00 ## p_filesz +EF 05 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +# :ELF_text + +# Where the ELF Header is going to hit +# Simply jump to _start +# Our main function + + # Register usage: + # RAX, RDX, RSI, RDI => Temps + # R15 => Flag + # R14 => High bits + # R13 => IP + # R12 => MALLOC + # R11 => HEAD + + # Struct format: (size 24) + # NEXT => 0 + # TARGET => 8 + # NAME => 16 + +# :_start + 48C7C7 00000000 ; mov_rdi, %0 # Get current pointer + E8 %w ; call %malloc # Get current HEAP + 4889C7 ; mov_rdi,rax # Using current + 4989C4 ; mov_r12,rax # Setup MALLOC + 4881C7 00008000 ; add_rdi, %8388608 # Create space for temp [8MB] + E8 %w ; call %malloc # Give ourselves 8192000 bytes to work with + + 4C8925 %T ; mov_[rip+DWORD],r12 %scratch # Allocate space for scratch area + 4981C4 00080000 ; add_r12, %0x800 # 2 KiB of scratch + + 58 ; pop_rax # Get the number of arguments + 5F ; pop_rdi # Get the program name + 5F ; pop_rdi # Get the actual input name + 48C7C6 00000000 ; mov_rsi, %0 # prepare read_only + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 4989C1 ; mov_r9,rax # Preserve the file pointer we were given + + 5F ; pop_rdi # Get the actual output name + 48C7C6 41020000 ; mov_rsi, %577 # Prepare file as O_WRONLY|O_CREAT|O_TRUNC + 48C7C2 C0010000 ; mov_rdx, %448 # Prepare file as RWX for owner only (700 in octal) + 48C7C0 02000000 ; mov_rax, %2 # the syscall number for open() + 0F05 ; syscall # Now open that damn file + 4883F8 00 ; cmp_rax, !0 # Check for missing output + 0F8F %R ; jg %_start_out # Have real input + 48C7C0 01000000 ; mov_rax, %1 # Use stdout + +:R # :_start_out + 4989C2 ; mov_r10,rax # Preserve the file pointer we were given + + E8 %H ; call %ClearScratch # Zero scratch + 49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing + 49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum + 49C7C5 00006000 ; mov_r13, %0x00600000 # Our starting IP + 49C7C3 00000000 ; mov_r11, %0 # HEAD = NULL + E8 %a ; call %First_pass # Process it + + # rewind input file + 4C89CF ; mov_rdi,r9 # Using our input file + 48C7C6 00000000 ; mov_rsi, %0 # Offset Zero + 48C7C2 00000000 ; mov_rdx, %0 # Whence Zero + 48C7C0 08000000 ; mov_rax, %8 # lseek + 4153 ; push_r11 # Protect HEAD + 0F05 ; syscall + 415B ; pop_r11 # Restore HEAD + + 49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing + 49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum + 49C7C5 00006000 ; mov_r13, %0x00600000 # Our starting IP + E8 %k ; call %Second_pass # Process it + + E9 %v ; jmp %Done + +:a # :First_pass + E8 %x ; call %Read_byte + + # Deal with EOF + 4883F8 FC ; cmp_rax, !-4 + 0F84 %i ; je %First_pass_done + + # Check for : + 4883F8 3A ; cmp_rax, !0x3A + 0F85 %b ; jne %First_pass_0 + + # Deal with label + E9 %C ; jmp %StoreLabel + +:b # :First_pass_0 + # Check for ! + 4883F8 21 ; cmp_rax, !0x21 + 0F84 %h ; je %First_pass_pointer + + # Check for @ + 4883F8 40 ; cmp_rax, !0x40 + 0F84 %h ; je %First_pass_pointer + + # Check for $ + 4883F8 24 ; cmp_rax, !0x24 + 0F84 %h ; je %First_pass_pointer + + # Check for % + 4883F8 25 ; cmp_rax, !0x25 + 0F84 %h ; je %First_pass_pointer + + # Check for & + 4883F8 26 ; cmp_rax, !0x26 + 0F84 %h ; je %First_pass_pointer + + # Deal with everything else + E8 %j ; call %hex # Process our char + + # Deal with EOF + 4883F8 FC ; cmp_rax, !-4 + 0F84 %i ; je %First_pass_done + + # deal with -1 values + 4883F8 00 ; cmp_rax, !0 + 0F8C %a ; jl %First_pass + + # deal with toggle + 4983FF 00 ; cmp_r15, !0 + 0F84 %c ; je %First_pass_1 + 4983C5 01 ; add_r13, !1 # Increment IP + +:c # :First_pass_1 + 49F7D7 ; not_r15 + E9 %a ; jmp %First_pass + +:d # :Update_Pointer + # Check for ! + 4883F8 21 ; cmp_rax, !0x21 + 0F84 %g ; je %Update_Pointer_1 + + # Check for @ + 4883F8 40 ; cmp_rax, !0x40 + 0F84 %f ; je %Update_Pointer_2 + + # Check for $ + 4883F8 24 ; cmp_rax, !0x24 + 0F84 %f ; je %Update_Pointer_2 + + # Check for % + 4883F8 25 ; cmp_rax, !0x25 + 0F84 %e ; je %Update_Pointer_4 + + # Check for & + 4883F8 26 ; cmp_rax, !0x26 + 0F84 %e ; je %Update_Pointer_4 + + # deal with bad input + E8 %Q # call %fail + +:e # :Update_Pointer_4 + 4983C5 02 ; add_r13, !2 # Increment IP +:f # :Update_Pointer_2 + 4983C5 01 ; add_r13, !1 # Increment IP +:g # :Update_Pointer_1 + 4983C5 01 ; add_r13, !1 # Increment IP + C3 ; ret + +:h # :First_pass_pointer + # Deal with Pointer to label + E8 %d ; call %Update_Pointer # Increment IP + 488B1D %T ; mov_rbx,[rip+DWORD] %scratch # Using scratch + E8 %A ; call %consume_token # Read token + E8 %H ; call %ClearScratch # Throw away token + 4883F8 3E ; cmp_rax, !0x3E # check for '>' + 0F85 %a ; jne %First_pass # Loop again + + # Deal with %label>label case + 488B1D %T ; mov_rbx,[rip+DWORD] %scratch # Write to scratch + E8 %A ; call %consume_token # get token + E8 %H ; call %ClearScratch # Clean up after ourselves + E9 %a ; jmp %First_pass # Loop again + +:i # :First_pass_done + C3 ; ret + +:j # :hex + # deal with EOF + 4883F8 FC ; cmp_rax, !-4 + 0F84 %n ; je %EOF + # deal with line comments starting with # + 4883F8 23 ; cmp_rax, !0x23 + 0F84 %s ; je %ascii_comment + # deal with line comments starting with ; + 4883F8 3B ; cmp_rax, !0x3B + 0F84 %s ; je %ascii_comment + # deal all ascii less than 0 + 4883F8 30 ; cmp_rax, !0x30 + 0F8C %r ; jl %ascii_other + # deal with 0-9 + 4883F8 3A ; cmp_rax, !0x3A + 0F8C %o ; jl %ascii_num + # deal with all ascii less than A + 4883F8 41 ; cmp_rax, !0x41 + 0F8C %r ; jl %ascii_other + # deal with A-F + 4883F8 47 ; cmp_rax, !0x47 + 0F8C %q ; jl %ascii_high + # deal with all ascii less than a + 4883F8 61 ; cmp_rax, !0x61 + 0F8C %r ; jl %ascii_other + # deal with a-f + 4883F8 67 ; cmp_rax, !0x67 + 0F8C %p ; jl %ascii_low + # The rest that remains needs to be ignored + E9 %r ; jmp %ascii_other + +:k # :Second_pass + E8 %x ; call %Read_byte + + # Deal with EOF + 4883F8 FC ; cmp_rax, !-4 + 0F84 %m ; je %Second_pass_done + + # Simply drop the label + 4883F8 3A ; cmp_rax, !0x3A + 0F85 %l ; jne %Second_pass_0 + + 488B1D %T ; mov_rbx,[rip+DWORD] %scratch # Using scratch + E8 %A ; call %consume_token # Read token + E8 %H ; call %ClearScratch # Throw away token + + E9 %k ; jmp %Second_pass + +:l # :Second_pass_0 + # Deal with % pointer + 4883F8 25 ; cmp_rax, !0x25 + 0F84 %L ; je %StorePointer_rel4 + + # Deal with @ pointer + 4883F8 40 ; cmp_rax, !0x40 + 0F84 %M ; je %StorePointer_rel2 + + # Deal with ! pointer + 4883F8 21 ; cmp_rax, !0x21 + 0F84 %N ; je %StorePointer_rel1 + + # Deal with & pointer + 4883F8 26 ; cmp_rax, !0x26 + 0F84 %O ; je %StorePointer_abs4 + + # Deal with $ pointer + 4883F8 24 ; cmp_rax, !0x24 + 0F84 %P ; je %StorePointer_abs2 + +# :Second_pass_1 + # Deal with everything else + E8 %j ; call %hex # Process our char + + # Deal with EOF + 4883F8 FC ; cmp_rax, !-4 + 0F84 %m ; je %Second_pass_done + + # deal with -1 values + 4883F8 00 ; cmp_rax, !0 + 0F8C %k ; jl %Second_pass + + # deal with toggle + 4983FF 00 ; cmp_r15, !0 + 0F84 %u ; je %print + + # process first byte of pair + 4989C6 ; mov_r14,rax + 49C7C7 00000000 ; mov_r15, %0 + E9 %k ; jmp %Second_pass + +:m # :Second_pass_done +:n # :EOF + C3 ; ret + +:o # :ascii_num + 83E8 30 ; sub_rax, !0x30 + C3 ; ret +:p # :ascii_low + 83E8 57 ; sub_rax, !0x57 + C3 ; ret +:q # :ascii_high + 83E8 37 ; sub_rax, !0x37 + C3 ; ret +:r # :ascii_other + 48C7C0 FFFFFFFF ; mov_rax, %-1 + C3 ; ret +:s # :ascii_comment + E8 %x ; call %Read_byte + 4883F8 0D ; cmp_rax, !0x0D + 0F84 %t ; je %ascii_comment_cr + 4883F8 0A ; cmp_rax, !0x0A + 0F85 %s ; jne %ascii_comment +:t # :ascii_comment_cr + 48C7C0 FFFFFFFF ; mov_rax, %-1 + C3 ; ret + +# process second byte of pair +:u # :print + # update the sum and store in output + 49C1E6 04 ; shl_r14, !4 + 4C01F0 ; add_rax,r14 + + # flip the toggle + 49F7D7 ; not_r15 + + # Print our first Hex + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + E8 %z ; call %print_chars + + 4983C5 01 ; add_r13, !1 # Increment IP + E9 %k ; jmp %Second_pass + +:v # :Done + # program completed Successfully + 48C7C7 00000000 ; mov_rdi, %0 # All is well + 48C7C0 3C000000 ; mov_rax, %0x3C # put the exit syscall number in eax + 0F05 ; syscall # Call it a good day + + +# Malloc isn't actually required if the program being built fits in the initial memory +# However, it doesn't take much to add it. +# Requires a value in RDI +:w # :malloc + 48C7C0 0C000000 ; mov_rax, %12 # the Syscall # for SYS_BRK + 4153 ; push_r11 # Protect r11 + 0F05 ; syscall # call the Kernel + 415B ; pop_r11 # Restore r11 + C3 ; ret + + +:x # :Read_byte + # Attempt to read 1 byte from STDIN + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + 488D35 %S ; lea_rsi,[rip+DWORD] %write # Where to put it + 4C89CF ; mov_rdi,r9 # Where are we reading from + 48C7C0 00000000 ; mov_rax, %0 # the syscall number for read + 4153 ; push_r11 # Protect r11 + 0F05 ; syscall # call the Kernel + 415B ; pop_r11 # Restore r11 + + 4885C0 ; test_rax,rax # check what we got + 0F84 %y ; je %Read_byte_1 # Got EOF call it done + + # load byte + 8A05 %S ; mov_al,[rip+DWORD] %write # load char + 480FB6C0 ; movzx_rax,al # We have to zero extend it to use it + C3 ; ret + +# Deal with EOF +:y # :Read_byte_1 + 48C7C0 FCFFFFFF ; mov_rax, %-4 # Put EOF in rax + C3 ; ret + +:z # :print_chars + 50 ; push_rax # Push address of chars onto stack + 4889E6 ; mov_rsi,rsp # What we are writing + 4C89D7 ; mov_rdi,r10 # Write to target file + 48C7C0 01000000 ; mov_rax, %1 # the syscall number for write + 4153 ; push_r11 # Protect HEAD + 0F05 ; syscall # call the Kernel + 415B ; pop_r11 # Restore HEAD + 58 ; pop_rax # deallocate stack + C3 ; ret + + # Receives pointer in RBX + # Writes out char and updates RBX +:A # :consume_token + E8 %x ; call %Read_byte # Consume_token + + # Check for \t + 4883F8 09 ; cmp_rax, !0x09 + 0F84 %B ; je %consume_token_done + + # Check for \n + 4883F8 0A ; cmp_rax, !0x0A + 0F84 %B ; je %consume_token_done + + # Check for ' ' + 4883F8 20 ; cmp_rax, !0x20 + 0F84 %B ; je %consume_token_done + + # Check for '>' + 4883F8 3E ; cmp_rax, !0x3E + 0F84 %B ; je %consume_token_done + + # Looks like we are still reading token + 8803 ; mov_[rbx],al # Store char + 4883C3 01 ; add_rbx, !1 # Point to next spot + E9 %A ; jmp %consume_token # loop until done + +:B # :consume_token_done + 48C7C1 00000000 ; mov_rcx, %0 # Pad with nulls + 48890B ; mov_[rbx],rcx + 4883C3 08 ; add_rbx, !8 + C3 ; ret + +:C # :StoreLabel + 4C89E0 ; mov_rax,r12 # ENTRY + 4981C4 18000000 ; add_r12, %24 # CALLOC + 4C8968 08 ; mov_[rax+BYTE],r13 !8 # ENTRY->TARGET = IP + 4C8918 ; mov_[rax],r11 # ENTRY->NEXT = JUMP_TABLE + 4989C3 ; mov_r11,rax # JUMP_TABLE = ENTRY + 4D8963 10 ; mov_[r11+BYTE],r12 !16 # ENTRY->NAME = TOKEN + 4C89E3 ; mov_rbx,r12 # Write Starting after struct + E8 %A ; call %consume_token # Collect whole string + 4989DC ; mov_r12,rbx # Update HEAP + E9 %a ; jmp %First_pass + +:D # :GetTarget + 488B3D %T ; mov_rdi,[rip+DWORD] %scratch # Reset scratch + 4C89D9 ; mov_rcx,r11 # Grab JUMP_TABLE + 488B71 10 ; mov_rsi,[rcx+BYTE] !16 # I->NAME +:E # :GetTarget_loop + 8A06 ; mov_al,[rsi] # I->NAME[0] + 8A1F ; mov_bl,[rdi] # scratch[0] + 480FB6DB ; movzx_rbx,bl # Zero extend + 480FB6C0 ; movzx_rax,al # Zero extend + 38D8 ; cmp_al,bl # IF TOKEN == I->NAME + 0F85 %F ; jne %GetTarget_miss # Oops + + 4883C6 01 ; add_rsi, !1 + 4881C7 01000000 ; add_rdi, %1 + 3C 00 ; cmp_al, !0 + 0F85 %E ; jne %GetTarget_loop # Loop until + E9 %G ; jmp %GetTarget_done # Match + + # Miss +:F # :GetTarget_miss + 488B09 ; mov_rcx,[rcx] # I = I->NEXT + 4883F9 00 ; cmp_rcx, !0 # IF NULL == I + 0F84 %Q ; je %fail # Abort hard + + 488B71 10 ; mov_rsi,[rcx+BYTE] !16 # I->NAME + 488B3D %T ; mov_rdi,[rip+DWORD] %scratch # Reset scratch + E9 %E ; jmp %GetTarget_loop + +:G # :GetTarget_done + 488B41 08 ; mov_rax,[rcx+BYTE] !8 # Get address + C3 ; ret + +:H # :ClearScratch + 50 ; push_rax # Protect against changes + 53 ; push_rbx # And overwrites + 51 ; push_rcx # While we work + 488B1D %T ; mov_rbx,[rip+DWORD] %scratch # Where our scratch is + 48C7C0 00000000 ; mov_rax, %0 # Using null + +:I # :ClearScratch_loop + 488B0B ; mov_rcx,[rbx] # Get current value + 8803 ; mov_[rbx],al # Because we want null + 4883C3 01 ; add_rbx, !1 # Increment + 4883F9 00 ; cmp_rcx, !0 # Check if we hit null + 0F85 %I ; jne %ClearScratch_loop # Keep looping + + 59 ; pop_rcx # Don't Forget to + 5B ; pop_rbx # Restore Damage + 58 ; pop_rax # Entirely + C3 ; ret + +:J # :StorePointer + E8 %d ; call %Update_Pointer # Increment IP + 488B1D %T ; mov_rbx,[rip+DWORD] %scratch # Write to scratch + E8 %A ; call %consume_token # get token + 50 ; push_rax # Protect base_sep_p + 488B05 %T ; mov_rax,[rip+DWORD] %scratch # Pointer to scratch + E8 %D ; call %GetTarget # Get address of pointer + E8 %H ; call %ClearScratch # Clean up after ourselves + 4C89EA ; mov_rdx,r13 # base = IP + 5B ; pop_rbx # Restore base_sep_p + 4883FB 3E ; cmp_rbx, !0x3E # If base_sep_p == '>' + 0F85 %K ; jne %StorePointer_done # If not + + # Deal with %label>label case + 50 ; push_rax # We need to preserve main target + 488B1D %T ; mov_rbx,[rip+DWORD] %scratch # Write to scratch + E8 %A ; call %consume_token # get token + 488B05 %T ; mov_rax,[rip+DWORD] %scratch # Pointer to scratch + E8 %D ; call %GetTarget # Get address of pointer + E8 %H ; call %ClearScratch # Clean up after ourselves + 4889C2 ; mov_rdx,rax # Use our new base + 58 ; pop_rax # Restore main target + +:K # :StorePointer_done + C3 ; ret + +:L # :StorePointer_rel4 + E8 %J ; call %StorePointer # Do Common + 4829D0 ; sub_rax,rdx # target - ip + 48C7C2 04000000 ; mov_rdx, %4 # set the size of chars we want + E8 %z ; call %print_chars + E8 %H ; call %ClearScratch # Clean up after ourselves + E9 %k ; jmp %Second_pass + +:M # :StorePointer_rel2 + E8 %J ; call %StorePointer # Do Common + 4829D0 ; sub_rax,rdx # target - ip + 48C7C2 02000000 ; mov_rdx, %2 # set the size of chars we want + E8 %z ; call %print_chars + E8 %H ; call %ClearScratch # Clean up after ourselves + E9 %k ; jmp %Second_pass + +:N # :StorePointer_rel1 + E8 %J ; call %StorePointer # Do Common + 4829D0 ; sub_rax,rdx # target - ip + 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want + E8 %z ; call %print_chars + E8 %H ; call %ClearScratch # Clean up after ourselves + E9 %k ; jmp %Second_pass + +:O # :StorePointer_abs4 + E8 %J ; call %StorePointer # Do Common + 48C7C2 04000000 ; mov_rdx, %4 # set the size of chars we want + E8 %z ; call %print_chars + E8 %H ; call %ClearScratch # Clean up after ourselves + E9 %k ; jmp %Second_pass + +:P # :StorePointer_abs2 + E8 %J ; call %StorePointer # Do Common + 48C7C2 02000000 ; mov_rdx, %2 # set the size of chars we want + E8 %z ; call %print_chars + E8 %H ; call %ClearScratch # Clean up after ourselves + E9 %k ; jmp %Second_pass + +:Q # :fail + # Some shit went wrong + 48C7C7 01000000 ; mov_rdi, %1 # All is wrong + 48C7C0 3C000000 ; mov_rax, %0x3C # put the exit syscall number in eax + 0F05 ; syscall # Call it a good day + + +:S # :write + 00000000 ; NULL + 00000000 ; NULL + +:T # :scratch + 00000000 ; NULL + 00000000 ; NULL + +# :ELF_end diff --git a/vendor/seed/riscv64/ELF.hex2 b/vendor/seed/riscv64/ELF.hex2 @@ -0,0 +1,68 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org> +### This file is part of M2-Planet. +### +### M2-Planet is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### M2-Planet is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with M2-Planet. If not, see <http://www.gnu.org/licenses/>. + +### 64 bit elf header in hex2 +### if you wish to use this header, you need to add :ELF_end to the end of your +### M1 or hex2 files. + +## ELF Header + +:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +F3 00 ## e_machine Indicating RISC-V +01 00 00 00 ## e_version Indicating original elf + +&_start 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +%ELF_program_headers>ELF_base 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +&ELF_base 00 00 00 00 ## p_vaddr +&ELF_base 00 00 00 00 ## p_physaddr + +%ELF_end>ELF_base 00 00 00 00 ## p_filesz +%ELF_end>ELF_base 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +:ELF_text diff --git a/vendor/seed/riscv64/M0.hex2 b/vendor/seed/riscv64/M0.hex2 @@ -0,0 +1,1700 @@ +## Copyright (C) 2017 Jeremiah Orians +## Copyright (C) 2021 Andrius Štikonas +## Copyright (C) 2021 Gabriel Wicki +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +; Where the ELF Header is going to hit +; Simply jump to _start +; Our main function + +; Register use: +; s1: malloc pointer +; s2: input fd +; s3: output fd +; s4: struct HEAD +; s5: protected char +; s6: scratch + +; Struct format: (size 32) +; NEXT => 0 ; Next element in linked list +; TYPE => 8 ; Token type +; TEXT => 16 +; EXPRESSION => 24 + +; Types +; None => 0 +; MACRO => 1 +; STRING => 2 + +:_start + # rd_s4 addi + .000A0000 13000000 + + # rd_a2 rs1_sp !16 ld ; Input file name + .00060000 .00000100 .00000001 03300000 + + ; Open input file and store FD in s2 + # rd_a7 !56 addi ; sys_openat + .80080000 .00008003 13000000 + # rd_a0 !-100 addi ; AT_FDCWD + .00050000 .0000C0F9 13000000 + # rd_a1 rs1_a2 mv ; file name + .80050000 .00000600 13000000 + # rd_a2 addi ; read only + .00060000 13000000 + # ecall ; syscall + 73000000 + # rs1_a0 @Fail bltz ; Error opening file + .00000500 @Fail 63400000 + # rd_s2 rs1_a0 mv ; Save fd in for later + .00090000 .00000500 13000000 + + ; Set default FD for output file to stdout + # rd_s3 !1 addi + .80090000 .00001000 13000000 + + ; If we only have 2 arguments, don't use the third (it's not set) + # rd_t0 !2 addi + .80020000 .00002000 13000000 + # rd_a0 rs1_sp ld ; Get number of the args + .00050000 .00000100 03300000 + # rs1_a0 rs2_t0 @Fail blt ; No input file provided + .00000500 .00005000 @Fail 63400000 + # rs1_a0 rs2_t0 @after_open beq ; No output file provided. Use stdout + .00000500 .00005000 @after_open 63000000 + + ; Open output file and store the FD in s3 + # rd_a7 !56 addi ; sys_openat + .80080000 .00008003 13000000 + # rd_a0 !-100 addi ; AT_FDCWD + .00050000 .0000C0F9 13000000 + # rd_a1 rs1_sp !24 ld ; Output file (argument 3) + .80050000 .00000100 .00008001 03300000 + # rd_a2 !577 addi ; octal 00001101 + .00060000 .00001024 13000000 + ; O_TRUNC 00001000 + ; O_CREAT 00000100 + ; O_WRONLY 00000001 + ; OCTAL! + # rd_a3 !384 addi ; Set read and write permission on user + .80060000 .00000018 13000000 + # ecall ; syscall + 73000000 + # rd_s3 rs1_a0 mv ; Save fd in for later + .80090000 .00000500 13000000 + +:after_open + ; Prepare heap memory + # rd_a7 !214 addi ; sys_brk + .80080000 .0000600D 13000000 + # rd_a0 addi ; Get current brk + .00050000 13000000 + # ecall ; syscall + 73000000 + # rd_s1 rs1_a0 mv ; Set our malloc pointer + .80040000 .00000500 13000000 + + # rd_a0 !512 addi ; Allocate scratch + .00050000 .00000020 13000000 + # rd_ra $malloc jal ; Get S pointer + .80000000 $malloc 6F000000 + # rd_s6 rs1_a0 mv ; Save scratch pointer + .000B0000 .00000500 13000000 + + # rd_ra $Tokenize_Line jal ; Get all lines + .80000000 $Tokenize_Line 6F000000 + # rd_a0 rs1_s4 mv ; Prepare for Reverse_List + .00050000 .00000A00 13000000 + # rd_ra $Reverse_List jal ; Correct order + .80000000 $Reverse_List 6F000000 + # rd_s4 rs1_a0 mv ; Update HEAD + .000A0000 .00000500 13000000 + # rd_ra $Identify_Macros jal ; Find the DEFINEs + .80000000 $Identify_Macros 6F000000 + # rd_ra $Line_Macro jal ; Apply the DEFINEs + .80000000 $Line_Macro 6F000000 + # rd_ra $Process_String jal ; Handle strings + .80000000 $Process_String 6F000000 + # rd_ra $Eval_Immediates jal ; Handle numbers + .80000000 $Eval_Immediates 6F000000 + # rd_ra $Preserve_Other jal ; Collect the remaining + .80000000 $Preserve_Other 6F000000 + # rd_ra $Print_Hex jal ; Output our results + .80000000 $Print_Hex 6F000000 + + ; Terminate program with 0 return code + # rd_a7 !93 addi ; sys_exit + .80080000 .0000D005 13000000 + # rd_a0 mv ; Return code 0 + .00050000 13000000 + # ecall ; exit(0) + 73000000 + + +; Tokenize_Line Function +; Using input file s2 and Head s4 +; Creates a linked list of structs +; Uses a1 for in_set strings, a2 for Int C and a3 for Struct Token* p +:Tokenize_Line + # rd_sp rs1_sp !-8 addi ; allocate stack + .00010000 .00000100 .000080FF 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + +:restart + # rd_ra $fgetc jal ; Read a char + .80000000 $fgetc 6F000000 + # rd_t0 !-4 addi ; EOF + .80020000 .0000C0FF 13000000 + # rs1_a0 rs2_t0 @done beq ; File is collected + .00000500 .00005000 @done 63000000 + + # rd_a2 rs1_a0 mv ; Protect C + .00060000 .00000500 13000000 + + # rd_a1 ~comments auipc ; Get pointer to "#;" + .80050000 ~comments 17000000 + # rd_a1 rs1_a1 !comments addi ; Get pointer to "#;" + .80050000 .00800500 !comments 13000000 + # rd_ra $In_Set jal ; Check for comments + .80000000 $In_Set 6F000000 + # rd_t0 !1 addi ; If comment + .80020000 .00001000 13000000 + # rs1_a0 rs2_t0 @Purge_LineComment beq ; try again + .00000500 .00005000 @Purge_LineComment 63000000 + + # rd_a0 rs1_a2 mv ; Put C in place for check + .00050000 .00000600 13000000 + # rd_a1 ~terminators auipc ; Get pointer to "\n\t " + .80050000 ~terminators 17000000 + # rd_a1 rs1_a1 !terminators addi ; Get pointer to "\n\t " + .80050000 .00800500 !terminators 13000000 + # rd_ra $In_Set jal ; Check for terminators + .80000000 $In_Set 6F000000 + # rd_t0 !1 addi ; If terminator + .80020000 .00001000 13000000 + # rs1_a0 rs2_t0 @restart beq ; try again + .00000500 .00005000 @restart 63000000 + + # rd_a0 !32 addi ; malloc struct P + .00050000 .00000002 13000000 + # rd_ra $malloc jal ; Get pointer to P + .80000000 $malloc 6F000000 + # rd_a3 rs1_a0 mv ; Protect P + .80060000 .00000500 13000000 + # rs1_a3 rs2_s4 sd ; P->NEXT = HEAD + .00800600 .00004001 23300000 + # rd_s4 rs1_a3 mv ; HEAD = P + .000A0000 .00800600 13000000 + + # rd_a0 rs1_a2 mv ; Put C in place for check + .00050000 .00000600 13000000 + # rd_a1 ~string_char auipc ; Get pointer to "\"'" + .80050000 ~string_char 17000000 + # rd_a1 rs1_a1 !string_char addi ; Get pointer to "\"'" + .80050000 .00800500 !string_char 13000000 + # rd_ra $In_Set jal ; Check for string char + .80000000 $In_Set 6F000000 + # rd_t0 !1 addi ; If string char + .80020000 .00001000 13000000 + # rs1_a0 rs2_t0 @Store_String beq ; Get string + .00000500 .00005000 @Store_String 63000000 + + # rd_ra $Store_Atom jal ; Get whole token + .80000000 $Store_Atom 6F000000 + # $restart jal + $restart 6F000000 + +:done + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_sp rs1_sp !8 addi ; deallocate stack + .00010000 .00000100 .00008000 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; In_Set function +; Receives char C in a0 and Char* in a1 +; Returns 1 if true, zero if false in a0 +:In_Set + # rd_sp rs1_sp !-8 addi ; allocate stack + .00010000 .00000100 .000080FF 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + +:In_Set_loop + # rd_t0 rs1_a1 lbu ; Read char + .80020000 .00800500 03400000 + # rs1_a0 rs2_t0 @In_Set_True beq ; Return true + .00000500 .00005000 @In_Set_True 63000000 + # rs1_t0 @In_Set_False beqz ; Return False if NULL + .00800200 @In_Set_False 63000000 + # rd_a1 rs1_a1 !1 addi ; s = s + 1 + .80050000 .00800500 .00001000 13000000 + # $In_Set_loop jal ; Continue looping + $In_Set_loop 6F000000 + +:In_Set_True + # rd_a0 !1 addi ; Set True + .00050000 .00001000 13000000 + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_sp rs1_sp !8 addi ; deallocate stack + .00010000 .00000100 .00008000 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + +:In_Set_False + # rd_a0 mv ; Set False + .00050000 13000000 + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_sp rs1_sp !8 addi ; deallocate stack + .00010000 .00000100 .00008000 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Purge_LineComment function +; Reads chars until LF and jumps to restart +:Purge_LineComment + # rd_ra $fgetc jal ; Get a char + .80000000 $fgetc 6F000000 + # rd_t0 !10 addi ; While not LF + .80020000 .0000A000 13000000 + # rs1_a0 rs2_t0 @Purge_LineComment bne ; Keep reading + .00000500 .00005000 @Purge_LineComment 63100000 + # $restart jal + $restart 6F000000 + + +; Store_String Function +; Receives C in a2, HEAD in a3 and Input file in s2 +; Uses a1 for terminator, a2 for C and a3 for string +:Store_String + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + # rs1_sp rs2_a2 @8 sd ; protect a2 + .00000100 .0000C000 .00040000 23300000 + # rs1_sp rs2_a3 @16 sd ; protect a3 + .00000100 .0000D000 .00080000 23300000 + + # rd_a0 !2 addi ; Using TYPE STRING + .00050000 .00002000 13000000 + # rs1_a3 rs2_a0 @8 sd ; HEAD->TYPE = STRING + .00800600 .0000A000 .00040000 23300000 + # rd_a1 rs1_a2 mv ; Protect terminator + .80050000 .00000600 13000000 + # rd_a3 rs1_s6 mv ; Protect string pointer + .80060000 .00000B00 13000000 +:Store_String_Loop + # rs1_a3 rs2_a2 sb ; write byte + .00800600 .0000C000 23000000 + # rd_ra $fgetc jal ; read next char + .80000000 $fgetc 6F000000 + # rd_a2 rs1_a0 mv ; Update C + .00060000 .00000500 13000000 + # rd_a3 rs1_a3 !1 addi ; STRING = STRING + 1 + .80060000 .00800600 .00001000 13000000 + # rs1_a1 rs2_a2 @Store_String_Loop bne ; Keep looping unless we hit terminator + .00800500 .0000C000 @Store_String_Loop 63100000 + + # rd_a0 rs1_s6 mv ; Prepare the string in scratch + .00050000 .00000B00 13000000 + # rd_ra $string_length jal ; Calculate length + .80000000 $string_length 6F000000 + # rd_a0 rs1_a0 !1 addi ; Add 1 for 0 terminator + .00050000 .00000500 .00001000 13000000 + # rd_ra $malloc jal ; Allocate memory + .80000000 $malloc 6F000000 + # rd_a3 rs1_sp !16 ld ; restore a3 (HEAD) + .80060000 .00000100 .00000001 03300000 + # rs1_a3 rs2_a0 @16 sd ; HEAD->TEXT = STRING + .00800600 .0000A000 .00080000 23300000 + # rd_ra $copy_string jal ; Copy the string + .80000000 $copy_string 6F000000 + + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_a2 rs1_sp !8 ld ; restore a2 + .00060000 .00000100 .00008000 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # $restart jal + $restart 6F000000 + +; copy_string function +; Receives target in a0, and scratch s6 for source +; Uses a0, for target string T, a1 for C, a2 for source string S +; Returns nothing +:copy_string + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a1 @8 sd ; protect a1 + .00000100 .0000B000 .00040000 23300000 + # rs1_sp rs2_a2 @16 sd ; protect a2 + .00000100 .0000C000 .00080000 23300000 + + # rd_a2 rs1_s6 mv ; Get S + .00060000 .00000B00 13000000 + +:copy_string_loop + # rd_a1 rs1_a2 lbu ; S[0] + .80050000 .00000600 03400000 + # rs1_a1 @copy_string_done beqz ; Check if we are done + .00800500 @copy_string_done 63000000 + + # rs1_a0 rs2_a1 sb ; Copy char + .00000500 .0000B000 23000000 + # rd_a2 rs1_a2 !1 addi ; S = S + 1 + .00060000 .00000600 .00001000 13000000 + # rd_a0 rs1_a0 !1 addi ; T = T + 1 + .00050000 .00000500 .00001000 13000000 + # $copy_string_loop jal ; Keep going + $copy_string_loop 6F000000 + +:copy_string_done + # rd_ra $ClearScratch jal ; Clear scratch + .80000000 $ClearScratch 6F000000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a1 rs1_sp !8 ld ; restore a1 + .80050000 .00000100 .00008000 03300000 + # rd_a2 rs1_sp !16 ld ; restore a2 + .00060000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # ret + 67800000 + + +; Zero scratch area +:ClearScratch + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; protect a0 + .00000100 .0000A000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + + # rd_a0 rs1_s6 mv ; Prepare scratch + .00050000 .00000B00 13000000 + +:ClearScratch_loop + # rd_a1 rs1_a0 lb ; Read current byte: s[i] + .80050000 .00000500 03000000 + # rs1_a0 sb ; Write zero: s[i] = 0 + .00000500 23000000 + # rd_a0 rs1_a0 !1 addi ; Increment: i = i + 1 + .00050000 .00000500 .00001000 13000000 + # rs1_a1 @ClearScratch_loop bnez ; Keep looping + .00800500 @ClearScratch_loop 63100000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a0 rs1_sp !8 ld ; restore a0 + .00050000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # ret + 67800000 + + +; Store_Atom Function +; Receives C in a2, HEAD in a3 and Input file in s2 +; Uses a1 for in_set strings, a2 for C and a3 for string +:Store_Atom + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a1 @8 sd ; protect a1 + .00000100 .0000B000 .00040000 23300000 + # rs1_sp rs2_a2 @16 sd ; protect a2 + .00000100 .0000C000 .00080000 23300000 + # rs1_sp rs2_a3 @24 sd ; protect a3 + .00000100 .0000D000 .000C0000 23300000 + + # rd_a1 ~terminators auipc ; Get pointer to "\n\t " + .80050000 ~terminators 17000000 + # rd_a1 rs1_a1 !terminators addi ; Get pointer to "\n\t " + .80050000 .00800500 !terminators 13000000 + # rd_a3 rs1_s6 mv ; Protect string pointer + .80060000 .00000B00 13000000 + +:Store_Atom_loop + # rs1_a3 rs2_a2 sb ; write byte + .00800600 .0000C000 23000000 + # rd_ra $fgetc jal ; read next char + .80000000 $fgetc 6F000000 + # rd_a2 rs1_a0 mv ; Update C + .00060000 .00000500 13000000 + # rd_a3 rs1_a3 !1 addi ; STRING = STRING + 1 + .80060000 .00800600 .00001000 13000000 + # rd_ra $In_Set jal ; Check for terminators + .80000000 $In_Set 6F000000 + # rs1_a0 @Store_Atom_loop beqz ; Loop if not "\n\t " + .00000500 @Store_Atom_loop 63000000 + + # rd_a0 rs1_s6 mv ; Prepare the string in scratch + .00050000 .00000B00 13000000 + # rd_ra $string_length jal ; Calculate length + .80000000 $string_length 6F000000 + # rd_a0 rs1_a0 !1 addi ; Add 1 for 0 terminator + .00050000 .00000500 .00001000 13000000 + # rd_ra $malloc jal ; Allocate memory + .80000000 $malloc 6F000000 + # rd_a3 rs1_sp !24 ld ; restore a3 + .80060000 .00000100 .00008001 03300000 + # rs1_a3 rs2_a0 @16 sd ; HEAD->TEXT = STRING + .00800600 .0000A000 .00080000 23300000 + # rd_ra $copy_string jal ; Copy the string + .80000000 $copy_string 6F000000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a1 rs1_sp !8 ld ; restore a1 + .80050000 .00000100 .00008000 03300000 + # rd_a2 rs1_sp !16 ld ; restore a2 + .00060000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Reverse_List function +; Receives list in a0 +; Returns the list reversed in a0 +:Reverse_List + # rd_sp rs1_sp !-16 addi ; allocate stack + .00010000 .00000100 .000000FF 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + # rs1_sp rs2_a2 @8 sd ; protect a2 + .00000100 .0000C000 .00040000 23300000 + # rd_a1 rs1_a0 mv ; Set HEAD + .80050000 .00000500 13000000 + # rd_a0 mv ; ROOT = NULL + .00050000 13000000 +:Reverse_List_Loop + # rs1_a1 @Reverse_List_Done beqz ; Stop if HEAD == NULL + .00800500 @Reverse_List_Done 63000000 + + # rd_a2 rs1_a1 ld ; NEXT = HEAD->NEXT + .00060000 .00800500 03300000 + # rs1_a1 rs2_a0 sd ; HEAD->NEXT = ROOT + .00800500 .0000A000 23300000 + # rd_a0 rs1_a1 mv ; ROOT = HEAD + .00050000 .00800500 13000000 + # rd_a1 rs1_a2 mv ; HEAD = NEXT + .80050000 .00000600 13000000 + # $Reverse_List_Loop jal ; Continue looping + $Reverse_List_Loop 6F000000 + +:Reverse_List_Done + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_a2 rs1_sp !8 ld ; restore a2 + .00060000 .00000100 .00008000 03300000 + # rd_sp rs1_sp !16 addi ; deallocate stack + .00010000 .00000100 .00000001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Identify_Macros function +; Receives List in a0 +; Updates the list in place; does not modify registers +; Uses a1 for DEFINE, a2 for I +:Identify_Macros + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; protect a0 + .00000100 .0000A000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + + # rd_a1 ~DEFINE_str auipc ; Setup DEFINE string + .80050000 ~DEFINE_str 17000000 + # rd_a1 rs1_a1 !DEFINE_str addi ; Setup DEFINE string + .80050000 .00800500 !DEFINE_str 13000000 + # rd_a2 rs1_a0 mv ; I = HEAD + .00060000 .00000500 13000000 + +:Identify_Macros_Loop + # rd_a0 rs1_a2 !16 ld ; I->TEXT + .00050000 .00000600 .00000001 03300000 + # rd_ra $match jal ; IF "DEFINE" == I->TEXT + .80000000 $match 6F000000 + # rs1_a0 @Identify_Macros_Next bnez ; Check if we got macro + .00000500 @Identify_Macros_Next 63100000 + + ; Deal with MACRO + # rd_a0 !1 addi ; a0 = MACRO + .00050000 .00001000 13000000 + # rs1_a2 rs2_a0 @8 sd ; I->TYPE = MACRO + .00000600 .0000A000 .00040000 23300000 + + # rd_a0 rs1_a2 ld ; I->NEXT + .00050000 .00000600 03300000 + # rd_a0 rs1_a0 !16 ld ; I->NEXT->TEXT + .00050000 .00000500 .00000001 03300000 + # rs1_a2 rs2_a0 @16 sd ; I->TEXT = I->NEXT->TEXT + .00000600 .0000A000 .00080000 23300000 + + # rd_a0 rs1_a2 ld ; I->NEXT + .00050000 .00000600 03300000 + # rd_a0 rs1_a0 ld ; I->NEXT->NEXT + .00050000 .00000500 03300000 + # rd_a0 rs1_a0 !16 ld ; I->NEXT->NEXT->TEXT + .00050000 .00000500 .00000001 03300000 + # rs1_a2 rs2_a0 @24 sd ; I->EXPRESSION = I->NEXT->NEXT->TEXT + .00000600 .0000A000 .000C0000 23300000 + + # rd_a0 rs1_a2 ld ; I->NEXT + .00050000 .00000600 03300000 + # rd_a0 rs1_a0 ld ; I->NEXT->NEXT + .00050000 .00000500 03300000 + # rd_a0 rs1_a0 ld ; I->NEXT->NEXT->NEXT + .00050000 .00000500 03300000 + # rs1_a2 rs2_a0 sd ; I->NEXT = I->NEXT->NEXT->NEXT + .00000600 .0000A000 23300000 + +:Identify_Macros_Next + # rd_a2 rs1_a2 ld ; I = I->NEXT + .00060000 .00000600 03300000 + # rs1_a2 @Identify_Macros_Loop bnez ; Check if we are done + .00000600 @Identify_Macros_Loop 63100000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a0 rs1_sp !8 ld ; restore a0 + .00050000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; match function +; Receives CHAR* in a0 and CHAR* in a1 +; Returns 0 (TRUE) or 1 (FALSE) in a0 +:match + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + # rs1_sp rs2_a2 @8 sd ; protect a2 + .00000100 .0000C000 .00040000 23300000 + # rs1_sp rs2_a3 @16 sd ; protect a3 + .00000100 .0000D000 .00080000 23300000 + + # rd_a2 rs1_a0 mv ; S1 in place + .00060000 .00000500 13000000 + # rd_a3 rs1_a1 mv ; S2 in place + .80060000 .00800500 13000000 + +:match_Loop + # rd_a0 rs1_a2 lbu ; S1[i] + .00050000 .00000600 03400000 + # rd_a1 rs1_a3 lbu ; S2[i] + .80050000 .00800600 03400000 + # rs1_a0 rs2_a1 @match_False bne ; Check if they match + .00000500 .0000B000 @match_False 63100000 + + # rd_a2 rs1_a2 !1 addi ; S1 = S1 + 1 + .00060000 .00000600 .00001000 13000000 + # rd_a3 rs1_a3 !1 addi ; S2 = S2 + 1 + .80060000 .00800600 .00001000 13000000 + # rs1_a0 @match_Done beqz ; Match if we reached end of string + .00000500 @match_Done 63000000 + # $match_Loop jal ; Otherwise keep looping + $match_Loop 6F000000 + +:match_False + # rd_a0 !1 addi ; Return false + .00050000 .00001000 13000000 +:match_Done + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_a2 rs1_sp !8 ld ; restore a2 + .00060000 .00000100 .00008000 03300000 + # rd_a3 rs1_sp !16 ld ; restore a3 + .80060000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Line_Macro function +; Receives List in a0 +; Updates the list in place; does not modify registers +; Uses a0 for I, a1 for I->TEXT, a2 for I->EXPRESSION +:Line_Macro + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; protect a0 + .00000100 .0000A000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + +:Line_Macro_Loop + # rd_a1 rs1_a0 !8 ld ; I->TYPE + .80050000 .00000500 .00008000 03300000 + # rd_t0 !1 addi ; t0 = MACRO + .80020000 .00001000 13000000 + # rs1_a1 rs2_t0 @Line_Macro_Next bne ; Move on unless I->TYPE == MACRO + .00800500 .00005000 @Line_Macro_Next 63100000 + + ; Apply macro + # rd_a1 rs1_a0 !16 ld ; I->TEXT + .80050000 .00000500 .00000001 03300000 + # rd_a2 rs1_a0 !24 ld ; I->EXPRESSION + .00060000 .00000500 .00008001 03300000 + # rd_a0 rs1_a0 ld ; I->NEXT + .00050000 .00000500 03300000 + # rd_ra $Set_Expression jal ; Apply it + .80000000 $Set_Expression 6F000000 + # $Line_Macro_Loop jal ; Move on to next + $Line_Macro_Loop 6F000000 + +:Line_Macro_Next + # rd_a0 rs1_a0 ld ; I->NEXT + .00050000 .00000500 03300000 + # rs1_a0 @Line_Macro_Loop bnez ; Check if we are done + .00000500 @Line_Macro_Loop 63100000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a0 rs1_sp !8 ld ; restore a0 + .00050000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Set_Expression function +; Receives List in a0, CHAR* in a1 and CHAR* in a2 +; Updates the list in place; does not modify registers +; Uses a1 for C, a2 for EXP and a3 for I +:Set_Expression + # rd_sp rs1_sp !-40 addi ; allocate stack + .00010000 .00000100 .000080FD 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; protect a0 + .00000100 .0000A000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + # rs1_sp rs2_a3 @32 sd ; protect a3 + .00000100 .0000D000 .00000002 23300000 + + # rd_a3 rs1_a0 mv ; Set I + .80060000 .00000500 13000000 +:Set_Expression_Loop + # rd_a0 rs1_a3 !8 ld ; I->TYPE + .00050000 .00800600 .00008000 03300000 + # rd_t0 !1 addi ; t0 = MACRO + .80020000 .00001000 13000000 + # rs1_a0 rs2_t0 @Set_Expression_Next beq ; If MACRO == I->Type then ignore and move on + .00000500 .00005000 @Set_Expression_Next 63000000 + + # rd_a0 rs1_a3 !16 ld ; I->TEXT + .00050000 .00800600 .00000001 03300000 + # rd_ra $match jal ; Check for match + .80000000 $match 6F000000 + # rs1_a0 @Set_Expression_Next bnez ; Check next if does not match + .00000500 @Set_Expression_Next 63100000 + + ; Non-macro match + # rs1_a3 rs2_a2 @24 sd ; I->EXPRESSION = EXP + .00800600 .0000C000 .000C0000 23300000 + +:Set_Expression_Next + # rd_a3 rs1_a3 ld ; I = I->NEXT + .80060000 .00800600 03300000 + # rs1_a3 @Set_Expression_Loop bnez ; Check if we are done + .00800600 @Set_Expression_Loop 63100000 + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a0 rs1_sp !8 ld ; restore a0 + .00050000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_a3 rs1_sp !32 ld ; restore a3 + .80060000 .00000100 .00000002 03300000 + # rd_sp rs1_sp !40 addi ; deallocate stack + .00010000 .00000100 .00008002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Process_String function +; Receives List in a0 +; Update the list in place; does not modify registers +; Uses a1 for I->TEXT, a2 for I and RDX for S +:Process_String + # rd_sp rs1_sp !-40 addi ; allocate stack + .00010000 .00000100 .000080FD 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; protect a0 + .00000100 .0000A000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + # rs1_sp rs2_a3 @32 sd ; protect a3 + .00000100 .0000D000 .00000002 23300000 + + # rd_a2 rs1_a0 mv ; I = HEAD + .00060000 .00000500 13000000 + +:Process_String_loop + # rd_a0 rs1_a2 !8 ld ; I->TYPE + .00050000 .00000600 .00008000 03300000 + # rd_t0 !2 addi ; t0 = STRING + .80020000 .00002000 13000000 + # rs1_a0 rs2_t0 @Process_String_Next bne ; Skip to next + .00000500 .00005000 @Process_String_Next 63100000 + + # rd_a1 rs1_a2 !16 ld ; I->TEXT + .80050000 .00000600 .00000001 03300000 + # rd_a0 rs1_a1 lbu ; I->TEXT[0] + .00050000 .00800500 03400000 + # rd_t0 !39 addi ; t0 = \' + .80020000 .00007002 13000000 + # rs1_a0 rs2_t0 @Process_String_Raw bne ; Deal with '"' + .00000500 .00005000 @Process_String_Raw 63100000 + + ; Deal with \' + # rd_a1 rs1_a1 !1 addi ; I->TEXT + 1 + .80050000 .00800500 .00001000 13000000 + # rs1_a2 rs2_a1 @24 sd ; I->EXPRESSION = I->TEXT + 1 + .00000600 .0000B000 .000C0000 23300000 + # $Process_String_Next jal ; Move on to next + $Process_String_Next 6F000000 + +:Process_String_Raw + # rd_a0 rs1_a1 mv ; I->TEXT + .00050000 .00800500 13000000 + # rd_ra $string_length jal ; Get length of I->TEXT + .80000000 $string_length 6F000000 + # rd_a0 rs1_a0 rs2_x2 srli ; LENGTH = LENGTH >> 2 + .00050000 .00000500 .00002000 13500000 + # rd_a0 rs1_a0 !1 addi ; LENGTH = LENGTH + 1 + .00050000 .00000500 .00001000 13000000 + # rd_a0 rs1_a0 rs2_x3 slli ; LENGTH = LENGTH << 3 + .00050000 .00000500 .00003000 13100000 + # rd_ra $malloc jal ; Get string + .80000000 $malloc 6F000000 + # rd_a3 rs1_a1 mv ; S = I->TEXT + .80060000 .00800500 13000000 + # rd_a3 rs1_a3 !1 addi ; S = S + 1 + .80060000 .00800600 .00001000 13000000 + # rs1_a2 rs2_a0 @24 sd ; I->EXPRESSION = hexify + .00000600 .0000A000 .000C0000 23300000 + # rd_a1 rs1_a0 mv ; Put hexify buffer in a1 + .80050000 .00000500 13000000 + +:Process_String_Raw_Loop + # rd_a0 rs1_a3 lbu ; Read 1 character + .00050000 .00800600 03400000 + # rd_a3 rs1_a3 !1 addi ; S = S + 1 + .80060000 .00800600 .00001000 13000000 + # rd_s5 rs1_a0 mv ; Protect character + .800A0000 .00000500 13000000 + # rd_ra $hex8 jal ; write them all + .80000000 $hex8 6F000000 + # rd_a0 rs1_s5 mv ; Restore character + .00050000 .00800A00 13000000 + # rs1_a0 @Process_String_Raw_Loop bnez ; Keep looping + .00000500 @Process_String_Raw_Loop 63100000 + +:Process_String_Next + # rd_a2 rs1_a2 ld ; I = I->NEXT + .00060000 .00000600 03300000 + # rs1_a2 @Process_String_loop bnez ; Check if we are done + .00000600 @Process_String_loop 63100000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a0 rs1_sp !8 ld ; restore a0 + .00050000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_a3 rs1_sp !32 ld ; restore a3 + .80060000 .00000100 .00000002 03300000 + # rd_sp rs1_sp !40 addi ; deallocate stack + .00010000 .00000100 .00008002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; string_length function +; Receives CHAR* in a0 +; Returns INT in a0 +; Uses a0 for CH, a1 for S and a2 for INDEX +:string_length + # rd_sp rs1_sp !-16 addi ; allocate stack + .00010000 .00000100 .000000FF 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + # rs1_sp rs2_a2 @8 sd ; protect a2 + .00000100 .0000C000 .00040000 23300000 + + # rd_a1 rs1_a0 mv ; Set S + .80050000 .00000500 13000000 + # rd_a2 mv ; INDEX = 0 + .00060000 13000000 + +:string_length_loop + # rd_t0 rs1_a1 rs2_a2 add ; S + INDEX + .80020000 .00800500 .0000C000 33000000 + # rd_a0 rs1_t0 lbu ; S[INDEX] + .00050000 .00800200 03400000 + # rs1_a0 @string_length_done beqz ; Check if we are done + .00000500 @string_length_done 63000000 + + # rd_a2 rs1_a2 !1 addi ; INDEX = INDEX + 1 + .00060000 .00000600 .00001000 13000000 + # $string_length_loop jal ; Keep going + $string_length_loop 6F000000 + +:string_length_done + # rd_a0 rs1_a2 mv ; return INDEX + .00050000 .00000600 13000000 + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_a2 rs1_sp !8 ld ; restore a2 + .00060000 .00000100 .00008000 03300000 + # rd_sp rs1_sp !16 addi ; deallocate stack + .00010000 .00000100 .00000001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Eval_Immediates function +; Receives List in a0 +; Updates the list in place; does not modify registers +; Uses a1 for I->TEXT[0], a2 for I->TEXT[1] and a3 for I +:Eval_Immediates + # rd_sp rs1_sp !-40 addi ; allocate stack + .00010000 .00000100 .000080FD 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; protect a0 + .00000100 .0000A000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + # rs1_sp rs2_a3 @32 sd ; protect a3 + .00000100 .0000D000 .00000002 23300000 + + # rd_a3 rs1_a0 mv ; I = HEAD + .80060000 .00000500 13000000 + +:Eval_Immediates_Loop + ; Check for MACRO + # rd_a0 rs1_a3 !8 ld ; I->TYPE + .00050000 .00800600 .00008000 03300000 + # rd_t0 !1 addi ; t0 = MACRO + .80020000 .00001000 13000000 + # rs1_a0 rs2_t0 @Eval_Immediates_Next beq ; Skip to next if I->TYPE == MACRO + .00000500 .00005000 @Eval_Immediates_Next 63000000 + + ; Check for NULL EXPRESSION + # rd_a0 rs1_a3 !24 ld ; I->EXPRESSION + .00050000 .00800600 .00008001 03300000 + # rs1_a0 @Eval_Immediates_Next bnez ; Skip to next if NULL == I->EXPRESSION + .00000500 @Eval_Immediates_Next 63100000 + + ; Check if number + # rd_a0 rs1_a3 !16 ld ; I->TEXT + .00050000 .00800600 .00000001 03300000 + # rd_a1 rs1_a0 lbu ; I->TEXT[0] + .80050000 .00000500 03400000 + # rd_a0 rs1_a0 !1 addi ; I->TEXT + 1 + .00050000 .00000500 .00001000 13000000 + # rd_a2 rs1_a0 lbu ; I->TEXT[1] + .00060000 .00000500 03400000 + # rd_ra $numerate_string jal ; Convert string to INT + .80000000 $numerate_string 6F000000 + # rs1_a0 @Eval_Immediates_value bnez ; Has a value IF 0 != numerate_string(I->TEXT + 1) + .00000500 @Eval_Immediates_value 63100000 + + ; Last chance for Immediate + # rd_t0 !48 addi ; If '0' = I->TEXT[1] + .80020000 .00000003 13000000 + # rs1_a2 rs2_t0 @Eval_Immediates_Next bne ; Skip to next + .00000600 .00005000 @Eval_Immediates_Next 63100000 + +:Eval_Immediates_value + # rd_ra $express_number jal ; Convert value to hex string + .80000000 $express_number 6F000000 + # rs1_a3 rs2_a0 @24 sd ; I->EXPRESSION = express_number(value, I-TEXT[0]) + .00800600 .0000A000 .000C0000 23300000 + +:Eval_Immediates_Next + # rd_a3 rs1_a3 ld ; I = I->NEXT + .80060000 .00800600 03300000 + # rs1_a3 @Eval_Immediates_Loop bnez ; Check if we are done + .00800600 @Eval_Immediates_Loop 63100000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a0 rs1_sp !8 ld ; restore a0 + .00050000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_a3 rs1_sp !32 ld ; restore a3 + .80060000 .00000100 .00000002 03300000 + # rd_sp rs1_sp !40 addi ; deallocate stack + .00010000 .00000100 .00008002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; numerate_string function +; Receives CHAR* in a0 +; Returns value of CHAR* in a0 +; Uses a0 for VALUE, a1 for S, a2 for CH and a3 for NEGATIVE? +:numerate_string + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + # rs1_sp rs2_a2 @8 sd ; protect a2 + .00000100 .0000C000 .00040000 23300000 + # rs1_sp rs2_a3 @16 sd ; protect a3 + .00000100 .0000D000 .00080000 23300000 + + # rd_a1 rs1_a0 mv ; put S in correct place + .80050000 .00000500 13000000 + # rd_a0 mv ; Initialize to Zero + .00050000 13000000 +:numerate_string_loop + # rd_t0 rs1_a1 !1 addi ; S + 1 + .80020000 .00800500 .00001000 13000000 + # rd_a2 rs1_t0 lbu ; S[1] + .00060000 .00800200 03400000 + # rd_t0 !120 addi ; 'x' + .80020000 .00008007 13000000 + # rs1_a2 rs2_t0 @numerate_hex beq ; Deal with hex_input + .00000600 .00005000 @numerate_hex 63000000 + + ; Assume decimal input + # rd_a3 mv ; Assume no negation + .80060000 13000000 + # rd_a2 rs1_a1 lbu ; S[0] + .00060000 .00800500 03400000 + # rd_t0 !45 addi ; '-' + .80020000 .0000D002 13000000 + # rs1_a2 rs2_t0 @numerate_decimal bne ; Skip negation + .00000600 .00005000 @numerate_decimal 63100000 + + # rd_a3 !1 addi ; Set FLAG + .80060000 .00001000 13000000 + # rd_a1 rs1_a1 !1 addi ; S = S + 1 + .80050000 .00800500 .00001000 13000000 + +:numerate_decimal + # rd_a2 rs1_a1 lbu ; S[i] + .00060000 .00800500 03400000 + # rs1_a2 @numerate_decimal_done beqz ; We are done if NULL == S[i] + .00000600 @numerate_decimal_done 63000000 + + ; a0 = a0 * 10 = (a0 << 3) + (a0 << 1) + # rd_t0 rs1_a0 rs2_x3 slli ; a0 * 8 + .80020000 .00000500 .00003000 13100000 + # rd_t1 rs1_a0 rs2_x1 slli ; a0 * 2 + .00030000 .00000500 .00001000 13100000 + # rd_a0 rs1_t0 rs2_t1 add ; VALUE = VALUE * 10 + .00050000 .00800200 .00006000 33000000 + # rd_a2 rs1_a2 !-48 addi ; CH = CH - '0' + .00060000 .00000600 .000000FD 13000000 + # rd_t0 !9 addi ; t0 = 9 + .80020000 .00009000 13000000 + # rs1_t0 rs2_a2 @numerate_string_fail blt ; Check for illegal CH > 9 + .00800200 .0000C000 @numerate_string_fail 63400000 + # rs1_a2 @numerate_string_fail bltz ; Check for illegal CH < 0 + .00000600 @numerate_string_fail 63400000 + # rd_a0 rs1_a0 rs2_a2 add ; VALUE = VALUE + CH + .00050000 .00000500 .0000C000 33000000 + # rd_a1 rs1_a1 !1 addi ; S = S + 1 + .80050000 .00800500 .00001000 13000000 + # $numerate_decimal jal + $numerate_decimal 6F000000 + +:numerate_decimal_done + # rd_t0 !1 addi ; Check for negative FLAG + .80020000 .00001000 13000000 + # rs1_a3 rs2_t0 @numerate_string_done bne ; Nope + .00800600 .00005000 @numerate_string_done 63100000 + + # rd_a0 rs2_a0 sub ; VALUE = -VALUE + .00050000 .0000A000 33000040 + # $numerate_string_done jal ; Done + $numerate_string_done 6F000000 + +:numerate_hex + # rd_a1 rs1_a1 !2 addi ; S = S + 2 + .80050000 .00800500 .00002000 13000000 +:numerate_hex_loop + # rd_a2 rs1_a1 lbu ; S[i] + .00060000 .00800500 03400000 + # rs1_a2 @numerate_string_done beqz ; We are done if NULL == S[i] + .00000600 @numerate_string_done 63000000 + + # rd_a0 rs1_a0 rs2_x4 slli ; VALUE = VALUE << 4 + .00050000 .00000500 .00004000 13100000 + # rd_a2 rs1_a2 !-48 addi ; CH = CH - '0' + .00060000 .00000600 .000000FD 13000000 + # rd_t0 !10 addi ; t0 = 10 + .80020000 .0000A000 13000000 + # rs1_a2 rs2_t0 @numerate_hex_digit blt ; Check if we are dealing with number or letter + .00000600 .00005000 @numerate_hex_digit 63400000 + # rd_a2 rs1_a2 !-7 addi ; Push A-F into range + .00060000 .00000600 .000090FF 13000000 + +:numerate_hex_digit + # rd_t0 !15 addi ; t0 = 15 + .80020000 .0000F000 13000000 + # rs1_t0 rs2_a2 @numerate_string_fail blt ; Check for CH > 'F' + .00800200 .0000C000 @numerate_string_fail 63400000 + # rs1_a2 @numerate_string_fail bltz ; Check for CH < 0 + .00000600 @numerate_string_fail 63400000 + # rd_a0 rs1_a0 rs2_a2 add ; VALUE = VALUE + CH + .00050000 .00000500 .0000C000 33000000 + # rd_a1 rs1_a1 !1 addi ; S = S + 1 + .80050000 .00800500 .00001000 13000000 + # $numerate_hex_loop jal ; Keep looping + $numerate_hex_loop 6F000000 + +:numerate_string_fail + # rd_a0 mv ; return ZERO + .00050000 13000000 + +:numerate_string_done + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_a2 rs1_sp !8 ld ; restore a2 + .00060000 .00000100 .00008000 03300000 + # rd_a3 rs1_sp !16 ld ; restore a3 + .80060000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; express_number function +; Receives INT in a0 and CHAR in a1 +; Allocates a string and expresses the value in appropriate RISC-V encoding +; Returns string in a0 +; Uses a0 for VALUE, a1 for S and a2 for CH +:express_number + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a1 @8 sd ; protect a1 + .00000100 .0000B000 .00040000 23300000 + # rs1_sp rs2_a2 @16 sd ; protect a2 + .00000100 .0000C000 .00080000 23300000 + # rs1_sp rs2_a3 @24 sd ; protect a3 + .00000100 .0000D000 .000C0000 23300000 + + # rd_a2 rs1_a1 mv ; Put CH in right place + .00060000 .00800500 13000000 + # rd_s5 rs1_a0 mv ; Protect VALUE + .800A0000 .00000500 13000000 + + # rd_a0 !10 addi ; We need 10 bytes + .00050000 .0000A000 13000000 + # rd_ra $malloc jal ; Get S pointer + .80000000 $malloc 6F000000 + # rd_a1 rs1_a0 mv ; Put S in place + .80050000 .00000500 13000000 + # rd_a0 rs1_s5 mv ; Restore VALUE + .00050000 .00800A00 13000000 + + ; Check for % + # rd_t0 !0x25 addi + .80020000 .00005002 13000000 + # rs1_a2 rs2_t0 @express_number_const beq + .00000600 .00005000 @express_number_const 63000000 + + # rd_s5 rs1_a1 mv ; Protect S + .800A0000 .00800500 13000000 + # rd_t0 !0x2E addi ; t0 = '.' + .80020000 .0000E002 13000000 + # rs1_a1 rs2_t0 sd ; S[0] = '.' + .00800500 .00005000 23300000 + # rd_a1 rs1_a1 !1 addi ; Next byte + .80050000 .00800500 .00001000 13000000 + + ; Check for ! + # rd_t0 !0x21 addi + .80020000 .00001002 13000000 + # rs1_a2 rs2_t0 @express_number_I beq + .00000600 .00005000 @express_number_I 63000000 + + ; Check for @ + # rd_t0 !0x40 addi + .80020000 .00000004 13000000 + # rs1_a2 rs2_t0 @express_number_S beq + .00000600 .00005000 @express_number_S 63000000 + + ; Check for ~ + # rd_t0 !0x7E addi + .80020000 .0000E007 13000000 + # rs1_a2 rs2_t0 @express_number_U beq + .00000600 .00005000 @express_number_U 63000000 + + # $Fail jal ; Error + $Fail 6F000000 + +:express_number_const + ; provides an option for 32-bit immediate constants + + # rd_t0 !2 addi + .80020000 .00002000 13000000 + # rd_t0 rs1_t0 rs2_x31 slli + .80020000 .00800200 .0000F001 13100000 + # rd_t0 rs1_t0 !-1 addi ; t0 = 0xffffffff + .80020000 .00800200 .0000F0FF 13000000 + # rd_a0 rs1_a0 rs2_t0 and ; immediate = value & 0xffffffff + .00050000 .00000500 .00005000 33700000 + + # rd_s5 rs1_a1 mv ; Protect S + .800A0000 .00800500 13000000 + # rd_ra $hex32l jal ; Store 32-bits + .80000000 $hex32l 6F000000 + # $express_number_done jal ; done + $express_number_done 6F000000 + +:express_number_I + ; Corresponds to RISC-V S format + ; (value & 0xfff) << 20 + # rd_t0 !0xFFF addi + .80020000 .0000F0FF 13000000 + # rd_a0 rs1_a0 rs2_t0 and ; value & 0xfff + .00050000 .00000500 .00005000 33700000 + # rd_a0 rs1_a0 rs2_x20 slli ; (value & 0xfff) << 20 + .00050000 .00000500 .00004001 13100000 + # rd_ra $hex32l jal ; Store 32-bits + .80000000 $hex32l 6F000000 + # $express_number_done jal ; done + $express_number_done 6F000000 + +:express_number_S + ; Corresponds to RISC-V S format + ; ((value & 0x1f) << 7) | ((value & 0xfe0) << (31 - 11)) + # rd_t0 !0x1F addi + .80020000 .0000F001 13000000 + # rd_t1 rs1_a0 rs2_t0 and ; value & 0x1f + .00030000 .00000500 .00005000 33700000 + # rd_t1 rs1_t1 rs2_x7 slli ; (value & 0x1f) << 7 + .00030000 .00000300 .00007000 13100000 + # rd_t0 !0xFE0 addi + .80020000 .000000FE 13000000 + # rd_t0 rs1_a0 rs2_t0 and ; value & 0xfe0 + .80020000 .00000500 .00005000 33700000 + # rd_t0 rs1_t0 rs2_x20 slli ; (value & 0xfe0) << (31 - 11) + .80020000 .00800200 .00004001 13100000 + # rd_a0 rs1_t0 rs2_t1 or ; Combine two parts + .00050000 .00800200 .00006000 33600000 + # rd_ra $hex32l jal ; Store 32-bits + .80000000 $hex32l 6F000000 + # $express_number_done jal ; done + $express_number_done 6F000000 + +:express_number_U + ; Corresponds to RISC-V U format + ; if value is 0x800 or more we have to add 11-th bit (0x1000) to compensate for signed extension + + # rd_t0 ~0x800 lui ; load higher bits + .80020000 .00100000 37000000 + # rd_t0 rs1_t0 !0x800 addiw + .80020000 .00800200 .00000080 1B000000 + # rd_t1 ~0xFFF lui ; load higher bits + .00030000 .00100000 37000000 + # rd_t1 rs1_t1 !0xFFF addiw + .00030000 .00000300 .0000F0FF 1B000000 + ; We are outside 31-bit that ~ can normally load + # rd_t2 ~0x100000 lui ; load 0xfffff000 + .80030000 .00001000 37000000 + # rd_t2 rs1_t2 !-1 addiw ; load 0xfffff000 + .80030000 .00800300 .0000F0FF 1B000000 + # rd_t2 rs1_t2 rs2_x12 slli ; load 0xfffff000 + .80030000 .00800300 .0000C000 13100000 + # rd_t1 rs1_a0 rs2_t1 and ; value & 0xfff + .00030000 .00000500 .00006000 33700000 + # rd_a0 rs1_a0 rs2_t2 and ; value & 0xfffff000 + .00050000 .00000500 .00007000 33700000 + # rs1_t1 rs2_t0 @express_number_U_small blt + .00000300 .00005000 @express_number_U_small 63400000 + + ; Deal with sign extension: add 0x1000 + # rd_t0 ~0x1000 lui + .80020000 .00100000 37000000 + # rd_a0 rs1_t0 rs2_a0 addw ; (value & 0xfffff000) + 0x1000 + .00050000 .00800200 .0000A000 3B000000 +:express_number_U_small + # rd_ra $hex32l jal ; Store 32-bits + .80000000 $hex32l 6F000000 + # $express_number_done jal ; done + $express_number_done 6F000000 + +:express_number_done + # rd_a0 rs1_s5 mv ; Restore S + .00050000 .00800A00 13000000 + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a1 rs1_sp !8 ld ; restore a1 + .80050000 .00000100 .00008000 03300000 + # rd_a2 rs1_sp !16 ld ; restore a2 + .00060000 .00000100 .00000001 03300000 + # rd_a3 rs1_sp !24 ld ; restore a3 + .80060000 .00000100 .00008001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; HEX to ascii routine +; Receives INT in a0 and CHAR* in a1 +; Stores ascii of INT in CHAR* +; Returns only modifying a0 +:hex32l + # rd_sp rs1_sp !-16 addi ; allocate stack + .00010000 .00000100 .000000FF 13000000 + # rs1_sp rs2_ra sd ; Protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; Protect top 16 bits + .00000100 .0000A000 .00040000 23300000 + # rd_ra $hex16l jal ; Store it + .80000000 $hex16l 6F000000 + # rd_a0 rs1_sp !8 ld ; do high 16-bits + .00050000 .00000100 .00008000 03300000 + # rd_a0 rs1_a0 rs2_x16 srli ; do bottom 16 bits + .00050000 .00000500 .00000001 13500000 + # rd_ra $hex16l jal ; Store it + .80000000 $hex16l 6F000000 + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_sp rs1_sp !16 addi ; deallocate stack + .00010000 .00000100 .00000001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + +:hex16l + # rd_sp rs1_sp !-16 addi ; allocate stack + .00010000 .00000100 .000000FF 13000000 + # rs1_sp rs2_ra sd ; Protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; Protect top byte + .00000100 .0000A000 .00040000 23300000 + # rd_ra $hex8 jal ; Store it + .80000000 $hex8 6F000000 + # rd_a0 rs1_sp !8 ld ; do high byte + .00050000 .00000100 .00008000 03300000 + # rd_a0 rs1_a0 rs2_x8 srli ; do bottom byte + .00050000 .00000500 .00008000 13500000 + # rd_ra $hex8 jal ; Store it + .80000000 $hex8 6F000000 + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_sp rs1_sp !16 addi ; deallocate stack + .00010000 .00000100 .00000001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + +:hex8 + # rd_sp rs1_sp !-16 addi ; allocate stack + .00010000 .00000100 .000000FF 13000000 + # rs1_sp rs2_ra sd ; Protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a0 @8 sd ; Protect bottom nibble + .00000100 .0000A000 .00040000 23300000 + # rd_a0 rs1_a0 rs2_x4 srli ; do high nibble first + .00050000 .00000500 .00004000 13500000 + # rd_ra $hex4 jal ; Store it + .80000000 $hex4 6F000000 + # rd_a0 rs1_sp !8 ld ; do low nibble + .00050000 .00000100 .00008000 03300000 + # rd_ra $hex4 jal ; Store it + .80000000 $hex4 6F000000 + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_sp rs1_sp !16 addi ; deallocate stack + .00010000 .00000100 .00000001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + +:hex4 + # rd_t0 !0xF addi + .80020000 .0000F000 13000000 + # rd_a0 rs1_a0 rs2_t0 and ; isolate nibble + .00050000 .00000500 .00005000 33700000 + # rd_a0 rs1_a0 !0x30 addi ; convert to ascii + .00050000 .00000500 .00000003 13000000 + # rd_t0 !0x39 addi ; t0 = '9' + .80020000 .00009003 13000000 + # rs1_t0 rs2_a0 @hex1 bge ; check if valid digit + .00800200 .0000A000 @hex1 63500000 + # rd_a0 rs1_a0 !7 addi ; use alpha range + .00050000 .00000500 .00007000 13000000 +:hex1 + # rs1_a1 rs2_a0 sb ; store result + .00800500 .0000A000 23000000 + # rd_a1 rs1_a1 !1 addi ; next position + .80050000 .00800500 .00001000 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Preserve_Other function +; Receives list in a0 +; Update the list in place; does not modify registers +; Uses a0 for I, a1 for I->TEXT +:Preserve_Other + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_a1 sd ; protect a1 + .00000100 .0000B000 23300000 + # rs1_sp rs2_a2 @8 sd ; protect a2 + .00000100 .0000C000 .00040000 23300000 + # rs1_sp rs2_a3 @16 sd ; protect a3 + .00000100 .0000D000 .00080000 23300000 + # rs1_sp rs2_a4 @24 sd ; protect a4 + .00000100 .0000E000 .000C0000 23300000 +:Preserve_Other_Loop + # rd_a1 rs1_a0 !24 ld ; I->EXPRESSION + .80050000 .00000500 .00008001 03300000 + # rs1_a1 @Preserve_Other_Next bnez ; IF NULL == I->EXPRESSION then preserve + .00800500 @Preserve_Other_Next 63100000 + + # rd_a1 rs1_a0 !16 ld ; I->TEXT + .80050000 .00000500 .00000001 03300000 + # rs1_a0 rs2_a1 @24 sd ; I->EXPRESSION = I->TEXT + .00000500 .0000B000 .000C0000 23300000 + +:Preserve_Other_Next + # rd_a0 rs1_a0 ld ; I = I->NEXT + .00050000 .00000500 03300000 + # rs1_a0 @Preserve_Other_Loop bnez ; Keep looping until I == NULL + .00000500 @Preserve_Other_Loop 63100000 + + # rd_a1 rs1_sp ld ; restore a1 + .80050000 .00000100 03300000 + # rd_a2 rs1_sp !8 ld ; restore a2 + .00060000 .00000100 .00008000 03300000 + # rd_a3 rs1_sp !16 ld ; restore a3 + .80060000 .00000100 .00000001 03300000 + # rd_a4 rs1_sp !24 ld ; restore a4 + .00070000 .00000100 .00008001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Print_Hex function +; Receives list in a0 +; walks the list and prints the I->EXPRESSION for all nodes followed by newline +; Uses a1 for I +:Print_Hex + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a1 @8 sd ; protect a1 + .00000100 .0000B000 .00040000 23300000 + # rs1_sp rs2_a2 @16 sd ; protect a2 + .00000100 .0000C000 .00080000 23300000 + + # rd_a1 rs1_s4 mv ; I = HEAD + .80050000 .00000A00 13000000 + +:Print_Hex_Loop + # rd_a0 rs1_a1 !8 ld ; I->TYPE + .00050000 .00800500 .00008000 03300000 + # rd_t0 !1 addi ; t0 = MACRO + .80020000 .00001000 13000000 + # rs1_a0 rs2_t0 @Print_Hex_Next beq ; Skip if MACRO = I->TYPE + .00000500 .00005000 @Print_Hex_Next 63000000 + + # rd_a0 rs1_a1 !24 ld ; Using EXPRESSION + .00050000 .00800500 .00008001 03300000 + # rd_ra $File_Print jal ; Print it + .80000000 $File_Print 6F000000 + # rd_a0 !10 addi ; \n + .00050000 .0000A000 13000000 + # rd_ra $fputc jal ; Print newline + .80000000 $fputc 6F000000 + +:Print_Hex_Next + # rd_a1 rs1_a1 ld ; Iterate to next Token + .80050000 .00800500 03300000 + # rs1_a1 @Print_Hex_Loop bnez ; Stop if NULL, otherwise keep looping + .00800500 @Print_Hex_Loop 63100000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a1 rs1_sp !8 ld ; restore a1 + .80050000 .00000100 .00008000 03300000 + # rd_a2 rs1_sp !16 ld ; restore a2 + .00060000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; File_Print function +; Receives CHAR* in a0 +; calls fputc for every non-null char +:File_Print + # rd_sp rs1_sp !-24 addi ; allocate stack + .00010000 .00000100 .000080FE 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a1 @8 sd ; protect a1 + .00000100 .0000B000 .00040000 23300000 + # rs1_sp rs2_a2 @16 sd ; protect a2 + .00000100 .0000C000 .00080000 23300000 + # rd_a1 rs1_a0 mv ; protect a0 + .80050000 .00000500 13000000 + + # rs1_a0 @File_Print_Done beqz ; Protect against nulls + .00000500 @File_Print_Done 63000000 + +:File_Print_Loop + # rd_a0 rs1_a1 lbu ; Read byte + .00050000 .00800500 03400000 + # rs1_a0 @File_Print_Done beqz ; Stop at NULL + .00000500 @File_Print_Done 63000000 + + # rd_ra $fputc jal ; print it + .80000000 $fputc 6F000000 + # rd_a1 rs1_a1 !1 addi ; S = S + 1 + .80050000 .00800500 .00001000 13000000 + # $File_Print_Loop jal ; Keep printing + $File_Print_Loop 6F000000 + +:File_Print_Done + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a1 rs1_sp !8 ld ; restore a1 + .80050000 .00000100 .00008000 03300000 + # rd_a2 rs1_sp !16 ld ; restore a2 + .00060000 .00000100 .00000001 03300000 + # rd_sp rs1_sp !24 addi ; deallocate stack + .00010000 .00000100 .00008001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; fgetc function +; Loads FILE* from s2 +; Returns -4 (EOF) or char in a0 +:fgetc + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_ra @8 sd ; protect ra + .00000100 .00001000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + + # rd_a7 !63 addi ; sys_read + .80080000 .0000F003 13000000 + # rd_a1 rs1_sp mv ; Get stack address for buffer + .80050000 .00000100 13000000 + # rd_a0 rs1_s2 mv ; read from input file + .00050000 .00000900 13000000 + # rd_a2 !1 addi ; read 1 character + .00060000 .00001000 13000000 + # ecall ; syscall + 73000000 + + # rs1_a0 @fgetc_done bnez ; Check if nothing was read + .00000500 @fgetc_done 63100000 + # rd_a2 !-4 addi ; Use -4 as EOF + .00060000 .0000C0FF 13000000 + # rs1_a1 rs2_a2 sb ; Store EOF in *a1 + .00800500 .0000C000 23000000 + +:fgetc_done + # rd_a0 rs1_a1 lb ; return char in a0 + .00050000 .00800500 03000000 + # rd_ra rs1_sp !8 ld ; restore ra + .80000000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; Malloc isn't actually required if the program being built fits in the initial memory +; However, it doesn't take much to add it. +; Requires MALLOC pointer to be initialized and a0 to have the number of desired bytes +:malloc + # rd_sp rs1_sp !-16 addi ; allocate stack + .00010000 .00000100 .000000FF 13000000 + # rs1_sp rs2_ra sd ; protect ra + .00000100 .00001000 23300000 + # rs1_sp rs2_a1 @8 sd ; protect a1 + .00000100 .0000B000 .00040000 23300000 + + # rd_a1 rs1_s1 mv ; Store the current pointer + .80050000 .00800400 13000000 + # rd_a0 rs1_a0 rs2_s1 add ; Request the number of desired bytes + .00050000 .00000500 .00009000 33000000 + # rd_a7 !214 addi ; sys_brk + .80080000 .0000600D 13000000 + # ecall ; syscall + 73000000 + # rd_s1 rs1_a0 mv ; Set our malloc pointer + .80040000 .00000500 13000000 + # rd_a0 rs1_a1 mv ; Return the pointer + .00050000 .00800500 13000000 + + # rd_ra rs1_sp ld ; restore ra + .80000000 .00000100 03300000 + # rd_a1 rs1_sp !8 ld ; restore a1 + .80050000 .00000100 .00008000 03300000 + # rd_sp rs1_sp !16 addi ; deallocate stack + .00010000 .00000100 .00000001 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +; fputc function +; receives CHAR in a0 and load FILE* from s3 +; writes char and returns +:fputc + # rd_sp rs1_sp !-32 addi ; allocate stack + .00010000 .00000100 .000000FE 13000000 + # rs1_sp rs2_a0 sd ; protect a0 + .00000100 .0000A000 23300000 + # rs1_sp rs2_ra @8 sd ; protect ra + .00000100 .00001000 .00040000 23300000 + # rs1_sp rs2_a1 @16 sd ; protect a1 + .00000100 .0000B000 .00080000 23300000 + # rs1_sp rs2_a2 @24 sd ; protect a2 + .00000100 .0000C000 .000C0000 23300000 + + # rd_a7 !64 addi ; sys_write + .80080000 .00000004 13000000 + # rd_a0 rs1_s3 mv ; write to output + .00050000 .00800900 13000000 + # rd_a1 rs1_sp mv ; Get stack address + .80050000 .00000100 13000000 + # rd_a2 !1 addi ; write 1 character + .00060000 .00001000 13000000 + # ecall ; syscall + 73000000 + + # rd_a0 rs1_sp ld ; restore a0 + .00050000 .00000100 03300000 + # rd_ra rs1_sp !8 ld ; restore ra + .80000000 .00000100 .00008000 03300000 + # rd_a1 rs1_sp !16 ld ; restore a1 + .80050000 .00000100 .00000001 03300000 + # rd_a2 rs1_sp !24 ld ; restore a2 + .00060000 .00000100 .00008001 03300000 + # rd_sp rs1_sp !32 addi ; deallocate stack + .00010000 .00000100 .00000002 13000000 + # rs1_ra jalr ; return + .00800000 67000000 + + +:Fail + ; Terminate program with 1 return code + # rd_a7 !93 addi ; sys_exit + .80080000 .0000D005 13000000 + # rd_a0 !1 addi ; Return code 1 + .00050000 .00001000 13000000 + # ecall ; exit(1) + 73000000 + +; PROGRAM END + +:terminators +# " +# " +0A 09 20 00 + +:comments +# "#;" +23 3B 00 + +:string_char +# '22 27 00' +22 27 00 + +:DEFINE_str +# "DEFINE" +44 45 46 49 4E 45 00 + +:ELF_end diff --git a/vendor/seed/riscv64/catm.hex2 b/vendor/seed/riscv64/catm.hex2 @@ -0,0 +1,171 @@ +## Copyright (C) 2021 Andrius Štikonas +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY# without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + + +## ELF Header + +:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +F3 00 ## e_machine Indicating RISC-V +01 00 00 00 ## e_version Indicating original elf + +&_start 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +%ELF_program_headers>ELF_base 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +&ELF_base 00 00 00 00 ## p_vaddr +&ELF_base 00 00 00 00 ## p_physaddr + +%ELF_end>ELF_base 00 00 00 00 ## p_filesz +%ELF_end>ELF_base 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +:ELF_text + +; Simply jump to _start +; Our main function +:_start + +# Register use: +# s1: output file descriptor +# s2: buffer +# s3: input file descriptor +# s4: number of bytes read + +# Open output file and store the FD in s1 + # rd_a7 !56 addi ; sys_openat + .80080000 .00008003 13000000 + # rd_a0 !-100 addi ; AT_FDCWD + .00050000 .0000C0F9 13000000 + # rd_sp rs1_sp !16 addi ; Prepare stack for reading output file + .00010000 .00000100 .00000001 13000000 + # rd_a1 rs1_sp ld ; Output file (argument 1) + .80050000 .00000100 03300000 + # rd_a2 !577 addi ; Prepare file as O_WRONLY|O_CREAT|O_TRUNC + .00060000 .00001024 13000000 + # rd_a3 !384 addi ; Prepare file as RW for owner only (600 in octal) + .80060000 .00000018 13000000 + # ecall ; syscall + 73000000 + # rd_s1 rs1_a0 addi ; Save fd in for later + .80040000 .00000500 13000000 + + # Prepare heap memory + # rd_a7 !214 addi ; sys_brk + .80080000 .0000600D 13000000 + # rd_a0 addi ; Get current brk + .00050000 13000000 + # ecall ; syscall + 73000000 + # rd_s2 rs1_a0 addi ; Set our malloc pointer + .00090000 .00000500 13000000 + + # rd_a1 ~0x100000 lui ; a1=1MiB + .80050000 .00001000 37000000 + # rd_a0 rs1_a0 rs2_a1 add ; Allocate 1MiB + .00050000 .00000500 .0000B000 33000000 + # ecall ; syscall + 73000000 + +:core + # rd_sp rs1_sp !8 addi ; Move stack pointer to next input file + .00010000 .00000100 .00008000 13000000 + # rd_a1 rs1_sp ld ; Get the input file name + .80050000 .00000100 03300000 + # rs1_a1 @Done beq ; This was the last file, we are done + .00800500 @Done 63000000 + + # rd_a7 !56 addi ; sys_openat + .80080000 .00008003 13000000 + # rd_a0 !-100 addi ; AT_FDCWD + .00050000 .0000C0F9 13000000 + # rd_a2 addi ; read only + .00060000 13000000 + # ecall ; syscall + 73000000 + # rd_s3 rs1_a0 addi ; protect input fd + .80090000 .00000500 13000000 + +:keep + # rd_a7 !63 addi ; sys_read + .80080000 .0000F003 13000000 + # rd_a0 rs1_s3 addi ; input fd + .00050000 .00800900 13000000 + # rd_a1 rs1_s2 addi ; read into buffer + .80050000 .00000900 13000000 + # rd_a2 ~0x100000 lui ; a2=1MiB + .00060000 .00001000 37000000 + # ecall ; syscall + 73000000 + # rd_s4 rs1_a0 addi ; actual number of bytes read + .000A0000 .00000500 13000000 + + # rd_a7 !64 addi ; sys_write + .80080000 .00000004 13000000 + # rd_a0 rs1_s1 addi ; output fd + .00050000 .00800400 13000000 + # rd_a1 rs1_s2 addi ; write from buffer + .80050000 .00000900 13000000 + # rd_a2 rs1_s4 addi ; number of bytes to write + .00060000 .00000A00 13000000 + # ecall ; syscall + 73000000 + + # rd_a2 ~0x100000 lui ; 1MiB + .00060000 .00001000 37000000 + # rs1_s4 rs2_a2 @keep beq ; keep looping if buffer was full + .00000A00 .0000C000 @keep 63000000 + # $core jal ; otherwise move to next file + $core 6F000000 + +:Done + # Terminate program with 0 return code + # rd_a7 !93 addi ; sys_exit + .80080000 .0000D005 13000000 + # rd_a0 addi ; Return code 0 + .00050000 13000000 + # ecall ; exit(0) + 73000000 + +:ELF_end diff --git a/vendor/seed/riscv64/hex0-seed b/vendor/seed/riscv64/hex0-seed Binary files differ. diff --git a/vendor/seed/riscv64/hex0.hex0 b/vendor/seed/riscv64/hex0.hex0 @@ -0,0 +1,211 @@ +## Copyright (C) 2021 Ekaitz Zarraga +## Copyright (C) 2021 Andrius Štikonas +## Copyright (C) 2021,2022 Gabriel Wicki +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +; Register use: +; s2: input fd +; s3: output fd +; s4: toggle +; s5: hold + +; Uses top of the stack as i/o buffer + +## ELF Header +#:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 for Linux +00 ## e_ident[EI_ABIVERSION] Ignored for Statically linked executables + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +F3 00 ## e_machine Indicating RISC-V +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## Flags +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +88 01 00 00 00 00 00 00 ## p_filesz +88 01 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +# :_start ; (0x0600078) + 13 0A 00 00 # rd_s4 mv ; Initialize register + 83 35 01 01 # rd_a1 rs1_sp !16 ld ; Input file name + + ; Open input file and store FD in s2 + 93 08 80 03 # rd_a7 !56 addi ; sys_openat + 13 05 C0 F9 # rd_a0 !-100 addi ; AT_FDCWD + 13 06 00 00 # rd_a2 mv ; read only + 73 00 00 00 # ecall + 13 09 05 00 # rd_s2 rs1_a0 mv ; Save fd in s2 for later + + ; Open output file and store the FD in s3 + 13 05 C0 F9 # rd_a0 !-100 addi ; AT_FDCWD + 83 35 81 01 # rd_a1 rs1_sp !24 ld ; Output file (argument 3) + 13 06 10 24 # rd_a2 !577 addi ; octal 00001101 + ; O_TRUNC 00001000 + ; O_CREAT 00000100 + ; O_WRONLY 00000001 + ; OCTAL! + + 93 06 00 1C # rd_a3 !448 addi ; Set read, write, execute permission on user + ; S_IRWXU 00700 + ; OCTAL! + + 73 00 00 00 # ecall + 93 09 05 00 # rd_s3 rs1_a0 mv ; Save fd in s3 for later + +# :next_byte ; (0x06000AC) + 93 08 F0 03 # rd_a7 !63 addi ; sys_read + 13 05 09 00 # rd_a0 rs1_s2 mv ; File descriptor + 93 05 01 00 # rd_a1 rs1_sp mv ; Buffer + 13 06 10 00 # rd_a2 !1 addi ; Size of what we want to read (set for all subsequent syscalls) + 73 00 00 00 # ecall + + ; If the file ended (0 bytes read) terminate + 63 00 05 0C # rs1_a0 @terminate beqz + # +192B + + ; Check if it's a comment + 03 05 01 00 # rd_a0 rs1_sp lb + 93 02 30 02 # rd_t0 !0x23 addi + 63 08 55 00 # rs1_a0 rs2_t0 @loop beq ; a0 eq to '#' + # +16B + 93 02 B0 03 # rd_t0 !0x3B addi + 63 04 55 00 # rs1_a0 rs2_t0 @loop beq ; a0 eq to ';' + # +8B + 6F 00 80 02 # $not_comment jal + # +40B +# :loop ; (0x06000DC) + 13 05 09 00 # rd_a0 rs1_s2 mv ; File descriptor + 73 00 00 00 # ecall ; sys_read + + ; If the file ended (0 bytes read) terminate + 63 0E 05 08 # rs1_a0 @terminate beqz + # +156B + ; Check if read byte is the end of the comment (i.e. a newline character), + ; in that case we continue processing + 03 05 01 00 # rd_a0 rs1_sp lb + 93 02 A0 00 # rd_t0 !0xA addi + E3 0E 55 FA # rs1_a0 rs2_t0 @next_byte beq ; a0 eq to \n + # -68B + 93 02 D0 00 # rd_t0 !0xD addi + E3 0A 55 FA # rs1_a0 rs2_t0 @next_byte beq ; a0 eq to \r + # -76B + 6F F0 1F FE # $loop jal +# :not_comment ; (0x0600100) + ; Check if it's a hex character: + ; in the case it's not, ignores and reads next byte + 03 05 01 00 # rd_a0 rs1_sp lb + + ; Is it between '0' and '9'? + 93 02 00 03 # rd_t0 !48 addi ; '0' character + 63 4A 55 00 # rs1_a0 rs2_t0 @uppercase_alpha blt + # +20B + 93 02 90 03 # rd_t0 !57 addi ; '9' character + 63 C6 A2 00 # rs1_t0 rs2_a0 @uppercase_alpha blt + # +12B + 13 05 05 FD # rd_a0 rs1_a0 !-48 addi + 6F 00 00 03 # $hex_read jal + # +48B +# :uppercase_alpha ; (0x060011C) + ; Is it between 'A' and 'F'? + 93 02 10 04 # rd_t0 !65 addi ; 'A' character + 63 4A 55 00 # rs1_a0 rs2_t0 @lowercase_alpha blt + # +20B + 93 02 60 04 # rd_t0 !70 addi ; 'F' character + 63 C6 A2 00 # rs1_t0 rs2_a0 @lowercase_alpha blt + # +12B + 13 05 95 FC # rd_a0 rs1_a0 !-55 addi + 6F 00 80 01 # $hex_read jal + # +24B +# :lowercase_alpha ; (0x0600134) + ; Is it between 'a' and 'f'? + 93 02 10 06 # rd_t0 !97 addi ; 'a' character + E3 4A 55 F6 # rs1_a0 rs2_t0 @next_byte blt ; Not hex, continue reading + # -140B + 93 02 60 06 # rd_t0 !102 addi ; 'f' character + E3 C6 A2 F6 # rs1_t0 rs2_a0 @next_byte blt ; Not hex, continue reading + # -148B + 13 05 95 FA # rd_a0 rs1_a0 !-87 addi +# :hex_read ; (0x0600148) + ; END check hex -- leaves the half byte in a0 + + 63 18 0A 00 # rs1_s4 @combine bnez ; if toggle != 0 -> combine + # +16B + ; Toggle == 0, we need to prepare for later + 93 0A 05 00 # rd_s5 rs1_a0 mv ; Load hold + + 13 0A 10 00 # rd_s4 !1 addi ; Set toggle + 6F F0 9F F5 # $next_byte jal ; Read next byte + # -168B + +# :combine ; (0x0600158) + ; Combine half bytes + 93 95 4A 00 # rd_a1 rs1_s5 rs2_x4 slli ; Shift logical left 4 times + 33 05 B5 00 # rd_a0 rs1_a0 rs2_a1 add ; Combine two half bytes + ; Leaves the full byte in a0 + + ; Values are combined, now we write in the file + 23 00 A1 00 # rs1_sp rs2_a0 sb ; Store prepared byte in buffer + 93 08 00 04 # rd_a7 !64 addi ; sys_write + 13 85 09 00 # rd_a0 rs1_s3 mv ; file descriptor (stdout) + 93 05 01 00 # rd_a1 rs1_sp mv ; string address + 73 00 00 00 # ecall + + ; Update globals + 13 0A 00 00 # rd_s4 mv ; Clear toggle + 93 0A 00 00 # rd_s5 mv ; Clear hold + + ; Read next byte + 6F F0 1F F3 # $next_byte jal + # -208B + +# :terminate ; (0x600180) + ; Terminate program with 0 return code + 93 08 D0 05 # rd_a7 !93 addi ; sys_exit + 73 00 00 00 # ecall +# PROGRAM END + +# :ELF_end ; (0x600188) diff --git a/vendor/seed/riscv64/hex1.hex0 b/vendor/seed/riscv64/hex1.hex0 @@ -0,0 +1,647 @@ +## Copyright (C) 2021 Andrius Štikonas +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +## ELF Header +# :ELF_base ; (0x600000) +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 for Linux +00 ## e_ident[EI_ABIVERSION] Ignored for Statically linked executables + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +F3 00 ## e_machine Indicating RISC-V +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) # TODO +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## Flags +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +C1 04 00 00 00 00 00 00 ## p_filesz +C1 04 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +#:ELF_text + +; Register use: +; s2: input fd +; s3: output fd +; s4: toggle +; s5: hold +; s6: ip +; s7: tempword +; s8: shiftregister + +; Our main function +# :_start ; (0x0600078) + + 03 36 01 01 # rd_a2 rs1_sp !16 ld ; Input file name + + ; Initialize globals + 13 0A F0 FF # rd_s4 !-1 addi ; Toggle + 93 0A 00 00 # rd_s5 addi ; Hold + 13 0B 00 00 # rd_s6 addi ; Instruction Pointer + + ; Open input file and store FD in s2 + 93 08 80 03 # rd_a7 !56 addi ; sys_openat + 13 05 C0 F9 # rd_a0 !-100 addi ; AT_FDCWD + 93 05 06 00 # rd_a1 rs1_a2 mv ; file name + 13 06 00 00 # rd_a2 addi ; read only + 73 00 00 00 # ecall ; syscall + 63 4C 05 40 # rs1_a0 @Fail bltz ; Error opening file + # +1048 + 13 09 05 00 # rd_s2 rs1_a0 mv ; Save fd in for later + + ; Set default FD for output file to stdout + 93 09 10 00 # rd_s3 !1 addi + + ; If we only have 2 arguments, don't use the third (it's not set) + 93 02 20 00 # rd_t0 !2 addi + 03 35 01 00 # rd_a0 rs1_sp ld ; Get number of the args + 63 42 55 40 # rs1_a0 rs2_t0 @Fail blt ; No input file provided + # +1028B + 63 00 55 02 # rs1_a0 rs2_t0 @after_open beq ; No output file provided. Use stdout + # +32B + + ; Open output file and store the FD in s3 + 93 08 80 03 # rd_a7 !56 addi ; sys_openat + 13 05 C0 F9 # rd_a0 !-100 addi ; AT_FDCWD + 83 35 81 01 # rd_a1 rs1_sp !24 ld ; Output file (argument 3) + 13 06 10 24 # rd_a2 !00001101 addi ; decimal 577 + ; O_TRUNC 00001000 + ; O_CREAT 00000100 + ; O_WRONLY 00000001 + ; OCTAL! + 93 06 00 1C # rd_a3 !00700 addi ; Set read, write, execute permission on user + ; S_IRWXU 00700 + ; OCTAL! + 73 00 00 00 # ecall ; syscall + 93 09 05 00 # rd_s3 rs1_a0 mv ; Save fd in for later + +# :after_open ; (0x06000D4) + EF 00 40 03 # rd_ra $First_pass jal ; First pass + # +52B + + ; Rewind input file + 93 08 E0 03 # rd_a7 !62 addi ; sys_lseek + 13 05 09 00 # rd_a0 rs1_s2 mv ; Input file descriptor + 93 05 00 00 # rd_a1 mv ; Set offset to zero + 13 06 00 00 # rd_a2 mv ; Set whence to zero + 73 00 00 00 # ecall ; syscall + + ; Initialize globals + 13 0A F0 FF # rd_s4 !-1 addi ; Toggle + 93 0A 00 00 # rd_s5 addi ; Hold + 13 0B 00 00 # rd_s6 addi ; Instruction Pointer + 93 0B 00 00 # rd_s7 addi ; tempword + 13 0C 00 00 # rd_s8 addi ; Shift register + + EF 00 00 07 # rd_ra $Second_pass jal ; Now do the second pass + # +112B + + 6F 00 40 3A # $Done jal ; We are done + # +392B + +; First pass loop to determine addresses of labels +# :First_pass ; (0x0600108) + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + +# :First_pass_loop ; (0x0600110) + EF 00 C0 2D # rd_ra $Read_byte jal ; Get another byte + # +732B + + ; Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi + 63 06 65 04 # rs1_a0 rs2_t1 @First_pass_done beq + # +76B + + ; Check for : + 13 03 A0 03 # rd_t1 !0x3a addi + 63 14 65 00 # rs1_a0 rs2_t1 @First_pass_0 bne + # +8B + EF 00 C0 32 # rd_ra $StoreLabel jal ; Store this label + # +812B + +# :First_pass_0 ; (0x0600128) + ; Check for ! + 13 03 10 02 # rd_t1 !0x21 addi + 63 08 65 02 # rs1_a0 rs2_t1 @Throwaway_token beq + # +48B + + ; Check for @ + 13 03 00 04 # rd_t1 !0x40 addi + 63 04 65 02 # rs1_a0 rs2_t1 @Throwaway_token beq + # +40B + + ; Check for $ + 13 03 40 02 # rd_t1 !0x24 addi + 63 00 65 02 # rs1_a0 rs2_t1 @Throwaway_token beq + # +32B + + ; Check for ~ + 13 03 E0 07 # rd_t1 !0x7e addi + 63 0C 65 00 # rs1_a0 rs2_t1 @Throwaway_token beq + # +24B + + 93 05 F0 FF # rd_a1 !-1 addi ; write = false + EF 00 C0 19 # rd_ra $DoByte jal ; Deal with everything else + # +412B + + 13 03 C0 FF # rd_t1 !-4 addi ; Deal with EOF + 63 08 65 00 # rs1_a0 rs2_t1 @First_pass_done beq + # +16B + + 6F F0 9F FB # $First_pass_loop jal ; Keep looping + # -72B + +# :Throwaway_token ; (0x060015C) + ; Deal with Pointer to label + EF 00 00 29 # rd_ra $Read_byte jal ; Drop the char + # +656B + 6F F0 1F FB # $First_pass_loop jal ; Loop again + # -80B + +# :First_pass_done ; (0x0600164) + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +# :Second_pass ; (0x0600170) + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + +# :Second_pass_loop ; (0x0600178) + EF 00 40 27 # rd_ra $Read_byte jal ; Read another byte + # +628B + + ; Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi ; Deal with EOF + 63 0E 65 14 # rs1_a0 rs2_t1 @Second_pass_done beq + # +348B + + ; Drop the label + 13 03 A0 03 # rd_t1 !0x3a addi + 63 16 65 00 # rs1_a0 rs2_t1 @Second_pass_0 bne + # +12B + + EF 00 00 26 # rd_ra $Read_byte jal ; Read the label + # +608B + 6F F0 9F FE # $Second_pass_loop jal ; Continue looping + # -24B + +# :Second_pass_0 ; (0x0600194) + ; Check for ! + 13 03 10 02 # rd_t1 !0x21 addi + 63 08 65 02 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + # +48B + + ; Check for @ + 13 03 00 04 # rd_t1 !0x40 addi + 63 04 65 02 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + # +40B + + ; Check for $ + 13 03 40 02 # rd_t1 !0x24 addi + 63 00 65 02 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + # +32B + + ; Check for ~ + 13 03 E0 07 # rd_t1 !0x7e addi + 63 0C 65 00 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + # +24B + + ; Deal with everything else + 93 05 00 00 # rd_a1 mv ; write = true + EF 00 00 13 # rd_ra $DoByte jal ; Process our char + # +304B + + # Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi + 63 0E 65 10 # rs1_a0 rs2_t1 @Second_pass_done beq ; We are done + # +284B + + 6F F0 5F FB # $Second_pass_loop jal ; continue looping + # -76B + +# :UpdateShiftRegister ; (0x06001C8) + 93 05 05 00 # rd_a1 rs1_a0 mv ; Store label prefix + EF 00 C0 25 # rd_ra $Get_table_target jal ; Get target + # +604B + 03 35 05 00 # rd_a0 rs1_a0 ld ; Dereference pointer + 33 05 65 41 # rd_a0 rs1_a0 rs2_s6 sub ; target - ip + + ; Check for ! + 13 03 10 02 # rd_t1 !0x21 addi + 63 80 65 02 # rs1_a1 rs2_t1 @UpdateShiftRegister_I beq + # +32B + + ; Check for @ + 13 03 00 04 # rd_t1 !0x40 addi + 63 8A 65 02 # rs1_a1 rs2_t1 @UpdateShiftRegister_B beq + # +52B + + ; Check for $ + 13 03 40 02 # rd_t1 !0x24 addi + 63 8A 65 06 # rs1_a1 rs2_t1 @UpdateShiftRegister_J beq + # +116B + + ; Check for ~ + 13 03 E0 07 # rd_t1 !0x7e addi + 63 88 65 0A # rs1_a1 rs2_t1 @UpdateShiftRegister_U beq + # +176B + + 6F F0 1F F8 # $Second_pass_loop jal ; continue looping + # -128B + +# :UpdateShiftRegister_I ; (0x06001FC) + ; Corresponds to RISC-V I format + 13 05 45 00 # rd_a0 rs1_a0 !4 addi ; add 4 due to this being 2nd part of auipc combo + + 37 13 00 00 # rd_t1 ~0xfff lui ; load higher bits + 1B 03 F3 FF # rd_t1 rs1_t1 !0xfff addiw + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; (value & 0xfff) + 93 1B 43 01 # rd_s7 rs1_t1 rs2_x20 slli ; tempword = (value & 0xfff) << 20 + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + 6F F0 5F F6 # $Second_pass_loop jal ; continue looping + # -156B + +# :UpdateShiftRegister_B ; (0x0600218) + ; Corresponds to RISC-V B format + + ; tempword = ((value & 0x1e) << 7) ; imm[4:1] + ; | ((value & 0x7e0) << (31 - 11)) ; imm[10:5] + ; | ((value & 0x800) >> 4) ; imm[11] + ; | ((value & 0x1000) << (31 - 12)) ; imm[12] + + 13 03 E0 01 # rd_t1 !0x1e addi + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x1e + 93 12 73 00 # rd_t0 rs1_t1 rs2_x7 slli ; tempword = (value & 0x1e) << 7 + + 13 03 00 7E # rd_t1 !0x7e0 addi + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x7e0 + 13 13 43 01 # rd_t1 rs1_t1 rs2_x20 slli ; (value & 0x7e0) << (31 - 11) + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 13 00 00 # rd_t1 ~0x800 lui ; load higher bits + 1B 03 03 80 # rd_t1 rs1_t1 !0x800 addiw + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x800 + 13 53 43 00 # rd_t1 rs1_t1 rs2_x4 srli ; (value & 0x800) >> 4 + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 13 00 00 # rd_t1 ~0x1000 lui ; load higher bits + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x1000 + 13 13 33 01 # rd_t1 rs1_t1 rs2_x19 slli ; (value & 0x1000) << (31 - 12) + B3 EB 62 00 # rd_s7 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + 6F F0 DF F1 # $Second_pass_loop jal ; continue looping + # -228B + +# :UpdateShiftRegister_J ; (0x0600260) + ; Corresponds to RISC-V J format + + ; tempword = ((value & 0x7fe) << (30 - 10)) ; imm[10:1] + ; | ((value & 0x800) << (20 - 11)) ; imm[11] + ; | ((value & 0xff000)) ; imm[19:12] + ; | ((value & 0x100000) << (31 - 20)) ; imm[20] + + 13 03 E0 7F # rd_t1 !0x7fe addi + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x7fe + 93 12 43 01 # rd_t0 rs1_t1 rs2_x20 slli ; tempword = (value & 0x7fe) << 20 + + 37 13 00 00 # rd_t1 ~0x800 lui ; load higher bits + 1B 03 03 80 # rd_t1 rs1_t1 !0x800 addiw + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x800 + 13 13 93 00 # rd_t1 rs1_t1 rs2_x9 slli ; (value & 0x800) << (20 - 11) + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 F3 0F 00 # rd_t1 ~0xff000 lui ; load higher bits + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0xff000 + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 03 10 00 # rd_t1 ~0x100000 lui ; load higher bits + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x100000 + 13 13 B3 00 # rd_t1 rs1_t1 rs2_x11 slli ; (value & 0x100000) << (31 - 20) + B3 EB 62 00 # rd_s7 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + 6F F0 9F ED # $Second_pass_loop jal ; continue looping + # -296B + +# :UpdateShiftRegister_U ; (0x06002A4) + ; Corresponds to RISC-V U format + ; if value is 0x800 or more we have to add 11-th bit (0x1000) to compensate for signed extension + + B7 12 00 00 # rd_t0 ~0x800 lui ; load higher bits + 9B 82 02 80 # rd_t0 rs1_t0 !0x800 addiw + 37 13 00 00 # rd_t1 ~0xfff lui ; load higher bits + 1B 03 F3 FF # rd_t1 rs1_t1 !0xfff addiw + + ; We are outside 31-bit that ~ can normally load + B7 03 10 00 # rd_t2 ~0x100000 lui ; load 0xfffff000 + 9B 83 F3 FF # rd_t2 rs1_t2 !-1 addiw ; load 0xfffff000 + 93 93 C3 00 # rd_t2 rs1_t2 rs2_x12 slli ; load 0xfffff000 + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0xfff + B3 7B 75 00 # rd_s7 rs1_a0 rs2_t2 and ; value & 0xfffff000 + 63 46 53 00 # rs1_t1 rs2_t0 @UpdateShiftRegister_U_small blt + # +12B + + # Deal with sign extension: add 0x1000 + B7 12 00 00 # rd_t0 ~0x1000 lui ; load higher bits + BB 8B 72 01 # rd_s7 rs1_t0 rs2_s7 addw ; (value & 0xfffff000) + 0x1000 + +# :UpdateShiftRegister_U_small ; (0x06002D4) + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + 6F F0 1F EA # $Second_pass_loop jal ; continue looping + # -352B + +# :Second_pass_done ; (0x06002DC) + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + + +; DoByte function +; Receives: +; character in a0 +; bool write in a1 +; Does not return anything +# :DoByte ; (0x06002E8) + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + + EF 00 00 05 # rd_ra $hex jal ; Process hex, store it in a6 + # +80B + + 63 40 08 04 # rs1_a6 @DoByte_Done bltz ; Deal with EOF and unrecognized characters + # +64B + + 63 1A 0A 02 # rs1_s4 @DoByte_NotToggle bnez ; Check if toggle is set + # +56B + + ; toggle = true + 63 92 05 02 # rs1_a1 @DoByte_1 bnez ; check if we have to write + # +36B + + ; write = true + ; We calculate (hold * 16) + hex(c) ^ sr_nextb() + ; First, calculate new shiftregister + 93 02 F0 0F # rd_t0 !0xff addi + B3 72 5C 00 # rd_t0 rs1_s8 rs2_t0 and ; sr_nextb = shiftregister & 0xff + 13 5C 8C 00 # rd_s8 rs1_s8 rs2_x8 srli ; shiftregister >> 8 + + B3 C2 02 01 # rd_t0 rs1_t0 rs2_a6 xor ; hex(c) ^ sr_nextb + 13 93 4A 00 # rd_t1 rs1_s5 rs2_x4 slli ; hold << 4 + 33 85 62 00 # rd_a0 rs1_t0 rs2_t1 add ; (hold << 4) + hex(c) ^ sr_nextb() + EF 00 40 15 # rd_ra $fputc jal ; print it + # +340B + 63 0C 05 18 # rs1_a0 @Fail beqz ; Fail if nothing was written + # +408B + +# :DoByte_1 ; (0x0600320) + 13 0B 1B 00 # rd_s6 rs1_s6 !1 addi ; Increment IP + 93 0A 00 00 # rd_s5 mv ; hold = 0 + 6F 00 80 00 # $DoByte_FlipToggle jal ; return + # +8B + +# :DoByte_NotToggle ; (0x060032C) + 93 0A 08 00 # rd_s5 rs1_a6 mv ; hold = hex(c) + +# :DoByte_FlipToggle ; (0x0600330) + 13 4A FA FF # rd_s4 rs1_s4 not ; Flip the toggle + +# :DoByte_Done ; (0x0600334) + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Convert ASCII hex characters into binary representation, e.g. 'a' -> 0xA +; Receives: +; character in a0 +; Returns: +; a6 with character's hex value. +# :hex ; (0x0600340) + 13 01 01 FF # rd_sp rs1_sp !-16 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + 23 34 B1 00 # rs1_sp rs2_a1 @8 sd ; protect a1 + + ; Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi + 63 06 65 08 # rs1_a0 rs2_t1 @hex_return beq + # +140B + + ; deal with line comments starting with # + 13 03 30 02 # rd_t1 !0x23 addi + 63 06 65 06 # rs1_a0 rs2_t1 @ascii_comment beq ; a0 eq to '#' + # +108B + + ; deal with line comments starting with ; + 13 03 B0 03 # rd_t1 !0x3b addi + 63 02 65 06 # rs1_a0 rs2_t1 @ascii_comment beq ; a0 eq to ';' + # +100B + + ; deal all ascii less than 0 + 13 03 00 03 # rd_t1 !0x30 addi + 63 4A 65 04 # rs1_a0 rs2_t1 @ascii_other blt + # +84B + + ; deal with 0-9 + 13 03 A0 03 # rd_t1 !0x3a addi + 63 44 65 02 # rs1_a0 rs2_t1 @ascii_num blt + # +40B + + ; deal with all ascii less than A + 13 03 10 04 # rd_t1 !0x41 addi + 63 42 65 04 # rs1_a0 rs2_t1 @ascii_other blt + # +68B + + ; deal with A-F + 13 03 70 04 # rd_t1 !0x47 addi + 63 48 65 02 # rs1_a0 rs2_t1 @ascii_high blt + # +48B + + ; deal with all ascii less than a + 13 03 10 06 # rd_t1 !0x61 addi + 63 4A 65 02 # rs1_a0 rs2_t1 @ascii_other blt + # +52B + + ; deal with a-f + 13 03 70 06 # rd_t1 !0x67 addi + 63 4A 65 00 # rs1_a0 rs2_t1 @ascii_low blt + # +20B + + ; The rest that remains needs to be ignored + 6F 00 80 02 # $ascii_other jal + # +40B + +# :ascii_num ; (0x0600398) + 13 03 00 03 # rd_t1 !0x30 addi ; '0' -> 0 + 33 08 65 40 # rd_a6 rs1_a0 rs2_t1 sub + 6F 00 C0 03 # $hex_return jal ; return + # +60B +# :ascii_low ; (0x06003A4) + 13 03 70 05 # rd_t1 !0x57 addi ; 'a' -> 0xA + 33 08 65 40 # rd_a6 rs1_a0 rs2_t1 sub + 6F 00 00 03 # $hex_return jal ; return + # +48B +# :ascii_high ; (0x06003B0) + 13 03 70 03 # rd_t1 !0x37 addi ; 'A' -> 0xA + 33 08 65 40 # rd_a6 rs1_a0 rs2_t1 sub + 6F 00 40 02 # $hex_return jal ; return + # +36B +# :ascii_other ; (0x06003BC) + 13 08 F0 FF # rd_a6 !-1 addi ; Return -1 + 6F 00 C0 01 # $hex_return jal ; return + # +28B +# :ascii_comment ; (0x06003C4) ; Read the comment until newline + EF 00 80 02 # rd_ra $Read_byte jal + # +40B + 13 03 D0 00 # rd_t1 !0xd addi ; CR + 63 06 65 00 # rs1_a0 rs2_t1 @ascii_comment_cr beq + # +12B + 13 03 A0 00 # rd_t1 !0xa addi ; LF + E3 18 65 FE # rs1_a0 rs2_t1 @ascii_comment bne ; Keep reading comment + # -16B +# :ascii_comment_cr ; (0x06003D8) + 13 08 F0 FF # rd_a6 !-1 addi ; Return -1 +# :hex_return ; (0x06003DC) + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 83 35 81 00 # rd_a1 rs1_sp !8 ld ; restore a1 + 13 01 01 01 # rd_sp rs1_sp !16 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Read byte into a0 +# :Read_byte ; (0x06003EC) + 13 01 01 FF # rd_sp rs1_sp !-16 addi ; Allocate stack + 23 34 B1 00 # rs1_sp rs2_a1 @8 sd ; protect a1 + + 93 08 F0 03 # rd_a7 !63 addi ; sys_read + 13 05 09 00 # rd_a0 rs1_s2 mv ; File descriptor + 93 05 01 00 # rd_a1 rs1_sp mv ; Get stack address for buffer + 13 00 00 00 # nop ; no-op + 13 06 10 00 # rd_a2 !1 addi ; Size of what we want to read + 73 00 00 00 # ecall ; syscall + + 63 06 05 00 # rs1_a0 @Read_byte_1 beqz ; Deal with EOF + # +12B + 03 85 05 00 # rd_a0 rs1_a1 lb ; Dereference pointer + + 6F 00 80 00 # $Read_byte_done jal ; return + # +8B + +# :Read_byte_1 ; (0x0600418) + 13 05 C0 FF # rd_a0 !-4 addi ; Put EOF in a0 +# :Read_byte_done ; (0x060041C) + 83 35 81 00 # rd_a1 rs1_sp !8 ld ; restore a1 + 13 01 01 01 # rd_sp rs1_sp !16 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Reads a byte and calculates table address +; Returns a pointer in a0 +# :Get_table_target ; (0x0600428) + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + + EF F0 DF FB # rd_ra $Read_byte jal ; Get single char label + # -68B + 13 15 35 00 # rd_a0 rs1_a0 rs2_x3 slli ; Each label in table takes 8 bytes to store + 97 02 00 00 # rd_t0 ~table auipc ; Load address of table + 93 82 82 08 # rd_t0 rs1_t0 !table addi ; into register t0 + # +136B + 33 05 55 00 # rd_a0 rs1_a0 rs2_t0 add ; Calculate offset + + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +# :StoreLabel ; (0x0600450) + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + + EF F0 1F FD # rd_ra $Get_table_target jal + # -48B + 23 30 65 01 # rs1_a0 rs2_s6 sd ; Store ip into table target + + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; fputc function +; Receives CHAR in a0 +; Writes and returns number of bytes written in a0 +# :fputc ; (0x060046C) + 13 01 01 FE # rd_sp rs1_sp !-32 addi ; allocate stack + 23 30 A1 00 # rs1_sp rs2_a0 sd ; protect a0 + 23 34 11 00 # rs1_sp rs2_ra @8 sd ; protect ra + 23 38 B1 00 # rs1_sp rs2_a1 @16 sd ; protect a1 + 23 3C C1 00 # rs1_sp rs2_a2 @24 sd ; protect a2 + + 93 08 00 04 # rd_a7 !64 addi ; sys_write + 13 85 09 00 # rd_a0 rs1_s3 mv ; write to output + 93 05 01 00 # rd_a1 rs1_sp mv ; Get stack address + 13 06 10 00 # rd_a2 !1 addi ; write 1 character + 73 00 00 00 # ecall ; syscall + + 83 30 81 00 # rd_ra rs1_sp !8 ld ; restore ra + 83 35 01 01 # rd_a1 rs1_sp !16 ld ; restore a1 + 03 36 81 01 # rd_a2 rs1_sp !24 ld ; restore a2 + 13 01 01 02 # rd_sp rs1_sp !32 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +# :Done ; (0x06004A8) + ; Terminate program with 0 return code + 93 08 D0 05 # rd_a7 !93 addi ; sys_exit + 13 05 00 00 # rd_a0 mv ; Return code 0 + 73 00 00 00 # ecall ; exit(0) +# :Fail ; (0x06004B4) + ; Terminate program with 1 return code + 93 08 D0 05 # rd_a7 !93 addi ; sys_exit + 13 05 10 00 # rd_a0 !1 addi ; Return code 1 + 73 00 00 00 # ecall ; exit(1) +# PROGRAM END + +# :table; (0x06004C0) + 00 diff --git a/vendor/seed/riscv64/hex2.hex1 b/vendor/seed/riscv64/hex2.hex1 @@ -0,0 +1,903 @@ +## Copyright (C) 2021 Andrius Štikonas +## This file is part of stage0. +## +## stage0 is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## stage0 is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with stage0. If not, see <http://www.gnu.org/licenses/>. + +## ELF Header + +#:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +F3 00 ## e_machine Indicating RISC-V +01 00 00 00 ## e_version Indicating original elf + +78 00 60 00 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +40 00 00 00 00 00 00 00 ## e_phoff Address of program header table +00 00 00 00 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +00 00 ## e_shentsize size of a section header table +00 00 ## e_shnum number of entries in section table + +00 00 ## e_shstrndx index of the section names + +## Program Header +#:ELF_program_headers +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +00 00 60 00 00 00 00 00 ## p_vaddr +00 00 60 00 00 00 00 00 ## p_physaddr + +EC 07 00 00 00 00 00 00 ## p_filesz +EC 07 00 00 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +#:ELF_text +; Register use: +; s1: jump table +; s2: input fd +; s3: output fd +; s4: toggle +; s5: hold +; s6: ip +; s7: tempword +; s8: shiftregister +; s9: malloc pointer +; s10: updates + +; Struct format: (size 24) +; next => 0 ; Next element in linked list +; target => 8 ; Target (ip) +; name => 16 ; Label name + +; Our main function +#:_start + + 03 36 01 01 # rd_a2 rs1_sp !16 ld ; Input file name + + ; Initialize globals + 13 0A F0 FF # rd_s4 !-1 addi ; Toggle + 93 0A 00 00 # rd_s5 addi ; Hold + 37 0B 60 00 # rd_s6 ~0x600000 lui ; Instruction Pointer + + ; Open input file and store FD in s2 + 93 08 80 03 # rd_a7 !56 addi ; sys_openat + 13 05 C0 F9 # rd_a0 !-100 addi ; AT_FDCWD + 93 05 06 00 # rd_a1 rs1_a2 mv ; file name + 13 06 00 00 # rd_a2 addi ; read only + 73 00 00 00 # ecall ; syscall + @F 63 40 05 00 # rs1_a0 @Fail bltz ; Error opening file + 13 09 05 00 # rd_s2 rs1_a0 mv ; Save fd in for later + + ; Set default FD for output file to stdout + 93 09 10 00 # rd_s3 !1 addi + + ; If we only have 2 arguments, don't use the third (it's not set) + 93 02 20 00 # rd_t0 !2 addi + 03 35 01 00 # rd_a0 rs1_sp ld ; Get number of the args + @F 63 40 55 00 # rs1_a0 rs2_t0 @Fail blt ; No input file provided + @a 63 00 55 00 # rs1_a0 rs2_t0 @after_open beq ; No output file provided. Use stdout + + ; Open output file and store the FD in s3 + 93 08 80 03 # rd_a7 !56 addi ; sys_openat + 13 05 C0 F9 # rd_a0 !-100 addi ; AT_FDCWD + 83 35 81 01 # rd_a1 rs1_sp !24 ld ; Output file (argument 3) + 13 06 10 24 # rd_a2 !00001101 addi ; decimal 577 + ; O_TRUNC 00001000 + ; O_CREAT 00000100 + ; O_WRONLY 00000001 + ; OCTAL! + 93 06 00 1C # rd_a3 !00700 addi ; Set read, write, execute permission on user + ; S_IRWXU 00700 + ; OCTAL! + 73 00 00 00 # ecall ; syscall + 93 09 05 00 # rd_s3 rs1_a0 mv ; Save fd in for later + +:a ;after_open + ; Prepare heap memory + 93 08 60 0D # rd_a7 !214 addi ; sys_brk + 13 05 00 00 # rd_a0 addi ; Get current brk + 73 00 00 00 # ecall ; syscall + 93 0C 05 00 # rd_s9 rs1_a0 addi ; Set our malloc pointer + + B7 05 10 00 # rd_a1 ~0x100000 lui + 33 05 B5 00 # rd_a0 rs1_a0 rs2_a1 add ; Request the 1 MiB + 93 08 60 0D # rd_a7 !214 addi ; sys_brk + 73 00 00 00 # ecall ; syscall + + $C EF 00 00 00 # rd_ra $ClearScratch jal ; Zero scratch + $f EF 00 00 00 # rd_ra $First_pass jal ; First pass + + ; Rewind input file + 93 08 E0 03 # rd_a7 !62 addi ; sys_lseek + 13 05 09 00 # rd_a0 rs1_s2 mv ; Input file descriptor + 93 05 00 00 # rd_a1 mv ; Set offset to zero + 13 06 00 00 # rd_a2 mv ; Set whence to zero + 73 00 00 00 # ecall ; syscall + + ; Initialize globals + 13 0A F0 FF # rd_s4 !-1 addi ; Toggle + 93 0A 00 00 # rd_s5 addi ; Hold + 37 0B 60 00 # rd_s6 ~0x600000 lui ; Instruction Pointer + 93 0B 00 00 # rd_s7 addi ; tempword + 13 0C 00 00 # rd_s8 addi ; Shift register + + $X EF 00 00 00 # rd_ra $Second_pass jal ; Now do the second pass + + ; Terminate program with 0 return code + 93 08 D0 05 # rd_a7 !93 addi ; sys_exit + 13 05 00 00 # rd_a0 mv ; Return code 0 + 73 00 00 00 # ecall ; exit(0) + +; First pass loop to determine addresses of labels +:f ;First_pass + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + +:1 ;First_pass_loop + $R EF 00 00 00 # rd_ra $Read_byte jal ; Get another byte + + ; Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi + @3 63 00 65 00 # rs1_a0 rs2_t1 @First_pass_done beq + + ; Check for : + 13 03 A0 03 # rd_t1 !0x3a addi + @L 63 00 65 00 # rs1_a0 rs2_t1 @StoreLabel beq ; Store this label + + ; Check for . + 13 03 E0 02 # rd_t1 !0x2e addi + @w 63 00 65 00 # rs1_a0 rs2_t1 @First_pass_UpdateWord beq + + ; Check for % + 13 03 50 02 # rd_t1 !0x25 addi + @p 63 00 65 00 # rs1_a0 rs2_t1 @First_pass_pointer beq + + ; Check for & + 13 03 60 02 # rd_t1 !0x26 addi + @p 63 00 65 00 # rs1_a0 rs2_t1 @First_pass_pointer beq + + ; Check for ! + 13 03 10 02 # rd_t1 !0x21 addi + @T 63 00 65 00 # rs1_a0 rs2_t1 @Throwaway_token beq + + ; Check for @ + 13 03 00 04 # rd_t1 !0x40 addi + @T 63 00 65 00 # rs1_a0 rs2_t1 @Throwaway_token beq + + ; Check for $ + 13 03 40 02 # rd_t1 !0x24 addi + @T 63 00 65 00 # rs1_a0 rs2_t1 @Throwaway_token beq + + ; Check for ~ + 13 03 E0 07 # rd_t1 !0x7e addi + @T 63 00 65 00 # rs1_a0 rs2_t1 @Throwaway_token beq + + ; Check for < + 13 03 C0 03 # rd_t1 !0x3c addi + 93 05 F0 FF # rd_a1 !-1 addi ; write = false + @A 63 00 65 00 # rs1_a0 rs2_t1 @PadToAlign beq + + 93 05 F0 FF # rd_a1 !-1 addi ; write = false + 13 06 F0 FF # rd_a2 !-1 addi ; update = false + $D EF 00 00 00 # rd_ra $DoByte jal ; Deal with everything else + + 13 03 C0 FF # rd_t1 !-4 addi ; Deal with EOF + @3 63 00 65 00 # rs1_a0 rs2_t1 @First_pass_done beq + + $1 6F 00 00 00 # $First_pass_loop jal ; Keep looping + +:T ;Throwaway_token + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; get scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; Read token + $C EF 00 00 00 # rd_ra $ClearScratch jal ; Throw away token + $1 6F 00 00 00 # $First_pass_loop jal ; Loop again + +:p ;First_pass_pointer + 13 0B 4B 00 # rd_s6 rs1_s6 !4 addi ; Update ip + ; Deal with Pointer to label + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; Using scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; Read token + $C EF 00 00 00 # rd_ra $ClearScratch jal ; Throw away token + 13 03 E0 03 # rd_t1 !0x3e addi ; Check for '>' + @1 63 10 65 00 # rs1_a0 rs2_t1 @First_pass_loop bne ; Loop again + + ; Deal with %label>label case + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; Using scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; Read token + $C EF 00 00 00 # rd_ra $ClearScratch jal ; Throw away token + $1 6F 00 00 00 # $First_pass_loop jal ; Loop again + +:w ;First_pass_UpdateWord + 13 0D 00 00 # rd_s10 addi ; updates = 0 + 93 0B 00 00 # rd_s7 addi ; tempword = 0 + 93 07 40 00 # rd_a5 !4 addi ; a5 = 4 +:4 ;First_pass_UpdateWord_loop + $R EF 00 00 00 # rd_ra $Read_byte jal ; Read another byte into a0 + + 93 05 F0 FF # rd_a1 !-1 addi ; write = false + 13 06 00 00 # rd_a2 addi ; update = true + $D EF 00 00 00 # rd_ra $DoByte jal ; Process byte + @4 63 40 FD 00 # rs1_s10 rs2_a5 @First_pass_UpdateWord_loop blt ; loop 4 times + + 13 0B CB FF # rd_s6 rs1_s6 !-4 addi ; ip = ip - 4 + + $1 6F 00 00 00 # $First_pass_loop jal ; Loop again + +:3 ;First_pass_done + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +:X ;Second_pass + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + +:5 ;Second_pass_loop + $R EF 00 00 00 # rd_ra $Read_byte jal ; Read another byte + + ; Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi ; Deal with EOF + @6 63 00 65 00 # rs1_a0 rs2_t1 @Second_pass_done beq + + ; Drop the label + 13 03 A0 03 # rd_t1 !0x3a addi + @7 63 10 65 00 # rs1_a0 rs2_t1 @Second_pass_0 bne + + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; Using scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; Read the label + $C EF 00 00 00 # rd_ra $ClearScratch jal ; Throw away token + + $5 6F 00 00 00 # $Second_pass_loop jal ; Continue looping + +:7 ;Second_pass_0 + ; Check for . + 13 03 E0 02 # rd_t1 !0x2e addi + @8 63 00 65 00 # rs1_a0 rs2_t1 @Second_pass_UpdateWord beq + + ; Check for % + 13 03 50 02 # rd_t1 !0x25 addi + @S 63 00 65 00 # rs1_a0 rs2_t1 @StorePointer beq + + ; Check for & + 13 03 60 02 # rd_t1 !0x26 addi + @S 63 00 65 00 # rs1_a0 rs2_t1 @StorePointer beq + + ; Check for ! + 13 03 10 02 # rd_t1 !0x21 addi + @Y 63 00 65 00 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + + ; Check for @ + 13 03 00 04 # rd_t1 !0x40 addi + @Y 63 00 65 00 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + + ; Check for $ + 13 03 40 02 # rd_t1 !0x24 addi + @Y 63 00 65 00 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + + ; Check for ~ + 13 03 E0 07 # rd_t1 !0x7e addi + @Y 63 00 65 00 # rs1_a0 rs2_t1 @UpdateShiftRegister beq + + ; Check for < + 13 03 C0 03 # rd_t1 !0x3c addi + 93 05 00 00 # rd_a1 addi ; write = true + @A 63 00 65 00 # rs1_a0 rs2_t1 @PadToAlign beq + + ; Deal with everything else + 93 05 00 00 # rd_a1 addi ; write = true + 13 06 F0 FF # rd_a2 !-1 addi ; update = false + $D EF 00 00 00 # rd_ra $DoByte jal ; Process our char + + # Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi + @6 63 00 65 00 # rs1_a0 rs2_t1 @Second_pass_done beq ; We are done + + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:8 ;Second_pass_UpdateWord + 13 0D 00 00 # rd_s10 addi ; updates = 0 + 93 0B 00 00 # rd_s7 addi ; tempword = 0 + 93 07 40 00 # rd_a5 !4 addi ; a5 = 4 + +:9 ;Second_pass_UpdateWord_loop + $R EF 00 00 00 # rd_ra $Read_byte jal ; Read another byte into a0 + + 93 05 F0 FF # rd_a1 !-1 addi ; write = false + 13 06 00 00 # rd_a2 addi ; update = true + $D EF 00 00 00 # rd_ra $DoByte jal ; Process our char + @9 63 40 FD 00 # rs1_s10 rs2_a5 @Second_pass_UpdateWord_loop blt ; loop 4 times + + 13 85 0B 00 # rd_a0 rs1_s7 mv ; tempword + $d 6F 00 00 00 # $UpdateShiftRegister_DOT jal ; UpdateShiftRegister('.', tempword) + +:Y ;UpdateShiftRegister + 13 06 05 00 # rd_a2 rs1_a0 mv ; Store label prefix + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; Get scratch + $C EF 00 00 00 # rd_ra $ClearScratch jal ; Clear scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; Read token + $G EF 00 00 00 # rd_ra $GetTarget jal ; Get target + 03 35 05 00 # rd_a0 rs1_a0 ld ; Dereference pointer + 33 05 65 41 # rd_a0 rs1_a0 rs2_s6 sub ; target - ip + + ; Check for ! + 13 03 10 02 # rd_t1 !0x21 addi + @I 63 00 66 00 # rs1_a2 rs2_t1 @UpdateShiftRegister_I beq + + ; Check for @ + 13 03 00 04 # rd_t1 !0x40 addi + @B 63 00 66 00 # rs1_a2 rs2_t1 @UpdateShiftRegister_B beq + + ; Check for $ + 13 03 40 02 # rd_t1 !0x24 addi + @J 63 00 66 00 # rs1_a2 rs2_t1 @UpdateShiftRegister_J beq + + ; Check for ~ + 13 03 E0 07 # rd_t1 !0x7e addi + @U 63 00 66 00 # rs1_a2 rs2_t1 @UpdateShiftRegister_U beq + + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:d ;UpdateShiftRegister_DOT + ; . before instruction means it has to be added to the final word + + ; swap = (((value >> 24) & 0xff) | + ; ((value << 8) & 0xff0000) | + ; ((value >> 8) & 0xff00) | + ; ((value << 24) & 0xff000000)) + + 9B 53 85 01 # rd_t2 rs1_a0 rs2_x24 srliw ; value >> 24 + 13 03 F0 0F # rd_t1 !0xff addi ; t1 = 0xff + B3 72 73 00 # rd_t0 rs1_t1 rs2_t2 and ; (value >> 24) & 0xff + + 9B 13 85 00 # rd_t2 rs1_a0 rs2_x8 slliw ; value << 8 + 37 03 FF 00 # rd_t1 ~0xff0000 lui ; t1 = 0xff0000 + B3 73 73 00 # rd_t2 rs1_t1 rs2_t2 and ; (value << 8) & 0xff0000 + B3 E2 72 00 # rd_t0 rs1_t0 rs2_t2 or ; logical or with the previous expression + + 9B 53 85 00 # rd_t2 rs1_a0 rs2_x8 srliw ; value >> 8 + 37 03 01 00 # rd_t1 ~0xff00 lui ; t1 = 0xff00 + 1B 03 03 F0 # rd_t1 rs1_t1 !0xff00 addiw ; t1 = 0xff00 + B3 73 73 00 # rd_t2 rs1_t1 rs2_t2 and ; (value << 8) & 0xff00 + B3 E2 72 00 # rd_t0 rs1_t0 rs2_t2 or ; logical or with the previous expression + + 9B 13 85 01 # rd_t2 rs1_a0 rs2_x24 slliw ; value << 24 + 13 03 F0 0F # rd_t1 !0xff addi + 13 13 83 01 # rd_t1 rs1_t1 rs2_x24 slli ; t1 = 0xff000000 + B3 73 73 00 # rd_t2 rs1_t1 rs2_t2 and ; (value << 24) & 0xff000000 + B3 E2 72 00 # rd_t0 rs1_t0 rs2_t2 or ; swap + + 33 4C 5C 00 # rd_s8 rs1_s8 rs2_t0 xor ; shiftregister = shiftregister ^ swap + + 13 0B CB FF # rd_s6 rs1_s6 !-4 addi ; ip = ip - 4 + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:I ;UpdateShiftRegister_I + ; Corresponds to RISC-V I format + 1B 05 45 00 # rd_a0 rs1_a0 !4 addiw ; add 4 due to this being 2nd part of auipc combo + + 37 13 00 00 # rd_t1 ~0xfff lui ; load higher bits + 1B 03 F3 FF # rd_t1 rs1_t1 !0xfff addiw + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; (value & 0xfff) + 9B 1B 43 01 # rd_s7 rs1_t1 rs2_x20 slliw ; tempword = (value & 0xfff) << 20 + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:B ;UpdateShiftRegister_B + ; Corresponds to RISC-V B format + + ; tempword = ((value & 0x1e) << 7) ; imm[4:1] + ; | ((value & 0x7e0) << (31 - 11)) ; imm[10:5] + ; | ((value & 0x800) >> 4) ; imm[11] + ; | ((value & 0x1000) << (31 - 12)) ; imm[12] + + 13 03 E0 01 # rd_t1 !0x1e addi + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x1e + 9B 12 73 00 # rd_t0 rs1_t1 rs2_x7 slliw ; tempword = (value & 0x1e) << 7 + + 13 03 00 7E # rd_t1 !0x7e0 addi + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x7e0 + 1B 13 43 01 # rd_t1 rs1_t1 rs2_x20 slliw ; (value & 0x7e0) << (31 - 11) + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 13 00 00 # rd_t1 ~0x800 lui ; load higher bits + 1B 03 03 80 # rd_t1 rs1_t1 !0x800 addiw + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x800 + 1B 53 43 00 # rd_t1 rs1_t1 rs2_x4 srliw ; (value & 0x800) >> 4 + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 13 00 00 # rd_t1 ~0x1000 lui ; load higher bits + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x1000 + 1B 13 33 01 # rd_t1 rs1_t1 rs2_x19 slliw ; (value & 0x1000) << (31 - 12) + B3 EB 62 00 # rd_s7 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:J ;UpdateShiftRegister_J + ; Corresponds to RISC-V J format + + ; tempword = ((value & 0x7fe) << (30 - 10)) ; imm[10:1] + ; | ((value & 0x800) << (20 - 11)) ; imm[11] + ; | ((value & 0xff000)) ; imm[19:12] + ; | ((value & 0x100000) << (31 - 20)) ; imm[20] + + 13 03 E0 7F # rd_t1 !0x7fe addi + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x7fe + 9B 12 43 01 # rd_t0 rs1_t1 rs2_x20 slliw ; tempword = (value & 0x7fe) << 20 + + 37 13 00 00 # rd_t1 ~0x800 lui ; load higher bits + 1B 03 03 80 # rd_t1 rs1_t1 !0x800 addiw + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x800 + 1B 13 93 00 # rd_t1 rs1_t1 rs2_x9 slliw ; (value & 0x800) << (20 - 11) + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 F3 0F 00 # rd_t1 ~0xff000 lui ; load higher bits + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0xff000 + B3 E2 62 00 # rd_t0 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 37 03 10 00 # rd_t1 ~0x100000 lui ; load higher bits + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0x100000 + 1B 13 B3 00 # rd_t1 rs1_t1 rs2_x11 slliw ; (value & 0x100000) << (31 - 20) + B3 EB 62 00 # rd_s7 rs1_t0 rs2_t1 or ; logical or with the previous expression + + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:U ;UpdateShiftRegister_U + ; Corresponds to RISC-V U format + ; if value is 0x800 or more we have to add 11-th bit (0x1000) to compensate for signed extension + + B7 12 00 00 # rd_t0 ~0x800 lui ; load higher bits + 9B 82 02 80 # rd_t0 rs1_t0 !0x800 addiw + 37 13 00 00 # rd_t1 ~0xfff lui ; load higher bits + 1B 03 F3 FF # rd_t1 rs1_t1 !0xfff addiw + + ; We are outside 31-bit that ~ can normally load + B7 03 10 00 # rd_t2 ~0x100000 lui ; load 0xfffff000 + 9B 83 F3 FF # rd_t2 rs1_t2 !-1 addiw ; load 0xfffff000 + 93 93 C3 00 # rd_t2 rs1_t2 rs2_x12 slli ; load 0xfffff000 + 33 73 65 00 # rd_t1 rs1_a0 rs2_t1 and ; value & 0xfff + B3 7B 75 00 # rd_s7 rs1_a0 rs2_t2 and ; value & 0xfffff000 + @u 63 40 53 00 # rs1_t1 rs2_t0 @UpdateShiftRegister_U_small blt + + # Deal with sign extension: add 0x1000 + B7 12 00 00 # rd_t0 ~0x1000 lui ; load higher bits + BB 8B 72 01 # rd_s7 rs1_t0 rs2_s7 addw ; (value & 0xfffff000) + 0x1000 + +:u ;UpdateShiftRegister_U_small + 33 4C 7C 01 # rd_s8 rs1_s8 rs2_s7 xor ; shiftregister = shiftregister ^ tempword + + $5 6F 00 00 00 # $Second_pass_loop jal ; continue looping + +:S ;StorePointer + 13 0B 4B 00 # rd_s6 rs1_s6 !4 addi ; update ip + 13 06 05 00 # rd_a2 rs1_a0 mv ; Store label prefix + + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; Get scratch + $C EF 00 00 00 # rd_ra $ClearScratch jal ; clear scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; Read token + 93 07 05 00 # rd_a5 rs1_a0 mv ; save char + $G EF 00 00 00 # rd_ra $GetTarget jal ; Get target + 83 35 05 00 # rd_a1 rs1_a0 ld ; Dereference pointer + + ; If char is > then change relative base address to ip + 13 03 E0 03 # rd_t1 !0x3e addi ; t1 = 0x3e + @P 63 00 F3 00 # rs1_t1 rs2_a5 @StorePointer_1 beq + + ; Check for & + 13 03 60 02 # rd_t1 !0x26 addi + @0 63 00 66 00 # rs1_a2 rs2_t1 @StorePointer_0 beq + + ; Check for % + 13 03 50 02 # rd_t1 !0x25 addi + @F 63 10 66 00 # rs1_a2 rs2_t1 @Fail bne + B3 85 65 41 # rd_a1 rs1_a1 rs2_s6 sub ; displacement = target - ip + +:0 ;StorePointer_0 + ; Output pointer + 93 07 40 00 # rd_a5 !4 addi ; number of bytes +:l ;StorePointer_loop + 13 D3 85 00 # rd_t1 rs1_a1 rs2_x8 srli ; value / 256 + 13 15 83 00 # rd_a0 rs1_t1 rs2_x8 slli + 33 85 A5 40 # rd_a0 rs1_a1 rs2_a0 sub ; byte = value % 256 + + 93 05 03 00 # rd_a1 rs1_t1 mv ; value = value / 256 + $t EF 00 00 00 # rd_ra $fputc jal ; write value + 93 87 F7 FF # rd_a5 rs1_a5 !-1 addi ; decrease number of bytes to write + @l 63 90 07 00 # rs1_a5 @StorePointer_loop bnez ; continue looping + + $5 6F 00 00 00 # $Second_pass_loop jal ; Continue looping + +:P ;StorePointer_1 + 13 86 05 00 # rd_a2 rs1_a1 mv ; save target + ~s 97 05 00 00 # rd_a1 ~scratch auipc + !s 93 85 05 00 # rd_a1 rs1_a1 !scratch addi ; Get scratch + $C EF 00 00 00 # rd_ra $ClearScratch jal ; clear scratch + $c EF 00 00 00 # rd_ra $consume_token jal ; consume token + $G EF 00 00 00 # rd_ra $GetTarget jal ; Get target + 83 35 05 00 # rd_a1 rs1_a0 ld ; Dereference pointer + B3 05 B6 40 # rd_a1 rs1_a2 rs2_a1 sub ; displacement = target - ip + + $0 6F 00 00 00 # $StorePointer_0 jal ; Continue looping + +:6 ;Second_pass_done + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Pad with zeros to align to word size +; bool write in a1 +:A ;PadToAlign + 13 03 10 00 # rd_t1 !1 addi ; t1 = 1 + 33 75 6B 00 # rd_a0 rs1_s6 rs2_t1 and ; ip & 0x1 + @b 63 10 65 00 # rs1_a0 rs2_t1 @PadToAlign_1 bne ; check if ip & 0x1 == 1 + 33 0B 6B 00 # rd_s6 rs1_s6 rs2_t1 add ; ip = ip + 1 + + @b 63 90 05 00 # rs1_a1 @PadToAlign_1 bnez ; check if we have to write + 13 05 00 00 # rd_a0 mv ; a0 = 0 + $t EF 00 00 00 # rd_ra $fputc jal ; write 0 + +:b ;PadToAlign_1 + 13 03 20 00 # rd_t1 !2 addi ; t1 = 2 + 33 75 6B 00 # rd_a0 rs1_s6 rs2_t1 and ; ip & 0x1 + @e 63 10 65 00 # rs1_a0 rs2_t1 @PadToAlign_2 bne ; check if ip & 0x2 == 2 + 33 0B 6B 00 # rd_s6 rs1_s6 rs2_t1 add ; ip = ip + 2 + + @e 63 90 05 00 # rs1_a1 @PadToAlign_2 bnez ; check if we have to write + 13 05 00 00 # rd_a0 mv ; a0 = 0 + $t EF 00 00 00 # rd_ra $fputc jal ; write 0 + 13 05 00 00 # rd_a0 mv ; a0 = 0 + $t EF 00 00 00 # rd_ra $fputc jal ; write 0 + +:e ;PadToAlign_2 + @5 63 80 05 00 # rs1_a1 @Second_pass_loop beqz ; return to Second_pass + $1 6F 00 00 00 # $First_pass_loop jal ; return to First_pass + +; Zero scratch area +:C ;ClearScratch + 13 01 81 FE # rd_sp rs1_sp !-24 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + 23 34 A1 00 # rs1_sp rs2_a0 @8 sd ; protect a0 + 23 38 B1 00 # rs1_sp rs2_a1 @16 sd ; protect a1 + + ~s 17 05 00 00 # rd_a0 ~scratch auipc + !s 13 05 05 00 # rd_a0 rs1_a0 !scratch addi ; Find where our scratch area is + +:g ;ClearScratch_loop + 83 05 05 00 # rd_a1 rs1_a0 lb ; Read current byte: s[i] + 23 00 05 00 # rs1_a0 sb ; Write zero: s[i] = 0 + 13 05 15 00 # rd_a0 rs1_a0 !1 addi ; Increment: i = i + 1 + @g 63 90 05 00 # rs1_a1 @ClearScratch_loop bnez ; Keep looping + + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 03 35 81 00 # rd_a0 rs1_sp !8 ld ; restore a0 + 83 35 01 01 # rd_a1 rs1_sp !16 ld ; restore a1 + 13 01 81 01 # rd_sp rs1_sp !24 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Receives pointer in a1 +; Writes our token and updates pointer in a1 +:c ;consume_token + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + +:h ;consume_token_0 + $R EF 00 00 00 # rd_ra $Read_byte jal ; Read byte into a0 + + ; Check for \t + 13 03 90 00 # rd_t1 !0x09 addi + @j 63 00 65 00 # rs1_a0 rs2_t1 @consume_token_done beq + + ; Check for \n + 13 03 A0 00 # rd_t1 !0x0a addi + @j 63 00 65 00 # rs1_a0 rs2_t1 @consume_token_done beq + + ; Check for ' ' + 13 03 00 02 # rd_t1 !0x20 addi + @j 63 00 65 00 # rs1_a0 rs2_t1 @consume_token_done beq + + ; Check for > + 13 03 E0 03 # rd_t1 !0x3e addi + @j 63 00 65 00 # rs1_a0 rs2_t1 @consume_token_done beq + + 23 80 A5 00 # rs1_a1 rs2_a0 sb ; Store char + 93 85 15 00 # rd_a1 rs1_a1 !1 addi ; Point to next spot + $h 6F 00 00 00 # $consume_token_0 jal ; Continue looping + +:j ;consume_token_done + 23 B0 05 00 # rs1_a1 sd ; Pad with nulls + 93 85 85 00 # rd_a1 rs1_a1 !8 addi ; Update the pointer + + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; DoByte function +; Receives: +; character in a0 +; bool write in a1 +; bool update in a2 +; Does not return anything +:D ;DoByte + 13 01 01 FF # rd_sp rs1_sp !-16 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + 23 34 01 01 # rs1_sp rs2_a6 @8 sd ; protect a6 + + $H EF 00 00 00 # rd_ra $hex jal ; Process hex, store it in a6 + + @k 63 40 08 00 # rs1_a6 @DoByte_Done bltz ; Deal with EOF and unrecognized characters + + @2 63 10 0A 00 # rs1_s4 @DoByte_NotToggle bnez ; Check if toggle is set + + ; toggle = true + @m 63 90 05 00 # rs1_a1 @DoByte_1 bnez ; check if we have to write + + ; write = true + ; We calculate (hold * 16) + hex(c) ^ sr_nextb() + ; First, calculate new shiftregister + 93 02 F0 0F # rd_t0 !0xff addi + B3 72 5C 00 # rd_t0 rs1_s8 rs2_t0 and ; sr_nextb = shiftregister & 0xff + 1B 5C 8C 00 # rd_s8 rs1_s8 rs2_x8 srliw ; shiftregister >> 8 + + B3 C2 02 01 # rd_t0 rs1_t0 rs2_a6 xor ; hex(c) ^ sr_nextb + 13 93 4A 00 # rd_t1 rs1_s5 rs2_x4 slli ; hold << 4 + 33 85 62 00 # rd_a0 rs1_t0 rs2_t1 add ; (hold << 4) + hex(c) ^ sr_nextb() + $t EF 00 00 00 # rd_ra $fputc jal ; print it + @F 63 00 05 00 # rs1_a0 @Fail beqz ; Fail if nothing was written + +:m ;DoByte_1 + 13 0B 1B 00 # rd_s6 rs1_s6 !1 addi ; Increment IP + @o 63 00 06 00 # rs1_a2 @DoByte_2 beqz ; check if we have to update +:n ;DoByte_2b + 93 0A 00 00 # rd_s5 mv ; hold = 0 + $q 6F 00 00 00 # $DoByte_FlipToggle jal ; return + +:2 ;DoByte_NotToggle + 93 0A 08 00 # rd_s5 rs1_a6 mv ; hold = hex(c) + +:q ;DoByte_FlipToggle + 13 4A FA FF # rd_s4 rs1_s4 not ; Flip the toggle + +:k ;DoByte_Done + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 03 38 81 00 # rd_a6 rs1_sp !8 ld ; restore a6 + 13 01 01 01 # rd_sp rs1_sp !16 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +:o ;DoByte_2 + 13 93 4A 00 # rd_t1 rs1_s5 rs2_x4 slli ; hold * 16 + B3 0A 03 01 # rd_s5 rs1_t1 rs2_a6 add ; hold = hold * 16 + hex(c) + 13 93 8B 00 # rd_t1 rs1_s7 rs2_x8 slli ; tempword << 8 + B3 4B 53 01 # rd_s7 rs1_t1 rs2_s5 xor ; tempword = (tempword << 8) ^ hold + 13 0D 1D 00 # rd_s10 rs1_s10 !1 addi ; updates = updates + 1 + $n 6F 00 00 00 # $DoByte_2b jal + +; Convert ASCII hex characters into binary representation, e.g. 'a' -> 0xA +; Receives: +; character in a0 +; Returns: +; a6 with character's hex value. +:H ;hex + 13 01 01 FF # rd_sp rs1_sp !-16 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + 23 34 B1 00 # rs1_sp rs2_a1 @8 sd ; protect a1 + + ; Deal with EOF + 13 03 C0 FF # rd_t1 !-4 addi + @r 63 00 65 00 # rs1_a0 rs2_t1 @hex_return beq + + ; deal with line comments starting with # + 13 03 30 02 # rd_t1 !0x23 addi + @x 63 00 65 00 # rs1_a0 rs2_t1 @ascii_comment beq ; a0 eq to '#' + + ; deal with line comments starting with ; + 13 03 B0 03 # rd_t1 !0x3b addi + @x 63 00 65 00 # rs1_a0 rs2_t1 @ascii_comment beq ; a0 eq to ';' + + ; deal all ascii less than 0 + 13 03 00 03 # rd_t1 !0x30 addi + @y 63 40 65 00 # rs1_a0 rs2_t1 @ascii_other blt + + ; deal with 0-9 + 13 03 A0 03 # rd_t1 !0x3a addi + @N 63 40 65 00 # rs1_a0 rs2_t1 @ascii_num blt + + ; deal with all ascii less than A + 13 03 10 04 # rd_t1 !0x41 addi + @y 63 40 65 00 # rs1_a0 rs2_t1 @ascii_other blt + + ; deal with A-F + 13 03 70 04 # rd_t1 !0x47 addi + @z 63 40 65 00 # rs1_a0 rs2_t1 @ascii_high blt + + ; deal with all ascii less than a + 13 03 10 06 # rd_t1 !0x61 addi + @y 63 40 65 00 # rs1_a0 rs2_t1 @ascii_other blt + + ; deal with a-f + 13 03 70 06 # rd_t1 !0x67 addi + @Z 63 40 65 00 # rs1_a0 rs2_t1 @ascii_low blt + + ; The rest that remains needs to be ignored + $y 6F 00 00 00 # $ascii_other jal + +:N ;ascii_num + 13 03 00 03 # rd_t1 !0x30 addi ; '0' -> 0 + 33 08 65 40 # rd_a6 rs1_a0 rs2_t1 sub + $r 6F 00 00 00 # $hex_return jal ; return +:Z ;ascii_low + 13 03 70 05 # rd_t1 !0x57 addi ; 'a' -> 0xA + 33 08 65 40 # rd_a6 rs1_a0 rs2_t1 sub + $r 6F 00 00 00 # $hex_return jal ; return +:z ;ascii_high + 13 03 70 03 # rd_t1 !0x37 addi ; 'A' -> 0xA + 33 08 65 40 # rd_a6 rs1_a0 rs2_t1 sub + $r 6F 00 00 00 # $hex_return jal ; return +:y ;ascii_other + 13 08 F0 FF # rd_a6 !-1 addi ; Return -1 + $r 6F 00 00 00 # $hex_return jal ; return +:x ;ascii_comment ; Read the comment until newline + $R EF 00 00 00 # rd_ra $Read_byte jal + 13 03 D0 00 # rd_t1 !0xd addi ; CR + @E 63 00 65 00 # rs1_a0 rs2_t1 @ascii_comment_cr beq + 13 03 A0 00 # rd_t1 !0xa addi ; LF + @x 63 10 65 00 # rs1_a0 rs2_t1 @ascii_comment bne ; Keep reading comment +:E ;ascii_comment_cr + 13 08 F0 FF # rd_a6 !-1 addi ; Return -1 +:r ;hex_return + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 83 35 81 00 # rd_a1 rs1_sp !8 ld ; restore a1 + 13 01 01 01 # rd_sp rs1_sp !16 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Read byte into a0 +:R ;Read_byte + 13 01 81 FE # rd_sp rs1_sp !-24 addi ; Allocate stack + 23 34 B1 00 # rs1_sp rs2_a1 @8 sd ; protect a1 + 23 38 C1 00 # rs1_sp rs2_a2 @16 sd ; protect a2 + + 93 08 F0 03 # rd_a7 !63 addi ; sys_read + 13 05 09 00 # rd_a0 rs1_s2 mv ; File descriptor + 93 05 01 00 # rd_a1 rs1_sp mv ; Get stack address for buffer + 13 06 10 00 # rd_a2 !1 addi ; Size of what we want to read + 73 00 00 00 # ecall ; syscall + + @K 63 00 05 00 # rs1_a0 @Read_byte_1 beqz ; Deal with EOF + 03 C5 05 00 # rd_a0 rs1_a1 lbu ; return char in a0 + + $M 6F 00 00 00 # $Read_byte_done jal ; return + +:K ;Read_byte_1 + 13 05 C0 FF # rd_a0 !-4 addi ; Put EOF in a0 +:M ;Read_byte_done + 83 35 81 00 # rd_a1 rs1_sp !8 ld ; restore a1 + 03 36 01 01 # rd_a2 rs1_sp !16 ld ; restore a2 + 13 01 81 01 # rd_sp rs1_sp !24 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +; Find a label matching pointer in scratch +; Returns a pointer in a0 +:G ;GetTarget + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + + 93 82 04 00 # rd_t0 rs1_s1 mv ; grab jump_table + +:O ;GetTarget_loop_0 + ; Compare the strings + ~s 17 03 00 00 # rd_t1 ~scratch auipc + !s 13 03 03 00 # rd_t1 rs1_t1 !scratch addi ; reset scratch + 83 B3 02 01 # rd_t2 rs1_t0 !16 ld ; I->name +:Q ;GetTarget_loop + 83 CE 03 00 # rd_t4 rs1_t2 lbu ; I->name[i] + 03 4E 03 00 # rd_t3 rs1_t1 lbu ; scratch[i] + @v 63 10 DE 01 # rs1_t3 rs2_t4 @GetTarget_miss bne ; strings don't match + + ; Look at the next char + 13 03 13 00 # rd_t1 rs1_t1 !1 addi + 93 83 13 00 # rd_t2 rs1_t2 !1 addi + @Q 63 90 0E 00 # rs1_t4 @GetTarget_loop bnez ; Loop until zero (end of string) + $V 6F 00 00 00 # $GetTarget_done jal ; We have a match + +:v ;GetTarget_miss + 83 B2 02 00 # rd_t0 rs1_t0 ld ; I = I->next + @F 63 80 02 00 # rs1_t0 @Fail beqz ; Abort, no match found + + $O 6F 00 00 00 # $GetTarget_loop_0 jal ; Try another label + +:V ;GetTarget_done + 13 85 82 00 # rd_a0 rs1_t0 !8 addi ; Get target address + + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +:L ;StoreLabel + 13 01 81 FF # rd_sp rs1_sp !-8 addi ; Allocate stack + 23 30 11 00 # rs1_sp rs2_ra sd ; protect ra + + 13 85 0C 00 # rd_a0 rs1_s9 mv ; struct entry + 93 8C 8C 01 # rd_s9 rs1_s9 !24 addi ; calloc + 23 34 65 01 # rs1_a0 rs2_s6 @8 sd ; entry->target = ip + 23 30 95 00 # rs1_a0 rs2_s1 sd ; entry->next = jump_table + 93 04 05 00 # rd_s1 rs1_a0 mv ; jump_table = entry + 23 38 95 01 # rs1_a0 rs2_s9 @16 sd ; entry->name = token + 93 85 0C 00 # rd_a1 rs1_s9 mv ; Write after struct + $c EF 00 00 00 # rd_ra $consume_token jal ; Collect string + 93 8C 05 00 # rd_s9 rs1_a1 mv ; update HEAP + + 83 30 01 00 # rd_ra rs1_sp ld ; restore ra + 13 01 81 00 # rd_sp rs1_sp !8 addi ; deallocate stack + $1 6F 00 00 00 # $First_pass_loop jal ; return + +; fputc function +; Receives CHAR in a0 +; Writes and returns number of bytes written in a0 +:t ;fputc + 13 01 01 FE # rd_sp rs1_sp !-32 addi ; allocate stack + 23 30 A1 00 # rs1_sp rs2_a0 sd ; protect a0 + 23 34 11 00 # rs1_sp rs2_ra @8 sd ; protect ra + 23 38 B1 00 # rs1_sp rs2_a1 @16 sd ; protect a1 + 23 3C C1 00 # rs1_sp rs2_a2 @24 sd ; protect a2 + + 93 08 00 04 # rd_a7 !64 addi ; sys_write + 13 85 09 00 # rd_a0 rs1_s3 mv ; write to output + 93 05 01 00 # rd_a1 rs1_sp mv ; Get stack address + 13 06 10 00 # rd_a2 !1 addi ; write 1 character + 73 00 00 00 # ecall ; syscall + + 83 30 81 00 # rd_ra rs1_sp !8 ld ; restore ra + 83 35 01 01 # rd_a1 rs1_sp !16 ld ; restore a1 + 03 36 81 01 # rd_a2 rs1_sp !24 ld ; restore a2 + 13 01 01 02 # rd_sp rs1_sp !32 addi ; Deallocate stack + 67 80 00 00 # rs1_ra jalr ; return + +:F ;Fail + ; Terminate program with 1 return code + 93 08 D0 05 # rd_a7 !93 addi ; sys_exit + 13 05 10 00 # rd_a0 !1 addi ; Return code 1 + 73 00 00 00 # ecall ; exit(1) +# PROGRAM END + +:s ;scratch + 00 00 00 00 + +#:ELF_end