#
# Copyright (C) 1998-2001, The University of Queensland
# Copyright (C) 2000-2001, Sun Microsystems, Inc
#
# See the file "LICENSE.TERMS" for information on usage and 
# redistribution of this file, and for a DISCLAIMER OF ALL 
# WARRANTIES. 
# 
# File: Makefile
# Desc: Makefile for the UQBT tool. Called from a shell script called "make",
#	which passes a multitude of parameters specifying source and target
#	dependent names, directories, etc
#
# Copyright (C) 1998-2001, The University of Queensland, BT group
# Copyright (C) 2000-2001, Sun Microsystems, Inc
#
######################################################

# $Revision: 1.44 $
# Note: gnu make is required (for ifdef etc)

# 5 Feb 98 - Mike
#	Added RTL directory to the include path
#   Added rules and dependencies for RTL files (based on Doug's SRD Makefile)
# 13 Feb 98 - Mike
#	Added the -frtti switch
# 16 Feb 98 - Cristina
#	Added CFG dependencies
# 3 Mar 98 - Cristina
#	Added BINARYFILE dependencies
# 5 Mar 98 - Mike
#	Added VPOBACK dependancies
# 5 Mar 98 - Cristina
#	Added PROC dependencies
# 11 Mar 98 - Mike
#	Fixed __EXTENSIONS__ for scanner.c (prevents many warnings due to the
#	definition for strdup())
# 24 Mar 98 - Cristina
#	Added PROG dependencies
# 27 Mar 98 - Mike
#	Added make make capability
# 2 Jun 98 - Mike
#	Changed Loader to BinaryFile
# 4 Jun 98 - Mike
#	Removed Pair.VHSet.* etc since not using the -f logic
# 23 Jul 98 - Mike
#	Removed register.o etc from SRD-OBJS; added ss.o, miniparser.o etc
#	Also removed rtti
# 6 Aug 98 - Mike
#	Mods for new source dependent front end
# 13 Aug 98 - Mike
#	Mods for control transfer source dependent code
# 11 Sep 98 - Mike: added switch.o; beefed up the dependencies
# 23 Sep 98 - Mike: added -DSPARC_TARG, pending a proper src/targ scheme
# 03 Nov 98 - Mike:
#	removed -ansi since it seems to cause problems with 64 bit integers
# 28 Jan 99 - Mike: Added reg.o for Ian's descriptive registers
# 05 Feb 99 - Mike: Added more dependencies
# 18 Feb 99 - Mike: integration with interpreter
# 19 Feb 99 - Mike: miniparser.o depends on ss.h
# 03 Mar 99 - Doug: Sparc decoders now generated by SML version of toolkit
# 04 Mar 99 - Mike: Finally properly fixed dependencies for scanner and parser
# 09 Mar 99 - Mike: removed interpreter
# 10 Mar 99 - Mike: removed jcond.o
# 06 Apr 99 - Mike: changed ARCH to ARCH_S
# 07 Apr 99 - Mike: Changes for HEADERS directory; removed PROG/graph.cc
# 08 Apr 99 - Mike: Removed Load_elf.cc module
# 16 Apr 99 - Doug: changed interface.o to csr.o
# 16 Apr 99 - Mike: Dependency for FRONT-PROC on BinaryFile.h
# 21 Apr 99 - Mike: Added -Wno-return-type to the general CC macro; supposed to
#				make processing of STL code less memory hungry (hence faster)
# 03 May 99 - Mike: Renamed back to Makefile again. Has to be run from the
#				"make" script now. Added backend files.
# 11 May 99 - Doug: Added -I${PROCDIR} to CC_NH so that decoder.h can be
#                   included
# 12 May 99 - Mike: added CFG/bitset.cc
# 13 May 99 - Mike: SRC= and TGT=
# 13 May 99 - Doug: Added rules and dependencies for basicblock.o
# 17 May 99 - Mike: Replaced SPARCDIR etc (make clean deletes decoder.o now)
# 18 May 99 - Mike: Removed -DNJ; added -Wno-unused for compiling decoder.cc;
#				added frontend.o
# 25 May 99 - Doug: Added DEBUG_CSR flag
# 02 Jun 99 - Mike: translate2c backend
# 10 Jun 99 - Mike: Made the -g1 for decoder.cc optional
# 15 Jun 99 - Mike: Replaced -Wno-return-type; this one is actually useful, and
#					despite advertisements to the contrary, doesn't seem to slow
#					down compiles appreciably
# 17 Jun 99 - Mike: Removed semtable.o again
# 09 Jul 99 - Mike: Added make remote
# 16 Jul 99 - Mike: Use GENSRC now for cleanAll target
# 26 Jul 99 - Doug: Removed all VPO backend objects and as such
#                   removed need for CC_NH
# 10 Aug 99 - Doug: Removed NJDIR define and usage (don't need it for
#                   decoding apps)
# 17 Aug 99 - Mike: removed util.cc from dependencies of PATTERN-PARSER-DEPS;
#					was making all the pattern stuff for no good reason 
# 25 Nov 99 - Mike: More dependencies on options.h
# 09 Feb 00 - Mike: Changes for mc68k
# 18 Feb 00 - Mike: Fixed "make remote" for mc68k
# 13 Mar 00 - Cristina: changed ordering of files in GENSRC ("make remote")
#                   so works properly
# 09 Jun 00 - Mike: added coverage.o
# 16 Jun 00 - Mike: Moved front end processor specific files to approp dir
# 23 Jun 00 - Mike: Added HPPA
# 03 Jul 00 - Mike: Added TRANSTIME directory for translate time sources
# 19 Jul 00 - Cristina: mltk.sh now takes a new (first) parameter (sun
#                   or uq) to setup paths
# 21 Jul 00 - Mike: mltk.sh argument now supplied by define UQBTSITE
#  3 Aug 00 - Brian: Added target "make cscope" to create a cscope database
#                    so that the sources can be browsed easily.
#  4 Aug 00 - Brian: Changed CSR to PAL to reflect that module name change.
# 21 Aug 00 - Mike: "Copied a lot message" properly commented out
# 30 Aug 00 - Brian: Added Sameer's changes for the new JVM backend
#  2 Jan 01 - Cristina: Added GUI/toupper to the utilities section
# 17 Jan 01 - Brian: Added new files for "make cscope" to cross-reference.
# 22 Jan 01 - Mike: Added TRANSTIME and JVM to ALLOBJ so that "make clean" will
#             be complete
#  6 Feb 01 - Brian: Commented out lines that ran xtools to recreate
#                    sparc-names.{h,c} and mc68000-names.{h,c}. These files
#                    are now treated as immutable.
#  8 Feb 01 - Mike: Added files for Overlap class
#  5 Mar 01 - Simon: Support for multiple tables per instruction
#  8 Mar 01 - Mike: DEBUG_PAL -> DEBUG_CSR to agree with use in csr.cc
#  9 Mar 01 - Mike: Added util/*.o to ALLOBJ (make clean didn't clean utils)
# 16 Mar 01 - Mike: Added ExeBinaryFile.o
# 21 Mar 01 - Mike: "make change" deletes analysis.o now
# 24 Mar 01 - Mike: Added 286 directory and files
# 27 Mar 01 - Brian: Fixed unterminated variable reference in GENSRC.
# 27 Mar 01 - Mike: "make change" deletes backend/c/overlap.o now
# 28 Mar 01 - Mike: Redirect all mltk.sh output to mltk.err now
# 04 Apr 01 - Manel: Moved many backend files to backend/common
# 06 Apr 01 - Mike: Dependencies for some files like backend.o weren't
#                    updated
#  8 Apr 01 - Brian: removed Makefiles from set of files cscope scans.
#  9 Apr 01 - Brian: cscope must not scan the docs directory: it holds old
#                    versions of header files.
# 13 Apr 01 - Manel: Changes to support the Postoptimizer backend.
# 16 Apr 01 - Brian: Suppress the amazing number of warnings when compiling
#                    sparc-encoder.c and mclib.c.
# 17 Apr 01 - Mike: Added backend/po/sparc-encoder.[ch] to $GENSRC so can
#                    "make remote" if no NJMC toolkit available; also a few
#                    other Linux or (SRC != TGT) changes
# 01 May 01 - Nathan: Moved to Makefile.in and updated for autoconf
# 07 May 01 - Brian: Fixed "--enable-po" bug where a rule assigned to an
#              otherwise unused variable.
# 08 May 01 - Nathan: Fixed building from other directories
# 08 May 01 - Nathan: Brought disassembler and bffDump under central make
# 10 May 01 - Mike: Use ${UQBTSITE} to choose arg to mltk.sh
# 11 May 01 - Mike: Don't build disasm if there DISASM_BIN not defined
# 11 May 01 - Nathan: Build loader objects as loader/libloader.a
# 11 May 01 - Mike: Main target is now uqbtxy where x is SRC and y is TGT
# 14 may 01 - Brian: Modifies "make cscope.files" to not include translated
#              sources in the test subdirectory.
# 16 May 01 - Simon: Updated some dependencies, eg csrparser.cc on scrscanner.h
# 17 Jun 01 - Brian: Added support for the ARM VPO backend.
# 25 Jul 01 - Brian: Added support for the ARM and different VPO backends.
#              Changed "make clean" to also remove config.cache.
# 30 Jul 01 - David: Instrumentation support to the emulator added.
# 31 Jul 01 - Brian: File hrtl.cc now contains the code that was in
#              rtlist.cc and hrtlist.cc.
# 13 Aug 01 - Bernard: Added support for type analysis
# 17 Aug 01 - Mike: Suppress warnings about EMU with "ifdef EMU_BIN" etc
# 21 Aug 01 - Mike: Added a few lines for making Jens' uqbt library
# 21 Aug 01 - Bernard: Separated type analysis to separate files and introduced
#              type analysis stubs
# 22 Aug 01 - Brian: Changes required for move of emu, tools, emuDebug files to
#              inside-Sun dynamic repository.
# 23 Aug 01 - Brian: Don't link with VPO libraries unless target=SPARC or ARM.
# 11 Sep 01 - Cristina: Separated emulators being generated; sparcemu and pathfinder
# 24 Oct 01 - Mike: machine/pent -> machine/pentium

