code-factory.cc 18.4 KB
Newer Older
1 2 3 4
// Copyright 2014 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
#include "src/code-factory.h"
6 7

#include "src/bootstrapper.h"
8
#include "src/builtins/builtins-descriptors.h"
9
#include "src/ic/ic.h"
10
#include "src/objects-inl.h"
11
#include "src/objects/allocation-site-inl.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17
// static
Handle<Code> CodeFactory::RuntimeCEntry(Isolate* isolate, int result_size) {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  return CodeFactory::CEntry(isolate, result_size);
}

#define CENTRY_CODE(RS, SD, AM, BE) \
  BUILTIN_CODE(isolate, CEntry_##RS##_##SD##_##AM##_##BE)

// static
Handle<Code> CodeFactory::CEntry(Isolate* isolate, int result_size,
                                 SaveFPRegsMode save_doubles,
                                 ArgvMode argv_mode, bool builtin_exit_frame) {
  // Aliases for readability below.
  const int rs = result_size;
  const SaveFPRegsMode sd = save_doubles;
  const ArgvMode am = argv_mode;
  const bool be = builtin_exit_frame;

  if (rs == 1 && sd == kDontSaveFPRegs && am == kArgvOnStack && !be) {
    return CENTRY_CODE(Return1, DontSaveFPRegs, ArgvOnStack, NoBuiltinExit);
  } else if (rs == 1 && sd == kDontSaveFPRegs && am == kArgvOnStack && be) {
    return CENTRY_CODE(Return1, DontSaveFPRegs, ArgvOnStack, BuiltinExit);
  } else if (rs == 1 && sd == kDontSaveFPRegs && am == kArgvInRegister && !be) {
    return CENTRY_CODE(Return1, DontSaveFPRegs, ArgvInRegister, NoBuiltinExit);
  } else if (rs == 1 && sd == kSaveFPRegs && am == kArgvOnStack && !be) {
    return CENTRY_CODE(Return1, SaveFPRegs, ArgvOnStack, NoBuiltinExit);
  } else if (rs == 1 && sd == kSaveFPRegs && am == kArgvOnStack && be) {
    return CENTRY_CODE(Return1, SaveFPRegs, ArgvOnStack, BuiltinExit);
  } else if (rs == 2 && sd == kDontSaveFPRegs && am == kArgvOnStack && !be) {
    return CENTRY_CODE(Return2, DontSaveFPRegs, ArgvOnStack, NoBuiltinExit);
  } else if (rs == 2 && sd == kDontSaveFPRegs && am == kArgvOnStack && be) {
    return CENTRY_CODE(Return2, DontSaveFPRegs, ArgvOnStack, BuiltinExit);
  } else if (rs == 2 && sd == kDontSaveFPRegs && am == kArgvInRegister && !be) {
    return CENTRY_CODE(Return2, DontSaveFPRegs, ArgvInRegister, NoBuiltinExit);
  } else if (rs == 2 && sd == kSaveFPRegs && am == kArgvOnStack && !be) {
    return CENTRY_CODE(Return2, SaveFPRegs, ArgvOnStack, NoBuiltinExit);
  } else if (rs == 2 && sd == kSaveFPRegs && am == kArgvOnStack && be) {
    return CENTRY_CODE(Return2, SaveFPRegs, ArgvOnStack, BuiltinExit);
  }

  UNREACHABLE();
57 58
}

59 60
#undef CENTRY_CODE

61 62
// static
Callable CodeFactory::ApiGetter(Isolate* isolate) {
63
  return Builtins::CallableFor(isolate, Builtins::kCallApiGetter);
64
}
65

66
// static
67
Callable CodeFactory::CallApiCallback(Isolate* isolate) {
68
  return Builtins::CallableFor(isolate, Builtins::kCallApiCallback);
69 70
}

71 72
// static
Callable CodeFactory::LoadGlobalIC(Isolate* isolate, TypeofMode typeof_mode) {
73 74 75 76
  return typeof_mode == NOT_INSIDE_TYPEOF
             ? Builtins::CallableFor(isolate, Builtins::kLoadGlobalICTrampoline)
             : Builtins::CallableFor(
                   isolate, Builtins::kLoadGlobalICInsideTypeofTrampoline);
77 78 79 80 81
}

// static
Callable CodeFactory::LoadGlobalICInOptimizedCode(Isolate* isolate,
                                                  TypeofMode typeof_mode) {
82 83 84 85
  return typeof_mode == NOT_INSIDE_TYPEOF
             ? Builtins::CallableFor(isolate, Builtins::kLoadGlobalIC)
             : Builtins::CallableFor(isolate,
                                     Builtins::kLoadGlobalICInsideTypeof);
86
}
87

88 89 90
Callable CodeFactory::StoreOwnIC(Isolate* isolate) {
  // TODO(ishell): Currently we use StoreOwnIC only for storing properties that
  // already exist in the boilerplate therefore we can use StoreIC.
91
  return Builtins::CallableFor(isolate, Builtins::kStoreICTrampoline);
92 93 94 95 96
}

Callable CodeFactory::StoreOwnICInOptimizedCode(Isolate* isolate) {
  // TODO(ishell): Currently we use StoreOwnIC only for storing properties that
  // already exist in the boilerplate therefore we can use StoreIC.
97
  return Builtins::CallableFor(isolate, Builtins::kStoreIC);
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
Callable CodeFactory::KeyedStoreIC_SloppyArguments(Isolate* isolate,
                                                   KeyedAccessStoreMode mode) {
  Builtins::Name builtin_index;
  switch (mode) {
    case STANDARD_STORE:
      builtin_index = Builtins::kKeyedStoreIC_SloppyArguments_Standard;
      break;
    case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW:
      builtin_index =
          Builtins::kKeyedStoreIC_SloppyArguments_GrowNoTransitionHandleCOW;
      break;
    case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS:
      builtin_index =
          Builtins::kKeyedStoreIC_SloppyArguments_NoTransitionIgnoreOOB;
      break;
    case STORE_NO_TRANSITION_HANDLE_COW:
      builtin_index =
          Builtins::kKeyedStoreIC_SloppyArguments_NoTransitionHandleCOW;
      break;
    default:
      UNREACHABLE();
  }
  return isolate->builtins()->CallableFor(isolate, builtin_index);
}

Callable CodeFactory::KeyedStoreIC_Slow(Isolate* isolate,
                                        KeyedAccessStoreMode mode) {
  Builtins::Name builtin_index;
  switch (mode) {
    case STANDARD_STORE:
      builtin_index = Builtins::kKeyedStoreIC_Slow_Standard;
      break;
    case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW:
      builtin_index = Builtins::kKeyedStoreIC_Slow_GrowNoTransitionHandleCOW;
      break;
    case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS:
      builtin_index = Builtins::kKeyedStoreIC_Slow_NoTransitionIgnoreOOB;
      break;
    case STORE_NO_TRANSITION_HANDLE_COW:
      builtin_index = Builtins::kKeyedStoreIC_Slow_NoTransitionHandleCOW;
      break;
    default:
      UNREACHABLE();
  }
  return isolate->builtins()->CallableFor(isolate, builtin_index);
}

Callable CodeFactory::StoreInArrayLiteralIC_Slow(Isolate* isolate,
                                                 KeyedAccessStoreMode mode) {
  Builtins::Name builtin_index;
  switch (mode) {
    case STANDARD_STORE:
      builtin_index = Builtins::kStoreInArrayLiteralIC_Slow_Standard;
      break;
    case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW:
      builtin_index =
          Builtins::kStoreInArrayLiteralIC_Slow_GrowNoTransitionHandleCOW;
      break;
    case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS:
      builtin_index =
          Builtins::kStoreInArrayLiteralIC_Slow_NoTransitionIgnoreOOB;
      break;
    case STORE_NO_TRANSITION_HANDLE_COW:
      builtin_index =
          Builtins::kStoreInArrayLiteralIC_Slow_NoTransitionHandleCOW;
      break;
    default:
      UNREACHABLE();
  }
  return isolate->builtins()->CallableFor(isolate, builtin_index);
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
Callable CodeFactory::ElementsTransitionAndStore(Isolate* isolate,
                                                 KeyedAccessStoreMode mode) {
  Builtins::Name builtin_index;
  switch (mode) {
    case STANDARD_STORE:
      builtin_index = Builtins::kElementsTransitionAndStore_Standard;
      break;
    case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW:
      builtin_index =
          Builtins::kElementsTransitionAndStore_GrowNoTransitionHandleCOW;
      break;
    case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS:
      builtin_index =
          Builtins::kElementsTransitionAndStore_NoTransitionIgnoreOOB;
      break;
    case STORE_NO_TRANSITION_HANDLE_COW:
      builtin_index =
          Builtins::kElementsTransitionAndStore_NoTransitionHandleCOW;
      break;
    default:
      UNREACHABLE();
  }
  return isolate->builtins()->CallableFor(isolate, builtin_index);
}

Callable CodeFactory::StoreFastElementIC(Isolate* isolate,
                                         KeyedAccessStoreMode mode) {
  Builtins::Name builtin_index;
  switch (mode) {
    case STANDARD_STORE:
      builtin_index = Builtins::kStoreFastElementIC_Standard;
      break;
    case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW:
      builtin_index = Builtins::kStoreFastElementIC_GrowNoTransitionHandleCOW;
      break;
    case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS:
      builtin_index = Builtins::kStoreFastElementIC_NoTransitionIgnoreOOB;
      break;
    case STORE_NO_TRANSITION_HANDLE_COW:
      builtin_index = Builtins::kStoreFastElementIC_NoTransitionHandleCOW;
      break;
    default:
      UNREACHABLE();
  }
  return isolate->builtins()->CallableFor(isolate, builtin_index);
}

219
// static
220
Callable CodeFactory::BinaryOperation(Isolate* isolate, Operation op) {
221
  switch (op) {
222
    case Operation::kShiftRight:
223
      return Builtins::CallableFor(isolate, Builtins::kShiftRight);
224
    case Operation::kShiftLeft:
225
      return Builtins::CallableFor(isolate, Builtins::kShiftLeft);
226
    case Operation::kShiftRightLogical:
227
      return Builtins::CallableFor(isolate, Builtins::kShiftRightLogical);
228
    case Operation::kAdd:
229
      return Builtins::CallableFor(isolate, Builtins::kAdd);
230
    case Operation::kSubtract:
231
      return Builtins::CallableFor(isolate, Builtins::kSubtract);
232
    case Operation::kMultiply:
233
      return Builtins::CallableFor(isolate, Builtins::kMultiply);
234
    case Operation::kDivide:
235
      return Builtins::CallableFor(isolate, Builtins::kDivide);
236
    case Operation::kModulus:
237
      return Builtins::CallableFor(isolate, Builtins::kModulus);
238
    case Operation::kBitwiseOr:
239
      return Builtins::CallableFor(isolate, Builtins::kBitwiseOr);
240
    case Operation::kBitwiseAnd:
241
      return Builtins::CallableFor(isolate, Builtins::kBitwiseAnd);
242
    case Operation::kBitwiseXor:
243 244 245 246 247
      return Builtins::CallableFor(isolate, Builtins::kBitwiseXor);
    default:
      break;
  }
  UNREACHABLE();
248 249
}

250 251 252 253
// static
Callable CodeFactory::NonPrimitiveToPrimitive(Isolate* isolate,
                                              ToPrimitiveHint hint) {
  return Callable(isolate->builtins()->NonPrimitiveToPrimitive(hint),
254
                  TypeConversionDescriptor{});
255 256 257 258 259 260
}

// static
Callable CodeFactory::OrdinaryToPrimitive(Isolate* isolate,
                                          OrdinaryToPrimitiveHint hint) {
  return Callable(isolate->builtins()->OrdinaryToPrimitive(hint),
261
                  TypeConversionDescriptor{});
262
}
263

264
// static
265 266 267 268 269 270 271 272
Callable CodeFactory::StringAdd(Isolate* isolate, StringAddFlags flags) {
  switch (flags) {
    case STRING_ADD_CHECK_NONE:
      return Builtins::CallableFor(isolate, Builtins::kStringAdd_CheckNone);
    case STRING_ADD_CONVERT_LEFT:
      return Builtins::CallableFor(isolate, Builtins::kStringAdd_ConvertLeft);
    case STRING_ADD_CONVERT_RIGHT:
      return Builtins::CallableFor(isolate, Builtins::kStringAdd_ConvertRight);
273 274
  }
  UNREACHABLE();
275 276
}

277 278
// static
Callable CodeFactory::ResumeGenerator(Isolate* isolate) {
279
  return Builtins::CallableFor(isolate, Builtins::kResumeGeneratorTrampoline);
280 281
}

282 283
// static
Callable CodeFactory::FrameDropperTrampoline(Isolate* isolate) {
284
  return Builtins::CallableFor(isolate, Builtins::kFrameDropperTrampoline);
285 286 287 288
}

// static
Callable CodeFactory::HandleDebuggerStatement(Isolate* isolate) {
289
  return Builtins::CallableFor(isolate, Builtins::kHandleDebuggerStatement);
290 291
}

292
// static
293 294
Callable CodeFactory::FastNewFunctionContext(Isolate* isolate,
                                             ScopeType scope_type) {
295 296 297 298 299 300 301 302 303 304
  switch (scope_type) {
    case ScopeType::EVAL_SCOPE:
      return Builtins::CallableFor(isolate,
                                   Builtins::kFastNewFunctionContextEval);
    case ScopeType::FUNCTION_SCOPE:
      return Builtins::CallableFor(isolate,
                                   Builtins::kFastNewFunctionContextFunction);
    default:
      UNREACHABLE();
  }
305 306
}

307 308
// static
Callable CodeFactory::ArgumentAdaptor(Isolate* isolate) {
309
  return Builtins::CallableFor(isolate, Builtins::kArgumentsAdaptorTrampoline);
310 311
}

312
// static
313
Callable CodeFactory::Call(Isolate* isolate, ConvertReceiverMode mode) {
314
  return Callable(isolate->builtins()->Call(mode), CallTrampolineDescriptor{});
315 316
}

317 318
// static
Callable CodeFactory::CallWithArrayLike(Isolate* isolate) {
319
  return Builtins::CallableFor(isolate, Builtins::kCallWithArrayLike);
320 321
}

322 323
// static
Callable CodeFactory::CallWithSpread(Isolate* isolate) {
324
  return Builtins::CallableFor(isolate, Builtins::kCallWithSpread);
325 326
}

327
// static
328 329
Callable CodeFactory::CallFunction(Isolate* isolate, ConvertReceiverMode mode) {
  return Callable(isolate->builtins()->CallFunction(mode),
330
                  CallTrampolineDescriptor{});
331 332
}

333 334
// static
Callable CodeFactory::CallVarargs(Isolate* isolate) {
335
  return Builtins::CallableFor(isolate, Builtins::kCallVarargs);
336 337
}

338 339
// static
Callable CodeFactory::CallForwardVarargs(Isolate* isolate) {
340
  return Builtins::CallableFor(isolate, Builtins::kCallForwardVarargs);
341 342 343 344
}

// static
Callable CodeFactory::CallFunctionForwardVarargs(Isolate* isolate) {
345
  return Builtins::CallableFor(isolate, Builtins::kCallFunctionForwardVarargs);
346 347
}

348 349
// static
Callable CodeFactory::Construct(Isolate* isolate) {
350
  return Builtins::CallableFor(isolate, Builtins::kConstruct);
351 352
}

353 354
// static
Callable CodeFactory::ConstructWithSpread(Isolate* isolate) {
355
  return Builtins::CallableFor(isolate, Builtins::kConstructWithSpread);
356 357
}

358 359
// static
Callable CodeFactory::ConstructFunction(Isolate* isolate) {
360
  return Builtins::CallableFor(isolate, Builtins::kConstructFunction);
361 362
}

363 364
// static
Callable CodeFactory::ConstructVarargs(Isolate* isolate) {
365
  return Builtins::CallableFor(isolate, Builtins::kConstructVarargs);
366 367
}

368 369
// static
Callable CodeFactory::ConstructForwardVarargs(Isolate* isolate) {
370
  return Builtins::CallableFor(isolate, Builtins::kConstructForwardVarargs);
371 372 373 374
}

// static
Callable CodeFactory::ConstructFunctionForwardVarargs(Isolate* isolate) {
375 376
  return Builtins::CallableFor(isolate,
                               Builtins::kConstructFunctionForwardVarargs);
377 378
}

379
// static
380 381
Callable CodeFactory::InterpreterPushArgsThenCall(
    Isolate* isolate, ConvertReceiverMode receiver_mode,
382
    InterpreterPushArgsMode mode) {
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  switch (mode) {
    case InterpreterPushArgsMode::kArrayFunction:
      // There is no special-case handling of calls to Array. They will all go
      // through the kOther case below.
      UNREACHABLE();
    case InterpreterPushArgsMode::kWithFinalSpread:
      return Builtins::CallableFor(
          isolate, Builtins::kInterpreterPushArgsThenCallWithFinalSpread);
    case InterpreterPushArgsMode::kOther:
      switch (receiver_mode) {
        case ConvertReceiverMode::kNullOrUndefined:
          return Builtins::CallableFor(
              isolate, Builtins::kInterpreterPushUndefinedAndArgsThenCall);
        case ConvertReceiverMode::kNotNullOrUndefined:
        case ConvertReceiverMode::kAny:
          return Builtins::CallableFor(isolate,
                                       Builtins::kInterpreterPushArgsThenCall);
      }
  }
  UNREACHABLE();
403 404
}

405
// static
406
Callable CodeFactory::InterpreterPushArgsThenConstruct(
407
    Isolate* isolate, InterpreterPushArgsMode mode) {
408 409 410 411 412 413 414 415 416 417 418 419
  switch (mode) {
    case InterpreterPushArgsMode::kArrayFunction:
      return Builtins::CallableFor(
          isolate, Builtins::kInterpreterPushArgsThenConstructArrayFunction);
    case InterpreterPushArgsMode::kWithFinalSpread:
      return Builtins::CallableFor(
          isolate, Builtins::kInterpreterPushArgsThenConstructWithFinalSpread);
    case InterpreterPushArgsMode::kOther:
      return Builtins::CallableFor(isolate,
                                   Builtins::kInterpreterPushArgsThenConstruct);
  }
  UNREACHABLE();
420 421
}

422
// static
423
Callable CodeFactory::InterpreterCEntry(Isolate* isolate, int result_size) {
424 425
  // Note: If we ever use fpregs in the interpreter then we will need to
  // save fpregs too.
426 427
  Handle<Code> code = CodeFactory::CEntry(isolate, result_size, kDontSaveFPRegs,
                                          kArgvInRegister);
428 429 430 431 432 433
  if (result_size == 1) {
    return Callable(code, InterpreterCEntry1Descriptor{});
  } else {
    DCHECK_EQ(result_size, 2);
    return Callable(code, InterpreterCEntry2Descriptor{});
  }
434 435
}

436 437
// static
Callable CodeFactory::InterpreterOnStackReplacement(Isolate* isolate) {
438 439
  return Builtins::CallableFor(isolate,
                               Builtins::kInterpreterOnStackReplacement);
440 441
}

442 443 444 445
// static
Callable CodeFactory::ArrayNoArgumentConstructor(
    Isolate* isolate, ElementsKind kind,
    AllocationSiteOverrideMode override_mode) {
446 447 448 449 450
#define CASE(kind_caps, kind_camel, mode_camel) \
  case kind_caps:                               \
    return Builtins::CallableFor(               \
        isolate,                                \
        Builtins::kArrayNoArgumentConstructor_##kind_camel##_##mode_camel);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  if (override_mode == DONT_OVERRIDE && AllocationSite::ShouldTrack(kind)) {
    DCHECK(IsSmiElementsKind(kind));
    switch (kind) {
      CASE(PACKED_SMI_ELEMENTS, PackedSmi, DontOverride);
      CASE(HOLEY_SMI_ELEMENTS, HoleySmi, DontOverride);
      default:
        UNREACHABLE();
    }
  } else {
    DCHECK(override_mode == DISABLE_ALLOCATION_SITES ||
           !AllocationSite::ShouldTrack(kind));
    switch (kind) {
      CASE(PACKED_SMI_ELEMENTS, PackedSmi, DisableAllocationSites);
      CASE(HOLEY_SMI_ELEMENTS, HoleySmi, DisableAllocationSites);
      CASE(PACKED_ELEMENTS, Packed, DisableAllocationSites);
      CASE(HOLEY_ELEMENTS, Holey, DisableAllocationSites);
      CASE(PACKED_DOUBLE_ELEMENTS, PackedDouble, DisableAllocationSites);
      CASE(HOLEY_DOUBLE_ELEMENTS, HoleyDouble, DisableAllocationSites);
      default:
        UNREACHABLE();
    }
  }
#undef CASE
474 475 476 477 478 479
}

// static
Callable CodeFactory::ArraySingleArgumentConstructor(
    Isolate* isolate, ElementsKind kind,
    AllocationSiteOverrideMode override_mode) {
480 481 482 483 484
#define CASE(kind_caps, kind_camel, mode_camel) \
  case kind_caps:                               \
    return Builtins::CallableFor(               \
        isolate,                                \
        Builtins::kArraySingleArgumentConstructor_##kind_camel##_##mode_camel)
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  if (override_mode == DONT_OVERRIDE && AllocationSite::ShouldTrack(kind)) {
    DCHECK(IsSmiElementsKind(kind));
    switch (kind) {
      CASE(PACKED_SMI_ELEMENTS, PackedSmi, DontOverride);
      CASE(HOLEY_SMI_ELEMENTS, HoleySmi, DontOverride);
      default:
        UNREACHABLE();
    }
  } else {
    DCHECK(override_mode == DISABLE_ALLOCATION_SITES ||
           !AllocationSite::ShouldTrack(kind));
    switch (kind) {
      CASE(PACKED_SMI_ELEMENTS, PackedSmi, DisableAllocationSites);
      CASE(HOLEY_SMI_ELEMENTS, HoleySmi, DisableAllocationSites);
      CASE(PACKED_ELEMENTS, Packed, DisableAllocationSites);
      CASE(HOLEY_ELEMENTS, Holey, DisableAllocationSites);
      CASE(PACKED_DOUBLE_ELEMENTS, PackedDouble, DisableAllocationSites);
      CASE(HOLEY_DOUBLE_ELEMENTS, HoleyDouble, DisableAllocationSites);
      default:
        UNREACHABLE();
    }
  }
#undef CASE
508 509
}

510 511
}  // namespace internal
}  // namespace v8