Skip to content

Commit

Permalink
Merge pull request #24 from zourenDevote/develop
Browse files Browse the repository at this point in the history
feat/test: 完善循环,条件语句翻译,实现多维数组结构, 添加测试用例
  • Loading branch information
zourenDevote committed Sep 5, 2023
2 parents 8617bc2 + 9e8c002 commit e0cee4a
Show file tree
Hide file tree
Showing 32 changed files with 365 additions and 182 deletions.
12 changes: 7 additions & 5 deletions include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ class VariableAST : public IdDefAST {
unsigned VarFlag; // the flag that record the variable message
std::vector<ExprAST *> Dims; // the dimensions record of the variable
ExprAST *InitExpr = nullptr; // the init expr of the variable

public:
INSERT_ACCEPT
};
Expand Down Expand Up @@ -674,7 +673,7 @@ class NumberExprAST : public ExprAST {

char CValue;
bool BValue;
long LValue;
long long LValue;
double DValue;

public:
Expand All @@ -683,7 +682,7 @@ class NumberExprAST : public ExprAST {

public:
explicit NumberExprAST(const LineNo&, ASTBase*, char);
explicit NumberExprAST(const LineNo&, ASTBase*, long);
explicit NumberExprAST(const LineNo&, ASTBase*, long long);
explicit NumberExprAST(const LineNo&, ASTBase*, double);
explicit NumberExprAST(const LineNo&, ASTBase*, bool);

Expand All @@ -695,8 +694,8 @@ class NumberExprAST : public ExprAST {
bool isChar () { return IsChar; }
bool isSigned () { return IsSigned; }
char getCValue () { return CValue; }
long getIValue () { return LValue; }
unsigned long getUIValue () { return (unsigned long)LValue; }
long long getIValue () { return LValue; }
unsigned long long getUIValue () { return (unsigned long long)LValue; }
double getFValue () { return DValue; }
bool getBoolValue () { return BValue; }

Expand Down Expand Up @@ -768,6 +767,7 @@ class CallExprAST : public ExprAST {
FuncAST *TheCallFunction = nullptr; // 对应的FuncAST的定义
const std::string FuncName; // 函数名
std::vector<ExprAST *> Args;
bool IsCallStd; // flag to call std function
public:
explicit CallExprAST(const LineNo&, ASTBase*, const std::string&);

Expand All @@ -777,10 +777,12 @@ class CallExprAST : public ExprAST {

void setFunction (FuncAST *func) { this->TheCallFunction = func; }
void addArg (ExprAST *arg) { Args.push_back(arg); }
void setIsCallStd (bool flag) { IsCallStd = flag; }

const std::vector<ExprAST*> &getArgs() const { return this->Args; }
const std::string &getName() const { return this->FuncName; }
bool isArgEmpty() const { return this->Args.empty(); }
bool isCallStd() const { return this->IsCallStd; }
llvm::Function *getLLVMFunction();
FuncAST *getFuncDef() { return TheCallFunction; }

Expand Down
3 changes: 3 additions & 0 deletions include/global_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand Down Expand Up @@ -55,6 +56,8 @@ extern bool OnlyPrintIR;

#endif

extern std::unordered_set<std::string> StdFunctionSet;

}

#endif
13 changes: 8 additions & 5 deletions include/ir_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class KaleIRBuilder: public AstVisitor {
llvm::IRBuilder<> *TheIRBuilder;
llvm::Function *CurFunc;
llvm::BasicBlock *CurBblk;
FuncAST *CurFuncAst;
ProgramAST *Prog;
bool IsRootScope;
bool IsNeedPointer = false;
Expand Down Expand Up @@ -57,22 +58,24 @@ class KaleIRBuilder: public AstVisitor {
private:
llvm::FunctionType *getFunctionTypeByFuncASTNode(FuncAST *);
llvm::Type *getLLVMType(DataTypeAST *);
llvm::Type *kaleTypeToLLVMType(KType ty);
llvm::Constant *createConstantValue(llvm::Type *ty);

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);
void storeValueToPointer(llvm::Value *lv, llvm::Value *rv);
void typeConvert(llvm::Value *&lv, llvm::Value *&rv);
void convertToAimType(llvm::Type *t1);
void convertToI1();
llvm::Type *kaleTypeToLLVMType(KType ty);

void generateStdFuncCall(CallExprAST *node);
private:
static std::unordered_map<ProgramAST *, KaleIRBuilder *> ProgToIrBuilderMap;

static std::unordered_map<std::string, std::vector<KType>> StdKaleFuncTypeMap;
static std::unordered_map<std::string, llvm::FunctionType*> StdLLVMFuncTypeMap;
public:
static KaleIRBuilder *getOrCreateIrBuilderByProg(ProgramAST *prog);
static void initStdFunctionTypeMap();
};

}
Expand Down
4 changes: 2 additions & 2 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TokenParser {
private:
LineNo LineInfo;
double DoubleNumVal;
long IntNumVal;
long long IntNumVal;
std::string LiteralVal;
std::string IdStr;
int LastChar;
Expand All @@ -36,7 +36,7 @@ class TokenParser {

LineNo getCurLineNo() const { return LineInfo; }
double getDoubleVal() const { return DoubleNumVal; }
long getIntVal() const { return IntNumVal; }
long long getIntVal() const { return IntNumVal; }
const std::string& getIdStr() const { return IdStr; }
const std::string& getLiteral() const { return LiteralVal; }
bool isSigned() { return IsSigned; }
Expand Down
4 changes: 4 additions & 0 deletions kale_std/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ project(kale_std)

add_library(${PROJECT_NAME}
kaleidoscope_std.c)

add_executable(kale_std_test test.c)

target_link_libraries(kale_std_test kale_std)
27 changes: 11 additions & 16 deletions kale_std/kaleidoscope_std.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "kaleidoscope_std.h"
#include <stdio.h>
#include <stdarg.h>

/// @brief kaleidoscope 标准库函数 获取整型变量,获取浮点变量
/// @return
Expand All @@ -17,24 +18,18 @@ double GetDouble() {
}

/// @brief kaleidoscope 标准库函数 打印整型变量,打印浮点变量,换行, 空格
void PutInt(int v) {
printf("%d", v);
void Print(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}

void PutDouble(double v) {
printf("%f", v);
}

void NewLine() {
void PrintLn(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
}

void Space() {
printf(" ");
}

void PutChar(int v) {
char c = (char)v;
printf("%c", c);
}

10 changes: 5 additions & 5 deletions kale_std/kaleidoscope_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ int GetInt();
double GetDouble();

/// @brief kaleidoscope 标准库函数 打印整型变量,打印浮点变量,换行, 空格
void PutInt(int);
void PutDouble(double);
void NewLine();
void Space();
void PutChar(int);

void Print(const char *s, ...);
void PrintLn(const char *s, ...);


#endif


11 changes: 11 additions & 0 deletions kale_std/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Created by 20580 on 2023/9/3.
//

#include "kaleidoscope_std.h"

int main() {
Print("Hello, World %d, %d\n", 10, 20);
PrintLn("Hello, World %d, %d", 10, 20);
return 0;
}
3 changes: 2 additions & 1 deletion src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ NumberExprAST::NumberExprAST(const LineNo &lineNo, ASTBase *parent, char v) : Ex
this->IsChar = true;
}

NumberExprAST::NumberExprAST(const LineNo &lineNo, ASTBase *parent, long v) : ExprAST(lineNo, parent) {
NumberExprAST::NumberExprAST(const LineNo &lineNo, ASTBase *parent, long long v) : ExprAST(lineNo, parent) {
this->LValue = v;
this->IsLong = true;
}
Expand Down Expand Up @@ -224,6 +224,7 @@ IdIndexedRefAST::IdIndexedRefAST(const LineNo &lineNo, ASTBase *parent, const st
/// CallExprAST define code
CallExprAST::CallExprAST(const LineNo &lineNo, ASTBase *parent, const std::string& name) : ExprAST(lineNo, parent), FuncName(name){
TheCallFunction = nullptr;
IsCallStd = false;
}

llvm::Function *CallExprAST::getLLVMFunction() {
Expand Down
9 changes: 9 additions & 0 deletions src/global_variable.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "global_variable.h"
#include "llvm/IR/Module.h"
#include <unordered_set>

namespace kale {

Expand Down Expand Up @@ -46,6 +47,14 @@ bool OnlyParse = false;
bool OnlyPrintIR = false;
bool OnlyPrintAST = false;

/// T ==> Std Function map
std::unordered_set<std::string> StdFunctionSet = {
"Print",
"PrintLn",
"GetInt",
"GetDouble",
};

#endif

}
Loading

0 comments on commit e0cee4a

Please sign in to comment.