1 #include "llvm/ADT/APFloat.h"
2 #include "llvm/ADT/STLExtras.h"
3 #include "llvm/IR/BasicBlock.h"
4 #include "llvm/IR/Constants.h"
5 #include "llvm/IR/DerivedTypes.h"
6 #include "llvm/IR/Function.h"
7 #include "llvm/IR/IRBuilder.h"
8 #include "llvm/IR/LLVMContext.h"
9 #include "llvm/IR/Module.h"
10 #include "llvm/IR/Type.h"
11 #include "llvm/IR/Verifier.h"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
28 // of these for known things.
41 static std::string IdentifierStr
; // Filled in if tok_identifier
42 static double NumVal
; // Filled in if tok_number
44 /// gettok - Return the next token from standard input.
46 static int LastChar
= ' ';
48 // Skip any whitespace.
49 while (isspace(LastChar
))
52 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
53 IdentifierStr
= LastChar
;
54 while (isalnum((LastChar
= getchar())))
55 IdentifierStr
+= LastChar
;
57 if (IdentifierStr
== "def")
59 if (IdentifierStr
== "extern")
61 return tok_identifier
;
64 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
69 } while (isdigit(LastChar
) || LastChar
== '.');
71 NumVal
= strtod(NumStr
.c_str(), nullptr);
75 if (LastChar
== '#') {
76 // Comment until end of line.
79 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
85 // Check for end of file. Don't eat the EOF.
89 // Otherwise, just return the character as its ascii value.
90 int ThisChar
= LastChar
;
95 //===----------------------------------------------------------------------===//
96 // Abstract Syntax Tree (aka Parse Tree)
97 //===----------------------------------------------------------------------===//
101 /// ExprAST - Base class for all expression nodes.
104 virtual ~ExprAST() = default;
106 virtual Value
*codegen() = 0;
109 /// NumberExprAST - Expression class for numeric literals like "1.0".
110 class NumberExprAST
: public ExprAST
{
114 NumberExprAST(double Val
) : Val(Val
) {}
116 Value
*codegen() override
;
119 /// VariableExprAST - Expression class for referencing a variable, like "a".
120 class VariableExprAST
: public ExprAST
{
124 VariableExprAST(const std::string
&Name
) : Name(Name
) {}
126 Value
*codegen() override
;
129 /// BinaryExprAST - Expression class for a binary operator.
130 class BinaryExprAST
: public ExprAST
{
132 std::unique_ptr
<ExprAST
> LHS
, RHS
;
135 BinaryExprAST(char Op
, std::unique_ptr
<ExprAST
> LHS
,
136 std::unique_ptr
<ExprAST
> RHS
)
137 : Op(Op
), LHS(std::move(LHS
)), RHS(std::move(RHS
)) {}
139 Value
*codegen() override
;
142 /// CallExprAST - Expression class for function calls.
143 class CallExprAST
: public ExprAST
{
145 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
148 CallExprAST(const std::string
&Callee
,
149 std::vector
<std::unique_ptr
<ExprAST
>> Args
)
150 : Callee(Callee
), Args(std::move(Args
)) {}
152 Value
*codegen() override
;
155 /// PrototypeAST - This class represents the "prototype" for a function,
156 /// which captures its name, and its argument names (thus implicitly the number
157 /// of arguments the function takes).
160 std::vector
<std::string
> Args
;
163 PrototypeAST(const std::string
&Name
, std::vector
<std::string
> Args
)
164 : Name(Name
), Args(std::move(Args
)) {}
167 const std::string
&getName() const { return Name
; }
170 /// FunctionAST - This class represents a function definition itself.
172 std::unique_ptr
<PrototypeAST
> Proto
;
173 std::unique_ptr
<ExprAST
> Body
;
176 FunctionAST(std::unique_ptr
<PrototypeAST
> Proto
,
177 std::unique_ptr
<ExprAST
> Body
)
178 : Proto(std::move(Proto
)), Body(std::move(Body
)) {}
183 } // end anonymous namespace
185 //===----------------------------------------------------------------------===//
187 //===----------------------------------------------------------------------===//
189 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
190 /// token the parser is looking at. getNextToken reads another token from the
191 /// lexer and updates CurTok with its results.
193 static int getNextToken() { return CurTok
= gettok(); }
195 /// BinopPrecedence - This holds the precedence for each binary operator that is
197 static std::map
<char, int> BinopPrecedence
;
199 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
200 static int GetTokPrecedence() {
201 if (!isascii(CurTok
))
204 // Make sure it's a declared binop.
205 int TokPrec
= BinopPrecedence
[CurTok
];
211 /// LogError* - These are little helper functions for error handling.
212 std::unique_ptr
<ExprAST
> LogError(const char *Str
) {
213 fprintf(stderr
, "Error: %s\n", Str
);
217 std::unique_ptr
<PrototypeAST
> LogErrorP(const char *Str
) {
222 static std::unique_ptr
<ExprAST
> ParseExpression();
224 /// numberexpr ::= number
225 static std::unique_ptr
<ExprAST
> ParseNumberExpr() {
226 auto Result
= std::make_unique
<NumberExprAST
>(NumVal
);
227 getNextToken(); // consume the number
228 return std::move(Result
);
231 /// parenexpr ::= '(' expression ')'
232 static std::unique_ptr
<ExprAST
> ParseParenExpr() {
233 getNextToken(); // eat (.
234 auto V
= ParseExpression();
239 return LogError("expected ')'");
240 getNextToken(); // eat ).
246 /// ::= identifier '(' expression* ')'
247 static std::unique_ptr
<ExprAST
> ParseIdentifierExpr() {
248 std::string IdName
= IdentifierStr
;
250 getNextToken(); // eat identifier.
252 if (CurTok
!= '(') // Simple variable ref.
253 return std::make_unique
<VariableExprAST
>(IdName
);
256 getNextToken(); // eat (
257 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
260 if (auto Arg
= ParseExpression())
261 Args
.push_back(std::move(Arg
));
269 return LogError("Expected ')' or ',' in argument list");
277 return std::make_unique
<CallExprAST
>(IdName
, std::move(Args
));
281 /// ::= identifierexpr
284 static std::unique_ptr
<ExprAST
> ParsePrimary() {
287 return LogError("unknown token when expecting an expression");
289 return ParseIdentifierExpr();
291 return ParseNumberExpr();
293 return ParseParenExpr();
298 /// ::= ('+' primary)*
299 static std::unique_ptr
<ExprAST
> ParseBinOpRHS(int ExprPrec
,
300 std::unique_ptr
<ExprAST
> LHS
) {
301 // If this is a binop, find its precedence.
303 int TokPrec
= GetTokPrecedence();
305 // If this is a binop that binds at least as tightly as the current binop,
306 // consume it, otherwise we are done.
307 if (TokPrec
< ExprPrec
)
310 // Okay, we know this is a binop.
312 getNextToken(); // eat binop
314 // Parse the primary expression after the binary operator.
315 auto RHS
= ParsePrimary();
319 // If BinOp binds less tightly with RHS than the operator after RHS, let
320 // the pending operator take RHS as its LHS.
321 int NextPrec
= GetTokPrecedence();
322 if (TokPrec
< NextPrec
) {
323 RHS
= ParseBinOpRHS(TokPrec
+ 1, std::move(RHS
));
330 std::make_unique
<BinaryExprAST
>(BinOp
, std::move(LHS
), std::move(RHS
));
335 /// ::= primary binoprhs
337 static std::unique_ptr
<ExprAST
> ParseExpression() {
338 auto LHS
= ParsePrimary();
342 return ParseBinOpRHS(0, std::move(LHS
));
346 /// ::= id '(' id* ')'
347 static std::unique_ptr
<PrototypeAST
> ParsePrototype() {
348 if (CurTok
!= tok_identifier
)
349 return LogErrorP("Expected function name in prototype");
351 std::string FnName
= IdentifierStr
;
355 return LogErrorP("Expected '(' in prototype");
357 std::vector
<std::string
> ArgNames
;
358 while (getNextToken() == tok_identifier
)
359 ArgNames
.push_back(IdentifierStr
);
361 return LogErrorP("Expected ')' in prototype");
364 getNextToken(); // eat ')'.
366 return std::make_unique
<PrototypeAST
>(FnName
, std::move(ArgNames
));
369 /// definition ::= 'def' prototype expression
370 static std::unique_ptr
<FunctionAST
> ParseDefinition() {
371 getNextToken(); // eat def.
372 auto Proto
= ParsePrototype();
376 if (auto E
= ParseExpression())
377 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
381 /// toplevelexpr ::= expression
382 static std::unique_ptr
<FunctionAST
> ParseTopLevelExpr() {
383 if (auto E
= ParseExpression()) {
384 // Make an anonymous proto.
385 auto Proto
= std::make_unique
<PrototypeAST
>("__anon_expr",
386 std::vector
<std::string
>());
387 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
392 /// external ::= 'extern' prototype
393 static std::unique_ptr
<PrototypeAST
> ParseExtern() {
394 getNextToken(); // eat extern.
395 return ParsePrototype();
398 //===----------------------------------------------------------------------===//
400 //===----------------------------------------------------------------------===//
402 static std::unique_ptr
<LLVMContext
> TheContext
;
403 static std::unique_ptr
<Module
> TheModule
;
404 static std::unique_ptr
<IRBuilder
<>> Builder
;
405 static std::map
<std::string
, Value
*> NamedValues
;
407 Value
*LogErrorV(const char *Str
) {
412 Value
*NumberExprAST::codegen() {
413 return ConstantFP::get(*TheContext
, APFloat(Val
));
416 Value
*VariableExprAST::codegen() {
417 // Look this variable up in the function.
418 Value
*V
= NamedValues
[Name
];
420 return LogErrorV("Unknown variable name");
424 Value
*BinaryExprAST::codegen() {
425 Value
*L
= LHS
->codegen();
426 Value
*R
= RHS
->codegen();
432 return Builder
->CreateFAdd(L
, R
, "addtmp");
434 return Builder
->CreateFSub(L
, R
, "subtmp");
436 return Builder
->CreateFMul(L
, R
, "multmp");
438 L
= Builder
->CreateFCmpULT(L
, R
, "cmptmp");
439 // Convert bool 0/1 to double 0.0 or 1.0
440 return Builder
->CreateUIToFP(L
, Type::getDoubleTy(*TheContext
), "booltmp");
442 return LogErrorV("invalid binary operator");
446 Value
*CallExprAST::codegen() {
447 // Look up the name in the global module table.
448 Function
*CalleeF
= TheModule
->getFunction(Callee
);
450 return LogErrorV("Unknown function referenced");
452 // If argument mismatch error.
453 if (CalleeF
->arg_size() != Args
.size())
454 return LogErrorV("Incorrect # arguments passed");
456 std::vector
<Value
*> ArgsV
;
457 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
458 ArgsV
.push_back(Args
[i
]->codegen());
463 return Builder
->CreateCall(CalleeF
, ArgsV
, "calltmp");
466 Function
*PrototypeAST::codegen() {
467 // Make the function type: double(double,double) etc.
468 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(*TheContext
));
470 FunctionType::get(Type::getDoubleTy(*TheContext
), Doubles
, false);
473 Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
.get());
475 // Set names for all arguments.
477 for (auto &Arg
: F
->args())
478 Arg
.setName(Args
[Idx
++]);
483 Function
*FunctionAST::codegen() {
484 // First, check for an existing function from a previous 'extern' declaration.
485 Function
*TheFunction
= TheModule
->getFunction(Proto
->getName());
488 TheFunction
= Proto
->codegen();
493 // Create a new basic block to start insertion into.
494 BasicBlock
*BB
= BasicBlock::Create(*TheContext
, "entry", TheFunction
);
495 Builder
->SetInsertPoint(BB
);
497 // Record the function arguments in the NamedValues map.
499 for (auto &Arg
: TheFunction
->args())
500 NamedValues
[std::string(Arg
.getName())] = &Arg
;
502 if (Value
*RetVal
= Body
->codegen()) {
503 // Finish off the function.
504 Builder
->CreateRet(RetVal
);
506 // Validate the generated code, checking for consistency.
507 verifyFunction(*TheFunction
);
512 // Error reading body, remove function.
513 TheFunction
->eraseFromParent();
517 //===----------------------------------------------------------------------===//
518 // Top-Level parsing and JIT Driver
519 //===----------------------------------------------------------------------===//
521 static void InitializeModule() {
522 // Open a new context and module.
523 TheContext
= std::make_unique
<LLVMContext
>();
524 TheModule
= std::make_unique
<Module
>("my cool jit", *TheContext
);
526 // Create a new builder for the module.
527 Builder
= std::make_unique
<IRBuilder
<>>(*TheContext
);
530 static void HandleDefinition() {
531 if (auto FnAST
= ParseDefinition()) {
532 if (auto *FnIR
= FnAST
->codegen()) {
533 fprintf(stderr
, "Read function definition:");
535 fprintf(stderr
, "\n");
538 // Skip token for error recovery.
543 static void HandleExtern() {
544 if (auto ProtoAST
= ParseExtern()) {
545 if (auto *FnIR
= ProtoAST
->codegen()) {
546 fprintf(stderr
, "Read extern: ");
548 fprintf(stderr
, "\n");
551 // Skip token for error recovery.
556 static void HandleTopLevelExpression() {
557 // Evaluate a top-level expression into an anonymous function.
558 if (auto FnAST
= ParseTopLevelExpr()) {
559 if (auto *FnIR
= FnAST
->codegen()) {
560 fprintf(stderr
, "Read top-level expression:");
562 fprintf(stderr
, "\n");
564 // Remove the anonymous expression.
565 FnIR
->eraseFromParent();
568 // Skip token for error recovery.
573 /// top ::= definition | external | expression | ';'
574 static void MainLoop() {
576 fprintf(stderr
, "ready> ");
580 case ';': // ignore top-level semicolons.
590 HandleTopLevelExpression();
596 //===----------------------------------------------------------------------===//
598 //===----------------------------------------------------------------------===//
601 // Install standard binary operators.
602 // 1 is lowest precedence.
603 BinopPrecedence
['<'] = 10;
604 BinopPrecedence
['+'] = 20;
605 BinopPrecedence
['-'] = 20;
606 BinopPrecedence
['*'] = 40; // highest.
608 // Prime the first token.
609 fprintf(stderr
, "ready> ");
612 // Make the module, which holds all the code.
615 // Run the main "interpreter loop" now.
618 // Print out all of the generated code.
619 TheModule
->print(errs(), nullptr);