From: Jay Freeman (saurik) Date: Tue, 17 Nov 2009 05:20:24 +0000 (+0000) Subject: Optimized the variable renamer to rename more variables to 'a'. X-Git-Tag: v0.9.432~154 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/6a9812501258df26b7c487e50744b91abe8ebe39 Optimized the variable renamer to rename more variables to 'a'. --- diff --git a/Console.cpp b/Console.cpp index 0ed0bfe..65eeb4f 100644 --- a/Console.cpp +++ b/Console.cpp @@ -38,7 +38,6 @@ /* }}} */ #include "cycript.hpp" -#include "Context.hpp" #ifdef CY_EXECUTE #include "JavaScript.hpp" diff --git a/Context.hpp b/Context.hpp deleted file mode 100644 index 994f3b6..0000000 --- a/Context.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) -*/ - -/* Modified BSD License {{{ */ -/* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the - * above copyright notice, this list of conditions - * and the following disclaimer. - * 2. Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions - * and the following disclaimer in the documentation - * and/or other materials provided with the - * distribution. - * 3. The name of the author may not be used to endorse - * or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -/* }}} */ - -#ifndef CYCRIPT_CONTEXT_HPP -#define CYCRIPT_CONTEXT_HPP - -#include "Options.hpp" - -class CYProgram; -class CYScope; - -struct CYContext { - apr_pool_t *pool_; - CYOptions &options_; - CYScope *scope_; - CYProgram *program_; - - CYContext(apr_pool_t *pool, CYOptions &options) : - pool_(pool), - options_(options), - scope_(NULL), - program_(NULL) - { - } - - template - void Replace(Type_ *&value) { - for (;;) if (value == NULL) - break; - else { - Type_ *replace(value->Replace(*this)); - if (replace != value) - value = replace; - else break; - } - } -}; - -#endif/*CYCRIPT_CONTEXT_HPP*/ diff --git a/Handler.mm b/Handler.mm index 9c1a8df..4e4ccfe 100644 --- a/Handler.mm +++ b/Handler.mm @@ -42,7 +42,6 @@ #include "Pooling.hpp" #include "Parser.hpp" -#include "Context.hpp" #include "Cycript.tab.hh" diff --git a/Library.cpp b/Library.cpp index 610e11b..e6ab2a9 100644 --- a/Library.cpp +++ b/Library.cpp @@ -43,7 +43,6 @@ #include "cycript.hpp" #include "Pooling.hpp" -#include "Context.hpp" #include diff --git a/Parser.hpp b/Parser.hpp index 008be05..cdeff50 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -243,10 +243,12 @@ struct CYIdentifier : CYWord { CYIdentifier *replace_; + size_t offset_; CYIdentifier(const char *word) : CYWord(word), - replace_(NULL) + replace_(NULL), + offset_(0) { } @@ -318,17 +320,15 @@ struct CYScope { CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - size_t offset_; CYScope() : - parent_(NULL), - offset_(0) + parent_(NULL) { } void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags); virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier); - void Merge(CYContext &context, CYIdentifierAddressVector &external); + void Merge(CYContext &context, CYIdentifier *identifier); void Scope(CYContext &context, CYStatement *&statements); }; @@ -338,7 +338,6 @@ struct CYProgram : { CYStatement *statements_; CYIdentifierAddressVector rename_; - CYCStringSet external_; CYProgram(CYStatement *statements) : statements_(statements) @@ -349,6 +348,35 @@ struct CYProgram : virtual void Output(CYOutput &out) const; }; +struct CYContext : + CYScope +{ + apr_pool_t *pool_; + CYOptions &options_; + CYScope *scope_; + CYProgram *program_; + + CYContext(apr_pool_t *pool, CYOptions &options) : + pool_(pool), + options_(options), + scope_(this), + program_(NULL) + { + } + + template + void Replace(Type_ *&value) { + for (;;) if (value == NULL) + break; + else { + Type_ *replace(value->Replace(*this)); + if (replace != value) + value = replace; + else break; + } + } +}; + struct CYBlock : CYStatement, CYThing diff --git a/Replace.cpp b/Replace.cpp index 1a03a5c..45be919 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -38,12 +38,10 @@ /* }}} */ #include "Parser.hpp" -#include "Context.hpp" +#include "Replace.hpp" #include -#include "Replace.hpp" - CYExpression *CYAdd::Replace(CYContext &context) { CYInfix::Replace(context); @@ -332,7 +330,7 @@ CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement * } void CYFunction::Inject(CYContext &context) { - name_ = name_->Replace(context); + context.Replace(name_); context.scope_->Declare(context, name_, CYIdentifierOther); } @@ -473,6 +471,10 @@ void CYProgram::Replace(CYContext &context) { size_t offset(0); + CYCStringSet external; + for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) + external.insert((*i)->Word()); + // XXX: totalling the probable occurrences and sorting by them would improve the result for (CYIdentifierAddressVector::const_iterator i(rename_.begin()); i != rename_.end(); ++i, ++offset) { const char *name; @@ -492,7 +494,7 @@ void CYProgram::Replace(CYContext &context) { id[--position] = index == 0 ? '0' : index < 27 ? index - 1 + 'a' : index - 27 + 'A'; } while (local != 0); - if (external_.find(id + position) != external_.end()) { + if (external.find(id + position) != external.end()) { ++offset; goto id; } @@ -525,34 +527,26 @@ CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) { return *insert.first; } -void CYScope::Merge(CYContext &context, CYIdentifierAddressVector &external) { - for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i) { - std::pair insert(identifiers_.insert(*i)); - if (!insert.second) - (*i)->replace_ = *insert.first; +void CYScope::Merge(CYContext &context, CYIdentifier *identifier) { + std::pair insert(identifiers_.insert(identifier)); + if (!insert.second) { + if ((*insert.first)->offset_ < identifier->offset_) + (*insert.first)->offset_ = identifier->offset_; + identifier->replace_ = *insert.first; } } void CYScope::Scope(CYContext &context, CYStatement *&statements) { - CYIdentifierAddressVector external; - - for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) - if (internal_.find(*i) == internal_.end()) - external.push_back(*i); - CYDeclarations *last(NULL), *curr(NULL); CYProgram *program(context.program_); + typedef std::multimap IdentifierOffsetMap; + IdentifierOffsetMap offsetted; + // XXX: we don't want to do this in order, we want to sort it by probable occurrence for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) { - if (program != NULL && i->second != CYIdentifierMagic) { - if (program->rename_.size() <= offset_) - program->rename_.resize(offset_ + 1); - CYIdentifier *&identifier(program->rename_[offset_++]); - i->first->SetNext(identifier); - identifier = i->first; - } - + if (program != NULL && i->second != CYIdentifierMagic) + offsetted.insert(IdentifierOffsetMap::value_type(i->first->offset_, i->first)); if (i->second == CYIdentifierVariable) { CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->first))); if (last == NULL) @@ -563,19 +557,30 @@ void CYScope::Scope(CYContext &context, CYStatement *&statements) { } } + size_t offset(0); + + for (IdentifierOffsetMap::const_iterator i(offsetted.begin()); i != offsetted.end(); ++i) { + if (offset < i->first) + offset = i->first; + if (program->rename_.size() <= offset) + program->rename_.resize(offset + 1); + CYIdentifier *&identifier(program->rename_[offset++]); + i->second->SetNext(identifier); + identifier = i->second; + } + if (last != NULL) { CYVar *var($ CYVar(last)); var->SetNext(statements); statements = var; } - if (parent_ != NULL) { - if (parent_->offset_ < offset_) - parent_->offset_ = offset_; - parent_->Merge(context, external); - } else if (program != NULL) - for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i) - program->external_.insert((*i)->Word()); + for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) + if (internal_.find(*i) == internal_.end()) { + if ((*i)->offset_ < offset) + (*i)->offset_ = offset; + parent_->Merge(context, *i); + } } CYStatement *CYStatement::Collapse(CYContext &context) { diff --git a/makefile b/makefile index 372ea98..551bd73 100644 --- a/makefile +++ b/makefile @@ -22,7 +22,7 @@ ifneq ($(dpkg_architecture),) arch := $(shell $(dpkg_architecture) -qDEB_HOST_ARCH 2>/dev/null) endif -header := Cycript.tab.hh Parser.hpp Pooling.hpp cycript.hpp Internal.hpp Error.hpp String.hpp Exception.hpp Standard.hpp Context.hpp +header := Cycript.tab.hh Parser.hpp Pooling.hpp cycript.hpp Internal.hpp Error.hpp String.hpp Exception.hpp Standard.hpp code := code += Replace.o Output.o