diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h index 1a410aafbc4..22716b2b521 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h @@ -93,7 +93,6 @@ public: void removeModule(ModuleHandle H) { CompileLayer.removeModuleSet(H); } - }; } // end namespace orc diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp index 22b0819cd71..50912109e81 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp @@ -7,15 +7,13 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Transforms/Scalar/GVN.h" #include "KaleidoscopeJIT.h" +#include #include #include #include @@ -135,11 +133,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -149,6 +150,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -158,8 +160,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -170,6 +173,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -182,6 +186,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -194,6 +199,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -205,6 +211,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -219,6 +226,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -232,6 +240,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -249,6 +258,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -272,8 +282,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h index cf480aacef8..91d903029a0 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h @@ -25,10 +25,13 @@ #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/Support/DynamicLibrary.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/GVN.h" #include #include #include @@ -105,7 +108,6 @@ public: } private: - std::unique_ptr optimizeModule(std::unique_ptr M) { // Create a function pass manager. auto FPM = llvm::make_unique(M.get()); @@ -124,7 +126,6 @@ private: return M; } - }; } // end namespace orc diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp index 22b0819cd71..50912109e81 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp @@ -7,15 +7,13 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Transforms/Scalar/GVN.h" #include "KaleidoscopeJIT.h" +#include #include #include #include @@ -135,11 +133,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -149,6 +150,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -158,8 +160,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -170,6 +173,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -182,6 +186,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -194,6 +199,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -205,6 +211,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -219,6 +226,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -232,6 +240,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -249,6 +258,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -272,8 +282,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h index 62c61aac6e6..5ecc869f80f 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h @@ -26,12 +26,16 @@ #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/GVN.h" #include #include +#include #include #include @@ -116,7 +120,6 @@ public: } private: - std::unique_ptr optimizeModule(std::unique_ptr M) { // Create a function pass manager. auto FPM = llvm::make_unique(M.get()); @@ -135,7 +138,6 @@ private: return M; } - }; } // end namespace orc diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp index 22b0819cd71..50912109e81 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp @@ -7,15 +7,13 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Transforms/Scalar/GVN.h" #include "KaleidoscopeJIT.h" +#include #include #include #include @@ -135,11 +133,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -149,6 +150,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -158,8 +160,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -170,6 +173,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -182,6 +186,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -194,6 +199,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -205,6 +211,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -219,6 +226,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -232,6 +240,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -249,6 +258,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -272,8 +282,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h index 6d94c8f9bcd..527d4be09f0 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h @@ -26,11 +26,17 @@ #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/Support/DynamicLibrary.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/GVN.h" #include +#include +#include #include #include #include @@ -47,6 +53,7 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + const PrototypeAST& getProto() const; const std::string& getName() const; llvm::Function *codegen(); @@ -197,7 +204,6 @@ public: } private: - std::string mangle(const std::string &Name) { std::string MangledName; raw_string_ostream MangledNameStream(MangledName); @@ -223,7 +229,6 @@ private: return M; } - }; } // end namespace orc diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp index ddce0dc35b2..07981a8f79b 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp @@ -7,16 +7,15 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Transforms/Scalar/GVN.h" #include "KaleidoscopeJIT.h" +#include #include #include #include @@ -140,7 +139,8 @@ static int gettok() { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -150,6 +150,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -159,8 +160,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -171,6 +173,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -183,6 +186,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -195,6 +199,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -206,6 +211,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -220,6 +226,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -233,6 +240,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -250,6 +258,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h index f11f0369c42..d1ef3c9549f 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h @@ -15,11 +15,12 @@ #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H #include "RemoteJITUtils.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Triple.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/JITSymbol.h" #include "llvm/ExecutionEngine/RuntimeDyld.h" -#include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h" #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" @@ -28,11 +29,17 @@ #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/Support/DynamicLibrary.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/GVN.h" #include +#include +#include #include #include #include @@ -49,6 +56,7 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + const PrototypeAST& getProto() const; const std::string& getName() const; llvm::Function *codegen(); @@ -229,7 +237,6 @@ public: } private: - std::string mangle(const std::string &Name) { std::string MangledName; raw_string_ostream MangledNameStream(MangledName); @@ -255,7 +262,6 @@ private: return M; } - }; } // end namespace orc diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter5/RemoteJITUtils.h b/examples/Kaleidoscope/BuildingAJIT/Chapter5/RemoteJITUtils.h index 718b99e4b24..48a19385596 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter5/RemoteJITUtils.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter5/RemoteJITUtils.h @@ -15,8 +15,10 @@ #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H #include "llvm/ExecutionEngine/Orc/RawByteChannel.h" -#include "llvm/ExecutionEngine/RTDyldMemoryManager.h" -#include +#include "llvm/Support/Error.h" +#include +#include +#include #if !defined(_MSC_VER) && !defined(__MINGW32__) #include @@ -71,4 +73,4 @@ private: int InFD, OutFD; }; -#endif +#endif // LLVM_TOOLS_LLI_REMOTEJITUTILS_H diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp index f5a06cf2bf4..aa08df9ceb5 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp @@ -7,29 +7,30 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Transforms/Scalar/GVN.h" #include "KaleidoscopeJIT.h" +#include "RemoteJITUtils.h" +#include #include #include #include #include #include +#include #include #include #include #include #include - #include -#include #include #include @@ -155,7 +156,8 @@ static int gettok() { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -165,6 +167,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -174,8 +177,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -186,6 +190,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -198,6 +203,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -210,6 +216,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -221,6 +228,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -235,6 +243,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -248,6 +257,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -265,6 +275,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -1229,9 +1240,9 @@ std::unique_ptr connect() { } sockaddr_in servAddr; - bzero(&servAddr, sizeof(servAddr)); + memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = PF_INET; - bcopy(server->h_addr, &servAddr.sin_addr.s_addr, server->h_length); + memcpy(&servAddr.sin_addr.s_addr, server->h_addr, server->h_length); servAddr.sin_port = htons(Port); if (connect(sockfd, reinterpret_cast(&servAddr), sizeof(servAddr)) < 0) { diff --git a/examples/Kaleidoscope/Chapter2/toy.cpp b/examples/Kaleidoscope/Chapter2/toy.cpp index bab34adefde..8357c5b63fb 100644 --- a/examples/Kaleidoscope/Chapter2/toy.cpp +++ b/examples/Kaleidoscope/Chapter2/toy.cpp @@ -1,4 +1,5 @@ #include "llvm/ADT/STLExtras.h" +#include #include #include #include @@ -82,11 +83,13 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; }; /// NumberExprAST - Expression class for numeric literals like "1.0". @@ -149,6 +152,7 @@ public: std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/Chapter3/toy.cpp b/examples/Kaleidoscope/Chapter3/toy.cpp index cdf0d6c8cdb..fdefb50cc20 100644 --- a/examples/Kaleidoscope/Chapter3/toy.cpp +++ b/examples/Kaleidoscope/Chapter3/toy.cpp @@ -9,6 +9,7 @@ #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" +#include #include #include #include @@ -94,11 +95,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -108,6 +112,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -117,6 +122,7 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} + Value *codegen() override; }; @@ -129,6 +135,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -141,6 +148,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -154,6 +162,7 @@ class PrototypeAST { public: PrototypeAST(const std::string &Name, std::vector Args) : Name(Name), Args(std::move(Args)) {} + Function *codegen(); const std::string &getName() const { return Name; } }; @@ -167,8 +176,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/Chapter4/toy.cpp b/examples/Kaleidoscope/Chapter4/toy.cpp index 836a2053cbe..39b9563bc28 100644 --- a/examples/Kaleidoscope/Chapter4/toy.cpp +++ b/examples/Kaleidoscope/Chapter4/toy.cpp @@ -15,6 +15,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/GVN.h" #include "../include/KaleidoscopeJIT.h" +#include #include #include #include @@ -103,11 +104,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -117,6 +121,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -126,6 +131,7 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} + Value *codegen() override; }; @@ -138,6 +144,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -150,6 +157,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -163,6 +171,7 @@ class PrototypeAST { public: PrototypeAST(const std::string &Name, std::vector Args) : Name(Name), Args(std::move(Args)) {} + Function *codegen(); const std::string &getName() const { return Name; } }; @@ -176,8 +185,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/Chapter5/toy.cpp b/examples/Kaleidoscope/Chapter5/toy.cpp index a080bd00cb5..568ec8de5c3 100644 --- a/examples/Kaleidoscope/Chapter5/toy.cpp +++ b/examples/Kaleidoscope/Chapter5/toy.cpp @@ -16,6 +16,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/GVN.h" #include "../include/KaleidoscopeJIT.h" +#include #include #include #include @@ -121,11 +122,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -135,6 +139,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -144,6 +149,7 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} + Value *codegen() override; }; @@ -156,6 +162,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -168,6 +175,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -179,6 +187,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -193,6 +202,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -206,6 +216,7 @@ class PrototypeAST { public: PrototypeAST(const std::string &Name, std::vector Args) : Name(Name), Args(std::move(Args)) {} + Function *codegen(); const std::string &getName() const { return Name; } }; @@ -219,8 +230,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/Chapter6/toy.cpp b/examples/Kaleidoscope/Chapter6/toy.cpp index 85d953b0c75..0fa128d2e55 100644 --- a/examples/Kaleidoscope/Chapter6/toy.cpp +++ b/examples/Kaleidoscope/Chapter6/toy.cpp @@ -16,6 +16,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/GVN.h" #include "../include/KaleidoscopeJIT.h" +#include #include #include #include @@ -129,11 +130,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -143,6 +147,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -152,6 +157,7 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} + Value *codegen() override; }; @@ -163,6 +169,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -175,6 +182,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -187,6 +195,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -198,6 +207,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -212,6 +222,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -229,6 +240,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -252,8 +264,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/Chapter7/toy.cpp b/examples/Kaleidoscope/Chapter7/toy.cpp index 3206ca8c5d7..58b7ce1ceda 100644 --- a/examples/Kaleidoscope/Chapter7/toy.cpp +++ b/examples/Kaleidoscope/Chapter7/toy.cpp @@ -16,6 +16,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/GVN.h" #include "../include/KaleidoscopeJIT.h" +#include #include #include #include @@ -135,11 +136,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -149,6 +153,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -158,8 +163,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -170,6 +176,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -182,6 +189,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -194,6 +202,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -205,6 +214,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -219,6 +229,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -232,6 +243,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -249,6 +261,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -272,8 +285,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===// diff --git a/examples/Kaleidoscope/Chapter8/toy.cpp b/examples/Kaleidoscope/Chapter8/toy.cpp index f880bb8ea0f..f7bb814da77 100644 --- a/examples/Kaleidoscope/Chapter8/toy.cpp +++ b/examples/Kaleidoscope/Chapter8/toy.cpp @@ -1,26 +1,33 @@ #include "llvm/ADT/APFloat.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/Analysis/Passes.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassManager.h" -#include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" -#include "llvm/Transforms/Scalar.h" +#include +#include #include #include #include #include #include #include +#include #include #include @@ -132,11 +139,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// + namespace { + /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST() {} + virtual ~ExprAST() = default; + virtual Value *codegen() = 0; }; @@ -146,6 +156,7 @@ class NumberExprAST : public ExprAST { public: NumberExprAST(double Val) : Val(Val) {} + Value *codegen() override; }; @@ -155,8 +166,9 @@ class VariableExprAST : public ExprAST { public: VariableExprAST(const std::string &Name) : Name(Name) {} - const std::string &getName() const { return Name; } + Value *codegen() override; + const std::string &getName() const { return Name; } }; /// UnaryExprAST - Expression class for a unary operator. @@ -167,6 +179,7 @@ class UnaryExprAST : public ExprAST { public: UnaryExprAST(char Opcode, std::unique_ptr Operand) : Opcode(Opcode), Operand(std::move(Operand)) {} + Value *codegen() override; }; @@ -179,6 +192,7 @@ public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + Value *codegen() override; }; @@ -191,6 +205,7 @@ public: CallExprAST(const std::string &Callee, std::vector> Args) : Callee(Callee), Args(std::move(Args)) {} + Value *codegen() override; }; @@ -202,6 +217,7 @@ public: IfExprAST(std::unique_ptr Cond, std::unique_ptr Then, std::unique_ptr Else) : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} + Value *codegen() override; }; @@ -216,6 +232,7 @@ public: std::unique_ptr Body) : VarName(VarName), Start(std::move(Start)), End(std::move(End)), Step(std::move(Step)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -229,6 +246,7 @@ public: std::vector>> VarNames, std::unique_ptr Body) : VarNames(std::move(VarNames)), Body(std::move(Body)) {} + Value *codegen() override; }; @@ -246,6 +264,7 @@ public: bool IsOperator = false, unsigned Prec = 0) : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), Precedence(Prec) {} + Function *codegen(); const std::string &getName() const { return Name; } @@ -269,8 +288,10 @@ public: FunctionAST(std::unique_ptr Proto, std::unique_ptr Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} + Function *codegen(); }; + } // end anonymous namespace //===----------------------------------------------------------------------===//