build-p1pp.sh (2300B)
1 #!/bin/sh 2 ## build-p1pp.sh — assemble a P1pp source (.P1pp) into a runnable ELF. 3 ## 4 ## Pipeline: 5 ## 1. cat — P1/P1-<arch>.M1pp ++ P1/P1.M1pp ++ <source.P1pp> -> combined.M1pp 6 ## 2. m1pp — combined.M1pp -> expanded.M1 (macros -> raw hex + labels) 7 ## 3. M1 — expanded.M1 -> prog.hex2 (stringify literals, pass hex through) 8 ## 4. cat — vendor/seed/<arch>/ELF.hex2 ++ prog.hex2 -> linked.hex2 9 ## 5. hex2 — linked.hex2 -> raw ELF 10 ## 6. trim p_filesz, chmod 0700 11 ## 12 ## Uses the native-compiled m1pp + mescc-tools M1/hex2 (host binaries built 13 ## by scripts/build-native-tools.sh). The host-compiled m1pp has 64K-token / 14 ## 512K-text buffers, versus ~4K / 32K in the aarch64 self-hosted m1pp.P1; 15 ## the combined backend + frontend + source easily overflows the latter. 16 ## 17 ## Usage: scripts/build-p1pp.sh <arch> <source.P1pp> <output_binary> 18 19 set -eu 20 21 if [ "$#" -ne 3 ]; then 22 echo "usage: $0 <arch> <source.P1pp> <output>" >&2 23 exit 2 24 fi 25 26 ARCH=$1 27 SRC=$2 28 OUT=$3 29 30 REPO=$(cd "$(dirname "$0")/.." && pwd) 31 cd "$REPO" 32 33 case "$ARCH" in 34 aarch64|amd64|riscv64) ;; 35 *) echo "build-p1pp.sh: unsupported arch '$ARCH'" >&2; exit 1 ;; 36 esac 37 38 ELF_HDR=vendor/seed/$ARCH/ELF.hex2 39 BASE_ADDR=0x600000 40 FRONTEND=P1/P1.M1pp 41 BACKEND=P1/P1-$ARCH.M1pp 42 43 for f in "$BACKEND" "$FRONTEND" "$ELF_HDR" "$SRC"; do 44 [ -e "$f" ] || { echo "build-p1pp.sh: missing input: $f" >&2; exit 1; } 45 done 46 47 NATIVE_M1PP=build/native-tools/m1pp 48 NATIVE_M1=build/native-tools/M1 49 NATIVE_HEX2=build/native-tools/hex2 50 if [ ! -x "$NATIVE_M1PP" ] || [ ! -x "$NATIVE_M1" ] || [ ! -x "$NATIVE_HEX2" ]; then 51 sh scripts/build-native-tools.sh 52 fi 53 54 NAME=$(basename "$SRC" .P1pp) 55 WORK=build/p1pp/$ARCH/$NAME.work 56 mkdir -p "$WORK" "$(dirname "$OUT")" 57 58 COMBINED=$WORK/combined.M1pp 59 EXPANDED=$WORK/expanded.M1 60 PROG_HEX2=$WORK/prog.hex2 61 LINKED=$WORK/linked.hex2 62 RAW=$WORK/prog.raw 63 64 cat "$BACKEND" "$FRONTEND" "$SRC" > "$COMBINED" 65 66 "$NATIVE_M1PP" "$COMBINED" "$EXPANDED" 67 68 "$NATIVE_M1" --architecture "$ARCH" --little-endian \ 69 -f "$EXPANDED" -o "$PROG_HEX2" 70 71 cat "$ELF_HDR" "$PROG_HEX2" > "$LINKED" 72 73 "$NATIVE_HEX2" --architecture "$ARCH" --little-endian \ 74 --base-address "$BASE_ADDR" \ 75 -f "$LINKED" -o "$RAW" 76 77 size=$(od -An -tu4 -N4 -j96 "$RAW" | tr -d ' ') 78 head -c "$size" "$RAW" > "$OUT" 79 chmod 0700 "$OUT"