X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FStacker.html;h=225a27bbf77556707220239e905f8a3d89dff404;hb=e50fb9ac174b791047ffa8648443ab94b2097cd9;hp=835c8380b8b9f731877a75a8949f94e6bbb2d4a1;hpb=d000e1dc2f08892aaa8c1bfd96f85f24b9d66cbb;p=oota-llvm.git diff --git a/docs/Stacker.html b/docs/Stacker.html index 835c8380b8b..225a27bbf77 100644 --- a/docs/Stacker.html +++ b/docs/Stacker.html @@ -1,12 +1,14 @@ - + - Stacker: An Example Of Using LLVM + Stacker: An Example Of Using LLVM +
Stacker: An Example Of Using LLVM
-
+
  1. Abstract
  2. Introduction
  3. @@ -19,19 +21,17 @@
  4. The Wily GetElementPtrInst
  5. Getting Linkage Types Right
  6. Constants Are Easier Than That!
  7. -
- +
  • The Stacker Lexicon
      -
    1. The Stack -
    2. Punctuation -
    3. Comments -
    4. Literals -
    5. Words -
    6. Standard Style -
    7. Built-Ins -
    -
  • +
  • The Stack
  • +
  • Punctuation
  • +
  • Comments
  • +
  • Literals
  • +
  • Words
  • +
  • Standard Style
  • +
  • Built-Ins
  • +
  • Prime: A Complete Example
  • Internal Code Details
      @@ -44,16 +44,15 @@
    1. Test Programs
    2. Exercise
    3. Things Remaining To Be Done
    4. -
    -
  • + -
    -

    Written by Reid Spencer

    -

    + +
    +

    Written by Reid Spencer

    -
    + -
    Abstract
    +
    Abstract

    This document is another way to learn about LLVM. Unlike the LLVM Reference Manual or @@ -128,31 +127,28 @@ I noted that most of the important LLVM IR (Intermediate Representation) C++ classes were derived from the Value class. The full power of that simple design only became fully understood once I started constructing executable expressions for Stacker.

    +

    This really makes your programming go faster. Think about compiling code for the following C/C++ expression: (a|b)*((x+1)/(y+1)). Assuming the values are on the stack in the order a, b, x, y, this could be expressed in stacker as: 1 + SWAP 1 + / ROT2 OR *. -You could write a function using LLVM that computes this expression like this:

    -
    
    +You could write a function using LLVM that computes this expression like 
    +this: 

    + +
     Value* 
     expression(BasicBlock* bb, Value* a, Value* b, Value* x, Value* y )
     {
    -    Instruction* tail = bb->getTerminator();
    -    ConstantSInt* one = ConstantSInt::get( Type::IntTy, 1);
    -    BinaryOperator* or1 = 
    -	BinaryOperator::create( Instruction::Or, a, b, "", tail );
    -    BinaryOperator* add1 = 
    -	BinaryOperator::create( Instruction::Add, x, one, "", tail );
    -    BinaryOperator* add2 =
    -	BinaryOperator::create( Instruction::Add, y, one, "", tail );
    -    BinaryOperator* div1 = 
    -	BinaryOperator::create( Instruction::Div, add1, add2, "", tail);
    -    BinaryOperator* mult1 = 
    -	BinaryOperator::create( Instruction::Mul, or1, div1, "", tail );
    -
    +    ConstantInt* one = ConstantInt::get(Type::IntTy, 1);
    +    BinaryOperator* or1 = BinaryOperator::createOr(a, b, "", bb);
    +    BinaryOperator* add1 = BinaryOperator::createAdd(x, one, "", bb);
    +    BinaryOperator* add2 = BinaryOperator::createAdd(y, one, "", bb);
    +    BinaryOperator* div1 = BinaryOperator::createDiv(add1, add2, "", bb);
    +    BinaryOperator* mult1 = BinaryOperator::createMul(or1, div1, "", bb);
         return mult1;
     }
    -
    +
    +

    "Okay, big deal," you say? It is a big deal. Here's why. Note that I didn't have to tell this function which kinds of Values are being passed in. They could be Instructions, Constants, GlobalVariables, or @@ -202,7 +198,7 @@ should be constructed. In general, here's what I learned:

  • Create your blocks early. While writing your compiler, you will encounter several situations where you know apriori that you will need several blocks. For example, if-then-else, switch, while, and for - statements in C/C++ all need multiple blocks for expression in LVVM. + statements in C/C++ all need multiple blocks for expression in LLVM. The rule is, create them early.
  • Terminate your blocks early. This just reduces the chances that you forget to terminate your blocks which is required (go @@ -222,18 +218,18 @@ should be constructed. In general, here's what I learned: before. This makes for some very clean compiler design.
  • The foregoing is such an important principal, its worth making an idiom:

    -
    
    -BasicBlock* bb = new BasicBlock();
    +
    +BasicBlock* bb = new BasicBlock();
     bb->getInstList().push_back( new Branch( ... ) );
     new Instruction(..., bb->getTerminator() );
    -
    +

    To make this clear, consider the typical if-then-else statement (see StackerCompiler::handle_if() method). We can set this up in a single function using LLVM in the following way:

     using namespace llvm;
     BasicBlock*
    -MyCompiler::handle_if( BasicBlock* bb, SetCondInst* condition )
    +MyCompiler::handle_if( BasicBlock* bb, ICmpInst* condition )
     {
         // Create the blocks to contain code in the structure of if/then/else
         BasicBlock* then_bb = new BasicBlock(); 
    @@ -301,20 +297,21 @@ read the Language Reference and Programmer's Manual a couple times each, I still
     missed a few very key points:
     

    This means that when you look up an element in the global variable (assuming it's a struct or array), you must deference the pointer first! For many things, this leads to the idiom:

    -
    
    -std::vector index_vector;
    -index_vector.push_back( ConstantSInt::get( Type::LongTy, 0 );
    +
    +std::vector<Value*> index_vector;
    +index_vector.push_back( ConstantInt::get( Type::LongTy, 0 );
     // ... push other indices ...
     GetElementPtrInst* gep = new GetElementPtrInst( ptr, index_vector );
    -
    +

    For example, suppose we have a global variable whose type is [24 x int]. The variable itself represents a pointer to that array. To subscript the array, we need two indices, not just one. The first index (0) dereferences the @@ -346,7 +343,7 @@ different. I recommend you read the carefully. Then, read it again.

    Here are some handy tips that I discovered along the way:

    +
    Standard Style
    +
    +

    TODO

    +
    +
    Built In Words

    The built-in words of the Stacker language are put in several groups @@ -509,330 +511,331 @@ using the following construction:

    - - - - - - - - - - - - -
    Definition Of Operation Of Built In Words
    LOGICAL OPERATIONS
    WordNameOperationDescription
    <LTw1 w2 -- bTwo values (w1 and w2) are popped off the stack and + + + + + + + + + + + + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + + - - - - + + + + - + - - - - - - - - - + + + + + + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - + - - - - - - - - - + + + + + + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - + - - - - - - - - - + + + + + + + + - - - - + + + - - - - + + + - - - - + + + - + - - - - - - - - - + + + + + + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - + 10 WHILE >d -- END
    + This will print the numbers from 10 down to 1. 10 is pushed on the + stack. Since that is non-zero, the while loop is entered. The top of + the stack (10) is printed out with >d. The top of the stack is + decremented, yielding 9 and control is transfered back to the WHILE + keyword. The process starts all over again and repeats until + the top of stack is decremented to 0 at which point the WHILE test + fails and control is transfered to the word after the END. + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Definition Of Operation Of Built In Words
    LOGICAL OPERATIONS
    WordNameOperationDescription
    <LTw1 w2 -- bTwo values (w1 and w2) are popped off the stack and compared. If w1 is less than w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack.
    >GTw1 w2 -- bTwo values (w1 and w2) are popped off the stack and +
    >GTw1 w2 -- bTwo values (w1 and w2) are popped off the stack and compared. If w1 is greater than w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack.
    >=GEw1 w2 -- bTwo values (w1 and w2) are popped off the stack and +
    >=GEw1 w2 -- bTwo values (w1 and w2) are popped off the stack and compared. If w1 is greater than or equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack.
    <=LEw1 w2 -- bTwo values (w1 and w2) are popped off the stack and +
    <=LEw1 w2 -- bTwo values (w1 and w2) are popped off the stack and compared. If w1 is less than or equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack.
    =EQw1 w2 -- bTwo values (w1 and w2) are popped off the stack and +
    =EQw1 w2 -- bTwo values (w1 and w2) are popped off the stack and compared. If w1 is equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back
    <>NEw1 w2 -- bTwo values (w1 and w2) are popped off the stack and +
    <>NEw1 w2 -- bTwo values (w1 and w2) are popped off the stack and compared. If w1 is equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back
    FALSEFALSE -- bThe boolean value FALSE (0) is pushed on to the stack.
    FALSEFALSE -- bThe boolean value FALSE (0) is pushed on to the stack.
    TRUETRUE -- bThe boolean value TRUE (-1) is pushed on to the stack.
    TRUETRUE -- bThe boolean value TRUE (-1) is pushed on to the stack.
    BITWISE OPERATORS
    BITWISE OPERATORS
    WordNameOperationDescription
    <<SHLw1 w2 -- w1<<w2Two values (w1 and w2) are popped off the stack. The w2 + WordNameOperationDescription
    <<SHLw1 w2 -- w1<<w2Two values (w1 and w2) are popped off the stack. The w2 operand is shifted left by the number of bits given by the w1 operand. The result is pushed back to the stack.
    >>SHRw1 w2 -- w1>>w2Two values (w1 and w2) are popped off the stack. The w2 +
    >>SHRw1 w2 -- w1>>w2Two values (w1 and w2) are popped off the stack. The w2 operand is shifted right by the number of bits given by the w1 operand. The result is pushed back to the stack.
    ORORw1 w2 -- w2|w1Two values (w1 and w2) are popped off the stack. The values +
    ORORw1 w2 -- w2|w1Two values (w1 and w2) are popped off the stack. The values are bitwise OR'd together and pushed back on the stack. This is not a logical OR. The sequence 1 2 OR yields 3 not 1.
    ANDANDw1 w2 -- w2&w1Two values (w1 and w2) are popped off the stack. The values +
    ANDANDw1 w2 -- w2&w1Two values (w1 and w2) are popped off the stack. The values are bitwise AND'd together and pushed back on the stack. This is not a logical AND. The sequence 1 2 AND yields 0 not 1.
    XORXORw1 w2 -- w2^w1Two values (w1 and w2) are popped off the stack. The values +
    XORXORw1 w2 -- w2^w1Two values (w1 and w2) are popped off the stack. The values are bitwise exclusive OR'd together and pushed back on the stack. For example, The sequence 1 3 XOR yields 2.
    ARITHMETIC OPERATORS
    ARITHMETIC OPERATORS
    WordNameOperationDescription
    ABSABSw -- |w|One value s popped off the stack; its absolute value is computed + WordNameOperationDescription
    ABSABSw -- |w|One value s popped off the stack; its absolute value is computed and then pushed on to the stack. If w1 is -1 then w2 is 1. If w1 is 1 then w2 is also 1.
    NEGNEGw -- -wOne value is popped off the stack which is negated and then +
    NEGNEGw -- -wOne value is popped off the stack which is negated and then pushed back on to the stack. If w1 is -1 then w2 is 1. If w1 is 1 then w2 is -1.
    + ADDw1 w2 -- w2+w1Two values are popped off the stack. Their sum is pushed back +
    + ADDw1 w2 -- w2+w1Two values are popped off the stack. Their sum is pushed back on to the stack
    - SUBw1 w2 -- w2-w1Two values are popped off the stack. Their difference is pushed back +
    - SUBw1 w2 -- w2-w1Two values are popped off the stack. Their difference is pushed back on to the stack
    * MULw1 w2 -- w2*w1Two values are popped off the stack. Their product is pushed back +
    * MULw1 w2 -- w2*w1Two values are popped off the stack. Their product is pushed back on to the stack
    / DIVw1 w2 -- w2/w1Two values are popped off the stack. Their quotient is pushed back +
    / DIVw1 w2 -- w2/w1Two values are popped off the stack. Their quotient is pushed back on to the stack
    MODMODw1 w2 -- w2%w1Two values are popped off the stack. Their remainder after division +
    MODMODw1 w2 -- w2%w1Two values are popped off the stack. Their remainder after division of w1 by w2 is pushed back on to the stack
    */ STAR_SLAHw1 w2 w3 -- (w3*w2)/w1Three values are popped off the stack. The product of w1 and w2 is +
    */ STAR_SLAHw1 w2 w3 -- (w3*w2)/w1Three values are popped off the stack. The product of w1 and w2 is divided by w3. The result is pushed back on to the stack.
    ++ INCRw -- w+1One value is popped off the stack. It is incremented by one and then +
    ++ INCRw -- w+1One value is popped off the stack. It is incremented by one and then pushed back on to the stack.
    -- DECRw -- w-1One value is popped off the stack. It is decremented by one and then +
    -- DECRw -- w-1One value is popped off the stack. It is decremented by one and then pushed back on to the stack.
    MINMINw1 w2 -- (w2<w1?w2:w1)Two values are popped off the stack. The larger one is pushed back +
    MINMINw1 w2 -- (w2<w1?w2:w1)Two values are popped off the stack. The larger one is pushed back on to the stack.
    MAXMAXw1 w2 -- (w2>w1?w2:w1)Two values are popped off the stack. The larger value is pushed back +
    MAXMAXw1 w2 -- (w2>w1?w2:w1)Two values are popped off the stack. The larger value is pushed back on to the stack.
    STACK MANIPULATION OPERATORS
    STACK MANIPULATION OPERATORS
    WordNameOperationDescription
    DROPDROPw -- One value is popped off the stack.
    DROP2DROP2w1 w2 -- Two values are popped off the stack.
    NIPNIPw1 w2 -- w2The second value on the stack is removed from the stack. That is, + WordNameOperationDescription
    DROPDROPw -- One value is popped off the stack.
    DROP2DROP2w1 w2 -- Two values are popped off the stack.
    NIPNIPw1 w2 -- w2The second value on the stack is removed from the stack. That is, a value is popped off the stack and retained. Then a second value is popped and the retained value is pushed.
    NIP2NIP2w1 w2 w3 w4 -- w3 w4The third and fourth values on the stack are removed from it. That is, +
    NIP2NIP2w1 w2 w3 w4 -- w3 w4The third and fourth values on the stack are removed from it. That is, two values are popped and retained. Then two more values are popped and the two retained values are pushed back on.
    DUPDUPw1 -- w1 w1One value is popped off the stack. That value is then pushed on to +
    DUPDUPw1 -- w1 w1One value is popped off the stack. That value is then pushed on to the stack twice to duplicate the top stack vaue.
    DUP2DUP2w1 w2 -- w1 w2 w1 w2The top two values on the stack are duplicated. That is, two vaues +
    DUP2DUP2w1 w2 -- w1 w2 w1 w2The top two values on the stack are duplicated. That is, two vaues are popped off the stack. They are alternately pushed back on the stack twice each.
    SWAPSWAPw1 w2 -- w2 w1The top two stack items are reversed in their order. That is, two +
    SWAPSWAPw1 w2 -- w2 w1The top two stack items are reversed in their order. That is, two values are popped off the stack and pushed back on to the stack in the opposite order they were popped.
    SWAP2SWAP2w1 w2 w3 w4 -- w3 w4 w2 w1The top four stack items are swapped in pairs. That is, two values +
    SWAP2SWAP2w1 w2 w3 w4 -- w3 w4 w2 w1The top four stack items are swapped in pairs. That is, two values are popped and retained. Then, two more values are popped and retained. The values are pushed back on to the stack in the reverse order but - in pairs.

    + in pairs.
    OVEROVERw1 w2-- w1 w2 w1Two values are popped from the stack. They are pushed back +
    OVEROVERw1 w2-- w1 w2 w1Two values are popped from the stack. They are pushed back on to the stack in the order w1 w2 w1. This seems to cause the top stack element to be duplicated "over" the next value.
    OVER2OVER2w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2The third and fourth values on the stack are replicated on to the +
    OVER2OVER2w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2The third and fourth values on the stack are replicated on to the top of the stack
    ROTROTw1 w2 w3 -- w2 w3 w1The top three values are rotated. That is, three value are popped +
    ROTROTw1 w2 w3 -- w2 w3 w1The top three values are rotated. That is, three value are popped off the stack. They are pushed back on to the stack in the order w1 w3 w2.
    ROT2ROT2w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2Like ROT but the rotation is done using three pairs instead of +
    ROT2ROT2w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2Like ROT but the rotation is done using three pairs instead of three singles.
    RROTRROTw1 w2 w3 -- w2 w3 w1Reverse rotation. Like ROT, but it rotates the other way around. +
    RROTRROTw1 w2 w3 -- w3 w1 w2Reverse rotation. Like ROT, but it rotates the other way around. Essentially, the third element on the stack is moved to the top of the stack.
    RROT2RROT2w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2Double reverse rotation. Like RROT but the rotation is done using +
    RROT2RROT2w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2Double reverse rotation. Like RROT but the rotation is done using three pairs instead of three singles. The fifth and sixth stack elements are moved to the first and second positions
    TUCKTUCKw1 w2 -- w2 w1 w2Similar to OVER except that the second operand is being +
    TUCKTUCKw1 w2 -- w2 w1 w2Similar to OVER except that the second operand is being replicated. Essentially, the first operand is being "tucked" in between two instances of the second operand. Logically, two values are popped off the stack. They are placed back on the stack in the order w2 w1 w2.
    TUCK2TUCK2w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4Like TUCK but a pair of elements is tucked over two pairs. +
    TUCK2TUCK2w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4Like TUCK but a pair of elements is tucked over two pairs. That is, the top two elements of the stack are duplicated and inserted into the stack at the fifth and positions.
    PICKPICKx0 ... Xn n -- x0 ... Xn x0The top of the stack is used as an index into the remainder of +
    PICKPICKx0 ... Xn n -- x0 ... Xn x0The top of the stack is used as an index into the remainder of the stack. The element at the nth position replaces the index (top of stack). This is useful for cycling through a set of values. Note that indexing is zero based. So, if n=0 then you get the second item on the stack. If n=1 you get the third, etc. Note also that the index is replaced by the n'th value.
    SELECTSELECTm n X0..Xm Xm+1 .. Xn -- XmThis is like PICK but the list is removed and you need to specify +
    SELECTSELECTm n X0..Xm Xm+1 .. Xn -- XmThis is like PICK but the list is removed and you need to specify both the index and the size of the list. Careful with this one, the wrong value for n can blow away a huge amount of the stack.
    ROLLROLLx0 x1 .. xn n -- x1 .. xn x0Not Implemented. This one has been left as an exercise to +
    ROLLROLLx0 x1 .. xn n -- x1 .. xn x0Not Implemented. This one has been left as an exercise to the student. See Exercise. ROLL requires a value, "n", to be on the top of the stack. This value specifies how far into the stack to "roll". The n'th value is moved (not @@ -842,25 +845,25 @@ using the following construction:

    how much to rotate. That is, ROLL with n=1 is the same as ROT and ROLL with n=2 is the same as ROT2.
    MEMORY OPERATORS
    MEMORY OPERATORS
    WordNameOperationDescription
    MALLOCMALLOCw1 -- pOne value is popped off the stack. The value is used as the size + WordNameOperationDescription
    MALLOCMALLOCw1 -- pOne value is popped off the stack. The value is used as the size of a memory block to allocate. The size is in bytes, not words. The memory allocation is completed and the address of the memory block is pushed on to the stack.
    FREEFREEp -- One pointer value is popped off the stack. The value should be +
    FREEFREEp -- One pointer value is popped off the stack. The value should be the address of a memory block created by the MALLOC operation. The associated memory block is freed. Nothing is pushed back on the stack. Many bugs can be created by attempting to FREE something @@ -872,20 +875,20 @@ using the following construction:

    the stack (for the FREE at the end) and that every use of the pointer is preceded by a DUP to retain the copy for FREE.
    GETGETw1 p -- w2 pAn integer index and a pointer to a memory block are popped of +
    GETGETw1 p -- w2 pAn integer index and a pointer to a memory block are popped of the block. The index is used to index one byte from the memory block. That byte value is retained, the pointer is pushed again and the retained value is pushed. Note that the pointer value s essentially retained in its position so this doesn't count as a "use ptr" in the FREE idiom.
    PUTPUTw1 w2 p -- p An integer value is popped of the stack. This is the value to +
    PUTPUTw1 w2 p -- p An integer value is popped of the stack. This is the value to be put into a memory block. Another integer value is popped of the stack. This is the indexed byte in the memory block. A pointer to the memory block is popped off the stack. The @@ -895,33 +898,33 @@ using the following construction:

    pushed back on the stack so this doesn't count as a "use ptr" in the FREE idiom.
    CONTROL FLOW OPERATORS
    CONTROL FLOW OPERATORS
    WordNameOperationDescription
    RETURNRETURN -- The currently executing definition returns immediately to its caller. + WordNameOperationDescription
    RETURNRETURN -- The currently executing definition returns immediately to its caller. Note that there is an implicit RETURN at the end of each definition, logically located at the semi-colon. The sequence RETURN ; is valid but redundant.
    EXITEXITw1 -- A return value for the program is popped off the stack. The program is +
    EXITEXITw1 -- A return value for the program is popped off the stack. The program is then immediately terminated. This is normally an abnormal exit from the program. For a normal exit (when MAIN finishes), the exit code will always be zero in accordance with UNIX conventions.
    RECURSERECURSE -- The currently executed definition is called again. This operation is +
    RECURSERECURSE -- The currently executed definition is called again. This operation is needed since the definition of a word doesn't exist until the semi colon is reacher. Attempting something like:
    : recurser recurser ;
    will yield and error saying that @@ -929,102 +932,109 @@ using the following construction:

    to:
    : recurser RECURSE ;
    IF (words...) ENDIFIF (words...) ENDIFb -- A boolean value is popped of the stack. If it is non-zero then the "words..." +
    IF (words...) ENDIFIF (words...) ENDIFb -- A boolean value is popped of the stack. If it is non-zero then the "words..." are executed. Otherwise, execution continues immediately following the ENDIF.
    IF (words...) ELSE (words...) ENDIFIF (words...) ELSE (words...) ENDIFb -- A boolean value is popped of the stack. If it is non-zero then the "words..." +
    IF (words...) ELSE (words...) ENDIFIF (words...) ELSE (words...) ENDIFb -- A boolean value is popped of the stack. If it is non-zero then the "words..." between IF and ELSE are executed. Otherwise the words between ELSE and ENDIF are executed. In either case, after the (words....) have executed, execution continues immediately following the ENDIF.
    WHILE (words...) ENDWHILE (words...) ENDb -- b The boolean value on the top of the stack is examined. If it is non-zero then the - "words..." between WHILE and END are executed. Execution then begins again at the WHILE where another - boolean is popped off the stack. To prevent this operation from eating up the entire - stack, you should push on to the stack (just before the END) a boolean value that indicates - whether to terminate. Note that since booleans and integers can be coerced you can - use the following "for loop" idiom:
    - (push count) WHILE (words...) -- END
    +
    WHILE word ENDWHILE word ENDb -- b The boolean value on the top of the stack is examined (not popped). If + it is non-zero then the "word" between WHILE and END is executed. + Execution then begins again at the WHILE where the boolean on the top of + the stack is examined again. The stack is not modified by the WHILE...END + loop, only examined. It is imperative that the "word" in the body of the + loop ensure that the top of the stack contains the next boolean to examine + when it completes. Note that since booleans and integers can be coerced + you can use the following "for loop" idiom:
    + (push count) WHILE word -- END
    For example:
    - 10 WHILE DUP >d -- END
    - This will print the numbers from 10 down to 1. 10 is pushed on the stack. Since that is - non-zero, the while loop is entered. The top of the stack (10) is duplicated and then - printed out with >d. The top of the stack is decremented, yielding 9 and control is - transfered back to the WHILE keyword. The process starts all over again and repeats until - the top of stack is decremented to 0 at which the WHILE test fails and control is - transfered to the word after the END.
    INPUT & OUTPUT OPERATORS
    INPUT & OUTPUT OPERATORS
    WordNameOperationDescription
    SPACESPACE -- A space character is put out. There is no stack effect.
    TABTAB -- A tab character is put out. There is no stack effect.
    CRCR -- A carriage return character is put out. There is no stack effect.
    >sOUT_STR -- A string pointer is popped from the stack. It is put out.
    >dOUT_STR -- A value is popped from the stack. It is put out as a decimal integer.
    >cOUT_CHR -- A value is popped from the stack. It is put out as an ASCII character.
    <sIN_STR -- s A string is read from the input via the scanf(3) format string " %as". The - resulting string is pushed on to the stack.
    <dIN_STR -- w An integer is read from the input via the scanf(3) format string " %d". The - resulting value is pushed on to the stack
    <cIN_CHR -- w A single character is read from the input via the scanf(3) format string - " %c". The value is converted to an integer and pushed on to the stack.
    DUMPDUMP -- The stack contents are dumped to standard output. This is useful for + WordNameOperationDescription
    SPACESPACE -- A space character is put out. There is no stack effect.
    TABTAB -- A tab character is put out. There is no stack effect.
    CRCR -- A carriage return character is put out. There is no stack effect.
    >sOUT_STR -- A string pointer is popped from the stack. It is put out.
    >dOUT_STR -- A value is popped from the stack. It is put out as a decimal + integer.
    >cOUT_CHR -- A value is popped from the stack. It is put out as an ASCII + character.
    <sIN_STR -- s A string is read from the input via the scanf(3) format string " %as". + The resulting string is pushed on to the stack.
    <dIN_STR -- w An integer is read from the input via the scanf(3) format string " %d". + The resulting value is pushed on to the stack
    <cIN_CHR -- w A single character is read from the input via the scanf(3) format string + " %c". The value is converted to an integer and pushed on to the stack.
    DUMPDUMP -- The stack contents are dumped to standard output. This is useful for debugging your definitions. Put DUMP at the beginning and end of a definition to see instantly the net effect of the definition.
    + @@ -1051,7 +1061,7 @@ remainder of the story. ################################################################################ # Utility definitions ################################################################################ -: print >d CR ; +: print >d CR ; : it_is_a_prime TRUE ; : it_is_not_a_prime FALSE ; : continue_loop TRUE ; @@ -1061,10 +1071,10 @@ remainder of the story. # This definition tries an actual division of a candidate prime number. It # determines whether the division loop on this candidate should continue or # not. -# STACK<: +# STACK<: # div - the divisor to try # p - the prime number we are working on -# STACK>: +# STACK>: # cont - should we continue the loop ? # div - the next divisor to try # p - the prime number we are working on @@ -1090,7 +1100,7 @@ remainder of the story. # cont - should we continue the loop (ignored)? # div - the divisor to try # p - the prime number we are working on -# STACK>: +# STACK>: # cont - should we continue the loop ? # div - the next divisor to try # p - the prime number we are working on @@ -1115,9 +1125,9 @@ remainder of the story. # definition which returns a loop continuation value (which we also seed with # the value 1). After the loop, we check the divisor. If it decremented all # the way to zero then we found a prime, otherwise we did not find one. -# STACK<: +# STACK<: # p - the prime number to check -# STACK>: +# STACK>: # yn - boolean indicating if its a prime or not # p - the prime number checked ################################################################################ @@ -1138,18 +1148,18 @@ remainder of the story. ################################################################################ # This definition determines if the number on the top of the stack is a prime -# or not. It does this by testing if the value is degenerate (<= 3) and +# or not. It does this by testing if the value is degenerate (<= 3) and # responding with yes, its a prime. Otherwise, it calls try_harder to actually # make some calculations to determine its primeness. -# STACK<: +# STACK<: # p - the prime number to check -# STACK>: +# STACK>: # yn - boolean indicating if its a prime or not # p - the prime number checked ################################################################################ : is_prime DUP ( save the prime number ) - 3 >= IF ( see if its <= 3 ) + 3 >= IF ( see if its <= 3 ) it_is_a_prime ( its <= 3 just indicate its prime ) ELSE try_harder ( have to do a little more work ) @@ -1159,11 +1169,11 @@ remainder of the story. ################################################################################ # This definition is called when it is time to exit the program, after we have # found a sufficiently large number of primes. -# STACK<: ignored -# STACK>: exits +# STACK<: ignored +# STACK>: exits ################################################################################ : done - "Finished" >s CR ( say we are finished ) + "Finished" >s CR ( say we are finished ) 0 EXIT ( exit nicely ) ; @@ -1174,14 +1184,14 @@ remainder of the story. # If it is a prime, it prints it. Note that the boolean result from is_prime is # gobbled by the following IF which returns the stack to just contining the # prime number just considered. -# STACK<: +# STACK<: # p - one less than the prime number to consider -# STACK> +# STAC>K # p+1 - the prime number considered ################################################################################ : consider_prime DUP ( save the prime number to consider ) - 1000000 < IF ( check to see if we are done yet ) + 1000000 < IF ( check to see if we are done yet ) done ( we are done, call "done" ) ENDIF ++ ( increment to next prime number ) @@ -1195,11 +1205,11 @@ remainder of the story. # This definition starts at one, prints it out and continues into a loop calling # consider_prime on each iteration. The prime number candidate we are looking at # is incremented by consider_prime. -# STACK<: empty -# STACK>: empty +# STACK<: empty +# STACK>: empty ################################################################################ : find_primes - "Prime Numbers: " >s CR ( say hello ) + "Prime Numbers: " >s CR ( say hello ) DROP ( get rid of that pesky string ) 1 ( stoke the fires ) print ( print the first one, we know its prime ) @@ -1212,17 +1222,17 @@ remainder of the story. # ################################################################################ : say_yes - >d ( Print the prime number ) + >d ( Print the prime number ) " is prime." ( push string to output ) - >s ( output it ) + >s ( output it ) CR ( print carriage return ) DROP ( pop string ) ; : say_no - >d ( Print the prime number ) + >d ( Print the prime number ) " is NOT prime." ( push string to put out ) - >s ( put out the string ) + >s ( put out the string ) CR ( print carriage return ) DROP ( pop string ) ; @@ -1230,10 +1240,10 @@ remainder of the story. ################################################################################ # This definition processes a single command line argument and determines if it # is a prime number or not. -# STACK<: +# STACK<: # n - number of arguments # arg1 - the prime numbers to examine -# STACK>: +# STACK>: # n-1 - one less than number of arguments # arg2 - we processed one argument ################################################################################ @@ -1250,7 +1260,7 @@ remainder of the story. ################################################################################ # The MAIN program just prints a banner and processes its arguments. -# STACK<: +# STACK<: # n - number of arguments # ... - the arguments ################################################################################ @@ -1262,13 +1272,13 @@ remainder of the story. ################################################################################ # The MAIN program just prints a banner and processes its arguments. -# STACK<: arguments +# STACK<: arguments ################################################################################ : MAIN NIP ( get rid of the program name ) -- ( reduce number of arguments ) DUP ( save the arg counter ) - 1 <= IF ( See if we got an argument ) + 1 <= IF ( See if we got an argument ) process_arguments ( tell user if they are prime ) ELSE find_primes ( see how many we can find ) @@ -1288,11 +1298,16 @@ remainder of the story.

    The source code, test programs, and sample programs can all be found -under the LLVM "projects" directory. You will need to obtain the LLVM sources -to find it (either via anonymous CVS or a tarball. See the -Getting Started document).

    -

    Under the "projects" directory there is a directory named "Stacker". That -directory contains everything, as follows:

    +in the LLVM repository named llvm-stacker This should be checked out to +the projects directory so that it will auto-configure. To do that, make +sure you have the llvm sources in llvm +(see Getting Started) and then use these +commands:
    +    cd llvm/projects
    +    cvs co llvm-stacker
    +

    +

    Under the projects/llvm-stacker directory you will find the +implementation of the Stacker compiler, as follows:

    • lib - contains most of the source code
        @@ -1310,33 +1325,33 @@ directory contains everything, as follows:

        The Lexer
        -

        See projects/Stacker/lib/compiler/Lexer.l

        -

        +

        See projects/llvm-stacker/lib/compiler/Lexer.l

        +
    The Parser
    -

    See projects/Stacker/lib/compiler/StackerParser.y

    -

    +

    See projects/llvm-stacker/lib/compiler/StackerParser.y

    +
    The Compiler
    -

    See projects/Stacker/lib/compiler/StackerCompiler.cpp

    -

    +

    See projects/llvm-stacker/lib/compiler/StackerCompiler.cpp

    +
    The Runtime
    -

    See projects/Stacker/lib/runtime/stacker_rt.c

    -

    +

    See projects/llvm-stacker/lib/runtime/stacker_rt.c

    +
    Compiler Driver
    -

    See projects/Stacker/tools/stkrc/stkrc.cpp

    -

    +

    See projects/llvm-stacker/tools/stkrc/stkrc.cpp

    +
    Test Programs
    -

    See projects/Stacker/test/*.st

    -

    +

    See projects/llvm-stacker/test/*.st

    +
    @@ -1352,13 +1367,13 @@ operations. The work will almost be completely limited to the by the compiler. That means you don't have to futz around with figuring out how to get the keyword recognized. It already is. The part of the compiler that you need to implement is the ROLL case in the -StackerCompiler::handle_word(int) method.

    See the implementations -of PICk and SELECT in the same method to get some hints about how to complete -this exercise.

    +StackerCompiler::handle_word(int) method.

    See the +implementations of PICK and SELECT in the same method to get some hints about +how to complete this exercise.

    Good luck!

    - +

    The initial implementation of Stacker has several deficiencies. If you're interested, here are some things that could be implemented better:

    @@ -1366,22 +1381,15 @@ interested, here are some things that could be implemented better:

  • Write an LLVM pass to compute the correct stack depth needed by the program. Currently the stack is set to a fixed number which means programs with large numbers of definitions might fail.
  • -
  • Enhance to run on 64-bit platforms like SPARC. Right now the size of a - pointer on 64-bit machines will cause incorrect results because of the 32-bit - size of a stack element currently supported. This feature was not implemented - because LLVM needs a union type to be able to support the different sizes - correctly (portably and efficiently).
  • Write an LLVM pass to optimize the use of the global stack. The code emitted currently is somewhat wasteful. It gets cleaned up a lot by existing passes but more could be done.
  • -
  • Add -O -O1 -O2 and -O3 optimization switches to the compiler driver to - allow LLVM optimization without using "opt."
  • -
  • Make the compiler driver use the LLVM linking facilities (with IPO) before - depending on GCC to do the final link.
  • +
  • Make the compiler driver use the LLVM linking facilities (with IPO) + before depending on GCC to do the final link.
  • Clean up parsing. It doesn't handle errors very well.
  • Rearrange the StackerCompiler.cpp code to make better use of inserting instructions before a block's terminating instruction. I didn't figure this - technique out until I was nearly done with LLVM. As it is, its a bad example + technique out until I was nearly done with LLVM. As it is, its a bad example of how to insert instructions!
  • Provide for I/O to arbitrary files instead of just stdin/stdout.
  • Write additional built-in words; with inspiration from FORTH
  • @@ -1390,11 +1398,20 @@ interested, here are some things that could be implemented better:

    Lessons I Learned About LLVM section.
    - + + +
    - +
    + Valid CSS! + Valid HTML 4.01! + + Reid Spencer
    + LLVM Compiler Infrastructure
    + Last modified: $Date$ +
    +