Skip to content

Commit

Permalink
⚡ fix(Gen): 重写二元运算符
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Aug 9, 2024
1 parent a09ecdd commit ab23eb6
Show file tree
Hide file tree
Showing 19 changed files with 835 additions and 793 deletions.
10 changes: 2 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ include_directories(${ANTLR4_INCLUDE_DIR})
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(parser)

file(GLOB AST_FILES "parser/*.cpp" "parser/*.h")
file(GLOB SOURCE_FILES
"src/*"
"src/Tools/*"
"src/Tools/Managers/*"
"src/Types/*"
"src/Visitors/*"
"src/ext/*")
file(GLOB_RECURSE AST_FILES "parser/*.cpp" "parser/*.h")
file(GLOB_RECURSE SOURCE_FILES "src/*")

add_executable(Riddle_Language ${SOURCE_FILES} ${AST_FILES})

Expand Down
3 changes: 1 addition & 2 deletions RiddleParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ classBody
exprPtr
: funcName=id LeftBracket args=argsExpr RightBracket #funcExpr
| id #objectExpr
| exprPtr Dot exprPtr #blendExpr
;

expression
: Less type=typeName Greater LeftBracket value=exprPtr RightBracket #castExpr
| LeftBracket expr=expression RightBracket #bracketExpr // (x)
Expand Down Expand Up @@ -142,7 +142,6 @@ expression
| string #stringExpr
| number #numberExpr
| boolean #booleanExpr
| id #objValExpr
;

