blob: 81b623efa9a15eca20c51a6d4b78b7f755147b7c [file] [log] [blame]
Misha Brukman36692992004-05-12 19:52:00 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
Brian Gaeke90181482003-11-24 02:52:51 +00003<html>
4<head>
Misha Brukman36692992004-05-12 19:52:00 +00005 <title>Stacker: An Example Of Using LLVM</title>
Brian Gaeke90181482003-11-24 02:52:51 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8<body>
Misha Brukman36692992004-05-12 19:52:00 +00009
Brian Gaeke90181482003-11-24 02:52:51 +000010<div class="doc_title">Stacker: An Example Of Using LLVM</div>
Misha Brukman36692992004-05-12 19:52:00 +000011
Brian Gaeke90181482003-11-24 02:52:51 +000012<ol>
13 <li><a href="#abstract">Abstract</a></li>
14 <li><a href="#introduction">Introduction</a></li>
Brian Gaeke07e89e42003-11-24 17:03:38 +000015 <li><a href="#lessons">Lessons I Learned About LLVM</a>
16 <ol>
17 <li><a href="#value">Everything's a Value!</a></li>
18 <li><a href="#terminate">Terminate Those Blocks!</a></li>
19 <li><a href="#blocks">Concrete Blocks</a></li>
20 <li><a href="#push_back">push_back Is Your Friend</a></li>
21 <li><a href="#gep">The Wily GetElementPtrInst</a></li>
22 <li><a href="#linkage">Getting Linkage Types Right</a></li>
23 <li><a href="#constants">Constants Are Easier Than That!</a></li>
Misha Brukman36692992004-05-12 19:52:00 +000024 </ol></li>
Brian Gaeke90181482003-11-24 02:52:51 +000025 <li><a href="#lexicon">The Stacker Lexicon</a>
26 <ol>
Misha Brukman36692992004-05-12 19:52:00 +000027 <li><a href="#stack">The Stack</a></li>
28 <li><a href="#punctuation">Punctuation</a></li>
29 <li><a href="#comments">Comments</a></li>
30 <li><a href="#literals">Literals</a></li>
31 <li><a href="#words">Words</a></li>
Misha Brukman4fcfaa52004-06-03 23:47:34 +000032 <li><a href="#style">Standard Style</a></li>
Misha Brukman36692992004-05-12 19:52:00 +000033 <li><a href="#builtins">Built-Ins</a></li>
34 </ol></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>
Misha Brukman36692992004-05-12 19:52:00 +000047 </ol></li>
Brian Gaeke90181482003-11-24 02:52:51 +000048</ol>
Misha Brukman36692992004-05-12 19:52:00 +000049
Chris Lattner7911ce22004-05-23 21:07:27 +000050<div class="doc_author">
51 <p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a></p>
Brian Gaeke90181482003-11-24 02:52:51 +000052</div>
Misha Brukman36692992004-05-12 19:52:00 +000053
Brian Gaeke90181482003-11-24 02:52:51 +000054<!-- ======================================================================= -->
Misha Brukman36692992004-05-12 19:52:00 +000055<div class="doc_section"><a name="abstract">Abstract</a></div>
Brian Gaeke90181482003-11-24 02:52:51 +000056<div class="doc_text">
57<p>This document is another way to learn about LLVM. Unlike the
58<a href="LangRef.html">LLVM Reference Manual</a> or
Chris Lattner45ab10c2003-12-18 06:40:22 +000059<a href="ProgrammersManual.html">LLVM Programmer's Manual</a>, here we learn
Chris Lattnere46d6012003-11-25 01:35:06 +000060about LLVM through the experience of creating a simple programming language
61named Stacker. Stacker was invented specifically as a demonstration of
Brian Gaeke90181482003-11-24 02:52:51 +000062LLVM. The emphasis in this document is not on describing the
John Criswelld000e1d2003-12-18 16:43:17 +000063intricacies of LLVM itself but on how to use it to build your own
Brian Gaeke90181482003-11-24 02:52:51 +000064compiler system.</p>
65</div>
66<!-- ======================================================================= -->
67<div class="doc_section"> <a name="introduction">Introduction</a> </div>
68<div class="doc_text">
69<p>Amongst other things, LLVM is a platform for compiler writers.
70Because of its exceptionally clean and small IR (intermediate
71representation), compiler writing with LLVM is much easier than with
Chris Lattner45ab10c2003-12-18 06:40:22 +000072other system. As proof, I wrote the entire compiler (language definition,
73lexer, parser, code generator, etc.) in about <em>four days</em>!
74That's important to know because it shows how quickly you can get a new
75language running when using LLVM. Furthermore, this was the <em >first</em>
Brian Gaeke90181482003-11-24 02:52:51 +000076language the author ever created using LLVM. The learning curve is
77included in that four days.</p>
78<p>The language described here, Stacker, is Forth-like. Programs
John Criswelld000e1d2003-12-18 16:43:17 +000079are simple collections of word definitions, and the only thing definitions
Brian Gaeke90181482003-11-24 02:52:51 +000080can do is manipulate a stack or generate I/O. Stacker is not a "real"
John Criswelld000e1d2003-12-18 16:43:17 +000081programming language; it's very simple. Although it is computationally
Brian Gaeke90181482003-11-24 02:52:51 +000082complete, you wouldn't use it for your next big project. However,
John Criswelld000e1d2003-12-18 16:43:17 +000083the fact that it is complete, it's simple, and it <em>doesn't</em> have
Brian Gaeke90181482003-11-24 02:52:51 +000084a C-like syntax make it useful for demonstration purposes. It shows
Chris Lattnere46d6012003-11-25 01:35:06 +000085that LLVM could be applied to a wide variety of languages.</p>
Brian Gaeke90181482003-11-24 02:52:51 +000086<p>The basic notions behind stacker is very simple. There's a stack of
87integers (or character pointers) that the program manipulates. Pretty
88much the only thing the program can do is manipulate the stack and do
89some limited I/O operations. The language provides you with several
90built-in words that manipulate the stack in interesting ways. To get
91your feet wet, here's how you write the traditional "Hello, World"
92program in Stacker:</p>
93<p><code>: hello_world "Hello, World!" &gt;s DROP CR ;<br>
94: MAIN hello_world ;<br></code></p>
95<p>This has two "definitions" (Stacker manipulates words, not
96functions and words have definitions): <code>MAIN</code> and <code>
John Criswelld000e1d2003-12-18 16:43:17 +000097hello_world</code>. The <code>MAIN</code> definition is standard; it
Brian Gaeke90181482003-11-24 02:52:51 +000098tells Stacker where to start. Here, <code>MAIN</code> is defined to
99simply invoke the word <code>hello_world</code>. The
100<code>hello_world</code> definition tells stacker to push the
John Criswelld000e1d2003-12-18 16:43:17 +0000101<code>"Hello, World!"</code> string on to the stack, print it out
Brian Gaeke90181482003-11-24 02:52:51 +0000102(<code>&gt;s</code>), pop it off the stack (<code>DROP</code>), and
103finally print a carriage return (<code>CR</code>). Although
104<code>hello_world</code> uses the stack, its net effect is null. Well
105written Stacker definitions have that characteristic. </p>
106<p>Exercise for the reader: how could you make this a one line program?</p>
107</div>
108<!-- ======================================================================= -->
Brian Gaeke07e89e42003-11-24 17:03:38 +0000109<div class="doc_section"><a name="lessons"></a>Lessons I Learned About LLVM</div>
Brian Gaeke90181482003-11-24 02:52:51 +0000110<div class="doc_text">
Chris Lattnere46d6012003-11-25 01:35:06 +0000111<p>Stacker was written for two purposes: </p>
112<ol>
113 <li>to get the author over the learning curve, and</li>
114 <li>to provide a simple example of how to write a compiler using LLVM.</li>
115</ol>
116<p>During the development of Stacker, many lessons about LLVM were
Brian Gaeke90181482003-11-24 02:52:51 +0000117learned. Those lessons are described in the following subsections.<p>
118</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000119<!-- ======================================================================= -->
120<div class="doc_subsection"><a name="value"></a>Everything's a Value!</div>
121<div class="doc_text">
Chris Lattnere46d6012003-11-25 01:35:06 +0000122<p>Although I knew that LLVM uses a Single Static Assignment (SSA) format,
Brian Gaeke07e89e42003-11-24 17:03:38 +0000123it wasn't obvious to me how prevalent this idea was in LLVM until I really
Chris Lattnere46d6012003-11-25 01:35:06 +0000124started using it. Reading the <a href="ProgrammersManual.html">
John Criswelld000e1d2003-12-18 16:43:17 +0000125Programmer's Manual</a> and <a href="LangRef.html">Language Reference</a>,
Chris Lattnere46d6012003-11-25 01:35:06 +0000126I noted that most of the important LLVM IR (Intermediate Representation) C++
Brian Gaeke07e89e42003-11-24 17:03:38 +0000127classes were derived from the Value class. The full power of that simple
128design only became fully understood once I started constructing executable
129expressions for Stacker.</p>
Chris Lattner532c92d2004-08-03 00:17:21 +0000130
Brian Gaeke07e89e42003-11-24 17:03:38 +0000131<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>.
Chris Lattner532c92d2004-08-03 00:17:21 +0000135You could write a function using LLVM that computes this expression like
136this: </p>
137
138<div class="doc_code"><pre>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000139Value*
Chris Lattner45ab10c2003-12-18 06:40:22 +0000140expression(BasicBlock* bb, Value* a, Value* b, Value* x, Value* y )
Brian Gaeke07e89e42003-11-24 17:03:38 +0000141{
Reid Spencerb83eb642006-10-20 07:07:24 +0000142 ConstantInt* one = ConstantInt::get(Type::IntTy, 1);
Chris Lattner532c92d2004-08-03 00:17:21 +0000143 BinaryOperator* or1 = BinaryOperator::createOr(a, b, "", bb);
144 BinaryOperator* add1 = BinaryOperator::createAdd(x, one, "", bb);
145 BinaryOperator* add2 = BinaryOperator::createAdd(y, one, "", bb);
146 BinaryOperator* div1 = BinaryOperator::createDiv(add1, add2, "", bb);
147 BinaryOperator* mult1 = BinaryOperator::createMul(or1, div1, "", bb);
Brian Gaeke07e89e42003-11-24 17:03:38 +0000148 return mult1;
149}
Chris Lattner532c92d2004-08-03 00:17:21 +0000150</pre></div>
151
Chris Lattner45ab10c2003-12-18 06:40:22 +0000152<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 +0000153have to tell this function which kinds of Values are being passed in. They could be
Chris Lattner45ab10c2003-12-18 06:40:22 +0000154<code>Instruction</code>s, <code>Constant</code>s, <code>GlobalVariable</code>s, or
155any of the other subclasses of <code>Value</code> that LLVM supports.
156Furthermore, if you specify Values that are incorrect for this sequence of
Chris Lattnere46d6012003-11-25 01:35:06 +0000157operations, LLVM will either notice right away (at compilation time) or the LLVM
Chris Lattner45ab10c2003-12-18 06:40:22 +0000158Verifier will pick up the inconsistency when the compiler runs. In either case
159LLVM prevents you from making a type error that gets passed through to the
160generated program. This <em>really</em> helps you write a compiler that
161always generates correct code!<p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000162<p>The second point is that we don't have to worry about branching, registers,
163stack variables, saving partial results, etc. The instructions we create
164<em>are</em> the values we use. Note that all that was created in the above
165code is a Constant value and five operators. Each of the instructions <em>is</em>
Chris Lattnere46d6012003-11-25 01:35:06 +0000166the resulting value of that instruction. This saves a lot of time.</p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000167<p>The lesson is this: <em>SSA form is very powerful: there is no difference
Chris Lattnere46d6012003-11-25 01:35:06 +0000168between a value and the instruction that created it.</em> This is fully
Brian Gaeke07e89e42003-11-24 17:03:38 +0000169enforced by the LLVM IR. Use it to your best advantage.</p>
170</div>
171<!-- ======================================================================= -->
172<div class="doc_subsection"><a name="terminate"></a>Terminate Those Blocks!</div>
173<div class="doc_text">
174<p>I had to learn about terminating blocks the hard way: using the debugger
175to figure out what the LLVM verifier was trying to tell me and begging for
176help on the LLVMdev mailing list. I hope you avoid this experience.</p>
177<p>Emblazon this rule in your mind:</p>
178<ul>
179 <li><em>All</em> <code>BasicBlock</code>s in your compiler <b>must</b> be
180 terminated with a terminating instruction (branch, return, etc.).
181 </li>
182</ul>
183<p>Terminating instructions are a semantic requirement of the LLVM IR. There
184is no facility for implicitly chaining together blocks placed into a function
185in the order they occur. Indeed, in the general case, blocks will not be
186added to the function in the order of execution because of the recursive
187way compilers are written.</p>
188<p>Furthermore, if you don't terminate your blocks, your compiler code will
189compile just fine. You won't find out about the problem until you're running
190the compiler and the module you just created fails on the LLVM Verifier.</p>
191</div>
192<!-- ======================================================================= -->
193<div class="doc_subsection"><a name="blocks"></a>Concrete Blocks</div>
194<div class="doc_text">
195<p>After a little initial fumbling around, I quickly caught on to how blocks
Chris Lattnere46d6012003-11-25 01:35:06 +0000196should be constructed. In general, here's what I learned:
Brian Gaeke07e89e42003-11-24 17:03:38 +0000197<ol>
198 <li><em>Create your blocks early.</em> While writing your compiler, you
199 will encounter several situations where you know apriori that you will
John Criswelld000e1d2003-12-18 16:43:17 +0000200 need several blocks. For example, if-then-else, switch, while, and for
Nick Lewycky790fb922006-12-31 03:44:08 +0000201 statements in C/C++ all need multiple blocks for expression in LLVM.
Brian Gaeke07e89e42003-11-24 17:03:38 +0000202 The rule is, create them early.</li>
203 <li><em>Terminate your blocks early.</em> This just reduces the chances
204 that you forget to terminate your blocks which is required (go
205 <a href="#terminate">here</a> for more).
206 <li><em>Use getTerminator() for instruction insertion.</em> I noticed early on
207 that many of the constructors for the Instruction classes take an optional
208 <code>insert_before</code> argument. At first, I thought this was a mistake
209 because clearly the normal mode of inserting instructions would be one at
210 a time <em>after</em> some other instruction, not <em>before</em>. However,
211 if you hold on to your terminating instruction (or use the handy dandy
212 <code>getTerminator()</code> method on a <code>BasicBlock</code>), it can
213 always be used as the <code>insert_before</code> argument to your instruction
214 constructors. This causes the instruction to automatically be inserted in
Chris Lattnere46d6012003-11-25 01:35:06 +0000215 the RightPlace&trade; place, just before the terminating instruction. The
Brian Gaeke07e89e42003-11-24 17:03:38 +0000216 nice thing about this design is that you can pass blocks around and insert
Chris Lattnere46d6012003-11-25 01:35:06 +0000217 new instructions into them without ever knowing what instructions came
Brian Gaeke07e89e42003-11-24 17:03:38 +0000218 before. This makes for some very clean compiler design.</li>
219</ol>
220<p>The foregoing is such an important principal, its worth making an idiom:</p>
Misha Brukman36692992004-05-12 19:52:00 +0000221<pre>
Gabor Greifdf7d2b42008-04-19 22:25:09 +0000222BasicBlock* bb = BasicBlock::Create();
223bb->getInstList().push_back( BranchInst::Create( ... ) );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000224new Instruction(..., bb->getTerminator() );
Misha Brukman36692992004-05-12 19:52:00 +0000225</pre>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000226<p>To make this clear, consider the typical if-then-else statement
227(see StackerCompiler::handle_if() method). We can set this up
228in a single function using LLVM in the following way: </p>
229<pre>
230using namespace llvm;
231BasicBlock*
Reid Spencer6388e302006-12-19 19:47:54 +0000232MyCompiler::handle_if( BasicBlock* bb, ICmpInst* condition )
Brian Gaeke07e89e42003-11-24 17:03:38 +0000233{
234 // Create the blocks to contain code in the structure of if/then/else
Gabor Greifdf7d2b42008-04-19 22:25:09 +0000235 BasicBlock* then_bb = BasicBlock::Create();
236 BasicBlock* else_bb = BasicBlock::Create();
237 BasicBlock* exit_bb = BasicBlock::Create();
Brian Gaeke07e89e42003-11-24 17:03:38 +0000238
239 // Insert the branch instruction for the "if"
Gabor Greifdf7d2b42008-04-19 22:25:09 +0000240 bb->getInstList().push_back( BranchInst::Create( then_bb, else_bb, condition ) );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000241
242 // Set up the terminating instructions
Gabor Greifdf7d2b42008-04-19 22:25:09 +0000243 then->getInstList().push_back( BranchInst::Create( exit_bb ) );
244 else->getInstList().push_back( BranchInst::Create( exit_bb ) );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000245
246 // Fill in the then part .. details excised for brevity
Chris Lattner45ab10c2003-12-18 06:40:22 +0000247 this->fill_in( then_bb );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000248
249 // Fill in the else part .. details excised for brevity
Chris Lattner45ab10c2003-12-18 06:40:22 +0000250 this->fill_in( else_bb );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000251
252 // Return a block to the caller that can be filled in with the code
253 // that follows the if/then/else construct.
Chris Lattner45ab10c2003-12-18 06:40:22 +0000254 return exit_bb;
Brian Gaeke07e89e42003-11-24 17:03:38 +0000255}
256</pre>
257<p>Presumably in the foregoing, the calls to the "fill_in" method would add
258the instructions for the "then" and "else" parts. They would use the third part
259of the idiom almost exclusively (inserting new instructions before the
260terminator). Furthermore, they could even recurse back to <code>handle_if</code>
John Criswelld000e1d2003-12-18 16:43:17 +0000261should they encounter another if/then/else statement, and it will just work.</p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000262<p>Note how cleanly this all works out. In particular, the push_back methods on
263the <code>BasicBlock</code>'s instruction list. These are lists of type
Chris Lattner45ab10c2003-12-18 06:40:22 +0000264<code>Instruction</code> (which is also of type <code>Value</code>). To create
Brian Gaeke07e89e42003-11-24 17:03:38 +0000265the "if" branch we merely instantiate a <code>BranchInst</code> that takes as
Chris Lattner45ab10c2003-12-18 06:40:22 +0000266arguments the blocks to branch to and the condition to branch on. The
267<code>BasicBlock</code> objects act like branch labels! This new
268<code>BranchInst</code> terminates the <code>BasicBlock</code> provided
269as an argument. To give the caller a way to keep inserting after calling
John Criswelld000e1d2003-12-18 16:43:17 +0000270<code>handle_if</code>, we create an <code>exit_bb</code> block which is
271returned
Chris Lattner45ab10c2003-12-18 06:40:22 +0000272to the caller. Note that the <code>exit_bb</code> block is used as the
273terminator for both the <code>then_bb</code> and the <code>else_bb</code>
274blocks. This guarantees that no matter what else <code>handle_if</code>
275or <code>fill_in</code> does, they end up at the <code>exit_bb</code> block.
Brian Gaeke07e89e42003-11-24 17:03:38 +0000276</p>
277</div>
278<!-- ======================================================================= -->
279<div class="doc_subsection"><a name="push_back"></a>push_back Is Your Friend</div>
280<div class="doc_text">
281<p>
282One of the first things I noticed is the frequent use of the "push_back"
283method on the various lists. This is so common that it is worth mentioning.
284The "push_back" inserts a value into an STL list, vector, array, etc. at the
285end. The method might have also been named "insert_tail" or "append".
John Criswelld000e1d2003-12-18 16:43:17 +0000286Although I've used STL quite frequently, my use of push_back wasn't very
Brian Gaeke07e89e42003-11-24 17:03:38 +0000287high in other programs. In LLVM, you'll use it all the time.
288</p>
289</div>
290<!-- ======================================================================= -->
291<div class="doc_subsection"><a name="gep"></a>The Wily GetElementPtrInst</div>
292<div class="doc_text">
293<p>
294It took a little getting used to and several rounds of postings to the LLVM
John Criswelld000e1d2003-12-18 16:43:17 +0000295mailing list to wrap my head around this instruction correctly. Even though I had
Brian Gaeke07e89e42003-11-24 17:03:38 +0000296read the Language Reference and Programmer's Manual a couple times each, I still
297missed a few <em>very</em> key points:
298</p>
299<ul>
Misha Brukman36692992004-05-12 19:52:00 +0000300<li>GetElementPtrInst gives you back a Value for the last thing indexed.</li>
301<li>All global variables in LLVM are <em>pointers</em>.</li>
302<li>Pointers must also be dereferenced with the GetElementPtrInst
303instruction.</li>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000304</ul>
305<p>This means that when you look up an element in the global variable (assuming
John Criswelld000e1d2003-12-18 16:43:17 +0000306it's a struct or array), you <em>must</em> deference the pointer first! For many
Brian Gaeke07e89e42003-11-24 17:03:38 +0000307things, this leads to the idiom:
308</p>
Misha Brukman36692992004-05-12 19:52:00 +0000309<pre>
310std::vector&lt;Value*&gt; index_vector;
Reid Spencerb83eb642006-10-20 07:07:24 +0000311index_vector.push_back( ConstantInt::get( Type::LongTy, 0 );
Brian Gaeke07e89e42003-11-24 17:03:38 +0000312// ... push other indices ...
Gabor Greifdf7d2b42008-04-19 22:25:09 +0000313GetElementPtrInst* gep = GetElementPtrInst::Create( ptr, index_vector );
Misha Brukman36692992004-05-12 19:52:00 +0000314</pre>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000315<p>For example, suppose we have a global variable whose type is [24 x int]. The
316variable itself represents a <em>pointer</em> to that array. To subscript the
317array, we need two indices, not just one. The first index (0) dereferences the
318pointer. The second index subscripts the array. If you're a "C" programmer, this
319will run against your grain because you'll naturally think of the global array
320variable and the address of its first element as the same. That tripped me up
321for a while until I realized that they really do differ .. by <em>type</em>.
Chris Lattner45ab10c2003-12-18 06:40:22 +0000322Remember that LLVM is strongly typed. Everything has a type.
John Criswelld000e1d2003-12-18 16:43:17 +0000323The "type" of the global variable is [24 x int]*. That is, it's
Brian Gaeke07e89e42003-11-24 17:03:38 +0000324a pointer to an array of 24 ints. When you dereference that global variable with
Chris Lattnere46d6012003-11-25 01:35:06 +0000325a single (0) index, you now have a "[24 x int]" type. Although
Brian Gaeke07e89e42003-11-24 17:03:38 +0000326the pointer value of the dereferenced global and the address of the zero'th element
327in the array will be the same, they differ in their type. The zero'th element has
328type "int" while the pointer value has type "[24 x int]".</p>
John Criswelld000e1d2003-12-18 16:43:17 +0000329<p>Get this one aspect of LLVM right in your head, and you'll save yourself
Brian Gaeke07e89e42003-11-24 17:03:38 +0000330a lot of compiler writing headaches down the road.</p>
331</div>
332<!-- ======================================================================= -->
Brian Gaeke90181482003-11-24 02:52:51 +0000333<div class="doc_subsection"><a name="linkage"></a>Getting Linkage Types Right</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000334<div class="doc_text">
335<p>Linkage types in LLVM can be a little confusing, especially if your compiler
Chris Lattner45ab10c2003-12-18 06:40:22 +0000336writing mind has affixed firm concepts to particular words like "weak",
Brian Gaeke07e89e42003-11-24 17:03:38 +0000337"external", "global", "linkonce", etc. LLVM does <em>not</em> use the precise
John Criswelld000e1d2003-12-18 16:43:17 +0000338definitions of, say, ELF or GCC, even though they share common terms. To be fair,
Brian Gaeke07e89e42003-11-24 17:03:38 +0000339the concepts are related and similar but not precisely the same. This can lead
340you to think you know what a linkage type represents but in fact it is slightly
341different. I recommend you read the
342<a href="LangRef.html#linkage"> Language Reference on this topic</a> very
Chris Lattnere46d6012003-11-25 01:35:06 +0000343carefully. Then, read it again.<p>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000344<p>Here are some handy tips that I discovered along the way:</p>
345<ul>
Alkis Evlogimenos0744b5f2004-03-11 10:14:21 +0000346 <li><em>Uninitialized means external.</em> That is, the symbol is declared in the current
John Criswelld000e1d2003-12-18 16:43:17 +0000347 module and can be used by that module, but it is not defined by that module.</li>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000348 <li><em>Setting an initializer changes a global' linkage type.</em> Setting an
349 initializer changes a global's linkage type from whatever it was to a normal,
John Criswelld000e1d2003-12-18 16:43:17 +0000350 defined global (not external). You'll need to call the setLinkage() method to
Chris Lattner45ab10c2003-12-18 06:40:22 +0000351 reset it if you specify the initializer after the GlobalValue has been constructed.
352 This is important for LinkOnce and Weak linkage types.</li>
353 <li><em>Appending linkage can keep track of things.</em> Appending linkage can
354 be used to keep track of compilation information at runtime. It could be used,
355 for example, to build a full table of all the C++ virtual tables or hold the
356 C++ RTTI data, or whatever. Appending linkage can only be applied to arrays.
357 All arrays with the same name in each module are concatenated together at link
358 time.</li>
Brian Gaeke07e89e42003-11-24 17:03:38 +0000359</ul>
360</div>
361<!-- ======================================================================= -->
362<div class="doc_subsection"><a name="constants"></a>Constants Are Easier Than That!</div>
363<div class="doc_text">
364<p>
365Constants in LLVM took a little getting used to until I discovered a few utility
366functions in the LLVM IR that make things easier. Here's what I learned: </p>
367<ul>
368 <li>Constants are Values like anything else and can be operands of instructions</li>
John Criswelld000e1d2003-12-18 16:43:17 +0000369 <li>Integer constants, frequently needed, can be created using the static "get"
Reid Spencerb83eb642006-10-20 07:07:24 +0000370 methods of the ConstantInt class. The nice thing about these is that you can
371 "get" any kind of integer quickly.</li>
372 <li>There's a special method on Constant class which allows you to get the null
Brian Gaeke07e89e42003-11-24 17:03:38 +0000373 constant for <em>any</em> type. This is really handy for initializing large
374 arrays or structures, etc.</li>
375</ul>
376</div>
Brian Gaeke90181482003-11-24 02:52:51 +0000377<!-- ======================================================================= -->
378<div class="doc_section"> <a name="lexicon">The Stacker Lexicon</a></div>
Chris Lattnere46d6012003-11-25 01:35:06 +0000379<div class="doc_text"><p>This section describes the Stacker language</p></div>
Brian Gaeke90181482003-11-24 02:52:51 +0000380<div class="doc_subsection"><a name="stack"></a>The Stack</div>
381<div class="doc_text">
382<p>Stacker definitions define what they do to the global stack. Before
383proceeding, a few words about the stack are in order. The stack is simply
384a global array of 32-bit integers or pointers. A global index keeps track
Chris Lattnere46d6012003-11-25 01:35:06 +0000385of the location of the top of the stack. All of this is hidden from the
John Criswelld000e1d2003-12-18 16:43:17 +0000386programmer, but it needs to be noted because it is the foundation of the
Brian Gaeke90181482003-11-24 02:52:51 +0000387conceptual programming model for Stacker. When you write a definition,
388you are, essentially, saying how you want that definition to manipulate
389the global stack.</p>
390<p>Manipulating the stack can be quite hazardous. There is no distinction
391given and no checking for the various types of values that can be placed
392on the stack. Automatic coercion between types is performed. In many
John Criswelld000e1d2003-12-18 16:43:17 +0000393cases, this is useful. For example, a boolean value placed on the stack
Brian Gaeke90181482003-11-24 02:52:51 +0000394can be interpreted as an integer with good results. However, using a
395word that interprets that boolean value as a pointer to a string to
396print out will almost always yield a crash. Stacker simply leaves it
397to the programmer to get it right without any interference or hindering
Chris Lattnere46d6012003-11-25 01:35:06 +0000398on interpretation of the stack values. You've been warned. :) </p>
Brian Gaeke90181482003-11-24 02:52:51 +0000399</div>
400<!-- ======================================================================= -->
401<div class="doc_subsection"> <a name="punctuation"></a>Punctuation</div>
402<div class="doc_text">
403<p>Punctuation in Stacker is very simple. The colon and semi-colon
404characters are used to introduce and terminate a definition
405(respectively). Except for <em>FORWARD</em> declarations, definitions
406are all you can specify in Stacker. Definitions are read left to right.
Chris Lattnere46d6012003-11-25 01:35:06 +0000407Immediately after the colon comes the name of the word being defined.
408The remaining words in the definition specify what the word does. The definition
409is terminated by a semi-colon.</p>
410<p>So, your typical definition will have the form:</p>
411<pre><code>: name ... ;</code></pre>
412<p>The <code>name</code> is up to you but it must start with a letter and contain
John Criswelld000e1d2003-12-18 16:43:17 +0000413only letters, numbers, and underscore. Names are case sensitive and must not be
Chris Lattnere46d6012003-11-25 01:35:06 +0000414the same as the name of a built-in word. The <code>...</code> is replaced by
John Criswelld000e1d2003-12-18 16:43:17 +0000415the stack manipulating words that you wish to define <code>name</code> as. <p>
Chris Lattnere46d6012003-11-25 01:35:06 +0000416</div>
417<!-- ======================================================================= -->
418<div class="doc_subsection"><a name="comments"></a>Comments</div>
419<div class="doc_text">
420 <p>Stacker supports two types of comments. A hash mark (#) starts a comment
421 that extends to the end of the line. It is identical to the kind of comments
422 commonly used in shell scripts. A pair of parentheses also surround a comment.
423 In both cases, the content of the comment is ignored by the Stacker compiler. The
424 following does nothing in Stacker.
425 </p>
426<pre><code>
427# This is a comment to end of line
428( This is an enclosed comment )
429</code></pre>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000430<p>See the <a href="#example">example</a> program to see comments in use in
Chris Lattnere46d6012003-11-25 01:35:06 +0000431a real program.</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000432</div>
433<!-- ======================================================================= -->
434<div class="doc_subsection"><a name="literals"></a>Literals</div>
435<div class="doc_text">
John Criswelld000e1d2003-12-18 16:43:17 +0000436 <p>There are three kinds of literal values in Stacker: Integers, Strings,
Brian Gaeke90181482003-11-24 02:52:51 +0000437 and Booleans. In each case, the stack operation is to simply push the
John Criswelld000e1d2003-12-18 16:43:17 +0000438 value on to the stack. So, for example:<br/>
Brian Gaeke90181482003-11-24 02:52:51 +0000439 <code> 42 " is the answer." TRUE </code><br/>
John Criswelld000e1d2003-12-18 16:43:17 +0000440 will push three values on to the stack: the integer 42, the
441 string " is the answer.", and the boolean TRUE.</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000442</div>
443<!-- ======================================================================= -->
444<div class="doc_subsection"><a name="words"></a>Words</div>
445<div class="doc_text">
446<p>Each definition in Stacker is composed of a set of words. Words are
447read and executed in order from left to right. There is very little
448checking in Stacker to make sure you're doing the right thing with
449the stack. It is assumed that the programmer knows how the stack
450transformation he applies will affect the program.</p>
451<p>Words in a definition come in two flavors: built-in and programmer
452defined. Simply mentioning the name of a previously defined or declared
Chris Lattner45ab10c2003-12-18 06:40:22 +0000453programmer-defined word causes that word's stack actions to be invoked. It
Brian Gaeke90181482003-11-24 02:52:51 +0000454is somewhat like a function call in other languages. The built-in
Chris Lattner45ab10c2003-12-18 06:40:22 +0000455words have various effects, described <a href="#builtins">below</a>.</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000456<p>Sometimes you need to call a word before it is defined. For this, you can
Chris Lattnere46d6012003-11-25 01:35:06 +0000457use the <code>FORWARD</code> declaration. It looks like this:</p>
Brian Gaeke90181482003-11-24 02:52:51 +0000458<p><code>FORWARD name ;</code></p>
459<p>This simply states to Stacker that "name" is the name of a definition
460that is defined elsewhere. Generally it means the definition can be found
461"forward" in the file. But, it doesn't have to be in the current compilation
462unit. Anything declared with <code>FORWARD</code> is an external symbol for
463linking.</p>
464</div>
465<!-- ======================================================================= -->
Misha Brukman4fcfaa52004-06-03 23:47:34 +0000466<div class="doc_subsection"><a name="style"></a>Standard Style</div>
467<div class="doc_text">
468<p>TODO</p>
469</div>
470<!-- ======================================================================= -->
Brian Gaeke90181482003-11-24 02:52:51 +0000471<div class="doc_subsection"><a name="builtins"></a>Built In Words</div>
472<div class="doc_text">
473<p>The built-in words of the Stacker language are put in several groups
474depending on what they do. The groups are as follows:</p>
475<ol>
John Criswelld000e1d2003-12-18 16:43:17 +0000476 <li><em>Logical</em>: These words provide the logical operations for
Brian Gaeke90181482003-11-24 02:52:51 +0000477 comparing stack operands.<br/>The words are: &lt; &gt; &lt;= &gt;=
478 = &lt;&gt; true false.</li>
John Criswelld000e1d2003-12-18 16:43:17 +0000479 <li><em>Bitwise</em>: These words perform bitwise computations on
Brian Gaeke90181482003-11-24 02:52:51 +0000480 their operands. <br/> The words are: &lt;&lt; &gt;&gt; XOR AND NOT</li>
John Criswelld000e1d2003-12-18 16:43:17 +0000481 <li><em>Arithmetic</em>: These words perform arithmetic computations on
Brian Gaeke90181482003-11-24 02:52:51 +0000482 their operands. <br/> The words are: ABS NEG + - * / MOD */ ++ -- MIN MAX</li>
483 <li><em>Stack</em>These words manipulate the stack directly by moving
Chris Lattner45ab10c2003-12-18 06:40:22 +0000484 its elements around.<br/> The words are: DROP DROP2 NIP NIP2 DUP DUP2
485 SWAP SWAP2 OVER OVER2 ROT ROT2 RROT RROT2 TUCK TUCK2 PICK SELECT ROLL</li>
John Criswelld000e1d2003-12-18 16:43:17 +0000486 <li><em>Memory</em>These words allocate, free, and manipulate memory
Brian Gaeke90181482003-11-24 02:52:51 +0000487 areas outside the stack.<br/>The words are: MALLOC FREE GET PUT</li>
John Criswelld000e1d2003-12-18 16:43:17 +0000488 <li><em>Control</em>: These words alter the normal left to right flow
Brian Gaeke90181482003-11-24 02:52:51 +0000489 of execution.<br/>The words are: IF ELSE ENDIF WHILE END RETURN EXIT RECURSE</li>
John Criswelld000e1d2003-12-18 16:43:17 +0000490 <li><em>I/O</em>: These words perform output on the standard output
Brian Gaeke90181482003-11-24 02:52:51 +0000491 and input on the standard input. No other I/O is possible in Stacker.
492 <br/>The words are: SPACE TAB CR &gt;s &gt;d &gt;c &lt;s &lt;d &lt;c.</li>
493</ol>
494<p>While you may be familiar with many of these operations from other
495programming languages, a careful review of their semantics is important
496for correct programming in Stacker. Of most importance is the effect
497that each of these built-in words has on the global stack. The effect is
498not always intuitive. To better describe the effects, we'll borrow from Forth the idiom of
499describing the effect on the stack with:</p>
500<p><code> BEFORE -- AFTER </code></p>
501<p>That is, to the left of the -- is a representation of the stack before
502the operation. To the right of the -- is a representation of the stack
503after the operation. In the table below that describes the operation of
504each of the built in words, we will denote the elements of the stack
505using the following construction:</p>
506<ol>
507 <li><em>b</em> - a boolean truth value</li>
508 <li><em>w</em> - a normal integer valued word.</li>
509 <li><em>s</em> - a pointer to a string value</li>
Chris Lattnere46d6012003-11-25 01:35:06 +0000510 <li><em>p</em> - a pointer to a malloc'd memory block</li>
Brian Gaeke90181482003-11-24 02:52:51 +0000511</ol>
512</div>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000513<div class="doc_text" >
Reid Spencer1f923012004-11-01 20:47:22 +0000514 <table>
515<tr><th colspan="4">Definition Of Operation Of Built In Words</th></tr>
516<tr><th colspan="4"><b>LOGICAL OPERATIONS</b></th></tr>
517<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000518 <td>Word</td>
519 <td>Name</td>
520 <td>Operation</td>
521 <td>Description</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000522</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000523<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000524 <td>&lt;</td>
525 <td>LT</td>
526 <td>w1 w2 -- b</td>
527 <td>Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000528 compared. If w1 is less than w2, TRUE is pushed back on
529 the stack, otherwise FALSE is pushed back on the stack.</td>
530</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000531<tr><td>&gt;</td>
532 <td>GT</td>
533 <td>w1 w2 -- b</td>
534 <td>Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000535 compared. If w1 is greater than w2, TRUE is pushed back on
536 the stack, otherwise FALSE is pushed back on the stack.</td>
537</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000538<tr><td>&gt;=</td>
539 <td>GE</td>
540 <td>w1 w2 -- b</td>
541 <td>Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000542 compared. If w1 is greater than or equal to w2, TRUE is
543 pushed back on the stack, otherwise FALSE is pushed back
544 on the stack.</td>
545</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000546<tr><td>&lt;=</td>
547 <td>LE</td>
548 <td>w1 w2 -- b</td>
549 <td>Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000550 compared. If w1 is less than or equal to w2, TRUE is
551 pushed back on the stack, otherwise FALSE is pushed back
552 on the stack.</td>
553</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000554<tr><td>=</td>
555 <td>EQ</td>
556 <td>w1 w2 -- b</td>
557 <td>Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000558 compared. If w1 is equal to w2, TRUE is
559 pushed back on the stack, otherwise FALSE is pushed back
560 </td>
561</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000562<tr><td>&lt;&gt;</td>
563 <td>NE</td>
564 <td>w1 w2 -- b</td>
565 <td>Two values (w1 and w2) are popped off the stack and
Brian Gaeke90181482003-11-24 02:52:51 +0000566 compared. If w1 is equal to w2, TRUE is
567 pushed back on the stack, otherwise FALSE is pushed back
568 </td>
569</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000570<tr><td>FALSE</td>
571 <td>FALSE</td>
572 <td> -- b</td>
573 <td>The boolean value FALSE (0) is pushed on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000574</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000575<tr><td>TRUE</td>
576 <td>TRUE</td>
577 <td> -- b</td>
578 <td>The boolean value TRUE (-1) is pushed on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000579</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000580<tr><th colspan="4"><b>BITWISE OPERATORS</b></th></tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000581<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000582 <td>Word</td>
583 <td>Name</td>
584 <td>Operation</td>
585 <td>Description</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000586</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000587<tr><td>&lt;&lt;</td>
588 <td>SHL</td>
589 <td>w1 w2 -- w1&lt;&lt;w2</td>
590 <td>Two values (w1 and w2) are popped off the stack. The w2
Brian Gaeke90181482003-11-24 02:52:51 +0000591 operand is shifted left by the number of bits given by the
592 w1 operand. The result is pushed back to the stack.</td>
593</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000594<tr><td>&gt;&gt;</td>
595 <td>SHR</td>
596 <td>w1 w2 -- w1&gt;&gt;w2</td>
597 <td>Two values (w1 and w2) are popped off the stack. The w2
Brian Gaeke90181482003-11-24 02:52:51 +0000598 operand is shifted right by the number of bits given by the
599 w1 operand. The result is pushed back to the stack.</td>
600</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000601<tr><td>OR</td>
602 <td>OR</td>
603 <td>w1 w2 -- w2|w1</td>
604 <td>Two values (w1 and w2) are popped off the stack. The values
Brian Gaeke90181482003-11-24 02:52:51 +0000605 are bitwise OR'd together and pushed back on the stack. This is
606 not a logical OR. The sequence 1 2 OR yields 3 not 1.</td>
607</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000608<tr><td>AND</td>
609 <td>AND</td>
610 <td>w1 w2 -- w2&amp;w1</td>
611 <td>Two values (w1 and w2) are popped off the stack. The values
Brian Gaeke90181482003-11-24 02:52:51 +0000612 are bitwise AND'd together and pushed back on the stack. This is
613 not a logical AND. The sequence 1 2 AND yields 0 not 1.</td>
614</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000615<tr><td>XOR</td>
616 <td>XOR</td>
617 <td>w1 w2 -- w2^w1</td>
618 <td>Two values (w1 and w2) are popped off the stack. The values
Brian Gaeke90181482003-11-24 02:52:51 +0000619 are bitwise exclusive OR'd together and pushed back on the stack.
620 For example, The sequence 1 3 XOR yields 2.</td>
621</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000622<tr><th colspan="4"><b>ARITHMETIC OPERATORS</b></th></tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000623<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000624 <td>Word</td>
625 <td>Name</td>
626 <td>Operation</td>
627 <td>Description</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000628</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000629<tr><td>ABS</td>
630 <td>ABS</td>
631 <td>w -- |w|</td>
632 <td>One value s popped off the stack; its absolute value is computed
John Criswelld000e1d2003-12-18 16:43:17 +0000633 and then pushed on to the stack. If w1 is -1 then w2 is 1. If w1 is
Brian Gaeke90181482003-11-24 02:52:51 +0000634 1 then w2 is also 1.</td>
635</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000636<tr><td>NEG</td>
637 <td>NEG</td>
638 <td>w -- -w</td>
639 <td>One value is popped off the stack which is negated and then
John Criswelld000e1d2003-12-18 16:43:17 +0000640 pushed back on to the stack. If w1 is -1 then w2 is 1. If w1 is
Brian Gaeke90181482003-11-24 02:52:51 +0000641 1 then w2 is -1.</td>
642</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000643<tr><td> + </td>
644 <td>ADD</td>
645 <td>w1 w2 -- w2+w1</td>
646 <td>Two values are popped off the stack. Their sum is pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000647 on to the stack</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000648</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000649<tr><td> - </td>
650 <td>SUB</td>
651 <td>w1 w2 -- w2-w1</td>
652 <td>Two values are popped off the stack. Their difference is pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000653 on to the stack</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000654</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000655<tr><td> * </td>
656 <td>MUL</td>
657 <td>w1 w2 -- w2*w1</td>
658 <td>Two values are popped off the stack. Their product is pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000659 on to the stack</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000660</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000661<tr><td> / </td>
662 <td>DIV</td>
663 <td>w1 w2 -- w2/w1</td>
664 <td>Two values are popped off the stack. Their quotient is pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000665 on to the stack</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000666</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000667<tr><td>MOD</td>
668 <td>MOD</td>
669 <td>w1 w2 -- w2%w1</td>
670 <td>Two values are popped off the stack. Their remainder after division
John Criswelld000e1d2003-12-18 16:43:17 +0000671 of w1 by w2 is pushed back on to the stack</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000672</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000673<tr><td> */ </td>
674 <td>STAR_SLAH</td>
675 <td>w1 w2 w3 -- (w3*w2)/w1</td>
676 <td>Three values are popped off the stack. The product of w1 and w2 is
John Criswelld000e1d2003-12-18 16:43:17 +0000677 divided by w3. The result is pushed back on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000678</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000679<tr><td> ++ </td>
680 <td>INCR</td>
681 <td>w -- w+1</td>
682 <td>One value is popped off the stack. It is incremented by one and then
John Criswelld000e1d2003-12-18 16:43:17 +0000683 pushed back on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000684</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000685<tr><td> -- </td>
686 <td>DECR</td>
687 <td>w -- w-1</td>
688 <td>One value is popped off the stack. It is decremented by one and then
John Criswelld000e1d2003-12-18 16:43:17 +0000689 pushed back on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000690</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000691<tr><td>MIN</td>
692 <td>MIN</td>
693 <td>w1 w2 -- (w2&lt;w1?w2:w1)</td>
694 <td>Two values are popped off the stack. The larger one is pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000695 on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000696</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000697<tr><td>MAX</td>
698 <td>MAX</td>
699 <td>w1 w2 -- (w2&gt;w1?w2:w1)</td>
700 <td>Two values are popped off the stack. The larger value is pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000701 on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000702</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000703<tr><th colspan="4"><b>STACK MANIPULATION OPERATORS</b></th></tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000704<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000705 <td>Word</td>
706 <td>Name</td>
707 <td>Operation</td>
708 <td>Description</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000709</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000710<tr><td>DROP</td>
711 <td>DROP</td>
712 <td>w -- </td>
713 <td>One value is popped off the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000714</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000715<tr><td>DROP2</td>
716 <td>DROP2</td>
717 <td>w1 w2 -- </td>
718 <td>Two values are popped off the stack.</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000719</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000720<tr><td>NIP</td>
721 <td>NIP</td>
722 <td>w1 w2 -- w2</td>
723 <td>The second value on the stack is removed from the stack. That is,
Brian Gaeke90181482003-11-24 02:52:51 +0000724 a value is popped off the stack and retained. Then a second value is
725 popped and the retained value is pushed.</td>
726</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000727<tr><td>NIP2</td>
728 <td>NIP2</td>
729 <td>w1 w2 w3 w4 -- w3 w4</td>
730 <td>The third and fourth values on the stack are removed from it. That is,
Brian Gaeke90181482003-11-24 02:52:51 +0000731 two values are popped and retained. Then two more values are popped and
732 the two retained values are pushed back on.</td>
733</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000734<tr><td>DUP</td>
735 <td>DUP</td>
736 <td>w1 -- w1 w1</td>
737 <td>One value is popped off the stack. That value is then pushed on to
Brian Gaeke90181482003-11-24 02:52:51 +0000738 the stack twice to duplicate the top stack vaue.</td>
739</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000740<tr><td>DUP2</td>
741 <td>DUP2</td>
742 <td>w1 w2 -- w1 w2 w1 w2</td>
743 <td>The top two values on the stack are duplicated. That is, two vaues
Brian Gaeke90181482003-11-24 02:52:51 +0000744 are popped off the stack. They are alternately pushed back on the
745 stack twice each.</td>
746</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000747<tr><td>SWAP</td>
748 <td>SWAP</td>
749 <td>w1 w2 -- w2 w1</td>
750 <td>The top two stack items are reversed in their order. That is, two
John Criswelld000e1d2003-12-18 16:43:17 +0000751 values are popped off the stack and pushed back on to the stack in
Brian Gaeke90181482003-11-24 02:52:51 +0000752 the opposite order they were popped.</td>
753</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000754<tr><td>SWAP2</td>
755 <td>SWAP2</td>
756 <td>w1 w2 w3 w4 -- w3 w4 w2 w1</td>
757 <td>The top four stack items are swapped in pairs. That is, two values
Brian Gaeke90181482003-11-24 02:52:51 +0000758 are popped and retained. Then, two more values are popped and retained.
John Criswelld000e1d2003-12-18 16:43:17 +0000759 The values are pushed back on to the stack in the reverse order but
Misha Brukman36692992004-05-12 19:52:00 +0000760 in pairs.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000761</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000762<tr><td>OVER</td>
763 <td>OVER</td>
764 <td>w1 w2-- w1 w2 w1</td>
765 <td>Two values are popped from the stack. They are pushed back
John Criswelld000e1d2003-12-18 16:43:17 +0000766 on to the stack in the order w1 w2 w1. This seems to cause the
Brian Gaeke90181482003-11-24 02:52:51 +0000767 top stack element to be duplicated "over" the next value.</td>
768</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000769<tr><td>OVER2</td>
770 <td>OVER2</td>
771 <td>w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2</td>
772 <td>The third and fourth values on the stack are replicated on to the
Brian Gaeke90181482003-11-24 02:52:51 +0000773 top of the stack</td>
774</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000775<tr><td>ROT</td>
776 <td>ROT</td>
777 <td>w1 w2 w3 -- w2 w3 w1</td>
778 <td>The top three values are rotated. That is, three value are popped
John Criswelld000e1d2003-12-18 16:43:17 +0000779 off the stack. They are pushed back on to the stack in the order
Brian Gaeke90181482003-11-24 02:52:51 +0000780 w1 w3 w2.</td>
781</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000782<tr><td>ROT2</td>
783 <td>ROT2</td>
784 <td>w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2</td>
785 <td>Like ROT but the rotation is done using three pairs instead of
Brian Gaeke90181482003-11-24 02:52:51 +0000786 three singles.</td>
787</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000788<tr><td>RROT</td>
789 <td>RROT</td>
Reid Spencer68fb5532005-05-04 15:43:40 +0000790 <td>w1 w2 w3 -- w3 w1 w2</td>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000791 <td>Reverse rotation. Like ROT, but it rotates the other way around.
Brian Gaeke90181482003-11-24 02:52:51 +0000792 Essentially, the third element on the stack is moved to the top
793 of the stack.</td>
794</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000795<tr><td>RROT2</td>
796 <td>RROT2</td>
797 <td>w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2</td>
798 <td>Double reverse rotation. Like RROT but the rotation is done using
Brian Gaeke90181482003-11-24 02:52:51 +0000799 three pairs instead of three singles. The fifth and sixth stack
800 elements are moved to the first and second positions</td>
801</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000802<tr><td>TUCK</td>
803 <td>TUCK</td>
804 <td>w1 w2 -- w2 w1 w2</td>
805 <td>Similar to OVER except that the second operand is being
Brian Gaeke90181482003-11-24 02:52:51 +0000806 replicated. Essentially, the first operand is being "tucked"
807 in between two instances of the second operand. Logically, two
808 values are popped off the stack. They are placed back on the
809 stack in the order w2 w1 w2.</td>
810</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000811<tr><td>TUCK2</td>
812 <td>TUCK2</td>
813 <td>w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4</td>
814 <td>Like TUCK but a pair of elements is tucked over two pairs.
Brian Gaeke90181482003-11-24 02:52:51 +0000815 That is, the top two elements of the stack are duplicated and
816 inserted into the stack at the fifth and positions.</td>
817</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000818<tr><td>PICK</td>
819 <td>PICK</td>
820 <td>x0 ... Xn n -- x0 ... Xn x0</td>
821 <td>The top of the stack is used as an index into the remainder of
Brian Gaeke90181482003-11-24 02:52:51 +0000822 the stack. The element at the nth position replaces the index
823 (top of stack). This is useful for cycling through a set of
824 values. Note that indexing is zero based. So, if n=0 then you
825 get the second item on the stack. If n=1 you get the third, etc.
826 Note also that the index is replaced by the n'th value. </td>
827</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000828<tr><td>SELECT</td>
829 <td>SELECT</td>
830 <td>m n X0..Xm Xm+1 .. Xn -- Xm</td>
831 <td>This is like PICK but the list is removed and you need to specify
Brian Gaeke90181482003-11-24 02:52:51 +0000832 both the index and the size of the list. Careful with this one,
833 the wrong value for n can blow away a huge amount of the stack.</td>
834</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000835<tr><td>ROLL</td>
836 <td>ROLL</td>
837 <td>x0 x1 .. xn n -- x1 .. xn x0</td>
838 <td><b>Not Implemented</b>. This one has been left as an exercise to
Chris Lattnere46d6012003-11-25 01:35:06 +0000839 the student. See <a href="#exercise">Exercise</a>. ROLL requires
840 a value, "n", to be on the top of the stack. This value specifies how
841 far into the stack to "roll". The n'th value is <em>moved</em> (not
842 copied) from its location and replaces the "n" value on the top of the
843 stack. In this way, all the values between "n" and x0 roll up the stack.
844 The operation of ROLL is a generalized ROT. The "n" value specifies
845 how much to rotate. That is, ROLL with n=1 is the same as ROT and
846 ROLL with n=2 is the same as ROT2.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000847</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000848<tr><th colspan="4"><b>MEMORY OPERATORS</b></th></tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000849<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000850 <td>Word</td>
851 <td>Name</td>
852 <td>Operation</td>
853 <td>Description</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000854</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000855<tr><td>MALLOC</td>
856 <td>MALLOC</td>
857 <td>w1 -- p</td>
858 <td>One value is popped off the stack. The value is used as the size
Brian Gaeke90181482003-11-24 02:52:51 +0000859 of a memory block to allocate. The size is in bytes, not words.
860 The memory allocation is completed and the address of the memory
John Criswelld000e1d2003-12-18 16:43:17 +0000861 block is pushed on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000862</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000863<tr><td>FREE</td>
864 <td>FREE</td>
865 <td>p -- </td>
866 <td>One pointer value is popped off the stack. The value should be
Brian Gaeke90181482003-11-24 02:52:51 +0000867 the address of a memory block created by the MALLOC operation. The
868 associated memory block is freed. Nothing is pushed back on the
869 stack. Many bugs can be created by attempting to FREE something
870 that isn't a pointer to a MALLOC allocated memory block. Make
871 sure you know what's on the stack. One way to do this is with
872 the following idiom:<br/>
873 <code>64 MALLOC DUP DUP (use ptr) DUP (use ptr) ... FREE</code>
874 <br/>This ensures that an extra copy of the pointer is placed on
875 the stack (for the FREE at the end) and that every use of the
876 pointer is preceded by a DUP to retain the copy for FREE.</td>
877</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000878<tr><td>GET</td>
879 <td>GET</td>
880 <td>w1 p -- w2 p</td>
881 <td>An integer index and a pointer to a memory block are popped of
Brian Gaeke90181482003-11-24 02:52:51 +0000882 the block. The index is used to index one byte from the memory
883 block. That byte value is retained, the pointer is pushed again
884 and the retained value is pushed. Note that the pointer value
885 s essentially retained in its position so this doesn't count
886 as a "use ptr" in the FREE idiom.</td>
887</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000888<tr><td>PUT</td>
889 <td>PUT</td>
890 <td>w1 w2 p -- p </td>
891 <td>An integer value is popped of the stack. This is the value to
Brian Gaeke90181482003-11-24 02:52:51 +0000892 be put into a memory block. Another integer value is popped of
893 the stack. This is the indexed byte in the memory block. A
894 pointer to the memory block is popped off the stack. The
895 first value (w1) is then converted to a byte and written
896 to the element of the memory block(p) at the index given
897 by the second value (w2). The pointer to the memory block is
898 pushed back on the stack so this doesn't count as a "use ptr"
899 in the FREE idiom.</td>
900</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000901<tr><th colspan="4"><b>CONTROL FLOW OPERATORS</b></th></tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000902<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000903 <td>Word</td>
904 <td>Name</td>
905 <td>Operation</td>
906 <td>Description</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000907</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000908<tr><td>RETURN</td>
909 <td>RETURN</td>
910 <td> -- </td>
911 <td>The currently executing definition returns immediately to its caller.
Brian Gaeke90181482003-11-24 02:52:51 +0000912 Note that there is an implicit <code>RETURN</code> at the end of each
913 definition, logically located at the semi-colon. The sequence
914 <code>RETURN ;</code> is valid but redundant.</td>
915</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000916<tr><td>EXIT</td>
917 <td>EXIT</td>
918 <td>w1 -- </td>
919 <td>A return value for the program is popped off the stack. The program is
Brian Gaeke90181482003-11-24 02:52:51 +0000920 then immediately terminated. This is normally an abnormal exit from the
921 program. For a normal exit (when <code>MAIN</code> finishes), the exit
922 code will always be zero in accordance with UNIX conventions.</td>
923</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000924<tr><td>RECURSE</td>
925 <td>RECURSE</td>
926 <td> -- </td>
927 <td>The currently executed definition is called again. This operation is
Brian Gaeke90181482003-11-24 02:52:51 +0000928 needed since the definition of a word doesn't exist until the semi colon
929 is reacher. Attempting something like:<br/>
930 <code> : recurser recurser ; </code><br/> will yield and error saying that
931 "recurser" is not defined yet. To accomplish the same thing, change this
932 to:<br/>
933 <code> : recurser RECURSE ; </code></td>
934</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000935<tr><td>IF (words...) ENDIF</td>
936 <td>IF (words...) ENDIF</td>
937 <td>b -- </td>
938 <td>A boolean value is popped of the stack. If it is non-zero then the "words..."
Brian Gaeke90181482003-11-24 02:52:51 +0000939 are executed. Otherwise, execution continues immediately following the ENDIF.</td>
940</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000941<tr><td>IF (words...) ELSE (words...) ENDIF</td>
942 <td>IF (words...) ELSE (words...) ENDIF</td>
943 <td>b -- </td>
944 <td>A boolean value is popped of the stack. If it is non-zero then the "words..."
Brian Gaeke90181482003-11-24 02:52:51 +0000945 between IF and ELSE are executed. Otherwise the words between ELSE and ENDIF are
946 executed. In either case, after the (words....) have executed, execution continues
947 immediately following the ENDIF. </td>
948</tr>
Reid Spencer68fb5532005-05-04 15:43:40 +0000949<tr><td>WHILE word END</td>
950 <td>WHILE word END</td>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000951 <td>b -- b </td>
Reid Spencer68fb5532005-05-04 15:43:40 +0000952 <td>The boolean value on the top of the stack is examined (not popped). If
953 it is non-zero then the "word" between WHILE and END is executed.
954 Execution then begins again at the WHILE where the boolean on the top of
955 the stack is examined again. The stack is not modified by the WHILE...END
956 loop, only examined. It is imperative that the "word" in the body of the
957 loop ensure that the top of the stack contains the next boolean to examine
958 when it completes. Note that since booleans and integers can be coerced
959 you can use the following "for loop" idiom:<br/>
960 <code>(push count) WHILE word -- END</code><br/>
Brian Gaeke90181482003-11-24 02:52:51 +0000961 For example:<br/>
Reid Spencer68fb5532005-05-04 15:43:40 +0000962 <code>10 WHILE &gt;d -- END</code><br/>
963 This will print the numbers from 10 down to 1. 10 is pushed on the
964 stack. Since that is non-zero, the while loop is entered. The top of
965 the stack (10) is printed out with &gt;d. The top of the stack is
966 decremented, yielding 9 and control is transfered back to the WHILE
967 keyword. The process starts all over again and repeats until
968 the top of stack is decremented to 0 at which point the WHILE test
969 fails and control is transfered to the word after the END.
970 </td>
Brian Gaeke90181482003-11-24 02:52:51 +0000971</tr>
Reid Spencer1f923012004-11-01 20:47:22 +0000972<tr><th colspan="4"><b>INPUT &amp; OUTPUT OPERATORS</b></th></tr>
Chris Lattner45ab10c2003-12-18 06:40:22 +0000973<tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000974 <td>Word</td>
975 <td>Name</td>
976 <td>Operation</td>
977 <td>Description</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000978</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000979<tr><td>SPACE</td>
980 <td>SPACE</td>
981 <td> -- </td>
982 <td>A space character is put out. There is no stack effect.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000983</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000984<tr><td>TAB</td>
985 <td>TAB</td>
986 <td> -- </td>
987 <td>A tab character is put out. There is no stack effect.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000988</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000989<tr><td>CR</td>
990 <td>CR</td>
991 <td> -- </td>
992 <td>A carriage return character is put out. There is no stack effect.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000993</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000994<tr><td>&gt;s</td>
995 <td>OUT_STR</td>
996 <td> -- </td>
997 <td>A string pointer is popped from the stack. It is put out.</td>
Brian Gaeke90181482003-11-24 02:52:51 +0000998</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +0000999<tr><td>&gt;d</td>
1000 <td>OUT_STR</td>
1001 <td> -- </td>
1002 <td>A value is popped from the stack. It is put out as a decimal
1003 integer.</td>
Brian Gaeke90181482003-11-24 02:52:51 +00001004</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001005<tr><td>&gt;c</td>
1006 <td>OUT_CHR</td>
1007 <td> -- </td>
1008 <td>A value is popped from the stack. It is put out as an ASCII
1009 character.</td>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001010</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001011<tr><td>&lt;s</td>
1012 <td>IN_STR</td>
1013 <td> -- s </td>
1014 <td>A string is read from the input via the scanf(3) format string " %as".
1015 The resulting string is pushed on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +00001016</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001017<tr><td>&lt;d</td>
1018 <td>IN_STR</td>
1019 <td> -- w </td>
1020 <td>An integer is read from the input via the scanf(3) format string " %d".
1021 The resulting value is pushed on to the stack</td>
Brian Gaeke90181482003-11-24 02:52:51 +00001022</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001023<tr><td>&lt;c</td>
1024 <td>IN_CHR</td>
1025 <td> -- w </td>
1026 <td>A single character is read from the input via the scanf(3) format string
1027 " %c". The value is converted to an integer and pushed on to the stack.</td>
Brian Gaeke90181482003-11-24 02:52:51 +00001028</tr>
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001029<tr><td>DUMP</td>
1030 <td>DUMP</td>
1031 <td> -- </td>
1032 <td>The stack contents are dumped to standard output. This is useful for
Brian Gaeke90181482003-11-24 02:52:51 +00001033 debugging your definitions. Put DUMP at the beginning and end of a definition
1034 to see instantly the net effect of the definition.</td>
1035</tr>
1036</table>
Misha Brukmanfd90f882004-05-13 00:37:23 +00001037
Brian Gaeke90181482003-11-24 02:52:51 +00001038</div>
1039<!-- ======================================================================= -->
Brian Gaeke07e89e42003-11-24 17:03:38 +00001040<div class="doc_section"> <a name="example">Prime: A Complete Example</a></div>
Brian Gaeke90181482003-11-24 02:52:51 +00001041<div class="doc_text">
Brian Gaeke07e89e42003-11-24 17:03:38 +00001042<p>The following fully documented program highlights many features of both
1043the Stacker language and what is possible with LLVM. The program has two modes
John Criswelld000e1d2003-12-18 16:43:17 +00001044of operation. If you provide numeric arguments to the program, it checks to see
Chris Lattner45ab10c2003-12-18 06:40:22 +00001045if those arguments are prime numbers and prints out the results. Without any
John Criswelld000e1d2003-12-18 16:43:17 +00001046arguments, the program prints out any prime numbers it finds between 1 and one
Chris Lattner45ab10c2003-12-18 06:40:22 +00001047million (there's a lot of them!). The source code comments below tell the
Brian Gaeke07e89e42003-11-24 17:03:38 +00001048remainder of the story.
Brian Gaeke90181482003-11-24 02:52:51 +00001049</p>
1050</div>
1051<div class="doc_text">
Brian Gaeke07e89e42003-11-24 17:03:38 +00001052<pre><code>
Brian Gaeke90181482003-11-24 02:52:51 +00001053################################################################################
1054#
1055# Brute force prime number generator
1056#
1057# This program is written in classic Stacker style, that being the style of a
1058# stack. Start at the bottom and read your way up !
1059#
1060# Reid Spencer - Nov 2003
1061################################################################################
1062# Utility definitions
1063################################################################################
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001064: print &gt;d CR ;
Brian Gaeke90181482003-11-24 02:52:51 +00001065: it_is_a_prime TRUE ;
1066: it_is_not_a_prime FALSE ;
1067: continue_loop TRUE ;
1068: exit_loop FALSE;
1069
1070################################################################################
John Criswelld000e1d2003-12-18 16:43:17 +00001071# This definition tries an actual division of a candidate prime number. It
Brian Gaeke90181482003-11-24 02:52:51 +00001072# determines whether the division loop on this candidate should continue or
1073# not.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001074# STACK&lt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001075# div - the divisor to try
1076# p - the prime number we are working on
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001077# STACK&gt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001078# cont - should we continue the loop ?
1079# div - the next divisor to try
1080# p - the prime number we are working on
1081################################################################################
1082: try_dividing
1083 DUP2 ( save div and p )
1084 SWAP ( swap to put divisor second on stack)
1085 MOD 0 = ( get remainder after division and test for 0 )
1086 IF
1087 exit_loop ( remainder = 0, time to exit )
1088 ELSE
1089 continue_loop ( remainder != 0, keep going )
1090 ENDIF
1091;
1092
1093################################################################################
1094# This function tries one divisor by calling try_dividing. But, before doing
1095# that it checks to see if the value is 1. If it is, it does not bother with
1096# the division because prime numbers are allowed to be divided by one. The
1097# top stack value (cont) is set to determine if the loop should continue on
1098# this prime number or not.
1099# STACK<:
1100# cont - should we continue the loop (ignored)?
1101# div - the divisor to try
1102# p - the prime number we are working on
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001103# STACK&gt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001104# cont - should we continue the loop ?
1105# div - the next divisor to try
1106# p - the prime number we are working on
1107################################################################################
1108: try_one_divisor
1109 DROP ( drop the loop continuation )
1110 DUP ( save the divisor )
1111 1 = IF ( see if divisor is == 1 )
1112 exit_loop ( no point dividing by 1 )
1113 ELSE
1114 try_dividing ( have to keep going )
1115 ENDIF
1116 SWAP ( get divisor on top )
1117 -- ( decrement it )
1118 SWAP ( put loop continuation back on top )
1119;
1120
1121################################################################################
1122# The number on the stack (p) is a candidate prime number that we must test to
1123# determine if it really is a prime number. To do this, we divide it by every
1124# number from one p-1 to 1. The division is handled in the try_one_divisor
1125# definition which returns a loop continuation value (which we also seed with
1126# the value 1). After the loop, we check the divisor. If it decremented all
1127# the way to zero then we found a prime, otherwise we did not find one.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001128# STACK&lt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001129# p - the prime number to check
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001130# STACK&gt;:
John Criswelld000e1d2003-12-18 16:43:17 +00001131# yn - boolean indicating if its a prime or not
Brian Gaeke90181482003-11-24 02:52:51 +00001132# p - the prime number checked
1133################################################################################
1134: try_harder
1135 DUP ( duplicate to get divisor value ) )
1136 -- ( first divisor is one less than p )
1137 1 ( continue the loop )
1138 WHILE
1139 try_one_divisor ( see if its prime )
1140 END
1141 DROP ( drop the continuation value )
1142 0 = IF ( test for divisor == 1 )
1143 it_is_a_prime ( we found one )
1144 ELSE
1145 it_is_not_a_prime ( nope, this one is not a prime )
1146 ENDIF
1147;
1148
1149################################################################################
1150# This definition determines if the number on the top of the stack is a prime
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001151# or not. It does this by testing if the value is degenerate (&lt;= 3) and
Brian Gaeke90181482003-11-24 02:52:51 +00001152# responding with yes, its a prime. Otherwise, it calls try_harder to actually
1153# make some calculations to determine its primeness.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001154# STACK&lt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001155# p - the prime number to check
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001156# STACK&gt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001157# yn - boolean indicating if its a prime or not
1158# p - the prime number checked
1159################################################################################
1160: is_prime
1161 DUP ( save the prime number )
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001162 3 &gt;= IF ( see if its &lt;= 3 )
Brian Gaeke90181482003-11-24 02:52:51 +00001163 it_is_a_prime ( its <= 3 just indicate its prime )
1164 ELSE
1165 try_harder ( have to do a little more work )
1166 ENDIF
1167;
1168
1169################################################################################
1170# This definition is called when it is time to exit the program, after we have
1171# found a sufficiently large number of primes.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001172# STACK&lt;: ignored
1173# STACK&gt;: exits
Brian Gaeke90181482003-11-24 02:52:51 +00001174################################################################################
1175: done
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001176 "Finished" &gt;s CR ( say we are finished )
Brian Gaeke90181482003-11-24 02:52:51 +00001177 0 EXIT ( exit nicely )
1178;
1179
1180################################################################################
1181# This definition checks to see if the candidate is greater than the limit. If
1182# it is, it terminates the program by calling done. Otherwise, it increments
1183# the value and calls is_prime to determine if the candidate is a prime or not.
1184# If it is a prime, it prints it. Note that the boolean result from is_prime is
1185# gobbled by the following IF which returns the stack to just contining the
1186# prime number just considered.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001187# STACK&lt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001188# p - one less than the prime number to consider
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001189# STAC&gt;K
Brian Gaeke90181482003-11-24 02:52:51 +00001190# p+1 - the prime number considered
1191################################################################################
1192: consider_prime
1193 DUP ( save the prime number to consider )
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001194 1000000 &lt; IF ( check to see if we are done yet )
Brian Gaeke90181482003-11-24 02:52:51 +00001195 done ( we are done, call "done" )
1196 ENDIF
1197 ++ ( increment to next prime number )
1198 is_prime ( see if it is a prime )
1199 IF
1200 print ( it is, print it )
1201 ENDIF
1202;
1203
1204################################################################################
1205# This definition starts at one, prints it out and continues into a loop calling
1206# consider_prime on each iteration. The prime number candidate we are looking at
1207# is incremented by consider_prime.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001208# STACK&lt;: empty
1209# STACK&gt;: empty
Brian Gaeke90181482003-11-24 02:52:51 +00001210################################################################################
1211: find_primes
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001212 "Prime Numbers: " &gt;s CR ( say hello )
Brian Gaeke90181482003-11-24 02:52:51 +00001213 DROP ( get rid of that pesky string )
1214 1 ( stoke the fires )
1215 print ( print the first one, we know its prime )
1216 WHILE ( loop while the prime to consider is non zero )
1217 consider_prime ( consider one prime number )
1218 END
1219;
1220
1221################################################################################
1222#
1223################################################################################
1224: say_yes
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001225 &gt;d ( Print the prime number )
Brian Gaeke90181482003-11-24 02:52:51 +00001226 " is prime." ( push string to output )
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001227 &gt;s ( output it )
Brian Gaeke90181482003-11-24 02:52:51 +00001228 CR ( print carriage return )
1229 DROP ( pop string )
1230;
1231
1232: say_no
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001233 &gt;d ( Print the prime number )
Brian Gaeke90181482003-11-24 02:52:51 +00001234 " is NOT prime." ( push string to put out )
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001235 &gt;s ( put out the string )
Brian Gaeke90181482003-11-24 02:52:51 +00001236 CR ( print carriage return )
1237 DROP ( pop string )
1238;
1239
1240################################################################################
1241# This definition processes a single command line argument and determines if it
1242# is a prime number or not.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001243# STACK&lt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001244# n - number of arguments
1245# arg1 - the prime numbers to examine
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001246# STACK&gt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001247# n-1 - one less than number of arguments
1248# arg2 - we processed one argument
1249################################################################################
1250: do_one_argument
1251 -- ( decrement loop counter )
1252 SWAP ( get the argument value )
1253 is_prime IF ( determine if its prime )
1254 say_yes ( uhuh )
1255 ELSE
1256 say_no ( nope )
1257 ENDIF
1258 DROP ( done with that argument )
1259;
1260
1261################################################################################
1262# The MAIN program just prints a banner and processes its arguments.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001263# STACK&lt;:
Brian Gaeke90181482003-11-24 02:52:51 +00001264# n - number of arguments
1265# ... - the arguments
1266################################################################################
1267: process_arguments
1268 WHILE ( while there are more arguments )
1269 do_one_argument ( process one argument )
1270 END
1271;
1272
1273################################################################################
1274# The MAIN program just prints a banner and processes its arguments.
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001275# STACK&lt;: arguments
Brian Gaeke90181482003-11-24 02:52:51 +00001276################################################################################
1277: MAIN
1278 NIP ( get rid of the program name )
1279 -- ( reduce number of arguments )
1280 DUP ( save the arg counter )
Misha Brukmanf39d5d62004-05-13 00:24:43 +00001281 1 &lt;= IF ( See if we got an argument )
Brian Gaeke90181482003-11-24 02:52:51 +00001282 process_arguments ( tell user if they are prime )
1283 ELSE
1284 find_primes ( see how many we can find )
1285 ENDIF
1286 0 ( push return code )
1287;
Brian Gaeke90181482003-11-24 02:52:51 +00001288</code>
Brian Gaeke07e89e42003-11-24 17:03:38 +00001289</pre>
Brian Gaeke90181482003-11-24 02:52:51 +00001290</div>
1291<!-- ======================================================================= -->
Brian Gaeke07e89e42003-11-24 17:03:38 +00001292<div class="doc_section"> <a name="internal">Internals</a></div>
1293<div class="doc_text">
1294 <p><b>This section is under construction.</b>
1295 <p>In the mean time, you can always read the code! It has comments!</p>
1296</div>
1297<!-- ======================================================================= -->
1298<div class="doc_subsection"> <a name="directory">Directory Structure</a></div>
Bill Wendling79070052007-09-22 10:13:39 +00001299
Brian Gaeke07e89e42003-11-24 17:03:38 +00001300<div class="doc_text">
1301<p>The source code, test programs, and sample programs can all be found
Reid Spencer72826f62007-01-17 05:37:42 +00001302in the LLVM repository named <tt>llvm-stacker</tt> This should be checked out to
1303the <tt>projects</tt> directory so that it will auto-configure. To do that, make
1304sure you have the llvm sources in <tt><i>llvm</i></tt>
1305(see <a href="GettingStarted.html">Getting Started</a>) and then use these
Bill Wendling79070052007-09-22 10:13:39 +00001306commands:</p>
1307
1308<div class="doc_code">
1309<pre>
1310% svn co http://llvm.org/svn/llvm-project/llvm-top/trunk llvm-top
1311% cd llvm-top
1312% make build MODULE=stacker
1313</pre>
1314</div>
1315
Reid Spencer72826f62007-01-17 05:37:42 +00001316<p>Under the <tt>projects/llvm-stacker</tt> directory you will find the
1317implementation of the Stacker compiler, as follows:</p>
Bill Wendling79070052007-09-22 10:13:39 +00001318
Brian Gaeke07e89e42003-11-24 17:03:38 +00001319<ul>
1320 <li><em>lib</em> - contains most of the source code
1321 <ul>
1322 <li><em>lib/compiler</em> - contains the compiler library
1323 <li><em>lib/runtime</em> - contains the runtime library
1324 </ul></li>
1325 <li><em>test</em> - contains the test programs</li>
1326 <li><em>tools</em> - contains the Stacker compiler main program, stkrc
1327 <ul>
1328 <li><em>lib/stkrc</em> - contains the Stacker compiler main program
1329 </ul</li>
1330 <li><em>sample</em> - contains the sample programs</li>
1331</ul>
1332</div>
Bill Wendling79070052007-09-22 10:13:39 +00001333
Brian Gaeke07e89e42003-11-24 17:03:38 +00001334<!-- ======================================================================= -->
1335<div class="doc_subsection"><a name="lexer"></a>The Lexer</div>
Bill Wendling79070052007-09-22 10:13:39 +00001336
Brian Gaeke07e89e42003-11-24 17:03:38 +00001337<div class="doc_text">
Reid Spencer72826f62007-01-17 05:37:42 +00001338<p>See projects/llvm-stacker/lib/compiler/Lexer.l</p>
Misha Brukman36692992004-05-12 19:52:00 +00001339</div>
Bill Wendling79070052007-09-22 10:13:39 +00001340
Brian Gaeke07e89e42003-11-24 17:03:38 +00001341<!-- ======================================================================= -->
1342<div class="doc_subsection"><a name="parser"></a>The Parser</div>
1343<div class="doc_text">
Reid Spencer72826f62007-01-17 05:37:42 +00001344<p>See projects/llvm-stacker/lib/compiler/StackerParser.y</p>
Misha Brukman36692992004-05-12 19:52:00 +00001345</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +00001346<!-- ======================================================================= -->
1347<div class="doc_subsection"><a name="compiler"></a>The Compiler</div>
1348<div class="doc_text">
Reid Spencer72826f62007-01-17 05:37:42 +00001349<p>See projects/llvm-stacker/lib/compiler/StackerCompiler.cpp</p>
Misha Brukman36692992004-05-12 19:52:00 +00001350</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +00001351<!-- ======================================================================= -->
1352<div class="doc_subsection"><a name="runtime"></a>The Runtime</div>
1353<div class="doc_text">
Reid Spencer72826f62007-01-17 05:37:42 +00001354<p>See projects/llvm-stacker/lib/runtime/stacker_rt.c</p>
Misha Brukman36692992004-05-12 19:52:00 +00001355</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +00001356<!-- ======================================================================= -->
1357<div class="doc_subsection"><a name="driver"></a>Compiler Driver</div>
1358<div class="doc_text">
Reid Spencer72826f62007-01-17 05:37:42 +00001359<p>See projects/llvm-stacker/tools/stkrc/stkrc.cpp</p>
Misha Brukman36692992004-05-12 19:52:00 +00001360</div>
Brian Gaeke07e89e42003-11-24 17:03:38 +00001361<!-- ======================================================================= -->
1362<div class="doc_subsection"><a name="tests"></a>Test Programs</div>
1363<div class="doc_text">
Reid Spencer72826f62007-01-17 05:37:42 +00001364<p>See projects/llvm-stacker/test/*.st</p>
Misha Brukman36692992004-05-12 19:52:00 +00001365</div>
Brian Gaeke90181482003-11-24 02:52:51 +00001366<!-- ======================================================================= -->
Chris Lattnere46d6012003-11-25 01:35:06 +00001367<div class="doc_subsection"> <a name="exercise">Exercise</a></div>
1368<div class="doc_text">
1369<p>As you may have noted from a careful inspection of the Built-In word
1370definitions, the ROLL word is not implemented. This word was left out of
1371Stacker on purpose so that it can be an exercise for the student. The exercise
1372is to implement the ROLL functionality (in your own workspace) and build a test
John Criswelld000e1d2003-12-18 16:43:17 +00001373program for it. If you can implement ROLL, you understand Stacker and probably
Chris Lattnere46d6012003-11-25 01:35:06 +00001374a fair amount about LLVM since this is one of the more complicated Stacker
1375operations. The work will almost be completely limited to the
1376<a href="#compiler">compiler</a>.
1377<p>The ROLL word is already recognized by both the lexer and parser but ignored
1378by the compiler. That means you don't have to futz around with figuring out how
1379to get the keyword recognized. It already is. The part of the compiler that
1380you need to implement is the <code>ROLL</code> case in the
Misha Brukmanfe22af62004-04-16 16:20:07 +00001381<code>StackerCompiler::handle_word(int)</code> method.</p> See the
1382implementations of PICK and SELECT in the same method to get some hints about
1383how to complete this exercise.<p>
Chris Lattnere46d6012003-11-25 01:35:06 +00001384<p>Good luck!</p>
1385</div>
1386<!-- ======================================================================= -->
Misha Brukmanfe22af62004-04-16 16:20:07 +00001387<div class="doc_subsection"><a name="todo">Things Remaining To Be Done</a></div>
Chris Lattnere46d6012003-11-25 01:35:06 +00001388<div class="doc_text">
1389<p>The initial implementation of Stacker has several deficiencies. If you're
1390interested, here are some things that could be implemented better:</p>
1391<ol>
1392 <li>Write an LLVM pass to compute the correct stack depth needed by the
Chris Lattner45ab10c2003-12-18 06:40:22 +00001393 program. Currently the stack is set to a fixed number which means programs
1394 with large numbers of definitions might fail.</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001395 <li>Write an LLVM pass to optimize the use of the global stack. The code
1396 emitted currently is somewhat wasteful. It gets cleaned up a lot by existing
1397 passes but more could be done.</li>
Misha Brukmanfe22af62004-04-16 16:20:07 +00001398 <li>Make the compiler driver use the LLVM linking facilities (with IPO)
1399 before depending on GCC to do the final link.</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001400 <li>Clean up parsing. It doesn't handle errors very well.</li>
1401 <li>Rearrange the StackerCompiler.cpp code to make better use of inserting
1402 instructions before a block's terminating instruction. I didn't figure this
Misha Brukmanfe22af62004-04-16 16:20:07 +00001403 technique out until I was nearly done with LLVM. As it is, its a bad example
Chris Lattnere46d6012003-11-25 01:35:06 +00001404 of how to insert instructions!</li>
1405 <li>Provide for I/O to arbitrary files instead of just stdin/stdout.</li>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001406 <li>Write additional built-in words; with inspiration from FORTH</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001407 <li>Write additional sample Stacker programs.</li>
Chris Lattner45ab10c2003-12-18 06:40:22 +00001408 <li>Add your own compiler writing experiences and tips in the
1409 <a href="#lessons">Lessons I Learned About LLVM</a> section.</li>
Chris Lattnere46d6012003-11-25 01:35:06 +00001410</ol>
1411</div>
Misha Brukman36692992004-05-12 19:52:00 +00001412
1413<!-- *********************************************************************** -->
1414
Brian Gaeke90181482003-11-24 02:52:51 +00001415<hr>
Misha Brukman36692992004-05-12 19:52:00 +00001416<address>
1417 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1418 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1419 <a href="http://validator.w3.org/check/referer"><img
1420 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1421
1422 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a><br>
Reid Spencer05fe4b02006-03-14 05:39:39 +00001423 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Misha Brukman36692992004-05-12 19:52:00 +00001424 Last modified: $Date$
1425</address>
1426
Brian Gaeke90181482003-11-24 02:52:51 +00001427</body>
1428</html>