interpreter-intrinsics-generator.cc 16.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.

#include "src/interpreter/interpreter-intrinsics-generator.h"

#include "src/allocation.h"
#include "src/builtins/builtins.h"
#include "src/code-factory.h"
#include "src/frames.h"
#include "src/interpreter/bytecodes.h"
#include "src/interpreter/interpreter-assembler.h"
#include "src/interpreter/interpreter-intrinsics.h"
14 15
#include "src/objects-inl.h"
#include "src/objects/module.h"
16 17 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

namespace v8 {
namespace internal {
namespace interpreter {

using compiler::Node;

class IntrinsicsGenerator {
 public:
  explicit IntrinsicsGenerator(InterpreterAssembler* assembler)
      : isolate_(assembler->isolate()),
        zone_(assembler->zone()),
        assembler_(assembler) {}

  Node* InvokeIntrinsic(Node* function_id, Node* context, Node* first_arg_reg,
                        Node* arg_count);

 private:
  enum InstanceTypeCompareMode {
    kInstanceTypeEqual,
    kInstanceTypeGreaterThanOrEqual
  };

  Node* IsInstanceType(Node* input, int type);
  Node* CompareInstanceType(Node* map, int type, InstanceTypeCompareMode mode);
  Node* IntrinsicAsStubCall(Node* input, Node* context,
                            Callable const& callable);
43
  Node* IntrinsicAsBuiltinCall(Node* input, Node* context, Builtins::Name name);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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
  void AbortIfArgCountMismatch(int expected, compiler::Node* actual);

#define DECLARE_INTRINSIC_HELPER(name, lower_case, count) \
  Node* name(Node* input, Node* arg_count, Node* context);
  INTRINSICS_LIST(DECLARE_INTRINSIC_HELPER)
#undef DECLARE_INTRINSIC_HELPER

  Isolate* isolate() { return isolate_; }
  Zone* zone() { return zone_; }

  Isolate* isolate_;
  Zone* zone_;
  InterpreterAssembler* assembler_;

