instructions.cc 28.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright 2018 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/torque/instructions.h"
#include "src/torque/cfg.h"
#include "src/torque/type-oracle.h"

namespace v8 {
namespace internal {
namespace torque {

#define TORQUE_INSTRUCTION_BOILERPLATE_DEFINITIONS(Name)        \
  const InstructionKind Name::kKind = InstructionKind::k##Name; \
  std::unique_ptr<InstructionBase> Name::Clone() const {        \
    return std::unique_ptr<InstructionBase>(new Name(*this));   \
  }                                                             \
  void Name::Assign(const InstructionBase& other) {             \
    *this = static_cast<const Name&>(other);                    \
  }
TORQUE_INSTRUCTION_LIST(TORQUE_INSTRUCTION_BOILERPLATE_DEFINITIONS)
#undef TORQUE_INSTRUCTION_BOILERPLATE_DEFINITIONS

24 25 26 27 28 29 30 31 32 33 34 35 36
namespace {
void ExpectType(const Type* expected, const Type* actual) {
  if (expected != actual) {
    ReportError("expected type ", *expected, " but found ", *actual);
  }
}
void ExpectSubtype(const Type* subtype, const Type* supertype) {
  if (!subtype->IsSubtypeOf(supertype)) {
    ReportError("type ", *subtype, " is not a subtype of ", *supertype);
  }
}
}  // namespace

37 38 39 40
void PeekInstruction::TypeInstruction(Stack<const Type*>* stack,
                                      ControlFlowGraph* cfg) const {
  const Type* type = stack->Peek(slot);
  if (widened_type) {
41 42 43 44
    if (type->IsTopType()) {
      const TopType* top_type = TopType::cast(type);
      ReportError("use of " + top_type->reason());
    }
45
    ExpectSubtype(type, *widened_type);
46 47 48 49 50
    type = *widened_type;
  }
  stack->Push(type);
}

51 52 53 54 55
void PeekInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Push(locations->Peek(slot));
}

56 57 58 59
void PokeInstruction::TypeInstruction(Stack<const Type*>* stack,
                                      ControlFlowGraph* cfg) const {
  const Type* type = stack->Top();
  if (widened_type) {
60
    ExpectSubtype(type, *widened_type);
61 62 63 64 65 66
    type = *widened_type;
  }
  stack->Poke(slot, type);
  stack->Pop();
}

67 68 69 70 71
void PokeInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Poke(slot, locations->Pop());
}

72 73 74 75 76
void DeleteRangeInstruction::TypeInstruction(Stack<const Type*>* stack,
                                             ControlFlowGraph* cfg) const {
  stack->DeleteRange(range);
}

77 78 79 80 81
void DeleteRangeInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->DeleteRange(range);
}

82 83 84 85 86
void PushUninitializedInstruction::TypeInstruction(
    Stack<const Type*>* stack, ControlFlowGraph* cfg) const {
  stack->Push(type);
}

87 88 89 90 91 92 93 94 95
void PushUninitializedInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Push(GetValueDefinition());
}

DefinitionLocation PushUninitializedInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

96 97
void PushBuiltinPointerInstruction::TypeInstruction(
    Stack<const Type*>* stack, ControlFlowGraph* cfg) const {
98 99 100
  stack->Push(type);
}

101 102 103 104 105 106 107 108 109
void PushBuiltinPointerInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Push(GetValueDefinition());
}

DefinitionLocation PushBuiltinPointerInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

110 111
void NamespaceConstantInstruction::TypeInstruction(
    Stack<const Type*>* stack, ControlFlowGraph* cfg) const {
112 113 114
  stack->PushMany(LowerType(constant->type()));
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
void NamespaceConstantInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  for (std::size_t i = 0; i < GetValueDefinitionCount(); ++i) {
    locations->Push(GetValueDefinition(i));
  }
}

std::size_t NamespaceConstantInstruction::GetValueDefinitionCount() const {
  return LowerType(constant->type()).size();
}

DefinitionLocation NamespaceConstantInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

