blob: 444d1368ac889fa300acafe253066f827cb63e0b [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
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800151 int readBC(const char *bitcode,
152 size_t bitcodeSize,
153 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()
165 char *getErrorMessage() {
166 return const_cast<char*>(mError.c_str());
167 }
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:
204 // Note: loadCacheFile() and genCacheFile() go hand in hand
205 void genCacheFile();
206
207 // OpenCacheFile() returns fd of the cache file.
208 // Input:
209 // BCCchar *resName: Used to genCacheFileName()
210 // bool createIfMissing: If false, turn off caching
211 // Output:
212 // returns fd: If -1: Failed
213 // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
214 // cache file's file descriptor
215 // Note: openCacheFile() will check the cache file's validity,
216 // such as Magic number, sourceWhen... dependencies.
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800217 int openCacheFile(const BCCchar *resName,
218 const BCCchar *cacheDir,
219 bool createIfMissing);
Logan1f028c02010-11-27 01:02:48 +0800220
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800221 char *genCacheFileName(const char *cacheDir,
222 const char *fileName,
223 const char *subFileName);
Logan1f028c02010-11-27 01:02:48 +0800224
225 /*
226 * Read the oBCC header, verify it, then read the dependent section
227 * and verify that data as well.
228 *
229 * On successful return, the file will be seeked immediately past the
230 * oBCC header.
231 */
232 bool checkHeaderAndDependencies(int fd,
233 uint32_t sourceWhen,
234 uint32_t rslibWhen,
235 uint32_t libRSWhen,
236 uint32_t libbccWhen);
237
Logande2ca792010-11-27 22:15:39 +0800238 private:
239
240 bool hasError() const {
241 return !mError.empty();
242 }
243
244 void setError(const char *Error) {
245 mError.assign(Error); // Copying
246 }
247
248 void setError(const std::string &Error) {
249 mError = Error;
250 }
251
Logan1f028c02010-11-27 01:02:48 +0800252 };
253
254
255
Logan9b504eb2010-11-27 18:19:35 +0800256} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800257
258#endif // BCC_COMPILER_H