clang 22.0.0git
InterpBlock.cpp
Go to the documentation of this file.
1//===--- Block.cpp - Allocated blocks for the interpreter -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines the classes describing allocated blocks.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InterpBlock.h"
14#include "Pointer.h"
15
16using namespace clang;
17using namespace clang::interp;
18
19void Block::addPointer(Pointer *P) {
20 assert(P);
21
22#ifndef NDEBUG
23 assert(!hasPointer(P));
24#endif
25 if (Pointers)
26 Pointers->BS.Prev = P;
27 P->BS.Next = Pointers;
28 P->BS.Prev = nullptr;
29 Pointers = P;
30#ifndef NDEBUG
31 assert(hasPointer(P));
32#endif
33}
34
35void Block::removePointer(Pointer *P) {
36 assert(P->isBlockPointer());
37 assert(P);
38
39#ifndef NDEBUG
40 assert(hasPointer(P));
41#endif
42
43 BlockPointer &BP = P->BS;
44
45 if (Pointers == P)
46 Pointers = BP.Next;
47
48 if (BP.Prev)
49 BP.Prev->BS.Next = BP.Next;
50 if (BP.Next)
51 BP.Next->BS.Prev = BP.Prev;
52 P->BS.Pointee = nullptr;
53#ifndef NDEBUG
54 assert(!hasPointer(P));
55#endif
56}
57
58void Block::cleanup() {
59 if (Pointers == nullptr && !isDynamic() && isDead())
60 (reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();
61}
62
63void Block::replacePointer(Pointer *Old, Pointer *New) {
64 assert(Old);
65 assert(Old->isBlockPointer());
66 assert(New);
67 assert(New->isBlockPointer());
68 assert(Old != New);
69#ifndef NDEBUG
70 assert(hasPointer(Old));
71#endif
72
73 BlockPointer &OldBP = Old->BS;
74 BlockPointer &NewBP = New->BS;
75
76 if (OldBP.Prev)
77 OldBP.Prev->BS.Next = New;
78 if (OldBP.Next)
79 OldBP.Next->BS.Prev = New;
80 NewBP.Prev = OldBP.Prev;
81 NewBP.Next = OldBP.Next;
82 if (Pointers == Old)
83 Pointers = New;
84
85 OldBP.Pointee = nullptr;
86 NewBP.Pointee = this;
87#ifndef NDEBUG
88 assert(!hasPointer(Old));
89 assert(hasPointer(New));
90#endif
91}
92
93#ifndef NDEBUG
94bool Block::hasPointer(const Pointer *P) const {
95 for (const Pointer *C = Pointers; C; C = C->asBlockPointer().Next) {
96 if (C == P)
97 return true;
98 }
99 return false;
100}
101#endif
102
104 : Root(Root), B(~0u, Blk->Desc, Blk->isExtern(), Blk->IsStatic,
105 Blk->isWeak(), Blk->isDummy(), /*IsDead=*/true) {
106 // Add the block to the chain of dead blocks.
107 if (Root)
108 Root->Prev = this;
109
110 Next = Root;
111 Prev = nullptr;
112 Root = this;
113
114 B.DynAllocId = Blk->DynAllocId;
115
116 // Transfer pointers.
117 B.Pointers = Blk->Pointers;
118 for (Pointer *P = Blk->Pointers; P; P = P->asBlockPointer().Next)
119 P->BS.Pointee = &B;
120 Blk->Pointers = nullptr;
121}
122
123void DeadBlock::free() {
124 assert(!B.isInitialized());
125
126 if (Prev)
127 Prev->Next = Next;
128 if (Next)
129 Next->Prev = Prev;
130 if (Root == this)
131 Root = Next;
132 std::free(this);
133}
FormatToken * Next
The next token in the unwrapped line.
friend class Pointer
bool isDead() const
Definition InterpBlock.h:85
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
bool isDynamic() const
Definition InterpBlock.h:83
friend class DeadBlock
DeadBlock(DeadBlock *&Root, Block *Blk)
Copies the block.
A pointer to a memory block, live or dead.
Definition Pointer.h:90
bool isBlockPointer() const
Definition Pointer.h:464
BlockPointer BS
Definition Pointer.h:819
const BlockPointer & asBlockPointer() const
Definition Pointer.h:447
The JSON file list parser is used to communicate input to InstallAPI.
for(const auto &A :T->param_types())
#define true
Definition stdbool.h:25
Pointer * Prev
Previous link in the pointer chain.
Definition Pointer.h:41
Pointer * Next
Next link in the pointer chain.
Definition Pointer.h:43
Block * Pointee
The block the pointer is pointing to.
Definition Pointer.h:37