v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
handler-table.h
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_CODEGEN_HANDLER_TABLE_H_
6#define V8_CODEGEN_HANDLER_TABLE_H_
7
10#include "src/common/globals.h"
11
12namespace v8 {
13namespace internal {
14
15class Assembler;
17class BytecodeArray;
18class InstructionStream;
19class Code;
20
21namespace wasm {
22class WasmCode;
23} // namespace wasm
24
25// HandlerTable is a byte array containing entries for exception handlers in
26// the code object it is associated with. The tables come in two flavors:
27// 1) Based on ranges: Used for unoptimized code. Stored in a
28// {TrustedByteArray} that is attached to each {BytecodeArray}. Contains one
29// entry per exception handler and a range representing the try-block covered
30// by that handler. Layout looks as follows:
31// [ range-start , range-end , handler-offset , handler-data ]
32// 2) Based on return addresses: Used for turbofanned code. Stored directly in
33// the instruction stream of the {InstructionStream} object. Contains one
34// entry per call-site that could throw an exception. Layout looks as
35// follows:
36// [ return-address-offset , handler-offset ]
38 public:
39 // Conservative prediction whether a given handler will locally catch an
40 // exception or cause a re-throw to outside the code boundary. Since this is
41 // undecidable it is merely an approximation (e.g. useful for debugger).
43 UNCAUGHT, // The handler will (likely) rethrow the exception.
44 CAUGHT, // The exception will be caught by the handler.
45 PROMISE, // The exception will be caught and cause a promise rejection.
46 ASYNC_AWAIT, // The exception will be caught and cause a promise rejection
47 // in the desugaring of an async function, so special
48 // async/await handling in the debugger can take place.
49 UNCAUGHT_ASYNC_AWAIT, // The exception will be caught and cause a promise
50 // rejection in the desugaring of an async REPL
51 // script. The corresponding message object needs to
52 // be kept alive on the Isolate though.
53 };
54
55 enum EncodingMode { kRangeBasedEncoding, kReturnAddressBasedEncoding };
56
57 // Constructors for the various encodings.
59 explicit HandlerTable(Tagged<Code> code);
60 explicit HandlerTable(Tagged<TrustedByteArray> byte_array);
61#if V8_ENABLE_WEBASSEMBLY
62 explicit HandlerTable(const wasm::WasmCode* code);
63#endif // V8_ENABLE_WEBASSEMBLY
64 explicit HandlerTable(Tagged<BytecodeArray> bytecode_array);
65 HandlerTable(Address handler_table, int handler_table_size,
66 EncodingMode encoding_mode);
67
68 // Getters for handler table based on ranges.
69 int GetRangeStart(int index) const;
70 int GetRangeEnd(int index) const;
71 int GetRangeHandler(int index) const;
72 int GetRangeData(int index) const;
73
74 // Setters for handler table based on ranges.
75 void SetRangeStart(int index, int value);
76 void SetRangeEnd(int index, int value);
77 void SetRangeHandler(int index, int offset, CatchPrediction pred);
78 void SetRangeData(int index, int value);
79
80 // Returns the required length of the underlying byte array.
81 static int LengthForRange(int entries);
82
83 // Emitters for handler table based on return addresses.
84 static int EmitReturnTableStart(Assembler* masm);
85 static void EmitReturnEntry(Assembler* masm, int offset, int handler);
86
87 // Lookup handler in a table based on ranges. The {pc_offset} is an offset to
88 // the start of the potentially throwing instruction (using return addresses
89 // for this value would be invalid).
90 int LookupHandlerIndexForRange(int pc_offset) const;
91
92 // Lookup handler in a table based on return addresses.
93 int LookupReturn(int pc_offset);
94
95 // Returns the number of entries in the table.
96 int NumberOfRangeEntries() const;
97 int NumberOfReturnEntries() const;
98
99#ifdef ENABLE_DISASSEMBLER
100 void HandlerTableRangePrint(std::ostream& os);
101 void HandlerTableReturnPrint(std::ostream& os);
102#endif
103
104 bool HandlerWasUsed(int index) const;
105 void MarkHandlerUsed(int index);
106 // Getters for handler table based on ranges.
107 CatchPrediction GetRangePrediction(int index) const;
108
109 static const int kNoHandlerFound = -1;
110
111 private:
112 // Gets entry size based on mode.
113 static int EntrySizeFromMode(EncodingMode mode);
114 int GetRangeHandlerBitfield(int index) const;
115
116 // Getters for handler table based on return addresses.
117 int GetReturnOffset(int index) const;
118 int GetReturnHandler(int index) const;
119
120 // Number of entries in the loaded handler table.
122
123#ifdef DEBUG
124 // The encoding mode of the table. Mostly useful for debugging to check that
125 // used accessors and constructors fit together.
126 const EncodingMode mode_;
127#endif
128
129 // Direct pointer into the encoded data. This pointer potentially points into
130 // objects on the GC heap (either {TrustedByteArray} or {InstructionStream})
131 // and could become stale during a collection. Hence we disallow any
132 // allocation.
133 const Address raw_encoded_data_;
135
136 // Layout description for handler table based on ranges.
137 static const int kRangeStartIndex = 0;
138 static const int kRangeEndIndex = 1;
139 static const int kRangeHandlerIndex = 2;
140 static const int kRangeDataIndex = 3;
141 static const int kRangeEntrySize = 4;
142
143 // Layout description for handler table based on return addresses.
144 static const int kReturnOffsetIndex = 0;
145 static const int kReturnHandlerIndex = 1;
146 static const int kReturnEntrySize = 2;
147
148 // Encoding of the {handler} field.
152
153 public:
154 static const int kLazyDeopt = HandlerOffsetField::kMax;
155};
156
157} // namespace internal
158} // namespace v8
159
160#endif // V8_CODEGEN_HANDLER_TABLE_H_
#define DISALLOW_GARBAGE_COLLECTION(name)
HandlerTable(Tagged< InstructionStream > code)
RecordWriteMode const mode_
DisallowGarbageCollection no_gc_
int32_t offset
ZoneVector< Entry > entries
int pc_offset
Definition c-api.cc:87
#define V8_EXPORT_PRIVATE
Definition macros.h:460