-#
-# Rules for building lex/yacc files
-#
-LEX_FILES = $(filter %.l, $(Source))
-LEX_OUTPUT = $(LEX_FILES:%.l=%.cpp)
-YACC_FILES = $(filter %.y, $(Source))
-YACC_OUTPUT = $(addprefix $(YACC_FILES:%.y=%), .h .cpp .output)
-.PRECIOUS: $(LEX_OUTPUT) $(YACC_OUTPUT)
-
-# Create a .cpp source file from a flex input file... this uses sed to cut down
-# on the warnings emited by GCC...
-#
-# The last line is a gross hack to work around flex aparently not being able to
-# resize the buffer on a large token input. Currently, for uninitialized string
-# buffers in LLVM we can generate very long tokens, so this is a hack around it.
-# FIXME. (f.e. char Buffer[10000] )
-#
+endif
+
+#---------------------------------------------------------
+# Provide rule to build .bc files from .ll sources,
+# regardless of dependencies
+#---------------------------------------------------------
+$(ObjDir)/%.bc: %.ll $(ObjDir)/.dir $(LLVMAS)
+ $(Echo) "Compiling $*.ll for $(BuildMode) build"
+ $(Verb) $(LLVMAS) $< -f -o $@
+
+###############################################################################
+# TABLEGEN: Provide rules for running tblgen to produce *.inc files
+###############################################################################
+
+ifdef TARGET
+
+TDFiles := $(strip $(wildcard $(PROJ_SRC_DIR)/*.td) $(LLVM_SRC_ROOT)/lib/Target/Target.td $(LLVM_SRC_ROOT)/lib/Target/TargetSelectionDAG.td)
+INCFiles := $(filter %.inc,$(BUILT_SOURCES))
+INCTMPFiles := $(INCFiles:%=$(ObjDir)/%.tmp)
+.PRECIOUS: $(INCTMPFiles) $(INCFiles)
+
+# All of these files depend on tblgen and the .td files.
+$(INCTMPFiles) : $(TBLGEN) $(TDFiles)
+
+# INCFiles rule: All of the tblgen generated files are emitted to
+# $(ObjDir)/%.inc.tmp, instead of emitting them directly to %.inc. This allows
+# us to only "touch" the real file if the contents of it change. IOW, if
+# tblgen is modified, all of the .inc.tmp files are regereated, but no
+# dependencies of the .inc files are, unless the contents of the .inc file
+# changes.
+$(INCFiles) : %.inc : $(ObjDir)/%.inc.tmp
+ $(Verb) $(CMP) -s $@ $< || $(CP) $< $@
+
+$(TARGET:%=$(ObjDir)/%GenRegisterNames.inc.tmp): \
+$(ObjDir)/%GenRegisterNames.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) register names with tblgen"
+ $(Verb) $(TableGen) -gen-register-enums -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenRegisterInfo.h.inc.tmp): \
+$(ObjDir)/%GenRegisterInfo.h.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) register information header with tblgen"
+ $(Verb) $(TableGen) -gen-register-desc-header -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenRegisterInfo.inc.tmp): \
+$(ObjDir)/%GenRegisterInfo.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) register info implementation with tblgen"
+ $(Verb) $(TableGen) -gen-register-desc -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenInstrNames.inc.tmp): \
+$(ObjDir)/%GenInstrNames.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) instruction names with tblgen"
+ $(Verb) $(TableGen) -gen-instr-enums -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenInstrInfo.inc.tmp): \
+$(ObjDir)/%GenInstrInfo.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) instruction information with tblgen"
+ $(Verb) $(TableGen) -gen-instr-desc -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenAsmWriter.inc.tmp): \
+$(ObjDir)/%GenAsmWriter.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) assembly writer with tblgen"
+ $(Verb) $(TableGen) -gen-asm-writer -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenAsmWriter1.inc.tmp): \
+$(ObjDir)/%GenAsmWriter1.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) assembly writer #1 with tblgen"
+ $(Verb) $(TableGen) -gen-asm-writer -asmwriternum=1 -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenCodeEmitter.inc.tmp): \
+$(ObjDir)/%GenCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) code emitter with tblgen"
+ $(Verb) $(TableGen) -gen-emitter -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenDAGISel.inc.tmp): \
+$(ObjDir)/%GenDAGISel.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) instruction selector implementation with tblgen"
+ $(Verb) $(TableGen) -gen-dag-isel -o $@ $<
+
+$(TARGET:%=$(ObjDir)/%GenSubtarget.inc.tmp): \
+$(ObjDir)/%GenSubtarget.inc.tmp : %.td $(ObjDir)/.dir
+ $(Echo) "Building $(<F) subtarget information with tblgen"
+ $(Verb) $(TableGen) -gen-subtarget -o $@ $<
+
+clean-local::
+ -$(Verb) $(RM) -f $(INCFiles)
+
+endif
+
+###############################################################################
+# LEX AND YACC: Provide rules for generating sources with lex and yacc
+###############################################################################
+
+#---------------------------------------------------------
+# Provide rules for generating a .cpp source file from
+# (f)lex input sources.
+#---------------------------------------------------------
+
+LexFiles := $(filter %.l,$(Sources))
+
+ifneq ($(LexFiles),)
+
+# Cancel built-in rules for lex
+%.c: %.l