blob: 2369f070c882252904bafe8faa183f6543bd8b80 [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 //////////////////////////////////////////////////////////////////////////
54 // The variable section below (e.g., Triple, CodeGenOptLevel)
55 // is initialized in GlobalInitialization()
56 //
57 static bool GlobalInitialized;
58
Logan1f028c02010-11-27 01:02:48 +080059 // If given, this will be the name of the target triple to compile for.
60 // If not given, the initial values defined in this file will be used.
61 static std::string Triple;
62
63 static llvm::CodeGenOpt::Level CodeGenOptLevel;
64
65 // End of section of GlobalInitializing variables
66 /////////////////////////////////////////////////////////////////////////
67 // If given, the name of the target CPU to generate code for.
68 static std::string CPU;
69
70 // The list of target specific features to enable or disable -- this should
71 // be a list of strings starting with '+' (enable) or '-' (disable).
72 static std::vector<std::string> Features;
73
Logan1f028c02010-11-27 01:02:48 +080074 static void GlobalInitialization();
75
76 static void LLVMErrorHandler(void *UserData, const std::string &Message);
77
78 static const llvm::StringRef PragmaMetadataName;
79 static const llvm::StringRef ExportVarMetadataName;
80 static const llvm::StringRef ExportFuncMetadataName;
81
82 friend class CodeEmitter;
83 friend class CodeMemoryManager;
84
Logande2ca792010-11-27 22:15:39 +080085
Logan1f028c02010-11-27 01:02:48 +080086 private:
87 std::string mError;
88
Logan1f028c02010-11-27 01:02:48 +080089 bool mUseCache; // Set by readBC()
90 bool mCacheNew; // Set by readBC()
91 int mCacheFd; // Set by readBC()
92 char *mCacheMapAddr; // Set by loadCacheFile() if mCacheNew is false
93 oBCCHeader *mCacheHdr; // Set by loadCacheFile()
94 size_t mCacheSize; // Set by loadCacheFile()
95 ptrdiff_t mCacheDiff; // Set by loadCacheFile()
Logan83913492011-01-01 01:42:09 +080096 bool mCacheLoadFailed; // Set by loadCacheFile() used by readBC()
Logan1f028c02010-11-27 01:02:48 +080097 char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true.
Logande2ca792010-11-27 22:15:39 +080098 // Used by genCacheFile() for dumping
Logan1f028c02010-11-27 01:02:48 +080099
Logan8b77a772010-12-21 09:11:01 +0800100 unsigned char mSourceSHA1[20]; // Set by readBC()
101
Logan1f028c02010-11-27 01:02:48 +0800102 PragmaList mPragmas;
103
Logan1f028c02010-11-27 01:02:48 +0800104 ExportVarList mExportVars;
105
Logan1f028c02010-11-27 01:02:48 +0800106 ExportFuncList mExportFuncs;
107
108 // The memory manager for code emitter
109 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +0800110
111 // The CodeEmitter
112 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan1f028c02010-11-27 01:02:48 +0800113
114 BCCSymbolLookupFn mpSymbolLookupFn;
115 void *mpSymbolLookupContext;
116
117 llvm::LLVMContext *mContext;
118 llvm::Module *mModule;
119
120 bool mHasLinked;
121
122 public:
123 Compiler();
124
125 // interface for BCCscript::registerSymbolCallback()
126 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
127 mpSymbolLookupFn = pFn;
128 mpSymbolLookupContext = pContext;
129 }
130
Logande2ca792010-11-27 22:15:39 +0800131 CodeMemoryManager *createCodeMemoryManager();
132
133 CodeEmitter *createCodeEmitter();
134
Logan1f028c02010-11-27 01:02:48 +0800135 int readModule(llvm::Module *module) {
136 GlobalInitialization();
137 mModule = module;
138 return hasError();
139 }
140
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800141 int readBC(const char *bitcode,
142 size_t bitcodeSize,
Loganb9b04162010-12-20 16:36:57 +0800143 long bitcodeFileModTime,
144 long bitcodeFileCRC32,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800145 const BCCchar *resName,
146 const BCCchar *cacheDir);
Logan1f028c02010-11-27 01:02:48 +0800147
148 int linkBC(const char *bitcode, size_t bitcodeSize);
149
150 // interface for bccLoadBinary()
151 int loadCacheFile();
152
153 // interace for bccCompileBC()
154 int compile();
155
156 // interface for bccGetScriptInfoLog()
Logan38d06072010-12-29 00:16:39 +0800157 char const *getErrorMessage() {
158 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800159 }
160
161 // interface for bccGetScriptLabel()
162 void *lookup(const char *name);
163
164 // Interface for bccGetExportVars()
165 void getExportVars(BCCsizei *actualVarCount,
166 BCCsizei maxVarCount,
167 BCCvoid **vars);
168
169 // Interface for bccGetExportFuncs()
170 void getExportFuncs(BCCsizei *actualFuncCount,
171 BCCsizei maxFuncCount,
172 BCCvoid **funcs);
173
174 // Interface for bccGetPragmas()
175 void getPragmas(BCCsizei *actualStringCount,
176 BCCsizei maxStringCount,
177 BCCchar **strings);
178
179 // Interface for bccGetFunctions()
180 void getFunctions(BCCsizei *actualFunctionCount,
181 BCCsizei maxFunctionCount,
182 BCCchar **functions);
183
184 // Interface for bccGetFunctionBinary()
185 void getFunctionBinary(BCCchar *function,
186 BCCvoid **base,
187 BCCsizei *length);
188
189 const llvm::Module *getModule() const {
190 return mModule;
191 }
192
193 ~Compiler();
194
195 private:
Logan8b77a772010-12-21 09:11:01 +0800196 void computeSourceSHA1(char const *bitcode, size_t size);
197
Logan1f028c02010-11-27 01:02:48 +0800198 // Note: loadCacheFile() and genCacheFile() go hand in hand
199 void genCacheFile();
200
201 // OpenCacheFile() returns fd of the cache file.
202 // Input:
203 // BCCchar *resName: Used to genCacheFileName()
204 // bool createIfMissing: If false, turn off caching
205 // Output:
206 // returns fd: If -1: Failed
207 // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
208 // cache file's file descriptor
209 // Note: openCacheFile() will check the cache file's validity,
210 // such as Magic number, sourceWhen... dependencies.
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800211 int openCacheFile(const BCCchar *resName,
212 const BCCchar *cacheDir,
213 bool createIfMissing);
Logan1f028c02010-11-27 01:02:48 +0800214
Shih-wei Liaoe6a18512010-12-09 12:38:10 -0800215 char *genCacheFileName(const char *cacheDir,
216 const char *fileName,
217 const char *subFileName);
Logan1f028c02010-11-27 01:02:48 +0800218
219 /*
220 * Read the oBCC header, verify it, then read the dependent section
221 * and verify that data as well.
222 *
223 * On successful return, the file will be seeked immediately past the
224 * oBCC header.
225 */
226 bool checkHeaderAndDependencies(int fd,
Loganb9b04162010-12-20 16:36:57 +0800227 long sourceWhen,
Logan1f028c02010-11-27 01:02:48 +0800228 uint32_t rslibWhen,
229 uint32_t libRSWhen,
230 uint32_t libbccWhen);
231
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800232
233 struct {
234 bool mNoCache;
235 } props;
236
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
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800252 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800253
Logan9b504eb2010-11-27 18:19:35 +0800254} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800255
256#endif // BCC_COMPILER_H