Apply patches from PR136
authorBrian Gaeke <gaeke@uiuc.edu>
Mon, 24 Nov 2003 02:57:25 +0000 (02:57 +0000)
committerBrian Gaeke <gaeke@uiuc.edu>
Mon, 24 Nov 2003 02:57:25 +0000 (02:57 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10192 91177308-0d34-0410-b5e6-96231b3b80d8

projects/Stacker/Makefile
projects/Stacker/lib/compiler/StackerCompiler.cpp
projects/Stacker/lib/runtime/stacker_rt.c
projects/Stacker/samples/Makefile
projects/Stacker/samples/goof.st
projects/Stacker/samples/prime.st
projects/Stacker/test/Makefile

index 24da35878ffc11ab6bfb5d9284ba05e1c0095875..1503fadb3d3b87378c2e1a4d2ac29ac2fd02b636 100644 (file)
@@ -12,9 +12,9 @@ LEVEL = ../..
 #
 # 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.
index 4cd06461030800c5aa12036c96aa8b9cd3a57216..931796f9a81a42d2e9be46cd38e14e817579930a 100644 (file)
@@ -131,8 +131,8 @@ StackerCompiler::compile(
        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 @@ StackerCompiler::compile(
            /*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 @@ Function*
 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;
 }
 
index 46edf14c131d09bfe66756c8c505f61978da88d1..7deb54d6ea388cbfdb8165a8ad1390764bd8459a 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+#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 @@ _stacker_dump_stack_()
        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;
+}
index 2082d72ac1308625ee72cddbf5ee9bbb02c4a80e..b6b03c42bf231be4ca5fc973735238a8ad120d89 100644 (file)
@@ -14,34 +14,46 @@ LEVEL = ../../..
 #
 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.
 #
index 8fe9c51d92505fac96ef5a34ade93d00c4b9db4f..8c1ff72ba484ac7227dad3fe4e88922f931549c0 100644 (file)
@@ -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
+;
index 4e1a607a46974e2bb850c6cab1d1cf4886880777..3b8703db18f2b14446eb9debdcc2357034770d64 100644 (file)
 ################################################################################
 : 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 )
 # 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 )
 ; 
 
 ################################################################################
-# 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 )
 ;
index c7680dcaf55b3ae49b3c39e2e616de1e2e6163b7..e4c7d9a5b54f40687ab499a0f3b14c0b1f6685de 100644 (file)
@@ -34,28 +34,38 @@ TESTS = $(LOGIC_TESTS) $(ARITHMETIC_TESTS) $(BITWISE_TESTS) $(STACK_TESTS) \
 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