11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/interpreter/handler-table-builder.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/execution/isolate.h"
81cb0ef41Sopenharmony_ci#include "src/heap/factory.h"
91cb0ef41Sopenharmony_ci#include "src/interpreter/bytecode-register.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace v8 {
131cb0ef41Sopenharmony_cinamespace internal {
141cb0ef41Sopenharmony_cinamespace interpreter {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciHandlerTableBuilder::HandlerTableBuilder(Zone* zone) : entries_(zone) {}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_citemplate <typename IsolateT>
191cb0ef41Sopenharmony_ciHandle<ByteArray> HandlerTableBuilder::ToHandlerTable(IsolateT* isolate) {
201cb0ef41Sopenharmony_ci  int handler_table_size = static_cast<int>(entries_.size());
211cb0ef41Sopenharmony_ci  Handle<ByteArray> table_byte_array = isolate->factory()->NewByteArray(
221cb0ef41Sopenharmony_ci      HandlerTable::LengthForRange(handler_table_size), AllocationType::kOld);
231cb0ef41Sopenharmony_ci  HandlerTable table(*table_byte_array);
241cb0ef41Sopenharmony_ci  for (int i = 0; i < handler_table_size; ++i) {
251cb0ef41Sopenharmony_ci    Entry& entry = entries_[i];
261cb0ef41Sopenharmony_ci    HandlerTable::CatchPrediction pred = entry.catch_prediction_;
271cb0ef41Sopenharmony_ci    table.SetRangeStart(i, static_cast<int>(entry.offset_start));
281cb0ef41Sopenharmony_ci    table.SetRangeEnd(i, static_cast<int>(entry.offset_end));
291cb0ef41Sopenharmony_ci    table.SetRangeHandler(i, static_cast<int>(entry.offset_target), pred);
301cb0ef41Sopenharmony_ci    table.SetRangeData(i, entry.context.index());
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci  return table_byte_array;
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_citemplate Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(
361cb0ef41Sopenharmony_ci    Isolate* isolate);
371cb0ef41Sopenharmony_citemplate Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(
381cb0ef41Sopenharmony_ci    LocalIsolate* isolate);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciint HandlerTableBuilder::NewHandlerEntry() {
411cb0ef41Sopenharmony_ci  int handler_id = static_cast<int>(entries_.size());
421cb0ef41Sopenharmony_ci  Entry entry = {0, 0, 0, Register::invalid_value(), HandlerTable::UNCAUGHT};
431cb0ef41Sopenharmony_ci  entries_.push_back(entry);
441cb0ef41Sopenharmony_ci  return handler_id;
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_civoid HandlerTableBuilder::SetTryRegionStart(int handler_id, size_t offset) {
491cb0ef41Sopenharmony_ci  DCHECK(Smi::IsValid(offset));  // Encoding of handler table requires this.
501cb0ef41Sopenharmony_ci  entries_[handler_id].offset_start = offset;
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_civoid HandlerTableBuilder::SetTryRegionEnd(int handler_id, size_t offset) {
551cb0ef41Sopenharmony_ci  DCHECK(Smi::IsValid(offset));  // Encoding of handler table requires this.
561cb0ef41Sopenharmony_ci  entries_[handler_id].offset_end = offset;
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_civoid HandlerTableBuilder::SetHandlerTarget(int handler_id, size_t offset) {
611cb0ef41Sopenharmony_ci  DCHECK(Smi::IsValid(offset));  // Encoding of handler table requires this.
621cb0ef41Sopenharmony_ci  entries_[handler_id].offset_target = offset;
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_civoid HandlerTableBuilder::SetPrediction(
661cb0ef41Sopenharmony_ci    int handler_id, HandlerTable::CatchPrediction prediction) {
671cb0ef41Sopenharmony_ci  entries_[handler_id].catch_prediction_ = prediction;
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_civoid HandlerTableBuilder::SetContextRegister(int handler_id, Register reg) {
721cb0ef41Sopenharmony_ci  entries_[handler_id].context = reg;
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci}  // namespace interpreter
761cb0ef41Sopenharmony_ci}  // namespace internal
771cb0ef41Sopenharmony_ci}  // namespace v8
78