From ebf120c39cccc4de163a3d16df2f07f79036774b Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Fri, 10 Apr 2009 08:38:39 +0100 Subject: [PATCH] [SimplifyVisitor] should be fine --- src/ozulis/ast/scope.hh | 70 +++++++++++++++++++------------------- src/ozulis/visitors/simplify.cc | 75 +++++++++++++++++++++++++++++------------ src/ozulis/visitors/simplify.hh | 11 ++++-- 3 files changed, 97 insertions(+), 59 deletions(-) rewrite src/ozulis/ast/scope.hh (66%) diff --git a/src/ozulis/ast/scope.hh b/src/ozulis/ast/scope.hh dissimilarity index 66% index 32aa832..97c6fe8 100644 --- a/src/ozulis/ast/scope.hh +++ b/src/ozulis/ast/scope.hh @@ -1,35 +1,35 @@ -#ifndef SCOPE_HH -# define SCOPE_HH - -# include - -# include "ast.hh" - -namespace ozulis -{ -namespace ast -{ - class Scope - { - public: - Scope(); - - bool addSymbol(Symbol * symbol); - Symbol * findSymbol(const std::string & name) const; - std::string nextId(); - std::string nextStringId(); - - std::string prefix; - Scope * parent; - - protected: - typedef std::map symbols_t; - symbols_t symbols_; - int32_t nextId_; - }; -} -} - -extern template class std::map; - -#endif /* !SCOPE_HH */ +#ifndef SCOPE_HH +# define SCOPE_HH + +# include + +# include "ast.hh" + +namespace ozulis +{ + namespace ast + { + class Scope + { + public: + Scope(); + + bool addSymbol(Symbol * symbol); + Symbol * findSymbol(const std::string & name) const; + std::string nextId(); + std::string nextStringId(); + + std::string prefix; + Scope * parent; + + protected: + typedef std::map symbols_t; + symbols_t symbols_; + int32_t nextId_; + }; + } +} + +extern template class std::map; + +#endif /* !SCOPE_HH */ diff --git a/src/ozulis/visitors/simplify.cc b/src/ozulis/visitors/simplify.cc index aac7b74..e7acd14 100644 --- a/src/ozulis/visitors/simplify.cc +++ b/src/ozulis/visitors/simplify.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -28,8 +29,8 @@ namespace ozulis : Visitor(), simplifications(0), replacement(0), - nextId_(0), - scope(0) + scope(0), + nextId_(0) { } @@ -77,8 +78,10 @@ namespace ozulis sexp->symbol = new ast::Symbol; sexp->symbol->name = nextId(); sexp->symbol->type = node.type; - sexp->symbol->address = new ast::RegisterAddress; sexp->type = node.type; + ast::RegisterAddress * addr = new ast::RegisterAddress; + addr->address = scope->prefix + sexp->symbol->name; + sexp->symbol->address = addr; ast::AssignExp * assign = new ast::AssignExp(); assign->value = &node; @@ -106,11 +109,6 @@ namespace ozulis ctx.simplifications = backup; } - static void visitExp(ast::Node & /*node_*/, Simplify & /*ctx*/) - { - assert_msg(false, "unimplemented visit method"); - } - static void visitVoidExp(ast::Node & node_, Simplify & ctx) { ast::VoidExp & node = reinterpret_cast (node_); @@ -211,24 +209,26 @@ namespace ozulis assert_msg(false, "should never get here."); \ } + BAD_NODE(Exp) BAD_NODE(StringExp) BAD_NODE(IdExp) + /** @internal load a symbol if it's in memory */ static void visitSymbolExp(ast::Node & node_, Simplify & ctx) { ast::SymbolExp & node = reinterpret_cast (node_); if (node.symbol->address->nodeType == ast::MemoryAddress::nodeTypeId()) { - ast::SymbolExp * id = new ast::SymbolExp; - id->symbol = new ast::Symbol; - id->symbol->name = node.symbol->name + "_" + ctx.nextId();; - id->symbol->type = unreferencedType(node.type); - id->symbol->address = new ast::RegisterAddress; - id->type = id->symbol->type; - ctx.replacement = id; + ast::SymbolExp * sexp = new ast::SymbolExp; + sexp->symbol = new ast::Symbol; + sexp->symbol->name = node.symbol->name + "_" + ctx.nextId();; + sexp->symbol->type = unreferencedType(node.type); + sexp->symbol->address = new ast::RegisterAddress; + sexp->type = sexp->symbol->type; + ctx.replacement = sexp; ast::LoadVar * lv = new ast::LoadVar; - lv->to = id->symbol->name; + lv->to = sexp->symbol->name; lv->from = &node; ctx.simplifications.push_back(lv); } @@ -265,14 +265,15 @@ namespace ozulis /** @internal just do (node.exp + node.index)* */ static void visitDereferenceByIndexExp(ast::Node & node_, Simplify & ctx) { - ast::DereferenceByIndexExp & node = reinterpret_cast (node_); - ast::AddExp * add = new ast::AddExp; - add->left = node.exp; - add->right = node.index; + ast::DereferenceByIndexExp & node = + reinterpret_cast (node_); + ast::PointerArithExp * add = new ast::PointerArithExp; + add->pointer = node.exp; + add->offset = node.index; ast::DereferenceExp * deref = new ast::DereferenceExp; - deref->exp = add; - ast::Exp * tmp = deref; + deref->exp = add; + ast::Exp * tmp = deref; TypeChecker v; v.scope = ctx.scope; @@ -297,6 +298,7 @@ namespace ozulis ctx.simplify(node.exp); assert(node.exp); + // The cast exp is dummy, remove it if (!castToBestType(node.type, node.exp->type)) return; @@ -322,6 +324,17 @@ namespace ozulis ctx.simplifications.push_back(&node); } + /** @internal + * @code + * if (cond) + * trueLabel: + * { ... } + * goto endLabel; + * falseLabel: + * { ... } + * endLabel: + * @code + */ static void visitIf(ast::Node & node_, Simplify & ctx) { ast::If & node = reinterpret_cast (node_); @@ -345,6 +358,16 @@ namespace ozulis ctx.simplifications.push_back(node.endLabel); } + /** @internal + * @code + * beginLabel: + * while (cond) + * trueLabel: + * { ... } + * goto beginLabel; + * falseLabel: + * @code + */ static void visitWhile(ast::Node & node_, Simplify & ctx) { ast::While & node = reinterpret_cast (node_); @@ -371,6 +394,14 @@ namespace ozulis ctx.simplifications.push_back(node.branch->falseLabel); } + /** @internal + * @code + * trueLabel: + * { ... } + * while (cond) + * falseLabel: + * @code + */ static void visitDoWhile(ast::Node & node_, Simplify & ctx) { ast::DoWhile & node = reinterpret_cast (node_); diff --git a/src/ozulis/visitors/simplify.hh b/src/ozulis/visitors/simplify.hh index 07b8af0..929d87e 100644 --- a/src/ozulis/visitors/simplify.hh +++ b/src/ozulis/visitors/simplify.hh @@ -19,10 +19,15 @@ namespace ozulis Simplify(); virtual ~Simplify(); + /** @brief initialize the table of the visitor */ static void initBase(); + /** @brief when you simplify lvalue, you want the pointer and not the + * value to be able to do a StoreVar latter */ void simplifyLValue(ast::Exp *& exp); + /** @brief replaces node by it's simplified expression */ void simplify(ast::Exp *& node); + /** @brief create a temporary in a register which is the result of node */ void makeTmpResult(ast::Exp & node); std::string currentId() const; @@ -32,10 +37,12 @@ namespace ozulis std::vector simplifications; /// @brief the child which replace the previously visited node ast::Exp * replacement; - /// @brief the next id for register count - int nextId_; /// @brief the current scope, to retype check some generated expressions ast::Scope * scope; + + private: + /// @brief the next id for register count + int nextId_; }; } } -- 2.11.4.GIT