diff --git a/include/triton/lang/wgtcc/t_code_gen.h b/include/triton/lang/wgtcc/t_code_gen.h new file mode 100644 index 000000000..00abfabba --- /dev/null +++ b/include/triton/lang/wgtcc/t_code_gen.h @@ -0,0 +1,122 @@ +#ifndef _WGTCC_CODE_GEN_H_ +#define _WGTCC_CODE_GEN_H_ + +#include "ast.h" +#include "visitor.h" + +namespace triton{ +namespace ir{ + +class value; +class module; + +} +} + +using namespace triton; + +class Parser; +struct Addr; +template<> class Evaluator; +struct StaticInitializer; + +using TypeList = std::vector; +using LocationList = std::vector; +using StaticInitList = std::vector; + + +class Generator: public Visitor { + friend class Evaluator; +public: + Generator(Parser* parser, ir::module& mod) : parser_(parser), mod_(mod){} + + virtual void Visit(ASTNode* node) { node->Accept(this); } + void VisitExpr(Expr* expr) { expr->Accept(this); } + void VisitStmt(Stmt* stmt) { stmt->Accept(this); } + + // Expression + virtual void VisitBinaryOp(BinaryOp* binaryOp); + virtual void VisitUnaryOp(UnaryOp* unaryOp); + virtual void VisitConditionalOp(ConditionalOp* condOp); + virtual void VisitFuncCall(FuncCall* funcCall); + virtual void VisitObject(Object* obj); + virtual void VisitEnumerator(Enumerator* enumer); + virtual void VisitIdentifier(Identifier* ident); + virtual void VisitConstant(Constant* cons); + virtual void VisitTempVar(TempVar* tempVar); + + // Statement + virtual void VisitDeclaration(Declaration* init); + virtual void VisitEmptyStmt(EmptyStmt* emptyStmt); + virtual void VisitIfStmt(IfStmt* ifStmt); + virtual void VisitJumpStmt(JumpStmt* jumpStmt); + virtual void VisitReturnStmt(ReturnStmt* returnStmt); + virtual void VisitLabelStmt(LabelStmt* labelStmt); + virtual void VisitCompoundStmt(CompoundStmt* compoundStmt); + + virtual void VisitFuncDef(FuncDef* funcDef); + virtual void VisitTranslationUnit(TranslationUnit* unit); + + void Gen(); + +protected: + // Binary + void GenCommaOp(BinaryOp* comma); + void GenMemberRefOp(BinaryOp* binaryOp); + void GenAndOp(BinaryOp* binaryOp); + void GenOrOp(BinaryOp* binaryOp); + void GenAddOp(BinaryOp* binaryOp); + void GenSubOp(BinaryOp* binaryOp); + void GenAssignOp(BinaryOp* assign); + void GenCastOp(UnaryOp* cast); + void GenDerefOp(UnaryOp* deref); + void GenMinusOp(UnaryOp* minus); + void GenPointerArithm(BinaryOp* binary); + void GenDivOp(bool flt, bool sign, int width, int op); + void GenMulOp(int width, bool flt, bool sign); + void GenCompOp(int width, bool flt, const char* set); + void GenCompZero(Type* type); + + // Unary + void GenIncDec(Expr* operand, bool postfix, const std::string& inst); + StaticInitializer GetStaticInit(InitList::iterator& iter, + InitList::iterator end, int offset); + void GenStaticDecl(Declaration* decl); + void GenSaveArea(); + void GenBuiltin(FuncCall* funcCall); + + void AllocObjects(Scope* scope, + const FuncDef::ParamList& params=FuncDef::ParamList()); + +protected: + Parser* parser_; + ir::module& mod_; +}; + + +class LValGenerator: public Generator { +public: + LValGenerator(Parser* parser, ir::module& mod): Generator(parser, mod) {} + + // Expression + virtual void VisitBinaryOp(BinaryOp* binaryOp); + virtual void VisitUnaryOp(UnaryOp* unaryOp); + virtual void VisitObject(Object* obj); + virtual void VisitIdentifier(Identifier* ident); + + virtual void VisitConditionalOp(ConditionalOp* condOp) { assert(false); } + virtual void VisitFuncCall(FuncCall* funcCall) { assert(false); } + virtual void VisitEnumerator(Enumerator* enumer) { assert(false); } + virtual void VisitConstant(Constant* cons) { assert(false); } + virtual void VisitTempVar(TempVar* tempVar); + + ir::value* GenExpr(Expr* expr) { + expr->Accept(this); + return addr_; + } + +private: + ir::value* addr_; +}; + +#endif diff --git a/lib/lang/wgtcc/ast.cc b/lib/lang/wgtcc/ast.cc index 5646fbb4c..d194d4c0f 100644 --- a/lib/lang/wgtcc/ast.cc +++ b/lib/lang/wgtcc/ast.cc @@ -1,6 +1,4 @@ #include "triton/lang/wgtcc/ast.h" - -#include "triton/lang/wgtcc/code_gen.h" #include "triton/lang/wgtcc/error.h" #include "triton/lang/wgtcc/evaluator.h" #include "triton/lang/wgtcc/mem_pool.h" diff --git a/lib/lang/wgtcc/evaluator.cc b/lib/lang/wgtcc/evaluator.cc index 956fe21a6..02cb224f9 100644 --- a/lib/lang/wgtcc/evaluator.cc +++ b/lib/lang/wgtcc/evaluator.cc @@ -1,7 +1,5 @@ #include "triton/lang/wgtcc/evaluator.h" - #include "triton/lang/wgtcc/ast.h" -#include "triton/lang/wgtcc/code_gen.h" #include "triton/lang/wgtcc/token.h" @@ -201,9 +199,7 @@ void Evaluator::VisitConstant(Constant* cons) { if (cons->Type()->IsInteger()) { addr_ = {"", static_cast(cons->IVal())}; } else if (cons->Type()->ToArray()) { - Generator().ConsLabel(cons); // Add the literal to rodatas_. - addr_.label_ = Generator::rodatas_.back().label_; - addr_.offset_ = 0; + assert(false); } else { assert(false); } diff --git a/lib/lang/wgtcc/t_code_gen.cc b/lib/lang/wgtcc/t_code_gen.cc new file mode 100644 index 000000000..4d78944e8 --- /dev/null +++ b/lib/lang/wgtcc/t_code_gen.cc @@ -0,0 +1,8 @@ +#include "triton/lang/wgtcc/t_code_gen.h" +#include "triton/lang/wgtcc/evaluator.h" +#include "triton/lang/wgtcc/parser.h" +#include "triton/lang/wgtcc/token.h" + +void Generator::Gen() { + VisitTranslationUnit(parser_->Unit()); +}