81ad60e8fd609bbbc55b46ce4053f5a11b82dcf3
[oota-llvm.git] / docs / Stacker.html
1 <!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>
9 <ol>
10   <li><a href="#abstract">Abstract</a></li>
11   <li><a href="#introduction">Introduction</a></li>
12   <li><a href="#lexicon">The Stacker Lexicon</a>
13     <ol>
14       <li><a href="#stack">The Stack</a>
15       <li><a href="#punctuation">Punctuation</a>
16       <li><a href="#literals">Literals</a>
17       <li><a href="#words">Words</a>
18       <li><a href="#builtins">Built-Ins</a>
19     </ol>
20   </li>
21   <li><a href="#directory">The Directory Structure </a>
22 </ol>
23 <div class="doc_text">
24 <p><b>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a> </b></p>
25 <p> </p>
26 </div>
27 <!-- ======================================================================= -->
28 <div class="doc_section"> <a name="abstract">Abstract </a></div>
29 <div class="doc_text">
30 <p>This document is another way to learn about LLVM. Unlike the 
31 <a href="LangRef.html">LLVM Reference Manual</a> or 
32 <a href="ProgrammersManual.html">LLVM Programmer's Manual</a>, this
33 document walks you through the implementation of a programming language
34 named Stacker. Stacker was invented specifically as a demonstration of
35 LLVM. The emphasis in this document is not on describing the
36 intricacies of LLVM itself, but on how to use it to build your own
37 compiler system.</p>
38 </div>
39 <!-- ======================================================================= -->
40 <div class="doc_section"> <a name="introduction">Introduction</a> </div>
41 <div class="doc_text">
42 <p>Amongst other things, LLVM is a platform for compiler writers.
43 Because of its exceptionally clean and small IR (intermediate
44 representation), compiler writing with LLVM is much easier than with
45 other system. As proof, the author of Stacker wrote the entire
46 compiler (language definition, lexer, parser, code generator, etc.) in
47 about <em>four days</em>! That's important to know because it shows 
48 how quickly you can get a new
49 language up when using LLVM. Furthermore, this was the <em >first</em> 
50 language the author ever created using LLVM. The learning curve is 
51 included in that four days.</p>
52 <p>The language described here, Stacker, is Forth-like. Programs
53 are simple collections of word definitions and the only thing definitions
54 can do is manipulate a stack or generate I/O.  Stacker is not a "real" 
55 programming language; its very simple.  Although it is computationally 
56 complete, you wouldn't use it for your next big project. However, 
57 the fact that it is complete, its simple, and it <em>doesn't</em> have 
58 a C-like syntax make it useful for demonstration purposes. It shows
59 that LLVM could be applied to a wide variety of language syntaxes.</p>
60 <p>The basic notions behind stacker is very simple. There's a stack of 
61 integers (or character pointers) that the program manipulates. Pretty 
62 much the only thing the program can do is manipulate the stack and do 
63 some limited I/O operations. The language provides you with several 
64 built-in words that manipulate the stack in interesting ways. To get 
65 your feet wet, here's how you write the traditional "Hello, World" 
66 program in Stacker:</p>
67 <p><code>: hello_world "Hello, World!" &gt;s DROP CR ;<br>
68 : MAIN hello_world ;<br></code></p>
69 <p>This has two "definitions" (Stacker manipulates words, not
70 functions and words have definitions): <code>MAIN</code> and <code>
71 hello_world</code>. The <code>MAIN</code> definition is standard, it
72 tells Stacker where to start. Here, <code>MAIN</code> is defined to 
73 simply invoke the word <code>hello_world</code>. The
74 <code>hello_world</code> definition tells stacker to push the 
75 <code>"Hello, World!"</code> string onto the stack, print it out 
76 (<code>&gt;s</code>), pop it off the stack (<code>DROP</code>), and
77 finally print a carriage return (<code>CR</code>). Although 
78 <code>hello_world</code> uses the stack, its net effect is null. Well
79 written Stacker definitions have that characteristic. </p>
80 <p>Exercise for the reader: how could you make this a one line program?</p>
81 </div>
82 <!-- ======================================================================= -->
83 <div class="doc_section"><a name="stack"></a>Lessons Learned About LLVM</div>
84 <div class="doc_text">
85 <p>Stacker was written for two purposes: (a) to get the author over the 
86 learning curve and (b) to provide a simple example of how to write a compiler
87 using LLVM. During the development of Stacker, many lessons about LLVM were
88 learned. Those lessons are described in the following subsections.<p>
89 </div>
90 <div class="doc_subsection"><a name="linkage"></a>Getting Linkage Types Right</div>
91 <div class="doc_text"><p>To be completed.</p></div>
92 <div class="doc_subsection"><a name="linkage"></a>Everything's a Value!</div>
93 <div class="doc_text"><p>To be completed.</p></div>
94 <div class="doc_subsection"><a name="linkage"></a>The Wily GetElementPtrInst</div>
95 <div class="doc_text"><p>To be completed.</p></div>
96 <div class="doc_subsection"><a name="linkage"></a>Constants Are Easier Than That!</div>
97 <div class="doc_text"><p>To be completed.</p></div>
98 <div class="doc_subsection"><a name="linkage"></a>Terminate Those Blocks!</div>
99 <div class="doc_text"><p>To be completed.</p></div>
100 <div class="doc_subsection"><a name="linkage"></a>new,get,create .. Its All The Same</div>
101 <div class="doc_text"><p>To be completed.</p></div>
102 <div class="doc_subsection"><a name="linkage"></a>Utility Functions To The Rescue</div>
103 <div class="doc_text"><p>To be completed.</p></div>
104 <div class="doc_subsection"><a name="linkage"></a>push_back Is Your Friend</div>
105 <div class="doc_text"><p>To be completed.</p></div>
106 <div class="doc_subsection"><a name="linkage"></a>Block Heads Come First</div>
107 <div class="doc_text"><p>To be completed.</p></div>
108 <!-- ======================================================================= -->
109 <div class="doc_section"> <a name="lexicon">The Stacker Lexicon</a></div>
110 <div class="doc_subsection"><a name="stack"></a>The Stack</div>
111 <div class="doc_text">
112 <p>Stacker definitions define what they do to the global stack. Before
113 proceeding, a few words about the stack are in order. The stack is simply
114 a global array of 32-bit integers or pointers. A global index keeps track
115 of the location of the to of the stack. All of this is hidden from the 
116 programmer but it needs to be noted because it is the foundation of the 
117 conceptual programming model for Stacker. When you write a definition,
118 you are, essentially, saying how you want that definition to manipulate
119 the global stack.</p>
120 <p>Manipulating the stack can be quite hazardous. There is no distinction
121 given and no checking for the various types of values that can be placed
122 on the stack. Automatic coercion between types is performed. In many 
123 cases this is useful. For example, a boolean value placed on the stack
124 can be interpreted as an integer with good results. However, using a
125 word that interprets that boolean value as a pointer to a string to
126 print out will almost always yield a crash. Stacker simply leaves it
127 to the programmer to get it right without any interference or hindering
128 on interpretation of the stack values. You've been warned :) </p>
129 </div>
130 <!-- ======================================================================= -->
131 <div class="doc_subsection"> <a name="punctuation"></a>Punctuation</div>
132 <div class="doc_text">
133 <p>Punctuation in Stacker is very simple. The colon and semi-colon 
134 characters are used to introduce and terminate a definition
135 (respectively). Except for <em>FORWARD</em> declarations, definitions 
136 are all you can specify in Stacker.  Definitions are read left to right. 
137 Immediately after the semi-colon comes the name of the word being defined. 
138 The remaining words in the definition specify what the word does.</p>
139 </div>
140 <!-- ======================================================================= -->
141 <div class="doc_subsection"><a name="literals"></a>Literals</div>
142 <div class="doc_text">
143     <p>There are three kinds of literal values in Stacker. Integer, Strings,
144     and Booleans. In each case, the stack operation is to simply push the
145     value onto the stack. So, for example:<br/>
146     <code> 42 " is the answer." TRUE </code><br/>
147     will push three values onto the stack: the integer 42, the
148     string " is the answer." and the boolean TRUE.</p>
149 </div>
150 <!-- ======================================================================= -->
151 <div class="doc_subsection"><a name="words"></a>Words</div>
152 <div class="doc_text">
153 <p>Each definition in Stacker is composed of a set of words. Words are
154 read and executed in order from left to right. There is very little
155 checking in Stacker to make sure you're doing the right thing with 
156 the stack. It is assumed that the programmer knows how the stack 
157 transformation he applies will affect the program.</p>
158 <p>Words in a definition come in two flavors: built-in and programmer
159 defined. Simply mentioning the name of a previously defined or declared
160 programmer-defined word causes that words definition to be invoked. It
161 is somewhat like a function call in other languages. The built-in
162 words have various effects, described below.</p>
163 <p>Sometimes you need to call a word before it is defined. For this, you can
164 use the <code>FORWARD</code> declaration. It looks like this</p>
165 <p><code>FORWARD name ;</code></p>
166 <p>This simply states to Stacker that "name" is the name of a definition
167 that is defined elsewhere. Generally it means the definition can be found
168 "forward" in the file. But, it doesn't have to be in the current compilation
169 unit. Anything declared with <code>FORWARD</code> is an external symbol for
170 linking.</p>
171 </div>
172 <!-- ======================================================================= -->
173 <div class="doc_subsection"><a name="builtins"></a>Built In Words</div>
174 <div class="doc_text">
175 <p>The built-in words of the Stacker language are put in several groups 
176 depending on what they do. The groups are as follows:</p>
177 <ol> 
178     <li><em>Logical</em>These words provide the logical operations for
179     comparing stack operands.<br/>The words are: &lt; &gt; &lt;= &gt;= 
180     = &lt;&gt; true false.</li>
181     <li><em>Bitwise</em>These words perform bitwise computations on 
182     their operands. <br/> The words are: &lt;&lt; &gt;&gt; XOR AND NOT</li>
183     <li><em>Arithmetic</em>These words perform arithmetic computations on
184     their operands. <br/> The words are: ABS NEG + - * / MOD */ ++ -- MIN MAX</li>
185     <li><em>Stack</em>These words manipulate the stack directly by moving
186     its elements around.<br/> The words are: DROP DUP SWAP OVER ROT DUP2 DROP2 PICK TUCK</li>
187     <li><em>Memory></em>These words allocate, free and manipulate memory
188     areas outside the stack.<br/>The words are: MALLOC FREE GET PUT</li>
189     <li><em>Control</em>These words alter the normal left to right flow
190     of execution.<br/>The words are: IF ELSE ENDIF WHILE END RETURN EXIT RECURSE</li>
191     <li><em>I/O</em> These words perform output on the standard output
192     and input on the standard input. No other I/O is possible in Stacker.
193     <br/>The words are: SPACE TAB CR &gt;s &gt;d &gt;c &lt;s &lt;d &lt;c.</li>
194 </ol>
195 <p>While you may be familiar with many of these operations from other
196 programming languages, a careful review of their semantics is important
197 for correct programming in Stacker. Of most importance is the effect 
198 that each of these built-in words has on the global stack. The effect is
199 not always intuitive. To better describe the effects, we'll borrow from Forth the idiom of
200 describing the effect on the stack with:</p>
201 <p><code> BEFORE -- AFTER </code></p> 
202 <p>That is, to the left of the -- is a representation of the stack before
203 the operation. To the right of the -- is a representation of the stack
204 after the operation. In the table below that describes the operation of
205 each of the built in words, we will denote the elements of the stack 
206 using the following construction:</p>
207 <ol>
208     <li><em>b</em> - a boolean truth value</li>
209     <li><em>w</em> - a normal integer valued word.</li>
210     <li><em>s</em> - a pointer to a string value</li>
211     <li><em>p</em> - a pointer to a malloc's memory block</li>
212 </ol>
213 </div>
214 <div class="doc_text">
215 <table class="doc_table" >
216 <tr class="doc_table"><td colspan="4">Definition Of Operation Of Built In Words</td></tr>
217 <tr class="doc_table"><td colspan="4">LOGICAL OPERATIONS</td></tr>
218 <tr class="doc_table"><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
219 <tr class="doc_table"><td>&lt;</td>
220     <td>LT</td>
221     <td>w1 w2 -- b</td>
222     <td>Two values (w1 and w2) are popped off the stack and
223     compared. If w1 is less than w2, TRUE is pushed back on
224     the stack, otherwise FALSE is pushed back on the stack.</td>
225 </tr>
226 <tr><td>&gt;</td>
227     <td>GT</td>
228     <td>w1 w2 -- b</td>
229     <td>Two values (w1 and w2) are popped off the stack and
230     compared. If w1 is greater than w2, TRUE is pushed back on
231     the stack, otherwise FALSE is pushed back on the stack.</td>
232 </tr>
233 <tr><td>&gt;=</td>
234     <td>GE</td>
235     <td>w1 w2 -- b</td>
236     <td>Two values (w1 and w2) are popped off the stack and
237     compared. If w1 is greater than or equal to w2, TRUE is 
238     pushed back on the stack, otherwise FALSE is pushed back 
239     on the stack.</td>
240 </tr>
241 <tr><td>&lt;=</td>
242     <td>LE</td>
243     <td>w1 w2 -- b</td>
244     <td>Two values (w1 and w2) are popped off the stack and
245     compared. If w1 is less than or equal to w2, TRUE is 
246     pushed back on the stack, otherwise FALSE is pushed back 
247     on the stack.</td>
248 </tr>
249 <tr><td>=</td>
250     <td>EQ</td>
251     <td>w1 w2 -- b</td>
252     <td>Two values (w1 and w2) are popped off the stack and
253     compared. If w1 is equal to w2, TRUE is 
254     pushed back on the stack, otherwise FALSE is pushed back 
255     </td>
256 </tr>
257 <tr><td>&lt;&gt;</td>
258     <td>NE</td>
259     <td>w1 w2 -- b</td>
260     <td>Two values (w1 and w2) are popped off the stack and
261     compared. If w1 is equal to w2, TRUE is 
262     pushed back on the stack, otherwise FALSE is pushed back 
263     </td>
264 </tr>
265 <tr><td>FALSE</td>
266     <td>FALSE</td>
267     <td> -- b</td>
268     <td>The boolean value FALSE (0) is pushed onto the stack.</td>
269 </tr>
270 <tr><td>TRUE</td>
271     <td>TRUE</td>
272     <td> -- b</td>
273     <td>The boolean value TRUE (-1) is pushed onto the stack.</td>
274 </tr>
275 <tr><td colspan="4">BITWISE OPERATIONS</td></tr>
276 <tr><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
277 <tr><td>&lt;&lt;</td>
278     <td>SHL</td>
279     <td>w1 w2 -- w1&lt;&lt;w2</td>
280     <td>Two values (w1 and w2) are popped off the stack. The w2
281     operand is shifted left by the number of bits given by the
282     w1 operand. The result is pushed back to the stack.</td>
283 </tr>
284 <tr><td>&gt;&gt;</td>
285     <td>SHR</td>
286     <td>w1 w2 -- w1&gt;&gt;w2</td>
287     <td>Two values (w1 and w2) are popped off the stack. The w2
288     operand is shifted right by the number of bits given by the
289     w1 operand. The result is pushed back to the stack.</td>
290 </tr>
291 <tr><td>OR</td>
292     <td>OR</td>
293     <td>w1 w2 -- w2|w1</td>
294     <td>Two values (w1 and w2) are popped off the stack. The values
295     are bitwise OR'd together and pushed back on the stack. This is 
296     not a logical OR. The sequence 1 2 OR yields 3 not 1.</td>
297 </tr>
298 <tr><td>AND</td>
299     <td>AND</td>
300     <td>w1 w2 -- w2&amp;w1</td>
301     <td>Two values (w1 and w2) are popped off the stack. The values
302     are bitwise AND'd together and pushed back on the stack. This is 
303     not a logical AND. The sequence 1 2 AND yields 0 not 1.</td>
304 </tr>
305 <tr><td>XOR</td>
306     <td>XOR</td>
307     <td>w1 w2 -- w2^w1</td>
308     <td>Two values (w1 and w2) are popped off the stack. The values
309     are bitwise exclusive OR'd together and pushed back on the stack. 
310     For example, The sequence 1 3 XOR yields 2.</td>
311 </tr>
312 <tr><td colspan="4">ARITHMETIC OPERATIONS</td></tr>
313 <tr><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
314 <tr><td>ABS</td>
315     <td>ABS</td>
316     <td>w -- |w|</td>
317     <td>One value s popped off the stack; its absolute value is computed
318     and then pushed onto the stack. If w1 is -1 then w2 is 1. If w1 is
319     1 then w2 is also 1.</td>
320 </tr>
321 <tr><td>NEG</td>
322     <td>NEG</td>
323     <td>w -- -w</td>
324     <td>One value is popped off the stack which is negated and then
325     pushed back onto the stack. If w1 is -1 then w2 is 1. If w1 is
326     1 then w2 is -1.</td>
327 </tr>
328 <tr><td> + </td>
329     <td>ADD</td>
330     <td>w1 w2 -- w2+w1</td>
331     <td>Two values are popped off the stack. Their sum is pushed back
332     onto the stack</td>
333 </tr>
334 <tr><td> - </td>
335     <td>SUB</td>
336     <td>w1 w2 -- w2-w1</td>
337     <td>Two values are popped off the stack. Their difference is pushed back
338     onto the stack</td>
339 </tr>
340 <tr><td> * </td>
341     <td>MUL</td>
342     <td>w1 w2 -- w2*w1</td>
343     <td>Two values are popped off the stack. Their product is pushed back
344     onto the stack</td>
345 </tr>
346 <tr><td> / </td>
347     <td>DIV</td>
348     <td>w1 w2 -- w2/w1</td>
349     <td>Two values are popped off the stack. Their quotient is pushed back
350     onto the stack</td>
351 </tr>
352 <tr><td>MOD</td>
353     <td>MOD</td>
354     <td>w1 w2 -- w2%w1</td>
355     <td>Two values are popped off the stack. Their remainder after division
356     of w1 by w2 is pushed back onto the stack</td>
357 </tr>
358 <tr><td> */ </td>
359     <td>STAR_SLAH</td>
360     <td>w1 w2 w3 -- (w3*w2)/w1</td>
361     <td>Three values are popped off the stack. The product of w1 and w2 is
362     divided by w3. The result is pushed back onto the stack.</td>
363 </tr>
364 <tr><td> ++ </td>
365     <td>INCR</td>
366     <td>w -- w+1</td>
367     <td>One value is popped off the stack. It is incremented by one and then
368     pushed back onto the stack.</td>
369 </tr>
370 <tr><td> -- </td>
371     <td>DECR</td>
372     <td>w -- w-1</td>
373     <td>One value is popped off the stack. It is decremented by one and then
374     pushed back onto the stack.</td>
375 </tr>
376 <tr><td>MIN</td>
377     <td>MIN</td>
378     <td>w1 w2 -- (w2&lt;w1?w2:w1)</td>
379     <td>Two values are popped off the stack. The larger one is pushed back
380     onto the stack.</td>
381 </tr>
382 <tr><td>MAX</td>
383     <td>MAX</td>
384     <td>w1 w2 -- (w2&gt;w1?w2:w1)</td>
385     <td>Two values are popped off the stack. The larger value is pushed back
386         onto the stack.</td>
387 </tr>
388 <tr><td colspan="4">STACK MANIPULATION OPERATIONS</td></tr>
389 <tr><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
390 <tr><td>DROP</td>
391     <td>DROP</td>
392     <td>w -- </td>
393     <td>One value is popped off the stack.</td>
394 </tr>
395 <tr><td>DROP2</td>
396     <td>DROP2</td>
397     <td>w1 w2 -- </td>
398     <td>Two values are popped off the stack.</td>
399 </tr>
400 <tr><td>NIP</td>
401     <td>NIP</td>
402     <td>w1 w2 -- w2</td>
403     <td>The second value on the stack is removed from the stack. That is,
404         a value is popped off the stack and retained. Then a second value is
405         popped and the retained value is pushed.</td>
406 </tr>
407 <tr><td>NIP2</td>
408     <td>NIP2</td>
409     <td>w1 w2 w3 w4 -- w3 w4</td>
410     <td>The third and fourth values on the stack are removed from it. That is,
411         two values are popped and retained. Then two more values are popped and
412         the two retained values are pushed back on.</td>
413 </tr>
414 <tr><td>DUP</td>
415     <td>DUP</td>
416     <td>w1 -- w1 w1</td>
417     <td>One value is popped off the stack. That value is then pushed onto
418         the stack twice to duplicate the top stack vaue.</td>
419 </tr>
420 <tr><td>DUP2</td>
421     <td>DUP2</td>
422     <td>w1 w2 -- w1 w2 w1 w2</td>
423     <td>The top two values on the stack are duplicated. That is, two vaues
424         are popped off the stack. They are alternately pushed back on the
425         stack twice each.</td>
426 </tr>
427 <tr><td>SWAP</td>
428     <td>SWAP</td>
429     <td>w1 w2 -- w2 w1</td>
430     <td>The top two stack items are reversed in their order. That is, two
431         values are popped off the stack and pushed back onto the stack in
432         the opposite order they were popped.</td>
433 </tr>
434 <tr><td>SWAP2</td>
435     <td>SWAP2</td>
436     <td>w1 w2 w3 w4 -- w3 w4 w2 w1</td>
437     <td>The top four stack items are swapped in pairs. That is, two values
438         are popped and retained. Then, two more values are popped and retained.
439         The values are pushed back onto the stack in the reverse order but
440         in pairs.</p>
441 </tr>
442 <tr><td>OVER</td>
443     <td>OVER</td>
444     <td>w1 w2-- w1 w2 w1</td>
445     <td>Two values are popped from the stack. They are pushed back
446         onto the stack in the order w1 w2 w1. This seems to cause the
447         top stack element to be duplicated "over" the next value.</td>
448 </tr>
449 <tr><td>OVER2</td>
450     <td>OVER2</td>
451     <td>w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2</td>
452     <td>The third and fourth values on the stack are replicated onto the
453         top of the stack</td>
454 </tr>
455 <tr><td>ROT</td>
456     <td>ROT</td>
457     <td>w1 w2 w3 -- w2 w3 w1</td>
458     <td>The top three values are rotated. That is, three value are popped
459         off the stack. They are pushed back onto the stack in the order
460         w1 w3 w2.</td>
461 </tr>
462 <tr><td>ROT2</td>
463     <td>ROT2</td>
464     <td>w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2</td>
465     <td>Like ROT but the rotation is done using three pairs instead of
466         three singles.</td>
467 </tr>
468 <tr><td>RROT</td>
469     <td>RROT</td>
470     <td>w1 w2 w3 -- w2 w3 w1</td>
471     <td>Reverse rotation. Like ROT, but it rotates the other way around.
472         Essentially, the third element on the stack is moved to the top
473         of the stack.</td>
474 </tr>
475 <tr><td>RROT2</td>
476     <td>RROT2</td>
477     <td>w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2</td>
478     <td>Double reverse rotation. Like RROT but the rotation is done using 
479         three pairs instead of three singles. The fifth and sixth stack 
480         elements are moved to the first and second positions</td>
481 </tr>
482 <tr><td>TUCK</td>
483     <td>TUCK</td>
484     <td>w1 w2 -- w2 w1 w2</td>
485     <td>Similar to OVER except that the second operand is being 
486         replicated. Essentially, the first operand is being "tucked"
487         in between two instances of the second operand. Logically, two
488         values are popped off the stack. They are placed back on the
489         stack in the order w2 w1 w2.</td>
490 </tr>
491 <tr><td>TUCK2</td>
492     <td>TUCK2</td>
493     <td>w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4</td>
494     <td>Like TUCK but a pair of elements is tucked over two pairs.
495         That is, the top two elements of the stack are duplicated and
496         inserted into the stack at the fifth and positions.</td>
497 </tr>
498 <tr><td>PICK</td>
499     <td>PICK</td>
500     <td>x0 ... Xn n -- x0 ... Xn x0</td>
501     <td>The top of the stack is used as an index into the remainder of
502         the stack. The element at the nth position replaces the index 
503         (top of stack). This is useful for cycling through a set of 
504         values. Note that indexing is zero based. So, if n=0 then you
505         get the second item on the stack. If n=1 you get the third, etc.
506         Note also that the index is replaced by the n'th value. </td>
507 </tr>
508 <tr><td>SELECT</td>
509     <td>SELECT</td>
510     <td>m n X0..Xm Xm+1 .. Xn -- Xm</td>
511     <td>This is like PICK but the list is removed and you need to specify
512         both the index and the size of the list. Careful with this one,
513         the wrong value for n can blow away a huge amount of the stack.</td>
514 </tr>
515 <tr><td>ROLL</td>
516     <td>ROLL</td>
517     <td>x0 x1 .. xn n -- x1 .. xn x0</td>
518     <td><b>Not Implemented</b>. This one has been left as an exercise to
519         the student. If you can implement this one you understand Stacker
520         and probably a fair amount about LLVM since this is one of the
521         more complicated Stacker operations.  See the StackerCompiler.cpp 
522         file in the projects/Stacker/lib/compiler directory.  The operation 
523         of ROLL is like a generalized ROT. That is ROLL with n=1 is the 
524         same as ROT. The n value (top of stack) is used as an index to 
525         select a value up the stack that is <em>moved</em> to the top of 
526         the stack. See the implementations of PICk and SELECT to get 
527         some hints.<p>
528 </tr>
529 <tr><td colspan="4">MEMORY OPERATIONS</td></tr>
530 <tr><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
531 <tr><td>MALLOC</td>
532     <td>MALLOC</td>
533     <td>w1 -- p</td>
534     <td>One value is popped off the stack. The value is used as the size
535         of a memory block to allocate. The size is in bytes, not words.
536         The memory allocation is completed and the address of the memory
537         block is pushed onto the stack.</td>
538 </tr>
539 <tr><td>FREE</td>
540     <td>FREE</td>
541     <td>p -- </td>
542     <td>One pointer value is popped off the stack. The value should be
543         the address of a memory block created by the MALLOC operation. The
544         associated memory block is freed. Nothing is pushed back on the
545         stack. Many bugs can be created by attempting to FREE something
546         that isn't a pointer to a MALLOC allocated memory block. Make
547         sure you know what's on the stack.  One way to do this is with
548         the following idiom:<br/>
549         <code>64 MALLOC DUP DUP (use ptr) DUP (use ptr) ...  FREE</code>
550         <br/>This ensures that an extra copy of the pointer is placed on
551         the stack (for the FREE at the end) and that every use of the
552         pointer is preceded by a DUP to retain the copy for FREE.</td>
553 </tr>
554 <tr><td>GET</td>
555     <td>GET</td>
556     <td>w1 p -- w2 p</td>
557     <td>An integer index and a pointer to a memory block are popped of
558         the block. The index is used to index one byte from the memory
559         block. That byte value is retained, the pointer is pushed again
560         and the retained value is pushed. Note that the pointer value
561         s essentially retained in its position so this doesn't count
562         as a "use ptr" in the FREE idiom.</td>
563 </tr>
564 <tr><td>PUT</td>
565     <td>PUT</td>
566     <td>w1 w2 p -- p </td>
567     <td>An integer value is popped of the stack. This is the value to
568         be put into a memory block. Another integer value is popped of
569         the stack. This is the indexed byte in the memory block. A
570         pointer to the memory block is popped off the stack. The
571         first value (w1) is then converted to a byte and written
572         to the element of the memory block(p) at the index given
573         by the second value (w2). The pointer to the memory block is
574         pushed back on the stack so this doesn't count as a "use ptr"
575         in the FREE idiom.</td>
576 </tr>
577 <tr><td colspan="4">CONTROL FLOW OPERATIONS</td></tr>
578 <tr><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
579 <tr><td>RETURN</td>
580     <td>RETURN</td>
581     <td> --  </td>
582     <td>The currently executing definition returns immediately to its caller.
583         Note that there is an implicit <code>RETURN</code> at the end of each
584         definition, logically located at the semi-colon. The sequence 
585         <code>RETURN ;</code>  is valid but redundant.</td>
586 </tr>
587 <tr><td>EXIT</td>
588     <td>EXIT</td>
589     <td>w1 -- </td>
590     <td>A return value for the program is popped off the stack. The program is
591         then immediately terminated. This is normally an abnormal exit from the
592         program. For a normal exit (when <code>MAIN</code> finishes), the exit
593         code will always be zero in accordance with UNIX conventions.</td>
594 </tr>
595 <tr><td>RECURSE</td>
596     <td>RECURSE</td>
597     <td> -- </td>
598     <td>The currently executed definition is called again. This operation is 
599         needed since the definition of a word doesn't exist until the semi colon
600         is reacher. Attempting something like:<br/>
601         <code> : recurser recurser ; </code><br/> will yield and error saying that 
602         "recurser" is not defined yet. To accomplish the same thing, change this
603         to:<br/>
604         <code> : recurser RECURSE ; </code></td>
605 </tr>
606 <tr><td>IF (words...) ENDIF</td>
607     <td>IF (words...) ENDIF</td>
608     <td>b -- </td>
609     <td>A boolean value is popped of the stack. If it is non-zero then the "words..." 
610         are executed. Otherwise, execution continues immediately following the ENDIF.</td>
611 </tr>
612 <tr><td>IF (words...) ELSE (words...) ENDIF</td>
613     <td>IF (words...) ELSE (words...) ENDIF</td>
614     <td>b -- </td>
615     <td>A boolean value is popped of the stack. If it is non-zero then the "words..."
616         between IF and ELSE are executed. Otherwise the words between ELSE and ENDIF are
617         executed. In either case, after the (words....) have executed, execution continues
618         immediately following the ENDIF. </td>
619 </tr>
620 <tr><td>WHILE (words...) END</td>
621     <td>WHILE (words...) END</td>
622     <td>b -- b </td>
623     <td>The boolean value on the top of the stack is examined. If it is non-zero then the 
624         "words..." between WHILE and END are executed. Execution then begins again at the WHILE where another
625         boolean is popped off the stack. To prevent this operation from eating up the entire
626         stack, you should push onto the stack (just before the END) a boolean value that indicates
627         whether to terminate. Note that since booleans and integers can be coerced you can
628         use the following "for loop" idiom:<br/>
629         <code>(push count) WHILE (words...) -- END</code><br/>
630         For example:<br/>
631         <code>10 WHILE DUP &gt;d -- END</code><br/>
632         This will print the numbers from 10 down to 1. 10 is pushed on the stack. Since that is
633         non-zero, the while loop is entered. The top of the stack (10) is duplicated and then
634         printed out with &gt;d. The top of the stack is decremented, yielding 9 and control is
635         transfered back to the WHILE keyword. The process starts all over again and repeats until
636         the top of stack is decremented to 0 at which the WHILE test fails and control is
637         transfered to the word after the END.</td>
638 </tr>
639 <tr><td colspan="4">INPUT &amp; OUTPUT OPERATIONS</td></tr>
640 <tr><td>Word</td><td>Name</td><td>Operation</td><td>Description</td></tr>
641 <tr><td>SPACE</td>
642     <td>SPACE</td>
643     <td> --  </td>
644     <td>A space character is put out. There is no stack effect.</td>
645 </tr>
646 <tr><td>TAB</td>
647     <td>TAB</td>
648     <td> --  </td>
649     <td>A tab character is put out. There is no stack effect.</td>
650 </tr>
651 <tr><td>CR</td>
652     <td>CR</td>
653     <td> --  </td>
654     <td>A carriage return character is put out. There is no stack effect.</td>
655 </tr>
656 <tr><td>&gt;s</td>
657     <td>OUT_STR</td>
658     <td> -- </td>
659     <td>A string pointer is popped from the stack. It is put out.</td>
660 </tr>
661 <tr><td>&gt;d</td>
662     <td>OUT_STR</td>
663     <td> -- </td>
664     <td>A value is popped from the stack. It is put out as a decimal integer.</td>
665 </tr>
666 <tr><td>&gt;c</td>
667     <td>OUT_CHR</td>
668     <td> -- </td>
669     <td>A value is popped from the stack. It is put out as an ASCII character.</td>
670 </tr>
671 <tr><td>&lt;s</td>
672     <td>IN_STR</td>
673     <td> -- s </td>
674     <td>A string is read from the input via the scanf(3) format string " %as". The
675         resulting string is pushed onto the stack.</td>
676 </tr>
677 <tr><td>&lt;d</td>
678     <td>IN_STR</td>
679     <td> -- w </td>
680     <td>An integer is read from the input via the scanf(3) format string " %d". The
681         resulting value is pushed onto the stack</td>
682 </tr>
683 <tr><td>&lt;c</td>
684     <td>IN_CHR</td>
685     <td> -- w </td>
686     <td>A single character is read from the input via the scanf(3) format string 
687         " %c". The value is converted to an integer and pushed onto the stack.</td>
688 </tr>
689 <tr><td>DUMP</td>
690     <td>DUMP</td>
691     <td> -- </td>
692     <td>The stack contents are dumped to standard output. This is useful for
693         debugging your definitions. Put DUMP at the beginning and end of a definition
694         to see instantly the net effect of the definition.</td>
695 </tr>
696 </table>
697 </div>
698 <!-- ======================================================================= -->
699 <div class="doc_section"> <a name="directory">Directory Structure</a></div>
700 <div class="doc_text">
701 <p>The source code, test programs, and sample programs can all be found
702 under the LLVM "projects" directory. You will need to obtain the LLVM sources
703 to find it (either via anonymous CVS or a tarball. See the 
704 <a href="GettingStarted.html">Getting Started</a> document).</p>
705 <p>Under the "projects" directory there is a directory named "stacker". That
706 directory contains everything, as follows:</p>
707 <ul>
708     <li><em>lib</em> - contains most of the source code
709     <ul>
710         <li><em>lib/compiler</em> - contains the compiler library
711         <li><em>lib/runtime</em> - contains the runtime library
712     </ul></li>
713     <li><em>test</em> - contains the test programs</li>
714     <li><em>tools</em> - contains the Stacker compiler main program, stkrc
715     <ul>
716         <li><em>lib/stkrc</em> - contains the Stacker compiler main program
717     </ul</li>
718     <li><em>sample</em> - contains the sample programs</li>
719 </ul>
720 </div>
721 <!-- ======================================================================= -->
722 <div class="doc_section"> <a name="directory">Prime: A Complete Example</a></div>
723 <div class="doc_text">
724 <p>The following fully documented program highlights many of features of both
725 the Stacker language and what is possible with LLVM. The program simply 
726 prints out the prime numbers until it reaches
727 </p>
728 </div>
729 <div class="doc_text">
730 <p><code>
731 <![CDATA[
732 ################################################################################
733 #
734 # Brute force prime number generator
735 #
736 # This program is written in classic Stacker style, that being the style of a 
737 # stack. Start at the bottom and read your way up !
738 #
739 # Reid Spencer - Nov 2003 
740 ################################################################################
741 # Utility definitions
742 ################################################################################
743 : print >d CR ;
744 : it_is_a_prime TRUE ;
745 : it_is_not_a_prime FALSE ;
746 : continue_loop TRUE ;
747 : exit_loop FALSE;
748     
749 ################################################################################
750 # This definition tryies an actual division of a candidate prime number. It
751 # determines whether the division loop on this candidate should continue or
752 # not.
753 # STACK<:
754 #    div - the divisor to try
755 #    p   - the prime number we are working on
756 # STACK>:
757 #    cont - should we continue the loop ?
758 #    div - the next divisor to try
759 #    p   - the prime number we are working on
760 ################################################################################
761 : try_dividing
762     DUP2                        ( save div and p )
763     SWAP                        ( swap to put divisor second on stack)
764     MOD 0 =                     ( get remainder after division and test for 0 )
765     IF 
766         exit_loop               ( remainder = 0, time to exit )
767     ELSE
768         continue_loop           ( remainder != 0, keep going )
769     ENDIF
770 ;
771
772 ################################################################################
773 # This function tries one divisor by calling try_dividing. But, before doing
774 # that it checks to see if the value is 1. If it is, it does not bother with
775 # the division because prime numbers are allowed to be divided by one. The
776 # top stack value (cont) is set to determine if the loop should continue on
777 # this prime number or not.
778 # STACK<:
779 #    cont - should we continue the loop (ignored)?
780 #    div - the divisor to try
781 #    p   - the prime number we are working on
782 # STACK>:
783 #    cont - should we continue the loop ?
784 #    div - the next divisor to try
785 #    p   - the prime number we are working on
786 ################################################################################
787 : try_one_divisor
788     DROP                        ( drop the loop continuation )
789     DUP                         ( save the divisor )
790     1 = IF                      ( see if divisor is == 1 )
791         exit_loop               ( no point dividing by 1 )
792     ELSE
793         try_dividing            ( have to keep going )
794     ENDIF
795     SWAP                        ( get divisor on top )
796     --                          ( decrement it )
797     SWAP                        ( put loop continuation back on top )
798 ;
799
800 ################################################################################
801 # The number on the stack (p) is a candidate prime number that we must test to 
802 # determine if it really is a prime number. To do this, we divide it by every 
803 # number from one p-1 to 1. The division is handled in the try_one_divisor 
804 # definition which returns a loop continuation value (which we also seed with
805 # the value 1).  After the loop, we check the divisor. If it decremented all
806 # the way to zero then we found a prime, otherwise we did not find one.
807 # STACK<:
808 #   p - the prime number to check
809 # STACK>:
810 #   yn - boolean indiating if its a prime or not
811 #   p - the prime number checked
812 ################################################################################
813 : try_harder
814     DUP                         ( duplicate to get divisor value ) )
815     --                          ( first divisor is one less than p )
816     1                           ( continue the loop )
817     WHILE
818        try_one_divisor          ( see if its prime )
819     END
820     DROP                        ( drop the continuation value )
821     0 = IF                      ( test for divisor == 1 )
822        it_is_a_prime            ( we found one )
823     ELSE
824        it_is_not_a_prime        ( nope, this one is not a prime )
825     ENDIF
826 ;
827
828 ################################################################################
829 # This definition determines if the number on the top of the stack is a prime 
830 # or not. It does this by testing if the value is degenerate (<= 3) and 
831 # responding with yes, its a prime. Otherwise, it calls try_harder to actually 
832 # make some calculations to determine its primeness.
833 # STACK<:
834 #    p - the prime number to check
835 # STACK>:
836 #    yn - boolean indicating if its a prime or not
837 #    p  - the prime number checked
838 ################################################################################
839 : is_prime 
840     DUP                         ( save the prime number )
841     3 >= IF                     ( see if its <= 3 )
842         it_is_a_prime           ( its <= 3 just indicate its prime )
843     ELSE 
844         try_harder              ( have to do a little more work )
845     ENDIF 
846 ;
847
848 ################################################################################
849 # This definition is called when it is time to exit the program, after we have 
850 # found a sufficiently large number of primes.
851 # STACK<: ignored
852 # STACK>: exits
853 ################################################################################
854 : done 
855     "Finished" >s CR            ( say we are finished )
856     0 EXIT                      ( exit nicely )
857 ;
858
859 ################################################################################
860 # This definition checks to see if the candidate is greater than the limit. If 
861 # it is, it terminates the program by calling done. Otherwise, it increments 
862 # the value and calls is_prime to determine if the candidate is a prime or not. 
863 # If it is a prime, it prints it. Note that the boolean result from is_prime is
864 # gobbled by the following IF which returns the stack to just contining the
865 # prime number just considered.
866 # STACK<: 
867 #    p - one less than the prime number to consider
868 # STACK>
869 #    p+1 - the prime number considered
870 ################################################################################
871 : consider_prime 
872     DUP                         ( save the prime number to consider )
873     1000000 < IF                ( check to see if we are done yet )
874         done                    ( we are done, call "done" )
875     ENDIF 
876     ++                          ( increment to next prime number )
877     is_prime                    ( see if it is a prime )
878     IF 
879        print                    ( it is, print it )
880     ENDIF 
881 ;
882
883 ################################################################################
884 # This definition starts at one, prints it out and continues into a loop calling
885 # consider_prime on each iteration. The prime number candidate we are looking at
886 # is incremented by consider_prime.
887 # STACK<: empty
888 # STACK>: empty
889 ################################################################################
890 : find_primes 
891     "Prime Numbers: " >s CR     ( say hello )
892     DROP                        ( get rid of that pesky string )
893     1                           ( stoke the fires )
894     print                       ( print the first one, we know its prime )
895     WHILE                       ( loop while the prime to consider is non zero )
896         consider_prime          ( consider one prime number )
897     END 
898
899
900 ################################################################################
901 #
902 ################################################################################
903 : say_yes
904     >d                          ( Print the prime number )
905     " is prime."                ( push string to output )
906     >s                          ( output it )
907     CR                          ( print carriage return )
908     DROP                        ( pop string )
909 ;
910
911 : say_no
912     >d                          ( Print the prime number )
913     " is NOT prime."            ( push string to put out )
914     >s                          ( put out the string )
915     CR                          ( print carriage return )
916     DROP                        ( pop string )
917 ;
918
919 ################################################################################
920 # This definition processes a single command line argument and determines if it
921 # is a prime number or not.
922 # STACK<:
923 #    n - number of arguments
924 #    arg1 - the prime numbers to examine
925 # STACK>:
926 #    n-1 - one less than number of arguments
927 #    arg2 - we processed one argument
928 ################################################################################
929 : do_one_argument
930     --                          ( decrement loop counter )
931     SWAP                        ( get the argument value  )
932     is_prime IF                 ( determine if its prime )
933         say_yes                 ( uhuh )
934     ELSE
935         say_no                  ( nope )
936     ENDIF
937     DROP                        ( done with that argument )
938 ;
939
940 ################################################################################
941 # The MAIN program just prints a banner and processes its arguments.
942 # STACK<:
943 #    n - number of arguments
944 #    ... - the arguments
945 ################################################################################
946 : process_arguments
947     WHILE                       ( while there are more arguments )
948        do_one_argument          ( process one argument )
949     END
950 ;
951     
952 ################################################################################
953 # The MAIN program just prints a banner and processes its arguments.
954 # STACK<: arguments
955 ################################################################################
956 : MAIN 
957     NIP                         ( get rid of the program name )
958     --                          ( reduce number of arguments )
959     DUP                         ( save the arg counter )
960     1 <= IF                     ( See if we got an argument )
961         process_arguments       ( tell user if they are prime )
962     ELSE
963         find_primes             ( see how many we can find )
964     ENDIF
965     0                           ( push return code )
966 ;
967 ]]>
968 </code>
969 </p>
970 </div>
971 <!-- ======================================================================= -->
972 <div class="doc_section"> <a name="lexicon">Internals</a></div>
973 <div class="doc_text"><p>To be completed.</p></div>
974 <div class="doc_subsection"><a name="stack"></a>The Lexer</div>
975 <div class="doc_subsection"><a name="stack"></a>The Parser</div>
976 <div class="doc_subsection"><a name="stack"></a>The Compiler</div>
977 <div class="doc_subsection"><a name="stack"></a>The Stack</div>
978 <div class="doc_subsection"><a name="stack"></a>Definitions Are Functions</div>
979 <div class="doc_subsection"><a name="stack"></a>Words Are BasicBlocks</div>
980 <!-- ======================================================================= -->
981 <hr>
982 <div class="doc_footer">
983 <address><a href="mailto:rspencer@x10sys.com">Reid Spencer</a></address>
984 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a> 
985 <br>Last modified: $Date$ </div>
986 </body>
987 </html>