# Makefile

# Generated files
JUNK := asm vm register.o memory.o instruction.o label.o main.o asm.o vm.o
JUNK += grammar.o scanner.o grammar.c grammar.h scanner.c adder.map adder.img
JUNK += adder.asm adder.m4

# Ultimate targets
all: asm vm adder.img adder.map

# Executable adder code
adder.img adder.map: adder.asm asm
	./asm $< adder.img adder.map

# Preprocessed adder code
adder.asm: adder.m4
	m4 $< > $@

# m4 source
adder.m4: adder-mod.m4
	ln -sf $^ $@

# Assembler program
asm: register.o memory.o instruction.o label.o main.o asm.o vm.o grammar.o \
scanner.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

# Virtual machine program
vm: asm
	ln -f $^ $@

# Scanner.l include grammar.h
scanner.l: grammar.h

# All include project.h
asm.c grammar.y instruction.c label.c main.c memory.c register.c scanner.l \
vm.c: project.h

# Grammar parser
grammar.c grammar.h: grammar.y
	$(YACC) $(YFLAGS) -d -ogrammar.c $^

# Lexical analyzer
scanner.c: scanner.l
	$(LEX) $(LFLAGS) -o$@ $^

# Remove all generated files
clean:
	-$(RM) -f $(JUNK)

# Execute the adder program
run: vm adder.img adder.map
	./vm adder.img adder.map

# These targets aren't really files
.PHONY: all clean run

# EOF