Changes For Bug 352
[oota-llvm.git] / projects / Stacker / lib / compiler / StackerParser.y
1 //===-- StackerParser.y - Parser for Stacker programs -----------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the bison parser for Stacker programs.
11 //
12 //===----------------------------------------------------------------------===//
13
14 %{
15 #include "StackerCompiler.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/DepthFirstIterator.h"
21 #include <list>
22 #include <utility>
23 #include <algorithm>
24
25 #define YYERROR_VERBOSE 1
26 #define SCI StackerCompiler::TheInstance
27
28 int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
29 int yylex();                       // declaration" of xxx warnings.
30 int yyparse();
31
32 %}
33
34 %union 
35 {
36   llvm::Module*         ModuleVal;
37   llvm::Function*       FunctionVal;
38   llvm::BasicBlock*     BasicBlockVal;
39   int64_t               IntegerVal;
40   char*                 StringVal;
41 }
42
43 /* Typed Productions */
44 %type <ModuleVal>       Module DefinitionList
45 %type <FunctionVal>     Definition ForwardDef ColonDef MainDef
46 %type <FunctionVal>     WordList
47 %type <BasicBlockVal>   Word
48
49 /* Typed Tokens */
50 %token <IntegerVal>     INTEGER
51 %token <StringVal>      STRING IDENTIFIER
52
53 /* Terminal Tokens */
54 %token                  SEMI COLON FORWARD MAIN DUMP
55 %token                  TRUETOK FALSETOK LESS MORE LESS_EQUAL MORE_EQUAL NOT_EQUAL EQUAL
56 %token                  PLUS MINUS INCR DECR MULT DIV MODULUS NEGATE ABS MIN MAX STAR_SLASH 
57 %token                  AND OR XOR LSHIFT RSHIFT 
58 %token                  DROP DROP2 NIP NIP2 DUP DUP2 SWAP SWAP2 OVER OVER2 ROT ROT2 
59 %token                  RROT RROT2 TUCK TUCK2 ROLL PICK SELECT
60 %token                  MALLOC FREE GET PUT
61 %token                  IF ELSE ENDIF WHILE END RECURSE RETURN EXIT
62 %token                  TAB SPACE CR IN_STR IN_NUM IN_CHAR OUT_STR OUT_NUM OUT_CHAR
63
64 /* Start Token */
65 %start Module
66
67 %%
68
69 /* A module is just a DefinitionList */
70 Module          :                               { SCI->handle_module_start( ); } 
71                 DefinitionList                  { $$ = SCI->handle_module_end( $2 ); } ;
72
73 /* A Definitionlist is just a sequence of definitions */
74 DefinitionList  : DefinitionList Definition     { $$ = SCI->handle_definition_list_end( $1, $2 ); }
75                 | /* empty */                   { $$ = SCI->handle_definition_list_start(); } ;
76
77 /* A definition can be one of three flavors */
78 Definition      : ForwardDef                    { $$ = $1; }
79                 | ColonDef                      { $$ = $1; }
80                 | MainDef                       { $$ = $1; } ;
81
82 /* Forward definitions just introduce a name */
83 ForwardDef : FORWARD IDENTIFIER SEMI            { $$ = SCI->handle_forward( $2 ); } ;
84
85 /* The main definition has to generate additional code so we treat it specially */
86 MainDef : COLON MAIN WordList SEMI              { $$ = SCI->handle_main_definition($3); } ;
87
88 /* Regular definitions have a name and a WordList */
89 ColonDef : COLON IDENTIFIER WordList SEMI       { $$ = SCI->handle_definition( $2, $3 ); } ;
90
91 /* A WordList is just a sequence of words */
92 WordList : WordList Word                        { $$ = SCI->handle_word_list_end( $1, $2 ); } 
93          | /* empty */                          { $$ = SCI->handle_word_list_start(); } ;
94
95 /* A few "words" have a funky syntax */
96 /* FIXME: The body of compound words can currently only be function calls */
97 /* This is not acceptable, it should be a WordList, but that produces a Function */
98 /* Which is hard to merge into the function the compound statement is working on */
99 Word : IF IDENTIFIER ELSE IDENTIFIER ENDIF      { $$ = SCI->handle_if( $2, $4 ); } 
100      | IF IDENTIFIER ENDIF                      { $$ = SCI->handle_if( $2 ); } 
101      | WHILE IDENTIFIER END                     { $$ = SCI->handle_while( $2 ); } ;
102
103 /* A few words are handled specially */
104 Word : IDENTIFIER                               { $$ = SCI->handle_identifier( $1 ); } ;
105 Word : STRING                                   { $$ = SCI->handle_string( $1 ); } ;
106 Word : INTEGER                                  { $$ = SCI->handle_integer( $1 ); } ;
107
108 /* Everything else is a terminal symbol and goes to handle_word */
109 Word : TRUETOK                                  { $$ = SCI->handle_word( TRUETOK ); } ;
110 Word : FALSETOK                                 { $$ = SCI->handle_word( FALSETOK ); } ;
111 Word : LESS                                     { $$ = SCI->handle_word( LESS ); } ;
112 Word : MORE                                     { $$ = SCI->handle_word( MORE ); } ;
113 Word : LESS_EQUAL                               { $$ = SCI->handle_word( LESS_EQUAL ); } ;
114 Word : MORE_EQUAL                               { $$ = SCI->handle_word( MORE_EQUAL ); } ;
115 Word : NOT_EQUAL                                { $$ = SCI->handle_word( NOT_EQUAL ); } ;
116 Word : EQUAL                                    { $$ = SCI->handle_word( EQUAL ); } ;
117 Word : PLUS                                     { $$ = SCI->handle_word( PLUS ); } ;
118 Word : MINUS                                    { $$ = SCI->handle_word( MINUS ); } ;
119 Word : INCR                                     { $$ = SCI->handle_word( INCR ); } ;
120 Word : DECR                                     { $$ = SCI->handle_word( DECR ); } ;
121 Word : MULT                                     { $$ = SCI->handle_word( MULT ); } ;
122 Word : DIV                                      { $$ = SCI->handle_word( DIV ); } ;
123 Word : MODULUS                                  { $$ = SCI->handle_word( MODULUS ); } ;
124 Word : NEGATE                                   { $$ = SCI->handle_word( NEGATE ); } ;
125 Word : ABS                                      { $$ = SCI->handle_word( ABS ); } ;
126 Word : MIN                                      { $$ = SCI->handle_word( MIN ); } ;
127 Word : MAX                                      { $$ = SCI->handle_word( MAX ); } ;
128 Word : STAR_SLASH                               { $$ = SCI->handle_word( STAR_SLASH ); } ;
129 Word : AND                                      { $$ = SCI->handle_word( AND ); } ;
130 Word : OR                                       { $$ = SCI->handle_word( OR ); } ;
131 Word : XOR                                      { $$ = SCI->handle_word( XOR ); } ;
132 Word : LSHIFT                                   { $$ = SCI->handle_word( LSHIFT ); } ;
133 Word : RSHIFT                                   { $$ = SCI->handle_word( RSHIFT ); } ;
134 Word : DROP                                     { $$ = SCI->handle_word( DROP ); } ;
135 Word : DROP2                                    { $$ = SCI->handle_word( DROP2 ); } ;
136 Word : NIP                                      { $$ = SCI->handle_word( NIP ); } ;
137 Word : NIP2                                     { $$ = SCI->handle_word( NIP2 ); } ;
138 Word : DUP                                      { $$ = SCI->handle_word( DUP ); } ;
139 Word : DUP2                                     { $$ = SCI->handle_word( DUP2 ); } ;
140 Word : SWAP                                     { $$ = SCI->handle_word( SWAP ); } ;
141 Word : SWAP2                                    { $$ = SCI->handle_word( SWAP2 ); } ;
142 Word : OVER                                     { $$ = SCI->handle_word( OVER ); } ;
143 Word : OVER2                                    { $$ = SCI->handle_word( OVER2 ); } ;
144 Word : ROT                                      { $$ = SCI->handle_word( ROT ); } ;
145 Word : ROT2                                     { $$ = SCI->handle_word( ROT2 ); } ;
146 Word : RROT                                     { $$ = SCI->handle_word( RROT ); } ;
147 Word : RROT2                                    { $$ = SCI->handle_word( RROT2 ); } ;
148 Word : TUCK                                     { $$ = SCI->handle_word( TUCK ); } ;
149 Word : TUCK2                                    { $$ = SCI->handle_word( TUCK2 ); } ;
150 Word : ROLL                                     { $$ = SCI->handle_word( ROLL ); } ;
151 Word : PICK                                     { $$ = SCI->handle_word( PICK ); } ;
152 Word : SELECT                                   { $$ = SCI->handle_word( SELECT ); } ;
153 Word : MALLOC                                   { $$ = SCI->handle_word( MALLOC ); } ;
154 Word : FREE                                     { $$ = SCI->handle_word( FREE ); } ;
155 Word : GET                                      { $$ = SCI->handle_word( GET ); } ;
156 Word : PUT                                      { $$ = SCI->handle_word( PUT ); } ;
157 Word : RECURSE                                  { $$ = SCI->handle_word( RECURSE ); } ;
158 Word : RETURN                                   { $$ = SCI->handle_word( RETURN ); } ;
159 Word : EXIT                                     { $$ = SCI->handle_word( EXIT ); } ;
160 Word : TAB                                      { $$ = SCI->handle_word( TAB ); };
161 Word : SPACE                                    { $$ = SCI->handle_word( SPACE ); } ;
162 Word : CR                                       { $$ = SCI->handle_word( CR ); } ;
163 Word : IN_STR                                   { $$ = SCI->handle_word( IN_STR ); } ;
164 Word : IN_NUM                                   { $$ = SCI->handle_word( IN_NUM ); } ;
165 Word : IN_CHAR                                  { $$ = SCI->handle_word( IN_CHAR ); } ;
166 Word : OUT_STR                                  { $$ = SCI->handle_word( OUT_STR ); } ;
167 Word : OUT_NUM                                  { $$ = SCI->handle_word( OUT_NUM ); } ;
168 Word : OUT_CHAR                                 { $$ = SCI->handle_word( OUT_CHAR ); } ;
169 Word : DUMP                                     { $$ = SCI->handle_word( DUMP ); } ;
170
171 %%
172
173 /* Handle messages a little more nicely than the default yyerror */
174 int yyerror(const char *ErrorMsg) {
175   std::string where 
176     = std::string((SCI->filename() == "-") ? std::string("<stdin>") : SCI->filename())
177                   + ":" + utostr((unsigned) Stackerlineno ) + ": ";
178   std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
179   if (yychar == YYEMPTY)
180     errMsg += "end-of-file.";
181   else
182     errMsg += "token: '" + std::string(Stackertext, Stackerleng) + "'";
183   StackerCompiler::ThrowException(errMsg);
184   return 0;
185 }