blob: bf9642604da957d782ecb9391b3a6dd0ddd36905 [file] [log] [blame]
Chris Lattnerf608e852003-11-23 17:52:55 +00001//===-- StackerCompiler.h - Interface to the Stacker Compiler ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// 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
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This header file defines the various variables that are shared among the
12// 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
35/// @brief This class provides the Compiler for the Stacker language.
36///
37/// The main method to call is \c compile. The other methods are
38/// all internal to the compiler and protected. In general the
39/// 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:
49 /// Default Constructor
50 StackerCompiler();
51
52 /// Destructor
53 ~StackerCompiler();
54 private:
55 /// Do not copy StackerCompilers
56 StackerCompiler(const StackerCompiler&);
57
58 /// Do not copy StackerCompilers.
59 StackerCompiler& operator=(const StackerCompiler& );
60
61 /// @}
62 /// @name High Level Interface
63 /// @{
64 public:
65 /// @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
Chris Lattnerf608e852003-11-23 17:52:55 +000073 size_t stack_size ); ///< Size of generated stack
74 /// @}
75 /// @name Accessors
76 /// @{
77 public:
78 /// @brief Returns the name of the file being compiled.
79 std::string& filename() { return CurFilename; }
80
81 /// @}
82 /// @name Parse Handling Methods
83 /// @{
84 private:
85 /// Allow only the parser to access these methods. No
86 /// one else should call them.
87 friend int Stackerparse();
88
89 /// @brief Handle the start of a module
90 Module* handle_module_start();
91
92 /// @brief Handle the end of a module
93 /// @param mod The module we're defining.
94 Module* handle_module_end( Module* mod );
95
96 /// @brief Handle the start of a list of definitions
97 Module* handle_definition_list_start( );
98
99 /// @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 );
103
104 /// @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 );
107
108 /// @brief Handle a forward definition
109 /// @param name The name of the definition being declared
110 Function* handle_forward( char* name );
111
112 /// @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 );
116
117 /// @brief Handle the start of a definition's word list
118 Function* handle_word_list_start();
119
120 /// @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 );
124
125 /// @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 );
129
130 /// @brief Handle a while statement
131 /// @brief todo The block to repeatedly execute
132 BasicBlock* handle_while( char* todo );
133
134 /// @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 );
137
138 /// @brief Handle the push of a string onto the stack
139 /// @param value The string to be pushed.
140 BasicBlock* handle_string( char * value );
141
142 /// @brief Handle the push of an integer onto the stack.
143 /// @param value The integer value to be pushed.
Reid Spencerdc8e6b52004-05-09 23:20:19 +0000144 BasicBlock* handle_integer( const int64_t value );
Chris Lattnerf608e852003-11-23 17:52:55 +0000145
146 /// @brief Handle one of the reserved words (given as a token)
147 BasicBlock* handle_word( int tkn );
148
149 /// @}
150 /// @name Utility functions
151 /// @{
152 public:
Misha Brukmane8036822004-04-15 15:38:48 +0000153 /// @brief Throws an exception to indicate an error
154 /// @param message The message to be output
Chris Lattnerf608e852003-11-23 17:52:55 +0000155 /// @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 }
163 private:
164 /// @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.
Reid Spencerdc8e6b52004-05-09 23:20:19 +0000173 Instruction* push_integer( BasicBlock* bb, int64_t value );
Chris Lattnerf608e852003-11-23 17:52:55 +0000174 /// @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);
186
187 /// @}
188 /// @name Data Members (used during parsing)
189 /// @{
190 public:
191 static StackerCompiler* TheInstance; ///< The instance for the parser
192
193 private:
194 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
Chris Lattnerf608e852003-11-23 17:52:55 +0000215 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
219 /// @}
220};
221
222#endif