blob: 6a1a9e8f1e44bd45bfe052d6d60c7bc1aeb21b3f [file] [log] [blame]
Logancf3e5212010-12-29 01:44:55 +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_SCRIPTCOMPILED_H
18#define BCC_SCRIPTCOMPILED_H
19
20#include "Compiler.h"
Logan Chien7890d432011-08-03 14:55:17 +080021#include "Script.h"
Logancf3e5212010-12-29 01:44:55 +080022
23#include <bcc/bcc.h>
24
Logan7dcaac92011-01-06 04:26:23 +080025#include <list>
26#include <map>
27#include <string>
28#include <utility>
29#include <vector>
30
Logancf3e5212010-12-29 01:44:55 +080031namespace llvm {
32 class Module;
33}
34
35namespace bcc {
Logancf3e5212010-12-29 01:44:55 +080036 class ScriptCompiled {
Logan2a6dc822011-01-06 04:05:20 +080037 friend class Compiler;
Logan7dcaac92011-01-06 04:26:23 +080038 friend class CodeEmitter;
Logan2a6dc822011-01-06 04:05:20 +080039
40 private:
Logan7dcaac92011-01-06 04:26:23 +080041 typedef std::list<std::pair<std::string, std::string> > PragmaList;
Logan2a6dc822011-01-06 04:05:20 +080042 typedef std::list<void*> ExportVarList;
43 typedef std::list<void*> ExportFuncList;
Loganf340bf72011-01-14 17:51:40 +080044 typedef std::map<std::string, FuncInfo *> FuncInfoMap;
Stephen Hines071288a2011-01-27 14:38:26 -080045 typedef std::list<uint32_t> ObjectSlotList;
Logan2a6dc822011-01-06 04:05:20 +080046
Logancf3e5212010-12-29 01:44:55 +080047 private:
48 Script *mpOwner;
49
50 Compiler mCompiler;
51
Logan2a6dc822011-01-06 04:05:20 +080052 ExportVarList mExportVars;
Joseph Wenf36637f2011-07-06 18:27:12 -070053
54 std::vector<std::string> mExportVarsName;
55 std::vector<std::string> mExportFuncsName;
56
Logan2a6dc822011-01-06 04:05:20 +080057 ExportFuncList mExportFuncs;
Logan02286cb2011-01-07 00:30:47 +080058 PragmaList mPragmas;
Stephen Hines071288a2011-01-27 14:38:26 -080059 ObjectSlotList mObjectSlots;
Logan02286cb2011-01-07 00:30:47 +080060
Loganf340bf72011-01-14 17:51:40 +080061 FuncInfoMap mEmittedFunctions;
Logan2a6dc822011-01-06 04:05:20 +080062
Logan Chiend2a5f302011-07-19 20:32:25 +080063#if USE_OLD_JIT
Logan02286cb2011-01-07 00:30:47 +080064 char *mContext; // Context of BCC script (code and data)
Logan Chiend2a5f302011-07-19 20:32:25 +080065#endif
Logan02286cb2011-01-07 00:30:47 +080066
Logancf3e5212010-12-29 01:44:55 +080067 public:
Logan65719812011-01-07 11:17:14 +080068 ScriptCompiled(Script *owner)
Logan Chiend2a5f302011-07-19 20:32:25 +080069 : mpOwner(owner), mCompiler(this)
70#if USE_OLD_JIT
71 , mContext(NULL)
72#endif
73 {
Logancf3e5212010-12-29 01:44:55 +080074 }
75
Logan7dcaac92011-01-06 04:26:23 +080076 ~ScriptCompiled();
77
Logan474cbd22011-01-31 01:47:44 +080078 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM) {
79 return mCompiler.parseBitcodeFile(MEM);
Logancf3e5212010-12-29 01:44:55 +080080 }
81
Logan474cbd22011-01-31 01:47:44 +080082 int readModule(llvm::Module *module) {
Loganf340bf72011-01-14 17:51:40 +080083 return mCompiler.readModule(module);
84 }
85
Logan474cbd22011-01-31 01:47:44 +080086 int linkModule(llvm::Module *module) {
87 return mCompiler.linkModule(module);
Logancf3e5212010-12-29 01:44:55 +080088 }
89
Joseph Wen34c600a2011-07-25 17:59:17 -070090 int compile(bool compileOnly) {
91 return mCompiler.compile(compileOnly);
Logancf3e5212010-12-29 01:44:55 +080092 }
93
94 char const *getCompilerErrorMessage() {
95 return mCompiler.getErrorMessage();
96 }
97
Logan7dcaac92011-01-06 04:26:23 +080098 void *lookup(const char *name);
Logancf3e5212010-12-29 01:44:55 +080099
Logancf3e5212010-12-29 01:44:55 +0800100
Loganbe79ada2011-01-13 01:33:45 +0800101 size_t getExportVarCount() const {
102 return mExportVars.size();
103 }
Logancf3e5212010-12-29 01:44:55 +0800104
Loganbe79ada2011-01-13 01:33:45 +0800105 size_t getExportFuncCount() const {
106 return mExportFuncs.size();
107 }
Logancf3e5212010-12-29 01:44:55 +0800108
Loganbe79ada2011-01-13 01:33:45 +0800109 size_t getPragmaCount() const {
110 return mPragmas.size();
111 }
Logancf3e5212010-12-29 01:44:55 +0800112
Loganbe79ada2011-01-13 01:33:45 +0800113 size_t getFuncCount() const {
114 return mEmittedFunctions.size();
115 }
116
Stephen Hines071288a2011-01-27 14:38:26 -0800117 size_t getObjectSlotCount() const {
118 return mObjectSlots.size();
119 }
Loganbe79ada2011-01-13 01:33:45 +0800120
121 void getExportVarList(size_t varListSize, void **varList);
122
123 void getExportFuncList(size_t funcListSize, void **funcList);
124
Joseph Wenf36637f2011-07-06 18:27:12 -0700125 void getExportVarNameList(std::vector<std::string> &varList);
126
127 void getExportFuncNameList(std::vector<std::string> &funcList);
128
Loganbe79ada2011-01-13 01:33:45 +0800129 void getPragmaList(size_t pragmaListSize,
130 char const **keyList,
131 char const **valueList);
132
Loganf340bf72011-01-14 17:51:40 +0800133 void getFuncInfoList(size_t funcInfoListSize,
134 FuncInfo *funcInfoList);
Logancf3e5212010-12-29 01:44:55 +0800135
Stephen Hines071288a2011-01-27 14:38:26 -0800136 void getObjectSlotList(size_t objectSlotListSize,
137 uint32_t *objectSlotList);
138
Logan Chien7890d432011-08-03 14:55:17 +0800139 std::vector<char const *> const & getUserDefinedExternalSymbols() const {
140 return mpOwner->getUserDefinedExternalSymbols();
141 }
142
Logan Chiend2a5f302011-07-19 20:32:25 +0800143#if USE_OLD_JIT
Logana27a83f2011-01-07 10:25:48 +0800144 char *getContext() {
Logan02286cb2011-01-07 00:30:47 +0800145 return mContext;
146 }
Logan Chiend2a5f302011-07-19 20:32:25 +0800147#endif
148
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700149#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
Logan02286cb2011-01-07 00:30:47 +0800158
Loganf340bf72011-01-14 17:51:40 +0800159 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logancf3e5212010-12-29 01:44:55 +0800160 mCompiler.registerSymbolCallback(pFn, pContext);
161 }
Logancf3e5212010-12-29 01:44:55 +0800162 };
163
164} // namespace bcc
165
166#endif // BCC_SCRIPTCOMPILED_H