id: Identifier (Dot Identifier)*;
Expand Down
978 changes: 515 additions & 463 deletions parser/RiddleParser.cpp

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions parser/RiddleParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,21 @@ class RiddleParser : public antlr4::Parser {
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

ExprPtrContext* exprPtr();
class BlendExprContext : public ExprPtrContext {
public:
BlendExprContext(ExprPtrContext *ctx);

std::vector<ExprPtrContext *> exprPtr();
ExprPtrContext* exprPtr(size_t i);
antlr4::tree::TerminalNode *Dot();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

ExprPtrContext* exprPtr();
ExprPtrContext* exprPtr(int precedence);
class ExpressionContext : public antlr4::ParserRuleContext {
public:
ExpressionContext(antlr4::ParserRuleContext *parent, size_t invokingState);
Expand Down Expand Up @@ -568,17 +581,6 @@ class RiddleParser : public antlr4::Parser {
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

class ObjValExprContext : public ExpressionContext {
public:
ObjValExprContext(ExpressionContext *ctx);

IdContext *id();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

class MulAssignExprContext : public ExpressionContext {
public:
MulAssignExprContext(ExpressionContext *ctx);
Expand Down Expand Up @@ -1300,6 +1302,7 @@ class RiddleParser : public antlr4::Parser {

bool sempred(antlr4::RuleContext *_localctx, size_t ruleIndex, size_t predicateIndex) override;

bool exprPtrSempred(ExprPtrContext *_localctx, size_t predicateIndex);
bool expressionSempred(ExpressionContext *_localctx, size_t predicateIndex);
bool typeNameSempred(TypeNameContext *_localctx, size_t predicateIndex);

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParser.interp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions parser/RiddleParserBaseListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class RiddleParserBaseListener : public RiddleParserListener {
virtual void enterObjectExpr(RiddleParser::ObjectExprContext * /*ctx*/) override { }
virtual void exitObjectExpr(RiddleParser::ObjectExprContext * /*ctx*/) override { }

virtual void enterBlendExpr(RiddleParser::BlendExprContext * /*ctx*/) override { }
virtual void exitBlendExpr(RiddleParser::BlendExprContext * /*ctx*/) override { }

virtual void enterSelfSubRightExpr(RiddleParser::SelfSubRightExprContext * /*ctx*/) override { }
virtual void exitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext * /*ctx*/) override { }

Expand All @@ -91,9 +94,6 @@ class RiddleParserBaseListener : public RiddleParserListener {
virtual void enterNumberExpr(RiddleParser::NumberExprContext * /*ctx*/) override { }
virtual void exitNumberExpr(RiddleParser::NumberExprContext * /*ctx*/) override { }

virtual void enterObjValExpr(RiddleParser::ObjValExprContext * /*ctx*/) override { }
virtual void exitObjValExpr(RiddleParser::ObjValExprContext * /*ctx*/) override { }

virtual void enterMulAssignExpr(RiddleParser::MulAssignExprContext * /*ctx*/) override { }
virtual void exitMulAssignExpr(RiddleParser::MulAssignExprContext * /*ctx*/) override { }

Expand Down
8 changes: 4 additions & 4 deletions parser/RiddleParserBaseVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class RiddleParserBaseVisitor : public RiddleParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitBlendExpr(RiddleParser::BlendExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *ctx) override {
return visitChildren(ctx);
}
Expand Down Expand Up @@ -115,10 +119,6 @@ class RiddleParserBaseVisitor : public RiddleParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitObjValExpr(RiddleParser::ObjValExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitMulAssignExpr(RiddleParser::MulAssignExprContext *ctx) override {
return visitChildren(ctx);
}
Expand Down
6 changes: 3 additions & 3 deletions parser/RiddleParserListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class RiddleParserListener : public antlr4::tree::ParseTreeListener {
virtual void enterObjectExpr(RiddleParser::ObjectExprContext *ctx) = 0;
virtual void exitObjectExpr(RiddleParser::ObjectExprContext *ctx) = 0;

virtual void enterBlendExpr(RiddleParser::BlendExprContext *ctx) = 0;
virtual void exitBlendExpr(RiddleParser::BlendExprContext *ctx) = 0;

virtual void enterSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *ctx) = 0;
virtual void exitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *ctx) = 0;

Expand All @@ -89,9 +92,6 @@ class RiddleParserListener : public antlr4::tree::ParseTreeListener {
virtual void enterNumberExpr(RiddleParser::NumberExprContext *ctx) = 0;
virtual void exitNumberExpr(RiddleParser::NumberExprContext *ctx) = 0;

virtual void enterObjValExpr(RiddleParser::ObjValExprContext *ctx) = 0;
virtual void exitObjValExpr(RiddleParser::ObjValExprContext *ctx) = 0;

virtual void enterMulAssignExpr(RiddleParser::MulAssignExprContext *ctx) = 0;
virtual void exitMulAssignExpr(RiddleParser::MulAssignExprContext *ctx) = 0;

Expand Down
4 changes: 2 additions & 2 deletions parser/RiddleParserVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class RiddleParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitObjectExpr(RiddleParser::ObjectExprContext *context) = 0;

virtual std::any visitBlendExpr(RiddleParser::BlendExprContext *context) = 0;

virtual std::any visitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *context) = 0;

virtual std::any visitAndAssignExpr(RiddleParser::AndAssignExprContext *context) = 0;
Expand All @@ -69,8 +71,6 @@ class RiddleParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitNumberExpr(RiddleParser::NumberExprContext *context) = 0;

virtual std::any visitObjValExpr(RiddleParser::ObjValExprContext *context) = 0;

virtual std::any visitMulAssignExpr(RiddleParser::MulAssignExprContext *context) = 0;

virtual std::any visitXorAssignExpr(RiddleParser::XorAssignExprContext *context) = 0;
Expand Down
51 changes: 51 additions & 0 deletions src/Setup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Setup.h"
#include <filesystem>
#include <string>

namespace Riddle {

binaryOpMapTeleTy binaryOpMapTele;

const std::unordered_map<std::string, std::function<llvm::Type *(llvm::IRBuilder<> &)>> SampleType = {
{"int", [](llvm::IRBuilder<> &Builder) { return Builder.getInt32Ty(); }},
{"short", [](llvm::IRBuilder<> &Builder) { return Builder.getInt16Ty(); }},
{"char", [](llvm::IRBuilder<> &Builder) { return Builder.getInt8Ty(); }},
{"bool", [](llvm::IRBuilder<> &Builder) { return Builder.getInt1Ty(); }},
{"double", [](llvm::IRBuilder<> &Builder) { return Builder.getDoubleTy(); }},
{"float", [](llvm::IRBuilder<> &Builder) { return Builder.getFloatTy(); }},
};


void __attribute__((constructor)) initSetup() {
#define BOM_ARG llvm::IRBuilder<> &builder, llvm::Value *v1, llvm::Value *v2
llvm::LLVMContext ctx;
llvm::IRBuilder<> builder(ctx);
for(const auto &[name, func]: SampleType) {
llvm::Type* type = func(builder);
binaryOpMapTele[""][type][type] = [](BOM_ARG) { return v2; };
binaryOpMapTele["+"][type][type] = [](BOM_ARG) { return builder.CreateAdd(v1, v2, "AddV"); };
binaryOpMapTele["-"][type][type] = [](BOM_ARG) { return builder.CreateSub(v1, v2, "SubV"); };
binaryOpMapTele["*"][type][type] = [](BOM_ARG) { return builder.CreateMul(v1, v2, "MulV"); };
binaryOpMapTele["/"][type][type] = [](BOM_ARG) { return builder.CreateFDiv(v1, v2, "DivV"); };
binaryOpMapTele["%"][type][type] = [](BOM_ARG) { return builder.CreateSRem(v1, v2, "ModV"); };
binaryOpMapTele["<<"][type][type] = [](BOM_ARG) { return builder.CreateShl(v1, v2, "ShlV"); };
binaryOpMapTele[">>"][type][type] = [](BOM_ARG) { return builder.CreateAShr(v1, v2, "AShrV"); };
binaryOpMapTele[">>>"][type][type] = [](BOM_ARG) { return builder.CreateLShr(v1, v2, "LShrV"); };
binaryOpMapTele["^"][type][type] = [](BOM_ARG) { return builder.CreateXor(v1, v2, "XorV"); };
binaryOpMapTele["&"][type][type] = [](BOM_ARG) { return builder.CreateAnd(v1, v2, "AndV"); };
binaryOpMapTele["&&"][type][type] = [](BOM_ARG) { return builder.CreateAnd(v1, v2, "LAndV"); };
binaryOpMapTele["|"][type][type] = [](BOM_ARG) { return builder.CreateOr(v1, v2, "OrV"); };
binaryOpMapTele["||"][type][type] = [](BOM_ARG) { return builder.CreateOr(v1, v2, "LOrV"); };
binaryOpMapTele[">"][type][type] = [](BOM_ARG) { return builder.CreateICmpSGT(v1, v2, "cmpV"); };
binaryOpMapTele[">="][type][type] = [](BOM_ARG) { return builder.CreateICmpSGE(v1, v2, "cmpV"); };
binaryOpMapTele["<"][type][type] = [](BOM_ARG) { return builder.CreateICmpSLT(v1, v2, "cmpV"); };
binaryOpMapTele["<="][type][type] = [](BOM_ARG) { return builder.CreateICmpSLE(v1, v2, "cmpV"); };
if(type == SampleType.at("double")(builder) || type == SampleType.at("float")(builder)) {
binaryOpMapTele["!="][type][type] = [](BOM_ARG) { return builder.CreateFCmpUNE(v1, v2, "fne"); };
} else {
binaryOpMapTele["!="][type][type] = [](BOM_ARG) { return builder.CreateICmpNE(v1, v2, "ne"); };
}
}
#undef BOM_ARG
}
}// namespace Riddle
65 changes: 65 additions & 0 deletions src/Setup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef RIDDLE_LANGUAGE_SETUP_H
#define RIDDLE_LANGUAGE_SETUP_H

#include <functional>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Value.h>
#include <string>
#include <unordered_map>

namespace Riddle {
typedef std::unordered_map<std::string, std::unordered_map<std::string, std::function<llvm::Value *(llvm::IRBuilder<> &, llvm::Value *)>>>castMapTy;
typedef std::unordered_map<std::string, std::unordered_map<llvm::Type*, std::pmr::unordered_map<llvm::Type*, std::function<llvm::Value *(llvm::IRBuilder<> &, llvm::Value *, llvm::Value *)>>>> binaryOpMapTeleTy;
const static castMapTy castMapTemplate = {
{"int",
{
{"float", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateSIToFP(value, builder.getFloatTy(), "IntToFloat");
}},
{"double", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateSIToFP(value, builder.getDoubleTy(), "IntToDouble");
}},
{"short", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateTrunc(value, builder.getInt16Ty(), "IntToShort");
}},
{"char", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateTrunc(value, builder.getInt8Ty(), "IntToChar");
}},
}},
{"float",

{
{"int", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPToSI(value, builder.getInt32Ty(), "FloatToInt");
}},
{"short", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPToSI(value, builder.getInt16Ty(), "FloatToShort");
}},
{"char", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPToSI(value, builder.getInt8Ty(), "FloatToChar");
}},
{"double", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPExt(value, builder.getDoubleTy(), "FloatToDouble");
}},
}},
{"double",

{
{"int", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPToSI(value, builder.getInt32Ty(), "DoubleToInt");
}},
{"short", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPToSI(value, builder.getInt16Ty(), "DoubleToShort");
}},
{"char", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPToSI(value, builder.getInt8Ty(), "DoubleToChar");
}},
{"float", [](llvm::IRBuilder<> &builder, llvm::Value *value) -> llvm::Value * {
return builder.CreateFPExt(value, builder.getFloatTy(), "DoubleToFloat");
}},
}}};
extern binaryOpMapTeleTy binaryOpMapTele;
extern const std::unordered_map<std::string, std::function<llvm::Type *(llvm::IRBuilder<> &)>> SampleType;
}// namespace Riddle

