• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010-2012, 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_SCRIPT_H
18 #define BCC_SCRIPT_H
19 
20 #include <bcc/bcc.h>
21 #include "bcc_internal.h"
22 
23 #include "Compiler.h"
24 
25 #include <llvm/Support/CodeGen.h>
26 
27 #include <vector>
28 #include <string>
29 
30 #include <stddef.h>
31 
32 namespace llvm {
33   class Module;
34   class GDBJITRegistrar;
35 }
36 
37 namespace bcc {
38   class ScriptCompiled;
39   class ScriptCached;
40   class SourceInfo;
41   struct CompilerOption;
42 
43   namespace ScriptStatus {
44     enum StatusType {
45       Unknown,
46       Compiled,
47       Cached
48     };
49   }
50 
51   namespace ScriptObject {
52     enum ObjectType {
53       Unknown,
54       Relocatable,
55       SharedObject,
56       Executable,
57     };
58   }
59 
60   class Script {
61   private:
62     int mErrorCode;
63 
64     ScriptStatus::StatusType mStatus;
65     // The type of the object behind this script after compilation. For
66     // example, after returning from a successful call to prepareRelocatable(),
67     // the value of mObjectType will be ScriptObject::Relocatable.
68     ScriptObject::ObjectType mObjectType;
69 
70     union {
71       ScriptCompiled *mCompiled;
72       ScriptCached *mCached;
73     };
74 
75     std::string mCacheDir;
76     std::string mCacheName;
77 
getCachedObjectPath()78     inline std::string getCachedObjectPath() const {
79       return std::string(mCacheDir + mCacheName + ".o");
80     }
81 
getCacheInfoPath()82     inline std::string getCacheInfoPath() const {
83       return getCachedObjectPath().append(".info");
84     }
85 
86     bool mIsContextSlotNotAvail;
87 
88     // Source List
89     SourceInfo *mSourceList[2];
90     // Note: mSourceList[0] (main source)
91     // Note: mSourceList[1] (library source)
92     // TODO(logan): Generalize this, use vector or SmallVector instead!
93 
94     // External Function List
95     std::vector<char const *> mUserDefinedExternalSymbols;
96 
97     // Register Symbol Lookup Function
98     BCCSymbolLookupFn mpExtSymbolLookupFn;
99     void *mpExtSymbolLookupFnContext;
100 
101   public:
Script()102     Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
103                mObjectType(ScriptObject::Unknown),
104                mIsContextSlotNotAvail(false),
105                mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
106       Compiler::GlobalInitialization();
107 
108       mSourceList[0] = NULL;
109       mSourceList[1] = NULL;
110     }
111 
112     ~Script();
113 
114     int addSourceBC(size_t idx,
115                     char const *resName,
116                     const char *bitcode,
117                     size_t bitcodeSize,
118                     unsigned long flags);
119 
120     int addSourceModule(size_t idx,
121                         llvm::Module *module,
122                         unsigned long flags);
123 
124     int addSourceFile(size_t idx,
125                       char const *path,
126                       unsigned long flags);
127 
markExternalSymbol(char const * name)128     void markExternalSymbol(char const *name) {
129       mUserDefinedExternalSymbols.push_back(name);
130     }
131 
getUserDefinedExternalSymbols()132     std::vector<char const *> const &getUserDefinedExternalSymbols() const {
133       return mUserDefinedExternalSymbols;
134     }
135 
136     int prepareExecutable(char const *cacheDir,
137                           char const *cacheName,
138                           unsigned long flags);
139     int writeCache();
140 
141     /*
142      * Link the given bitcodes in mSourceList to shared object (.so).
143      *
144      * Currently, it requires one to provide the relocatable object files with
145      * given bitcodes to output a shared object.
146      *
147      * The usage of this function is flexible. You can have a relocatable object
148      * compiled before and pass it in objPath to generate shared object. If the
149      * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
150      * you haven't done that yet) and then link the output relocatable object
151      * file to .so in dsoPath.
152      *
153      * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
154      *
155      */
156     int prepareSharedObject(char const *objPath,
157                             char const *dsoPath,
158                             unsigned long flags);
159 
160     int prepareRelocatable(char const *objPath,
161                            llvm::Reloc::Model RelocModel,
162                            unsigned long flags);
163 
164     char const *getCompilerErrorMessage();
165 
166     void *lookup(const char *name);
167 
168     size_t getExportVarCount() const;
169 
170     size_t getExportFuncCount() const;
171 
172     size_t getExportForEachCount() const;
173 
174     size_t getPragmaCount() const;
175 
176     size_t getFuncCount() const;
177 
178     size_t getObjectSlotCount() const;
179 
180     void getExportVarList(size_t size, void **list);
181 
182     void getExportFuncList(size_t size, void **list);
183 
184     void getExportForEachList(size_t size, void **list);
185 
186     void getExportVarNameList(std::vector<std::string> &list);
187 
188     void getExportFuncNameList(std::vector<std::string> &list);
189 
190     void getExportForEachNameList(std::vector<std::string> &list);
191 
192     void getPragmaList(size_t size,
193                        char const **keyList,
194                        char const **valueList);
195 
196     void getFuncInfoList(size_t size, FuncInfo *list);
197 
198     void getObjectSlotList(size_t size, uint32_t *list);
199 
200     size_t getELFSize() const;
201 
202     const char *getELF() const;
203 
204     int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
205 
206     bool isCacheable() const;
207 
setError(int error)208     void setError(int error) {
209       if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
210         mErrorCode = error;
211       }
212     }
213 
getError()214     int getError() {
215       int result = mErrorCode;
216       mErrorCode = BCC_NO_ERROR;
217       return result;
218     }
219 
220   private:
221     //
222     // It returns 0 if there's a cache hit.
223     //
224     // Side effect: it will set mCacheDir, mCacheName.
225     int internalLoadCache(char const *cacheDir, char const *cacheName,
226                           bool checkOnly);
227 
228     int internalCompile(const CompilerOption&);
229   };
230 
231 } // namespace bcc
232 
233 #endif // BCC_SCRIPT_H
234