Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor snapshot updating/checking scripts #172

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
strategy:
matrix:
compiler: [gcc-12, clang]
architecture: [arm, riscv]
steps:
- name: checkout code
uses: actions/checkout@v4
Expand All @@ -19,11 +20,8 @@ jobs:
sudo apt-get install -q -y graphviz jq
sudo apt-get install -q -y qemu-user
sudo apt-get install -q -y build-essential
make clean config
make check-snapshots || exit 1
make distclean config ARCH=arm
make check || exit 1
make distclean config ARCH=riscv
make distclean config ARCH=${{ matrix.architecture }}
make check-snapshot || exit 1
make check || exit 1

host-arm:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ a.out
*.dot
config
src/codegen.c
.session.mk
36 changes: 30 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ CFLAGS := -O -g \
-Wno-format \
-Wno-format-pedantic

BUILD_SESSION := .session.mk

include mk/common.mk
include mk/arm.mk
include mk/riscv.mk
-include $(BUILD_SESSION)

STAGE0 := shecc
STAGE1 := shecc-stage1.elf
STAGE2 := shecc-stage2.elf

OUT ?= out
ARCH ?= arm
ARCHS = arm riscv
ChAoSUnItY marked this conversation as resolved.
Show resolved Hide resolved
ARCH ?= $(firstword $(ARCHS))
SRCDIR := $(shell find src -type d)
LIBDIR := $(shell find lib -type d)

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

all: config bootstrap

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

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

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

check-snapshots: $(OUT)/$(STAGE0) $(SNAPSHOTS) tests/check-snapshots.sh
$(VECHO) " TEST SNAPSHOTS\n"
tests/check-snapshots.sh
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config check-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
$(VECHO) "Switching backend back to %s\n" $(ARCH)
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent

check-snapshot: $(OUT)/$(STAGE0) tests/check-snapshots.sh
$(VECHO) "Checking snapshot for %s\n" $(ARCH)
tests/check-snapshots.sh $(ARCH)
$(VECHO) " OK\n"

update-snapshots: tests/update-snapshots.sh
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config update-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
$(VECHO) "Switching backend back to %s\n" $(ARCH)
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent

update-snapshot: $(OUT)/$(STAGE0) tests/update-snapshots.sh
$(VECHO) "Updating snapshot for %s\n" $(ARCH)
tests/update-snapshots.sh $(ARCH)
$(VECHO) " OK\n"

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

$(BUILD_SESSION):
$(PRINTF) "ARCH=$(ARCH)" > $@

.PHONY: clean
clean:
-$(RM) $(OUT)/$(STAGE0) $(OUT)/$(STAGE1) $(OUT)/$(STAGE2)
Expand All @@ -107,6 +131,6 @@ clean:
-$(RM) $(OUT)/libc.inc

distclean: clean
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config $(BUILD_SESSION)

-include $(deps)
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,28 @@ $ chmod +x fib
$ qemu-arm fib
```

Verify that the emitted IRs are identical to the snapshots by specifying `check-snapshots` target when invoking `make`:
### IR Regression Tests

To ensure the consistency of frontend (lexer, parser) behavior when working on it, the snapshot test is introduced.
The snapshot test dumps IRs from the executable and compares the structural identity with the provided snapshots.

Verify the emitted IRs by specifying `check-snapshots` target when invoking `make`:
```shell
$ make check-snapshots
```

`shecc` comes with unit tests consist of stage 0, stage 2. To run these tests, give `check` as an argument:
If the compiler frontend is updated, the emitted IRs might be changed.
Thus, you can update snapshots by specifying `update-snapshots` target when invoking `make`:
```shell
$ make update-snapshots
```

Notice that the above 2 targets will update all backend snapshots at once, to update/check current backend's snapshot,
use `update-snapshot` / `check-snapshot` instead.

### Unit Tests

`shecc` comes with unit tests. To run the tests, give `check` as an argument:
```shell
$ make check
```
Expand Down
9 changes: 8 additions & 1 deletion tests/check-snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ set -u

readonly SHECC="$PWD/out/shecc"

if [ "$#" != 1 ]; then
echo "Usage: $0 <architecture>"
exit 1
fi

readonly ARCH="$1"

function check_snapshot() {
local source="$1"
local ref="tests/snapshots/$(basename $source .c).json"
local ref="tests/snapshots/$(basename $source .c)-$ARCH.json"
local temp_exe=$(mktemp)
local temp_json=$(mktemp --suffix .json)

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions tests/snapshots/fib-riscv.json

Large diffs are not rendered by default.

File renamed without changes.
1 change: 1 addition & 0 deletions tests/snapshots/hello-riscv.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion tests/update-snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ set -u

readonly SHECC="$PWD/out/shecc"

if [ "$#" != 1 ]; then
echo "Usage: $0 <architecture>"
exit 1
fi

readonly ARCH="$1"

function update_snapshot() {
local source="$1"
local dest="tests/snapshots/$(basename $source .c).json"
local dest="tests/snapshots/$(basename $source .c)-$ARCH.json"
local temp_exe=$(mktemp)
local temp_json=$(mktemp --suffix .json)

Expand Down