#endif//RIDDLE_LANGUAGE_SETUP_H
2 changes: 1 addition & 1 deletion src/Tools/GenTools.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "GenTools.h"
#include "../Setup.h"
#include "RiddleLexer.h"
#include "Setup.h"

namespace Riddle {
bool isTerminalNode(antlr4::tree::ParseTree *tree) {
Expand Down
10 changes: 4 additions & 6 deletions src/Tools/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace Riddle {
Linker::Linker() {
}
std::string Linker::findSourceLib(const std::string &libPackName, const std::string &sourcePath) {
std::string libName = getLibName(libPackName);
auto files = Files::getTreeSource(sourcePath);
for(int i = (int)files.size() - 1; i >= 0; i--) {
const std::string libName = getLibName(libPackName);
const auto files = Files::getTreeSource(sourcePath);
for(int i = static_cast<int>(files.size()) - 1; i >= 0; i--) {
fs::path filePath(files[i]);
std::string name = filePath.filename().string();
name = name.substr(0, name.size() - 4);
Expand All @@ -36,9 +36,7 @@ namespace Riddle {
return "UNKNOWN";
}
std::string Linker::findLib(const std::string &libPackName, const std::string &sourcePath) {
std::string sp;
//source
sp = findSourceLib(libPackName, sourcePath);
std::string sp = findSourceLib(libPackName, sourcePath);
if(sp != "UNKNOWN") return sp;
return "UNKNOWN";
}
Expand Down
1 change: 0 additions & 1 deletion src/Tools/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Riddle {
/// @brief 从多个路径寻找库
/// @param libPackName 需要寻找的库的包名
/// @param sourcePath 主动导入该库的源文件的绝对路径
/// @param packName 源文件的包名
/// @return 返回库源文件的绝对路径
std::string findLib(const std::string &libPackName, const std::string &sourcePath);
};
Expand Down
7 changes: 0 additions & 7 deletions src/Tools/Setup.cpp

This file was deleted.

Loading

0 comments on commit ab23eb6

Please sign in to comment.