blob: 4595d419f79bb76d032dae131218c175098b8ffd [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +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
Loganc4395232010-11-27 18:54:17 +080017#include "Compiler.h"
Logan1f028c02010-11-27 01:02:48 +080018
Logan35849002011-01-15 07:30:43 +080019#include "Config.h"
20
Loganeb3d12b2010-12-16 06:20:18 +080021#include "ContextManager.h"
Logan4dcd6792011-02-28 05:12:00 +080022#include "DebugHelper.h"
Shih-wei Liao6c0c7b02011-05-21 21:47:14 -070023#include "FileHandle.h"
Logan Chienda5e0c32011-06-13 03:47:21 +080024#include "Runtime.h"
Logan2a6dc822011-01-06 04:05:20 +080025#include "ScriptCompiled.h"
Logan75cc8a52011-01-07 06:06:52 +080026#include "Sha1Helper.h"
Loganeb3d12b2010-12-16 06:20:18 +080027
Shih-wei Liao320b5492011-06-20 22:53:33 -070028#if USE_MCJIT
Logan Chienda5e0c32011-06-13 03:47:21 +080029#include "librsloader.h"
Shih-wei Liao320b5492011-06-20 22:53:33 -070030#endif
Logan Chienda5e0c32011-06-13 03:47:21 +080031
Logandf23afa2010-11-27 11:04:54 +080032#include "llvm/ADT/StringRef.h"
Logan1f028c02010-11-27 01:02:48 +080033
Logandf23afa2010-11-27 11:04:54 +080034#include "llvm/Analysis/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080035
Logan1f028c02010-11-27 01:02:48 +080036#include "llvm/Bitcode/ReaderWriter.h"
37
Logan1f028c02010-11-27 01:02:48 +080038#include "llvm/CodeGen/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080039#include "llvm/CodeGen/RegAllocRegistry.h"
40#include "llvm/CodeGen/SchedulerRegistry.h"
Logan1f028c02010-11-27 01:02:48 +080041
Logandf23afa2010-11-27 11:04:54 +080042#include "llvm/Transforms/IPO.h"
43#include "llvm/Transforms/Scalar.h"
44
45#include "llvm/Target/SubtargetFeature.h"
46#include "llvm/Target/TargetData.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetOptions.h"
49#include "llvm/Target/TargetRegistry.h"
50#include "llvm/Target/TargetSelect.h"
51
Shih-wei Liao90cd3d12011-06-20 15:43:34 -070052#if USE_DISASSEMBLER
53#include "llvm/MC/MCAsmInfo.h"
54#include "llvm/MC/MCDisassembler.h"
55#include "llvm/MC/MCInst.h"
56#include "llvm/MC/MCInstPrinter.h"
57#include "llvm/Support/MemoryObject.h"
58#include "llvm/LLVMContext.h"
59#endif
60
Logandf23afa2010-11-27 11:04:54 +080061#include "llvm/Support/ErrorHandling.h"
Shih-wei Liao898c5a92011-05-18 07:02:39 -070062#include "llvm/Support/FormattedStream.h"
Logandf23afa2010-11-27 11:04:54 +080063#include "llvm/Support/MemoryBuffer.h"
64
Shih-wei Liao90cd3d12011-06-20 15:43:34 -070065#include "llvm/Type.h"
Logandf23afa2010-11-27 11:04:54 +080066#include "llvm/GlobalValue.h"
67#include "llvm/Linker.h"
68#include "llvm/LLVMContext.h"
69#include "llvm/Metadata.h"
70#include "llvm/Module.h"
71#include "llvm/PassManager.h"
72#include "llvm/Value.h"
73
74#include <errno.h>
75#include <sys/file.h>
Logandf23afa2010-11-27 11:04:54 +080076#include <sys/stat.h>
77#include <sys/types.h>
78#include <unistd.h>
79
Logan75cc8a52011-01-07 06:06:52 +080080#include <string.h>
Logan8b77a772010-12-21 09:11:01 +080081
Logandf23afa2010-11-27 11:04:54 +080082#include <string>
83#include <vector>
Logan1f028c02010-11-27 01:02:48 +080084
Shih-wei Liao90cd3d12011-06-20 15:43:34 -070085namespace {
86
87#if USE_DISASSEMBLER
88class BufferMemoryObject : public llvm::MemoryObject {
89private:
90 const uint8_t *mBytes;
91 uint64_t mLength;
92
93public:
94 BufferMemoryObject(const uint8_t *Bytes, uint64_t Length)
95 : mBytes(Bytes), mLength(Length) {
96 }
97
98 virtual uint64_t getBase() const { return 0; }
99 virtual uint64_t getExtent() const { return mLength; }
100
101 virtual int readByte(uint64_t Addr, uint8_t *Byte) const {
102 if (Addr > getExtent())
103 return -1;
104 *Byte = mBytes[Addr];
105 return 0;
106 }
107};
108#endif
109
110}; // namespace anonymous
111
112#define DEBUG_MCJIT REFLECT 0
Shih-wei Liao320b5492011-06-20 22:53:33 -0700113#define DEBUG_MCJIT_DISASSEMBLE 0
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700114
Logan1f028c02010-11-27 01:02:48 +0800115namespace bcc {
116
117//////////////////////////////////////////////////////////////////////////////
118// BCC Compiler Static Variables
119//////////////////////////////////////////////////////////////////////////////
120
121bool Compiler::GlobalInitialized = false;
122
Logan1f028c02010-11-27 01:02:48 +0800123// Code generation optimization level for the compiler
124llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel;
125
126std::string Compiler::Triple;
127
128std::string Compiler::CPU;
129
130std::vector<std::string> Compiler::Features;
131
Stephen Hines071288a2011-01-27 14:38:26 -0800132// Name of metadata node where pragma info resides (should be synced with
Logan1f028c02010-11-27 01:02:48 +0800133// slang.cpp)
134const llvm::StringRef Compiler::PragmaMetadataName = "#pragma";
135
Stephen Hines071288a2011-01-27 14:38:26 -0800136// Name of metadata node where exported variable names reside (should be
Logan1f028c02010-11-27 01:02:48 +0800137// synced with slang_rs_metadata.h)
138const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var";
139
Stephen Hines071288a2011-01-27 14:38:26 -0800140// Name of metadata node where exported function names reside (should be
Logan1f028c02010-11-27 01:02:48 +0800141// synced with slang_rs_metadata.h)
142const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func";
143
Stephen Hines071288a2011-01-27 14:38:26 -0800144// Name of metadata node where RS object slot info resides (should be
145// synced with slang_rs_metadata.h)
146const llvm::StringRef Compiler::ObjectSlotMetadataName = "#rs_object_slots";
Logan1f028c02010-11-27 01:02:48 +0800147
148//////////////////////////////////////////////////////////////////////////////
149// Compiler
150//////////////////////////////////////////////////////////////////////////////
151
152void Compiler::GlobalInitialization() {
153 if (GlobalInitialized)
154 return;
155
Joseph Wen5de1adf2011-06-21 15:41:31 -0700156 LOGI("LIBBCC BUILD CHECKSUM: %s\n", libbcc_build_checksum);
Logan87066272010-12-29 00:34:32 +0800157
Logan1f028c02010-11-27 01:02:48 +0800158 // if (!llvm::llvm_is_multithreaded())
159 // llvm::llvm_start_multithreaded();
160
161 // Set Triple, CPU and Features here
162 Triple = TARGET_TRIPLE_STRING;
163
Logan1f028c02010-11-27 01:02:48 +0800164 Features.push_back("+vfp3");
165 Features.push_back("+d16");
166
Logan4fe966f2011-02-27 08:26:40 +0800167 // NOTE: Currently, we have to turn off the support for NEON explicitly.
168 // Since the ARMCodeEmitter.cpp is not ready for JITing NEON
169 // instructions.
170 Features.push_back("-neon"); // TODO(sliao): NEON for JIT
171 Features.push_back("-neonfp");
172 Features.push_back("-vmlx");
173
Logan1f028c02010-11-27 01:02:48 +0800174#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
175 LLVMInitializeARMTargetInfo();
176 LLVMInitializeARMTarget();
Logan35849002011-01-15 07:30:43 +0800177#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800178 LLVMInitializeARMDisassembler();
179 LLVMInitializeARMAsmPrinter();
180#endif
181#endif
182
183#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
184 LLVMInitializeX86TargetInfo();
185 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800186#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800187 LLVMInitializeX86Disassembler();
188 LLVMInitializeX86AsmPrinter();
189#endif
190#endif
191
192#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
193 LLVMInitializeX86TargetInfo();
194 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800195#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800196 LLVMInitializeX86Disassembler();
197 LLVMInitializeX86AsmPrinter();
198#endif
199#endif
200
201 // -O0: llvm::CodeGenOpt::None
202 // -O1: llvm::CodeGenOpt::Less
203 // -O2: llvm::CodeGenOpt::Default
204 // -O3: llvm::CodeGenOpt::Aggressive
Shih-wei Liao72f67a62010-12-14 13:36:15 -0800205 CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
Logan1f028c02010-11-27 01:02:48 +0800206
207 // Below are the global settings to LLVM
208
209 // Disable frame pointer elimination optimization
210 llvm::NoFramePointerElim = false;
211
212 // Use hardfloat ABI
213 //
214 // TODO(all): Need to detect the CPU capability and decide whether to use
215 // softfp. To use softfp, change following 2 lines to
216 //
217 // llvm::FloatABIType = llvm::FloatABI::Soft;
218 // llvm::UseSoftFloat = true;
219 //
Shih-wei Liaoe728cb82010-12-15 15:20:47 -0800220 llvm::FloatABIType = llvm::FloatABI::Soft;
Logan1f028c02010-11-27 01:02:48 +0800221 llvm::UseSoftFloat = false;
222
223 // BCC needs all unknown symbols resolved at JIT/compilation time.
224 // So we don't need any dynamic relocation model.
225 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static);
226
227#if defined(DEFAULT_X64_CODEGEN)
228 // Data address in X86_64 architecture may reside in a far-away place
229 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium);
230#else
231 // This is set for the linker (specify how large of the virtual addresses
232 // we can access for all unknown symbols.)
233 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small);
234#endif
235
236 // Register the scheduler
237 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
238
239 // Register allocation policy:
240 // createFastRegisterAllocator: fast but bad quality
241 // createLinearScanRegisterAllocator: not so fast but good quality
242 llvm::RegisterRegAlloc::setDefault
243 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
244 llvm::createFastRegisterAllocator :
245 llvm::createLinearScanRegisterAllocator);
246
Logan35849002011-01-15 07:30:43 +0800247#if USE_CACHE
Logan75cc8a52011-01-07 06:06:52 +0800248 // Calculate the SHA1 checksum of libbcc and libRS.
Logan35849002011-01-15 07:30:43 +0800249#if USE_LIBBCC_SHA1SUM
Logan75cc8a52011-01-07 06:06:52 +0800250 calcFileSHA1(sha1LibBCC, pathLibBCC);
Logane1323992011-01-12 04:47:13 +0800251#endif
Logan75cc8a52011-01-07 06:06:52 +0800252 calcFileSHA1(sha1LibRS, pathLibRS);
Logan35849002011-01-15 07:30:43 +0800253#endif
Logan75cc8a52011-01-07 06:06:52 +0800254
Logan1f028c02010-11-27 01:02:48 +0800255 GlobalInitialized = true;
256}
257
258
259void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
260 std::string *Error = static_cast<std::string*>(UserData);
261 Error->assign(Message);
262 LOGE("%s", Message.c_str());
263 exit(1);
264}
265
266
Logan Chienda5e0c32011-06-13 03:47:21 +0800267#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800268CodeMemoryManager *Compiler::createCodeMemoryManager() {
269 mCodeMemMgr.reset(new CodeMemoryManager());
270 return mCodeMemMgr.get();
271}
Logan Chienda5e0c32011-06-13 03:47:21 +0800272#endif
Logan1f028c02010-11-27 01:02:48 +0800273
274
Logan Chienda5e0c32011-06-13 03:47:21 +0800275#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800276CodeEmitter *Compiler::createCodeEmitter() {
Logan7dcaac92011-01-06 04:26:23 +0800277 mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get()));
Logan1f028c02010-11-27 01:02:48 +0800278 return mCodeEmitter.get();
279}
Logan Chienda5e0c32011-06-13 03:47:21 +0800280#endif
Logan1f028c02010-11-27 01:02:48 +0800281
282
Logan2a6dc822011-01-06 04:05:20 +0800283Compiler::Compiler(ScriptCompiled *result)
284 : mpResult(result),
Logan Chienda5e0c32011-06-13 03:47:21 +0800285#if USE_MCJIT
286 mRSExecutable(NULL),
287#endif
Logan1f028c02010-11-27 01:02:48 +0800288 mpSymbolLookupFn(NULL),
289 mpSymbolLookupContext(NULL),
290 mContext(NULL),
291 mModule(NULL),
292 mHasLinked(false) /* Turn off linker */ {
293 llvm::remove_fatal_error_handler();
294 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
295 mContext = new llvm::LLVMContext();
296 return;
297}
298
Logan1f028c02010-11-27 01:02:48 +0800299
Logan Chienda5e0c32011-06-13 03:47:21 +0800300#if USE_MCJIT
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700301// input objPath: For example,
302// /data/user/0/com.example.android.rs.fountain/cache/
303// @com.example.android.rs.fountain:raw@fountain.oBCC
304// output objPath: /data/user/0/com.example.android.rs.fountain/cache/
305// fountain.o
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700306//
307bool Compiler::getObjPath(std::string &objPath) {
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700308 size_t found0 = objPath.find("@");
309 size_t found1 = objPath.rfind("@");
310
311 if (found0 == found1 ||
Nowar Gu09b6c1c2011-05-24 23:49:07 +0800312 found0 == std::string::npos ||
313 found1 == std::string::npos) {
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700314 LOGE("Ill formatted resource name '%s'. The name should contain 2 @s",
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700315 objPath.c_str());
316 return false;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700317 }
318
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700319 objPath.replace(found0, found1 - found0 + 1, "", 0);
320 objPath.resize(objPath.length() - 3);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700321
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700322 LOGV("objPath = %s", objPath.c_str());
323 return true;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700324}
Logan Chienda5e0c32011-06-13 03:47:21 +0800325#endif
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700326
327
Logan474cbd22011-01-31 01:47:44 +0800328llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) {
329 llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError);
Logan1f028c02010-11-27 01:02:48 +0800330
Logan474cbd22011-01-31 01:47:44 +0800331 if (!result) {
332 LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str());
333 return NULL;
Logan1f028c02010-11-27 01:02:48 +0800334 }
335
Logan474cbd22011-01-31 01:47:44 +0800336 return result;
Logan1f028c02010-11-27 01:02:48 +0800337}
338
339
Logan474cbd22011-01-31 01:47:44 +0800340int Compiler::linkModule(llvm::Module *moduleWith) {
341 if (llvm::Linker::LinkModules(mModule, moduleWith, &mError) != 0) {
Logan1f028c02010-11-27 01:02:48 +0800342 return hasError();
343 }
344
Logan1f028c02010-11-27 01:02:48 +0800345 // Everything for linking should be settled down here with no error occurs
346 mHasLinked = true;
347 return hasError();
348}
349
350
Logan1f028c02010-11-27 01:02:48 +0800351int Compiler::compile() {
Logan Chienda5e0c32011-06-13 03:47:21 +0800352 llvm::Target const *Target = NULL;
Logan1f028c02010-11-27 01:02:48 +0800353 llvm::TargetData *TD = NULL;
Logan1f028c02010-11-27 01:02:48 +0800354 llvm::TargetMachine *TM = NULL;
Logan Chienda5e0c32011-06-13 03:47:21 +0800355
Logan1f028c02010-11-27 01:02:48 +0800356 std::string FeaturesStr;
357
Logan Chienda5e0c32011-06-13 03:47:21 +0800358 llvm::NamedMDNode const *PragmaMetadata;
359 llvm::NamedMDNode const *ExportVarMetadata;
360 llvm::NamedMDNode const *ExportFuncMetadata;
361 llvm::NamedMDNode const *ObjectSlotMetadata;
Logan1f028c02010-11-27 01:02:48 +0800362
Logan1f028c02010-11-27 01:02:48 +0800363 if (mModule == NULL) // No module was loaded
364 return 0;
365
366 // Create TargetMachine
367 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
368 if (hasError())
369 goto on_bcc_compile_error;
370
371 if (!CPU.empty() || !Features.empty()) {
372 llvm::SubtargetFeatures F;
373 F.setCPU(CPU);
Logana4994f52010-11-27 14:06:02 +0800374
375 for (std::vector<std::string>::const_iterator
376 I = Features.begin(), E = Features.end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800377 F.AddFeature(*I);
Logana4994f52010-11-27 14:06:02 +0800378 }
379
Logan1f028c02010-11-27 01:02:48 +0800380 FeaturesStr = F.getString();
381 }
382
383 TM = Target->createTargetMachine(Triple, FeaturesStr);
384 if (TM == NULL) {
385 setError("Failed to create target machine implementation for the"
386 " specified triple '" + Triple + "'");
387 goto on_bcc_compile_error;
388 }
389
Logan Chienda5e0c32011-06-13 03:47:21 +0800390 // Get target data from Module
391 TD = new llvm::TargetData(mModule);
392
393 // Load named metadata
394 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName);
395 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName);
396 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName);
397 ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName);
398
399 // Perform link-time optimization if we have multiple modules
400 if (mHasLinked) {
401 runLTO(new llvm::TargetData(*TD), ExportVarMetadata, ExportFuncMetadata);
402 }
403
404 // Perform code generation
405#if USE_OLD_JIT
406 if (runCodeGen(new llvm::TargetData(*TD), TM,
407 ExportVarMetadata, ExportFuncMetadata) != 0) {
408 goto on_bcc_compile_error;
409 }
410#endif
411
412#if USE_MCJIT
413 if (runMCCodeGen(new llvm::TargetData(*TD), TM,
414 ExportVarMetadata, ExportFuncMetadata) != 0) {
415 goto on_bcc_compile_error;
416 }
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700417#if USE_DISASSEMBLER && DEBUG_MCJIT_DISASSEMBLE
418 {
Shih-wei Liao320b5492011-06-20 22:53:33 -0700419 // llvm::LLVMContext Ctx;
420 // LOGD("@ long long alignment: %d\n", TD->getABITypeAlignment((llvm::Type const *)llvm::Type::getInt64Ty(Ctx)));
421 char const *func_list[] = { "root" };
422
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700423 size_t func_list_size = sizeof(func_list) / sizeof(char const *);
424
425 for (size_t i = 0; i < func_list_size; ++i) {
426 void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]);
427 if (func) {
428 size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]);
429 Disassemble(Target, TM, func_list[i], (unsigned char const *)func, size);
430 }
431 }
432 }
433#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800434#endif
435
436 // Read pragma information from the metadata node of the module.
437 if (PragmaMetadata) {
438 ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas;
439
440 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) {
441 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
442 if (Pragma != NULL &&
443 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) {
444 llvm::Value *PragmaNameMDS = Pragma->getOperand(0);
445 llvm::Value *PragmaValueMDS = Pragma->getOperand(1);
446
447 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) &&
448 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) {
449 llvm::StringRef PragmaName =
450 static_cast<llvm::MDString*>(PragmaNameMDS)->getString();
451 llvm::StringRef PragmaValue =
452 static_cast<llvm::MDString*>(PragmaValueMDS)->getString();
453
454 pragmaList.push_back(
455 std::make_pair(std::string(PragmaName.data(),
456 PragmaName.size()),
457 std::string(PragmaValue.data(),
458 PragmaValue.size())));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700459#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700460 LOGD("compile(): Pragma: %s -> %s\n", pragmaList.back().first.c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800461 pragmaList.back().second.c_str());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700462#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800463 }
464 }
465 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800466 }
467
468 if (ObjectSlotMetadata) {
469 ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots;
470
471 for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) {
472 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i);
473 if (ObjectSlot != NULL &&
474 ObjectSlot->getNumOperands() == 1) {
475 llvm::Value *SlotMDS = ObjectSlot->getOperand(0);
476 if (SlotMDS->getValueID() == llvm::Value::MDStringVal) {
477 llvm::StringRef Slot =
478 static_cast<llvm::MDString*>(SlotMDS)->getString();
479 uint32_t USlot = 0;
480 if (Slot.getAsInteger(10, USlot)) {
481 setError("Non-integer object slot value '" + Slot.str() + "'");
482 goto on_bcc_compile_error;
483 }
484 objectSlotList.push_back(USlot);
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700485#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700486 LOGD("compile(): RefCount Slot: %s @ %u\n", Slot.str().c_str(), USlot);
487#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800488 }
489 }
490 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800491 }
492
493on_bcc_compile_error:
494 // LOGE("on_bcc_compiler_error");
495 if (TD) {
496 delete TD;
497 }
498
499 if (TM) {
500 delete TM;
501 }
502
503 if (mError.empty()) {
504 return 0;
505 }
506
507 // LOGE(getErrorMessage());
508 return 1;
509}
510
511
512#if USE_OLD_JIT
513int Compiler::runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
514 llvm::NamedMDNode const *ExportVarMetadata,
515 llvm::NamedMDNode const *ExportFuncMetadata) {
Logan1f028c02010-11-27 01:02:48 +0800516 // Create memory manager for creation of code emitter later.
517 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) {
518 setError("Failed to startup memory management for further compilation");
Logan Chienda5e0c32011-06-13 03:47:21 +0800519 return 1;
Logan1f028c02010-11-27 01:02:48 +0800520 }
Logan02286cb2011-01-07 00:30:47 +0800521
522 mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase());
Logan1f028c02010-11-27 01:02:48 +0800523
524 // Create code emitter
525 if (!mCodeEmitter.get()) {
526 if (!createCodeEmitter()) {
Logan Chienda5e0c32011-06-13 03:47:21 +0800527 setError("Failed to create machine code emitter for compilation");
528 return 1;
Logan1f028c02010-11-27 01:02:48 +0800529 }
530 } else {
531 // Reuse the code emitter
532 mCodeEmitter->reset();
533 }
534
535 mCodeEmitter->setTargetMachine(*TM);
536 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn,
537 mpSymbolLookupContext);
538
Logan1f028c02010-11-27 01:02:48 +0800539 // Create code-gen pass to run the code emitter
Logan Chienda5e0c32011-06-13 03:47:21 +0800540 llvm::OwningPtr<llvm::FunctionPassManager> CodeGenPasses(
541 new llvm::FunctionPassManager(mModule));
Logan1f028c02010-11-27 01:02:48 +0800542
Logan Chienda5e0c32011-06-13 03:47:21 +0800543 // Add TargetData to code generation pass manager
544 CodeGenPasses->add(TD);
545
546 // Add code emit passes
Logan1f028c02010-11-27 01:02:48 +0800547 if (TM->addPassesToEmitMachineCode(*CodeGenPasses,
548 *mCodeEmitter,
549 CodeGenOptLevel)) {
Logan Chienda5e0c32011-06-13 03:47:21 +0800550 setError("The machine code emission is not supported on '" + Triple + "'");
551 return 1;
Logan1f028c02010-11-27 01:02:48 +0800552 }
553
Logan Chienda5e0c32011-06-13 03:47:21 +0800554 // Run the code emitter on every non-declaration function in the module
Logan1f028c02010-11-27 01:02:48 +0800555 CodeGenPasses->doInitialization();
Logan Chienda5e0c32011-06-13 03:47:21 +0800556 for (llvm::Module::iterator
557 I = mModule->begin(), E = mModule->end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800558 if (!I->isDeclaration()) {
559 CodeGenPasses->run(*I);
560 }
561 }
562
563 CodeGenPasses->doFinalization();
Logan Chienb0ceca22011-06-12 13:34:49 +0800564
Logan1f028c02010-11-27 01:02:48 +0800565 // Copy the global address mapping from code emitter and remapping
566 if (ExportVarMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800567 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
568
Logan1f028c02010-11-27 01:02:48 +0800569 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
570 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
571 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
572 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
573 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
574 llvm::StringRef ExportVarName =
575 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
576
577 CodeEmitter::global_addresses_const_iterator I, E;
578 for (I = mCodeEmitter->global_address_begin(),
579 E = mCodeEmitter->global_address_end();
580 I != E; I++) {
581 if (I->first->getValueID() != llvm::Value::GlobalVariableVal)
582 continue;
583 if (ExportVarName == I->first->getName()) {
Logan2a6dc822011-01-06 04:05:20 +0800584 varList.push_back(I->second);
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700585#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700586 LOGD("runCodeGen(): Exported VAR: %s @ %p\n", ExportVarName.str().c_str(), I->second);
587#endif
Logan1f028c02010-11-27 01:02:48 +0800588 break;
589 }
590 }
591 if (I != mCodeEmitter->global_address_end())
592 continue; // found
Logan Chien7d1bf582011-06-13 23:22:40 +0800593
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700594#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700595 LOGD("runCodeGen(): Exported VAR: %s @ %p\n",
Logan Chien7d1bf582011-06-13 23:22:40 +0800596 ExportVarName.str().c_str(), (void *)0);
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700597#endif
Logan1f028c02010-11-27 01:02:48 +0800598 }
599 }
600 // if reaching here, we know the global variable record in metadata is
601 // not found. So we make an empty slot
Logan2a6dc822011-01-06 04:05:20 +0800602 varList.push_back(NULL);
Logan1f028c02010-11-27 01:02:48 +0800603 }
Logan2a6dc822011-01-06 04:05:20 +0800604
Stephen Hinesbbcef8a2011-05-04 19:40:10 -0700605 bccAssert((varList.size() == ExportVarMetadata->getNumOperands()) &&
606 "Number of slots doesn't match the number of export variables!");
Logan1f028c02010-11-27 01:02:48 +0800607 }
608
609 if (ExportFuncMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800610 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
611
Logan1f028c02010-11-27 01:02:48 +0800612 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
613 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
614 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
615 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
616 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
617 llvm::StringRef ExportFuncName =
618 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
Logan7dcaac92011-01-06 04:26:23 +0800619 funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700620#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700621 LOGD("runCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800622 funcList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700623#endif
Logan1f028c02010-11-27 01:02:48 +0800624 }
625 }
626 }
627 }
628
629 // Tell code emitter now can release the memory using during the JIT since
630 // we have done the code emission
631 mCodeEmitter->releaseUnnecessary();
632
Logan Chienda5e0c32011-06-13 03:47:21 +0800633 return 0;
Logan1f028c02010-11-27 01:02:48 +0800634}
Logan Chienda5e0c32011-06-13 03:47:21 +0800635#endif // USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800636
637
Logan Chienda5e0c32011-06-13 03:47:21 +0800638#if USE_MCJIT
639int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
640 llvm::NamedMDNode const *ExportVarMetadata,
641 llvm::NamedMDNode const *ExportFuncMetadata) {
642 // Decorate mEmittedELFExecutable with formatted ostream
643 llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable);
644
645 // Relax all machine instructions
646 TM->setMCRelaxAll(/* RelaxAll= */ true);
647
648 // Create MC code generation pass manager
649 llvm::PassManager MCCodeGenPasses;
650
651 // Add TargetData to MC code generation pass manager
652 MCCodeGenPasses.add(TD);
653
654 // Add MC code generation passes to pass manager
655 llvm::MCContext *Ctx;
656 if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS,
657 CodeGenOptLevel, false)) {
658 setError("Fail to add passes to emit file");
659 return 1;
660 }
661
662 MCCodeGenPasses.run(*mModule);
663 OutSVOS.flush();
664
665 // Load the ELF Object
666 mRSExecutable =
667 rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(),
668 mEmittedELFExecutable.size(),
669 &resolveSymbolAdapter, this);
670
671 if (!mRSExecutable) {
672 setError("Fail to load emitted ELF relocatable file");
673 return 1;
674 }
675
676#if !USE_OLD_JIT
677 // Note: If old JIT is compiled then we prefer the old version instead of the
678 // new version.
679
680 if (ExportVarMetadata) {
681 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
682
683 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
684 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
Logan Chien70dd9982011-06-13 23:21:39 +0800685 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
686 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
687 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
688 llvm::StringRef ExportVarName =
689 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
690
691 varList.push_back(
692 rsloaderGetSymbolAddress(mRSExecutable,
693 ExportVarName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700694#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700695 LOGD("runMCCodeGen(): Exported Var: %s @ %p\n", ExportVarName.str().c_str(),
Logan Chien70dd9982011-06-13 23:21:39 +0800696 varList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700697#endif
Logan Chien70dd9982011-06-13 23:21:39 +0800698 continue;
699 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800700 }
701
Logan Chien70dd9982011-06-13 23:21:39 +0800702 varList.push_back(NULL);
Logan Chienda5e0c32011-06-13 03:47:21 +0800703 }
704 }
705
706 if (ExportFuncMetadata) {
707 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
708
709 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
710 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
711 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
712 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
713 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
714 llvm::StringRef ExportFuncName =
715 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
716
717 funcList.push_back(
718 rsloaderGetSymbolAddress(mRSExecutable,
719 ExportFuncName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700720#if DEBUG_MCJIT_RELECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700721 LOGD("runMCCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800722 funcList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700723#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800724 }
725 }
726 }
727 }
728#endif // !USE_OLD_JIT
729
730#if USE_CACHE
731 // Write generated executable to file.
732 if (writeELFExecToFile() != 0) {
733 setError("Fail to write mcjit-ed executable to file");
734 return 1;
735 }
736#endif
737
738 return 0;
739}
740#endif // USE_MCJIT
741
742
743int Compiler::runLTO(llvm::TargetData *TD,
744 llvm::NamedMDNode const *ExportVarMetadata,
745 llvm::NamedMDNode const *ExportFuncMetadata) {
Logan Chien4cc00332011-06-12 14:00:46 +0800746 llvm::PassManager LTOPasses;
747
748 // Add TargetData to LTO passes
749 LTOPasses.add(TD);
750
751 // Collect All Exported Symbols
752 std::vector<const char*> ExportSymbols;
753
754 // Note: This is a workaround for getting export variable and function name.
755 // We should refine it soon.
756 if (ExportVarMetadata) {
757 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
758 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
759 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
760 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
761 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
762 llvm::StringRef ExportVarName =
763 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
764 ExportSymbols.push_back(ExportVarName.data());
765 }
766 }
767 }
768 }
769
770 if (ExportFuncMetadata) {
771 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
772 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
773 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
774 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
775 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
776 llvm::StringRef ExportFuncName =
777 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
778 ExportSymbols.push_back(ExportFuncName.data());
779 }
780 }
781 }
782 }
783
784 // root() and init() are born to be exported
785 ExportSymbols.push_back("root");
786 ExportSymbols.push_back("init");
787
788 // We now create passes list performing LTO. These are copied from
789 // (including comments) llvm::createStandardLTOPasses().
790
791 // Internalize all other symbols not listed in ExportSymbols
792 LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
793
794 // Propagate constants at call sites into the functions they call. This
795 // opens opportunities for globalopt (and inlining) by substituting
796 // function pointers passed as arguments to direct uses of functions.
797 LTOPasses.add(llvm::createIPSCCPPass());
798
799 // Now that we internalized some globals, see if we can hack on them!
800 LTOPasses.add(llvm::createGlobalOptimizerPass());
801
802 // Linking modules together can lead to duplicated global constants, only
803 // keep one copy of each constant...
804 LTOPasses.add(llvm::createConstantMergePass());
805
806 // Remove unused arguments from functions...
807 LTOPasses.add(llvm::createDeadArgEliminationPass());
808
809 // Reduce the code after globalopt and ipsccp. Both can open up
810 // significant simplification opportunities, and both can propagate
811 // functions through function pointers. When this happens, we often have
812 // to resolve varargs calls, etc, so let instcombine do this.
813 LTOPasses.add(llvm::createInstructionCombiningPass());
814
815 // Inline small functions
816 LTOPasses.add(llvm::createFunctionInliningPass());
817
818 // Remove dead EH info.
819 LTOPasses.add(llvm::createPruneEHPass());
820
821 // Internalize the globals again after inlining
822 LTOPasses.add(llvm::createGlobalOptimizerPass());
823
824 // Remove dead functions.
825 LTOPasses.add(llvm::createGlobalDCEPass());
826
827 // If we didn't decide to inline a function, check to see if we can
828 // transform it to pass arguments by value instead of by reference.
829 LTOPasses.add(llvm::createArgumentPromotionPass());
830
831 // The IPO passes may leave cruft around. Clean up after them.
832 LTOPasses.add(llvm::createInstructionCombiningPass());
833 LTOPasses.add(llvm::createJumpThreadingPass());
834
835 // Break up allocas
836 LTOPasses.add(llvm::createScalarReplAggregatesPass());
837
838 // Run a few AA driven optimizations here and now, to cleanup the code.
839 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture.
840 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
841
842 // Hoist loop invariants.
843 LTOPasses.add(llvm::createLICMPass());
844
845 // Remove redundancies.
846 LTOPasses.add(llvm::createGVNPass());
847
848 // Remove dead memcpys.
849 LTOPasses.add(llvm::createMemCpyOptPass());
850
851 // Nuke dead stores.
852 LTOPasses.add(llvm::createDeadStoreEliminationPass());
853
854 // Cleanup and simplify the code after the scalar optimizations.
855 LTOPasses.add(llvm::createInstructionCombiningPass());
856
857 LTOPasses.add(llvm::createJumpThreadingPass());
858
859 // Delete basic blocks, which optimization passes may have killed.
860 LTOPasses.add(llvm::createCFGSimplificationPass());
861
862 // Now that we have optimized the program, discard unreachable functions.
863 LTOPasses.add(llvm::createGlobalDCEPass());
864
865 LTOPasses.run(*mModule);
Logan Chienda5e0c32011-06-13 03:47:21 +0800866
867 return 0;
Logan Chien4cc00332011-06-12 14:00:46 +0800868}
869
870
Logan Chienda5e0c32011-06-13 03:47:21 +0800871#if USE_MCJIT
872int Compiler::writeELFExecToFile() {
873 std::string objPath(mCachePath);
874 if (!getObjPath(objPath)) {
875 LOGE("Fail to create objPath");
876 return 1;
877 }
878
879 FileHandle file;
880
881 int Fd = file.open(objPath.c_str(), OpenMode::Write);
882 if (Fd < 0) {
883 LOGE("Fail to open file '%s'", objPath.c_str());
884 return 1;
885 }
886
887 file.write(&*mEmittedELFExecutable.begin(), mEmittedELFExecutable.size());
888
889 return 0;
890}
891#endif
892
893
894#if USE_MCJIT
895void *Compiler::getSymbolAddress(char const *name) {
896 return rsloaderGetSymbolAddress(mRSExecutable, name);
897}
898#endif
899
900
901#if USE_MCJIT
902void *Compiler::resolveSymbolAdapter(void *context, char const *name) {
903 Compiler *self = reinterpret_cast<Compiler *>(context);
904
905 if (void *Addr = FindRuntimeFunction(name)) {
906 return Addr;
907 }
908
909 if (self->mpSymbolLookupFn) {
910 if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
911 return Addr;
912 }
913 }
914
915 LOGE("Unable to resolve symbol: %s\n", name);
916 return NULL;
917}
918#endif
919
920
Logan1f028c02010-11-27 01:02:48 +0800921Compiler::~Compiler() {
Logan1f028c02010-11-27 01:02:48 +0800922 delete mModule;
Logan1f028c02010-11-27 01:02:48 +0800923 delete mContext;
Logana4994f52010-11-27 14:06:02 +0800924
Logan Chienda5e0c32011-06-13 03:47:21 +0800925#if USE_MCJIT
926 rsloaderDisposeExec(mRSExecutable);
927#endif
928
Logana4994f52010-11-27 14:06:02 +0800929 // llvm::llvm_shutdown();
Logan1f028c02010-11-27 01:02:48 +0800930}
931
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700932#if USE_MCJIT && USE_DISASSEMBLER
933void Compiler::Disassemble(llvm::Target const *Target,
934 llvm::TargetMachine *TM,
935 std::string const &Name,
936 unsigned char const *Func,
937 size_t FuncSize) {
938 llvm::raw_ostream *OS;
939
940#if USE_DISASSEMBLER_FILE
941 std::string ErrorInfo;
942 OS = new llvm::raw_fd_ostream("/data/local/tmp/mcjit-dis.s", ErrorInfo,
943 llvm::raw_fd_ostream::F_Append);
944
945 if (!ErrorInfo.empty()) { // some errors occurred
946 // LOGE("Error in creating disassembly file");
947 delete OS;
948 return;
949 }
950#else
951 OS = &llvm::outs();
952#endif
953
954 *OS << "MC/ Disassembled code: " << Name << "\n";
955
956 const llvm::MCAsmInfo *AsmInfo;
957 const llvm::MCDisassembler *Disassmbler;
958 llvm::MCInstPrinter *IP;
959
960 AsmInfo = Target->createAsmInfo(Compiler::Triple);
961 Disassmbler = Target->createMCDisassembler();
962 IP = Target->createMCInstPrinter(*TM,
963 AsmInfo->getAssemblerDialect(),
964 *AsmInfo);
965
966 const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize);
967
968 uint64_t Size;
969 uint64_t Index;
970
971 for (Index = 0; Index < FuncSize; Index += Size) {
972 llvm::MCInst Inst;
973
974 if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
975 /* REMOVED */ llvm::nulls())) {
976 OS->indent(4);
977 OS->write("0x", 2);
978 OS->write_hex((uint32_t)Func + Index);
979 OS->write(": 0x", 4);
980 OS->write_hex(*(uint32_t *)(Func + Index));
981 IP->printInst(&Inst, *OS);
982 *OS << "\n";
983 } else {
984 if (Size == 0)
985 Size = 1; // skip illegible bytes
986 }
987 }
988
989 *OS << "\n";
990 delete BufferMObj;
991
992 delete AsmInfo;
993 delete Disassmbler;
994 delete IP;
995
996#if USE_DISASSEMBLER_FILE
997 // If you want the disassemble results write to file, uncomment this.
998 ((llvm::raw_fd_ostream*)OS)->close();
999 delete OS;
1000#endif
1001}
1002#endif
1003
1004
Logan1f028c02010-11-27 01:02:48 +08001005} // namespace bcc