Skip to content

Commit f2050f3

Browse files
ChAoSUnItYvacantron
andcommitted
Refactor snapshot updating/checking scripts
- Adds .session.mk generation for consistent makefile execution behavior. - Refine document about snapshot test Co-authored-by: Meng-Hung Chen <[email protected]>
1 parent 5b47efd commit f2050f3

File tree

10 files changed

+70
-15
lines changed

10 files changed

+70
-15
lines changed

.github/workflows/main.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ jobs:
88
strategy:
99
matrix:
1010
compiler: [gcc-12, clang]
11+
architecture: [arm, riscv]
1112
steps:
1213
- name: checkout code
1314
uses: actions/checkout@v4
@@ -19,11 +20,8 @@ jobs:
1920
sudo apt-get install -q -y graphviz jq
2021
sudo apt-get install -q -y qemu-user
2122
sudo apt-get install -q -y build-essential
22-
make clean config
23-
make check-snapshots || exit 1
24-
make distclean config ARCH=arm
25-
make check || exit 1
26-
make distclean config ARCH=riscv
23+
make distclean config ARCH=${{ matrix.architecture }}
24+
make check-snapshot || exit 1
2725
make check || exit 1
2826
2927
host-arm:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ a.out
77
*.dot
88
config
99
src/codegen.c
10+
.session.mk

Makefile

+30-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ CFLAGS := -O -g \
1010
-Wno-format \
1111
-Wno-format-pedantic
1212

13+
BUILD_SESSION := .session.mk
14+
1315
include mk/common.mk
1416
include mk/arm.mk
1517
include mk/riscv.mk
18+
-include $(BUILD_SESSION)
1619

1720
STAGE0 := shecc
1821
STAGE1 := shecc-stage1.elf
1922
STAGE2 := shecc-stage2.elf
2023

2124
OUT ?= out
22-
ARCH ?= arm
25+
ARCHS = arm riscv
26+
ARCH ?= $(firstword $(ARCHS))
2327
SRCDIR := $(shell find src -type d)
2428
LIBDIR := $(shell find lib -type d)
2529

