# This makefile should be executed by 'nohup make -k &'
#
# nohup will prevent the script to be terminated when you 
# logout.
#
# With -k, gmake will go on to the next target (simulation) 
# if the current one aborts. Without it, it will just abort.
#
# Cleanup can be done with 'make clean'

######## BE SURE TO CHANGE THIS TO MATCH YOURS #############
SIMULATOR	= ../SS3_base/sim-bpred
GCC		= ../SS3_base/cc1.ss
GCC_TARGET	= ../SS3_base/cccp.i
############################################################

# $(LOGNAME) is an environment variable containing your
# login. Shall the generated email address be invalid,
# replace it with a valid one.
#
# BUG: On the lava machines, I couldn't get it to print the
#      subject line properly... You will get mails without
#      subjects.
#
MAILTO = $(LOGNAME)@cs.virginia.edu

############## UNCOMMENT ONE OF EACH PAIR ##################
#
MAXINST = 100 		# Test the script
# MAXINST = 0		# Run the simulations
#
############################################################

# Path to commands used, which shouldn't need to be changed
# if running the script from within the UVa CS department.

MAIL		= /usr/bin/mail
CAT		= /bin/cat
SED		= /usr/bin/sed
NOHUP		= /usr/bin/nohup
TIME		= /usr/bin/time
ECHO		= /bin/echo
RM		= /bin/rm

# The following is dependent on the actual program simulated
# Here it is meant for the gcc simulation 

GCC_FLAGS 	= -funroll-loops -fforce-mem -fcse-follow-jumps \
		  -fcse-skip-blocks -fexpensive-optimizations \
		  -fstrength-reduce -fpeephole -fschedule-insns\
		  -finline-functions -fschedule-insns2
GCC_OUT 	= -quiet -O $(GCC_TARGET) -o /dev/null

# Simulator command line

SIM_RUN 	= $(NOHUP) $(TIME) $(SIMULATOR) -nice 8 -max:inst $(MAXINST)
SIMULATION	= $(GCC) $(GCC_FLAGS) $(GCC_OUT)

# Assignment-dependent operations, these are provided for assignment 4

L2		= -bpred 2lev -bpred:2lev
BIM		= -bpred bimod -bpred:bimod

# Each macro MUST match the name of the target it is defined for 
# Parameters are (predictor) BHT PHT history XOR

GAg_1_32k_15 	= $(L2) 1 32768 15 0
GAs_1_32k_8 	= $(L2) 1 32768 8 0
GAs_1_8k_6	= $(L2) 1 8192 6 0
PAs_4k_32k_4	= $(L2) 4096 32768 4 0
PAs_1k_32k_4	= $(L2) 1024 32768 4 0
Bim_8k		= $(BIM) 8192

# Add here the new targets you want to define

TARGETS		= GAg_1_32k_15 GAg_1_32k_8 GAs_1_8k_6 PAs_4k_32k_4 \
		  PAs_1k_32k_4 Bim_8k

# Default rule, runs all simulations, one by one
# Do NOT try to run them in parallel
#
# $(LOGNAME) is an environment variable containing your login

all: $(TARGETS)
	@$(ECHO) I'm done.
	@$(ECHO) 'Subject: Simulations ended' | $(CAT) - nohup.out | $(MAIL) $(MAILTO);
	@$(RM) -f nohup.out

# Remove all the output files

clean:
	@$(RM) -f $(TARGETS)
	@$(ECHO) All clean now.

# The actual rule for running a simulation. Once each simulation 
# has run, you will be emailed the simulation file, with the
# individual branch data filtered out if your output format was 
# the same as the one given in assigmnent 1.

# $@ matches the target being processed.
# Hence $($@) matches the macro which has the same name as the 
# file being processed.

$(TARGETS) :
	@$(ECHO) Processing $@
	@$(SIM_RUN) $($@) $(SIMULATION) 2>$@ 1>>$@ # 1>/dev/null
	@$(ECHO) 'Subject: Results from $@' | $(CAT) - $@ | $(SED) -e "/^0x.*/d" | $(MAIL) $(MAILTO);
