From 166de9e0bcb7ac0afe19a4272ae0e490614177ef Mon Sep 17 00:00:00 2001 From: niming Date: Wed, 27 Apr 2022 21:00:35 +0800 Subject: [PATCH 01/14] test zfx_script --- projects/ZenoFX/ppw.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/ZenoFX/ppw.cpp b/projects/ZenoFX/ppw.cpp index b04b758505..3afdb54bff 100644 --- a/projects/ZenoFX/ppw.cpp +++ b/projects/ZenoFX/ppw.cpp @@ -7,7 +7,7 @@ #include #include #include "dbg_printf.h" - +//this is git test namespace { static zfx::Compiler compiler; From 849cfc45a85ae4d7e15e1a2ae8b5570286991342 Mon Sep 17 00:00:00 2001 From: niming Date: Thu, 28 Apr 2022 14:49:07 +0800 Subject: [PATCH 02/14] test2 --- projects/ZenoFX/ppw.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/ZenoFX/ppw.cpp b/projects/ZenoFX/ppw.cpp index 3afdb54bff..0e7c9c30eb 100644 --- a/projects/ZenoFX/ppw.cpp +++ b/projects/ZenoFX/ppw.cpp @@ -8,6 +8,7 @@ #include #include "dbg_printf.h" //this is git test +//this is git test2 namespace { static zfx::Compiler compiler; From e6b9850bea1ba30e5d7c6800478e79af9855aa92 Mon Sep 17 00:00:00 2001 From: niming Date: Fri, 29 Apr 2022 19:53:07 +0800 Subject: [PATCH 03/14] test Lex --- projects/ZenoFX/ZFX/Lexical.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/projects/ZenoFX/ZFX/Lexical.h b/projects/ZenoFX/ZFX/Lexical.h index 848d4fa91f..2d663b8f90 100644 --- a/projects/ZenoFX/ZFX/Lexical.h +++ b/projects/ZenoFX/ZFX/Lexical.h @@ -4,14 +4,27 @@ namespace zfx { -inline char opchars[] = "+-*/%=(),.;<>!&|^?:"; -inline std::set opstrs = { +//inline char opchars[] = "+-*/%=(),.;<>!&|^?:"; +// +enum class Op { + '+'; + '-' + +}; + +/*inline std::set opstrs = { "(", ")", ",", ".", ";", "+", "-", "*", "/", "%", "=", "+=", "-=", "*=", "/=", "%=", "==", "!=", "<", "<=", ">", ">=", "&", "&!", "|", "^", "!", "?", ":", }; +*/ +enum class Seprator { + '(', + + +}; inline bool is_literial_atom(std::string const &s) { if (!s.size()) return false; From b7114029919ab2e0fcf3152332390a0d95d98405 Mon Sep 17 00:00:00 2001 From: niming Date: Tue, 3 May 2022 18:00:12 +0800 Subject: [PATCH 04/14] creat A test Project PiLeiFX --- projects/PiLeiFX/Ast.h | 0 projects/PiLeiFX/IR.h | 0 projects/PiLeiFX/Parser.h | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 projects/PiLeiFX/Ast.h create mode 100644 projects/PiLeiFX/IR.h create mode 100644 projects/PiLeiFX/Parser.h diff --git a/projects/PiLeiFX/Ast.h b/projects/PiLeiFX/Ast.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/projects/PiLeiFX/IR.h b/projects/PiLeiFX/IR.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/projects/PiLeiFX/Parser.h b/projects/PiLeiFX/Parser.h new file mode 100644 index 0000000000..e69de29bb2 From 8d8cd0fcdcd36250eda2ef1e7ee7c12d80ec3c39 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Sat, 7 May 2022 18:54:34 +0800 Subject: [PATCH 05/14] PiLeiFX --- projects/PiLeiFX/Ast.h | 0 projects/PiLeiFX/IR.h | 0 projects/PiLeiFX/Parser.h | 0 projects/PiLeiFX/ZenoFX/Ast.h | 47 ++++++++++++++ projects/PiLeiFX/ZenoFX/IR.h | 21 +++++++ projects/PiLeiFX/ZenoFX/Lexical.h | 90 +++++++++++++++++++++++++++ projects/PiLeiFX/ZenoFX/Parser.h | 21 +++++++ projects/PiLeiFX/ZenoFX/include/x64.h | 7 +++ projects/PiLeiFX/ZenoFX/include/zfx.h | 63 +++++++++++++++++++ projects/PiLeiFX/ZenoFX/parser.cpp | 5 ++ projects/ZenoFX/ZFX/include/zfx/zfx.h | 3 +- 11 files changed, 256 insertions(+), 1 deletion(-) delete mode 100644 projects/PiLeiFX/Ast.h delete mode 100644 projects/PiLeiFX/IR.h delete mode 100644 projects/PiLeiFX/Parser.h create mode 100644 projects/PiLeiFX/ZenoFX/Ast.h create mode 100644 projects/PiLeiFX/ZenoFX/IR.h create mode 100644 projects/PiLeiFX/ZenoFX/Lexical.h create mode 100644 projects/PiLeiFX/ZenoFX/Parser.h create mode 100644 projects/PiLeiFX/ZenoFX/include/x64.h create mode 100644 projects/PiLeiFX/ZenoFX/include/zfx.h create mode 100644 projects/PiLeiFX/ZenoFX/parser.cpp diff --git a/projects/PiLeiFX/Ast.h b/projects/PiLeiFX/Ast.h deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/projects/PiLeiFX/IR.h b/projects/PiLeiFX/IR.h deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/projects/PiLeiFX/Parser.h b/projects/PiLeiFX/Parser.h deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h new file mode 100644 index 0000000000..828a2760e1 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -0,0 +1,47 @@ +// +// Created by admin on 2022/5/6. +// + +#pragma once + +#include +#include +#include +namespace zfx { + + class AstNode; + class Variable; + class Binary; + class Unary; + + class AstVisitor { + virtual ~visitor() { + + } + }; + + class AstTypeArray { + + }; + + class AstNode { + public: + + virtual void dump() {} + + }; + /* + struct AST { + using Iter = typename std::vector::iterator; + + Iter iter; + std::string Token; + + + void dump() {} + + }; + */ + +} + diff --git a/projects/PiLeiFX/ZenoFX/IR.h b/projects/PiLeiFX/ZenoFX/IR.h new file mode 100644 index 0000000000..5cac25c742 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR.h @@ -0,0 +1,21 @@ +// +// Created by admin on 2022/5/7. +// + +#pragma once + +namespace zfx { + class IRNode { + + }; + + class BinaryNode { + + }; + + class UnaryNode { + + }; + + +} diff --git a/projects/PiLeiFX/ZenoFX/Lexical.h b/projects/PiLeiFX/ZenoFX/Lexical.h new file mode 100644 index 0000000000..923b71cb3f --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Lexical.h @@ -0,0 +1,90 @@ +// +// Created by admin on 2022/5/7. +// + +#pragma once +//inline char opchars[] = "+-*/%=(),.;<>!&|^?:"; +/*inline std::set opstrs = { + "(", ")", ",", ".", ";", + "+", "-", "*", "/", "%", "=", + "+=", "-=", "*=", "/=", "%=", + "==", "!=", "<", "<=", ">", ">=", + "&", "&!", "|", "^", "!", "?", ":", + }; +*/ +namespace zfx { + + enum TokenKind {Op, Seprator, KeywordKind, Eof}; + enum class Op{ + Plus, //+ + Minus, //- + Multiply, // * + Divide, // / + Modules, // % + Assign, // = + L, // < + G, // > + LE, // <= + GE, // >= + MultiplyAssign, // *= + DivideAssign, // /= + ModulesAssign, // %= + PlusAssign, //+= + MinusAssign, //-= + BitNot, //~ + BitAnd, //& + BitXor, //^ + BitOr, // | + At, //@ + Comma, //, + Dot, //. + Not, // ! + And, // && + Or, // || + QuesstionMark // ? + }; + + enum class Seprator { + OpenBracket = 0, //[ + CloseBracket, //] + OpenParen, //( + CloseParen, //) + OpenBrace, //{ + CloseBrace, //} + Colon, //: + SemiColon, //; + }; + + enum class KeywordKind { + + }; + + std::string toString(TokenKind kind); + std::string toString(Op op); + + struct Token { + TokenKind kind; + std::string text; + + }; + + + Class CharStream { + public: + std::string data; + CharStream(const std::string& data) : data(data){ + + } + + + }; + + + class Scanner { + public: + std::vector tokens; + CharStream stream; + }; + +} + diff --git a/projects/PiLeiFX/ZenoFX/Parser.h b/projects/PiLeiFX/ZenoFX/Parser.h new file mode 100644 index 0000000000..bfe7d851bd --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Parser.h @@ -0,0 +1,21 @@ +// +// Created by admin on 2022/5/7. +// +#pragma once +#include "Lexical.h" +#include + +namespace zfx { + + class Parser { + public: + Scanner& scanner; + Parser(Scanner& scanner) : scanner(scanner) { + + } + + //begin Parser and Generate Ast + }; + +} + diff --git a/projects/PiLeiFX/ZenoFX/include/x64.h b/projects/PiLeiFX/ZenoFX/include/x64.h new file mode 100644 index 0000000000..d667fb05db --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/include/x64.h @@ -0,0 +1,7 @@ +// +// Created by admin on 2022/5/7. +// + +#pragma once + + diff --git a/projects/PiLeiFX/ZenoFX/include/zfx.h b/projects/PiLeiFX/ZenoFX/include/zfx.h new file mode 100644 index 0000000000..d0fa440947 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/include/zfx.h @@ -0,0 +1,63 @@ +// +// Created by admin on 2022/5/6. +// + +#pragma once + +#include +#include +#include +#include +#include + +namespace zfx { + + struct Options { + + // Options two + Options() {} + + Options(int a) {} + }; + + struct Program { + std::vector> symbols; + std::vector> params; + std::string assembly; + + auto const& get_assembly() { + return assembly; + } + + auto const& get_symbols() { + return symbols; + } + + auto const& get_params() { + + } + + void + + }; + + struct Compiler { + std::map> cache; + + Program *compile(const std::string &code, const Options &option) { + std::ostringstream ss; + ss << code << ""; + + auto key = ss.str(); + auto it = cache.find(key); + if (it != cache.end()) { + return it->second; + } + + } + + }; + + +} + diff --git a/projects/PiLeiFX/ZenoFX/parser.cpp b/projects/PiLeiFX/ZenoFX/parser.cpp new file mode 100644 index 0000000000..9675851225 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/parser.cpp @@ -0,0 +1,5 @@ +// +// Created by admin on 2022/5/7. +// + + diff --git a/projects/ZenoFX/ZFX/include/zfx/zfx.h b/projects/ZenoFX/ZFX/include/zfx/zfx.h index 324468df2c..483231a9d3 100644 --- a/projects/ZenoFX/ZFX/include/zfx/zfx.h +++ b/projects/ZenoFX/ZFX/include/zfx/zfx.h @@ -9,7 +9,7 @@ #include namespace zfx { - +//增加一个编译器选项,主要是一些默认的值 struct Options { bool const_parametrize = true; bool global_localize = true; @@ -21,6 +21,7 @@ struct Options { bool reassign_parameters = true; bool reassign_channels = true; + //开启编译优化选项 bool merge_identical = true; bool kill_unreachable = true; bool constant_fold = true; From 4f3eb6c348d59b97aa8bbc355836cf7822f5a802 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Sun, 8 May 2022 18:32:03 +0800 Subject: [PATCH 06/14] fix Lexical --- projects/PiLeiFX/ZenoFX/IR.h | 2 +- projects/PiLeiFX/ZenoFX/Lexical.cpp | 4 ++ projects/PiLeiFX/ZenoFX/Lexical.h | 75 ++++++++++++++++++++++++- projects/PiLeiFX/ZenoFX/Parser.h | 26 +++++++++ projects/PiLeiFX/ZenoFX/visitor.h | 6 ++ projects/PiLeiFX/ZenoFX/x64/Assembler.h | 31 ++++++++++ projects/PiLeiFX/ZenoFX/zfx.cpp | 21 +++++++ projects/ZenoFX/ZFX/Lexical.h | 5 +- 8 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/Lexical.cpp create mode 100644 projects/PiLeiFX/ZenoFX/visitor.h create mode 100644 projects/PiLeiFX/ZenoFX/x64/Assembler.h create mode 100644 projects/PiLeiFX/ZenoFX/zfx.cpp diff --git a/projects/PiLeiFX/ZenoFX/IR.h b/projects/PiLeiFX/ZenoFX/IR.h index 5cac25c742..03e66874f0 100644 --- a/projects/PiLeiFX/ZenoFX/IR.h +++ b/projects/PiLeiFX/ZenoFX/IR.h @@ -14,7 +14,7 @@ namespace zfx { }; class UnaryNode { - + }; diff --git a/projects/PiLeiFX/ZenoFX/Lexical.cpp b/projects/PiLeiFX/ZenoFX/Lexical.cpp new file mode 100644 index 0000000000..f6524d56df --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Lexical.cpp @@ -0,0 +1,4 @@ +// +// Created by admin on 2022/5/8. +// +#include "Lexical.h" diff --git a/projects/PiLeiFX/ZenoFX/Lexical.h b/projects/PiLeiFX/ZenoFX/Lexical.h index 923b71cb3f..61dbf23c92 100644 --- a/projects/PiLeiFX/ZenoFX/Lexical.h +++ b/projects/PiLeiFX/ZenoFX/Lexical.h @@ -1,7 +1,9 @@ // // Created by admin on 2022/5/7. // - +#include +#include +#include #pragma once //inline char opchars[] = "+-*/%=(),.;<>!&|^?:"; /*inline std::set opstrs = { @@ -56,7 +58,7 @@ namespace zfx { }; enum class KeywordKind { - +//for instance if while for ... }; std::string toString(TokenKind kind); @@ -65,6 +67,8 @@ namespace zfx { struct Token { TokenKind kind; std::string text; + Token(TokenKind kind, const std::string& text) : kind(kind), text(text) {} + Token(TokenKind kind, char c) : kind(kind), text(std::string(1, c)) {} }; @@ -72,10 +76,32 @@ namespace zfx { Class CharStream { public: std::string data; + uint32_t pos = 0; + uint32_t line = 1; + uint32_t col = 0; CharStream(const std::string& data) : data(data){ } + char peak() { + return this->data[this->pos]; + } + + char next() { + char ch = this->data[this->pos++]; + if (ch == '\n') { + this->line++; + this->col = 0; + } else { + this->col++; + } + return ch; + } + + bool eof () { + return this->peak() == '\0'; + } + }; @@ -84,6 +110,51 @@ namespace zfx { public: std::vector tokens; CharStream stream; + Token next() { + if (this->tokens.empty()) { + auto t = this->getAToken(); + return t; + // + } else { + auto t = thi->tokens.front(); + // + } + } + + Token peek() { + + } + + Token peek2() { + // + } + + + private: + Token getAToken() { + + } + + + + void skipWhiteSpaces() { + while (this->isWhiteSpace()) { + + } + + } + + bool isWhiteSpace(char ch) { + return (ch == ' ' || ch == '\n' || ch == '\t'); + } + + bool isDigit(char ch) { + return (ch >= '0' && ch <= '9'); + } + + bool isLetter() {} + + }; } diff --git a/projects/PiLeiFX/ZenoFX/Parser.h b/projects/PiLeiFX/ZenoFX/Parser.h index bfe7d851bd..ee21d30660 100644 --- a/projects/PiLeiFX/ZenoFX/Parser.h +++ b/projects/PiLeiFX/ZenoFX/Parser.h @@ -3,7 +3,10 @@ // #pragma once #include "Lexical.h" +#include "Ast.h" #include +#include +#include namespace zfx { @@ -15,7 +18,30 @@ namespace zfx { } //begin Parser and Generate Ast + std::vector Error; + std::vector Warnings; +/* + auto parse() { + std::vector asts; + + return asts; + } + +*/ + + std::vector> parseStatement() { + + } + + std::shared_ptr parseVariable() {} + + std::shared_ptr parseAssignment() { + + } }; + std::vector parse(const std::string& code) { + + } } diff --git a/projects/PiLeiFX/ZenoFX/visitor.h b/projects/PiLeiFX/ZenoFX/visitor.h new file mode 100644 index 0000000000..665e86cb7e --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/visitor.h @@ -0,0 +1,6 @@ +// +// Created by admin on 2022/5/8. +// + +#pragma once + diff --git a/projects/PiLeiFX/ZenoFX/x64/Assembler.h b/projects/PiLeiFX/ZenoFX/x64/Assembler.h new file mode 100644 index 0000000000..4ee436c398 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/x64/Assembler.h @@ -0,0 +1,31 @@ +// +// Created by admin on 2022/5/7. +// + +#pragma once +#include +#include + +//cpp17 +namespace zfx ::x64{ + enum class AsmOpCode { + + }; + + enum class AsmJmpCode { + + }; + + enum class OpReg { + + }; + + enum class SimdType { + + }; + + struct SimdBuilder { + std::vector + }; +} + diff --git a/projects/PiLeiFX/ZenoFX/zfx.cpp b/projects/PiLeiFX/ZenoFX/zfx.cpp new file mode 100644 index 0000000000..85c0272595 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/zfx.cpp @@ -0,0 +1,21 @@ +// +// Created by admin on 2022/5/8. +// +#include "include/zfx.h" +#include +#include "parser.h" +namespace zfx { + +std::tuple<> compile_to_assembly (const std::string& code, const Options& options) { +#ifdef ZFX_PRINT_IR + std::cout << "start zfx" << std::endl; + std::cout << code << std::endl; +#endif +#ifdef ZFX_PRINT_IR + std::cout << "Parse Ast" << std::endl; + auto asts = parse(code); +} + + +} + diff --git a/projects/ZenoFX/ZFX/Lexical.h b/projects/ZenoFX/ZFX/Lexical.h index 2d663b8f90..27e69e06ed 100644 --- a/projects/ZenoFX/ZFX/Lexical.h +++ b/projects/ZenoFX/ZFX/Lexical.h @@ -26,7 +26,10 @@ enum class Seprator { }; -inline bool is_literial_atom(std::string const &s) { +inline bool + + +is_literial_atom(std::string const &s) { if (!s.size()) return false; if (isdigit(s[0]) || s.size() > 1 && s[0] == '-' && isdigit(s[1])) { return true; From 9b91d724f57852312204c9299aea4d8c6a9231c1 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Tue, 10 May 2022 19:28:40 +0800 Subject: [PATCH 07/14] continue cihou zfx --- projects/PiLeiFX/ZenoFX/Ast.h | 46 ++++++--- projects/PiLeiFX/ZenoFX/Lexical.cpp | 57 +++++++++++ projects/PiLeiFX/ZenoFX/Lexical.h | 87 ++++++++++++++--- projects/PiLeiFX/ZenoFX/Location.h | 94 +++++++++++++++++++ projects/PiLeiFX/ZenoFX/cuda/CAssembler.cpp | 14 +++ projects/PiLeiFX/ZenoFX/cuda/context.h | 12 +++ projects/PiLeiFX/ZenoFX/include/cuda.h | 8 ++ projects/PiLeiFX/ZenoFX/include/x64.h | 11 +++ projects/PiLeiFX/ZenoFX/include/zfx.h | 5 + projects/PiLeiFX/ZenoFX/visitor.h | 16 ++++ projects/PiLeiFX/ZenoFX/x64/Assembler.h | 8 ++ .../PiLeiFX/ZenoFX/x64/BuilderAssembler.cpp | 17 ++++ projects/PiLeiFX/ZenoFX/x64/Func.h | 13 +++ projects/ZenoFX/ZFX/cuda/NVRTC.cuh | 1 + 14 files changed, 366 insertions(+), 23 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/Location.h create mode 100644 projects/PiLeiFX/ZenoFX/cuda/CAssembler.cpp create mode 100644 projects/PiLeiFX/ZenoFX/cuda/context.h create mode 100644 projects/PiLeiFX/ZenoFX/include/cuda.h create mode 100644 projects/PiLeiFX/ZenoFX/x64/BuilderAssembler.cpp create mode 100644 projects/PiLeiFX/ZenoFX/x64/Func.h diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h index 828a2760e1..b7e7cb1399 100644 --- a/projects/PiLeiFX/ZenoFX/Ast.h +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -4,11 +4,21 @@ #pragma once +#include "Lexical.h" +#include "Location.h" #include #include #include +#include namespace zfx { - + enum class Ast_Node_Type { + Ast_Type_Statement, + Ast_Type_Declaration, + Ast_Type_Identifier, + Ast_Type_Integer_Literal, + Ast_Type_Binary_Op, + Ast_Type_Unary_Op + }; class AstNode; class Variable; class Binary; @@ -17,31 +27,45 @@ namespace zfx { class AstVisitor { virtual ~visitor() { + } }; - class AstTypeArray { + + class AstNode { + public: + Ast_Node_Type type ; + Position beginPos; + Position endPos; + bool isErrorNode {false}; + + virtual std::any accept(AstVisitor& visitor, std::string additional = ""); }; - class AstNode { + class Statement : public AstNode { public: + Ast_Node_Type type = Ast_Node_Type::Ast_Type_Statement; + Position beginPos; + Position endPos; + + }; - virtual void dump() {} + class Ast_Identifier { }; - /* - struct AST { - using Iter = typename std::vector::iterator; - Iter iter; - std::string Token; + class Ast_Binary_Op { + Op op; + }; - void dump() {} + class Ast_Unary_Op { }; - */ + class IntegerLiteral { + + }; } diff --git a/projects/PiLeiFX/ZenoFX/Lexical.cpp b/projects/PiLeiFX/ZenoFX/Lexical.cpp index f6524d56df..080f93d7d1 100644 --- a/projects/PiLeiFX/ZenoFX/Lexical.cpp +++ b/projects/PiLeiFX/ZenoFX/Lexical.cpp @@ -2,3 +2,60 @@ // Created by admin on 2022/5/8. // #include "Lexical.h" +#include +#include + +namespace zfx { + std::unordered_map tokenToString { + {TokenKind::Op, "Op"}, + {TokenKind::Seprator, "Seprator"}, + {TokenKind::KeywordKind, "KeywordKind"}, + {TokenKind::Eof, "Eof"} + } + std::unordered_map OpToString { + {Op::Plus, "Plus"} , + {Op::Minus, "Minus"}, + {Op::Multiply,"Multiply"}, + {Op::Divide, "Divide"}, + {Op::Modules, "Modules"}, + {Op::L, "L"}, + {Op::G, "G"}, // > + {Op::LE, "LE"}, // <= + {Op::GE, "GE"}, // >= + {Op::MultiplyAssign, "MultiplyAssign"}, // *= + {Op::DivideAssign, "DivideAssign"}, // /= + {Op::ModulesAssign, "ModulesAssign"}, // %= + {Op::PlusAssign,"PlusAssign"}, //+= + {Op::MinusAssign, "MinusAssign"}, //-= + {Op::BitNot, "BitNot"}, //~ + {Op::BitAnd, "BitAnd"}, //&{ BitXor, //^ + {Op::BitOr, "BitOr"}, // | + {Op::At, "At"}, //@ + {Op::Comma, "Comma"}, //, + {Op::Dot, "Dot"}, //. + {Op::Not, "Not"}, // ! + {Op::And, "And"}, // && + {Op::Or, "Or"}, // || + {Op::QuesstionMark, "?"} // ? + } + std::unordered_map Scanner::KeywordMap = { + {"Pos",KeywordKind::Pos}, + {"data", KeywordKind::data}, + {"frame", KeywordKind::frame} + }; + std::string toString(TokenKind kind) { + auto it = tokenToString.find(kind); + if (it != tokenToString.end()) { + return it->second; + } + return "UnKnown"; + } + + std::string toString(Op op) { + auto it = OpToString.find(op); + if (it != OpToString.end()) { + return it->second; + } + return "UnKnown"; + } +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Lexical.h b/projects/PiLeiFX/ZenoFX/Lexical.h index 61dbf23c92..a2e8270933 100644 --- a/projects/PiLeiFX/ZenoFX/Lexical.h +++ b/projects/PiLeiFX/ZenoFX/Lexical.h @@ -1,6 +1,7 @@ // // Created by admin on 2022/5/7. // +#include "Location.h" #include #include #include @@ -16,7 +17,7 @@ */ namespace zfx { - enum TokenKind {Op, Seprator, KeywordKind, Eof}; + enum class TokenKind {Op, Seprator, KeywordKind, Eof}; enum class Op{ Plus, //+ Minus, //- @@ -58,7 +59,12 @@ namespace zfx { }; enum class KeywordKind { -//for instance if while for ... +//for instance if while for ... to be added later as appropriate + Pos, + data, + frame, + rad, + $// }; std::string toString(TokenKind kind); @@ -67,9 +73,12 @@ namespace zfx { struct Token { TokenKind kind; std::string text; - Token(TokenKind kind, const std::string& text) : kind(kind), text(text) {} - Token(TokenKind kind, char c) : kind(kind), text(std::string(1, c)) {} - + Position pos; + Token(TokenKind kind, const std::string& text, const Position& pos) : kind(kind), text(text), pos(pos) {} + Token(TokenKind kind, char c, const Position& pos) : kind(kind), text(std::string(1, c)), pos(pos) {} + std::string toString() { + return std::string("Token") + ":" + this->pos.toString() + this->text; + } }; @@ -102,14 +111,20 @@ namespace zfx { return this->peak() == '\0'; } - + Position getPosition() { + //return the line and column numbers of the current charactor + return Position(this->line, this->col); + } }; class Scanner { - public: + private: std::vector tokens; - CharStream stream; + CharStream& stream; + Position lastPos; + static std::unordered_map KeywordMap; + public: Token next() { if (this->tokens.empty()) { auto t = this->getAToken(); @@ -122,26 +137,68 @@ namespace zfx { } Token peek() { - + if(this->tokens.empty()) { + auto t = this->getAToken(); + this->tokens.push_back(t); + return t; + } else { + auto t = this->tokens.front(); + return t; + } } Token peek2() { - // + while (this->tokens.size() < 2) { + auto t = this->getAToken(); + this->tokens.push_back(t); + } } + Position getNextPos() { + } + + Position getLastPos() { + + } private: Token getAToken() { + this->skipWhiteSpaces(); + auto pos = this->stream.getPosition(); + if (this->stream.eof() == '\n') { + return Token(TokenKind::Eof, "EOF", pos); + } else { + auto ch = this->stream.peek(); + if (ch == '#') { + this->skipSingleComment(); + return this->getAToken(); + } else if (this->isDigit(ch)) { + this->stream.next(); + auto ch1 = this->stream.peek(); + std::string literal = ""; + if (ch == '0') { + + } + } else if () { + + } + } } void skipWhiteSpaces() { while (this->isWhiteSpace()) { - + this->stream.next(); } + } + void skipSingleLineComment() { + this->stream.next(); + while (this->stream.peek() != '\n' && this->stream.eof()) { + this->stream.next(); + } } bool isWhiteSpace(char ch) { @@ -153,8 +210,14 @@ namespace zfx { } bool isLetter() {} - + Token parseIdentifer() { + Token token; + if (this->KeywordMap.find(token.text)) { + + } + return token; + } }; } diff --git a/projects/PiLeiFX/ZenoFX/Location.h b/projects/PiLeiFX/ZenoFX/Location.h new file mode 100644 index 0000000000..e9e723ff51 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Location.h @@ -0,0 +1,94 @@ +// +// Created by admin on 2022/5/8. +// + +#pragma once + +#include +#include +namespace zfx { + class Position { + public: + uint32_t line; + uint32_t col; + Position(uint32_t line, uint32_t col) : line(line), col(col){ + + } + + Position(const Position& rhs) { + this->line = rhs.line; + this->col = rhs.col; + } + bool operator==(const Position& rhs) const { + return this->col == rhs.col && this->line == rhs.line; + } + + bool operator!=(const Position& rhs) const { + return !(*this == rhs); + } + + bool operator<(const Position& rhs) const { + if (this->line == rhs.line) { + return this->col < rhs.col; + } else { + return line < rhs.line; + } + } + + bool operator>(const Position& rhs) const { + if (this->line == rhs.line) { + return this->col > rhs.col; + } else { + return this->line > rhs.line; + } + } + + bool operator<=(const Position& rhs) const { + return *this == rhs || *this < rhs; + } + + bool operator>=(const Position& rhs) const { + return *this == rhs || *this > rhs; + } + + std::string toString() { + return ("(ln" + std::to_string(this->line) + + ", col : " + std::to_string(this->col) + ")"); + } + }; + + class Location { + public: + Position begin, end; + Location() : begin(0, 0), end(0, 0){ + + } + + Location(const Position& begin, uint32_t length) : begin(begin), end(begin.line, begin.col + length) { + + } + + Location(const Location& begin, const Location& end) : begin(begin), end(end){ + + } + + bool operator==(const Location& rhs) { + + } + + bool operator!=(const Location& rhs) { + + } + + bool operator<(const Location& rhs) { + + } + + bool operator>(const Location& rhs) { + + } + std::string toString() { + + } + }; +} diff --git a/projects/PiLeiFX/ZenoFX/cuda/CAssembler.cpp b/projects/PiLeiFX/ZenoFX/cuda/CAssembler.cpp new file mode 100644 index 0000000000..9e8c1b9b96 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/cuda/CAssembler.cpp @@ -0,0 +1,14 @@ +// +// Created by admin on 2022/5/9. +// + +#include + +namespace zfx::cuda { + + struct CUDABuilder { + + }; +} + +std::string diff --git a/projects/PiLeiFX/ZenoFX/cuda/context.h b/projects/PiLeiFX/ZenoFX/cuda/context.h new file mode 100644 index 0000000000..9aa37699d9 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/cuda/context.h @@ -0,0 +1,12 @@ +// +// Created by admin on 2022/5/9. +// + +#pragma once + +namespace zfx::cuda { + class CUcontext { + + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/include/cuda.h b/projects/PiLeiFX/ZenoFX/include/cuda.h new file mode 100644 index 0000000000..3732120d5c --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/include/cuda.h @@ -0,0 +1,8 @@ +// +// Created by admin on 2022/5/9. +// +#pragma once + +namespace zfx::cuda { + +} diff --git a/projects/PiLeiFX/ZenoFX/include/x64.h b/projects/PiLeiFX/ZenoFX/include/x64.h index d667fb05db..14bf4785f8 100644 --- a/projects/PiLeiFX/ZenoFX/include/x64.h +++ b/projects/PiLeiFX/ZenoFX/include/x64.h @@ -4,4 +4,15 @@ #pragma once +#include +#include +namespace zfx::x64 { + struct Executable { + }; + + struct Assembler { + std::map> cache; + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/include/zfx.h b/projects/PiLeiFX/ZenoFX/include/zfx.h index d0fa440947..ae1e7151fb 100644 --- a/projects/PiLeiFX/ZenoFX/include/zfx.h +++ b/projects/PiLeiFX/ZenoFX/include/zfx.h @@ -19,7 +19,12 @@ namespace zfx { Options(int a) {} }; + class CompileError { + private: + std::string message; + + }; struct Program { std::vector> symbols; std::vector> params; diff --git a/projects/PiLeiFX/ZenoFX/visitor.h b/projects/PiLeiFX/ZenoFX/visitor.h index 665e86cb7e..d92fb0c472 100644 --- a/projects/PiLeiFX/ZenoFX/visitor.h +++ b/projects/PiLeiFX/ZenoFX/visitor.h @@ -4,3 +4,19 @@ #pragma once +#include "Ast.h" +#include +#include +/* +namespace zfx { + class AstVisitor { + + virtual std::any visit() { + + } + virtual ~AstVisitor() { + + } + }; +} +*/ \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/x64/Assembler.h b/projects/PiLeiFX/ZenoFX/x64/Assembler.h index 4ee436c398..46f1487bbd 100644 --- a/projects/PiLeiFX/ZenoFX/x64/Assembler.h +++ b/projects/PiLeiFX/ZenoFX/x64/Assembler.h @@ -27,5 +27,13 @@ namespace zfx ::x64{ struct SimdBuilder { std::vector }; + + class Inst { + + }; + + class Oprand{ + + }; } diff --git a/projects/PiLeiFX/ZenoFX/x64/BuilderAssembler.cpp b/projects/PiLeiFX/ZenoFX/x64/BuilderAssembler.cpp new file mode 100644 index 0000000000..fd32a1c60f --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/x64/BuilderAssembler.cpp @@ -0,0 +1,17 @@ +// +// Created by admin on 2022/5/10. +// +#include + +namespace zfx { + #define ERROR_IF(x) do { \ + if(x) { \ + error("%s", #x); \ + } \ + } while(0) + + struct ImplAssembler { + + }; +} + diff --git a/projects/PiLeiFX/ZenoFX/x64/Func.h b/projects/PiLeiFX/ZenoFX/x64/Func.h new file mode 100644 index 0000000000..e40b1dd799 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/x64/Func.h @@ -0,0 +1,13 @@ +// +// Created by admin on 2022/5/10. +// + +#pragma once +#include + +namespace zfx::x64 { + struct BuiltFunc { +#define DEF_FUN1(name) static void func_##name(float *a) {} +#define DEF_FUN2(name) static void func_##name(float *a , float *b) {} + }; +} \ No newline at end of file diff --git a/projects/ZenoFX/ZFX/cuda/NVRTC.cuh b/projects/ZenoFX/ZFX/cuda/NVRTC.cuh index d6bc98a2b0..456e6f93a7 100644 --- a/projects/ZenoFX/ZFX/cuda/NVRTC.cuh +++ b/projects/ZenoFX/ZFX/cuda/NVRTC.cuh @@ -127,6 +127,7 @@ static CUmodule compileJITModule #if 0 + int main() { CU(cuInit(0)); From ea06fde8902f1103617cd934970ae73984cc4267 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Wed, 11 May 2022 20:37:25 +0800 Subject: [PATCH 08/14] cihou ast parser --- projects/PiLeiFX/ZenoFX/Ast.h | 100 +++++++++++++++++++++--- projects/PiLeiFX/ZenoFX/CodeGen.h | 6 ++ projects/PiLeiFX/ZenoFX/IR.h | 48 +++++++++++- projects/PiLeiFX/ZenoFX/Parser.h | 41 +++++++++- projects/PiLeiFX/ZenoFX/Symbol.h | 27 +++++++ projects/PiLeiFX/ZenoFX/parser.cpp | 1 + projects/PiLeiFX/ZenoFX/x64/Func.h | 4 +- projects/ZenoFX/ZFX/KillUnreachable.cpp | 1 + 8 files changed, 212 insertions(+), 16 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/CodeGen.h create mode 100644 projects/PiLeiFX/ZenoFX/Symbol.h diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h index b7e7cb1399..fbf51b3b21 100644 --- a/projects/PiLeiFX/ZenoFX/Ast.h +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -10,6 +10,8 @@ #include #include #include +#include +#include namespace zfx { enum class Ast_Node_Type { Ast_Type_Statement, @@ -27,44 +29,120 @@ namespace zfx { class AstVisitor { virtual ~visitor() { - } + + virtual std::any visitVariable(Variable& variable, std::string additional = ""); + + virtual std::any visitFunctionCall(FunctionCall& functionCall, std::string additional = ""); + + virtual std::any visitBinary(Binary& binary, std::strig additional = ""); + + virtual std::any visitUnary(Unary& unary, std::string additional = ""); + + virtual std::any visitTenary(Tenary& tenary, std::string additional = ""); + + virtual std::any visitAssign(AssignStmt& assign, std::string additional = ""); + + virtual std::any visitLiteral(Literal& literal, std::string additional = ""); + + virtual std::any visitIfStmt(ExprIfStmt& exprIfStmt, std::string additional = ""); + + virtual std::any visitForStmt() }; class AstNode { public: - Ast_Node_Type type ; - Position beginPos; + Position beginPos;// Position endPos; bool isErrorNode {false}; - virtual std::any accept(AstVisitor& visitor, std::string additional = ""); - + virtual std::any accept(AstVisitor& visitor, std::string additional = "") = 0; }; class Statement : public AstNode { public: - Ast_Node_Type type = Ast_Node_Type::Ast_Type_Statement; - Position beginPos; - Position endPos; + int id; + int dim = 0; + Statement(const Position& beginPos, const Position& endPos, bool isErrorNode, int id, int dim) + : AstNode(beginPos, endPos, isErrorNode),id(id), dim(dim) { + } }; + class Ast_Identifier { }; - class Ast_Binary_Op { + class Expression : public AstNode{ + + }; + + class Variable : public Expression { + std::string name; + //if we + //std::shared_ptr<>; + + std::string toString() { + return this->name; + } + }; + + class FunctionCall : public Expression { + public: + std::string name; + std::vector> arguments; + FunctionCall() { + + } + + virtual std::string + }; + + class Binary { + public: Op op; + std::shared_ptr exp1;// left expression + std::shared_ptr exp2;// right expression + Binary() { + } + std::string toString() { + + } }; - class Ast_Unary_Op { + class Unary { + public: + Op op; + bool isPrefix;//whether is prefix operation; + Unary() { + } }; - class IntegerLiteral { + class Tenary { + //Tenary Operation; + public: + std::shared_ptr cond; + std::shared_ptr lhs; + std::shared_ptr rhs; + }; + + class AssignStmt { + public: + std::shared_ptr lhs; + std::shared_ptr value_to_assign; + }; + + + class ExprIfStmt { + + }; + + + class Literal { }; } diff --git a/projects/PiLeiFX/ZenoFX/CodeGen.h b/projects/PiLeiFX/ZenoFX/CodeGen.h new file mode 100644 index 0000000000..e707802f4d --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/CodeGen.h @@ -0,0 +1,6 @@ +// +// Created by admin on 2022/5/11. +// + +#pragma once + diff --git a/projects/PiLeiFX/ZenoFX/IR.h b/projects/PiLeiFX/ZenoFX/IR.h index 03e66874f0..49ad6afef5 100644 --- a/projects/PiLeiFX/ZenoFX/IR.h +++ b/projects/PiLeiFX/ZenoFX/IR.h @@ -3,7 +3,10 @@ // #pragma once - +#include "Ast.h" +#include "parser.h" +#include +#include namespace zfx { class IRNode { @@ -17,5 +20,48 @@ namespace zfx { }; + class Use { + + }; + + enum class Value_Type { + + }; + + class Value { + public: + Value_Type type; // + Use* use = nullptr; + + bool visited = false; + bool live = true; + + Value(Value_Type type) : type(type) {} + + + }; + + class Constant : public Value { + + }; + class Global : public Value { + + }; + + class Instruction_Binary : public Value { + + }; + + class Instruction_Unary : public Value { + + }; + + class Instruction_Branch : public Value { + + }; + + class Function_Call : Value { + + }; } diff --git a/projects/PiLeiFX/ZenoFX/Parser.h b/projects/PiLeiFX/ZenoFX/Parser.h index ee21d30660..7ba3c25661 100644 --- a/projects/PiLeiFX/ZenoFX/Parser.h +++ b/projects/PiLeiFX/ZenoFX/Parser.h @@ -1,6 +1,20 @@ // // Created by admin on 2022/5/7. // +/* + *Prog : statementList? EOF; + *statementList : statement+; + * statement : block | expressionStatement | ifStatement | forStatement + * emptyStatement | functionDecl | variableDecl + * + * ifStatement : 'if' '(' expression ') statement ('else' statement)?; + * forStatement : + * variableStatement: + * variableDecl : (Identifier| + * expression:assignment; + *Identifier : [a-zA-z][a-zA-Z0-9]*; + * IntegerLiteral : '0' | [1-9][0-9]* + * */ #pragma once #include "Lexical.h" #include "Ast.h" @@ -33,15 +47,38 @@ namespace zfx { } - std::shared_ptr parseVariable() {} + std::shared_ptr parseVariable() { + + } std::shared_ptr parseAssignment() { } - }; + std::shared_ptr parseIfStatement() { + + }; + + std::shared_ptr parseBinary() { + + } + + std::shared_ptr parseUnary() { + + } + + std::shared_ptr parseTenary() { + + } + + std::shared_ptr parseFunctionCall() { + + } + }; +/* std::vector parse(const std::string& code) { } + */ } diff --git a/projects/PiLeiFX/ZenoFX/Symbol.h b/projects/PiLeiFX/ZenoFX/Symbol.h new file mode 100644 index 0000000000..7f3232bf91 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Symbol.h @@ -0,0 +1,27 @@ +// +// Created by admin on 2022/5/11. +// +#pragma once + +#include +#include +#include +#include +#include + +namespace zfx { + + enum class SymKind{}; + class VarSymbol; + class FunctionSymbol; + class Symbol { + std::string name; + // + + }; + + class SymbolVisitor { + virtual std::any visitVarSymbol(VarSymbol& sym, std::string additional); + }; + +} diff --git a/projects/PiLeiFX/ZenoFX/parser.cpp b/projects/PiLeiFX/ZenoFX/parser.cpp index 9675851225..42bba14ccd 100644 --- a/projects/PiLeiFX/ZenoFX/parser.cpp +++ b/projects/PiLeiFX/ZenoFX/parser.cpp @@ -2,4 +2,5 @@ // Created by admin on 2022/5/7. // +#include "parser.h" diff --git a/projects/PiLeiFX/ZenoFX/x64/Func.h b/projects/PiLeiFX/ZenoFX/x64/Func.h index e40b1dd799..2996b16f7b 100644 --- a/projects/PiLeiFX/ZenoFX/x64/Func.h +++ b/projects/PiLeiFX/ZenoFX/x64/Func.h @@ -7,7 +7,7 @@ namespace zfx::x64 { struct BuiltFunc { -#define DEF_FUN1(name) static void func_##name(float *a) {} -#define DEF_FUN2(name) static void func_##name(float *a , float *b) {} + #define DEF_FUN1(name) static void func_##name(float *a) {} + #define DEF_FUN2(name) static void func_##name(float *a , float *b) {} }; } \ No newline at end of file diff --git a/projects/ZenoFX/ZFX/KillUnreachable.cpp b/projects/ZenoFX/ZFX/KillUnreachable.cpp index 2163dc2daa..f23ddd0762 100644 --- a/projects/ZenoFX/ZFX/KillUnreachable.cpp +++ b/projects/ZenoFX/ZFX/KillUnreachable.cpp @@ -32,6 +32,7 @@ struct GatherReachable : Visitor { auto stmtid = it->second; deps[stmt->id].insert(stmtid); } + for (int r: dst) { regs[r] = stmt->id; } From aff7d762d031e3a916e9da6f322b0c508d194c13 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Thu, 12 May 2022 20:48:43 +0800 Subject: [PATCH 09/14] fix immature symbol table --- projects/PiLeiFX/ZenoFX/Ast.h | 82 +++++++++++++++++++++-- projects/PiLeiFX/ZenoFX/EmitAssembler.cpp | 13 ++++ projects/PiLeiFX/ZenoFX/IR.h | 4 ++ projects/PiLeiFX/ZenoFX/Parser.h | 33 +++++++-- projects/PiLeiFX/ZenoFX/Symbol.cpp | 14 ++++ projects/PiLeiFX/ZenoFX/Symbol.h | 42 +++++++++++- projects/PiLeiFX/ZenoFX/TypeCheck.cpp | 17 +++++ projects/PiLeiFX/ZenoFX/x64/Assembler.h | 19 ++++++ 8 files changed, 209 insertions(+), 15 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/EmitAssembler.cpp create mode 100644 projects/PiLeiFX/ZenoFX/Symbol.cpp create mode 100644 projects/PiLeiFX/ZenoFX/TypeCheck.cpp diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h index fbf51b3b21..1d58bc5038 100644 --- a/projects/PiLeiFX/ZenoFX/Ast.h +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -27,6 +27,7 @@ namespace zfx { class Unary; class AstVisitor { + public: virtual ~visitor() { } @@ -47,16 +48,24 @@ namespace zfx { virtual std::any visitIfStmt(ExprIfStmt& exprIfStmt, std::string additional = ""); - virtual std::any visitForStmt() }; + class AsmVisitor { + public: + virtual ~AsmVisitor() { + + } + + virtual std::any visitAsm + + }; class AstNode { public: Position beginPos;// Position endPos; - bool isErrorNode {false}; + AstNode(const Position& beginPos, const Position& endPos) : beginPos(beginPos), endPos(endPos) {}; virtual std::any accept(AstVisitor& visitor, std::string additional = "") = 0; }; @@ -64,8 +73,8 @@ namespace zfx { public: int id; int dim = 0; - Statement(const Position& beginPos, const Position& endPos, bool isErrorNode, int id, int dim) - : AstNode(beginPos, endPos, isErrorNode),id(id), dim(dim) { + Statement(const Position& beginPos, const Position& endPos, int id, int dim) + : AstNode(beginPos, endPos),id(id), dim(dim) { } }; @@ -76,7 +85,7 @@ namespace zfx { }; class Expression : public AstNode{ - + Expression(const Position& beginPos, const Position& endPos) }; class Variable : public Expression { @@ -91,8 +100,9 @@ namespace zfx { class FunctionCall : public Expression { public: - std::string name; + std::string name;//function name std::vector> arguments; + FunctionCall() { } @@ -142,8 +152,66 @@ namespace zfx { }; - class Literal { + class IntegerLiteral : public Expression { + int32_t value; }; + + class FloatLiteral : public Expression { + + }; + + class AsmStatement { + + }; + + class AsmAssignStmt : public AsmStatement{ + + }; + + class AsmLoadConstStmt : public AsmStatement { + + }; + + class AsmTernaryOpStmt : public AsmStatement { + + }; + + class AsmBinaryOpStmt : public AsmStatement { + + }; + + class AsmUnaryOpStmt : public AsmStatement { + + }; + + class AsmFuncCallStmt : public AsmStatement { + + }; + + class AsmLocalLoadStmt : public AsmStatement { + + }; + + class AsmLocalStoreStmt : public AsmStatement { + + }; + + class AsmGlobalLoadStmt : public AsmStatement { + + }; + + class AsmGlobalStoreStmt : public AsmStatement { + + }; + + class AsmParamLoadStmt : public AsmStatement { + + }; + + class AsmIfStmt : public AsmStatement { + + }; + } diff --git a/projects/PiLeiFX/ZenoFX/EmitAssembler.cpp b/projects/PiLeiFX/ZenoFX/EmitAssembler.cpp new file mode 100644 index 0000000000..221e22a977 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/EmitAssembler.cpp @@ -0,0 +1,13 @@ +// +// Created by admin on 2022/5/12. +// + +#include + +namespace zfx { + struct EmitAssembly { + + std::stringstream oss; + + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/IR.h b/projects/PiLeiFX/ZenoFX/IR.h index 49ad6afef5..c51b29331c 100644 --- a/projects/PiLeiFX/ZenoFX/IR.h +++ b/projects/PiLeiFX/ZenoFX/IR.h @@ -64,4 +64,8 @@ namespace zfx { class Function_Call : Value { }; + + struct IRVisitor : public AstVisitor { + + }; } diff --git a/projects/PiLeiFX/ZenoFX/Parser.h b/projects/PiLeiFX/ZenoFX/Parser.h index 7ba3c25661..5c37d8b822 100644 --- a/projects/PiLeiFX/ZenoFX/Parser.h +++ b/projects/PiLeiFX/ZenoFX/Parser.h @@ -18,6 +18,7 @@ #pragma once #include "Lexical.h" #include "Ast.h" +#include "Location.h" #include #include #include @@ -34,6 +35,14 @@ namespace zfx { //begin Parser and Generate Ast std::vector Error; std::vector Warnings; + + void addError(const std::string msg, Position pos) { + + } + + void addWarnings(const std::string msg, Position pos) { + + } /* auto parse() { std::vector asts; @@ -47,6 +56,7 @@ namespace zfx { } + std::shared_ptr parseVariable() { } @@ -55,6 +65,10 @@ namespace zfx { } + std::shared_ptr parseExpression() { + + } + std::shared_ptr parseIfStatement() { }; @@ -64,7 +78,16 @@ namespace zfx { } std::shared_ptr parseUnary() { - + auto t = this->scanner.peek(); + //前缀一元表达式 + if (t.kind == TokenKind::Op) { + this->scanner.next(); + auto exp = this->parseUnary(); + return std::make_shared(); + } else { + //后缀只能是++ -- + + } } std::shared_ptr parseTenary() { @@ -72,13 +95,11 @@ namespace zfx { } std::shared_ptr parseFunctionCall() { - + + auto name = this->scanner.next().text; + return std::make_shared(); } }; -/* - std::vector parse(const std::string& code) { - } - */ } diff --git a/projects/PiLeiFX/ZenoFX/Symbol.cpp b/projects/PiLeiFX/ZenoFX/Symbol.cpp new file mode 100644 index 0000000000..58013d11bb --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Symbol.cpp @@ -0,0 +1,14 @@ +// +// Created by admin on 2022/5/12. +// + +#include "Symbol.h" +#include + +namespace zfx { + /* + std::map> built_ins { + + }; + */ +} diff --git a/projects/PiLeiFX/ZenoFX/Symbol.h b/projects/PiLeiFX/ZenoFX/Symbol.h index 7f3232bf91..493a4135f8 100644 --- a/projects/PiLeiFX/ZenoFX/Symbol.h +++ b/projects/PiLeiFX/ZenoFX/Symbol.h @@ -1,6 +1,11 @@ // // Created by admin on 2022/5/11. // +/* + * this is Symbol table + * save variable name , variable kind and some other information + * if there are other needs in the future , we will add + * */ #pragma once #include @@ -11,17 +16,50 @@ namespace zfx { - enum class SymKind{}; + enum class SymKind{Variable, Function}; class VarSymbol; class FunctionSymbol; class Symbol { + public: std::string name; - // + Symbol(const std::string& name, SymKind kind) {} + + virtual std::any accept(SymbolVisitor& visitor, std::string additional) = 0; }; class SymbolVisitor { + public: virtual std::any visitVarSymbol(VarSymbol& sym, std::string additional); + virtual std::any visitFunctionSymbol(FunctionSymbol& sym, std::string additional); + + }; + + class VarSymbol : public Symbol { + public: + VarSymbol(const std::string& name, SymKind kind) : Symbol(name, kind){ + + } + + std::any accept(SymbolVisitor& visitor, std::string additional) { + return visitor.visitVarSymbol(*this, additional); + } }; + class FunctionSymbol : public Symbol { + public: + FunctionSymbol(const std::string name, SymKind kind) : Symbol(name, kind) : Symbol(name, kind){ + + } + + std::any accept(SymbolVisitor& visitor, std::string additional) { + return visitor.visitFunctionSymbol(*this, additional); + } + + }; + class SymbolDumper : public SymbolVisitor{ + + }; + //extern std::map> built_ins; + } diff --git a/projects/PiLeiFX/ZenoFX/TypeCheck.cpp b/projects/PiLeiFX/ZenoFX/TypeCheck.cpp new file mode 100644 index 0000000000..147e3d730b --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/TypeCheck.cpp @@ -0,0 +1,17 @@ +// +// Created by admin on 2022/5/12. +// + +#include "Ast.h" + +namespace zfx { +#define ERROR_IF(x) do { \ + if (x) { \ + error("%s", #x); \ + } \ + } while(0) + + enum class TypeKind {}; + + +} diff --git a/projects/PiLeiFX/ZenoFX/x64/Assembler.h b/projects/PiLeiFX/ZenoFX/x64/Assembler.h index 46f1487bbd..31cb7af723 100644 --- a/projects/PiLeiFX/ZenoFX/x64/Assembler.h +++ b/projects/PiLeiFX/ZenoFX/x64/Assembler.h @@ -24,6 +24,9 @@ namespace zfx ::x64{ }; + enum class OprandKind { + + }; struct SimdBuilder { std::vector }; @@ -32,8 +35,24 @@ namespace zfx ::x64{ }; + class Inst_0 { + // + }; + + class Inst_1 { + + }; + + class Inst_2 { + + }; + class Oprand{ }; + + class MemAddress { + + }; } From f2c0abccaf07bb242d71456c3c3ccdfe59e05658 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Mon, 16 May 2022 21:29:30 +0800 Subject: [PATCH 10/14] want to add ctx Module --- projects/PiLeiFX/ZenoFX/Ast.h | 14 +++- projects/PiLeiFX/ZenoFX/CodeGen.h | 34 ++++++++ projects/PiLeiFX/ZenoFX/ControlCheck.cpp | 24 ++++++ projects/PiLeiFX/ZenoFX/MergeIdentical.cpp | 23 ++++++ projects/PiLeiFX/ZenoFX/Module.h | 14 ++++ projects/PiLeiFX/ZenoFX/Statement.h | 30 +++++++ projects/PiLeiFX/ZenoFX/Symbol.h | 18 +++-- projects/PiLeiFX/ZenoFX/SymbolCheck.cpp | 9 +++ projects/PiLeiFX/ZenoFX/TypeCheck.cpp | 17 ---- projects/PiLeiFX/ZenoFX/TypeFinder.h | 75 ++++++++++++++++++ projects/PiLeiFX/ZenoFX/ZFXContext.h | 11 +++ projects/PiLeiFX/ZenoFX/visitor.h | 21 +++-- projects/PiLeiFX/ZenoFX/vm.h | 91 ++++++++++++++++++++++ projects/PiLeiFX/ZenoFX/zfx.cpp | 27 ++++++- projects/ZenoFX/ZFX/IRVisitor.h | 3 +- projects/ZenoFX/ZFX/include/zfx/zfx.h | 4 +- 16 files changed, 372 insertions(+), 43 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/ControlCheck.cpp create mode 100644 projects/PiLeiFX/ZenoFX/MergeIdentical.cpp create mode 100644 projects/PiLeiFX/ZenoFX/Module.h create mode 100644 projects/PiLeiFX/ZenoFX/Statement.h create mode 100644 projects/PiLeiFX/ZenoFX/SymbolCheck.cpp delete mode 100644 projects/PiLeiFX/ZenoFX/TypeCheck.cpp create mode 100644 projects/PiLeiFX/ZenoFX/TypeFinder.h create mode 100644 projects/PiLeiFX/ZenoFX/ZFXContext.h create mode 100644 projects/PiLeiFX/ZenoFX/vm.h diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h index 1d58bc5038..401207b259 100644 --- a/projects/PiLeiFX/ZenoFX/Ast.h +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -87,10 +87,13 @@ namespace zfx { class Expression : public AstNode{ Expression(const Position& beginPos, const Position& endPos) }; - - class Variable : public Expression { +/* + * when we declare a variable , we will create a Ast Node : VariableDecl, if this Ast node has initialization + * we will create a single node represent initialization, but if only to declare we will only create one node; + * */ + class VariableDecl : public Expression { std::string name; - //if we + //std::shared_ptr<>; std::string toString() { @@ -139,7 +142,10 @@ namespace zfx { std::shared_ptr lhs; std::shared_ptr rhs; }; - +/* + * Notice assignStmt is right associative + * + * */ class AssignStmt { public: std::shared_ptr lhs; diff --git a/projects/PiLeiFX/ZenoFX/CodeGen.h b/projects/PiLeiFX/ZenoFX/CodeGen.h index e707802f4d..3f4fb3a760 100644 --- a/projects/PiLeiFX/ZenoFX/CodeGen.h +++ b/projects/PiLeiFX/ZenoFX/CodeGen.h @@ -3,4 +3,38 @@ // #pragma once +#include +#include +/* + * This class is an interface to complete a codegen in the Statement class + * */ +namespace zfx { + Value* BinaryExprAst::codegen() { + if (op == '=') { + VariableExprAst* LHSE = static_cast<> + if (!LHSE) { + std::cout << "destination of '=' must be" << std::endl; + } + Value* Val = RHS->codegen(); + if (!Val) { + return nullptr; + } + } + } + + static AllocaInst *createEntryBlock(Function* function, std::string& VarName) { + + } + + Value* NumberExprAst::codegen() { + + } + + Value* UnaryExprAst::codegen() { + Value* OperandV = Operand->codegen(); + if (!OperandV) { + return nullptr; + } + } +} diff --git a/projects/PiLeiFX/ZenoFX/ControlCheck.cpp b/projects/PiLeiFX/ZenoFX/ControlCheck.cpp new file mode 100644 index 0000000000..3fef3049bb --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/ControlCheck.cpp @@ -0,0 +1,24 @@ +// +// Created by admin on 2022/5/14. +// + +#include "Ast.h +#include +#include +/* + * 这里我们用了一个最简单的bool 栈来判断if else 结构语句是否匹配成功 + * + * */ +namespace zfx { + struct ControlCheck { + using visit_emit_types = std::tuple<>; + + void visit() { + + } + }; + + void apply_control_check() { + + } +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/MergeIdentical.cpp b/projects/PiLeiFX/ZenoFX/MergeIdentical.cpp new file mode 100644 index 0000000000..a0b9e8f45d --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/MergeIdentical.cpp @@ -0,0 +1,23 @@ +// +// Created by admin on 2022/5/14. +// +#include "Ast.h" +#include +#include + +namespace zfx { + struct MergeIdentical { + using visit_emit_types = std::tuple<>; + + void visit() { + + } + + + }; + + std::unique_ptr<> apply_merge_identical() { + MergeIdentical visitor; + + } +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Module.h b/projects/PiLeiFX/ZenoFX/Module.h new file mode 100644 index 0000000000..a9d08f11fe --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Module.h @@ -0,0 +1,14 @@ +// +// Created by admin on 2022/5/16. +// + +#pragma once + +#include +#include + +namespace zfx { + class Module { + // + }; +} diff --git a/projects/PiLeiFX/ZenoFX/Statement.h b/projects/PiLeiFX/ZenoFX/Statement.h new file mode 100644 index 0000000000..73c7792c76 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Statement.h @@ -0,0 +1,30 @@ +// +// Created by admin on 2022/5/14. +// +#pragma once +#include "Ast.h" +#include +/*Statement you can think of it as one High IR based ast, + * and for the time + * */ + +namespace zfx { + struct Statement { + int id;// + int dim;//dimensionality; + explicit Statement(int id , int dim) : id(id), dim(dim) { + + } + + std::string print() { + + } + + + }; + + + +} + + diff --git a/projects/PiLeiFX/ZenoFX/Symbol.h b/projects/PiLeiFX/ZenoFX/Symbol.h index 493a4135f8..3707ec6765 100644 --- a/projects/PiLeiFX/ZenoFX/Symbol.h +++ b/projects/PiLeiFX/ZenoFX/Symbol.h @@ -3,7 +3,7 @@ // /* * this is Symbol table - * save variable name , variable kind and some other information + * save variable name , variable kind and variable dimensional * if there are other needs in the future , we will add * */ #pragma once @@ -17,13 +17,21 @@ namespace zfx { enum class SymKind{Variable, Function}; + enum class SymDim{ + zero = 0, + OneDimensional = 1, + TwoDimensional = 2, + ThreeDimensional = 3, + FourDimensional = 4 + }; class VarSymbol; class FunctionSymbol; class Symbol { public: std::string name; - - Symbol(const std::string& name, SymKind kind) {} + SymKind kind; + SymDim dim; + Symbol(const std::string& name, SymKind kind, SymDim dim) {} virtual std::any accept(SymbolVisitor& visitor, std::string additional) = 0; }; @@ -37,7 +45,7 @@ namespace zfx { class VarSymbol : public Symbol { public: - VarSymbol(const std::string& name, SymKind kind) : Symbol(name, kind){ + VarSymbol(const std::string& name, SymKind kind, SymDim dim) : Symbol(name, kind, dim){ } @@ -48,7 +56,7 @@ namespace zfx { class FunctionSymbol : public Symbol { public: - FunctionSymbol(const std::string name, SymKind kind) : Symbol(name, kind) : Symbol(name, kind){ + FunctionSymbol(const std::string name, SymKind kind, SymDim dim) : Symbol(name, kind, kind) { } diff --git a/projects/PiLeiFX/ZenoFX/SymbolCheck.cpp b/projects/PiLeiFX/ZenoFX/SymbolCheck.cpp new file mode 100644 index 0000000000..9286ef41c1 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/SymbolCheck.cpp @@ -0,0 +1,9 @@ +// +// Created by admin on 2022/5/14. +// + +#include "Ast.h" +#include " +namespace zfx { + +} diff --git a/projects/PiLeiFX/ZenoFX/TypeCheck.cpp b/projects/PiLeiFX/ZenoFX/TypeCheck.cpp deleted file mode 100644 index 147e3d730b..0000000000 --- a/projects/PiLeiFX/ZenoFX/TypeCheck.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// -// Created by admin on 2022/5/12. -// - -#include "Ast.h" - -namespace zfx { -#define ERROR_IF(x) do { \ - if (x) { \ - error("%s", #x); \ - } \ - } while(0) - - enum class TypeKind {}; - - -} diff --git a/projects/PiLeiFX/ZenoFX/TypeFinder.h b/projects/PiLeiFX/ZenoFX/TypeFinder.h new file mode 100644 index 0000000000..62721d1605 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/TypeFinder.h @@ -0,0 +1,75 @@ +// +// Created by admin on 2022/5/14. +// +#pragma once + +#include "Ast.h" +#include "Module.h" +#include +#include +/*TypeFinder - Walk various Statements, identifying various information including + * + * */ + +namespace zfx { +#define ERROR_IF(x) do { \ + if (x) { \ + error("%s", #x); \ + } \ + } while(0) + + struct TypeFinder { + using visit_emit_type = std::tuple<>; + + std::unordered_map VisitStatement ; + /* + * This is helper map to avoid repeated visit statement + * */ + void visit(Statement* stmt) { + if (VisitStatement.find(stmt) != VisitStatement.end()) { + std::cout << "this is " << std::endl; + } + VisitStatement.insert(stmt, true); + } + + // + void visit(ParamSymbolStmt* stmt) { + stmt->dim = stmt->;//设置维数 + visit((Statement*)stmt); + } + + void visit(SymbolStmt* stmt) { + stmt->dim = stmt->; + visit((Statement*)stmt); + } + + void visit(LiteralStmt* stmt) { + stmt->dim = 1; + visit((Statement*)stmt); + } + + void visit(FunctionCallStmt* stmt) { + stmt->dim = 0;//初始化维度为0 + auto const& name = stmt->name; + + //如果是vec那么就是 + if (name.substr(0, 3) == "vec" && name.size() == 4 && isdigit(name[3])) { + int dim = name[3] - '0';//字符串转换为数字 + stmt->dim = dim; + } else { + if () + //开始判断参数 + + } + + + } + + }; + + void apply_type_check(Module* m) { + TypeFinder visitor; + visitor.apply(m); + } +} + diff --git a/projects/PiLeiFX/ZenoFX/ZFXContext.h b/projects/PiLeiFX/ZenoFX/ZFXContext.h new file mode 100644 index 0000000000..0236b147a3 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/ZFXContext.h @@ -0,0 +1,11 @@ +// +// Created by admin on 2022/5/16. +// + +#pragma once + +#include + +namespace zfx { + +} diff --git a/projects/PiLeiFX/ZenoFX/visitor.h b/projects/PiLeiFX/ZenoFX/visitor.h index d92fb0c472..f4017a83e8 100644 --- a/projects/PiLeiFX/ZenoFX/visitor.h +++ b/projects/PiLeiFX/ZenoFX/visitor.h @@ -4,19 +4,16 @@ #pragma once -#include "Ast.h" +#include "Module.h" #include #include -/* -namespace zfx { - class AstVisitor { - - virtual std::any visit() { - } - virtual ~AstVisitor() { +namespace zfx { + void apply_check(Module* m); + /* + * + * + * */ - } - }; -} -*/ \ No newline at end of file + std::string apply_emit_assembly(Module* m); +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/vm.h b/projects/PiLeiFX/ZenoFX/vm.h new file mode 100644 index 0000000000..b6abaac99c --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/vm.h @@ -0,0 +1,91 @@ +// +// Created by admin on 2022/5/13. +// + +#pragma once +//this is a trial stack machine base Ast tree +#include "Lexical.h +#include "Ast.h" +#include "Symbol.h" +#include +#include +namespace zfx { + enum class OpCode { + // + }; + + class BCModule { + //This is a bitcode module + std::shared_ptr _main;// + + std::vector consts;//常量值 + + BCModule() { + //construct function 将内置函数加入到常量池 + } + }; + + classBCModuleDumper { + + }; + + class StackFrame { + + }; + class BCGenerator : public AstVisitor { + public: + std::shared_ptr m;//编译后生成的模型 + std::shared_ptr functionSym; + + BCGenerator() { + this->m = std::make_shared(); + } + + std::any visitVariable(Variable& variable, std::string additional) override { + + } + + std::any visitFunctionCall(FunctionCall& functionCall, std::string additional) override { + + } + + std::any visitBinary(Binary& binary, std::strig additional) { + + } + + std::any visitUnary(Unary& unary, std::string additional) override { + + } + + std::any visitTenary(Tenary& tenary, std::string additional) override { + + } + + std::any visitAssign(AssignStmt& assign, std::string additional) override { + + } + + std::any visitLiteral(Literal& literal, std::string additional) override { + + } + + std::any visitIfStmt(ExprIfStmt& exprIfStmt, std::string additional) override { + + } + + }; + + class VM { + public: + //this + + int32_t execute(const BCModule& bcModule) { + //找到入口函数 + std::shared_ptr functionSym; + if (bcModule._main == nullptr) { + std::cout << "Can not find main function" << std::endl; + return -1; + } + } + }; +} diff --git a/projects/PiLeiFX/ZenoFX/zfx.cpp b/projects/PiLeiFX/ZenoFX/zfx.cpp index 85c0272595..3e60db0608 100644 --- a/projects/PiLeiFX/ZenoFX/zfx.cpp +++ b/projects/PiLeiFX/ZenoFX/zfx.cpp @@ -11,9 +11,34 @@ std::tuple<> compile_to_assembly (const std::string& code, const Options& option std::cout << "start zfx" << std::endl; std::cout << code << std::endl; #endif +#ifdef ZFX_PRINT_IR + std::cout << "scanner Token" << std::endl; + //here may output print tokens; +#endif #ifdef ZFX_PRINT_IR std::cout << "Parse Ast" << std::endl; - auto asts = parse(code); + + //here i want to print ast tree; +#endif +#ifdef ZFX_PRINT_IR + std::cout << "Transform IR" << std::endl; + //output IR; +#endif +//There is a problem + //begin Semantic Analysis +#ifdef ZFX_PRINT_IR + std::cout << "Controlcheck" << std::endl; +#endif +#ifdef ZFX_PRINT_IR + std::cout << "SymbolCheck" << std::endl; +#endif +#ifdef ZFX_PRINT_IR + std::cout << "TypeCheck" << std::endl; +#endif + +#ifdef ZFX_PRINT_IR + std::cout << "Assemble" << std::endl; +#endif } diff --git a/projects/ZenoFX/ZFX/IRVisitor.h b/projects/ZenoFX/ZFX/IRVisitor.h index faebf0b565..3010ed7ecd 100644 --- a/projects/ZenoFX/ZFX/IRVisitor.h +++ b/projects/ZenoFX/ZFX/IRVisitor.h @@ -16,7 +16,8 @@ struct IRVisitor { }; template -struct Visitor : IRVisitor { +struct + Visitor : IRVisitor { using IRVisitor::apply; virtual void apply(Statement *stmt) override { diff --git a/projects/ZenoFX/ZFX/include/zfx/zfx.h b/projects/ZenoFX/ZFX/include/zfx/zfx.h index 483231a9d3..98bca24727 100644 --- a/projects/ZenoFX/ZFX/include/zfx/zfx.h +++ b/projects/ZenoFX/ZFX/include/zfx/zfx.h @@ -9,19 +9,17 @@ #include namespace zfx { -//增加一个编译器选项,主要是一些默认的值 struct Options { bool const_parametrize = true; bool global_localize = true; bool demote_math_funcs = true; - bool save_math_registers = true; + bool save_math_registwmers = true; int arch_maxregs = 16; bool detect_new_symbols = false; bool reassign_parameters = true; bool reassign_channels = true; - //开启编译优化选项 bool merge_identical = true; bool kill_unreachable = true; bool constant_fold = true; From 844d323d92b815046b7969da1a485e228e0a2739 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Thu, 26 May 2022 21:16:21 +0800 Subject: [PATCH 11/14] next needs to be IRBuilder and some basic optimization --- projects/PiLeiFX/ZenoFX/Builder_CFG.cpp | 10 +++ projects/PiLeiFX/ZenoFX/CFG.cpp | 9 +++ projects/PiLeiFX/ZenoFX/CFG.h | 86 ++++++++++++++++++++++++ projects/PiLeiFX/ZenoFX/IR.h | 66 ------------------ projects/PiLeiFX/ZenoFX/Lexical.h | 81 +++++++++++++++++++++- projects/PiLeiFX/ZenoFX/LowerAST.h | 19 ++++++ projects/PiLeiFX/ZenoFX/Module.h | 13 ++-- projects/PiLeiFX/ZenoFX/Parser.h | 45 +++++-------- projects/PiLeiFX/ZenoFX/Pass.h | 24 +++++++ projects/PiLeiFX/ZenoFX/Statement.h | 49 +++++++++++++- projects/PiLeiFX/ZenoFX/Stmt.h | 34 ++++++++++ projects/PiLeiFX/ZenoFX/Symbol.h | 1 + projects/PiLeiFX/ZenoFX/Type.h | 13 ++++ projects/PiLeiFX/ZenoFX/User.h | 15 +++++ projects/PiLeiFX/ZenoFX/Value.h | 42 ++++++++++++ projects/PiLeiFX/ZenoFX/general_struct.h | 20 ++++++ projects/PiLeiFX/ZenoFX/jit_module.h | 11 +++ projects/PiLeiFX/ZenoFX/visitor.h | 16 ++++- 18 files changed, 448 insertions(+), 106 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/Builder_CFG.cpp create mode 100644 projects/PiLeiFX/ZenoFX/CFG.cpp create mode 100644 projects/PiLeiFX/ZenoFX/CFG.h create mode 100644 projects/PiLeiFX/ZenoFX/LowerAST.h create mode 100644 projects/PiLeiFX/ZenoFX/Pass.h create mode 100644 projects/PiLeiFX/ZenoFX/Stmt.h create mode 100644 projects/PiLeiFX/ZenoFX/Type.h create mode 100644 projects/PiLeiFX/ZenoFX/User.h create mode 100644 projects/PiLeiFX/ZenoFX/Value.h create mode 100644 projects/PiLeiFX/ZenoFX/general_struct.h create mode 100644 projects/PiLeiFX/ZenoFX/jit_module.h diff --git a/projects/PiLeiFX/ZenoFX/Builder_CFG.cpp b/projects/PiLeiFX/ZenoFX/Builder_CFG.cpp new file mode 100644 index 0000000000..44a16d2c24 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Builder_CFG.cpp @@ -0,0 +1,10 @@ +// +// Created by admin on 2022/5/26. +// + +namespace zfx { + + class CFGBuilder : public IRVisitor { + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/CFG.cpp b/projects/PiLeiFX/ZenoFX/CFG.cpp new file mode 100644 index 0000000000..2b723c8548 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/CFG.cpp @@ -0,0 +1,9 @@ +// +// Created by admin on 2022/5/26. +// +#include "CFG.h" + + +namespace zfx { + +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/CFG.h b/projects/PiLeiFX/ZenoFX/CFG.h new file mode 100644 index 0000000000..55b2ea5651 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/CFG.h @@ -0,0 +1,86 @@ +// +// Created by admin on 2022/5/26. +// +#pragma once + +#include +#include +#include + +namespace zfx { + class CFGNode { + private:: + std::unordered_set parent_blocks; + public: + Block *block; + int begin_location, end_location; + + CFGNode *prev_in_the_same_block; + CFGNode *next_in_the_same_block; + + // + std::unordered_set live_gen, live_kill, live_in, live_out; + std::unordered_set reach_gen, reach_kill, reach_in, reach_out; + std::vector prev, next; + CFGNode(); + + CFGNode(Block *block, int begin_location, + int end_location, CFGNode *prev_node_in_same_block); + static void add_edge(CFGNode *from, CFGNode *to); + + bool empty() const; + + std::size_t size(); + + void erase(int location); + + void insert(std::unique_ptr &&stmt, int location); + + void replace_with(int location, std::unique_ptr &&newStmt, + bool replace_usage = true) const; + + static bool contain_variable(const std::unordered_set &stmt_set, + Stmt *var); + + static bool may_contain_variable(const std::unordered_set &stmt, + Stmt *var); + + bool reach_kill_variable(Stmt *var) const; + + Stmt *get_store_forwarding_data(Stmt *stmt, int position) const; + + //Analyses and optimization inside CFGNode + + void reaching_definition_analysis(bool after_lower_access); + + void live_variable_anaylysis(); + }; + + + class ControlFlowGraph { + private: + void erase(int node_id); + public: + struct LiveVarAnalysisConfig { + + }; + + std::vector> nodes; + const int state_node = 0; + int final_node{0}; + + template + CFGNode *push_back(Args...args) { + nodes.template emplace_back(std::make_unique(std::forward(args)...)); + return nodes.back().get(); + } + + void reaching_definition_analysis(bool after_lower_access); + + void live_variable_analysis() { + + } + + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/IR.h b/projects/PiLeiFX/ZenoFX/IR.h index c51b29331c..bfdf0b94fd 100644 --- a/projects/PiLeiFX/ZenoFX/IR.h +++ b/projects/PiLeiFX/ZenoFX/IR.h @@ -1,71 +1,5 @@ // // Created by admin on 2022/5/7. // - #pragma once -#include "Ast.h" -#include "parser.h" -#include -#include -namespace zfx { - class IRNode { - - }; - - class BinaryNode { - - }; - - class UnaryNode { - - }; - - class Use { - - }; - - enum class Value_Type { - - }; - - class Value { - public: - Value_Type type; // - Use* use = nullptr; - - bool visited = false; - bool live = true; - - Value(Value_Type type) : type(type) {} - - - }; - - class Constant : public Value { - - }; - - class Global : public Value { - - }; - - class Instruction_Binary : public Value { - - }; - - class Instruction_Unary : public Value { - - }; - - class Instruction_Branch : public Value { - - }; - - class Function_Call : Value { - - }; - - struct IRVisitor : public AstVisitor { - }; -} diff --git a/projects/PiLeiFX/ZenoFX/Lexical.h b/projects/PiLeiFX/ZenoFX/Lexical.h index a2e8270933..70a90c4877 100644 --- a/projects/PiLeiFX/ZenoFX/Lexical.h +++ b/projects/PiLeiFX/ZenoFX/Lexical.h @@ -117,7 +117,7 @@ namespace zfx { } }; - +/* class Scanner { private: std::vector tokens; @@ -205,12 +205,32 @@ namespace zfx { return (ch == ' ' || ch == '\n' || ch == '\t'); } - bool isDigit(char ch) { + inline bool isDigit(char ch) { return (ch >= '0' && ch <= '9'); } - bool isLetter() {} + inline bool is_symbolic_atom(char ch) { + if () return false; + if (isspace(ch) ||) { + return true; + } + return false; + } + + inline int swizzle_from_char(char ch) { + if ('x' <= c && c <= 'z') { + return c - 'z'; + } else if (c == 'w') { + return 3; + } else if('0' <= c && c <= '9') { + return c - '0'; + } else if () { + + } else { + return -1; + } + } Token parseIdentifer() { Token token; if (this->KeywordMap.find(token.text)) { @@ -219,6 +239,61 @@ namespace zfx { return token; } }; +*/ + + class Scanner { + private: + std::list tokens; + std::string data; + Position lastPos{0, 0, 0, 0}; + public: + Scanner(const std::string &data) : data(data) { + } + Token next() { + if (this->tokens.empty()) { + auto t = this->getAToken(); + //set pos + this->lastPos = t.pos; + return t; + } else { + auto t = this->tokens.front(); + this->lastPos = t.pos; + this->tokens.pop_front(); + return t; + } + } + + Token peek() { + if (this->tokens.empty()) { + auto t = this->getAToken(); + this->tokens->push_back(t); + return t; + } else { + auto t = this->tokens.front(); + return t; + } + } + + Token peek2() { + while (this->tokens.size() < 2) { + auto t = this->getAToken(); + this->tokens.push_back(t); + } + + if (this->tokens.size() < 2) { + return Token{};//EofToken + } + + auto it = this->tokens.begin(); + std::advance(it, 1); + return *it1; + } + + private: + Token getAToken() { + + } + }; } diff --git a/projects/PiLeiFX/ZenoFX/LowerAST.h b/projects/PiLeiFX/ZenoFX/LowerAST.h new file mode 100644 index 0000000000..7332e817e8 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/LowerAST.h @@ -0,0 +1,19 @@ + +// +// Created by admin on 2022/5/18. +// +#pragma once +#include "Ast.h" +#include "Stmt.h" +#include "IR.h" +#include +#include +/* + * convent ast to statement + * */ +namespace zfx { + std::tuple<> lower_ast(std::vector asts, + std::map const& symdims, + std::map const& pardims); +} + diff --git a/projects/PiLeiFX/ZenoFX/Module.h b/projects/PiLeiFX/ZenoFX/Module.h index a9d08f11fe..67b1529e5e 100644 --- a/projects/PiLeiFX/ZenoFX/Module.h +++ b/projects/PiLeiFX/ZenoFX/Module.h @@ -1,14 +1,15 @@ // -// Created by admin on 2022/5/16. +// Created by admin on 2022/5/19. // #pragma once -#include -#include - namespace zfx { class Module { - // + public: + std::string name; + explicit Module(const std::string name) : name(name) { + + } }; -} +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Parser.h b/projects/PiLeiFX/ZenoFX/Parser.h index 5c37d8b822..e42ed5ec84 100644 --- a/projects/PiLeiFX/ZenoFX/Parser.h +++ b/projects/PiLeiFX/ZenoFX/Parser.h @@ -43,63 +43,52 @@ namespace zfx { void addWarnings(const std::string msg, Position pos) { } -/* - auto parse() { - std::vector asts; - return asts; - } + AstNode parse_atom() { -*/ + } - std::vector> parseStatement() { + AstNode parse_operator() { } + AstNode parse_compound() { + + } - std::shared_ptr parseVariable() { + AstNode parse_factor() { } - std::shared_ptr parseAssignment() { + AstNode parse_term() { } - std::shared_ptr parseExpression() { + AstNode parse_side() { } - std::shared_ptr parseIfStatement() { + AstNode parse_cond() { - }; + } - std::shared_ptr parseBinary() { + AstNode parse_andexpr() { } - std::shared_ptr parseUnary() { - auto t = this->scanner.peek(); - //前缀一元表达式 - if (t.kind == TokenKind::Op) { - this->scanner.next(); - auto exp = this->parseUnary(); - return std::make_shared(); - } else { - //后缀只能是++ -- - - } + AstNode parse_orexpr() { + } - std::shared_ptr parseTenary() { + AstNode parse_expr() { } - std::shared_ptr parseFunctionCall() { + AstNode parse_stmt() { - auto name = this->scanner.next().text; - return std::make_shared(); } }; + } diff --git a/projects/PiLeiFX/ZenoFX/Pass.h b/projects/PiLeiFX/ZenoFX/Pass.h new file mode 100644 index 0000000000..2b3970a4f3 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Pass.h @@ -0,0 +1,24 @@ +// +// Created by admin on 2022/5/25. +// + +#pragma once + +namespace zfx { + class Pass { + public: + Pass() = default; + virtual ~Pass() = default; + virtual void run() = 0; + }; + + class BasicBlockPass : public Pass{ + public: + void run() override; + }; + + class PassManger { + public: + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/Statement.h b/projects/PiLeiFX/ZenoFX/Statement.h index 73c7792c76..30810fcfa7 100644 --- a/projects/PiLeiFX/ZenoFX/Statement.h +++ b/projects/PiLeiFX/ZenoFX/Statement.h @@ -4,27 +4,74 @@ #pragma once #include "Ast.h" #include +#include /*Statement you can think of it as one High IR based ast, * and for the time * */ namespace zfx { + + strcut Statement; + using StmtFields = std::vector>; + using RegFields = std::vector; + struct Statement { int id;// - int dim;//dimensionality; + int dim = 0;//dimensionality; explicit Statement(int id , int dim) : id(id), dim(dim) { } std::string print() { + return ; + } + + virtual std::string to_string() = 0; + + virtual std::unique_ptr clone(int newid) const = 0; + virtual StmtFields fields() = 0; + + virtual ~Statement() = default; + + virtual std::string serialize_identity() const { + return to_string(); + } + + virtual bool is_control_stmt() const { + return false; } }; + template + struct Stmt : Statement { + using Statement::Statement; + virtual std::unique_ptr clone(int newid) override { + auto ret = std::make_unique(static_cast*this); + ret->id = newid; + return ret; + } + }; + + template + struct AsmStmt: Stmt { + using Stmt::Stmt; + + virtual StmtFields fields() override { + } + }; + + struct EmptyStmt : Stmt { + explicit EmptyStmt(int id_) : Stmt(id_) {} + + virtual StmtFields fields() override { + return {}; + } + }; } diff --git a/projects/PiLeiFX/ZenoFX/Stmt.h b/projects/PiLeiFX/ZenoFX/Stmt.h new file mode 100644 index 0000000000..2721859be4 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Stmt.h @@ -0,0 +1,34 @@ +// +// Created by admin on 2022/5/17. +// + +#include "Statement.h" + +namespace zfx { + struct UnaryOpStmt : Stmt { + std::string op; + Statement *src; + UnaryOpStmt(int id, const std::string op, Statement* src) : Stmt(id), op(op), src(src) { + + } + + virtual StmtFields fields() override { + return { + src, + };// + } + + }; + + struct VectorSwizzleStmt : Stmt { +//转换坐标 + + }; + + struct VectorComposeStmt : Stmt { + int dimension; + std::vector args; + + VectorComposeStmt(int id, int dimension, std::vector*) + }; +} diff --git a/projects/PiLeiFX/ZenoFX/Symbol.h b/projects/PiLeiFX/ZenoFX/Symbol.h index 3707ec6765..49902f1e5b 100644 --- a/projects/PiLeiFX/ZenoFX/Symbol.h +++ b/projects/PiLeiFX/ZenoFX/Symbol.h @@ -1,3 +1,4 @@ + // // Created by admin on 2022/5/11. // diff --git a/projects/PiLeiFX/ZenoFX/Type.h b/projects/PiLeiFX/ZenoFX/Type.h new file mode 100644 index 0000000000..dbe7939945 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Type.h @@ -0,0 +1,13 @@ +// +// Created by admin on 2022/5/19. +// +#pragma once + +#include +#include + +namespace zfx { + class Type { + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/User.h b/projects/PiLeiFX/ZenoFX/User.h new file mode 100644 index 0000000000..dc8eae2703 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/User.h @@ -0,0 +1,15 @@ +// +// Created by admin on 2022/5/19. +// + +#pragma once + +#include "Value.h" +#include + +namespace zfx { + class User : public Value { + public: + User(Type *ty, const std::) + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Value.h b/projects/PiLeiFX/ZenoFX/Value.h new file mode 100644 index 0000000000..bf1db21129 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Value.h @@ -0,0 +1,42 @@ +// +// Created by admin on 2022/5/19. +// + +#pragma once + +#include "Type.h" +#include +#include + +namespace zfx { + + class Use { + + }; + class Value { + public: + + explicit Value(Type *type, const std::string& name = ""); + + ~Value() = default; + + std::list &get_use_list() {return use_list;} + + void add_use(Value *value); + + bool set_name(std::string name) { + + } + + std::string get_name() const; + + void remove_use(Value *val); + + virtual std::string print() = 0; + private: + std::string name; + Type *type; + std::list use_list; + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/general_struct.h b/projects/PiLeiFX/ZenoFX/general_struct.h new file mode 100644 index 0000000000..825741582b --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/general_struct.h @@ -0,0 +1,20 @@ +// +// Created by admin on 2022/5/19. +// +#pragma once + +#include +#include +#include + +namespace zfx { + class Module { + public: + std::string name; + //module name actually file name + Module(std::string name) : name(name) {} + Module() : name("Module") {} + virtual ~Module() {} + virtual void EmitCode() = 0; + }; +} diff --git a/projects/PiLeiFX/ZenoFX/jit_module.h b/projects/PiLeiFX/ZenoFX/jit_module.h new file mode 100644 index 0000000000..4c914f5c0c --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/jit_module.h @@ -0,0 +1,11 @@ +// +// Created by admin on 2022/5/25. +// +#pragma once + +namespace zfx { + class JITModule { + public: + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/visitor.h b/projects/PiLeiFX/ZenoFX/visitor.h index f4017a83e8..5a2e02421d 100644 --- a/projects/PiLeiFX/ZenoFX/visitor.h +++ b/projects/PiLeiFX/ZenoFX/visitor.h @@ -7,9 +7,21 @@ #include "Module.h" #include #include - +/* + * This is a semantic analysis module + * */ namespace zfx { - void apply_check(Module* m); + void apply_control_check(Module *m); + void apply_symbol_check(Module *m); + void apply_type_check(Module *m); + std::map apply_detect_new_symbols(Module *m, + std::mapconst &temps, + std::vector& symbols>); + std::unique_ptr apply_expand_function(Module *m); + std::unique_ptr apply_lower_math(Module *m); + std::unique_ptr apply_demote_math_funcs(Module *m); + + /* * * From febc3d0c6741d4b74544b21c90b4aebd8eb8f7f7 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Tue, 31 May 2022 20:28:45 +0800 Subject: [PATCH 12/14] change code structure --- projects/PiLeiFX/ZenoFX/Ast.h | 68 ++- projects/PiLeiFX/ZenoFX/CFG.h | 86 ---- projects/PiLeiFX/ZenoFX/Error.h | 32 ++ projects/PiLeiFX/ZenoFX/IR/BasicBlock.h | 36 ++ projects/PiLeiFX/ZenoFX/{ => IR}/IR.h | 0 projects/PiLeiFX/ZenoFX/IR/Module.h | 20 + projects/PiLeiFX/ZenoFX/IR/Type.h | 81 ++++ projects/PiLeiFX/ZenoFX/{ => IR}/TypeFinder.h | 2 +- projects/PiLeiFX/ZenoFX/{ => IR}/User.h | 0 projects/PiLeiFX/ZenoFX/{ => IR}/Value.h | 10 +- projects/PiLeiFX/ZenoFX/Lexical.h | 401 ++++++++---------- projects/PiLeiFX/ZenoFX/Location.h | 94 ---- projects/PiLeiFX/ZenoFX/LowerAST.h | 4 +- projects/PiLeiFX/ZenoFX/Module.h | 15 - projects/PiLeiFX/ZenoFX/Pass.h | 31 +- .../PiLeiFX/ZenoFX/{ => Pass}/Builder_CFG.cpp | 0 projects/PiLeiFX/ZenoFX/{ => Pass}/CFG.cpp | 1 - projects/PiLeiFX/ZenoFX/Pass/CFG.h | 57 +++ projects/PiLeiFX/ZenoFX/Pass/active_var.h | 23 + projects/PiLeiFX/ZenoFX/Type.h | 13 - projects/PiLeiFX/ZenoFX/visitor.h | 4 +- projects/PiLeiFX/ZenoFX/zfx.cpp | 5 +- 22 files changed, 522 insertions(+), 461 deletions(-) delete mode 100644 projects/PiLeiFX/ZenoFX/CFG.h create mode 100644 projects/PiLeiFX/ZenoFX/Error.h create mode 100644 projects/PiLeiFX/ZenoFX/IR/BasicBlock.h rename projects/PiLeiFX/ZenoFX/{ => IR}/IR.h (100%) create mode 100644 projects/PiLeiFX/ZenoFX/IR/Module.h create mode 100644 projects/PiLeiFX/ZenoFX/IR/Type.h rename projects/PiLeiFX/ZenoFX/{ => IR}/TypeFinder.h (98%) rename projects/PiLeiFX/ZenoFX/{ => IR}/User.h (100%) rename projects/PiLeiFX/ZenoFX/{ => IR}/Value.h (73%) delete mode 100644 projects/PiLeiFX/ZenoFX/Location.h delete mode 100644 projects/PiLeiFX/ZenoFX/Module.h rename projects/PiLeiFX/ZenoFX/{ => Pass}/Builder_CFG.cpp (100%) rename projects/PiLeiFX/ZenoFX/{ => Pass}/CFG.cpp (98%) create mode 100644 projects/PiLeiFX/ZenoFX/Pass/CFG.h create mode 100644 projects/PiLeiFX/ZenoFX/Pass/active_var.h delete mode 100644 projects/PiLeiFX/ZenoFX/Type.h diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h index 401207b259..ad492e8155 100644 --- a/projects/PiLeiFX/ZenoFX/Ast.h +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -13,7 +13,8 @@ #include #include namespace zfx { - enum class Ast_Node_Type { +/* +enum class Ast_Node_Type { Ast_Type_Statement, Ast_Type_Declaration, Ast_Type_Identifier, @@ -21,10 +22,22 @@ namespace zfx { Ast_Type_Binary_Op, Ast_Type_Unary_Op }; - class AstNode; - class Variable; - class Binary; - class Unary; + */ +/* + class UnaryOpStmt; + class BinaryOpStmt; + class TernaryOpStmt; + class FunctionCallStmt; + class AssignStmt; + class SymbolStmt; + class SymbolStmt; + class ParamSymbolStmt; + class TempSymbolStmt; + class LiterialStnt; + class FrontedIfStmt; + class FrontedElseStmt; + class FrontedElseIfStmt; + class FrontedEndIfStmt; class AstVisitor { public: @@ -79,6 +92,9 @@ namespace zfx { } }; + class UnaryOpStmt : public Statement { + + }; class Ast_Identifier { @@ -87,10 +103,12 @@ namespace zfx { class Expression : public AstNode{ Expression(const Position& beginPos, const Position& endPos) }; + */ /* * when we declare a variable , we will create a Ast Node : VariableDecl, if this Ast node has initialization * we will create a single node represent initialization, but if only to declare we will only create one node; * */ +/* class VariableDecl : public Expression { std::string name; @@ -142,10 +160,12 @@ namespace zfx { std::shared_ptr lhs; std::shared_ptr rhs; }; + */ /* * Notice assignStmt is right associative * * */ +/* class AssignStmt { public: std::shared_ptr lhs; @@ -217,7 +237,45 @@ namespace zfx { class AsmIfStmt : public AsmStatement { + }; + */ +/* + class Ast { + public: + using Iter = std::vector ::iterator + Ast() { + + } + private: + Token token; + }; + + inline std::unique_ptr make_ast() { + + } + */ + + class AstVisitor { + + }; + + class AstNode { + public: + Position beginPos; + Position endPos; + + virtual std::any accept(AstNode &visitor, std::string additional = "") = 0; + + }; +//语句 + class Statement: AstNode { + + }; + //声明,子类为$ @ + class Decl { + }; + class } diff --git a/projects/PiLeiFX/ZenoFX/CFG.h b/projects/PiLeiFX/ZenoFX/CFG.h deleted file mode 100644 index 55b2ea5651..0000000000 --- a/projects/PiLeiFX/ZenoFX/CFG.h +++ /dev/null @@ -1,86 +0,0 @@ -// -// Created by admin on 2022/5/26. -// -#pragma once - -#include -#include -#include - -namespace zfx { - class CFGNode { - private:: - std::unordered_set parent_blocks; - public: - Block *block; - int begin_location, end_location; - - CFGNode *prev_in_the_same_block; - CFGNode *next_in_the_same_block; - - // - std::unordered_set live_gen, live_kill, live_in, live_out; - std::unordered_set reach_gen, reach_kill, reach_in, reach_out; - std::vector prev, next; - CFGNode(); - - CFGNode(Block *block, int begin_location, - int end_location, CFGNode *prev_node_in_same_block); - static void add_edge(CFGNode *from, CFGNode *to); - - bool empty() const; - - std::size_t size(); - - void erase(int location); - - void insert(std::unique_ptr &&stmt, int location); - - void replace_with(int location, std::unique_ptr &&newStmt, - bool replace_usage = true) const; - - static bool contain_variable(const std::unordered_set &stmt_set, - Stmt *var); - - static bool may_contain_variable(const std::unordered_set &stmt, - Stmt *var); - - bool reach_kill_variable(Stmt *var) const; - - Stmt *get_store_forwarding_data(Stmt *stmt, int position) const; - - //Analyses and optimization inside CFGNode - - void reaching_definition_analysis(bool after_lower_access); - - void live_variable_anaylysis(); - }; - - - class ControlFlowGraph { - private: - void erase(int node_id); - public: - struct LiveVarAnalysisConfig { - - }; - - std::vector> nodes; - const int state_node = 0; - int final_node{0}; - - template - CFGNode *push_back(Args...args) { - nodes.template emplace_back(std::make_unique(std::forward(args)...)); - return nodes.back().get(); - } - - void reaching_definition_analysis(bool after_lower_access); - - void live_variable_analysis() { - - } - - - }; -} diff --git a/projects/PiLeiFX/ZenoFX/Error.h b/projects/PiLeiFX/ZenoFX/Error.h new file mode 100644 index 0000000000..7257c115c8 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Error.h @@ -0,0 +1,32 @@ +// +// Created by admin on 2022/5/30. +// +#pragma once + +#include +namespace zfx { + struct Position{ + uint32_t begin; + uint32_t end; + uint32_t line; + uint32_t col; + + Position(){} + Position(uint32_t begin, uint32_t end, uint32_t line, uint32_t col) : begin(begin), + end(end), line(line), col(col) { + + } + + Position(const Position &rhs) { + this->begin = rhs.begin; + this->end = rhs.end; + this->line = rhs.line; + this->col = rhs.col; + } + + std::string ToString() { + return "ln" + std::to_string(this->line) + ", col" + std::to_string(this->col) + + ", Pos:" + std::to_string(this->pos); + } + }; +} diff --git a/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h b/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h new file mode 100644 index 0000000000..94c7ec7c65 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h @@ -0,0 +1,36 @@ +// +// Created by admin on 2022/5/27. +// + +#include "Module.h" +#include +#include +#include +#include + + +namespace zfx { + class BasicBlock { + public: + static BasicBlock *create(Module *m, const std::string& name) { + + return new BasicBlock(m, name); + } + + Module* getModule() ; + + virtual std::string print() override; + + //api for cfg + std::list &get_pre_basic_blocks() { + + } + + std::list + private: + explicit BasicBlock(Module *m, const std::string &name); + std::list pre_bbs; + std::list succ_bbs; + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/IR.h b/projects/PiLeiFX/ZenoFX/IR/IR.h similarity index 100% rename from projects/PiLeiFX/ZenoFX/IR.h rename to projects/PiLeiFX/ZenoFX/IR/IR.h diff --git a/projects/PiLeiFX/ZenoFX/IR/Module.h b/projects/PiLeiFX/ZenoFX/IR/Module.h new file mode 100644 index 0000000000..7aa4290e63 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/Module.h @@ -0,0 +1,20 @@ +// +// Created by admin on 2022/5/19. +// + +#pragma once + +namespace zfx { + class Module { + public: + explicit Module(const std::string name) : name(name) { + + } + // + virtual std::string print(); + private: + std::string name;//Module name + //need to include global variables, constants, function + std::list functions; + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/IR/Type.h b/projects/PiLeiFX/ZenoFX/IR/Type.h new file mode 100644 index 0000000000..826bd6240c --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/Type.h @@ -0,0 +1,81 @@ +// +// Created by admin on 2022/5/19. +// +#pragma once + +#include +#include +/* + * zfx only three types $ is the Parameter @ is the symbol, The third is the built-in function type + * compare by dimension + * */ +namespace zfx { + enum class TypeKind { + Parameter, + Symbol, + Function + }; + class ParaType; + class SymbolType; + class FunctionType; + class Type { + public: + Type(TypeKind kind, int dim) : kind(kind), dim(dim) {} + + virtual std::string ToString(); + + virtual bool LE(const Type &type) const; + + virtual bool isFunction(); + + virtual bool isParaType(); + + virtual bool isFunctionType(); + + virtual ~Type() = default; + public: + int dim; + TypeKind kind; + }; + bool operator== (const Type &lhs, const Type &rhs) const{ + if (lhs.kind != rhs.kind) { + return false; + } else { + return lhs.dim == rhs.dim; + } + return false; + } + + class ParaType : public Type{ + public: + ParaType(TypeKind kind, int dim) : Type(kind, dim) { + + } + + bool isFunctionType() override final { + return false; + } + + ~ParaType() = default; + }; + + class SymbolType : public Type { + public: + + bool LE(const Type &rhs) override { + return this->dim < rhs.dim; + } + + ~SymbolType() = default; + }; + + class FunctionType : public Type { + public: + + std::string ToString() override { + + } + + ~FunctionType() = default; + }; +} diff --git a/projects/PiLeiFX/ZenoFX/TypeFinder.h b/projects/PiLeiFX/ZenoFX/IR/TypeFinder.h similarity index 98% rename from projects/PiLeiFX/ZenoFX/TypeFinder.h rename to projects/PiLeiFX/ZenoFX/IR/TypeFinder.h index 62721d1605..d8302a3016 100644 --- a/projects/PiLeiFX/ZenoFX/TypeFinder.h +++ b/projects/PiLeiFX/ZenoFX/IR/TypeFinder.h @@ -3,7 +3,7 @@ // #pragma once -#include "Ast.h" +#include "../Ast.h" #include "Module.h" #include #include diff --git a/projects/PiLeiFX/ZenoFX/User.h b/projects/PiLeiFX/ZenoFX/IR/User.h similarity index 100% rename from projects/PiLeiFX/ZenoFX/User.h rename to projects/PiLeiFX/ZenoFX/IR/User.h diff --git a/projects/PiLeiFX/ZenoFX/Value.h b/projects/PiLeiFX/ZenoFX/IR/Value.h similarity index 73% rename from projects/PiLeiFX/ZenoFX/Value.h rename to projects/PiLeiFX/ZenoFX/IR/Value.h index bf1db21129..0935b2db8b 100644 --- a/projects/PiLeiFX/ZenoFX/Value.h +++ b/projects/PiLeiFX/ZenoFX/IR/Value.h @@ -8,11 +8,13 @@ #include #include - +/*Value zfx中所有的数据类型都是继承于value的*/ namespace zfx { class Use { + friend bool operator==() { + } }; class Value { public: @@ -26,14 +28,18 @@ namespace zfx { void add_use(Value *value); bool set_name(std::string name) { + if (name == "") { + } } std::string get_name() const; void remove_use(Value *val); - virtual std::string print() = 0; + virtual std::string print() { + return ""; + } private: std::string name; Type *type; diff --git a/projects/PiLeiFX/ZenoFX/Lexical.h b/projects/PiLeiFX/ZenoFX/Lexical.h index 70a90c4877..161b945734 100644 --- a/projects/PiLeiFX/ZenoFX/Lexical.h +++ b/projects/PiLeiFX/ZenoFX/Lexical.h @@ -1,145 +1,154 @@ // // Created by admin on 2022/5/7. // -#include "Location.h" +#include "Error.h" #include #include #include #pragma once -//inline char opchars[] = "+-*/%=(),.;<>!&|^?:"; -/*inline std::set opstrs = { - "(", ")", ",", ".", ";", - "+", "-", "*", "/", "%", "=", - "+=", "-=", "*=", "/=", "%=", - "==", "!=", "<", "<=", ">", ">=", - "&", "&!", "|", "^", "!", "?", ":", - }; -*/ -namespace zfx { - - enum class TokenKind {Op, Seprator, KeywordKind, Eof}; - enum class Op{ - Plus, //+ - Minus, //- - Multiply, // * - Divide, // / - Modules, // % - Assign, // = - L, // < - G, // > - LE, // <= - GE, // >= - MultiplyAssign, // *= - DivideAssign, // /= - ModulesAssign, // %= - PlusAssign, //+= - MinusAssign, //-= - BitNot, //~ - BitAnd, //& - BitXor, //^ - BitOr, // | - At, //@ - Comma, //, - Dot, //. - Not, // ! - And, // && - Or, // || - QuesstionMark // ? - }; - - enum class Seprator { - OpenBracket = 0, //[ - CloseBracket, //] - OpenParen, //( - CloseParen, //) - OpenBrace, //{ - CloseBrace, //} - Colon, //: - SemiColon, //; - }; - - enum class KeywordKind { -//for instance if while for ... to be added later as appropriate - Pos, - data, - frame, - rad, - $// - }; - - std::string toString(TokenKind kind); - std::string toString(Op op); - - struct Token { - TokenKind kind; - std::string text; - Position pos; - Token(TokenKind kind, const std::string& text, const Position& pos) : kind(kind), text(text), pos(pos) {} - Token(TokenKind kind, char c, const Position& pos) : kind(kind), text(std::string(1, c)), pos(pos) {} - std::string toString() { - return std::string("Token") + ":" + this->pos.toString() + this->text; - } - }; - - - Class CharStream { - public: - std::string data; - uint32_t pos = 0; - uint32_t line = 1; - uint32_t col = 0; - CharStream(const std::string& data) : data(data){ - - } - - char peak() { - return this->data[this->pos]; - } - char next() { - char ch = this->data[this->pos++]; - if (ch == '\n') { - this->line++; - this->col = 0; - } else { - this->col++; - } - return ch; - } +namespace zfx { - bool eof () { - return this->peak() == '\0'; - } +enum class TokenKind { + Op, + Seprator, + KeywordKind, + Decl, + Eof +}; +enum class Op { + Plus, //+ + Minus, //- + Multiply, // * + Divide, // / + Modules, // % + Assign, // = + L, // < + G, // > + LE, // <= + GE, // >= + MultiplyAssign, // *= + DivideAssign, // /= + ModulesAssign, // %= + PlusAssign, //+= + MinusAssign, //-= + BitNot, //~ + BitAnd, //& + BitXor, //^ + BitOr, // | + At, //@ + Comma, //, + Dot, //. + Not, // ! + And, // && + Or, // || + QuesstionMark // ? +}; + +enum class Seprator { + OpenBracket = 0, //[ + CloseBracket, //] + OpenParen, //( + CloseParen, //) + OpenBrace, //{ + CloseBrace, //} + Colon, //: + SemiColon, //; +}; + +enum class KeywordKind { + //for instance if while for ... to be added later as appropriate + Pos, + data, + frame, + rad, + // +}; + +enum class Decl { + Para, + Symbol +}; + +std::string toString(TokenKind kind); +std::string toString(Op op); + +struct Token { + TokenKind kind; + std::string text; + Position pos; + Token(TokenKind kind, const std::string &text, const Position &pos) : kind(kind), text(text), pos(pos) { + } + Token(TokenKind kind, char c, const Position &pos) : kind(kind), text(std::string(1, c)), pos(pos) { + } + std::string toString() { + return std::string("Token") + ":" + this->pos.toString() + this->text; + } +}; + +class CharStream { + public: + std::string data;//rep source code + uint32_t pos = 0; + uint32_t line = 1; + uint32_t col = 0; + CharStream(const std::string &data) : data(data) { + } + + char peak() { + return this->data[this->pos]; + } + + char next() { + char ch = this->data[this->pos++]; + if (ch == '\n') { + this->line++; + this->col = 0; + } else { + this->col++; + } + return ch; + } + + bool eof() { + return this->peak() == '\0'; + } + + Position getPosition() { + //return the line and column numbers of the current charactor + return Position(this->pos+1, this->pos + 1,this->line, this->col); + } +}; - Position getPosition() { - //return the line and column numbers of the current charactor - return Position(this->line, this->col); - } - }; - -/* class Scanner { private: - std::vector tokens; - CharStream& stream; - Position lastPos; + std::list tokens; + CharStream stream; + Position lastPos{0, 0, 0, 0}; static std::unordered_map KeywordMap; + public: + Scanner(CharStream &stream) : stream(stream) { + + } Token next() { if (this->tokens.empty()) { auto t = this->getAToken(); + //set pos + this->lastPos = t.pos; return t; - // } else { - auto t = thi->tokens.front(); - // + auto t = this->tokens.front(); + this->lastPos = t.pos; + this->tokens.pop_front(); + return t; } } Token peek() { - if(this->tokens.empty()) { + if (this->tokens.empty()) { auto t = this->getAToken(); - this->tokens.push_back(t); + this->tokens->push_back(t); return t; } else { auto t = this->tokens.front(); @@ -152,147 +161,75 @@ namespace zfx { auto t = this->getAToken(); this->tokens.push_back(t); } - } - Position getNextPos() { + if (this->tokens.size() < 2) { + return Token{};//EofToken + } + auto it = this->tokens.begin(); + std::advance(it, 1); + return *it1; } - Position getLastPos() { - - } private: + //getAToken主要还是由Token peek Token next这几个函数驱动 Token getAToken() { - this->skipWhiteSpaces(); - auto pos = this->stream.getPosition(); - if (this->stream.eof() == '\n') { - return Token(TokenKind::Eof, "EOF", pos); + this->skipWhiteSpace(); + auto Pos = this->stream.getPosition(); + if (this->stream.eof()) { + return Token(TokenKind::Eof, "Eof", pos); } else { - auto ch = this->stream.peek(); - if (ch == '#') { - this->skipSingleComment(); - return this->getAToken(); - } else if (this->isDigit(ch)) { + auto ch = this->stream.peak(); + if (this->) { + + } else if (ch == '"') { + return this->parseStringLiteral(); + } else if (ch == '$') { + this->stream.next(); + return Token(TokenKind::Decl, '$', pos); + } else if (ch == '@') { this->stream.next(); - auto ch1 = this->stream.peek(); - std::string literal = ""; - if (ch == '0') { - - } - } else if () { + return Token(TokenKind::Decl, '@', pos); + } else if (this->Digit(ch)) { } - - } - } - - - - void skipWhiteSpaces() { - while (this->isWhiteSpace()) { + } else { + std::cout << "unexpected character : " + std::string(1,ch) << std::endl; this->stream.next(); + return this->getAToken(); } } - void skipSingleLineComment() { + void skipSingleComment() { this->stream.next(); - while (this->stream.peek() != '\n' && this->stream.eof()) { + while (this->stream.peek() != '\n' && !this->stream.eof()) { this->stream.next(); } } - bool isWhiteSpace(char ch) { - return (ch == ' ' || ch == '\n' || ch == '\t'); - } - - inline bool isDigit(char ch) { - return (ch >= '0' && ch <= '9'); - } - - inline bool is_symbolic_atom(char ch) { - if () return false; - if (isspace(ch) ||) { - return true; - } - return false; - } - - inline int swizzle_from_char(char ch) { - if ('x' <= c && c <= 'z') { - return c - 'z'; - } else if (c == 'w') { - return 3; - } else if('0' <= c && c <= '9') { - return c - '0'; - } else if () { - - } else { - return -1; - } - - } - Token parseIdentifer() { - Token token; - if (this->KeywordMap.find(token.text)) { - + void skipWhiteSpace() { + while (this->isWhiteSpace(this->stream.peak())) { + this->stream.next(); } - return token; - } - }; -*/ - - class Scanner { - private: - std::list tokens; - std::string data; - Position lastPos{0, 0, 0, 0}; - public: - Scanner(const std::string &data) : data(data) { + }; - } - Token next() { - if (this->tokens.empty()) { - auto t = this->getAToken(); - //set pos - this->lastPos = t.pos; - return t; - } else { - auto t = this->tokens.front(); - this->lastPos = t.pos; - this->tokens.pop_front(); - return t; - } + bool isWhiteSpace(char ch) { + return (ch == ' ' || ch == '\n', || ch == '\t'); } - Token peek() { - if (this->tokens.empty()) { - auto t = this->getAToken(); - this->tokens->push_back(t); - return t; - } else { - auto t = this->tokens.front(); - return t; - } + bool isLetter(char ch) const { + return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' || ch <= 'z'); } - Token peek2() { - while (this->tokens.size() < 2) { - auto t = this->getAToken(); - this->tokens.push_back(t); - } - - if (this->tokens.size() < 2) { - return Token{};//EofToken - } - - auto it = this->tokens.begin(); - std::advance(it, 1); - return *it1; + bool isLetterDigitOurUnderScore(char ch) { + return (ch >= 'A' && ch <= 'Z' || + ch >= 'a' && ch <= 'z' || + ch >= '0' && ch <= '9' || + ch == '-'); } - private: - Token getAToken() { - + bool isDigit(char ch) { + return (ch >= '0' && ch <= '9'); } }; } diff --git a/projects/PiLeiFX/ZenoFX/Location.h b/projects/PiLeiFX/ZenoFX/Location.h deleted file mode 100644 index e9e723ff51..0000000000 --- a/projects/PiLeiFX/ZenoFX/Location.h +++ /dev/null @@ -1,94 +0,0 @@ -// -// Created by admin on 2022/5/8. -// - -#pragma once - -#include -#include -namespace zfx { - class Position { - public: - uint32_t line; - uint32_t col; - Position(uint32_t line, uint32_t col) : line(line), col(col){ - - } - - Position(const Position& rhs) { - this->line = rhs.line; - this->col = rhs.col; - } - bool operator==(const Position& rhs) const { - return this->col == rhs.col && this->line == rhs.line; - } - - bool operator!=(const Position& rhs) const { - return !(*this == rhs); - } - - bool operator<(const Position& rhs) const { - if (this->line == rhs.line) { - return this->col < rhs.col; - } else { - return line < rhs.line; - } - } - - bool operator>(const Position& rhs) const { - if (this->line == rhs.line) { - return this->col > rhs.col; - } else { - return this->line > rhs.line; - } - } - - bool operator<=(const Position& rhs) const { - return *this == rhs || *this < rhs; - } - - bool operator>=(const Position& rhs) const { - return *this == rhs || *this > rhs; - } - - std::string toString() { - return ("(ln" + std::to_string(this->line) + - ", col : " + std::to_string(this->col) + ")"); - } - }; - - class Location { - public: - Position begin, end; - Location() : begin(0, 0), end(0, 0){ - - } - - Location(const Position& begin, uint32_t length) : begin(begin), end(begin.line, begin.col + length) { - - } - - Location(const Location& begin, const Location& end) : begin(begin), end(end){ - - } - - bool operator==(const Location& rhs) { - - } - - bool operator!=(const Location& rhs) { - - } - - bool operator<(const Location& rhs) { - - } - - bool operator>(const Location& rhs) { - - } - std::string toString() { - - } - }; -} diff --git a/projects/PiLeiFX/ZenoFX/LowerAST.h b/projects/PiLeiFX/ZenoFX/LowerAST.h index 7332e817e8..5e25e1ed19 100644 --- a/projects/PiLeiFX/ZenoFX/LowerAST.h +++ b/projects/PiLeiFX/ZenoFX/LowerAST.h @@ -4,10 +4,10 @@ // #pragma once #include "Ast.h" +#include "IR/IR.h" #include "Stmt.h" -#include "IR.h" -#include #include +#include /* * convent ast to statement * */ diff --git a/projects/PiLeiFX/ZenoFX/Module.h b/projects/PiLeiFX/ZenoFX/Module.h deleted file mode 100644 index 67b1529e5e..0000000000 --- a/projects/PiLeiFX/ZenoFX/Module.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by admin on 2022/5/19. -// - -#pragma once - -namespace zfx { - class Module { - public: - std::string name; - explicit Module(const std::string name) : name(name) { - - } - }; -} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Pass.h b/projects/PiLeiFX/ZenoFX/Pass.h index 2b3970a4f3..7bf0ee270e 100644 --- a/projects/PiLeiFX/ZenoFX/Pass.h +++ b/projects/PiLeiFX/ZenoFX/Pass.h @@ -4,21 +4,38 @@ #pragma once +#include "IR/Module.h" + namespace zfx { class Pass { public: - Pass() = default; - virtual ~Pass() = default; + Pass(Module *m) {} + virtual ~Pass() ; virtual void run() = 0; - }; - - class BasicBlockPass : public Pass{ - public: - void run() override; + private: + //this is zfx top-level data structure + Module *m; }; class PassManger { public: + PassManger(Module *m) : m(m) {} + template + void add_pass(bool print_ir = false) { + passes.push_back(std::pair(new Pass(m), print_ir)); + } + + void run() { + for (auto Pass : passes) { + Pass.first->run(); + if (Pass.second) { + //print ir; + } + } + } + private: + std::vector> passes; + Module *m; }; } diff --git a/projects/PiLeiFX/ZenoFX/Builder_CFG.cpp b/projects/PiLeiFX/ZenoFX/Pass/Builder_CFG.cpp similarity index 100% rename from projects/PiLeiFX/ZenoFX/Builder_CFG.cpp rename to projects/PiLeiFX/ZenoFX/Pass/Builder_CFG.cpp diff --git a/projects/PiLeiFX/ZenoFX/CFG.cpp b/projects/PiLeiFX/ZenoFX/Pass/CFG.cpp similarity index 98% rename from projects/PiLeiFX/ZenoFX/CFG.cpp rename to projects/PiLeiFX/ZenoFX/Pass/CFG.cpp index 2b723c8548..63e45acb54 100644 --- a/projects/PiLeiFX/ZenoFX/CFG.cpp +++ b/projects/PiLeiFX/ZenoFX/Pass/CFG.cpp @@ -3,7 +3,6 @@ // #include "CFG.h" - namespace zfx { } \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Pass/CFG.h b/projects/PiLeiFX/ZenoFX/Pass/CFG.h new file mode 100644 index 0000000000..53f298f0a4 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Pass/CFG.h @@ -0,0 +1,57 @@ +// +// Created by admin on 2022/5/26. +// +#pragma once + +#include "../Pass.h" +#include +#include +#include + +namespace zfx { + class CFG : public Pass { + //build CFG + + public: + explicit CFG(Module *m) : Pass(m) {} + + void run() final; + + BasicBlock * getEntryBB(){ + return entry; + } + + //get those unreachable block + std::set getUnreachableBB { + return unreachable; + }; + + //Get the predecessors of a basic block (all blocks that can reach the basic block in one step) + std::set getPrevBB(BasicBlock *b) { + + } + + std::set getSuccBB(BasicBlock *b) { + + } + + std::set getTerminators() { + // + } + private: + + void cleanAll() { + //clean CFG; + entry = nullptr; + successor_map.clear(); + precessor_map.clear(); + term_set.clear(); + } + + BasicBlock *entry{nullptr}; + std::map> successor_map; + std::map> precessor_map; + std::set unreachable;//some unreachable BasicBlock used as dead code elimination + std::set term_set; + }; +} diff --git a/projects/PiLeiFX/ZenoFX/Pass/active_var.h b/projects/PiLeiFX/ZenoFX/Pass/active_var.h new file mode 100644 index 0000000000..904d8a6a3d --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Pass/active_var.h @@ -0,0 +1,23 @@ +// +// Created by admin on 2022/5/31. +// +#include "../Pass.h" +#include "../IR/BasicBlock.h" +#include +#include +//用来死代码删除 +namespace zfx { + class ActiveVars : public Pass { + public: + explicit ActiveVars(Module *m) : Pass(M) {} + void run() override; + std::unordered_set getLiveOut(BasicBlock *bb); + std::unordered_set getLiveIn(BasicBlock *bb); + bool isLiveOut() { + + } + + private: + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/Type.h b/projects/PiLeiFX/ZenoFX/Type.h deleted file mode 100644 index dbe7939945..0000000000 --- a/projects/PiLeiFX/ZenoFX/Type.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// Created by admin on 2022/5/19. -// -#pragma once - -#include -#include - -namespace zfx { - class Type { - - }; -} diff --git a/projects/PiLeiFX/ZenoFX/visitor.h b/projects/PiLeiFX/ZenoFX/visitor.h index 5a2e02421d..e5cebb04ed 100644 --- a/projects/PiLeiFX/ZenoFX/visitor.h +++ b/projects/PiLeiFX/ZenoFX/visitor.h @@ -4,9 +4,9 @@ #pragma once -#include "Module.h" -#include +#include "IR/Module.h" #include +#include /* * This is a semantic analysis module * */ diff --git a/projects/PiLeiFX/ZenoFX/zfx.cpp b/projects/PiLeiFX/ZenoFX/zfx.cpp index 3e60db0608..9e1b2db17b 100644 --- a/projects/PiLeiFX/ZenoFX/zfx.cpp +++ b/projects/PiLeiFX/ZenoFX/zfx.cpp @@ -5,7 +5,10 @@ #include #include "parser.h" namespace zfx { - +/* + * zfx + * + * */ std::tuple<> compile_to_assembly (const std::string& code, const Options& options) { #ifdef ZFX_PRINT_IR std::cout << "start zfx" << std::endl; From eaf06e748df0e0876105b7cf24368905951dae5b Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Sat, 11 Jun 2022 14:39:20 +0800 Subject: [PATCH 13/14] continue --- projects/PiLeiFX/ZenoFX/Ast.h | 258 ++++-------------- projects/PiLeiFX/ZenoFX/IR/BasicBlock.h | 13 +- projects/PiLeiFX/ZenoFX/IR/IR.h | 27 ++ projects/PiLeiFX/ZenoFX/IR/IRBuilder.h | 28 ++ projects/PiLeiFX/ZenoFX/IR/Instruction.h | 67 +++++ projects/PiLeiFX/ZenoFX/IR/statements.h | 32 +++ projects/PiLeiFX/ZenoFX/Parser.h | 53 ++-- projects/PiLeiFX/ZenoFX/Pass/const_flold.h | 16 ++ projects/PiLeiFX/ZenoFX/Pass/die.cpp | 5 + projects/PiLeiFX/ZenoFX/include/x64.h | 290 ++++++++++++++++++++- projects/PiLeiFX/ZenoFX/include/zfx.h | 12 +- 11 files changed, 550 insertions(+), 251 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/IR/IRBuilder.h create mode 100644 projects/PiLeiFX/ZenoFX/IR/Instruction.h create mode 100644 projects/PiLeiFX/ZenoFX/IR/statements.h create mode 100644 projects/PiLeiFX/ZenoFX/Pass/const_flold.h create mode 100644 projects/PiLeiFX/ZenoFX/Pass/die.cpp diff --git a/projects/PiLeiFX/ZenoFX/Ast.h b/projects/PiLeiFX/ZenoFX/Ast.h index ad492e8155..506bf451ea 100644 --- a/projects/PiLeiFX/ZenoFX/Ast.h +++ b/projects/PiLeiFX/ZenoFX/Ast.h @@ -13,133 +13,87 @@ #include #include namespace zfx { -/* -enum class Ast_Node_Type { - Ast_Type_Statement, - Ast_Type_Declaration, - Ast_Type_Identifier, - Ast_Type_Integer_Literal, - Ast_Type_Binary_Op, - Ast_Type_Unary_Op - }; - */ -/* - class UnaryOpStmt; - class BinaryOpStmt; - class TernaryOpStmt; - class FunctionCallStmt; - class AssignStmt; - class SymbolStmt; - class SymbolStmt; - class ParamSymbolStmt; - class TempSymbolStmt; - class LiterialStnt; - class FrontedIfStmt; - class FrontedElseStmt; - class FrontedElseIfStmt; - class FrontedEndIfStmt; - - class AstVisitor { - public: - virtual ~visitor() { - - } - - virtual std::any visitVariable(Variable& variable, std::string additional = ""); - - virtual std::any visitFunctionCall(FunctionCall& functionCall, std::string additional = ""); - - virtual std::any visitBinary(Binary& binary, std::strig additional = ""); - - virtual std::any visitUnary(Unary& unary, std::string additional = ""); - - virtual std::any visitTenary(Tenary& tenary, std::string additional = ""); - - virtual std::any visitAssign(AssignStmt& assign, std::string additional = ""); - - virtual std::any visitLiteral(Literal& literal, std::string additional = ""); - - virtual std::any visitIfStmt(ExprIfStmt& exprIfStmt, std::string additional = ""); + enum VarType { + Symbol, + Parameter }; - class AsmVisitor { - public: - virtual ~AsmVisitor() { + enum Op { - } - - virtual std::any visitAsm + }; + class AstVisitor { }; class AstNode { public: - Position beginPos;// + Position beginPos; Position endPos; - - AstNode(const Position& beginPos, const Position& endPos) : beginPos(beginPos), endPos(endPos) {}; - virtual std::any accept(AstVisitor& visitor, std::string additional = "") = 0; + virtual void dump() = 0; + virtual std::any accept(AstVisitor &visitor, std::string additional = "") = 0; + virtual ~AstNode(); }; +//语句 + class Statement: AstNode { - class Statement : public AstNode { + }; + //声明,子类为$ @ + class Decl :public AstNode { public: - int id; - int dim = 0; - Statement(const Position& beginPos, const Position& endPos, int id, int dim) - : AstNode(beginPos, endPos),id(id), dim(dim) { + VarType varType;//变量类型 + std::string name;//变量名称 + expilic Decl(VarType varType) : varType (varType) { } }; - class UnaryOpStmt : public Statement { + class VariableDecl : public Decl { + //变量类型 + //变量初始化的形式, + public: + VarType varType; + //std::shared_ptr init;//变量初始化的语句 + explicit VariableDecl(VarType varType , const std::string& name) : Decl(varType), name(name) { - }; + } + std::any accept(AstVisitor& visitor) { - class Ast_Identifier { + } - }; + void dump(std::string prefix) { - class Expression : public AstNode{ - Expression(const Position& beginPos, const Position& endPos) + } }; - */ -/* - * when we declare a variable , we will create a Ast Node : VariableDecl, if this Ast node has initialization - * we will create a single node represent initialization, but if only to declare we will only create one node; - * */ -/* - class VariableDecl : public Expression { - std::string name; - //std::shared_ptr<>; + class Statement : public AstNode { - std::string toString() { - return this->name; - } }; - class FunctionCall : public Expression { + class Expression:public AstNode { public: - std::string name;//function name - std::vector> arguments; + std::any constValue;//本表达式的常量值,用作后面的常量折叠等分析 + }; - FunctionCall() { + class ExpressionStatement : public Statement { + public: + std::shared_ptr exp; - } - virtual std::string }; - + /* + * 二元表达式 + * */ class Binary { public: Op op; - std::shared_ptr exp1;// left expression - std::shared_ptr exp2;// right expression + //左边表达式 + //右边表达式 Binary() { } - std::string toString() { + + std::any accept(AstVisitor& visitor, const std::string &additional) { } }; @@ -147,135 +101,15 @@ enum class Ast_Node_Type { class Unary { public: Op op; - bool isPrefix;//whether is prefix operation; + //表达式 Unary() { } - }; - - class Tenary { - //Tenary Operation; - public: - std::shared_ptr cond; - std::shared_ptr lhs; - std::shared_ptr rhs; - }; - */ -/* - * Notice assignStmt is right associative - * - * */ -/* - class AssignStmt { - public: - std::shared_ptr lhs; - std::shared_ptr value_to_assign; - }; - - - class ExprIfStmt { - - }; - - - class IntegerLiteral : public Expression { - int32_t value; - - }; - - class FloatLiteral : public Expression { - - }; - - class AsmStatement { - - }; - - class AsmAssignStmt : public AsmStatement{ - - }; - - class AsmLoadConstStmt : public AsmStatement { - - }; - - class AsmTernaryOpStmt : public AsmStatement { - - }; - - class AsmBinaryOpStmt : public AsmStatement { - - }; - - class AsmUnaryOpStmt : public AsmStatement { - - }; - - class AsmFuncCallStmt : public AsmStatement { - - }; - - class AsmLocalLoadStmt : public AsmStatement { - - }; - - class AsmLocalStoreStmt : public AsmStatement { - - }; - - class AsmGlobalLoadStmt : public AsmStatement { - - }; - - class AsmGlobalStoreStmt : public AsmStatement { - }; - - class AsmParamLoadStmt : public AsmStatement { - - }; - - class AsmIfStmt : public AsmStatement { - - }; - */ -/* - class Ast { - public: - using Iter = std::vector ::iterator - Ast() { + std::any accept(AstVisitor& visitor, const std::string &additional) { } - private: - Token token; - }; - - inline std::unique_ptr make_ast() { - - } - */ - - class AstVisitor { - - }; - - class AstNode { - public: - Position beginPos; - Position endPos; - - virtual std::any accept(AstNode &visitor, std::string additional = "") = 0; - - }; -//语句 - class Statement: AstNode { - - }; - //声明,子类为$ @ - class Decl { - }; - class } diff --git a/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h b/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h index 94c7ec7c65..793ee37943 100644 --- a/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h +++ b/projects/PiLeiFX/ZenoFX/IR/BasicBlock.h @@ -17,16 +17,25 @@ namespace zfx { return new BasicBlock(m, name); } - Module* getModule() ; + BasicBlock(const BasicBlock& ) = delete; + BasicBlock& operator=(const BasicBlock&) = delete; + const Module* getModule() const { + + } + + Module* getModule() { + + } virtual std::string print() override; + //api for cfg std::list &get_pre_basic_blocks() { } - std::list + //选一个迭代器遍历一遍 private: explicit BasicBlock(Module *m, const std::string &name); std::list pre_bbs; diff --git a/projects/PiLeiFX/ZenoFX/IR/IR.h b/projects/PiLeiFX/ZenoFX/IR/IR.h index bfdf0b94fd..9ada945704 100644 --- a/projects/PiLeiFX/ZenoFX/IR/IR.h +++ b/projects/PiLeiFX/ZenoFX/IR/IR.h @@ -2,4 +2,31 @@ // Created by admin on 2022/5/7. // #pragma once +#include +#include +#include +#include +/* + * zfx中statement就相当于一条一条的Instruction + * */ +namespace zfx { + class Stmt ; + + class IRNode { + public: + + virtual IRNode* get_parent() const = 0; + + virtual IRNode* get_ir_root(); + virtual ~IRNode() = default; + }; + class Statement { + protected: + std::vector<> + }; + + class IRVisitor { + + }; +} diff --git a/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h b/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h new file mode 100644 index 0000000000..c076b36120 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h @@ -0,0 +1,28 @@ +// +// Created by admin on 2022/6/7. +// + +#pragma once + +#include "./BasicBlock.h" +#include "./Instruction.h" + +namespace zfx { + class IRBuilder { + public: + IRBuilder() = default; + IRBuilder(BasicBlock *bb) : bb(bb) {} + + inline BasicBlock *getInsertBlock() { + return bb; + } + + void setInsertPoint(BasicBlock *bb) const { + this->bb = bb; + } + + //接下来是创建指令 + private: + BasicBlock *bb; + }; +} diff --git a/projects/PiLeiFX/ZenoFX/IR/Instruction.h b/projects/PiLeiFX/ZenoFX/IR/Instruction.h new file mode 100644 index 0000000000..0b84579f82 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/Instruction.h @@ -0,0 +1,67 @@ +// +// Created by admin on 2022/6/7. +// + +#pragma once +#include "./User.h" +#include "./BasicBlock.h" +#include "./Module.h" +#include +#include + +namespace zfx { + class BasicBlock; + class Module; + class Instruction : public User { + public: + Instruction(const Instruction &) = delete; + Instruction& operator= (const Instruction &) = delete; + + const BasicBlock* getParent() const { + + } + + BasicBlock* getParent() { + + } + + const Module* getModule() const { + + } + + Module* getModule() { + + } + + + /* + * 留几个bool函数来判断指令到底是属于啥类型的 + * */ + + inline bool isUnaryOp() const { + + } + + inline bool isBinaryOp() const { + + } + + inline bool isTenaryOp() const { + + } + + void insertBefore(Instruction *InsertPos); + + void insertAfter(Instruction *InsertPos); + + void moveBefore(Instruction *MovBefore); + + + private: + BasicBlock* Parent; + + }; + + //接下来定义几个指令继承自Instruction + +} diff --git a/projects/PiLeiFX/ZenoFX/IR/statements.h b/projects/PiLeiFX/ZenoFX/IR/statements.h new file mode 100644 index 0000000000..fd1ce74f9e --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/statements.h @@ -0,0 +1,32 @@ +// +// Created by admin on 2022/6/10. +// + +#pragma once + +#include "./IR.h" +#include + +namespace zfx { + //所有的操作语句都继承自Stmt + class UnaryOpStmt : public Stmt { + public: + //一元操作符的数据类型 + //操作数 + // + }; + + class BinaryOpStmt : public Stmt { + public: + //数据类型 + //左右操作数 + + }; + + class TernaryOpStmt : public Stmt { + public: + //三元操作数类型 + //三个stmt + + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Parser.h b/projects/PiLeiFX/ZenoFX/Parser.h index e42ed5ec84..f91cd76a34 100644 --- a/projects/PiLeiFX/ZenoFX/Parser.h +++ b/projects/PiLeiFX/ZenoFX/Parser.h @@ -3,14 +3,15 @@ // /* *Prog : statementList? EOF; - *statementList : statement+; + *statementList : (variableDecl | functionDecl | expressionStatement); + * variableDecl : '$'|'@' Identifier * statement : block | expressionStatement | ifStatement | forStatement * emptyStatement | functionDecl | variableDecl * * ifStatement : 'if' '(' expression ') statement ('else' statement)?; * forStatement : * variableStatement: - * variableDecl : (Identifier| + * * expression:assignment; *Identifier : [a-zA-z][a-zA-Z0-9]*; * IntegerLiteral : '0' | [1-9][0-9]* @@ -44,48 +45,32 @@ namespace zfx { } - AstNode parse_atom() { - - } - - AstNode parse_operator() { - - } - - AstNode parse_compound() { - - } - - AstNode parse_factor() { - - } - - AstNode parse_term() { - - } - - AstNode parse_side() { - - } - - AstNode parse_cond() { - - } - - AstNode parse_andexpr() { + std::shared_ptr parseVariableDecl() { + auto t = this->scanner.next(); + //解析$或者@ + if (t.kind = TokenKind::Decl) { + } } - AstNode parse_orexpr() { + std::shared_ptr parseAssignment() { } - AstNode parse_expr() { + std::shared_ptr parseBinary(int32_t prec) { } - AstNode parse_stmt() { + std::shared_ptr parseUnary() { + auto t = this->scanner.peak(); + if (t.kind == TokenKind::Op) { + //前缀的一元表达式 + //跳过运算符 + this->scanner.next(); + auto exp = this->parseUnary(); + //return std::make_shared + } } }; diff --git a/projects/PiLeiFX/ZenoFX/Pass/const_flold.h b/projects/PiLeiFX/ZenoFX/Pass/const_flold.h new file mode 100644 index 0000000000..4f70dca3b9 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Pass/const_flold.h @@ -0,0 +1,16 @@ +// +// Created by admin on 2022/6/11. +// + +#pragma once + +#include "../Pass.h" +//让所有的优化都基于ir中的那个pass 类 +namespace zfx { + class ConstantFoldPass : public Pass { + public: + //用一个id去标记一下 + //用Program代表优化后的对象 + + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Pass/die.cpp b/projects/PiLeiFX/ZenoFX/Pass/die.cpp new file mode 100644 index 0000000000..5a78d59313 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/Pass/die.cpp @@ -0,0 +1,5 @@ +// +// Created by admin on 2022/6/11. +// + +//死代码消除 diff --git a/projects/PiLeiFX/ZenoFX/include/x64.h b/projects/PiLeiFX/ZenoFX/include/x64.h index 14bf4785f8..ef2fb752a0 100644 --- a/projects/PiLeiFX/ZenoFX/include/x64.h +++ b/projects/PiLeiFX/ZenoFX/include/x64.h @@ -1,12 +1,13 @@ // // Created by admin on 2022/5/7. // - +//试验性的将zfx转成x64汇编代码 #pragma once #include #include namespace zfx::x64 { + /* struct Executable { }; @@ -14,5 +15,292 @@ namespace zfx::x64 { struct Assembler { std::map> cache; + }; + */ + + enum AsmOpCode { + jmp=0, + je, + jne, + jle, + jl, + jge, + jg, + jbe, + jb, + jae, + ja, + + sete=20, + setne, + setl, + setle, + setg, + setge, + + //8字节指令 + movq=40, + addq, + subq, + mulq, + imulq, + divq, + idivq, + negq, + incq, + decq, + xorq, + orq, + andq, + notq, + leaq, + callq, + retq, + pushq, + popq, + cmpq, + + //4字节指令 + movl=80, + addl, + subl, + mull, + imull, + divl, + idivl, + negl, + incl, + decl, + xorl, + orl, + andl, + notl, + leal, + calll, + retl, + pushl, + popl, + cmpl, + + //2字节指令 + movw=120, + addw, + subw, + mulw, + imulw, + divw, + idivw, + negw, + incw, + decw, + xorw, + orw, + andw, + notw, + leaw, + callw, + retw, + pushw, + popw, + cmpw, + + //单字节指令 + movb=160, + addb, + subb, + mulb, //无符号乘 + imulb, //有符号乘 + divb, //无符号除 + idivb, //有符号除 + negb, + incb, + decb, + xorb, + orb, + andb, + notb, + leab, + callb, + retb, + pushb, + popb, + cmpb, + + //SSE指令 + movsd = 200, + addsd, + subsd, + mulsd, + divsd, + sqrtsd, + maxsd, + minsd, + cmpsd, + comisd, + ucomisd, + + cvttsd2si = 240, //double 到long或int都可以,会导致截断, 第一个操作数可以是内存 + cvtsi2sdq, //从long到double + + + //伪指令 + declVar = 300, //变量声明 + reload, //重新装载被溢出到内存的变量到寄存器 + tailRecursive, //尾递归的函数调用 + tailCall, //尾调用 + tailRecursiveJmp, //尾递归产生的jmp指令,操作数是一个基本块,是序曲下的第一个基本块。 + tailCallJmp, //尾调用产生的jmp指令,操作数是一个字符串(标签) + }; + /* + * 操作数的类型 + * */ + enum OprandKind { + varIndex, //变量下标 + returnSlot,//用于存放返回值的位置 + bb,//调准指令指向的基本快 + function,//函数调用 + stringConst,//字符串常量 + + }; + class OpCodeHelper { + public: + static bool isReturn(AsmOpCode op) { + return op == AsmOpcode::retb || op == AsmOpcode::retw || op == AsmOpcode.retl || op == AsmOpcode::retq; + } + + static bool isJump(AsmOpCode op) { + return op < AsmOpcode; + } + + static bool isMov() { + + } + + }; + + class Oprand{ + public: + OprandKind kind; + std::string name; + + //判断操作数是否相同 + bool isSame() + virtual std::string toString() { + + } + }; + + class Inst { + public: + AsmOpCode op; + static uint32_t index;//下标 + uint32_t numOpRands; + std::string comment; + Inst(AsmOpCode op, uint32_t numOpRands) : op(op), numOpRands(numOpRands) { + index++; + } + + virtual bool is_Inst_0() { + return false; + } + + virtual bool is_Inst_1() { + return false; + } + + virtual bool is_Inst_2() { + return false; + } + virtual std::string toString() = 0; + }; + + + //没有操作数的指令 + class Inst_0 : public Inst{ + public: + // + }; + + class Inst_1 : public Inst { + public: + std::shared_ptr oprand; + bool is_Inst1() override { + return true; + } + + std::string toString() override { + auto str = + } + }; + + class Inst_2 : public Inst { + public: + std::shared_ptr oprand1; + std::shared_ptr oprand2; + + bool isInst_2() override { + return true; + } + + std::string toString() override { + + } + + }; + + class BasicBlock { + public: + std::vector> insts;//基本快内的指令 + int32_t funcIndex{-1};//函数编号 + int32_t bbIndex {-1};//基本块编号 + //是否有其他快跳转到该块 + bool isDestination{false}; + + std::string getName() { + + } + + std::string toString() { + std::string str; + + return str; + } + }; + //变量活跃性分析结果 + struct LivenessResult { + std::map, std::set> liveVars; + std::map, std::set> initalVars; + }; + class AsmModule { + public: + + /* + * 输出代表一个模块的asm文件字符串 + * */ + std::string toString() { + std::string str; + + return std::move(str); + } + }; + //当前汇编依靠直接便令Ast生成,并没有用到自定义ir + class AsmGenerator : public AstVisitor { + public: + std::shared_ptr asmModule; + + //存放一些临时变量 + //std::shared_ptr s; + std::shared_ptr returnSlot; + AsmGenerator() { + this->asmModule = std::make_shared(); + this->returnSlot = std::make_shared(); + } + + //接下来就是用访问者模式生成asm + std::any visitVariable(Variable& variable, std::string prefix) override { + + } + + }; } diff --git a/projects/PiLeiFX/ZenoFX/include/zfx.h b/projects/PiLeiFX/ZenoFX/include/zfx.h index ae1e7151fb..c93090e765 100644 --- a/projects/PiLeiFX/ZenoFX/include/zfx.h +++ b/projects/PiLeiFX/ZenoFX/include/zfx.h @@ -11,11 +11,19 @@ #include namespace zfx { - +//可以单独设置一个表示arch的同文件 struct Options { + //一些基本设置参数加优化选项 // Options two - Options() {} + int simd_width; + int arch_max_regs{16}; + bool const_fold {false}; + bool kill_unreachable_code {false}; + constexpr struct {} for_x64{}; + + constexpr struct {} cuda{}; + Options(decltype(for_x64)) {} Options(int a) {} }; From 10fcb912fff9aecc568c6807366f47e0ff4c88c4 Mon Sep 17 00:00:00 2001 From: "nm243012@163.com" Date: Thu, 16 Jun 2022 21:09:03 +0800 Subject: [PATCH 14/14] add numericeval --- projects/PiLeiFX/ZenoFX/IR/IR.cpp | 5 + projects/PiLeiFX/ZenoFX/IR/IR.h | 5 + projects/PiLeiFX/ZenoFX/IR/IRBuilder.h | 3 + projects/PiLeiFX/ZenoFX/IR/analysis.h | 10 ++ projects/PiLeiFX/ZenoFX/IR/constants.h | 15 +++ projects/PiLeiFX/ZenoFX/Pass.h | 4 +- projects/ZenoFX/CMakeLists.txt | 2 +- projects/ZenoFX/ZFX/include/zfx/zfx.h | 8 +- projects/ZenoFX/ZFX/test.cpp | 3 + projects/ZenoFX/ZFX/zfx.cpp | 1 + projects/ZenoFX/ne.cpp | 144 +++++++++++++++++++++++++ projects/ZenoFX/pw.cpp | 1 - 12 files changed, 194 insertions(+), 7 deletions(-) create mode 100644 projects/PiLeiFX/ZenoFX/IR/IR.cpp create mode 100644 projects/PiLeiFX/ZenoFX/IR/analysis.h create mode 100644 projects/PiLeiFX/ZenoFX/IR/constants.h create mode 100644 projects/ZenoFX/ZFX/test.cpp create mode 100644 projects/ZenoFX/ne.cpp diff --git a/projects/PiLeiFX/ZenoFX/IR/IR.cpp b/projects/PiLeiFX/ZenoFX/IR/IR.cpp new file mode 100644 index 0000000000..c9fb71fc40 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/IR.cpp @@ -0,0 +1,5 @@ +// +// Created by admin on 2022/6/13. +// +#include "IR.h + diff --git a/projects/PiLeiFX/ZenoFX/IR/IR.h b/projects/PiLeiFX/ZenoFX/IR/IR.h index 9ada945704..22719ba096 100644 --- a/projects/PiLeiFX/ZenoFX/IR/IR.h +++ b/projects/PiLeiFX/ZenoFX/IR/IR.h @@ -19,7 +19,12 @@ namespace zfx { virtual IRNode* get_parent() const = 0; virtual IRNode* get_ir_root(); + virtual ~IRNode() = default; + + //获取一个编译选项,表示怎么处理IR + // + std::unique_ptr clone();//克隆IRNode节点 }; class Statement { protected: diff --git a/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h b/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h index c076b36120..ab66a4d0e5 100644 --- a/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h +++ b/projects/PiLeiFX/ZenoFX/IR/IRBuilder.h @@ -13,6 +13,9 @@ namespace zfx { IRBuilder() = default; IRBuilder(BasicBlock *bb) : bb(bb) {} + void reset() ; + //把所有的指令清空 + inline BasicBlock *getInsertBlock() { return bb; } diff --git a/projects/PiLeiFX/ZenoFX/IR/analysis.h b/projects/PiLeiFX/ZenoFX/IR/analysis.h new file mode 100644 index 0000000000..b17ed1734d --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/analysis.h @@ -0,0 +1,10 @@ +// +// Created by admin on 2022/6/13. +// +//对IR做基础的分析 +#include +#include + +namespace zfx { + +} diff --git a/projects/PiLeiFX/ZenoFX/IR/constants.h b/projects/PiLeiFX/ZenoFX/IR/constants.h new file mode 100644 index 0000000000..b3144cad50 --- /dev/null +++ b/projects/PiLeiFX/ZenoFX/IR/constants.h @@ -0,0 +1,15 @@ +// +// Created by admin on 2022/6/13. +// + +#pragma once + +namespace zfx { + //zfx中的一些常量表示 + class Constant { + public: + Constant() {} + + ~Constant() = default; + }; +} \ No newline at end of file diff --git a/projects/PiLeiFX/ZenoFX/Pass.h b/projects/PiLeiFX/ZenoFX/Pass.h index 7bf0ee270e..693ef1d004 100644 --- a/projects/PiLeiFX/ZenoFX/Pass.h +++ b/projects/PiLeiFX/ZenoFX/Pass.h @@ -5,10 +5,12 @@ #pragma once #include "IR/Module.h" - +#include namespace zfx { + using PassID = std::string; class Pass { public: + PassID Pass(Module *m) {} virtual ~Pass() ; virtual void run() = 0; diff --git a/projects/ZenoFX/CMakeLists.txt b/projects/ZenoFX/CMakeLists.txt index c7b83083f0..ac9296a245 100644 --- a/projects/ZenoFX/CMakeLists.txt +++ b/projects/ZenoFX/CMakeLists.txt @@ -3,7 +3,7 @@ add_subdirectory(ZFX) target_include_directories(zeno PRIVATE .) target_link_libraries(zeno PRIVATE $) -target_sources(zeno PRIVATE nw.cpp pw.cpp pnw.cpp ppw.cpp pew.cpp dbg_printf.h) +target_sources(zeno PRIVATE nw.cpp pw.cpp pnw.cpp ppw.cpp pew.cpp ne.cpp dbg_printf.h) if (ZENO_WITH_zenvdb) option(ZENOFX_ENABLE_OPENVDB "Enable OpenVDB wrangler in ZenoFX" ON) diff --git a/projects/ZenoFX/ZFX/include/zfx/zfx.h b/projects/ZenoFX/ZFX/include/zfx/zfx.h index 98bca24727..c4e9470c1c 100644 --- a/projects/ZenoFX/ZFX/include/zfx/zfx.h +++ b/projects/ZenoFX/ZFX/include/zfx/zfx.h @@ -50,11 +50,11 @@ struct Options { void define_symbol(std::string const &name, int dimension) { symdims[name] = dimension; } - +//插入定义的符号 void define_param(std::string const &name, int dimension) { pardims[name] = dimension; } - +//插入参数 void dump(std::ostream &os) const { for (auto const &[name, dim]: symdims) { os << '/' << name << '/' << dim; @@ -79,10 +79,10 @@ std::tuple ( std::string const &code , Options const &options ); - +//分别是code symbol params new symbol; struct Program { std::vector> symbols; - std::vector> params; + std::vector> params;@ 和$ std::map newsyms; std::string assembly; diff --git a/projects/ZenoFX/ZFX/test.cpp b/projects/ZenoFX/ZFX/test.cpp new file mode 100644 index 0000000000..aad89f6586 --- /dev/null +++ b/projects/ZenoFX/ZFX/test.cpp @@ -0,0 +1,3 @@ +// +// Created by admin on 2022/6/15. +// diff --git a/projects/ZenoFX/ZFX/zfx.cpp b/projects/ZenoFX/ZFX/zfx.cpp index 045cfd3875..a9afe50786 100644 --- a/projects/ZenoFX/ZFX/zfx.cpp +++ b/projects/ZenoFX/ZFX/zfx.cpp @@ -78,6 +78,7 @@ std::tuple newsyms = apply_detect_new_symbols( ir.get(), temporaries, symbols); #ifdef ZFX_PRINT_IR + ir->print(); #endif } diff --git a/projects/ZenoFX/ne.cpp b/projects/ZenoFX/ne.cpp new file mode 100644 index 0000000000..875c9f6ee4 --- /dev/null +++ b/projects/ZenoFX/ne.cpp @@ -0,0 +1,144 @@ +// +// Created by admin on 2022/6/15. +// + +#include +#include + +#include +#include +#include +#include +#include "dbg_printf.h" + +namespace { + static zfx::Compiler compiler; + static zfx::x64::Assembler assembler; + + static void numeric_eval (zfx::x64::Executable *exec, + std::vector &chs) { + auto ctx = exec->make_context(); + for (int j = 0; j < chs.size(); j++) { + ctx.channel(j)[0] = chs[j]; + } + ctx.execute(); + for (int j = 0; j < chs.size(); j++) { + chs[j] = ctx.channel(j)[0]; + } + + } + + struct NumericEval : zeno::INode { + virtual void apply() { + auto code = get_input("zfxCode")->get(); +//一个模板函数,返回一个std::shared_ptr,这里对这一个智能指针调用get()返回一个裸指针 + zfx::Options opts(zfx::Options::for_x64); + opts.detect_new_symbols = true; + + //接收参数 + auto params = has_input("params") ? get_input("params") : + std::make_shared(); + + std::vector parvals;//存储参数值 + std::vector> parnames; + for (auto const &[key_, obj] : params->lut) { + //lut是DictObject中的一个map + //zany是std::shared_ptr的别名 + auto key = '$' + key_; + auto par = zeno::objectToLiterial(obj); + //par 是一个NumericValue + //ObjectToLiterial是一个模板函数由两个重载一个返回bool, 一个返回T + //获取参数的维数 + auto dim = std::visit([&](auto const &v){ + using T = std::decay_t; + //判断参数是三维数组还是,单浮点数 + if constexpr(std::is_same_v(T, zeno::vec3f)) { + parvals.push_back(v[0]); + parvals.push_back(v[1]); + parvals.push_back(v[2]); + parnames.emplace_back(key, 0); + paranames.emplace_back(key, 1); + paranames.emplace_back(key, 2); + return 3; + } else if constexpr(std::is_constructible_v) { + parvals.push_back(float(v)); + paranames(emplace_back(key, 0)); + return 1; + } else return 0; + }, par); + dbg_print("define param : %s dim %d\n, key.c_str(), dim"); + opts.define_param(key, dim); + } + + //开始编译 + auto prog = compiler.compile(code, opts); + auto exec = assembler.assemble(prog->assembly); + + //计算输出结果 + auto result = std::make_shared(); + for (auto const &[name, dim] : prog->newsyms) { + dbg_printf("output numeric value %s with dim %d\n", name.c_str(), dim); + assert(name[0] == '@'); + auto key = name.substr(1); + zeno::NumericValue value; + if (dim == 4) { + value = zeno::vec4f{}; + } else if (dim == 3) { + value = zeno::vec3f{}; + } else if (dim == 2) { + value = zeno::vec2f{}; + } else if (dim == 1) { + value = float{}; + } else { + dbg_printf("ERROR : bad output dimension for numeric : %d\n", dim); + abort(); + } + } + result->set(value); + //result->lut[key] = std::make_shared(value); + } + + for (int i = 0; i < prog->params.size(); i++) { + auto [name, dimid] = prog->params[i]; + dbg_printf("parameter %d: %s.%d\n", i , name.c_str(), dimid); + assert(name[0] == '$'); + auto it = std::find(parnames.begin(), paranames.end(), std::pair{name , dimid}); + auto value = parvals.at(it - paranames.begin()); + dbg_printf("(value %f)\n", value); + exec->parameter(prog->param_id(name, dimid)) = value; + } + + std::vector chs(prog->symbols.size());//初始化chs的大小 + for (int i = 0; i < chs.size(); i++) { + auto [name, dimid] = prog->symbols[i]; + dbg_printf("output %d : %s.%d\n", i, name.c_str(), dimid); + assert(name[0] == '@'); + } + + numeric_eval(exec, chs); + + for (int i = 0; i < chs.size(); i++) { + auto [name, dimid] = prog->symbols[i]; + float value = chs[i]; + dbg_printf("output %d : %s. %d = %f\n", i , name.c_str(), dimid, value); + auto key = name.substr(1); + std::visit([dimid = dimid, value] (auto &res) { + dimid[(float*)(void*)&res] = value; + }, result->get()) + } + set_output("result", std::move(result)); + }; + + ZENDEFNODE(NumericEval, { + /* inputs*/ + { + {"DictObject:NumericObject", "params"}, {"string", "zfxCode"} + }, + /*OutPut*/ + { + {"NumericObject", "result"} + }, + {}, + {"zenofx"}, + }); +} diff --git a/projects/ZenoFX/pw.cpp b/projects/ZenoFX/pw.cpp index 2fd1e8c981..006194f0b9 100644 --- a/projects/ZenoFX/pw.cpp +++ b/projects/ZenoFX/pw.cpp @@ -59,7 +59,6 @@ struct ParticlesWrangle : zeno::INode { virtual void apply() override { auto prim = get_input("prim"); auto code = get_input("zfxCode")->get(); - zfx::Options opts(zfx::Options::for_x64); opts.detect_new_symbols = true; prim->foreach_attr([&] (auto const &key, auto const &attr) {