@@ -28,11 +32,11 @@ OBJS := $(SRCS:%.c=$(OUT)/%.o)
2832
deps := $(OBJS:%.o=%.o.d)
2933
TESTS := $(wildcard tests/*.c)
3034
TESTBINS := $(TESTS:%.c=$(OUT)/%.elf)
31-
SNAPSHOTS := $(patsubst tests/%.c, tests/snapshots/%.json, $(TESTS))
35+
SNAPSHOTS := $(foreach SNAPSHOT_ARCH,$(ARCHS), $(patsubst tests/%.c, tests/snapshots/%-$(SNAPSHOT_ARCH).json, $(TESTS)))
3236

3337
all: config bootstrap
3438

35-
ifeq (,$(filter $(ARCH),arm riscv))
39+
ifeq (,$(filter $(ARCH),$(ARCHS)))
3640
$(error Support ARM and RISC-V only. Select the target with "ARCH=arm" or "ARCH=riscv")
3741
endif
3842

@@ -45,6 +49,7 @@ config:
4549
$(Q)ln -s $(PWD)/$(SRCDIR)/$(ARCH)-codegen.c $(SRCDIR)/codegen.c
4650
$(call $(ARCH)-specific-defs) > $@
4751
$(VECHO) "Target machine code switch to %s\n" $(ARCH)
52+
$(Q)$(MAKE) $(BUILD_SESSION) --silent
4853

4954
$(OUT)/tests/%.elf: tests/%.c $(OUT)/$(STAGE0)
5055
$(VECHO) " SHECC\t$@\n"
@@ -63,8 +68,24 @@ check-stage2: $(OUT)/$(STAGE2) $(TESTBINS) tests/driver.sh
6368
tests/driver.sh 2
6469

6570
check-snapshots: $(OUT)/$(STAGE0) $(SNAPSHOTS) tests/check-snapshots.sh
66-
$(VECHO) " TEST SNAPSHOTS\n"
67-
tests/check-snapshots.sh
71+
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config check-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
72+
$(VECHO) "Switching backend back to %s\n" $(ARCH)
73+
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent
74+
75+
check-snapshot: $(OUT)/$(STAGE0) tests/check-snapshots.sh
76+
$(VECHO) "Checking snapshot for %s\n" $(ARCH)
77+
tests/check-snapshots.sh $(ARCH)
78+
$(VECHO) " OK\n"
79+
80+
update-snapshots: tests/update-snapshots.sh
81+
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config update-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
82+
$(VECHO) "Switching backend back to %s\n" $(ARCH)
83+
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent
84+
85+
update-snapshot: $(OUT)/$(STAGE0) tests/update-snapshots.sh
86+
$(VECHO) "Updating snapshot for %s\n" $(ARCH)
87+
tests/update-snapshots.sh $(ARCH)
88+
$(VECHO) " OK\n"
6889

6990
$(OUT)/%.o: %.c
7091
$(VECHO) " CC\t$@\n"
@@ -98,6 +119,9 @@ bootstrap: $(OUT)/$(STAGE2)
98119
echo "Unable to bootstrap. Aborting"; false; \
99120
fi
100121

122+
$(BUILD_SESSION):
123+
$(PRINTF) "ARCH=$(ARCH)" > $@
124+
101125
.PHONY: clean
102126
clean:
103127
-$(RM) $(OUT)/$(STAGE0) $(OUT)/$(STAGE1) $(OUT)/$(STAGE2)
@@ -107,6 +131,6 @@ clean:
107131
-$(RM) $(OUT)/libc.inc
108132

109133
distclean: clean
110-
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config
134+
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config $(BUILD_SESSION)
111135

112136
-include $(deps)

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,28 @@ $ chmod +x fib
107107
$ qemu-arm fib
108108
```
109109

110-
Verify that the emitted IRs are identical to the snapshots by specifying `check-snapshots` target when invoking `make`:
110+
### IR Regression Tests
111+
112+
To ensure the consistency of frontend (lexer, parser) behavior when working on it, the snapshot test is introduced.
113+
The snapshot test dumps IRs from the executable and compares the structural identity with the provided snapshots.
114+
115+
Verify the emitted IRs by specifying `check-snapshots` target when invoking `make`:
111116
```shell
112117
$ make check-snapshots
113118
```
114119

115-
`shecc` comes with unit tests consist of stage 0, stage 2. To run these tests, give `check` as an argument:
120+
If the compiler frontend is updated, the emitted IRs might be changed.
121+
Thus, you can update snapshots by specifying `update-snapshots` target when invoking `make`:
122+
```shell
123+
$ make update-snapshots
124+
```
125+
126+
Notice that the above 2 targets will update all backend snapshots at once, to update/check current backend's snapshot,
127+
use `update-snapshot` / `check-snapshot` instead.
128+
129+
### Unit Tests
130+
131+
`shecc` comes with unit tests. To run the tests, give `check` as an argument:
116132
```shell
117133
$ make check
118134
```

tests/check-snapshots.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ set -u
44

55
readonly SHECC="$PWD/out/shecc"
66

7+
if [ "$#" != 1 ]; then
8+
echo "Usage: $0 <architecture>"
9+
exit 1
10+
fi
11+
12+
readonly ARCH="$1"
13+
714
function check_snapshot() {
815
local source="$1"
9-
local ref="tests/snapshots/$(basename $source .c).json"
16+
local ref="tests/snapshots/$(basename $source .c)-$ARCH.json"
1017
local temp_exe=$(mktemp)
1118
local temp_json=$(mktemp --suffix .json)
1219

File renamed without changes.

tests/snapshots/fib-riscv.json

+1
Large diffs are not rendered by default.
File renamed without changes.

tests/snapshots/hello-riscv.json

+1
Large diffs are not rendered by default.

tests/update-snapshots.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ set -u
44

55
readonly SHECC="$PWD/out/shecc"
66

7+
if [ "$#" != 1 ]; then
8+
echo "Usage: $0 <architecture>"
9+
exit 1
10+
fi
11+
12+
readonly ARCH="$1"
13+
714
function update_snapshot() {
815
local source="$1"
9-
local dest="tests/snapshots/$(basename $source .c).json"
16+
local dest="tests/snapshots/$(basename $source .c)-$ARCH.json"
1017
local temp_exe=$(mktemp)
1118
local temp_json=$(mktemp --suffix .json)
1219

0 commit comments

Comments
 (0)