blob: 2510aadc267fddb7fb6032bc55ac40cae7bb4f65 [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
2 * Copyright 2010, 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
Loganc4395232010-11-27 18:54:17 +080022#include "CodeEmitter.h"
23#include "CodeMemoryManager.h"
Logan1f028c02010-11-27 01:02:48 +080024
25#include "llvm/ADT/OwningPtr.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Target/TargetMachine.h"
28
29#include <stddef.h>
30
31#include <list>
32#include <string>
33#include <vector>
34#include <utility>
35
36
Logan1f028c02010-11-27 01:02:48 +080037namespace llvm {
38 class LLVMContext;
39 class Module;
40}
41
42
43namespace bcc {
44
45 class Compiler {
Logande2ca792010-11-27 22:15:39 +080046 private:
47 typedef std::list< std::pair<std::string, std::string> > PragmaList;
48 typedef std::list<void*> ExportVarList;
49 typedef std::list<void*> ExportFuncList;
50
51
52 private:
Logan1f028c02010-11-27 01:02:48 +080053 // This part is designed to be orthogonal to those exported bcc*() functions
54 // implementation and internal struct BCCscript.
55
56 //////////////////////////////////////////////////////////////////////////
57 // The variable section below (e.g., Triple, CodeGenOptLevel)
58 // is initialized in GlobalInitialization()
59 //
60 static bool GlobalInitialized;
Shih-wei Liao931501a2010-12-16 04:43:04 -080061 static const char *resNames[64];
62 static int resNamesMmaped[64];
Logan1f028c02010-11-27 01:02:48 +080063
Logan1f028c02010-11-27 01:02:48 +080064 // If given, this will be the name of the target triple to compile for.
65 // If not given, the initial values defined in this file will be used.
66 static std::string Triple;
67
68 static llvm::CodeGenOpt::Level CodeGenOptLevel;
69
70 // End of section of GlobalInitializing variables
71 /////////////////////////////////////////////////////////////////////////
72 // If given, the name of the target CPU to generate code for.
73 static std::string CPU;
74
75 // The list of target specific features to enable or disable -- this should
76 // be a list of strings starting with '+' (enable) or '-' (disable).
77 static std::vector<std::string> Features;
78
Logan1f028c02010-11-27 01:02:48 +080079 static void GlobalInitialization();
80
81 static void LLVMErrorHandler(void *UserData, const std::string &Message);
82
83 static const llvm::StringRef PragmaMetadataName;
84 static const llvm::StringRef ExportVarMetadataName;
85 static const llvm::StringRef ExportFuncMetadataName;
86
87 friend class CodeEmitter;
88 friend class CodeMemoryManager;
89
Logande2ca792010-11-27 22:15:39 +080090
Logan1f028c02010-11-27 01:02:48 +080091 private:
92 std::string mError;
93
Shih-wei Liao931501a2010-12-16 04:43:04 -080094 int mResId; // Set by readBC()
Logan1f028c02010-11-27 01:02:48 +080095 bool mUseCache; // Set by readBC()
96 bool mCacheNew; // Set by readBC()
97 int mCacheFd; // Set by readBC()
Loganb9b04162010-12-20 16:36:57 +080098 long mSourceModTime; // Set by readBC()
99 long mSourceCRC32; // Set by readBC();
Logan1f028c02010-11-27 01:02:48 +0800100 char *mCacheMapAddr; // Set by loadCacheFile() if mCacheNew is false
101 oBCCHeader *mCacheHdr; // Set by loadCacheFile()
102 size_t mCacheSize; // Set by loadCacheFile()
103 ptrdiff_t mCacheDiff; // Set by loadCacheFile()
104 char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true.
Logande2ca792010-11-27 22:15:39 +0800105 // Used by genCacheFile() for dumping
Logan1f028c02010-11-27 01:02:48 +0800106
Logan8b77a772010-12-21 09:11:01 +0800107 unsigned char mSourceSHA1[20]; // Set by readBC()
108
Logan1f028c02010-11-27 01:02:48 +0800109 PragmaList mPragmas;
110
Logan1f028c02010-11-27 01:02:48 +0800111 ExportVarList mExportVars;
112
Logan1f028c02010-11-27 01:02:48 +0800113 ExportFuncList mExportFuncs;
114
115 // The memory manager for code emitter
116 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +0800117
118 // The CodeEmitter
119 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan1f028c02010-11-27 01:02:48 +0800120
121 BCCSymbolLookupFn mpSymbolLookupFn;
122 void *mpSymbolLookupContext;
123
124 llvm::LLVMContext *mContext;
125 llvm::Module *mModule;
126
127 bool mHasLinked;
128
129 public:
130 Compiler();
131
132 // interface for BCCscript::registerSymbolCallback()
133 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
134 mpSymbolLookupFn = pFn;
135 mpSymbolLookupContext = pContext;
136 }
137
Logande2ca792010-11-27 22:15:39 +0800138 CodeMemoryManager *createCodeMemoryManager();
139
140 CodeEmitter *createCodeEmitter();
141
Logan1f028c02010-11-27 01:02:48 +0800142 int readModule(llvm::Module *module) {
143 GlobalInitialization();
144 mModule = module;
145 return hasError();
146 }
147
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800148 int readBC(const char *bitcode,
149 size_t bitcodeSize,
Loganb9b04162010-12-20 16:36:57 +0800150 long bitcodeFileModTime,
151 long bitcodeFileCRC32,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800152 const BCCchar *resName,
153 const BCCchar *cacheDir);
Logan1f028c02010-11-27 01:02:48 +0800154
155 int linkBC(const char *bitcode, size_t bitcodeSize);
156
157 // interface for bccLoadBinary()
158 int loadCacheFile();
159
160 // interace for bccCompileBC()
161 int compile();
162
163 // interface for bccGetScriptInfoLog()
164 char *getErrorMessage() {
165 return const_cast<char*>(mError.c_str());
166 }
167
168 // interface for bccGetScriptLabel()
169 void *lookup(const char *name);
170
171 // Interface for bccGetExportVars()
172 void getExportVars(BCCsizei *actualVarCount,
173 BCCsizei maxVarCount,
174 BCCvoid **vars);
175
176 // Interface for bccGetExportFuncs()
177 void getExportFuncs(BCCsizei *actualFuncCount,
178 BCCsizei maxFuncCount,
179 BCCvoid **funcs);
180
181 // Interface for bccGetPragmas()
182 void getPragmas(BCCsizei *actualStringCount,
183 BCCsizei maxStringCount,
184 BCCchar **strings);
185
186 // Interface for bccGetFunctions()
187 void getFunctions(BCCsizei *actualFunctionCount,
188 BCCsizei maxFunctionCount,
189 BCCchar **functions);
190
191 // Interface for bccGetFunctionBinary()
192 void getFunctionBinary(BCCchar *function,
193 BCCvoid **base,
194 BCCsizei *length);
195
196 const llvm::Module *getModule() const {
197 return mModule;
198 }
199
200 ~Compiler();
201
202 private:
Logan8b77a772010-12-21 09:11:01 +0800203 void computeSourceSHA1(char const *bitcode, size_t size);
204
Logan1f028c02010-11-27 01:02:48 +0800205 // Note: loadCacheFile() and genCacheFile() go hand in hand
206 void genCacheFile();
207
208 // OpenCacheFile() returns fd of the cache file.
209 // Input:
210 // BCCchar *resName: Used to genCacheFileName()
211 // bool createIfMissing: If false, turn off caching
212 // Output:
213 // returns fd: If -1: Failed
214 // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
215 // cache file's file descriptor
216 // Note: openCacheFile() will check the cache file's validity,
217 // such as Magic number, sourceWhen... dependencies.
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800218 int openCacheFile(const BCCchar *resName,
219 const BCCchar *cacheDir,
220 bool createIfMissing);
Logan1f028c02010-11-27 01:02:48 +0800221
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800222 char *genCacheFileName(const char *cacheDir,
223 const char *fileName,
224 const char *subFileName);
Logan1f028c02010-11-27 01:02:48 +0800225
226 /*
227 * Read the oBCC header, verify it, then read the dependent section
228 * and verify that data as well.
229 *
230 * On successful return, the file will be seeked immediately past the
231 * oBCC header.
232 */
233 bool checkHeaderAndDependencies(int fd,
Loganb9b04162010-12-20 16:36:57 +0800234 long sourceWhen,
Logan1f028c02010-11-27 01:02:48 +0800235 uint32_t rslibWhen,
236 uint32_t libRSWhen,
237 uint32_t libbccWhen);
238
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800239
240 struct {
241 bool mNoCache;
242 } props;
243
244
Logande2ca792010-11-27 22:15:39 +0800245 private:
246
247 bool hasError() const {
248 return !mError.empty();
249 }
250
251 void setError(const char *Error) {
252 mError.assign(Error); // Copying
253 }
254
255 void setError(const std::string &Error) {
256 mError = Error;
257 }
258
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800259 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800260
Logan9b504eb2010-11-27 18:19:35 +0800261} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800262
263#endif // BCC_COMPILER_H