X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=examples%2FKaleidoscope%2FChapter7%2Ftoy.cpp;h=a900c5f7b16f3039e6b74b0e6da8867f5f6bdcca;hb=44364a33be4f2729f0b00c6ab48685a39749ad6f;hp=3c988ac9aa2f78f0c649c1dab4cd3fbe5252a3af;hpb=09f1564d23c7f606a5419a89a5126b698624c3a7;p=oota-llvm.git diff --git a/examples/Kaleidoscope/Chapter7/toy.cpp b/examples/Kaleidoscope/Chapter7/toy.cpp index 3c988ac9aa2..a900c5f7b16 100644 --- a/examples/Kaleidoscope/Chapter7/toy.cpp +++ b/examples/Kaleidoscope/Chapter7/toy.cpp @@ -136,6 +136,7 @@ public: /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; + public: NumberExprAST(double Val) : Val(Val) {} Value *Codegen() override; @@ -144,6 +145,7 @@ public: /// VariableExprAST - Expression class for referencing a variable, like "a". class VariableExprAST : public ExprAST { std::string Name; + public: VariableExprAST(const std::string &Name) : Name(Name) {} const std::string &getName() const { return Name; } @@ -154,6 +156,7 @@ public: class UnaryExprAST : public ExprAST { char Opcode; std::unique_ptr Operand; + public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} @@ -164,6 +167,7 @@ public: class BinaryExprAST : public ExprAST { char Op; std::unique_ptr LHS, RHS; + public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) @@ -175,6 +179,7 @@ public: class CallExprAST : public ExprAST { std::string Callee; std::vector> Args; + public: CallExprAST(const std::string &Callee, std::vector> Args) @@ -185,6 +190,7 @@ public: /// IfExprAST - Expression class for if/then/else. class IfExprAST : public ExprAST { std::unique_ptr Cond, Then, Else; + public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) @@ -196,6 +202,7 @@ public: class ForExprAST : public ExprAST { std::string VarName; std::unique_ptr Start, End, Step, Body; + public: ForExprAST(const std::string &VarName, std::unique_ptr Start, std::unique_ptr End, std::unique_ptr Step, @@ -209,25 +216,29 @@ public: class VarExprAST : public ExprAST { std::vector>> VarNames; std::unique_ptr Body; + public: - VarExprAST(std::vector>> VarNames, - std::unique_ptr Body) - : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + VarExprAST( + std::vector>> VarNames, + std::unique_ptr Body) + : VarNames(std::move(VarNames)), Body(std::move(Body)) {} Value *Codegen() override; }; /// PrototypeAST - This class represents the "prototype" for a function, -/// which captures its argument names as well as if it is an operator. +/// which captures its name, and its argument names (thus implicitly the number +/// of arguments the function takes), as well as if it is an operator. class PrototypeAST { std::string Name; std::vector Args; bool IsOperator; unsigned Precedence; // Precedence if a binary op. + public: PrototypeAST(const std::string &Name, std::vector Args, bool IsOperator = false, unsigned Prec = 0) - : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), - Precedence(Prec) {} + : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), + Precedence(Prec) {} bool isUnaryOp() const { return IsOperator && Args.size() == 1; } bool isBinaryOp() const { return IsOperator && Args.size() == 2; } @@ -248,8 +259,10 @@ public: class FunctionAST { std::unique_ptr Proto; std::unique_ptr Body; + public: - FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) + FunctionAST(std::unique_ptr Proto, + std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} Function *Codegen(); }; @@ -297,6 +310,26 @@ std::unique_ptr ErrorF(const char *Str) { static std::unique_ptr ParseExpression(); +/// numberexpr ::= number +static std::unique_ptr ParseNumberExpr() { + auto Result = llvm::make_unique(NumVal); + getNextToken(); // consume the number + return std::move(Result); +} + +/// parenexpr ::= '(' expression ')' +static std::unique_ptr ParseParenExpr() { + getNextToken(); // eat (. + auto V = ParseExpression(); + if (!V) + return nullptr; + + if (CurTok != ')') + return Error("expected ')'"); + getNextToken(); // eat ). + return V; +} + /// identifierexpr /// ::= identifier /// ::= identifier '(' expression* ')' @@ -333,26 +366,6 @@ static std::unique_ptr ParseIdentifierExpr() { return llvm::make_unique(IdName, std::move(Args)); } -/// numberexpr ::= number -static std::unique_ptr ParseNumberExpr() { - auto Result = llvm::make_unique(NumVal); - getNextToken(); // consume the number - return std::move(Result); -} - -/// parenexpr ::= '(' expression ')' -static std::unique_ptr ParseParenExpr() { - getNextToken(); // eat (. - auto V = ParseExpression(); - if (!V) - return nullptr; - - if (CurTok != ')') - return Error("expected ')'"); - getNextToken(); // eat ). - return V; -} - /// ifexpr ::= 'if' expression 'then' expression 'else' expression static std::unique_ptr ParseIfExpr() { getNextToken(); // eat the if. @@ -521,7 +534,8 @@ static std::unique_ptr ParseUnary() { /// binoprhs /// ::= ('+' unary)* - static std::unique_ptr ParseBinOpRHS(int ExprPrec, std::unique_ptr LHS) { +static std::unique_ptr ParseBinOpRHS(int ExprPrec, + std::unique_ptr LHS) { // If this is a binop, find its precedence. while (1) { int TokPrec = GetTokPrecedence(); @@ -550,7 +564,8 @@ static std::unique_ptr ParseUnary() { } // Merge LHS/RHS. - LHS = llvm::make_unique(BinOp, std::move(LHS), std::move(RHS)); + LHS = + llvm::make_unique(BinOp, std::move(LHS), std::move(RHS)); } } @@ -647,7 +662,8 @@ static std::unique_ptr ParseDefinition() { static std::unique_ptr ParseTopLevelExpr() { if (auto E = ParseExpression()) { // Make an anonymous proto. - auto Proto = llvm::make_unique("", std::vector()); + auto Proto = + llvm::make_unique("", std::vector()); return llvm::make_unique(std::move(Proto), std::move(E)); } return nullptr; @@ -716,7 +732,7 @@ Value *BinaryExprAST::Codegen() { // This assume we're building without RTTI because LLVM builds that way by // default. If you build LLVM with RTTI this can be changed to a // dynamic_cast for automatic error checking. - VariableExprAST *LHSE = static_cast(LHS.get()); + VariableExprAST *LHSE = static_cast(LHS.get()); if (!LHSE) return ErrorV("destination of '=' must be a variable"); // Codegen the RHS. @@ -759,7 +775,7 @@ Value *BinaryExprAST::Codegen() { Function *F = TheModule->getFunction(std::string("binary") + Op); assert(F && "binary operator not found!"); - Value *Ops[] = { L, R }; + Value *Ops[] = {L, R}; return Builder.CreateCall(F, Ops, "binop"); } @@ -837,27 +853,26 @@ Value *IfExprAST::Codegen() { return PN; } +// Output for-loop as: +// var = alloca double +// ... +// start = startexpr +// store start -> var +// goto loop +// loop: +// ... +// bodyexpr +// ... +// loopend: +// step = stepexpr +// endcond = endexpr +// +// curvar = load var +// nextvar = curvar + step +// store nextvar -> var +// br endcond, loop, endloop +// outloop: Value *ForExprAST::Codegen() { - // Output this as: - // var = alloca double - // ... - // start = startexpr - // store start -> var - // goto loop - // loop: - // ... - // bodyexpr - // ... - // loopend: - // step = stepexpr - // endcond = endexpr - // - // curvar = load var - // nextvar = curvar + step - // store nextvar -> var - // br endcond, loop, endloop - // outloop: - Function *TheFunction = Builder.GetInsertBlock()->getParent(); // Create an alloca for the variable in the entry block. @@ -894,7 +909,7 @@ Value *ForExprAST::Codegen() { return nullptr; // Emit the step value. - Value *StepVal; + Value *StepVal = nullptr; if (Step) { StepVal = Step->Codegen(); if (!StepVal) @@ -907,7 +922,7 @@ Value *ForExprAST::Codegen() { // Compute the end condition. Value *EndCond = End->Codegen(); if (!EndCond) - return EndCond; + return nullptr; // Reload, increment, and restore the alloca. This handles the case where // the body of the loop mutates the variable. @@ -1137,9 +1152,9 @@ static void MainLoop() { switch (CurTok) { case tok_eof: return; - case ';': + case ';': // ignore top-level semicolons. getNextToken(); - break; // ignore top-level semicolons. + break; case tok_def: HandleDefinition(); break;