### Variables set by autoconf

# Paths
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
datadir=@datadir@
libdir=@libdir@
includedir=@includedir@
mandir=@mandir@
srcdir=@srcdir@
VPATH=@srcdir@
INSTR_DIR=@INSTR_DIR@
INSTRM=@INSTRM@

COPTS=@CFLAGS@
CDEFS=@DEFS@
CPPFLAGS=@CPPFLAGS@
CXXOPTS=@CXXFLAGS@
DEBUGS=@DEBUGS@
LDFLAGS=@LDFLAGS@
LIBS=@LIBS@

CFLAGS=${COPTS} ${CDEFS} ${DEBUGS}
CXXFLAGS=${CDEFS} ${DEBUGS}

# Note we don't take CXXOPTS at the moment, because it insists on giving -O2

# Programs
CC=@CC@
CXX=@CXX@
LEX=@LEX@
YACC=@YACC@
AR=@AR@
RANLIB=@RANLIB@
FLEXPP=@FLEXPP@
BISONPP=@BISONPP@
JAVAC=@JAVAC@
UQBTSITE=sun            # Can be overridden by an environment var; use make -e
MLTK=${srcdir}/mltk.sh ${UQBTSITE}

# Conditionals (1 = yes, 0 = no)
REMOTE=@REMOTE@
BUILDTOOLS=@TOOLS@
BUILDDYNAMIC=@DYNAMIC@

