handler-table-builder.cc 2.61 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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/handler-table-builder.h"

7
#include "src/execution/isolate.h"
8
#include "src/heap/factory.h"
9
#include "src/interpreter/bytecode-register.h"
10
#include "src/objects/objects-inl.h"
11 12 13 14 15

namespace v8 {
namespace internal {
namespace interpreter {

16
HandlerTableBuilder::HandlerTableBuilder(Zone* zone) : entries_(zone) {}
17

18 19
template <typename IsolateT>
Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(IsolateT* isolate) {
20
  int handler_table_size = static_cast<int>(entries_.size());
21 22
  Handle<ByteArray> table_byte_array = isolate->factory()->NewByteArray(
      HandlerTable::LengthForRange(handler_table_size), AllocationType::kOld);
23
  HandlerTable table(*table_byte_array);
24 25
  for (int i = 0; i < handler_table_size; ++i) {
    Entry& entry = entries_[i];
26
    HandlerTable::CatchPrediction pred = entry.catch_prediction_;
27 28 29 30
    table.SetRangeStart(i, static_cast<int>(entry.offset_start));
    table.SetRangeEnd(i, static_cast<int>(entry.offset_end));
    table.SetRangeHandler(i, static_cast<int>(entry.offset_target), pred);
    table.SetRangeData(i, entry.context.index());
31
  }
32
  return table_byte_array;
33 34
}

35 36
template Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(
    Isolate* isolate);
37
template Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(
38
    LocalIsolate* isolate);
39 40 41

int HandlerTableBuilder::NewHandlerEntry() {
  int handler_id = static_cast<int>(entries_.size());
42
  Entry entry = {0, 0, 0, Register::invalid_value(), HandlerTable::UNCAUGHT};
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  entries_.push_back(entry);
  return handler_id;
}


void HandlerTableBuilder::SetTryRegionStart(int handler_id, size_t offset) {
  DCHECK(Smi::IsValid(offset));  // Encoding of handler table requires this.
  entries_[handler_id].offset_start = offset;
}


void HandlerTableBuilder::SetTryRegionEnd(int handler_id, size_t offset) {
  DCHECK(Smi::IsValid(offset));  // Encoding of handler table requires this.
  entries_[handler_id].offset_end = offset;
}


void HandlerTableBuilder::SetHandlerTarget(int handler_id, size_t offset) {
  DCHECK(Smi::IsValid(offset));  // Encoding of handler table requires this.
  entries_[handler_id].offset_target = offset;
}

65 66 67
void HandlerTableBuilder::SetPrediction(
    int handler_id, HandlerTable::CatchPrediction prediction) {
  entries_[handler_id].catch_prediction_ = prediction;
68 69 70 71 72 73 74 75 76 77
}


void HandlerTableBuilder::SetContextRegister(int handler_id, Register reg) {
  entries_[handler_id].context = reg;
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8