seed/update 5.1 KiB raw
1
#!/bin/sh
2
#
3
# Find the compiler fixed point and update the seed binary.
4
#
5
# Iterates self-compilation stages starting from the current seed until a fixed
6
# point is reached, then copies the result back to the seed.
7
#
8
# Usage: seed/update [--seed <path>] [--from-s0]
9
#
10
# Options:
11
#   --seed <path>            Use <path> as the seed binary instead of seed/radiance.rv64
12
#   --from-s0                Run the seed as a native binary (stage 0) instead of
13
#                            through the emulator
14
#
15
# Environment variables:
16
#   EMULATOR_MEMORY_SIZE     Emulator memory in KB (default: 385024)
17
#   EMULATOR_DATA_SIZE       Emulator data segment in KB (default: 348160)
18
#
19
set -eu
20
21
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
22
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
23
24
cd "${ROOT_DIR}"
25
26
# ---------------------------------------------------------------------------
27
# Helpers
28
# ---------------------------------------------------------------------------
29
30
step() {
31
  printf "\n==> %s\n" "$*"
32
}
33
34
fail() {
35
  printf "!! %s\n" "$*" >&2
36
  exit 1
37
}
38
39
# ---------------------------------------------------------------------------
40
# Command line flags for Radiance compiler
41
# ---------------------------------------------------------------------------
42
43
STD_MODS="$(sed 's/^/-mod /' std.lib | tr '\n' ' ')"
44
OPTS="-pkg std ${STD_MODS} -pkg radiance -mod compiler/radiance.rad -mod compiler/radiance/codegen.rad -entry radiance"
45
46
# ---------------------------------------------------------------------------
47
# Emulator settings
48
# ---------------------------------------------------------------------------
49
50
MEMORY_SIZE="${EMULATOR_MEMORY_SIZE:-385024}"
51
DATA_SIZE="${EMULATOR_DATA_SIZE:-348160}"
52
EMU="${RAD_EMULATOR:-emulator} -debug -no-validate \
53
    -memory-size=${MEMORY_SIZE} -data-size=${DATA_SIZE} \
54
    -count-instructions -run"
55
56
# ---------------------------------------------------------------------------
57
# Compile one self-hosted stage.
58
#   stage <input> <output>
59
# ---------------------------------------------------------------------------
60
61
stage() {
62
  time -p $EMU "$1" $OPTS -o "$2"
63
}
64
65
# ---------------------------------------------------------------------------
66
# Compile one stage using a native (s0) binary.
67
#   stageS0 <input> <output>
68
# ---------------------------------------------------------------------------
69
70
stageS0() {
71
  time -p "$1" compiler/radiance.rad $OPTS -o "$2"
72
}
73
74
# ---------------------------------------------------------------------------
75
# Compare two stage binaries. Returns 0 if identical, 1 if they differ.
76
# ---------------------------------------------------------------------------
77
78
compareStages() {
79
  if cmp -s "$1" "$2"; then
80
    printf " IDENTICAL\n  sha256: %s\n" "$(sha256sum "$2" | cut -d' ' -f1)"
81
    return 0
82
  else
83
    printf " DIFFERENT\n"
84
    return 1
85
  fi
86
}
87
88
# ---------------------------------------------------------------------------
89
# Copy the fixed-point binary to the seed.
90
# ---------------------------------------------------------------------------
91
92
updateSeed() {
93
  [ "$1" != seed/radiance.rv64 ] && cp "$1" seed/radiance.rv64
94
95
  git rev-parse HEAD > seed/radiance.rv64.git
96
97
  printf "\n"
98
  printf "Fixed point reached at %s\n" "$2"
99
  printf "Seed updated (commit $(git rev-parse --short HEAD))\n"
100
}
101
102
# ---------------------------------------------------------------------------
103
# Parse arguments
104
# ---------------------------------------------------------------------------
105
106
SEED="seed/radiance.rv64"
107
FROM_S0=0
108
109
while [ $# -gt 0 ]; do
110
  case "$1" in
111
    --seed)
112
      [ $# -ge 2 ] || fail "--seed requires an argument"
113
      SEED="$(realpath "$2")"
114
      shift 2
115
      ;;
116
    --from-s0)
117
      FROM_S0=1
118
      shift
119
      ;;
120
    *)
121
      fail "unknown option: $1"
122
      ;;
123
  esac
124
done
125
126
command -v "${RAD_EMULATOR:-emulator}" >/dev/null 2>&1 \
127
  || fail "emulator not found: set RAD_EMULATOR or add 'emulator' to PATH"
128
[ -f "${SEED}" ] || fail "seed binary not found: ${SEED}"
129
130
# ---------------------------------------------------------------------------
131
# Stage 1: seed compiles source
132
# ---------------------------------------------------------------------------
133
134
S1=seed/radiance.rv64.s1
135
S2=seed/radiance.rv64.s2
136
S3=seed/radiance.rv64.s3
137
S4=seed/radiance.rv64.s4
138
139
step "Stage 1: S0 builds S1"
140
if [ "${FROM_S0}" -eq 1 ]; then
141
  stageS0 "${SEED}" "${S1}"
142
else
143
  stage "${SEED}" "${S1}"
144
fi
145
146
printf "\nComparing S0 and S1 ..."
147
if compareStages "${SEED}" "${S1}"; then
148
  updateSeed "${SEED}" "S0"
149
  exit 0
150
fi
151
152
# ---------------------------------------------------------------------------
153
# Stage 2: S1 compiles source
154
# ---------------------------------------------------------------------------
155
156
step "Stage 2: S1 builds S2"
157
stage "${S1}" "${S2}"
158
159
printf "\nComparing S1 and S2 ..."
160
if compareStages "${S1}" "${S2}"; then
161
  updateSeed "${S1}" "S1"
162
  exit 0
163
fi
164
165
# ---------------------------------------------------------------------------
166
# Stage 3: S2 compiles source
167
# ---------------------------------------------------------------------------
168
169
step "Stage 3: S2 builds S3"
170
stage "${S2}" "${S3}"
171
172
printf "\nComparing S2 and S3 ..."
173
if compareStages "${S2}" "${S3}"; then
174
  updateSeed "${S2}" "S2"
175
  exit 0
176
fi
177
178
fail "No fixed point reached after 3 stages"