Line data Source code
1 : // Copyright 2017 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_INTERPRETER_BLOCK_COVERAGE_BUILDER_H_
6 : #define V8_INTERPRETER_BLOCK_COVERAGE_BUILDER_H_
7 :
8 : #include "src/ast/ast-source-ranges.h"
9 : #include "src/interpreter/bytecode-array-builder.h"
10 :
11 : #include "src/zone/zone-containers.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 : namespace interpreter {
16 :
17 : // Used to generate IncBlockCounter bytecodes and the {source range, slot}
18 : // mapping for block coverage.
19 : class BlockCoverageBuilder final : public ZoneObject {
20 : public:
21 : BlockCoverageBuilder(Zone* zone, BytecodeArrayBuilder* builder,
22 : SourceRangeMap* source_range_map)
23 : : slots_(0, zone),
24 : builder_(builder),
25 892 : source_range_map_(source_range_map) {
26 : DCHECK_NOT_NULL(builder);
27 : DCHECK_NOT_NULL(source_range_map);
28 : }
29 :
30 : static constexpr int kNoCoverageArraySlot = -1;
31 :
32 4644 : int AllocateBlockCoverageSlot(ZoneObject* node, SourceRangeKind kind) {
33 4644 : AstNodeSourceRanges* ranges = source_range_map_->Find(node);
34 4644 : if (ranges == nullptr) return kNoCoverageArraySlot;
35 :
36 3592 : SourceRange range = ranges->GetRange(kind);
37 3592 : if (range.IsEmpty()) return kNoCoverageArraySlot;
38 :
39 2024 : const int slot = static_cast<int>(slots_.size());
40 2024 : slots_.emplace_back(range);
41 2024 : return slot;
42 : }
43 :
44 244 : int AllocateNaryBlockCoverageSlot(NaryOperation* node, size_t index) {
45 : NaryOperationSourceRanges* ranges =
46 244 : static_cast<NaryOperationSourceRanges*>(source_range_map_->Find(node));
47 244 : if (ranges == nullptr) return kNoCoverageArraySlot;
48 :
49 244 : SourceRange range = ranges->GetRangeAtIndex(index);
50 244 : if (range.IsEmpty()) return kNoCoverageArraySlot;
51 :
52 244 : const int slot = static_cast<int>(slots_.size());
53 244 : slots_.emplace_back(range);
54 244 : return slot;
55 : }
56 :
57 : void IncrementBlockCounter(int coverage_array_slot) {
58 3480 : if (coverage_array_slot == kNoCoverageArraySlot) return;
59 2032 : builder_->IncBlockCounter(coverage_array_slot);
60 : }
61 :
62 2344 : void IncrementBlockCounter(ZoneObject* node, SourceRangeKind kind) {
63 2344 : int slot = AllocateBlockCoverageSlot(node, kind);
64 : IncrementBlockCounter(slot);
65 2344 : }
66 :
67 892 : const ZoneVector<SourceRange>& slots() const { return slots_; }
68 :
69 : private:
70 : // Contains source range information for allocated block coverage counter
71 : // slots. Slot i covers range slots_[i].
72 : ZoneVector<SourceRange> slots_;
73 : BytecodeArrayBuilder* builder_;
74 : SourceRangeMap* source_range_map_;
75 : };
76 :
77 : } // namespace interpreter
78 : } // namespace internal
79 : } // namespace v8
80 :
81 : #endif // V8_INTERPRETER_BLOCK_COVERAGE_BUILDER_H_
|