blob: 7ce8c36aa494735ae3777dc04b94c4b5e2071784 [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()
Logan83913492011-01-01 01:42:09 +0800104 bool mCacheLoadFailed; // Set by loadCacheFile() used by readBC()
Logan1f028c02010-11-27 01:02:48 +0800105 char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true.
Logande2ca792010-11-27 22:15:39 +0800106 // Used by genCacheFile() for dumping
Logan1f028c02010-11-27 01:02:48 +0800107
Logan8b77a772010-12-21 09:11:01 +0800108 unsigned char mSourceSHA1[20]; // Set by readBC()
109
Logan1f028c02010-11-27 01:02:48 +0800110 PragmaList mPragmas;
111
Logan1f028c02010-11-27 01:02:48 +0800112 ExportVarList mExportVars;
113
Logan1f028c02010-11-27 01:02:48 +0800114 ExportFuncList mExportFuncs;
115
116 // The memory manager for code emitter
117 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +0800118
119 // The CodeEmitter
120 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan1f028c02010-11-27 01:02:48 +0800121
122 BCCSymbolLookupFn mpSymbolLookupFn;
123 void *mpSymbolLookupContext;
124
125 llvm::LLVMContext *mContext;
126 llvm::Module *mModule;
127
128 bool mHasLinked;
129
130 public:
131 Compiler();
132
133 // interface for BCCscript::registerSymbolCallback()
134 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
135 mpSymbolLookupFn = pFn;
136 mpSymbolLookupContext = pContext;
137 }
138
Logande2ca792010-11-27 22:15:39 +0800139 CodeMemoryManager *createCodeMemoryManager();
140
141 CodeEmitter *createCodeEmitter();
142
Logan1f028c02010-11-27 01:02:48 +0800143 int readModule(llvm::Module *module) {
144 GlobalInitialization();
145 mModule = module;
146 return hasError();
147 }
148
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800149 int readBC(const char *bitcode,
150 size_t bitcodeSize,
Loganb9b04162010-12-20 16:36:57 +0800151 long bitcodeFileModTime,
152 long bitcodeFileCRC32,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800153 const BCCchar *resName,
154 const BCCchar *cacheDir);
Logan1f028c02010-11-27 01:02:48 +0800155
156 int linkBC(const char *bitcode, size_t bitcodeSize);
157
158 // interface for bccLoadBinary()
159 int loadCacheFile();
160
161 // interace for bccCompileBC()
162 int compile();
163
164 // interface for bccGetScriptInfoLog()
Logan38d06072010-12-29 00:16:39 +0800165 char const *getErrorMessage() {
166 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800167 }
168
169 // interface for bccGetScriptLabel()
170 void *lookup(const char *name);
171
172 // Interface for bccGetExportVars()
173 void getExportVars(BCCsizei *actualVarCount,
174 BCCsizei maxVarCount,
175 BCCvoid **vars);
176
177 // Interface for bccGetExportFuncs()
178 void getExportFuncs(BCCsizei *actualFuncCount,
179 BCCsizei maxFuncCount,
180 BCCvoid **funcs);
181
182 // Interface for bccGetPragmas()
183 void getPragmas(BCCsizei *actualStringCount,
184 BCCsizei maxStringCount,
185 BCCchar **strings);
186
187 // Interface for bccGetFunctions()
188 void getFunctions(BCCsizei *actualFunctionCount,
189 BCCsizei maxFunctionCount,
190 BCCchar **functions);
191
192 // Interface for bccGetFunctionBinary()
193 void getFunctionBinary(BCCchar *function,
194 BCCvoid **base,
195 BCCsizei *length);
196
197 const llvm::Module *getModule() const {
198 return mModule;
199 }
200
201 ~Compiler();
202
203 private:
Logan8b77a772010-12-21 09:11:01 +0800204 void computeSourceSHA1(char const *bitcode, size_t size);
205
Logan1f028c02010-11-27 01:02:48 +0800206 // Note: loadCacheFile() and genCacheFile() go hand in hand
207 void genCacheFile();
208
209 // OpenCacheFile() returns fd of the cache file.
210 // Input:
211 // BCCchar *resName: Used to genCacheFileName()
212 // bool createIfMissing: If false, turn off caching
213 // Output:
214 // returns fd: If -1: Failed
215 // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
216 // cache file's file descriptor
217 // Note: openCacheFile() will check the cache file's validity,
218 // such as Magic number, sourceWhen... dependencies.
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800219 int openCacheFile(const BCCchar *resName,
220 const BCCchar *cacheDir,
221 bool createIfMissing);
Logan1f028c02010-11-27 01:02:48 +0800222
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800223 char *genCacheFileName(const char *cacheDir,
224 const char *fileName,
225 const char *subFileName);
Logan1f028c02010-11-27 01:02:48 +0800226
227 /*
228 * Read the oBCC header, verify it, then read the dependent section
229 * and verify that data as well.
230 *
231 * On successful return, the file will be seeked immediately past the
232 * oBCC header.
233 */
234 bool checkHeaderAndDependencies(int fd,
Loganb9b04162010-12-20 16:36:57 +0800235 long sourceWhen,
Logan1f028c02010-11-27 01:02:48 +0800236 uint32_t rslibWhen,
237 uint32_t libRSWhen,
238 uint32_t libbccWhen);
239
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800240
241 struct {
242 bool mNoCache;
243 } props;
244
245
Logande2ca792010-11-27 22:15:39 +0800246 private:
247
248 bool hasError() const {
249 return !mError.empty();
250 }
251
252 void setError(const char *Error) {
253 mError.assign(Error); // Copying
254 }
255
256 void setError(const std::string &Error) {
257 mError = Error;
258 }
259
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800260 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800261
Logan9b504eb2010-11-27 18:19:35 +0800262} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800263
264#endif // BCC_COMPILER_H