diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/cps/AST.h | 18 | ||||
| -rw-r--r-- | include/cps/ArrayHandler.h | 3 | ||||
| -rw-r--r-- | include/cps/CodeGen.h | 2 | ||||
| -rw-r--r-- | include/cps/Lexer.h | 2 | ||||
| -rw-r--r-- | include/cps/RuntimeCheck.h | 28 |
5 files changed, 47 insertions, 6 deletions
diff --git a/include/cps/AST.h b/include/cps/AST.h index aaf81cb..a0c0ace 100644 --- a/include/cps/AST.h +++ b/include/cps/AST.h @@ -27,22 +27,26 @@ public: class ArrayAccessExprAST : public ExprAST { std::string Name; std::vector<std::unique_ptr<ExprAST>> Indices; + int Line; public: - ArrayAccessExprAST(const std::string &Name, std::vector<std::unique_ptr<ExprAST>> Indices) - : Name(Name), Indices(std::move(Indices)) {} + ArrayAccessExprAST(const std::string &Name, std::vector<std::unique_ptr<ExprAST>> Indices, int Line) + : Name(Name), Indices(std::move(Indices)), Line(Line) {} const std::string &getName() const { return Name; } const std::vector<std::unique_ptr<ExprAST>> &getIndices() const { return Indices; } + int getLine() const { return Line; } }; class BinaryExprAST : public ExprAST { int Op; std::unique_ptr<ExprAST> LHS, RHS; + int Line; public: - BinaryExprAST(int Op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS) - : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + BinaryExprAST(int Op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS, int Line) + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)), Line(Line) {} int getOp() const { return Op; } ExprAST *getLHS() const { return LHS.get(); } ExprAST *getRHS() const { return RHS.get(); } + int getLine() const { return Line; } }; class StmtAST { @@ -86,12 +90,14 @@ class ArrayAssignStmtAST : public StmtAST { std::string Name; std::vector<std::unique_ptr<ExprAST>> Indices; std::unique_ptr<ExprAST> Expr; + int Line; public: - ArrayAssignStmtAST(const std::string &Name, std::vector<std::unique_ptr<ExprAST>> Indices, std::unique_ptr<ExprAST> Expr) - : Name(Name), Indices(std::move(Indices)), Expr(std::move(Expr)) {} + ArrayAssignStmtAST(const std::string &Name, std::vector<std::unique_ptr<ExprAST>> Indices, std::unique_ptr<ExprAST> Expr, int Line) + : Name(Name), Indices(std::move(Indices)), Expr(std::move(Expr)), Line(Line) {} const std::string &getName() const { return Name; } const std::vector<std::unique_ptr<ExprAST>> &getIndices() const { return Indices; } ExprAST *getExpr() const { return Expr.get(); } + int getLine() const { return Line; } }; class InputStmtAST : public StmtAST { diff --git a/include/cps/ArrayHandler.h b/include/cps/ArrayHandler.h index 5052de4..734f464 100644 --- a/include/cps/ArrayHandler.h +++ b/include/cps/ArrayHandler.h @@ -3,6 +3,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "cps/AST.h" +#include "cps/RuntimeCheck.h" #include <map> #include <string> #include <vector> @@ -26,6 +27,7 @@ class ArrayHandler { std::map<std::string, llvm::AllocaInst*> *NamedValues; std::map<std::string, ArrayMetadata> ArrayTable; + RuntimeCheck &RuntimeChecker; llvm::FunctionCallee MallocFunc; llvm::FunctionCallee FreeFunc; @@ -41,6 +43,7 @@ public: void emitArrayDeclare(ArrayDeclareStmtAST *Stmt, CodeGen &CG); void emitArrayAssign(ArrayAssignStmtAST *Stmt, CodeGen &CG); + ArrayHandler(llvm::LLVMContext &C, llvm::IRBuilder<> &B, llvm::Module &M, std::map<std::string, llvm::AllocaInst*> &NV, RuntimeCheck &RC); llvm::Value* emitArrayAccess(ArrayAccessExprAST *Expr, CodeGen &CG); diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index 8bf5819..57f0899 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -3,6 +3,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "cps/AST.h" +#include "cps/RuntimeCheck.h" #include <map> #include <memory> @@ -17,6 +18,7 @@ class CodeGen { std::map<std::string, llvm::AllocaInst*> NamedValues; std::unique_ptr<ArrayHandler> Arrays; + std::unique_ptr<RuntimeCheck> RuntimeChecker; llvm::FunctionCallee PrintfFunc; llvm::FunctionCallee ScanfFunc; diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h index a7a6218..237f76a 100644 --- a/include/cps/Lexer.h +++ b/include/cps/Lexer.h @@ -44,11 +44,13 @@ enum Token { }; class Lexer { + int CurrentLine = 1; public: std::string IdentifierStr; int NumVal; int gettok(); + int getLine() const { return CurrentLine; } }; } diff --git a/include/cps/RuntimeCheck.h b/include/cps/RuntimeCheck.h new file mode 100644 index 0000000..3c31f41 --- /dev/null +++ b/include/cps/RuntimeCheck.h @@ -0,0 +1,28 @@ +#pragma once +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" + +namespace cps { + +class RuntimeCheck { + llvm::Module &TheModule; + llvm::LLVMContext &TheContext; + llvm::IRBuilder<> &Builder; + + llvm::FunctionCallee PrintfFunc; + llvm::FunctionCallee ExitFunc; + llvm::Value *DivZeroMsg; + llvm::Value *OutOfBoundsMsg; + + void setupExternalFunctions(); + void emitErrorAndExit(llvm::Value *Condition, llvm::Value *Msg, int Line); + +public: + RuntimeCheck(llvm::Module &M, llvm::LLVMContext &C, llvm::IRBuilder<> &B); + + void emitDivZeroCheck(llvm::Value *Divisor, int Line); + void emitIndexCheck(llvm::Value *Index, llvm::Value *Lower, llvm::Value *Upper, int Line); +}; + +} |
