blob: 9b0d2ecf67c602ae29dcedad186f436d0f4ee33e [file] [log] [blame]
Lang Hames09bf4c12015-08-18 18:11:06 +00001#include "llvm/ADT/STLExtras.h"
Chandler Carruth17e0bc32015-08-06 07:33:15 +00002#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +00003#include "llvm/Analysis/Passes.h"
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +00004#include "llvm/ExecutionEngine/ExecutionEngine.h"
Eric Christopher1b74b652014-12-08 18:00:38 +00005#include "llvm/ExecutionEngine/MCJIT.h"
6#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +00007#include "llvm/IR/DataLayout.h"
8#include "llvm/IR/DerivedTypes.h"
9#include "llvm/IR/IRBuilder.h"
10#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000011#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000012#include "llvm/IR/Module.h"
Chandler Carruth20d4e6b2014-01-13 09:58:03 +000013#include "llvm/IR/Verifier.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000014#include "llvm/Support/TargetSelect.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000015#include "llvm/Transforms/Scalar.h"
Will Dietz981af002013-10-12 00:55:57 +000016#include <cctype>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000017#include <cstdio>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000018#include <map>
Chandler Carruth605e30e2012-12-04 10:16:57 +000019#include <string>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000020#include <vector>
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Lexer
25//===----------------------------------------------------------------------===//
26
27// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
28// of these for known things.
29enum Token {
30 tok_eof = -1,
31
32 // commands
Eric Christopherc0239362014-12-08 18:12:28 +000033 tok_def = -2,
34 tok_extern = -3,
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000035
36 // primary
Eric Christopherc0239362014-12-08 18:12:28 +000037 tok_identifier = -4,
38 tok_number = -5
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000039};
40
Eric Christopherc0239362014-12-08 18:12:28 +000041static std::string IdentifierStr; // Filled in if tok_identifier
42static double NumVal; // Filled in if tok_number
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000043
44/// gettok - Return the next token from standard input.
45static int gettok() {
46 static int LastChar = ' ';
47
48 // Skip any whitespace.
49 while (isspace(LastChar))
50 LastChar = getchar();
51
52 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
53 IdentifierStr = LastChar;
54 while (isalnum((LastChar = getchar())))
55 IdentifierStr += LastChar;
56
Eric Christopherc0239362014-12-08 18:12:28 +000057 if (IdentifierStr == "def")
58 return tok_def;
59 if (IdentifierStr == "extern")
60 return tok_extern;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000061 return tok_identifier;
62 }
63
Eric Christopherc0239362014-12-08 18:12:28 +000064 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000065 std::string NumStr;
66 do {
67 NumStr += LastChar;
68 LastChar = getchar();
69 } while (isdigit(LastChar) || LastChar == '.');
70
71 NumVal = strtod(NumStr.c_str(), 0);
72 return tok_number;
73 }
74
75 if (LastChar == '#') {
76 // Comment until end of line.
Eric Christopherc0239362014-12-08 18:12:28 +000077 do
78 LastChar = getchar();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000079 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
Eric Christopherc0239362014-12-08 18:12:28 +000080
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000081 if (LastChar != EOF)
82 return gettok();
83 }
Eric Christopherc0239362014-12-08 18:12:28 +000084
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000085 // Check for end of file. Don't eat the EOF.
86 if (LastChar == EOF)
87 return tok_eof;
88
89 // Otherwise, just return the character as its ascii value.
90 int ThisChar = LastChar;
91 LastChar = getchar();
92 return ThisChar;
93}
94
95//===----------------------------------------------------------------------===//
96// Abstract Syntax Tree (aka Parse Tree)
97//===----------------------------------------------------------------------===//
Juergen Ributzka05c5a932013-11-19 03:08:35 +000098namespace {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000099/// ExprAST - Base class for all expression nodes.
100class ExprAST {
101public:
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000102 virtual ~ExprAST() {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000103 virtual Value *Codegen() = 0;
104};
105
106/// NumberExprAST - Expression class for numeric literals like "1.0".
107class NumberExprAST : public ExprAST {
108 double Val;
Lang Hames59b0da82015-08-19 18:15:58 +0000109
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000110public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000111 NumberExprAST(double Val) : Val(Val) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000112 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000113};
114
115/// VariableExprAST - Expression class for referencing a variable, like "a".
116class VariableExprAST : public ExprAST {
117 std::string Name;
Lang Hames59b0da82015-08-19 18:15:58 +0000118
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000119public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000120 VariableExprAST(const std::string &Name) : Name(Name) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000121 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000122};
123
124/// BinaryExprAST - Expression class for a binary operator.
125class BinaryExprAST : public ExprAST {
126 char Op;
Lang Hames09bf4c12015-08-18 18:11:06 +0000127 std::unique_ptr<ExprAST> LHS, RHS;
Lang Hames59b0da82015-08-19 18:15:58 +0000128
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000129public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000130 BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
131 std::unique_ptr<ExprAST> RHS)
132 : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000133 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000134};
135
136/// CallExprAST - Expression class for function calls.
137class CallExprAST : public ExprAST {
138 std::string Callee;
Lang Hames09bf4c12015-08-18 18:11:06 +0000139 std::vector<std::unique_ptr<ExprAST>> Args;
Lang Hames59b0da82015-08-19 18:15:58 +0000140
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000141public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000142 CallExprAST(const std::string &Callee,
143 std::vector<std::unique_ptr<ExprAST>> Args)
144 : Callee(Callee), Args(std::move(Args)) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000145 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000146};
147
148/// PrototypeAST - This class represents the "prototype" for a function,
149/// which captures its name, and its argument names (thus implicitly the number
150/// of arguments the function takes).
151class PrototypeAST {
152 std::string Name;
153 std::vector<std::string> Args;
Lang Hames59b0da82015-08-19 18:15:58 +0000154
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000155public:
Lang Hames59b0da82015-08-19 18:15:58 +0000156 PrototypeAST(const std::string &Name, std::vector<std::string> Args)
157 : Name(Name), Args(std::move(Args)) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000158 Function *Codegen();
159};
160
161/// FunctionAST - This class represents a function definition itself.
162class FunctionAST {
Lang Hames09bf4c12015-08-18 18:11:06 +0000163 std::unique_ptr<PrototypeAST> Proto;
164 std::unique_ptr<ExprAST> Body;
Lang Hames59b0da82015-08-19 18:15:58 +0000165
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000166public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000167 FunctionAST(std::unique_ptr<PrototypeAST> Proto,
168 std::unique_ptr<ExprAST> Body)
Lang Hames59b0da82015-08-19 18:15:58 +0000169 : Proto(std::move(Proto)), Body(std::move(Body)) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000170 Function *Codegen();
171};
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000172} // end anonymous namespace
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000173
174//===----------------------------------------------------------------------===//
175// Parser
176//===----------------------------------------------------------------------===//
177
178/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
179/// token the parser is looking at. getNextToken reads another token from the
180/// lexer and updates CurTok with its results.
181static int CurTok;
Eric Christopherc0239362014-12-08 18:12:28 +0000182static int getNextToken() { return CurTok = gettok(); }
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000183
184/// BinopPrecedence - This holds the precedence for each binary operator that is
185/// defined.
186static std::map<char, int> BinopPrecedence;
187
188/// GetTokPrecedence - Get the precedence of the pending binary operator token.
189static int GetTokPrecedence() {
190 if (!isascii(CurTok))
191 return -1;
Eric Christopherc0239362014-12-08 18:12:28 +0000192
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000193 // Make sure it's a declared binop.
194 int TokPrec = BinopPrecedence[CurTok];
Eric Christopherc0239362014-12-08 18:12:28 +0000195 if (TokPrec <= 0)
196 return -1;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000197 return TokPrec;
198}
199
200/// Error* - These are little helper functions for error handling.
Lang Hames09bf4c12015-08-18 18:11:06 +0000201std::unique_ptr<ExprAST> Error(const char *Str) {
Eric Christopherc0239362014-12-08 18:12:28 +0000202 fprintf(stderr, "Error: %s\n", Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000203 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000204}
Lang Hames09bf4c12015-08-18 18:11:06 +0000205std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
Eric Christopherc0239362014-12-08 18:12:28 +0000206 Error(Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000207 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000208}
Lang Hames09bf4c12015-08-18 18:11:06 +0000209std::unique_ptr<FunctionAST> ErrorF(const char *Str) {
Eric Christopherc0239362014-12-08 18:12:28 +0000210 Error(Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000211 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000212}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000213
Lang Hames09bf4c12015-08-18 18:11:06 +0000214static std::unique_ptr<ExprAST> ParseExpression();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000215
Lang Hames59b0da82015-08-19 18:15:58 +0000216/// numberexpr ::= number
217static std::unique_ptr<ExprAST> ParseNumberExpr() {
218 auto Result = llvm::make_unique<NumberExprAST>(NumVal);
219 getNextToken(); // consume the number
220 return std::move(Result);
221}
222
223/// parenexpr ::= '(' expression ')'
224static std::unique_ptr<ExprAST> ParseParenExpr() {
225 getNextToken(); // eat (.
226 auto V = ParseExpression();
227 if (!V)
228 return nullptr;
229
230 if (CurTok != ')')
231 return Error("expected ')'");
232 getNextToken(); // eat ).
233 return V;
234}
235
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000236/// identifierexpr
237/// ::= identifier
238/// ::= identifier '(' expression* ')'
Lang Hames09bf4c12015-08-18 18:11:06 +0000239static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000240 std::string IdName = IdentifierStr;
Eric Christopherc0239362014-12-08 18:12:28 +0000241
242 getNextToken(); // eat identifier.
243
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000244 if (CurTok != '(') // Simple variable ref.
Lang Hames09bf4c12015-08-18 18:11:06 +0000245 return llvm::make_unique<VariableExprAST>(IdName);
Eric Christopherc0239362014-12-08 18:12:28 +0000246
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000247 // Call.
Eric Christopherc0239362014-12-08 18:12:28 +0000248 getNextToken(); // eat (
Lang Hames09bf4c12015-08-18 18:11:06 +0000249 std::vector<std::unique_ptr<ExprAST>> Args;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000250 if (CurTok != ')') {
251 while (1) {
Lang Hames09bf4c12015-08-18 18:11:06 +0000252 if (auto Arg = ParseExpression())
253 Args.push_back(std::move(Arg));
254 else
255 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000256
Eric Christopherc0239362014-12-08 18:12:28 +0000257 if (CurTok == ')')
258 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000259
260 if (CurTok != ',')
261 return Error("Expected ')' or ',' in argument list");
262 getNextToken();
263 }
264 }
265
266 // Eat the ')'.
267 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000268
Lang Hames09bf4c12015-08-18 18:11:06 +0000269 return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000270}
271
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000272/// primary
273/// ::= identifierexpr
274/// ::= numberexpr
275/// ::= parenexpr
Lang Hames09bf4c12015-08-18 18:11:06 +0000276static std::unique_ptr<ExprAST> ParsePrimary() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000277 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000278 default:
279 return Error("unknown token when expecting an expression");
280 case tok_identifier:
281 return ParseIdentifierExpr();
282 case tok_number:
283 return ParseNumberExpr();
284 case '(':
285 return ParseParenExpr();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000286 }
287}
288
289/// binoprhs
290/// ::= ('+' primary)*
Lang Hames09bf4c12015-08-18 18:11:06 +0000291static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
292 std::unique_ptr<ExprAST> LHS) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000293 // If this is a binop, find its precedence.
294 while (1) {
295 int TokPrec = GetTokPrecedence();
Eric Christopherc0239362014-12-08 18:12:28 +0000296
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000297 // If this is a binop that binds at least as tightly as the current binop,
298 // consume it, otherwise we are done.
299 if (TokPrec < ExprPrec)
300 return LHS;
Eric Christopherc0239362014-12-08 18:12:28 +0000301
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000302 // Okay, we know this is a binop.
303 int BinOp = CurTok;
Eric Christopherc0239362014-12-08 18:12:28 +0000304 getNextToken(); // eat binop
305
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000306 // Parse the primary expression after the binary operator.
Lang Hames09bf4c12015-08-18 18:11:06 +0000307 auto RHS = ParsePrimary();
Eric Christopherc0239362014-12-08 18:12:28 +0000308 if (!RHS)
Lang Hames09bf4c12015-08-18 18:11:06 +0000309 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000310
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000311 // If BinOp binds less tightly with RHS than the operator after RHS, let
312 // the pending operator take RHS as its LHS.
313 int NextPrec = GetTokPrecedence();
314 if (TokPrec < NextPrec) {
Lang Hames09bf4c12015-08-18 18:11:06 +0000315 RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
316 if (!RHS)
317 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000318 }
Eric Christopherc0239362014-12-08 18:12:28 +0000319
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000320 // Merge LHS/RHS.
Lang Hames59b0da82015-08-19 18:15:58 +0000321 LHS =
322 llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000323 }
324}
325
326/// expression
327/// ::= primary binoprhs
328///
Lang Hames09bf4c12015-08-18 18:11:06 +0000329static std::unique_ptr<ExprAST> ParseExpression() {
330 auto LHS = ParsePrimary();
Eric Christopherc0239362014-12-08 18:12:28 +0000331 if (!LHS)
Lang Hames09bf4c12015-08-18 18:11:06 +0000332 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000333
Lang Hames09bf4c12015-08-18 18:11:06 +0000334 return ParseBinOpRHS(0, std::move(LHS));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000335}
336
337/// prototype
338/// ::= id '(' id* ')'
Lang Hames09bf4c12015-08-18 18:11:06 +0000339static std::unique_ptr<PrototypeAST> ParsePrototype() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000340 if (CurTok != tok_identifier)
341 return ErrorP("Expected function name in prototype");
342
343 std::string FnName = IdentifierStr;
344 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000345
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000346 if (CurTok != '(')
347 return ErrorP("Expected '(' in prototype");
Eric Christopherc0239362014-12-08 18:12:28 +0000348
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000349 std::vector<std::string> ArgNames;
350 while (getNextToken() == tok_identifier)
351 ArgNames.push_back(IdentifierStr);
352 if (CurTok != ')')
353 return ErrorP("Expected ')' in prototype");
Eric Christopherc0239362014-12-08 18:12:28 +0000354
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000355 // success.
Eric Christopherc0239362014-12-08 18:12:28 +0000356 getNextToken(); // eat ')'.
357
Lang Hames09bf4c12015-08-18 18:11:06 +0000358 return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000359}
360
361/// definition ::= 'def' prototype expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000362static std::unique_ptr<FunctionAST> ParseDefinition() {
Eric Christopherc0239362014-12-08 18:12:28 +0000363 getNextToken(); // eat def.
Lang Hames09bf4c12015-08-18 18:11:06 +0000364 auto Proto = ParsePrototype();
365 if (!Proto)
366 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000367
Lang Hames09bf4c12015-08-18 18:11:06 +0000368 if (auto E = ParseExpression())
369 return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
370 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000371}
372
373/// toplevelexpr ::= expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000374static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
375 if (auto E = ParseExpression()) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000376 // Make an anonymous proto.
Lang Hames59b0da82015-08-19 18:15:58 +0000377 auto Proto =
378 llvm::make_unique<PrototypeAST>("", std::vector<std::string>());
Lang Hames09bf4c12015-08-18 18:11:06 +0000379 return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000380 }
Lang Hames09bf4c12015-08-18 18:11:06 +0000381 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000382}
383
384/// external ::= 'extern' prototype
Lang Hames09bf4c12015-08-18 18:11:06 +0000385static std::unique_ptr<PrototypeAST> ParseExtern() {
Eric Christopherc0239362014-12-08 18:12:28 +0000386 getNextToken(); // eat extern.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000387 return ParsePrototype();
388}
389
390//===----------------------------------------------------------------------===//
Lang Hames1cb54812015-01-16 19:44:46 +0000391// Quick and dirty hack
392//===----------------------------------------------------------------------===//
393
394// FIXME: Obviously we can do better than this
Lang Hames37679192015-01-16 21:42:07 +0000395std::string GenerateUniqueName(const char *root) {
Lang Hames1cb54812015-01-16 19:44:46 +0000396 static int i = 0;
397 char s[16];
398 sprintf(s, "%s%d", root, i++);
399 std::string S = s;
400 return S;
401}
402
Lang Hames37679192015-01-16 21:42:07 +0000403std::string MakeLegalFunctionName(std::string Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000404 std::string NewName;
405 if (!Name.length())
Lang Hames37679192015-01-16 21:42:07 +0000406 return GenerateUniqueName("anon_func_");
Lang Hames1cb54812015-01-16 19:44:46 +0000407
408 // Start with what we have
409 NewName = Name;
410
411 // Look for a numberic first character
412 if (NewName.find_first_of("0123456789") == 0) {
413 NewName.insert(0, 1, 'n');
414 }
415
416 // Replace illegal characters with their ASCII equivalent
Lang Hames37679192015-01-16 21:42:07 +0000417 std::string legal_elements =
418 "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Lang Hames1cb54812015-01-16 19:44:46 +0000419 size_t pos;
Lang Hames37679192015-01-16 21:42:07 +0000420 while ((pos = NewName.find_first_not_of(legal_elements)) !=
421 std::string::npos) {
Lang Hames1cb54812015-01-16 19:44:46 +0000422 char old_c = NewName.at(pos);
423 char new_str[16];
424 sprintf(new_str, "%d", (int)old_c);
425 NewName = NewName.replace(pos, 1, new_str);
426 }
427
428 return NewName;
429}
430
431//===----------------------------------------------------------------------===//
432// MCJIT helper class
433//===----------------------------------------------------------------------===//
434
Lang Hames37679192015-01-16 21:42:07 +0000435class MCJITHelper {
Lang Hames1cb54812015-01-16 19:44:46 +0000436public:
Lang Hames37679192015-01-16 21:42:07 +0000437 MCJITHelper(LLVMContext &C) : Context(C), OpenModule(NULL) {}
Lang Hames1cb54812015-01-16 19:44:46 +0000438 ~MCJITHelper();
439
440 Function *getFunction(const std::string FnName);
441 Module *getModuleForNewFunction();
Lang Hames37679192015-01-16 21:42:07 +0000442 void *getPointerToFunction(Function *F);
Lang Hames1cb54812015-01-16 19:44:46 +0000443 void *getSymbolAddress(const std::string &Name);
444 void dump();
445
446private:
Lang Hames37679192015-01-16 21:42:07 +0000447 typedef std::vector<Module *> ModuleVector;
448 typedef std::vector<ExecutionEngine *> EngineVector;
Lang Hames1cb54812015-01-16 19:44:46 +0000449
Lang Hames37679192015-01-16 21:42:07 +0000450 LLVMContext &Context;
451 Module *OpenModule;
452 ModuleVector Modules;
453 EngineVector Engines;
Lang Hames1cb54812015-01-16 19:44:46 +0000454};
455
Lang Hames37679192015-01-16 21:42:07 +0000456class HelpingMemoryManager : public SectionMemoryManager {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000457 HelpingMemoryManager(const HelpingMemoryManager &) = delete;
458 void operator=(const HelpingMemoryManager &) = delete;
Lang Hames1cb54812015-01-16 19:44:46 +0000459
460public:
461 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000462 ~HelpingMemoryManager() override {}
Lang Hames1cb54812015-01-16 19:44:46 +0000463
464 /// This method returns the address of the specified symbol.
465 /// Our implementation will attempt to find symbols in other
466 /// modules associated with the MCJITHelper to cross link symbols
467 /// from one generated module to another.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000468 uint64_t getSymbolAddress(const std::string &Name) override;
Lang Hames37679192015-01-16 21:42:07 +0000469
Lang Hames1cb54812015-01-16 19:44:46 +0000470private:
471 MCJITHelper *MasterHelper;
472};
473
Lang Hames37679192015-01-16 21:42:07 +0000474uint64_t HelpingMemoryManager::getSymbolAddress(const std::string &Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000475 uint64_t FnAddr = SectionMemoryManager::getSymbolAddress(Name);
476 if (FnAddr)
477 return FnAddr;
478
Lang Hames37679192015-01-16 21:42:07 +0000479 uint64_t HelperFun = (uint64_t)MasterHelper->getSymbolAddress(Name);
Lang Hames1cb54812015-01-16 19:44:46 +0000480 if (!HelperFun)
481 report_fatal_error("Program used extern function '" + Name +
482 "' which could not be resolved!");
483
484 return HelperFun;
485}
486
Lang Hames37679192015-01-16 21:42:07 +0000487MCJITHelper::~MCJITHelper() {
Lang Hames1cb54812015-01-16 19:44:46 +0000488 if (OpenModule)
489 delete OpenModule;
490 EngineVector::iterator begin = Engines.begin();
491 EngineVector::iterator end = Engines.end();
492 EngineVector::iterator it;
493 for (it = begin; it != end; ++it)
494 delete *it;
495}
496
497Function *MCJITHelper::getFunction(const std::string FnName) {
498 ModuleVector::iterator begin = Modules.begin();
499 ModuleVector::iterator end = Modules.end();
500 ModuleVector::iterator it;
501 for (it = begin; it != end; ++it) {
502 Function *F = (*it)->getFunction(FnName);
503 if (F) {
504 if (*it == OpenModule)
Lang Hames37679192015-01-16 21:42:07 +0000505 return F;
Lang Hames1cb54812015-01-16 19:44:46 +0000506
507 assert(OpenModule != NULL);
508
509 // This function is in a module that has already been JITed.
510 // We need to generate a new prototype for external linkage.
511 Function *PF = OpenModule->getFunction(FnName);
512 if (PF && !PF->empty()) {
513 ErrorF("redefinition of function across modules");
Lang Hames09bf4c12015-08-18 18:11:06 +0000514 return nullptr;
Lang Hames1cb54812015-01-16 19:44:46 +0000515 }
516
517 // If we don't have a prototype yet, create one.
518 if (!PF)
Lang Hames37679192015-01-16 21:42:07 +0000519 PF = Function::Create(F->getFunctionType(), Function::ExternalLinkage,
520 FnName, OpenModule);
Lang Hames1cb54812015-01-16 19:44:46 +0000521 return PF;
522 }
523 }
524 return NULL;
525}
526
527Module *MCJITHelper::getModuleForNewFunction() {
528 // If we have a Module that hasn't been JITed, use that.
529 if (OpenModule)
530 return OpenModule;
531
532 // Otherwise create a new Module.
533 std::string ModName = GenerateUniqueName("mcjit_module_");
534 Module *M = new Module(ModName, Context);
535 Modules.push_back(M);
536 OpenModule = M;
537 return M;
538}
539
Lang Hames37679192015-01-16 21:42:07 +0000540void *MCJITHelper::getPointerToFunction(Function *F) {
Lang Hames1cb54812015-01-16 19:44:46 +0000541 // See if an existing instance of MCJIT has this function.
542 EngineVector::iterator begin = Engines.begin();
543 EngineVector::iterator end = Engines.end();
544 EngineVector::iterator it;
545 for (it = begin; it != end; ++it) {
546 void *P = (*it)->getPointerToFunction(F);
547 if (P)
548 return P;
549 }
550
551 // If we didn't find the function, see if we can generate it.
552 if (OpenModule) {
553 std::string ErrStr;
Lang Hames37679192015-01-16 21:42:07 +0000554 ExecutionEngine *NewEngine =
555 EngineBuilder(std::unique_ptr<Module>(OpenModule))
556 .setErrorStr(&ErrStr)
557 .setMCJITMemoryManager(std::unique_ptr<HelpingMemoryManager>(
558 new HelpingMemoryManager(this)))
559 .create();
Lang Hames1cb54812015-01-16 19:44:46 +0000560 if (!NewEngine) {
561 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
562 exit(1);
563 }
564
565 // Create a function pass manager for this engine
Chandler Carruth7ecd9912015-02-13 10:21:05 +0000566 auto *FPM = new legacy::FunctionPassManager(OpenModule);
Lang Hames1cb54812015-01-16 19:44:46 +0000567
568 // Set up the optimizer pipeline. Start with registering info about how the
569 // target lays out data structures.
Mehdi Aminicd253da2015-07-16 16:47:18 +0000570 OpenModule->setDataLayout(NewEngine->getDataLayout());
Lang Hames1cb54812015-01-16 19:44:46 +0000571 // Provide basic AliasAnalysis support for GVN.
572 FPM->add(createBasicAliasAnalysisPass());
573 // Promote allocas to registers.
574 FPM->add(createPromoteMemoryToRegisterPass());
575 // Do simple "peephole" optimizations and bit-twiddling optzns.
576 FPM->add(createInstructionCombiningPass());
577 // Reassociate expressions.
578 FPM->add(createReassociatePass());
579 // Eliminate Common SubExpressions.
580 FPM->add(createGVNPass());
581 // Simplify the control flow graph (deleting unreachable blocks, etc).
582 FPM->add(createCFGSimplificationPass());
583 FPM->doInitialization();
584
585 // For each function in the module
586 Module::iterator it;
587 Module::iterator end = OpenModule->end();
588 for (it = OpenModule->begin(); it != end; ++it) {
589 // Run the FPM on this function
590 FPM->run(*it);
591 }
592
593 // We don't need this anymore
594 delete FPM;
595
596 OpenModule = NULL;
597 Engines.push_back(NewEngine);
598 NewEngine->finalizeObject();
599 return NewEngine->getPointerToFunction(F);
600 }
601 return NULL;
602}
603
Lang Hames37679192015-01-16 21:42:07 +0000604void *MCJITHelper::getSymbolAddress(const std::string &Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000605 // Look for the symbol in each of our execution engines.
606 EngineVector::iterator begin = Engines.begin();
607 EngineVector::iterator end = Engines.end();
608 EngineVector::iterator it;
609 for (it = begin; it != end; ++it) {
610 uint64_t FAddr = (*it)->getFunctionAddress(Name);
611 if (FAddr) {
Lang Hames37679192015-01-16 21:42:07 +0000612 return (void *)FAddr;
Lang Hames1cb54812015-01-16 19:44:46 +0000613 }
614 }
615 return NULL;
616}
617
Lang Hames37679192015-01-16 21:42:07 +0000618void MCJITHelper::dump() {
Lang Hames1cb54812015-01-16 19:44:46 +0000619 ModuleVector::iterator begin = Modules.begin();
620 ModuleVector::iterator end = Modules.end();
621 ModuleVector::iterator it;
622 for (it = begin; it != end; ++it)
623 (*it)->dump();
624}
625//===----------------------------------------------------------------------===//
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000626// Code Generation
627//===----------------------------------------------------------------------===//
628
Lang Hames1cb54812015-01-16 19:44:46 +0000629static MCJITHelper *JITHelper;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000630static IRBuilder<> Builder(getGlobalContext());
Eric Christopherc0239362014-12-08 18:12:28 +0000631static std::map<std::string, Value *> NamedValues;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000632
Eric Christopherc0239362014-12-08 18:12:28 +0000633Value *ErrorV(const char *Str) {
634 Error(Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000635 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000636}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000637
638Value *NumberExprAST::Codegen() {
639 return ConstantFP::get(getGlobalContext(), APFloat(Val));
640}
641
642Value *VariableExprAST::Codegen() {
643 // Look this variable up in the function.
644 Value *V = NamedValues[Name];
Lang Hames596aec92015-08-19 18:32:58 +0000645 if (!V)
646 return ErrorV("Unknown variable name");
647 return V;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000648}
649
650Value *BinaryExprAST::Codegen() {
651 Value *L = LHS->Codegen();
652 Value *R = RHS->Codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +0000653 if (!L || !R)
654 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000655
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000656 switch (Op) {
Eric Christopherc0239362014-12-08 18:12:28 +0000657 case '+':
658 return Builder.CreateFAdd(L, R, "addtmp");
659 case '-':
660 return Builder.CreateFSub(L, R, "subtmp");
661 case '*':
662 return Builder.CreateFMul(L, R, "multmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000663 case '<':
664 L = Builder.CreateFCmpULT(L, R, "cmptmp");
665 // Convert bool 0/1 to double 0.0 or 1.0
666 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
667 "booltmp");
Eric Christopherc0239362014-12-08 18:12:28 +0000668 default:
669 return ErrorV("invalid binary operator");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000670 }
671}
672
673Value *CallExprAST::Codegen() {
674 // Look up the name in the global module table.
Lang Hames1cb54812015-01-16 19:44:46 +0000675 Function *CalleeF = JITHelper->getFunction(Callee);
Lang Hames09bf4c12015-08-18 18:11:06 +0000676 if (!CalleeF)
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000677 return ErrorV("Unknown function referenced");
Eric Christopherc0239362014-12-08 18:12:28 +0000678
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000679 // If argument mismatch error.
680 if (CalleeF->arg_size() != Args.size())
681 return ErrorV("Incorrect # arguments passed");
682
Eric Christopherc0239362014-12-08 18:12:28 +0000683 std::vector<Value *> ArgsV;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000684 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
685 ArgsV.push_back(Args[i]->Codegen());
Lang Hames09bf4c12015-08-18 18:11:06 +0000686 if (!ArgsV.back())
687 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000688 }
Eric Christopherc0239362014-12-08 18:12:28 +0000689
Francois Pichetc5d10502011-07-15 10:59:52 +0000690 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000691}
692
693Function *PrototypeAST::Codegen() {
694 // Make the function type: double(double,double) etc.
Eric Christopherc0239362014-12-08 18:12:28 +0000695 std::vector<Type *> Doubles(Args.size(),
696 Type::getDoubleTy(getGlobalContext()));
697 FunctionType *FT =
698 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
699
Lang Hames1cb54812015-01-16 19:44:46 +0000700 std::string FnName = MakeLegalFunctionName(Name);
701
702 Module *M = JITHelper->getModuleForNewFunction();
703
Lang Hames37679192015-01-16 21:42:07 +0000704 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
Eric Christopherc0239362014-12-08 18:12:28 +0000705
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000706 // If F conflicted, there was already something named 'Name'. If it has a
707 // body, don't allow redefinition or reextern.
Lang Hames1cb54812015-01-16 19:44:46 +0000708 if (F->getName() != FnName) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000709 // Delete the one we just made and get the existing one.
710 F->eraseFromParent();
Lang Hames37679192015-01-16 21:42:07 +0000711 F = JITHelper->getFunction(Name);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000712 // If F already has a body, reject this.
713 if (!F->empty()) {
714 ErrorF("redefinition of function");
Lang Hames09bf4c12015-08-18 18:11:06 +0000715 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000716 }
Eric Christopherc0239362014-12-08 18:12:28 +0000717
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000718 // If F took a different number of args, reject.
719 if (F->arg_size() != Args.size()) {
720 ErrorF("redefinition of function with different # args");
Lang Hames09bf4c12015-08-18 18:11:06 +0000721 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000722 }
723 }
Eric Christopherc0239362014-12-08 18:12:28 +0000724
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000725 // Set names for all arguments.
726 unsigned Idx = 0;
727 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
728 ++AI, ++Idx) {
729 AI->setName(Args[Idx]);
Eric Christopherc0239362014-12-08 18:12:28 +0000730
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000731 // Add arguments to variable symbol table.
732 NamedValues[Args[Idx]] = AI;
733 }
Eric Christopherc0239362014-12-08 18:12:28 +0000734
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000735 return F;
736}
737
738Function *FunctionAST::Codegen() {
739 NamedValues.clear();
Eric Christopherc0239362014-12-08 18:12:28 +0000740
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000741 Function *TheFunction = Proto->Codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +0000742 if (!TheFunction)
743 return nullptr;
Eric Christopherc0239362014-12-08 18:12:28 +0000744
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000745 // Create a new basic block to start insertion into.
746 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
747 Builder.SetInsertPoint(BB);
Eric Christopherc0239362014-12-08 18:12:28 +0000748
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000749 if (Value *RetVal = Body->Codegen()) {
750 // Finish off the function.
751 Builder.CreateRet(RetVal);
752
753 // Validate the generated code, checking for consistency.
754 verifyFunction(*TheFunction);
755
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000756 return TheFunction;
757 }
Eric Christopherc0239362014-12-08 18:12:28 +0000758
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000759 // Error reading body, remove function.
760 TheFunction->eraseFromParent();
Lang Hames09bf4c12015-08-18 18:11:06 +0000761 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000762}
763
764//===----------------------------------------------------------------------===//
765// Top-Level parsing and JIT Driver
766//===----------------------------------------------------------------------===//
767
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000768static void HandleDefinition() {
Lang Hames09bf4c12015-08-18 18:11:06 +0000769 if (auto FnAST = ParseDefinition()) {
770 if (auto *FnIR = FnAST->Codegen()) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000771 fprintf(stderr, "Read function definition:");
Lang Hames09bf4c12015-08-18 18:11:06 +0000772 FnIR->dump();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000773 }
774 } else {
775 // Skip token for error recovery.
776 getNextToken();
777 }
778}
779
780static void HandleExtern() {
Lang Hames09bf4c12015-08-18 18:11:06 +0000781 if (auto ProtoAST = ParseExtern()) {
782 if (auto *FnIR = ProtoAST->Codegen()) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000783 fprintf(stderr, "Read extern: ");
Lang Hames09bf4c12015-08-18 18:11:06 +0000784 FnIR->dump();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000785 }
786 } else {
787 // Skip token for error recovery.
788 getNextToken();
789 }
790}
791
792static void HandleTopLevelExpression() {
793 // Evaluate a top-level expression into an anonymous function.
Lang Hames09bf4c12015-08-18 18:11:06 +0000794 if (auto FnAST = ParseTopLevelExpr()) {
795 if (auto *FnIR = FnAST->Codegen()) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000796 // JIT the function, returning a function pointer.
Lang Hames09bf4c12015-08-18 18:11:06 +0000797 void *FPtr = JITHelper->getPointerToFunction(FnIR);
Eric Christopherc0239362014-12-08 18:12:28 +0000798
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000799 // Cast it to the right type (takes no arguments, returns a double) so we
800 // can call it as a native function.
801 double (*FP)() = (double (*)())(intptr_t)FPtr;
802 fprintf(stderr, "Evaluated to %f\n", FP());
803 }
804 } else {
805 // Skip token for error recovery.
806 getNextToken();
807 }
808}
809
810/// top ::= definition | external | expression | ';'
811static void MainLoop() {
812 while (1) {
813 fprintf(stderr, "ready> ");
814 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000815 case tok_eof:
816 return;
Lang Hames59b0da82015-08-19 18:15:58 +0000817 case ';': // ignore top-level semicolons.
Eric Christopherc0239362014-12-08 18:12:28 +0000818 getNextToken();
Lang Hames59b0da82015-08-19 18:15:58 +0000819 break;
Eric Christopherc0239362014-12-08 18:12:28 +0000820 case tok_def:
821 HandleDefinition();
822 break;
823 case tok_extern:
824 HandleExtern();
825 break;
826 default:
827 HandleTopLevelExpression();
828 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000829 }
830 }
831}
832
833//===----------------------------------------------------------------------===//
834// "Library" functions that can be "extern'd" from user code.
835//===----------------------------------------------------------------------===//
836
837/// putchard - putchar that takes a double and returns 0.
Eric Christopherc0239362014-12-08 18:12:28 +0000838extern "C" double putchard(double X) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000839 putchar((char)X);
840 return 0;
841}
842
Lang Hames59b0da82015-08-19 18:15:58 +0000843/// printd - printf that takes a double prints it as "%f\n", returning 0.
844extern "C" double printd(double X) {
845 printf("%f\n", X);
846 return 0;
847}
848
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000849//===----------------------------------------------------------------------===//
850// Main driver code.
851//===----------------------------------------------------------------------===//
852
853int main() {
854 InitializeNativeTarget();
Eric Christopher1b74b652014-12-08 18:00:38 +0000855 InitializeNativeTargetAsmPrinter();
856 InitializeNativeTargetAsmParser();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000857 LLVMContext &Context = getGlobalContext();
Lang Hames1cb54812015-01-16 19:44:46 +0000858 JITHelper = new MCJITHelper(Context);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000859
860 // Install standard binary operators.
861 // 1 is lowest precedence.
862 BinopPrecedence['<'] = 10;
863 BinopPrecedence['+'] = 20;
864 BinopPrecedence['-'] = 20;
Eric Christopherc0239362014-12-08 18:12:28 +0000865 BinopPrecedence['*'] = 40; // highest.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000866
867 // Prime the first token.
868 fprintf(stderr, "ready> ");
869 getNextToken();
870
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000871 // Run the main "interpreter loop" now.
872 MainLoop();
873
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000874 // Print out all of the generated code.
Lang Hames1cb54812015-01-16 19:44:46 +0000875 JITHelper->dump();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000876
877 return 0;
878}