blob: 4ec8654eb20f9563df6e52a11ce8d241305180dd [file] [log] [blame]
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "code-stubs.h"
31#include "hydrogen.h"
32#include "lithium.h"
33
34namespace v8 {
35namespace internal {
36
37
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +000038static LChunk* OptimizeGraph(HGraph* graph) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +000039 DisallowHeapAllocation no_allocation;
40 DisallowHandleAllocation no_handles;
41 DisallowHandleDereference no_deref;
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +000042
43 ASSERT(graph != NULL);
44 SmartArrayPointer<char> bailout_reason;
45 if (!graph->Optimize(&bailout_reason)) {
46 FATAL(bailout_reason.is_empty() ? "unknown" : *bailout_reason);
47 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000048 LChunk* chunk = LChunk::NewChunk(graph);
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +000049 if (chunk == NULL) {
50 FATAL(graph->info()->bailout_reason());
51 }
52 return chunk;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000053}
54
55
56class CodeStubGraphBuilderBase : public HGraphBuilder {
57 public:
58 CodeStubGraphBuilderBase(Isolate* isolate, HydrogenCodeStub* stub)
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000059 : HGraphBuilder(&info_),
60 arguments_length_(NULL),
61 info_(stub, isolate),
62 context_(NULL) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +000063 descriptor_ = stub->GetInterfaceDescriptor(isolate);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000064 parameters_.Reset(new HParameter*[descriptor_->register_param_count_]);
65 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000066 virtual bool BuildGraph();
67
68 protected:
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000069 virtual HValue* BuildCodeStub() = 0;
70 HParameter* GetParameter(int parameter) {
71 ASSERT(parameter < descriptor_->register_param_count_);
72 return parameters_[parameter];
73 }
74 HValue* GetArgumentsLength() {
75 // This is initialized in BuildGraph()
76 ASSERT(arguments_length_ != NULL);
77 return arguments_length_;
78 }
danno@chromium.org94b0d6f2013-02-04 13:33:20 +000079 CompilationInfo* info() { return &info_; }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000080 HydrogenCodeStub* stub() { return info_.code_stub(); }
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +000081 HContext* context() { return context_; }
danno@chromium.org94b0d6f2013-02-04 13:33:20 +000082 Isolate* isolate() { return info_.isolate(); }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000083
ulan@chromium.org57ff8812013-05-10 08:16:55 +000084 class ArrayContextChecker {
85 public:
86 ArrayContextChecker(HGraphBuilder* builder, HValue* constructor,
87 HValue* array_function)
88 : checker_(builder) {
89 checker_.If<HCompareObjectEqAndBranch, HValue*>(constructor,
90 array_function);
91 checker_.Then();
92 }
93
94 ~ArrayContextChecker() {
95 checker_.ElseDeopt();
96 checker_.End();
97 }
98 private:
99 IfBuilder checker_;
100 };
101
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000102 private:
103 SmartArrayPointer<HParameter*> parameters_;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000104 HValue* arguments_length_;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000105 CompilationInfoWithZone info_;
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000106 CodeStubInterfaceDescriptor* descriptor_;
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +0000107 HContext* context_;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000108};
109
110
111bool CodeStubGraphBuilderBase::BuildGraph() {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000112 // Update the static counter each time a new code stub is generated.
113 isolate()->counters()->code_stubs()->Increment();
114
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000115 if (FLAG_trace_hydrogen) {
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000116 const char* name = CodeStub::MajorName(stub()->MajorKey(), false);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000117 PrintF("-----------------------------------------------------------\n");
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000118 PrintF("Compiling stub %s using hydrogen\n", name);
ulan@chromium.org750145a2013-03-07 15:14:13 +0000119 isolate()->GetHTracer()->TraceCompilation(&info_);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000120 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000121
122 Zone* zone = this->zone();
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000123 int param_count = descriptor_->register_param_count_;
124 HEnvironment* start_environment = graph()->start_environment();
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000125 HBasicBlock* next_block = CreateBasicBlock(start_environment);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000126 current_block()->Goto(next_block);
127 next_block->SetJoinId(BailoutId::StubEntry());
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000128 set_current_block(next_block);
129
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000130 HConstant* undefined_constant = new(zone) HConstant(
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000131 isolate()->factory()->undefined_value(), Representation::Tagged());
132 AddInstruction(undefined_constant);
133 graph()->set_undefined_constant(undefined_constant);
134
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000135 for (int i = 0; i < param_count; ++i) {
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000136 HParameter* param =
137 new(zone) HParameter(i, HParameter::REGISTER_PARAMETER);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000138 AddInstruction(param);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000139 start_environment->Bind(i, param);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000140 parameters_[i] = param;
141 }
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000142
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000143 HInstruction* stack_parameter_count;
144 if (descriptor_->stack_parameter_count_ != NULL) {
145 ASSERT(descriptor_->environment_length() == (param_count + 1));
146 stack_parameter_count = new(zone) HParameter(param_count,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000147 HParameter::REGISTER_PARAMETER,
148 Representation::Integer32());
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000149 stack_parameter_count->set_type(HType::Smi());
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000150 // It's essential to bind this value to the environment in case of deopt.
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000151 AddInstruction(stack_parameter_count);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000152 start_environment->Bind(param_count, stack_parameter_count);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000153 arguments_length_ = stack_parameter_count;
154 } else {
155 ASSERT(descriptor_->environment_length() == param_count);
156 stack_parameter_count = graph()->GetConstantMinus1();
157 arguments_length_ = graph()->GetConstant0();
158 }
159
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000160 context_ = new(zone) HContext();
161 AddInstruction(context_);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000162 start_environment->BindContext(context_);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000163
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000164 AddSimulate(BailoutId::StubEntry());
165
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000166 NoObservableSideEffectsScope no_effects(this);
167
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000168 HValue* return_value = BuildCodeStub();
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000169
170 // We might have extra expressions to pop from the stack in addition to the
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000171 // arguments above.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000172 HInstruction* stack_pop_count = stack_parameter_count;
173 if (descriptor_->function_mode_ == JS_FUNCTION_STUB_MODE) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000174 if (!stack_parameter_count->IsConstant() &&
175 descriptor_->hint_stack_parameter_count_ < 0) {
176 HInstruction* amount = graph()->GetConstant1();
177 stack_pop_count = AddInstruction(
178 HAdd::New(zone, context_, stack_parameter_count, amount));
179 stack_pop_count->ChangeRepresentation(Representation::Integer32());
180 stack_pop_count->ClearFlag(HValue::kCanOverflow);
181 } else {
182 int count = descriptor_->hint_stack_parameter_count_;
183 stack_pop_count = AddInstruction(new(zone)
184 HConstant(count, Representation::Integer32()));
185 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000186 }
187
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000188 if (current_block() != NULL) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000189 HReturn* hreturn_instruction = new(zone) HReturn(return_value,
190 context_,
191 stack_pop_count);
192 current_block()->Finish(hreturn_instruction);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000193 set_current_block(NULL);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000194 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000195 return true;
196}
197
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000198
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000199template <class Stub>
200class CodeStubGraphBuilder: public CodeStubGraphBuilderBase {
201 public:
202 explicit CodeStubGraphBuilder(Stub* stub)
203 : CodeStubGraphBuilderBase(Isolate::Current(), stub) {}
204
205 protected:
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000206 virtual HValue* BuildCodeStub() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000207 if (casted_stub()->IsUninitialized()) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000208 return BuildCodeUninitializedStub();
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000209 } else {
210 return BuildCodeInitializedStub();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000211 }
212 }
213
214 virtual HValue* BuildCodeInitializedStub() {
215 UNIMPLEMENTED();
216 return NULL;
217 }
218
219 virtual HValue* BuildCodeUninitializedStub() {
220 // Force a deopt that falls back to the runtime.
221 HValue* undefined = graph()->GetConstantUndefined();
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000222 IfBuilder builder(this);
223 builder.IfNot<HCompareObjectEqAndBranch, HValue*>(undefined, undefined);
224 builder.Then();
225 builder.ElseDeopt();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000226 return undefined;
227 }
228
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000229 Stub* casted_stub() { return static_cast<Stub*>(stub()); }
230};
231
232
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000233Handle<Code> HydrogenCodeStub::GenerateLightweightMissCode(Isolate* isolate) {
234 Factory* factory = isolate->factory();
235
236 // Generate the new code.
237 MacroAssembler masm(isolate, NULL, 256);
238
239 {
240 // Update the static counter each time a new code stub is generated.
241 isolate->counters()->code_stubs()->Increment();
242
243 // Nested stubs are not allowed for leaves.
244 AllowStubCallsScope allow_scope(&masm, false);
245
246 // Generate the code for the stub.
247 masm.set_generating_stub(true);
248 NoCurrentFrameScope scope(&masm);
249 GenerateLightweightMiss(&masm);
250 }
251
252 // Create the code object.
253 CodeDesc desc;
254 masm.GetCode(&desc);
255
256 // Copy the generated code into a heap object.
257 Code::Flags flags = Code::ComputeFlags(
258 GetCodeKind(),
259 GetICState(),
260 GetExtraICState(),
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000261 GetStubType(),
262 GetStubFlags());
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000263 Handle<Code> new_object = factory->NewCode(
264 desc, flags, masm.CodeObject(), NeedsImmovableCode());
265 return new_object;
266}
267
268
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000269template <class Stub>
270static Handle<Code> DoGenerateCode(Stub* stub) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000271 Isolate* isolate = Isolate::Current();
272 CodeStub::Major major_key =
273 static_cast<HydrogenCodeStub*>(stub)->MajorKey();
274 CodeStubInterfaceDescriptor* descriptor =
275 isolate->code_stub_interface_descriptor(major_key);
276 if (descriptor->register_param_count_ < 0) {
277 stub->InitializeInterfaceDescriptor(isolate, descriptor);
278 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000279
280 // If we are uninitialized we can use a light-weight stub to enter
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000281 // the runtime that is significantly faster than using the standard
282 // stub-failure deopt mechanism.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000283 if (stub->IsUninitialized() && descriptor->has_miss_handler()) {
284 ASSERT(descriptor->stack_parameter_count_ == NULL);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000285 return stub->GenerateLightweightMissCode(isolate);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000286 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000287 CodeStubGraphBuilder<Stub> builder(stub);
288 LChunk* chunk = OptimizeGraph(builder.CreateGraph());
289 return chunk->Codegen();
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000290}
291
292
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000293template <>
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000294HValue* CodeStubGraphBuilder<FastCloneShallowArrayStub>::BuildCodeStub() {
295 Zone* zone = this->zone();
296 Factory* factory = isolate()->factory();
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000297 HValue* undefined = graph()->GetConstantUndefined();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000298 AllocationSiteMode alloc_site_mode = casted_stub()->allocation_site_mode();
299 FastCloneShallowArrayStub::Mode mode = casted_stub()->mode();
300 int length = casted_stub()->length();
301
302 HInstruction* boilerplate =
303 AddInstruction(new(zone) HLoadKeyed(GetParameter(0),
304 GetParameter(1),
305 NULL,
306 FAST_ELEMENTS));
307
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000308 IfBuilder checker(this);
309 checker.IfNot<HCompareObjectEqAndBranch, HValue*>(boilerplate, undefined);
310 checker.Then();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000311
312 if (mode == FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000313 HValue* elements = AddLoadElements(boilerplate);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000314
315 IfBuilder if_fixed_cow(this);
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000316 if_fixed_cow.IfCompareMap(elements, factory->fixed_cow_array_map());
317 if_fixed_cow.Then();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000318 environment()->Push(BuildCloneShallowArray(context(),
319 boilerplate,
320 alloc_site_mode,
321 FAST_ELEMENTS,
322 0/*copy-on-write*/));
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000323 if_fixed_cow.Else();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000324
325 IfBuilder if_fixed(this);
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000326 if_fixed.IfCompareMap(elements, factory->fixed_array_map());
327 if_fixed.Then();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000328 environment()->Push(BuildCloneShallowArray(context(),
329 boilerplate,
330 alloc_site_mode,
331 FAST_ELEMENTS,
332 length));
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000333 if_fixed.Else();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000334 environment()->Push(BuildCloneShallowArray(context(),
335 boilerplate,
336 alloc_site_mode,
337 FAST_DOUBLE_ELEMENTS,
338 length));
339 } else {
340 ElementsKind elements_kind = casted_stub()->ComputeElementsKind();
341 environment()->Push(BuildCloneShallowArray(context(),
342 boilerplate,
343 alloc_site_mode,
344 elements_kind,
345 length));
346 }
347
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000348 HValue* result = environment()->Pop();
349 checker.ElseDeopt();
350 return result;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000351}
352
353
354Handle<Code> FastCloneShallowArrayStub::GenerateCode() {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000355 return DoGenerateCode(this);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000356}
357
358
359template <>
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000360HValue* CodeStubGraphBuilder<FastCloneShallowObjectStub>::BuildCodeStub() {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000361 Zone* zone = this->zone();
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000362 HValue* undefined = graph()->GetConstantUndefined();
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000363
364 HInstruction* boilerplate =
365 AddInstruction(new(zone) HLoadKeyed(GetParameter(0),
366 GetParameter(1),
367 NULL,
368 FAST_ELEMENTS));
369
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000370 IfBuilder checker(this);
371 checker.IfNot<HCompareObjectEqAndBranch, HValue*>(boilerplate, undefined);
372 checker.And();
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000373
374 int size = JSObject::kHeaderSize + casted_stub()->length() * kPointerSize;
375 HValue* boilerplate_size =
376 AddInstruction(new(zone) HInstanceSize(boilerplate));
377 HValue* size_in_words =
378 AddInstruction(new(zone) HConstant(size >> kPointerSizeLog2,
379 Representation::Integer32()));
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000380 checker.IfCompare(boilerplate_size, size_in_words, Token::EQ);
381 checker.Then();
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000382
383 HValue* size_in_bytes =
384 AddInstruction(new(zone) HConstant(size, Representation::Integer32()));
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000385 HAllocate::Flags flags = HAllocate::CAN_ALLOCATE_IN_NEW_SPACE;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000386 if (isolate()->heap()->ShouldGloballyPretenure()) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000387 flags = static_cast<HAllocate::Flags>(
388 flags | HAllocate::CAN_ALLOCATE_IN_OLD_POINTER_SPACE);
389 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000390
391 HInstruction* object = AddInstruction(new(zone)
392 HAllocate(context(), size_in_bytes, HType::JSObject(), flags));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000393
394 for (int i = 0; i < size; i += kPointerSize) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000395 HObjectAccess access = HObjectAccess::ForJSObjectOffset(i);
396 AddStore(object, access, AddLoad(boilerplate, access));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000397 }
398
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000399 checker.ElseDeopt();
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000400 return object;
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000401}
402
403
404Handle<Code> FastCloneShallowObjectStub::GenerateCode() {
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000405 return DoGenerateCode(this);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000406}
407
408
409template <>
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000410HValue* CodeStubGraphBuilder<KeyedLoadFastElementStub>::BuildCodeStub() {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000411 HInstruction* load = BuildUncheckedMonomorphicElementAccess(
412 GetParameter(0), GetParameter(1), NULL, NULL,
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000413 casted_stub()->is_js_array(), casted_stub()->elements_kind(),
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +0000414 false, NEVER_RETURN_HOLE, STANDARD_STORE);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000415 return load;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000416}
417
418
419Handle<Code> KeyedLoadFastElementStub::GenerateCode() {
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000420 return DoGenerateCode(this);
421}
422
423
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000424template<>
425HValue* CodeStubGraphBuilder<LoadFieldStub>::BuildCodeStub() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000426 HObjectAccess access = casted_stub()->is_inobject() ?
427 HObjectAccess::ForJSObjectOffset(casted_stub()->offset()) :
428 HObjectAccess::ForBackingStoreOffset(casted_stub()->offset());
429 return AddInstruction(BuildLoadNamedField(GetParameter(0), access,
430 casted_stub()->representation()));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000431}
432
433
434Handle<Code> LoadFieldStub::GenerateCode() {
435 return DoGenerateCode(this);
436}
437
438
439template<>
440HValue* CodeStubGraphBuilder<KeyedLoadFieldStub>::BuildCodeStub() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000441 HObjectAccess access = casted_stub()->is_inobject() ?
442 HObjectAccess::ForJSObjectOffset(casted_stub()->offset()) :
443 HObjectAccess::ForBackingStoreOffset(casted_stub()->offset());
444 return AddInstruction(BuildLoadNamedField(GetParameter(0), access,
445 casted_stub()->representation()));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000446}
447
448
449Handle<Code> KeyedLoadFieldStub::GenerateCode() {
450 return DoGenerateCode(this);
451}
452
453
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000454template <>
455HValue* CodeStubGraphBuilder<KeyedStoreFastElementStub>::BuildCodeStub() {
456 BuildUncheckedMonomorphicElementAccess(
457 GetParameter(0), GetParameter(1), GetParameter(2), NULL,
458 casted_stub()->is_js_array(), casted_stub()->elements_kind(),
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +0000459 true, NEVER_RETURN_HOLE, casted_stub()->store_mode());
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000460
461 return GetParameter(2);
462}
463
464
465Handle<Code> KeyedStoreFastElementStub::GenerateCode() {
466 return DoGenerateCode(this);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000467}
468
469
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000470template <>
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000471HValue* CodeStubGraphBuilder<TransitionElementsKindStub>::BuildCodeStub() {
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000472 Zone* zone = this->zone();
473
474 HValue* js_array = GetParameter(0);
475 HValue* map = GetParameter(1);
476
477 info()->MarkAsSavesCallerDoubles();
478
479 AddInstruction(new(zone) HTrapAllocationMemento(js_array));
480
481 HInstruction* array_length =
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000482 AddLoad(js_array, HObjectAccess::ForArrayLength());
483 array_length->set_type(HType::Smi());
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000484
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000485 ElementsKind to_kind = casted_stub()->to_kind();
486 BuildNewSpaceArrayCheck(array_length, to_kind);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000487
danno@chromium.org2d18b362013-03-22 17:19:26 +0000488 IfBuilder if_builder(this);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000489
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000490 if_builder.IfCompare(array_length, graph()->GetConstant0(), Token::EQ);
491 if_builder.Then();
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000492
493 // Nothing to do, just change the map.
494
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000495 if_builder.Else();
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000496
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000497 HInstruction* elements = AddLoadElements(js_array);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000498
499 HInstruction* elements_length =
500 AddInstruction(new(zone) HFixedArrayBaseLength(elements));
501
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000502 HValue* new_elements = BuildAllocateElementsAndInitializeElementsHeader(
503 context(), to_kind, elements_length);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000504
505 BuildCopyElements(context(), elements,
506 casted_stub()->from_kind(), new_elements,
danno@chromium.org2d18b362013-03-22 17:19:26 +0000507 to_kind, array_length, elements_length);
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000508
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000509 AddStore(js_array, HObjectAccess::ForElementsPointer(), new_elements);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000510
511 if_builder.End();
512
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000513 AddStore(js_array, HObjectAccess::ForMap(), map);
514
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000515 return js_array;
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000516}
517
518
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000519Handle<Code> TransitionElementsKindStub::GenerateCode() {
520 return DoGenerateCode(this);
521}
522
523
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000524template <>
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000525HValue* CodeStubGraphBuilder<ArrayNoArgumentConstructorStub>::BuildCodeStub() {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000526 // ----------- S t a t e -------------
527 // -- Parameter 1 : type info cell
528 // -- Parameter 0 : constructor
529 // -----------------------------------
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000530 HInstruction* array_function = BuildGetArrayFunction(context());
531 ArrayContextChecker(this,
532 GetParameter(ArrayConstructorStubBase::kConstructor),
533 array_function);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000534 // Get the right map
535 // Should be a constant
536 JSArrayBuilder array_builder(
537 this,
538 casted_stub()->elements_kind(),
539 GetParameter(ArrayConstructorStubBase::kPropertyCell),
540 casted_stub()->mode());
541 return array_builder.AllocateEmptyArray();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000542}
543
544
545Handle<Code> ArrayNoArgumentConstructorStub::GenerateCode() {
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000546 return DoGenerateCode(this);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000547}
548
549
550template <>
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000551HValue* CodeStubGraphBuilder<ArraySingleArgumentConstructorStub>::
552 BuildCodeStub() {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000553 HInstruction* array_function = BuildGetArrayFunction(context());
554 ArrayContextChecker(this,
555 GetParameter(ArrayConstructorStubBase::kConstructor),
556 array_function);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000557 // Smi check and range check on the input arg.
558 HValue* constant_one = graph()->GetConstant1();
559 HValue* constant_zero = graph()->GetConstant0();
560
561 HInstruction* elements = AddInstruction(
562 new(zone()) HArgumentsElements(false));
563 HInstruction* argument = AddInstruction(
564 new(zone()) HAccessArgumentsAt(elements, constant_one, constant_zero));
565
566 HConstant* max_alloc_length =
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000567 new(zone()) HConstant(JSObject::kInitialMaxFastElementArray);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000568 AddInstruction(max_alloc_length);
569 const int initial_capacity = JSArray::kPreallocatedArrayElements;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000570 HConstant* initial_capacity_node = new(zone()) HConstant(initial_capacity);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000571 AddInstruction(initial_capacity_node);
572
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000573 HBoundsCheck* checked_arg = AddBoundsCheck(argument, max_alloc_length);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000574 IfBuilder if_builder(this);
575 if_builder.IfCompare(checked_arg, constant_zero, Token::EQ);
576 if_builder.Then();
577 Push(initial_capacity_node); // capacity
578 Push(constant_zero); // length
579 if_builder.Else();
580 Push(checked_arg); // capacity
581 Push(checked_arg); // length
582 if_builder.End();
583
584 // Figure out total size
585 HValue* length = Pop();
586 HValue* capacity = Pop();
587
588 JSArrayBuilder array_builder(
589 this,
590 casted_stub()->elements_kind(),
591 GetParameter(ArrayConstructorStubBase::kPropertyCell),
592 casted_stub()->mode());
593 return array_builder.AllocateArray(capacity, length, true);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000594}
595
596
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000597Handle<Code> ArraySingleArgumentConstructorStub::GenerateCode() {
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000598 return DoGenerateCode(this);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000599}
600
601
602template <>
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000603HValue* CodeStubGraphBuilder<ArrayNArgumentsConstructorStub>::BuildCodeStub() {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000604 HInstruction* array_function = BuildGetArrayFunction(context());
605 ArrayContextChecker(this,
606 GetParameter(ArrayConstructorStubBase::kConstructor),
607 array_function);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000608 ElementsKind kind = casted_stub()->elements_kind();
609 HValue* length = GetArgumentsLength();
610
611 JSArrayBuilder array_builder(
612 this,
613 kind,
614 GetParameter(ArrayConstructorStubBase::kPropertyCell),
615 casted_stub()->mode());
616
617 // We need to fill with the hole if it's a smi array in the multi-argument
618 // case because we might have to bail out while copying arguments into
619 // the array because they aren't compatible with a smi array.
620 // If it's a double array, no problem, and if it's fast then no
621 // problem either because doubles are boxed.
622 bool fill_with_hole = IsFastSmiElementsKind(kind);
623 HValue* new_object = array_builder.AllocateArray(length,
624 length,
625 fill_with_hole);
626 HValue* elements = array_builder.GetElementsLocation();
627 ASSERT(elements != NULL);
628
629 // Now populate the elements correctly.
630 LoopBuilder builder(this,
631 context(),
632 LoopBuilder::kPostIncrement);
633 HValue* start = graph()->GetConstant0();
634 HValue* key = builder.BeginBody(start, length, Token::LT);
635 HInstruction* argument_elements = AddInstruction(
636 new(zone()) HArgumentsElements(false));
637 HInstruction* argument = AddInstruction(new(zone()) HAccessArgumentsAt(
638 argument_elements, length, key));
639
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000640 AddInstruction(new(zone()) HStoreKeyed(elements, key, argument, kind));
641 builder.EndBody();
642 return new_object;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000643}
644
645
646Handle<Code> ArrayNArgumentsConstructorStub::GenerateCode() {
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000647 return DoGenerateCode(this);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000648}
649
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000650
651template <>
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000652HValue* CodeStubGraphBuilder<CompareNilICStub>::BuildCodeInitializedStub() {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000653 CompareNilICStub* stub = casted_stub();
654 HIfContinuation continuation;
655 Handle<Map> sentinel_map(graph()->isolate()->heap()->meta_map());
656 BuildCompareNil(GetParameter(0), stub->GetKind(),
657 stub->GetTypes(), sentinel_map,
658 RelocInfo::kNoPosition, &continuation);
659 IfBuilder if_nil(this, &continuation);
660 if_nil.Then();
661 if (continuation.IsFalseReachable()) {
662 if_nil.Else();
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +0000663 if_nil.Return(graph()->GetConstant0());
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000664 }
665 if_nil.End();
666 return continuation.IsTrueReachable()
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +0000667 ? graph()->GetConstant1()
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000668 : graph()->GetConstantUndefined();
669}
670
671
672Handle<Code> CompareNilICStub::GenerateCode() {
673 return DoGenerateCode(this);
674}
675
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000676
677template <>
678HValue* CodeStubGraphBuilder<ToBooleanStub>::BuildCodeInitializedStub() {
679 ToBooleanStub* stub = casted_stub();
680
681 IfBuilder if_true(this);
682 if_true.If<HBranch>(GetParameter(0), stub->GetTypes());
683 if_true.Then();
684 if_true.Return(graph()->GetConstant1());
685 if_true.Else();
686 if_true.End();
687 return graph()->GetConstant0();
688}
689
690
691Handle<Code> ToBooleanStub::GenerateCode() {
692 return DoGenerateCode(this);
693}
694
695
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000696} } // namespace v8::internal