# List of backends to build
BACKENDS=@BACKENDS@

# VPO backend object files and libraries
VPOBACKEND=@VPOBACKEND@
VPOLIBS=@VPOLIBS@

###

UQBT := @TRANSLATOR@
LOADER_LIB := loader/libloader.a

all: all-uqbt

SPARCDIR = machine/sparc
386DIR = machine/pentium
MC68KDIR = machine/mc68k
HPPADIR = machine/hppa
286DIR = machine/286

ALLBIN = uqbt[spmwh][spmwh]
DYNBIN = dynamic/emu/${EMU_BIN} dynamic/emu/${EMU_BIN_JAVA} \
			dynamic/emu/${INSTRM_EMU_BIN}
ALLDYNBIN = dynamic/tools/genemu ${DYNBIN}

UTILS = backend/runtime/changerel backend/runtime/fixLabels gui/toupper
TOOLS = pal/parsepatterns loader/bffDump
ifdef DISASM_BIN
    TOOLS += disasm
endif
ifeq (1,${BUILDDYNAMIC})
    TOOLS += dynamic/tools/genemu
endif
MAPFILE =

###############################
# General rules
###############################

# Try to stop .pat files matching the automatic rules
%.pat:

INCLUDES = -I. -Iinclude -I${srcdir}/include -I${srcdir}/${PROC_DIR}
CPPFLAGS += -DUNIX ${INCLUDES}
CXXFLAGS += -Wall -g3
CFLAGS   += -Wall -g3

XTOOLS=xtools

