Skip to content

Commit

Permalink
Merge pull request #26 from zourenDevote/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zourenDevote committed Sep 10, 2023
2 parents 6fad4ec + ee4c215 commit aa62a6a
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "third_party/cibe"]
path = third_party/cibe
url = https://github.com/zourenDevote/CIBE.git
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ project(kalecc)

cmake_minimum_required(VERSION 3.10)

# How to enable: -DENABLE_CTEST=On
option(ENABLE_CTEST "Ctest option" OFF)
# How to enable: -DBUILD_WITH_CMODEL=On
option(BUILD_WITH_CMODEL "Translation kale to c code and compile it to executable file" OFF)

# 调用命令创建目录
execute_process(
Expand Down Expand Up @@ -48,6 +51,11 @@ if(ENABLE_CTEST)
enable_testing()
endif()

if(BUILD_WITH_CMODEL)
message(STATUS "Use c module translation method.")
add_compile_definitions(__USE_C_MODULE_TRANSLATION_METHOD__)
endif()

add_subdirectory(kale_std)
add_subdirectory(src)
add_subdirectory(test)
2 changes: 2 additions & 0 deletions include/asm_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class LLVMBuilderChain : public AsmBuilder {
std::string Rpath;
};



}


Expand Down
28 changes: 25 additions & 3 deletions include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
// clang-format off

#include "common.h"

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
#include "llvm/IR/Function.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Type.h"
#endif

#include <vector>
#include <string>


#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
using namespace llvm;
#endif

