xref: /freebsd/contrib/llvm-project/clang/lib/AST/Interp/InterpBlock.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 
16 using namespace clang;
17 using namespace clang::interp;
18 
19 
20 
21 void Block::addPointer(Pointer *P) {
22   if (IsStatic)
23     return;
24   if (Pointers)
25     Pointers->Prev = P;
26   P->Next = Pointers;
27   P->Prev = nullptr;
28   Pointers = P;
29 }
30 
31 void Block::removePointer(Pointer *P) {
32   if (IsStatic)
33     return;
34   if (Pointers == P)
35     Pointers = P->Next;
36   if (P->Prev)
37     P->Prev->Next = P->Next;
38   if (P->Next)
39     P->Next->Prev = P->Prev;
40 }
41 
42 void Block::cleanup() {
43   if (Pointers == nullptr && IsDead)
44     (reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();
45 }
46 
47 void Block::movePointer(Pointer *From, Pointer *To) {
48   if (IsStatic)
49     return;
50   To->Prev = From->Prev;
51   if (To->Prev)
52     To->Prev->Next = To;
53   To->Next = From->Next;
54   if (To->Next)
55     To->Next->Prev = To;
56   if (Pointers == From)
57     Pointers = To;
58 
59   From->Prev = nullptr;
60   From->Next = nullptr;
61 }
62 
63 DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
64     : Root(Root), B(Blk->Desc, Blk->IsStatic, Blk->IsExtern, /*isDead=*/true) {
65   // Add the block to the chain of dead blocks.
66   if (Root)
67     Root->Prev = this;
68 
69   Next = Root;
70   Prev = nullptr;
71   Root = this;
72 
73   // Transfer pointers.
74   B.Pointers = Blk->Pointers;
75   for (Pointer *P = Blk->Pointers; P; P = P->Next)
76     P->Pointee = &B;
77 }
78 
79 void DeadBlock::free() {
80   if (Prev)
81     Prev->Next = Next;
82   if (Next)
83     Next->Prev = Prev;
84   if (Root == this)
85     Root = Next;
86   ::free(this);
87 }
88