# Generated source files. For "make remote" or "real clean"
GENSRC = \
	include/typeAnalysis.h typeAnalysis/typeAnalysis.cc \
	rtl/sslparser.cc include/sslparser.h \
	rtl/sslscanner.cc include/sslscanner.h \
	pal/csrparser.cc include/csrparser.h \
	pal/csrscanner.cc include/csrscanner.h \
	pal/patternparser.cc include/patternparser.h \
	pal/patternscanner.cc include/patternscanner.h \
	include/sledparser.h \
	include/instrm_parser.h include/instrm_scanner.h \
	backend/po/sparc-encoder.c backend/po/sparc-encoder.h \
	${SPARCDIR}/decoder_low.cc ${386DIR}/decoder_low.cc \
	${MC68KDIR}/decoder_low.cc ${HPPADIR}/decoder_low.cc \
	${286DIR}/decoder_low.cc \
	${SPARCDIR}/sparc.pat.m ${SPARCDIR}/sparc.pat.h \
	${SPARCDIR}/sparc.pat.cc \
	${386DIR}/386.pat.m ${386DIR}/386.pat.h ${386DIR}/386.pat.cc \
	${MC68KDIR}/mc68k.pat.m ${MC68KDIR}/mc68k.pat.h \
	${MC68KDIR}/mc68k.pat.cc \
	${HPPADIR}/hppa.pat.m ${HPPADIR}/hppa.pat.h ${HPPADIR}/hppa.pat.cc \
	${386DIR}/286.pat.m ${286DIR}/386.pat.h ${286DIR}/386.pat.cc \
	${SPARCDIR}/decoder.cc ${386DIR}/decoder.cc  ${MC68KDIR}/decoder.cc \
	${HPPADIR}/decoder.cc ${286DIR}/decoder.cc

DYNGENSRC = \
	include/typeAnalysis.h typeAnalysis/typeAnalysis.cc \
	rtl/sslparser.cc include/sslparser.h \
	rtl/sslscanner.cc include/sslscanner.h \
	pal/csrparser.cc include/csrparser.h \
	pal/csrscanner.cc include/csrscanner.h \
	pal/patternparser.cc include/patternparser.h \
	pal/patternscanner.cc include/patternscanner.h \
	dynamic/tools/sledparser.cc include/sledparser.h \
	dynamic/tools/sledscanner.cc \
	dynamic/tools/instrm/instrm_parser.cc \
	dynamic/tools/instrm/instrm_scanner.cc \
	${DYN_GEN_H} ${DYN_GEN_CC} ${DYN_GEN_M} \
	dynamic/emu/${EMU_JAVA_LIB} \
	include/instrm_parser.h include/instrm_scanner.h \
	backend/po/sparc-encoder.c backend/po/sparc-encoder.h \
	${SPARCDIR}/decoder_low.cc ${386DIR}/decoder_low.cc \
	${MC68KDIR}/decoder_low.cc ${HPPADIR}/decoder_low.cc \
	${286DIR}/decoder_low.cc \
	${SPARCDIR}/sparc.pat.m ${SPARCDIR}/sparc.pat.h \
	${SPARCDIR}/sparc.pat.cc \
	${386DIR}/386.pat.m ${386DIR}/386.pat.h ${386DIR}/386.pat.cc \
	${MC68KDIR}/mc68k.pat.m ${MC68KDIR}/mc68k.pat.h \
	${MC68KDIR}/mc68k.pat.cc \
	${HPPADIR}/hppa.pat.m ${HPPADIR}/hppa.pat.h ${HPPADIR}/hppa.pat.cc \
	${386DIR}/286.pat.m ${286DIR}/386.pat.h ${286DIR}/386.pat.cc \
	${SPARCDIR}/decoder.cc ${386DIR}/decoder.cc  ${MC68KDIR}/decoder.cc \
	${HPPADIR}/decoder.cc ${286DIR}/decoder.cc

# Lines removed by Brian 2/5/01 to avoid attempt to run "xtools" to create
# the "-names" files. These files are now treated as immutable. Remember to
# add the appropriate name filenames to the definition of GENSRC and to
# remake the name files when adding support for a new architecture.
#    ${SPARCDIR}/sparc-names.c  ${SPARCDIR}/sparc-names.h \
#    ${MC68KDIR}/mc68000-names.c ${MC68KDIR}/mc68000-names.h \

#####
# Object files
#####

loader-OBJS = loader/BinaryFile.o loader/ElfBinaryFile.o loader/SymTab.o \
              loader/PalmBinaryFile.o loader/Win32BinaryFile.o \
              loader/HpSomBinaryFile.o loader/ExeBinaryFile.o \
	      loader/ElfDetails.o

LOADER_SHOBJS := $(loader-OBJS:%.o=%.lo)

SRD-OBJS = typeAnalysis/typeAnalysis.o \
	   rtl/sslscanner.o rtl/sslparser.o \
	   rtl/hrtl.o rtl/rt.o rtl/ss.o rtl/sslinst.o \
	   rtl/table.o rtl/reg.o rtl/insnameelem.o rtl/sselem.o \
           rtl/rttelem.o rtl/sstype.o

