Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

注释dumper #20

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions include/ast.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


#ifndef KALEIDSCOPE_AST
#define KALEIDSCOPE_AST
#ifndef KALE_AST_H
#define KALE_AST_H

// clang-format off

Expand All @@ -12,8 +12,11 @@

#include <vector>


using namespace llvm;

namespace kale {

struct LineNo {
unsigned FileIndex;
unsigned Row;
Expand Down Expand Up @@ -541,6 +544,12 @@ class ExprAST : public ASTBase {
public:
INSERT_ENUM(ExprId)
static bool canCastTo(KAstId id) { return (id == ExprId); }

public:
void setExprType(KType ty) { ExprType = ty; }
KType getExprType() { return ExprType; }
private:
KType ExprType;
};

/// ------------------------------------------------------------------------
Expand Down Expand Up @@ -782,6 +791,8 @@ class CallExprAST : public ExprAST {
INSERT_ACCEPT
};

}

// clang-format on

#endif
Expand Down
8 changes: 6 additions & 2 deletions include/ast_dumper.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

#ifndef KALE_AST_DUMPER
#define KALE_AST_DUMPER
#ifndef KALE_AST_DUMPER_H
#define KALE_AST_DUMPER_H

#include "ast_visitor.h"

namespace kale {

class DumpVisitor : public AstVisitor {

public:
Expand All @@ -14,4 +16,6 @@ class DumpVisitor : public AstVisitor {
void postAction(ASTBase *node) override;
};

}

#endif
11 changes: 8 additions & 3 deletions include/ast_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* @overview: This file is the visitor defined to visit the ast node.
*/

#ifndef KALEIDOSCOPE_AST_VISITOR
#define KALEIDOSCOPE_AST_VISITOR
#ifndef KALE_AST_VISITOR_H
#define KALE_AST_VISITOR_H

namespace kale {

class ASTBase;
class ProgramAST;
Expand All @@ -24,6 +26,7 @@ class ContinueStmtAST;
class ForStmtAST;
class WhileStmtAST;
class IfStmtAST;
class ExprAST;
class BinaryExprAST;
class UnaryExprAST;
class LiteralExprAST;
Expand All @@ -38,7 +41,7 @@ class ExprStmtAST;
#define TraversArray(X) for(auto *node : X) { node->accept(this); }

#ifndef ADD_VISITOR
#define ADD_VISITOR(X) void visit(X *node);
#define ADD_VISITOR(X) virtual void visit(X *node);
/// ------------------------------------------------------------------------
/// @brief AstVisitor this class is the base class to visit the ast node.
/// ------------------------------------------------------------------------
Expand Down Expand Up @@ -82,6 +85,8 @@ class AstVisitor {
#undef ADD_VISITOR
#endif

}

#endif


Expand Down
24 changes: 24 additions & 0 deletions include/cast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "ast.h"
#include <cassert>

#ifndef KALE_CAST_H
#define KALE_CAST_H

namespace kale {

template<class Dest, class Source>
Dest *kale_cast(Source *node) {
if(Dest::canCastTo(node->getClassId())) {
return dynamic_cast<Dest*>(node);
}
return nullptr;
}

}

#endif




12 changes: 9 additions & 3 deletions include/common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

#ifndef KALEIDOSCOPE_COMMON
#define KALEIDOSCOPE_COMMON
#ifndef KALE_COMMON_H
#define KALE_COMMON_H

namespace kale {

/// ------------------------------------------------------------------------
/// @brief The enum type enum the ast type of kaleidoscope language's
Expand Down Expand Up @@ -98,6 +99,11 @@ enum Operator {
};

#define INSERT_ACCEPT void accept(AstVisitor &v) override;
#define INSERT_ENUM(X) KAstId getClassId() override { return X; }
#define INSERT_ENUM(X) KAstId getClassId() override { return X; } \
static KAstId classId() { return X; }

#define ADD_VISITOR_OVERRIDE(X) void visit(X *node) override;

}

#endif
15 changes: 14 additions & 1 deletion include/global_variable.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@

#ifndef KALE_GLOBAL_VARIABLE_H
#define KALE_GLOBAL_VARIABLE_H

#include <vector>
#include <string>
#include "ast.h"
#include "llvm/IR/Module.h"

namespace kale {

/// T ==> The global context
extern llvm::LLVMContext GlobalContext;

/// T ==> The input source file list
extern std::vector<std::string> InputFileList;
Expand Down Expand Up @@ -35,4 +44,8 @@ extern bool TokenParserTestFlag;
extern bool OnlyParse;
extern bool OnlyPrintAST;
extern bool OnlyPrintIR;
#endif
#endif

}

#endif
68 changes: 68 additions & 0 deletions include/ir_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

#ifndef KALE_IR_BUILDER_H
#define KALE_IR_BUILDER_H

#include "common.h"
#include "ast_visitor.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include <unordered_map>

namespace kale {

class KaleIRBuilder: public AstVisitor {

private:
llvm::Module *TheModule;
llvm::IRBuilder<> *TheIRBuilder;
llvm::Function *CurFunc;
llvm::BasicBlock *CurBblk;
ProgramAST *Prog;
bool IsRootScope;
llvm::Value *LastValue;
public:
KaleIRBuilder(ProgramAST *prog);
void generateProgToIr();

protected:
ADD_VISITOR_OVERRIDE(FuncAST)
ADD_VISITOR_OVERRIDE(InitializedAST)
ADD_VISITOR_OVERRIDE(StructDefAST)
ADD_VISITOR_OVERRIDE(VariableAST)
ADD_VISITOR_OVERRIDE(ReturnStmtAST)
ADD_VISITOR_OVERRIDE(BreakStmtAST)
ADD_VISITOR_OVERRIDE(ContinueStmtAST)
ADD_VISITOR_OVERRIDE(ForStmtAST)
ADD_VISITOR_OVERRIDE(WhileStmtAST)
ADD_VISITOR_OVERRIDE(IfStmtAST)
ADD_VISITOR_OVERRIDE(BinaryExprAST)
ADD_VISITOR_OVERRIDE(UnaryExprAST)
ADD_VISITOR_OVERRIDE(LiteralExprAST)
ADD_VISITOR_OVERRIDE(NumberExprAST)
ADD_VISITOR_OVERRIDE(IdRefAST)
ADD_VISITOR_OVERRIDE(IdIndexedRefAST)
ADD_VISITOR_OVERRIDE(CallExprAST)

private:
llvm::FunctionType *getFunctionTypeByFuncASTNode(FuncAST *);
llvm::Type *getLLVMType(DataTypeAST *);
long getConstIntByExpr(ExprAST *expr);

void createAndSetCurrentFunc(const llvm::StringRef& name, llvm::FunctionType *ty);
void createAndSetCurrentBblk(const llvm::StringRef& name);
llvm::Constant *createConstantValue(llvm::Type *ty);
private:
static std::unordered_map<ProgramAST *, KaleIRBuilder *> ProgToIrBuilderMap;

public:
static KaleIRBuilder *getOrCreateIrBuilderByProg(ProgramAST *prog);
};

}

#endif

52 changes: 52 additions & 0 deletions include/ir_support.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#ifndef KALE_IR_SUPPORT_H
#define KALE_IR_SUPPORT_H


#include "llvm/IR/Type.h"
#include "llvm/IR/Constant.h"

namespace kale {

class KaleIRTypeSupport {

public:
static llvm::Type *KaleVoidType;
static llvm::Type *KaleBoolType;
static llvm::Type *KaleCharType;
static llvm::Type *KaleUCharType;
static llvm::Type *KaleShortType;
static llvm::Type *KaleUShortType;
static llvm::Type *KaleIntType;
static llvm::Type *KaleUIntType;
static llvm::Type *KaleLongType;
static llvm::Type *KaleULongType;
static llvm::Type *KaleFloatType;
static llvm::Type *KaleDoubleType;

static void initIRTypeSupport();
};


class KaleIRConstantValueSupport {

public:
static llvm::Constant *KaleTrue;
static llvm::Constant *KaleFalse;
static llvm::Constant *KaleCharZero;
static llvm::Constant *KaleUCharZero;
static llvm::Constant *KaleShortZero;
static llvm::Constant *KaleUShortZero;
static llvm::Constant *KaleIntZero;
static llvm::Constant *KaleUIntZero;
static llvm::Constant *KaleLongZero;
static llvm::Constant *KaleULongZero;
static llvm::Constant *KaleFloatZero;
static llvm::Constant *KaleDoubleZero;

static void initIRConastantSupport();
};

}

#endif
22 changes: 22 additions & 0 deletions include/kale_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


#ifndef KALE_UTIL_H
#define KALE_UTIL_H

#include "ast.h"

namespace kale {

/**
* @brief KaleUtils class give api to help compiler generate IR.
*/
class KaleUtils {

public:
static bool isConstant(ExprAST *expr);

};

}

#endif
6 changes: 4 additions & 2 deletions include/parser.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

#ifndef KALEIDOSCOPE_PARSER
#define KALEIDOSCOPE_PARSER
#ifndef KALE_PARSER_H
#define KALE_PARSER_H

#include <unordered_map>
#include "token.h"
#include "ast.h"

namespace kale {

/// -----------------------------------------------------
/// @brief Token parser class, this class defined to parse
Expand Down Expand Up @@ -103,5 +104,6 @@ class GrammarParser {
};
/// -----------------------------------------------------

}

#endif
8 changes: 8 additions & 0 deletions include/pre_analysis.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

#ifndef KALE_PRE_ANALYSIS_H
#define KALE_PRE_ANALYSIS_H

namespace kale {

/// -------------------------------------------------------------
/// @brief This function parse input sources file list
Expand All @@ -9,6 +13,10 @@
/// -------------------------------------------------------------
bool preFileDepAnalysis();

}

#endif




Expand Down
7 changes: 5 additions & 2 deletions include/token.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#ifndef KALEIDOSCOPE_TOKEN
#define KALEIDOSCOPE_TOKEN
#ifndef KALE_TOKEN_H
#define KALE_TOKEN_H

namespace kale {

enum Token: unsigned {
tok_eof = 256, // --> 终结符 EOF
Expand Down Expand Up @@ -72,6 +74,7 @@ enum Token: unsigned {
tok_bitxor // --> operator ^
};

}

#endif

Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ set(SRC_FILE
ast_visitor.cpp
ast_dumper.cpp
ast.cpp
ir_support.cpp
kale_util.cpp
parser.cpp
ir_builder.cpp
test/token_parser_test.cpp
main.cpp)

Expand Down
Loading