• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef BCC_COMPILER_H
18 #define BCC_COMPILER_H
19 
20 #include <bcc/bcc.h>
21 
22 #include <Config.h>
23 
24 #include "librsloader.h"
25 
26 #include "llvm/ADT/OwningPtr.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/Triple.h"
30 #include "llvm/Target/TargetMachine.h"
31 
32 #include <stddef.h>
33 
34 #include <list>
35 #include <string>
36 #include <vector>
37 #include <utility>
38 
39 
40 namespace llvm {
41   class Module;
42   class NamedMDNode;
43   class TargetData;
44 }
45 
46 
47 namespace bcc {
48   class ScriptCompiled;
49   struct CompilerOption;
50 
51   class Compiler {
52   private:
53     //////////////////////////////////////////////////////////////////////////
54     // The variable section below (e.g., Triple, CodeGenOptLevel)
55     // is initialized in GlobalInitialization()
56     //
57     static bool GlobalInitialized;
58 
59     // If given, this will be the name of the target triple to compile for.
60     // If not given, the initial values defined in this file will be used.
61     static std::string Triple;
62     static llvm::Triple::ArchType ArchType;
63 
64     static llvm::CodeGenOpt::Level CodeGenOptLevel;
65 
66     // End of section of GlobalInitializing variables
67     /////////////////////////////////////////////////////////////////////////
68     // If given, the name of the target CPU to generate code for.
69     static std::string CPU;
70 
71     // The list of target specific features to enable or disable -- this should
72     // be a list of strings starting with '+' (enable) or '-' (disable).
73     static std::vector<std::string> Features;
74 
75     static void LLVMErrorHandler(void *UserData, const std::string &Message);
76 
77     friend class CodeEmitter;
78     friend class CodeMemoryManager;
79 
80   private:
81     ScriptCompiled *mpResult;
82 
83     std::string mError;
84 
85     // Compilation buffer for MC
86     llvm::SmallVector<char, 1024> mEmittedELFExecutable;
87 
88     // Loaded and relocated executable
89     RSExecRef mRSExecutable;
90 
91     BCCSymbolLookupFn mpSymbolLookupFn;
92     void *mpSymbolLookupContext;
93 
94     llvm::Module *mModule;
95 
96     bool mHasLinked;
97 
98   public:
99     Compiler(ScriptCompiled *result);
100 
101     static void GlobalInitialization();
102 
getTargetTriple()103     static std::string const &getTargetTriple() {
104       return Triple;
105     }
106 
getTargetArchType()107     static llvm::Triple::ArchType getTargetArchType() {
108       return ArchType;
109     }
110 
registerSymbolCallback(BCCSymbolLookupFn pFn,void * pContext)111     void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
112       mpSymbolLookupFn = pFn;
113       mpSymbolLookupContext = pContext;
114     }
115 
116     void *getSymbolAddress(char const *name);
117 
getELF()118     const llvm::SmallVector<char, 1024> &getELF() const {
119       return mEmittedELFExecutable;
120     }
121 
readModule(llvm::Module * module)122     int readModule(llvm::Module *module) {
123       mModule = module;
124       return hasError();
125     }
126 
127     int linkModule(llvm::Module *module);
128 
129     int compile(const CompilerOption &option);
130 
getErrorMessage()131     char const *getErrorMessage() {
132       return mError.c_str();
133     }
134 
getModule()135     const llvm::Module *getModule() const {
136       return mModule;
137     }
138 
139     ~Compiler();
140 
141   private:
142 
143     int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
144                    llvm::NamedMDNode const *ExportVarMetadata,
145                    llvm::NamedMDNode const *ExportFuncMetadata);
146 
147     int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
148 
149     static void *resolveSymbolAdapter(void *context, char const *name);
150 
151     int runInternalPasses(std::vector<std::string>& Names,
152                           std::vector<uint32_t>& Signatures);
153 
154     int runLTO(llvm::TargetData *TD,
155                std::vector<const char*>& ExportSymbols,
156                llvm::CodeGenOpt::Level OptimizationLevel);
157 
hasError()158     bool hasError() const {
159       return !mError.empty();
160     }
161 
setError(const char * Error)162     void setError(const char *Error) {
163       mError.assign(Error);  // Copying
164     }
165 
setError(const std::string & Error)166     void setError(const std::string &Error) {
167       mError = Error;
168     }
169 
170   };  // End of class Compiler
171 
172 } // namespace bcc
173 
174 #endif // BCC_COMPILER_H
175