  DISALLOW_COPY_AND_ASSIGN(IntrinsicsGenerator);
};

Node* GenerateInvokeIntrinsic(InterpreterAssembler* assembler,
                              Node* function_id, Node* context,
                              Node* first_arg_reg, Node* arg_count) {
  IntrinsicsGenerator generator(assembler);
  return generator.InvokeIntrinsic(function_id, context, first_arg_reg,
                                   arg_count);
}

#define __ assembler_->

Node* IntrinsicsGenerator::InvokeIntrinsic(Node* function_id, Node* context,
                                           Node* first_arg_reg,
                                           Node* arg_count) {
  InterpreterAssembler::Label abort(assembler_), end(assembler_);
  InterpreterAssembler::Variable result(assembler_,
                                        MachineRepresentation::kTagged);

#define MAKE_LABEL(name, lower_case, count) \
  InterpreterAssembler::Label lower_case(assembler_);
  INTRINSICS_LIST(MAKE_LABEL)
#undef MAKE_LABEL

#define LABEL_POINTER(name, lower_case, count) &lower_case,
  InterpreterAssembler::Label* labels[] = {INTRINSICS_LIST(LABEL_POINTER)};
#undef LABEL_POINTER

#define CASE(name, lower_case, count) \
  static_cast<int32_t>(IntrinsicsHelper::IntrinsicId::k##name),
  int32_t cases[] = {INTRINSICS_LIST(CASE)};
#undef CASE

  __ Switch(function_id, &abort, cases, labels, arraysize(cases));
93 94 95 96 97 98 99 100 101 102 103 104
#define HANDLE_CASE(name, lower_case, expected_arg_count)     \
  __ BIND(&lower_case);                                       \
  {                                                           \
    if (FLAG_debug_code && expected_arg_count >= 0) {         \
      AbortIfArgCountMismatch(expected_arg_count, arg_count); \
    }                                                         \
    Node* value = name(first_arg_reg, arg_count, context);    \
    if (value) {                                              \
      result.Bind(value);                                     \
      __ Goto(&end);                                          \
    }                                                         \
  }
105 106 107
  INTRINSICS_LIST(HANDLE_CASE)
#undef HANDLE_CASE

108
  __ BIND(&abort);
109 110 111 112 113 114
  {
    __ Abort(BailoutReason::kUnexpectedFunctionIDForInvokeIntrinsic);
    result.Bind(__ UndefinedConstant());
    __ Goto(&end);
  }

115
  __ BIND(&end);
116 117 118 119 120 121 122 123 124 125
  return result.value();
}

Node* IntrinsicsGenerator::CompareInstanceType(Node* object, int type,
                                               InstanceTypeCompareMode mode) {
  Node* instance_type = __ LoadInstanceType(object);

  if (mode == kInstanceTypeEqual) {
    return __ Word32Equal(instance_type, __ Int32Constant(type));
  } else {
126
    DCHECK_EQ(mode, kInstanceTypeGreaterThanOrEqual);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    return __ Int32GreaterThanOrEqual(instance_type, __ Int32Constant(type));
  }
}

Node* IntrinsicsGenerator::IsInstanceType(Node* input, int type) {
  InterpreterAssembler::Variable return_value(assembler_,
                                              MachineRepresentation::kTagged);
  // TODO(ishell): Use Select here.
  InterpreterAssembler::Label if_not_smi(assembler_), return_true(assembler_),
      return_false(assembler_), end(assembler_);
  Node* arg = __ LoadRegister(input);
  __ GotoIf(__ TaggedIsSmi(arg), &return_false);

  Node* condition = CompareInstanceType(arg, type, kInstanceTypeEqual);
  __ Branch(condition, &return_true, &return_false);

143
  __ BIND(&return_true);
144
  {
145
    return_value.Bind(__ TrueConstant());
146 147 148
    __ Goto(&end);
  }

149
  __ BIND(&return_false);
150
  {
151
    return_value.Bind(__ FalseConstant());
152 153 154
    __ Goto(&end);
  }

155
  __ BIND(&end);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  return return_value.value();
}

Node* IntrinsicsGenerator::IsJSReceiver(Node* input, Node* arg_count,
                                        Node* context) {
  // TODO(ishell): Use Select here.
  // TODO(ishell): Use CSA::IsJSReceiverInstanceType here.
  InterpreterAssembler::Variable return_value(assembler_,
                                              MachineRepresentation::kTagged);
  InterpreterAssembler::Label return_true(assembler_), return_false(assembler_),
      end(assembler_);

  Node* arg = __ LoadRegister(input);
  __ GotoIf(__ TaggedIsSmi(arg), &return_false);

  STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
  Node* condition = CompareInstanceType(arg, FIRST_JS_RECEIVER_TYPE,
                                        kInstanceTypeGreaterThanOrEqual);
  __ Branch(condition, &return_true, &return_false);

176
  __ BIND(&return_true);
177
  {
178
    return_value.Bind(__ TrueConstant());
179 180 181
    __ Goto(&end);
  }

182
  __ BIND(&return_false);
183
  {
184
    return_value.Bind(__ FalseConstant());
185 186 187
    __ Goto(&end);
  }

188
  __ BIND(&end);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  return return_value.value();
}

Node* IntrinsicsGenerator::IsArray(Node* input, Node* arg_count,
                                   Node* context) {
  return IsInstanceType(input, JS_ARRAY_TYPE);
}

Node* IntrinsicsGenerator::IsJSProxy(Node* input, Node* arg_count,
                                     Node* context) {
  return IsInstanceType(input, JS_PROXY_TYPE);
}

Node* IntrinsicsGenerator::IsTypedArray(Node* input, Node* arg_count,
                                        Node* context) {
  return IsInstanceType(input, JS_TYPED_ARRAY_TYPE);
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
Node* IntrinsicsGenerator::IsJSMap(Node* input, Node* arg_count,
                                   Node* context) {
  return IsInstanceType(input, JS_MAP_TYPE);
}

Node* IntrinsicsGenerator::IsJSSet(Node* input, Node* arg_count,
                                   Node* context) {
  return IsInstanceType(input, JS_SET_TYPE);
}

Node* IntrinsicsGenerator::IsJSWeakMap(Node* input, Node* arg_count,
                                       Node* context) {
  return IsInstanceType(input, JS_WEAK_MAP_TYPE);
}

Node* IntrinsicsGenerator::IsJSWeakSet(Node* input, Node* arg_count,
                                       Node* context) {
  return IsInstanceType(input, JS_WEAK_SET_TYPE);
}

227 228 229 230 231 232 233 234 235 236
Node* IntrinsicsGenerator::IsSmi(Node* input, Node* arg_count, Node* context) {
  // TODO(ishell): Use SelectBooleanConstant here.
  InterpreterAssembler::Variable return_value(assembler_,
                                              MachineRepresentation::kTagged);
  InterpreterAssembler::Label if_smi(assembler_), if_not_smi(assembler_),
      end(assembler_);

  Node* arg = __ LoadRegister(input);

  __ Branch(__ TaggedIsSmi(arg), &if_smi, &if_not_smi);
237
  __ BIND(&if_smi);
238
  {
239
    return_value.Bind(__ TrueConstant());
240 241 242
    __ Goto(&end);
  }

243
  __ BIND(&if_not_smi);
244
  {
245
    return_value.Bind(__ FalseConstant());
246 247 248
    __ Goto(&end);
  }

249
  __ BIND(&end);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  return return_value.value();
}

Node* IntrinsicsGenerator::IntrinsicAsStubCall(Node* args_reg, Node* context,
                                               Callable const& callable) {
  int param_count = callable.descriptor().GetParameterCount();
  int input_count = param_count + 2;  // +2 for target and context
  Node** args = zone()->NewArray<Node*>(input_count);
  int index = 0;
  args[index++] = __ HeapConstant(callable.code());
  for (int i = 0; i < param_count; i++) {
    args[index++] = __ LoadRegister(args_reg);
    args_reg = __ NextRegister(args_reg);
  }
  args[index++] = context;
  return __ CallStubN(callable.descriptor(), 1, input_count, args);
}

268 269 270 271 272 273
Node* IntrinsicsGenerator::IntrinsicAsBuiltinCall(Node* input, Node* context,
                                                  Builtins::Name name) {
  Callable callable = Builtins::CallableFor(isolate_, name);
  return IntrinsicAsStubCall(input, context, callable);
}

274 275
Node* IntrinsicsGenerator::CreateIterResultObject(Node* input, Node* arg_count,
                                                  Node* context) {
276 277 278
  return IntrinsicAsStubCall(
      input, context,
      Builtins::CallableFor(isolate(), Builtins::kCreateIterResultObject));
279 280 281 282
}

Node* IntrinsicsGenerator::HasProperty(Node* input, Node* arg_count,
                                       Node* context) {
283 284
  return IntrinsicAsStubCall(
      input, context, Builtins::CallableFor(isolate(), Builtins::kHasProperty));
285 286 287 288
}

Node* IntrinsicsGenerator::ToString(Node* input, Node* arg_count,
                                    Node* context) {
289 290
  return IntrinsicAsStubCall(
      input, context, Builtins::CallableFor(isolate(), Builtins::kToString));
291 292 293 294
}

Node* IntrinsicsGenerator::ToLength(Node* input, Node* arg_count,
                                    Node* context) {
295 296
  return IntrinsicAsStubCall(
      input, context, Builtins::CallableFor(isolate(), Builtins::kToLength));
297 298 299 300
}

Node* IntrinsicsGenerator::ToInteger(Node* input, Node* arg_count,
                                     Node* context) {
301 302
  return IntrinsicAsStubCall(
      input, context, Builtins::CallableFor(isolate(), Builtins::kToInteger));
303 304 305 306
}

Node* IntrinsicsGenerator::ToNumber(Node* input, Node* arg_count,
                                    Node* context) {
307 308
  return IntrinsicAsStubCall(
      input, context, Builtins::CallableFor(isolate(), Builtins::kToNumber));
309 310 311 312
}

Node* IntrinsicsGenerator::ToObject(Node* input, Node* arg_count,
                                    Node* context) {
313 314
  return IntrinsicAsStubCall(
      input, context, Builtins::CallableFor(isolate(), Builtins::kToObject));
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
}

Node* IntrinsicsGenerator::Call(Node* args_reg, Node* arg_count,
                                Node* context) {
  // First argument register contains the function target.
  Node* function = __ LoadRegister(args_reg);

  // Receiver is the second runtime call argument.
  Node* receiver_reg = __ NextRegister(args_reg);
  Node* receiver_arg = __ RegisterLocation(receiver_reg);

  // Subtract function and receiver from arg count.
  Node* function_and_receiver_count = __ Int32Constant(2);
  Node* target_args_count = __ Int32Sub(arg_count, function_and_receiver_count);

  if (FLAG_debug_code) {
    InterpreterAssembler::Label arg_count_positive(assembler_);
    Node* comparison = __ Int32LessThan(target_args_count, __ Int32Constant(0));
    __ GotoIfNot(comparison, &arg_count_positive);
    __ Abort(kWrongArgumentCountForInvokeIntrinsic);
    __ Goto(&arg_count_positive);
336
    __ BIND(&arg_count_positive);
337 338
  }

339 340 341
  __ CallJSAndDispatch(function, context, receiver_arg, target_args_count,
                       ConvertReceiverMode::kAny);
  return nullptr;  // We never return from the CallJSAndDispatch above.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
}

Node* IntrinsicsGenerator::ClassOf(Node* args_reg, Node* arg_count,
                                   Node* context) {
  Node* value = __ LoadRegister(args_reg);
  return __ ClassOf(value);
}

Node* IntrinsicsGenerator::CreateAsyncFromSyncIterator(Node* args_reg,
                                                       Node* arg_count,
                                                       Node* context) {
  InterpreterAssembler::Label not_receiver(
      assembler_, InterpreterAssembler::Label::kDeferred);
  InterpreterAssembler::Label done(assembler_);
  InterpreterAssembler::Variable return_value(assembler_,
                                              MachineRepresentation::kTagged);

  Node* sync_iterator = __ LoadRegister(args_reg);

  __ GotoIf(__ TaggedIsSmi(sync_iterator), &not_receiver);
  __ GotoIfNot(__ IsJSReceiver(sync_iterator), &not_receiver);

  Node* const native_context = __ LoadNativeContext(context);
  Node* const map = __ LoadContextElement(
      native_context, Context::ASYNC_FROM_SYNC_ITERATOR_MAP_INDEX);
  Node* const iterator = __ AllocateJSObjectFromMap(map);

  __ StoreObjectFieldNoWriteBarrier(
      iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset, sync_iterator);

  return_value.Bind(iterator);
  __ Goto(&done);

375
  __ BIND(&not_receiver);
376 377 378 379 380 381 382 383
  {
    return_value.Bind(
        __ CallRuntime(Runtime::kThrowSymbolIteratorInvalid, context));

    // Unreachable due to the Throw in runtime call.
    __ Goto(&done);
  }

384
  __ BIND(&done);
385 386 387
  return return_value.value();
}

388 389 390 391 392 393
Node* IntrinsicsGenerator::CreateJSGeneratorObject(Node* input, Node* arg_count,
                                                   Node* context) {
  return IntrinsicAsBuiltinCall(input, context,
                                Builtins::kCreateGeneratorObject);
}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
Node* IntrinsicsGenerator::GeneratorGetContext(Node* args_reg, Node* arg_count,
                                               Node* context) {
  Node* generator = __ LoadRegister(args_reg);
  Node* const value =
      __ LoadObjectField(generator, JSGeneratorObject::kContextOffset);

  return value;
}

Node* IntrinsicsGenerator::GeneratorGetInputOrDebugPos(Node* args_reg,
                                                       Node* arg_count,
                                                       Node* context) {
  Node* generator = __ LoadRegister(args_reg);
  Node* const value =
      __ LoadObjectField(generator, JSGeneratorObject::kInputOrDebugPosOffset);

  return value;
}

Node* IntrinsicsGenerator::GeneratorGetResumeMode(Node* args_reg,
                                                  Node* arg_count,
                                                  Node* context) {
  Node* generator = __ LoadRegister(args_reg);
  Node* const value =
      __ LoadObjectField(generator, JSGeneratorObject::kResumeModeOffset);

  return value;
}

423 424 425 426 427 428
Node* IntrinsicsGenerator::GeneratorClose(Node* args_reg, Node* arg_count,
                                          Node* context) {
  Node* generator = __ LoadRegister(args_reg);
  __ StoreObjectFieldNoWriteBarrier(
      generator, JSGeneratorObject::kContinuationOffset,
      __ SmiConstant(JSGeneratorObject::kGeneratorClosed));
429
  return __ UndefinedConstant();
430 431
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
Node* IntrinsicsGenerator::GetImportMetaObject(Node* args_reg, Node* arg_count,
                                               Node* context) {
  Node* const module_context = __ LoadModuleContext(context);
  Node* const module =
      __ LoadContextElement(module_context, Context::EXTENSION_INDEX);
  Node* const import_meta =
      __ LoadObjectField(module, Module::kImportMetaOffset);

  InterpreterAssembler::Variable return_value(assembler_,
                                              MachineRepresentation::kTagged);
  return_value.Bind(import_meta);

  InterpreterAssembler::Label end(assembler_);
  __ GotoIfNot(__ IsTheHole(import_meta), &end);

  return_value.Bind(__ CallRuntime(Runtime::kGetImportMetaObject, context));
  __ Goto(&end);

  __ BIND(&end);
  return return_value.value();
}

454 455 456 457 458 459 460 461 462 463 464 465
Node* IntrinsicsGenerator::AsyncGeneratorReject(Node* input, Node* arg_count,
                                                Node* context) {
  return IntrinsicAsBuiltinCall(input, context,
                                Builtins::kAsyncGeneratorReject);
}

Node* IntrinsicsGenerator::AsyncGeneratorResolve(Node* input, Node* arg_count,
                                                 Node* context) {
  return IntrinsicAsBuiltinCall(input, context,
                                Builtins::kAsyncGeneratorResolve);
}

466 467 468 469 470
Node* IntrinsicsGenerator::AsyncGeneratorYield(Node* input, Node* arg_count,
                                               Node* context) {
  return IntrinsicAsBuiltinCall(input, context, Builtins::kAsyncGeneratorYield);
}

471 472 473 474 475 476
void IntrinsicsGenerator::AbortIfArgCountMismatch(int expected, Node* actual) {
  InterpreterAssembler::Label match(assembler_);
  Node* comparison = __ Word32Equal(actual, __ Int32Constant(expected));
  __ GotoIf(comparison, &match);
  __ Abort(kWrongArgumentCountForInvokeIntrinsic);
  __ Goto(&match);
477
  __ BIND(&match);
478 479
}

480 481
#undef __

482 483 484
}  // namespace interpreter
}  // namespace internal
}  // namespace v8