Apply patches from PR136


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10192 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/projects/Stacker/Makefile b/projects/Stacker/Makefile
index 24da358..1503fad 100644
--- a/projects/Stacker/Makefile
+++ b/projects/Stacker/Makefile
@@ -12,9 +12,9 @@
 #
 # Directories that needs to be built.
 #
-# Disable the 'test' subdirectory until it works correctly
-#   test
-DIRS = lib tools samples
+# Disable the 'test' and 'samples' subdirectories until they work correctly
+#DIRS = lib tools test samples
+DIRS = lib tools
 
 #
 # Include the Master Makefile that knows how to build all.
diff --git a/projects/Stacker/lib/compiler/StackerCompiler.cpp b/projects/Stacker/lib/compiler/StackerCompiler.cpp
index 4cd0646..931796f 100644
--- a/projects/Stacker/lib/compiler/StackerCompiler.cpp
+++ b/projects/Stacker/lib/compiler/StackerCompiler.cpp
@@ -131,8 +131,8 @@
 	TheStack = new GlobalVariable( 
 	    /*type=*/ stack_type, 
 	    /*isConstant=*/ false, 
-	    /*Linkage=*/ GlobalValue::AppendingLinkage, 
-	    /*initializer=*/Constant::getNullValue(stack_type), 
+	    /*Linkage=*/ GlobalValue::LinkOnceLinkage, 
+	    /*initializer=*/ Constant::getNullValue(stack_type),
 	    /*name=*/ "_stack_",
 	    /*parent=*/ TheModule 
 	);
@@ -144,7 +144,7 @@
 	    /*type=*/Type::LongTy, 
 	    /*isConstant=*/false,
 	    /*Linkage=*/GlobalValue::LinkOnceLinkage, 
-	    /*initializer=*/Constant::getNullValue(Type::LongTy), 
+	    /*initializer=*/ Constant::getNullValue(Type::LongTy),
 	    /*name=*/"_index_",
 	    /*parent=*/TheModule
 	);
@@ -559,28 +559,19 @@
 StackerCompiler::handle_main_definition( Function* func )
 {
     // Set the name of the function defined as the Stacker main
+    // This will get called by the "main" that is defined in 
+    // the runtime library.
     func->setName( "_MAIN_");
 
-    // Create the actual main for the runtime system.
-    //std::vector<const Type*> params; // No parameters
-    //FunctionType* main_type = FunctionType::get( Type::IntTy, params, false );
-    Function* SystemMain = new Function(
-	DefinitionType, 
-	GlobalValue::ExternalLinkage,
-	"main", TheModule);
-
-    // Create a basic block that just calls the STACKERMAIN function. Note
-    // that the basic block is automatically inserted into the end of SystemMain
-    BasicBlock* bb = new BasicBlock( (echo?"main":"a"), SystemMain ) ;
-    bb->getInstList().push_back( new CallInst( func, no_arguments) );
-    bb->getInstList().push_back( new ReturnInst() ); 
-
     // Turn "_stack_" into an initialized variable since this is the main
     // module. This causes it to not be "external" but defined in this module.
     TheStack->setInitializer( Constant::getNullValue(stack_type) );
+    TheStack->setLinkage( GlobalValue::LinkOnceLinkage );
 
     // Turn "_index_" into an intialized variable for the same reason.
     TheIndex->setInitializer( Constant::getNullValue(Type::LongTy) );
+    TheIndex->setLinkage( GlobalValue::LinkOnceLinkage );
+
     return func;
 }
 
diff --git a/projects/Stacker/lib/runtime/stacker_rt.c b/projects/Stacker/lib/runtime/stacker_rt.c
index 46edf14..7deb54d 100644
--- a/projects/Stacker/lib/runtime/stacker_rt.c
+++ b/projects/Stacker/lib/runtime/stacker_rt.c
@@ -17,10 +17,13 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "ctype.h"
 #include "stdio.h"
+#include "stdlib.h"
 
 extern long _index_;
 extern int _stack_[1024];
+extern void _MAIN_();
 
 void
 _stacker_dump_stack_()
@@ -32,3 +35,38 @@
 	printf("#%03d: %d\n", i, _stack_[i] );
     }
 }
+
+int
+main ( int argc, char** argv )
+{
+    // Avoid modifying argc
+    int a = argc;
+
+    // Make sure we're starting with the right index
+    _index_ = 0;
+
+    // Copy the arguments to the stack in reverse order
+    // so that they get popped in the order presented
+    while ( a > 0 )
+    {
+	if ( isdigit( argv[--a][0] ) )
+	{
+	    _stack_[_index_++] = atoi( argv[a] );
+	}
+	else
+	{
+	    _stack_[_index_++] = (int) argv[a];
+	}
+    }
+
+    // Put the argument count on the stack
+    _stack_[_index_] = argc;
+
+    // Invoke the user's main program
+    _MAIN_();
+
+    // Return last item on the stack
+    if ( _index_ >= 0 )
+	return _stack_[_index_];
+    return -1;
+}
diff --git a/projects/Stacker/samples/Makefile b/projects/Stacker/samples/Makefile
index 2082d72..b6b03c4 100644
--- a/projects/Stacker/samples/Makefile
+++ b/projects/Stacker/samples/Makefile
@@ -14,34 +14,46 @@
 #
 DIRS = 
 
