blob: 383e569c2518d9c0f3c6894d21d8adadf20b8624 [file] [log] [blame]
Chris Lattnerf608e852003-11-23 17:52:55 +00001//===-- StackerCompiler.h - Interface to the Stacker Compiler ---*- C++ -*-===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnerf608e852003-11-23 17:52:55 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukmanfd939082005-04-21 23:48:37 +00005// This file was developed by Reid Spencer and donated to the LLVM research
6// group and is distributed under the University of Illinois Open Source
Chris Lattnerf608e852003-11-23 17:52:55 +00007// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00008//
Chris Lattnerf608e852003-11-23 17:52:55 +00009//===----------------------------------------------------------------------===//
10//
Misha Brukmanfd939082005-04-21 23:48:37 +000011// This header file defines the various variables that are shared among the
Chris Lattnerf608e852003-11-23 17:52:55 +000012// different components of the parser...
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_STACKERCOMPILER_H
17#define LLVM_STACKERCOMPILER_H
18
19#include <llvm/Constants.h>
20#include <llvm/DerivedTypes.h>
21#include <llvm/Function.h>
22#include <llvm/Instruction.h>
23#include <llvm/Module.h>
24#include <llvm/Assembly/Parser.h>
Brian Gaeke065b6482004-09-02 03:24:08 +000025#include <llvm/ADT/StringExtras.h>
Chris Lattnerf608e852003-11-23 17:52:55 +000026
27using namespace llvm;
28
29// Global variables exported from the lexer...
30extern std::FILE *Stackerin;
31extern int Stackerlineno;
32extern char* Stackertext;
33extern int Stackerleng;
34
Misha Brukmanfd939082005-04-21 23:48:37 +000035/// @brief This class provides the Compiler for the Stacker language.
36///
Chris Lattnerf608e852003-11-23 17:52:55 +000037/// The main method to call is \c compile. The other methods are
Misha Brukmanfd939082005-04-21 23:48:37 +000038/// all internal to the compiler and protected. In general the
Chris Lattnerf608e852003-11-23 17:52:55 +000039/// handle_* methods are called by the BISON generated parser
40/// (see StackerParser.y). The methods returning Instruction* all
41/// produce some snippet of code to manipulate the stack in some
42/// way. These functions are just conveniences as they are used
43/// often by the compiler.
44class StackerCompiler
45{
46 /// @name Constructors and Operators
47 /// @{
48 public:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000049 /// Default Constructor
50 StackerCompiler();
Chris Lattnerf608e852003-11-23 17:52:55 +000051
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000052 /// Destructor
53 ~StackerCompiler();
Chris Lattnerf608e852003-11-23 17:52:55 +000054 private:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000055 /// Do not copy StackerCompilers
56 StackerCompiler(const StackerCompiler&);
Chris Lattnerf608e852003-11-23 17:52:55 +000057
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000058 /// Do not copy StackerCompilers.
59 StackerCompiler& operator=(const StackerCompiler& );
Chris Lattnerf608e852003-11-23 17:52:55 +000060
61 /// @}
62 /// @name High Level Interface
63 /// @{
64 public:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000065 /// @brief Compile a single file to LLVM bytecode.
66 ///
67 /// To use the StackerCompiler, just create one on
68 /// the stack and call this method.
69 Module* compile(
70 const std::string& filename, ///< File to compile
71 bool echo, ///< Causes compiler to echo output
Reid Spencerc37a5062004-09-04 19:07:32 +000072 unsigned optLevel, ///< Level of optimization
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000073 size_t stack_size ); ///< Size of generated stack
Chris Lattnerf608e852003-11-23 17:52:55 +000074 /// @}
75 /// @name Accessors
76 /// @{
77 public:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000078 /// @brief Returns the name of the file being compiled.
79 std::string& filename() { return CurFilename; }
Chris Lattnerf608e852003-11-23 17:52:55 +000080
81 /// @}
82 /// @name Parse Handling Methods
83 /// @{
84 private:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000085 /// Allow only the parser to access these methods. No
86 /// one else should call them.
87 friend int Stackerparse();
Chris Lattnerf608e852003-11-23 17:52:55 +000088
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000089 /// @brief Handle the start of a module
90 Module* handle_module_start();
Chris Lattnerf608e852003-11-23 17:52:55 +000091
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000092 /// @brief Handle the end of a module
93 /// @param mod The module we're defining.
94 Module* handle_module_end( Module* mod );
Chris Lattnerf608e852003-11-23 17:52:55 +000095
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000096 /// @brief Handle the start of a list of definitions
97 Module* handle_definition_list_start( );
Chris Lattnerf608e852003-11-23 17:52:55 +000098
Jeff Cohenb02fbfc2005-04-23 21:26:11 +000099 /// @brief Handle the end of a list of definitions
100 /// @param mod The module we're constructing
101 /// @param definition A definition (function) to add to the module
102 Module* handle_definition_list_end( Module* mod, Function* definition );
Chris Lattnerf608e852003-11-23 17:52:55 +0000103
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000104 /// @brief Handle creation of the MAIN definition
105 /// @param func The function to be used as the MAIN definition
106 Function* handle_main_definition( Function* func );
Chris Lattnerf608e852003-11-23 17:52:55 +0000107
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000108 /// @brief Handle a forward definition
109 /// @param name The name of the definition being declared
110 Function* handle_forward( char* name );
Chris Lattnerf608e852003-11-23 17:52:55 +0000111
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000112 /// @brief Handle a general definition
113 /// @param name The name of the definition being defined
114 /// @param func The Function definition.
115 Function* handle_definition( char* name, Function* func );
Chris Lattnerf608e852003-11-23 17:52:55 +0000116
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000117 /// @brief Handle the start of a definition's word list
118 Function* handle_word_list_start();
Chris Lattnerf608e852003-11-23 17:52:55 +0000119
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000120 /// @brief Handle the end of a definition's word list
121 /// @param func The function to which the basic block is added
122 /// @param next The block to add to the function
123 Function* handle_word_list_end( Function* func, BasicBlock* next );
Chris Lattnerf608e852003-11-23 17:52:55 +0000124
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000125 /// @brief Handle an if statement, possibly without an else
126 /// @brief ifTrue The block to execute if true
127 /// @brief ifFalse The optional block to execute if false
128 BasicBlock* handle_if( char* ifTrue, char* ifFalse = 0 );
Chris Lattnerf608e852003-11-23 17:52:55 +0000129
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000130 /// @brief Handle a while statement
131 /// @brief todo The block to repeatedly execute
132 BasicBlock* handle_while( char* todo );
Chris Lattnerf608e852003-11-23 17:52:55 +0000133
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000134 /// @brief Handle an identifier to call the identified definition
135 /// @param name The name of the identifier to be called.
136 BasicBlock* handle_identifier( char * name );
Chris Lattnerf608e852003-11-23 17:52:55 +0000137
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000138 /// @brief Handle the push of a string onto the stack
139 /// @param value The string to be pushed.
140 BasicBlock* handle_string( char * value );
Chris Lattnerf608e852003-11-23 17:52:55 +0000141
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000142 /// @brief Handle the push of an integer onto the stack.
143 /// @param value The integer value to be pushed.
144 BasicBlock* handle_integer( const int64_t value );
Chris Lattnerf608e852003-11-23 17:52:55 +0000145
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000146 /// @brief Handle one of the reserved words (given as a token)
147 BasicBlock* handle_word( int tkn );
Chris Lattnerf608e852003-11-23 17:52:55 +0000148
149 /// @}
150 /// @name Utility functions
151 /// @{
152 public:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000153 /// @brief Throws an exception to indicate an error
154 /// @param message The message to be output
155 /// @param line Override for the current line no
156 static inline void ThrowException( const std::string &message,
157 int line = -1)
158 {
159 if (line == -1) line = Stackerlineno;
160 // TODO: column number in exception
161 throw ParseException(TheInstance->CurFilename, message, line);
162 }
Chris Lattnerf608e852003-11-23 17:52:55 +0000163 private:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000164 /// @brief Generate code to increment the stack index
165 Instruction* incr_stack_index( BasicBlock* bb, Value* );
166 /// @brief Generate code to decrement the stack index.
167 Instruction* decr_stack_index( BasicBlock* bb, Value* );
168 /// @brief Generate code to dereference the top of stack.
169 Instruction* get_stack_pointer( BasicBlock* bb, Value* );
170 /// @brief Generate code to push any value onto the stack.
171 Instruction* push_value( BasicBlock* bb, Value* value );
172 /// @brief Generate code to push a constant integer onto the stack.
173 Instruction* push_integer( BasicBlock* bb, int64_t value );
174 /// @brief Generate code to pop an integer off the stack.
175 Instruction* pop_integer( BasicBlock* bb );
176 /// @brief Generate code to push a string pointer onto the stack.
177 Instruction* push_string( BasicBlock* bb, const char* value );
178 /// @brief Generate code to pop a string pointer off the stack.
179 Instruction* pop_string( BasicBlock* bb );
180 /// @brief Generate code to get the top stack element.
181 Instruction* stack_top( BasicBlock* bb, Value* index );
182 /// @brief Generate code to get the top stack element as a string.
183 Instruction* stack_top_string( BasicBlock* bb, Value* index );
184 /// @brief Generate code to replace the top element of the stack.
185 Instruction* replace_top( BasicBlock* bb, Value* new_top, Value* index);
Chris Lattnerf608e852003-11-23 17:52:55 +0000186
187 /// @}
188 /// @name Data Members (used during parsing)
189 /// @{
190 public:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000191 static StackerCompiler* TheInstance; ///< The instance for the parser
Chris Lattnerf608e852003-11-23 17:52:55 +0000192
193 private:
Jeff Cohenb02fbfc2005-04-23 21:26:11 +0000194 std::string CurFilename; ///< Current file name
195 Module* TheModule; ///< Module instance we'll build
196 Function* TheFunction; ///< Function we're building
197 FunctionType* DefinitionType; ///< FT for Definitions
198 GlobalVariable* TheStack; ///< For referencing _stack_
199 GlobalVariable* TheIndex; ///< For referencing _index_
200 Function* TheScanf; ///< External input function
201 Function* ThePrintf; ///< External output function
202 Function* TheExit; ///< External exit function
203 GlobalVariable* StrFormat; ///< Format for strings
204 GlobalVariable* NumFormat; ///< Format for numbers
205 GlobalVariable* ChrFormat; ///< Format for chars
206 GlobalVariable* InStrFormat; ///< Format for input strings
207 GlobalVariable* InNumFormat; ///< Format for input numbers
208 GlobalVariable* InChrFormat; ///< Format for input chars
209 ConstantInt* Zero; ///< long constant 0
210 ConstantInt* One; ///< long constant 1
211 ConstantInt* Two; ///< long constant 2
212 ConstantInt* Three; ///< long constant 3
213 ConstantInt* Four; ///< long constant 4
214 ConstantInt* Five; ///< long constant 5
215 std::vector<Value*> no_arguments; ///< no arguments for Stacker
216 bool echo; ///< Echo flag
217 size_t stack_size; ///< Size of stack to gen.
218 ArrayType* stack_type; ///< The type of the stack
Chris Lattnerf608e852003-11-23 17:52:55 +0000219 /// @}
220};
221
222#endif