183d59e05SAlexandre Ganea //===- CommonLinkerContext.cpp --------------------------------------------===// 283d59e05SAlexandre Ganea // 383d59e05SAlexandre Ganea // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 483d59e05SAlexandre Ganea // See https://llvm.org/LICENSE.txt for license information. 583d59e05SAlexandre Ganea // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 683d59e05SAlexandre Ganea // 783d59e05SAlexandre Ganea //===----------------------------------------------------------------------===// 883d59e05SAlexandre Ganea 983d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h" 1083d59e05SAlexandre Ganea #include "lld/Common/ErrorHandler.h" 1183d59e05SAlexandre Ganea #include "lld/Common/Memory.h" 1283d59e05SAlexandre Ganea 13*69297cf6SJez Ng #include "llvm/CodeGen/CommandFlags.h" 14*69297cf6SJez Ng 1583d59e05SAlexandre Ganea using namespace llvm; 1683d59e05SAlexandre Ganea using namespace lld; 1783d59e05SAlexandre Ganea 1883d59e05SAlexandre Ganea // Reference to the current LLD instance. This is a temporary situation, until 1983d59e05SAlexandre Ganea // we pass this context everywhere by reference, or we make it a thread_local, 2083d59e05SAlexandre Ganea // as in https://reviews.llvm.org/D108850?id=370678 where each thread can be 2183d59e05SAlexandre Ganea // associated with a LLD instance. Only then will LLD be free of global 2283d59e05SAlexandre Ganea // state. 2383d59e05SAlexandre Ganea static CommonLinkerContext *lctx; 2483d59e05SAlexandre Ganea CommonLinkerContext()25*69297cf6SJez NgCommonLinkerContext::CommonLinkerContext() { 26*69297cf6SJez Ng lctx = this; 27*69297cf6SJez Ng // Fire off the static initializations in CGF's constructor. 28*69297cf6SJez Ng codegen::RegisterCodeGenFlags CGF; 29*69297cf6SJez Ng } 3083d59e05SAlexandre Ganea ~CommonLinkerContext()3183d59e05SAlexandre GaneaCommonLinkerContext::~CommonLinkerContext() { 3283d59e05SAlexandre Ganea assert(lctx); 3383d59e05SAlexandre Ganea // Explicitly call the destructors since we created the objects with placement 3483d59e05SAlexandre Ganea // new in SpecificAlloc::create(). 3583d59e05SAlexandre Ganea for (auto &it : instances) 3683d59e05SAlexandre Ganea it.second->~SpecificAllocBase(); 3783d59e05SAlexandre Ganea lctx = nullptr; 3883d59e05SAlexandre Ganea } 3983d59e05SAlexandre Ganea commonContext()4083d59e05SAlexandre GaneaCommonLinkerContext &lld::commonContext() { 4183d59e05SAlexandre Ganea assert(lctx); 4283d59e05SAlexandre Ganea return *lctx; 4383d59e05SAlexandre Ganea } 4483d59e05SAlexandre Ganea hasContext()4583d59e05SAlexandre Ganeabool lld::hasContext() { return lctx != nullptr; } 4683d59e05SAlexandre Ganea destroy()4783d59e05SAlexandre Ganeavoid CommonLinkerContext::destroy() { 4883d59e05SAlexandre Ganea if (lctx == nullptr) 4983d59e05SAlexandre Ganea return; 5083d59e05SAlexandre Ganea delete lctx; 5183d59e05SAlexandre Ganea } 52