Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 1 | //===--- 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 Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 10 | // Plugin support. |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | 4a1a77c | 2008-09-22 20:50:40 +0000 | [diff] [blame] | 14 | #include "llvm/CompilerDriver/Plugin.h" |
Owen Anderson | a3e3175 | 2009-06-26 00:06:28 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ManagedStatic.h" |
| 16 | #include "llvm/System/Mutex.h" |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
| 20 | namespace { |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 21 | |
| 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 Anderson | a3e3175 | 2009-06-26 00:06:28 +0000 | [diff] [blame] | 32 | static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > PluginMutex; |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 33 | |
| 34 | struct ByPriority { |
| 35 | bool operator()(const llvmc::BasePlugin* lhs, |
| 36 | const llvmc::BasePlugin* rhs) { |
| 37 | return lhs->Priority() < rhs->Priority(); |
| 38 | } |
| 39 | }; |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | namespace llvmc { |
| 43 | |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 44 | PluginLoader::PluginLoader() { |
Owen Anderson | a3e3175 | 2009-06-26 00:06:28 +0000 | [diff] [blame] | 45 | llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex); |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 46 | if (!pluginListInitialized) { |
| 47 | for (PluginRegistry::iterator B = PluginRegistry::begin(), |
| 48 | E = PluginRegistry::end(); B != E; ++B) |
| 49 | Plugins.push_back(B->instantiate()); |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 50 | std::sort(Plugins.begin(), Plugins.end(), ByPriority()); |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 51 | } |
| 52 | pluginListInitialized = true; |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 55 | PluginLoader::~PluginLoader() { |
Owen Anderson | a3e3175 | 2009-06-26 00:06:28 +0000 | [diff] [blame] | 56 | llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex); |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 57 | 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 Anderson | a3e3175 | 2009-06-26 00:06:28 +0000 | [diff] [blame] | 66 | llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex); |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 67 | for (PluginList::iterator B = Plugins.begin(), E = Plugins.end(); |
| 68 | B != E; ++B) |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 69 | (*B)->PopulateLanguageMap(langMap); |
| 70 | } |
| 71 | |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 72 | void PluginLoader::PopulateCompilationGraph(CompilationGraph& graph) { |
Owen Anderson | a3e3175 | 2009-06-26 00:06:28 +0000 | [diff] [blame] | 73 | llvm::sys::SmartScopedLock<true> Lock(&*PluginMutex); |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 74 | for (PluginList::iterator B = Plugins.begin(), E = Plugins.end(); |
| 75 | B != E; ++B) |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 76 | (*B)->PopulateCompilationGraph(graph); |
| 77 | } |
| 78 | |
| 79 | } |