Home | History | Annotate | Line # | Download | only in Interp
      1  1.1  joerg //===--- Block.cpp - Allocated blocks for the interpreter -------*- C++ -*-===//
      2  1.1  joerg //
      3  1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4  1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      5  1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6  1.1  joerg //
      7  1.1  joerg //===----------------------------------------------------------------------===//
      8  1.1  joerg //
      9  1.1  joerg // Defines the classes describing allocated blocks.
     10  1.1  joerg //
     11  1.1  joerg //===----------------------------------------------------------------------===//
     12  1.1  joerg 
     13  1.1  joerg #include "InterpBlock.h"
     14  1.1  joerg #include "Pointer.h"
     15  1.1  joerg 
     16  1.1  joerg using namespace clang;
     17  1.1  joerg using namespace clang::interp;
     18  1.1  joerg 
     19  1.1  joerg 
     20  1.1  joerg 
     21  1.1  joerg void Block::addPointer(Pointer *P) {
     22  1.1  joerg   if (IsStatic)
     23  1.1  joerg     return;
     24  1.1  joerg   if (Pointers)
     25  1.1  joerg     Pointers->Prev = P;
     26  1.1  joerg   P->Next = Pointers;
     27  1.1  joerg   P->Prev = nullptr;
     28  1.1  joerg   Pointers = P;
     29  1.1  joerg }
     30  1.1  joerg 
     31  1.1  joerg void Block::removePointer(Pointer *P) {
     32  1.1  joerg   if (IsStatic)
     33  1.1  joerg     return;
     34  1.1  joerg   if (Pointers == P)
     35  1.1  joerg     Pointers = P->Next;
     36  1.1  joerg   if (P->Prev)
     37  1.1  joerg     P->Prev->Next = P->Next;
     38  1.1  joerg   if (P->Next)
     39  1.1  joerg     P->Next->Prev = P->Prev;
     40  1.1  joerg }
     41  1.1  joerg 
     42  1.1  joerg void Block::cleanup() {
     43  1.1  joerg   if (Pointers == nullptr && IsDead)
     44  1.1  joerg     (reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();
     45  1.1  joerg }
     46  1.1  joerg 
     47  1.1  joerg void Block::movePointer(Pointer *From, Pointer *To) {
     48  1.1  joerg   if (IsStatic)
     49  1.1  joerg     return;
     50  1.1  joerg   To->Prev = From->Prev;
     51  1.1  joerg   if (To->Prev)
     52  1.1  joerg     To->Prev->Next = To;
     53  1.1  joerg   To->Next = From->Next;
     54  1.1  joerg   if (To->Next)
     55  1.1  joerg     To->Next->Prev = To;
     56  1.1  joerg   if (Pointers == From)
     57  1.1  joerg     Pointers = To;
     58  1.1  joerg 
     59  1.1  joerg   From->Prev = nullptr;
     60  1.1  joerg   From->Next = nullptr;
     61  1.1  joerg }
     62  1.1  joerg 
     63  1.1  joerg DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
     64  1.1  joerg     : Root(Root), B(Blk->Desc, Blk->IsStatic, Blk->IsExtern, /*isDead=*/true) {
     65  1.1  joerg   // Add the block to the chain of dead blocks.
     66  1.1  joerg   if (Root)
     67  1.1  joerg     Root->Prev = this;
     68  1.1  joerg 
     69  1.1  joerg   Next = Root;
     70  1.1  joerg   Prev = nullptr;
     71  1.1  joerg   Root = this;
     72  1.1  joerg 
     73  1.1  joerg   // Transfer pointers.
     74  1.1  joerg   B.Pointers = Blk->Pointers;
     75  1.1  joerg   for (Pointer *P = Blk->Pointers; P; P = P->Next)
     76  1.1  joerg     P->Pointee = &B;
     77  1.1  joerg }
     78  1.1  joerg 
     79  1.1  joerg void DeadBlock::free() {
     80  1.1  joerg   if (Prev)
     81  1.1  joerg     Prev->Next = Next;
     82  1.1  joerg   if (Next)
     83  1.1  joerg     Next->Prev = Prev;
     84  1.1  joerg   if (Root == this)
     85  1.1  joerg     Root = Next;
     86  1.1  joerg   ::free(this);
     87  1.1  joerg }
     88