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