blob: 0d5f7bf0f2989cbcda4f453ac289a464aad50c0c [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
37#define BCC_MMAP_IMG_BEGIN 0x7e000000
38#define BCC_MMAP_IMG_COUNT 5
39
40#define BCC_MMAP_IMG_CODE_SIZE (128 * 1024)
41#define BCC_MMAP_IMG_DATA_SIZE (128 * 1024)
42#define BCC_MMAP_IMG_SIZE (BCC_MMAP_IMG_CODE_SIZE + BCC_MMAP_IMG_DATA_SIZE)
43
44
45namespace llvm {
46 class LLVMContext;
47 class Module;
48}
49
50
51namespace bcc {
52
53 class Compiler {
Logande2ca792010-11-27 22:15:39 +080054 private:
55 typedef std::list< std::pair<std::string, std::string> > PragmaList;
56 typedef std::list<void*> ExportVarList;
57 typedef std::list<void*> ExportFuncList;
58
59
60 private:
Logan1f028c02010-11-27 01:02:48 +080061 // This part is designed to be orthogonal to those exported bcc*() functions
62 // implementation and internal struct BCCscript.
63
64 //////////////////////////////////////////////////////////////////////////
65 // The variable section below (e.g., Triple, CodeGenOptLevel)
66 // is initialized in GlobalInitialization()
67 //
68 static bool GlobalInitialized;
69
70 static bool BccMmapImgAddrTaken[BCC_MMAP_IMG_COUNT];
71
72 // If given, this will be the name of the target triple to compile for.
73 // If not given, the initial values defined in this file will be used.
74 static std::string Triple;
75
76 static llvm::CodeGenOpt::Level CodeGenOptLevel;
77
78 // End of section of GlobalInitializing variables
79 /////////////////////////////////////////////////////////////////////////
80 // If given, the name of the target CPU to generate code for.
81 static std::string CPU;
82
83 // The list of target specific features to enable or disable -- this should
84 // be a list of strings starting with '+' (enable) or '-' (disable).
85 static std::vector<std::string> Features;
86
Logan1f028c02010-11-27 01:02:48 +080087 static void GlobalInitialization();
88
89 static void LLVMErrorHandler(void *UserData, const std::string &Message);
90
91 static const llvm::StringRef PragmaMetadataName;
92 static const llvm::StringRef ExportVarMetadataName;
93 static const llvm::StringRef ExportFuncMetadataName;
94
95 friend class CodeEmitter;
96 friend class CodeMemoryManager;
97
Logande2ca792010-11-27 22:15:39 +080098
Logan1f028c02010-11-27 01:02:48 +080099 private:
100 std::string mError;
101
Logan1f028c02010-11-27 01:02:48 +0800102 bool mUseCache; // Set by readBC()
103 bool mCacheNew; // Set by readBC()
104 int mCacheFd; // Set by readBC()
105 char *mCacheMapAddr; // Set by loadCacheFile() if mCacheNew is false
106 oBCCHeader *mCacheHdr; // Set by loadCacheFile()
107 size_t mCacheSize; // Set by loadCacheFile()
108 ptrdiff_t mCacheDiff; // Set by loadCacheFile()
109 char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true.
Logande2ca792010-11-27 22:15:39 +0800110 // Used by genCacheFile() for dumping
Logan1f028c02010-11-27 01:02:48 +0800111
Logan1f028c02010-11-27 01:02:48 +0800112 PragmaList mPragmas;
113
Logan1f028c02010-11-27 01:02:48 +0800114 ExportVarList mExportVars;
115
Logan1f028c02010-11-27 01:02:48 +0800116 ExportFuncList mExportFuncs;
117
118 // The memory manager for code emitter
119 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +0800120
121 // The CodeEmitter
122 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan1f028c02010-11-27 01:02:48 +0800123
124 BCCSymbolLookupFn mpSymbolLookupFn;
125 void *mpSymbolLookupContext;
126
127 llvm::LLVMContext *mContext;
128 llvm::Module *mModule;
129
130 bool mHasLinked;
131
132 public:
133 Compiler();
134
135 // interface for BCCscript::registerSymbolCallback()
136 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
137 mpSymbolLookupFn = pFn;
138 mpSymbolLookupContext = pContext;
139 }
140
Logande2ca792010-11-27 22:15:39 +0800141 CodeMemoryManager *createCodeMemoryManager();
142
143 CodeEmitter *createCodeEmitter();
144
Logan1f028c02010-11-27 01:02:48 +0800145 int readModule(llvm::Module *module) {
146 GlobalInitialization();
147 mModule = module;
148 return hasError();
149 }
150
151 int readBC(const char *bitcode, size_t bitcodeSize,const BCCchar *resName);
152
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.
214 int openCacheFile(const BCCchar *resName, bool createIfMissing);
215
216 char *genCacheFileName(const char *fileName, const char *subFileName);
217
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
Logande2ca792010-11-27 22:15:39 +0800231 private:
232
233 bool hasError() const {
234 return !mError.empty();
235 }
236
237 void setError(const char *Error) {
238 mError.assign(Error); // Copying
239 }
240
241 void setError(const std::string &Error) {
242 mError = Error;
243 }
244
Logan1f028c02010-11-27 01:02:48 +0800245 };
246
247
248
Logan9b504eb2010-11-27 18:19:35 +0800249} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800250
251#endif // BCC_COMPILER_H