132 133 134 135 136
std::ostream& operator<<(std::ostream& os,
                         const NamespaceConstantInstruction& instruction) {
  return os << "NamespaceConstant " << instruction.constant->external_name();
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
void InstructionBase::InvalidateTransientTypes(
    Stack<const Type*>* stack) const {
  auto current = stack->begin();
  while (current != stack->end()) {
    if ((*current)->IsTransient()) {
      std::stringstream stream;
      stream << "type " << **current
             << " is made invalid by transitioning callable invocation at "
             << PositionAsString(pos);
      *current = TypeOracle::GetTopType(stream.str(), *current);
    }
    ++current;
  }
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
void CallIntrinsicInstruction::TypeInstruction(Stack<const Type*>* stack,
                                               ControlFlowGraph* cfg) const {
  std::vector<const Type*> parameter_types =
      LowerParameterTypes(intrinsic->signature().parameter_types);
  for (intptr_t i = parameter_types.size() - 1; i >= 0; --i) {
    const Type* arg_type = stack->Pop();
    const Type* parameter_type = parameter_types.back();
    parameter_types.pop_back();
    if (arg_type != parameter_type) {
      ReportError("parameter ", i, ": expected type ", *parameter_type,
                  " but found type ", *arg_type);
    }
  }
  if (intrinsic->IsTransitioning()) {
    InvalidateTransientTypes(stack);
  }
  stack->PushMany(LowerType(intrinsic->signature().return_type));
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
void CallIntrinsicInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  auto parameter_types =
      LowerParameterTypes(intrinsic->signature().parameter_types);
  locations->PopMany(parameter_types.size());
  for (std::size_t i = 0; i < GetValueDefinitionCount(); ++i) {
    locations->Push(DefinitionLocation::Instruction(this, i));
  }
}

std::size_t CallIntrinsicInstruction::GetValueDefinitionCount() const {
  return LowerType(intrinsic->signature().return_type).size();
}

DefinitionLocation CallIntrinsicInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
std::ostream& operator<<(std::ostream& os,
                         const CallIntrinsicInstruction& instruction) {
  os << "CallIntrinsic " << instruction.intrinsic->ReadableName();
  if (!instruction.specialization_types.empty()) {
    os << "<";
    PrintCommaSeparatedList(
        os, instruction.specialization_types,
        [](const Type* type) -> const Type& { return *type; });
    os << ">";
  }
  os << "(";
  PrintCommaSeparatedList(os, instruction.constexpr_arguments);
  os << ")";
  return os;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220
void CallCsaMacroInstruction::TypeInstruction(Stack<const Type*>* stack,
                                              ControlFlowGraph* cfg) const {
  std::vector<const Type*> parameter_types =
      LowerParameterTypes(macro->signature().parameter_types);
  for (intptr_t i = parameter_types.size() - 1; i >= 0; --i) {
    const Type* arg_type = stack->Pop();
    const Type* parameter_type = parameter_types.back();
    parameter_types.pop_back();
    if (arg_type != parameter_type) {
      ReportError("parameter ", i, ": expected type ", *parameter_type,
                  " but found type ", *arg_type);
    }
  }

221 222 223 224
  if (macro->IsTransitioning()) {
    InvalidateTransientTypes(stack);
  }

225 226
  if (catch_block) {
    Stack<const Type*> catch_stack = *stack;
227
    catch_stack.Push(TypeOracle::GetJSAnyType());
228 229 230
    (*catch_block)->SetInputTypes(catch_stack);
  }

231 232 233
  stack->PushMany(LowerType(macro->signature().return_type));
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
void CallCsaMacroInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  auto parameter_types =
      LowerParameterTypes(macro->signature().parameter_types);
  locations->PopMany(parameter_types.size());

  if (catch_block) {
    locations->Push(*GetExceptionObjectDefinition());
    (*catch_block)->MergeInputDefinitions(*locations, worklist);
    locations->Pop();
  }

  for (std::size_t i = 0; i < GetValueDefinitionCount(); ++i) {
    locations->Push(GetValueDefinition(i));
  }
}

base::Optional<DefinitionLocation>
CallCsaMacroInstruction::GetExceptionObjectDefinition() const {
  if (!catch_block) return base::nullopt;
  return DefinitionLocation::Instruction(this, GetValueDefinitionCount());
}

std::size_t CallCsaMacroInstruction::GetValueDefinitionCount() const {
  return LowerType(macro->signature().return_type).size();
}

DefinitionLocation CallCsaMacroInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

267 268 269 270 271 272 273 274 275 276 277 278
std::ostream& operator<<(std::ostream& os,
                         const CallCsaMacroInstruction& instruction) {
  os << "CallCsaMacro " << instruction.macro->ReadableName();
  os << "(";
  PrintCommaSeparatedList(os, instruction.constexpr_arguments);
  os << ")";
  if (instruction.catch_block) {
    os << ", catch block " << (*instruction.catch_block)->id();
  }
  return os;
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
void CallCsaMacroAndBranchInstruction::TypeInstruction(
    Stack<const Type*>* stack, ControlFlowGraph* cfg) const {
  std::vector<const Type*> parameter_types =
      LowerParameterTypes(macro->signature().parameter_types);
  for (intptr_t i = parameter_types.size() - 1; i >= 0; --i) {
    const Type* arg_type = stack->Pop();
    const Type* parameter_type = parameter_types.back();
    parameter_types.pop_back();
    if (arg_type != parameter_type) {
      ReportError("parameter ", i, ": expected type ", *parameter_type,
                  " but found type ", *arg_type);
    }
  }

  if (label_blocks.size() != macro->signature().labels.size()) {
    ReportError("wrong number of labels");
  }
  for (size_t i = 0; i < label_blocks.size(); ++i) {
    Stack<const Type*> continuation_stack = *stack;
    continuation_stack.PushMany(
        LowerParameterTypes(macro->signature().labels[i].types));
    label_blocks[i]->SetInputTypes(std::move(continuation_stack));
  }

303 304 305 306
  if (macro->IsTransitioning()) {
    InvalidateTransientTypes(stack);
  }

307 308
  if (catch_block) {
    Stack<const Type*> catch_stack = *stack;
309
    catch_stack.Push(TypeOracle::GetJSAnyType());
310 311 312
    (*catch_block)->SetInputTypes(catch_stack);
  }

313 314 315 316 317 318 319 320 321 322 323 324 325 326
  if (macro->signature().return_type != TypeOracle::GetNeverType()) {
    Stack<const Type*> return_stack = *stack;
    return_stack.PushMany(LowerType(macro->signature().return_type));
    if (return_continuation == base::nullopt) {
      ReportError("missing return continuation.");
    }
    (*return_continuation)->SetInputTypes(return_stack);
  } else {
    if (return_continuation != base::nullopt) {
      ReportError("unreachable return continuation.");
    }
  }
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void CallCsaMacroAndBranchInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  auto parameter_types =
      LowerParameterTypes(macro->signature().parameter_types);
  locations->PopMany(parameter_types.size());

  for (std::size_t label_index = 0; label_index < label_blocks.size();
       ++label_index) {
    const std::size_t count = GetLabelValueDefinitionCount(label_index);
    for (std::size_t i = 0; i < count; ++i) {
      locations->Push(GetLabelValueDefinition(label_index, i));
    }
    label_blocks[label_index]->MergeInputDefinitions(*locations, worklist);
    locations->PopMany(count);
  }

  if (catch_block) {
    locations->Push(*GetExceptionObjectDefinition());
    (*catch_block)->MergeInputDefinitions(*locations, worklist);
    locations->Pop();
  }

  if (macro->signature().return_type != TypeOracle::GetNeverType()) {
    if (return_continuation) {
      const std::size_t count = GetValueDefinitionCount();
      for (std::size_t i = 0; i < count; ++i) {
        locations->Push(GetValueDefinition(i));
      }
      (*return_continuation)->MergeInputDefinitions(*locations, worklist);
      locations->PopMany(count);
    }
  }
}

std::size_t CallCsaMacroAndBranchInstruction::GetLabelCount() const {
  return label_blocks.size();
}

std::size_t CallCsaMacroAndBranchInstruction::GetLabelValueDefinitionCount(
    std::size_t label) const {
  DCHECK_LT(label, GetLabelCount());
  return LowerParameterTypes(macro->signature().labels[label].types).size();
}

DefinitionLocation CallCsaMacroAndBranchInstruction::GetLabelValueDefinition(
    std::size_t label, std::size_t index) const {
  DCHECK_LT(index, GetLabelValueDefinitionCount(label));
  std::size_t offset = GetValueDefinitionCount() + (catch_block ? 1 : 0);
  for (std::size_t label_index = 0; label_index < label; ++label_index) {
    offset += GetLabelValueDefinitionCount(label_index);
  }
  return DefinitionLocation::Instruction(this, offset + index);
}

std::size_t CallCsaMacroAndBranchInstruction::GetValueDefinitionCount() const {
  if (macro->signature().return_type == TypeOracle::GetNeverType()) return 0;
  if (!return_continuation) return 0;
  return LowerType(macro->signature().return_type).size();
}

DefinitionLocation CallCsaMacroAndBranchInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

base::Optional<DefinitionLocation>
CallCsaMacroAndBranchInstruction::GetExceptionObjectDefinition() const {
  if (!catch_block) return base::nullopt;
  return DefinitionLocation::Instruction(this, GetValueDefinitionCount());
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
std::ostream& operator<<(std::ostream& os,
                         const CallCsaMacroAndBranchInstruction& instruction) {
  os << "CallCsaMacroAndBranch " << instruction.macro->ReadableName();
  os << "(";
  PrintCommaSeparatedList(os, instruction.constexpr_arguments);
  os << ")";
  if (instruction.return_continuation) {
    os << ", return continuation " << (*instruction.return_continuation)->id();
  }
  if (!instruction.label_blocks.empty()) {
    os << ", label blocks ";
    PrintCommaSeparatedList(os, instruction.label_blocks,
                            [](Block* block) { return block->id(); });
  }
  if (instruction.catch_block) {
    os << ", catch block " << (*instruction.catch_block)->id();
  }
  return os;
}

419 420 421 422 423 424 425
void CallBuiltinInstruction::TypeInstruction(Stack<const Type*>* stack,
                                             ControlFlowGraph* cfg) const {
  std::vector<const Type*> argument_types = stack->PopMany(argc);
  if (argument_types !=
      LowerParameterTypes(builtin->signature().parameter_types)) {
    ReportError("wrong argument types");
  }
426 427 428
  if (builtin->IsTransitioning()) {
    InvalidateTransientTypes(stack);
  }
429 430 431

  if (catch_block) {
    Stack<const Type*> catch_stack = *stack;
432
    catch_stack.Push(TypeOracle::GetJSAnyType());
433 434 435
    (*catch_block)->SetInputTypes(catch_stack);
  }

436 437 438
  stack->PushMany(LowerType(builtin->signature().return_type));
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
void CallBuiltinInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->PopMany(argc);

  if (catch_block) {
    locations->Push(*GetExceptionObjectDefinition());
    (*catch_block)->MergeInputDefinitions(*locations, worklist);
    locations->Pop();
  }

  for (std::size_t i = 0; i < GetValueDefinitionCount(); ++i) {
    locations->Push(GetValueDefinition(i));
  }
}

std::size_t CallBuiltinInstruction::GetValueDefinitionCount() const {
  return LowerType(builtin->signature().return_type).size();
}

DefinitionLocation CallBuiltinInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

base::Optional<DefinitionLocation>
CallBuiltinInstruction::GetExceptionObjectDefinition() const {
  if (!catch_block) return base::nullopt;
  return DefinitionLocation::Instruction(this, GetValueDefinitionCount());
}

470 471 472
void CallBuiltinPointerInstruction::TypeInstruction(
    Stack<const Type*>* stack, ControlFlowGraph* cfg) const {
  std::vector<const Type*> argument_types = stack->PopMany(argc);
473
  const BuiltinPointerType* f = BuiltinPointerType::DynamicCast(stack->Pop());
474 475 476 477
  if (!f) ReportError("expected function pointer type");
  if (argument_types != LowerParameterTypes(f->parameter_types())) {
    ReportError("wrong argument types");
  }
478
  DCHECK_EQ(type, f);
479 480
  // TODO(turbofan): Only invalidate transient types if the function pointer
  // type is transitioning.
481
  InvalidateTransientTypes(stack);
482 483 484
  stack->PushMany(LowerType(f->return_type()));
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
void CallBuiltinPointerInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->PopMany(argc + 1);
  for (std::size_t i = 0; i < GetValueDefinitionCount(); ++i) {
    locations->Push(GetValueDefinition(i));
  }
}

std::size_t CallBuiltinPointerInstruction::GetValueDefinitionCount() const {
  return LowerType(type->return_type()).size();
}

DefinitionLocation CallBuiltinPointerInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

503 504 505 506 507 508 509 510 511 512 513 514 515
std::ostream& operator<<(std::ostream& os,
                         const CallBuiltinInstruction& instruction) {
  os << "CallBuiltin " << instruction.builtin->ReadableName()
     << ", argc: " << instruction.argc;
  if (instruction.is_tailcall) {
    os << ", is_tailcall";
  }
  if (instruction.catch_block) {
    os << ", catch block " << (*instruction.catch_block)->id();
  }
  return os;
}

516 517 518 519 520 521 522 523
void CallRuntimeInstruction::TypeInstruction(Stack<const Type*>* stack,
                                             ControlFlowGraph* cfg) const {
  std::vector<const Type*> argument_types = stack->PopMany(argc);
  if (argument_types !=
      LowerParameterTypes(runtime_function->signature().parameter_types,
                          argc)) {
    ReportError("wrong argument types");
  }
524 525 526
  if (runtime_function->IsTransitioning()) {
    InvalidateTransientTypes(stack);
  }
527 528 529

  if (catch_block) {
    Stack<const Type*> catch_stack = *stack;
530
    catch_stack.Push(TypeOracle::GetJSAnyType());
531 532 533
    (*catch_block)->SetInputTypes(catch_stack);
  }

534 535 536 537
  const Type* return_type = runtime_function->signature().return_type;
  if (return_type != TypeOracle::GetNeverType()) {
    stack->PushMany(LowerType(return_type));
  }
538 539
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
void CallRuntimeInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->PopMany(argc);

  if (catch_block) {
    locations->Push(*GetExceptionObjectDefinition());
    (*catch_block)->MergeInputDefinitions(*locations, worklist);
    locations->Pop();
  }

  const Type* return_type = runtime_function->signature().return_type;
  if (return_type != TypeOracle::GetNeverType()) {
    for (std::size_t i = 0; i < GetValueDefinitionCount(); ++i) {
      locations->Push(GetValueDefinition(i));
    }
  }
}

std::size_t CallRuntimeInstruction::GetValueDefinitionCount() const {
  const Type* return_type = runtime_function->signature().return_type;
  if (return_type == TypeOracle::GetNeverType()) return 0;
  return LowerType(return_type).size();
}

DefinitionLocation CallRuntimeInstruction::GetValueDefinition(
    std::size_t index) const {
  DCHECK_LT(index, GetValueDefinitionCount());
  return DefinitionLocation::Instruction(this, index);
}

base::Optional<DefinitionLocation>
CallRuntimeInstruction::GetExceptionObjectDefinition() const {
  if (!catch_block) return base::nullopt;
  return DefinitionLocation::Instruction(this, GetValueDefinitionCount());
}

576 577 578 579 580 581 582 583 584 585 586 587 588
std::ostream& operator<<(std::ostream& os,
                         const CallRuntimeInstruction& instruction) {
  os << "CallRuntime " << instruction.runtime_function->ReadableName()
     << ", argc: " << instruction.argc;
  if (instruction.is_tailcall) {
    os << ", is_tailcall";
  }
  if (instruction.catch_block) {
    os << ", catch block " << (*instruction.catch_block)->id();
  }
  return os;
}

589 590 591 592 593 594 595 596 597 598
void BranchInstruction::TypeInstruction(Stack<const Type*>* stack,
                                        ControlFlowGraph* cfg) const {
  const Type* condition_type = stack->Pop();
  if (condition_type != TypeOracle::GetBoolType()) {
    ReportError("condition has to have type bool");
  }
  if_true->SetInputTypes(*stack);
  if_false->SetInputTypes(*stack);
}

599 600 601 602 603 604 605
void BranchInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Pop();
  if_true->MergeInputDefinitions(*locations, worklist);
  if_false->MergeInputDefinitions(*locations, worklist);
}

606 607 608 609 610 611
std::ostream& operator<<(std::ostream& os,
                         const BranchInstruction& instruction) {
  return os << "Branch true: " << instruction.if_true->id()
            << ", false: " << instruction.if_false->id();
}

612 613 614 615 616 617
void ConstexprBranchInstruction::TypeInstruction(Stack<const Type*>* stack,
                                                 ControlFlowGraph* cfg) const {
  if_true->SetInputTypes(*stack);
  if_false->SetInputTypes(*stack);
}

618 619 620 621 622 623
void ConstexprBranchInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  if_true->MergeInputDefinitions(*locations, worklist);
  if_false->MergeInputDefinitions(*locations, worklist);
}

624 625 626 627 628 629 630
std::ostream& operator<<(std::ostream& os,
                         const ConstexprBranchInstruction& instruction) {
  return os << "ConstexprBranch " << instruction.condition
            << ", true: " << instruction.if_true->id()
            << ", false: " << instruction.if_false->id();
}

631 632 633 634 635
void GotoInstruction::TypeInstruction(Stack<const Type*>* stack,
                                      ControlFlowGraph* cfg) const {
  destination->SetInputTypes(*stack);
}

636 637 638 639 640
void GotoInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  destination->MergeInputDefinitions(*locations, worklist);
}

641 642 643 644
std::ostream& operator<<(std::ostream& os, const GotoInstruction& instruction) {
  return os << "Goto " << instruction.destination->id();
}

645 646 647 648 649 650 651
void GotoExternalInstruction::TypeInstruction(Stack<const Type*>* stack,
                                              ControlFlowGraph* cfg) const {
  if (variable_names.size() != stack->Size()) {
    ReportError("goto external label with wrong parameter count.");
  }
}

652 653 654
void GotoExternalInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {}

655 656
void ReturnInstruction::TypeInstruction(Stack<const Type*>* stack,
                                        ControlFlowGraph* cfg) const {
657
  cfg->SetReturnType(stack->PopMany(count));
658 659
}

660 661
void ReturnInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
662
  locations->PopMany(count);
663 664
}

665 666 667
void PrintConstantStringInstruction::TypeInstruction(
    Stack<const Type*>* stack, ControlFlowGraph* cfg) const {}

668 669 670
void PrintConstantStringInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {}

671 672
void AbortInstruction::TypeInstruction(Stack<const Type*>* stack,
                                       ControlFlowGraph* cfg) const {}
673

674 675 676
void AbortInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {}

677 678 679 680 681
void UnsafeCastInstruction::TypeInstruction(Stack<const Type*>* stack,
                                            ControlFlowGraph* cfg) const {
  stack->Poke(stack->AboveTop() - 1, destination_type);
}

682 683 684 685 686 687 688 689 690
void UnsafeCastInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Poke(locations->AboveTop() - 1, GetValueDefinition());
}

