omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / synapse / syn_gen.sh
| 1 | #!/bin/sh |
| 2 | # syn_gen.sh: generate the synapse codec unit source from the one layout list. |
| 3 | # syn_gen.sh gen LAYOUT TEMPLATE OUT_OSC OUT_H write the generated files |
| 4 | # syn_gen.sh check LAYOUT TEMPLATE OUT_OSC OUT_H regenerate to a temp dir and require |
| 5 | # byte-identical checked-in files |
| 6 | # OSC has no constants or includes, so the template (syn_codec.osc.in) names every |
| 7 | # workspace offset as @NAME@ and sed substitutes the number. The C header carries the |
| 8 | # same names for the host. POSIX sh and sed only. |
| 9 | set -eu |
| 10 | export LC_ALL=C |
| 11 | |
| 12 | mode=${1:?mode} |
| 13 | lst=${2:?layout} |
| 14 | tpl=${3:?template} |
| 15 | osc=${4:?out.osc} |
| 16 | hdr=${5:?out.h} |
| 17 | |
| 18 | # NAME VALUE pairs, comments and blank lines dropped |
| 19 | pairs() { |
| 20 | sed -e 's/#.*//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e '/^$/d' "$lst" |
| 21 | } |
| 22 | |
| 23 | build() { |
| 24 | out_osc=$1 |
| 25 | out_h=$2 |
| 26 | tmp=$(mktemp -d "${TMPDIR:-/tmp}/syn_gen.XXXXXX") |
| 27 | trap 'rm -rf "$tmp"' EXIT |
| 28 | pairs > "$tmp/pairs" |
| 29 | # every line is exactly NAME VALUE; names unique; values decimal |
| 30 | if grep -Env '^[A-Z][A-Z0-9_]* [0-9]+$' "$tmp/pairs" > "$tmp/bad"; [ -s "$tmp/bad" ]; then |
| 31 | echo "syn_gen: bad layout line:" >&2 |
| 32 | cat "$tmp/bad" >&2 |
| 33 | exit 1 |
| 34 | fi |
| 35 | dup=$(sed 's/ .*//' "$tmp/pairs" | sort | uniq -d) |
| 36 | if [ -n "$dup" ]; then |
| 37 | echo "syn_gen: duplicate layout name: $dup" >&2 |
| 38 | exit 1 |
| 39 | fi |
| 40 | # emb_open packs end, base and the prefix offset of a region into one word, 20 bits each |
| 41 | # (MASK20). Every such position is at most INPUT_MAX (decode input, encode output), so the |
| 42 | # packing holds only while INPUT_MAX < 2^20. Refuse a layout that breaks it. |
| 43 | m20=$(sed -n 's/^MASK20 //p' "$tmp/pairs") |
| 44 | inmax=$(sed -n 's/^INPUT_MAX //p' "$tmp/pairs") |
| 45 | if [ "$m20" != 1048575 ] || [ -z "$inmax" ] || [ "$inmax" -gt "$m20" ]; then |
| 46 | echo "syn_gen: REFUSED PACK20: INPUT_MAX ${inmax:-missing} must be at most MASK20 ${m20:-missing} = 2^20 - 1 (emb_open save word)" >&2 |
| 47 | exit 1 |
| 48 | fi |
| 49 | sed 's/^\([A-Z0-9_]*\) \([0-9]*\)$/s|@\1@|\2|g/' "$tmp/pairs" > "$tmp/sub.sed" |
| 50 | { |
| 51 | echo "// GENERATED by src/synapse/syn_gen.sh from syn_layout.lst and $(basename "$tpl"). Do not edit; edit the .in or the list." |
| 52 | sed -f "$tmp/sub.sed" "$tpl" |
| 53 | } > "$out_osc" |
| 54 | if grep -n '@[A-Z][A-Z0-9_]*@' "$out_osc" > "$tmp/left"; then |
| 55 | echo "syn_gen: unresolved @NAME@ in template:" >&2 |
| 56 | cat "$tmp/left" >&2 |
| 57 | exit 1 |
| 58 | fi |
| 59 | { |
| 60 | echo "/* GENERATED by src/synapse/syn_gen.sh from syn_layout.lst. Do not edit. */" |
| 61 | echo "#ifndef SYN_LAYOUT_H" |
| 62 | echo "#define SYN_LAYOUT_H" |
| 63 | sed 's/^\([A-Z0-9_]*\) \([0-9]*\)$/#define SYN_\1 \2ULL/' "$tmp/pairs" |
| 64 | echo "#endif /* SYN_LAYOUT_H */" |
| 65 | } > "$out_h" |
| 66 | rm -rf "$tmp" |
| 67 | trap - EXIT |
| 68 | } |
| 69 | |
| 70 | case $mode in |
| 71 | gen) |
| 72 | build "$osc" "$hdr" |
| 73 | ;; |
| 74 | check) |
| 75 | t=$(mktemp -d "${TMPDIR:-/tmp}/syn_gen_chk.XXXXXX") |
| 76 | build "$t/o.osc" "$t/o.h" |
| 77 | rc=0 |
| 78 | cmp -s "$t/o.osc" "$osc" || { echo "syn_gen: $osc is stale; run make syn-gen" >&2; rc=1; } |
| 79 | cmp -s "$t/o.h" "$hdr" || { echo "syn_gen: $hdr is stale; run make syn-gen" >&2; rc=1; } |
| 80 | rm -rf "$t" |
| 81 | [ $rc -eq 0 ] && echo "syn_gen: check OK (generated files match the layout list)" |
| 82 | exit $rc |
| 83 | ;; |
| 84 | *) |
| 85 | echo "usage: syn_gen.sh gen|check LAYOUT TEMPLATE OUT_OSC OUT_H" >&2 |
| 86 | exit 2 |
| 87 | ;; |
| 88 | esac |