• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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_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 <vector>
26 #include <string>
27 
28 #include <stddef.h>
29 
30 namespace llvm {
31   class Module;
32 }
33 
34 namespace bcc {
35   class ScriptCompiled;
36   class ScriptCached;
37   class SourceInfo;
38 
39   namespace ScriptStatus {
40     enum StatusType {
41       Unknown,
42       Compiled,
43 #if USE_CACHE
44       Cached,
45 #endif
46     };
47   }
48 
49   class Script {
50   private:
51     int mErrorCode;
52 
53     ScriptStatus::StatusType mStatus;
54 
55     union {
56       ScriptCompiled *mCompiled;
57 #if USE_CACHE
58       ScriptCached *mCached;
59 #endif
60     };
61 
62 #if USE_CACHE
63     std::string mCacheDir;
64     std::string mCacheName;
65 #endif
66 
67     bool mIsContextSlotNotAvail;
68 
69     // Source List
70     SourceInfo *mSourceList[2];
71     // Note: mSourceList[0] (main source)
72     // Note: mSourceList[1] (library source)
73     // TODO(logan): Generalize this, use vector or SmallVector instead!
74 
75     // External Function List
76     std::vector<char const *> mUserDefinedExternalSymbols;
77 
78     // Register Symbol Lookup Function
79     BCCSymbolLookupFn mpExtSymbolLookupFn;
80     void *mpExtSymbolLookupFnContext;
81 
82   public:
Script()83     Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
84                mIsContextSlotNotAvail(false),
85                mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
86       Compiler::GlobalInitialization();
87 
88       mSourceList[0] = NULL;
89       mSourceList[1] = NULL;
90     }
91 
92     ~Script();
93 
94     int addSourceBC(size_t idx,
95                     char const *resName,
96                     const char *bitcode,
97                     size_t bitcodeSize,
98                     unsigned long flags);
99 
100     int addSourceModule(size_t idx,
101                         llvm::Module *module,
102                         unsigned long flags);
103 
104     int addSourceFile(size_t idx,
105                       char const *path,
106                       unsigned long flags);
107 
markExternalSymbol(char const * name)108     void markExternalSymbol(char const *name) {
109       mUserDefinedExternalSymbols.push_back(name);
110     }
111 
getUserDefinedExternalSymbols()112     std::vector<char const *> const &getUserDefinedExternalSymbols() const {
113       return mUserDefinedExternalSymbols;
114     }
115 
116     int prepareExecutable(char const *cacheDir,
117                           char const *cacheName,
118                           unsigned long flags);
119 
120     int prepareSharedObject(char const *cacheDir,
121                           char const *cacheName,
122                           unsigned long flags);
123 
124     char const *getCompilerErrorMessage();
125 
126     void *lookup(const char *name);
127 
128 
129     size_t getExportVarCount() const;
130 
131     size_t getExportFuncCount() const;
132 
133     size_t getPragmaCount() const;
134 
135     size_t getFuncCount() const;
136 
137     size_t getObjectSlotCount() const;
138 
139     void getExportVarList(size_t size, void **list);
140 
141     void getExportFuncList(size_t size, void **list);
142 
143     void getExportVarNameList(std::vector<std::string> &list);
144 
145     void getExportFuncNameList(std::vector<std::string> &list);
146 
147     void getPragmaList(size_t size,
148                        char const **keyList,
149                        char const **valueList);
150 
151     void getFuncInfoList(size_t size, FuncInfo *list);
152 
153     void getObjectSlotList(size_t size, uint32_t *list);
154 
155     size_t getELFSize() const;
156 
157     const char *getELF() const;
158 
159     int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
160 
161 #if USE_OLD_JIT
162     char *getContext();
163 #endif
164 
165 
setError(int error)166     void setError(int error) {
167       if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
168         mErrorCode = error;
169       }
170     }
171 
getError()172     int getError() {
173       int result = mErrorCode;
174       mErrorCode = BCC_NO_ERROR;
175       return result;
176     }
177 
178   private:
179 #if USE_CACHE
180     int internalLoadCache(bool checkOnly);
181 #endif
182     int internalCompile(bool compileOnly);
183 
184   };
185 
186 } // namespace bcc
187 
188 #endif // BCC_SCRIPT_H
189