1 // Copyright 2016 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 #include "src/interpreter/handler-table-builder.h"
6
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 namespace v8 {
13 namespace internal {
14 namespace interpreter {
15
HandlerTableBuilder(Zone * zone)16 HandlerTableBuilder::HandlerTableBuilder(Zone* zone) : entries_(zone) {}
17
18 template <typename IsolateT>
ToHandlerTable(IsolateT * isolate)19 Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(IsolateT* isolate) {
20 int handler_table_size = static_cast<int>(entries_.size());
21 Handle<ByteArray> table_byte_array = isolate->factory()->NewByteArray(
22 HandlerTable::LengthForRange(handler_table_size), AllocationType::kOld);
23 HandlerTable table(*table_byte_array);
24 for (int i = 0; i < handler_table_size; ++i) {
25 Entry& entry = entries_[i];
26 HandlerTable::CatchPrediction pred = entry.catch_prediction_;
27 table.SetRangeStart(i, static_cast<int>(entry.offset_start));
28 table.SetRangeEnd(i, static_cast<int>(entry.offset_end));
29 table.SetRangeHandler(i, static_cast<int>(entry.offset_target), pred);
30 table.SetRangeData(i, entry.context.index());
31 }
32 return table_byte_array;
33 }
34
35 template Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(
36 Isolate* isolate);
37 template Handle<ByteArray> HandlerTableBuilder::ToHandlerTable(
38 LocalIsolate* isolate);
39
NewHandlerEntry()40 int HandlerTableBuilder::NewHandlerEntry() {
41 int handler_id = static_cast<int>(entries_.size());
42 Entry entry = {0, 0, 0, Register::invalid_value(), HandlerTable::UNCAUGHT};
43 entries_.push_back(entry);
44 return handler_id;
45 }
46
47
SetTryRegionStart(int handler_id,size_t offset)48 void HandlerTableBuilder::SetTryRegionStart(int handler_id, size_t offset) {
49 DCHECK(Smi::IsValid(offset)); // Encoding of handler table requires this.
50 entries_[handler_id].offset_start = offset;
51 }
52
53
SetTryRegionEnd(int handler_id,size_t offset)54 void HandlerTableBuilder::SetTryRegionEnd(int handler_id, size_t offset) {
55 DCHECK(Smi::IsValid(offset)); // Encoding of handler table requires this.
56 entries_[handler_id].offset_end = offset;
57 }
58
59
SetHandlerTarget(int handler_id,size_t offset)60 void HandlerTableBuilder::SetHandlerTarget(int handler_id, size_t offset) {
61 DCHECK(Smi::IsValid(offset)); // Encoding of handler table requires this.
62 entries_[handler_id].offset_target = offset;
63 }
64
SetPrediction(int handler_id,HandlerTable::CatchPrediction prediction)65 void HandlerTableBuilder::SetPrediction(
66 int handler_id, HandlerTable::CatchPrediction prediction) {
67 entries_[handler_id].catch_prediction_ = prediction;
68 }
69
70
SetContextRegister(int handler_id,Register reg)71 void HandlerTableBuilder::SetContextRegister(int handler_id, Register reg) {
72 entries_[handler_id].context = reg;
73 }
74
75 } // namespace interpreter
76 } // namespace internal
77 } // namespace v8
78