Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010-2012, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 17 | #ifndef BCC_SOURCE_H |
| 18 | #define BCC_SOURCE_H |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | namespace llvm { |
| 23 | class Module; |
| 24 | } |
| 25 | |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 26 | namespace bcinfo { |
| 27 | class MetadataExtractor; |
| 28 | } |
| 29 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 30 | namespace bcc { |
| 31 | |
| 32 | class BCCContext; |
| 33 | |
| 34 | class Source { |
| 35 | private: |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 36 | const std::string mName; // A unique name |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 37 | BCCContext &mContext; |
| 38 | llvm::Module *mModule; |
| 39 | |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 40 | bcinfo::MetadataExtractor *mMetadata; |
| 41 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 42 | // If true, destructor won't destroy the mModule. |
| 43 | bool mNoDelete; |
| 44 | |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 45 | // Keep track of whether mModule is destroyed (possibly as a consequence of |
| 46 | // getting linked with a different llvm::Module). |
| 47 | bool mIsModuleDestroyed; |
| 48 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 49 | private: |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 50 | Source(const char* name, BCCContext &pContext, llvm::Module &pModule, |
| 51 | bool pNoDelete = false); |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 52 | |
| 53 | public: |
| 54 | static Source *CreateFromBuffer(BCCContext &pContext, |
| 55 | const char *pName, |
| 56 | const char *pBitcode, |
| 57 | size_t pBitcodeSize); |
| 58 | |
| 59 | static Source *CreateFromFile(BCCContext &pContext, |
| 60 | const std::string &pPath); |
| 61 | |
| 62 | // Create a Source object from an existing module. If pNoDelete |
| 63 | // is true, destructor won't call delete on the given module. |
| 64 | static Source *CreateFromModule(BCCContext &pContext, |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 65 | const char* name, |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 66 | llvm::Module &pModule, |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 67 | uint32_t compilerVersion, |
| 68 | uint32_t optimizationLevel, |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 69 | bool pNoDelete = false); |
| 70 | |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame] | 71 | const std::string& getName() const { return mName; } |
| 72 | |
| 73 | // Merge the current source with pSource. pSource |
| 74 | // will be destroyed after successfully merged. Return false on error. |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 75 | bool merge(Source &pSource); |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 76 | |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 77 | unsigned getCompilerVersion() const; |
| 78 | |
| 79 | void getWrapperInformation(unsigned *compilerVersion, |
| 80 | unsigned *optimizationLevel) const; |
| 81 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 82 | inline BCCContext &getContext() |
| 83 | { return mContext; } |
| 84 | inline const BCCContext &getContext() const |
| 85 | { return mContext; } |
| 86 | |
Stephen Hines | 06731a6 | 2013-02-12 19:29:42 -0800 | [diff] [blame] | 87 | void setModule(llvm::Module *pModule); |
| 88 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 89 | inline llvm::Module &getModule() |
| 90 | { return *mModule; } |
| 91 | inline const llvm::Module &getModule() const |
| 92 | { return *mModule; } |
| 93 | |
| 94 | // Get the "identifier" of the bitcode. This will return the value of pName |
| 95 | // when it's created using CreateFromBuffer and pPath if CreateFromFile(). |
| 96 | const std::string &getIdentifier() const; |
| 97 | |
Pirama Arumuga Nainar | 51ee77b | 2015-02-19 16:33:27 -0800 | [diff] [blame] | 98 | void addBuildChecksumMetadata(const char *) const; |
| 99 | |
Dean De Leo | 09c7a41 | 2015-11-25 12:45:45 +0000 | [diff] [blame] | 100 | // Get whether debugging has been enabled for this module by checking |
| 101 | // for presence of debug info in the module. |
| 102 | bool getDebugInfoEnabled() const; |
| 103 | |
Pirama Arumuga Nainar | f229c40 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 104 | // Extract metadata from mModule using MetadataExtractor. |
| 105 | bool extractMetadata(); |
| 106 | bcinfo::MetadataExtractor* getMetadata() const { return mMetadata; } |
| 107 | |
| 108 | // Mark mModule was destroyed in the process of linking with a different |
| 109 | // llvm::Module |
| 110 | void markModuleDestroyed() { mIsModuleDestroyed = true; } |
| 111 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 112 | ~Source(); |
| 113 | }; |
| 114 | |
| 115 | } // namespace bcc |
| 116 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 117 | #endif // BCC_SOURCE_H |