# Makefile template for configure for the eBPF simulator
# Copyright (C) 2020 Free Software Foundation, Inc.
#
# This file is part of GDB, the GNU debugger.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

## COMMON_PRE_CONFIG_FRAG

CGEN_STD_OBJS = cgen-run.o cgen-scache.o cgen-trace.o cgen-utils.o
BPF_GEN_OBJS = arch.o cpu.o \
               decode-le.o decode-be.o \
               sem-le.o sem-be.o \
               mloop-le.o mloop-be.o
BPF_HAND_OBJS = bpf.o sim-if.o traps.o bpf-helpers.o

SIM_OBJS = \
	$(SIM_NEW_COMMON_OBJS) \
	$(CGEN_STD_OBJS) \
	$(BPF_GEN_OBJS) \
	$(BPF_HAND_OBJS)

SIM_EXTRA_DEPS = \
	$(CGEN_INCLUDE_DEPS) \
	arch.h \
	bpf-sim.h \
	$(srcdir)/../../opcodes/bpf-desc.h \
	$(srcdir)/../../opcodes/bpf-opc.h

SIM_EXTRA_CLEAN = bpf-clean

## COMMON_POST_CONFIG_FRAG

# cgen support, enable with --enable-cgen-maint
CGEN_MAINT = ; @true
# The following line is commented in or out depending upon --enable-cgen-maint.
@CGEN_MAINT@CGEN_MAINT =

# BPF headers

BPF_INCLUDE_DEPS = \
	$(CGEN_MAIN_CPU_DEPS) \
	$(SIM_EXTRA_DEPS) \
	cpu.h cpuall.h \
        decode-le.h decode-be.h \
        defs-le.h defs-be.h \
        eng-le.h eng-be.h \
        config.h

# Dependencies for binaries from CGEN generated source

arch.o: arch.c $(SIM_MAIN_DEPS)
cpu.o: cpu.c $(BPF_INCLUDE_DEPS)
decode-le.o: decode-le.c $(BPF_INCLUDE_DEPS)
decode-be.o: decode-be.c $(BPF_INCLUDE_DEPS)

sim-if.o: sim-if.c $(SIM_MAIN_DEPS) $(srcdir)/../common/sim-core.h eng.h
	$(COMPILE) $<
	$(POSTCOMPILE)

traps.o: traps.c $(SIM_MAIN_DEPS) eng.h
	$(COMPILE) $<
	$(POSTCOMPILE)

mloop-le.o: mloop-le.c $(BPF_INCLUDE_DEPS)
	$(CC) -c mloop-le.c $(ALL_CFLAGS) -DWANT_ISA_EBPFLE
mloop-be.o: mloop-be.c $(BPF_INCLUDE_DEPS)
	$(CC) -c mloop-be.c $(ALL_CFLAGS) -DWANT_ISA_EBPFBE

decode-le.o: decode-le.c $(BPF_INCLUDE_DEPS)
	$(CC) -c $(srcdir)/decode-le.c $(ALL_CFLAGS) -DWANT_ISA_EBPFLE
decode-be.o: decode-be.c $(BPF_INCLUDE_DEPS)
	$(CC) -c $(srcdir)/decode-be.c $(ALL_CFLAGS) -DWANT_ISA_EBPFBE

sem-le.o: sem-le.c $(BPF_INCLUDE_DEPS)
	$(CC) -c $(srcdir)/sem-le.c $(ALL_CFLAGS) -DWANT_ISA_EBPFLE
sem-be.o: sem-be.c $(BPF_INCLUDE_DEPS)
	$(CC) -c $(srcdir)/sem-be.c $(ALL_CFLAGS) -DWANT_ISA_EBPFBE

arch = bpf

CGEN_COMMON_DEPS = \
	$(CGEN_READ_SCM) \
	$(srcdir)/../../cpu/bpf.cpu \
	$(srcdir)/../../cpu/bpf.opc \
	Makefile

