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