diff options
author | Lang Hames <lhames@gmail.com> | 2015-08-19 18:15:58 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2015-08-19 18:15:58 +0000 |
commit | 59b0da886efd85ed8b93c74d3e32da7e7252e4a0 (patch) | |
tree | 0f55d1804f4c34aa4bdfa43ce9a8fb80fd2b4ab9 /llvm/docs/tutorial | |
parent | be6e0539381fa5aab0de22bd598688973b72ab85 (diff) | |
download | bcm5719-llvm-59b0da886efd85ed8b93c74d3e32da7e7252e4a0.tar.gz bcm5719-llvm-59b0da886efd85ed8b93c74d3e32da7e7252e4a0.zip |
[Kaleidoscope] Clang-format the Kaleidoscope tutorials.
Also reduces changes between tutorial chapters.
llvm-svn: 245472
Diffstat (limited to 'llvm/docs/tutorial')
-rw-r--r-- | llvm/docs/tutorial/LangImpl1.rst | 19 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl2.rst | 74 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl3.rst | 46 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl4.rst | 3 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl5.rst | 125 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl6.rst | 53 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl7.rst | 58 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl8.rst | 14 |
8 files changed, 255 insertions, 137 deletions
diff --git a/llvm/docs/tutorial/LangImpl1.rst b/llvm/docs/tutorial/LangImpl1.rst index 6840bd6b090..0240986164f 100644 --- a/llvm/docs/tutorial/LangImpl1.rst +++ b/llvm/docs/tutorial/LangImpl1.rst @@ -169,14 +169,16 @@ numeric value of a number). First, we define the possibilities: tok_eof = -1, // commands - tok_def = -2, tok_extern = -3, + tok_def = -2, + tok_extern = -3, // primary - tok_identifier = -4, tok_number = -5, + tok_identifier = -4, + tok_number = -5, }; - static std::string IdentifierStr; // Filled in if tok_identifier - static double NumVal; // Filled in if tok_number + static std::string IdentifierStr; // Filled in if tok_identifier + static double NumVal; // Filled in if tok_number Each token returned by our lexer will either be one of the Token enum values or it will be an 'unknown' character like '+', which is returned @@ -217,8 +219,10 @@ loop: while (isalnum((LastChar = getchar()))) IdentifierStr += LastChar; - if (IdentifierStr == "def") return tok_def; - if (IdentifierStr == "extern") return tok_extern; + if (IdentifierStr == "def") + return tok_def; + if (IdentifierStr == "extern") + return tok_extern; return tok_identifier; } @@ -250,7 +254,8 @@ extend it :). Next we handle comments: if (LastChar == '#') { // Comment until end of line. - do LastChar = getchar(); + do + LastChar = getchar(); while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); if (LastChar != EOF) diff --git a/llvm/docs/tutorial/LangImpl2.rst b/llvm/docs/tutorial/LangImpl2.rst index 4b28e02a523..09c55e60f3a 100644 --- a/llvm/docs/tutorial/LangImpl2.rst +++ b/llvm/docs/tutorial/LangImpl2.rst @@ -44,6 +44,7 @@ We'll start with expressions first: /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; + public: NumberExprAST(double Val) : Val(Val) {} }; @@ -65,6 +66,7 @@ language: /// VariableExprAST - Expression class for referencing a variable, like "a". class VariableExprAST : public ExprAST { std::string Name; + public: VariableExprAST(const std::string &Name) : Name(Name) {} }; @@ -73,6 +75,7 @@ language: class BinaryExprAST : public ExprAST { char Op; std::unique_ptr<ExprAST> LHS, RHS; + public: BinaryExprAST(char op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS) @@ -83,6 +86,7 @@ language: class CallExprAST : public ExprAST { std::string Callee; std::vector<ExprAST*> Args; + public: CallExprAST(const std::string &Callee, std::vector<std::unique_ptr<ExprAST>> Args) @@ -111,6 +115,7 @@ way to talk about functions themselves: class PrototypeAST { std::string Name; std::vector<std::string> Args; + public: PrototypeAST(const std::string &name, std::vector<std::string> Args) : Name(name), Args(std::move(Args)) {} @@ -120,6 +125,7 @@ way to talk about functions themselves: class FunctionAST { std::unique_ptr<PrototypeAST> Proto; std::unique_ptr<ExprAST> Body; + public: FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body) @@ -171,9 +177,14 @@ be parsed. /// Error* - These are little helper functions for error handling. - ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;} - PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; } - FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; } + std::unique_ptr<ExprAST> Error(const char *Str) { + fprintf(stderr, "Error: %s\n", Str); + return nullptr; + } + std::unique_ptr<PrototypeAST> ErrorP(const char *Str) { + Error(Str); + return nullptr; + } The ``Error`` routines are simple helper routines that our parser will use to handle errors. The error recovery in our parser will not be the @@ -216,13 +227,14 @@ the parenthesis operator is defined like this: /// parenexpr ::= '(' expression ')' static std::unique_ptr<ExprAST> ParseParenExpr() { - getNextToken(); // eat (. + getNextToken(); // eat (. auto V = ParseExpression(); - if (!V) return nullptr; + if (!V) + return nullptr; if (CurTok != ')') return Error("expected ')'"); - getNextToken(); // eat ). + getNextToken(); // eat ). return V; } @@ -267,11 +279,13 @@ function calls: std::vector<std::unique_ptr<ExprAST>> Args; if (CurTok != ')') { while (1) { - auto Arg = ParseExpression(); - if (!Arg) return nullptr; - Args.push_back(std::move(Arg)); + if (auto Arg = ParseExpression()) + Args.push_back(std::move(Arg)); + else + return nullptr; - if (CurTok == ')') break; + if (CurTok == ')') + break; if (CurTok != ',') return Error("Expected ')' or ',' in argument list"); @@ -309,10 +323,14 @@ primary expression, we need to determine what sort of expression it is: /// ::= parenexpr static std::unique_ptr<ExprAST> ParsePrimary() { switch (CurTok) { - default: return Error("unknown token when expecting an expression"); - case tok_identifier: return ParseIdentifierExpr(); - case tok_number: return ParseNumberExpr(); - case '(': return ParseParenExpr(); + default: + return Error("unknown token when expecting an expression"); + case tok_identifier: + return ParseIdentifierExpr(); + case tok_number: + return ParseNumberExpr(); + case '(': + return ParseParenExpr(); } } @@ -396,7 +414,8 @@ a sequence of [binop,primaryexpr] pairs: /// static std::unique_ptr<ExprAST> ParseExpression() { auto LHS = ParsePrimary(); - if (!LHS) return nullptr; + if (!LHS) + return nullptr; return ParseBinOpRHS(0, std::move(LHS)); } @@ -446,7 +465,8 @@ expression: // Parse the primary expression after the binary operator. auto RHS = ParsePrimary(); - if (!RHS) return nullptr; + if (!RHS) + return nullptr; As such, this code eats (and remembers) the binary operator and then parses the primary expression that follows. This builds up the whole @@ -505,7 +525,8 @@ above two blocks duplicated for context): int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) { RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS)); - if (RHS == 0) return 0; + if (!RHS) + return nullptr; } // Merge LHS/RHS. LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), @@ -633,11 +654,20 @@ include the top-level loop. See `below <#code>`_ for full code in the while (1) { fprintf(stderr, "ready> "); switch (CurTok) { - case tok_eof: return; - case ';': getNextToken(); break; // ignore top-level semicolons. - case tok_def: HandleDefinition(); break; - case tok_extern: HandleExtern(); break; - default: HandleTopLevelExpression(); break; + case tok_eof: + return; + case ';': // ignore top-level semicolons. + getNextToken(); + break; + case tok_def: + HandleDefinition(); + break; + case tok_extern: + HandleExtern(); + break; + default: + HandleTopLevelExpression(); + break; } } } diff --git a/llvm/docs/tutorial/LangImpl3.rst b/llvm/docs/tutorial/LangImpl3.rst index 0920aa3f6a2..a2d23e7bcf9 100644 --- a/llvm/docs/tutorial/LangImpl3.rst +++ b/llvm/docs/tutorial/LangImpl3.rst @@ -41,6 +41,7 @@ class: /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; + public: NumberExprAST(double Val) : Val(Val) {} virtual Value *Codegen(); @@ -72,7 +73,10 @@ parser, which will be used to report errors found during code generation .. code-block:: c++ - Value *ErrorV(const char *Str) { Error(Str); return 0; } + Value *ErrorV(const char *Str) { + Error(Str); + return nullptr; + } static Module *TheModule; static IRBuilder<> Builder(getGlobalContext()); @@ -145,18 +149,23 @@ variables <LangImpl7.html#localvars>`_. Value *BinaryExprAST::Codegen() { Value *L = LHS->Codegen(); Value *R = RHS->Codegen(); - if (L == 0 || R == 0) return 0; + if (!L || !R) + return nullptr; switch (Op) { - case '+': return Builder.CreateFAdd(L, R, "addtmp"); - case '-': return Builder.CreateFSub(L, R, "subtmp"); - case '*': return Builder.CreateFMul(L, R, "multmp"); + case '+': + return Builder.CreateFAdd(L, R, "addtmp"); + case '-': + return Builder.CreateFSub(L, R, "subtmp"); + case '*': + return Builder.CreateFMul(L, R, "multmp"); case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), "booltmp"); - default: return ErrorV("invalid binary operator"); + default: + return ErrorV("invalid binary operator"); } } @@ -201,17 +210,18 @@ would return 0.0 and -1.0, depending on the input value. Value *CallExprAST::Codegen() { // Look up the name in the global module table. Function *CalleeF = TheModule->getFunction(Callee); - if (CalleeF == 0) + if (!CalleeF) return ErrorV("Unknown function referenced"); // If argument mismatch error. if (CalleeF->arg_size() != Args.size()) return ErrorV("Incorrect # arguments passed"); - std::vector<Value*> ArgsV; + std::vector<Value *> ArgsV; for (unsigned i = 0, e = Args.size(); i != e; ++i) { ArgsV.push_back(Args[i]->Codegen()); - if (ArgsV.back() == 0) return 0; + if (!ArgsV.back()) + return nullptr; } return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); @@ -253,10 +263,11 @@ with: // Make the function type: double(double,double) etc. std::vector<Type*> Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); - FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), - Doubles, false); + FunctionType *FT = + FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); - Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); + Function *F = + Function::Create(FT, Function::ExternalLinkage, Name, TheModule); This code packs a lot of power into a few lines. Note first that this function returns a "Function\*" instead of a "Value\*". Because a @@ -321,13 +332,13 @@ then deletes it. // If F already has a body, reject this. if (!F->empty()) { ErrorF("redefinition of function"); - return 0; + return nullptr; } // If F took a different number of args, reject. if (F->arg_size() != Args.size()) { ErrorF("redefinition of function with different # args"); - return 0; + return nullptr; } } @@ -351,6 +362,7 @@ we emit an error. // Add arguments to variable symbol table. NamedValues[Args[Idx]] = AI; } + return F; } @@ -368,8 +380,8 @@ straight-forward with the mechanics we have already used above. NamedValues.clear(); Function *TheFunction = Proto->Codegen(); - if (TheFunction == 0) - return 0; + if (!TheFunction) + return nullptr; Code generation for function definitions starts out simply enough: we just codegen the prototype (Proto) and verify that it is ok. We then @@ -423,7 +435,7 @@ function is finished and validated, we return it. // Error reading body, remove function. TheFunction->eraseFromParent(); - return 0; + return nullptr; } The only piece left here is handling of the error case. For simplicity, diff --git a/llvm/docs/tutorial/LangImpl4.rst b/llvm/docs/tutorial/LangImpl4.rst index 8abb7456949..497a4c56a38 100644 --- a/llvm/docs/tutorial/LangImpl4.rst +++ b/llvm/docs/tutorial/LangImpl4.rst @@ -400,8 +400,7 @@ example, if we add: .. code-block:: c++ /// putchard - putchar that takes a double and returns 0. - extern "C" - double putchard(double X) { + extern "C" double putchard(double X) { putchar((char)X); return 0; } diff --git a/llvm/docs/tutorial/LangImpl5.rst b/llvm/docs/tutorial/LangImpl5.rst index add0e188734..c0420fa70f7 100644 --- a/llvm/docs/tutorial/LangImpl5.rst +++ b/llvm/docs/tutorial/LangImpl5.rst @@ -66,7 +66,9 @@ for the relevant tokens: .. code-block:: c++ // control - tok_if = -6, tok_then = -7, tok_else = -8, + tok_if = -6, + tok_then = -7, + tok_else = -8, Once we have that, we recognize the new keywords in the lexer. This is pretty simple stuff: @@ -74,11 +76,16 @@ pretty simple stuff: .. code-block:: c++ ... - if (IdentifierStr == "def") return tok_def; - if (IdentifierStr == "extern") return tok_extern; - if (IdentifierStr == "if") return tok_if; - if (IdentifierStr == "then") return tok_then; - if (IdentifierStr == "else") return tok_else; + if (IdentifierStr == "def") + return tok_def; + if (IdentifierStr == "extern") + return tok_extern; + if (IdentifierStr == "if") + return tok_if; + if (IdentifierStr == "then") + return tok_then; + if (IdentifierStr == "else") + return tok_else; return tok_identifier; AST Extensions for If/Then/Else @@ -91,6 +98,7 @@ To represent the new expression we add a new AST node for it: /// IfExprAST - Expression class for if/then/else. class IfExprAST : public ExprAST { std::unique<ExprAST> Cond, Then, Else; + public: IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then, std::unique_ptr<ExprAST> Else) @@ -115,14 +123,16 @@ First we define a new parsing function: // condition. auto Cond = ParseExpression(); - if (!Cond) return nullptr; + if (!Cond) + return nullptr; if (CurTok != tok_then) return Error("expected then"); getNextToken(); // eat the then auto Then = ParseExpression(); - if (Then) return nullptr; + if (!Then) + return nullptr; if (CurTok != tok_else) return Error("expected else"); @@ -130,7 +140,8 @@ First we define a new parsing function: getNextToken(); auto Else = ParseExpression(); - if (!Else) return nullptr; + if (!Else) + return nullptr; return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then), std::move(Else)); @@ -142,11 +153,16 @@ Next we hook it up as a primary expression: static std::unique_ptr<ExprAST> ParsePrimary() { switch (CurTok) { - default: return Error("unknown token when expecting an expression"); - case tok_identifier: return ParseIdentifierExpr(); - case tok_number: return ParseNumberExpr(); - case '(': return ParseParenExpr(); - case tok_if: return ParseIfExpr(); + default: + return Error("unknown token when expecting an expression"); + case tok_identifier: + return ParseIdentifierExpr(); + case tok_number: + return ParseNumberExpr(); + case '(': + return ParseParenExpr(); + case tok_if: + return ParseIfExpr(); } } @@ -271,12 +287,12 @@ for ``IfExprAST``: Value *IfExprAST::Codegen() { Value *CondV = Cond->Codegen(); - if (!CondV) return nullptr; + if (!CondV) + return nullptr; // Convert condition to a bool by comparing equal to 0.0. - CondV = Builder.CreateFCmpONE(CondV, - ConstantFP::get(getGlobalContext(), APFloat(0.0)), - "ifcond"); + CondV = Builder.CreateFCmpONE( + CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond"); This code is straightforward and similar to what we saw before. We emit the expression for the condition, then compare that value to zero to get @@ -288,7 +304,8 @@ a truth value as a 1-bit (bool) value. // Create blocks for the then and else cases. Insert the 'then' block at the // end of the function. - BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction); + BasicBlock *ThenBB = + BasicBlock::Create(getGlobalContext(), "then", TheFunction); BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else"); BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont"); @@ -321,7 +338,8 @@ that LLVM supports forward references. Builder.SetInsertPoint(ThenBB); Value *ThenV = Then->Codegen(); - if (ThenV == 0) return 0; + if (!ThenV) + return nullptr; Builder.CreateBr(MergeBB); // Codegen of 'Then' can change the current block, update ThenBB for the PHI. @@ -362,7 +380,8 @@ value for code that will set up the Phi node. Builder.SetInsertPoint(ElseBB); Value *ElseV = Else->Codegen(); - if (ElseV == 0) return 0; + if (!ElseV) + return nullptr; Builder.CreateBr(MergeBB); // Codegen of 'Else' can change the current block, update ElseBB for the PHI. @@ -380,8 +399,8 @@ code: // Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, - "iftmp"); + PHINode *PN = + Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); PN->addIncoming(ElseV, ElseBB); @@ -446,13 +465,20 @@ The lexer extensions are the same sort of thing as for if/then/else: tok_for = -9, tok_in = -10 ... in gettok ... - if (IdentifierStr == "def") return tok_def; - if (IdentifierStr == "extern") return tok_extern; - if (IdentifierStr == "if") return tok_if; - if (IdentifierStr == "then") return tok_then; - if (IdentifierStr == "else") return tok_else; - if (IdentifierStr == "for") return tok_for; - if (IdentifierStr == "in") return tok_in; + if (IdentifierStr == "def") + return tok_def; + if (IdentifierStr == "extern") + return tok_extern; + if (IdentifierStr == "if") + return tok_if; + if (IdentifierStr == "then") + return tok_then; + if (IdentifierStr == "else") + return tok_else; + if (IdentifierStr == "for") + return tok_for; + if (IdentifierStr == "in") + return tok_in; return tok_identifier; AST Extensions for the 'for' Loop @@ -467,6 +493,7 @@ variable name and the constituent expressions in the node. class ForExprAST : public ExprAST { std::string VarName; std::unique_ptr<ExprAST> Start, End, Step, Body; + public: ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start, std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step, @@ -502,20 +529,23 @@ value to null in the AST node: auto Start = ParseExpression(); - if (!Start) return nullptr; + if (!Start) + return nullptr; if (CurTok != ',') return Error("expected ',' after for start value"); getNextToken(); auto End = ParseExpression(); - if (!End) return nullptr; + if (!End) + return nullptr; // The step value is optional. std::unique_ptr<ExprAST> Step; if (CurTok == ',') { getNextToken(); Step = ParseExpression(); - if (!Step) return nullptr; + if (!Step) + return nullptr; } if (CurTok != tok_in) @@ -523,7 +553,8 @@ value to null in the AST node: getNextToken(); // eat 'in'. auto Body = ParseExpression(); - if (!Body) return nullptr; + if (!Body) + return nullptr; return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End), std::move(Step), @@ -593,7 +624,8 @@ expression). // block. Function *TheFunction = Builder.GetInsertBlock()->getParent(); BasicBlock *PreheaderBB = Builder.GetInsertBlock(); - BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction); + BasicBlock *LoopBB = + BasicBlock::Create(getGlobalContext(), "loop", TheFunction); // Insert an explicit fall through from the current block to the LoopBB. Builder.CreateBr(LoopBB); @@ -610,7 +642,8 @@ the two blocks. Builder.SetInsertPoint(LoopBB); // Start the PHI node with an entry for Start. - PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, VarName.c_str()); + PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + 2, VarName.c_str()); Variable->addIncoming(StartVal, PreheaderBB); Now that the "preheader" for the loop is set up, we switch to emitting @@ -630,8 +663,8 @@ backedge, but we can't set it up yet (because it doesn't exist!). // Emit the body of the loop. This, like any other expr, can change the // current BB. Note that we ignore the value computed by the body, but don't // allow an error. - if (Body->Codegen() == 0) - return 0; + if (!Body->Codegen()) + return nullptr; Now the code starts to get more interesting. Our 'for' loop introduces a new variable to the symbol table. This means that our symbol table can @@ -653,10 +686,11 @@ table. .. code-block:: c++ // Emit the step value. - Value *StepVal; + Value *StepVal = nullptr; if (Step) { StepVal = Step->Codegen(); - if (StepVal == 0) return 0; + if (!StepVal) + return nullptr; } else { // If not specified, use 1.0. StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0)); @@ -673,12 +707,12 @@ iteration of the loop. // Compute the end condition. Value *EndCond = End->Codegen(); - if (EndCond == 0) return EndCond; + if (!EndCond) + return nullptr; // Convert condition to a bool by comparing equal to 0.0. - EndCond = Builder.CreateFCmpONE(EndCond, - ConstantFP::get(getGlobalContext(), APFloat(0.0)), - "loopcond"); + EndCond = Builder.CreateFCmpONE( + EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond"); Finally, we evaluate the exit value of the loop, to determine whether the loop should exit. This mirrors the condition evaluation for the @@ -688,7 +722,8 @@ if/then/else statement. // Create the "after loop" block and insert it. BasicBlock *LoopEndBB = Builder.GetInsertBlock(); - BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); + BasicBlock *AfterBB = + BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); // Insert the conditional branch into the end of LoopEndBB. Builder.CreateCondBr(EndCond, LoopBB, AfterBB); diff --git a/llvm/docs/tutorial/LangImpl6.rst b/llvm/docs/tutorial/LangImpl6.rst index b3138182a29..4918cb08edf 100644 --- a/llvm/docs/tutorial/LangImpl6.rst +++ b/llvm/docs/tutorial/LangImpl6.rst @@ -96,15 +96,20 @@ keywords: enum Token { ... // operators - tok_binary = -11, tok_unary = -12 + tok_binary = -11, + tok_unary = -12 }; ... static int gettok() { ... - if (IdentifierStr == "for") return tok_for; - if (IdentifierStr == "in") return tok_in; - if (IdentifierStr == "binary") return tok_binary; - if (IdentifierStr == "unary") return tok_unary; + if (IdentifierStr == "for") + return tok_for; + if (IdentifierStr == "in") + return tok_in; + if (IdentifierStr == "binary") + return tok_binary; + if (IdentifierStr == "unary") + return tok_unary; return tok_identifier; This just adds lexer support for the unary and binary keywords, like we @@ -131,6 +136,7 @@ this: std::vector<std::string> Args; bool IsOperator; unsigned Precedence; // Precedence if a binary op. + public: PrototypeAST(const std::string &name, std::vector<std::string> Args, bool IsOperator = false, unsigned Prec = 0) @@ -211,8 +217,8 @@ user-defined operator, we need to parse it: if (Kind && ArgNames.size() != Kind) return ErrorP("Invalid number of operands for operator"); - return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames), - Kind != 0, BinaryPrecedence); + return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames), Kind != 0, + BinaryPrecedence); } This is all fairly straightforward parsing code, and we have already @@ -232,23 +238,28 @@ default case for our existing binary operator node: Value *BinaryExprAST::Codegen() { Value *L = LHS->Codegen(); Value *R = RHS->Codegen(); - if (L == 0 || R == 0) return 0; + if (!L || !R) + return nullptr; switch (Op) { - case '+': return Builder.CreateFAdd(L, R, "addtmp"); - case '-': return Builder.CreateFSub(L, R, "subtmp"); - case '*': return Builder.CreateFMul(L, R, "multmp"); + case '+': + return Builder.CreateFAdd(L, R, "addtmp"); + case '-': + return Builder.CreateFSub(L, R, "subtmp"); + case '*': + return Builder.CreateFMul(L, R, "multmp"); case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), "booltmp"); - default: break; + default: + break; } // If it wasn't a builtin binary operator, it must be a user defined one. Emit // a call to it. - Function *F = TheModule->getFunction(std::string("binary")+Op); + Function *F = TheModule->getFunction(std::string("binary") + Op); assert(F && "binary operator not found!"); Value *Ops[2] = { L, R }; @@ -269,8 +280,8 @@ The final piece of code we are missing, is a bit of top-level magic: NamedValues.clear(); Function *TheFunction = Proto->Codegen(); - if (TheFunction == 0) - return 0; + if (!TheFunction) + return nullptr; // If this is an operator, install it. if (Proto->isBinaryOp()) @@ -308,6 +319,7 @@ that, we need an AST node: class UnaryExprAST : public ExprAST { char Opcode; std::unique_ptr<ExprAST> Operand; + public: UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} @@ -357,7 +369,8 @@ call ParseUnary instead: ... // Parse the unary expression after the binary operator. auto RHS = ParseUnary(); - if (!RHS) return nullptr; + if (!RHS) + return nullptr; ... } /// expression @@ -365,7 +378,8 @@ call ParseUnary instead: /// static std::unique_ptr<ExprAST> ParseExpression() { auto LHS = ParseUnary(); - if (!LHS) return nullptr; + if (!LHS) + return nullptr; return ParseBinOpRHS(0, std::move(LHS)); } @@ -416,10 +430,11 @@ unary operators. It looks like this: Value *UnaryExprAST::Codegen() { Value *OperandV = Operand->Codegen(); - if (OperandV == 0) return 0; + if (!OperandV) + return nullptr; Function *F = TheModule->getFunction(std::string("unary")+Opcode); - if (F == 0) + if (!F) return ErrorV("Unknown unary operator"); return Builder.CreateCall(F, OperandV, "unop"); diff --git a/llvm/docs/tutorial/LangImpl7.rst b/llvm/docs/tutorial/LangImpl7.rst index 33f48535f16..8c35f2ac019 100644 --- a/llvm/docs/tutorial/LangImpl7.rst +++ b/llvm/docs/tutorial/LangImpl7.rst @@ -358,7 +358,8 @@ from the stack slot: Value *VariableExprAST::Codegen() { // Look this variable up in the function. Value *V = NamedValues[Name]; - if (V == 0) return ErrorV("Unknown variable name"); + if (!V) + return ErrorV("Unknown variable name"); // Load the value. return Builder.CreateLoad(V, Name.c_str()); @@ -378,7 +379,8 @@ the unabridged code): // Emit the start code first, without 'variable' in scope. Value *StartVal = Start->Codegen(); - if (StartVal == 0) return 0; + if (!StartVal) + return nullptr; // Store the value into the alloca. Builder.CreateStore(StartVal, Alloca); @@ -386,7 +388,8 @@ the unabridged code): // Compute the end condition. Value *EndCond = End->Codegen(); - if (EndCond == 0) return EndCond; + if (!EndCond) + return nullptr; // Reload, increment, and restore the alloca. This handles the case where // the body of the loop mutates the variable. @@ -588,11 +591,13 @@ allowed. // Codegen the RHS. Value *Val = RHS->Codegen(); - if (Val == 0) return 0; + if (!Val) + return nullptr; // Look up the name. Value *Variable = NamedValues[LHSE->getName()]; - if (Variable == 0) return ErrorV("Unknown variable name"); + if (!Variable) + return ErrorV("Unknown variable name"); Builder.CreateStore(Val, Variable); return Val; @@ -649,10 +654,14 @@ this: ... static int gettok() { ... - if (IdentifierStr == "in") return tok_in; - if (IdentifierStr == "binary") return tok_binary; - if (IdentifierStr == "unary") return tok_unary; - if (IdentifierStr == "var") return tok_var; + if (IdentifierStr == "in") + return tok_in; + if (IdentifierStr == "binary") + return tok_binary; + if (IdentifierStr == "unary") + return tok_unary; + if (IdentifierStr == "var") + return tok_var; return tok_identifier; ... @@ -665,6 +674,7 @@ var/in, it looks like this: class VarExprAST : public ExprAST { std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames; std::unique_ptr<ExprAST> Body; + public: VarExprAST(std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames, std::unique_ptr<ExprAST> body) @@ -692,13 +702,20 @@ do is add it as a primary expression: /// ::= varexpr static std::unique_ptr<ExprAST> ParsePrimary() { switch (CurTok) { - default: return Error("unknown token when expecting an expression"); - case tok_identifier: return ParseIdentifierExpr(); - case tok_number: return ParseNumberExpr(); - case '(': return ParseParenExpr(); - case tok_if: return ParseIfExpr(); - case tok_for: return ParseForExpr(); - case tok_var: return ParseVarExpr(); + default: + return Error("unknown token when expecting an expression"); + case tok_identifier: + return ParseIdentifierExpr(); + case tok_number: + return ParseNumberExpr(); + case '(': + return ParseParenExpr(); + case tok_if: + return ParseIfExpr(); + case tok_for: + return ParseForExpr(); + case tok_var: + return ParseVarExpr(); } } @@ -756,7 +773,8 @@ AST node: getNextToken(); // eat 'in'. auto Body = ParseExpression(); - if (!Body) return nullptr; + if (!Body) + return nullptr; return llvm::make_unique<VarExprAST>(std::move(VarNames), std::move(Body)); @@ -791,7 +809,8 @@ previous value that we replace in OldBindings. Value *InitVal; if (Init) { InitVal = Init->Codegen(); - if (InitVal == 0) return 0; + if (!InitVal) + return nullptr; } else { // If not specified, use 0.0. InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0)); } @@ -816,7 +835,8 @@ we evaluate the body of the var/in expression: // Codegen the body, now that all vars are in scope. Value *BodyVal = Body->Codegen(); - if (BodyVal == 0) return 0; + if (!BodyVal) + return nullptr; Finally, before returning, we restore the previous variable bindings: diff --git a/llvm/docs/tutorial/LangImpl8.rst b/llvm/docs/tutorial/LangImpl8.rst index 2f994c23d25..77e3e429674 100644 --- a/llvm/docs/tutorial/LangImpl8.rst +++ b/llvm/docs/tutorial/LangImpl8.rst @@ -307,10 +307,12 @@ and then we have added to all of our AST classes a source location: SourceLocation Loc; public: + ExprAST(SourceLocation Loc = CurLoc) : Loc(Loc) {} + virtual ~ExprAST() {} + virtual Value* Codegen() = 0; int getLine() const { return Loc.Line; } int getCol() const { return Loc.Col; } - ExprAST(SourceLocation Loc = CurLoc) : Loc(Loc) {} - virtual std::ostream &dump(std::ostream &out, int ind) { + virtual raw_ostream &dump(raw_ostream &out, int ind) { return out << ':' << getLine() << ':' << getCol() << '\n'; } @@ -399,9 +401,9 @@ argument allocas in ``PrototypeAST::CreateArgumentAllocas``. DILocalVariable D = DBuilder->createParameterVariable( Scope, Args[Idx], Idx + 1, Unit, Line, KSDbgInfo.getDoubleTy(), true); - Instruction *Call = DBuilder->insertDeclare( - Alloca, D, DBuilder->createExpression(), Builder.GetInsertBlock()); - Call->setDebugLoc(DebugLoc::get(Line, 0, Scope)); + DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(), + DebugLoc::get(Line, 0, Scope), + Builder.GetInsertBlock()); Here we're doing a few things. First, we're grabbing our current scope for the variable so we can say what range of code our variable is valid @@ -409,7 +411,7 @@ through. Second, we're creating the variable, giving it the scope, the name, source location, type, and since it's an argument, the argument index. Third, we create an ``lvm.dbg.declare`` call to indicate at the IR level that we've got a variable in an alloca (and it gives a starting -location for the variable). Lastly, we set a source location for the +location for the variable), and setting a source location for the beginning of the scope on the declare. One interesting thing to note at this point is that various debuggers have |