blob: 08c92c5e4e2809b3c9e283b2c7b695b90bbee93a [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 {
54 // This part is designed to be orthogonal to those exported bcc*() functions
55 // implementation and internal struct BCCscript.
56
57 //////////////////////////////////////////////////////////////////////////
58 // The variable section below (e.g., Triple, CodeGenOptLevel)
59 // is initialized in GlobalInitialization()
60 //
61 static bool GlobalInitialized;
62
63 static bool BccMmapImgAddrTaken[BCC_MMAP_IMG_COUNT];
64
65 // If given, this will be the name of the target triple to compile for.
66 // If not given, the initial values defined in this file will be used.
67 static std::string Triple;
68
69 static llvm::CodeGenOpt::Level CodeGenOptLevel;
70
71 // End of section of GlobalInitializing variables
72 /////////////////////////////////////////////////////////////////////////
73 // If given, the name of the target CPU to generate code for.
74 static std::string CPU;
75
76 // The list of target specific features to enable or disable -- this should
77 // be a list of strings starting with '+' (enable) or '-' (disable).
78 static std::vector<std::string> Features;
79
Logan1f028c02010-11-27 01:02:48 +080080 static void GlobalInitialization();
81
82 static void LLVMErrorHandler(void *UserData, const std::string &Message);
83
84 static const llvm::StringRef PragmaMetadataName;
85 static const llvm::StringRef ExportVarMetadataName;
86 static const llvm::StringRef ExportFuncMetadataName;
87
88 friend class CodeEmitter;
89 friend class CodeMemoryManager;
90
91 private:
92 std::string mError;
93
94 bool hasError() const {
95 return !mError.empty();
96 }
97
98 void setError(const char *Error) {
99 mError.assign(Error); // Copying
100 }
101
102 void setError(const std::string &Error) {
103 mError = Error;
104 }
105
106 private:
107 bool mUseCache; // Set by readBC()
108 bool mCacheNew; // Set by readBC()
109 int mCacheFd; // Set by readBC()
110 char *mCacheMapAddr; // Set by loadCacheFile() if mCacheNew is false
111 oBCCHeader *mCacheHdr; // Set by loadCacheFile()
112 size_t mCacheSize; // Set by loadCacheFile()
113 ptrdiff_t mCacheDiff; // Set by loadCacheFile()
114 char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true.
115 // Used by genCacheFile() for dumping
116
117 typedef std::list< std::pair<std::string, std::string> > PragmaList;
118 PragmaList mPragmas;
119
120 typedef std::list<void*> ExportVarList;
121 ExportVarList mExportVars;
122
123 typedef std::list<void*> ExportFuncList;
124 ExportFuncList mExportFuncs;
125
126 // The memory manager for code emitter
127 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
128 CodeMemoryManager *createCodeMemoryManager();
129
130 // The CodeEmitter
131 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
132 CodeEmitter *createCodeEmitter();
133
134 BCCSymbolLookupFn mpSymbolLookupFn;
135 void *mpSymbolLookupContext;
136
137 llvm::LLVMContext *mContext;
138 llvm::Module *mModule;
139
140 bool mHasLinked;
141
142 public:
143 Compiler();
144
145 // interface for BCCscript::registerSymbolCallback()
146 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
147 mpSymbolLookupFn = pFn;
148 mpSymbolLookupContext = pContext;
149 }
150
151 int readModule(llvm::Module *module) {
152 GlobalInitialization();
153 mModule = module;
154 return hasError();
155 }
156
157 int readBC(const char *bitcode, size_t bitcodeSize,const BCCchar *resName);
158
159 int linkBC(const char *bitcode, size_t bitcodeSize);
160
161 // interface for bccLoadBinary()
162 int loadCacheFile();
163
164 // interace for bccCompileBC()
165 int compile();
166
167 // interface for bccGetScriptInfoLog()
168 char *getErrorMessage() {
169 return const_cast<char*>(mError.c_str());
170 }
171
172 // interface for bccGetScriptLabel()
173 void *lookup(const char *name);
174
175 // Interface for bccGetExportVars()
176 void getExportVars(BCCsizei *actualVarCount,
177 BCCsizei maxVarCount,
178 BCCvoid **vars);
179
180 // Interface for bccGetExportFuncs()
181 void getExportFuncs(BCCsizei *actualFuncCount,
182 BCCsizei maxFuncCount,
183 BCCvoid **funcs);
184
185 // Interface for bccGetPragmas()
186 void getPragmas(BCCsizei *actualStringCount,
187 BCCsizei maxStringCount,
188 BCCchar **strings);
189
190 // Interface for bccGetFunctions()
191 void getFunctions(BCCsizei *actualFunctionCount,
192 BCCsizei maxFunctionCount,
193 BCCchar **functions);
194
195 // Interface for bccGetFunctionBinary()
196 void getFunctionBinary(BCCchar *function,
197 BCCvoid **base,
198 BCCsizei *length);
199
200 const llvm::Module *getModule() const {
201 return mModule;
202 }
203
204 ~Compiler();
205
206 private:
207 // Note: loadCacheFile() and genCacheFile() go hand in hand
208 void genCacheFile();
209
210 // OpenCacheFile() returns fd of the cache file.
211 // Input:
212 // BCCchar *resName: Used to genCacheFileName()
213 // bool createIfMissing: If false, turn off caching
214 // Output:
215 // returns fd: If -1: Failed
216 // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
217 // cache file's file descriptor
218 // Note: openCacheFile() will check the cache file's validity,
219 // such as Magic number, sourceWhen... dependencies.
220 int openCacheFile(const BCCchar *resName, bool createIfMissing);
221
222 char *genCacheFileName(const char *fileName, const char *subFileName);
223
224 /*
225 * Read the oBCC header, verify it, then read the dependent section
226 * and verify that data as well.
227 *
228 * On successful return, the file will be seeked immediately past the
229 * oBCC header.
230 */
231 bool checkHeaderAndDependencies(int fd,
232 uint32_t sourceWhen,
233 uint32_t rslibWhen,
234 uint32_t libRSWhen,
235 uint32_t libbccWhen);
236
237 };
238
239
240
Logan9b504eb2010-11-27 18:19:35 +0800241} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800242
243#endif // BCC_COMPILER_H