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/factory.h" 8 #include "src/isolate.h" 9 #include "src/objects-inl.h" 10 11 namespace v8 { 12 namespace internal { 13 namespace interpreter { 14 15 HandlerTableBuilder::HandlerTableBuilder(Isolate* isolate, Zone* zone) 16 : isolate_(isolate), entries_(zone) {} 17 18 Handle<HandlerTable> HandlerTableBuilder::ToHandlerTable() { 19 int handler_table_size = static_cast<int>(entries_.size()); 20 Handle<HandlerTable> table = 21 Handle<HandlerTable>::cast(isolate_->factory()->NewFixedArray( 22 HandlerTable::LengthForRange(handler_table_size), TENURED)); 23 for (int i = 0; i < handler_table_size; ++i) { 24 Entry& entry = entries_[i]; 25 HandlerTable::CatchPrediction pred = 26 entry.will_catch ? HandlerTable::CAUGHT : HandlerTable::UNCAUGHT; 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; 33 } 34 35 36 int HandlerTableBuilder::NewHandlerEntry() { 37 int handler_id = static_cast<int>(entries_.size()); 38 Entry entry = {0, 0, 0, Register(), false}; 39 entries_.push_back(entry); 40 return handler_id; 41 } 42 43 44 void HandlerTableBuilder::SetTryRegionStart(int handler_id, size_t offset) { 45 DCHECK(Smi::IsValid(offset)); // Encoding of handler table requires this. 46 entries_[handler_id].offset_start = offset; 47 } 48 49 50 void HandlerTableBuilder::SetTryRegionEnd(int handler_id, size_t offset) { 51 DCHECK(Smi::IsValid(offset)); // Encoding of handler table requires this. 52 entries_[handler_id].offset_end = offset; 53 } 54 55 56 void HandlerTableBuilder::SetHandlerTarget(int handler_id, size_t offset) { 57 DCHECK(Smi::IsValid(offset)); // Encoding of handler table requires this. 58 entries_[handler_id].offset_target = offset; 59 } 60 61 62 void HandlerTableBuilder::SetPrediction(int handler_id, bool will_catch) { 63 entries_[handler_id].will_catch = will_catch; 64 } 65 66 67 void HandlerTableBuilder::SetContextRegister(int handler_id, Register reg) { 68 entries_[handler_id].context = reg; 69 } 70 71 } // namespace interpreter 72 } // namespace internal 73 } // namespace v8 74