X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/a86e34d01f829a8b4f58f6066d78148c0901c1dd..15024d7e7c224d837592094932a2da18d17ee015:/Replace.cpp diff --git a/Replace.cpp b/Replace.cpp index 1a03a5c..c06c2a5 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); } @@ -340,8 +338,9 @@ void CYFunction::Replace_(CYContext &context, bool outer) { if (outer) Inject(context); - parent_ = context.scope_; - context.scope_ = this; + CYScope scope; + scope.parent_ = context.scope_; + context.scope_ = &scope; if (!outer && name_ != NULL) Inject(context); @@ -349,8 +348,8 @@ void CYFunction::Replace_(CYContext &context, bool outer) { parameters_->Replace(context); code_.Replace(context); - context.scope_ = parent_; - Scope(context, code_.statements_); + context.scope_ = scope.parent_; + scope.Scope(context, code_.statements_); } CYExpression *CYFunctionExpression::Replace(CYContext &context) { @@ -370,8 +369,9 @@ CYStatement *CYFunctionStatement::Replace(CYContext &context) { } CYIdentifier *CYIdentifier::Replace(CYContext &context) { - if (replace_ == NULL) - replace_ = context.scope_->Lookup(context, this); + if (replace_ != NULL && replace_ != this) + return replace_->Replace(context); + replace_ = context.scope_->Lookup(context, this); return replace_; } @@ -458,23 +458,48 @@ CYExpression *CYPrefix::Replace(CYContext &context) { return this; } -void CYProgram::Replace(CYContext &context) { - parent_ = context.scope_; - CYProgram *program(context.program_); +// XXX: this is evil evil black magic. don't ask, don't tell... don't believe! +#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ" +//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_" - context.scope_ = this; - context.program_ = this; +namespace { + struct IdentifierUsageLess : + std::binary_function + { + _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const { + if (lhs->usage_ != rhs->usage_) + return lhs->usage_ > rhs->usage_; + return lhs < rhs; + } + }; - statements_ = statements_->ReplaceAll(context); + typedef std::set IdentifierUsages; +} - context.scope_ = parent_; - context.program_ = program; - Scope(context, statements_); +void CYProgram::Replace(CYContext &context) { + CYScope scope; + scope.parent_ = context.scope_; + context.scope_ = &scope; + statements_ = statements_->ReplaceAll(context); + context.scope_ = scope.parent_; + scope.Scope(context, statements_); size_t offset(0); + CYCStringSet external; + for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i) + external.insert((*i)->Word()); + + IdentifierUsages usages; + + if (offset < context.rename_.size()) + for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_) + usages.insert(i); + // 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) { + for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) { + //std::cout << *i << ":" << (*i)->offset_ << std::endl; + const char *name; if (context.options_.verbose_) @@ -487,12 +512,12 @@ void CYProgram::Replace(CYContext &context) { unsigned position(7), local(offset + 1); do { - unsigned index(local % 53); - local /= 53; - id[--position] = index == 0 ? '0' : index < 27 ? index - 1 + 'a' : index - 27 + 'A'; + unsigned index(local % (sizeof(MappingSet) - 1)); + local /= sizeof(MappingSet) - 1; + id[--position] = MappingSet[index]; } while (local != 0); - if (external_.find(id + position) != external_.end()) { + if (external.find(id + position) != external.end()) { ++offset; goto id; } @@ -501,7 +526,7 @@ void CYProgram::Replace(CYContext &context) { // XXX: at some point, this could become a keyword } - for (CYIdentifier *identifier(*i); identifier != NULL; identifier = identifier->next_) + for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_) identifier->Set(name); } } @@ -525,42 +550,82 @@ 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; + (*insert.first)->usage_ += identifier->usage_ + 1; } } -void CYScope::Scope(CYContext &context, CYStatement *&statements) { - CYIdentifierAddressVector external; +namespace { + struct IdentifierOffset { + size_t offset_; + CYIdentifierFlags flags_; + size_t usage_; + CYIdentifier *identifier_; - for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) - if (internal_.find(*i) == internal_.end()) - external.push_back(*i); + IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) : + offset_(identifier->offset_), + flags_(flags), + usage_(identifier->usage_), + identifier_(identifier) + { + } + }; + + struct IdentifierOffsetLess : + std::binary_function + { + _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const { + if (lhs.offset_ != rhs.offset_) + return lhs.offset_ < rhs.offset_; + if (lhs.flags_ != rhs.flags_) + return lhs.flags_ < rhs.flags_; + /*if (lhs.usage_ != rhs.usage_) + return lhs.usage_ < rhs.usage_;*/ + return lhs.identifier_ < rhs.identifier_; + } + }; + + typedef std::set IdentifierOffsets; +} + +void CYScope::Scope(CYContext &context, CYStatement *&statements) { + if (parent_ == NULL) + return; CYDeclarations *last(NULL), *curr(NULL); - CYProgram *program(context.program_); - - // 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 (i->second == CYIdentifierVariable) { - CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->first))); + IdentifierOffsets offsets; + + for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) + if (i->second != CYIdentifierMagic) + offsets.insert(IdentifierOffset(i->first, i->second)); + + size_t offset(0); + + for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) { + if (i->flags_ == CYIdentifierVariable) { + CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_))); if (last == NULL) last = next; if (curr != NULL) curr->SetNext(next); curr = next; } + + if (offset < i->offset_) + offset = i->offset_; + if (context.rename_.size() <= offset) + context.rename_.resize(offset + 1); + + CYIdentifierUsage &rename(context.rename_[offset++]); + i->identifier_->SetNext(rename.identifier_); + rename.identifier_ = i->identifier_; + rename.usage_ += i->identifier_->usage_ + 1; } if (last != NULL) { @@ -569,13 +634,13 @@ void CYScope::Scope(CYContext &context, CYStatement *&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()) { + //std::cout << *i << '=' << offset << std::endl; + if ((*i)->offset_ < offset) + (*i)->offset_ = offset; + parent_->Merge(context, *i); + } } CYStatement *CYStatement::Collapse(CYContext &context) {