Home | History | Annotate | Download | only in builtins
      1 // Copyright 2017 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 #ifndef V8_BUILTINS_BUILTINS_ARRAY_GEN_H_
      6 #define V8_BUILTINS_BUILTINS_ARRAY_GEN_H_
      7 
      8 #include "torque-generated/builtins-base-from-dsl-gen.h"
      9 
     10 namespace v8 {
     11 namespace internal {
     12 
     13 class ArrayBuiltinsAssembler : public BaseBuiltinsFromDSLAssembler {
     14  public:
     15   explicit ArrayBuiltinsAssembler(compiler::CodeAssemblerState* state);
     16 
     17   typedef std::function<void(ArrayBuiltinsAssembler* masm)>
     18       BuiltinResultGenerator;
     19 
     20   typedef std::function<Node*(ArrayBuiltinsAssembler* masm, Node* k_value,
     21                               Node* k)>
     22       CallResultProcessor;
     23 
     24   typedef std::function<void(ArrayBuiltinsAssembler* masm)> PostLoopAction;
     25 
     26   enum class MissingPropertyMode { kSkip, kUseUndefined };
     27 
     28   void FindResultGenerator();
     29 
     30   Node* FindProcessor(Node* k_value, Node* k);
     31 
     32   void FindIndexResultGenerator();
     33 
     34   Node* FindIndexProcessor(Node* k_value, Node* k);
     35 
     36   void ForEachResultGenerator();
     37 
     38   Node* ForEachProcessor(Node* k_value, Node* k);
     39 
     40   void SomeResultGenerator();
     41 
     42   Node* SomeProcessor(Node* k_value, Node* k);
     43 
     44   void EveryResultGenerator();
     45 
     46   Node* EveryProcessor(Node* k_value, Node* k);
     47 
     48   void ReduceResultGenerator();
     49 
     50   Node* ReduceProcessor(Node* k_value, Node* k);
     51 
     52   void ReducePostLoopAction();
     53 
     54   void FilterResultGenerator();
     55 
     56   Node* FilterProcessor(Node* k_value, Node* k);
     57 
     58   void MapResultGenerator();
     59 
     60   void TypedArrayMapResultGenerator();
     61 
     62   Node* SpecCompliantMapProcessor(Node* k_value, Node* k);
     63 
     64   Node* FastMapProcessor(Node* k_value, Node* k);
     65 
     66   // See tc39.github.io/ecma262/#sec-%typedarray%.prototype.map.
     67   Node* TypedArrayMapProcessor(Node* k_value, Node* k);
     68 
     69   void NullPostLoopAction();
     70 
     71   // Uses memset to effectively initialize the given FixedArray with Smi zeroes.
     72   void FillFixedArrayWithSmiZero(TNode<FixedArray> array,
     73                                  TNode<Smi> smi_length);
     74 
     75  protected:
     76   TNode<Context> context() { return context_; }
     77   TNode<Object> receiver() { return receiver_; }
     78   TNode<IntPtrT> argc() { return argc_; }
     79   TNode<JSReceiver> o() { return o_; }
     80   TNode<Number> len() { return len_; }
     81   Node* callbackfn() { return callbackfn_; }
     82   Node* this_arg() { return this_arg_; }
     83   TNode<Number> k() { return CAST(k_.value()); }
     84   Node* a() { return a_.value(); }
     85 
     86   void ReturnFromBuiltin(Node* value);
     87 
     88   void InitIteratingArrayBuiltinBody(TNode<Context> context,
     89                                      TNode<Object> receiver, Node* callbackfn,
     90                                      Node* this_arg, TNode<IntPtrT> argc);
     91 
     92   void GenerateIteratingArrayBuiltinBody(
     93       const char* name, const BuiltinResultGenerator& generator,
     94       const CallResultProcessor& processor, const PostLoopAction& action,
     95       const Callable& slow_case_continuation,
     96       MissingPropertyMode missing_property_mode,
     97       ForEachDirection direction = ForEachDirection::kForward);
     98   void InitIteratingArrayBuiltinLoopContinuation(
     99       TNode<Context> context, TNode<Object> receiver, Node* callbackfn,
    100       Node* this_arg, Node* a, TNode<JSReceiver> o, Node* initial_k,
    101       TNode<Number> len, Node* to);
    102 
    103   void GenerateIteratingTypedArrayBuiltinBody(
    104       const char* name, const BuiltinResultGenerator& generator,
    105       const CallResultProcessor& processor, const PostLoopAction& action,
    106       ForEachDirection direction = ForEachDirection::kForward);
    107 
    108   void GenerateIteratingArrayBuiltinLoopContinuation(
    109       const CallResultProcessor& processor, const PostLoopAction& action,
    110       MissingPropertyMode missing_property_mode,
    111       ForEachDirection direction = ForEachDirection::kForward);
    112 
    113   void TailCallArrayConstructorStub(
    114       const Callable& callable, TNode<Context> context,
    115       TNode<JSFunction> target, TNode<HeapObject> allocation_site_or_undefined,
    116       TNode<Int32T> argc);
    117 
    118   void GenerateDispatchToArrayStub(
    119       TNode<Context> context, TNode<JSFunction> target, TNode<Int32T> argc,
    120       AllocationSiteOverrideMode mode,
    121       TNode<AllocationSite> allocation_site = TNode<AllocationSite>());
    122 
    123   void CreateArrayDispatchNoArgument(
    124       TNode<Context> context, TNode<JSFunction> target, TNode<Int32T> argc,
    125       AllocationSiteOverrideMode mode,
    126       TNode<AllocationSite> allocation_site = TNode<AllocationSite>());
    127 
    128   void CreateArrayDispatchSingleArgument(
    129       TNode<Context> context, TNode<JSFunction> target, TNode<Int32T> argc,
    130       AllocationSiteOverrideMode mode,
    131       TNode<AllocationSite> allocation_site = TNode<AllocationSite>());
    132 
    133   void GenerateConstructor(Node* context, Node* array_function, Node* array_map,
    134                            Node* array_size, Node* allocation_site,
    135                            ElementsKind elements_kind, AllocationSiteMode mode);
    136   void GenerateArrayNoArgumentConstructor(ElementsKind kind,
    137                                           AllocationSiteOverrideMode mode);
    138   void GenerateArraySingleArgumentConstructor(ElementsKind kind,
    139                                               AllocationSiteOverrideMode mode);
    140   void GenerateArrayNArgumentsConstructor(
    141       TNode<Context> context, TNode<JSFunction> target,
    142       TNode<Object> new_target, TNode<Int32T> argc,
    143       TNode<HeapObject> maybe_allocation_site);
    144 
    145   void GenerateInternalArrayNoArgumentConstructor(ElementsKind kind);
    146   void GenerateInternalArraySingleArgumentConstructor(ElementsKind kind);
    147 
    148  private:
    149   static ElementsKind ElementsKindForInstanceType(InstanceType type);
    150 
    151   void VisitAllTypedArrayElements(Node* array_buffer,
    152                                   const CallResultProcessor& processor,
    153                                   Label* detached, ForEachDirection direction,
    154                                   TNode<JSTypedArray> typed_array);
    155 
    156   void VisitAllFastElementsOneKind(ElementsKind kind,
    157                                    const CallResultProcessor& processor,
    158                                    Label* array_changed, ParameterMode mode,
    159                                    ForEachDirection direction,
    160                                    MissingPropertyMode missing_property_mode,
    161                                    TNode<Smi> length);
    162 
    163   void HandleFastElements(const CallResultProcessor& processor,
    164                           const PostLoopAction& action, Label* slow,
    165                           ForEachDirection direction,
    166                           MissingPropertyMode missing_property_mode);
    167 
    168   // Perform ArraySpeciesCreate (ES6 #sec-arrayspeciescreate).
    169   // This version is specialized to create a zero length array
    170   // of the elements kind of the input array.
    171   void GenerateArraySpeciesCreate();
    172 
    173   // Perform ArraySpeciesCreate (ES6 #sec-arrayspeciescreate).
    174   void GenerateArraySpeciesCreate(TNode<Number> len);
    175 
    176   Node* callbackfn_ = nullptr;
    177   TNode<JSReceiver> o_;
    178   Node* this_arg_ = nullptr;
    179   TNode<Number> len_;
    180   TNode<Context> context_;
    181   TNode<Object> receiver_;
    182   TNode<IntPtrT> argc_;
    183   Node* fast_typed_array_target_ = nullptr;
    184   const char* name_ = nullptr;
    185   Variable k_;
    186   Variable a_;
    187   Variable to_;
    188   Label fully_spec_compliant_;
    189   ElementsKind source_elements_kind_ = ElementsKind::NO_ELEMENTS;
    190 };
    191 
    192 }  // namespace internal
    193 }  // namespace v8
    194 
    195 #endif  // V8_BUILTINS_BUILTINS_ARRAY_GEN_H_
    196