blob: e4acc1689eaae87fae0f3e30b4e370f75146a035 [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 {
Zonr Chang2fcbd022012-01-06 21:04:31 +080036 struct CompilerOption;
37
Logancf3e5212010-12-29 01:44:55 +080038 class ScriptCompiled {
Logan2a6dc822011-01-06 04:05:20 +080039 friend class Compiler;
Logan7dcaac92011-01-06 04:26:23 +080040 friend class CodeEmitter;
Logan2a6dc822011-01-06 04:05:20 +080041
42 private:
Logan7dcaac92011-01-06 04:26:23 +080043 typedef std::list<std::pair<std::string, std::string> > PragmaList;
Logan2a6dc822011-01-06 04:05:20 +080044 typedef std::list<void*> ExportVarList;
45 typedef std::list<void*> ExportFuncList;
Loganf340bf72011-01-14 17:51:40 +080046 typedef std::map<std::string, FuncInfo *> FuncInfoMap;
Stephen Hines071288a2011-01-27 14:38:26 -080047 typedef std::list<uint32_t> ObjectSlotList;
Logan2a6dc822011-01-06 04:05:20 +080048
Logancf3e5212010-12-29 01:44:55 +080049 private:
50 Script *mpOwner;
51
52 Compiler mCompiler;
53
Logan2a6dc822011-01-06 04:05:20 +080054 ExportVarList mExportVars;
Joseph Wenf36637f2011-07-06 18:27:12 -070055
56 std::vector<std::string> mExportVarsName;
57 std::vector<std::string> mExportFuncsName;
58
Logan2a6dc822011-01-06 04:05:20 +080059 ExportFuncList mExportFuncs;
Logan02286cb2011-01-07 00:30:47 +080060 PragmaList mPragmas;
Stephen Hines071288a2011-01-27 14:38:26 -080061 ObjectSlotList mObjectSlots;
Logan02286cb2011-01-07 00:30:47 +080062
Loganf340bf72011-01-14 17:51:40 +080063 FuncInfoMap mEmittedFunctions;
Logan2a6dc822011-01-06 04:05:20 +080064
Logan Chiend2a5f302011-07-19 20:32:25 +080065#if USE_OLD_JIT
Logan02286cb2011-01-07 00:30:47 +080066 char *mContext; // Context of BCC script (code and data)
Logan Chiend2a5f302011-07-19 20:32:25 +080067#endif
Logan02286cb2011-01-07 00:30:47 +080068
Logancf3e5212010-12-29 01:44:55 +080069 public:
Logan65719812011-01-07 11:17:14 +080070 ScriptCompiled(Script *owner)
Logan Chiend2a5f302011-07-19 20:32:25 +080071 : mpOwner(owner), mCompiler(this)
72#if USE_OLD_JIT
73 , mContext(NULL)
74#endif
75 {
Logancf3e5212010-12-29 01:44:55 +080076 }
77
Logan7dcaac92011-01-06 04:26:23 +080078 ~ScriptCompiled();
79
Logan474cbd22011-01-31 01:47:44 +080080 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM) {
81 return mCompiler.parseBitcodeFile(MEM);
Logancf3e5212010-12-29 01:44:55 +080082 }
83
Logan474cbd22011-01-31 01:47:44 +080084 int readModule(llvm::Module *module) {
Loganf340bf72011-01-14 17:51:40 +080085 return mCompiler.readModule(module);
86 }
87
Logan474cbd22011-01-31 01:47:44 +080088 int linkModule(llvm::Module *module) {
89 return mCompiler.linkModule(module);
Logancf3e5212010-12-29 01:44:55 +080090 }
91
Zonr Chang2fcbd022012-01-06 21:04:31 +080092 int compile(CompilerOption &option) {
93 return mCompiler.compile(option);
Logancf3e5212010-12-29 01:44:55 +080094 }
95
96 char const *getCompilerErrorMessage() {
97 return mCompiler.getErrorMessage();
98 }
99
Logan7dcaac92011-01-06 04:26:23 +0800100 void *lookup(const char *name);
Logancf3e5212010-12-29 01:44:55 +0800101
Logancf3e5212010-12-29 01:44:55 +0800102
Loganbe79ada2011-01-13 01:33:45 +0800103 size_t getExportVarCount() const {
104 return mExportVars.size();
105 }
Logancf3e5212010-12-29 01:44:55 +0800106
Loganbe79ada2011-01-13 01:33:45 +0800107 size_t getExportFuncCount() const {
108 return mExportFuncs.size();
109 }
Logancf3e5212010-12-29 01:44:55 +0800110
Loganbe79ada2011-01-13 01:33:45 +0800111 size_t getPragmaCount() const {
112 return mPragmas.size();
113 }
Logancf3e5212010-12-29 01:44:55 +0800114
Loganbe79ada2011-01-13 01:33:45 +0800115 size_t getFuncCount() const {
116 return mEmittedFunctions.size();
117 }
118
Stephen Hines071288a2011-01-27 14:38:26 -0800119 size_t getObjectSlotCount() const {
120 return mObjectSlots.size();
121 }
Loganbe79ada2011-01-13 01:33:45 +0800122
123 void getExportVarList(size_t varListSize, void **varList);
124
125 void getExportFuncList(size_t funcListSize, void **funcList);
126
Joseph Wenf36637f2011-07-06 18:27:12 -0700127 void getExportVarNameList(std::vector<std::string> &varList);
128
129 void getExportFuncNameList(std::vector<std::string> &funcList);
130
Loganbe79ada2011-01-13 01:33:45 +0800131 void getPragmaList(size_t pragmaListSize,
132 char const **keyList,
133 char const **valueList);
134
Loganf340bf72011-01-14 17:51:40 +0800135 void getFuncInfoList(size_t funcInfoListSize,
136 FuncInfo *funcInfoList);
Logancf3e5212010-12-29 01:44:55 +0800137
Stephen Hines071288a2011-01-27 14:38:26 -0800138 void getObjectSlotList(size_t objectSlotListSize,
139 uint32_t *objectSlotList);
140
Logan Chien7890d432011-08-03 14:55:17 +0800141 std::vector<char const *> const & getUserDefinedExternalSymbols() const {
142 return mpOwner->getUserDefinedExternalSymbols();
143 }
144
Logan Chiend2a5f302011-07-19 20:32:25 +0800145#if USE_OLD_JIT
Logana27a83f2011-01-07 10:25:48 +0800146 char *getContext() {
Logan02286cb2011-01-07 00:30:47 +0800147 return mContext;
148 }
Logan Chiend2a5f302011-07-19 20:32:25 +0800149#endif
150
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700151#if USE_MCJIT
152 const char *getELF() const {
153 return &*mCompiler.getELF().begin();
154 }
155
156 size_t getELFSize() const {
157 return mCompiler.getELF().size();
158 }
159#endif
Logan02286cb2011-01-07 00:30:47 +0800160
Loganf340bf72011-01-14 17:51:40 +0800161 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logancf3e5212010-12-29 01:44:55 +0800162 mCompiler.registerSymbolCallback(pFn, pContext);
163 }
Logancf3e5212010-12-29 01:44:55 +0800164 };
165
166} // namespace bcc
167
168#endif // BCC_SCRIPTCOMPILED_H