builtins-arguments-gen.cc 14.4 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include "src/builtins/builtins-arguments-gen.h"

7
#include "src/arguments.h"
8
#include "src/builtins/builtins-utils-gen.h"
9 10 11
#include "src/builtins/builtins.h"
#include "src/code-factory.h"
#include "src/code-stub-assembler.h"
12
#include "src/frame-constants.h"
13
#include "src/interface-descriptors.h"
14
#include "src/objects-inl.h"
15
#include "src/objects/arguments.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

namespace v8 {
namespace internal {

typedef compiler::Node Node;

std::tuple<Node*, Node*, Node*>
ArgumentsBuiltinsAssembler::AllocateArgumentsObject(Node* map,
                                                    Node* arguments_count,
                                                    Node* parameter_map_count,
                                                    ParameterMode mode,
                                                    int base_size) {
  // Allocate the parameter object (either a Rest parameter object, a strict
  // argument object or a sloppy arguments object) and the elements/mapped
  // arguments together.
  int elements_offset = base_size;
  Node* element_count = arguments_count;
  if (parameter_map_count != nullptr) {
    base_size += FixedArray::kHeaderSize;
    element_count = IntPtrOrSmiAdd(element_count, parameter_map_count, mode);
  }
37
  bool empty = IsIntPtrOrSmiConstantZero(arguments_count, mode);
38
  DCHECK_IMPLIES(empty, parameter_map_count == nullptr);
39
  TNode<IntPtrT> size =
40
      empty ? IntPtrConstant(base_size)
41
            : ElementOffsetFromIndex(element_count, PACKED_ELEMENTS, mode,
42
                                     base_size + FixedArray::kHeaderSize);
43
  TNode<Object> result = Allocate(size);
44 45
  Comment("Initialize arguments object");
  StoreMapNoWriteBarrier(result, map);
46
  Node* empty_fixed_array = LoadRoot(RootIndex::kEmptyFixedArray);
47
  StoreObjectField(result, JSArray::kPropertiesOrHashOffset, empty_fixed_array);
48 49 50 51 52
  Node* smi_arguments_count = ParameterToTagged(arguments_count, mode);
  StoreObjectFieldNoWriteBarrier(result, JSArray::kLengthOffset,
                                 smi_arguments_count);
  Node* arguments = nullptr;
  if (!empty) {
53
    arguments = InnerAllocate(CAST(result), elements_offset);
54 55
    StoreObjectFieldNoWriteBarrier(arguments, FixedArray::kLengthOffset,
                                   smi_arguments_count);
56
    Node* fixed_array_map = LoadRoot(RootIndex::kFixedArrayMap);
57 58 59 60
    StoreMapNoWriteBarrier(arguments, fixed_array_map);
  }
  Node* parameter_map = nullptr;
  if (parameter_map_count != nullptr) {
61
    TNode<IntPtrT> parameter_map_offset = ElementOffsetFromIndex(
62
        arguments_count, PACKED_ELEMENTS, mode, FixedArray::kHeaderSize);
63
    parameter_map = InnerAllocate(CAST(arguments), parameter_map_offset);
64 65 66
    StoreObjectFieldNoWriteBarrier(result, JSArray::kElementsOffset,
                                   parameter_map);
    Node* sloppy_elements_map =
67
        LoadRoot(RootIndex::kSloppyArgumentsElementsMap);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    StoreMapNoWriteBarrier(parameter_map, sloppy_elements_map);
    parameter_map_count = ParameterToTagged(parameter_map_count, mode);
    StoreObjectFieldNoWriteBarrier(parameter_map, FixedArray::kLengthOffset,
                                   parameter_map_count);
  } else {
    if (empty) {
      StoreObjectFieldNoWriteBarrier(result, JSArray::kElementsOffset,
                                     empty_fixed_array);
    } else {
      StoreObjectFieldNoWriteBarrier(result, JSArray::kElementsOffset,
                                     arguments);
    }
  }
  return std::tuple<Node*, Node*, Node*>(result, arguments, parameter_map);
}

Node* ArgumentsBuiltinsAssembler::ConstructParametersObjectFromArgs(
    Node* map, Node* frame_ptr, Node* arg_count, Node* first_arg,
    Node* rest_count, ParameterMode param_mode, int base_size) {
  // Allocate the parameter object (either a Rest parameter object, a strict
  // argument object or a sloppy arguments object) and the elements together and
  // fill in the contents with the arguments above |formal_parameter_count|.
  Node* result;
  Node* elements;
  Node* unused;
  std::tie(result, elements, unused) =
      AllocateArgumentsObject(map, rest_count, nullptr, param_mode, base_size);
95
  DCHECK_NULL(unused);
96
  CodeStubArguments arguments(this, arg_count, frame_ptr, param_mode);
97
  VARIABLE(offset, MachineType::PointerRepresentation());
98 99 100 101 102 103
  offset.Bind(IntPtrConstant(FixedArrayBase::kHeaderSize - kHeapObjectTag));
  VariableList list({&offset}, zone());
  arguments.ForEach(list,
                    [this, elements, &offset](Node* arg) {
                      StoreNoWriteBarrier(MachineRepresentation::kTagged,
                                          elements, offset.value(), arg);
104
                      Increment(&offset, kSystemPointerSize);
105 106 107 108 109 110 111 112 113 114
                    },
                    first_arg, nullptr, param_mode);
  return result;
}

Node* ArgumentsBuiltinsAssembler::EmitFastNewRestParameter(Node* context,
                                                           Node* function) {
  ParameterMode mode = OptimalParameterMode();
  Node* zero = IntPtrOrSmiConstant(0, mode);

115 116 117
  ArgumentsBuiltinsFromDSLAssembler::ArgumentsInfo info =
      GetArgumentsFrameAndCount(CAST(context),
                                UncheckedCast<JSFunction>(function));
118

119
  VARIABLE(result, MachineRepresentation::kTagged);
120 121 122 123
  Label no_rest_parameters(this), runtime(this, Label::kDeferred),
      done(this, &result);

  Node* rest_count =
124
      IntPtrOrSmiSub(info.argument_count, info.formal_parameter_count, mode);
125
  Node* const native_context = LoadNativeContext(context);
126 127
  Node* const array_map =
      LoadJSArrayElementsMap(PACKED_ELEMENTS, native_context);
128 129 130 131 132 133 134 135 136
  GotoIf(IntPtrOrSmiLessThanOrEqual(rest_count, zero, mode),
         &no_rest_parameters);

  GotoIfFixedArraySizeDoesntFitInNewSpace(
      rest_count, &runtime, JSArray::kSize + FixedArray::kHeaderSize, mode);

  // Allocate the Rest JSArray and the elements together and fill in the
  // contents with the arguments above |formal_parameter_count|.
  result.Bind(ConstructParametersObjectFromArgs(
137 138
      array_map, info.frame, info.argument_count, info.formal_parameter_count,
      rest_count, mode, JSArray::kSize));
139 140
  Goto(&done);

141
  BIND(&no_rest_parameters);
142 143 144 145 146 147 148 149 150 151
  {
    Node* arguments;
    Node* elements;
    Node* unused;
    std::tie(arguments, elements, unused) =
        AllocateArgumentsObject(array_map, zero, nullptr, mode, JSArray::kSize);
    result.Bind(arguments);
    Goto(&done);
  }

152
  BIND(&runtime);
153 154 155 156 157
  {
    result.Bind(CallRuntime(Runtime::kNewRestParameter, context, function));
    Goto(&done);
  }

158
  BIND(&done);
159 160 161 162 163
  return result.value();
}

Node* ArgumentsBuiltinsAssembler::EmitFastNewStrictArguments(Node* context,
                                                             Node* function) {
164
  VARIABLE(result, MachineRepresentation::kTagged);
165 166 167 168 169
  Label done(this, &result), empty(this), runtime(this, Label::kDeferred);

  ParameterMode mode = OptimalParameterMode();
  Node* zero = IntPtrOrSmiConstant(0, mode);

170 171 172
  ArgumentsBuiltinsFromDSLAssembler::ArgumentsInfo info =
      GetArgumentsFrameAndCount(CAST(context),
                                UncheckedCast<JSFunction>(function));
173 174

  GotoIfFixedArraySizeDoesntFitInNewSpace(
175
      info.argument_count, &runtime,
176 177 178 179 180
      JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize, mode);

  Node* const native_context = LoadNativeContext(context);
  Node* const map =
      LoadContextElement(native_context, Context::STRICT_ARGUMENTS_MAP_INDEX);
181
  GotoIf(WordEqual(info.argument_count, zero), &empty);
182 183

  result.Bind(ConstructParametersObjectFromArgs(
184
      map, info.frame, info.argument_count, zero, info.argument_count, mode,
185 186 187
      JSStrictArgumentsObject::kSize));
  Goto(&done);

188
  BIND(&empty);
189 190 191 192 193 194 195 196 197 198
  {
    Node* arguments;
    Node* elements;
    Node* unused;
    std::tie(arguments, elements, unused) = AllocateArgumentsObject(
        map, zero, nullptr, mode, JSStrictArgumentsObject::kSize);
    result.Bind(arguments);
    Goto(&done);
  }

199
  BIND(&runtime);
200 201 202 203 204
  {
    result.Bind(CallRuntime(Runtime::kNewStrictArguments, context, function));
    Goto(&done);
  }

205
  BIND(&done);
206 207 208 209 210
  return result.value();
}

Node* ArgumentsBuiltinsAssembler::EmitFastNewSloppyArguments(Node* context,
                                                             Node* function) {
211
  VARIABLE(result, MachineRepresentation::kTagged);
212 213 214 215 216 217 218

  ParameterMode mode = OptimalParameterMode();
  Node* zero = IntPtrOrSmiConstant(0, mode);

  Label done(this, &result), empty(this), no_parameters(this),
      runtime(this, Label::kDeferred);

219 220 221
  ArgumentsBuiltinsFromDSLAssembler::ArgumentsInfo info =
      GetArgumentsFrameAndCount(CAST(context),
                                UncheckedCast<JSFunction>(function));
222

223
  GotoIf(WordEqual(info.argument_count, zero), &empty);
224

225
  GotoIf(WordEqual(info.formal_parameter_count, zero), &no_parameters);
226 227 228 229 230

  {
    Comment("Mapped parameter JSSloppyArgumentsObject");

    Node* mapped_count =
231
        IntPtrOrSmiMin(info.argument_count, info.formal_parameter_count, mode);
232 233 234 235 236 237

    Node* parameter_map_size =
        IntPtrOrSmiAdd(mapped_count, IntPtrOrSmiConstant(2, mode), mode);

    // Verify that the overall allocation will fit in new space.
    Node* elements_allocated =
238
        IntPtrOrSmiAdd(info.argument_count, parameter_map_size, mode);
239 240 241 242 243 244 245 246 247 248 249
    GotoIfFixedArraySizeDoesntFitInNewSpace(
        elements_allocated, &runtime,
        JSSloppyArgumentsObject::kSize + FixedArray::kHeaderSize * 2, mode);

    Node* const native_context = LoadNativeContext(context);
    Node* const map = LoadContextElement(
        native_context, Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX);
    Node* argument_object;
    Node* elements;
    Node* map_array;
    std::tie(argument_object, elements, map_array) =
250 251
        AllocateArgumentsObject(map, info.argument_count, parameter_map_size,
                                mode, JSSloppyArgumentsObject::kSize);
252 253
    StoreObjectFieldNoWriteBarrier(
        argument_object, JSSloppyArgumentsObject::kCalleeOffset, function);
254 255
    StoreFixedArrayElement(CAST(map_array), 0, context, SKIP_WRITE_BARRIER);
    StoreFixedArrayElement(CAST(map_array), 1, elements, SKIP_WRITE_BARRIER);
256 257 258

    Comment("Fill in non-mapped parameters");
    Node* argument_offset =
259
        ElementOffsetFromIndex(info.argument_count, PACKED_ELEMENTS, mode,
260 261
                               FixedArray::kHeaderSize - kHeapObjectTag);
    Node* mapped_offset =
262
        ElementOffsetFromIndex(mapped_count, PACKED_ELEMENTS, mode,
263
                               FixedArray::kHeaderSize - kHeapObjectTag);
264
    CodeStubArguments arguments(this, info.argument_count, info.frame, mode);
265
    VARIABLE(current_argument, MachineType::PointerRepresentation());
266
    current_argument.Bind(arguments.AtIndexPtr(info.argument_count, mode));
267 268 269 270
    VariableList var_list1({&current_argument}, zone());
    mapped_offset = BuildFastLoop(
        var_list1, argument_offset, mapped_offset,
        [this, elements, &current_argument](Node* offset) {
271
          Increment(&current_argument, kSystemPointerSize);
272 273 274 275
          Node* arg = LoadBufferObject(current_argument.value(), 0);
          StoreNoWriteBarrier(MachineRepresentation::kTagged, elements, offset,
                              arg);
        },
276
        -kTaggedSize, INTPTR_PARAMETERS);
277 278 279 280 281 282 283 284 285 286

    // Copy the parameter slots and the holes in the arguments.
    // We need to fill in mapped_count slots. They index the context,
    // where parameters are stored in reverse order, at
    //   MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+argument_count-1
    // The mapped parameter thus need to get indices
    //   MIN_CONTEXT_SLOTS+parameter_count-1 ..
    //       MIN_CONTEXT_SLOTS+argument_count-mapped_count
    // We loop from right to left.
    Comment("Fill in mapped parameters");
287
    VARIABLE(context_index, OptimalParameterRepresentation());
288 289
    context_index.Bind(IntPtrOrSmiSub(
        IntPtrOrSmiAdd(IntPtrOrSmiConstant(Context::MIN_CONTEXT_SLOTS, mode),
290
                       info.formal_parameter_count, mode),
291 292 293
        mapped_count, mode));
    Node* the_hole = TheHoleConstant();
    VariableList var_list2({&context_index}, zone());
294
    const int kParameterMapHeaderSize = FixedArray::OffsetOfElementAt(2);
295 296 297 298
    Node* adjusted_map_array = IntPtrAdd(
        BitcastTaggedToWord(map_array),
        IntPtrConstant(kParameterMapHeaderSize - FixedArray::kHeaderSize));
    Node* zero_offset = ElementOffsetFromIndex(
299
        zero, PACKED_ELEMENTS, mode, FixedArray::kHeaderSize - kHeapObjectTag);
300 301 302 303 304 305 306 307 308 309 310
    BuildFastLoop(
        var_list2, mapped_offset, zero_offset,
        [=, &context_index](Node* offset) {
          StoreNoWriteBarrier(MachineRepresentation::kTagged, elements, offset,
                              the_hole);
          StoreNoWriteBarrier(MachineRepresentation::kTagged,
                              adjusted_map_array, offset,
                              ParameterToTagged(context_index.value(), mode));
          Increment(&context_index, 1, mode);
        },
        -kTaggedSize, INTPTR_PARAMETERS);
311 312 313 314 315

    result.Bind(argument_object);
    Goto(&done);
  }

316
  BIND(&no_parameters);
317 318 319
  {
    Comment("No parameters JSSloppyArgumentsObject");
    GotoIfFixedArraySizeDoesntFitInNewSpace(
320
        info.argument_count, &runtime,
321 322 323 324 325
        JSSloppyArgumentsObject::kSize + FixedArray::kHeaderSize, mode);
    Node* const native_context = LoadNativeContext(context);
    Node* const map =
        LoadContextElement(native_context, Context::SLOPPY_ARGUMENTS_MAP_INDEX);
    result.Bind(ConstructParametersObjectFromArgs(
326
        map, info.frame, info.argument_count, zero, info.argument_count, mode,
327 328 329 330 331 332
        JSSloppyArgumentsObject::kSize));
    StoreObjectFieldNoWriteBarrier(
        result.value(), JSSloppyArgumentsObject::kCalleeOffset, function);
    Goto(&done);
  }

333
  BIND(&empty);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  {
    Comment("Empty JSSloppyArgumentsObject");
    Node* const native_context = LoadNativeContext(context);
    Node* const map =
        LoadContextElement(native_context, Context::SLOPPY_ARGUMENTS_MAP_INDEX);
    Node* arguments;
    Node* elements;
    Node* unused;
    std::tie(arguments, elements, unused) = AllocateArgumentsObject(
        map, zero, nullptr, mode, JSSloppyArgumentsObject::kSize);
    result.Bind(arguments);
    StoreObjectFieldNoWriteBarrier(
        result.value(), JSSloppyArgumentsObject::kCalleeOffset, function);
    Goto(&done);
  }

350
  BIND(&runtime);
351 352 353 354 355
  {
    result.Bind(CallRuntime(Runtime::kNewSloppyArguments, context, function));
    Goto(&done);
  }

356
  BIND(&done);
357 358 359 360 361
  return result.value();
}

}  // namespace internal
}  // namespace v8