PAL-OBJS = pal/csrparser.o pal/csrscanner.o pal/csr.o 

CFG-OBJS = cfg/cfg.o cfg/graph.o cfg/basicblock.o

LIBOBJS = ${SRD-OBJS} ${PAL-OBJS} ${CFG-OBJS} ${PROC-OBJS} \
          ${PROG-OBJS} ${FRONT-OBJS} ${loader-OBJS} ${UTIL-OBJS} \
          ${BACK-OBJS} ${SSL-OBJS} ${SLED-OBJS} ${ARCH_OBJ} \
          ${TYPE-OBJS} \
          switch.o analysis.o NJMCdecoder.o driver.o

# The following are for the common stuff of the backend
BACK-OBJS := backend/common/backend.o backend/common/overlap.o backend/common/outfile.o \
             backend/common/expander.o backend/common/njexp.o backend/common/mclib.o \
             backend/common/instance.o

# The following are for the C version of the backend
ifeq ($(filter c,$(BACKENDS)),c)
BACK-OBJS += backend/c/translate2c.o
endif

# The following is for the Java version of the backend
ifeq ($(filter jvm,$(BACKENDS)),jvm)
BACK-OBJS += backend/jvm/translate2jvm.o
endif

# The following is for the PostOptimizer version of the backend
ifeq ($(filter po,$(BACKENDS)),po)
BACK-OBJS   += backend/po/sparc-encoder.o backend/po/sparcnjexp.o
endif

# The following is for one of the VPO backends, SPARC or ARM
ifeq ($(filter vpo,$(BACKENDS)),vpo)
BACK-OBJS += ${VPOBACKEND}
LIBS += ${VPOLIBS}
endif

PROC-OBJS = proc/proc.o

PROG-OBJS = prog/prog.o

FRONT-OBJS= frontend.o coverage.o

UTIL-OBJS = util/util.o util/type.o util/bitset.o util/locationmap.o \
	    util/error.o util/bitstring.o

SLED-OBJS = dynamic/tools/sledscanner.o dynamic/tools/sledparser.o \
	dynamic/tools/sledtree.o

INSTRM-OBJS = dynamic/tools/instrm/instrm_parser.o \
	dynamic/tools/instrm/instrm_scanner.o

SSL-OBJS  = rtl/sslscanner.o rtl/sslparser.o \
	    rtl/hrtl.o rtl/rt.o rtl/ss.o rtl/sslinst.o \
	    rtl/table.o rtl/reg.o rtl/insnameelem.o \
	    rtl/sselem.o rtl/rttelem.o rtl/sstype.o 

GENEMU-OBJS = dynamic/tools/codegen.o dynamic/tools/genemu.o \
	dynamic/tools/gendasm.o dynamic/tools/gendecode.o \
	dynamic/tools/genregs.o dynamic/tools/genss.o \
	dynamic/tools/codegen_c.o dynamic/tools/codegen_java.o \
	dynamic/tools/match.o

TYPE-OBJS = typeAnalysis/analyzeBlocks.o typeAnalysis/typeLex.o \
	typeAnalysis/typeAnalysis.o typeAnalysis/HRTLUDStruct.o \
	typeAnalysis/RTUDStruct.o

EMUDEBUG-OBJS = dynamic/emu/personality.o dynamic/emu/solaris.o \
	dynamic/emuDebug/src/emu/emuLib.o \
	dynamic/emuDebug/src/emu/emumain.o dynamic/emu/linux.o ${EMU_C_OBJ} \
	dynamic/emu/sysv.o

EMUDEBUG-JAVA = dynamic/emuDebug/src/RegFile.java \
	dynamic/emuDebug/src/disasm.java \
	dynamic/emuDebug/src/emuDebug.java \
	dynamic/emuDebug/src/emuProcess.java \
	disasm/genericDis.java ${DISASM_JAVA_DEBUG}

EMUDEBUGGEN-JAVA = dynamic/emuDebug/src/emuDebugGen/CodeBlock.java \
	dynamic/emuDebug/src/emuDebugGen/CodeGen.java \
	dynamic/emuDebug/src/emuDebugGen/ReplacementStruct.java \
	dynamic/emuDebug/src/emuDebugGen/Token.java \
	dynamic/emuDebug/src/emuDebugGen/TokenReader.java \
	dynamic/emuDebug/src/emuDebugGen/errorToken.java \
	dynamic/emuDebug/src/emuDebugGen/identToken.java \
	dynamic/emuDebug/src/emuDebugGen/intToken.java \
	dynamic/emuDebug/src/emuDebugGen/labelToken.java \
	dynamic/emuDebug/src/emuDebugGen/symbolLookup.java 

