aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/cps/AST.h23
-rw-r--r--include/cps/ArithmeticHandler.h19
-rw-r--r--include/cps/BooleanHandler.h24
-rw-r--r--include/cps/CodeGen.h23
-rw-r--r--include/cps/IntegerHandler.h24
-rw-r--r--include/cps/Lexer.h14
-rw-r--r--include/cps/RealHandler.h24
7 files changed, 144 insertions, 7 deletions
diff --git a/include/cps/AST.h b/include/cps/AST.h
index 7c50ee9..588f5eb 100644
--- a/include/cps/AST.h
+++ b/include/cps/AST.h
@@ -11,13 +11,27 @@ public:
virtual ~ExprAST() = default;
};
-class NumberExprAST : public ExprAST {
+class IntegerExprAST : public ExprAST {
int64_t Val;
public:
- NumberExprAST(int64_t Val) : Val(Val) {}
+ IntegerExprAST(int64_t Val) : Val(Val) {}
int64_t getVal() const { return Val; }
};
+class RealExprAST : public ExprAST {
+ double Val;
+public:
+ RealExprAST(double Val) : Val(Val) {}
+ double getVal() const { return Val; }
+};
+
+class BooleanExprAST : public ExprAST {
+ bool Val;
+public:
+ BooleanExprAST(bool Val) : Val(Val) {}
+ bool getVal() const { return Val; }
+};
+
class VariableExprAST : public ExprAST {
std::string Name;
public:
@@ -57,9 +71,12 @@ public:
class DeclareStmtAST : public StmtAST {
std::string Name;
+ std::string Type;
public:
- DeclareStmtAST(const std::string &Name) : Name(Name) {}
+ DeclareStmtAST(const std::string &Name, const std::string &Type = "INTEGER")
+ : Name(Name), Type(Type) {}
const std::string &getName() const { return Name; }
+ const std::string &getType() const { return Type; }
};
class ArrayDeclareStmtAST : public StmtAST {
diff --git a/include/cps/ArithmeticHandler.h b/include/cps/ArithmeticHandler.h
new file mode 100644
index 0000000..8d6ea27
--- /dev/null
+++ b/include/cps/ArithmeticHandler.h
@@ -0,0 +1,19 @@
+#pragma once
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
+#include "cps/Lexer.h"
+
+namespace cps {
+
+class ArithmeticHandler {
+ llvm::LLVMContext &Context;
+ llvm::IRBuilder<> &Builder;
+
+public:
+ ArithmeticHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B)
+ : Context(Ctx), Builder(B) {}
+
+ llvm::Value *emitBinaryOp(int Op, llvm::Value *LHS, llvm::Value *RHS, int Line);
+};
+
+}
diff --git a/include/cps/BooleanHandler.h b/include/cps/BooleanHandler.h
new file mode 100644
index 0000000..67c7778
--- /dev/null
+++ b/include/cps/BooleanHandler.h
@@ -0,0 +1,24 @@
+#pragma once
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include <map>
+#include <string>
+
+namespace cps {
+
+class BooleanHandler {
+ llvm::LLVMContext &Context;
+ llvm::IRBuilder<> &Builder;
+ llvm::Module &Module;
+ std::map<std::string, llvm::Value*> &NamedValues;
+
+public:
+ BooleanHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, llvm::Module &M, std::map<std::string, llvm::Value*> &NV)
+ : Context(Ctx), Builder(B), Module(M), NamedValues(NV) {}
+
+ void emitDeclare(const std::string &Name);
+ llvm::Value *createLiteral(bool Val);
+};
+
+}
diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h
index 85f605f..df9eef4 100644
--- a/include/cps/CodeGen.h
+++ b/include/cps/CodeGen.h
@@ -5,6 +5,12 @@
#include "cps/AST.h"
#include "cps/RuntimeCheck.h"
#include "cps/FunctionGen.h"
+
+#include "cps/IntegerHandler.h"
+#include "cps/RealHandler.h"
+#include "cps/BooleanHandler.h"
+#include "cps/ArithmeticHandler.h"
+
#include <map>
#include <memory>
@@ -22,10 +28,23 @@ class CodeGen {
std::unique_ptr<RuntimeCheck> RuntimeChecker;
std::unique_ptr<FunctionGen> FuncGen;
+ std::unique_ptr<IntegerHandler> IntHandler;
+ std::unique_ptr<RealHandler> RealHelper;
+ std::unique_ptr<BooleanHandler> BoolHandler;
+ std::unique_ptr<ArithmeticHandler> ArithHandler;
+
llvm::FunctionCallee PrintfFunc;
llvm::FunctionCallee ScanfFunc;
- llvm::Value *PrintfFormatStr;
- llvm::Value *ScanfFormatStr;
+
+ llvm::Value *PrintfFormatStr; // %lld\n
+ llvm::Value *PrintfFloatFormatStr; // %f\n
+ llvm::Value *PrintfStringFormatStr; // %s\n
+
+ llvm::Value *ScanfFormatStr; // %lld
+ llvm::Value *ScanfFloatFormatStr; // %lf
+
+ llvm::Value *TrueStr; // "TRUE"
+ llvm::Value *FalseStr; // "FALSE"
void SetupExternalFunctions();
llvm::AllocaInst *CreateEntryBlockAlloca(llvm::Function *TheFunction, const std::string &VarName);
diff --git a/include/cps/IntegerHandler.h b/include/cps/IntegerHandler.h
new file mode 100644
index 0000000..2cd0bbc
--- /dev/null
+++ b/include/cps/IntegerHandler.h
@@ -0,0 +1,24 @@
+#pragma once
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include <map>
+#include <string>
+
+namespace cps {
+
+class IntegerHandler {
+ llvm::LLVMContext &Context;
+ llvm::IRBuilder<> &Builder;
+ llvm::Module &Module;
+ std::map<std::string, llvm::Value*> &NamedValues;
+
+public:
+ IntegerHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, llvm::Module &M, std::map<std::string, llvm::Value*> &NV)
+ : Context(Ctx), Builder(B), Module(M), NamedValues(NV) {}
+
+ void emitDeclare(const std::string &Name);
+ llvm::Value *createLiteral(int64_t Val);
+};
+
+}
diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h
index 4226cc3..15f56c4 100644
--- a/include/cps/Lexer.h
+++ b/include/cps/Lexer.h
@@ -32,15 +32,24 @@ enum Token {
tok_array = -19,
tok_of = -20,
+ tok_real_kw = -21,
+ tok_boolean_kw = -22,
+ tok_true = -23,
+ tok_false = -24,
+
tok_identifier = -100,
- tok_number = -101,
+ tok_number_int = -101,
+ tok_number_real = -102,
tok_assign = -200, // <-
tok_eq = -201, // =
tok_ne = -202, // <>
tok_le = -203, // <=
tok_ge = -204, // >=
-
+
+ tok_div = -205,
+ tok_mod = -206,
+
tok_colon = -300, // :
tok_function = -400,
@@ -59,6 +68,7 @@ class Lexer {
public:
std::string IdentifierStr;
int64_t NumVal;
+ double RealVal;
int gettok();
int getLine() const { return CurrentLine; }
diff --git a/include/cps/RealHandler.h b/include/cps/RealHandler.h
new file mode 100644
index 0000000..bdb113e
--- /dev/null
+++ b/include/cps/RealHandler.h
@@ -0,0 +1,24 @@
+#pragma once
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include <map>
+#include <string>
+
+namespace cps {
+
+class RealHandler {
+ llvm::LLVMContext &Context;
+ llvm::IRBuilder<> &Builder;
+ llvm::Module &Module;
+ std::map<std::string, llvm::Value*> &NamedValues;
+
+public:
+ RealHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, llvm::Module &M, std::map<std::string, llvm::Value*> &NV)
+ : Context(Ctx), Builder(B), Module(M), NamedValues(NV) {}
+
+ void emitDeclare(const std::string &Name);
+ llvm::Value *createLiteral(double Val);
+};
+
+}