svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | #include "rewriter.h" |
| 31 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 32 | #include "ast.h" |
| 33 | #include "compiler.h" |
| 34 | #include "scopes.h" |
| 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 39 | class Processor: public AstVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | public: |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 41 | Processor(Variable* result, Zone* zone) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | : result_(result), |
| 43 | result_assigned_(false), |
| 44 | is_set_(false), |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 45 | in_try_(false), |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 46 | factory_(Isolate::Current(), zone) { |
| 47 | InitializeAstVisitor(); |
| 48 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 49 | |
| 50 | virtual ~Processor() { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
| 52 | void Process(ZoneList<Statement*>* statements); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 53 | bool result_assigned() const { return result_assigned_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 55 | AstNodeFactory<AstNullVisitor>* factory() { |
| 56 | return &factory_; |
| 57 | } |
| 58 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | private: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 60 | Variable* result_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | |
| 62 | // We are not tracking result usage via the result_'s use |
| 63 | // counts (we leave the accurate computation to the |
| 64 | // usage analyzer). Instead we simple remember if |
| 65 | // there was ever an assignment to result_. |
| 66 | bool result_assigned_; |
| 67 | |
| 68 | // To avoid storing to .result all the time, we eliminate some of |
| 69 | // the stores by keeping track of whether or not we're sure .result |
| 70 | // will be overwritten anyway. This is a bit more tricky than what I |
| 71 | // was hoping for |
| 72 | bool is_set_; |
| 73 | bool in_try_; |
| 74 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 75 | AstNodeFactory<AstNullVisitor> factory_; |
| 76 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | Expression* SetResult(Expression* value) { |
| 78 | result_assigned_ = true; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 79 | VariableProxy* result_proxy = factory()->NewVariableProxy(result_); |
| 80 | return factory()->NewAssignment( |
| 81 | Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Node visitors. |
| 85 | #define DEF_VISIT(type) \ |
| 86 | virtual void Visit##type(type* node); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 87 | AST_NODE_LIST(DEF_VISIT) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | #undef DEF_VISIT |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 89 | |
| 90 | void VisitIterationStatement(IterationStatement* stmt); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 91 | |
| 92 | DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | |
| 96 | void Processor::Process(ZoneList<Statement*>* statements) { |
| 97 | for (int i = statements->length() - 1; i >= 0; --i) { |
| 98 | Visit(statements->at(i)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void Processor::VisitBlock(Block* node) { |
| 104 | // An initializer block is the rewritten form of a variable declaration |
| 105 | // with initialization expressions. The initializer block contains the |
| 106 | // list of assignments corresponding to the initialization expressions. |
| 107 | // While unclear from the spec (ECMA-262, 3rd., 12.2), the value of |
| 108 | // a variable declaration with initialization expression is 'undefined' |
| 109 | // with some JS VMs: For instance, using smjs, print(eval('var x = 7')) |
| 110 | // returns 'undefined'. To obtain the same behavior with v8, we need |
| 111 | // to prevent rewriting in that case. |
| 112 | if (!node->is_initializer_block()) Process(node->statements()); |
| 113 | } |
| 114 | |
| 115 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 116 | void Processor::VisitModuleStatement(ModuleStatement* node) { |
| 117 | bool set_after_body = is_set_; |
| 118 | Visit(node->body()); |
| 119 | is_set_ = is_set_ && set_after_body; |
| 120 | } |
| 121 | |
| 122 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | void Processor::VisitExpressionStatement(ExpressionStatement* node) { |
| 124 | // Rewrite : <x>; -> .result = <x>; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 125 | if (!is_set_ && !node->expression()->IsThrow()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | node->set_expression(SetResult(node->expression())); |
| 127 | if (!in_try_) is_set_ = true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void Processor::VisitIfStatement(IfStatement* node) { |
| 133 | // Rewrite both then and else parts (reversed). |
| 134 | bool save = is_set_; |
| 135 | Visit(node->else_statement()); |
| 136 | bool set_after_then = is_set_; |
| 137 | is_set_ = save; |
| 138 | Visit(node->then_statement()); |
| 139 | is_set_ = is_set_ && set_after_then; |
| 140 | } |
| 141 | |
| 142 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 143 | void Processor::VisitIterationStatement(IterationStatement* node) { |
| 144 | // Rewrite the body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | bool set_after_loop = is_set_; |
| 146 | Visit(node->body()); |
| 147 | is_set_ = is_set_ && set_after_loop; |
| 148 | } |
| 149 | |
| 150 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 151 | void Processor::VisitDoWhileStatement(DoWhileStatement* node) { |
| 152 | VisitIterationStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 156 | void Processor::VisitWhileStatement(WhileStatement* node) { |
| 157 | VisitIterationStatement(node); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void Processor::VisitForStatement(ForStatement* node) { |
| 162 | VisitIterationStatement(node); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void Processor::VisitForInStatement(ForInStatement* node) { |
| 167 | VisitIterationStatement(node); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | void Processor::VisitTryCatchStatement(TryCatchStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | // Rewrite both try and catch blocks (reversed order). |
| 173 | bool set_after_catch = is_set_; |
| 174 | Visit(node->catch_block()); |
| 175 | is_set_ = is_set_ && set_after_catch; |
| 176 | bool save = in_try_; |
| 177 | in_try_ = true; |
| 178 | Visit(node->try_block()); |
| 179 | in_try_ = save; |
| 180 | } |
| 181 | |
| 182 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 183 | void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | // Rewrite both try and finally block (reversed order). |
| 185 | Visit(node->finally_block()); |
| 186 | bool save = in_try_; |
| 187 | in_try_ = true; |
| 188 | Visit(node->try_block()); |
| 189 | in_try_ = save; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void Processor::VisitSwitchStatement(SwitchStatement* node) { |
| 194 | // Rewrite statements in all case clauses in reversed order. |
| 195 | ZoneList<CaseClause*>* clauses = node->cases(); |
| 196 | bool set_after_switch = is_set_; |
| 197 | for (int i = clauses->length() - 1; i >= 0; --i) { |
| 198 | CaseClause* clause = clauses->at(i); |
| 199 | Process(clause->statements()); |
| 200 | } |
| 201 | is_set_ = is_set_ && set_after_switch; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void Processor::VisitContinueStatement(ContinueStatement* node) { |
| 206 | is_set_ = false; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void Processor::VisitBreakStatement(BreakStatement* node) { |
| 211 | is_set_ = false; |
| 212 | } |
| 213 | |
| 214 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 215 | void Processor::VisitWithStatement(WithStatement* node) { |
| 216 | bool set_after_body = is_set_; |
| 217 | Visit(node->statement()); |
| 218 | is_set_ = is_set_ && set_after_body; |
| 219 | } |
| 220 | |
| 221 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | // Do nothing: |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 223 | void Processor::VisitVariableDeclaration(VariableDeclaration* node) {} |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 224 | void Processor::VisitFunctionDeclaration(FunctionDeclaration* node) {} |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 225 | void Processor::VisitModuleDeclaration(ModuleDeclaration* node) {} |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 226 | void Processor::VisitImportDeclaration(ImportDeclaration* node) {} |
| 227 | void Processor::VisitExportDeclaration(ExportDeclaration* node) {} |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 228 | void Processor::VisitModuleLiteral(ModuleLiteral* node) {} |
| 229 | void Processor::VisitModuleVariable(ModuleVariable* node) {} |
| 230 | void Processor::VisitModulePath(ModulePath* node) {} |
| 231 | void Processor::VisitModuleUrl(ModuleUrl* node) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | void Processor::VisitEmptyStatement(EmptyStatement* node) {} |
| 233 | void Processor::VisitReturnStatement(ReturnStatement* node) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 234 | void Processor::VisitDebuggerStatement(DebuggerStatement* node) {} |
| 235 | |
| 236 | |
| 237 | // Expressions are never visited yet. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 238 | #define DEF_VISIT(type) \ |
| 239 | void Processor::Visit##type(type* expr) { UNREACHABLE(); } |
| 240 | EXPRESSION_NODE_LIST(DEF_VISIT) |
| 241 | #undef DEF_VISIT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | |
| 243 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 244 | // Assumes code has been parsed. Mutates the AST, so the AST should not |
| 245 | // continue to be used in the case of failure. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 246 | bool Rewriter::Rewrite(CompilationInfo* info) { |
| 247 | FunctionLiteral* function = info->function(); |
| 248 | ASSERT(function != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | Scope* scope = function->scope(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 250 | ASSERT(scope != NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 251 | if (!scope->is_global_scope() && !scope->is_eval_scope()) return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | |
| 253 | ZoneList<Statement*>* body = function->body(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 254 | if (!body->is_empty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 255 | Variable* result = scope->NewTemporary( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 256 | info->isolate()->factory()->result_string()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 257 | Processor processor(result, info->zone()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 258 | processor.Process(body); |
| 259 | if (processor.HasStackOverflow()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 261 | if (processor.result_assigned()) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 262 | ASSERT(function->end_position() != RelocInfo::kNoPosition); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 263 | // Set the position of the assignment statement one character past the |
| 264 | // source code, such that it definitely is not in the source code range |
| 265 | // of an immediate inner scope. For example in |
| 266 | // eval('with ({x:1}) x = 1'); |
| 267 | // the end position of the function generated for executing the eval code |
| 268 | // coincides with the end of the with scope which is the position of '1'. |
| 269 | int position = function->end_position(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 270 | VariableProxy* result_proxy = processor.factory()->NewVariableProxy( |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 271 | result->name(), false, result->interface(), position); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 272 | result_proxy->BindTo(result); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 273 | Statement* result_statement = |
| 274 | processor.factory()->NewReturnStatement(result_proxy); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 275 | result_statement->set_statement_pos(position); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 276 | body->Add(result_statement, info->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 277 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | |
| 283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | } } // namespace v8::internal |