OBJS := ${SRD-OBJS} ${PAL-OBJS} ${CFG-OBJS} ${PROC-OBJS} \
	${PROG-OBJS} ${FRONT-OBJS} ${LOADER_LIB} ${UTIL-OBJS} \
	${BACK-OBJS} driver.o switch.o analysis.o NJMCdecoder.o \
	${TYPE-OBJS}

###############################
# Processor specific section
###############################

@src_rules@
ifeq (1, ${INSTRM})
@extra_rules@
endif


####
# M a i n   t a r g e t
####

ifeq (0,${BUILDTOOLS})
all-uqbt: ${UQBT} ${UTILS}
else
all-uqbt: ${UQBT} ${UTILS} ${TOOLS}
endif


# The binary, e.g. uqbtss
#EXTRA_LIBS = -lefence
${UQBT}: ${OBJS} ${ARCH_OBJ}
	${CC} $^ -o $@ ${EXTRA_LIBS} ${LIBS}

$(LOADER_LIB): $(loader-OBJS)                                 
	rm -f $@           
	$(AR) r $@ $^
	$(RANLIB) $@

# UQBT as a library, for Jens' work
uqbtlib: $(LIBOBJS)
	rm lib/libuqbt.a
	$(AR) r lib/libuqbt.a ${LIBOBJS}
	${RANLIB} lib/libuqbt.a

ifeq (0,${REMOTE})

%.pat.m %.pat.h: %.pat pal/parsepatterns
	pal/parsepatterns $<



rtl/sslscanner.cc include/sslscanner.h: rtl/sslscanner.l rtl/sslparser.y
	${FLEXPP} -ortl/sslscanner.cc -hinclude/sslscanner.h $<

rtl/sslparser.cc include/sslparser.h: rtl/sslparser.y rtl/sslscanner.l \
		include/rttelem.h
	${BISONPP} -d -v -ortl/sslparser.cc -hinclude/sslparser.h $<

pal/csrparser.cc include/csrparser.h: pal/csrparser.y include/csrscanner.h
	${BISONPP} -d -v -opal/csrparser.cc -hinclude/csrparser.h $<

pal/csrscanner.cc include/csrscanner.h: pal/csrscanner.l
	${FLEXPP} -h -opal/csrscanner.cc -hinclude/csrscanner.h $<

pal/patternparser.cc include/patternparser.h: pal/patternparser.y
	${BISONPP} -d -v -hinclude/patternparser.h -o$@ $<

pal/patternscanner.cc include/patternscanner.h: pal/patternscanner.l
	${FLEXPP} -hinclude/patternscanner.h -o$@ $<

typeAnalysis/typeAnalysis.cc include/typeAnalysis.h: typeAnalysis/typeAnalysis.y
	${BISONPP} -d -v -hinclude/typeAnalysis.h -otypeAnalysis/typeAnalysis.cc $<


backend/po/sparc-encoder.c backend/po/sparc-encoder.h: include/mclib.h \
    machine/sparc/sparc-core.spec machine/sparc/sparc-synth.spec
	tools -encoder sparc-encoder -byteorder b -indirect sparc_enc:sparc_enc_t \
        machine/sparc/sparc-core.spec machine/sparc/sparc-synth.spec
	mv sparc-encoder.* backend/po
endif

# Suppress the amazing number of warnings when compiling sparc-encoder.cc,
# which is generated by the NJTK (see comment above).
backend/po/sparc-encoder.o: backend/po/sparc-encoder.c include/mclib.h \
    backend/po/sparc-encoder.h
	${CC} ${CFLAGS} ${CPPFLAGS} -c -w -o $@ $<

# Ditto for backend/common/mclib.c
backend/common/mclib.o: backend/common/mclib.c
	${CC} ${CFLAGS} ${CPPFLAGS} -c -w -o $@ $<

#	#	#	#	#	#	#
#						#
#	U t i l i t i e s   #
#						#
#	#	#	#	#	#	#
CROBJS = backend/runtime/changerel.o ${loader-OBJS} util/error.o

backend/runtime/changerel: ${CROBJS}
	${CC} -o backend/runtime/changerel ${CROBJS} ${LIBS}
# This will be copied a lot, so make it as small as possible
	strip backend/runtime/changerel

backend/runtime/changerel.o: backend/runtime/changerel.cc
	${CXX} ${CXXFLAGS} ${CPPFLAGS} -O4 -c -o $@ $<

