| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 | 63 |
| 64 // Returns NULL if parsing failed. | 64 // Returns NULL if parsing failed. |
| 65 FunctionLiteral* ParseProgram(Handle<String> source, | 65 FunctionLiteral* ParseProgram(Handle<String> source, |
| 66 unibrow::CharacterStream* stream, | 66 unibrow::CharacterStream* stream, |
| 67 bool in_global_context); | 67 bool in_global_context); |
| 68 FunctionLiteral* ParseLazy(Handle<String> source, | 68 FunctionLiteral* ParseLazy(Handle<String> source, |
| 69 Handle<String> name, | 69 Handle<String> name, |
| 70 int start_position, bool is_expression); | 70 int start_position, bool is_expression); |
| 71 | 71 |
| 72 // The minimum number of contiguous assignment that will |
| 73 // be treated as an initialization block. Benchmarks show that |
| 74 // the overhead exceeds the savings below this limit. |
| 75 static const int kMinInitializationBlock = 3; |
| 76 |
| 72 protected: | 77 protected: |
| 73 | 78 |
| 74 enum Mode { | 79 enum Mode { |
| 75 PARSE_LAZILY, | 80 PARSE_LAZILY, |
| 76 PARSE_EAGERLY | 81 PARSE_EAGERLY |
| 77 }; | 82 }; |
| 78 | 83 |
| 79 // Report syntax error | 84 // Report syntax error |
| 80 void ReportUnexpectedToken(Token::Value token); | 85 void ReportUnexpectedToken(Token::Value token); |
| 81 | 86 |
| (...skipping 1126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1208 } | 1213 } |
| 1209 | 1214 |
| 1210 | 1215 |
| 1211 void PreParser::ReportMessageAt(Scanner::Location source_location, | 1216 void PreParser::ReportMessageAt(Scanner::Location source_location, |
| 1212 const char* type, | 1217 const char* type, |
| 1213 Vector<const char*> args) { | 1218 Vector<const char*> args) { |
| 1214 recorder()->LogMessage(source_location, type, args); | 1219 recorder()->LogMessage(source_location, type, args); |
| 1215 } | 1220 } |
| 1216 | 1221 |
| 1217 | 1222 |
| 1223 // An InitializationBlockFinder finds and marks sequences of statements of the |
| 1224 // form x.y.z.a = ...; x.y.z.b = ...; etc. |
| 1225 class InitializationBlockFinder { |
| 1226 public: |
| 1227 InitializationBlockFinder() |
| 1228 : first_in_block_(NULL), last_in_block_(NULL), block_size_(0) {} |
| 1229 |
| 1230 ~InitializationBlockFinder() { |
| 1231 if (InBlock()) EndBlock(); |
| 1232 } |
| 1233 |
| 1234 void Update(Statement* stat) { |
| 1235 Assignment* assignment = AsAssignment(stat); |
| 1236 if (InBlock()) { |
| 1237 if (BlockContinues(assignment)) { |
| 1238 UpdateBlock(assignment); |
| 1239 } else { |
| 1240 EndBlock(); |
| 1241 } |
| 1242 } |
| 1243 if (!InBlock() && (assignment != NULL) && |
| 1244 (assignment->op() == Token::ASSIGN)) { |
| 1245 StartBlock(assignment); |
| 1246 } |
| 1247 } |
| 1248 |
| 1249 private: |
| 1250 static Assignment* AsAssignment(Statement* stat) { |
| 1251 if (stat == NULL) return NULL; |
| 1252 ExpressionStatement* exp_stat = stat->AsExpressionStatement(); |
| 1253 if (exp_stat == NULL) return NULL; |
| 1254 return exp_stat->expression()->AsAssignment(); |
| 1255 } |
| 1256 |
| 1257 // Returns true if the expressions appear to denote the same object. |
| 1258 // In the context of initialization blocks, we only consider expressions |
| 1259 // of the form 'x.y.z'. |
| 1260 static bool SameObject(Expression* e1, Expression* e2) { |
| 1261 VariableProxy* v1 = e1->AsVariableProxy(); |
| 1262 VariableProxy* v2 = e2->AsVariableProxy(); |
| 1263 if (v1 != NULL && v2 != NULL) { |
| 1264 return v1->name()->Equals(*v2->name()); |
| 1265 } |
| 1266 Property* p1 = e1->AsProperty(); |
| 1267 Property* p2 = e2->AsProperty(); |
| 1268 if ((p1 == NULL) || (p2 == NULL)) return false; |
| 1269 Literal* key1 = p1->key()->AsLiteral(); |
| 1270 Literal* key2 = p2->key()->AsLiteral(); |
| 1271 if ((key1 == NULL) || (key2 == NULL)) return false; |
| 1272 if (!key1->handle()->IsString() || !key2->handle()->IsString()) { |
| 1273 return false; |
| 1274 } |
| 1275 String* name1 = String::cast(*key1->handle()); |
| 1276 String* name2 = String::cast(*key2->handle()); |
| 1277 if (!name1->Equals(name2)) return false; |
| 1278 return SameObject(p1->obj(), p2->obj()); |
| 1279 } |
| 1280 |
| 1281 // Returns true if the expressions appear to denote different properties |
| 1282 // of the same object. |
| 1283 static bool PropertyOfSameObject(Expression* e1, Expression* e2) { |
| 1284 Property* p1 = e1->AsProperty(); |
| 1285 Property* p2 = e2->AsProperty(); |
| 1286 if ((p1 == NULL) || (p2 == NULL)) return false; |
| 1287 return SameObject(p1->obj(), p2->obj()); |
| 1288 } |
| 1289 |
| 1290 bool BlockContinues(Assignment* assignment) { |
| 1291 if ((assignment == NULL) || (first_in_block_ == NULL)) return false; |
| 1292 if (assignment->op() != Token::ASSIGN) return false; |
| 1293 return PropertyOfSameObject(first_in_block_->target(), |
| 1294 assignment->target()); |
| 1295 } |
| 1296 |
| 1297 void StartBlock(Assignment* assignment) { |
| 1298 first_in_block_ = assignment; |
| 1299 last_in_block_ = assignment; |
| 1300 block_size_ = 1; |
| 1301 } |
| 1302 |
| 1303 void UpdateBlock(Assignment* assignment) { |
| 1304 last_in_block_ = assignment; |
| 1305 ++block_size_; |
| 1306 } |
| 1307 |
| 1308 void EndBlock() { |
| 1309 if (block_size_ >= Parser::kMinInitializationBlock) { |
| 1310 first_in_block_->mark_block_start(); |
| 1311 last_in_block_->mark_block_end(); |
| 1312 } |
| 1313 last_in_block_ = first_in_block_ = NULL; |
| 1314 block_size_ = 0; |
| 1315 } |
| 1316 |
| 1317 bool InBlock() { return first_in_block_ != NULL; } |
| 1318 |
| 1319 Assignment* first_in_block_; |
| 1320 Assignment* last_in_block_; |
| 1321 int block_size_; |
| 1322 |
| 1323 DISALLOW_COPY_AND_ASSIGN(InitializationBlockFinder); |
| 1324 }; |
| 1325 |
| 1326 |
| 1218 void* Parser::ParseSourceElements(ZoneListWrapper<Statement>* processor, | 1327 void* Parser::ParseSourceElements(ZoneListWrapper<Statement>* processor, |
| 1219 int end_token, | 1328 int end_token, |
| 1220 bool* ok) { | 1329 bool* ok) { |
| 1221 // SourceElements :: | 1330 // SourceElements :: |
| 1222 // (Statement)* <end_token> | 1331 // (Statement)* <end_token> |
| 1223 | 1332 |
| 1224 // Allocate a target stack to use for this set of source | 1333 // Allocate a target stack to use for this set of source |
| 1225 // elements. This way, all scripts and functions get their own | 1334 // elements. This way, all scripts and functions get their own |
| 1226 // target stack thus avoiding illegal breaks and continues across | 1335 // target stack thus avoiding illegal breaks and continues across |
| 1227 // functions. | 1336 // functions. |
| 1228 TargetScope scope(this); | 1337 TargetScope scope(this); |
| 1229 | 1338 |
| 1230 ASSERT(processor != NULL); | 1339 ASSERT(processor != NULL); |
| 1340 InitializationBlockFinder block_finder; |
| 1231 while (peek() != end_token) { | 1341 while (peek() != end_token) { |
| 1232 Statement* stat = ParseStatement(NULL, CHECK_OK); | 1342 Statement* stat = ParseStatement(NULL, CHECK_OK); |
| 1233 if (stat && !stat->IsEmpty()) processor->Add(stat); | 1343 if (stat == NULL || stat->IsEmpty()) continue; |
| 1344 // We find and mark the initialization blocks on top level code only. |
| 1345 // This is because the optimization prevents reuse of the map transitions, |
| 1346 // so it should be used only for code that will only be run once. |
| 1347 if (top_scope_->is_global_scope()) block_finder.Update(stat); |
| 1348 processor->Add(stat); |
| 1234 } | 1349 } |
| 1235 return 0; | 1350 return 0; |
| 1236 } | 1351 } |
| 1237 | 1352 |
| 1238 | 1353 |
| 1239 Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) { | 1354 Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) { |
| 1240 // Statement :: | 1355 // Statement :: |
| 1241 // Block | 1356 // Block |
| 1242 // VariableStatement | 1357 // VariableStatement |
| 1243 // EmptyStatement | 1358 // EmptyStatement |
| (...skipping 3151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4395 start_position, | 4510 start_position, |
| 4396 is_expression); | 4511 is_expression); |
| 4397 return result; | 4512 return result; |
| 4398 } | 4513 } |
| 4399 | 4514 |
| 4400 | 4515 |
| 4401 #undef NEW | 4516 #undef NEW |
| 4402 | 4517 |
| 4403 | 4518 |
| 4404 } } // namespace v8::internal | 4519 } } // namespace v8::internal |
| OLD | NEW |