blob: fba7a0160101b8c8a3b241ff6ba3ed0603491b34 [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
Logan1f028c02010-11-27 01:02:48 +0800107 PragmaList mPragmas;
108
Logan1f028c02010-11-27 01:02:48 +0800109 ExportVarList mExportVars;
110
Logan1f028c02010-11-27 01:02:48 +0800111 ExportFuncList mExportFuncs;
112
113 // The memory manager for code emitter
114 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +0800115
116 // The CodeEmitter
117 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan1f028c02010-11-27 01:02:48 +0800118
119 BCCSymbolLookupFn mpSymbolLookupFn;
120 void *mpSymbolLookupContext;
121
122 llvm::LLVMContext *mContext;
123 llvm::Module *mModule;
124
125 bool mHasLinked;
126
127 public:
128 Compiler();
129
130 // interface for BCCscript::registerSymbolCallback()
131 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
132 mpSymbolLookupFn = pFn;
133 mpSymbolLookupContext = pContext;
134 }
135
Logande2ca792010-11-27 22:15:39 +0800136 CodeMemoryManager *createCodeMemoryManager();
137
138 CodeEmitter *createCodeEmitter();
139
Logan1f028c02010-11-27 01:02:48 +0800140 int readModule(llvm::Module *module) {
141 GlobalInitialization();
142 mModule = module;
143 return hasError();
144 }
145
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800146 int readBC(const char *bitcode,
147 size_t bitcodeSize,
Loganb9b04162010-12-20 16:36:57 +0800148 long bitcodeFileModTime,
149 long bitcodeFileCRC32,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800150 const BCCchar *resName,
151 const BCCchar *cacheDir);
Logan1f028c02010-11-27 01:02:48 +0800152
153 int linkBC(const char *bitcode, size_t bitcodeSize);
154
155 // interface for bccLoadBinary()
156 int loadCacheFile();
157
158 // interace for bccCompileBC()
159 int compile();
160
161 // interface for bccGetScriptInfoLog()
162 char *getErrorMessage() {
163 return const_cast<char*>(mError.c_str());
164 }
165
166 // interface for bccGetScriptLabel()
167 void *lookup(const char *name);
168
169 // Interface for bccGetExportVars()
170 void getExportVars(BCCsizei *actualVarCount,
171 BCCsizei maxVarCount,
172 BCCvoid **vars);
173
174 // Interface for bccGetExportFuncs()
175 void getExportFuncs(BCCsizei *actualFuncCount,
176 BCCsizei maxFuncCount,
177 BCCvoid **funcs);
178
179 // Interface for bccGetPragmas()
180 void getPragmas(BCCsizei *actualStringCount,
181 BCCsizei maxStringCount,
182 BCCchar **strings);
183
184 // Interface for bccGetFunctions()
185 void getFunctions(BCCsizei *actualFunctionCount,
186 BCCsizei maxFunctionCount,
187 BCCchar **functions);
188
189 // Interface for bccGetFunctionBinary()
190 void getFunctionBinary(BCCchar *function,
191 BCCvoid **base,
192 BCCsizei *length);
193
194 const llvm::Module *getModule() const {
195 return mModule;
196 }
197
198 ~Compiler();
199
200 private:
201 // Note: loadCacheFile() and genCacheFile() go hand in hand
202 void genCacheFile();
203
204 // OpenCacheFile() returns fd of the cache file.
205 // Input:
206 // BCCchar *resName: Used to genCacheFileName()
207 // bool createIfMissing: If false, turn off caching
208 // Output:
209 // returns fd: If -1: Failed
210 // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
211 // cache file's file descriptor
212 // Note: openCacheFile() will check the cache file's validity,
213 // such as Magic number, sourceWhen... dependencies.
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800214 int openCacheFile(const BCCchar *resName,
215 const BCCchar *cacheDir,
216 bool createIfMissing);
Logan1f028c02010-11-27 01:02:48 +0800217
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800218 char *genCacheFileName(const char *cacheDir,
219 const char *fileName,
220 const char *subFileName);
Logan1f028c02010-11-27 01:02:48 +0800221
222 /*
223 * Read the oBCC header, verify it, then read the dependent section
224 * and verify that data as well.
225 *
226 * On successful return, the file will be seeked immediately past the
227 * oBCC header.
228 */
229 bool checkHeaderAndDependencies(int fd,
Loganb9b04162010-12-20 16:36:57 +0800230 long sourceWhen,
Logan1f028c02010-11-27 01:02:48 +0800231 uint32_t rslibWhen,
232 uint32_t libRSWhen,
233 uint32_t libbccWhen);
234
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800235
236 struct {
237 bool mNoCache;
238 } props;
239
240
Logande2ca792010-11-27 22:15:39 +0800241 private:
242
243 bool hasError() const {
244 return !mError.empty();
245 }
246
247 void setError(const char *Error) {
248 mError.assign(Error); // Copying
249 }
250
251 void setError(const std::string &Error) {
252 mError = Error;
253 }
254
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800255 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800256
Logan9b504eb2010-11-27 18:19:35 +0800257} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800258
259#endif // BCC_COMPILER_H