-TESTS = fibonacci hello prime
+SAMPLES = fibonacci hello prime
 
-all :: $(TESTS)
+all :: $(SAMPLES)
 
 ifdef OPTIMIZE
 %.bc : %.st 
-	stkrc -e -o - $< | opt -stats -q -f -o $*.bc \
+	@$(ECHO) "Compiling and Optimizing $< to $*.bc"
+	$(VERB)stkrc -e -o - $< | opt -stats -q -f -o $*.bc \
 	    -aa-eval -adce -branch-combine -cee -constmerge -constprop -dce -die -ds-aa \
 	    -ds-opt -gcse -globaldce -indvars -inline -instcombine \
 	    -ipconstprop -licm -loopsimplify -mem2reg -pre -sccp -simplifycfg \
 	    -tailcallelim -verify
 else
 %.bc : %.st
-	stkrc -e -f -o $*.bc $< 
+	@$(ECHO) "Compiling $< to $*.bc"
+	$(VERB)stkrc -e -f -o $*.bc $< 
 endif
 
 %.s : %.bc
-	llc -f -o $*.s $<
+	@$(ECHO) "Compiling $< to $*.s"
+	$(VERB)llc -f -o $*.s $<
 
 % : %.s
-	gcc -g -L$(BUILD_OBJ_ROOT)/lib/Debug -lstkr_runtime -o $* $*.s
+	@$(ECHO) "Compiling and Linking $< to $*"
+	$(VERB)gcc -g -L$(BUILD_OBJ_ROOT)/lib/Debug -lstkr_runtime -o $* $*.s
 
 %.ll : %.bc
-	llvm-dis -f -o $*.ll $<
+	@$(ECHO) "Disassembling $< to $*.ll"
+	$(VERB)llvm-dis -f -o $*.ll $<
 
 %.bc :  $(BUILD_OBJ_ROOT)/tools/Debug/stkrc
 
 .PRECIOUS: %.bc %.s %.ll %.st
+
+SAMPLES_LL = $(SAMPLES:%=%.ll)
+SAMPLES_BC = $(SAMPLES:%=%.bc)
+SAMPLES_S  = $(SAMPLES:%=%.s)
+
+clean ::
+	$(VERB)rm -f gmon.out $(SAMPLES_LL) $(SAMPLES_BC) $(SAMPLES_S) $(SAMPLES)
 #
 # Include the Master Makefile that knows how to build all.
 #
diff --git a/projects/Stacker/samples/goof.st b/projects/Stacker/samples/goof.st
index 8fe9c51..8c1ff72 100644
--- a/projects/Stacker/samples/goof.st
+++ b/projects/Stacker/samples/goof.st
@@ -1 +1,25 @@
-: defmebaby 23 0 = ;
+#
+# goof
+#
+: print_one
+    --
+    SWAP
+    >s
+    DROP
+;
+: print_it
+    WHILE 
+        print_one
+    END
+;
+    
+: MAIN
+    "MICKEY: I said she was f'in goofy!"
+    "MICKEY: I didn't say she was insane." 
+    "JUDGE: Yet you provide no evidence of this and I do not concur."
+    "JUDGE: In your pleadings you claim that Mini Mouse is insane."
+    "MICKEY: Well, what do you mean, your honor?"
+    "JUDGE: Mr. Mouse, I find your grounds for divorce insufficient. "
+    6
+    print_it
+;
diff --git a/projects/Stacker/samples/prime.st b/projects/Stacker/samples/prime.st
index 4e1a607..3b8703d 100644
--- a/projects/Stacker/samples/prime.st
+++ b/projects/Stacker/samples/prime.st
@@ -139,7 +139,7 @@
 ################################################################################
 : consider_prime 
     DUP 			( save the prime number to consider )
-    10000 < IF 		( check to see if we are done yet )
+    1000000 < IF 		( check to see if we are done yet )
         done 			( we are done, call "done" )
     ENDIF 
     ++ 				( increment to next prime number )
@@ -157,6 +157,8 @@
 # STACK>: empty
 ################################################################################
 : find_primes 
+    "Prime Numbers: " >s CR	( say hello )
+    DROP			( get rid of that pesky string )
     1 				( stoke the fires )
     print			( print the first one, we know its prime )
     WHILE  			( loop while the prime to consider is non zero )
@@ -165,12 +167,69 @@
 ; 
 
 ################################################################################
