1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "Replace.hpp"
45 CYExpression
*CYAdd::Replace(CYContext
&context
) {
46 CYInfix::Replace(context
);
48 CYExpression
*lhp(lhs_
->Primitive(context
));
49 CYExpression
*rhp(rhs_
->Primitive(context
));
51 CYString
*lhs(dynamic_cast<CYString
*>(lhp
));
52 CYString
*rhs(dynamic_cast<CYString
*>(rhp
));
54 if (lhs
!= NULL
|| rhs
!= NULL
) {
56 lhs
= lhp
->String(context
);
59 } else if (rhs
== NULL
) {
60 rhs
= rhp
->String(context
);
65 return lhs
->Concat(context
, rhs
);
68 if (CYNumber
*lhn
= lhp
->Number(context
))
69 if (CYNumber
*rhn
= rhp
->Number(context
))
70 return $
D(lhn
->Value() + rhn
->Value());
75 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
76 CYPrefix::Replace(context
);
77 return $
C0($
M(rhs_
, $
S("$cya")));
80 void CYArgument::Replace(CYContext
&context
) { $
T()
81 context
.Replace(value_
);
82 next_
->Replace(context
);
85 CYExpression
*CYArray::Replace(CYContext
&context
) {
86 elements_
->Replace(context
);
90 CYExpression
*CYArrayComprehension::Replace(CYContext
&context
) {
91 CYVariable
*cyv($
V("$cyv"));
93 return $
C0($
F(NULL
, $
P1("$cyv", comprehensions_
->Parameters(context
)), $$
->*
94 $
E($
CYAssign(cyv
, $
CYArray()))->*
95 comprehensions_
->Replace(context
, $
E($
C1($
M(cyv
, $
S("push")), expression_
)))->*
100 CYExpression
*CYAssignment::Replace(CYContext
&context
) {
101 context
.Replace(lhs_
);
102 context
.Replace(rhs_
);
106 CYStatement
*CYBlock::Replace(CYContext
&context
) {
107 statements_
= statements_
->ReplaceAll(context
);
108 if (statements_
== NULL
)
113 CYStatement
*CYBreak::Replace(CYContext
&context
) {
117 CYExpression
*CYCall::AddArgument(CYContext
&context
, CYExpression
*value
) {
118 CYArgument
**argument(&arguments_
);
119 while (*argument
!= NULL
)
120 argument
= &(*argument
)->next_
;
121 *argument
= $
CYArgument(value
);
125 CYExpression
*CYCall::Replace(CYContext
&context
) {
126 context
.Replace(function_
);
127 arguments_
->Replace(context
);
134 void Catch::Replace(CYContext
&context
) { $
T()
135 code_
.Replace(context
);
140 void CYClause::Replace(CYContext
&context
) { $
T()
141 context
.Replace(case_
);
142 statements_
= statements_
->ReplaceAll(context
);
143 next_
->Replace(context
);
146 CYStatement
*CYComment::Replace(CYContext
&context
) {
150 CYExpression
*CYCompound::Replace(CYContext
&context
) {
151 expressions_
= expressions_
->ReplaceAll(context
);
152 return expressions_
== NULL
? NULL
: this;
155 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
156 CYFunctionParameter
*next(next_
->Parameters(context
));
157 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
158 parameter
->SetNext(next
);
164 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
165 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
168 CYExpression
*CYCondition::Replace(CYContext
&context
) {
169 context
.Replace(test_
);
170 context
.Replace(true_
);
171 context
.Replace(false_
);
175 CYStatement
*CYContinue::Replace(CYContext
&context
) {
179 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
180 CYExpression
*variable(Replace(context
));
181 return initialiser_
== NULL
? NULL
: $
CYAssign(variable
, initialiser_
);
184 CYExpression
*CYDeclaration::ForEachIn(CYContext
&context
) {
185 return $
CYVariable(identifier_
);
188 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
189 context
.Replace(identifier_
);
190 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
191 return $
CYVariable(identifier_
);
194 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
195 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
?: $U
, next_
->Property(context
));
198 CYCompound
*CYDeclarations::Replace(CYContext
&context
) {
199 CYCompound
*compound
;
200 if (next_
== NULL
) compound
:
201 compound
= $
CYCompound();
203 compound
= next_
->Replace(context
);
204 if (compound
== NULL
)
208 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
209 compound
->AddPrev(assignment
);
213 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
218 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
219 context
.Replace(test_
);
220 context
.Replace(code_
);
224 void CYElement::Replace(CYContext
&context
) { $
T()
225 context
.Replace(value_
);
226 next_
->Replace(context
);
229 CYStatement
*CYEmpty::Collapse(CYContext
&context
) {
233 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
237 CYStatement
*CYExpress::Collapse(CYContext
&context
) {
238 if (CYExpress
*express
= dynamic_cast<CYExpress
*>(next_
)) {
239 CYCompound
*next(dynamic_cast<CYCompound
*>(express
->expression_
));
241 next
= $
CYCompound(express
->expression_
);
242 next
->AddPrev(expression_
);
244 SetNext(express
->next_
);
250 CYStatement
*CYExpress::Replace(CYContext
&context
) {
251 context
.Replace(expression_
);
252 if (expression_
== NULL
)
257 CYExpression
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
258 return $
C1(this, value
);
261 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
265 CYExpression
*CYExpression::ForEachIn(CYContext
&context
) {
269 CYExpression
*CYExpression::ReplaceAll(CYContext
&context
) { $
T(NULL
)
270 CYExpression
*replace(this);
271 context
.Replace(replace
);
273 if (CYExpression
*next
= next_
->ReplaceAll(context
))
274 replace
->SetNext(next
);
276 replace
->SetNext(next_
);
281 CYNumber
*CYFalse::Number(CYContext
&context
) {
285 CYString
*CYFalse::String(CYContext
&context
) {
289 void CYFinally::Replace(CYContext
&context
) { $
T()
290 code_
.Replace(context
);
293 CYStatement
*CYFor::Replace(CYContext
&context
) {
294 context
.Replace(initialiser_
);
295 context
.Replace(test_
);
296 context
.Replace(increment_
);
297 context
.Replace(code_
);
301 CYStatement
*CYForIn::Replace(CYContext
&context
) {
302 // XXX: this actually might need a prefix statement
303 context
.Replace(initialiser_
);
304 context
.Replace(set_
);
305 context
.Replace(code_
);
309 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
310 return $
CYFunctionParameter(name_
);
313 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
314 return $
CYForIn($
CYVariable(name_
), set_
, CYComprehension::Replace(context
, statement
));
317 CYStatement
*CYForEachIn::Replace(CYContext
&context
) {
318 CYVariable
*cys($
V("$cys")), *cyt($
V("$cyt"));
320 return $
CYLet($
L2($
L($
I("$cys"), set_
), $
L($
I("$cyt"))), $$
->*
321 $
CYForIn(cyt
, cys
, $
CYBlock($$
->*
322 $
E($
CYAssign(initialiser_
->ForEachIn(context
), $
M(cys
, cyt
)))->*
328 CYFunctionParameter
*CYForEachInComprehension::Parameter(CYContext
&context
) const {
329 return $
CYFunctionParameter(name_
);
332 CYStatement
*CYForEachInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
333 CYVariable
*cys($
V("$cys")), *name($
CYVariable(name_
));
335 return $
E($
C0($
F(NULL
, $
P1("$cys"), $$
->*
336 $
E($
CYAssign(cys
, set_
))->*
337 $
CYForIn(name
, cys
, $
CYBlock($$
->*
338 $
E($
CYAssign(name
, $
M(cys
, name
)))->*
339 CYComprehension::Replace(context
, statement
)
344 void CYFunction::Inject(CYContext
&context
) {
345 context
.Replace(name_
);
346 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
349 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
354 scope
.parent_
= context
.scope_
;
355 context
.scope_
= &scope
;
357 if (!outer
&& name_
!= NULL
)
360 if (parameters_
!= NULL
)
361 parameters_
= parameters_
->Replace(context
, code_
);
362 code_
.Replace(context
);
364 context
.scope_
= scope
.parent_
;
365 scope
.Scope(context
, code_
.statements_
);
368 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
369 Replace_(context
, false);
373 CYFunctionParameter
*CYFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) {
374 name_
= name_
->Replace(context
);
375 context
.scope_
->Declare(context
, name_
, CYIdentifierArgument
);
377 next_
= next_
->Replace(context
, code
);
381 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
382 Replace_(context
, true);
386 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
387 if (replace_
!= NULL
&& replace_
!= this)
388 return replace_
->Replace(context
);
389 replace_
= context
.scope_
->Lookup(context
, this);
393 CYStatement
*CYIf::Replace(CYContext
&context
) {
394 context
.Replace(test_
);
395 context
.Replace(true_
);
396 context
.Replace(false_
);
400 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
404 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
405 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
408 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
409 CYPrefix::Replace(context
);
410 return $
M(rhs_
, $
S("$cyi"));
413 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
415 return $
M($
CYIndirect(object_
), property_
);
418 CYExpression
*CYInfix::Replace(CYContext
&context
) {
419 context
.Replace(lhs_
);
420 context
.Replace(rhs_
);
424 CYStatement
*CYLabel::Replace(CYContext
&context
) {
425 context
.Replace(statement_
);
429 CYStatement
*CYLet::Replace(CYContext
&context
) {
430 return $
CYWith($
CYObject(declarations_
->Property(context
)), &code_
);
433 void CYMember::Replace_(CYContext
&context
) {
434 context
.Replace(object_
);
435 context
.Replace(property_
);
438 CYExpression
*CYNew::AddArgument(CYContext
&context
, CYExpression
*value
) {
439 CYArgument
**argument(&arguments_
);
440 while (*argument
!= NULL
)
441 argument
= &(*argument
)->next_
;
442 *argument
= $
CYArgument(value
);
446 CYExpression
*CYNew::Replace(CYContext
&context
) {
447 context
.Replace(constructor_
);
448 arguments_
->Replace(context
);
452 CYNumber
*CYNull::Number(CYContext
&context
) {
456 CYString
*CYNull::String(CYContext
&context
) {
460 CYNumber
*CYNumber::Number(CYContext
&context
) {
464 CYString
*CYNumber::String(CYContext
&context
) {
465 // XXX: there is a precise algorithm for this
466 return $
S(apr_psprintf(context
.pool_
, "%.17g", Value()));
469 CYExpression
*CYObject::Replace(CYContext
&context
) {
470 properties_
->Replace(context
);
474 CYFunctionParameter
*CYOptionalFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) {
475 CYFunctionParameter
*parameter($
CYFunctionParameter(name_
, next_
));
476 parameter
= parameter
->Replace(context
, code
);
478 CYVariable
*name($
CYVariable(name_
));
479 code
.AddPrev($
CYIf($
CYIdentical($
CYTypeOf(name
), $
S("undefined")), $$
->*
480 $
E($
CYAssign(name
, initializer_
))
486 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
487 context
.Replace(lhs_
);
491 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
492 context
.Replace(rhs_
);
496 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
497 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
498 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
501 struct IdentifierUsageLess
:
502 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
504 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
505 if (lhs
->usage_
!= rhs
->usage_
)
506 return lhs
->usage_
> rhs
->usage_
;
511 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
514 void CYProgram::Replace(CYContext
&context
) {
516 scope
.parent_
= context
.scope_
;
517 context
.scope_
= &scope
;
518 statements_
= statements_
->ReplaceAll(context
);
519 context
.scope_
= scope
.parent_
;
520 scope
.Scope(context
, statements_
);
524 CYCStringSet external
;
525 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
526 external
.insert((*i
)->Word());
528 IdentifierUsages usages
;
530 if (offset
< context
.rename_
.size())
531 for (CYIdentifier
*i(context
.rename_
[offset
].identifier_
); i
!= NULL
; i
= i
->next_
)
534 // XXX: totalling the probable occurrences and sorting by them would improve the result
535 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
536 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
540 if (context
.options_
.verbose_
)
541 name
= apr_psprintf(context
.pool_
, "$%"APR_SIZE_T_FMT
"", offset
);
547 unsigned position(7), local(offset
+ 1);
550 unsigned index(local
% (sizeof(MappingSet
) - 1));
551 local
/= sizeof(MappingSet
) - 1;
552 id
[--position
] = MappingSet
[index
];
553 } while (local
!= 0);
555 if (external
.find(id
+ position
) != external
.end()) {
560 name
= apr_pstrmemdup(context
.pool_
, id
+ position
, 7 - position
);
561 // XXX: at some point, this could become a keyword
564 for (CYIdentifier
*identifier(i
->identifier_
); identifier
!= NULL
; identifier
= identifier
->next_
)
565 identifier
->Set(name
);
569 void CYProperty::Replace(CYContext
&context
) { $
T()
570 context
.Replace(value_
);
571 next_
->Replace(context
);
574 CYStatement
*CYReturn::Replace(CYContext
&context
) {
575 context
.Replace(value_
);
579 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
580 // XXX: this needs to do something much more epic to handle return
581 return call_
->AddArgument(context
, proc_
->Replace(context
));
584 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
585 return $
CYFunctionExpression(NULL
, parameters_
, code_
);
588 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
589 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
592 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
593 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
594 return *insert
.first
;
597 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
598 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
599 if (!insert
.second
) {
600 if ((*insert
.first
)->offset_
< identifier
->offset_
)
601 (*insert
.first
)->offset_
= identifier
->offset_
;
602 identifier
->replace_
= *insert
.first
;
603 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
608 struct IdentifierOffset
{
610 CYIdentifierFlags flags_
;
612 CYIdentifier
*identifier_
;
614 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
615 offset_(identifier
->offset_
),
617 usage_(identifier
->usage_
),
618 identifier_(identifier
)
623 struct IdentifierOffsetLess
:
624 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
626 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
627 if (lhs
.offset_
!= rhs
.offset_
)
628 return lhs
.offset_
< rhs
.offset_
;
629 if (lhs
.flags_
!= rhs
.flags_
)
630 return lhs
.flags_
< rhs
.flags_
;
631 /*if (lhs.usage_ != rhs.usage_)
632 return lhs.usage_ < rhs.usage_;*/
633 return lhs
.identifier_
< rhs
.identifier_
;
637 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
640 void CYScope::Scope(CYContext
&context
, CYStatement
*&statements
) {
644 CYDeclarations
*last(NULL
), *curr(NULL
);
646 IdentifierOffsets offsets
;
648 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
649 if (i
->second
!= CYIdentifierMagic
)
650 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
654 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
655 if (i
->flags_
== CYIdentifierVariable
) {
656 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
664 if (offset
< i
->offset_
)
666 if (context
.rename_
.size() <= offset
)
667 context
.rename_
.resize(offset
+ 1);
669 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
670 i
->identifier_
->SetNext(rename
.identifier_
);
671 rename
.identifier_
= i
->identifier_
;
672 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
676 CYVar
*var($
CYVar(last
));
677 var
->SetNext(statements
);
681 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
682 if (internal_
.find(*i
) == internal_
.end()) {
683 //std::cout << *i << '=' << offset << std::endl;
684 if ((*i
)->offset_
< offset
)
685 (*i
)->offset_
= offset
;
686 parent_
->Merge(context
, *i
);
690 CYStatement
*CYStatement::Collapse(CYContext
&context
) {
694 CYStatement
*CYStatement::ReplaceAll(CYContext
&context
) { $
T(NULL
)
695 CYStatement
*replace(this);
696 context
.Replace(replace
);
697 replace
->SetNext(next_
->ReplaceAll(context
));
698 return replace
->Collapse(context
);
701 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
702 size_t size(size_
+ rhs
->size_
);
703 char *value(new(context
.pool_
) char[size
+ 1]);
704 memcpy(value
, value_
, size_
);
705 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
707 return $
S(value
, size
);
710 CYNumber
*CYString::Number(CYContext
&context
) {
711 // XXX: there is a precise algorithm for this
715 CYString
*CYString::String(CYContext
&context
) {
719 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
720 context
.Replace(value_
);
721 clauses_
->Replace(context
);
725 CYExpression
*CYThis::Replace(CYContext
&context
) {
732 CYStatement
*Throw::Replace(CYContext
&context
) {
733 context
.Replace(value_
);
739 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
743 CYNumber
*CYTrue::Number(CYContext
&context
) {
747 CYString
*CYTrue::String(CYContext
&context
) {
754 CYStatement
*Try::Replace(CYContext
&context
) {
755 code_
.Replace(context
);
756 catch_
->Replace(context
);
757 finally_
->Replace(context
);
763 CYStatement
*CYVar::Replace(CYContext
&context
) {
764 return $
E(declarations_
->Replace(context
));
767 CYExpression
*CYVariable::Replace(CYContext
&context
) {
768 name_
= name_
->Replace(context
);
772 CYStatement
*CYWhile::Replace(CYContext
&context
) {
773 context
.Replace(test_
);
774 context
.Replace(code_
);
778 CYStatement
*CYWith::Replace(CYContext
&context
) {
779 context
.Replace(scope_
);
780 context
.Replace(code_
);
784 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
785 CYString
*name($
S(this));
787 return $
C1($
V("objc_getClass"), name
);