# =============================================================================
# Makefile for CMU 15-213 Lecture 1 demos
# =============================================================================
#
# [What this builds]
#   Five runnable C demos, one per "reality" of Lecture 1, plus sanitizer and
#   assembly-viewing targets.
#
# [How to use]
#   make          # build every demo
#   make run      # build and run them all, printing an "Observations" block
#   make asm      # dump the assembly of demo 05 (native, and x86-64 if possible)
#   make asan     # build demo 03 under AddressSanitizer
#   make ubsan    # build demo 03 under UndefinedBehaviorSanitizer
#   make x86      # (Apple Silicon only) cross-build + run the x86-64-sensitive
#                 #  demos 03 and 05 under Rosetta 2, to contrast with arm64
#   make clean    # remove all built files
#
# [Why per-target flags]
#   Most demos build at -O2. Two exceptions, both pedagogical:
#     * Demo 01 adds -fwrapv so signed integer overflow is the plain two's-
#       complement wrap we want to SHOW (without it, overflow is undefined and
#       the optimizer may surprise us -- which is itself demo 05's Part 2).
#     * Demo 05 is built BOTH at -O2 (default signed-overflow assumptions) and
#       at -O0, so you can watch inc_gt(INT_MAX) flip between the two.
#
# [C tip] In a Makefile, a rule is  "target: prerequisites" then TAB-indented
#   shell commands. $@ = the target, $< = the first prerequisite, $^ = all of
#   them. Lines starting with @ are run without echoing the command itself.
# -----------------------------------------------------------------------------

CC      ?= cc                       # the C compiler (clang on macOS, gcc on Linux)
CSTD    := -std=c11
WARN    := -Wall -Wextra
CFLAGS  := $(CSTD) $(WARN) -O2 -g   # the common flags for most demos

# The executables built by `make all`.
BINS := 01_ints_are_not_integers \
        02_floats_are_not_reals \
        03_memory_referencing_bug \
        04_locality_and_the_memory_wall \
        05_you_have_to_know_assembly \
        05_you_have_to_know_assembly.O0

.PHONY: all run asm asan ubsan x86 clean
all: $(BINS)

# ---- Demo 01: needs -fwrapv so overflow is the plain wrap we demonstrate -----
01_ints_are_not_integers: 01_ints_are_not_integers.c
	$(CC) $(CSTD) $(WARN) -O2 -g -fwrapv -o $@ $<

# ---- Demos 02, 03, 04: ordinary -O2 ------------------------------------------
02_floats_are_not_reals: 02_floats_are_not_reals.c
	$(CC) $(CFLAGS) -o $@ $<

03_memory_referencing_bug: 03_memory_referencing_bug.c
	$(CC) $(CFLAGS) -o $@ $<

04_locality_and_the_memory_wall: 04_locality_and_the_memory_wall.c timer.h
	$(CC) $(CFLAGS) -o $@ $<

# ---- Demo 05: two builds so the UB-driven answer can be seen to flip ---------
05_you_have_to_know_assembly: 05_you_have_to_know_assembly.c timer.h
	$(CC) $(CFLAGS) -DCOMPILER_OPT_LEVEL='"-O2"' -o $@ $<

05_you_have_to_know_assembly.O0: 05_you_have_to_know_assembly.c timer.h
	$(CC) $(CSTD) $(WARN) -O0 -g -DCOMPILER_OPT_LEVEL='"-O0"' -o $@ $<

# ---- Run everything ----------------------------------------------------------
run: all
	@echo; ./01_ints_are_not_integers
	@echo; ./02_floats_are_not_reals
	@echo; ./03_memory_referencing_bug
	@echo; ./04_locality_and_the_memory_wall
	@echo; echo '----- demo 05 at -O2 -----'; ./05_you_have_to_know_assembly
	@echo; echo '----- demo 05 at -O0 -----'; ./05_you_have_to_know_assembly.O0

# ---- Sanitizer builds of the memory bug (demo 03) ----------------------------
# Pass an index as an argument, e.g.  ./03_memory_referencing_bug.asan 4
asan: 03_memory_referencing_bug.c
	$(CC) $(CSTD) $(WARN) -O1 -g -fsanitize=address -o 03_memory_referencing_bug.asan $<
	@echo 'built 03_memory_referencing_bug.asan  (try: ./03_memory_referencing_bug.asan 4)'

ubsan: 03_memory_referencing_bug.c
	$(CC) $(CSTD) $(WARN) -O1 -g -fsanitize=undefined -o 03_memory_referencing_bug.ubsan $<
	@echo 'built 03_memory_referencing_bug.ubsan (try: ./03_memory_referencing_bug.ubsan 4)'

# ---- View the assembly of demo 05 --------------------------------------------
# Emits native assembly always, and x86-64 too when the compiler can target it.
asm: 05_you_have_to_know_assembly.c
	$(CC) $(CSTD) -O2 -S -o 05_native.s $<
	@echo 'wrote 05_native.s (this machine)'
	@$(CC) $(CSTD) -O2 -S -arch x86_64 -o 05_x86_64.s $< 2>/dev/null \
	  && echo 'wrote 05_x86_64.s (x86-64)' \
	  || echo '(x86-64 cross-assembly not available on this toolchain; skipped)'

# ---- Apple-Silicon only: contrast arm64 with x86-64 under Rosetta 2 ----------
x86:
	@echo '--- cross-building demos 03 and 05 for x86-64 (needs Rosetta 2) ---'
	@$(CC) $(CSTD) $(WARN) -O2 -g -arch x86_64 -o 03_x86 03_memory_referencing_bug.c 2>/dev/null \
	  && { echo '[x86-64] demo 03:'; ./03_x86; } \
	  || echo '(could not build/run x86-64; are you on Apple Silicon with Rosetta 2?)'
	@$(CC) $(CSTD) $(WARN) -O2 -g -arch x86_64 -DCOMPILER_OPT_LEVEL='"-O2 x86-64"' \
	  -o 05_x86 05_you_have_to_know_assembly.c 2>/dev/null \
	  && { echo '[x86-64] demo 05:'; ./05_x86; } \
	  || true

clean:
	rm -f $(BINS) 03_memory_referencing_bug.asan 03_memory_referencing_bug.ubsan \
	      03_x86 05_x86 05_native.s 05_x86_64.s
	rm -rf *.dSYM
