blob: bbb4d6353bf701844784e76f4c2eaf95bcf34141 [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// Copyright 2015 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/compiler/code-stub-assembler.h"
6
7#include <ostream>
8
9#include "src/code-factory.h"
10#include "src/compiler/graph.h"
11#include "src/compiler/instruction-selector.h"
12#include "src/compiler/linkage.h"
13#include "src/compiler/pipeline.h"
14#include "src/compiler/raw-machine-assembler.h"
15#include "src/compiler/schedule.h"
16#include "src/frames.h"
17#include "src/interface-descriptors.h"
18#include "src/interpreter/bytecodes.h"
19#include "src/machine-type.h"
20#include "src/macro-assembler.h"
21#include "src/zone.h"
22
23namespace v8 {
24namespace internal {
25namespace compiler {
26
Ben Murdoch014dc512016-03-22 12:00:34 +000027CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone,
28 const CallInterfaceDescriptor& descriptor,
Ben Murdoch109988c2016-05-18 11:27:45 +010029 Code::Flags flags, const char* name,
30 size_t result_size)
Ben Murdoch3b9bc312016-06-02 14:46:10 +010031 : CodeStubAssembler(
32 isolate, zone,
Ben Murdoch109988c2016-05-18 11:27:45 +010033 Linkage::GetStubCallDescriptor(
34 isolate, zone, descriptor, descriptor.GetStackParameterCount(),
35 CallDescriptor::kNoFlags, Operator::kNoProperties,
Ben Murdoch3b9bc312016-06-02 14:46:10 +010036 MachineType::AnyTagged(), result_size),
37 flags, name) {}
38
39CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone,
40 int parameter_count, Code::Flags flags,
41 const char* name)
42 : CodeStubAssembler(isolate, zone, Linkage::GetJSCallDescriptor(
43 zone, false, parameter_count,
44 CallDescriptor::kNoFlags),
45 flags, name) {}
46
47CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone,
48 CallDescriptor* call_descriptor,
49 Code::Flags flags, const char* name)
50 : raw_assembler_(new RawMachineAssembler(
51 isolate, new (zone) Graph(zone), call_descriptor,
52 MachineType::PointerRepresentation(),
53 InstructionSelector::SupportedMachineOperatorFlags())),
Ben Murdoch109988c2016-05-18 11:27:45 +010054 flags_(flags),
Ben Murdoch014dc512016-03-22 12:00:34 +000055 name_(name),
Ben Murdoch109988c2016-05-18 11:27:45 +010056 code_generated_(false),
57 variables_(zone) {}
Ben Murdoch014dc512016-03-22 12:00:34 +000058
59CodeStubAssembler::~CodeStubAssembler() {}
60
Ben Murdoch109988c2016-05-18 11:27:45 +010061void CodeStubAssembler::CallPrologue() {}
62
63void CodeStubAssembler::CallEpilogue() {}
Ben Murdoch014dc512016-03-22 12:00:34 +000064
65Handle<Code> CodeStubAssembler::GenerateCode() {
66 DCHECK(!code_generated_);
67
68 Schedule* schedule = raw_assembler_->Export();
69 Handle<Code> code = Pipeline::GenerateCodeForCodeStub(
Ben Murdoch109988c2016-05-18 11:27:45 +010070 isolate(), raw_assembler_->call_descriptor(), graph(), schedule, flags_,
Ben Murdoch014dc512016-03-22 12:00:34 +000071 name_);
72
73 code_generated_ = true;
74 return code;
75}
76
77
78Node* CodeStubAssembler::Int32Constant(int value) {
79 return raw_assembler_->Int32Constant(value);
80}
81
82
83Node* CodeStubAssembler::IntPtrConstant(intptr_t value) {
84 return raw_assembler_->IntPtrConstant(value);
85}
86
87
88Node* CodeStubAssembler::NumberConstant(double value) {
89 return raw_assembler_->NumberConstant(value);
90}
91
Ben Murdoch3b9bc312016-06-02 14:46:10 +010092Node* CodeStubAssembler::SmiConstant(Smi* value) {
93 return IntPtrConstant(bit_cast<intptr_t>(value));
94}
Ben Murdoch014dc512016-03-22 12:00:34 +000095
96Node* CodeStubAssembler::HeapConstant(Handle<HeapObject> object) {
97 return raw_assembler_->HeapConstant(object);
98}
99
100
101Node* CodeStubAssembler::BooleanConstant(bool value) {
102 return raw_assembler_->BooleanConstant(value);
103}
104
Ben Murdoch109988c2016-05-18 11:27:45 +0100105Node* CodeStubAssembler::ExternalConstant(ExternalReference address) {
106 return raw_assembler_->ExternalConstant(address);
107}
Ben Murdoch014dc512016-03-22 12:00:34 +0000108
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100109Node* CodeStubAssembler::Float64Constant(double value) {
110 return raw_assembler_->Float64Constant(value);
111}
112
113Node* CodeStubAssembler::BooleanMapConstant() {
114 return HeapConstant(isolate()->factory()->boolean_map());
115}
116
117Node* CodeStubAssembler::HeapNumberMapConstant() {
118 return HeapConstant(isolate()->factory()->heap_number_map());
119}
120
121Node* CodeStubAssembler::NullConstant() {
122 return LoadRoot(Heap::kNullValueRootIndex);
123}
124
125Node* CodeStubAssembler::UndefinedConstant() {
126 return LoadRoot(Heap::kUndefinedValueRootIndex);
127}
128
Ben Murdoch014dc512016-03-22 12:00:34 +0000129Node* CodeStubAssembler::Parameter(int value) {
130 return raw_assembler_->Parameter(value);
131}
132
Ben Murdoch014dc512016-03-22 12:00:34 +0000133void CodeStubAssembler::Return(Node* value) {
134 return raw_assembler_->Return(value);
135}
136
Ben Murdoch109988c2016-05-18 11:27:45 +0100137void CodeStubAssembler::Bind(CodeStubAssembler::Label* label) {
138 return label->Bind();
139}
140
141Node* CodeStubAssembler::LoadFramePointer() {
142 return raw_assembler_->LoadFramePointer();
143}
144
145Node* CodeStubAssembler::LoadParentFramePointer() {
146 return raw_assembler_->LoadParentFramePointer();
147}
148
149Node* CodeStubAssembler::LoadStackPointer() {
150 return raw_assembler_->LoadStackPointer();
151}
Ben Murdoch014dc512016-03-22 12:00:34 +0000152
153Node* CodeStubAssembler::SmiShiftBitsConstant() {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100154 return IntPtrConstant(kSmiShiftSize + kSmiTagSize);
Ben Murdoch014dc512016-03-22 12:00:34 +0000155}
156
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100157Node* CodeStubAssembler::Float64Round(Node* x) {
158 Node* one = Float64Constant(1.0);
159 Node* one_half = Float64Constant(0.5);
160
161 Variable var_x(this, MachineRepresentation::kFloat64);
162 Label return_x(this);
163
164 // Round up {x} towards Infinity.
165 var_x.Bind(Float64Ceil(x));
166
167 GotoIf(Float64LessThanOrEqual(Float64Sub(var_x.value(), one_half), x),
168 &return_x);
169 var_x.Bind(Float64Sub(var_x.value(), one));
170 Goto(&return_x);
171
172 Bind(&return_x);
173 return var_x.value();
174}
175
176Node* CodeStubAssembler::Float64Ceil(Node* x) {
177 if (raw_assembler_->machine()->Float64RoundUp().IsSupported()) {
178 return raw_assembler_->Float64RoundUp(x);
179 }
180
181 Node* one = Float64Constant(1.0);
182 Node* zero = Float64Constant(0.0);
183 Node* two_52 = Float64Constant(4503599627370496.0E0);
184 Node* minus_two_52 = Float64Constant(-4503599627370496.0E0);
185
186 Variable var_x(this, MachineRepresentation::kFloat64);
187 Label return_x(this), return_minus_x(this);
188 var_x.Bind(x);
189
190 // Check if {x} is greater than zero.
191 Label if_xgreaterthanzero(this), if_xnotgreaterthanzero(this);
192 Branch(Float64GreaterThan(x, zero), &if_xgreaterthanzero,
193 &if_xnotgreaterthanzero);
194
195 Bind(&if_xgreaterthanzero);
196 {
197 // Just return {x} unless it's in the range ]0,2^52[.
198 GotoIf(Float64GreaterThanOrEqual(x, two_52), &return_x);
199
200 // Round positive {x} towards Infinity.
201 var_x.Bind(Float64Sub(Float64Add(two_52, x), two_52));
202 GotoUnless(Float64LessThan(var_x.value(), x), &return_x);
203 var_x.Bind(Float64Add(var_x.value(), one));
204 Goto(&return_x);
205 }
206
207 Bind(&if_xnotgreaterthanzero);
208 {
209 // Just return {x} unless it's in the range ]-2^52,0[
210 GotoIf(Float64LessThanOrEqual(x, minus_two_52), &return_x);
211 GotoUnless(Float64LessThan(x, zero), &return_x);
212
213 // Round negated {x} towards Infinity and return the result negated.
214 Node* minus_x = Float64Neg(x);
215 var_x.Bind(Float64Sub(Float64Add(two_52, minus_x), two_52));
216 GotoUnless(Float64GreaterThan(var_x.value(), minus_x), &return_minus_x);
217 var_x.Bind(Float64Sub(var_x.value(), one));
218 Goto(&return_minus_x);
219 }
220
221 Bind(&return_minus_x);
222 var_x.Bind(Float64Neg(var_x.value()));
223 Goto(&return_x);
224
225 Bind(&return_x);
226 return var_x.value();
227}
228
229Node* CodeStubAssembler::Float64Floor(Node* x) {
230 if (raw_assembler_->machine()->Float64RoundDown().IsSupported()) {
231 return raw_assembler_->Float64RoundDown(x);
232 }
233
234 Node* one = Float64Constant(1.0);
235 Node* zero = Float64Constant(0.0);
236 Node* two_52 = Float64Constant(4503599627370496.0E0);
237 Node* minus_two_52 = Float64Constant(-4503599627370496.0E0);
238
239 Variable var_x(this, MachineRepresentation::kFloat64);
240 Label return_x(this), return_minus_x(this);
241 var_x.Bind(x);
242
243 // Check if {x} is greater than zero.
244 Label if_xgreaterthanzero(this), if_xnotgreaterthanzero(this);
245 Branch(Float64GreaterThan(x, zero), &if_xgreaterthanzero,
246 &if_xnotgreaterthanzero);
247
248 Bind(&if_xgreaterthanzero);
249 {
250 // Just return {x} unless it's in the range ]0,2^52[.
251 GotoIf(Float64GreaterThanOrEqual(x, two_52), &return_x);
252
253 // Round positive {x} towards -Infinity.
254 var_x.Bind(Float64Sub(Float64Add(two_52, x), two_52));
255 GotoUnless(Float64GreaterThan(var_x.value(), x), &return_x);
256 var_x.Bind(Float64Sub(var_x.value(), one));
257 Goto(&return_x);
258 }
259
260 Bind(&if_xnotgreaterthanzero);
261 {
262 // Just return {x} unless it's in the range ]-2^52,0[
263 GotoIf(Float64LessThanOrEqual(x, minus_two_52), &return_x);
264 GotoUnless(Float64LessThan(x, zero), &return_x);
265
266 // Round negated {x} towards -Infinity and return the result negated.
267 Node* minus_x = Float64Neg(x);
268 var_x.Bind(Float64Sub(Float64Add(two_52, minus_x), two_52));
269 GotoUnless(Float64LessThan(var_x.value(), minus_x), &return_minus_x);
270 var_x.Bind(Float64Add(var_x.value(), one));
271 Goto(&return_minus_x);
272 }
273
274 Bind(&return_minus_x);
275 var_x.Bind(Float64Neg(var_x.value()));
276 Goto(&return_x);
277
278 Bind(&return_x);
279 return var_x.value();
280}
281
282Node* CodeStubAssembler::Float64Trunc(Node* x) {
283 if (raw_assembler_->machine()->Float64RoundTruncate().IsSupported()) {
284 return raw_assembler_->Float64RoundTruncate(x);
285 }
286
287 Node* one = Float64Constant(1.0);
288 Node* zero = Float64Constant(0.0);
289 Node* two_52 = Float64Constant(4503599627370496.0E0);
290 Node* minus_two_52 = Float64Constant(-4503599627370496.0E0);
291
292 Variable var_x(this, MachineRepresentation::kFloat64);
293 Label return_x(this), return_minus_x(this);
294 var_x.Bind(x);
295
296 // Check if {x} is greater than 0.
297 Label if_xgreaterthanzero(this), if_xnotgreaterthanzero(this);
298 Branch(Float64GreaterThan(x, zero), &if_xgreaterthanzero,
299 &if_xnotgreaterthanzero);
300
301 Bind(&if_xgreaterthanzero);
302 {
303 if (raw_assembler_->machine()->Float64RoundDown().IsSupported()) {
304 var_x.Bind(raw_assembler_->Float64RoundDown(x));
305 } else {
306 // Just return {x} unless it's in the range ]0,2^52[.
307 GotoIf(Float64GreaterThanOrEqual(x, two_52), &return_x);
308
309 // Round positive {x} towards -Infinity.
310 var_x.Bind(Float64Sub(Float64Add(two_52, x), two_52));
311 GotoUnless(Float64GreaterThan(var_x.value(), x), &return_x);
312 var_x.Bind(Float64Sub(var_x.value(), one));
313 }
314 Goto(&return_x);
315 }
316
317 Bind(&if_xnotgreaterthanzero);
318 {
319 if (raw_assembler_->machine()->Float64RoundUp().IsSupported()) {
320 var_x.Bind(raw_assembler_->Float64RoundUp(x));
321 Goto(&return_x);
322 } else {
323 // Just return {x} unless its in the range ]-2^52,0[.
324 GotoIf(Float64LessThanOrEqual(x, minus_two_52), &return_x);
325 GotoUnless(Float64LessThan(x, zero), &return_x);
326
327 // Round negated {x} towards -Infinity and return result negated.
328 Node* minus_x = Float64Neg(x);
329 var_x.Bind(Float64Sub(Float64Add(two_52, minus_x), two_52));
330 GotoUnless(Float64GreaterThan(var_x.value(), minus_x), &return_minus_x);
331 var_x.Bind(Float64Sub(var_x.value(), one));
332 Goto(&return_minus_x);
333 }
334 }
335
336 Bind(&return_minus_x);
337 var_x.Bind(Float64Neg(var_x.value()));
338 Goto(&return_x);
339
340 Bind(&return_x);
341 return var_x.value();
342}
Ben Murdoch014dc512016-03-22 12:00:34 +0000343
344Node* CodeStubAssembler::SmiTag(Node* value) {
345 return raw_assembler_->WordShl(value, SmiShiftBitsConstant());
346}
347
Ben Murdoch014dc512016-03-22 12:00:34 +0000348Node* CodeStubAssembler::SmiUntag(Node* value) {
349 return raw_assembler_->WordSar(value, SmiShiftBitsConstant());
350}
351
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100352Node* CodeStubAssembler::SmiToWord32(Node* value) {
353 Node* result = raw_assembler_->WordSar(value, SmiShiftBitsConstant());
354 if (raw_assembler_->machine()->Is64()) {
355 result = raw_assembler_->TruncateInt64ToInt32(result);
356 }
357 return result;
358}
359
360Node* CodeStubAssembler::SmiToFloat64(Node* value) {
361 return ChangeInt32ToFloat64(SmiUntag(value));
362}
363
364Node* CodeStubAssembler::SmiAdd(Node* a, Node* b) { return IntPtrAdd(a, b); }
365
366Node* CodeStubAssembler::SmiAddWithOverflow(Node* a, Node* b) {
367 return IntPtrAddWithOverflow(a, b);
368}
369
370Node* CodeStubAssembler::SmiSub(Node* a, Node* b) { return IntPtrSub(a, b); }
371
372Node* CodeStubAssembler::SmiSubWithOverflow(Node* a, Node* b) {
373 return IntPtrSubWithOverflow(a, b);
374}
375
376Node* CodeStubAssembler::SmiEqual(Node* a, Node* b) { return WordEqual(a, b); }
377
378Node* CodeStubAssembler::SmiLessThan(Node* a, Node* b) {
379 return IntPtrLessThan(a, b);
380}
381
382Node* CodeStubAssembler::SmiLessThanOrEqual(Node* a, Node* b) {
383 return IntPtrLessThanOrEqual(a, b);
384}
385
386Node* CodeStubAssembler::SmiMin(Node* a, Node* b) {
387 // TODO(bmeurer): Consider using Select once available.
388 Variable min(this, MachineRepresentation::kTagged);
389 Label if_a(this), if_b(this), join(this);
390 BranchIfSmiLessThan(a, b, &if_a, &if_b);
391 Bind(&if_a);
392 min.Bind(a);
393 Goto(&join);
394 Bind(&if_b);
395 min.Bind(b);
396 Goto(&join);
397 Bind(&join);
398 return min.value();
399}
400
Ben Murdoch109988c2016-05-18 11:27:45 +0100401#define DEFINE_CODE_STUB_ASSEMBER_BINARY_OP(name) \
402 Node* CodeStubAssembler::name(Node* a, Node* b) { \
403 return raw_assembler_->name(a, b); \
404 }
405CODE_STUB_ASSEMBLER_BINARY_OP_LIST(DEFINE_CODE_STUB_ASSEMBER_BINARY_OP)
406#undef DEFINE_CODE_STUB_ASSEMBER_BINARY_OP
Ben Murdoch014dc512016-03-22 12:00:34 +0000407
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100408Node* CodeStubAssembler::WordShl(Node* value, int shift) {
409 return raw_assembler_->WordShl(value, IntPtrConstant(shift));
Ben Murdoch014dc512016-03-22 12:00:34 +0000410}
411
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100412#define DEFINE_CODE_STUB_ASSEMBER_UNARY_OP(name) \
413 Node* CodeStubAssembler::name(Node* a) { return raw_assembler_->name(a); }
414CODE_STUB_ASSEMBLER_UNARY_OP_LIST(DEFINE_CODE_STUB_ASSEMBER_UNARY_OP)
415#undef DEFINE_CODE_STUB_ASSEMBER_UNARY_OP
Ben Murdoch014dc512016-03-22 12:00:34 +0000416
Ben Murdoch109988c2016-05-18 11:27:45 +0100417Node* CodeStubAssembler::WordIsSmi(Node* a) {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100418 return WordEqual(raw_assembler_->WordAnd(a, IntPtrConstant(kSmiTagMask)),
419 IntPtrConstant(0));
Ben Murdoch109988c2016-05-18 11:27:45 +0100420}
421
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100422Node* CodeStubAssembler::WordIsPositiveSmi(Node* a) {
423 return WordEqual(
424 raw_assembler_->WordAnd(a, IntPtrConstant(kSmiTagMask | kSmiSignMask)),
425 IntPtrConstant(0));
Ben Murdoch109988c2016-05-18 11:27:45 +0100426}
Ben Murdoch014dc512016-03-22 12:00:34 +0000427
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100428Node* CodeStubAssembler::LoadBufferObject(Node* buffer, int offset,
429 MachineType rep) {
430 return raw_assembler_->Load(rep, buffer, IntPtrConstant(offset));
431}
432
433Node* CodeStubAssembler::LoadObjectField(Node* object, int offset,
434 MachineType rep) {
435 return raw_assembler_->Load(rep, object,
Ben Murdoch014dc512016-03-22 12:00:34 +0000436 IntPtrConstant(offset - kHeapObjectTag));
437}
438
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100439Node* CodeStubAssembler::LoadHeapNumberValue(Node* object) {
440 return Load(MachineType::Float64(), object,
441 IntPtrConstant(HeapNumber::kValueOffset - kHeapObjectTag));
442}
443
444Node* CodeStubAssembler::StoreHeapNumberValue(Node* object, Node* value) {
445 return StoreNoWriteBarrier(
446 MachineRepresentation::kFloat64, object,
447 IntPtrConstant(HeapNumber::kValueOffset - kHeapObjectTag), value);
448}
449
450Node* CodeStubAssembler::TruncateHeapNumberValueToWord32(Node* object) {
451 Node* value = LoadHeapNumberValue(object);
452 return raw_assembler_->TruncateFloat64ToInt32(TruncationMode::kJavaScript,
453 value);
454}
455
456Node* CodeStubAssembler::LoadMapBitField(Node* map) {
457 return Load(MachineType::Uint8(), map,
458 IntPtrConstant(Map::kBitFieldOffset - kHeapObjectTag));
459}
460
461Node* CodeStubAssembler::LoadMapBitField2(Node* map) {
462 return Load(MachineType::Uint8(), map,
463 IntPtrConstant(Map::kBitField2Offset - kHeapObjectTag));
464}
465
466Node* CodeStubAssembler::LoadMapBitField3(Node* map) {
467 return Load(MachineType::Uint32(), map,
468 IntPtrConstant(Map::kBitField3Offset - kHeapObjectTag));
469}
470
471Node* CodeStubAssembler::LoadMapInstanceType(Node* map) {
472 return Load(MachineType::Uint8(), map,
473 IntPtrConstant(Map::kInstanceTypeOffset - kHeapObjectTag));
474}
475
476Node* CodeStubAssembler::LoadMapDescriptors(Node* map) {
477 return LoadObjectField(map, Map::kDescriptorsOffset);
478}
479
480Node* CodeStubAssembler::LoadNameHash(Node* name) {
481 return Load(MachineType::Uint32(), name,
482 IntPtrConstant(Name::kHashFieldOffset - kHeapObjectTag));
483}
484
485Node* CodeStubAssembler::LoadFixedArrayElementInt32Index(
486 Node* object, Node* int32_index, int additional_offset) {
487 Node* header_size = IntPtrConstant(additional_offset +
488 FixedArray::kHeaderSize - kHeapObjectTag);
489 Node* scaled_index = WordShl(int32_index, IntPtrConstant(kPointerSizeLog2));
490 Node* offset = IntPtrAdd(scaled_index, header_size);
491 return Load(MachineType::AnyTagged(), object, offset);
492}
493
Ben Murdoch109988c2016-05-18 11:27:45 +0100494Node* CodeStubAssembler::LoadFixedArrayElementSmiIndex(Node* object,
495 Node* smi_index,
496 int additional_offset) {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100497 int const kSmiShiftBits = kSmiShiftSize + kSmiTagSize;
498 Node* header_size = IntPtrConstant(additional_offset +
499 FixedArray::kHeaderSize - kHeapObjectTag);
Ben Murdoch109988c2016-05-18 11:27:45 +0100500 Node* scaled_index =
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100501 (kSmiShiftBits > kPointerSizeLog2)
502 ? WordSar(smi_index, IntPtrConstant(kSmiShiftBits - kPointerSizeLog2))
503 : WordShl(smi_index,
504 IntPtrConstant(kPointerSizeLog2 - kSmiShiftBits));
505 Node* offset = IntPtrAdd(scaled_index, header_size);
506 return Load(MachineType::AnyTagged(), object, offset);
Ben Murdoch109988c2016-05-18 11:27:45 +0100507}
508
509Node* CodeStubAssembler::LoadFixedArrayElementConstantIndex(Node* object,
510 int index) {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100511 Node* offset = IntPtrConstant(FixedArray::kHeaderSize - kHeapObjectTag +
512 index * kPointerSize);
Ben Murdoch109988c2016-05-18 11:27:45 +0100513 return raw_assembler_->Load(MachineType::AnyTagged(), object, offset);
514}
515
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100516Node* CodeStubAssembler::StoreFixedArrayElementNoWriteBarrier(Node* object,
517 Node* index,
518 Node* value) {
519 Node* offset =
520 IntPtrAdd(WordShl(index, IntPtrConstant(kPointerSizeLog2)),
521 IntPtrConstant(FixedArray::kHeaderSize - kHeapObjectTag));
522 return StoreNoWriteBarrier(MachineRepresentation::kTagged, object, offset,
523 value);
524}
525
Ben Murdoch109988c2016-05-18 11:27:45 +0100526Node* CodeStubAssembler::LoadRoot(Heap::RootListIndex root_index) {
527 if (isolate()->heap()->RootCanBeTreatedAsConstant(root_index)) {
528 Handle<Object> root = isolate()->heap()->root_handle(root_index);
529 if (root->IsSmi()) {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100530 return SmiConstant(Smi::cast(*root));
Ben Murdoch109988c2016-05-18 11:27:45 +0100531 } else {
532 return HeapConstant(Handle<HeapObject>::cast(root));
533 }
534 }
535
536 compiler::Node* roots_array_start =
537 ExternalConstant(ExternalReference::roots_array_start(isolate()));
538 USE(roots_array_start);
539
540 // TODO(danno): Implement thee root-access case where the root is not constant
541 // and must be loaded from the root array.
542 UNIMPLEMENTED();
543 return nullptr;
544}
545
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100546Node* CodeStubAssembler::AllocateRawUnaligned(Node* size_in_bytes,
547 AllocationFlags flags,
548 Node* top_address,
549 Node* limit_address) {
550 Node* top = Load(MachineType::Pointer(), top_address);
551 Node* limit = Load(MachineType::Pointer(), limit_address);
552
553 // If there's not enough space, call the runtime.
554 RawMachineLabel runtime_call(RawMachineLabel::kDeferred), no_runtime_call,
555 merge_runtime;
556 raw_assembler_->Branch(
557 raw_assembler_->IntPtrLessThan(IntPtrSub(limit, top), size_in_bytes),
558 &runtime_call, &no_runtime_call);
559
560 raw_assembler_->Bind(&runtime_call);
561 // AllocateInTargetSpace does not use the context.
562 Node* context = IntPtrConstant(0);
563 Node* runtime_flags = SmiTag(Int32Constant(
564 AllocateDoubleAlignFlag::encode(false) |
565 AllocateTargetSpace::encode(flags & kPretenured
566 ? AllocationSpace::OLD_SPACE
567 : AllocationSpace::NEW_SPACE)));
568 Node* runtime_result = CallRuntime(Runtime::kAllocateInTargetSpace, context,
569 SmiTag(size_in_bytes), runtime_flags);
570 raw_assembler_->Goto(&merge_runtime);
571
572 // When there is enough space, return `top' and bump it up.
573 raw_assembler_->Bind(&no_runtime_call);
574 Node* no_runtime_result = top;
575 StoreNoWriteBarrier(MachineType::PointerRepresentation(), top_address,
576 IntPtrAdd(top, size_in_bytes));
577 no_runtime_result =
578 IntPtrAdd(no_runtime_result, IntPtrConstant(kHeapObjectTag));
579 raw_assembler_->Goto(&merge_runtime);
580
581 raw_assembler_->Bind(&merge_runtime);
582 return raw_assembler_->Phi(MachineType::PointerRepresentation(),
583 runtime_result, no_runtime_result);
584}
585
586Node* CodeStubAssembler::AllocateRawAligned(Node* size_in_bytes,
587 AllocationFlags flags,
588 Node* top_address,
589 Node* limit_address) {
590 Node* top = Load(MachineType::Pointer(), top_address);
591 Node* limit = Load(MachineType::Pointer(), limit_address);
592 Node* adjusted_size = size_in_bytes;
593 if (flags & kDoubleAlignment) {
594 // TODO(epertoso): Simd128 alignment.
595 RawMachineLabel aligned, not_aligned, merge;
596 raw_assembler_->Branch(WordAnd(top, IntPtrConstant(kDoubleAlignmentMask)),
597 &not_aligned, &aligned);
598
599 raw_assembler_->Bind(&not_aligned);
600 Node* not_aligned_size =
601 IntPtrAdd(size_in_bytes, IntPtrConstant(kPointerSize));
602 raw_assembler_->Goto(&merge);
603
604 raw_assembler_->Bind(&aligned);
605 raw_assembler_->Goto(&merge);
606
607 raw_assembler_->Bind(&merge);
608 adjusted_size = raw_assembler_->Phi(MachineType::PointerRepresentation(),
609 not_aligned_size, adjusted_size);
610 }
611
612 Node* address = AllocateRawUnaligned(adjusted_size, kNone, top, limit);
613
614 RawMachineLabel needs_filler, doesnt_need_filler, merge_address;
615 raw_assembler_->Branch(
616 raw_assembler_->IntPtrEqual(adjusted_size, size_in_bytes),
617 &doesnt_need_filler, &needs_filler);
618
619 raw_assembler_->Bind(&needs_filler);
620 // Store a filler and increase the address by kPointerSize.
621 // TODO(epertoso): this code assumes that we only align to kDoubleSize. Change
622 // it when Simd128 alignment is supported.
623 StoreNoWriteBarrier(MachineType::PointerRepresentation(), top,
624 LoadRoot(Heap::kOnePointerFillerMapRootIndex));
625 Node* address_with_filler = IntPtrAdd(address, IntPtrConstant(kPointerSize));
626 raw_assembler_->Goto(&merge_address);
627
628 raw_assembler_->Bind(&doesnt_need_filler);
629 Node* address_without_filler = address;
630 raw_assembler_->Goto(&merge_address);
631
632 raw_assembler_->Bind(&merge_address);
633 address = raw_assembler_->Phi(MachineType::PointerRepresentation(),
634 address_with_filler, address_without_filler);
635 // Update the top.
636 StoreNoWriteBarrier(MachineType::PointerRepresentation(), top_address,
637 IntPtrAdd(top, adjusted_size));
638 return address;
639}
640
641Node* CodeStubAssembler::Allocate(int size_in_bytes, AllocationFlags flags) {
642 bool const new_space = !(flags & kPretenured);
643 Node* top_address = ExternalConstant(
644 new_space
645 ? ExternalReference::new_space_allocation_top_address(isolate())
646 : ExternalReference::old_space_allocation_top_address(isolate()));
647 Node* limit_address = ExternalConstant(
648 new_space
649 ? ExternalReference::new_space_allocation_limit_address(isolate())
650 : ExternalReference::old_space_allocation_limit_address(isolate()));
651
652#ifdef V8_HOST_ARCH_32_BIT
653 if (flags & kDoubleAlignment) {
654 return AllocateRawAligned(IntPtrConstant(size_in_bytes), flags, top_address,
655 limit_address);
656 }
657#endif
658
659 return AllocateRawUnaligned(IntPtrConstant(size_in_bytes), flags, top_address,
660 limit_address);
661}
662
663Node* CodeStubAssembler::AllocateHeapNumber() {
664 Node* result = Allocate(HeapNumber::kSize, kNone);
665 StoreMapNoWriteBarrier(result, HeapNumberMapConstant());
666 return result;
667}
668
669Node* CodeStubAssembler::AllocateHeapNumberWithValue(Node* value) {
670 Node* result = AllocateHeapNumber();
671 StoreHeapNumberValue(result, value);
672 return result;
673}
674
Ben Murdoch109988c2016-05-18 11:27:45 +0100675Node* CodeStubAssembler::Load(MachineType rep, Node* base) {
676 return raw_assembler_->Load(rep, base);
677}
678
679Node* CodeStubAssembler::Load(MachineType rep, Node* base, Node* index) {
680 return raw_assembler_->Load(rep, base, index);
681}
682
683Node* CodeStubAssembler::Store(MachineRepresentation rep, Node* base,
684 Node* value) {
685 return raw_assembler_->Store(rep, base, value, kFullWriteBarrier);
686}
687
688Node* CodeStubAssembler::Store(MachineRepresentation rep, Node* base,
689 Node* index, Node* value) {
690 return raw_assembler_->Store(rep, base, index, value, kFullWriteBarrier);
691}
692
693Node* CodeStubAssembler::StoreNoWriteBarrier(MachineRepresentation rep,
694 Node* base, Node* value) {
695 return raw_assembler_->Store(rep, base, value, kNoWriteBarrier);
696}
697
698Node* CodeStubAssembler::StoreNoWriteBarrier(MachineRepresentation rep,
699 Node* base, Node* index,
700 Node* value) {
701 return raw_assembler_->Store(rep, base, index, value, kNoWriteBarrier);
702}
703
704Node* CodeStubAssembler::Projection(int index, Node* value) {
705 return raw_assembler_->Projection(index, value);
706}
Ben Murdoch014dc512016-03-22 12:00:34 +0000707
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100708Node* CodeStubAssembler::LoadMap(Node* object) {
709 return LoadObjectField(object, HeapObject::kMapOffset);
710}
711
712Node* CodeStubAssembler::StoreMapNoWriteBarrier(Node* object, Node* map) {
713 return StoreNoWriteBarrier(
714 MachineRepresentation::kTagged, object,
715 IntPtrConstant(HeapNumber::kMapOffset - kHeapObjectTag), map);
716}
717
718Node* CodeStubAssembler::LoadInstanceType(Node* object) {
719 return LoadMapInstanceType(LoadMap(object));
720}
721
722Node* CodeStubAssembler::LoadElements(Node* object) {
723 return LoadObjectField(object, JSObject::kElementsOffset);
724}
725
726Node* CodeStubAssembler::LoadFixedArrayBaseLength(Node* array) {
727 return LoadObjectField(array, FixedArrayBase::kLengthOffset);
728}
729
730Node* CodeStubAssembler::BitFieldDecode(Node* word32, uint32_t shift,
731 uint32_t mask) {
732 return raw_assembler_->Word32Shr(
733 raw_assembler_->Word32And(word32, raw_assembler_->Int32Constant(mask)),
734 raw_assembler_->Int32Constant(shift));
735}
736
737Node* CodeStubAssembler::ChangeFloat64ToTagged(Node* value) {
738 Node* value32 = raw_assembler_->TruncateFloat64ToInt32(
739 TruncationMode::kRoundToZero, value);
740 Node* value64 = ChangeInt32ToFloat64(value32);
741
742 Label if_valueisint32(this), if_valueisheapnumber(this), if_join(this);
743
744 Label if_valueisequal(this), if_valueisnotequal(this);
745 Branch(Float64Equal(value, value64), &if_valueisequal, &if_valueisnotequal);
746 Bind(&if_valueisequal);
747 {
748 Label if_valueiszero(this), if_valueisnotzero(this);
749 Branch(Float64Equal(value, Float64Constant(0.0)), &if_valueiszero,
750 &if_valueisnotzero);
751
752 Bind(&if_valueiszero);
753 BranchIfInt32LessThan(raw_assembler_->Float64ExtractHighWord32(value),
754 Int32Constant(0), &if_valueisheapnumber,
755 &if_valueisint32);
756
757 Bind(&if_valueisnotzero);
758 Goto(&if_valueisint32);
759 }
760 Bind(&if_valueisnotequal);
761 Goto(&if_valueisheapnumber);
762
763 Variable var_result(this, MachineRepresentation::kTagged);
764 Bind(&if_valueisint32);
765 {
766 if (raw_assembler_->machine()->Is64()) {
767 Node* result = SmiTag(ChangeInt32ToInt64(value32));
768 var_result.Bind(result);
769 Goto(&if_join);
770 } else {
771 Node* pair = Int32AddWithOverflow(value32, value32);
772 Node* overflow = Projection(1, pair);
773 Label if_overflow(this, Label::kDeferred), if_notoverflow(this);
774 Branch(overflow, &if_overflow, &if_notoverflow);
775 Bind(&if_overflow);
776 Goto(&if_valueisheapnumber);
777 Bind(&if_notoverflow);
778 {
779 Node* result = Projection(0, pair);
780 var_result.Bind(result);
781 Goto(&if_join);
782 }
783 }
784 }
785 Bind(&if_valueisheapnumber);
786 {
787 Node* result = AllocateHeapNumberWithValue(value);
788 var_result.Bind(result);
789 Goto(&if_join);
790 }
791 Bind(&if_join);
792 return var_result.value();
793}
794
795Node* CodeStubAssembler::ChangeInt32ToTagged(Node* value) {
796 if (raw_assembler_->machine()->Is64()) {
797 return SmiTag(ChangeInt32ToInt64(value));
798 }
799 Variable var_result(this, MachineRepresentation::kTagged);
800 Node* pair = Int32AddWithOverflow(value, value);
801 Node* overflow = Projection(1, pair);
802 Label if_overflow(this, Label::kDeferred), if_notoverflow(this),
803 if_join(this);
804 Branch(overflow, &if_overflow, &if_notoverflow);
805 Bind(&if_overflow);
806 {
807 Node* value64 = ChangeInt32ToFloat64(value);
808 Node* result = AllocateHeapNumberWithValue(value64);
809 var_result.Bind(result);
810 }
811 Goto(&if_join);
812 Bind(&if_notoverflow);
813 {
814 Node* result = Projection(0, pair);
815 var_result.Bind(result);
816 }
817 Goto(&if_join);
818 Bind(&if_join);
819 return var_result.value();
820}
821
822Node* CodeStubAssembler::TruncateTaggedToFloat64(Node* context, Node* value) {
823 // We might need to loop once due to ToNumber conversion.
824 Variable var_value(this, MachineRepresentation::kTagged),
825 var_result(this, MachineRepresentation::kFloat64);
826 Label loop(this, &var_value), done_loop(this, &var_result);
827 var_value.Bind(value);
828 Goto(&loop);
829 Bind(&loop);
830 {
831 // Load the current {value}.
832 value = var_value.value();
833
834 // Check if the {value} is a Smi or a HeapObject.
835 Label if_valueissmi(this), if_valueisnotsmi(this);
836 Branch(WordIsSmi(value), &if_valueissmi, &if_valueisnotsmi);
837
838 Bind(&if_valueissmi);
839 {
840 // Convert the Smi {value}.
841 var_result.Bind(SmiToFloat64(value));
842 Goto(&done_loop);
843 }
844
845 Bind(&if_valueisnotsmi);
846 {
847 // Check if {value} is a HeapNumber.
848 Label if_valueisheapnumber(this),
849 if_valueisnotheapnumber(this, Label::kDeferred);
850 Branch(WordEqual(LoadMap(value), HeapNumberMapConstant()),
851 &if_valueisheapnumber, &if_valueisnotheapnumber);
852
853 Bind(&if_valueisheapnumber);
854 {
855 // Load the floating point value.
856 var_result.Bind(LoadHeapNumberValue(value));
857 Goto(&done_loop);
858 }
859
860 Bind(&if_valueisnotheapnumber);
861 {
862 // Convert the {value} to a Number first.
863 Callable callable = CodeFactory::NonNumberToNumber(isolate());
864 var_value.Bind(CallStub(callable, context, value));
865 Goto(&loop);
866 }
867 }
868 }
869 Bind(&done_loop);
870 return var_result.value();
871}
872
873Node* CodeStubAssembler::TruncateTaggedToWord32(Node* context, Node* value) {
874 // We might need to loop once due to ToNumber conversion.
875 Variable var_value(this, MachineRepresentation::kTagged),
876 var_result(this, MachineRepresentation::kWord32);
877 Label loop(this, &var_value), done_loop(this, &var_result);
878 var_value.Bind(value);
879 Goto(&loop);
880 Bind(&loop);
881 {
882 // Load the current {value}.
883 value = var_value.value();
884
885 // Check if the {value} is a Smi or a HeapObject.
886 Label if_valueissmi(this), if_valueisnotsmi(this);
887 Branch(WordIsSmi(value), &if_valueissmi, &if_valueisnotsmi);
888
889 Bind(&if_valueissmi);
890 {
891 // Convert the Smi {value}.
892 var_result.Bind(SmiToWord32(value));
893 Goto(&done_loop);
894 }
895
896 Bind(&if_valueisnotsmi);
897 {
898 // Check if {value} is a HeapNumber.
899 Label if_valueisheapnumber(this),
900 if_valueisnotheapnumber(this, Label::kDeferred);
901 Branch(WordEqual(LoadMap(value), HeapNumberMapConstant()),
902 &if_valueisheapnumber, &if_valueisnotheapnumber);
903
904 Bind(&if_valueisheapnumber);
905 {
906 // Truncate the floating point value.
907 var_result.Bind(TruncateHeapNumberValueToWord32(value));
908 Goto(&done_loop);
909 }
910
911 Bind(&if_valueisnotheapnumber);
912 {
913 // Convert the {value} to a Number first.
914 Callable callable = CodeFactory::NonNumberToNumber(isolate());
915 var_value.Bind(CallStub(callable, context, value));
916 Goto(&loop);
917 }
918 }
919 }
920 Bind(&done_loop);
921 return var_result.value();
922}
923
924void CodeStubAssembler::BranchIf(Node* condition, Label* if_true,
925 Label* if_false) {
926 Label if_condition_is_true(this), if_condition_is_false(this);
927 Branch(condition, &if_condition_is_true, &if_condition_is_false);
928 Bind(&if_condition_is_true);
929 Goto(if_true);
930 Bind(&if_condition_is_false);
931 Goto(if_false);
932}
933
Ben Murdoch014dc512016-03-22 12:00:34 +0000934Node* CodeStubAssembler::CallN(CallDescriptor* descriptor, Node* code_target,
935 Node** args) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100936 CallPrologue();
937 Node* return_value = raw_assembler_->CallN(descriptor, code_target, args);
938 CallEpilogue();
939 return return_value;
Ben Murdoch014dc512016-03-22 12:00:34 +0000940}
941
942
943Node* CodeStubAssembler::TailCallN(CallDescriptor* descriptor,
944 Node* code_target, Node** args) {
945 return raw_assembler_->TailCallN(descriptor, code_target, args);
946}
947
Ben Murdoch109988c2016-05-18 11:27:45 +0100948Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id,
949 Node* context) {
950 CallPrologue();
951 Node* return_value = raw_assembler_->CallRuntime0(function_id, context);
952 CallEpilogue();
953 return return_value;
954}
Ben Murdoch014dc512016-03-22 12:00:34 +0000955
956Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id,
957 Node* context, Node* arg1) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100958 CallPrologue();
959 Node* return_value = raw_assembler_->CallRuntime1(function_id, arg1, context);
960 CallEpilogue();
961 return return_value;
Ben Murdoch014dc512016-03-22 12:00:34 +0000962}
963
Ben Murdoch014dc512016-03-22 12:00:34 +0000964Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id,
965 Node* context, Node* arg1, Node* arg2) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100966 CallPrologue();
967 Node* return_value =
968 raw_assembler_->CallRuntime2(function_id, arg1, arg2, context);
969 CallEpilogue();
970 return return_value;
Ben Murdoch014dc512016-03-22 12:00:34 +0000971}
972
Ben Murdoch109988c2016-05-18 11:27:45 +0100973Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id,
974 Node* context, Node* arg1, Node* arg2,
975 Node* arg3) {
976 CallPrologue();
977 Node* return_value =
978 raw_assembler_->CallRuntime3(function_id, arg1, arg2, arg3, context);
979 CallEpilogue();
980 return return_value;
981}
982
983Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id,
984 Node* context, Node* arg1, Node* arg2,
985 Node* arg3, Node* arg4) {
986 CallPrologue();
987 Node* return_value = raw_assembler_->CallRuntime4(function_id, arg1, arg2,
988 arg3, arg4, context);
989 CallEpilogue();
990 return return_value;
991}
Ben Murdoch014dc512016-03-22 12:00:34 +0000992
993Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100994 Node* context) {
995 return raw_assembler_->TailCallRuntime0(function_id, context);
996}
997
998Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
Ben Murdoch014dc512016-03-22 12:00:34 +0000999 Node* context, Node* arg1) {
1000 return raw_assembler_->TailCallRuntime1(function_id, arg1, context);
1001}
1002
Ben Murdoch014dc512016-03-22 12:00:34 +00001003Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
1004 Node* context, Node* arg1,
1005 Node* arg2) {
1006 return raw_assembler_->TailCallRuntime2(function_id, arg1, arg2, context);
1007}
1008
Ben Murdoch109988c2016-05-18 11:27:45 +01001009Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
1010 Node* context, Node* arg1, Node* arg2,
1011 Node* arg3) {
1012 return raw_assembler_->TailCallRuntime3(function_id, arg1, arg2, arg3,
1013 context);
1014}
1015
1016Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
1017 Node* context, Node* arg1, Node* arg2,
1018 Node* arg3, Node* arg4) {
1019 return raw_assembler_->TailCallRuntime4(function_id, arg1, arg2, arg3, arg4,
1020 context);
1021}
1022
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001023Node* CodeStubAssembler::CallStub(Callable const& callable, Node* context,
1024 Node* arg1, size_t result_size) {
1025 Node* target = HeapConstant(callable.code());
1026 return CallStub(callable.descriptor(), target, context, arg1, result_size);
1027}
1028
Ben Murdoch109988c2016-05-18 11:27:45 +01001029Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor,
1030 Node* target, Node* context, Node* arg1,
1031 size_t result_size) {
1032 CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor(
1033 isolate(), zone(), descriptor, descriptor.GetStackParameterCount(),
1034 CallDescriptor::kNoFlags, Operator::kNoProperties,
1035 MachineType::AnyTagged(), result_size);
1036
1037 Node** args = zone()->NewArray<Node*>(2);
1038 args[0] = arg1;
1039 args[1] = context;
1040
1041 return CallN(call_descriptor, target, args);
1042}
1043
1044Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor,
1045 Node* target, Node* context, Node* arg1,
1046 Node* arg2, size_t result_size) {
1047 CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor(
1048 isolate(), zone(), descriptor, descriptor.GetStackParameterCount(),
1049 CallDescriptor::kNoFlags, Operator::kNoProperties,
1050 MachineType::AnyTagged(), result_size);
1051
1052 Node** args = zone()->NewArray<Node*>(3);
1053 args[0] = arg1;
1054 args[1] = arg2;
1055 args[2] = context;
1056
1057 return CallN(call_descriptor, target, args);
1058}
1059
1060Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor,
1061 Node* target, Node* context, Node* arg1,
1062 Node* arg2, Node* arg3, size_t result_size) {
1063 CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor(
1064 isolate(), zone(), descriptor, descriptor.GetStackParameterCount(),
1065 CallDescriptor::kNoFlags, Operator::kNoProperties,
1066 MachineType::AnyTagged(), result_size);
1067
1068 Node** args = zone()->NewArray<Node*>(4);
1069 args[0] = arg1;
1070 args[1] = arg2;
1071 args[2] = arg3;
1072 args[3] = context;
1073
1074 return CallN(call_descriptor, target, args);
1075}
1076
1077Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor,
1078 Node* target, Node* context, Node* arg1,
1079 Node* arg2, Node* arg3, Node* arg4,
1080 size_t result_size) {
1081 CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor(
1082 isolate(), zone(), descriptor, descriptor.GetStackParameterCount(),
1083 CallDescriptor::kNoFlags, Operator::kNoProperties,
1084 MachineType::AnyTagged(), result_size);
1085
1086 Node** args = zone()->NewArray<Node*>(5);
1087 args[0] = arg1;
1088 args[1] = arg2;
1089 args[2] = arg3;
1090 args[3] = arg4;
1091 args[4] = context;
1092
1093 return CallN(call_descriptor, target, args);
1094}
1095
1096Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor,
1097 Node* target, Node* context, Node* arg1,
1098 Node* arg2, Node* arg3, Node* arg4,
1099 Node* arg5, size_t result_size) {
1100 CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor(
1101 isolate(), zone(), descriptor, descriptor.GetStackParameterCount(),
1102 CallDescriptor::kNoFlags, Operator::kNoProperties,
1103 MachineType::AnyTagged(), result_size);
1104
1105 Node** args = zone()->NewArray<Node*>(6);
1106 args[0] = arg1;
1107 args[1] = arg2;
1108 args[2] = arg3;
1109 args[3] = arg4;
1110 args[4] = arg5;
1111 args[5] = context;
1112
1113 return CallN(call_descriptor, target, args);
1114}
1115
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001116Node* CodeStubAssembler::TailCallStub(Callable const& callable, Node* context,
1117 Node* arg1, Node* arg2,
1118 size_t result_size) {
1119 Node* target = HeapConstant(callable.code());
1120 return TailCallStub(callable.descriptor(), target, context, arg1, arg2,
1121 result_size);
1122}
1123
1124Node* CodeStubAssembler::TailCallStub(const CallInterfaceDescriptor& descriptor,
1125 Node* target, Node* context, Node* arg1,
1126 Node* arg2, size_t result_size) {
1127 CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor(
1128 isolate(), zone(), descriptor, descriptor.GetStackParameterCount(),
1129 CallDescriptor::kSupportsTailCalls, Operator::kNoProperties,
1130 MachineType::AnyTagged(), result_size);
1131
1132 Node** args = zone()->NewArray<Node*>(3);
1133 args[0] = arg1;
1134 args[1] = arg2;
1135 args[2] = context;
1136
1137 return raw_assembler_->TailCallN(call_descriptor, target, args);
Ben Murdoch109988c2016-05-18 11:27:45 +01001138}
1139
1140Node* CodeStubAssembler::TailCall(
1141 const CallInterfaceDescriptor& interface_descriptor, Node* code_target,
1142 Node** args, size_t result_size) {
1143 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
1144 isolate(), zone(), interface_descriptor,
1145 interface_descriptor.GetStackParameterCount(),
1146 CallDescriptor::kSupportsTailCalls, Operator::kNoProperties,
1147 MachineType::AnyTagged(), result_size);
1148 return raw_assembler_->TailCallN(descriptor, code_target, args);
1149}
1150
1151void CodeStubAssembler::Goto(CodeStubAssembler::Label* label) {
1152 label->MergeVariables();
1153 raw_assembler_->Goto(label->label_);
1154}
1155
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001156void CodeStubAssembler::GotoIf(Node* condition, Label* true_label) {
1157 Label false_label(this);
1158 Branch(condition, true_label, &false_label);
1159 Bind(&false_label);
1160}
1161
1162void CodeStubAssembler::GotoUnless(Node* condition, Label* false_label) {
1163 Label true_label(this);
1164 Branch(condition, &true_label, false_label);
1165 Bind(&true_label);
1166}
1167
Ben Murdoch109988c2016-05-18 11:27:45 +01001168void CodeStubAssembler::Branch(Node* condition,
1169 CodeStubAssembler::Label* true_label,
1170 CodeStubAssembler::Label* false_label) {
1171 true_label->MergeVariables();
1172 false_label->MergeVariables();
1173 return raw_assembler_->Branch(condition, true_label->label_,
1174 false_label->label_);
1175}
1176
1177void CodeStubAssembler::Switch(Node* index, Label* default_label,
1178 int32_t* case_values, Label** case_labels,
1179 size_t case_count) {
1180 RawMachineLabel** labels =
1181 new (zone()->New(sizeof(RawMachineLabel*) * case_count))
1182 RawMachineLabel*[case_count];
1183 for (size_t i = 0; i < case_count; ++i) {
1184 labels[i] = case_labels[i]->label_;
1185 case_labels[i]->MergeVariables();
1186 default_label->MergeVariables();
1187 }
1188 return raw_assembler_->Switch(index, default_label->label_, case_values,
1189 labels, case_count);
1190}
Ben Murdoch014dc512016-03-22 12:00:34 +00001191
1192// RawMachineAssembler delegate helpers:
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001193Isolate* CodeStubAssembler::isolate() const {
1194 return raw_assembler_->isolate();
1195}
Ben Murdoch014dc512016-03-22 12:00:34 +00001196
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001197Factory* CodeStubAssembler::factory() const { return isolate()->factory(); }
Ben Murdoch014dc512016-03-22 12:00:34 +00001198
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001199Graph* CodeStubAssembler::graph() const { return raw_assembler_->graph(); }
1200
1201Zone* CodeStubAssembler::zone() const { return raw_assembler_->zone(); }
Ben Murdoch014dc512016-03-22 12:00:34 +00001202
Ben Murdoch109988c2016-05-18 11:27:45 +01001203// The core implementation of Variable is stored through an indirection so
1204// that it can outlive the often block-scoped Variable declarations. This is
1205// needed to ensure that variable binding and merging through phis can
1206// properly be verified.
1207class CodeStubAssembler::Variable::Impl : public ZoneObject {
1208 public:
1209 explicit Impl(MachineRepresentation rep) : value_(nullptr), rep_(rep) {}
1210 Node* value_;
1211 MachineRepresentation rep_;
1212};
1213
1214CodeStubAssembler::Variable::Variable(CodeStubAssembler* assembler,
1215 MachineRepresentation rep)
1216 : impl_(new (assembler->zone()) Impl(rep)) {
1217 assembler->variables_.push_back(impl_);
1218}
1219
1220void CodeStubAssembler::Variable::Bind(Node* value) { impl_->value_ = value; }
1221
1222Node* CodeStubAssembler::Variable::value() const {
1223 DCHECK_NOT_NULL(impl_->value_);
1224 return impl_->value_;
1225}
1226
1227MachineRepresentation CodeStubAssembler::Variable::rep() const {
1228 return impl_->rep_;
1229}
1230
1231bool CodeStubAssembler::Variable::IsBound() const {
1232 return impl_->value_ != nullptr;
1233}
1234
Ben Murdoch109988c2016-05-18 11:27:45 +01001235CodeStubAssembler::Label::Label(CodeStubAssembler* assembler,
1236 int merged_value_count,
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001237 CodeStubAssembler::Variable** merged_variables,
1238 CodeStubAssembler::Label::Type type)
Ben Murdoch109988c2016-05-18 11:27:45 +01001239 : bound_(false), merge_count_(0), assembler_(assembler), label_(nullptr) {
1240 void* buffer = assembler->zone()->New(sizeof(RawMachineLabel));
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001241 label_ = new (buffer)
1242 RawMachineLabel(type == kDeferred ? RawMachineLabel::kDeferred
1243 : RawMachineLabel::kNonDeferred);
Ben Murdoch109988c2016-05-18 11:27:45 +01001244 for (int i = 0; i < merged_value_count; ++i) {
1245 variable_phis_[merged_variables[i]->impl_] = nullptr;
1246 }
1247}
1248
Ben Murdoch109988c2016-05-18 11:27:45 +01001249void CodeStubAssembler::Label::MergeVariables() {
1250 ++merge_count_;
1251 for (auto var : assembler_->variables_) {
1252 size_t count = 0;
1253 Node* node = var->value_;
1254 if (node != nullptr) {
1255 auto i = variable_merges_.find(var);
1256 if (i != variable_merges_.end()) {
1257 i->second.push_back(node);
1258 count = i->second.size();
1259 } else {
1260 count = 1;
1261 variable_merges_[var] = std::vector<Node*>(1, node);
1262 }
1263 }
1264 // If the following asserts, then you've jumped to a label without a bound
1265 // variable along that path that expects to merge its value into a phi.
1266 DCHECK(variable_phis_.find(var) == variable_phis_.end() ||
1267 count == merge_count_);
1268 USE(count);
1269
1270 // If the label is already bound, we already know the set of variables to
1271 // merge and phi nodes have already been created.
1272 if (bound_) {
1273 auto phi = variable_phis_.find(var);
1274 if (phi != variable_phis_.end()) {
1275 DCHECK_NOT_NULL(phi->second);
1276 assembler_->raw_assembler_->AppendPhiInput(phi->second, node);
1277 } else {
1278 auto i = variable_merges_.find(var);
Ben Murdoch3b9bc312016-06-02 14:46:10 +01001279 if (i != variable_merges_.end()) {
1280 // If the following assert fires, then you've declared a variable that
1281 // has the same bound value along all paths up until the point you
1282 // bound this label, but then later merged a path with a new value for
1283 // the variable after the label bind (it's not possible to add phis to
1284 // the bound label after the fact, just make sure to list the variable
1285 // in the label's constructor's list of merged variables).
1286 DCHECK(find_if(i->second.begin(), i->second.end(),
1287 [node](Node* e) -> bool { return node != e; }) ==
1288 i->second.end());
1289 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001290 }
1291 }
1292 }
1293}
1294
1295void CodeStubAssembler::Label::Bind() {
1296 DCHECK(!bound_);
1297 assembler_->raw_assembler_->Bind(label_);
1298
1299 // Make sure that all variables that have changed along any path up to this
1300 // point are marked as merge variables.
1301 for (auto var : assembler_->variables_) {
1302 Node* shared_value = nullptr;
1303 auto i = variable_merges_.find(var);
1304 if (i != variable_merges_.end()) {
1305 for (auto value : i->second) {
1306 DCHECK(value != nullptr);
1307 if (value != shared_value) {
1308 if (shared_value == nullptr) {
1309 shared_value = value;
1310 } else {
1311 variable_phis_[var] = nullptr;
1312 }
1313 }
1314 }
1315 }
1316 }
1317
1318 for (auto var : variable_phis_) {
1319 CodeStubAssembler::Variable::Impl* var_impl = var.first;
1320 auto i = variable_merges_.find(var_impl);
1321 // If the following assert fires, then a variable that has been marked as
1322 // being merged at the label--either by explicitly marking it so in the
1323 // label constructor or by having seen different bound values at branches
1324 // into the label--doesn't have a bound value along all of the paths that
1325 // have been merged into the label up to this point.
1326 DCHECK(i != variable_merges_.end() && i->second.size() == merge_count_);
1327 Node* phi = assembler_->raw_assembler_->Phi(
1328 var.first->rep_, static_cast<int>(merge_count_), &(i->second[0]));
1329 variable_phis_[var_impl] = phi;
1330 }
1331
1332 // Bind all variables to a merge phi, the common value along all paths or
1333 // null.
1334 for (auto var : assembler_->variables_) {
1335 auto i = variable_phis_.find(var);
1336 if (i != variable_phis_.end()) {
1337 var->value_ = i->second;
1338 } else {
1339 auto j = variable_merges_.find(var);
1340 if (j != variable_merges_.end() && j->second.size() == merge_count_) {
1341 var->value_ = j->second.back();
1342 } else {
1343 var->value_ = nullptr;
1344 }
1345 }
1346 }
1347
1348 bound_ = true;
1349}
Ben Murdoch014dc512016-03-22 12:00:34 +00001350
1351} // namespace compiler
1352} // namespace internal
1353} // namespace v8