DefinitionLocation UnsafeCastInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

691 692 693
void LoadReferenceInstruction::TypeInstruction(Stack<const Type*>* stack,
                                               ControlFlowGraph* cfg) const {
  ExpectType(TypeOracle::GetIntPtrType(), stack->Pop());
694 695 696
  ExpectSubtype(stack->Pop(), TypeOracle::GetUnionType(
                                  TypeOracle::GetHeapObjectType(),
                                  TypeOracle::GetTaggedZeroPatternType()));
697 698 699 700
  DCHECK_EQ(std::vector<const Type*>{type}, LowerType(type));
  stack->Push(type);
}

701 702 703 704 705 706 707 708 709 710 711
void LoadReferenceInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Pop();
  locations->Pop();
  locations->Push(GetValueDefinition());
}

DefinitionLocation LoadReferenceInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

712 713 714 715
void StoreReferenceInstruction::TypeInstruction(Stack<const Type*>* stack,
                                                ControlFlowGraph* cfg) const {
  ExpectSubtype(stack->Pop(), type);
  ExpectType(TypeOracle::GetIntPtrType(), stack->Pop());
716 717 718
  ExpectSubtype(stack->Pop(), TypeOracle::GetUnionType(
                                  TypeOracle::GetHeapObjectType(),
                                  TypeOracle::GetTaggedZeroPatternType()));
719 720
}