namespace kale {

Expand Down Expand Up @@ -56,8 +61,7 @@ class ASTBase {
void setProgram(ProgramAST *prog);

virtual ASTBase* deepCopy() { return nullptr; }

ASTBase *getChild(unsigned int index);

LineNo *getLineNo() { return &LineMsg; }
ASTBase *getParent() { return Parent; }
ProgramAST *getProgram() { return Program; }
Expand Down Expand Up @@ -167,7 +171,9 @@ class DataTypeAST : public ASTBase {
class FuncAST : public ASTBase {
private:
using ParamIter = std::vector<ParamAST *>::iterator;
#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
Function *LLVMFunc; //
#endif
std::string FuncName; // 函数名
std::vector<ParamAST *> FuncParams; // 函数参数列表
DataTypeAST *RetType; // 返回值类型
Expand All @@ -180,14 +186,19 @@ class FuncAST : public ASTBase {
public:
explicit FuncAST(const LineNo&, ASTBase*, const std::string&);

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
void setLLVMFunction(Function *func) { LLVMFunc = func; }
#endif
void setFuncName (const std::string& name) { FuncName = name; }
void setRetType (DataTypeAST *type) { RetType = type; }
void addFuncParam (ParamAST *param) { FuncParams.push_back(param); }
void setBlockStmt (BlockStmtAST *stmt) { BlockStmt = stmt; }

BlockStmtAST *getBlockStmt () { return BlockStmt; }

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
Function *getLLVMFunction() { return LLVMFunc; }
#endif
const std::string &getFuncName () { return FuncName; }
const std::vector<ParamAST *> &getParams () { return FuncParams; }
DataTypeAST *getRetType () { return RetType; }
Expand Down Expand Up @@ -285,17 +296,21 @@ class IdDefAST : public ASTBase {
DataTypeAST *getDataType() { return DataType; }
const char *getName () { return Name.c_str(); }

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
void setLLVMValue(llvm::Value *v) { Value = v; }
void setLLVMType(llvm::Type *ty) { VarLLVMType = ty; }
llvm::Value *getLLVMValue() { return Value; }
llvm::Type *getVarLLVMType() { return VarLLVMType; }
#endif
private:
DataTypeAST *DataType;
std::string Name;

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
/// llvm
llvm::Value *Value = nullptr;
llvm::Type* VarLLVMType = nullptr;
#endif
};


Expand Down Expand Up @@ -728,7 +743,9 @@ class IdRefAST : public ExprAST {
public:
void setId(IdDefAST *id) { Id = id; }

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
Value *getLLVMValue() { Id->getLLVMValue(); }
#endif
IdDefAST *getId() { return Id; }
std::string getIdName() const { return IdName; }

Expand Down Expand Up @@ -756,7 +773,9 @@ class IdIndexedRefAST : public ExprAST {
void addIndex(ExprAST *expr) { Indexes.push_back(expr); }
void setId(IdDefAST *id) { Id = id; }

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
Value *getLLVMValue() { Id->getLLVMValue(); }
#endif
IdDefAST *getId() { return Id; }
const std::vector<ExprAST*> &getIndexes() const { return Indexes; }
std::string getIdName() const { return IdName; }
Expand Down Expand Up @@ -789,7 +808,10 @@ class CallExprAST : public ExprAST {
const std::string &getName() const { return this->FuncName; }
bool isArgEmpty() const { return this->Args.empty(); }
bool isCallStd() const { return this->IsCallStd; }

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
llvm::Function *getLLVMFunction();
#endif
FuncAST *getFuncDef() { return TheCallFunction; }

public:
Expand Down
2 changes: 1 addition & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ enum KaleOptLevel {

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

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

Expand Down
12 changes: 12 additions & 0 deletions include/global_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#include <string>
#include <unordered_set>
#include "ast.h"
#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
#include "llvm/IR/Module.h"
#endif

namespace kale {

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
/// T ==> The global context
extern llvm::LLVMContext GlobalContext;
#endif

/// T ==> The input source file list
extern std::vector<std::string> InputFileList;
Expand All @@ -22,14 +26,18 @@ extern std::vector<ProgramAST*> ProgramList;
/// T ==> The output file name
extern std::string OutputFileName;

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
/// T ==> The print ir flag
extern bool PrintIR;
#endif

/// T ==> The print ast flag
extern bool PrintAST;

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
/// T ==> The dump ir to .ll flag
extern bool DumpIRToLL;
#endif

/// T ==> The flag of compile and run executable file
extern bool CompileAndRun;
Expand All @@ -38,8 +46,10 @@ extern bool CompileAndRun;
extern bool UseMultThreadCompile;
extern int ThreadCount;

#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
/// T ==> Use LLVM Tool Chain flag
extern bool UseLLVMToolChainFlag;
#endif

/// T ==> Opt level;
extern KaleOptLevel OptLevel;
Expand All @@ -52,7 +62,9 @@ extern std::string CheckInputFile;
extern bool TokenParserTestFlag;
extern bool OnlyParse;
extern bool OnlyPrintAST;
#ifndef __USE_C_MODULE_TRANSLATION_METHOD__
extern bool OnlyPrintIR;
#endif

#endif

Expand Down
152 changes: 88 additions & 64 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,78 +1,102 @@

set(CMAKE_CXX_STANDARD 14)

find_package(LLVM 15 REQUIRED CONFIG)
if(BUILD_WITH_CMODEL)
message(STATUS "Build with cmodel")
set(SRC_FILE
pre_analysis.cpp
global_variable.cpp
ast_visitor.cpp
ast_dumper.cpp
ast.cpp
kale_util.cpp
parser.cpp
test/token_parser_test.cpp
main.cpp
asm_builder.cpp
)

#set(ARCHS
# AArch64
# AMDGPU
# ARM
# AVR
# BPF
# Hexagon
# Lanai
# Mips
# MSP430
# # NVPTX
# PowerPC
# RISCV
# Sparc
# SystemZ
# WebAssembly
# X86
# # XCore
#)
add_executable(${PROJECT_NAME} ${SRC_FILE})

set(LLVM_LIBS
"LLVMAnalysis"
"LLVMCore"
"LLVMSupport"
# "LLVMAsmPrinter"
# "LLVMXCoreCodeGen"
# "LLVMXCoreDesc"
# "LLVMXCoreInfo"
# "LLVMNVPTXCodeGen"
# "LLVMNVPTXDesc"
# "LLVMNVPTXInfo"
)
target_include_directories(
${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}/third_party/cxxopts
PUBLIC ${PROJECT_SOURCE_DIR}/include
)
else ()
message(STATUS "Build with llvm-15")
find_package(LLVM 15 REQUIRED CONFIG)

#foreach(arch ${ARCHS})
# message(STATUS "ARCH ${arch}")
# list(APPEND LLVM_LIBS "LLVM${arch}CodeGen")
# list(APPEND LLVM_LIBS "LLVM${arch}AsmParser")
#endforeach ()
#set(ARCHS
# AArch64
# AMDGPU
# ARM
# AVR
# BPF
# Hexagon
# Lanai
# Mips
# MSP430
# # NVPTX
# PowerPC
# RISCV
# Sparc
# SystemZ
# WebAssembly
# X86
# # XCore
#)

link_directories(${LLVM_LIBRARY_DIRS})
set(LLVM_LIBS
"LLVMAnalysis"
"LLVMCore"
"LLVMSupport"
# "LLVMAsmPrinter"
# "LLVMXCoreCodeGen"
# "LLVMXCoreDesc"
# "LLVMXCoreInfo"
# "LLVMNVPTXCodeGen"
# "LLVMNVPTXDesc"
# "LLVMNVPTXInfo"
)

set(SRC_FILE
pre_analysis.cpp
global_variable.cpp
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
asm_builder.cpp
type_checker.cpp
)
#foreach(arch ${ARCHS})
# message(STATUS "ARCH ${arch}")
# list(APPEND LLVM_LIBS "LLVM${arch}CodeGen")
# list(APPEND LLVM_LIBS "LLVM${arch}AsmParser")
#endforeach ()

add_executable(${PROJECT_NAME} ${SRC_FILE})
link_directories(${LLVM_LIBRARY_DIRS})

target_include_directories(
${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}/third_party/cxxopts
PUBLIC ${PROJECT_SOURCE_DIR}/include
PUBLIC ${LLVM_INCLUDE_DIRS}
)
set(SRC_FILE
pre_analysis.cpp
global_variable.cpp
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
asm_builder.cpp
type_checker.cpp
)

message(STATUS ${LLVM_LIBS})
target_link_libraries(${PROJECT_NAME}
PUBLIC ${LLVM_LIBS}
)
add_executable(${PROJECT_NAME} ${SRC_FILE})

target_include_directories(
${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}/third_party/cxxopts
PUBLIC ${PROJECT_SOURCE_DIR}/include
PUBLIC ${LLVM_INCLUDE_DIRS}
)

message(STATUS ${LLVM_LIBS})
target_link_libraries(${PROJECT_NAME}
PUBLIC ${LLVM_LIBS}
)

endif ()

Loading

0 comments on commit aa62a6a

Please sign in to comment.