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_SCRIPTCOMPILED_H
18#define BCC_SCRIPTCOMPILED_H
19
20#include "Compiler.h"
21#include "Script.h"
22
23#include <bcc/bcc.h>
24
25#include <list>
26#include <map>
27#include <string>
28#include <utility>
29#include <vector>
30
31namespace llvm {
32  class Module;
33}
34
35namespace bcc {
36  class ScriptCompiled {
37    friend class Compiler;
38    friend class CodeEmitter;
39
40  private:
41    typedef std::list<std::pair<std::string, std::string> > PragmaList;
42    typedef std::list<void*> ExportVarList;
43    typedef std::list<void*> ExportFuncList;
44    typedef std::map<std::string, FuncInfo *> FuncInfoMap;
45    typedef std::list<uint32_t> ObjectSlotList;
46
47  private:
48    Script *mpOwner;
49
50    Compiler mCompiler;
51
52    ExportVarList mExportVars;
53
54    std::vector<std::string> mExportVarsName;
55    std::vector<std::string> mExportFuncsName;
56
57    ExportFuncList mExportFuncs;
58    PragmaList mPragmas;
59    ObjectSlotList mObjectSlots;
60
61    FuncInfoMap mEmittedFunctions;
62
63#if USE_OLD_JIT
64    char *mContext; // Context of BCC script (code and data)
65#endif
66
67  public:
68    ScriptCompiled(Script *owner)
69      : mpOwner(owner), mCompiler(this)
70#if USE_OLD_JIT
71        , mContext(NULL)
72#endif
73    {
74    }
75
76    ~ScriptCompiled();
77
78    llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM) {
79      return mCompiler.parseBitcodeFile(MEM);
80    }
81
82    int readModule(llvm::Module *module) {
83      return mCompiler.readModule(module);
84    }
85
86    int linkModule(llvm::Module *module) {
87      return mCompiler.linkModule(module);
88    }
89
90    int compile(bool compileOnly) {
91      return mCompiler.compile(compileOnly);
92    }
93
94    char const *getCompilerErrorMessage() {
95      return mCompiler.getErrorMessage();
96    }
97
98    void *lookup(const char *name);
99
100
101    size_t getExportVarCount() const {
102      return mExportVars.size();
103    }
104
105    size_t getExportFuncCount() const {
106      return mExportFuncs.size();
107    }
108
109    size_t getPragmaCount() const {
110      return mPragmas.size();
111    }
112
113    size_t getFuncCount() const {
114      return mEmittedFunctions.size();
115    }
116
117    size_t getObjectSlotCount() const {
118      return mObjectSlots.size();
119    }
120
121    void getExportVarList(size_t varListSize, void **varList);
122
123    void getExportFuncList(size_t funcListSize, void **funcList);
124
125    void getExportVarNameList(std::vector<std::string> &varList);
126
127    void getExportFuncNameList(std::vector<std::string> &funcList);
128
129    void getPragmaList(size_t pragmaListSize,
130                       char const **keyList,
131                       char const **valueList);
132
133    void getFuncInfoList(size_t funcInfoListSize,
134                         FuncInfo *funcInfoList);
135
136    void getObjectSlotList(size_t objectSlotListSize,
137                           uint32_t *objectSlotList);
138
139    std::vector<char const *> const & getUserDefinedExternalSymbols() const {
140      return mpOwner->getUserDefinedExternalSymbols();
141    }
142
143#if USE_OLD_JIT
144    char *getContext() {
145      return mContext;
146    }
147#endif
148
149#if USE_MCJIT
150    const char *getELF() const {
151      return &*mCompiler.getELF().begin();
152    }
153
154    size_t getELFSize() const {
155      return mCompiler.getELF().size();
156    }
157#endif
158
159    void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
160      mCompiler.registerSymbolCallback(pFn, pContext);
161    }
162  };
163
164} // namespace bcc
165
166#endif // BCC_SCRIPTCOMPILED_H
167