backend/runtime/fixLabels: backend/runtime/fixLabels.cc
	${CXX} ${CXXFLAGS} ${CPPFLAGS} -O4 -o $@ $<
	strip backend/runtime/fixLabels

gui/toupper: gui/toupper.c
	${CC} ${CFLAGS} ${CPPFLAGS} -O4 -o $@ $<

####
# Tools
####

#ifeq (1,${BUILDDYNAMIC})

dynamic/tools/sledscanner.cc: dynamic/tools/sledscanner.l include/sledparser.h
	${LEX} -o$@ $<

dynamic/tools/sledparser.cc include/sledparser.h: dynamic/tools/sledparser.y
	${YACC} -d -t -o dynamic/tools/sledparser.cc $<
	mv -f dynamic/tools/sledparser.cc.h include/sledparser.h

#endif   # BUILDDYNAMIC = 1

dynamic/tools/instrm/instrm_scanner.cc include/instrm_scanner.h: dynamic/tools/instrm/instrmscanner.l
	${FLEXPP} -odynamic/tools/instrm/instrm_scanner.cc -hinclude/instrm_scanner.h $<

dynamic/tools/instrm/instrm_parser.cc include/instrm_parser.h: dynamic/tools/instrm/instrmparser.y include/instrm_scanner.h
	${BISONPP} -d -v -odynamic/tools/instrm/instrm_parser.cc -hinclude/instrm_parser.h $<

dynamic/tools/sledtest: dynamic/tools/sledtest.o ${SLED-OBJS}
	${CC} -g3 $+ -o $@

dynamic/tools/genemu: ${GENEMU-OBJS} ${INSTRM-OBJS} ${SLED-OBJS} ${SSL-OBJS} \
		${TYPE-OBJS} ${UTIL-OBJS}  
	${CC} -g3 $+ -o $@ -lstdc++ ${EXTRA_LIBS}

dynamic/tools/gendec: dynamic/tools/gendec.o dynamic/tools/match.o ${SLED-OBJS} ${SSL-OBJS} \
		${UTIL-OBJS}
	${CC} -g3 $+ -o $@ -lstdc++

pal/parsepatterns: pal/patternscanner.o pal/patternparser.o pal/parsepatterns.o util/util.o
	${CC} $^ -o $@ -lstdc++

loader/bffDump: loader/bffDump.o $(loader-OBJS) util/error.o
	${CC} ${LDFLAGS} $^ -o $@ ${LIBS}

ifdef EMU_BIN
dynamic/emu/${EMU_BIN}: ${EMU_C_OBJ} dynamic/emu/emumain.o \
	        dynamic/emu/personality.o dynamic/emu/sysv.o \
			dynamic/emu/solaris.o dynamic/emu/linux.o 
	${CC} ${LDFLAGS} $^ -o $@ ${LIBS} -lm ${EXTRA_LIBS} ${MAPFILE}
endif

ifdef EMU_JAVA_LIB
dynamic/emu/${EMU_JAVA_LIB}: ${EMU_JAVA_SHOBJ} \
	        dynamic/emu/personality.lo dynamic/emu/sysv.lo \
		dynamic/emu/solaris.lo dynamic/emu/linux.lo
	${CC} ${LDFLAGS} -G $^ -o $@ ${LIBS}
endif

ifdef INSTRM_EMU_BIN
dynamic/emu/${INSTRM_EMU_BIN}: ${INSTRM_EMU_C_OBJ} dynamic/emu/emumain.o \
	        dynamic/emu/personality.o dynamic/emu/sysv.o \
			dynamic/emu/solaris.o dynamic/emu/linux.o 
	${CC} ${LDFLAGS} $^ -o $@ ${LIBS} -lm ${EXTRA_LIBS} ${MAPFILE}
endif

disasm/disasm.o: disasm/disasm.cc
	${CXX} ${CPPFLAGS} ${CXXFLAGS} -DDISASM -c -o $@ $<

disasm/$(DISASM_BIN): disasm/disasm.o $(DISASM_OBJ) $(loader-OBJS) util/error.o
	${CC} ${LDFLAGS} $^ -o $@ ${LIBS}

disasm/libdisasm.so: disasm/disasmj.lo $(LOADER_SHOBJS) util/error.lo
	${CC} -G ${LDFLAGS} $^ -o $@ ${LIBS}

ifeq (1,${BUILDDYNAMIC})
    CPPFLAGS += -Idynamic/emu
endif

dynamic/emuDebug: dynamic/emuDebugJava dynamic/emu ${EMUDEBUG-OBJS}
	${CC} ${CPPFLAGS} -DUNIX -DSRC=SPARC -DTGT=SPARC -DJAVA \
	-G ${EMUDEBUG-OBJS} \
	-o dynamic/emuDebug/lib/libemulib.so -lstdc++ -lelf

