blob: df6aedfceab9135523270ce84c3d8d3cd3106cc9 [file] [log] [blame]
Brian Gaeke90181482003-11-24 02:52:51 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2<html>
3<head>
4 <title>Stacker: An Example Of Using LLVM</title>
5 <link rel="stylesheet" href="llvm.css" type="text/css">
6</head>
7<body>
8<div class="doc_title">Stacker: An Example Of Using LLVM</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +00009<hr>
Brian Gaeke90181482003-11-24 02:52:51 +000010<ol>
11 <li><a href="#abstract">Abstract</a></li>
12 <li><a href="#introduction">Introduction</a></li>
Brian Gaeke07e89e42003-11-24 17:03:38 +000013 <li><a href="#lessons">Lessons I Learned About LLVM</a>
14 <ol>
15 <li><a href="#value">Everything's a Value!</a></li>
16 <li><a href="#terminate">Terminate Those Blocks!</a></li>
17 <li><a href="#blocks">Concrete Blocks</a></li>
18 <li><a href="#push_back">push_back Is Your Friend</a></li>
19 <li><a href="#gep">The Wily GetElementPtrInst</a></li>
20 <li><a href="#linkage">Getting Linkage Types Right</a></li>
21 <li><a href="#constants">Constants Are Easier Than That!</a></li>
22 </ol>
23 </li>
Brian Gaeke90181482003-11-24 02:52:51 +000024 <li><a href="#lexicon">The Stacker Lexicon</a>
25 <ol>
26 <li><a href="#stack">The Stack</a>
27 <li><a href="#punctuation">Punctuation</a>
Chris Lattnere46d6012003-11-25 01:35:06 +000028 <li><a href="#comments">Comments</a>
Brian Gaeke90181482003-11-24 02:52:51 +000029 <li><a href="#literals">Literals</a>
30 <li><a href="#words">Words</a>
Chris Lattnere46d6012003-11-25 01:35:06 +000031 <li><a href="style">Standard Style</a>
Brian Gaeke90181482003-11-24 02:52:51 +000032 <li><a href="#builtins">Built-Ins</a>
33 </ol>
34 </li>
Brian Gaeke07e89e42003-11-24 17:03:38 +000035 <li><a href="#example">Prime: A Complete Example</a></li>
36 <li><a href="#internal">Internal Code Details</a>
37 <ol>
38 <li><a href="#directory">The Directory Structure </a></li>
39 <li><a href="#lexer">The Lexer</a></li>
40 <li><a href="#parser">The Parser</a></li>
41 <li><a href="#compiler">The Compiler</a></li>
42 <li><a href="#runtime">The Runtime</a></li>
43 <li><a href="#driver">Compiler Driver</a></li>
44 <li><a href="#tests">Test Programs</a></li>
Chris Lattnere46d6012003-11-25 01:35:06 +000045 <li><a href="#exercise">Exercise</a></li>
46 <li><a href="#todo">Things Remaining To Be Done</a></li>
Brian Gaeke07e89e42003-11-24 17:03:38 +000047 </ol>
48 </li>
Brian Gaeke90181482003-11-24 02:52:51 +000049</ol>
50<div class="doc_text">
51<p><b>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a> </b></p>
52<p> </p>
53</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +000054<hr>
Brian Gaeke90181482003-11-24 02:52:51 +000055<!-- ======================================================================= -->
56<div class="doc_section"> <a name="abstract">Abstract </a></div>
57<div class="doc_text">
58<p>This document is another way to learn about LLVM. Unlike the
59<a href="LangRef.html">LLVM Reference Manual</a> or
Chris Lattner45ab10c2003-12-18 06:40:22 +000060<a href="ProgrammersManual.html">LLVM Programmer's Manual</a>, here we learn
Chris Lattnere46d6012003-11-25 01:35:06 +000061about LLVM through the experience of creating a simple programming language
62named Stacker. Stacker was invented specifically as a demonstration of
Brian Gaeke90181482003-11-24 02:52:51 +000063LLVM. The emphasis in this document is not on describing the
64intricacies of LLVM itself, but on how to use it to build your own
65compiler system.</p>
66</div>
67<!-- ======================================================================= -->
68<div class="doc_section"> <a name="introduction">Introduction</a> </div>
69<div class="doc_text">
70<p>Amongst other things, LLVM is a platform for compiler writers.
71Because of its exceptionally clean and small IR (intermediate
72representation), compiler writing with LLVM is much easier than with
Chris Lattner45ab10c2003-12-18 06:40:22 +000073other system. As proof, I wrote the entire compiler (language definition,
74lexer, parser, code generator, etc.) in about <em>four days</em>!
75That's important to know because it shows how quickly you can get a new
76language running when using LLVM. Furthermore, this was the <em >first</em>
Brian Gaeke90181482003-11-24 02:52:51 +000077language the author ever created using LLVM. The learning curve is
78included in that four days.</p>
79<p>The language described here, Stacker, is Forth-like. Programs
80are simple collections of word definitions and the only thing definitions
81can do is manipulate a stack or generate I/O. Stacker is not a "real"
82programming language; its very simple. Although it is computationally
83complete, you wouldn't use it for your next big project. However,
84the fact that it is complete, its simple, and it <em>doesn't</em> have
85a C-like syntax make it useful for demonstration purposes. It shows
Chris Lattnere46d6012003-11-25 01:35:06 +000086that LLVM could be applied to a wide variety of languages.</p>
Brian Gaeke90181482003-11-24 02:52:51 +000087<p>The basic notions behind stacker is very simple. There's a stack of
88integers (or character pointers) that the program manipulates. Pretty
89much the only thing the program can do is manipulate the stack and do
90some limited I/O operations. The language provides you with several
91built-in words that manipulate the stack in interesting ways. To get
92your feet wet, here's how you write the traditional "Hello, World"
93program in Stacker:</p>
94<p><code>: hello_world "Hello, World!" &gt;s DROP CR ;<br>
95: MAIN hello_world ;<br></code></p>
96<p>This has two "definitions" (Stacker manipulates words, not
97functions and words have definitions): <code>MAIN</code> and <code>
98hello_world</code>. The <code>MAIN</code> definition is standard, it
99tells Stacker where to start. Here, <code>MAIN</code> is defined to
100simply invoke the word <code>hello_world</code>. The
101<code>hello_world</code> definition tells stacker to push the
102<code>"Hello, World!"</code> string onto the stack, print it out
103(<code>&gt;s</code>), pop it off the stack (<code>DROP</code>), and
104finally print a carriage return (<code>CR</code>). Although
105<code>hello_world</code> uses the stack, its net effect is null. Well
106written Stacker definitions have that characteristic. </p>
107<p>Exercise for the reader: how could you make this a one line program?</p>
108</div>
109<!-- ======================================================================= -->
Brian Gaeke07e89e42003-11-24 17:03:38 +0000110<div class="doc_section"><a name="lessons"></a>Lessons I Learned About LLVM</div>
Brian Gaeke90181482003-11-24 02:52:51 +0000111<div class="doc_text">
Chris Lattnere46d6012003-11-25 01:35:06 +0000112<p>Stacker was written for two purposes: </p>
113<ol>
114 <li>to get the author over the learning curve, and</li>
115 <li>to provide a simple example of how to write a compiler using LLVM.</li>
116</ol>
117<p>During the development of Stacker, many lessons about LLVM were
Brian Gaeke90181482003-11-24 02:52:51 +0000118learned. Those lessons are described in the following subsections.<p>
119</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000120<!-- ======================================================================= -->
121<div class="doc_subsection"><a name="value"></a>Everything's a Value!</div>
122<div class="doc_text">
Chris Lattnere46d6012003-11-25 01:35:06 +0000123<p>Although I knew that LLVM uses a Single Static Assignment (SSA) format,
Brian Gaeke07e89e42003-11-24 17:03:38 +0000124it wasn't obvious to me how prevalent this idea was in LLVM until I really
Chris Lattnere46d6012003-11-25 01:35:06 +0000125started using it. Reading the <a href="ProgrammersManual.html">
126Programmer's Manual</a> and <a href="LangRef.html">Language Reference</a>
127I noted that most of the important LLVM IR (Intermediate Representation) C++
Brian Gaeke07e89e42003-11-24 17:03:38 +0000128classes were derived from the Value class. The full power of that simple
129design only became fully understood once I started constructing executable
130expressions for Stacker.</p>
131<p>This really makes your programming go faster. Think about compiling code
Chris Lattnere46d6012003-11-25 01:35:06 +0000132for the following C/C++ expression: <code>(a|b)*((x+1)/(y+1))</code>. Assuming
133the values are on the stack in the order a, b, x, y, this could be
134expressed in stacker as: <code>1 + SWAP 1 + / ROT2 OR *</code>.
135You could write a function using LLVM that computes this expression like this: </p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000136<pre><code>
137Value*
Chris Lattner45ab10c2003-12-18 06:40:22 +0000138expression(BasicBlock* bb, Value* a, Value* b, Value* x, Value* y )
Brian Gaeke07e89e42003-11-24 17:03:38 +0000139{
140 Instruction* tail = bb->getTerminator();
141 ConstantSInt* one = ConstantSInt::get( Type::IntTy, 1);
142 BinaryOperator* or1 =
Chris Lattner0b404c82003-11-25 01:44:27 +0000143 BinaryOperator::create( Instruction::Or, a, b, "", tail );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000144 BinaryOperator* add1 =
Chris Lattner0b404c82003-11-25 01:44:27 +0000145 BinaryOperator::create( Instruction::Add, x, one, "", tail );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000146 BinaryOperator* add2 =
Chris Lattner0b404c82003-11-25 01:44:27 +0000147 BinaryOperator::create( Instruction::Add, y, one, "", tail );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000148 BinaryOperator* div1 =
Chris Lattner0b404c82003-11-25 01:44:27 +0000149 BinaryOperator::create( Instruction::Div, add1, add2, "", tail);
Brian Gaeke07e89e42003-11-24 17:03:38 +0000150 BinaryOperator* mult1 =
Chris Lattner0b404c82003-11-25 01:44:27 +0000151 BinaryOperator::create( Instruction::Mul, or1, div1, "", tail );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000152
153 return mult1;
154}
155</code></pre>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000156<p>"Okay, big deal," you say? It is a big deal. Here's why. Note that I didn't
Brian Gaeke07e89e42003-11-24 17:03:38 +0000157have to tell this function which kinds of Values are being passed in. They could be
Chris Lattner45ab10c2003-12-18 06:40:22 +0000158<code>Instruction</code>s, <code>Constant</code>s, <code>GlobalVariable</code>s, or
159any of the other subclasses of <code>Value</code> that LLVM supports.
160Furthermore, if you specify Values that are incorrect for this sequence of
Chris Lattnere46d6012003-11-25 01:35:06 +0000161operations, LLVM will either notice right away (at compilation time) or the LLVM
Chris Lattner45ab10c2003-12-18 06:40:22 +0000162Verifier will pick up the inconsistency when the compiler runs. In either case
163LLVM prevents you from making a type error that gets passed through to the
164generated program. This <em>really</em> helps you write a compiler that
165always generates correct code!<p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000166<p>The second point is that we don't have to worry about branching, registers,
167stack variables, saving partial results, etc. The instructions we create
168<em>are</em> the values we use. Note that all that was created in the above
169code is a Constant value and five operators. Each of the instructions <em>is</em>
Chris Lattnere46d6012003-11-25 01:35:06 +0000170the resulting value of that instruction. This saves a lot of time.</p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000171<p>The lesson is this: <em>SSA form is very powerful: there is no difference
Chris Lattnere46d6012003-11-25 01:35:06 +0000172between a value and the instruction that created it.</em> This is fully
Brian Gaeke07e89e42003-11-24 17:03:38 +0000173enforced by the LLVM IR. Use it to your best advantage.</p>
174</div>
175<!-- ======================================================================= -->
176<div class="doc_subsection"><a name="terminate"></a>Terminate Those Blocks!</div>
177<div class="doc_text">
178<p>I had to learn about terminating blocks the hard way: using the debugger
179to figure out what the LLVM verifier was trying to tell me and begging for
180help on the LLVMdev mailing list. I hope you avoid this experience.</p>
181<p>Emblazon this rule in your mind:</p>
182<ul>
183 <li><em>All</em> <code>BasicBlock</code>s in your compiler <b>must</b> be
184 terminated with a terminating instruction (branch, return, etc.).
185 </li>
186</ul>
187<p>Terminating instructions are a semantic requirement of the LLVM IR. There
188is no facility for implicitly chaining together blocks placed into a function
189in the order they occur. Indeed, in the general case, blocks will not be
190added to the function in the order of execution because of the recursive
191way compilers are written.</p>
192<p>Furthermore, if you don't terminate your blocks, your compiler code will
193compile just fine. You won't find out about the problem until you're running
194the compiler and the module you just created fails on the LLVM Verifier.</p>
195</div>
196<!-- ======================================================================= -->
197<div class="doc_subsection"><a name="blocks"></a>Concrete Blocks</div>
198<div class="doc_text">
199<p>After a little initial fumbling around, I quickly caught on to how blocks
Chris Lattnere46d6012003-11-25 01:35:06 +0000200should be constructed. In general, here's what I learned:
Brian Gaeke07e89e42003-11-24 17:03:38 +0000201<ol>
202 <li><em>Create your blocks early.</em> While writing your compiler, you
203 will encounter several situations where you know apriori that you will
204 need several blocks. For example, if-then-else, switch, while and for
205 statements in C/C++ all need multiple blocks for expression in LVVM.
206 The rule is, create them early.</li>
207 <li><em>Terminate your blocks early.</em> This just reduces the chances
208 that you forget to terminate your blocks which is required (go
209 <a href="#terminate">here</a> for more).
210 <li><em>Use getTerminator() for instruction insertion.</em> I noticed early on
211 that many of the constructors for the Instruction classes take an optional
212 <code>insert_before</code> argument. At first, I thought this was a mistake
213 because clearly the normal mode of inserting instructions would be one at
214 a time <em>after</em> some other instruction, not <em>before</em>. However,
215 if you hold on to your terminating instruction (or use the handy dandy
216 <code>getTerminator()</code> method on a <code>BasicBlock</code>), it can
217 always be used as the <code>insert_before</code> argument to your instruction
218 constructors. This causes the instruction to automatically be inserted in
Chris Lattnere46d6012003-11-25 01:35:06 +0000219 the RightPlace&trade; place, just before the terminating instruction. The
Brian Gaeke07e89e42003-11-24 17:03:38 +0000220 nice thing about this design is that you can pass blocks around and insert
Chris Lattnere46d6012003-11-25 01:35:06 +0000221 new instructions into them without ever knowing what instructions came
Brian Gaeke07e89e42003-11-24 17:03:38 +0000222 before. This makes for some very clean compiler design.</li>
223</ol>
224<p>The foregoing is such an important principal, its worth making an idiom:</p>
Chris Lattnere46d6012003-11-25 01:35:06 +0000225<pre><code>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000226BasicBlock* bb = new BasicBlock();</li>
227bb->getInstList().push_back( new Branch( ... ) );
228new Instruction(..., bb->getTerminator() );
Chris Lattnere46d6012003-11-25 01:35:06 +0000229</code></pre>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000230<p>To make this clear, consider the typical if-then-else statement
231(see StackerCompiler::handle_if() method). We can set this up
232in a single function using LLVM in the following way: </p>
233<pre>
234using namespace llvm;
235BasicBlock*
236MyCompiler::handle_if( BasicBlock* bb, SetCondInst* condition )
237{
238 // Create the blocks to contain code in the structure of if/then/else
Chris Lattner45ab10c2003-12-18 06:40:22 +0000239 BasicBlock* then_bb = new BasicBlock();
240 BasicBlock* else_bb = new BasicBlock();
241 BasicBlock* exit_bb = new BasicBlock();
Brian Gaeke07e89e42003-11-24 17:03:38 +0000242
243 // Insert the branch instruction for the "if"
Chris Lattner45ab10c2003-12-18 06:40:22 +0000244 bb->getInstList().push_back( new BranchInst( then_bb, else_bb, condition ) );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000245
246 // Set up the terminating instructions
Chris Lattner45ab10c2003-12-18 06:40:22 +0000247 then->getInstList().push_back( new BranchInst( exit_bb ) );
248 else->getInstList().push_back( new BranchInst( exit_bb ) );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000249
250 // Fill in the then part .. details excised for brevity
Chris Lattner45ab10c2003-12-18 06:40:22 +0000251 this->fill_in( then_bb );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000252
253 // Fill in the else part .. details excised for brevity
Chris Lattner45ab10c2003-12-18 06:40:22 +0000254 this->fill_in( else_bb );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000255
256 // Return a block to the caller that can be filled in with the code
257 // that follows the if/then/else construct.
Chris Lattner45ab10c2003-12-18 06:40:22 +0000258 return exit_bb;
Brian Gaeke07e89e42003-11-24 17:03:38 +0000259}
260</pre>
261<p>Presumably in the foregoing, the calls to the "fill_in" method would add
262the instructions for the "then" and "else" parts. They would use the third part
263of the idiom almost exclusively (inserting new instructions before the
264terminator). Furthermore, they could even recurse back to <code>handle_if</code>
Chris Lattnere46d6012003-11-25 01:35:06 +0000265should they encounter another if/then/else statement and it will just work.</p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000266<p>Note how cleanly this all works out. In particular, the push_back methods on
267the <code>BasicBlock</code>'s instruction list. These are lists of type
Chris Lattner45ab10c2003-12-18 06:40:22 +0000268<code>Instruction</code> (which is also of type <code>Value</code>). To create
Brian Gaeke07e89e42003-11-24 17:03:38 +0000269the "if" branch we merely instantiate a <code>BranchInst</code> that takes as
Chris Lattner45ab10c2003-12-18 06:40:22 +0000270arguments the blocks to branch to and the condition to branch on. The
271<code>BasicBlock</code> objects act like branch labels! This new
272<code>BranchInst</code> terminates the <code>BasicBlock</code> provided
273as an argument. To give the caller a way to keep inserting after calling
274<code>handle_if</code> we create an <code>exit_bb</code> block which is returned
275to the caller. Note that the <code>exit_bb</code> block is used as the
276terminator for both the <code>then_bb</code> and the <code>else_bb</code>
277blocks. This guarantees that no matter what else <code>handle_if</code>
278or <code>fill_in</code> does, they end up at the <code>exit_bb</code> block.
Brian Gaeke07e89e42003-11-24 17:03:38 +0000279</p>
280</div>
281<!-- ======================================================================= -->
282<div class="doc_subsection"><a name="push_back"></a>push_back Is Your Friend</div>
283<div class="doc_text">
284<p>
285One of the first things I noticed is the frequent use of the "push_back"
286method on the various lists. This is so common that it is worth mentioning.
287The "push_back" inserts a value into an STL list, vector, array, etc. at the
288end. The method might have also been named "insert_tail" or "append".
289Althought I've used STL quite frequently, my use of push_back wasn't very
290high in other programs. In LLVM, you'll use it all the time.
291</p>
292</div>
293<!-- ======================================================================= -->
294<div class="doc_subsection"><a name="gep"></a>The Wily GetElementPtrInst</div>
295<div class="doc_text">
296<p>
297It took a little getting used to and several rounds of postings to the LLVM
298mail list to wrap my head around this instruction correctly. Even though I had
299read the Language Reference and Programmer's Manual a couple times each, I still
300missed a few <em>very</em> key points:
301</p>
302<ul>
303 <li>GetElementPtrInst gives you back a Value for the last thing indexed</em>
304 <li>All global variables in LLVM are <em>pointers</em>.
305 <li>Pointers must also be dereferenced with the GetElementPtrInst instruction.
306</ul>
307<p>This means that when you look up an element in the global variable (assuming
308its a struct or array), you <em>must</em> deference the pointer first! For many
309things, this leads to the idiom:
310</p>
311<pre><code>
312std::vector<Value*> index_vector;
313index_vector.push_back( ConstantSInt::get( Type::LongTy, 0 );
314// ... push other indices ...
315GetElementPtrInst* gep = new GetElementPtrInst( ptr, index_vector );
316</code></pre>
317<p>For example, suppose we have a global variable whose type is [24 x int]. The
318variable itself represents a <em>pointer</em> to that array. To subscript the
319array, we need two indices, not just one. The first index (0) dereferences the
320pointer. The second index subscripts the array. If you're a "C" programmer, this
321will run against your grain because you'll naturally think of the global array
322variable and the address of its first element as the same. That tripped me up
323for a while until I realized that they really do differ .. by <em>type</em>.
Chris Lattner45ab10c2003-12-18 06:40:22 +0000324Remember that LLVM is strongly typed. Everything has a type.
325The "type" of the global variable is [24 x int]*. That is, its
Brian Gaeke07e89e42003-11-24 17:03:38 +0000326a pointer to an array of 24 ints. When you dereference that global variable with
Chris Lattnere46d6012003-11-25 01:35:06 +0000327a single (0) index, you now have a "[24 x int]" type. Although
Brian Gaeke07e89e42003-11-24 17:03:38 +0000328the pointer value of the dereferenced global and the address of the zero'th element
329in the array will be the same, they differ in their type. The zero'th element has
330type "int" while the pointer value has type "[24 x int]".</p>
331<p>Get this one aspect of LLVM right in your head and you'll save yourself
332a lot of compiler writing headaches down the road.</p>
333</div>
334<!-- ======================================================================= -->
Brian Gaeke90181482003-11-24 02:52:51 +0000335<div class="doc_subsection"><a name="linkage"></a>Getting Linkage Types Right</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000336<div class="doc_text">
337<p>Linkage types in LLVM can be a little confusing, especially if your compiler
Chris Lattner45ab10c2003-12-18 06:40:22 +0000338writing mind has affixed firm concepts to particular words like "weak",
Brian Gaeke07e89e42003-11-24 17:03:38 +0000339"external", "global", "linkonce", etc. LLVM does <em>not</em> use the precise
340definitions of say ELF or GCC even though they share common terms. To be fair,
341the concepts are related and similar but not precisely the same. This can lead
342you to think you know what a linkage type represents but in fact it is slightly
343different. I recommend you read the
344<a href="LangRef.html#linkage"> Language Reference on this topic</a> very
Chris Lattnere46d6012003-11-25 01:35:06 +0000345carefully. Then, read it again.<p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000346<p>Here are some handy tips that I discovered along the way:</p>
347<ul>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000348 <li><em>Unitialized means external.</em> That is, the symbol is declared in the current
Brian Gaeke07e89e42003-11-24 17:03:38 +0000349 module and can be used by that module but it is not defined by that module.</li>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000350 <li><em>Setting an initializer changes a global' linkage type.</em> Setting an
351 initializer changes a global's linkage type from whatever it was to a normal,
352 defind global (not external). You'll need to call the setLinkage() method to
353 reset it if you specify the initializer after the GlobalValue has been constructed.
354 This is important for LinkOnce and Weak linkage types.</li>
355 <li><em>Appending linkage can keep track of things.</em> Appending linkage can
356 be used to keep track of compilation information at runtime. It could be used,
357 for example, to build a full table of all the C++ virtual tables or hold the
358 C++ RTTI data, or whatever. Appending linkage can only be applied to arrays.
359 All arrays with the same name in each module are concatenated together at link
360 time.</li>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000361</ul>
362</div>
363<!-- ======================================================================= -->
364<div class="doc_subsection"><a name="constants"></a>Constants Are Easier Than That!</div>
365<div class="doc_text">
366<p>
367Constants in LLVM took a little getting used to until I discovered a few utility
368functions in the LLVM IR that make things easier. Here's what I learned: </p>
369<ul>
370 <li>Constants are Values like anything else and can be operands of instructions</li>
371 <li>Integer constants, frequently needed can be created using the static "get"
372 methods of the ConstantInt, ConstantSInt, and ConstantUInt classes. The nice thing
373 about these is that you can "get" any kind of integer quickly.</li>
374 <li>There's a special method on Constant class which allows you to get the null
375 constant for <em>any</em> type. This is really handy for initializing large
376 arrays or structures, etc.</li>
377</ul>
378</div>
Brian Gaeke90181482003-11-24 02:52:51 +0000379<!-- ======================================================================= -->
380<div class="doc_section"> <a name="lexicon">The Stacker Lexicon</a></div>
Chris Lattnere46d6012003-11-25 01:35:06 +0000381<div class="doc_text"><p>This section describes the Stacker language</p></div>
Brian Gaeke90181482003-11-24 02:52:51 +0000382<div class="doc_subsection"><a name="stack"></a>The Stack</div>
383<div class="doc_text">
384<p>Stacker definitions define what they do to the global stack. Before
385proceeding, a few words about the stack are in order. The stack is simply
386a global array of 32-bit integers or pointers. A global index keeps track
Chris Lattnere46d6012003-11-25 01:35:06 +0000387of the location of the top of the stack. All of this is hidden from the
Brian Gaeke90181482003-11-24 02:52:51 +0000388programmer but it needs to be noted because it is the foundation of the
389conceptual programming model for Stacker. When you write a definition,
390you are, essentially, saying how you want that definition to manipulate
391the global stack.</p>
392<p>Manipulating the stack can be quite hazardous. There is no distinction
393given and no checking for the various types of values that can be placed
394on the stack. Automatic coercion between types is performed. In many
395cases this is useful. For example, a boolean value placed on the stack
396can be interpreted as an integer with good results. However, using a
397word that interprets that boolean value as a pointer to a string to
398print out will almost always yield a crash. Stacker simply leaves it
399to the programmer to get it right without any interference or hindering
Chris Lattnere46d6012003-11-25 01:35:06 +0000400on interpretation of the stack values. You've been warned. :) </p>
Brian Gaeke90181482003-11-24 02:52:51 +0000401</div>
402<!-- ======================================================================= -->
403<div class="doc_subsection"> <a name="punctuation"></a>Punctuation</div>
404<div class="doc_text">
405<p>Punctuation in Stacker is very simple. The colon and semi-colon
406characters are used to introduce and terminate a definition
407(respectively). Except for <em>FORWARD</em> declarations, definitions
408are all you can specify in Stacker. Definitions are read left to right.
Chris Lattnere46d6012003-11-25 01:35:06 +0000409Immediately after the colon comes the name of the word being defined.
410The remaining words in the definition specify what the word does. The definition
411is terminated by a semi-colon.</p>
412<p>So, your typical definition will have the form:</p>
413<pre><code>: name ... ;</code></pre>
414<p>The <code>name</code> is up to you but it must start with a letter and contain
415only letters numbers and underscore. Names are case sensitive and must not be
416the same as the name of a built-in word. The <code>...</code> is replaced by
417the stack manipulting words that you wish define <code>name</code> as. <p>
418</div>
419<!-- ======================================================================= -->
420<div class="doc_subsection"><a name="comments"></a>Comments</div>
421<div class="doc_text">
422 <p>Stacker supports two types of comments. A hash mark (#) starts a comment
423 that extends to the end of the line. It is identical to the kind of comments
424 commonly used in shell scripts. A pair of parentheses also surround a comment.
425 In both cases, the content of the comment is ignored by the Stacker compiler. The
426 following does nothing in Stacker.
427 </p>
428<pre><code>
429# This is a comment to end of line
430( This is an enclosed comment )
431</code></pre>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000432<p>See the <a href="#example">example</a> program to see comments in use in
Chris Lattnere46d6012003-11-25 01:35:06 +0000433a real program.</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000434</div>
435<!-- ======================================================================= -->
436<div class="doc_subsection"><a name="literals"></a>Literals</div>
437<div class="doc_text">
438 <p>There are three kinds of literal values in Stacker. Integer, Strings,
439 and Booleans. In each case, the stack operation is to simply push the
440 value onto the stack. So, for example:<br/>
441 <code> 42 " is the answer." TRUE </code><br/>
442 will push three values onto the stack: the integer 42, the
443 string " is the answer." and the boolean TRUE.</p>
444</div>
445<!-- ======================================================================= -->
446<div class="doc_subsection"><a name="words"></a>Words</div>
447<div class="doc_text">
448<p>Each definition in Stacker is composed of a set of words. Words are
449read and executed in order from left to right. There is very little
450checking in Stacker to make sure you're doing the right thing with
451the stack. It is assumed that the programmer knows how the stack
452transformation he applies will affect the program.</p>
453<p>Words in a definition come in two flavors: built-in and programmer
454defined. Simply mentioning the name of a previously defined or declared
Chris Lattner45ab10c2003-12-18 06:40:22 +0000455programmer-defined word causes that word's stack actions to be invoked. It
Brian Gaeke90181482003-11-24 02:52:51 +0000456is somewhat like a function call in other languages. The built-in
Chris Lattner45ab10c2003-12-18 06:40:22 +0000457words have various effects, described <a href="#builtins">below</a>.</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000458<p>Sometimes you need to call a word before it is defined. For this, you can
Chris Lattnere46d6012003-11-25 01:35:06 +0000459use the <code>FORWARD</code> declaration. It looks like this:</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000460<p><code>FORWARD name ;</code></p>
461<p>This simply states to Stacker that "name" is the name of a definition
462that is defined elsewhere. Generally it means the definition can be found
463"forward" in the file. But, it doesn't have to be in the current compilation
464unit. Anything declared with <code>FORWARD</code> is an external symbol for
465linking.</p>
466</div>
467<!-- ======================================================================= -->
468<div class="doc_subsection"><a name="builtins"></a>Built In Words</div>
469<div class="doc_text">
470<p>The built-in words of the Stacker language are put in several groups
471depending on what they do. The groups are as follows:</p>
472<ol>
473 <li><em>Logical</em>These words provide the logical operations for
474 comparing stack operands.<br/>The words are: &lt; &gt; &lt;= &gt;=
475 = &lt;&gt; true false.</li>
476 <li><em>Bitwise</em>These words perform bitwise computations on
477 their operands. <br/> The words are: &lt;&lt; &gt;&gt; XOR AND NOT</li>
478 <li><em>Arithmetic</em>These words perform arithmetic computations on
479 their operands. <br/> The words are: ABS NEG + - * / MOD */ ++ -- MIN MAX</li>
480 <li><em>Stack</em>These words manipulate the stack directly by moving
Chris Lattner45ab10c2003-12-18 06:40:22 +0000481 its elements around.<br/> The words are: DROP DROP2 NIP NIP2 DUP DUP2
482 SWAP SWAP2 OVER OVER2 ROT ROT2 RROT RROT2 TUCK TUCK2 PICK SELECT ROLL</li>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000483 <li><em>Memory</em>These words allocate, free and manipulate memory
Brian Gaeke90181482003-11-24 02:52:51 +0000484 areas outside the stack.<br/>The words are: MALLOC FREE GET PUT</li>
485 <li><em>Control</em>These words alter the normal left to right flow
486 of execution.<br/>The words are: IF ELSE ENDIF WHILE END RETURN EXIT RECURSE</li>
487 <li><em>I/O</em> These words perform output on the standard output
488 and input on the standard input. No other I/O is possible in Stacker.
489 <br/>The words are: SPACE TAB CR &gt;s &gt;d &gt;c &lt;s &lt;d &lt;c.</li>
490</ol>
491<p>While you may be familiar with many of these operations from other
492programming languages, a careful review of their semantics is important
493for correct programming in Stacker. Of most importance is the effect
494that each of these built-in words has on the global stack. The effect is
495not always intuitive. To better describe the effects, we'll borrow from Forth the idiom of
496describing the effect on the stack with:</p>
497<p><code> BEFORE -- AFTER </code></p>
498<p>That is, to the left of the -- is a representation of the stack before
499the operation. To the right of the -- is a representation of the stack
500after the operation. In the table below that describes the operation of
501each of the built in words, we will denote the elements of the stack
502using the following construction:</p>
503<ol>
504 <li><em>b</em> - a boolean truth value</li>
505 <li><em>w</em> - a normal integer valued word.</li>
506 <li><em>s</em> - a pointer to a string value</li>
Chris Lattnere46d6012003-11-25 01:35:06 +0000507 <li><em>p</em> - a pointer to a malloc'd memory block</li>
Brian Gaeke90181482003-11-24 02:52:51 +0000508</ol>
509</div>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000510<div class="doc_text" >
511 <table class="doc_table" style="border: 2px solid blue; border-collapse: collapse;" >
512<tr class="doc_table"><td colspan="4" style="border: 2px solid blue">Definition Of Operation Of Built In Words</td></tr>
513<tr class="doc_table"><td colspan="4" style="border: 2px solid blue"><b>LOGICAL OPERATIONS</b></td></tr>
514<tr class="doc_table">
515 <td style="border: 2px solid blue"><u>Word</u></td>
516 <td style="border: 2px solid blue"><u>Name</u></td>
517 <td style="border: 2px solid blue"><u>Operation</u></td>
518 <td style="border: 2px solid blue"><u>Description</u></td>
519</tr>
520<tr class="doc_table"><td style="border: 2px solid blue">&lt;</td>
521 <td style="border: 2px solid blue">LT</td>
522 <td style="border: 2px solid blue">w1 w2 -- b</td>
523 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000524 compared. If w1 is less than w2, TRUE is pushed back on
525 the stack, otherwise FALSE is pushed back on the stack.</td>
526</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000527<tr><td style="border: 2px solid blue">&gt;</td>
528 <td style="border: 2px solid blue">GT</td>
529 <td style="border: 2px solid blue">w1 w2 -- b</td>
530 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000531 compared. If w1 is greater than w2, TRUE is pushed back on
532 the stack, otherwise FALSE is pushed back on the stack.</td>
533</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000534<tr><td style="border: 2px solid blue">&gt;=</td>
535 <td style="border: 2px solid blue">GE</td>
536 <td style="border: 2px solid blue">w1 w2 -- b</td>
537 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000538 compared. If w1 is greater than or equal to w2, TRUE is
539 pushed back on the stack, otherwise FALSE is pushed back
540 on the stack.</td>
541</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000542<tr><td style="border: 2px solid blue">&lt;=</td>
543 <td style="border: 2px solid blue">LE</td>
544 <td style="border: 2px solid blue">w1 w2 -- b</td>
545 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000546 compared. If w1 is less than or equal to w2, TRUE is
547 pushed back on the stack, otherwise FALSE is pushed back
548 on the stack.</td>
549</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000550<tr><td style="border: 2px solid blue">=</td>
551 <td style="border: 2px solid blue">EQ</td>
552 <td style="border: 2px solid blue">w1 w2 -- b</td>
553 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000554 compared. If w1 is equal to w2, TRUE is
555 pushed back on the stack, otherwise FALSE is pushed back
556 </td>
557</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000558<tr><td style="border: 2px solid blue">&lt;&gt;</td>
559 <td style="border: 2px solid blue">NE</td>
560 <td style="border: 2px solid blue">w1 w2 -- b</td>
561 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000562 compared. If w1 is equal to w2, TRUE is
563 pushed back on the stack, otherwise FALSE is pushed back
564 </td>
565</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000566<tr><td style="border: 2px solid blue">FALSE</td>
567 <td style="border: 2px solid blue">FALSE</td>
568 <td style="border: 2px solid blue"> -- b</td>
569 <td style="border: 2px solid blue">The boolean value FALSE (0) is pushed onto the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000570</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000571<tr><td style="border: 2px solid blue">TRUE</td>
572 <td style="border: 2px solid blue">TRUE</td>
573 <td style="border: 2px solid blue"> -- b</td>
574 <td style="border: 2px solid blue">The boolean value TRUE (-1) is pushed onto the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000575</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000576<tr><td colspan="4"><b>BITWISE OPERATORS</b></td></tr>
577<tr>
578 <td style="border: 2px solid blue"><u>Word</u></td>
579 <td style="border: 2px solid blue"><u>Name</u></td>
580 <td style="border: 2px solid blue"><u>Operation</u></td>
581 <td style="border: 2px solid blue"><u>Description</u></td>
582</tr>
583<tr><td style="border: 2px solid blue">&lt;&lt;</td>
584 <td style="border: 2px solid blue">SHL</td>
585 <td style="border: 2px solid blue">w1 w2 -- w1&lt;&lt;w2</td>
586 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack. The w2
Brian Gaeke90181482003-11-24 02:52:51 +0000587 operand is shifted left by the number of bits given by the
588 w1 operand. The result is pushed back to the stack.</td>
589</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000590<tr><td style="border: 2px solid blue">&gt;&gt;</td>
591 <td style="border: 2px solid blue">SHR</td>
592 <td style="border: 2px solid blue">w1 w2 -- w1&gt;&gt;w2</td>
593 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack. The w2
Brian Gaeke90181482003-11-24 02:52:51 +0000594 operand is shifted right by the number of bits given by the
595 w1 operand. The result is pushed back to the stack.</td>
596</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000597<tr><td style="border: 2px solid blue">OR</td>
598 <td style="border: 2px solid blue">OR</td>
599 <td style="border: 2px solid blue">w1 w2 -- w2|w1</td>
600 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack. The values
Brian Gaeke90181482003-11-24 02:52:51 +0000601 are bitwise OR'd together and pushed back on the stack. This is
602 not a logical OR. The sequence 1 2 OR yields 3 not 1.</td>
603</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000604<tr><td style="border: 2px solid blue">AND</td>
605 <td style="border: 2px solid blue">AND</td>
606 <td style="border: 2px solid blue">w1 w2 -- w2&amp;w1</td>
607 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack. The values
Brian Gaeke90181482003-11-24 02:52:51 +0000608 are bitwise AND'd together and pushed back on the stack. This is
609 not a logical AND. The sequence 1 2 AND yields 0 not 1.</td>
610</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000611<tr><td style="border: 2px solid blue">XOR</td>
612 <td style="border: 2px solid blue">XOR</td>
613 <td style="border: 2px solid blue">w1 w2 -- w2^w1</td>
614 <td style="border: 2px solid blue">Two values (w1 and w2) are popped off the stack. The values
Brian Gaeke90181482003-11-24 02:52:51 +0000615 are bitwise exclusive OR'd together and pushed back on the stack.
616 For example, The sequence 1 3 XOR yields 2.</td>
617</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000618<tr><td colspan="4"><b>ARITHMETIC OPERATORS</b></td></tr>
619<tr>
620 <td style="border: 2px solid blue"><u>Word</u></td>
621 <td style="border: 2px solid blue"><u>Name</u></td>
622 <td style="border: 2px solid blue"><u>Operation</u></td>
623 <td style="border: 2px solid blue"><u>Description</u></td>
624</tr>
625<tr><td style="border: 2px solid blue">ABS</td>
626 <td style="border: 2px solid blue">ABS</td>
627 <td style="border: 2px solid blue">w -- |w|</td>
628 <td style="border: 2px solid blue">One value s popped off the stack; its absolute value is computed
Brian Gaeke90181482003-11-24 02:52:51 +0000629 and then pushed onto the stack. If w1 is -1 then w2 is 1. If w1 is
630 1 then w2 is also 1.</td>
631</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000632<tr><td style="border: 2px solid blue">NEG</td>
633 <td style="border: 2px solid blue">NEG</td>
634 <td style="border: 2px solid blue">w -- -w</td>
635 <td style="border: 2px solid blue">One value is popped off the stack which is negated and then
Brian Gaeke90181482003-11-24 02:52:51 +0000636 pushed back onto the stack. If w1 is -1 then w2 is 1. If w1 is
637 1 then w2 is -1.</td>
638</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000639<tr><td style="border: 2px solid blue"> + </td>
640 <td style="border: 2px solid blue">ADD</td>
641 <td style="border: 2px solid blue">w1 w2 -- w2+w1</td>
642 <td style="border: 2px solid blue">Two values are popped off the stack. Their sum is pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000643 onto the stack</td>
644</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000645<tr><td style="border: 2px solid blue"> - </td>
646 <td style="border: 2px solid blue">SUB</td>
647 <td style="border: 2px solid blue">w1 w2 -- w2-w1</td>
648 <td style="border: 2px solid blue">Two values are popped off the stack. Their difference is pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000649 onto the stack</td>
650</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000651<tr><td style="border: 2px solid blue"> * </td>
652 <td style="border: 2px solid blue">MUL</td>
653 <td style="border: 2px solid blue">w1 w2 -- w2*w1</td>
654 <td style="border: 2px solid blue">Two values are popped off the stack. Their product is pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000655 onto the stack</td>
656</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000657<tr><td style="border: 2px solid blue"> / </td>
658 <td style="border: 2px solid blue">DIV</td>
659 <td style="border: 2px solid blue">w1 w2 -- w2/w1</td>
660 <td style="border: 2px solid blue">Two values are popped off the stack. Their quotient is pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000661 onto the stack</td>
662</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000663<tr><td style="border: 2px solid blue">MOD</td>
664 <td style="border: 2px solid blue">MOD</td>
665 <td style="border: 2px solid blue">w1 w2 -- w2%w1</td>
666 <td style="border: 2px solid blue">Two values are popped off the stack. Their remainder after division
Brian Gaeke90181482003-11-24 02:52:51 +0000667 of w1 by w2 is pushed back onto the stack</td>
668</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000669<tr><td style="border: 2px solid blue"> */ </td>
670 <td style="border: 2px solid blue">STAR_SLAH</td>
671 <td style="border: 2px solid blue">w1 w2 w3 -- (w3*w2)/w1</td>
672 <td style="border: 2px solid blue">Three values are popped off the stack. The product of w1 and w2 is
Brian Gaeke90181482003-11-24 02:52:51 +0000673 divided by w3. The result is pushed back onto the stack.</td>
674</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000675<tr><td style="border: 2px solid blue"> ++ </td>
676 <td style="border: 2px solid blue">INCR</td>
677 <td style="border: 2px solid blue">w -- w+1</td>
678 <td style="border: 2px solid blue">One value is popped off the stack. It is incremented by one and then
Brian Gaeke90181482003-11-24 02:52:51 +0000679 pushed back onto the stack.</td>
680</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000681<tr><td style="border: 2px solid blue"> -- </td>
682 <td style="border: 2px solid blue">DECR</td>
683 <td style="border: 2px solid blue">w -- w-1</td>
684 <td style="border: 2px solid blue">One value is popped off the stack. It is decremented by one and then
Brian Gaeke90181482003-11-24 02:52:51 +0000685 pushed back onto the stack.</td>
686</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000687<tr><td style="border: 2px solid blue">MIN</td>
688 <td style="border: 2px solid blue">MIN</td>
689 <td style="border: 2px solid blue">w1 w2 -- (w2&lt;w1?w2:w1)</td>
690 <td style="border: 2px solid blue">Two values are popped off the stack. The larger one is pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000691 onto the stack.</td>
692</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000693<tr><td style="border: 2px solid blue">MAX</td>
694 <td style="border: 2px solid blue">MAX</td>
695 <td style="border: 2px solid blue">w1 w2 -- (w2&gt;w1?w2:w1)</td>
696 <td style="border: 2px solid blue">Two values are popped off the stack. The larger value is pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000697 onto the stack.</td>
698</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000699<tr><td colspan="4"><b>STACK MANIPULATION OPERATORS</b></td></tr>
700<tr>
701 <td style="border: 2px solid blue"><u>Word</u></td>
702 <td style="border: 2px solid blue"><u>Name</u></td>
703 <td style="border: 2px solid blue"><u>Operation</u></td>
704 <td style="border: 2px solid blue"><u>Description</u></td>
Brian Gaeke90181482003-11-24 02:52:51 +0000705</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000706<tr><td style="border: 2px solid blue">DROP</td>
707 <td style="border: 2px solid blue">DROP</td>
708 <td style="border: 2px solid blue">w -- </td>
709 <td style="border: 2px solid blue">One value is popped off the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000710</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000711<tr><td style="border: 2px solid blue">DROP2</td>
712 <td style="border: 2px solid blue">DROP2</td>
713 <td style="border: 2px solid blue">w1 w2 -- </td>
714 <td style="border: 2px solid blue">Two values are popped off the stack.</td>
715</tr>
716<tr><td style="border: 2px solid blue">NIP</td>
717 <td style="border: 2px solid blue">NIP</td>
718 <td style="border: 2px solid blue">w1 w2 -- w2</td>
719 <td style="border: 2px solid blue">The second value on the stack is removed from the stack. That is,
Brian Gaeke90181482003-11-24 02:52:51 +0000720 a value is popped off the stack and retained. Then a second value is
721 popped and the retained value is pushed.</td>
722</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000723<tr><td style="border: 2px solid blue">NIP2</td>
724 <td style="border: 2px solid blue">NIP2</td>
725 <td style="border: 2px solid blue">w1 w2 w3 w4 -- w3 w4</td>
726 <td style="border: 2px solid blue">The third and fourth values on the stack are removed from it. That is,
Brian Gaeke90181482003-11-24 02:52:51 +0000727 two values are popped and retained. Then two more values are popped and
728 the two retained values are pushed back on.</td>
729</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000730<tr><td style="border: 2px solid blue">DUP</td>
731 <td style="border: 2px solid blue">DUP</td>
732 <td style="border: 2px solid blue">w1 -- w1 w1</td>
733 <td style="border: 2px solid blue">One value is popped off the stack. That value is then pushed onto
Brian Gaeke90181482003-11-24 02:52:51 +0000734 the stack twice to duplicate the top stack vaue.</td>
735</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000736<tr><td style="border: 2px solid blue">DUP2</td>
737 <td style="border: 2px solid blue">DUP2</td>
738 <td style="border: 2px solid blue">w1 w2 -- w1 w2 w1 w2</td>
739 <td style="border: 2px solid blue">The top two values on the stack are duplicated. That is, two vaues
Brian Gaeke90181482003-11-24 02:52:51 +0000740 are popped off the stack. They are alternately pushed back on the
741 stack twice each.</td>
742</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000743<tr><td style="border: 2px solid blue">SWAP</td>
744 <td style="border: 2px solid blue">SWAP</td>
745 <td style="border: 2px solid blue">w1 w2 -- w2 w1</td>
746 <td style="border: 2px solid blue">The top two stack items are reversed in their order. That is, two
Brian Gaeke90181482003-11-24 02:52:51 +0000747 values are popped off the stack and pushed back onto the stack in
748 the opposite order they were popped.</td>
749</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000750<tr><td style="border: 2px solid blue">SWAP2</td>
751 <td style="border: 2px solid blue">SWAP2</td>
752 <td style="border: 2px solid blue">w1 w2 w3 w4 -- w3 w4 w2 w1</td>
753 <td style="border: 2px solid blue">The top four stack items are swapped in pairs. That is, two values
Brian Gaeke90181482003-11-24 02:52:51 +0000754 are popped and retained. Then, two more values are popped and retained.
755 The values are pushed back onto the stack in the reverse order but
756 in pairs.</p>
757</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000758<tr><td style="border: 2px solid blue">OVER</td>
759 <td style="border: 2px solid blue">OVER</td>
760 <td style="border: 2px solid blue">w1 w2-- w1 w2 w1</td>
761 <td style="border: 2px solid blue">Two values are popped from the stack. They are pushed back
Brian Gaeke90181482003-11-24 02:52:51 +0000762 onto the stack in the order w1 w2 w1. This seems to cause the
763 top stack element to be duplicated "over" the next value.</td>
764</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000765<tr><td style="border: 2px solid blue">OVER2</td>
766 <td style="border: 2px solid blue">OVER2</td>
767 <td style="border: 2px solid blue">w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2</td>
768 <td style="border: 2px solid blue">The third and fourth values on the stack are replicated onto the
Brian Gaeke90181482003-11-24 02:52:51 +0000769 top of the stack</td>
770</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000771<tr><td style="border: 2px solid blue">ROT</td>
772 <td style="border: 2px solid blue">ROT</td>
773 <td style="border: 2px solid blue">w1 w2 w3 -- w2 w3 w1</td>
774 <td style="border: 2px solid blue">The top three values are rotated. That is, three value are popped
Brian Gaeke90181482003-11-24 02:52:51 +0000775 off the stack. They are pushed back onto the stack in the order
776 w1 w3 w2.</td>
777</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000778<tr><td style="border: 2px solid blue">ROT2</td>
779 <td style="border: 2px solid blue">ROT2</td>
780 <td style="border: 2px solid blue">w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2</td>
781 <td style="border: 2px solid blue">Like ROT but the rotation is done using three pairs instead of
Brian Gaeke90181482003-11-24 02:52:51 +0000782 three singles.</td>
783</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000784<tr><td style="border: 2px solid blue">RROT</td>
785 <td style="border: 2px solid blue">RROT</td>
786 <td style="border: 2px solid blue">w1 w2 w3 -- w2 w3 w1</td>
787 <td style="border: 2px solid blue">Reverse rotation. Like ROT, but it rotates the other way around.
Brian Gaeke90181482003-11-24 02:52:51 +0000788 Essentially, the third element on the stack is moved to the top
789 of the stack.</td>
790</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000791<tr><td style="border: 2px solid blue">RROT2</td>
792 <td style="border: 2px solid blue">RROT2</td>
793 <td style="border: 2px solid blue">w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2</td>
794 <td style="border: 2px solid blue">Double reverse rotation. Like RROT but the rotation is done using
Brian Gaeke90181482003-11-24 02:52:51 +0000795 three pairs instead of three singles. The fifth and sixth stack
796 elements are moved to the first and second positions</td>
797</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000798<tr><td style="border: 2px solid blue">TUCK</td>
799 <td style="border: 2px solid blue">TUCK</td>
800 <td style="border: 2px solid blue">w1 w2 -- w2 w1 w2</td>
801 <td style="border: 2px solid blue">Similar to OVER except that the second operand is being
Brian Gaeke90181482003-11-24 02:52:51 +0000802 replicated. Essentially, the first operand is being "tucked"
803 in between two instances of the second operand. Logically, two
804 values are popped off the stack. They are placed back on the
805 stack in the order w2 w1 w2.</td>
806</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000807<tr><td style="border: 2px solid blue">TUCK2</td>
808 <td style="border: 2px solid blue">TUCK2</td>
809 <td style="border: 2px solid blue">w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4</td>
810 <td style="border: 2px solid blue">Like TUCK but a pair of elements is tucked over two pairs.
Brian Gaeke90181482003-11-24 02:52:51 +0000811 That is, the top two elements of the stack are duplicated and
812 inserted into the stack at the fifth and positions.</td>
813</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000814<tr><td style="border: 2px solid blue">PICK</td>
815 <td style="border: 2px solid blue">PICK</td>
816 <td style="border: 2px solid blue">x0 ... Xn n -- x0 ... Xn x0</td>
817 <td style="border: 2px solid blue">The top of the stack is used as an index into the remainder of
Brian Gaeke90181482003-11-24 02:52:51 +0000818 the stack. The element at the nth position replaces the index
819 (top of stack). This is useful for cycling through a set of
820 values. Note that indexing is zero based. So, if n=0 then you
821 get the second item on the stack. If n=1 you get the third, etc.
822 Note also that the index is replaced by the n'th value. </td>
823</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000824<tr><td style="border: 2px solid blue">SELECT</td>
825 <td style="border: 2px solid blue">SELECT</td>
826 <td style="border: 2px solid blue">m n X0..Xm Xm+1 .. Xn -- Xm</td>
827 <td style="border: 2px solid blue">This is like PICK but the list is removed and you need to specify
Brian Gaeke90181482003-11-24 02:52:51 +0000828 both the index and the size of the list. Careful with this one,
829 the wrong value for n can blow away a huge amount of the stack.</td>
830</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000831<tr><td style="border: 2px solid blue">ROLL</td>
832 <td style="border: 2px solid blue">ROLL</td>
833 <td style="border: 2px solid blue">x0 x1 .. xn n -- x1 .. xn x0</td>
834 <td style="border: 2px solid blue"><b>Not Implemented</b>. This one has been left as an exercise to
Chris Lattnere46d6012003-11-25 01:35:06 +0000835 the student. See <a href="#exercise">Exercise</a>. ROLL requires
836 a value, "n", to be on the top of the stack. This value specifies how
837 far into the stack to "roll". The n'th value is <em>moved</em> (not
838 copied) from its location and replaces the "n" value on the top of the
839 stack. In this way, all the values between "n" and x0 roll up the stack.
840 The operation of ROLL is a generalized ROT. The "n" value specifies
841 how much to rotate. That is, ROLL with n=1 is the same as ROT and
842 ROLL with n=2 is the same as ROT2.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000843</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000844<tr><td colspan="4"><b>MEMORY OPERATORS</b></td></tr>
845<tr>
846 <td style="border: 2px solid blue"><u>Word</u></td>
847 <td style="border: 2px solid blue"><u>Name</u></td>
848 <td style="border: 2px solid blue"><u>Operation</u></td>
849 <td style="border: 2px solid blue"><u>Description</u></td>
850</tr>
851<tr><td style="border: 2px solid blue">MALLOC</td>
852 <td style="border: 2px solid blue">MALLOC</td>
853 <td style="border: 2px solid blue">w1 -- p</td>
854 <td style="border: 2px solid blue">One value is popped off the stack. The value is used as the size
Brian Gaeke90181482003-11-24 02:52:51 +0000855 of a memory block to allocate. The size is in bytes, not words.
856 The memory allocation is completed and the address of the memory
857 block is pushed onto the stack.</td>
858</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000859<tr><td style="border: 2px solid blue">FREE</td>
860 <td style="border: 2px solid blue">FREE</td>
861 <td style="border: 2px solid blue">p -- </td>
862 <td style="border: 2px solid blue">One pointer value is popped off the stack. The value should be
Brian Gaeke90181482003-11-24 02:52:51 +0000863 the address of a memory block created by the MALLOC operation. The
864 associated memory block is freed. Nothing is pushed back on the
865 stack. Many bugs can be created by attempting to FREE something
866 that isn't a pointer to a MALLOC allocated memory block. Make
867 sure you know what's on the stack. One way to do this is with
868 the following idiom:<br/>
869 <code>64 MALLOC DUP DUP (use ptr) DUP (use ptr) ... FREE</code>
870 <br/>This ensures that an extra copy of the pointer is placed on
871 the stack (for the FREE at the end) and that every use of the
872 pointer is preceded by a DUP to retain the copy for FREE.</td>
873</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000874<tr><td style="border: 2px solid blue">GET</td>
875 <td style="border: 2px solid blue">GET</td>
876 <td style="border: 2px solid blue">w1 p -- w2 p</td>
877 <td style="border: 2px solid blue">An integer index and a pointer to a memory block are popped of
Brian Gaeke90181482003-11-24 02:52:51 +0000878 the block. The index is used to index one byte from the memory
879 block. That byte value is retained, the pointer is pushed again
880 and the retained value is pushed. Note that the pointer value
881 s essentially retained in its position so this doesn't count
882 as a "use ptr" in the FREE idiom.</td>
883</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000884<tr><td style="border: 2px solid blue">PUT</td>
885 <td style="border: 2px solid blue">PUT</td>
886 <td style="border: 2px solid blue">w1 w2 p -- p </td>
887 <td style="border: 2px solid blue">An integer value is popped of the stack. This is the value to
Brian Gaeke90181482003-11-24 02:52:51 +0000888 be put into a memory block. Another integer value is popped of
889 the stack. This is the indexed byte in the memory block. A
890 pointer to the memory block is popped off the stack. The
891 first value (w1) is then converted to a byte and written
892 to the element of the memory block(p) at the index given
893 by the second value (w2). The pointer to the memory block is
894 pushed back on the stack so this doesn't count as a "use ptr"
895 in the FREE idiom.</td>
896</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000897<tr><td colspan="4"><b>CONTROL FLOW OPERATORS</b></td></tr>
898<tr>
899 <td style="border: 2px solid blue"><u>Word</u></td>
900 <td style="border: 2px solid blue"><u>Name</u></td>
901 <td style="border: 2px solid blue"><u>Operation</u></td>
902 <td style="border: 2px solid blue"><u>Description</u></td>
903</tr>
904<tr><td style="border: 2px solid blue">RETURN</td>
905 <td style="border: 2px solid blue">RETURN</td>
906 <td style="border: 2px solid blue"> -- </td>
907 <td style="border: 2px solid blue">The currently executing definition returns immediately to its caller.
Brian Gaeke90181482003-11-24 02:52:51 +0000908 Note that there is an implicit <code>RETURN</code> at the end of each
909 definition, logically located at the semi-colon. The sequence
910 <code>RETURN ;</code> is valid but redundant.</td>
911</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000912<tr><td style="border: 2px solid blue">EXIT</td>
913 <td style="border: 2px solid blue">EXIT</td>
914 <td style="border: 2px solid blue">w1 -- </td>
915 <td style="border: 2px solid blue">A return value for the program is popped off the stack. The program is
Brian Gaeke90181482003-11-24 02:52:51 +0000916 then immediately terminated. This is normally an abnormal exit from the
917 program. For a normal exit (when <code>MAIN</code> finishes), the exit
918 code will always be zero in accordance with UNIX conventions.</td>
919</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000920<tr><td style="border: 2px solid blue">RECURSE</td>
921 <td style="border: 2px solid blue">RECURSE</td>
922 <td style="border: 2px solid blue"> -- </td>
923 <td style="border: 2px solid blue">The currently executed definition is called again. This operation is
Brian Gaeke90181482003-11-24 02:52:51 +0000924 needed since the definition of a word doesn't exist until the semi colon
925 is reacher. Attempting something like:<br/>
926 <code> : recurser recurser ; </code><br/> will yield and error saying that
927 "recurser" is not defined yet. To accomplish the same thing, change this
928 to:<br/>
929 <code> : recurser RECURSE ; </code></td>
930</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000931<tr><td style="border: 2px solid blue">IF (words...) ENDIF</td>
932 <td style="border: 2px solid blue">IF (words...) ENDIF</td>
933 <td style="border: 2px solid blue">b -- </td>
934 <td style="border: 2px solid blue">A boolean value is popped of the stack. If it is non-zero then the "words..."
Brian Gaeke90181482003-11-24 02:52:51 +0000935 are executed. Otherwise, execution continues immediately following the ENDIF.</td>
936</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000937<tr><td style="border: 2px solid blue">IF (words...) ELSE (words...) ENDIF</td>
938 <td style="border: 2px solid blue">IF (words...) ELSE (words...) ENDIF</td>
939 <td style="border: 2px solid blue">b -- </td>
940 <td style="border: 2px solid blue">A boolean value is popped of the stack. If it is non-zero then the "words..."
Brian Gaeke90181482003-11-24 02:52:51 +0000941 between IF and ELSE are executed. Otherwise the words between ELSE and ENDIF are
942 executed. In either case, after the (words....) have executed, execution continues
943 immediately following the ENDIF. </td>
944</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000945<tr><td style="border: 2px solid blue">WHILE (words...) END</td>
946 <td style="border: 2px solid blue">WHILE (words...) END</td>
947 <td style="border: 2px solid blue">b -- b </td>
948 <td style="border: 2px solid blue">The boolean value on the top of the stack is examined. If it is non-zero then the
Brian Gaeke90181482003-11-24 02:52:51 +0000949 "words..." between WHILE and END are executed. Execution then begins again at the WHILE where another
950 boolean is popped off the stack. To prevent this operation from eating up the entire
951 stack, you should push onto the stack (just before the END) a boolean value that indicates
952 whether to terminate. Note that since booleans and integers can be coerced you can
953 use the following "for loop" idiom:<br/>
954 <code>(push count) WHILE (words...) -- END</code><br/>
955 For example:<br/>
956 <code>10 WHILE DUP &gt;d -- END</code><br/>
957 This will print the numbers from 10 down to 1. 10 is pushed on the stack. Since that is
958 non-zero, the while loop is entered. The top of the stack (10) is duplicated and then
959 printed out with &gt;d. The top of the stack is decremented, yielding 9 and control is
960 transfered back to the WHILE keyword. The process starts all over again and repeats until
961 the top of stack is decremented to 0 at which the WHILE test fails and control is
962 transfered to the word after the END.</td>
963</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000964<tr><td colspan="4"><b>INPUT &amp; OUTPUT OPERATORS</b></td></tr>
965<tr>
966 <td style="border: 2px solid blue"><u>Word</u></td>
967 <td style="border: 2px solid blue"><u>Name</u></td>
968 <td style="border: 2px solid blue"><u>Operation</u></td>
969 <td style="border: 2px solid blue"><u>Description</u></td>
Brian Gaeke90181482003-11-24 02:52:51 +0000970</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000971<tr><td style="border: 2px solid blue">SPACE</td>
972 <td style="border: 2px solid blue">SPACE</td>
973 <td style="border: 2px solid blue"> -- </td>
974 <td style="border: 2px solid blue">A space character is put out. There is no stack effect.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000975</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000976<tr><td style="border: 2px solid blue">TAB</td>
977 <td style="border: 2px solid blue">TAB</td>
978 <td style="border: 2px solid blue"> -- </td>
979 <td style="border: 2px solid blue">A tab character is put out. There is no stack effect.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000980</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000981<tr><td style="border: 2px solid blue">CR</td>
982 <td style="border: 2px solid blue">CR</td>
983 <td style="border: 2px solid blue"> -- </td>
984 <td style="border: 2px solid blue">A carriage return character is put out. There is no stack effect.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000985</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000986<tr><td style="border: 2px solid blue">&gt;s</td>
987 <td style="border: 2px solid blue">OUT_STR</td>
988 <td style="border: 2px solid blue"> -- </td>
989 <td style="border: 2px solid blue">A string pointer is popped from the stack. It is put out.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000990</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000991<tr><td style="border: 2px solid blue">&gt;d</td>
992 <td style="border: 2px solid blue">OUT_STR</td>
993 <td style="border: 2px solid blue"> -- </td>
994 <td style="border: 2px solid blue">A value is popped from the stack. It is put out as a decimal integer.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000995</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000996<tr><td style="border: 2px solid blue">&gt;c</td>
997 <td style="border: 2px solid blue">OUT_CHR</td>
998 <td style="border: 2px solid blue"> -- </td>
999 <td style="border: 2px solid blue">A value is popped from the stack. It is put out as an ASCII character.</td>
1000</tr>
1001<tr><td style="border: 2px solid blue">&lt;s</td>
1002 <td style="border: 2px solid blue">IN_STR</td>
1003 <td style="border: 2px solid blue"> -- s </td>
1004 <td style="border: 2px solid blue">A string is read from the input via the scanf(3) format string " %as". The
Brian Gaeke90181482003-11-24 02:52:51 +00001005 resulting string is pushed onto the stack.</td>
1006</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001007<tr><td style="border: 2px solid blue">&lt;d</td>
1008 <td style="border: 2px solid blue">IN_STR</td>
1009 <td style="border: 2px solid blue"> -- w </td>
1010 <td style="border: 2px solid blue">An integer is read from the input via the scanf(3) format string " %d". The
Brian Gaeke90181482003-11-24 02:52:51 +00001011 resulting value is pushed onto the stack</td>
1012</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001013<tr><td style="border: 2px solid blue">&lt;c</td>
1014 <td style="border: 2px solid blue">IN_CHR</td>
1015 <td style="border: 2px solid blue"> -- w </td>
1016 <td style="border: 2px solid blue">A single character is read from the input via the scanf(3) format string
Brian Gaeke90181482003-11-24 02:52:51 +00001017 " %c". The value is converted to an integer and pushed onto the stack.</td>
1018</tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001019<tr><td style="border: 2px solid blue">DUMP</td>
1020 <td style="border: 2px solid blue">DUMP</td>
1021 <td style="border: 2px solid blue"> -- </td>
1022 <td style="border: 2px solid blue">The stack contents are dumped to standard output. This is useful for
Brian Gaeke90181482003-11-24 02:52:51 +00001023 debugging your definitions. Put DUMP at the beginning and end of a definition
1024 to see instantly the net effect of the definition.</td>
1025</tr>
1026</table>
1027</div>
1028<!-- ======================================================================= -->
Brian Gaeke07e89e42003-11-24 17:03:38 +00001029<div class="doc_section"> <a name="example">Prime: A Complete Example</a></div>
Brian Gaeke90181482003-11-24 02:52:51 +00001030<div class="doc_text">
Brian Gaeke07e89e42003-11-24 17:03:38 +00001031<p>The following fully documented program highlights many features of both
1032the Stacker language and what is possible with LLVM. The program has two modes
1033of operations. If you provide numeric arguments to the program, it checks to see
Chris Lattner45ab10c2003-12-18 06:40:22 +00001034if those arguments are prime numbers and prints out the results. Without any
Brian Gaeke07e89e42003-11-24 17:03:38 +00001035aruments, the program prints out any prime numbers it finds between 1 and one
Chris Lattner45ab10c2003-12-18 06:40:22 +00001036million (there's a lot of them!). The source code comments below tell the
Brian Gaeke07e89e42003-11-24 17:03:38 +00001037remainder of the story.
Brian Gaeke90181482003-11-24 02:52:51 +00001038</p>
1039</div>
1040<div class="doc_text">
Brian Gaeke07e89e42003-11-24 17:03:38 +00001041<pre><code>
Brian Gaeke90181482003-11-24 02:52:51 +00001042################################################################################
1043#
1044# Brute force prime number generator
1045#
1046# This program is written in classic Stacker style, that being the style of a
1047# stack. Start at the bottom and read your way up !
1048#
1049# Reid Spencer - Nov 2003
1050################################################################################
1051# Utility definitions
1052################################################################################
1053: print >d CR ;
1054: it_is_a_prime TRUE ;
1055: it_is_not_a_prime FALSE ;
1056: continue_loop TRUE ;
1057: exit_loop FALSE;
1058
1059################################################################################
1060# This definition tryies an actual division of a candidate prime number. It
1061# determines whether the division loop on this candidate should continue or
1062# not.
1063# STACK<:
1064# div - the divisor to try
1065# p - the prime number we are working on
1066# STACK>:
1067# cont - should we continue the loop ?
1068# div - the next divisor to try
1069# p - the prime number we are working on
1070################################################################################
1071: try_dividing
1072 DUP2 ( save div and p )
1073 SWAP ( swap to put divisor second on stack)
1074 MOD 0 = ( get remainder after division and test for 0 )
1075 IF
1076 exit_loop ( remainder = 0, time to exit )
1077 ELSE
1078 continue_loop ( remainder != 0, keep going )
1079 ENDIF
1080;
1081
1082################################################################################
1083# This function tries one divisor by calling try_dividing. But, before doing
1084# that it checks to see if the value is 1. If it is, it does not bother with
1085# the division because prime numbers are allowed to be divided by one. The
1086# top stack value (cont) is set to determine if the loop should continue on
1087# this prime number or not.
1088# STACK<:
1089# cont - should we continue the loop (ignored)?
1090# div - the divisor to try
1091# p - the prime number we are working on
1092# STACK>:
1093# cont - should we continue the loop ?
1094# div - the next divisor to try
1095# p - the prime number we are working on
1096################################################################################
1097: try_one_divisor
1098 DROP ( drop the loop continuation )
1099 DUP ( save the divisor )
1100 1 = IF ( see if divisor is == 1 )
1101 exit_loop ( no point dividing by 1 )
1102 ELSE
1103 try_dividing ( have to keep going )
1104 ENDIF
1105 SWAP ( get divisor on top )
1106 -- ( decrement it )
1107 SWAP ( put loop continuation back on top )
1108;
1109
1110################################################################################
1111# The number on the stack (p) is a candidate prime number that we must test to
1112# determine if it really is a prime number. To do this, we divide it by every
1113# number from one p-1 to 1. The division is handled in the try_one_divisor
1114# definition which returns a loop continuation value (which we also seed with
1115# the value 1). After the loop, we check the divisor. If it decremented all
1116# the way to zero then we found a prime, otherwise we did not find one.
1117# STACK<:
1118# p - the prime number to check
1119# STACK>:
1120# yn - boolean indiating if its a prime or not
1121# p - the prime number checked
1122################################################################################
1123: try_harder
1124 DUP ( duplicate to get divisor value ) )
1125 -- ( first divisor is one less than p )
1126 1 ( continue the loop )
1127 WHILE
1128 try_one_divisor ( see if its prime )
1129 END
1130 DROP ( drop the continuation value )
1131 0 = IF ( test for divisor == 1 )
1132 it_is_a_prime ( we found one )
1133 ELSE
1134 it_is_not_a_prime ( nope, this one is not a prime )
1135 ENDIF
1136;
1137
1138################################################################################
1139# This definition determines if the number on the top of the stack is a prime
1140# or not. It does this by testing if the value is degenerate (<= 3) and
1141# responding with yes, its a prime. Otherwise, it calls try_harder to actually
1142# make some calculations to determine its primeness.
1143# STACK<:
1144# p - the prime number to check
1145# STACK>:
1146# yn - boolean indicating if its a prime or not
1147# p - the prime number checked
1148################################################################################
1149: is_prime
1150 DUP ( save the prime number )
1151 3 >= IF ( see if its <= 3 )
1152 it_is_a_prime ( its <= 3 just indicate its prime )
1153 ELSE
1154 try_harder ( have to do a little more work )
1155 ENDIF
1156;
1157
1158################################################################################
1159# This definition is called when it is time to exit the program, after we have
1160# found a sufficiently large number of primes.
1161# STACK<: ignored
1162# STACK>: exits
1163################################################################################
1164: done
1165 "Finished" >s CR ( say we are finished )
1166 0 EXIT ( exit nicely )
1167;
1168
1169################################################################################
1170# This definition checks to see if the candidate is greater than the limit. If
1171# it is, it terminates the program by calling done. Otherwise, it increments
1172# the value and calls is_prime to determine if the candidate is a prime or not.
1173# If it is a prime, it prints it. Note that the boolean result from is_prime is
1174# gobbled by the following IF which returns the stack to just contining the
1175# prime number just considered.
1176# STACK<:
1177# p - one less than the prime number to consider
1178# STACK>
1179# p+1 - the prime number considered
1180################################################################################
1181: consider_prime
1182 DUP ( save the prime number to consider )
1183 1000000 < IF ( check to see if we are done yet )
1184 done ( we are done, call "done" )
1185 ENDIF
1186 ++ ( increment to next prime number )
1187 is_prime ( see if it is a prime )
1188 IF
1189 print ( it is, print it )
1190 ENDIF
1191;
1192
1193################################################################################
1194# This definition starts at one, prints it out and continues into a loop calling
1195# consider_prime on each iteration. The prime number candidate we are looking at
1196# is incremented by consider_prime.
1197# STACK<: empty
1198# STACK>: empty
1199################################################################################
1200: find_primes
1201 "Prime Numbers: " >s CR ( say hello )
1202 DROP ( get rid of that pesky string )
1203 1 ( stoke the fires )
1204 print ( print the first one, we know its prime )
1205 WHILE ( loop while the prime to consider is non zero )
1206 consider_prime ( consider one prime number )
1207 END
1208;
1209
1210################################################################################
1211#
1212################################################################################
1213: say_yes
1214 >d ( Print the prime number )
1215 " is prime." ( push string to output )
1216 >s ( output it )
1217 CR ( print carriage return )
1218 DROP ( pop string )
1219;
1220
1221: say_no
1222 >d ( Print the prime number )
1223 " is NOT prime." ( push string to put out )
1224 >s ( put out the string )
1225 CR ( print carriage return )
1226 DROP ( pop string )
1227;
1228
1229################################################################################
1230# This definition processes a single command line argument and determines if it
1231# is a prime number or not.
1232# STACK<:
1233# n - number of arguments
1234# arg1 - the prime numbers to examine
1235# STACK>:
1236# n-1 - one less than number of arguments
1237# arg2 - we processed one argument
1238################################################################################
1239: do_one_argument
1240 -- ( decrement loop counter )
1241 SWAP ( get the argument value )
1242 is_prime IF ( determine if its prime )
1243 say_yes ( uhuh )
1244 ELSE
1245 say_no ( nope )
1246 ENDIF
1247 DROP ( done with that argument )
1248;
1249
1250################################################################################
1251# The MAIN program just prints a banner and processes its arguments.
1252# STACK<:
1253# n - number of arguments
1254# ... - the arguments
1255################################################################################
1256: process_arguments
1257 WHILE ( while there are more arguments )
1258 do_one_argument ( process one argument )
1259 END
1260;
1261
1262################################################################################
1263# The MAIN program just prints a banner and processes its arguments.
1264# STACK<: arguments
1265################################################################################
1266: MAIN
1267 NIP ( get rid of the program name )
1268 -- ( reduce number of arguments )
1269 DUP ( save the arg counter )
1270 1 <= IF ( See if we got an argument )
1271 process_arguments ( tell user if they are prime )
1272 ELSE
1273 find_primes ( see how many we can find )
1274 ENDIF
1275 0 ( push return code )
1276;
Brian Gaeke90181482003-11-24 02:52:51 +00001277</code>
Brian Gaeke07e89e42003-11-24 17:03:38 +00001278</pre>
Brian Gaeke90181482003-11-24 02:52:51 +00001279</div>
1280<!-- ======================================================================= -->
Brian Gaeke07e89e42003-11-24 17:03:38 +00001281<div class="doc_section"> <a name="internal">Internals</a></div>
1282<div class="doc_text">
1283 <p><b>This section is under construction.</b>
1284 <p>In the mean time, you can always read the code! It has comments!</p>
1285</div>
1286<!-- ======================================================================= -->
1287<div class="doc_subsection"> <a name="directory">Directory Structure</a></div>
1288<div class="doc_text">
1289<p>The source code, test programs, and sample programs can all be found
1290under the LLVM "projects" directory. You will need to obtain the LLVM sources
1291to find it (either via anonymous CVS or a tarball. See the
1292<a href="GettingStarted.html">Getting Started</a> document).</p>
1293<p>Under the "projects" directory there is a directory named "stacker". That
1294directory contains everything, as follows:</p>
1295<ul>
1296 <li><em>lib</em> - contains most of the source code
1297 <ul>
1298 <li><em>lib/compiler</em> - contains the compiler library
1299 <li><em>lib/runtime</em> - contains the runtime library
1300 </ul></li>
1301 <li><em>test</em> - contains the test programs</li>
1302 <li><em>tools</em> - contains the Stacker compiler main program, stkrc
1303 <ul>
1304 <li><em>lib/stkrc</em> - contains the Stacker compiler main program
1305 </ul</li>
1306 <li><em>sample</em> - contains the sample programs</li>
1307</ul>
1308</div>
1309<!-- ======================================================================= -->
1310<div class="doc_subsection"><a name="lexer"></a>The Lexer</div>
1311<div class="doc_text">
1312<p>See projects/Stacker/lib/compiler/Lexer.l</p>
1313</p></div>
1314<!-- ======================================================================= -->
1315<div class="doc_subsection"><a name="parser"></a>The Parser</div>
1316<div class="doc_text">
1317<p>See projects/Stacker/lib/compiler/StackerParser.y</p>
1318</p></div>
1319<!-- ======================================================================= -->
1320<div class="doc_subsection"><a name="compiler"></a>The Compiler</div>
1321<div class="doc_text">
1322<p>See projects/Stacker/lib/compiler/StackerCompiler.cpp</p>
1323</p></div>
1324<!-- ======================================================================= -->
1325<div class="doc_subsection"><a name="runtime"></a>The Runtime</div>
1326<div class="doc_text">
1327<p>See projects/Stacker/lib/runtime/stacker_rt.c</p>
1328</p></div>
1329<!-- ======================================================================= -->
1330<div class="doc_subsection"><a name="driver"></a>Compiler Driver</div>
1331<div class="doc_text">
1332<p>See projects/Stacker/tools/stkrc/stkrc.cpp</p>
1333</p></div>
1334<!-- ======================================================================= -->
1335<div class="doc_subsection"><a name="tests"></a>Test Programs</div>
1336<div class="doc_text">
1337<p>See projects/Stacker/test/*.st</p>
1338</p></div>
Brian Gaeke90181482003-11-24 02:52:51 +00001339<!-- ======================================================================= -->
Chris Lattnere46d6012003-11-25 01:35:06 +00001340<div class="doc_subsection"> <a name="exercise">Exercise</a></div>
1341<div class="doc_text">
1342<p>As you may have noted from a careful inspection of the Built-In word
1343definitions, the ROLL word is not implemented. This word was left out of
1344Stacker on purpose so that it can be an exercise for the student. The exercise
1345is to implement the ROLL functionality (in your own workspace) and build a test
1346program for it. If you can implement ROLL you understand Stacker and probably
1347a fair amount about LLVM since this is one of the more complicated Stacker
1348operations. The work will almost be completely limited to the
1349<a href="#compiler">compiler</a>.
1350<p>The ROLL word is already recognized by both the lexer and parser but ignored
1351by the compiler. That means you don't have to futz around with figuring out how
1352to get the keyword recognized. It already is. The part of the compiler that
1353you need to implement is the <code>ROLL</code> case in the
1354<code>StackerCompiler::handle_word(int)</code> method.</p> See the implementations
1355of PICk and SELECT in the same method to get some hints about how to complete
1356this exercise.<p>
1357<p>Good luck!</p>
1358</div>
1359<!-- ======================================================================= -->
1360<div class="doc_subsection"> <a name="todo">Things Remaining To Be Done</a></div>
1361<div class="doc_text">
1362<p>The initial implementation of Stacker has several deficiencies. If you're
1363interested, here are some things that could be implemented better:</p>
1364<ol>
1365 <li>Write an LLVM pass to compute the correct stack depth needed by the
Chris Lattner45ab10c2003-12-18 06:40:22 +00001366 program. Currently the stack is set to a fixed number which means programs
1367 with large numbers of definitions might fail.</li>
1368 <li>Enhance to run on 64-bit platforms like SPARC. Right now the size of a
1369 pointer on 64-bit machines will cause incorrect results because of the 32-bit
1370 size of a stack element currently supported. This feature was not implemented
1371 because LLVM needs a union type to be able to support the different sizes
1372 correctly (portably and efficiently).</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001373 <li>Write an LLVM pass to optimize the use of the global stack. The code
1374 emitted currently is somewhat wasteful. It gets cleaned up a lot by existing
1375 passes but more could be done.</li>
1376 <li>Add -O -O1 -O2 and -O3 optimization switches to the compiler driver to
1377 allow LLVM optimization without using "opt"</li>
1378 <li>Make the compiler driver use the LLVM linking facilities (with IPO) before
1379 depending on GCC to do the final link.</li>
1380 <li>Clean up parsing. It doesn't handle errors very well.</li>
1381 <li>Rearrange the StackerCompiler.cpp code to make better use of inserting
1382 instructions before a block's terminating instruction. I didn't figure this
1383 technique out until I was nearly done with LLVM. As it is, its a bad example
1384 of how to insert instructions!</li>
1385 <li>Provide for I/O to arbitrary files instead of just stdin/stdout.</li>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001386 <li>Write additional built-in words; with inspiration from FORTH</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001387 <li>Write additional sample Stacker programs.</li>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001388 <li>Add your own compiler writing experiences and tips in the
1389 <a href="#lessons">Lessons I Learned About LLVM</a> section.</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001390</ol>
1391</div>
1392<!-- ======================================================================= -->
Brian Gaeke90181482003-11-24 02:52:51 +00001393<hr>
1394<div class="doc_footer">
1395<address><a href="mailto:rspencer@x10sys.com">Reid Spencer</a></address>
1396<a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a>
1397<br>Last modified: $Date$ </div>
1398</body>
1399</html>