1 // Copyright 2010 the V8 project authors. All rights reserved.
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
30 #include "codegen-inl.h"
31 #include "fast-codegen.h"
32 #include "data-flow.h"
33 #include "scopes.h"
34
35 namespace v8 {
36 namespace internal {
37
38 #define BAILOUT(reason) \
39 do { \
40 if (FLAG_trace_bailout) { \
41 PrintF("%s\n", reason); \
42 } \
43 has_supported_syntax_ = false; \
44 return; \
45 } while (false)
46
47
48 #define CHECK_BAILOUT \
49 do { \
50 if (!has_supported_syntax_) return; \
51 } while (false)
52
53
Check(CompilationInfo * info)54 void FastCodeGenSyntaxChecker::Check(CompilationInfo* info) {
55 info_ = info;
56
57 // We do not specialize if we do not have a receiver or if it is not a
58 // JS object with fast mode properties.
59 if (!info->has_receiver()) BAILOUT("No receiver");
60 if (!info->receiver()->IsJSObject()) BAILOUT("Receiver is not an object");
61 Handle<JSObject> object = Handle<JSObject>::cast(info->receiver());
62 if (!object->HasFastProperties()) BAILOUT("Receiver is in dictionary mode");
63
64 // We do not support stack or heap slots (both of which require
65 // allocation).
66 Scope* scope = info->scope();
67 if (scope->num_stack_slots() > 0) {
68 BAILOUT("Function has stack-allocated locals");
69 }
70 if (scope->num_heap_slots() > 0) {
71 BAILOUT("Function has context-allocated locals");
72 }
73
74 VisitDeclarations(scope->declarations());
75 CHECK_BAILOUT;
76
77 // We do not support empty function bodies.
78 if (info->function()->body()->is_empty()) {
79 BAILOUT("Function has an empty body");
80 }
81 VisitStatements(info->function()->body());
82 }
83
84
VisitDeclarations(ZoneList<Declaration * > * decls)85 void FastCodeGenSyntaxChecker::VisitDeclarations(
86 ZoneList<Declaration*>* decls) {
87 if (!decls->is_empty()) BAILOUT("Function has declarations");
88 }
89
90
VisitStatements(ZoneList<Statement * > * stmts)91 void FastCodeGenSyntaxChecker::VisitStatements(ZoneList<Statement*>* stmts) {
92 if (stmts->length() != 1) {
93 BAILOUT("Function body is not a singleton statement.");
94 }
95 Visit(stmts->at(0));
96 }
97
98
VisitDeclaration(Declaration * decl)99 void FastCodeGenSyntaxChecker::VisitDeclaration(Declaration* decl) {
100 UNREACHABLE();
101 }
102
103
VisitBlock(Block * stmt)104 void FastCodeGenSyntaxChecker::VisitBlock(Block* stmt) {
105 VisitStatements(stmt->statements());
106 }
107
108
VisitExpressionStatement(ExpressionStatement * stmt)109 void FastCodeGenSyntaxChecker::VisitExpressionStatement(
110 ExpressionStatement* stmt) {
111 Visit(stmt->expression());
112 }
113
114
VisitEmptyStatement(EmptyStatement * stmt)115 void FastCodeGenSyntaxChecker::VisitEmptyStatement(EmptyStatement* stmt) {
116 // Supported.
117 }
118
119
VisitIfStatement(IfStatement * stmt)120 void FastCodeGenSyntaxChecker::VisitIfStatement(IfStatement* stmt) {
121 BAILOUT("IfStatement");
122 }
123
124
VisitContinueStatement(ContinueStatement * stmt)125 void FastCodeGenSyntaxChecker::VisitContinueStatement(ContinueStatement* stmt) {
126 BAILOUT("Continuestatement");
127 }
128
129
VisitBreakStatement(BreakStatement * stmt)130 void FastCodeGenSyntaxChecker::VisitBreakStatement(BreakStatement* stmt) {
131 BAILOUT("BreakStatement");
132 }
133
134
VisitReturnStatement(ReturnStatement * stmt)135 void FastCodeGenSyntaxChecker::VisitReturnStatement(ReturnStatement* stmt) {
136 BAILOUT("ReturnStatement");
137 }
138
139
VisitWithEnterStatement(WithEnterStatement * stmt)140 void FastCodeGenSyntaxChecker::VisitWithEnterStatement(
141 WithEnterStatement* stmt) {
142 BAILOUT("WithEnterStatement");
143 }
144
145
VisitWithExitStatement(WithExitStatement * stmt)146 void FastCodeGenSyntaxChecker::VisitWithExitStatement(WithExitStatement* stmt) {
147 BAILOUT("WithExitStatement");
148 }
149
150
VisitSwitchStatement(SwitchStatement * stmt)151 void FastCodeGenSyntaxChecker::VisitSwitchStatement(SwitchStatement* stmt) {
152 BAILOUT("SwitchStatement");
153 }
154
155
VisitDoWhileStatement(DoWhileStatement * stmt)156 void FastCodeGenSyntaxChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
157 BAILOUT("DoWhileStatement");
158 }
159
160
VisitWhileStatement(WhileStatement * stmt)161 void FastCodeGenSyntaxChecker::VisitWhileStatement(WhileStatement* stmt) {
162 BAILOUT("WhileStatement");
163 }
164
165
VisitForStatement(ForStatement * stmt)166 void FastCodeGenSyntaxChecker::VisitForStatement(ForStatement* stmt) {
167 BAILOUT("ForStatement");
168 }
169
170
VisitForInStatement(ForInStatement * stmt)171 void FastCodeGenSyntaxChecker::VisitForInStatement(ForInStatement* stmt) {
172 BAILOUT("ForInStatement");
173 }
174
175
VisitTryCatchStatement(TryCatchStatement * stmt)176 void FastCodeGenSyntaxChecker::VisitTryCatchStatement(TryCatchStatement* stmt) {
177 BAILOUT("TryCatchStatement");
178 }
179
180
VisitTryFinallyStatement(TryFinallyStatement * stmt)181 void FastCodeGenSyntaxChecker::VisitTryFinallyStatement(
182 TryFinallyStatement* stmt) {
183 BAILOUT("TryFinallyStatement");
184 }
185
186
VisitDebuggerStatement(DebuggerStatement * stmt)187 void FastCodeGenSyntaxChecker::VisitDebuggerStatement(
188 DebuggerStatement* stmt) {
189 BAILOUT("DebuggerStatement");
190 }
191
192
VisitFunctionLiteral(FunctionLiteral * expr)193 void FastCodeGenSyntaxChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
194 BAILOUT("FunctionLiteral");
195 }
196
197
VisitFunctionBoilerplateLiteral(FunctionBoilerplateLiteral * expr)198 void FastCodeGenSyntaxChecker::VisitFunctionBoilerplateLiteral(
199 FunctionBoilerplateLiteral* expr) {
200 BAILOUT("FunctionBoilerplateLiteral");
201 }
202
203
VisitConditional(Conditional * expr)204 void FastCodeGenSyntaxChecker::VisitConditional(Conditional* expr) {
205 BAILOUT("Conditional");
206 }
207
208
VisitSlot(Slot * expr)209 void FastCodeGenSyntaxChecker::VisitSlot(Slot* expr) {
210 UNREACHABLE();
211 }
212
213
VisitVariableProxy(VariableProxy * expr)214 void FastCodeGenSyntaxChecker::VisitVariableProxy(VariableProxy* expr) {
215 // Only global variable references are supported.
216 Variable* var = expr->var();
217 if (!var->is_global() || var->is_this()) BAILOUT("Non-global variable");
218
219 // Check if the global variable is existing and non-deletable.
220 if (info()->has_global_object()) {
221 LookupResult lookup;
222 info()->global_object()->Lookup(*expr->name(), &lookup);
223 if (!lookup.IsProperty()) {
224 BAILOUT("Non-existing global variable");
225 }
226 // We do not handle global variables with accessors or interceptors.
227 if (lookup.type() != NORMAL) {
228 BAILOUT("Global variable with accessors or interceptors.");
229 }
230 // We do not handle deletable global variables.
231 if (!lookup.IsDontDelete()) {
232 BAILOUT("Deletable global variable");
233 }
234 }
235 }
236
237
VisitLiteral(Literal * expr)238 void FastCodeGenSyntaxChecker::VisitLiteral(Literal* expr) {
239 BAILOUT("Literal");
240 }
241
242
VisitRegExpLiteral(RegExpLiteral * expr)243 void FastCodeGenSyntaxChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
244 BAILOUT("RegExpLiteral");
245 }
246
247
VisitObjectLiteral(ObjectLiteral * expr)248 void FastCodeGenSyntaxChecker::VisitObjectLiteral(ObjectLiteral* expr) {
249 BAILOUT("ObjectLiteral");
250 }
251
252
VisitArrayLiteral(ArrayLiteral * expr)253 void FastCodeGenSyntaxChecker::VisitArrayLiteral(ArrayLiteral* expr) {
254 BAILOUT("ArrayLiteral");
255 }
256
257
VisitCatchExtensionObject(CatchExtensionObject * expr)258 void FastCodeGenSyntaxChecker::VisitCatchExtensionObject(
259 CatchExtensionObject* expr) {
260 BAILOUT("CatchExtensionObject");
261 }
262
263
VisitAssignment(Assignment * expr)264 void FastCodeGenSyntaxChecker::VisitAssignment(Assignment* expr) {
265 // Simple assignments to (named) this properties are supported.
266 if (expr->op() != Token::ASSIGN) BAILOUT("Non-simple assignment");
267
268 Property* prop = expr->target()->AsProperty();
269 if (prop == NULL) BAILOUT("Non-property assignment");
270 VariableProxy* proxy = prop->obj()->AsVariableProxy();
271 if (proxy == NULL || !proxy->var()->is_this()) {
272 BAILOUT("Non-this-property assignment");
273 }
274 if (!prop->key()->IsPropertyName()) {
275 BAILOUT("Non-named-property assignment");
276 }
277
278 // We will only specialize for fields on the object itself.
279 // Expression::IsPropertyName implies that the name is a literal
280 // symbol but we do not assume that.
281 Literal* key = prop->key()->AsLiteral();
282 if (key != NULL && key->handle()->IsString()) {
283 Handle<Object> receiver = info()->receiver();
284 Handle<String> name = Handle<String>::cast(key->handle());
285 LookupResult lookup;
286 receiver->Lookup(*name, &lookup);
287 if (!lookup.IsProperty()) {
288 BAILOUT("Assigned property not found at compile time");
289 }
290 if (lookup.holder() != *receiver) BAILOUT("Non-own property assignment");
291 if (!lookup.type() == FIELD) BAILOUT("Non-field property assignment");
292 } else {
293 UNREACHABLE();
294 BAILOUT("Unexpected non-string-literal property key");
295 }
296
297 Visit(expr->value());
298 }
299
300
VisitThrow(Throw * expr)301 void FastCodeGenSyntaxChecker::VisitThrow(Throw* expr) {
302 BAILOUT("Throw");
303 }
304
305
VisitProperty(Property * expr)306 void FastCodeGenSyntaxChecker::VisitProperty(Property* expr) {
307 // We support named this property references.
308 VariableProxy* proxy = expr->obj()->AsVariableProxy();
309 if (proxy == NULL || !proxy->var()->is_this()) {
310 BAILOUT("Non-this-property reference");
311 }
312 if (!expr->key()->IsPropertyName()) {
313 BAILOUT("Non-named-property reference");
314 }
315
316 // We will only specialize for fields on the object itself.
317 // Expression::IsPropertyName implies that the name is a literal
318 // symbol but we do not assume that.
319 Literal* key = expr->key()->AsLiteral();
320 if (key != NULL && key->handle()->IsString()) {
321 Handle<Object> receiver = info()->receiver();
322 Handle<String> name = Handle<String>::cast(key->handle());
323 LookupResult lookup;
324 receiver->Lookup(*name, &lookup);
325 if (!lookup.IsProperty()) {
326 BAILOUT("Referenced property not found at compile time");
327 }
328 if (lookup.holder() != *receiver) BAILOUT("Non-own property reference");
329 if (!lookup.type() == FIELD) BAILOUT("Non-field property reference");
330 } else {
331 UNREACHABLE();
332 BAILOUT("Unexpected non-string-literal property key");
333 }
334 }
335
336
VisitCall(Call * expr)337 void FastCodeGenSyntaxChecker::VisitCall(Call* expr) {
338 BAILOUT("Call");
339 }
340
341
VisitCallNew(CallNew * expr)342 void FastCodeGenSyntaxChecker::VisitCallNew(CallNew* expr) {
343 BAILOUT("CallNew");
344 }
345
346
VisitCallRuntime(CallRuntime * expr)347 void FastCodeGenSyntaxChecker::VisitCallRuntime(CallRuntime* expr) {
348 BAILOUT("CallRuntime");
349 }
350
351
VisitUnaryOperation(UnaryOperation * expr)352 void FastCodeGenSyntaxChecker::VisitUnaryOperation(UnaryOperation* expr) {
353 BAILOUT("UnaryOperation");
354 }
355
356
VisitCountOperation(CountOperation * expr)357 void FastCodeGenSyntaxChecker::VisitCountOperation(CountOperation* expr) {
358 BAILOUT("CountOperation");
359 }
360
361
VisitBinaryOperation(BinaryOperation * expr)362 void FastCodeGenSyntaxChecker::VisitBinaryOperation(BinaryOperation* expr) {
363 // We support bitwise OR.
364 switch (expr->op()) {
365 case Token::COMMA:
366 BAILOUT("BinaryOperation COMMA");
367 case Token::OR:
368 BAILOUT("BinaryOperation OR");
369 case Token::AND:
370 BAILOUT("BinaryOperation AND");
371
372 case Token::BIT_OR:
373 // We support expressions nested on the left because they only require
374 // a pair of registers to keep all intermediate values in registers
375 // (i.e., the expression stack has height no more than two).
376 if (!expr->right()->IsLeaf()) BAILOUT("expression nested on right");
377
378 // We do not allow subexpressions with side effects because we
379 // (currently) bail out to the beginning of the full function. The
380 // only expressions with side effects that we would otherwise handle
381 // are assignments.
382 if (expr->left()->AsAssignment() != NULL ||
383 expr->right()->AsAssignment() != NULL) {
384 BAILOUT("subexpression of binary operation has side effects");
385 }
386
387 Visit(expr->left());
388 CHECK_BAILOUT;
389 Visit(expr->right());
390 break;
391
392 case Token::BIT_XOR:
393 BAILOUT("BinaryOperation BIT_XOR");
394 case Token::BIT_AND:
395 BAILOUT("BinaryOperation BIT_AND");
396 case Token::SHL:
397 BAILOUT("BinaryOperation SHL");
398 case Token::SAR:
399 BAILOUT("BinaryOperation SAR");
400 case Token::SHR:
401 BAILOUT("BinaryOperation SHR");
402 case Token::ADD:
403 BAILOUT("BinaryOperation ADD");
404 case Token::SUB:
405 BAILOUT("BinaryOperation SUB");
406 case Token::MUL:
407 BAILOUT("BinaryOperation MUL");
408 case Token::DIV:
409 BAILOUT("BinaryOperation DIV");
410 case Token::MOD:
411 BAILOUT("BinaryOperation MOD");
412 default:
413 UNREACHABLE();
414 }
415 }
416
417
VisitCompareOperation(CompareOperation * expr)418 void FastCodeGenSyntaxChecker::VisitCompareOperation(CompareOperation* expr) {
419 BAILOUT("CompareOperation");
420 }
421
422
VisitThisFunction(ThisFunction * expr)423 void FastCodeGenSyntaxChecker::VisitThisFunction(ThisFunction* expr) {
424 BAILOUT("ThisFunction");
425 }
426
427 #undef BAILOUT
428 #undef CHECK_BAILOUT
429
430
431 #define __ ACCESS_MASM(masm())
432
MakeCode(CompilationInfo * info)433 Handle<Code> FastCodeGenerator::MakeCode(CompilationInfo* info) {
434 // Label the AST before calling MakeCodePrologue, so AST node numbers are
435 // printed with the AST.
436 AstLabeler labeler;
437 labeler.Label(info);
438
439 LivenessAnalyzer analyzer;
440 analyzer.Analyze(info->function());
441
442 CodeGenerator::MakeCodePrologue(info);
443
444 const int kInitialBufferSize = 4 * KB;
445 MacroAssembler masm(NULL, kInitialBufferSize);
446
447 // Generate the fast-path code.
448 FastCodeGenerator fast_cgen(&masm);
449 fast_cgen.Generate(info);
450 if (fast_cgen.HasStackOverflow()) {
451 ASSERT(!Top::has_pending_exception());
452 return Handle<Code>::null();
453 }
454
455 // Generate the full code for the function in bailout mode, using the same
456 // macro assembler.
457 CodeGenerator cgen(&masm);
458 CodeGeneratorScope scope(&cgen);
459 info->set_mode(CompilationInfo::SECONDARY);
460 cgen.Generate(info);
461 if (cgen.HasStackOverflow()) {
462 ASSERT(!Top::has_pending_exception());
463 return Handle<Code>::null();
464 }
465
466 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
467 return CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
468 }
469
470
accumulator0()471 Register FastCodeGenerator::accumulator0() { return eax; }
accumulator1()472 Register FastCodeGenerator::accumulator1() { return edx; }
scratch0()473 Register FastCodeGenerator::scratch0() { return ecx; }
scratch1()474 Register FastCodeGenerator::scratch1() { return edi; }
receiver_reg()475 Register FastCodeGenerator::receiver_reg() { return ebx; }
context_reg()476 Register FastCodeGenerator::context_reg() { return esi; }
477
478
EmitLoadReceiver()479 void FastCodeGenerator::EmitLoadReceiver() {
480 // Offset 2 is due to return address and saved frame pointer.
481 int index = 2 + function()->scope()->num_parameters();
482 __ mov(receiver_reg(), Operand(ebp, index * kPointerSize));
483 }
484
485
EmitGlobalVariableLoad(Handle<Object> cell)486 void FastCodeGenerator::EmitGlobalVariableLoad(Handle<Object> cell) {
487 ASSERT(!destination().is(no_reg));
488 ASSERT(cell->IsJSGlobalPropertyCell());
489
490 __ mov(destination(), Immediate(cell));
491 __ mov(destination(),
492 FieldOperand(destination(), JSGlobalPropertyCell::kValueOffset));
493 if (FLAG_debug_code) {
494 __ cmp(destination(), Factory::the_hole_value());
495 __ Check(not_equal, "DontDelete cells can't contain the hole");
496 }
497
498 // The loaded value is not known to be a smi.
499 clear_as_smi(destination());
500 }
501
502
EmitThisPropertyStore(Handle<String> name)503 void FastCodeGenerator::EmitThisPropertyStore(Handle<String> name) {
504 LookupResult lookup;
505 info()->receiver()->Lookup(*name, &lookup);
506
507 ASSERT(lookup.holder() == *info()->receiver());
508 ASSERT(lookup.type() == FIELD);
509 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map());
510 int index = lookup.GetFieldIndex() - map->inobject_properties();
511 int offset = index * kPointerSize;
512
513 // We will emit the write barrier unless the stored value is statically
514 // known to be a smi.
515 bool needs_write_barrier = !is_smi(accumulator0());
516
517 // Perform the store. Negative offsets are inobject properties.
518 if (offset < 0) {
519 offset += map->instance_size();
520 __ mov(FieldOperand(receiver_reg(), offset), accumulator0());
521 if (needs_write_barrier) {
522 // Preserve receiver from write barrier.
523 __ mov(scratch0(), receiver_reg());
524 }
525 } else {
526 offset += FixedArray::kHeaderSize;
527 __ mov(scratch0(),
528 FieldOperand(receiver_reg(), JSObject::kPropertiesOffset));
529 __ mov(FieldOperand(scratch0(), offset), accumulator0());
530 }
531
532 if (needs_write_barrier) {
533 if (destination().is(no_reg)) {
534 // After RecordWrite accumulator0 is only accidently a smi, but it is
535 // already marked as not known to be one.
536 __ RecordWrite(scratch0(), offset, accumulator0(), scratch1());
537 } else {
538 // Copy the value to the other accumulator to preserve a copy from the
539 // write barrier. One of the accumulators is available as a scratch
540 // register. Neither is a smi.
541 __ mov(accumulator1(), accumulator0());
542 clear_as_smi(accumulator1());
543 Register value_scratch = other_accumulator(destination());
544 __ RecordWrite(scratch0(), offset, value_scratch, scratch1());
545 }
546 } else if (destination().is(accumulator1())) {
547 __ mov(accumulator1(), accumulator0());
548 // Is a smi because we do not need the write barrier.
549 set_as_smi(accumulator1());
550 }
551 }
552
553
EmitThisPropertyLoad(Handle<String> name)554 void FastCodeGenerator::EmitThisPropertyLoad(Handle<String> name) {
555 ASSERT(!destination().is(no_reg));
556 LookupResult lookup;
557 info()->receiver()->Lookup(*name, &lookup);
558
559 ASSERT(lookup.holder() == *info()->receiver());
560 ASSERT(lookup.type() == FIELD);
561 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map());
562 int index = lookup.GetFieldIndex() - map->inobject_properties();
563 int offset = index * kPointerSize;
564
565 // Perform the load. Negative offsets are inobject properties.
566 if (offset < 0) {
567 offset += map->instance_size();
568 __ mov(destination(), FieldOperand(receiver_reg(), offset));
569 } else {
570 offset += FixedArray::kHeaderSize;
571 __ mov(scratch0(),
572 FieldOperand(receiver_reg(), JSObject::kPropertiesOffset));
573 __ mov(destination(), FieldOperand(scratch0(), offset));
574 }
575
576 // The loaded value is not known to be a smi.
577 clear_as_smi(destination());
578 }
579
580
EmitBitOr()581 void FastCodeGenerator::EmitBitOr() {
582 if (is_smi(accumulator0()) && is_smi(accumulator1())) {
583 // If both operands are known to be a smi then there is no need to check
584 // the operands or result. There is no need to perform the operation in
585 // an effect context.
586 if (!destination().is(no_reg)) {
587 // Leave the result in the destination register. Bitwise or is
588 // commutative.
589 __ or_(destination(), Operand(other_accumulator(destination())));
590 }
591 } else {
592 // Left is in accumulator1, right in accumulator0.
593 Label* bailout = NULL;
594 if (destination().is(accumulator0())) {
595 __ mov(scratch0(), accumulator0());
596 __ or_(destination(), Operand(accumulator1())); // Or is commutative.
597 __ test(destination(), Immediate(kSmiTagMask));
598 bailout = info()->AddBailout(accumulator1(), scratch0()); // Left, right.
599 } else if (destination().is(accumulator1())) {
600 __ mov(scratch0(), accumulator1());
601 __ or_(destination(), Operand(accumulator0()));
602 __ test(destination(), Immediate(kSmiTagMask));
603 bailout = info()->AddBailout(scratch0(), accumulator0());
604 } else {
605 ASSERT(destination().is(no_reg));
606 __ mov(scratch0(), accumulator1());
607 __ or_(scratch0(), Operand(accumulator0()));
608 __ test(scratch0(), Immediate(kSmiTagMask));
609 bailout = info()->AddBailout(accumulator1(), accumulator0());
610 }
611 __ j(not_zero, bailout, not_taken);
612 }
613
614 // If we didn't bailout, the result (in fact, both inputs too) is known to
615 // be a smi.
616 set_as_smi(accumulator0());
617 set_as_smi(accumulator1());
618 }
619
620
Generate(CompilationInfo * compilation_info)621 void FastCodeGenerator::Generate(CompilationInfo* compilation_info) {
622 ASSERT(info_ == NULL);
623 info_ = compilation_info;
624
625 // Save the caller's frame pointer and set up our own.
626 Comment prologue_cmnt(masm(), ";; Prologue");
627 __ push(ebp);
628 __ mov(ebp, esp);
629 __ push(esi); // Context.
630 __ push(edi); // Closure.
631 // Note that we keep a live register reference to esi (context) at this
632 // point.
633
634 Label* bailout_to_beginning = info()->AddBailout();
635 // Receiver (this) is allocated to a fixed register.
636 if (info()->has_this_properties()) {
637 Comment cmnt(masm(), ";; MapCheck(this)");
638 if (FLAG_print_ir) {
639 PrintF("#: MapCheck(this)\n");
640 }
641 ASSERT(info()->has_receiver() && info()->receiver()->IsHeapObject());
642 Handle<HeapObject> object = Handle<HeapObject>::cast(info()->receiver());
643 Handle<Map> map(object->map());
644 EmitLoadReceiver();
645 __ CheckMap(receiver_reg(), map, bailout_to_beginning, false);
646 }
647
648 // If there is a global variable access check if the global object is the
649 // same as at lazy-compilation time.
650 if (info()->has_globals()) {
651 Comment cmnt(masm(), ";; MapCheck(GLOBAL)");
652 if (FLAG_print_ir) {
653 PrintF("#: MapCheck(GLOBAL)\n");
654 }
655 ASSERT(info()->has_global_object());
656 Handle<Map> map(info()->global_object()->map());
657 __ mov(scratch0(), CodeGenerator::GlobalObject());
658 __ CheckMap(scratch0(), map, bailout_to_beginning, true);
659 }
660
661 VisitStatements(function()->body());
662
663 Comment return_cmnt(masm(), ";; Return(<undefined>)");
664 if (FLAG_print_ir) {
665 PrintF("#: Return(<undefined>)\n");
666 }
667 __ mov(eax, Factory::undefined_value());
668 __ mov(esp, ebp);
669 __ pop(ebp);
670 __ ret((scope()->num_parameters() + 1) * kPointerSize);
671 }
672
673
VisitDeclaration(Declaration * decl)674 void FastCodeGenerator::VisitDeclaration(Declaration* decl) {
675 UNREACHABLE();
676 }
677
678
VisitBlock(Block * stmt)679 void FastCodeGenerator::VisitBlock(Block* stmt) {
680 VisitStatements(stmt->statements());
681 }
682
683
VisitExpressionStatement(ExpressionStatement * stmt)684 void FastCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
685 Visit(stmt->expression());
686 }
687
688
VisitEmptyStatement(EmptyStatement * stmt)689 void FastCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
690 // Nothing to do.
691 }
692
693
VisitIfStatement(IfStatement * stmt)694 void FastCodeGenerator::VisitIfStatement(IfStatement* stmt) {
695 UNREACHABLE();
696 }
697
698
VisitContinueStatement(ContinueStatement * stmt)699 void FastCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
700 UNREACHABLE();
701 }
702
703
VisitBreakStatement(BreakStatement * stmt)704 void FastCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
705 UNREACHABLE();
706 }
707
708
VisitReturnStatement(ReturnStatement * stmt)709 void FastCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
710 UNREACHABLE();
711 }
712
713
VisitWithEnterStatement(WithEnterStatement * stmt)714 void FastCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
715 UNREACHABLE();
716 }
717
718
VisitWithExitStatement(WithExitStatement * stmt)719 void FastCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
720 UNREACHABLE();
721 }
722
723
VisitSwitchStatement(SwitchStatement * stmt)724 void FastCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
725 UNREACHABLE();
726 }
727
728
VisitDoWhileStatement(DoWhileStatement * stmt)729 void FastCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
730 UNREACHABLE();
731 }
732
733
VisitWhileStatement(WhileStatement * stmt)734 void FastCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
735 UNREACHABLE();
736 }
737
738
VisitForStatement(ForStatement * stmt)739 void FastCodeGenerator::VisitForStatement(ForStatement* stmt) {
740 UNREACHABLE();
741 }
742
743
VisitForInStatement(ForInStatement * stmt)744 void FastCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
745 UNREACHABLE();
746 }
747
748
VisitTryCatchStatement(TryCatchStatement * stmt)749 void FastCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
750 UNREACHABLE();
751 }
752
753
VisitTryFinallyStatement(TryFinallyStatement * stmt)754 void FastCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
755 UNREACHABLE();
756 }
757
758
VisitDebuggerStatement(DebuggerStatement * stmt)759 void FastCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
760 UNREACHABLE();
761 }
762
763
VisitFunctionLiteral(FunctionLiteral * expr)764 void FastCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
765 UNREACHABLE();
766 }
767
768
VisitFunctionBoilerplateLiteral(FunctionBoilerplateLiteral * expr)769 void FastCodeGenerator::VisitFunctionBoilerplateLiteral(
770 FunctionBoilerplateLiteral* expr) {
771 UNREACHABLE();
772 }
773
774
VisitConditional(Conditional * expr)775 void FastCodeGenerator::VisitConditional(Conditional* expr) {
776 UNREACHABLE();
777 }
778
779
VisitSlot(Slot * expr)780 void FastCodeGenerator::VisitSlot(Slot* expr) {
781 UNREACHABLE();
782 }
783
784
VisitVariableProxy(VariableProxy * expr)785 void FastCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
786 ASSERT(expr->var()->is_global() && !expr->var()->is_this());
787 // Check if we can compile a global variable load directly from the cell.
788 ASSERT(info()->has_global_object());
789 LookupResult lookup;
790 info()->global_object()->Lookup(*expr->name(), &lookup);
791 // We only support normal (non-accessor/interceptor) DontDelete properties
792 // for now.
793 ASSERT(lookup.IsProperty());
794 ASSERT_EQ(NORMAL, lookup.type());
795 ASSERT(lookup.IsDontDelete());
796 Handle<Object> cell(info()->global_object()->GetPropertyCell(&lookup));
797
798 // Global variable lookups do not have side effects, so we do not need to
799 // emit code if we are in an effect context.
800 if (!destination().is(no_reg)) {
801 Comment cmnt(masm(), ";; Global");
802 if (FLAG_print_ir) {
803 SmartPointer<char> name = expr->name()->ToCString();
804 PrintF("%d: t%d = Global(%s) // last_use = %d\n", expr->num(),
805 expr->num(), *name, expr->var_def()->last_use()->num());
806 }
807 EmitGlobalVariableLoad(cell);
808 }
809 }
810
811
VisitLiteral(Literal * expr)812 void FastCodeGenerator::VisitLiteral(Literal* expr) {
813 UNREACHABLE();
814 }
815
816
VisitRegExpLiteral(RegExpLiteral * expr)817 void FastCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
818 UNREACHABLE();
819 }
820
821
VisitObjectLiteral(ObjectLiteral * expr)822 void FastCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
823 UNREACHABLE();
824 }
825
826
VisitArrayLiteral(ArrayLiteral * expr)827 void FastCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
828 UNREACHABLE();
829 }
830
831
VisitCatchExtensionObject(CatchExtensionObject * expr)832 void FastCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
833 UNREACHABLE();
834 }
835
836
VisitAssignment(Assignment * expr)837 void FastCodeGenerator::VisitAssignment(Assignment* expr) {
838 // Known to be a simple this property assignment. Effectively a unary
839 // operation.
840 { Register my_destination = destination();
841 set_destination(accumulator0());
842 Visit(expr->value());
843 set_destination(my_destination);
844 }
845
846 Property* prop = expr->target()->AsProperty();
847 ASSERT_NOT_NULL(prop);
848 ASSERT_NOT_NULL(prop->obj()->AsVariableProxy());
849 ASSERT(prop->obj()->AsVariableProxy()->var()->is_this());
850 ASSERT(prop->key()->IsPropertyName());
851 Handle<String> name =
852 Handle<String>::cast(prop->key()->AsLiteral()->handle());
853
854 Comment cmnt(masm(), ";; Store to this");
855 if (FLAG_print_ir) {
856 SmartPointer<char> name_string = name->ToCString();
857 PrintF("%d: ", expr->num());
858 if (!destination().is(no_reg)) PrintF("t%d = ", expr->num());
859 PrintF("Store(this, \"%s\", t%d) // last_use(this) = %d\n", *name_string,
860 expr->value()->num(),
861 expr->var_def()->last_use()->num());
862 }
863
864 EmitThisPropertyStore(name);
865 }
866
867
VisitThrow(Throw * expr)868 void FastCodeGenerator::VisitThrow(Throw* expr) {
869 UNREACHABLE();
870 }
871
872
VisitProperty(Property * expr)873 void FastCodeGenerator::VisitProperty(Property* expr) {
874 ASSERT_NOT_NULL(expr->obj()->AsVariableProxy());
875 ASSERT(expr->obj()->AsVariableProxy()->var()->is_this());
876 ASSERT(expr->key()->IsPropertyName());
877 if (!destination().is(no_reg)) {
878 Handle<String> name =
879 Handle<String>::cast(expr->key()->AsLiteral()->handle());
880
881 Comment cmnt(masm(), ";; Load from this");
882 if (FLAG_print_ir) {
883 SmartPointer<char> name_string = name->ToCString();
884 PrintF("%d: t%d = Load(this, \"%s\") // last_use(this) = %d\n",
885 expr->num(), expr->num(), *name_string,
886 expr->var_def()->last_use()->num());
887 }
888 EmitThisPropertyLoad(name);
889 }
890 }
891
892
VisitCall(Call * expr)893 void FastCodeGenerator::VisitCall(Call* expr) {
894 UNREACHABLE();
895 }
896
897
VisitCallNew(CallNew * expr)898 void FastCodeGenerator::VisitCallNew(CallNew* expr) {
899 UNREACHABLE();
900 }
901
902
VisitCallRuntime(CallRuntime * expr)903 void FastCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
904 UNREACHABLE();
905 }
906
907
VisitUnaryOperation(UnaryOperation * expr)908 void FastCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
909 UNREACHABLE();
910 }
911
912
VisitCountOperation(CountOperation * expr)913 void FastCodeGenerator::VisitCountOperation(CountOperation* expr) {
914 UNREACHABLE();
915 }
916
917
VisitBinaryOperation(BinaryOperation * expr)918 void FastCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
919 // We support limited binary operations: bitwise OR only allowed to be
920 // nested on the left.
921 ASSERT(expr->op() == Token::BIT_OR);
922 ASSERT(expr->right()->IsLeaf());
923
924 { Register my_destination = destination();
925 set_destination(accumulator1());
926 Visit(expr->left());
927 set_destination(accumulator0());
928 Visit(expr->right());
929 set_destination(my_destination);
930 }
931
932 Comment cmnt(masm(), ";; BIT_OR");
933 if (FLAG_print_ir) {
934 PrintF("%d: ", expr->num());
935 if (!destination().is(no_reg)) PrintF("t%d = ", expr->num());
936 PrintF("BIT_OR(t%d, t%d)\n", expr->left()->num(), expr->right()->num());
937 }
938 EmitBitOr();
939 }
940
941
VisitCompareOperation(CompareOperation * expr)942 void FastCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
943 UNREACHABLE();
944 }
945
946
VisitThisFunction(ThisFunction * expr)947 void FastCodeGenerator::VisitThisFunction(ThisFunction* expr) {
948 UNREACHABLE();
949 }
950
951 #undef __
952
953
954 } } // namespace v8::internal
955