blob: 40411d962804b7447c4ee54df9ac580bc819a405 [file] [log] [blame]
Brian Gaekec745b492003-12-10 04:33:07 +00001//===-- StackerParser.y - Parser for Stacker programs -----------*- C++ -*-===//
Chris Lattnerf608e852003-11-23 17:52:55 +00002//
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//
Brian Gaekec745b492003-12-10 04:33:07 +000010// This file implements the bison parser for Stacker programs.
Chris Lattnerf608e852003-11-23 17:52:55 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerf608e852003-11-23 17:52:55 +000014%{
15#include "StackerCompiler.h"
16#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattnerf608e852003-11-23 17:52:55 +000021#include <list>
22#include <utility>
23#include <algorithm>
24
25#define YYERROR_VERBOSE 1
26#define SCI StackerCompiler::TheInstance
27
28int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
29int yylex(); // declaration" of xxx warnings.
30int yyparse();
31
32%}
33
34%union
35{
36 llvm::Module* ModuleVal;
37 llvm::Function* FunctionVal;
38 llvm::BasicBlock* BasicBlockVal;
Reid Spencerdc8e6b52004-05-09 23:20:19 +000039 int64_t IntegerVal;
Chris Lattnerf608e852003-11-23 17:52:55 +000040 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
Chris Lattner91ef4602004-03-31 03:49:47 +000055%token TRUETOK FALSETOK LESS MORE LESS_EQUAL MORE_EQUAL NOT_EQUAL EQUAL
Chris Lattnerf608e852003-11-23 17:52:55 +000056%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 */
70Module : { SCI->handle_module_start( ); }
71 DefinitionList { $$ = SCI->handle_module_end( $2 ); } ;
72
73/* A Definitionlist is just a sequence of definitions */
74DefinitionList : 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 */
78Definition : ForwardDef { $$ = $1; }
79 | ColonDef { $$ = $1; }
80 | MainDef { $$ = $1; } ;
81
82/* Forward definitions just introduce a name */
83ForwardDef : FORWARD IDENTIFIER SEMI { $$ = SCI->handle_forward( $2 ); } ;
84
85/* The main definition has to generate additional code so we treat it specially */
86MainDef : COLON MAIN WordList SEMI { $$ = SCI->handle_main_definition($3); } ;
87
88/* Regular definitions have a name and a WordList */
89ColonDef : COLON IDENTIFIER WordList SEMI { $$ = SCI->handle_definition( $2, $3 ); } ;
90
91/* A WordList is just a sequence of words */
92WordList : WordList Word { $$ = SCI->handle_word_list_end( $1, $2 ); }
Chris Lattner1b9ce472003-12-08 20:15:33 +000093 | /* empty */ { $$ = SCI->handle_word_list_start(); } ;
Chris Lattnerf608e852003-11-23 17:52:55 +000094
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 */
99Word : 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 */
104Word : IDENTIFIER { $$ = SCI->handle_identifier( $1 ); } ;
105Word : STRING { $$ = SCI->handle_string( $1 ); } ;
106Word : INTEGER { $$ = SCI->handle_integer( $1 ); } ;
107
108/* Everything else is a terminal symbol and goes to handle_word */
Chris Lattner91ef4602004-03-31 03:49:47 +0000109Word : TRUETOK { $$ = SCI->handle_word( TRUETOK ); } ;
110Word : FALSETOK { $$ = SCI->handle_word( FALSETOK ); } ;
Chris Lattnerf608e852003-11-23 17:52:55 +0000111Word : LESS { $$ = SCI->handle_word( LESS ); } ;
112Word : MORE { $$ = SCI->handle_word( MORE ); } ;
113Word : LESS_EQUAL { $$ = SCI->handle_word( LESS_EQUAL ); } ;
114Word : MORE_EQUAL { $$ = SCI->handle_word( MORE_EQUAL ); } ;
115Word : NOT_EQUAL { $$ = SCI->handle_word( NOT_EQUAL ); } ;
116Word : EQUAL { $$ = SCI->handle_word( EQUAL ); } ;
117Word : PLUS { $$ = SCI->handle_word( PLUS ); } ;
118Word : MINUS { $$ = SCI->handle_word( MINUS ); } ;
119Word : INCR { $$ = SCI->handle_word( INCR ); } ;
120Word : DECR { $$ = SCI->handle_word( DECR ); } ;
121Word : MULT { $$ = SCI->handle_word( MULT ); } ;
122Word : DIV { $$ = SCI->handle_word( DIV ); } ;
123Word : MODULUS { $$ = SCI->handle_word( MODULUS ); } ;
124Word : NEGATE { $$ = SCI->handle_word( NEGATE ); } ;
125Word : ABS { $$ = SCI->handle_word( ABS ); } ;
126Word : MIN { $$ = SCI->handle_word( MIN ); } ;
127Word : MAX { $$ = SCI->handle_word( MAX ); } ;
128Word : STAR_SLASH { $$ = SCI->handle_word( STAR_SLASH ); } ;
129Word : AND { $$ = SCI->handle_word( AND ); } ;
130Word : OR { $$ = SCI->handle_word( OR ); } ;
131Word : XOR { $$ = SCI->handle_word( XOR ); } ;
132Word : LSHIFT { $$ = SCI->handle_word( LSHIFT ); } ;
133Word : RSHIFT { $$ = SCI->handle_word( RSHIFT ); } ;
134Word : DROP { $$ = SCI->handle_word( DROP ); } ;
135Word : DROP2 { $$ = SCI->handle_word( DROP2 ); } ;
136Word : NIP { $$ = SCI->handle_word( NIP ); } ;
137Word : NIP2 { $$ = SCI->handle_word( NIP2 ); } ;
138Word : DUP { $$ = SCI->handle_word( DUP ); } ;
139Word : DUP2 { $$ = SCI->handle_word( DUP2 ); } ;
140Word : SWAP { $$ = SCI->handle_word( SWAP ); } ;
141Word : SWAP2 { $$ = SCI->handle_word( SWAP2 ); } ;
142Word : OVER { $$ = SCI->handle_word( OVER ); } ;
143Word : OVER2 { $$ = SCI->handle_word( OVER2 ); } ;
144Word : ROT { $$ = SCI->handle_word( ROT ); } ;
145Word : ROT2 { $$ = SCI->handle_word( ROT2 ); } ;
146Word : RROT { $$ = SCI->handle_word( RROT ); } ;
147Word : RROT2 { $$ = SCI->handle_word( RROT2 ); } ;
148Word : TUCK { $$ = SCI->handle_word( TUCK ); } ;
149Word : TUCK2 { $$ = SCI->handle_word( TUCK2 ); } ;
150Word : ROLL { $$ = SCI->handle_word( ROLL ); } ;
151Word : PICK { $$ = SCI->handle_word( PICK ); } ;
152Word : SELECT { $$ = SCI->handle_word( SELECT ); } ;
153Word : MALLOC { $$ = SCI->handle_word( MALLOC ); } ;
154Word : FREE { $$ = SCI->handle_word( FREE ); } ;
155Word : GET { $$ = SCI->handle_word( GET ); } ;
156Word : PUT { $$ = SCI->handle_word( PUT ); } ;
157Word : RECURSE { $$ = SCI->handle_word( RECURSE ); } ;
158Word : RETURN { $$ = SCI->handle_word( RETURN ); } ;
159Word : EXIT { $$ = SCI->handle_word( EXIT ); } ;
160Word : TAB { $$ = SCI->handle_word( TAB ); };
161Word : SPACE { $$ = SCI->handle_word( SPACE ); } ;
162Word : CR { $$ = SCI->handle_word( CR ); } ;
163Word : IN_STR { $$ = SCI->handle_word( IN_STR ); } ;
164Word : IN_NUM { $$ = SCI->handle_word( IN_NUM ); } ;
165Word : IN_CHAR { $$ = SCI->handle_word( IN_CHAR ); } ;
166Word : OUT_STR { $$ = SCI->handle_word( OUT_STR ); } ;
167Word : OUT_NUM { $$ = SCI->handle_word( OUT_NUM ); } ;
168Word : OUT_CHAR { $$ = SCI->handle_word( OUT_CHAR ); } ;
169Word : DUMP { $$ = SCI->handle_word( DUMP ); } ;
170
171%%
172
173/* Handle messages a little more nicely than the default yyerror */
174int 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}