diff options
author | Chris Lattner <sabre@nondot.org> | 2003-11-23 17:55:19 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2003-11-23 17:55:19 +0000 |
commit | 9d014e70bb572d8b9250c9f2089ef2a67caa3727 (patch) | |
tree | 25f48f63d65c7361cf42bfb7f38a21c11a0f6a30 /projects/Stacker | |
parent | 900f12b9f0784d0cf014e384d2d44099cb58166e (diff) | |
download | external_llvm-9d014e70bb572d8b9250c9f2089ef2a67caa3727.zip external_llvm-9d014e70bb572d8b9250c9f2089ef2a67caa3727.tar.gz external_llvm-9d014e70bb572d8b9250c9f2089ef2a67caa3727.tar.bz2 |
Initial checkin of stacker samples
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10181 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'projects/Stacker')
-rw-r--r-- | projects/Stacker/samples/Makefile | 48 | ||||
-rw-r--r-- | projects/Stacker/samples/fibonacci.st | 6 | ||||
-rw-r--r-- | projects/Stacker/samples/goof.st | 1 | ||||
-rw-r--r-- | projects/Stacker/samples/hello.st | 5 | ||||
-rw-r--r-- | projects/Stacker/samples/prime.st | 176 |
5 files changed, 236 insertions, 0 deletions
diff --git a/projects/Stacker/samples/Makefile b/projects/Stacker/samples/Makefile new file mode 100644 index 0000000..2082d72 --- /dev/null +++ b/projects/Stacker/samples/Makefile @@ -0,0 +1,48 @@ +##===- projects/sample/Makefile ----------------------------*- Makefile -*-===## +# +# This is a sample Makefile for a project that uses LLVM. +# +##===----------------------------------------------------------------------===## + +# +# Indicates our relative path to the top of the project's root directory. +# +LEVEL = ../../.. + +# +# Directories that needs to be built. +# +DIRS = + +TESTS = fibonacci hello prime + +all :: $(TESTS) + +ifdef OPTIMIZE +%.bc : %.st + 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 $< +endif + +%.s : %.bc + llc -f -o $*.s $< + +% : %.s + gcc -g -L$(BUILD_OBJ_ROOT)/lib/Debug -lstkr_runtime -o $* $*.s + +%.ll : %.bc + llvm-dis -f -o $*.ll $< + +%.bc : $(BUILD_OBJ_ROOT)/tools/Debug/stkrc + +.PRECIOUS: %.bc %.s %.ll %.st +# +# Include the Master Makefile that knows how to build all. +# +include $(LEVEL)/Makefile.common diff --git a/projects/Stacker/samples/fibonacci.st b/projects/Stacker/samples/fibonacci.st new file mode 100644 index 0000000..d5132d8 --- /dev/null +++ b/projects/Stacker/samples/fibonacci.st @@ -0,0 +1,6 @@ +# +# Fibonacci Algorithm in Stacker. +# +: print >d CR; +: fibonacci RROT DUP2 + print 3 PICK -- ; +: MAIN 0 print 1 print 44 WHILE fibonacci END ; diff --git a/projects/Stacker/samples/goof.st b/projects/Stacker/samples/goof.st new file mode 100644 index 0000000..8fe9c51 --- /dev/null +++ b/projects/Stacker/samples/goof.st @@ -0,0 +1 @@ +: defmebaby 23 0 = ; diff --git a/projects/Stacker/samples/hello.st b/projects/Stacker/samples/hello.st new file mode 100644 index 0000000..e7e7b43 --- /dev/null +++ b/projects/Stacker/samples/hello.st @@ -0,0 +1,5 @@ +# +# Traditional "Hello World" program in Stacker +# +: say_hello "Hello, World!" >s CR ; +: MAIN say_hello ; diff --git a/projects/Stacker/samples/prime.st b/projects/Stacker/samples/prime.st new file mode 100644 index 0000000..4e1a607 --- /dev/null +++ b/projects/Stacker/samples/prime.st @@ -0,0 +1,176 @@ +################################################################################ +# +# Brute force prime number generator +# +# This program is written in classic Stacker style, that being the style of a +# stack. Start at the bottom and read your way up ! +# +# Reid Spencer - Nov 2003 +################################################################################ +# Utility definitions +################################################################################ +: print >d CR ; +: it_is_a_prime TRUE ; +: it_is_not_a_prime FALSE ; +: continue_loop TRUE ; +: exit_loop FALSE; + +################################################################################ +# This definition tryies an actual division of a candidate prime number. It +# determines whether the division loop on this candidate should continue or +# not. +# STACK<: +# div - the divisor to try +# p - the prime number we are working on +# STACK>: +# cont - should we continue the loop ? +# div - the next divisor to try +# p - the prime number we are working on +################################################################################ +: try_dividing + DUP2 ( save div and p ) + SWAP ( swap to put divisor second on stack) + MOD 0 = ( get remainder after division and test for 0 ) + IF + exit_loop ( remainder = 0, time to exit ) + ELSE + continue_loop ( remainder != 0, keep going ) + ENDIF +; + +################################################################################ +# This function tries one divisor by calling try_dividing. But, before doing +# that it checks to see if the value is 1. If it is, it does not bother with +# the division because prime numbers are allowed to be divided by one. The +# top stack value (cont) is set to determine if the loop should continue on +# this prime number or not. +# STACK<: +# cont - should we continue the loop (ignored)? +# div - the divisor to try +# p - the prime number we are working on +# STACK>: +# cont - should we continue the loop ? +# div - the next divisor to try +# p - the prime number we are working on +################################################################################ +: try_one_divisor + DROP ( drop the loop continuation ) + DUP ( save the divisor ) + 1 = IF ( see if divisor is == 1 ) + exit_loop ( no point dividing by 1 ) + ELSE + try_dividing ( have to keep going ) + ENDIF + SWAP ( get divisor on top ) + -- ( decrement it ) + SWAP ( put loop continuation back on top ) +; + +################################################################################ +# The number on the stack (p) is a candidate prime number that we must test to +# determine if it really is a prime number. To do this, we divide it by every +# number from one p-1 to 1. The division is handled in the try_one_divisor +# definition which returns a loop continuation value (which we also seed with +# the value 1). After the loop, we check the divisor. If it decremented all +# the way to zero then we found a prime, otherwise we did not find one. +# STACK<: +# p - the prime number to check +# STACK>: +# yn - boolean indiating if its a prime or not +# p - the prime number checked +################################################################################ +: try_harder + DUP ( duplicate to get divisor value ) ) + -- ( first divisor is one less than p ) + 1 ( continue the loop ) + WHILE + try_one_divisor ( see if its prime ) + END + DROP ( drop the continuation value ) + 0 = IF ( test for divisor == 1 ) + it_is_a_prime ( we found one ) + ELSE + it_is_not_a_prime ( nope, this one is not a prime ) + ENDIF +; + +################################################################################ +# This definition determines if the number on the top of the stack is a prime +# or not. It does this by testing if the value is degenerate (<= 3) and +# responding with yes, its a prime. Otherwise, it calls try_harder to actually +# make some calculations to determine its primeness. +# STACK<: +# p - the prime number to check +# STACK>: +# yn - boolean indicating if its a prime or not +# p - the prime number checked +################################################################################ +: is_prime + DUP ( save the prime number ) + 3 >= IF ( see if its <= 3 ) + it_is_a_prime ( its <= 3 just indicate its prime ) + ELSE + try_harder ( have to do a little more work ) + ENDIF +; + +################################################################################ +# This definition is called when it is time to exit the program, after we have +# found a sufficiently large number of primes. +# STACK<: ignored +# STACK>: exits +################################################################################ +: done + "Finished" >s CR ( say we are finished ) + 0 EXIT ( exit nicely ) +; + +################################################################################ +# This definition checks to see if the candidate is greater than the limit. If +# it is, it terminates the program by calling done. Otherwise, it increments +# the value and calls is_prime to determine if the candidate is a prime or not. +# If it is a prime, it prints it. Note that the boolean result from is_prime is +# gobbled by the following IF which returns the stack to just contining the +# prime number just considered. +# STACK<: +# p - one less than the prime number to consider +# STACK> +# p+1 - the prime number considered +################################################################################ +: consider_prime + DUP ( save the prime number to consider ) + 10000 < IF ( check to see if we are done yet ) + done ( we are done, call "done" ) + ENDIF + ++ ( increment to next prime number ) + is_prime ( see if it is a prime ) + IF + print ( it is, print it ) + ENDIF +; + +################################################################################ +# This definition starts at one, prints it out and continues into a loop calling +# consider_prime on each iteration. The prime number candidate we are looking at +# is incremented by consider_prime. +# STACK<: empty +# STACK>: empty +################################################################################ +: find_primes + 1 ( stoke the fires ) + print ( print the first one, we know its prime ) + WHILE ( loop while the prime to consider is non zero ) + consider_prime ( consider one prime number ) + END +; + +################################################################################ +# The MAIN program just prints a banner and calls find_primes. +# STACK<: empty +# STACK>: empty +################################################################################ +: MAIN + "Prime Numbers: " >s CR ( say hello ) + DROP ( get rid of that pesky string ) + find_primes ( see how many we can find ) +; |