-# The MAIN program just prints a banner and calls find_primes.
-# STACK<: empty
-# STACK>: empty
+#
+################################################################################
+: say_yes
+    >d				( Print the prime number )
+    " is prime."		( push string to output )
+    >s				( output it )
+    CR				( print carriage return )
+    DROP			( pop string )
+;
+
+: say_no
+    >d				( Print the prime number )
+    " is NOT prime."		( push string to put out )
+    >s				( put out the string )
+    CR				( print carriage return )
+    DROP			( pop string )
+;
+
+################################################################################
+# This definition processes a single command line argument and determines if it
+# is a prime number or not.
+# STACK<:
+#    n - number of arguments
+#    arg1 - the prime numbers to examine
+# STACK>:
+#    n-1 - one less than number of arguments
+#    arg2 - we processed one argument
+################################################################################
+: do_one_argument
+    --				( decrement loop counter )
+    SWAP			( get the argument value  )
+    is_prime IF			( determine if its prime )
+        say_yes			( uhuh )
+    ELSE
+        say_no			( nope )
+    ENDIF
+    DROP			( done with that argument )
+;
+
+################################################################################
+# The MAIN program just prints a banner and processes its arguments.
+# STACK<:
+#    n - number of arguments
+#    ... - the arguments
+################################################################################
+: process_arguments
+    WHILE			( while there are more arguments )
+       do_one_argument		( process one argument )
+    END
+;
+    
+################################################################################
+# The MAIN program just prints a banner and processes its arguments.
+# STACK<: arguments
 ################################################################################
 : MAIN 
-    "Prime Numbers: " >s CR	( say hello )
-    DROP			( get rid of that pesky string )
-    find_primes  		( see how many we can find )
+    NIP				( get rid of the program name )
+    --				( reduce number of arguments )
+    DUP				( save the arg counter )
+    1 <= IF			( See if we got an argument )
+        process_arguments	( tell user if they are prime )
+    ELSE
+        find_primes		( see how many we can find )
+    ENDIF
+    0				( push return code )
 ;
diff --git a/projects/Stacker/test/Makefile b/projects/Stacker/test/Makefile
index c7680dc..e4c7d9a 100644
--- a/projects/Stacker/test/Makefile
+++ b/projects/Stacker/test/Makefile
@@ -34,28 +34,38 @@
 all :: test_each
 
 test_each: $(TESTS)
-	$(BUILD_SRC_DIR)/runtests $(BUILD_OBJ_DIR) $(TESTS) 
+	@$(ECHO) "Running Tests..."
+	$(VERB)$(BUILD_SRC_DIR)/runtests $(BUILD_OBJ_DIR) $(TESTS) 
 
 % : %.s testing.s
-	gcc -ggdb -L$(BUILD_OBJ_ROOT)/lib/Debug testing.s -lstkr_runtime -o $* $*.s
+	@$(ECHO) "Compiling and Linking $< to $*"
+	$(VERB)gcc -ggdb -L$(BUILD_OBJ_ROOT)/lib/Debug testing.s -lstkr_runtime -o $* $*.s
 
 %.s : %.bc
-	llc -f -o $*.s $<
+	@$(ECHO) "Compiling $< to $*.s"
+	$(VERB)llc -f -o $*.s $<
 
 ifdef OPTIMIZE
 %.bc : %.st $(BUILD_OBJ_ROOT)/tools/Debug/stkrc
-	stkrc -e -o - $< | opt -stats -q -f -o $*.bc -adce -branch-combine -cee -constmerge -constprop -dce -die -gcse -globaldce -instcombine -pre
+	@$(ECHO) "Compiling and Optimizing $< to $*.bc"
+	$(VERB)stkrc -e -o - $< | opt -stats -q -f -o $*.bc -adce -branch-combine -cee -constmerge -constprop -dce -die -gcse -globaldce -instcombine -pre
 else
 %.bc : %.st $(BUILD_OBJ_ROOT)/tools/Debug/stkrc
-	stkrc -e -f -o $*.bc $< 
+	@$(ECHO) "Compiling $< to $*.bc"
+	$(VERB)stkrc -e -f -o $*.bc $< 
 endif
 
 %.ll : %.bc
-	llvm-dis -o $*.ll $<
+	@$(ECHO) "Disassembling $< to $*.ll"
+	$(VERB)llvm-dis -o $*.ll $<
+
+TESTS_LL = $(TESTS:%=%.ll)
+TESTS_BC = $(TESTS:%=%.bc)
+TESTS_S  = $(TESTS:%=%.s)
 
 clean :: 
-	rm -f $(TESTS)
+	$(VERB)rm -f gmon.out $(TESTS_LL) $(TESTS_BC) $(TESTS_S) $(TESTS) testing.bc testing.s testing.ll
 
-.SUFFIXES: .st .s .ll
+.SUFFIXES: .st .s .ll .bc
 .PRECIOUS: %.s %.ll %.bc %.st
-.PHONY: test_each test_asm
+.PHONY: test_each