stamp-arch: $(CGEN_COMMON_DEPS) $(CGEN_ARCH_SCM)
	$(MAKE) cgen-arch $(CGEN_FLAGS_TO_PASS) \
		mach=bpf cpu=bpfbf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache"
	touch $@
$(srcdir)/arch.h $(srcdir)/arch.c $(srcdir)/cpuall.h: $(CGEN_MAINT) stamp-arch
	@true

stamp-cpu: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM)
	$(MAKE) cgen-cpu  $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfle,ebpfbe cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-multiple-isa with-scache"
	rm -f $(srcdir)/model.c
	touch $@
$(srcdir)/cpu.h $(srcdir)/cpu.c $(srcdir)/model.c: $(CGEN_MAINT) stamp-cpu
	@true

# We need to generate a group of files per ISA.
# For eBPF little-endian:
#    defs-le.h
#    sem-le.c, decode-le.c, decode-le.h
#    $(objdir)/mloop-le.c $(objdir)/eng-le.h
# For eBPF big-endian:
#    defs-be.h
#    sem-be.c, decode-be.c, decode-be.h
#    $(objdir)/mloop-be.c $(objdir)/eng-le.h
#
# The rules below take care of that.

stamp-defs-le: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM)
	$(MAKE) cgen-defs $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfle cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-le"
	touch $@
$(srcdir)/defs-le.h: $(CGEN_MAINT) stamp-defs-le
	@true


stamp-defs-be: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM)
	$(MAKE) cgen-defs $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfbe cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-be"
	touch $@
$(srcdir)/defs-be.h: $(CGEN_MAINT) stamp-defs-be
	@true

stamp-decode-le: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM) $(GEN_DECODE_SCM)
	$(MAKE) cgen-decode $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfle cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-le" \
		EXTRAFILES="$(CGEN_CPU_SEM)"
	touch $@
$(srcdir)/sem-le.c $(srcdir)/decode-le.c $(srcdir)/decode-le.h: \
              $(CGEN_MAINT) stamp-decode-le
	@true


stamp-decode-be: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM) $(GEN_DECODE_SCM)
	$(MAKE) cgen-decode $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfbe cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-be" \
		EXTRAFILES="$(CGEN_CPU_SEM)"
	touch $@
$(srcdir)/sem-be.c $(srcdir)/decode-be.c $(srcdir)/decode-be.h: \
              $(CGEN_MAINT) stamp-decode-be
	@true

# Note the following files are generated in objdir, not srcdir.

stamp-mloop: stamp-mloop-le stamp-mloop-be

stamp-mloop-le: $(srcdir)/../common/genmloop.sh mloop.in Makefile
	$(SHELL) $(srccom)/genmloop.sh -shell $(SHELL) \
		-mono -scache -prefix bpfbf_ebpfle -cpu bpfbf \
                -infile $(srcdir)/mloop.in -outfile-suffix -le
	$(SHELL) $(srcroot)/move-if-change eng-le.hin eng-le.h
	$(SHELL) $(srcroot)/move-if-change mloop-le.cin mloop-le.c
	touch $@
mloop-le.c eng-le.h: stamp-mloop-le
	@true

stamp-mloop-be: $(srcdir)/../common/genmloop.sh mloop.in Makefile
	$(SHELL) $(srccom)/genmloop.sh -shell $(SHELL) \
		-mono -scache -prefix bpfbf_ebpfbe -cpu bpfbf \
                -infile $(srcdir)/mloop.in -outfile-suffix -be
	$(SHELL) $(srcroot)/move-if-change eng-be.hin eng-be.h
	$(SHELL) $(srcroot)/move-if-change mloop-be.cin mloop-be.c
	touch $@
mloop-be.c eng-be.h: stamp-mloop-be
	@true

.PHONY = bpf-clean

bpf-clean:
	rm -f stamp-arch stamp-cpu stamp-decode stamp-defs stamp-mloop
