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#ifndef V8_INTERPRETER_HANDLER_TABLE_BUILDER_H_
61cb0ef41Sopenharmony_ci#define V8_INTERPRETER_HANDLER_TABLE_BUILDER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/codegen/handler-table.h"
91cb0ef41Sopenharmony_ci#include "src/interpreter/bytecode-register.h"
101cb0ef41Sopenharmony_ci#include "src/interpreter/bytecodes.h"
111cb0ef41Sopenharmony_ci#include "src/objects/fixed-array.h"
121cb0ef41Sopenharmony_ci#include "src/zone/zone-containers.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace internal {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_citemplate <typename T>
181cb0ef41Sopenharmony_ciclass Handle;
191cb0ef41Sopenharmony_ciclass HandlerTable;
201cb0ef41Sopenharmony_ciclass Isolate;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cinamespace interpreter {
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// A helper class for constructing exception handler tables for the interpreter.
251cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE HandlerTableBuilder final {
261cb0ef41Sopenharmony_ci public:
271cb0ef41Sopenharmony_ci  explicit HandlerTableBuilder(Zone* zone);
281cb0ef41Sopenharmony_ci  HandlerTableBuilder(const HandlerTableBuilder&) = delete;
291cb0ef41Sopenharmony_ci  HandlerTableBuilder& operator=(const HandlerTableBuilder&) = delete;
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Builds the actual handler table by copying the current values into a heap
321cb0ef41Sopenharmony_ci  // object. Any further mutations to the builder won't be reflected.
331cb0ef41Sopenharmony_ci  template <typename IsolateT>
341cb0ef41Sopenharmony_ci  Handle<ByteArray> ToHandlerTable(IsolateT* isolate);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Creates a new handler table entry and returns a {hander_id} identifying the
371cb0ef41Sopenharmony_ci  // entry, so that it can be referenced by below setter functions.
381cb0ef41Sopenharmony_ci  int NewHandlerEntry();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  // Setter functions that modify certain values within the handler table entry
411cb0ef41Sopenharmony_ci  // being referenced by the given {handler_id}. All values will be encoded by
421cb0ef41Sopenharmony_ci  // the resulting {HandlerTable} class when copied into the heap.
431cb0ef41Sopenharmony_ci  void SetTryRegionStart(int handler_id, size_t offset);
441cb0ef41Sopenharmony_ci  void SetTryRegionEnd(int handler_id, size_t offset);
451cb0ef41Sopenharmony_ci  void SetHandlerTarget(int handler_id, size_t offset);
461cb0ef41Sopenharmony_ci  void SetPrediction(int handler_id, HandlerTable::CatchPrediction prediction);
471cb0ef41Sopenharmony_ci  void SetContextRegister(int handler_id, Register reg);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci private:
501cb0ef41Sopenharmony_ci  struct Entry {
511cb0ef41Sopenharmony_ci    size_t offset_start;   // Bytecode offset starting try-region.
521cb0ef41Sopenharmony_ci    size_t offset_end;     // Bytecode offset ending try-region.
531cb0ef41Sopenharmony_ci    size_t offset_target;  // Bytecode offset of handler target.
541cb0ef41Sopenharmony_ci    Register context;      // Register holding context for handler.
551cb0ef41Sopenharmony_ci                           // Optimistic prediction for handler.
561cb0ef41Sopenharmony_ci    HandlerTable::CatchPrediction catch_prediction_;
571cb0ef41Sopenharmony_ci  };
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  ZoneVector<Entry> entries_;
601cb0ef41Sopenharmony_ci};
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci}  // namespace interpreter
631cb0ef41Sopenharmony_ci}  // namespace internal
641cb0ef41Sopenharmony_ci}  // namespace v8
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci#endif  // V8_INTERPRETER_HANDLER_TABLE_BUILDER_H_
67