dynamic/emuDebugJava: dynamic/emuDebug/bin/emuDebugGen/CodeGen.class
	${JAVAC} -classpath .:dynamic/emuDebug/src:disasm ${EMUDEBUG-JAVA}
	mv dynamic/emuDebug/src/*.class dynamic/emuDebug/bin

dynamic/emuDebug/bin/emuDebugGen/CodeGen.class: disasm/libdisasm.so \
		${DISASM_JAVA_DEBUG}
	${JAVAC} -classpath .:dynamic/emuDebug/src/emuDebugGen:disasm \
	        ${EMUDEBUGGEN-JAVA}
	mv dynamic/emuDebug/src/emuDebugGen/*.class dynamic/emuDebug/bin/emuDebugGen
	java -classpath ${CLASSPATH}:dynamic/emuDebug/bin/emuDebugGen CodeGen \
		dynamic/emuDebug/src/machine/sparc/emuDebugSPARC.spec \
		dynamic/emuDebug/src/machine/sparc/emuDebugSPARC.m \
		dynamic/emuDebug/src/machine/sparc/emuLib.skel
	mv emuDebug.java dynamic/emuDebug/src
	mv emuLib.cc dynamic/emuDebug/src/emu
	mv RegFile.java dynamic/emuDebug/src

# Alternate build commands
$(DISASM_BIN): disasm/$(DISASM_BIN)
disasm: disasm/$(DISASM_BIN)

$(EMU_BIN): dynamic/emu/$(EMU_BIN)
dynamic/emu: dynamic/emu/$(EMU_BIN)

#$(EMU_BIN_JAVA): dynamic/emu/$(EMU_BIN_JAVA)
#dynamic/emuj: dynamic/emu/${EMU_JAVA_LIB} $(EMU_JAVA:%.java=%.class)
dynamic/emuj: dynamic/emu/${EMU_JAVA_LIB} $(EMU_JAVA_OBJ)

$(INSTRM_EMU_BIN): dynamic/emu/$(INSTRM_EMU_BIN)

%.lo: %.cc
	${CXX} -fPIC ${CPPFLAGS} ${CXXFLAGS} -c -o $@ $<
%.class: %.java
	${JAVAC} -classpath .:$(PROC_DIR):disasm:dynamic/emu $<

#	#	#	#	#	#	#	#	#	#
#									#
#	cscope database creation					#
#									#
#	#	#	#	#	#	#	#	#	#

CSCOPE	= cscope
CSFLAGS	= -b
CSDIRS	= .

cscope: cscope.files cscope.out

cscope.out: cscope.files
	$(CSCOPE) $(CSFLAGS)

cscope.files:
	$(RM) cscope.files cscope.out
	find $(CSDIRS) -name CVS -prune -o \
	    -name 'docs' -prune -o -name '*~' -prune -o -name test -prune -o \
	    -type f \( -name '*.[Ccshlxym]' -o -name '*.cc' -o \
	    -name '*.spec' -o -name '*.ssl' -o -name '*.link' -o \
	    -name '*.m' -o -name '*.pal' -o \
	    -name '*.pat' \) -print > cscope.files

cscope.clean:
	$(RM) cscope.files cscope.out


#	#	#	#	#	#	#	#	#	#
#									#
#	Make clean and make change etc	#
#									#
#	#	#	#	#	#	#	#	#	#

clean: 
	find . -name \*.o | xargs rm -f uqbt
	rm -f config.cache

realclean: clean cscope.clean
	rm -f config.cache $(GENSRC) rtl/*.output pal/*.output

dynclean: 
	find dynamic -name \*.o | xargs rm -f uqbt/dynamic
	find dynamic -name \*.class | xargs rm -f uqbt/dynamic
	rm -f config.cache 

realdynclean: dynclean 
	rm -f ${ALLDYNBIN} $(DYNGENSRC) dynamic/tools/instrm/*.output 
	

.PHONY: depend
depend:
	${srcdir}/mkdep ${srcdir} "${CC} ${CPPFLAGS}" > .depend
.depend:
	${srcdir}/mkdep ${srcdir} "${CC} ${CPPFLAGS}" > .depend

include .depend

####
# Rules to redo configuration when needed
####

Makefile: ${srcdir}/Makefile.in config.status
	./config.status

backend/common/instance.cc: backend/common/instance.cc.in config.status
	./config.status

config.status include/config.h.in configure: configure.in
	( cd ${srcdir}; autoconf; autoheader )
	touch config.status

