Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(928)

Side by Side Diff: src/ppc/full-codegen-ppc.cc

Issue 1248443003: Move Full-codegen into its own folder. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rename define Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ppc/deoptimizer-ppc.cc ('k') | src/runtime-profiler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_PPC
8
9 #include "src/code-factory.h"
10 #include "src/code-stubs.h"
11 #include "src/codegen.h"
12 #include "src/compiler.h"
13 #include "src/debug.h"
14 #include "src/full-codegen.h"
15 #include "src/ic/ic.h"
16 #include "src/parser.h"
17 #include "src/scopes.h"
18
19 #include "src/ppc/code-stubs-ppc.h"
20 #include "src/ppc/macro-assembler-ppc.h"
21
22 namespace v8 {
23 namespace internal {
24
25 #define __ ACCESS_MASM(masm_)
26
27 // A patch site is a location in the code which it is possible to patch. This
28 // class has a number of methods to emit the code which is patchable and the
29 // method EmitPatchInfo to record a marker back to the patchable code. This
30 // marker is a cmpi rx, #yyy instruction, and x * 0x0000ffff + yyy (raw 16 bit
31 // immediate value is used) is the delta from the pc to the first instruction of
32 // the patchable code.
33 // See PatchInlinedSmiCode in ic-ppc.cc for the code that patches it
34 class JumpPatchSite BASE_EMBEDDED {
35 public:
36 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
37 #ifdef DEBUG
38 info_emitted_ = false;
39 #endif
40 }
41
42 ~JumpPatchSite() { DCHECK(patch_site_.is_bound() == info_emitted_); }
43
44 // When initially emitting this ensure that a jump is always generated to skip
45 // the inlined smi code.
46 void EmitJumpIfNotSmi(Register reg, Label* target) {
47 DCHECK(!patch_site_.is_bound() && !info_emitted_);
48 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
49 __ bind(&patch_site_);
50 __ cmp(reg, reg, cr0);
51 __ beq(target, cr0); // Always taken before patched.
52 }
53
54 // When initially emitting this ensure that a jump is never generated to skip
55 // the inlined smi code.
56 void EmitJumpIfSmi(Register reg, Label* target) {
57 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
58 DCHECK(!patch_site_.is_bound() && !info_emitted_);
59 __ bind(&patch_site_);
60 __ cmp(reg, reg, cr0);
61 __ bne(target, cr0); // Never taken before patched.
62 }
63
64 void EmitPatchInfo() {
65 if (patch_site_.is_bound()) {
66 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
67 Register reg;
68 // I believe this is using reg as the high bits of of the offset
69 reg.set_code(delta_to_patch_site / kOff16Mask);
70 __ cmpi(reg, Operand(delta_to_patch_site % kOff16Mask));
71 #ifdef DEBUG
72 info_emitted_ = true;
73 #endif
74 } else {
75 __ nop(); // Signals no inlined code.
76 }
77 }
78
79 private:
80 MacroAssembler* masm_;
81 Label patch_site_;
82 #ifdef DEBUG
83 bool info_emitted_;
84 #endif
85 };
86
87
88 // Generate code for a JS function. On entry to the function the receiver
89 // and arguments have been pushed on the stack left to right. The actual
90 // argument count matches the formal parameter count expected by the
91 // function.
92 //
93 // The live registers are:
94 // o r4: the JS function object being called (i.e., ourselves)
95 // o cp: our context
96 // o fp: our caller's frame pointer (aka r31)
97 // o sp: stack pointer
98 // o lr: return address
99 // o ip: our own function entry (required by the prologue)
100 //
101 // The function builds a JS frame. Please see JavaScriptFrameConstants in
102 // frames-ppc.h for its layout.
103 void FullCodeGenerator::Generate() {
104 CompilationInfo* info = info_;
105 profiling_counter_ = isolate()->factory()->NewCell(
106 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
107 SetFunctionPosition(function());
108 Comment cmnt(masm_, "[ function compiled by full code generator");
109
110 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
111
112 #ifdef DEBUG
113 if (strlen(FLAG_stop_at) > 0 &&
114 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
115 __ stop("stop-at");
116 }
117 #endif
118
119 // Sloppy mode functions and builtins need to replace the receiver with the
120 // global proxy when called as functions (without an explicit receiver
121 // object).
122 if (is_sloppy(info->language_mode()) && !info->is_native() &&
123 info->MayUseThis() && info->scope()->has_this_declaration()) {
124 Label ok;
125 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
126 __ LoadP(r5, MemOperand(sp, receiver_offset), r0);
127 __ CompareRoot(r5, Heap::kUndefinedValueRootIndex);
128 __ bne(&ok);
129
130 __ LoadP(r5, GlobalObjectOperand());
131 __ LoadP(r5, FieldMemOperand(r5, GlobalObject::kGlobalProxyOffset));
132
133 __ StoreP(r5, MemOperand(sp, receiver_offset), r0);
134
135 __ bind(&ok);
136 }
137
138 // Open a frame scope to indicate that there is a frame on the stack. The
139 // MANUAL indicates that the scope shouldn't actually generate code to set up
140 // the frame (that is done below).
141 FrameScope frame_scope(masm_, StackFrame::MANUAL);
142 int prologue_offset = masm_->pc_offset();
143
144 if (prologue_offset) {
145 // Prologue logic requires it's starting address in ip and the
146 // corresponding offset from the function entry.
147 prologue_offset += Instruction::kInstrSize;
148 __ addi(ip, ip, Operand(prologue_offset));
149 }
150 info->set_prologue_offset(prologue_offset);
151 __ Prologue(info->IsCodePreAgingActive(), prologue_offset);
152 info->AddNoFrameRange(0, masm_->pc_offset());
153
154 {
155 Comment cmnt(masm_, "[ Allocate locals");
156 int locals_count = info->scope()->num_stack_slots();
157 // Generators allocate locals, if any, in context slots.
158 DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
159 if (locals_count > 0) {
160 if (locals_count >= 128) {
161 Label ok;
162 __ Add(ip, sp, -(locals_count * kPointerSize), r0);
163 __ LoadRoot(r5, Heap::kRealStackLimitRootIndex);
164 __ cmpl(ip, r5);
165 __ bc_short(ge, &ok);
166 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
167 __ bind(&ok);
168 }
169 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
170 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
171 if (locals_count >= kMaxPushes) {
172 int loop_iterations = locals_count / kMaxPushes;
173 __ mov(r5, Operand(loop_iterations));
174 __ mtctr(r5);
175 Label loop_header;
176 __ bind(&loop_header);
177 // Do pushes.
178 for (int i = 0; i < kMaxPushes; i++) {
179 __ push(ip);
180 }
181 // Continue loop if not done.
182 __ bdnz(&loop_header);
183 }
184 int remaining = locals_count % kMaxPushes;
185 // Emit the remaining pushes.
186 for (int i = 0; i < remaining; i++) {
187 __ push(ip);
188 }
189 }
190 }
191
192 bool function_in_register = true;
193
194 // Possibly allocate a local context.
195 if (info->scope()->num_heap_slots() > 0) {
196 // Argument to NewContext is the function, which is still in r4.
197 Comment cmnt(masm_, "[ Allocate context");
198 bool need_write_barrier = true;
199 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
200 if (info->scope()->is_script_scope()) {
201 __ push(r4);
202 __ Push(info->scope()->GetScopeInfo(info->isolate()));
203 __ CallRuntime(Runtime::kNewScriptContext, 2);
204 } else if (slots <= FastNewContextStub::kMaximumSlots) {
205 FastNewContextStub stub(isolate(), slots);
206 __ CallStub(&stub);
207 // Result of FastNewContextStub is always in new space.
208 need_write_barrier = false;
209 } else {
210 __ push(r4);
211 __ CallRuntime(Runtime::kNewFunctionContext, 1);
212 }
213 function_in_register = false;
214 // Context is returned in r3. It replaces the context passed to us.
215 // It's saved in the stack and kept live in cp.
216 __ mr(cp, r3);
217 __ StoreP(r3, MemOperand(fp, StandardFrameConstants::kContextOffset));
218 // Copy any necessary parameters into the context.
219 int num_parameters = info->scope()->num_parameters();
220 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
221 for (int i = first_parameter; i < num_parameters; i++) {
222 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
223 if (var->IsContextSlot()) {
224 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
225 (num_parameters - 1 - i) * kPointerSize;
226 // Load parameter from stack.
227 __ LoadP(r3, MemOperand(fp, parameter_offset), r0);
228 // Store it in the context.
229 MemOperand target = ContextOperand(cp, var->index());
230 __ StoreP(r3, target, r0);
231
232 // Update the write barrier.
233 if (need_write_barrier) {
234 __ RecordWriteContextSlot(cp, target.offset(), r3, r6,
235 kLRHasBeenSaved, kDontSaveFPRegs);
236 } else if (FLAG_debug_code) {
237 Label done;
238 __ JumpIfInNewSpace(cp, r3, &done);
239 __ Abort(kExpectedNewSpaceObject);
240 __ bind(&done);
241 }
242 }
243 }
244 }
245
246 // Possibly set up a local binding to the this function which is used in
247 // derived constructors with super calls.
248 Variable* this_function_var = scope()->this_function_var();
249 if (this_function_var != nullptr) {
250 Comment cmnt(masm_, "[ This function");
251 if (!function_in_register) {
252 __ LoadP(r4, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
253 // The write barrier clobbers register again, keep is marked as such.
254 }
255 SetVar(this_function_var, r4, r3, r5);
256 }
257
258 Variable* new_target_var = scope()->new_target_var();
259 if (new_target_var != nullptr) {
260 Comment cmnt(masm_, "[ new.target");
261
262 // Get the frame pointer for the calling frame.
263 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
264
265 // Skip the arguments adaptor frame if it exists.
266 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kContextOffset));
267 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
268 Label skip;
269 __ bne(&skip);
270 __ LoadP(r5, MemOperand(r5, StandardFrameConstants::kCallerFPOffset));
271 __ bind(&skip);
272
273 // Check the marker in the calling frame.
274 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kMarkerOffset));
275 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::CONSTRUCT), r0);
276 Label non_construct_frame, done;
277
278 __ bne(&non_construct_frame);
279 __ LoadP(r3, MemOperand(
280 r5, ConstructFrameConstants::kOriginalConstructorOffset));
281 __ b(&done);
282
283 __ bind(&non_construct_frame);
284 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
285 __ bind(&done);
286
287 SetVar(new_target_var, r3, r5, r6);
288 }
289
290 // Possibly allocate RestParameters
291 int rest_index;
292 Variable* rest_param = scope()->rest_parameter(&rest_index);
293 if (rest_param) {
294 Comment cmnt(masm_, "[ Allocate rest parameter array");
295
296 int num_parameters = info->scope()->num_parameters();
297 int offset = num_parameters * kPointerSize;
298
299 __ addi(r6, fp, Operand(StandardFrameConstants::kCallerSPOffset + offset));
300 __ LoadSmiLiteral(r5, Smi::FromInt(num_parameters));
301 __ LoadSmiLiteral(r4, Smi::FromInt(rest_index));
302 __ LoadSmiLiteral(r3, Smi::FromInt(language_mode()));
303 __ Push(r6, r5, r4, r3);
304
305 RestParamAccessStub stub(isolate());
306 __ CallStub(&stub);
307
308 SetVar(rest_param, r3, r4, r5);
309 }
310
311 Variable* arguments = scope()->arguments();
312 if (arguments != NULL) {
313 // Function uses arguments object.
314 Comment cmnt(masm_, "[ Allocate arguments object");
315 if (!function_in_register) {
316 // Load this again, if it's used by the local context below.
317 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
318 } else {
319 __ mr(r6, r4);
320 }
321 // Receiver is just before the parameters on the caller's stack.
322 int num_parameters = info->scope()->num_parameters();
323 int offset = num_parameters * kPointerSize;
324 __ addi(r5, fp, Operand(StandardFrameConstants::kCallerSPOffset + offset));
325 __ LoadSmiLiteral(r4, Smi::FromInt(num_parameters));
326 __ Push(r6, r5, r4);
327
328 // Arguments to ArgumentsAccessStub:
329 // function, receiver address, parameter count.
330 // The stub will rewrite receiever and parameter count if the previous
331 // stack frame was an arguments adapter frame.
332 ArgumentsAccessStub::Type type;
333 if (is_strict(language_mode()) || !is_simple_parameter_list()) {
334 type = ArgumentsAccessStub::NEW_STRICT;
335 } else if (function()->has_duplicate_parameters()) {
336 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
337 } else {
338 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
339 }
340 ArgumentsAccessStub stub(isolate(), type);
341 __ CallStub(&stub);
342
343 SetVar(arguments, r3, r4, r5);
344 }
345
346 if (FLAG_trace) {
347 __ CallRuntime(Runtime::kTraceEnter, 0);
348 }
349
350 // Visit the declarations and body unless there is an illegal
351 // redeclaration.
352 if (scope()->HasIllegalRedeclaration()) {
353 Comment cmnt(masm_, "[ Declarations");
354 scope()->VisitIllegalRedeclaration(this);
355
356 } else {
357 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
358 {
359 Comment cmnt(masm_, "[ Declarations");
360 VisitDeclarations(scope()->declarations());
361 }
362
363 {
364 Comment cmnt(masm_, "[ Stack check");
365 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
366 Label ok;
367 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
368 __ cmpl(sp, ip);
369 __ bc_short(ge, &ok);
370 __ Call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
371 __ bind(&ok);
372 }
373
374 {
375 Comment cmnt(masm_, "[ Body");
376 DCHECK(loop_depth() == 0);
377 VisitStatements(function()->body());
378 DCHECK(loop_depth() == 0);
379 }
380 }
381
382 // Always emit a 'return undefined' in case control fell off the end of
383 // the body.
384 {
385 Comment cmnt(masm_, "[ return <undefined>;");
386 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
387 }
388 EmitReturnSequence();
389
390 if (HasStackOverflow()) {
391 masm_->AbortConstantPoolBuilding();
392 }
393 }
394
395
396 void FullCodeGenerator::ClearAccumulator() {
397 __ LoadSmiLiteral(r3, Smi::FromInt(0));
398 }
399
400
401 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
402 __ mov(r5, Operand(profiling_counter_));
403 __ LoadP(r6, FieldMemOperand(r5, Cell::kValueOffset));
404 __ SubSmiLiteral(r6, r6, Smi::FromInt(delta), r0);
405 __ StoreP(r6, FieldMemOperand(r5, Cell::kValueOffset), r0);
406 }
407
408
409 void FullCodeGenerator::EmitProfilingCounterReset() {
410 int reset_value = FLAG_interrupt_budget;
411 if (info_->is_debug()) {
412 // Detect debug break requests as soon as possible.
413 reset_value = FLAG_interrupt_budget >> 4;
414 }
415 __ mov(r5, Operand(profiling_counter_));
416 __ LoadSmiLiteral(r6, Smi::FromInt(reset_value));
417 __ StoreP(r6, FieldMemOperand(r5, Cell::kValueOffset), r0);
418 }
419
420
421 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
422 Label* back_edge_target) {
423 Comment cmnt(masm_, "[ Back edge bookkeeping");
424 Label ok;
425
426 DCHECK(back_edge_target->is_bound());
427 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target) +
428 kCodeSizeMultiplier / 2;
429 int weight = Min(kMaxBackEdgeWeight, Max(1, distance / kCodeSizeMultiplier));
430 EmitProfilingCounterDecrement(weight);
431 {
432 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
433 Assembler::BlockConstantPoolEntrySharingScope prevent_entry_sharing(masm_);
434 // BackEdgeTable::PatchAt manipulates this sequence.
435 __ cmpi(r6, Operand::Zero());
436 __ bc_short(ge, &ok);
437 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
438
439 // Record a mapping of this PC offset to the OSR id. This is used to find
440 // the AST id from the unoptimized code in order to use it as a key into
441 // the deoptimization input data found in the optimized code.
442 RecordBackEdge(stmt->OsrEntryId());
443 }
444 EmitProfilingCounterReset();
445
446 __ bind(&ok);
447 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
448 // Record a mapping of the OSR id to this PC. This is used if the OSR
449 // entry becomes the target of a bailout. We don't expect it to be, but
450 // we want it to work if it is.
451 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
452 }
453
454
455 void FullCodeGenerator::EmitReturnSequence() {
456 Comment cmnt(masm_, "[ Return sequence");
457 if (return_label_.is_bound()) {
458 __ b(&return_label_);
459 } else {
460 __ bind(&return_label_);
461 if (FLAG_trace) {
462 // Push the return value on the stack as the parameter.
463 // Runtime::TraceExit returns its parameter in r3
464 __ push(r3);
465 __ CallRuntime(Runtime::kTraceExit, 1);
466 }
467 // Pretend that the exit is a backwards jump to the entry.
468 int weight = 1;
469 if (info_->ShouldSelfOptimize()) {
470 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
471 } else {
472 int distance = masm_->pc_offset() + kCodeSizeMultiplier / 2;
473 weight = Min(kMaxBackEdgeWeight, Max(1, distance / kCodeSizeMultiplier));
474 }
475 EmitProfilingCounterDecrement(weight);
476 Label ok;
477 __ cmpi(r6, Operand::Zero());
478 __ bge(&ok);
479 __ push(r3);
480 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
481 __ pop(r3);
482 EmitProfilingCounterReset();
483 __ bind(&ok);
484
485 // Make sure that the constant pool is not emitted inside of the return
486 // sequence.
487 {
488 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
489 int32_t arg_count = info_->scope()->num_parameters() + 1;
490 int32_t sp_delta = arg_count * kPointerSize;
491 SetReturnPosition(function());
492 int no_frame_start = __ LeaveFrame(StackFrame::JAVA_SCRIPT, sp_delta);
493 __ blr();
494 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
495 }
496 }
497 }
498
499
500 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
501 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
502 codegen()->GetVar(result_register(), var);
503 __ push(result_register());
504 }
505
506
507 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {}
508
509
510 void FullCodeGenerator::AccumulatorValueContext::Plug(
511 Heap::RootListIndex index) const {
512 __ LoadRoot(result_register(), index);
513 }
514
515
516 void FullCodeGenerator::StackValueContext::Plug(
517 Heap::RootListIndex index) const {
518 __ LoadRoot(result_register(), index);
519 __ push(result_register());
520 }
521
522
523 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
524 codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
525 false_label_);
526 if (index == Heap::kUndefinedValueRootIndex ||
527 index == Heap::kNullValueRootIndex ||
528 index == Heap::kFalseValueRootIndex) {
529 if (false_label_ != fall_through_) __ b(false_label_);
530 } else if (index == Heap::kTrueValueRootIndex) {
531 if (true_label_ != fall_through_) __ b(true_label_);
532 } else {
533 __ LoadRoot(result_register(), index);
534 codegen()->DoTest(this);
535 }
536 }
537
538
539 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {}
540
541
542 void FullCodeGenerator::AccumulatorValueContext::Plug(
543 Handle<Object> lit) const {
544 __ mov(result_register(), Operand(lit));
545 }
546
547
548 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
549 // Immediates cannot be pushed directly.
550 __ mov(result_register(), Operand(lit));
551 __ push(result_register());
552 }
553
554
555 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
556 codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
557 false_label_);
558 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
559 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
560 if (false_label_ != fall_through_) __ b(false_label_);
561 } else if (lit->IsTrue() || lit->IsJSObject()) {
562 if (true_label_ != fall_through_) __ b(true_label_);
563 } else if (lit->IsString()) {
564 if (String::cast(*lit)->length() == 0) {
565 if (false_label_ != fall_through_) __ b(false_label_);
566 } else {
567 if (true_label_ != fall_through_) __ b(true_label_);
568 }
569 } else if (lit->IsSmi()) {
570 if (Smi::cast(*lit)->value() == 0) {
571 if (false_label_ != fall_through_) __ b(false_label_);
572 } else {
573 if (true_label_ != fall_through_) __ b(true_label_);
574 }
575 } else {
576 // For simplicity we always test the accumulator register.
577 __ mov(result_register(), Operand(lit));
578 codegen()->DoTest(this);
579 }
580 }
581
582
583 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
584 Register reg) const {
585 DCHECK(count > 0);
586 __ Drop(count);
587 }
588
589
590 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
591 int count, Register reg) const {
592 DCHECK(count > 0);
593 __ Drop(count);
594 __ Move(result_register(), reg);
595 }
596
597
598 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
599 Register reg) const {
600 DCHECK(count > 0);
601 if (count > 1) __ Drop(count - 1);
602 __ StoreP(reg, MemOperand(sp, 0));
603 }
604
605
606 void FullCodeGenerator::TestContext::DropAndPlug(int count,
607 Register reg) const {
608 DCHECK(count > 0);
609 // For simplicity we always test the accumulator register.
610 __ Drop(count);
611 __ Move(result_register(), reg);
612 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
613 codegen()->DoTest(this);
614 }
615
616
617 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
618 Label* materialize_false) const {
619 DCHECK(materialize_true == materialize_false);
620 __ bind(materialize_true);
621 }
622
623
624 void FullCodeGenerator::AccumulatorValueContext::Plug(
625 Label* materialize_true, Label* materialize_false) const {
626 Label done;
627 __ bind(materialize_true);
628 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
629 __ b(&done);
630 __ bind(materialize_false);
631 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
632 __ bind(&done);
633 }
634
635
636 void FullCodeGenerator::StackValueContext::Plug(
637 Label* materialize_true, Label* materialize_false) const {
638 Label done;
639 __ bind(materialize_true);
640 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
641 __ b(&done);
642 __ bind(materialize_false);
643 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
644 __ bind(&done);
645 __ push(ip);
646 }
647
648
649 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
650 Label* materialize_false) const {
651 DCHECK(materialize_true == true_label_);
652 DCHECK(materialize_false == false_label_);
653 }
654
655
656 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
657 Heap::RootListIndex value_root_index =
658 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
659 __ LoadRoot(result_register(), value_root_index);
660 }
661
662
663 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
664 Heap::RootListIndex value_root_index =
665 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
666 __ LoadRoot(ip, value_root_index);
667 __ push(ip);
668 }
669
670
671 void FullCodeGenerator::TestContext::Plug(bool flag) const {
672 codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
673 false_label_);
674 if (flag) {
675 if (true_label_ != fall_through_) __ b(true_label_);
676 } else {
677 if (false_label_ != fall_through_) __ b(false_label_);
678 }
679 }
680
681
682 void FullCodeGenerator::DoTest(Expression* condition, Label* if_true,
683 Label* if_false, Label* fall_through) {
684 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
685 CallIC(ic, condition->test_id());
686 __ cmpi(result_register(), Operand::Zero());
687 Split(ne, if_true, if_false, fall_through);
688 }
689
690
691 void FullCodeGenerator::Split(Condition cond, Label* if_true, Label* if_false,
692 Label* fall_through, CRegister cr) {
693 if (if_false == fall_through) {
694 __ b(cond, if_true, cr);
695 } else if (if_true == fall_through) {
696 __ b(NegateCondition(cond), if_false, cr);
697 } else {
698 __ b(cond, if_true, cr);
699 __ b(if_false);
700 }
701 }
702
703
704 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
705 DCHECK(var->IsStackAllocated());
706 // Offset is negative because higher indexes are at lower addresses.
707 int offset = -var->index() * kPointerSize;
708 // Adjust by a (parameter or local) base offset.
709 if (var->IsParameter()) {
710 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
711 } else {
712 offset += JavaScriptFrameConstants::kLocal0Offset;
713 }
714 return MemOperand(fp, offset);
715 }
716
717
718 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
719 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
720 if (var->IsContextSlot()) {
721 int context_chain_length = scope()->ContextChainLength(var->scope());
722 __ LoadContext(scratch, context_chain_length);
723 return ContextOperand(scratch, var->index());
724 } else {
725 return StackOperand(var);
726 }
727 }
728
729
730 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
731 // Use destination as scratch.
732 MemOperand location = VarOperand(var, dest);
733 __ LoadP(dest, location, r0);
734 }
735
736
737 void FullCodeGenerator::SetVar(Variable* var, Register src, Register scratch0,
738 Register scratch1) {
739 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
740 DCHECK(!scratch0.is(src));
741 DCHECK(!scratch0.is(scratch1));
742 DCHECK(!scratch1.is(src));
743 MemOperand location = VarOperand(var, scratch0);
744 __ StoreP(src, location, r0);
745
746 // Emit the write barrier code if the location is in the heap.
747 if (var->IsContextSlot()) {
748 __ RecordWriteContextSlot(scratch0, location.offset(), src, scratch1,
749 kLRHasBeenSaved, kDontSaveFPRegs);
750 }
751 }
752
753
754 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
755 bool should_normalize,
756 Label* if_true,
757 Label* if_false) {
758 // Only prepare for bailouts before splits if we're in a test
759 // context. Otherwise, we let the Visit function deal with the
760 // preparation to avoid preparing with the same AST id twice.
761 if (!context()->IsTest() || !info_->IsOptimizable()) return;
762
763 Label skip;
764 if (should_normalize) __ b(&skip);
765 PrepareForBailout(expr, TOS_REG);
766 if (should_normalize) {
767 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
768 __ cmp(r3, ip);
769 Split(eq, if_true, if_false, NULL);
770 __ bind(&skip);
771 }
772 }
773
774
775 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
776 // The variable in the declaration always resides in the current function
777 // context.
778 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
779 if (generate_debug_code_) {
780 // Check that we're not inside a with or catch context.
781 __ LoadP(r4, FieldMemOperand(cp, HeapObject::kMapOffset));
782 __ CompareRoot(r4, Heap::kWithContextMapRootIndex);
783 __ Check(ne, kDeclarationInWithContext);
784 __ CompareRoot(r4, Heap::kCatchContextMapRootIndex);
785 __ Check(ne, kDeclarationInCatchContext);
786 }
787 }
788
789
790 void FullCodeGenerator::VisitVariableDeclaration(
791 VariableDeclaration* declaration) {
792 // If it was not possible to allocate the variable at compile time, we
793 // need to "declare" it at runtime to make sure it actually exists in the
794 // local context.
795 VariableProxy* proxy = declaration->proxy();
796 VariableMode mode = declaration->mode();
797 Variable* variable = proxy->var();
798 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
799 switch (variable->location()) {
800 case VariableLocation::GLOBAL:
801 case VariableLocation::UNALLOCATED:
802 globals_->Add(variable->name(), zone());
803 globals_->Add(variable->binding_needs_init()
804 ? isolate()->factory()->the_hole_value()
805 : isolate()->factory()->undefined_value(),
806 zone());
807 break;
808
809 case VariableLocation::PARAMETER:
810 case VariableLocation::LOCAL:
811 if (hole_init) {
812 Comment cmnt(masm_, "[ VariableDeclaration");
813 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
814 __ StoreP(ip, StackOperand(variable));
815 }
816 break;
817
818 case VariableLocation::CONTEXT:
819 if (hole_init) {
820 Comment cmnt(masm_, "[ VariableDeclaration");
821 EmitDebugCheckDeclarationContext(variable);
822 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
823 __ StoreP(ip, ContextOperand(cp, variable->index()), r0);
824 // No write barrier since the_hole_value is in old space.
825 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
826 }
827 break;
828
829 case VariableLocation::LOOKUP: {
830 Comment cmnt(masm_, "[ VariableDeclaration");
831 __ mov(r5, Operand(variable->name()));
832 // Declaration nodes are always introduced in one of four modes.
833 DCHECK(IsDeclaredVariableMode(mode));
834 PropertyAttributes attr =
835 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
836 __ LoadSmiLiteral(r4, Smi::FromInt(attr));
837 // Push initial value, if any.
838 // Note: For variables we must not push an initial value (such as
839 // 'undefined') because we may have a (legal) redeclaration and we
840 // must not destroy the current value.
841 if (hole_init) {
842 __ LoadRoot(r3, Heap::kTheHoleValueRootIndex);
843 __ Push(cp, r5, r4, r3);
844 } else {
845 __ LoadSmiLiteral(r3, Smi::FromInt(0)); // Indicates no initial value.
846 __ Push(cp, r5, r4, r3);
847 }
848 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
849 break;
850 }
851 }
852 }
853
854
855 void FullCodeGenerator::VisitFunctionDeclaration(
856 FunctionDeclaration* declaration) {
857 VariableProxy* proxy = declaration->proxy();
858 Variable* variable = proxy->var();
859 switch (variable->location()) {
860 case VariableLocation::GLOBAL:
861 case VariableLocation::UNALLOCATED: {
862 globals_->Add(variable->name(), zone());
863 Handle<SharedFunctionInfo> function =
864 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
865 // Check for stack-overflow exception.
866 if (function.is_null()) return SetStackOverflow();
867 globals_->Add(function, zone());
868 break;
869 }
870
871 case VariableLocation::PARAMETER:
872 case VariableLocation::LOCAL: {
873 Comment cmnt(masm_, "[ FunctionDeclaration");
874 VisitForAccumulatorValue(declaration->fun());
875 __ StoreP(result_register(), StackOperand(variable));
876 break;
877 }
878
879 case VariableLocation::CONTEXT: {
880 Comment cmnt(masm_, "[ FunctionDeclaration");
881 EmitDebugCheckDeclarationContext(variable);
882 VisitForAccumulatorValue(declaration->fun());
883 __ StoreP(result_register(), ContextOperand(cp, variable->index()), r0);
884 int offset = Context::SlotOffset(variable->index());
885 // We know that we have written a function, which is not a smi.
886 __ RecordWriteContextSlot(cp, offset, result_register(), r5,
887 kLRHasBeenSaved, kDontSaveFPRegs,
888 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
889 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
890 break;
891 }
892
893 case VariableLocation::LOOKUP: {
894 Comment cmnt(masm_, "[ FunctionDeclaration");
895 __ mov(r5, Operand(variable->name()));
896 __ LoadSmiLiteral(r4, Smi::FromInt(NONE));
897 __ Push(cp, r5, r4);
898 // Push initial value for function declaration.
899 VisitForStackValue(declaration->fun());
900 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
901 break;
902 }
903 }
904 }
905
906
907 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
908 // Call the runtime to declare the globals.
909 // The context is the first argument.
910 __ mov(r4, Operand(pairs));
911 __ LoadSmiLiteral(r3, Smi::FromInt(DeclareGlobalsFlags()));
912 __ Push(cp, r4, r3);
913 __ CallRuntime(Runtime::kDeclareGlobals, 3);
914 // Return value is ignored.
915 }
916
917
918 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
919 // Call the runtime to declare the modules.
920 __ Push(descriptions);
921 __ CallRuntime(Runtime::kDeclareModules, 1);
922 // Return value is ignored.
923 }
924
925
926 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
927 Comment cmnt(masm_, "[ SwitchStatement");
928 Breakable nested_statement(this, stmt);
929 SetStatementPosition(stmt);
930
931 // Keep the switch value on the stack until a case matches.
932 VisitForStackValue(stmt->tag());
933 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
934
935 ZoneList<CaseClause*>* clauses = stmt->cases();
936 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
937
938 Label next_test; // Recycled for each test.
939 // Compile all the tests with branches to their bodies.
940 for (int i = 0; i < clauses->length(); i++) {
941 CaseClause* clause = clauses->at(i);
942 clause->body_target()->Unuse();
943
944 // The default is not a test, but remember it as final fall through.
945 if (clause->is_default()) {
946 default_clause = clause;
947 continue;
948 }
949
950 Comment cmnt(masm_, "[ Case comparison");
951 __ bind(&next_test);
952 next_test.Unuse();
953
954 // Compile the label expression.
955 VisitForAccumulatorValue(clause->label());
956
957 // Perform the comparison as if via '==='.
958 __ LoadP(r4, MemOperand(sp, 0)); // Switch value.
959 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
960 JumpPatchSite patch_site(masm_);
961 if (inline_smi_code) {
962 Label slow_case;
963 __ orx(r5, r4, r3);
964 patch_site.EmitJumpIfNotSmi(r5, &slow_case);
965
966 __ cmp(r4, r3);
967 __ bne(&next_test);
968 __ Drop(1); // Switch value is no longer needed.
969 __ b(clause->body_target());
970 __ bind(&slow_case);
971 }
972
973 // Record position before stub call for type feedback.
974 SetExpressionPosition(clause);
975 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
976 strength(language_mode())).code();
977 CallIC(ic, clause->CompareId());
978 patch_site.EmitPatchInfo();
979
980 Label skip;
981 __ b(&skip);
982 PrepareForBailout(clause, TOS_REG);
983 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
984 __ cmp(r3, ip);
985 __ bne(&next_test);
986 __ Drop(1);
987 __ b(clause->body_target());
988 __ bind(&skip);
989
990 __ cmpi(r3, Operand::Zero());
991 __ bne(&next_test);
992 __ Drop(1); // Switch value is no longer needed.
993 __ b(clause->body_target());
994 }
995
996 // Discard the test value and jump to the default if present, otherwise to
997 // the end of the statement.
998 __ bind(&next_test);
999 __ Drop(1); // Switch value is no longer needed.
1000 if (default_clause == NULL) {
1001 __ b(nested_statement.break_label());
1002 } else {
1003 __ b(default_clause->body_target());
1004 }
1005
1006 // Compile all the case bodies.
1007 for (int i = 0; i < clauses->length(); i++) {
1008 Comment cmnt(masm_, "[ Case body");
1009 CaseClause* clause = clauses->at(i);
1010 __ bind(clause->body_target());
1011 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1012 VisitStatements(clause->statements());
1013 }
1014
1015 __ bind(nested_statement.break_label());
1016 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1017 }
1018
1019
1020 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1021 Comment cmnt(masm_, "[ ForInStatement");
1022 SetStatementPosition(stmt, SKIP_BREAK);
1023
1024 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1025
1026 Label loop, exit;
1027 ForIn loop_statement(this, stmt);
1028 increment_loop_depth();
1029
1030 // Get the object to enumerate over. If the object is null or undefined, skip
1031 // over the loop. See ECMA-262 version 5, section 12.6.4.
1032 SetExpressionAsStatementPosition(stmt->enumerable());
1033 VisitForAccumulatorValue(stmt->enumerable());
1034 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1035 __ cmp(r3, ip);
1036 __ beq(&exit);
1037 Register null_value = r7;
1038 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1039 __ cmp(r3, null_value);
1040 __ beq(&exit);
1041
1042 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1043
1044 // Convert the object to a JS object.
1045 Label convert, done_convert;
1046 __ JumpIfSmi(r3, &convert);
1047 __ CompareObjectType(r3, r4, r4, FIRST_SPEC_OBJECT_TYPE);
1048 __ bge(&done_convert);
1049 __ bind(&convert);
1050 __ push(r3);
1051 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1052 __ bind(&done_convert);
1053 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1054 __ push(r3);
1055
1056 // Check for proxies.
1057 Label call_runtime;
1058 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1059 __ CompareObjectType(r3, r4, r4, LAST_JS_PROXY_TYPE);
1060 __ ble(&call_runtime);
1061
1062 // Check cache validity in generated code. This is a fast case for
1063 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1064 // guarantee cache validity, call the runtime system to check cache
1065 // validity or get the property names in a fixed array.
1066 __ CheckEnumCache(null_value, &call_runtime);
1067
1068 // The enum cache is valid. Load the map of the object being
1069 // iterated over and use the cache for the iteration.
1070 Label use_cache;
1071 __ LoadP(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
1072 __ b(&use_cache);
1073
1074 // Get the set of properties to enumerate.
1075 __ bind(&call_runtime);
1076 __ push(r3); // Duplicate the enumerable object on the stack.
1077 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1078 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1079
1080 // If we got a map from the runtime call, we can do a fast
1081 // modification check. Otherwise, we got a fixed array, and we have
1082 // to do a slow check.
1083 Label fixed_array;
1084 __ LoadP(r5, FieldMemOperand(r3, HeapObject::kMapOffset));
1085 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1086 __ cmp(r5, ip);
1087 __ bne(&fixed_array);
1088
1089 // We got a map in register r3. Get the enumeration cache from it.
1090 Label no_descriptors;
1091 __ bind(&use_cache);
1092
1093 __ EnumLength(r4, r3);
1094 __ CmpSmiLiteral(r4, Smi::FromInt(0), r0);
1095 __ beq(&no_descriptors);
1096
1097 __ LoadInstanceDescriptors(r3, r5);
1098 __ LoadP(r5, FieldMemOperand(r5, DescriptorArray::kEnumCacheOffset));
1099 __ LoadP(r5,
1100 FieldMemOperand(r5, DescriptorArray::kEnumCacheBridgeCacheOffset));
1101
1102 // Set up the four remaining stack slots.
1103 __ push(r3); // Map.
1104 __ LoadSmiLiteral(r3, Smi::FromInt(0));
1105 // Push enumeration cache, enumeration cache length (as smi) and zero.
1106 __ Push(r5, r4, r3);
1107 __ b(&loop);
1108
1109 __ bind(&no_descriptors);
1110 __ Drop(1);
1111 __ b(&exit);
1112
1113 // We got a fixed array in register r3. Iterate through that.
1114 Label non_proxy;
1115 __ bind(&fixed_array);
1116
1117 __ Move(r4, FeedbackVector());
1118 __ mov(r5, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1119 int vector_index = FeedbackVector()->GetIndex(slot);
1120 __ StoreP(
1121 r5, FieldMemOperand(r4, FixedArray::OffsetOfElementAt(vector_index)), r0);
1122
1123 __ LoadSmiLiteral(r4, Smi::FromInt(1)); // Smi indicates slow check
1124 __ LoadP(r5, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1125 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1126 __ CompareObjectType(r5, r6, r6, LAST_JS_PROXY_TYPE);
1127 __ bgt(&non_proxy);
1128 __ LoadSmiLiteral(r4, Smi::FromInt(0)); // Zero indicates proxy
1129 __ bind(&non_proxy);
1130 __ Push(r4, r3); // Smi and array
1131 __ LoadP(r4, FieldMemOperand(r3, FixedArray::kLengthOffset));
1132 __ LoadSmiLiteral(r3, Smi::FromInt(0));
1133 __ Push(r4, r3); // Fixed array length (as smi) and initial index.
1134
1135 // Generate code for doing the condition check.
1136 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1137 __ bind(&loop);
1138 SetExpressionAsStatementPosition(stmt->each());
1139
1140 // Load the current count to r3, load the length to r4.
1141 __ LoadP(r3, MemOperand(sp, 0 * kPointerSize));
1142 __ LoadP(r4, MemOperand(sp, 1 * kPointerSize));
1143 __ cmpl(r3, r4); // Compare to the array length.
1144 __ bge(loop_statement.break_label());
1145
1146 // Get the current entry of the array into register r6.
1147 __ LoadP(r5, MemOperand(sp, 2 * kPointerSize));
1148 __ addi(r5, r5, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1149 __ SmiToPtrArrayOffset(r6, r3);
1150 __ LoadPX(r6, MemOperand(r6, r5));
1151
1152 // Get the expected map from the stack or a smi in the
1153 // permanent slow case into register r5.
1154 __ LoadP(r5, MemOperand(sp, 3 * kPointerSize));
1155
1156 // Check if the expected map still matches that of the enumerable.
1157 // If not, we may have to filter the key.
1158 Label update_each;
1159 __ LoadP(r4, MemOperand(sp, 4 * kPointerSize));
1160 __ LoadP(r7, FieldMemOperand(r4, HeapObject::kMapOffset));
1161 __ cmp(r7, r5);
1162 __ beq(&update_each);
1163
1164 // For proxies, no filtering is done.
1165 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1166 __ CmpSmiLiteral(r5, Smi::FromInt(0), r0);
1167 __ beq(&update_each);
1168
1169 // Convert the entry to a string or (smi) 0 if it isn't a property
1170 // any more. If the property has been removed while iterating, we
1171 // just skip it.
1172 __ Push(r4, r6); // Enumerable and current entry.
1173 __ CallRuntime(Runtime::kForInFilter, 2);
1174 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1175 __ mr(r6, r3);
1176 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1177 __ cmp(r3, r0);
1178 __ beq(loop_statement.continue_label());
1179
1180 // Update the 'each' property or variable from the possibly filtered
1181 // entry in register r6.
1182 __ bind(&update_each);
1183 __ mr(result_register(), r6);
1184 // Perform the assignment as if via '='.
1185 {
1186 EffectContext context(this);
1187 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1188 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1189 }
1190
1191 // Generate code for the body of the loop.
1192 Visit(stmt->body());
1193
1194 // Generate code for the going to the next element by incrementing
1195 // the index (smi) stored on top of the stack.
1196 __ bind(loop_statement.continue_label());
1197 __ pop(r3);
1198 __ AddSmiLiteral(r3, r3, Smi::FromInt(1), r0);
1199 __ push(r3);
1200
1201 EmitBackEdgeBookkeeping(stmt, &loop);
1202 __ b(&loop);
1203
1204 // Remove the pointers stored on the stack.
1205 __ bind(loop_statement.break_label());
1206 __ Drop(5);
1207
1208 // Exit and decrement the loop depth.
1209 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1210 __ bind(&exit);
1211 decrement_loop_depth();
1212 }
1213
1214
1215 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1216 bool pretenure) {
1217 // Use the fast case closure allocation code that allocates in new
1218 // space for nested functions that don't need literals cloning. If
1219 // we're running with the --always-opt or the --prepare-always-opt
1220 // flag, we need to use the runtime function so that the new function
1221 // we are creating here gets a chance to have its code optimized and
1222 // doesn't just get a copy of the existing unoptimized code.
1223 if (!FLAG_always_opt && !FLAG_prepare_always_opt && !pretenure &&
1224 scope()->is_function_scope() && info->num_literals() == 0) {
1225 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1226 __ mov(r5, Operand(info));
1227 __ CallStub(&stub);
1228 } else {
1229 __ mov(r3, Operand(info));
1230 __ LoadRoot(
1231 r4, pretenure ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
1232 __ Push(cp, r3, r4);
1233 __ CallRuntime(Runtime::kNewClosure, 3);
1234 }
1235 context()->Plug(r3);
1236 }
1237
1238
1239 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1240 int offset,
1241 FeedbackVectorICSlot slot) {
1242 if (NeedsHomeObject(initializer)) {
1243 __ LoadP(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1244 __ mov(StoreDescriptor::NameRegister(),
1245 Operand(isolate()->factory()->home_object_symbol()));
1246 __ LoadP(StoreDescriptor::ValueRegister(),
1247 MemOperand(sp, offset * kPointerSize));
1248 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1249 CallStoreIC();
1250 }
1251 }
1252
1253
1254 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1255 TypeofMode typeof_mode,
1256 Label* slow) {
1257 Register current = cp;
1258 Register next = r4;
1259 Register temp = r5;
1260
1261 Scope* s = scope();
1262 while (s != NULL) {
1263 if (s->num_heap_slots() > 0) {
1264 if (s->calls_sloppy_eval()) {
1265 // Check that extension is NULL.
1266 __ LoadP(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1267 __ cmpi(temp, Operand::Zero());
1268 __ bne(slow);
1269 }
1270 // Load next context in chain.
1271 __ LoadP(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1272 // Walk the rest of the chain without clobbering cp.
1273 current = next;
1274 }
1275 // If no outer scope calls eval, we do not need to check more
1276 // context extensions.
1277 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1278 s = s->outer_scope();
1279 }
1280
1281 if (s->is_eval_scope()) {
1282 Label loop, fast;
1283 if (!current.is(next)) {
1284 __ Move(next, current);
1285 }
1286 __ bind(&loop);
1287 // Terminate at native context.
1288 __ LoadP(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1289 __ LoadRoot(ip, Heap::kNativeContextMapRootIndex);
1290 __ cmp(temp, ip);
1291 __ beq(&fast);
1292 // Check that extension is NULL.
1293 __ LoadP(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1294 __ cmpi(temp, Operand::Zero());
1295 __ bne(slow);
1296 // Load next context in chain.
1297 __ LoadP(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1298 __ b(&loop);
1299 __ bind(&fast);
1300 }
1301
1302 // All extension objects were empty and it is safe to use a normal global
1303 // load machinery.
1304 EmitGlobalVariableLoad(proxy, typeof_mode);
1305 }
1306
1307
1308 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1309 Label* slow) {
1310 DCHECK(var->IsContextSlot());
1311 Register context = cp;
1312 Register next = r6;
1313 Register temp = r7;
1314
1315 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1316 if (s->num_heap_slots() > 0) {
1317 if (s->calls_sloppy_eval()) {
1318 // Check that extension is NULL.
1319 __ LoadP(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1320 __ cmpi(temp, Operand::Zero());
1321 __ bne(slow);
1322 }
1323 __ LoadP(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1324 // Walk the rest of the chain without clobbering cp.
1325 context = next;
1326 }
1327 }
1328 // Check that last extension is NULL.
1329 __ LoadP(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1330 __ cmpi(temp, Operand::Zero());
1331 __ bne(slow);
1332
1333 // This function is used only for loads, not stores, so it's safe to
1334 // return an cp-based operand (the write barrier cannot be allowed to
1335 // destroy the cp register).
1336 return ContextOperand(context, var->index());
1337 }
1338
1339
1340 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1341 TypeofMode typeof_mode,
1342 Label* slow, Label* done) {
1343 // Generate fast-case code for variables that might be shadowed by
1344 // eval-introduced variables. Eval is used a lot without
1345 // introducing variables. In those cases, we do not want to
1346 // perform a runtime call for all variables in the scope
1347 // containing the eval.
1348 Variable* var = proxy->var();
1349 if (var->mode() == DYNAMIC_GLOBAL) {
1350 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1351 __ b(done);
1352 } else if (var->mode() == DYNAMIC_LOCAL) {
1353 Variable* local = var->local_if_not_shadowed();
1354 __ LoadP(r3, ContextSlotOperandCheckExtensions(local, slow));
1355 if (local->mode() == LET || local->mode() == CONST ||
1356 local->mode() == CONST_LEGACY) {
1357 __ CompareRoot(r3, Heap::kTheHoleValueRootIndex);
1358 __ bne(done);
1359 if (local->mode() == CONST_LEGACY) {
1360 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1361 } else { // LET || CONST
1362 __ mov(r3, Operand(var->name()));
1363 __ push(r3);
1364 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1365 }
1366 }
1367 __ b(done);
1368 }
1369 }
1370
1371
1372 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1373 TypeofMode typeof_mode) {
1374 Variable* var = proxy->var();
1375 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1376 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1377 if (var->IsGlobalSlot()) {
1378 DCHECK(var->index() > 0);
1379 DCHECK(var->IsStaticGlobalObjectProperty());
1380 // Each var occupies two slots in the context: for reads and writes.
1381 int slot_index = var->index();
1382 int depth = scope()->ContextChainLength(var->scope());
1383 __ mov(LoadGlobalViaContextDescriptor::DepthRegister(),
1384 Operand(Smi::FromInt(depth)));
1385 __ mov(LoadGlobalViaContextDescriptor::SlotRegister(),
1386 Operand(Smi::FromInt(slot_index)));
1387 __ mov(LoadGlobalViaContextDescriptor::NameRegister(),
1388 Operand(var->name()));
1389 LoadGlobalViaContextStub stub(isolate(), depth);
1390 __ CallStub(&stub);
1391
1392 } else {
1393 __ LoadP(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1394 __ mov(LoadDescriptor::NameRegister(), Operand(var->name()));
1395 __ mov(LoadDescriptor::SlotRegister(),
1396 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1397 CallLoadIC(typeof_mode);
1398 }
1399 }
1400
1401
1402 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1403 TypeofMode typeof_mode) {
1404 // Record position before possible IC call.
1405 SetExpressionPosition(proxy);
1406 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1407 Variable* var = proxy->var();
1408
1409 // Three cases: global variables, lookup variables, and all other types of
1410 // variables.
1411 switch (var->location()) {
1412 case VariableLocation::GLOBAL:
1413 case VariableLocation::UNALLOCATED: {
1414 Comment cmnt(masm_, "[ Global variable");
1415 EmitGlobalVariableLoad(proxy, typeof_mode);
1416 context()->Plug(r3);
1417 break;
1418 }
1419
1420 case VariableLocation::PARAMETER:
1421 case VariableLocation::LOCAL:
1422 case VariableLocation::CONTEXT: {
1423 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1424 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1425 : "[ Stack variable");
1426 if (var->binding_needs_init()) {
1427 // var->scope() may be NULL when the proxy is located in eval code and
1428 // refers to a potential outside binding. Currently those bindings are
1429 // always looked up dynamically, i.e. in that case
1430 // var->location() == LOOKUP.
1431 // always holds.
1432 DCHECK(var->scope() != NULL);
1433
1434 // Check if the binding really needs an initialization check. The check
1435 // can be skipped in the following situation: we have a LET or CONST
1436 // binding in harmony mode, both the Variable and the VariableProxy have
1437 // the same declaration scope (i.e. they are both in global code, in the
1438 // same function or in the same eval code) and the VariableProxy is in
1439 // the source physically located after the initializer of the variable.
1440 //
1441 // We cannot skip any initialization checks for CONST in non-harmony
1442 // mode because const variables may be declared but never initialized:
1443 // if (false) { const x; }; var y = x;
1444 //
1445 // The condition on the declaration scopes is a conservative check for
1446 // nested functions that access a binding and are called before the
1447 // binding is initialized:
1448 // function() { f(); let x = 1; function f() { x = 2; } }
1449 //
1450 bool skip_init_check;
1451 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1452 skip_init_check = false;
1453 } else if (var->is_this()) {
1454 CHECK(info_->function() != nullptr &&
1455 (info_->function()->kind() & kSubclassConstructor) != 0);
1456 // TODO(dslomov): implement 'this' hole check elimination.
1457 skip_init_check = false;
1458 } else {
1459 // Check that we always have valid source position.
1460 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1461 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1462 skip_init_check = var->mode() != CONST_LEGACY &&
1463 var->initializer_position() < proxy->position();
1464 }
1465
1466 if (!skip_init_check) {
1467 Label done;
1468 // Let and const need a read barrier.
1469 GetVar(r3, var);
1470 __ CompareRoot(r3, Heap::kTheHoleValueRootIndex);
1471 __ bne(&done);
1472 if (var->mode() == LET || var->mode() == CONST) {
1473 // Throw a reference error when using an uninitialized let/const
1474 // binding in harmony mode.
1475 __ mov(r3, Operand(var->name()));
1476 __ push(r3);
1477 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1478 } else {
1479 // Uninitalized const bindings outside of harmony mode are unholed.
1480 DCHECK(var->mode() == CONST_LEGACY);
1481 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1482 }
1483 __ bind(&done);
1484 context()->Plug(r3);
1485 break;
1486 }
1487 }
1488 context()->Plug(var);
1489 break;
1490 }
1491
1492 case VariableLocation::LOOKUP: {
1493 Comment cmnt(masm_, "[ Lookup variable");
1494 Label done, slow;
1495 // Generate code for loading from variables potentially shadowed
1496 // by eval-introduced variables.
1497 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1498 __ bind(&slow);
1499 __ mov(r4, Operand(var->name()));
1500 __ Push(cp, r4); // Context and name.
1501 Runtime::FunctionId function_id =
1502 typeof_mode == NOT_INSIDE_TYPEOF
1503 ? Runtime::kLoadLookupSlot
1504 : Runtime::kLoadLookupSlotNoReferenceError;
1505 __ CallRuntime(function_id, 2);
1506 __ bind(&done);
1507 context()->Plug(r3);
1508 }
1509 }
1510 }
1511
1512
1513 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1514 Comment cmnt(masm_, "[ RegExpLiteral");
1515 Label materialized;
1516 // Registers will be used as follows:
1517 // r8 = materialized value (RegExp literal)
1518 // r7 = JS function, literals array
1519 // r6 = literal index
1520 // r5 = RegExp pattern
1521 // r4 = RegExp flags
1522 // r3 = RegExp literal clone
1523 __ LoadP(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1524 __ LoadP(r7, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1525 int literal_offset =
1526 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1527 __ LoadP(r8, FieldMemOperand(r7, literal_offset), r0);
1528 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1529 __ cmp(r8, ip);
1530 __ bne(&materialized);
1531
1532 // Create regexp literal using runtime function.
1533 // Result will be in r3.
1534 __ LoadSmiLiteral(r6, Smi::FromInt(expr->literal_index()));
1535 __ mov(r5, Operand(expr->pattern()));
1536 __ mov(r4, Operand(expr->flags()));
1537 __ Push(r7, r6, r5, r4);
1538 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1539 __ mr(r8, r3);
1540
1541 __ bind(&materialized);
1542 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1543 Label allocated, runtime_allocate;
1544 __ Allocate(size, r3, r5, r6, &runtime_allocate, TAG_OBJECT);
1545 __ b(&allocated);
1546
1547 __ bind(&runtime_allocate);
1548 __ LoadSmiLiteral(r3, Smi::FromInt(size));
1549 __ Push(r8, r3);
1550 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1551 __ pop(r8);
1552
1553 __ bind(&allocated);
1554 // After this, registers are used as follows:
1555 // r3: Newly allocated regexp.
1556 // r8: Materialized regexp.
1557 // r5: temp.
1558 __ CopyFields(r3, r8, r5.bit(), size / kPointerSize);
1559 context()->Plug(r3);
1560 }
1561
1562
1563 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1564 if (expression == NULL) {
1565 __ LoadRoot(r4, Heap::kNullValueRootIndex);
1566 __ push(r4);
1567 } else {
1568 VisitForStackValue(expression);
1569 }
1570 }
1571
1572
1573 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1574 Comment cmnt(masm_, "[ ObjectLiteral");
1575
1576 Handle<FixedArray> constant_properties = expr->constant_properties();
1577 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1578 __ LoadP(r6, FieldMemOperand(r6, JSFunction::kLiteralsOffset));
1579 __ LoadSmiLiteral(r5, Smi::FromInt(expr->literal_index()));
1580 __ mov(r4, Operand(constant_properties));
1581 int flags = expr->ComputeFlags();
1582 __ LoadSmiLiteral(r3, Smi::FromInt(flags));
1583 if (MustCreateObjectLiteralWithRuntime(expr)) {
1584 __ Push(r6, r5, r4, r3);
1585 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1586 } else {
1587 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1588 __ CallStub(&stub);
1589 }
1590 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1591
1592 // If result_saved is true the result is on top of the stack. If
1593 // result_saved is false the result is in r3.
1594 bool result_saved = false;
1595
1596 AccessorTable accessor_table(zone());
1597 int property_index = 0;
1598 // store_slot_index points to the vector IC slot for the next store IC used.
1599 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1600 // and must be updated if the number of store ICs emitted here changes.
1601 int store_slot_index = 0;
1602 for (; property_index < expr->properties()->length(); property_index++) {
1603 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1604 if (property->is_computed_name()) break;
1605 if (property->IsCompileTimeValue()) continue;
1606
1607 Literal* key = property->key()->AsLiteral();
1608 Expression* value = property->value();
1609 if (!result_saved) {
1610 __ push(r3); // Save result on stack
1611 result_saved = true;
1612 }
1613 switch (property->kind()) {
1614 case ObjectLiteral::Property::CONSTANT:
1615 UNREACHABLE();
1616 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1617 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1618 // Fall through.
1619 case ObjectLiteral::Property::COMPUTED:
1620 // It is safe to use [[Put]] here because the boilerplate already
1621 // contains computed properties with an uninitialized value.
1622 if (key->value()->IsInternalizedString()) {
1623 if (property->emit_store()) {
1624 VisitForAccumulatorValue(value);
1625 DCHECK(StoreDescriptor::ValueRegister().is(r3));
1626 __ mov(StoreDescriptor::NameRegister(), Operand(key->value()));
1627 __ LoadP(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1628 if (FLAG_vector_stores) {
1629 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1630 CallStoreIC();
1631 } else {
1632 CallStoreIC(key->LiteralFeedbackId());
1633 }
1634 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1635
1636 if (NeedsHomeObject(value)) {
1637 __ Move(StoreDescriptor::ReceiverRegister(), r3);
1638 __ mov(StoreDescriptor::NameRegister(),
1639 Operand(isolate()->factory()->home_object_symbol()));
1640 __ LoadP(StoreDescriptor::ValueRegister(), MemOperand(sp));
1641 if (FLAG_vector_stores) {
1642 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1643 }
1644 CallStoreIC();
1645 }
1646 } else {
1647 VisitForEffect(value);
1648 }
1649 break;
1650 }
1651 // Duplicate receiver on stack.
1652 __ LoadP(r3, MemOperand(sp));
1653 __ push(r3);
1654 VisitForStackValue(key);
1655 VisitForStackValue(value);
1656 if (property->emit_store()) {
1657 EmitSetHomeObjectIfNeeded(
1658 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1659 __ LoadSmiLiteral(r3, Smi::FromInt(SLOPPY)); // PropertyAttributes
1660 __ push(r3);
1661 __ CallRuntime(Runtime::kSetProperty, 4);
1662 } else {
1663 __ Drop(3);
1664 }
1665 break;
1666 case ObjectLiteral::Property::PROTOTYPE:
1667 // Duplicate receiver on stack.
1668 __ LoadP(r3, MemOperand(sp));
1669 __ push(r3);
1670 VisitForStackValue(value);
1671 DCHECK(property->emit_store());
1672 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1673 break;
1674 case ObjectLiteral::Property::GETTER:
1675 if (property->emit_store()) {
1676 accessor_table.lookup(key)->second->getter = value;
1677 }
1678 break;
1679 case ObjectLiteral::Property::SETTER:
1680 if (property->emit_store()) {
1681 accessor_table.lookup(key)->second->setter = value;
1682 }
1683 break;
1684 }
1685 }
1686
1687 // Emit code to define accessors, using only a single call to the runtime for
1688 // each pair of corresponding getters and setters.
1689 for (AccessorTable::Iterator it = accessor_table.begin();
1690 it != accessor_table.end(); ++it) {
1691 __ LoadP(r3, MemOperand(sp)); // Duplicate receiver.
1692 __ push(r3);
1693 VisitForStackValue(it->first);
1694 EmitAccessor(it->second->getter);
1695 EmitSetHomeObjectIfNeeded(
1696 it->second->getter, 2,
1697 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1698 EmitAccessor(it->second->setter);
1699 EmitSetHomeObjectIfNeeded(
1700 it->second->setter, 3,
1701 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1702 __ LoadSmiLiteral(r3, Smi::FromInt(NONE));
1703 __ push(r3);
1704 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1705 }
1706
1707 // Object literals have two parts. The "static" part on the left contains no
1708 // computed property names, and so we can compute its map ahead of time; see
1709 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1710 // starts with the first computed property name, and continues with all
1711 // properties to its right. All the code from above initializes the static
1712 // component of the object literal, and arranges for the map of the result to
1713 // reflect the static order in which the keys appear. For the dynamic
1714 // properties, we compile them into a series of "SetOwnProperty" runtime
1715 // calls. This will preserve insertion order.
1716 for (; property_index < expr->properties()->length(); property_index++) {
1717 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1718
1719 Expression* value = property->value();
1720 if (!result_saved) {
1721 __ push(r3); // Save result on the stack
1722 result_saved = true;
1723 }
1724
1725 __ LoadP(r3, MemOperand(sp)); // Duplicate receiver.
1726 __ push(r3);
1727
1728 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1729 DCHECK(!property->is_computed_name());
1730 VisitForStackValue(value);
1731 DCHECK(property->emit_store());
1732 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1733 } else {
1734 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1735 VisitForStackValue(value);
1736 EmitSetHomeObjectIfNeeded(
1737 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1738
1739 switch (property->kind()) {
1740 case ObjectLiteral::Property::CONSTANT:
1741 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1742 case ObjectLiteral::Property::COMPUTED:
1743 if (property->emit_store()) {
1744 __ LoadSmiLiteral(r3, Smi::FromInt(NONE));
1745 __ push(r3);
1746 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1747 } else {
1748 __ Drop(3);
1749 }
1750 break;
1751
1752 case ObjectLiteral::Property::PROTOTYPE:
1753 UNREACHABLE();
1754 break;
1755
1756 case ObjectLiteral::Property::GETTER:
1757 __ mov(r3, Operand(Smi::FromInt(NONE)));
1758 __ push(r3);
1759 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1760 break;
1761
1762 case ObjectLiteral::Property::SETTER:
1763 __ mov(r3, Operand(Smi::FromInt(NONE)));
1764 __ push(r3);
1765 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1766 break;
1767 }
1768 }
1769 }
1770
1771 if (expr->has_function()) {
1772 DCHECK(result_saved);
1773 __ LoadP(r3, MemOperand(sp));
1774 __ push(r3);
1775 __ CallRuntime(Runtime::kToFastProperties, 1);
1776 }
1777
1778 if (result_saved) {
1779 context()->PlugTOS();
1780 } else {
1781 context()->Plug(r3);
1782 }
1783
1784 // Verify that compilation exactly consumed the number of store ic slots that
1785 // the ObjectLiteral node had to offer.
1786 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1787 }
1788
1789
1790 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1791 Comment cmnt(masm_, "[ ArrayLiteral");
1792
1793 expr->BuildConstantElements(isolate());
1794 Handle<FixedArray> constant_elements = expr->constant_elements();
1795 bool has_fast_elements =
1796 IsFastObjectElementsKind(expr->constant_elements_kind());
1797 Handle<FixedArrayBase> constant_elements_values(
1798 FixedArrayBase::cast(constant_elements->get(1)));
1799
1800 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1801 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1802 // If the only customer of allocation sites is transitioning, then
1803 // we can turn it off if we don't have anywhere else to transition to.
1804 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1805 }
1806
1807 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1808 __ LoadP(r6, FieldMemOperand(r6, JSFunction::kLiteralsOffset));
1809 __ LoadSmiLiteral(r5, Smi::FromInt(expr->literal_index()));
1810 __ mov(r4, Operand(constant_elements));
1811 if (MustCreateArrayLiteralWithRuntime(expr)) {
1812 __ LoadSmiLiteral(r3, Smi::FromInt(expr->ComputeFlags()));
1813 __ Push(r6, r5, r4, r3);
1814 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1815 } else {
1816 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1817 __ CallStub(&stub);
1818 }
1819 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1820
1821 bool result_saved = false; // Is the result saved to the stack?
1822 ZoneList<Expression*>* subexprs = expr->values();
1823 int length = subexprs->length();
1824
1825 // Emit code to evaluate all the non-constant subexpressions and to store
1826 // them into the newly cloned array.
1827 int array_index = 0;
1828 for (; array_index < length; array_index++) {
1829 Expression* subexpr = subexprs->at(array_index);
1830 if (subexpr->IsSpread()) break;
1831 // If the subexpression is a literal or a simple materialized literal it
1832 // is already set in the cloned array.
1833 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1834
1835 if (!result_saved) {
1836 __ push(r3);
1837 __ Push(Smi::FromInt(expr->literal_index()));
1838 result_saved = true;
1839 }
1840 VisitForAccumulatorValue(subexpr);
1841
1842 if (has_fast_elements) {
1843 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1844 __ LoadP(r8, MemOperand(sp, kPointerSize)); // Copy of array literal.
1845 __ LoadP(r4, FieldMemOperand(r8, JSObject::kElementsOffset));
1846 __ StoreP(result_register(), FieldMemOperand(r4, offset), r0);
1847 // Update the write barrier for the array store.
1848 __ RecordWriteField(r4, offset, result_register(), r5, kLRHasBeenSaved,
1849 kDontSaveFPRegs, EMIT_REMEMBERED_SET,
1850 INLINE_SMI_CHECK);
1851 } else {
1852 __ LoadSmiLiteral(r6, Smi::FromInt(array_index));
1853 StoreArrayLiteralElementStub stub(isolate());
1854 __ CallStub(&stub);
1855 }
1856
1857 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1858 }
1859
1860 // In case the array literal contains spread expressions it has two parts. The
1861 // first part is the "static" array which has a literal index is handled
1862 // above. The second part is the part after the first spread expression
1863 // (inclusive) and these elements gets appended to the array. Note that the
1864 // number elements an iterable produces is unknown ahead of time.
1865 if (array_index < length && result_saved) {
1866 __ Drop(1); // literal index
1867 __ Pop(r3);
1868 result_saved = false;
1869 }
1870 for (; array_index < length; array_index++) {
1871 Expression* subexpr = subexprs->at(array_index);
1872
1873 __ Push(r3);
1874 if (subexpr->IsSpread()) {
1875 VisitForStackValue(subexpr->AsSpread()->expression());
1876 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1877 } else {
1878 VisitForStackValue(subexpr);
1879 __ CallRuntime(Runtime::kAppendElement, 2);
1880 }
1881
1882 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1883 }
1884
1885 if (result_saved) {
1886 __ Drop(1); // literal index
1887 context()->PlugTOS();
1888 } else {
1889 context()->Plug(r3);
1890 }
1891 }
1892
1893
1894 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1895 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1896
1897 Comment cmnt(masm_, "[ Assignment");
1898 SetExpressionPosition(expr, INSERT_BREAK);
1899
1900 Property* property = expr->target()->AsProperty();
1901 LhsKind assign_type = Property::GetAssignType(property);
1902
1903 // Evaluate LHS expression.
1904 switch (assign_type) {
1905 case VARIABLE:
1906 // Nothing to do here.
1907 break;
1908 case NAMED_PROPERTY:
1909 if (expr->is_compound()) {
1910 // We need the receiver both on the stack and in the register.
1911 VisitForStackValue(property->obj());
1912 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1913 } else {
1914 VisitForStackValue(property->obj());
1915 }
1916 break;
1917 case NAMED_SUPER_PROPERTY:
1918 VisitForStackValue(
1919 property->obj()->AsSuperPropertyReference()->this_var());
1920 VisitForAccumulatorValue(
1921 property->obj()->AsSuperPropertyReference()->home_object());
1922 __ Push(result_register());
1923 if (expr->is_compound()) {
1924 const Register scratch = r4;
1925 __ LoadP(scratch, MemOperand(sp, kPointerSize));
1926 __ Push(scratch, result_register());
1927 }
1928 break;
1929 case KEYED_SUPER_PROPERTY: {
1930 const Register scratch = r4;
1931 VisitForStackValue(
1932 property->obj()->AsSuperPropertyReference()->this_var());
1933 VisitForAccumulatorValue(
1934 property->obj()->AsSuperPropertyReference()->home_object());
1935 __ mr(scratch, result_register());
1936 VisitForAccumulatorValue(property->key());
1937 __ Push(scratch, result_register());
1938 if (expr->is_compound()) {
1939 const Register scratch1 = r5;
1940 __ LoadP(scratch1, MemOperand(sp, 2 * kPointerSize));
1941 __ Push(scratch1, scratch, result_register());
1942 }
1943 break;
1944 }
1945 case KEYED_PROPERTY:
1946 if (expr->is_compound()) {
1947 VisitForStackValue(property->obj());
1948 VisitForStackValue(property->key());
1949 __ LoadP(LoadDescriptor::ReceiverRegister(),
1950 MemOperand(sp, 1 * kPointerSize));
1951 __ LoadP(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1952 } else {
1953 VisitForStackValue(property->obj());
1954 VisitForStackValue(property->key());
1955 }
1956 break;
1957 }
1958
1959 // For compound assignments we need another deoptimization point after the
1960 // variable/property load.
1961 if (expr->is_compound()) {
1962 {
1963 AccumulatorValueContext context(this);
1964 switch (assign_type) {
1965 case VARIABLE:
1966 EmitVariableLoad(expr->target()->AsVariableProxy());
1967 PrepareForBailout(expr->target(), TOS_REG);
1968 break;
1969 case NAMED_PROPERTY:
1970 EmitNamedPropertyLoad(property);
1971 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1972 break;
1973 case NAMED_SUPER_PROPERTY:
1974 EmitNamedSuperPropertyLoad(property);
1975 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1976 break;
1977 case KEYED_SUPER_PROPERTY:
1978 EmitKeyedSuperPropertyLoad(property);
1979 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1980 break;
1981 case KEYED_PROPERTY:
1982 EmitKeyedPropertyLoad(property);
1983 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1984 break;
1985 }
1986 }
1987
1988 Token::Value op = expr->binary_op();
1989 __ push(r3); // Left operand goes on the stack.
1990 VisitForAccumulatorValue(expr->value());
1991
1992 AccumulatorValueContext context(this);
1993 if (ShouldInlineSmiCase(op)) {
1994 EmitInlineSmiBinaryOp(expr->binary_operation(), op, expr->target(),
1995 expr->value());
1996 } else {
1997 EmitBinaryOp(expr->binary_operation(), op);
1998 }
1999
2000 // Deoptimization point in case the binary operation may have side effects.
2001 PrepareForBailout(expr->binary_operation(), TOS_REG);
2002 } else {
2003 VisitForAccumulatorValue(expr->value());
2004 }
2005
2006 SetExpressionPosition(expr);
2007
2008 // Store the value.
2009 switch (assign_type) {
2010 case VARIABLE:
2011 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2012 expr->op(), expr->AssignmentSlot());
2013 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2014 context()->Plug(r3);
2015 break;
2016 case NAMED_PROPERTY:
2017 EmitNamedPropertyAssignment(expr);
2018 break;
2019 case NAMED_SUPER_PROPERTY:
2020 EmitNamedSuperPropertyStore(property);
2021 context()->Plug(r3);
2022 break;
2023 case KEYED_SUPER_PROPERTY:
2024 EmitKeyedSuperPropertyStore(property);
2025 context()->Plug(r3);
2026 break;
2027 case KEYED_PROPERTY:
2028 EmitKeyedPropertyAssignment(expr);
2029 break;
2030 }
2031 }
2032
2033
2034 void FullCodeGenerator::VisitYield(Yield* expr) {
2035 Comment cmnt(masm_, "[ Yield");
2036 SetExpressionPosition(expr);
2037
2038 // Evaluate yielded value first; the initial iterator definition depends on
2039 // this. It stays on the stack while we update the iterator.
2040 VisitForStackValue(expr->expression());
2041
2042 switch (expr->yield_kind()) {
2043 case Yield::kSuspend:
2044 // Pop value from top-of-stack slot; box result into result register.
2045 EmitCreateIteratorResult(false);
2046 __ push(result_register());
2047 // Fall through.
2048 case Yield::kInitial: {
2049 Label suspend, continuation, post_runtime, resume;
2050
2051 __ b(&suspend);
2052 __ bind(&continuation);
2053 __ RecordGeneratorContinuation();
2054 __ b(&resume);
2055
2056 __ bind(&suspend);
2057 VisitForAccumulatorValue(expr->generator_object());
2058 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2059 __ LoadSmiLiteral(r4, Smi::FromInt(continuation.pos()));
2060 __ StoreP(r4, FieldMemOperand(r3, JSGeneratorObject::kContinuationOffset),
2061 r0);
2062 __ StoreP(cp, FieldMemOperand(r3, JSGeneratorObject::kContextOffset), r0);
2063 __ mr(r4, cp);
2064 __ RecordWriteField(r3, JSGeneratorObject::kContextOffset, r4, r5,
2065 kLRHasBeenSaved, kDontSaveFPRegs);
2066 __ addi(r4, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2067 __ cmp(sp, r4);
2068 __ beq(&post_runtime);
2069 __ push(r3); // generator object
2070 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2071 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2072 __ bind(&post_runtime);
2073 __ pop(result_register());
2074 EmitReturnSequence();
2075
2076 __ bind(&resume);
2077 context()->Plug(result_register());
2078 break;
2079 }
2080
2081 case Yield::kFinal: {
2082 VisitForAccumulatorValue(expr->generator_object());
2083 __ LoadSmiLiteral(r4, Smi::FromInt(JSGeneratorObject::kGeneratorClosed));
2084 __ StoreP(r4, FieldMemOperand(result_register(),
2085 JSGeneratorObject::kContinuationOffset),
2086 r0);
2087 // Pop value from top-of-stack slot, box result into result register.
2088 EmitCreateIteratorResult(true);
2089 EmitUnwindBeforeReturn();
2090 EmitReturnSequence();
2091 break;
2092 }
2093
2094 case Yield::kDelegating: {
2095 VisitForStackValue(expr->generator_object());
2096
2097 // Initial stack layout is as follows:
2098 // [sp + 1 * kPointerSize] iter
2099 // [sp + 0 * kPointerSize] g
2100
2101 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2102 Label l_next, l_call;
2103 Register load_receiver = LoadDescriptor::ReceiverRegister();
2104 Register load_name = LoadDescriptor::NameRegister();
2105
2106 // Initial send value is undefined.
2107 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
2108 __ b(&l_next);
2109
2110 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2111 __ bind(&l_catch);
2112 __ LoadRoot(load_name, Heap::kthrow_stringRootIndex); // "throw"
2113 __ LoadP(r6, MemOperand(sp, 1 * kPointerSize)); // iter
2114 __ Push(load_name, r6, r3); // "throw", iter, except
2115 __ b(&l_call);
2116
2117 // try { received = %yield result }
2118 // Shuffle the received result above a try handler and yield it without
2119 // re-boxing.
2120 __ bind(&l_try);
2121 __ pop(r3); // result
2122 int handler_index = NewHandlerTableEntry();
2123 EnterTryBlock(handler_index, &l_catch);
2124 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2125 __ push(r3); // result
2126
2127 __ b(&l_suspend);
2128 __ bind(&l_continuation);
2129 __ RecordGeneratorContinuation();
2130 __ b(&l_resume);
2131
2132 __ bind(&l_suspend);
2133 const int generator_object_depth = kPointerSize + try_block_size;
2134 __ LoadP(r3, MemOperand(sp, generator_object_depth));
2135 __ push(r3); // g
2136 __ Push(Smi::FromInt(handler_index)); // handler-index
2137 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2138 __ LoadSmiLiteral(r4, Smi::FromInt(l_continuation.pos()));
2139 __ StoreP(r4, FieldMemOperand(r3, JSGeneratorObject::kContinuationOffset),
2140 r0);
2141 __ StoreP(cp, FieldMemOperand(r3, JSGeneratorObject::kContextOffset), r0);
2142 __ mr(r4, cp);
2143 __ RecordWriteField(r3, JSGeneratorObject::kContextOffset, r4, r5,
2144 kLRHasBeenSaved, kDontSaveFPRegs);
2145 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2146 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2147 __ pop(r3); // result
2148 EmitReturnSequence();
2149 __ bind(&l_resume); // received in r3
2150 ExitTryBlock(handler_index);
2151
2152 // receiver = iter; f = 'next'; arg = received;
2153 __ bind(&l_next);
2154
2155 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2156 __ LoadP(r6, MemOperand(sp, 1 * kPointerSize)); // iter
2157 __ Push(load_name, r6, r3); // "next", iter, received
2158
2159 // result = receiver[f](arg);
2160 __ bind(&l_call);
2161 __ LoadP(load_receiver, MemOperand(sp, kPointerSize));
2162 __ LoadP(load_name, MemOperand(sp, 2 * kPointerSize));
2163 __ mov(LoadDescriptor::SlotRegister(),
2164 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2165 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2166 CallIC(ic, TypeFeedbackId::None());
2167 __ mr(r4, r3);
2168 __ StoreP(r4, MemOperand(sp, 2 * kPointerSize));
2169 SetCallPosition(expr, 1);
2170 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2171 __ CallStub(&stub);
2172
2173 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2174 __ Drop(1); // The function is still on the stack; drop it.
2175
2176 // if (!result.done) goto l_try;
2177 __ Move(load_receiver, r3);
2178
2179 __ push(load_receiver); // save result
2180 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2181 __ mov(LoadDescriptor::SlotRegister(),
2182 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2183 CallLoadIC(NOT_INSIDE_TYPEOF); // r0=result.done
2184 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2185 CallIC(bool_ic);
2186 __ cmpi(r3, Operand::Zero());
2187 __ beq(&l_try);
2188
2189 // result.value
2190 __ pop(load_receiver); // result
2191 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2192 __ mov(LoadDescriptor::SlotRegister(),
2193 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2194 CallLoadIC(NOT_INSIDE_TYPEOF); // r3=result.value
2195 context()->DropAndPlug(2, r3); // drop iter and g
2196 break;
2197 }
2198 }
2199 }
2200
2201
2202 void FullCodeGenerator::EmitGeneratorResume(
2203 Expression* generator, Expression* value,
2204 JSGeneratorObject::ResumeMode resume_mode) {
2205 // The value stays in r3, and is ultimately read by the resumed generator, as
2206 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2207 // is read to throw the value when the resumed generator is already closed.
2208 // r4 will hold the generator object until the activation has been resumed.
2209 VisitForStackValue(generator);
2210 VisitForAccumulatorValue(value);
2211 __ pop(r4);
2212
2213 // Load suspended function and context.
2214 __ LoadP(cp, FieldMemOperand(r4, JSGeneratorObject::kContextOffset));
2215 __ LoadP(r7, FieldMemOperand(r4, JSGeneratorObject::kFunctionOffset));
2216
2217 // Load receiver and store as the first argument.
2218 __ LoadP(r5, FieldMemOperand(r4, JSGeneratorObject::kReceiverOffset));
2219 __ push(r5);
2220
2221 // Push holes for the rest of the arguments to the generator function.
2222 __ LoadP(r6, FieldMemOperand(r7, JSFunction::kSharedFunctionInfoOffset));
2223 __ LoadWordArith(
2224 r6, FieldMemOperand(r6, SharedFunctionInfo::kFormalParameterCountOffset));
2225 __ LoadRoot(r5, Heap::kTheHoleValueRootIndex);
2226 Label argument_loop, push_frame;
2227 #if V8_TARGET_ARCH_PPC64
2228 __ cmpi(r6, Operand::Zero());
2229 __ beq(&push_frame);
2230 #else
2231 __ SmiUntag(r6, SetRC);
2232 __ beq(&push_frame, cr0);
2233 #endif
2234 __ mtctr(r6);
2235 __ bind(&argument_loop);
2236 __ push(r5);
2237 __ bdnz(&argument_loop);
2238
2239 // Enter a new JavaScript frame, and initialize its slots as they were when
2240 // the generator was suspended.
2241 Label resume_frame, done;
2242 __ bind(&push_frame);
2243 __ b(&resume_frame, SetLK);
2244 __ b(&done);
2245 __ bind(&resume_frame);
2246 // lr = return address.
2247 // fp = caller's frame pointer.
2248 // cp = callee's context,
2249 // r7 = callee's JS function.
2250 __ PushFixedFrame(r7);
2251 // Adjust FP to point to saved FP.
2252 __ addi(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
2253
2254 // Load the operand stack size.
2255 __ LoadP(r6, FieldMemOperand(r4, JSGeneratorObject::kOperandStackOffset));
2256 __ LoadP(r6, FieldMemOperand(r6, FixedArray::kLengthOffset));
2257 __ SmiUntag(r6, SetRC);
2258
2259 // If we are sending a value and there is no operand stack, we can jump back
2260 // in directly.
2261 Label call_resume;
2262 if (resume_mode == JSGeneratorObject::NEXT) {
2263 Label slow_resume;
2264 __ bne(&slow_resume, cr0);
2265 __ LoadP(ip, FieldMemOperand(r7, JSFunction::kCodeEntryOffset));
2266 {
2267 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
2268 if (FLAG_enable_embedded_constant_pool) {
2269 __ LoadConstantPoolPointerRegisterFromCodeTargetAddress(ip);
2270 }
2271 __ LoadP(r5, FieldMemOperand(r4, JSGeneratorObject::kContinuationOffset));
2272 __ SmiUntag(r5);
2273 __ add(ip, ip, r5);
2274 __ LoadSmiLiteral(r5,
2275 Smi::FromInt(JSGeneratorObject::kGeneratorExecuting));
2276 __ StoreP(r5, FieldMemOperand(r4, JSGeneratorObject::kContinuationOffset),
2277 r0);
2278 __ Jump(ip);
2279 __ bind(&slow_resume);
2280 }
2281 } else {
2282 __ beq(&call_resume, cr0);
2283 }
2284
2285 // Otherwise, we push holes for the operand stack and call the runtime to fix
2286 // up the stack and the handlers.
2287 Label operand_loop;
2288 __ mtctr(r6);
2289 __ bind(&operand_loop);
2290 __ push(r5);
2291 __ bdnz(&operand_loop);
2292
2293 __ bind(&call_resume);
2294 DCHECK(!result_register().is(r4));
2295 __ Push(r4, result_register());
2296 __ Push(Smi::FromInt(resume_mode));
2297 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2298 // Not reached: the runtime call returns elsewhere.
2299 __ stop("not-reached");
2300
2301 __ bind(&done);
2302 context()->Plug(result_register());
2303 }
2304
2305
2306 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2307 Label gc_required;
2308 Label allocated;
2309
2310 const int instance_size = 5 * kPointerSize;
2311 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2312 instance_size);
2313
2314 __ Allocate(instance_size, r3, r5, r6, &gc_required, TAG_OBJECT);
2315 __ b(&allocated);
2316
2317 __ bind(&gc_required);
2318 __ Push(Smi::FromInt(instance_size));
2319 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2320 __ LoadP(context_register(),
2321 MemOperand(fp, StandardFrameConstants::kContextOffset));
2322
2323 __ bind(&allocated);
2324 __ LoadP(r4, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2325 __ LoadP(r4, FieldMemOperand(r4, GlobalObject::kNativeContextOffset));
2326 __ LoadP(r4, ContextOperand(r4, Context::ITERATOR_RESULT_MAP_INDEX));
2327 __ pop(r5);
2328 __ mov(r6, Operand(isolate()->factory()->ToBoolean(done)));
2329 __ mov(r7, Operand(isolate()->factory()->empty_fixed_array()));
2330 __ StoreP(r4, FieldMemOperand(r3, HeapObject::kMapOffset), r0);
2331 __ StoreP(r7, FieldMemOperand(r3, JSObject::kPropertiesOffset), r0);
2332 __ StoreP(r7, FieldMemOperand(r3, JSObject::kElementsOffset), r0);
2333 __ StoreP(r5,
2334 FieldMemOperand(r3, JSGeneratorObject::kResultValuePropertyOffset),
2335 r0);
2336 __ StoreP(r6,
2337 FieldMemOperand(r3, JSGeneratorObject::kResultDonePropertyOffset),
2338 r0);
2339
2340 // Only the value field needs a write barrier, as the other values are in the
2341 // root set.
2342 __ RecordWriteField(r3, JSGeneratorObject::kResultValuePropertyOffset, r5, r6,
2343 kLRHasBeenSaved, kDontSaveFPRegs);
2344 }
2345
2346
2347 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2348 SetExpressionPosition(prop);
2349 Literal* key = prop->key()->AsLiteral();
2350 DCHECK(!prop->IsSuperAccess());
2351
2352 __ mov(LoadDescriptor::NameRegister(), Operand(key->value()));
2353 __ mov(LoadDescriptor::SlotRegister(),
2354 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2355 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2356 }
2357
2358
2359 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2360 // Stack: receiver, home_object.
2361 SetExpressionPosition(prop);
2362 Literal* key = prop->key()->AsLiteral();
2363 DCHECK(!key->value()->IsSmi());
2364 DCHECK(prop->IsSuperAccess());
2365
2366 __ Push(key->value());
2367 __ Push(Smi::FromInt(language_mode()));
2368 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2369 }
2370
2371
2372 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2373 SetExpressionPosition(prop);
2374 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2375 __ mov(LoadDescriptor::SlotRegister(),
2376 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2377 CallIC(ic);
2378 }
2379
2380
2381 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2382 // Stack: receiver, home_object, key.
2383 SetExpressionPosition(prop);
2384 __ Push(Smi::FromInt(language_mode()));
2385 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2386 }
2387
2388
2389 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2390 Token::Value op,
2391 Expression* left_expr,
2392 Expression* right_expr) {
2393 Label done, smi_case, stub_call;
2394
2395 Register scratch1 = r5;
2396 Register scratch2 = r6;
2397
2398 // Get the arguments.
2399 Register left = r4;
2400 Register right = r3;
2401 __ pop(left);
2402
2403 // Perform combined smi check on both operands.
2404 __ orx(scratch1, left, right);
2405 STATIC_ASSERT(kSmiTag == 0);
2406 JumpPatchSite patch_site(masm_);
2407 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2408
2409 __ bind(&stub_call);
2410 Handle<Code> code =
2411 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2412 CallIC(code, expr->BinaryOperationFeedbackId());
2413 patch_site.EmitPatchInfo();
2414 __ b(&done);
2415
2416 __ bind(&smi_case);
2417 // Smi case. This code works the same way as the smi-smi case in the type
2418 // recording binary operation stub.
2419 switch (op) {
2420 case Token::SAR:
2421 __ GetLeastBitsFromSmi(scratch1, right, 5);
2422 __ ShiftRightArith(right, left, scratch1);
2423 __ ClearRightImm(right, right, Operand(kSmiTagSize + kSmiShiftSize));
2424 break;
2425 case Token::SHL: {
2426 __ GetLeastBitsFromSmi(scratch2, right, 5);
2427 #if V8_TARGET_ARCH_PPC64
2428 __ ShiftLeft_(right, left, scratch2);
2429 #else
2430 __ SmiUntag(scratch1, left);
2431 __ ShiftLeft_(scratch1, scratch1, scratch2);
2432 // Check that the *signed* result fits in a smi
2433 __ JumpIfNotSmiCandidate(scratch1, scratch2, &stub_call);
2434 __ SmiTag(right, scratch1);
2435 #endif
2436 break;
2437 }
2438 case Token::SHR: {
2439 __ SmiUntag(scratch1, left);
2440 __ GetLeastBitsFromSmi(scratch2, right, 5);
2441 __ srw(scratch1, scratch1, scratch2);
2442 // Unsigned shift is not allowed to produce a negative number.
2443 __ JumpIfNotUnsignedSmiCandidate(scratch1, r0, &stub_call);
2444 __ SmiTag(right, scratch1);
2445 break;
2446 }
2447 case Token::ADD: {
2448 __ AddAndCheckForOverflow(scratch1, left, right, scratch2, r0);
2449 __ BranchOnOverflow(&stub_call);
2450 __ mr(right, scratch1);
2451 break;
2452 }
2453 case Token::SUB: {
2454 __ SubAndCheckForOverflow(scratch1, left, right, scratch2, r0);
2455 __ BranchOnOverflow(&stub_call);
2456 __ mr(right, scratch1);
2457 break;
2458 }
2459 case Token::MUL: {
2460 Label mul_zero;
2461 #if V8_TARGET_ARCH_PPC64
2462 // Remove tag from both operands.
2463 __ SmiUntag(ip, right);
2464 __ SmiUntag(r0, left);
2465 __ Mul(scratch1, r0, ip);
2466 // Check for overflowing the smi range - no overflow if higher 33 bits of
2467 // the result are identical.
2468 __ TestIfInt32(scratch1, r0);
2469 __ bne(&stub_call);
2470 #else
2471 __ SmiUntag(ip, right);
2472 __ mullw(scratch1, left, ip);
2473 __ mulhw(scratch2, left, ip);
2474 // Check for overflowing the smi range - no overflow if higher 33 bits of
2475 // the result are identical.
2476 __ TestIfInt32(scratch2, scratch1, ip);
2477 __ bne(&stub_call);
2478 #endif
2479 // Go slow on zero result to handle -0.
2480 __ cmpi(scratch1, Operand::Zero());
2481 __ beq(&mul_zero);
2482 #if V8_TARGET_ARCH_PPC64
2483 __ SmiTag(right, scratch1);
2484 #else
2485 __ mr(right, scratch1);
2486 #endif
2487 __ b(&done);
2488 // We need -0 if we were multiplying a negative number with 0 to get 0.
2489 // We know one of them was zero.
2490 __ bind(&mul_zero);
2491 __ add(scratch2, right, left);
2492 __ cmpi(scratch2, Operand::Zero());
2493 __ blt(&stub_call);
2494 __ LoadSmiLiteral(right, Smi::FromInt(0));
2495 break;
2496 }
2497 case Token::BIT_OR:
2498 __ orx(right, left, right);
2499 break;
2500 case Token::BIT_AND:
2501 __ and_(right, left, right);
2502 break;
2503 case Token::BIT_XOR:
2504 __ xor_(right, left, right);
2505 break;
2506 default:
2507 UNREACHABLE();
2508 }
2509
2510 __ bind(&done);
2511 context()->Plug(r3);
2512 }
2513
2514
2515 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2516 int* used_store_slots) {
2517 // Constructor is in r3.
2518 DCHECK(lit != NULL);
2519 __ push(r3);
2520
2521 // No access check is needed here since the constructor is created by the
2522 // class literal.
2523 Register scratch = r4;
2524 __ LoadP(scratch,
2525 FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
2526 __ push(scratch);
2527
2528 for (int i = 0; i < lit->properties()->length(); i++) {
2529 ObjectLiteral::Property* property = lit->properties()->at(i);
2530 Expression* value = property->value();
2531
2532 if (property->is_static()) {
2533 __ LoadP(scratch, MemOperand(sp, kPointerSize)); // constructor
2534 } else {
2535 __ LoadP(scratch, MemOperand(sp, 0)); // prototype
2536 }
2537 __ push(scratch);
2538 EmitPropertyKey(property, lit->GetIdForProperty(i));
2539
2540 // The static prototype property is read only. We handle the non computed
2541 // property name case in the parser. Since this is the only case where we
2542 // need to check for an own read only property we special case this so we do
2543 // not need to do this for every property.
2544 if (property->is_static() && property->is_computed_name()) {
2545 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2546 __ push(r3);
2547 }
2548
2549 VisitForStackValue(value);
2550 EmitSetHomeObjectIfNeeded(value, 2,
2551 lit->SlotForHomeObject(value, used_store_slots));
2552
2553 switch (property->kind()) {
2554 case ObjectLiteral::Property::CONSTANT:
2555 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2556 case ObjectLiteral::Property::PROTOTYPE:
2557 UNREACHABLE();
2558 case ObjectLiteral::Property::COMPUTED:
2559 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2560 break;
2561
2562 case ObjectLiteral::Property::GETTER:
2563 __ mov(r3, Operand(Smi::FromInt(DONT_ENUM)));
2564 __ push(r3);
2565 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2566 break;
2567
2568 case ObjectLiteral::Property::SETTER:
2569 __ mov(r3, Operand(Smi::FromInt(DONT_ENUM)));
2570 __ push(r3);
2571 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2572 break;
2573
2574 default:
2575 UNREACHABLE();
2576 }
2577 }
2578
2579 // prototype
2580 __ CallRuntime(Runtime::kToFastProperties, 1);
2581
2582 // constructor
2583 __ CallRuntime(Runtime::kToFastProperties, 1);
2584
2585 if (is_strong(language_mode())) {
2586 __ LoadP(scratch,
2587 FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
2588 __ Push(r3, scratch);
2589 // TODO(conradw): It would be more efficient to define the properties with
2590 // the right attributes the first time round.
2591 // Freeze the prototype.
2592 __ CallRuntime(Runtime::kObjectFreeze, 1);
2593 // Freeze the constructor.
2594 __ CallRuntime(Runtime::kObjectFreeze, 1);
2595 }
2596 }
2597
2598
2599 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2600 __ pop(r4);
2601 Handle<Code> code =
2602 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2603 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2604 CallIC(code, expr->BinaryOperationFeedbackId());
2605 patch_site.EmitPatchInfo();
2606 context()->Plug(r3);
2607 }
2608
2609
2610 void FullCodeGenerator::EmitAssignment(Expression* expr,
2611 FeedbackVectorICSlot slot) {
2612 DCHECK(expr->IsValidReferenceExpressionOrThis());
2613
2614 Property* prop = expr->AsProperty();
2615 LhsKind assign_type = Property::GetAssignType(prop);
2616
2617 switch (assign_type) {
2618 case VARIABLE: {
2619 Variable* var = expr->AsVariableProxy()->var();
2620 EffectContext context(this);
2621 EmitVariableAssignment(var, Token::ASSIGN, slot);
2622 break;
2623 }
2624 case NAMED_PROPERTY: {
2625 __ push(r3); // Preserve value.
2626 VisitForAccumulatorValue(prop->obj());
2627 __ Move(StoreDescriptor::ReceiverRegister(), r3);
2628 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2629 __ mov(StoreDescriptor::NameRegister(),
2630 Operand(prop->key()->AsLiteral()->value()));
2631 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2632 CallStoreIC();
2633 break;
2634 }
2635 case NAMED_SUPER_PROPERTY: {
2636 __ Push(r3);
2637 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2638 VisitForAccumulatorValue(
2639 prop->obj()->AsSuperPropertyReference()->home_object());
2640 // stack: value, this; r3: home_object
2641 Register scratch = r5;
2642 Register scratch2 = r6;
2643 __ mr(scratch, result_register()); // home_object
2644 __ LoadP(r3, MemOperand(sp, kPointerSize)); // value
2645 __ LoadP(scratch2, MemOperand(sp, 0)); // this
2646 __ StoreP(scratch2, MemOperand(sp, kPointerSize)); // this
2647 __ StoreP(scratch, MemOperand(sp, 0)); // home_object
2648 // stack: this, home_object; r3: value
2649 EmitNamedSuperPropertyStore(prop);
2650 break;
2651 }
2652 case KEYED_SUPER_PROPERTY: {
2653 __ Push(r3);
2654 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2655 VisitForStackValue(
2656 prop->obj()->AsSuperPropertyReference()->home_object());
2657 VisitForAccumulatorValue(prop->key());
2658 Register scratch = r5;
2659 Register scratch2 = r6;
2660 __ LoadP(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2661 // stack: value, this, home_object; r3: key, r6: value
2662 __ LoadP(scratch, MemOperand(sp, kPointerSize)); // this
2663 __ StoreP(scratch, MemOperand(sp, 2 * kPointerSize));
2664 __ LoadP(scratch, MemOperand(sp, 0)); // home_object
2665 __ StoreP(scratch, MemOperand(sp, kPointerSize));
2666 __ StoreP(r3, MemOperand(sp, 0));
2667 __ Move(r3, scratch2);
2668 // stack: this, home_object, key; r3: value.
2669 EmitKeyedSuperPropertyStore(prop);
2670 break;
2671 }
2672 case KEYED_PROPERTY: {
2673 __ push(r3); // Preserve value.
2674 VisitForStackValue(prop->obj());
2675 VisitForAccumulatorValue(prop->key());
2676 __ Move(StoreDescriptor::NameRegister(), r3);
2677 __ Pop(StoreDescriptor::ValueRegister(),
2678 StoreDescriptor::ReceiverRegister());
2679 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2680 Handle<Code> ic =
2681 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2682 CallIC(ic);
2683 break;
2684 }
2685 }
2686 context()->Plug(r3);
2687 }
2688
2689
2690 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2691 Variable* var, MemOperand location) {
2692 __ StoreP(result_register(), location, r0);
2693 if (var->IsContextSlot()) {
2694 // RecordWrite may destroy all its register arguments.
2695 __ mr(r6, result_register());
2696 int offset = Context::SlotOffset(var->index());
2697 __ RecordWriteContextSlot(r4, offset, r6, r5, kLRHasBeenSaved,
2698 kDontSaveFPRegs);
2699 }
2700 }
2701
2702
2703 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2704 FeedbackVectorICSlot slot) {
2705 if (var->IsUnallocated()) {
2706 // Global var, const, or let.
2707 __ mov(StoreDescriptor::NameRegister(), Operand(var->name()));
2708 __ LoadP(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2709 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2710 CallStoreIC();
2711
2712 } else if (var->IsGlobalSlot()) {
2713 // Global var, const, or let.
2714 DCHECK(var->index() > 0);
2715 DCHECK(var->IsStaticGlobalObjectProperty());
2716 // Each var occupies two slots in the context: for reads and writes.
2717 int slot_index = var->index() + 1;
2718 int depth = scope()->ContextChainLength(var->scope());
2719 __ mov(StoreGlobalViaContextDescriptor::DepthRegister(),
2720 Operand(Smi::FromInt(depth)));
2721 __ mov(StoreGlobalViaContextDescriptor::SlotRegister(),
2722 Operand(Smi::FromInt(slot_index)));
2723 __ mov(StoreGlobalViaContextDescriptor::NameRegister(),
2724 Operand(var->name()));
2725 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(r3));
2726 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2727 __ CallStub(&stub);
2728
2729 } else if (var->mode() == LET && op != Token::INIT_LET) {
2730 // Non-initializing assignment to let variable needs a write barrier.
2731 DCHECK(!var->IsLookupSlot());
2732 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2733 Label assign;
2734 MemOperand location = VarOperand(var, r4);
2735 __ LoadP(r6, location);
2736 __ CompareRoot(r6, Heap::kTheHoleValueRootIndex);
2737 __ bne(&assign);
2738 __ mov(r6, Operand(var->name()));
2739 __ push(r6);
2740 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2741 // Perform the assignment.
2742 __ bind(&assign);
2743 EmitStoreToStackLocalOrContextSlot(var, location);
2744
2745 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2746 // Assignment to const variable needs a write barrier.
2747 DCHECK(!var->IsLookupSlot());
2748 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2749 Label const_error;
2750 MemOperand location = VarOperand(var, r4);
2751 __ LoadP(r6, location);
2752 __ CompareRoot(r6, Heap::kTheHoleValueRootIndex);
2753 __ bne(&const_error);
2754 __ mov(r6, Operand(var->name()));
2755 __ push(r6);
2756 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2757 __ bind(&const_error);
2758 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2759
2760 } else if (var->is_this() && op == Token::INIT_CONST) {
2761 // Initializing assignment to const {this} needs a write barrier.
2762 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2763 Label uninitialized_this;
2764 MemOperand location = VarOperand(var, r4);
2765 __ LoadP(r6, location);
2766 __ CompareRoot(r6, Heap::kTheHoleValueRootIndex);
2767 __ beq(&uninitialized_this);
2768 __ mov(r4, Operand(var->name()));
2769 __ push(r4);
2770 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2771 __ bind(&uninitialized_this);
2772 EmitStoreToStackLocalOrContextSlot(var, location);
2773
2774 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2775 if (var->IsLookupSlot()) {
2776 // Assignment to var.
2777 __ push(r3); // Value.
2778 __ mov(r4, Operand(var->name()));
2779 __ mov(r3, Operand(Smi::FromInt(language_mode())));
2780 __ Push(cp, r4, r3); // Context, name, language mode.
2781 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2782 } else {
2783 // Assignment to var or initializing assignment to let/const in harmony
2784 // mode.
2785 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2786 MemOperand location = VarOperand(var, r4);
2787 if (generate_debug_code_ && op == Token::INIT_LET) {
2788 // Check for an uninitialized let binding.
2789 __ LoadP(r5, location);
2790 __ CompareRoot(r5, Heap::kTheHoleValueRootIndex);
2791 __ Check(eq, kLetBindingReInitialization);
2792 }
2793 EmitStoreToStackLocalOrContextSlot(var, location);
2794 }
2795 } else if (op == Token::INIT_CONST_LEGACY) {
2796 // Const initializers need a write barrier.
2797 DCHECK(var->mode() == CONST_LEGACY);
2798 DCHECK(!var->IsParameter()); // No const parameters.
2799 if (var->IsLookupSlot()) {
2800 __ push(r3);
2801 __ mov(r3, Operand(var->name()));
2802 __ Push(cp, r3); // Context and name.
2803 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2804 } else {
2805 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2806 Label skip;
2807 MemOperand location = VarOperand(var, r4);
2808 __ LoadP(r5, location);
2809 __ CompareRoot(r5, Heap::kTheHoleValueRootIndex);
2810 __ bne(&skip);
2811 EmitStoreToStackLocalOrContextSlot(var, location);
2812 __ bind(&skip);
2813 }
2814
2815 } else {
2816 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2817 if (is_strict(language_mode())) {
2818 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2819 }
2820 // Silently ignore store in sloppy mode.
2821 }
2822 }
2823
2824
2825 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2826 // Assignment to a property, using a named store IC.
2827 Property* prop = expr->target()->AsProperty();
2828 DCHECK(prop != NULL);
2829 DCHECK(prop->key()->IsLiteral());
2830
2831 __ mov(StoreDescriptor::NameRegister(),
2832 Operand(prop->key()->AsLiteral()->value()));
2833 __ pop(StoreDescriptor::ReceiverRegister());
2834 if (FLAG_vector_stores) {
2835 EmitLoadStoreICSlot(expr->AssignmentSlot());
2836 CallStoreIC();
2837 } else {
2838 CallStoreIC(expr->AssignmentFeedbackId());
2839 }
2840
2841 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2842 context()->Plug(r3);
2843 }
2844
2845
2846 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2847 // Assignment to named property of super.
2848 // r3 : value
2849 // stack : receiver ('this'), home_object
2850 DCHECK(prop != NULL);
2851 Literal* key = prop->key()->AsLiteral();
2852 DCHECK(key != NULL);
2853
2854 __ Push(key->value());
2855 __ Push(r3);
2856 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2857 : Runtime::kStoreToSuper_Sloppy),
2858 4);
2859 }
2860
2861
2862 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2863 // Assignment to named property of super.
2864 // r3 : value
2865 // stack : receiver ('this'), home_object, key
2866 DCHECK(prop != NULL);
2867
2868 __ Push(r3);
2869 __ CallRuntime(
2870 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2871 : Runtime::kStoreKeyedToSuper_Sloppy),
2872 4);
2873 }
2874
2875
2876 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2877 // Assignment to a property, using a keyed store IC.
2878 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2879 DCHECK(StoreDescriptor::ValueRegister().is(r3));
2880
2881 Handle<Code> ic =
2882 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2883 if (FLAG_vector_stores) {
2884 EmitLoadStoreICSlot(expr->AssignmentSlot());
2885 CallIC(ic);
2886 } else {
2887 CallIC(ic, expr->AssignmentFeedbackId());
2888 }
2889
2890 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2891 context()->Plug(r3);
2892 }
2893
2894
2895 void FullCodeGenerator::VisitProperty(Property* expr) {
2896 Comment cmnt(masm_, "[ Property");
2897 SetExpressionPosition(expr);
2898
2899 Expression* key = expr->key();
2900
2901 if (key->IsPropertyName()) {
2902 if (!expr->IsSuperAccess()) {
2903 VisitForAccumulatorValue(expr->obj());
2904 __ Move(LoadDescriptor::ReceiverRegister(), r3);
2905 EmitNamedPropertyLoad(expr);
2906 } else {
2907 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2908 VisitForStackValue(
2909 expr->obj()->AsSuperPropertyReference()->home_object());
2910 EmitNamedSuperPropertyLoad(expr);
2911 }
2912 } else {
2913 if (!expr->IsSuperAccess()) {
2914 VisitForStackValue(expr->obj());
2915 VisitForAccumulatorValue(expr->key());
2916 __ Move(LoadDescriptor::NameRegister(), r3);
2917 __ pop(LoadDescriptor::ReceiverRegister());
2918 EmitKeyedPropertyLoad(expr);
2919 } else {
2920 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2921 VisitForStackValue(
2922 expr->obj()->AsSuperPropertyReference()->home_object());
2923 VisitForStackValue(expr->key());
2924 EmitKeyedSuperPropertyLoad(expr);
2925 }
2926 }
2927 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2928 context()->Plug(r3);
2929 }
2930
2931
2932 void FullCodeGenerator::CallIC(Handle<Code> code, TypeFeedbackId ast_id) {
2933 ic_total_count_++;
2934 __ Call(code, RelocInfo::CODE_TARGET, ast_id);
2935 }
2936
2937
2938 // Code common for calls using the IC.
2939 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2940 Expression* callee = expr->expression();
2941
2942 CallICState::CallType call_type =
2943 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2944
2945 // Get the target function.
2946 if (call_type == CallICState::FUNCTION) {
2947 {
2948 StackValueContext context(this);
2949 EmitVariableLoad(callee->AsVariableProxy());
2950 PrepareForBailout(callee, NO_REGISTERS);
2951 }
2952 // Push undefined as receiver. This is patched in the method prologue if it
2953 // is a sloppy mode method.
2954 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2955 __ push(r0);
2956 } else {
2957 // Load the function from the receiver.
2958 DCHECK(callee->IsProperty());
2959 DCHECK(!callee->AsProperty()->IsSuperAccess());
2960 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2961 EmitNamedPropertyLoad(callee->AsProperty());
2962 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2963 // Push the target function under the receiver.
2964 __ LoadP(r0, MemOperand(sp, 0));
2965 __ push(r0);
2966 __ StoreP(r3, MemOperand(sp, kPointerSize));
2967 }
2968
2969 EmitCall(expr, call_type);
2970 }
2971
2972
2973 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2974 Expression* callee = expr->expression();
2975 DCHECK(callee->IsProperty());
2976 Property* prop = callee->AsProperty();
2977 DCHECK(prop->IsSuperAccess());
2978 SetExpressionPosition(prop);
2979
2980 Literal* key = prop->key()->AsLiteral();
2981 DCHECK(!key->value()->IsSmi());
2982 // Load the function from the receiver.
2983 const Register scratch = r4;
2984 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2985 VisitForAccumulatorValue(super_ref->home_object());
2986 __ mr(scratch, r3);
2987 VisitForAccumulatorValue(super_ref->this_var());
2988 __ Push(scratch, r3, r3, scratch);
2989 __ Push(key->value());
2990 __ Push(Smi::FromInt(language_mode()));
2991
2992 // Stack here:
2993 // - home_object
2994 // - this (receiver)
2995 // - this (receiver) <-- LoadFromSuper will pop here and below.
2996 // - home_object
2997 // - key
2998 // - language_mode
2999 __ CallRuntime(Runtime::kLoadFromSuper, 4);
3000
3001 // Replace home_object with target function.
3002 __ StoreP(r3, MemOperand(sp, kPointerSize));
3003
3004 // Stack here:
3005 // - target function
3006 // - this (receiver)
3007 EmitCall(expr, CallICState::METHOD);
3008 }
3009
3010
3011 // Code common for calls using the IC.
3012 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr, Expression* key) {
3013 // Load the key.
3014 VisitForAccumulatorValue(key);
3015
3016 Expression* callee = expr->expression();
3017
3018 // Load the function from the receiver.
3019 DCHECK(callee->IsProperty());
3020 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3021 __ Move(LoadDescriptor::NameRegister(), r3);
3022 EmitKeyedPropertyLoad(callee->AsProperty());
3023 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3024
3025 // Push the target function under the receiver.
3026 __ LoadP(ip, MemOperand(sp, 0));
3027 __ push(ip);
3028 __ StoreP(r3, MemOperand(sp, kPointerSize));
3029
3030 EmitCall(expr, CallICState::METHOD);
3031 }
3032
3033
3034 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3035 Expression* callee = expr->expression();
3036 DCHECK(callee->IsProperty());
3037 Property* prop = callee->AsProperty();
3038 DCHECK(prop->IsSuperAccess());
3039
3040 SetExpressionPosition(prop);
3041 // Load the function from the receiver.
3042 const Register scratch = r4;
3043 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3044 VisitForAccumulatorValue(super_ref->home_object());
3045 __ mr(scratch, r3);
3046 VisitForAccumulatorValue(super_ref->this_var());
3047 __ Push(scratch, r3, r3, scratch);
3048 VisitForStackValue(prop->key());
3049 __ Push(Smi::FromInt(language_mode()));
3050
3051 // Stack here:
3052 // - home_object
3053 // - this (receiver)
3054 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3055 // - home_object
3056 // - key
3057 // - language_mode
3058 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3059
3060 // Replace home_object with target function.
3061 __ StoreP(r3, MemOperand(sp, kPointerSize));
3062
3063 // Stack here:
3064 // - target function
3065 // - this (receiver)
3066 EmitCall(expr, CallICState::METHOD);
3067 }
3068
3069
3070 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3071 // Load the arguments.
3072 ZoneList<Expression*>* args = expr->arguments();
3073 int arg_count = args->length();
3074 for (int i = 0; i < arg_count; i++) {
3075 VisitForStackValue(args->at(i));
3076 }
3077
3078 SetCallPosition(expr, arg_count);
3079 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3080 __ LoadSmiLiteral(r6, SmiFromSlot(expr->CallFeedbackICSlot()));
3081 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3082 // Don't assign a type feedback id to the IC, since type feedback is provided
3083 // by the vector above.
3084 CallIC(ic);
3085
3086 RecordJSReturnSite(expr);
3087 // Restore context register.
3088 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3089 context()->DropAndPlug(1, r3);
3090 }
3091
3092
3093 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3094 // r7: copy of the first argument or undefined if it doesn't exist.
3095 if (arg_count > 0) {
3096 __ LoadP(r7, MemOperand(sp, arg_count * kPointerSize), r0);
3097 } else {
3098 __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
3099 }
3100
3101 // r6: the receiver of the enclosing function.
3102 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3103
3104 // r5: language mode.
3105 __ LoadSmiLiteral(r5, Smi::FromInt(language_mode()));
3106
3107 // r4: the start position of the scope the calls resides in.
3108 __ LoadSmiLiteral(r4, Smi::FromInt(scope()->start_position()));
3109
3110 // Do the runtime call.
3111 __ Push(r7, r6, r5, r4);
3112 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3113 }
3114
3115
3116 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3117 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3118 VariableProxy* callee = expr->expression()->AsVariableProxy();
3119 if (callee->var()->IsLookupSlot()) {
3120 Label slow, done;
3121 SetExpressionPosition(callee);
3122 // Generate code for loading from variables potentially shadowed by
3123 // eval-introduced variables.
3124 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3125
3126 __ bind(&slow);
3127 // Call the runtime to find the function to call (returned in r3) and
3128 // the object holding it (returned in r4).
3129 DCHECK(!context_register().is(r5));
3130 __ mov(r5, Operand(callee->name()));
3131 __ Push(context_register(), r5);
3132 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3133 __ Push(r3, r4); // Function, receiver.
3134 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3135
3136 // If fast case code has been generated, emit code to push the function
3137 // and receiver and have the slow path jump around this code.
3138 if (done.is_linked()) {
3139 Label call;
3140 __ b(&call);
3141 __ bind(&done);
3142 // Push function.
3143 __ push(r3);
3144 // Pass undefined as the receiver, which is the WithBaseObject of a
3145 // non-object environment record. If the callee is sloppy, it will patch
3146 // it up to be the global receiver.
3147 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
3148 __ push(r4);
3149 __ bind(&call);
3150 }
3151 } else {
3152 VisitForStackValue(callee);
3153 // refEnv.WithBaseObject()
3154 __ LoadRoot(r5, Heap::kUndefinedValueRootIndex);
3155 __ push(r5); // Reserved receiver slot.
3156 }
3157 }
3158
3159
3160 void FullCodeGenerator::VisitCall(Call* expr) {
3161 #ifdef DEBUG
3162 // We want to verify that RecordJSReturnSite gets called on all paths
3163 // through this function. Avoid early returns.
3164 expr->return_is_recorded_ = false;
3165 #endif
3166
3167 Comment cmnt(masm_, "[ Call");
3168 Expression* callee = expr->expression();
3169 Call::CallType call_type = expr->GetCallType(isolate());
3170
3171 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3172 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3173 // to resolve the function we need to call. Then we call the resolved
3174 // function using the given arguments.
3175 ZoneList<Expression*>* args = expr->arguments();
3176 int arg_count = args->length();
3177
3178 PushCalleeAndWithBaseObject(expr);
3179
3180 // Push the arguments.
3181 for (int i = 0; i < arg_count; i++) {
3182 VisitForStackValue(args->at(i));
3183 }
3184
3185 // Push a copy of the function (found below the arguments) and
3186 // resolve eval.
3187 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3188 __ push(r4);
3189 EmitResolvePossiblyDirectEval(arg_count);
3190
3191 // Touch up the stack with the resolved function.
3192 __ StoreP(r3, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3193
3194 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3195
3196 // Record source position for debugger.
3197 SetCallPosition(expr, arg_count);
3198 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3199 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3200 __ CallStub(&stub);
3201 RecordJSReturnSite(expr);
3202 // Restore context register.
3203 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3204 context()->DropAndPlug(1, r3);
3205 } else if (call_type == Call::GLOBAL_CALL) {
3206 EmitCallWithLoadIC(expr);
3207
3208 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3209 // Call to a lookup slot (dynamically introduced variable).
3210 PushCalleeAndWithBaseObject(expr);
3211 EmitCall(expr);
3212 } else if (call_type == Call::PROPERTY_CALL) {
3213 Property* property = callee->AsProperty();
3214 bool is_named_call = property->key()->IsPropertyName();
3215 if (property->IsSuperAccess()) {
3216 if (is_named_call) {
3217 EmitSuperCallWithLoadIC(expr);
3218 } else {
3219 EmitKeyedSuperCallWithLoadIC(expr);
3220 }
3221 } else {
3222 VisitForStackValue(property->obj());
3223 if (is_named_call) {
3224 EmitCallWithLoadIC(expr);
3225 } else {
3226 EmitKeyedCallWithLoadIC(expr, property->key());
3227 }
3228 }
3229 } else if (call_type == Call::SUPER_CALL) {
3230 EmitSuperConstructorCall(expr);
3231 } else {
3232 DCHECK(call_type == Call::OTHER_CALL);
3233 // Call to an arbitrary expression not handled specially above.
3234 VisitForStackValue(callee);
3235 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
3236 __ push(r4);
3237 // Emit function call.
3238 EmitCall(expr);
3239 }
3240
3241 #ifdef DEBUG
3242 // RecordJSReturnSite should have been called.
3243 DCHECK(expr->return_is_recorded_);
3244 #endif
3245 }
3246
3247
3248 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3249 Comment cmnt(masm_, "[ CallNew");
3250 // According to ECMA-262, section 11.2.2, page 44, the function
3251 // expression in new calls must be evaluated before the
3252 // arguments.
3253
3254 // Push constructor on the stack. If it's not a function it's used as
3255 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3256 // ignored.
3257 DCHECK(!expr->expression()->IsSuperPropertyReference());
3258 VisitForStackValue(expr->expression());
3259
3260 // Push the arguments ("left-to-right") on the stack.
3261 ZoneList<Expression*>* args = expr->arguments();
3262 int arg_count = args->length();
3263 for (int i = 0; i < arg_count; i++) {
3264 VisitForStackValue(args->at(i));
3265 }
3266
3267 // Call the construct call builtin that handles allocation and
3268 // constructor invocation.
3269 SetConstructCallPosition(expr);
3270
3271 // Load function and argument count into r4 and r3.
3272 __ mov(r3, Operand(arg_count));
3273 __ LoadP(r4, MemOperand(sp, arg_count * kPointerSize), r0);
3274
3275 // Record call targets in unoptimized code.
3276 if (FLAG_pretenuring_call_new) {
3277 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3278 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3279 expr->CallNewFeedbackSlot().ToInt() + 1);
3280 }
3281
3282 __ Move(r5, FeedbackVector());
3283 __ LoadSmiLiteral(r6, SmiFromSlot(expr->CallNewFeedbackSlot()));
3284
3285 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3286 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3287 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3288 context()->Plug(r3);
3289 }
3290
3291
3292 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3293 SuperCallReference* super_call_ref =
3294 expr->expression()->AsSuperCallReference();
3295 DCHECK_NOT_NULL(super_call_ref);
3296
3297 EmitLoadSuperConstructor(super_call_ref);
3298 __ push(result_register());
3299
3300 // Push the arguments ("left-to-right") on the stack.
3301 ZoneList<Expression*>* args = expr->arguments();
3302 int arg_count = args->length();
3303 for (int i = 0; i < arg_count; i++) {
3304 VisitForStackValue(args->at(i));
3305 }
3306
3307 // Call the construct call builtin that handles allocation and
3308 // constructor invocation.
3309 SetConstructCallPosition(expr);
3310
3311 // Load original constructor into r7.
3312 VisitForAccumulatorValue(super_call_ref->new_target_var());
3313 __ mr(r7, result_register());
3314
3315 // Load function and argument count into r1 and r0.
3316 __ mov(r3, Operand(arg_count));
3317 __ LoadP(r4, MemOperand(sp, arg_count * kPointerSize));
3318
3319 // Record call targets in unoptimized code.
3320 if (FLAG_pretenuring_call_new) {
3321 UNREACHABLE();
3322 /* TODO(dslomov): support pretenuring.
3323 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3324 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3325 expr->CallNewFeedbackSlot().ToInt() + 1);
3326 */
3327 }
3328
3329 __ Move(r5, FeedbackVector());
3330 __ LoadSmiLiteral(r6, SmiFromSlot(expr->CallFeedbackSlot()));
3331
3332 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3333 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3334
3335 RecordJSReturnSite(expr);
3336
3337 context()->Plug(r3);
3338 }
3339
3340
3341 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3342 ZoneList<Expression*>* args = expr->arguments();
3343 DCHECK(args->length() == 1);
3344
3345 VisitForAccumulatorValue(args->at(0));
3346
3347 Label materialize_true, materialize_false;
3348 Label* if_true = NULL;
3349 Label* if_false = NULL;
3350 Label* fall_through = NULL;
3351 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3352 &if_false, &fall_through);
3353
3354 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3355 __ TestIfSmi(r3, r0);
3356 Split(eq, if_true, if_false, fall_through, cr0);
3357
3358 context()->Plug(if_true, if_false);
3359 }
3360
3361
3362 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3363 ZoneList<Expression*>* args = expr->arguments();
3364 DCHECK(args->length() == 1);
3365
3366 VisitForAccumulatorValue(args->at(0));
3367
3368 Label materialize_true, materialize_false;
3369 Label* if_true = NULL;
3370 Label* if_false = NULL;
3371 Label* fall_through = NULL;
3372 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3373 &if_false, &fall_through);
3374
3375 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3376 __ TestIfPositiveSmi(r3, r0);
3377 Split(eq, if_true, if_false, fall_through, cr0);
3378
3379 context()->Plug(if_true, if_false);
3380 }
3381
3382
3383 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3384 ZoneList<Expression*>* args = expr->arguments();
3385 DCHECK(args->length() == 1);
3386
3387 VisitForAccumulatorValue(args->at(0));
3388
3389 Label materialize_true, materialize_false;
3390 Label* if_true = NULL;
3391 Label* if_false = NULL;
3392 Label* fall_through = NULL;
3393 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3394 &if_false, &fall_through);
3395
3396 __ JumpIfSmi(r3, if_false);
3397 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3398 __ cmp(r3, ip);
3399 __ beq(if_true);
3400 __ LoadP(r5, FieldMemOperand(r3, HeapObject::kMapOffset));
3401 // Undetectable objects behave like undefined when tested with typeof.
3402 __ lbz(r4, FieldMemOperand(r5, Map::kBitFieldOffset));
3403 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
3404 __ bne(if_false, cr0);
3405 __ lbz(r4, FieldMemOperand(r5, Map::kInstanceTypeOffset));
3406 __ cmpi(r4, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
3407 __ blt(if_false);
3408 __ cmpi(r4, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
3409 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3410 Split(le, if_true, if_false, fall_through);
3411
3412 context()->Plug(if_true, if_false);
3413 }
3414
3415
3416 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3417 ZoneList<Expression*>* args = expr->arguments();
3418 DCHECK(args->length() == 1);
3419
3420 VisitForAccumulatorValue(args->at(0));
3421
3422 Label materialize_true, materialize_false;
3423 Label* if_true = NULL;
3424 Label* if_false = NULL;
3425 Label* fall_through = NULL;
3426 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3427 &if_false, &fall_through);
3428
3429 __ JumpIfSmi(r3, if_false);
3430 __ CompareObjectType(r3, r4, r4, FIRST_SPEC_OBJECT_TYPE);
3431 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3432 Split(ge, if_true, if_false, fall_through);
3433
3434 context()->Plug(if_true, if_false);
3435 }
3436
3437
3438 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3439 ZoneList<Expression*>* args = expr->arguments();
3440 DCHECK(args->length() == 1);
3441
3442 VisitForAccumulatorValue(args->at(0));
3443
3444 Label materialize_true, materialize_false;
3445 Label* if_true = NULL;
3446 Label* if_false = NULL;
3447 Label* fall_through = NULL;
3448 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3449 &if_false, &fall_through);
3450
3451 __ JumpIfSmi(r3, if_false);
3452 __ LoadP(r4, FieldMemOperand(r3, HeapObject::kMapOffset));
3453 __ lbz(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
3454 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
3455 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3456 Split(ne, if_true, if_false, fall_through, cr0);
3457
3458 context()->Plug(if_true, if_false);
3459 }
3460
3461
3462 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3463 CallRuntime* expr) {
3464 ZoneList<Expression*>* args = expr->arguments();
3465 DCHECK(args->length() == 1);
3466
3467 VisitForAccumulatorValue(args->at(0));
3468
3469 Label materialize_true, materialize_false, skip_lookup;
3470 Label* if_true = NULL;
3471 Label* if_false = NULL;
3472 Label* fall_through = NULL;
3473 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3474 &if_false, &fall_through);
3475
3476 __ AssertNotSmi(r3);
3477
3478 __ LoadP(r4, FieldMemOperand(r3, HeapObject::kMapOffset));
3479 __ lbz(ip, FieldMemOperand(r4, Map::kBitField2Offset));
3480 __ andi(r0, ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3481 __ bne(&skip_lookup, cr0);
3482
3483 // Check for fast case object. Generate false result for slow case object.
3484 __ LoadP(r5, FieldMemOperand(r3, JSObject::kPropertiesOffset));
3485 __ LoadP(r5, FieldMemOperand(r5, HeapObject::kMapOffset));
3486 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
3487 __ cmp(r5, ip);
3488 __ beq(if_false);
3489
3490 // Look for valueOf name in the descriptor array, and indicate false if
3491 // found. Since we omit an enumeration index check, if it is added via a
3492 // transition that shares its descriptor array, this is a false positive.
3493 Label entry, loop, done;
3494
3495 // Skip loop if no descriptors are valid.
3496 __ NumberOfOwnDescriptors(r6, r4);
3497 __ cmpi(r6, Operand::Zero());
3498 __ beq(&done);
3499
3500 __ LoadInstanceDescriptors(r4, r7);
3501 // r7: descriptor array.
3502 // r6: valid entries in the descriptor array.
3503 __ mov(ip, Operand(DescriptorArray::kDescriptorSize));
3504 __ Mul(r6, r6, ip);
3505 // Calculate location of the first key name.
3506 __ addi(r7, r7, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3507 // Calculate the end of the descriptor array.
3508 __ mr(r5, r7);
3509 __ ShiftLeftImm(ip, r6, Operand(kPointerSizeLog2));
3510 __ add(r5, r5, ip);
3511
3512 // Loop through all the keys in the descriptor array. If one of these is the
3513 // string "valueOf" the result is false.
3514 // The use of ip to store the valueOf string assumes that it is not otherwise
3515 // used in the loop below.
3516 __ mov(ip, Operand(isolate()->factory()->value_of_string()));
3517 __ b(&entry);
3518 __ bind(&loop);
3519 __ LoadP(r6, MemOperand(r7, 0));
3520 __ cmp(r6, ip);
3521 __ beq(if_false);
3522 __ addi(r7, r7, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3523 __ bind(&entry);
3524 __ cmp(r7, r5);
3525 __ bne(&loop);
3526
3527 __ bind(&done);
3528
3529 // Set the bit in the map to indicate that there is no local valueOf field.
3530 __ lbz(r5, FieldMemOperand(r4, Map::kBitField2Offset));
3531 __ ori(r5, r5, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3532 __ stb(r5, FieldMemOperand(r4, Map::kBitField2Offset));
3533
3534 __ bind(&skip_lookup);
3535
3536 // If a valueOf property is not found on the object check that its
3537 // prototype is the un-modified String prototype. If not result is false.
3538 __ LoadP(r5, FieldMemOperand(r4, Map::kPrototypeOffset));
3539 __ JumpIfSmi(r5, if_false);
3540 __ LoadP(r5, FieldMemOperand(r5, HeapObject::kMapOffset));
3541 __ LoadP(r6, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3542 __ LoadP(r6, FieldMemOperand(r6, GlobalObject::kNativeContextOffset));
3543 __ LoadP(r6,
3544 ContextOperand(r6, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3545 __ cmp(r5, r6);
3546 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3547 Split(eq, if_true, if_false, fall_through);
3548
3549 context()->Plug(if_true, if_false);
3550 }
3551
3552
3553 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3554 ZoneList<Expression*>* args = expr->arguments();
3555 DCHECK(args->length() == 1);
3556
3557 VisitForAccumulatorValue(args->at(0));
3558
3559 Label materialize_true, materialize_false;
3560 Label* if_true = NULL;
3561 Label* if_false = NULL;
3562 Label* fall_through = NULL;
3563 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3564 &if_false, &fall_through);
3565
3566 __ JumpIfSmi(r3, if_false);
3567 __ CompareObjectType(r3, r4, r5, JS_FUNCTION_TYPE);
3568 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3569 Split(eq, if_true, if_false, fall_through);
3570
3571 context()->Plug(if_true, if_false);
3572 }
3573
3574
3575 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3576 ZoneList<Expression*>* args = expr->arguments();
3577 DCHECK(args->length() == 1);
3578
3579 VisitForAccumulatorValue(args->at(0));
3580
3581 Label materialize_true, materialize_false;
3582 Label* if_true = NULL;
3583 Label* if_false = NULL;
3584 Label* fall_through = NULL;
3585 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3586 &if_false, &fall_through);
3587
3588 __ CheckMap(r3, r4, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3589 #if V8_TARGET_ARCH_PPC64
3590 __ LoadP(r4, FieldMemOperand(r3, HeapNumber::kValueOffset));
3591 __ li(r5, Operand(1));
3592 __ rotrdi(r5, r5, 1); // r5 = 0x80000000_00000000
3593 __ cmp(r4, r5);
3594 #else
3595 __ lwz(r5, FieldMemOperand(r3, HeapNumber::kExponentOffset));
3596 __ lwz(r4, FieldMemOperand(r3, HeapNumber::kMantissaOffset));
3597 Label skip;
3598 __ lis(r0, Operand(SIGN_EXT_IMM16(0x8000)));
3599 __ cmp(r5, r0);
3600 __ bne(&skip);
3601 __ cmpi(r4, Operand::Zero());
3602 __ bind(&skip);
3603 #endif
3604
3605 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3606 Split(eq, if_true, if_false, fall_through);
3607
3608 context()->Plug(if_true, if_false);
3609 }
3610
3611
3612 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3613 ZoneList<Expression*>* args = expr->arguments();
3614 DCHECK(args->length() == 1);
3615
3616 VisitForAccumulatorValue(args->at(0));
3617
3618 Label materialize_true, materialize_false;
3619 Label* if_true = NULL;
3620 Label* if_false = NULL;
3621 Label* fall_through = NULL;
3622 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3623 &if_false, &fall_through);
3624
3625 __ JumpIfSmi(r3, if_false);
3626 __ CompareObjectType(r3, r4, r4, JS_ARRAY_TYPE);
3627 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3628 Split(eq, if_true, if_false, fall_through);
3629
3630 context()->Plug(if_true, if_false);
3631 }
3632
3633
3634 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3635 ZoneList<Expression*>* args = expr->arguments();
3636 DCHECK(args->length() == 1);
3637
3638 VisitForAccumulatorValue(args->at(0));
3639
3640 Label materialize_true, materialize_false;
3641 Label* if_true = NULL;
3642 Label* if_false = NULL;
3643 Label* fall_through = NULL;
3644 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3645 &if_false, &fall_through);
3646
3647 __ JumpIfSmi(r3, if_false);
3648 __ CompareObjectType(r3, r4, r4, JS_TYPED_ARRAY_TYPE);
3649 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3650 Split(eq, if_true, if_false, fall_through);
3651
3652 context()->Plug(if_true, if_false);
3653 }
3654
3655
3656 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3657 ZoneList<Expression*>* args = expr->arguments();
3658 DCHECK(args->length() == 1);
3659
3660 VisitForAccumulatorValue(args->at(0));
3661
3662 Label materialize_true, materialize_false;
3663 Label* if_true = NULL;
3664 Label* if_false = NULL;
3665 Label* fall_through = NULL;
3666 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3667 &if_false, &fall_through);
3668
3669 __ JumpIfSmi(r3, if_false);
3670 __ CompareObjectType(r3, r4, r4, JS_REGEXP_TYPE);
3671 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3672 Split(eq, if_true, if_false, fall_through);
3673
3674 context()->Plug(if_true, if_false);
3675 }
3676
3677
3678 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3679 ZoneList<Expression*>* args = expr->arguments();
3680 DCHECK(args->length() == 1);
3681
3682 VisitForAccumulatorValue(args->at(0));
3683
3684 Label materialize_true, materialize_false;
3685 Label* if_true = NULL;
3686 Label* if_false = NULL;
3687 Label* fall_through = NULL;
3688 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3689 &if_false, &fall_through);
3690
3691 __ JumpIfSmi(r3, if_false);
3692 Register map = r4;
3693 Register type_reg = r5;
3694 __ LoadP(map, FieldMemOperand(r3, HeapObject::kMapOffset));
3695 __ lbz(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
3696 __ subi(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3697 __ cmpli(type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE));
3698 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3699 Split(le, if_true, if_false, fall_through);
3700
3701 context()->Plug(if_true, if_false);
3702 }
3703
3704
3705 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3706 DCHECK(expr->arguments()->length() == 0);
3707
3708 Label materialize_true, materialize_false;
3709 Label* if_true = NULL;
3710 Label* if_false = NULL;
3711 Label* fall_through = NULL;
3712 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3713 &if_false, &fall_through);
3714
3715 // Get the frame pointer for the calling frame.
3716 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3717
3718 // Skip the arguments adaptor frame if it exists.
3719 Label check_frame_marker;
3720 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kContextOffset));
3721 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
3722 __ bne(&check_frame_marker);
3723 __ LoadP(r5, MemOperand(r5, StandardFrameConstants::kCallerFPOffset));
3724
3725 // Check the marker in the calling frame.
3726 __ bind(&check_frame_marker);
3727 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kMarkerOffset));
3728 STATIC_ASSERT(StackFrame::CONSTRUCT < 0x4000);
3729 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::CONSTRUCT), r0);
3730 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3731 Split(eq, if_true, if_false, fall_through);
3732
3733 context()->Plug(if_true, if_false);
3734 }
3735
3736
3737 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3738 ZoneList<Expression*>* args = expr->arguments();
3739 DCHECK(args->length() == 2);
3740
3741 // Load the two objects into registers and perform the comparison.
3742 VisitForStackValue(args->at(0));
3743 VisitForAccumulatorValue(args->at(1));
3744
3745 Label materialize_true, materialize_false;
3746 Label* if_true = NULL;
3747 Label* if_false = NULL;
3748 Label* fall_through = NULL;
3749 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3750 &if_false, &fall_through);
3751
3752 __ pop(r4);
3753 __ cmp(r3, r4);
3754 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3755 Split(eq, if_true, if_false, fall_through);
3756
3757 context()->Plug(if_true, if_false);
3758 }
3759
3760
3761 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3762 ZoneList<Expression*>* args = expr->arguments();
3763 DCHECK(args->length() == 1);
3764
3765 // ArgumentsAccessStub expects the key in r4 and the formal
3766 // parameter count in r3.
3767 VisitForAccumulatorValue(args->at(0));
3768 __ mr(r4, r3);
3769 __ LoadSmiLiteral(r3, Smi::FromInt(info_->scope()->num_parameters()));
3770 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3771 __ CallStub(&stub);
3772 context()->Plug(r3);
3773 }
3774
3775
3776 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3777 DCHECK(expr->arguments()->length() == 0);
3778 Label exit;
3779 // Get the number of formal parameters.
3780 __ LoadSmiLiteral(r3, Smi::FromInt(info_->scope()->num_parameters()));
3781
3782 // Check if the calling frame is an arguments adaptor frame.
3783 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3784 __ LoadP(r6, MemOperand(r5, StandardFrameConstants::kContextOffset));
3785 __ CmpSmiLiteral(r6, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
3786 __ bne(&exit);
3787
3788 // Arguments adaptor case: Read the arguments length from the
3789 // adaptor frame.
3790 __ LoadP(r3, MemOperand(r5, ArgumentsAdaptorFrameConstants::kLengthOffset));
3791
3792 __ bind(&exit);
3793 context()->Plug(r3);
3794 }
3795
3796
3797 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3798 ZoneList<Expression*>* args = expr->arguments();
3799 DCHECK(args->length() == 1);
3800 Label done, null, function, non_function_constructor;
3801
3802 VisitForAccumulatorValue(args->at(0));
3803
3804 // If the object is a smi, we return null.
3805 __ JumpIfSmi(r3, &null);
3806
3807 // Check that the object is a JS object but take special care of JS
3808 // functions to make sure they have 'Function' as their class.
3809 // Assume that there are only two callable types, and one of them is at
3810 // either end of the type range for JS object types. Saves extra comparisons.
3811 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3812 __ CompareObjectType(r3, r3, r4, FIRST_SPEC_OBJECT_TYPE);
3813 // Map is now in r3.
3814 __ blt(&null);
3815 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3816 FIRST_SPEC_OBJECT_TYPE + 1);
3817 __ beq(&function);
3818
3819 __ cmpi(r4, Operand(LAST_SPEC_OBJECT_TYPE));
3820 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_SPEC_OBJECT_TYPE - 1);
3821 __ beq(&function);
3822 // Assume that there is no larger type.
3823 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3824
3825 // Check if the constructor in the map is a JS function.
3826 Register instance_type = r5;
3827 __ GetMapConstructor(r3, r3, r4, instance_type);
3828 __ cmpi(instance_type, Operand(JS_FUNCTION_TYPE));
3829 __ bne(&non_function_constructor);
3830
3831 // r3 now contains the constructor function. Grab the
3832 // instance class name from there.
3833 __ LoadP(r3, FieldMemOperand(r3, JSFunction::kSharedFunctionInfoOffset));
3834 __ LoadP(r3,
3835 FieldMemOperand(r3, SharedFunctionInfo::kInstanceClassNameOffset));
3836 __ b(&done);
3837
3838 // Functions have class 'Function'.
3839 __ bind(&function);
3840 __ LoadRoot(r3, Heap::kFunction_stringRootIndex);
3841 __ b(&done);
3842
3843 // Objects with a non-function constructor have class 'Object'.
3844 __ bind(&non_function_constructor);
3845 __ LoadRoot(r3, Heap::kObject_stringRootIndex);
3846 __ b(&done);
3847
3848 // Non-JS objects have class null.
3849 __ bind(&null);
3850 __ LoadRoot(r3, Heap::kNullValueRootIndex);
3851
3852 // All done.
3853 __ bind(&done);
3854
3855 context()->Plug(r3);
3856 }
3857
3858
3859 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3860 ZoneList<Expression*>* args = expr->arguments();
3861 DCHECK(args->length() == 1);
3862 VisitForAccumulatorValue(args->at(0)); // Load the object.
3863
3864 Label done;
3865 // If the object is a smi return the object.
3866 __ JumpIfSmi(r3, &done);
3867 // If the object is not a value type, return the object.
3868 __ CompareObjectType(r3, r4, r4, JS_VALUE_TYPE);
3869 __ bne(&done);
3870 __ LoadP(r3, FieldMemOperand(r3, JSValue::kValueOffset));
3871
3872 __ bind(&done);
3873 context()->Plug(r3);
3874 }
3875
3876
3877 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3878 ZoneList<Expression*>* args = expr->arguments();
3879 DCHECK_EQ(1, args->length());
3880
3881 VisitForAccumulatorValue(args->at(0));
3882
3883 Label materialize_true, materialize_false;
3884 Label* if_true = nullptr;
3885 Label* if_false = nullptr;
3886 Label* fall_through = nullptr;
3887 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3888 &if_false, &fall_through);
3889
3890 __ JumpIfSmi(r3, if_false);
3891 __ CompareObjectType(r3, r4, r4, JS_DATE_TYPE);
3892 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3893 Split(eq, if_true, if_false, fall_through);
3894
3895 context()->Plug(if_true, if_false);
3896 }
3897
3898
3899 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3900 ZoneList<Expression*>* args = expr->arguments();
3901 DCHECK(args->length() == 2);
3902 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3903 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3904
3905 VisitForAccumulatorValue(args->at(0)); // Load the object.
3906
3907 Register object = r3;
3908 Register result = r3;
3909 Register scratch0 = r11;
3910 Register scratch1 = r4;
3911
3912 if (index->value() == 0) {
3913 __ LoadP(result, FieldMemOperand(object, JSDate::kValueOffset));
3914 } else {
3915 Label runtime, done;
3916 if (index->value() < JSDate::kFirstUncachedField) {
3917 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3918 __ mov(scratch1, Operand(stamp));
3919 __ LoadP(scratch1, MemOperand(scratch1));
3920 __ LoadP(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3921 __ cmp(scratch1, scratch0);
3922 __ bne(&runtime);
3923 __ LoadP(result,
3924 FieldMemOperand(object, JSDate::kValueOffset +
3925 kPointerSize * index->value()),
3926 scratch0);
3927 __ b(&done);
3928 }
3929 __ bind(&runtime);
3930 __ PrepareCallCFunction(2, scratch1);
3931 __ LoadSmiLiteral(r4, index);
3932 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3933 __ bind(&done);
3934 }
3935
3936 context()->Plug(result);
3937 }
3938
3939
3940 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3941 ZoneList<Expression*>* args = expr->arguments();
3942 DCHECK_EQ(3, args->length());
3943
3944 Register string = r3;
3945 Register index = r4;
3946 Register value = r5;
3947
3948 VisitForStackValue(args->at(0)); // index
3949 VisitForStackValue(args->at(1)); // value
3950 VisitForAccumulatorValue(args->at(2)); // string
3951 __ Pop(index, value);
3952
3953 if (FLAG_debug_code) {
3954 __ TestIfSmi(value, r0);
3955 __ Check(eq, kNonSmiValue, cr0);
3956 __ TestIfSmi(index, r0);
3957 __ Check(eq, kNonSmiIndex, cr0);
3958 __ SmiUntag(index, index);
3959 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3960 __ EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
3961 __ SmiTag(index, index);
3962 }
3963
3964 __ SmiUntag(value);
3965 __ addi(ip, string, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3966 __ SmiToByteArrayOffset(r0, index);
3967 __ stbx(value, MemOperand(ip, r0));
3968 context()->Plug(string);
3969 }
3970
3971
3972 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3973 ZoneList<Expression*>* args = expr->arguments();
3974 DCHECK_EQ(3, args->length());
3975
3976 Register string = r3;
3977 Register index = r4;
3978 Register value = r5;
3979
3980 VisitForStackValue(args->at(0)); // index
3981 VisitForStackValue(args->at(1)); // value
3982 VisitForAccumulatorValue(args->at(2)); // string
3983 __ Pop(index, value);
3984
3985 if (FLAG_debug_code) {
3986 __ TestIfSmi(value, r0);
3987 __ Check(eq, kNonSmiValue, cr0);
3988 __ TestIfSmi(index, r0);
3989 __ Check(eq, kNonSmiIndex, cr0);
3990 __ SmiUntag(index, index);
3991 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3992 __ EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
3993 __ SmiTag(index, index);
3994 }
3995
3996 __ SmiUntag(value);
3997 __ addi(ip, string, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3998 __ SmiToShortArrayOffset(r0, index);
3999 __ sthx(value, MemOperand(ip, r0));
4000 context()->Plug(string);
4001 }
4002
4003
4004 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
4005 ZoneList<Expression*>* args = expr->arguments();
4006 DCHECK(args->length() == 2);
4007 VisitForStackValue(args->at(0)); // Load the object.
4008 VisitForAccumulatorValue(args->at(1)); // Load the value.
4009 __ pop(r4); // r3 = value. r4 = object.
4010
4011 Label done;
4012 // If the object is a smi, return the value.
4013 __ JumpIfSmi(r4, &done);
4014
4015 // If the object is not a value type, return the value.
4016 __ CompareObjectType(r4, r5, r5, JS_VALUE_TYPE);
4017 __ bne(&done);
4018
4019 // Store the value.
4020 __ StoreP(r3, FieldMemOperand(r4, JSValue::kValueOffset), r0);
4021 // Update the write barrier. Save the value as it will be
4022 // overwritten by the write barrier code and is needed afterward.
4023 __ mr(r5, r3);
4024 __ RecordWriteField(r4, JSValue::kValueOffset, r5, r6, kLRHasBeenSaved,
4025 kDontSaveFPRegs);
4026
4027 __ bind(&done);
4028 context()->Plug(r3);
4029 }
4030
4031
4032 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4033 ZoneList<Expression*>* args = expr->arguments();
4034 DCHECK_EQ(args->length(), 1);
4035 // Load the argument into r3 and call the stub.
4036 VisitForAccumulatorValue(args->at(0));
4037
4038 NumberToStringStub stub(isolate());
4039 __ CallStub(&stub);
4040 context()->Plug(r3);
4041 }
4042
4043
4044 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4045 ZoneList<Expression*>* args = expr->arguments();
4046 DCHECK(args->length() == 1);
4047 VisitForAccumulatorValue(args->at(0));
4048
4049 Label done;
4050 StringCharFromCodeGenerator generator(r3, r4);
4051 generator.GenerateFast(masm_);
4052 __ b(&done);
4053
4054 NopRuntimeCallHelper call_helper;
4055 generator.GenerateSlow(masm_, call_helper);
4056
4057 __ bind(&done);
4058 context()->Plug(r4);
4059 }
4060
4061
4062 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4063 ZoneList<Expression*>* args = expr->arguments();
4064 DCHECK(args->length() == 2);
4065 VisitForStackValue(args->at(0));
4066 VisitForAccumulatorValue(args->at(1));
4067
4068 Register object = r4;
4069 Register index = r3;
4070 Register result = r6;
4071
4072 __ pop(object);
4073
4074 Label need_conversion;
4075 Label index_out_of_range;
4076 Label done;
4077 StringCharCodeAtGenerator generator(object, index, result, &need_conversion,
4078 &need_conversion, &index_out_of_range,
4079 STRING_INDEX_IS_NUMBER);
4080 generator.GenerateFast(masm_);
4081 __ b(&done);
4082
4083 __ bind(&index_out_of_range);
4084 // When the index is out of range, the spec requires us to return
4085 // NaN.
4086 __ LoadRoot(result, Heap::kNanValueRootIndex);
4087 __ b(&done);
4088
4089 __ bind(&need_conversion);
4090 // Load the undefined value into the result register, which will
4091 // trigger conversion.
4092 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4093 __ b(&done);
4094
4095 NopRuntimeCallHelper call_helper;
4096 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4097
4098 __ bind(&done);
4099 context()->Plug(result);
4100 }
4101
4102
4103 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4104 ZoneList<Expression*>* args = expr->arguments();
4105 DCHECK(args->length() == 2);
4106 VisitForStackValue(args->at(0));
4107 VisitForAccumulatorValue(args->at(1));
4108
4109 Register object = r4;
4110 Register index = r3;
4111 Register scratch = r6;
4112 Register result = r3;
4113
4114 __ pop(object);
4115
4116 Label need_conversion;
4117 Label index_out_of_range;
4118 Label done;
4119 StringCharAtGenerator generator(object, index, scratch, result,
4120 &need_conversion, &need_conversion,
4121 &index_out_of_range, STRING_INDEX_IS_NUMBER);
4122 generator.GenerateFast(masm_);
4123 __ b(&done);
4124
4125 __ bind(&index_out_of_range);
4126 // When the index is out of range, the spec requires us to return
4127 // the empty string.
4128 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4129 __ b(&done);
4130
4131 __ bind(&need_conversion);
4132 // Move smi zero into the result register, which will trigger
4133 // conversion.
4134 __ LoadSmiLiteral(result, Smi::FromInt(0));
4135 __ b(&done);
4136
4137 NopRuntimeCallHelper call_helper;
4138 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4139
4140 __ bind(&done);
4141 context()->Plug(result);
4142 }
4143
4144
4145 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4146 ZoneList<Expression*>* args = expr->arguments();
4147 DCHECK_EQ(2, args->length());
4148 VisitForStackValue(args->at(0));
4149 VisitForAccumulatorValue(args->at(1));
4150
4151 __ pop(r4);
4152 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4153 __ CallStub(&stub);
4154 context()->Plug(r3);
4155 }
4156
4157
4158 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4159 ZoneList<Expression*>* args = expr->arguments();
4160 DCHECK(args->length() >= 2);
4161
4162 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4163 for (int i = 0; i < arg_count + 1; i++) {
4164 VisitForStackValue(args->at(i));
4165 }
4166 VisitForAccumulatorValue(args->last()); // Function.
4167
4168 Label runtime, done;
4169 // Check for non-function argument (including proxy).
4170 __ JumpIfSmi(r3, &runtime);
4171 __ CompareObjectType(r3, r4, r4, JS_FUNCTION_TYPE);
4172 __ bne(&runtime);
4173
4174 // InvokeFunction requires the function in r4. Move it in there.
4175 __ mr(r4, result_register());
4176 ParameterCount count(arg_count);
4177 __ InvokeFunction(r4, count, CALL_FUNCTION, NullCallWrapper());
4178 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4179 __ b(&done);
4180
4181 __ bind(&runtime);
4182 __ push(r3);
4183 __ CallRuntime(Runtime::kCall, args->length());
4184 __ bind(&done);
4185
4186 context()->Plug(r3);
4187 }
4188
4189
4190 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4191 ZoneList<Expression*>* args = expr->arguments();
4192 DCHECK(args->length() == 2);
4193
4194 // new.target
4195 VisitForStackValue(args->at(0));
4196
4197 // .this_function
4198 VisitForStackValue(args->at(1));
4199 __ CallRuntime(Runtime::kGetPrototype, 1);
4200 __ mr(r4, result_register());
4201 __ Push(r4);
4202
4203 // Load original constructor into r7.
4204 __ LoadP(r7, MemOperand(sp, 1 * kPointerSize));
4205
4206 // Check if the calling frame is an arguments adaptor frame.
4207 Label adaptor_frame, args_set_up, runtime;
4208 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4209 __ LoadP(r6, MemOperand(r5, StandardFrameConstants::kContextOffset));
4210 __ CmpSmiLiteral(r6, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
4211 __ beq(&adaptor_frame);
4212
4213 // default constructor has no arguments, so no adaptor frame means no args.
4214 __ li(r3, Operand::Zero());
4215 __ b(&args_set_up);
4216
4217 // Copy arguments from adaptor frame.
4218 {
4219 __ bind(&adaptor_frame);
4220 __ LoadP(r3, MemOperand(r5, ArgumentsAdaptorFrameConstants::kLengthOffset));
4221 __ SmiUntag(r3);
4222
4223 // Get arguments pointer in r5.
4224 __ ShiftLeftImm(r0, r3, Operand(kPointerSizeLog2));
4225 __ add(r5, r5, r0);
4226 __ addi(r5, r5, Operand(StandardFrameConstants::kCallerSPOffset));
4227
4228 Label loop;
4229 __ mtctr(r3);
4230 __ bind(&loop);
4231 // Pre-decrement in order to skip receiver.
4232 __ LoadPU(r6, MemOperand(r5, -kPointerSize));
4233 __ Push(r6);
4234 __ bdnz(&loop);
4235 }
4236
4237 __ bind(&args_set_up);
4238 __ LoadRoot(r5, Heap::kUndefinedValueRootIndex);
4239
4240 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4241 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4242
4243 __ Drop(1);
4244
4245 context()->Plug(result_register());
4246 }
4247
4248
4249 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4250 RegExpConstructResultStub stub(isolate());
4251 ZoneList<Expression*>* args = expr->arguments();
4252 DCHECK(args->length() == 3);
4253 VisitForStackValue(args->at(0));
4254 VisitForStackValue(args->at(1));
4255 VisitForAccumulatorValue(args->at(2));
4256 __ Pop(r5, r4);
4257 __ CallStub(&stub);
4258 context()->Plug(r3);
4259 }
4260
4261
4262 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4263 ZoneList<Expression*>* args = expr->arguments();
4264 DCHECK_EQ(2, args->length());
4265 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4266 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4267
4268 Handle<FixedArray> jsfunction_result_caches(
4269 isolate()->native_context()->jsfunction_result_caches());
4270 if (jsfunction_result_caches->length() <= cache_id) {
4271 __ Abort(kAttemptToUseUndefinedCache);
4272 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
4273 context()->Plug(r3);
4274 return;
4275 }
4276
4277 VisitForAccumulatorValue(args->at(1));
4278
4279 Register key = r3;
4280 Register cache = r4;
4281 __ LoadP(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
4282 __ LoadP(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4283 __ LoadP(cache,
4284 ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4285 __ LoadP(cache,
4286 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)), r0);
4287
4288 Label done, not_found;
4289 __ LoadP(r5, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
4290 // r5 now holds finger offset as a smi.
4291 __ addi(r6, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4292 // r6 now points to the start of fixed array elements.
4293 __ SmiToPtrArrayOffset(r5, r5);
4294 __ LoadPUX(r5, MemOperand(r6, r5));
4295 // r6 now points to the key of the pair.
4296 __ cmp(key, r5);
4297 __ bne(&not_found);
4298
4299 __ LoadP(r3, MemOperand(r6, kPointerSize));
4300 __ b(&done);
4301
4302 __ bind(&not_found);
4303 // Call runtime to perform the lookup.
4304 __ Push(cache, key);
4305 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4306
4307 __ bind(&done);
4308 context()->Plug(r3);
4309 }
4310
4311
4312 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4313 ZoneList<Expression*>* args = expr->arguments();
4314 VisitForAccumulatorValue(args->at(0));
4315
4316 Label materialize_true, materialize_false;
4317 Label* if_true = NULL;
4318 Label* if_false = NULL;
4319 Label* fall_through = NULL;
4320 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
4321 &if_false, &fall_through);
4322
4323 __ lwz(r3, FieldMemOperand(r3, String::kHashFieldOffset));
4324 // PPC - assume ip is free
4325 __ mov(ip, Operand(String::kContainsCachedArrayIndexMask));
4326 __ and_(r0, r3, ip, SetRC);
4327 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4328 Split(eq, if_true, if_false, fall_through, cr0);
4329
4330 context()->Plug(if_true, if_false);
4331 }
4332
4333
4334 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4335 ZoneList<Expression*>* args = expr->arguments();
4336 DCHECK(args->length() == 1);
4337 VisitForAccumulatorValue(args->at(0));
4338
4339 __ AssertString(r3);
4340
4341 __ lwz(r3, FieldMemOperand(r3, String::kHashFieldOffset));
4342 __ IndexFromHash(r3, r3);
4343
4344 context()->Plug(r3);
4345 }
4346
4347
4348 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4349 Label bailout, done, one_char_separator, long_separator, non_trivial_array,
4350 not_size_one_array, loop, empty_separator_loop, one_char_separator_loop,
4351 one_char_separator_loop_entry, long_separator_loop;
4352 ZoneList<Expression*>* args = expr->arguments();
4353 DCHECK(args->length() == 2);
4354 VisitForStackValue(args->at(1));
4355 VisitForAccumulatorValue(args->at(0));
4356
4357 // All aliases of the same register have disjoint lifetimes.
4358 Register array = r3;
4359 Register elements = no_reg; // Will be r3.
4360 Register result = no_reg; // Will be r3.
4361 Register separator = r4;
4362 Register array_length = r5;
4363 Register result_pos = no_reg; // Will be r5
4364 Register string_length = r6;
4365 Register string = r7;
4366 Register element = r8;
4367 Register elements_end = r9;
4368 Register scratch1 = r10;
4369 Register scratch2 = r11;
4370
4371 // Separator operand is on the stack.
4372 __ pop(separator);
4373
4374 // Check that the array is a JSArray.
4375 __ JumpIfSmi(array, &bailout);
4376 __ CompareObjectType(array, scratch1, scratch2, JS_ARRAY_TYPE);
4377 __ bne(&bailout);
4378
4379 // Check that the array has fast elements.
4380 __ CheckFastElements(scratch1, scratch2, &bailout);
4381
4382 // If the array has length zero, return the empty string.
4383 __ LoadP(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4384 __ SmiUntag(array_length);
4385 __ cmpi(array_length, Operand::Zero());
4386 __ bne(&non_trivial_array);
4387 __ LoadRoot(r3, Heap::kempty_stringRootIndex);
4388 __ b(&done);
4389
4390 __ bind(&non_trivial_array);
4391
4392 // Get the FixedArray containing array's elements.
4393 elements = array;
4394 __ LoadP(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4395 array = no_reg; // End of array's live range.
4396
4397 // Check that all array elements are sequential one-byte strings, and
4398 // accumulate the sum of their lengths, as a smi-encoded value.
4399 __ li(string_length, Operand::Zero());
4400 __ addi(element, elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4401 __ ShiftLeftImm(elements_end, array_length, Operand(kPointerSizeLog2));
4402 __ add(elements_end, element, elements_end);
4403 // Loop condition: while (element < elements_end).
4404 // Live values in registers:
4405 // elements: Fixed array of strings.
4406 // array_length: Length of the fixed array of strings (not smi)
4407 // separator: Separator string
4408 // string_length: Accumulated sum of string lengths (smi).
4409 // element: Current array element.
4410 // elements_end: Array end.
4411 if (generate_debug_code_) {
4412 __ cmpi(array_length, Operand::Zero());
4413 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin);
4414 }
4415 __ bind(&loop);
4416 __ LoadP(string, MemOperand(element));
4417 __ addi(element, element, Operand(kPointerSize));
4418 __ JumpIfSmi(string, &bailout);
4419 __ LoadP(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4420 __ lbz(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4421 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4422 __ LoadP(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4423
4424 __ AddAndCheckForOverflow(string_length, string_length, scratch1, scratch2,
4425 r0);
4426 __ BranchOnOverflow(&bailout);
4427
4428 __ cmp(element, elements_end);
4429 __ blt(&loop);
4430
4431 // If array_length is 1, return elements[0], a string.
4432 __ cmpi(array_length, Operand(1));
4433 __ bne(&not_size_one_array);
4434 __ LoadP(r3, FieldMemOperand(elements, FixedArray::kHeaderSize));
4435 __ b(&done);
4436
4437 __ bind(&not_size_one_array);
4438
4439 // Live values in registers:
4440 // separator: Separator string
4441 // array_length: Length of the array.
4442 // string_length: Sum of string lengths (smi).
4443 // elements: FixedArray of strings.
4444
4445 // Check that the separator is a flat one-byte string.
4446 __ JumpIfSmi(separator, &bailout);
4447 __ LoadP(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4448 __ lbz(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4449 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4450
4451 // Add (separator length times array_length) - separator length to the
4452 // string_length to get the length of the result string.
4453 __ LoadP(scratch1,
4454 FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4455 __ sub(string_length, string_length, scratch1);
4456 #if V8_TARGET_ARCH_PPC64
4457 __ SmiUntag(scratch1, scratch1);
4458 __ Mul(scratch2, array_length, scratch1);
4459 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4460 // zero.
4461 __ ShiftRightImm(ip, scratch2, Operand(31), SetRC);
4462 __ bne(&bailout, cr0);
4463 __ SmiTag(scratch2, scratch2);
4464 #else
4465 // array_length is not smi but the other values are, so the result is a smi
4466 __ mullw(scratch2, array_length, scratch1);
4467 __ mulhw(ip, array_length, scratch1);
4468 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4469 // zero.
4470 __ cmpi(ip, Operand::Zero());
4471 __ bne(&bailout);
4472 __ cmpwi(scratch2, Operand::Zero());
4473 __ blt(&bailout);
4474 #endif
4475
4476 __ AddAndCheckForOverflow(string_length, string_length, scratch2, scratch1,
4477 r0);
4478 __ BranchOnOverflow(&bailout);
4479 __ SmiUntag(string_length);
4480
4481 // Get first element in the array to free up the elements register to be used
4482 // for the result.
4483 __ addi(element, elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4484 result = elements; // End of live range for elements.
4485 elements = no_reg;
4486 // Live values in registers:
4487 // element: First array element
4488 // separator: Separator string
4489 // string_length: Length of result string (not smi)
4490 // array_length: Length of the array.
4491 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4492 elements_end, &bailout);
4493 // Prepare for looping. Set up elements_end to end of the array. Set
4494 // result_pos to the position of the result where to write the first
4495 // character.
4496 __ ShiftLeftImm(elements_end, array_length, Operand(kPointerSizeLog2));
4497 __ add(elements_end, element, elements_end);
4498 result_pos = array_length; // End of live range for array_length.
4499 array_length = no_reg;
4500 __ addi(result_pos, result,
4501 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4502
4503 // Check the length of the separator.
4504 __ LoadP(scratch1,
4505 FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4506 __ CmpSmiLiteral(scratch1, Smi::FromInt(1), r0);
4507 __ beq(&one_char_separator);
4508 __ bgt(&long_separator);
4509
4510 // Empty separator case
4511 __ bind(&empty_separator_loop);
4512 // Live values in registers:
4513 // result_pos: the position to which we are currently copying characters.
4514 // element: Current array element.
4515 // elements_end: Array end.
4516
4517 // Copy next array element to the result.
4518 __ LoadP(string, MemOperand(element));
4519 __ addi(element, element, Operand(kPointerSize));
4520 __ LoadP(string_length, FieldMemOperand(string, String::kLengthOffset));
4521 __ SmiUntag(string_length);
4522 __ addi(string, string,
4523 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4524 __ CopyBytes(string, result_pos, string_length, scratch1);
4525 __ cmp(element, elements_end);
4526 __ blt(&empty_separator_loop); // End while (element < elements_end).
4527 DCHECK(result.is(r3));
4528 __ b(&done);
4529
4530 // One-character separator case
4531 __ bind(&one_char_separator);
4532 // Replace separator with its one-byte character value.
4533 __ lbz(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4534 // Jump into the loop after the code that copies the separator, so the first
4535 // element is not preceded by a separator
4536 __ b(&one_char_separator_loop_entry);
4537
4538 __ bind(&one_char_separator_loop);
4539 // Live values in registers:
4540 // result_pos: the position to which we are currently copying characters.
4541 // element: Current array element.
4542 // elements_end: Array end.
4543 // separator: Single separator one-byte char (in lower byte).
4544
4545 // Copy the separator character to the result.
4546 __ stb(separator, MemOperand(result_pos));
4547 __ addi(result_pos, result_pos, Operand(1));
4548
4549 // Copy next array element to the result.
4550 __ bind(&one_char_separator_loop_entry);
4551 __ LoadP(string, MemOperand(element));
4552 __ addi(element, element, Operand(kPointerSize));
4553 __ LoadP(string_length, FieldMemOperand(string, String::kLengthOffset));
4554 __ SmiUntag(string_length);
4555 __ addi(string, string,
4556 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4557 __ CopyBytes(string, result_pos, string_length, scratch1);
4558 __ cmpl(element, elements_end);
4559 __ blt(&one_char_separator_loop); // End while (element < elements_end).
4560 DCHECK(result.is(r3));
4561 __ b(&done);
4562
4563 // Long separator case (separator is more than one character). Entry is at the
4564 // label long_separator below.
4565 __ bind(&long_separator_loop);
4566 // Live values in registers:
4567 // result_pos: the position to which we are currently copying characters.
4568 // element: Current array element.
4569 // elements_end: Array end.
4570 // separator: Separator string.
4571
4572 // Copy the separator to the result.
4573 __ LoadP(string_length, FieldMemOperand(separator, String::kLengthOffset));
4574 __ SmiUntag(string_length);
4575 __ addi(string, separator,
4576 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4577 __ CopyBytes(string, result_pos, string_length, scratch1);
4578
4579 __ bind(&long_separator);
4580 __ LoadP(string, MemOperand(element));
4581 __ addi(element, element, Operand(kPointerSize));
4582 __ LoadP(string_length, FieldMemOperand(string, String::kLengthOffset));
4583 __ SmiUntag(string_length);
4584 __ addi(string, string,
4585 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4586 __ CopyBytes(string, result_pos, string_length, scratch1);
4587 __ cmpl(element, elements_end);
4588 __ blt(&long_separator_loop); // End while (element < elements_end).
4589 DCHECK(result.is(r3));
4590 __ b(&done);
4591
4592 __ bind(&bailout);
4593 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
4594 __ bind(&done);
4595 context()->Plug(r3);
4596 }
4597
4598
4599 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4600 DCHECK(expr->arguments()->length() == 0);
4601 ExternalReference debug_is_active =
4602 ExternalReference::debug_is_active_address(isolate());
4603 __ mov(ip, Operand(debug_is_active));
4604 __ lbz(r3, MemOperand(ip));
4605 __ SmiTag(r3);
4606 context()->Plug(r3);
4607 }
4608
4609
4610 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4611 // Push the builtins object as the receiver.
4612 Register receiver = LoadDescriptor::ReceiverRegister();
4613 __ LoadP(receiver, GlobalObjectOperand());
4614 __ LoadP(receiver, FieldMemOperand(receiver, GlobalObject::kBuiltinsOffset));
4615 __ push(receiver);
4616
4617 // Load the function from the receiver.
4618 __ mov(LoadDescriptor::NameRegister(), Operand(expr->name()));
4619 __ mov(LoadDescriptor::SlotRegister(),
4620 Operand(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4621 CallLoadIC(NOT_INSIDE_TYPEOF);
4622 }
4623
4624
4625 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4626 ZoneList<Expression*>* args = expr->arguments();
4627 int arg_count = args->length();
4628
4629 SetCallPosition(expr, arg_count);
4630 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4631 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
4632 __ CallStub(&stub);
4633 }
4634
4635
4636 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4637 ZoneList<Expression*>* args = expr->arguments();
4638 int arg_count = args->length();
4639
4640 if (expr->is_jsruntime()) {
4641 Comment cmnt(masm_, "[ CallRuntime");
4642 EmitLoadJSRuntimeFunction(expr);
4643
4644 // Push the target function under the receiver.
4645 __ LoadP(ip, MemOperand(sp, 0));
4646 __ push(ip);
4647 __ StoreP(r3, MemOperand(sp, kPointerSize));
4648
4649 // Push the arguments ("left-to-right").
4650 for (int i = 0; i < arg_count; i++) {
4651 VisitForStackValue(args->at(i));
4652 }
4653
4654 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4655 EmitCallJSRuntimeFunction(expr);
4656
4657 // Restore context register.
4658 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4659
4660 context()->DropAndPlug(1, r3);
4661
4662 } else {
4663 const Runtime::Function* function = expr->function();
4664 switch (function->function_id) {
4665 #define CALL_INTRINSIC_GENERATOR(Name) \
4666 case Runtime::kInline##Name: { \
4667 Comment cmnt(masm_, "[ Inline" #Name); \
4668 return Emit##Name(expr); \
4669 }
4670 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4671 #undef CALL_INTRINSIC_GENERATOR
4672 default: {
4673 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4674 // Push the arguments ("left-to-right").
4675 for (int i = 0; i < arg_count; i++) {
4676 VisitForStackValue(args->at(i));
4677 }
4678
4679 // Call the C runtime function.
4680 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4681 __ CallRuntime(expr->function(), arg_count);
4682 context()->Plug(r3);
4683 }
4684 }
4685 }
4686 }
4687
4688
4689 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4690 switch (expr->op()) {
4691 case Token::DELETE: {
4692 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4693 Property* property = expr->expression()->AsProperty();
4694 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4695
4696 if (property != NULL) {
4697 VisitForStackValue(property->obj());
4698 VisitForStackValue(property->key());
4699 __ LoadSmiLiteral(r4, Smi::FromInt(language_mode()));
4700 __ push(r4);
4701 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4702 context()->Plug(r3);
4703 } else if (proxy != NULL) {
4704 Variable* var = proxy->var();
4705 // Delete of an unqualified identifier is disallowed in strict mode but
4706 // "delete this" is allowed.
4707 bool is_this = var->HasThisName(isolate());
4708 DCHECK(is_sloppy(language_mode()) || is_this);
4709 if (var->IsUnallocatedOrGlobalSlot()) {
4710 __ LoadP(r5, GlobalObjectOperand());
4711 __ mov(r4, Operand(var->name()));
4712 __ LoadSmiLiteral(r3, Smi::FromInt(SLOPPY));
4713 __ Push(r5, r4, r3);
4714 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4715 context()->Plug(r3);
4716 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4717 // Result of deleting non-global, non-dynamic variables is false.
4718 // The subexpression does not have side effects.
4719 context()->Plug(is_this);
4720 } else {
4721 // Non-global variable. Call the runtime to try to delete from the
4722 // context where the variable was introduced.
4723 DCHECK(!context_register().is(r5));
4724 __ mov(r5, Operand(var->name()));
4725 __ Push(context_register(), r5);
4726 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4727 context()->Plug(r3);
4728 }
4729 } else {
4730 // Result of deleting non-property, non-variable reference is true.
4731 // The subexpression may have side effects.
4732 VisitForEffect(expr->expression());
4733 context()->Plug(true);
4734 }
4735 break;
4736 }
4737
4738 case Token::VOID: {
4739 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4740 VisitForEffect(expr->expression());
4741 context()->Plug(Heap::kUndefinedValueRootIndex);
4742 break;
4743 }
4744
4745 case Token::NOT: {
4746 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4747 if (context()->IsEffect()) {
4748 // Unary NOT has no side effects so it's only necessary to visit the
4749 // subexpression. Match the optimizing compiler by not branching.
4750 VisitForEffect(expr->expression());
4751 } else if (context()->IsTest()) {
4752 const TestContext* test = TestContext::cast(context());
4753 // The labels are swapped for the recursive call.
4754 VisitForControl(expr->expression(), test->false_label(),
4755 test->true_label(), test->fall_through());
4756 context()->Plug(test->true_label(), test->false_label());
4757 } else {
4758 // We handle value contexts explicitly rather than simply visiting
4759 // for control and plugging the control flow into the context,
4760 // because we need to prepare a pair of extra administrative AST ids
4761 // for the optimizing compiler.
4762 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4763 Label materialize_true, materialize_false, done;
4764 VisitForControl(expr->expression(), &materialize_false,
4765 &materialize_true, &materialize_true);
4766 __ bind(&materialize_true);
4767 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4768 __ LoadRoot(r3, Heap::kTrueValueRootIndex);
4769 if (context()->IsStackValue()) __ push(r3);
4770 __ b(&done);
4771 __ bind(&materialize_false);
4772 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4773 __ LoadRoot(r3, Heap::kFalseValueRootIndex);
4774 if (context()->IsStackValue()) __ push(r3);
4775 __ bind(&done);
4776 }
4777 break;
4778 }
4779
4780 case Token::TYPEOF: {
4781 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4782 {
4783 AccumulatorValueContext context(this);
4784 VisitForTypeofValue(expr->expression());
4785 }
4786 __ mr(r6, r3);
4787 TypeofStub typeof_stub(isolate());
4788 __ CallStub(&typeof_stub);
4789 context()->Plug(r3);
4790 break;
4791 }
4792
4793 default:
4794 UNREACHABLE();
4795 }
4796 }
4797
4798
4799 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4800 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4801
4802 Comment cmnt(masm_, "[ CountOperation");
4803
4804 Property* prop = expr->expression()->AsProperty();
4805 LhsKind assign_type = Property::GetAssignType(prop);
4806
4807 // Evaluate expression and get value.
4808 if (assign_type == VARIABLE) {
4809 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4810 AccumulatorValueContext context(this);
4811 EmitVariableLoad(expr->expression()->AsVariableProxy());
4812 } else {
4813 // Reserve space for result of postfix operation.
4814 if (expr->is_postfix() && !context()->IsEffect()) {
4815 __ LoadSmiLiteral(ip, Smi::FromInt(0));
4816 __ push(ip);
4817 }
4818 switch (assign_type) {
4819 case NAMED_PROPERTY: {
4820 // Put the object both on the stack and in the register.
4821 VisitForStackValue(prop->obj());
4822 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4823 EmitNamedPropertyLoad(prop);
4824 break;
4825 }
4826
4827 case NAMED_SUPER_PROPERTY: {
4828 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4829 VisitForAccumulatorValue(
4830 prop->obj()->AsSuperPropertyReference()->home_object());
4831 __ Push(result_register());
4832 const Register scratch = r4;
4833 __ LoadP(scratch, MemOperand(sp, kPointerSize));
4834 __ Push(scratch, result_register());
4835 EmitNamedSuperPropertyLoad(prop);
4836 break;
4837 }
4838
4839 case KEYED_SUPER_PROPERTY: {
4840 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4841 VisitForAccumulatorValue(
4842 prop->obj()->AsSuperPropertyReference()->home_object());
4843 const Register scratch = r4;
4844 const Register scratch1 = r5;
4845 __ mr(scratch, result_register());
4846 VisitForAccumulatorValue(prop->key());
4847 __ Push(scratch, result_register());
4848 __ LoadP(scratch1, MemOperand(sp, 2 * kPointerSize));
4849 __ Push(scratch1, scratch, result_register());
4850 EmitKeyedSuperPropertyLoad(prop);
4851 break;
4852 }
4853
4854 case KEYED_PROPERTY: {
4855 VisitForStackValue(prop->obj());
4856 VisitForStackValue(prop->key());
4857 __ LoadP(LoadDescriptor::ReceiverRegister(),
4858 MemOperand(sp, 1 * kPointerSize));
4859 __ LoadP(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4860 EmitKeyedPropertyLoad(prop);
4861 break;
4862 }
4863
4864 case VARIABLE:
4865 UNREACHABLE();
4866 }
4867 }
4868
4869 // We need a second deoptimization point after loading the value
4870 // in case evaluating the property load my have a side effect.
4871 if (assign_type == VARIABLE) {
4872 PrepareForBailout(expr->expression(), TOS_REG);
4873 } else {
4874 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4875 }
4876
4877 // Inline smi case if we are in a loop.
4878 Label stub_call, done;
4879 JumpPatchSite patch_site(masm_);
4880
4881 int count_value = expr->op() == Token::INC ? 1 : -1;
4882 if (ShouldInlineSmiCase(expr->op())) {
4883 Label slow;
4884 patch_site.EmitJumpIfNotSmi(r3, &slow);
4885
4886 // Save result for postfix expressions.
4887 if (expr->is_postfix()) {
4888 if (!context()->IsEffect()) {
4889 // Save the result on the stack. If we have a named or keyed property
4890 // we store the result under the receiver that is currently on top
4891 // of the stack.
4892 switch (assign_type) {
4893 case VARIABLE:
4894 __ push(r3);
4895 break;
4896 case NAMED_PROPERTY:
4897 __ StoreP(r3, MemOperand(sp, kPointerSize));
4898 break;
4899 case NAMED_SUPER_PROPERTY:
4900 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4901 break;
4902 case KEYED_PROPERTY:
4903 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4904 break;
4905 case KEYED_SUPER_PROPERTY:
4906 __ StoreP(r3, MemOperand(sp, 3 * kPointerSize));
4907 break;
4908 }
4909 }
4910 }
4911
4912 Register scratch1 = r4;
4913 Register scratch2 = r5;
4914 __ LoadSmiLiteral(scratch1, Smi::FromInt(count_value));
4915 __ AddAndCheckForOverflow(r3, r3, scratch1, scratch2, r0);
4916 __ BranchOnNoOverflow(&done);
4917 // Call stub. Undo operation first.
4918 __ sub(r3, r3, scratch1);
4919 __ b(&stub_call);
4920 __ bind(&slow);
4921 }
4922 if (!is_strong(language_mode())) {
4923 ToNumberStub convert_stub(isolate());
4924 __ CallStub(&convert_stub);
4925 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4926 }
4927
4928 // Save result for postfix expressions.
4929 if (expr->is_postfix()) {
4930 if (!context()->IsEffect()) {
4931 // Save the result on the stack. If we have a named or keyed property
4932 // we store the result under the receiver that is currently on top
4933 // of the stack.
4934 switch (assign_type) {
4935 case VARIABLE:
4936 __ push(r3);
4937 break;
4938 case NAMED_PROPERTY:
4939 __ StoreP(r3, MemOperand(sp, kPointerSize));
4940 break;
4941 case NAMED_SUPER_PROPERTY:
4942 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4943 break;
4944 case KEYED_PROPERTY:
4945 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4946 break;
4947 case KEYED_SUPER_PROPERTY:
4948 __ StoreP(r3, MemOperand(sp, 3 * kPointerSize));
4949 break;
4950 }
4951 }
4952 }
4953
4954 __ bind(&stub_call);
4955 __ mr(r4, r3);
4956 __ LoadSmiLiteral(r3, Smi::FromInt(count_value));
4957
4958 SetExpressionPosition(expr);
4959
4960 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4961 strength(language_mode())).code();
4962 CallIC(code, expr->CountBinOpFeedbackId());
4963 patch_site.EmitPatchInfo();
4964 __ bind(&done);
4965
4966 if (is_strong(language_mode())) {
4967 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4968 }
4969 // Store the value returned in r3.
4970 switch (assign_type) {
4971 case VARIABLE:
4972 if (expr->is_postfix()) {
4973 {
4974 EffectContext context(this);
4975 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4976 Token::ASSIGN, expr->CountSlot());
4977 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4978 context.Plug(r3);
4979 }
4980 // For all contexts except EffectConstant We have the result on
4981 // top of the stack.
4982 if (!context()->IsEffect()) {
4983 context()->PlugTOS();
4984 }
4985 } else {
4986 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4987 Token::ASSIGN, expr->CountSlot());
4988 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4989 context()->Plug(r3);
4990 }
4991 break;
4992 case NAMED_PROPERTY: {
4993 __ mov(StoreDescriptor::NameRegister(),
4994 Operand(prop->key()->AsLiteral()->value()));
4995 __ pop(StoreDescriptor::ReceiverRegister());
4996 if (FLAG_vector_stores) {
4997 EmitLoadStoreICSlot(expr->CountSlot());
4998 CallStoreIC();
4999 } else {
5000 CallStoreIC(expr->CountStoreFeedbackId());
5001 }
5002 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5003 if (expr->is_postfix()) {
5004 if (!context()->IsEffect()) {
5005 context()->PlugTOS();
5006 }
5007 } else {
5008 context()->Plug(r3);
5009 }
5010 break;
5011 }
5012 case NAMED_SUPER_PROPERTY: {
5013 EmitNamedSuperPropertyStore(prop);
5014 if (expr->is_postfix()) {
5015 if (!context()->IsEffect()) {
5016 context()->PlugTOS();
5017 }
5018 } else {
5019 context()->Plug(r3);
5020 }
5021 break;
5022 }
5023 case KEYED_SUPER_PROPERTY: {
5024 EmitKeyedSuperPropertyStore(prop);
5025 if (expr->is_postfix()) {
5026 if (!context()->IsEffect()) {
5027 context()->PlugTOS();
5028 }
5029 } else {
5030 context()->Plug(r3);
5031 }
5032 break;
5033 }
5034 case KEYED_PROPERTY: {
5035 __ Pop(StoreDescriptor::ReceiverRegister(),
5036 StoreDescriptor::NameRegister());
5037 Handle<Code> ic =
5038 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5039 if (FLAG_vector_stores) {
5040 EmitLoadStoreICSlot(expr->CountSlot());
5041 CallIC(ic);
5042 } else {
5043 CallIC(ic, expr->CountStoreFeedbackId());
5044 }
5045 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5046 if (expr->is_postfix()) {
5047 if (!context()->IsEffect()) {
5048 context()->PlugTOS();
5049 }
5050 } else {
5051 context()->Plug(r3);
5052 }
5053 break;
5054 }
5055 }
5056 }
5057
5058
5059 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5060 Expression* sub_expr,
5061 Handle<String> check) {
5062 Label materialize_true, materialize_false;
5063 Label* if_true = NULL;
5064 Label* if_false = NULL;
5065 Label* fall_through = NULL;
5066 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
5067 &if_false, &fall_through);
5068
5069 {
5070 AccumulatorValueContext context(this);
5071 VisitForTypeofValue(sub_expr);
5072 }
5073 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5074
5075 Factory* factory = isolate()->factory();
5076 if (String::Equals(check, factory->number_string())) {
5077 __ JumpIfSmi(r3, if_true);
5078 __ LoadP(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
5079 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
5080 __ cmp(r3, ip);
5081 Split(eq, if_true, if_false, fall_through);
5082 } else if (String::Equals(check, factory->string_string())) {
5083 __ JumpIfSmi(r3, if_false);
5084 // Check for undetectable objects => false.
5085 __ CompareObjectType(r3, r3, r4, FIRST_NONSTRING_TYPE);
5086 __ bge(if_false);
5087 __ lbz(r4, FieldMemOperand(r3, Map::kBitFieldOffset));
5088 STATIC_ASSERT((1 << Map::kIsUndetectable) < 0x8000);
5089 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
5090 Split(eq, if_true, if_false, fall_through, cr0);
5091 } else if (String::Equals(check, factory->symbol_string())) {
5092 __ JumpIfSmi(r3, if_false);
5093 __ CompareObjectType(r3, r3, r4, SYMBOL_TYPE);
5094 Split(eq, if_true, if_false, fall_through);
5095 } else if (String::Equals(check, factory->float32x4_string())) {
5096 __ JumpIfSmi(r3, if_false);
5097 __ CompareObjectType(r3, r3, r4, FLOAT32X4_TYPE);
5098 Split(eq, if_true, if_false, fall_through);
5099 } else if (String::Equals(check, factory->boolean_string())) {
5100 __ CompareRoot(r3, Heap::kTrueValueRootIndex);
5101 __ beq(if_true);
5102 __ CompareRoot(r3, Heap::kFalseValueRootIndex);
5103 Split(eq, if_true, if_false, fall_through);
5104 } else if (String::Equals(check, factory->undefined_string())) {
5105 __ CompareRoot(r3, Heap::kUndefinedValueRootIndex);
5106 __ beq(if_true);
5107 __ JumpIfSmi(r3, if_false);
5108 // Check for undetectable objects => true.
5109 __ LoadP(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
5110 __ lbz(r4, FieldMemOperand(r3, Map::kBitFieldOffset));
5111 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
5112 Split(ne, if_true, if_false, fall_through, cr0);
5113
5114 } else if (String::Equals(check, factory->function_string())) {
5115 __ JumpIfSmi(r3, if_false);
5116 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5117 __ CompareObjectType(r3, r3, r4, JS_FUNCTION_TYPE);
5118 __ beq(if_true);
5119 __ cmpi(r4, Operand(JS_FUNCTION_PROXY_TYPE));
5120 Split(eq, if_true, if_false, fall_through);
5121 } else if (String::Equals(check, factory->object_string())) {
5122 __ JumpIfSmi(r3, if_false);
5123 __ CompareRoot(r3, Heap::kNullValueRootIndex);
5124 __ beq(if_true);
5125 // Check for JS objects => true.
5126 __ CompareObjectType(r3, r3, r4, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
5127 __ blt(if_false);
5128 __ CompareInstanceType(r3, r4, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5129 __ bgt(if_false);
5130 // Check for undetectable objects => false.
5131 __ lbz(r4, FieldMemOperand(r3, Map::kBitFieldOffset));
5132 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
5133 Split(eq, if_true, if_false, fall_through, cr0);
5134 } else {
5135 if (if_false != fall_through) __ b(if_false);
5136 }
5137 context()->Plug(if_true, if_false);
5138 }
5139
5140
5141 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5142 Comment cmnt(masm_, "[ CompareOperation");
5143 SetExpressionPosition(expr);
5144
5145 // First we try a fast inlined version of the compare when one of
5146 // the operands is a literal.
5147 if (TryLiteralCompare(expr)) return;
5148
5149 // Always perform the comparison for its control flow. Pack the result
5150 // into the expression's context after the comparison is performed.
5151 Label materialize_true, materialize_false;
5152 Label* if_true = NULL;
5153 Label* if_false = NULL;
5154 Label* fall_through = NULL;
5155 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
5156 &if_false, &fall_through);
5157
5158 Token::Value op = expr->op();
5159 VisitForStackValue(expr->left());
5160 switch (op) {
5161 case Token::IN:
5162 VisitForStackValue(expr->right());
5163 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5164 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5165 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
5166 __ cmp(r3, ip);
5167 Split(eq, if_true, if_false, fall_through);
5168 break;
5169
5170 case Token::INSTANCEOF: {
5171 VisitForStackValue(expr->right());
5172 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5173 __ CallStub(&stub);
5174 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5175 // The stub returns 0 for true.
5176 __ cmpi(r3, Operand::Zero());
5177 Split(eq, if_true, if_false, fall_through);
5178 break;
5179 }
5180
5181 default: {
5182 VisitForAccumulatorValue(expr->right());
5183 Condition cond = CompareIC::ComputeCondition(op);
5184 __ pop(r4);
5185
5186 bool inline_smi_code = ShouldInlineSmiCase(op);
5187 JumpPatchSite patch_site(masm_);
5188 if (inline_smi_code) {
5189 Label slow_case;
5190 __ orx(r5, r3, r4);
5191 patch_site.EmitJumpIfNotSmi(r5, &slow_case);
5192 __ cmp(r4, r3);
5193 Split(cond, if_true, if_false, NULL);
5194 __ bind(&slow_case);
5195 }
5196
5197 Handle<Code> ic = CodeFactory::CompareIC(
5198 isolate(), op, strength(language_mode())).code();
5199 CallIC(ic, expr->CompareOperationFeedbackId());
5200 patch_site.EmitPatchInfo();
5201 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5202 __ cmpi(r3, Operand::Zero());
5203 Split(cond, if_true, if_false, fall_through);
5204 }
5205 }
5206
5207 // Convert the result of the comparison into one expected for this
5208 // expression's context.
5209 context()->Plug(if_true, if_false);
5210 }
5211
5212
5213 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5214 Expression* sub_expr,
5215 NilValue nil) {
5216 Label materialize_true, materialize_false;
5217 Label* if_true = NULL;
5218 Label* if_false = NULL;
5219 Label* fall_through = NULL;
5220 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
5221 &if_false, &fall_through);
5222
5223 VisitForAccumulatorValue(sub_expr);
5224 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5225 if (expr->op() == Token::EQ_STRICT) {
5226 Heap::RootListIndex nil_value = nil == kNullValue
5227 ? Heap::kNullValueRootIndex
5228 : Heap::kUndefinedValueRootIndex;
5229 __ LoadRoot(r4, nil_value);
5230 __ cmp(r3, r4);
5231 Split(eq, if_true, if_false, fall_through);
5232 } else {
5233 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5234 CallIC(ic, expr->CompareOperationFeedbackId());
5235 __ cmpi(r3, Operand::Zero());
5236 Split(ne, if_true, if_false, fall_through);
5237 }
5238 context()->Plug(if_true, if_false);
5239 }
5240
5241
5242 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5243 __ LoadP(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5244 context()->Plug(r3);
5245 }
5246
5247
5248 Register FullCodeGenerator::result_register() { return r3; }
5249
5250
5251 Register FullCodeGenerator::context_register() { return cp; }
5252
5253
5254 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5255 DCHECK_EQ(static_cast<int>(POINTER_SIZE_ALIGN(frame_offset)), frame_offset);
5256 __ StoreP(value, MemOperand(fp, frame_offset), r0);
5257 }
5258
5259
5260 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5261 __ LoadP(dst, ContextOperand(cp, context_index), r0);
5262 }
5263
5264
5265 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5266 Scope* declaration_scope = scope()->DeclarationScope();
5267 if (declaration_scope->is_script_scope() ||
5268 declaration_scope->is_module_scope()) {
5269 // Contexts nested in the native context have a canonical empty function
5270 // as their closure, not the anonymous closure containing the global
5271 // code. Pass a smi sentinel and let the runtime look up the empty
5272 // function.
5273 __ LoadSmiLiteral(ip, Smi::FromInt(0));
5274 } else if (declaration_scope->is_eval_scope()) {
5275 // Contexts created by a call to eval have the same closure as the
5276 // context calling eval, not the anonymous closure containing the eval
5277 // code. Fetch it from the context.
5278 __ LoadP(ip, ContextOperand(cp, Context::CLOSURE_INDEX));
5279 } else {
5280 DCHECK(declaration_scope->is_function_scope());
5281 __ LoadP(ip, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5282 }
5283 __ push(ip);
5284 }
5285
5286
5287 // ----------------------------------------------------------------------------
5288 // Non-local control flow support.
5289
5290 void FullCodeGenerator::EnterFinallyBlock() {
5291 DCHECK(!result_register().is(r4));
5292 // Store result register while executing finally block.
5293 __ push(result_register());
5294 // Cook return address in link register to stack (smi encoded Code* delta)
5295 __ mflr(r4);
5296 __ mov(ip, Operand(masm_->CodeObject()));
5297 __ sub(r4, r4, ip);
5298 __ SmiTag(r4);
5299
5300 // Store result register while executing finally block.
5301 __ push(r4);
5302
5303 // Store pending message while executing finally block.
5304 ExternalReference pending_message_obj =
5305 ExternalReference::address_of_pending_message_obj(isolate());
5306 __ mov(ip, Operand(pending_message_obj));
5307 __ LoadP(r4, MemOperand(ip));
5308 __ push(r4);
5309
5310 ClearPendingMessage();
5311 }
5312
5313
5314 void FullCodeGenerator::ExitFinallyBlock() {
5315 DCHECK(!result_register().is(r4));
5316 // Restore pending message from stack.
5317 __ pop(r4);
5318 ExternalReference pending_message_obj =
5319 ExternalReference::address_of_pending_message_obj(isolate());
5320 __ mov(ip, Operand(pending_message_obj));
5321 __ StoreP(r4, MemOperand(ip));
5322
5323 // Restore result register from stack.
5324 __ pop(r4);
5325
5326 // Uncook return address and return.
5327 __ pop(result_register());
5328 __ SmiUntag(r4);
5329 __ mov(ip, Operand(masm_->CodeObject()));
5330 __ add(ip, ip, r4);
5331 __ mtctr(ip);
5332 __ bctr();
5333 }
5334
5335
5336 void FullCodeGenerator::ClearPendingMessage() {
5337 DCHECK(!result_register().is(r4));
5338 ExternalReference pending_message_obj =
5339 ExternalReference::address_of_pending_message_obj(isolate());
5340 __ LoadRoot(r4, Heap::kTheHoleValueRootIndex);
5341 __ mov(ip, Operand(pending_message_obj));
5342 __ StoreP(r4, MemOperand(ip));
5343 }
5344
5345
5346 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5347 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5348 __ mov(VectorStoreICTrampolineDescriptor::SlotRegister(),
5349 Operand(SmiFromSlot(slot)));
5350 }
5351
5352
5353 #undef __
5354
5355
5356 void BackEdgeTable::PatchAt(Code* unoptimized_code, Address pc,
5357 BackEdgeState target_state,
5358 Code* replacement_code) {
5359 Address mov_address = Assembler::target_address_from_return_address(pc);
5360 Address cmp_address = mov_address - 2 * Assembler::kInstrSize;
5361 CodePatcher patcher(cmp_address, 1);
5362
5363 switch (target_state) {
5364 case INTERRUPT: {
5365 // <decrement profiling counter>
5366 // cmpi r6, 0
5367 // bge <ok> ;; not changed
5368 // mov r12, <interrupt stub address>
5369 // mtlr r12
5370 // blrl
5371 // <reset profiling counter>
5372 // ok-label
5373 patcher.masm()->cmpi(r6, Operand::Zero());
5374 break;
5375 }
5376 case ON_STACK_REPLACEMENT:
5377 case OSR_AFTER_STACK_CHECK:
5378 // <decrement profiling counter>
5379 // crset
5380 // bge <ok> ;; not changed
5381 // mov r12, <on-stack replacement address>
5382 // mtlr r12
5383 // blrl
5384 // <reset profiling counter>
5385 // ok-label ----- pc_after points here
5386
5387 // Set the LT bit such that bge is a NOP
5388 patcher.masm()->crset(Assembler::encode_crbit(cr7, CR_LT));
5389 break;
5390 }
5391
5392 // Replace the stack check address in the mov sequence with the
5393 // entry address of the replacement code.
5394 Assembler::set_target_address_at(mov_address, unoptimized_code,
5395 replacement_code->entry());
5396
5397 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5398 unoptimized_code, mov_address, replacement_code);
5399 }
5400
5401
5402 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5403 Isolate* isolate, Code* unoptimized_code, Address pc) {
5404 Address mov_address = Assembler::target_address_from_return_address(pc);
5405 Address cmp_address = mov_address - 2 * Assembler::kInstrSize;
5406 Address interrupt_address =
5407 Assembler::target_address_at(mov_address, unoptimized_code);
5408
5409 if (Assembler::IsCmpImmediate(Assembler::instr_at(cmp_address))) {
5410 DCHECK(interrupt_address == isolate->builtins()->InterruptCheck()->entry());
5411 return INTERRUPT;
5412 }
5413
5414 DCHECK(Assembler::IsCrSet(Assembler::instr_at(cmp_address)));
5415
5416 if (interrupt_address == isolate->builtins()->OnStackReplacement()->entry()) {
5417 return ON_STACK_REPLACEMENT;
5418 }
5419
5420 DCHECK(interrupt_address ==
5421 isolate->builtins()->OsrAfterStackCheck()->entry());
5422 return OSR_AFTER_STACK_CHECK;
5423 }
5424 } // namespace internal
5425 } // namespace v8
5426 #endif // V8_TARGET_ARCH_PPC
OLDNEW
« no previous file with comments | « src/ppc/deoptimizer-ppc.cc ('k') | src/runtime-profiler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698