#pragma once #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "cps/AST.h" #include "cps/RuntimeCheck.h" #include "cps/FunctionGen.h" #include #include namespace cps { class ArrayHandler; class CodeGen { std::unique_ptr TheContext; std::unique_ptr TheModule; std::unique_ptr> Builder; std::map NamedValues; std::unique_ptr Arrays; std::unique_ptr RuntimeChecker; std::unique_ptr FuncGen; llvm::FunctionCallee PrintfFunc; llvm::FunctionCallee ScanfFunc; llvm::Value *PrintfFormatStr; llvm::Value *ScanfFormatStr; void SetupExternalFunctions(); llvm::AllocaInst *CreateEntryBlockAlloca(llvm::Function *TheFunction, const std::string &VarName); void emitIfStmt(IfStmtAST *Stmt); void emitWhileStmt(WhileStmtAST *Stmt); void emitRepeatStmt(RepeatStmtAST *Stmt); void emitForStmt(ForStmtAST *Stmt); public: CodeGen(); ~CodeGen(); void compile(const std::vector> &Statements); void print(); llvm::Value *emitExpr(ExprAST *Expr); void emitStmt(StmtAST *Stmt); friend class ArrayHandler; }; }