# Builds the RISC-V emulator.

PREFIX        ?= $(HOME)/bin
CC            := clang
CPPFLAGS      := -iquote .
CFLAGS        := -fvisibility=hidden -std=c99 -O3 -g \
                 -Wall -Wextra -Wpedantic \
                 -Wformat=2 -Wformat-security \
                 -Wnull-dereference \
                 -Wno-format-nonliteral \
                 -Wcast-align \
                 -Wunused -Wuninitialized \
                 -Wmissing-field-initializers \
                 -fno-common -fstack-protector-all \
                 -mcmodel=medium \
                 -march=native -flto
BLAKE3_CFLAGS := $(shell pkg-config --cflags libblake3)
BLAKE3_LIBS   := $(shell pkg-config --libs libblake3)
LDFLAGS       := -fuse-ld=lld -Wl,-z,stack-size=33554432
EMULATOR_SRCS := emulator.c abi_host.c abi_linux.c image_native.c image_rad0.c \
                 image_raw.c host_posix.c jit.c profile.c debug_map.c riscv/debug.c io.c riscv.c \
                 vector.c machine.c machine_cpu.c machine_csr.c machine_devices.c machine_fdt.c

default: bin/emulator

bin/emulator: $(EMULATOR_SRCS) profile.h debug_map.h jit.h bitmanip.h vector.h muldiv.h color.h types.h io.h host.h hosted.h riscv.h riscv/debug.h machine.h machine_internal.h
	@echo "cc    emulator => $@"
	@mkdir -p bin
	@$(CC) $(CPPFLAGS) $(CFLAGS) $(BLAKE3_CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) $(BLAKE3_LIBS) -o $@

install: bin/emulator
	@echo "copy  bin/emulator => $(PREFIX)/emulator"
	@mkdir -p $(PREFIX)
	@cp bin/emulator $(PREFIX)/emulator

fmt:
	git ls-files "*.c" "*.h" | xargs clang-format -i

clean:
	@rm -f bin/emulator

.PHONY: default clean install
.SUFFIXES:
.DELETE_ON_ERROR:
.SILENT:

# Check integer vector execution with host memory diagnostics.
bin/vector-test: test/vector.c vector.c vector.h types.h
	@mkdir -p bin
	@$(CC) $(CPPFLAGS) -std=c99 -Wall -Wextra -g -fsanitize=address,undefined test/vector.c vector.c -o $@

bin/vector-machine-test: test/vector_machine.c vector.c vector.h machine_cpu.c machine_csr.c machine_devices.c machine.c machine_fdt.c machine_internal.h machine.h types.h bitmanip.h muldiv.h
	@mkdir -p bin
	@$(CC) $(CPPFLAGS) -std=c99 -Wall -Wextra -g -fsanitize=address,undefined test/vector_machine.c vector.c machine_cpu.c machine_csr.c machine_devices.c machine.c machine_fdt.c -o $@

test-vector: bin/vector-test bin/vector-machine-test test-jit
	@ASAN_OPTIONS=detect_leaks=0 timeout 30 bin/vector-test
	@ASAN_OPTIONS=detect_leaks=0 timeout 30 bin/vector-machine-test

.PHONY: test-vector

# Compare hosted vector translation with architectural interpreter state.
bin/jit-vector-test: test/jit_vector.c jit.c jit.h profile.h vector.c vector.h riscv.c riscv.h riscv/debug.c riscv/debug.h bitmanip.h color.h types.h
	@mkdir -p bin
	@$(CC) $(CPPFLAGS) -std=c99 -O2 -g -Wall -Wextra -fsanitize=address,undefined test/jit_vector.c jit.c vector.c riscv.c riscv/debug.c -o $@

test-jit: bin/jit-vector-test test-jit-calls
	@ASAN_OPTIONS=detect_leaks=0 timeout 60 $<

# Compare translated and interpreted runs of calls, returns and LR/SC.
bin/jit-calls-gen: test/jit_calls.c
	@mkdir -p bin
	@$(CC) $(CPPFLAGS) -std=c99 -Wall -Wextra -g $< -o $@

test-jit-calls: bin/emulator bin/jit-calls-gen
	@bin/jit-calls-gen bin/jit-calls.bin
	@jit=$$(bin/emulator -run -count-instructions bin/jit-calls.bin 2>&1; \
	        echo "status $$?"); \
	 int=$$(bin/emulator -run -count-instructions -no-jit bin/jit-calls.bin 2>&1; \
	        echo "status $$?"); \
	 if [ "$$jit" != "$$int" ]; then \
	     echo "JIT call checks failed: '$$jit' != '$$int'"; exit 1; \
	 fi; \
	 echo "JIT call checks passed"

.PHONY: test-jit test-jit-calls