721 722 723 724 725 726 727
void StoreReferenceInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Pop();
  locations->Pop();
  locations->Pop();
}

728 729 730 731 732 733
void LoadBitFieldInstruction::TypeInstruction(Stack<const Type*>* stack,
                                              ControlFlowGraph* cfg) const {
  ExpectType(bit_field_struct_type, stack->Pop());
  stack->Push(bit_field.name_and_type.type);
}

734 735 736 737 738 739 740 741 742 743
void LoadBitFieldInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Pop();
  locations->Push(GetValueDefinition());
}

DefinitionLocation LoadBitFieldInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

744 745 746 747 748 749 750
void StoreBitFieldInstruction::TypeInstruction(Stack<const Type*>* stack,
                                               ControlFlowGraph* cfg) const {
  ExpectSubtype(bit_field.name_and_type.type, stack->Pop());
  ExpectType(bit_field_struct_type, stack->Pop());
  stack->Push(bit_field_struct_type);
}

751 752 753 754 755 756 757 758 759 760 761
void StoreBitFieldInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  locations->Pop();
  locations->Pop();
  locations->Push(GetValueDefinition());
}

DefinitionLocation StoreBitFieldInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
void MakeLazyNodeInstruction::TypeInstruction(Stack<const Type*>* stack,
                                              ControlFlowGraph* cfg) const {
  std::vector<const Type*> parameter_types =
      LowerParameterTypes(macro->signature().parameter_types);
  for (intptr_t i = parameter_types.size() - 1; i >= 0; --i) {
    const Type* arg_type = stack->Pop();
    const Type* parameter_type = parameter_types.back();
    parameter_types.pop_back();
    if (arg_type != parameter_type) {
      ReportError("parameter ", i, ": expected type ", *parameter_type,
                  " but found type ", *arg_type);
    }
  }

  stack->Push(result_type);
}

void MakeLazyNodeInstruction::RecomputeDefinitionLocations(
    Stack<DefinitionLocation>* locations, Worklist<Block*>* worklist) const {
  auto parameter_types =
      LowerParameterTypes(macro->signature().parameter_types);
  locations->PopMany(parameter_types.size());

  locations->Push(GetValueDefinition());
}

DefinitionLocation MakeLazyNodeInstruction::GetValueDefinition() const {
  return DefinitionLocation::Instruction(this, 0);
}

792 793 794 795 796 797 798 799 800 801
std::ostream& operator<<(std::ostream& os,
                         const MakeLazyNodeInstruction& instruction) {
  os << "MakeLazyNode " << instruction.macro->ReadableName() << ", "
     << *instruction.result_type;
  for (const std::string& arg : instruction.constexpr_arguments) {
    os << ", " << arg;
  }
  return os;
}

802 803 804 805 806
bool CallRuntimeInstruction::IsBlockTerminator() const {
  return is_tailcall || runtime_function->signature().return_type ==
                            TypeOracle::GetNeverType();
}

807 808 809
}  // namespace torque
}  // namespace internal
}  // namespace v8