seed/update 5.2 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
COMPILER_MODS="$(for source in compiler/radiance.rad compiler/radiance/*.rad compiler/radiance/*/*.rad; do if [ -f "$source" ]; then printf '%s %s ' -mod "$source"; fi; done)"
45
OPTS="-pkg std ${STD_MODS} -pkg radiance ${COMPILER_MODS} -entry radiance"
46
47
# ---------------------------------------------------------------------------
48
# Emulator settings
49
# ---------------------------------------------------------------------------
50
51
MEMORY_SIZE="${EMULATOR_MEMORY_SIZE:-385024}"
52
DATA_SIZE="${EMULATOR_DATA_SIZE:-348160}"
53
EMU="${RAD_EMULATOR:-emulator} -debug -no-validate \
54
    -memory-size=${MEMORY_SIZE} -data-size=${DATA_SIZE} \
55
    -count-instructions -run"
56
57
# ---------------------------------------------------------------------------
58
# Compile one self-hosted stage.
59
#   stage <input> <output>
60
# ---------------------------------------------------------------------------
61
62
stage() {
63
  time $EMU "$1" $OPTS -o "$2"
64
}
65
66
# ---------------------------------------------------------------------------
67
# Compile one stage using a native (s0) binary.
68
#   stageS0 <input> <output>
69
# ---------------------------------------------------------------------------
70
71
stageS0() {
72
  time "$1" compiler/radiance.rad $OPTS -o "$2"
73
}
74
75
# ---------------------------------------------------------------------------
76
# Compare two stage binaries. Returns 0 if identical, 1 if they differ.
77
# ---------------------------------------------------------------------------
78
79
compareStages() {
80
  if cmp -s "$1" "$2"; then
81
    printf " IDENTICAL\n  sha256: %s\n" "$(sha256sum "$2" | cut -d' ' -f1)"
82
    return 0
83
  else
84
    printf " DIFFERENT\n"
85
    return 1
86
  fi
87
}
88
89
# ---------------------------------------------------------------------------
90
# Copy the fixed-point binary to the seed.
91
# ---------------------------------------------------------------------------
92
93
updateSeed() {
94
  [ "$1" != seed/radiance.rv64 ] && cp "$1" seed/radiance.rv64
95
96
  git rev-parse HEAD > seed/radiance.rv64.git
97
98
  printf "\n"
99
  printf "Fixed point reached at %s\n" "$2"
100
  printf "Seed updated (commit $(git rev-parse --short HEAD))\n"
101
}
102
103
# ---------------------------------------------------------------------------
104
# Parse arguments
105
# ---------------------------------------------------------------------------
106
107
SEED="seed/radiance.rv64"
108
FROM_S0=0
109
110
while [ $# -gt 0 ]; do
111
  case "$1" in
112
    --seed)
113
      [ $# -ge 2 ] || fail "--seed requires an argument"
114
      SEED="$(realpath "$2")"
115
      shift 2
116
      ;;
117
    --from-s0)
118
      FROM_S0=1
119
      shift
120
      ;;
121
    *)
122
      fail "unknown option: $1"
123
      ;;
124
  esac
125
done
126
127
command -v "${RAD_EMULATOR:-emulator}" >/dev/null 2>&1 \
128
  || fail "emulator not found: set RAD_EMULATOR or add 'emulator' to PATH"
129
[ -f "${SEED}" ] || fail "seed binary not found: ${SEED}"
130
131
# ---------------------------------------------------------------------------
132
# Stage 1: seed compiles source
133
# ---------------------------------------------------------------------------
134
135
S1=seed/radiance.rv64.s1
136
S2=seed/radiance.rv64.s2
137
S3=seed/radiance.rv64.s3
138
S4=seed/radiance.rv64.s4
139
140
step "Stage 1: S0 builds S1"
141
if [ "${FROM_S0}" -eq 1 ]; then
142
  stageS0 "${SEED}" "${S1}"
143
else
144
  stage "${SEED}" "${S1}"
145
fi
146
147
printf "\nComparing S0 and S1 ..."
148
if compareStages "${SEED}" "${S1}"; then
149
  updateSeed "${SEED}" "S0"
150
  exit 0
151
fi
152
153
# ---------------------------------------------------------------------------
154
# Stage 2: S1 compiles source
155
# ---------------------------------------------------------------------------
156
157
step "Stage 2: S1 builds S2"
158
stage "${S1}" "${S2}"
159
160
printf "\nComparing S1 and S2 ..."
161
if compareStages "${S1}" "${S2}"; then
162
  updateSeed "${S1}" "S1"
163
  exit 0
164
fi
165
166
# ---------------------------------------------------------------------------
167
# Stage 3: S2 compiles source
168
# ---------------------------------------------------------------------------
169
170
step "Stage 3: S2 builds S3"
171
stage "${S2}" "${S3}"
172
173
printf "\nComparing S2 and S3 ..."
174
if compareStages "${S2}" "${S3}"; then
175
  updateSeed "${S2}" "S2"
176
  exit 0
177
fi
178
179
fail "No fixed point reached after 3 stages"