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 781 : 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 5252 : int AllocateBlockCoverageSlot(ZoneObject* node, SourceRangeKind kind) {
33 5252 : AstNodeSourceRanges* ranges = source_range_map_->Find(node);
34 5252 : if (ranges == nullptr) return kNoCoverageArraySlot;
35 :
36 3542 : SourceRange range = ranges->GetRange(kind);
37 3542 : if (range.IsEmpty()) return kNoCoverageArraySlot;
38 :
39 6338 : const int slot = static_cast<int>(slots_.size());
40 3169 : slots_.emplace_back(range);
41 3169 : return slot;
42 : }
43 :
44 : void IncrementBlockCounter(int coverage_array_slot) {
45 3718 : if (coverage_array_slot == kNoCoverageArraySlot) return;
46 2464 : builder_->IncBlockCounter(coverage_array_slot);
47 : }
48 :
49 2353 : void IncrementBlockCounter(ZoneObject* node, SourceRangeKind kind) {
50 2353 : int slot = AllocateBlockCoverageSlot(node, kind);
51 : IncrementBlockCounter(slot);
52 2353 : }
53 :
54 : const ZoneVector<SourceRange>& slots() const { return slots_; }
55 :
56 : private:
57 : // Contains source range information for allocated block coverage counter
58 : // slots. Slot i covers range slots_[i].
59 : ZoneVector<SourceRange> slots_;
60 : BytecodeArrayBuilder* builder_;
61 : SourceRangeMap* source_range_map_;
62 : };
63 :
64 : } // namespace interpreter
65 : } // namespace internal
66 : } // namespace v8
67 :
68 : #endif // V8_INTERPRETER_BLOCK_COVERAGE_BUILDER_H_
|