Commit 2aaacddd authored by tebbi's avatar tebbi Committed by Commit bot

[builtins] Implement %TypedArray%.prototype.{reduce,reduceRight} in the CSA

R=danno@chromium.org

Review-Url: https://codereview.chromium.org/2796343002
Cr-Commit-Position: refs/heads/master@{#44558}
parent 5fda8c38
...@@ -3841,6 +3841,10 @@ void Genesis::InitializeGlobal_experimental_fast_array_builtins() { ...@@ -3841,6 +3841,10 @@ void Genesis::InitializeGlobal_experimental_fast_array_builtins() {
Builtins::kTypedArrayPrototypeEvery); Builtins::kTypedArrayPrototypeEvery);
InstallOneBuiltinFunction(typed_array_prototype, "some", InstallOneBuiltinFunction(typed_array_prototype, "some",
Builtins::kTypedArrayPrototypeSome); Builtins::kTypedArrayPrototypeSome);
InstallOneBuiltinFunction(typed_array_prototype, "reduce",
Builtins::kTypedArrayPrototypeReduce);
InstallOneBuiltinFunction(typed_array_prototype, "reduceRight",
Builtins::kTypedArrayPrototypeReduceRight);
} }
} }
......
...@@ -13,8 +13,8 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -13,8 +13,8 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
public: public:
explicit ArrayBuiltinCodeStubAssembler(compiler::CodeAssemblerState* state) explicit ArrayBuiltinCodeStubAssembler(compiler::CodeAssemblerState* state)
: CodeStubAssembler(state), : CodeStubAssembler(state),
k_(this, MachineRepresentation::kTagged, SmiConstant(0)), k_(this, MachineRepresentation::kTagged),
a_(this, MachineRepresentation::kTagged, SmiConstant(0)), a_(this, MachineRepresentation::kTagged),
to_(this, MachineRepresentation::kTagged, SmiConstant(0)) {} to_(this, MachineRepresentation::kTagged, SmiConstant(0)) {}
typedef std::function<Node*(ArrayBuiltinCodeStubAssembler* masm)> typedef std::function<Node*(ArrayBuiltinCodeStubAssembler* masm)>
...@@ -186,9 +186,6 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -186,9 +186,6 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
new_target_ = new_target; new_target_ = new_target;
callbackfn_ = callbackfn; callbackfn_ = callbackfn;
this_arg_ = this_arg; this_arg_ = this_arg;
k_.Bind(SmiConstant(0));
a_.Bind(UndefinedConstant());
} }
void GenerateIteratingArrayBuiltinBody( void GenerateIteratingArrayBuiltinBody(
...@@ -261,8 +258,7 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -261,8 +258,7 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
// 7. Let k be 0. // 7. Let k be 0.
k_.Bind(SmiConstant(0)); k_.Bind(SmiConstant(0));
} else { } else {
k_.Bind(len()); k_.Bind(NumberDec(len()));
k_.Bind(NumberDec(k_.value()));
} }
a_.Bind(generator(this)); a_.Bind(generator(this));
...@@ -297,7 +293,8 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -297,7 +293,8 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
void GenerateIteratingTypedArrayBuiltinBody( void GenerateIteratingTypedArrayBuiltinBody(
const char* name, const BuiltinResultGenerator& generator, const char* name, const BuiltinResultGenerator& generator,
const CallResultProcessor& processor, const PostLoopAction& action) { const CallResultProcessor& processor, const PostLoopAction& action,
ForEachDirection direction = ForEachDirection::kForward) {
Node* name_string = Node* name_string =
HeapConstant(isolate()->factory()->NewStringFromAsciiChecked(name)); HeapConstant(isolate()->factory()->NewStringFromAsciiChecked(name));
...@@ -364,6 +361,12 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -364,6 +361,12 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
} }
BIND(&distinguish_types); BIND(&distinguish_types);
if (direction == ForEachDirection::kForward) {
k_.Bind(SmiConstant(0));
} else {
k_.Bind(NumberDec(len()));
}
a_.Bind(generator(this)); a_.Bind(generator(this));
Node* elements_type = LoadInstanceType(LoadElements(o_)); Node* elements_type = LoadInstanceType(LoadElements(o_));
Switch(elements_type, &unexpected_instance_type, instance_types.data(), Switch(elements_type, &unexpected_instance_type, instance_types.data(),
...@@ -377,11 +380,11 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -377,11 +380,11 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
VisitAllTypedArrayElements( VisitAllTypedArrayElements(
ElementsKindForInstanceType( ElementsKindForInstanceType(
static_cast<InstanceType>(instance_types[i])), static_cast<InstanceType>(instance_types[i])),
array_buffer, processor, &done); array_buffer, processor, &done, direction);
Goto(&done); Goto(&done);
action(this);
// No exception, return success // No exception, return success
BIND(&done); BIND(&done);
action(this);
Return(a_.value()); Return(a_.value());
} }
} }
...@@ -458,7 +461,7 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -458,7 +461,7 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
void VisitAllTypedArrayElements(ElementsKind kind, Node* array_buffer, void VisitAllTypedArrayElements(ElementsKind kind, Node* array_buffer,
const CallResultProcessor& processor, const CallResultProcessor& processor,
Label* detached) { Label* detached, ForEachDirection direction) {
VariableList list({&a_, &k_, &to_}, zone()); VariableList list({&a_, &k_, &to_}, zone());
FastLoopBody body = [&](Node* index) { FastLoopBody body = [&](Node* index) {
...@@ -475,8 +478,17 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler { ...@@ -475,8 +478,17 @@ class ArrayBuiltinCodeStubAssembler : public CodeStubAssembler {
k_.Bind(index); k_.Bind(index);
a_.Bind(processor(this, value, index)); a_.Bind(processor(this, value, index));
}; };
BuildFastLoop(list, SmiConstant(0), len_, body, 1, Node* start = SmiConstant(0);
ParameterMode::SMI_PARAMETERS, IndexAdvanceMode::kPost); Node* end = len_;
IndexAdvanceMode advance_mode = IndexAdvanceMode::kPost;
int incr = 1;
if (direction == ForEachDirection::kReverse) {
std::swap(start, end);
advance_mode = IndexAdvanceMode::kPre;
incr = -1;
}
BuildFastLoop(list, start, end, body, incr, ParameterMode::SMI_PARAMETERS,
advance_mode);
} }
void VisitAllFastElementsOneKind(ElementsKind kind, void VisitAllFastElementsOneKind(ElementsKind kind,
...@@ -948,6 +960,23 @@ TF_BUILTIN(ArrayReduce, ArrayBuiltinCodeStubAssembler) { ...@@ -948,6 +960,23 @@ TF_BUILTIN(ArrayReduce, ArrayBuiltinCodeStubAssembler) {
CodeFactory::ArrayReduceLoopContinuation(isolate())); CodeFactory::ArrayReduceLoopContinuation(isolate()));
} }
TF_BUILTIN(TypedArrayPrototypeReduce, ArrayBuiltinCodeStubAssembler) {
Node* context = Parameter(Descriptor::kContext);
Node* receiver = Parameter(Descriptor::kReceiver);
Node* callbackfn = Parameter(Descriptor::kCallbackFn);
Node* initial_value = Parameter(Descriptor::kInitialValue);
Node* new_target = Parameter(Descriptor::kNewTarget);
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, initial_value,
new_target);
GenerateIteratingTypedArrayBuiltinBody(
"%TypedArray%.prototype.reduce",
&ArrayBuiltinCodeStubAssembler::ReduceResultGenerator,
&ArrayBuiltinCodeStubAssembler::ReduceProcessor,
&ArrayBuiltinCodeStubAssembler::ReducePostLoopAction);
}
TF_BUILTIN(ArrayReduceRightLoopContinuation, ArrayBuiltinCodeStubAssembler) { TF_BUILTIN(ArrayReduceRightLoopContinuation, ArrayBuiltinCodeStubAssembler) {
Node* context = Parameter(Descriptor::kContext); Node* context = Parameter(Descriptor::kContext);
Node* receiver = Parameter(Descriptor::kReceiver); Node* receiver = Parameter(Descriptor::kReceiver);
...@@ -988,6 +1017,24 @@ TF_BUILTIN(ArrayReduceRight, ArrayBuiltinCodeStubAssembler) { ...@@ -988,6 +1017,24 @@ TF_BUILTIN(ArrayReduceRight, ArrayBuiltinCodeStubAssembler) {
ForEachDirection::kReverse); ForEachDirection::kReverse);
} }
TF_BUILTIN(TypedArrayPrototypeReduceRight, ArrayBuiltinCodeStubAssembler) {
Node* context = Parameter(Descriptor::kContext);
Node* receiver = Parameter(Descriptor::kReceiver);
Node* callbackfn = Parameter(Descriptor::kCallbackFn);
Node* initial_value = Parameter(Descriptor::kInitialValue);
Node* new_target = Parameter(Descriptor::kNewTarget);
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, initial_value,
new_target);
GenerateIteratingTypedArrayBuiltinBody(
"%TypedArray%.prototype.reduceRight",
&ArrayBuiltinCodeStubAssembler::ReduceResultGenerator,
&ArrayBuiltinCodeStubAssembler::ReduceProcessor,
&ArrayBuiltinCodeStubAssembler::ReducePostLoopAction,
ForEachDirection::kReverse);
}
TF_BUILTIN(ArrayFilterLoopContinuation, ArrayBuiltinCodeStubAssembler) { TF_BUILTIN(ArrayFilterLoopContinuation, ArrayBuiltinCodeStubAssembler) {
Node* context = Parameter(Descriptor::kContext); Node* context = Parameter(Descriptor::kContext);
Node* receiver = Parameter(Descriptor::kReceiver); Node* receiver = Parameter(Descriptor::kReceiver);
......
...@@ -906,6 +906,10 @@ namespace internal { ...@@ -906,6 +906,10 @@ namespace internal {
TFJ(TypedArrayPrototypeEvery, 2, kCallbackFn, kThisArg) \ TFJ(TypedArrayPrototypeEvery, 2, kCallbackFn, kThisArg) \
/* ES6 %TypedArray%.prototype.some */ \ /* ES6 %TypedArray%.prototype.some */ \
TFJ(TypedArrayPrototypeSome, 2, kCallbackFn, kThisArg) \ TFJ(TypedArrayPrototypeSome, 2, kCallbackFn, kThisArg) \
/* ES6 %TypedArray%.prototype.reduce */ \
TFJ(TypedArrayPrototypeReduce, 2, kCallbackFn, kInitialValue) \
/* ES6 %TypedArray%.prototype.reduceRight */ \
TFJ(TypedArrayPrototypeReduceRight, 2, kCallbackFn, kInitialValue) \
\ \
/* Wasm */ \ /* Wasm */ \
ASM(WasmCompileLazy) \ ASM(WasmCompileLazy) \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment