blob: cb3c7be39dd375b1b8b6b7d77cd210127a258465 [file] [log] [blame]
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001//===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000010// Plugin support.
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000014#include "llvm/CompilerDriver/Plugin.h"
Owen Andersona3e31752009-06-26 00:06:28 +000015#include "llvm/Support/ManagedStatic.h"
16#include "llvm/System/Mutex.h"
Mikhail Glushenkov35fde152008-11-17 17:30:25 +000017#include <algorithm>
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000018#include <vector>
19
20namespace {
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000021
22 // Registry::Add<> does not do lifetime management (probably issues
23 // with static constructor/destructor ordering), so we have to
24 // implement it here.
25 //
26 // All this static registration/life-before-main model seems
27 // unnecessary convoluted to me.
28
29 static bool pluginListInitialized = false;
30 typedef std::vector<const llvmc::BasePlugin*> PluginList;
31 static PluginList Plugins;
Owen Andersona3e31752009-06-26 00:06:28 +000032 static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > PluginMutex;
Mikhail Glushenkov35fde152008-11-17 17:30:25 +000033
34 struct ByPriority {
35 bool operator()(const llvmc::BasePlugin* lhs,
36 const llvmc::BasePlugin* rhs) {
37 return lhs->Priority() < rhs->Priority();
38 }
39 };
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000040}
41
42namespace llvmc {
43
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000044 PluginLoader::PluginLoader() {
Owen Andersona3e31752009-06-26 00:06:28 +000045 llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex);
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000046 if (!pluginListInitialized) {
47 for (PluginRegistry::iterator B = PluginRegistry::begin(),
48 E = PluginRegistry::end(); B != E; ++B)
49 Plugins.push_back(B->instantiate());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +000050 std::sort(Plugins.begin(), Plugins.end(), ByPriority());
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000051 }
52 pluginListInitialized = true;
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000053 }
54
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000055 PluginLoader::~PluginLoader() {
Owen Andersona3e31752009-06-26 00:06:28 +000056 llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex);
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000057 if (pluginListInitialized) {
58 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
59 B != E; ++B)
60 delete (*B);
61 }
62 pluginListInitialized = false;
63 }
64
65 void PluginLoader::PopulateLanguageMap(LanguageMap& langMap) {
Owen Andersona3e31752009-06-26 00:06:28 +000066 llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex);
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000067 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
68 B != E; ++B)
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000069 (*B)->PopulateLanguageMap(langMap);
70 }
71
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000072 void PluginLoader::PopulateCompilationGraph(CompilationGraph& graph) {
Owen Andersona3e31752009-06-26 00:06:28 +000073 llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex);
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000074 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
75 B != E; ++B)
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000076 (*B)->PopulateCompilationGraph(graph);
77 }
78
79}