blob: bccf57117bb495d570687509262ef44ad74bc79c [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;
Logan1f028c02010-11-27 01:02:48 +0800155 // if (!llvm::llvm_is_multithreaded())
156 // llvm::llvm_start_multithreaded();
157
158 // Set Triple, CPU and Features here
159 Triple = TARGET_TRIPLE_STRING;
160
Logan1f028c02010-11-27 01:02:48 +0800161 Features.push_back("+vfp3");
Logan1f028c02010-11-27 01:02:48 +0800162
Logan4fe966f2011-02-27 08:26:40 +0800163 // NOTE: Currently, we have to turn off the support for NEON explicitly.
164 // Since the ARMCodeEmitter.cpp is not ready for JITing NEON
165 // instructions.
Joseph Wen51001b82011-06-23 18:56:45 -0700166#if ARCH_ARM_HAVE_NEON
167 Features.push_back("+d32");
168 Features.push_back("+neon");
169 Features.push_back("+neonfp");
170#else
171 Features.push_back("+d16");
172 Features.push_back("-neon");
Logan4fe966f2011-02-27 08:26:40 +0800173 Features.push_back("-neonfp");
Joseph Wen51001b82011-06-23 18:56:45 -0700174#endif
175
Logan4fe966f2011-02-27 08:26:40 +0800176 Features.push_back("-vmlx");
177
Logan1f028c02010-11-27 01:02:48 +0800178#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
179 LLVMInitializeARMTargetInfo();
180 LLVMInitializeARMTarget();
Logan35849002011-01-15 07:30:43 +0800181#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800182 LLVMInitializeARMDisassembler();
183 LLVMInitializeARMAsmPrinter();
184#endif
185#endif
186
187#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
188 LLVMInitializeX86TargetInfo();
189 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800190#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800191 LLVMInitializeX86Disassembler();
192 LLVMInitializeX86AsmPrinter();
193#endif
194#endif
195
196#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
197 LLVMInitializeX86TargetInfo();
198 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800199#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800200 LLVMInitializeX86Disassembler();
201 LLVMInitializeX86AsmPrinter();
202#endif
203#endif
204
205 // -O0: llvm::CodeGenOpt::None
206 // -O1: llvm::CodeGenOpt::Less
207 // -O2: llvm::CodeGenOpt::Default
208 // -O3: llvm::CodeGenOpt::Aggressive
Shih-wei Liao72f67a62010-12-14 13:36:15 -0800209 CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
Logan1f028c02010-11-27 01:02:48 +0800210
211 // Below are the global settings to LLVM
212
213 // Disable frame pointer elimination optimization
214 llvm::NoFramePointerElim = false;
215
216 // Use hardfloat ABI
217 //
218 // TODO(all): Need to detect the CPU capability and decide whether to use
219 // softfp. To use softfp, change following 2 lines to
220 //
221 // llvm::FloatABIType = llvm::FloatABI::Soft;
222 // llvm::UseSoftFloat = true;
223 //
Shih-wei Liaoe728cb82010-12-15 15:20:47 -0800224 llvm::FloatABIType = llvm::FloatABI::Soft;
Logan1f028c02010-11-27 01:02:48 +0800225 llvm::UseSoftFloat = false;
226
227 // BCC needs all unknown symbols resolved at JIT/compilation time.
228 // So we don't need any dynamic relocation model.
229 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static);
230
231#if defined(DEFAULT_X64_CODEGEN)
232 // Data address in X86_64 architecture may reside in a far-away place
233 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium);
234#else
235 // This is set for the linker (specify how large of the virtual addresses
236 // we can access for all unknown symbols.)
237 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small);
238#endif
239
240 // Register the scheduler
241 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
242
243 // Register allocation policy:
244 // createFastRegisterAllocator: fast but bad quality
245 // createLinearScanRegisterAllocator: not so fast but good quality
246 llvm::RegisterRegAlloc::setDefault
247 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
248 llvm::createFastRegisterAllocator :
249 llvm::createLinearScanRegisterAllocator);
250
Logan35849002011-01-15 07:30:43 +0800251#if USE_CACHE
Logan75cc8a52011-01-07 06:06:52 +0800252 // Calculate the SHA1 checksum of libbcc and libRS.
Joseph Wen89175b92011-06-24 14:12:23 -0700253 calcFileSHA1(sha1LibBCC_SHA1, pathLibBCC_SHA1);
Logan35849002011-01-15 07:30:43 +0800254#endif
Logan75cc8a52011-01-07 06:06:52 +0800255
Logan1f028c02010-11-27 01:02:48 +0800256 GlobalInitialized = true;
257}
258
259
260void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
261 std::string *Error = static_cast<std::string*>(UserData);
262 Error->assign(Message);
263 LOGE("%s", Message.c_str());
264 exit(1);
265}
266
267
Logan Chienda5e0c32011-06-13 03:47:21 +0800268#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800269CodeMemoryManager *Compiler::createCodeMemoryManager() {
270 mCodeMemMgr.reset(new CodeMemoryManager());
271 return mCodeMemMgr.get();
272}
Logan Chienda5e0c32011-06-13 03:47:21 +0800273#endif
Logan1f028c02010-11-27 01:02:48 +0800274
275
Logan Chienda5e0c32011-06-13 03:47:21 +0800276#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800277CodeEmitter *Compiler::createCodeEmitter() {
Logan7dcaac92011-01-06 04:26:23 +0800278 mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get()));
Logan1f028c02010-11-27 01:02:48 +0800279 return mCodeEmitter.get();
280}
Logan Chienda5e0c32011-06-13 03:47:21 +0800281#endif
Logan1f028c02010-11-27 01:02:48 +0800282
283
Logan2a6dc822011-01-06 04:05:20 +0800284Compiler::Compiler(ScriptCompiled *result)
285 : mpResult(result),
Logan Chienda5e0c32011-06-13 03:47:21 +0800286#if USE_MCJIT
287 mRSExecutable(NULL),
288#endif
Logan1f028c02010-11-27 01:02:48 +0800289 mpSymbolLookupFn(NULL),
290 mpSymbolLookupContext(NULL),
291 mContext(NULL),
292 mModule(NULL),
293 mHasLinked(false) /* Turn off linker */ {
294 llvm::remove_fatal_error_handler();
295 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
296 mContext = new llvm::LLVMContext();
297 return;
298}
299
Logan1f028c02010-11-27 01:02:48 +0800300
Logan Chienda5e0c32011-06-13 03:47:21 +0800301#if USE_MCJIT
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700302// input objPath: For example,
303// /data/user/0/com.example.android.rs.fountain/cache/
304// @com.example.android.rs.fountain:raw@fountain.oBCC
305// output objPath: /data/user/0/com.example.android.rs.fountain/cache/
306// fountain.o
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700307//
308bool Compiler::getObjPath(std::string &objPath) {
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700309 size_t found0 = objPath.find("@");
310 size_t found1 = objPath.rfind("@");
311
312 if (found0 == found1 ||
Nowar Gu09b6c1c2011-05-24 23:49:07 +0800313 found0 == std::string::npos ||
314 found1 == std::string::npos) {
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700315 LOGE("Ill formatted resource name '%s'. The name should contain 2 @s",
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700316 objPath.c_str());
317 return false;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700318 }
319
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700320 objPath.replace(found0, found1 - found0 + 1, "", 0);
321 objPath.resize(objPath.length() - 3);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700322
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700323 LOGV("objPath = %s", objPath.c_str());
324 return true;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700325}
Logan Chienda5e0c32011-06-13 03:47:21 +0800326#endif
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700327
328
Logan474cbd22011-01-31 01:47:44 +0800329llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) {
330 llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError);
Logan1f028c02010-11-27 01:02:48 +0800331
Logan474cbd22011-01-31 01:47:44 +0800332 if (!result) {
333 LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str());
334 return NULL;
Logan1f028c02010-11-27 01:02:48 +0800335 }
336
Logan474cbd22011-01-31 01:47:44 +0800337 return result;
Logan1f028c02010-11-27 01:02:48 +0800338}
339
340
Logan474cbd22011-01-31 01:47:44 +0800341int Compiler::linkModule(llvm::Module *moduleWith) {
342 if (llvm::Linker::LinkModules(mModule, moduleWith, &mError) != 0) {
Logan1f028c02010-11-27 01:02:48 +0800343 return hasError();
344 }
345
Logan1f028c02010-11-27 01:02:48 +0800346 // Everything for linking should be settled down here with no error occurs
347 mHasLinked = true;
348 return hasError();
349}
350
351
Logan1f028c02010-11-27 01:02:48 +0800352int Compiler::compile() {
Logan Chienda5e0c32011-06-13 03:47:21 +0800353 llvm::Target const *Target = NULL;
Logan1f028c02010-11-27 01:02:48 +0800354 llvm::TargetData *TD = NULL;
Logan1f028c02010-11-27 01:02:48 +0800355 llvm::TargetMachine *TM = NULL;
Logan Chienda5e0c32011-06-13 03:47:21 +0800356
Logan1f028c02010-11-27 01:02:48 +0800357 std::string FeaturesStr;
358
Logan Chienda5e0c32011-06-13 03:47:21 +0800359 llvm::NamedMDNode const *PragmaMetadata;
360 llvm::NamedMDNode const *ExportVarMetadata;
361 llvm::NamedMDNode const *ExportFuncMetadata;
362 llvm::NamedMDNode const *ObjectSlotMetadata;
Logan1f028c02010-11-27 01:02:48 +0800363
Logan1f028c02010-11-27 01:02:48 +0800364 if (mModule == NULL) // No module was loaded
365 return 0;
366
367 // Create TargetMachine
368 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
369 if (hasError())
370 goto on_bcc_compile_error;
371
372 if (!CPU.empty() || !Features.empty()) {
373 llvm::SubtargetFeatures F;
374 F.setCPU(CPU);
Logana4994f52010-11-27 14:06:02 +0800375
376 for (std::vector<std::string>::const_iterator
377 I = Features.begin(), E = Features.end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800378 F.AddFeature(*I);
Logana4994f52010-11-27 14:06:02 +0800379 }
380
Logan1f028c02010-11-27 01:02:48 +0800381 FeaturesStr = F.getString();
382 }
383
384 TM = Target->createTargetMachine(Triple, FeaturesStr);
385 if (TM == NULL) {
386 setError("Failed to create target machine implementation for the"
387 " specified triple '" + Triple + "'");
388 goto on_bcc_compile_error;
389 }
390
Logan Chienda5e0c32011-06-13 03:47:21 +0800391 // Get target data from Module
392 TD = new llvm::TargetData(mModule);
393
394 // Load named metadata
395 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName);
396 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName);
397 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName);
398 ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName);
399
400 // Perform link-time optimization if we have multiple modules
401 if (mHasLinked) {
402 runLTO(new llvm::TargetData(*TD), ExportVarMetadata, ExportFuncMetadata);
403 }
404
405 // Perform code generation
406#if USE_OLD_JIT
407 if (runCodeGen(new llvm::TargetData(*TD), TM,
408 ExportVarMetadata, ExportFuncMetadata) != 0) {
409 goto on_bcc_compile_error;
410 }
411#endif
412
413#if USE_MCJIT
414 if (runMCCodeGen(new llvm::TargetData(*TD), TM,
415 ExportVarMetadata, ExportFuncMetadata) != 0) {
416 goto on_bcc_compile_error;
417 }
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700418#if USE_DISASSEMBLER && DEBUG_MCJIT_DISASSEMBLE
419 {
Shih-wei Liao320b5492011-06-20 22:53:33 -0700420 // llvm::LLVMContext Ctx;
421 // LOGD("@ long long alignment: %d\n", TD->getABITypeAlignment((llvm::Type const *)llvm::Type::getInt64Ty(Ctx)));
422 char const *func_list[] = { "root" };
423
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700424 size_t func_list_size = sizeof(func_list) / sizeof(char const *);
425
426 for (size_t i = 0; i < func_list_size; ++i) {
427 void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]);
428 if (func) {
429 size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]);
430 Disassemble(Target, TM, func_list[i], (unsigned char const *)func, size);
431 }
432 }
433 }
434#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800435#endif
436
437 // Read pragma information from the metadata node of the module.
438 if (PragmaMetadata) {
439 ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas;
440
441 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) {
442 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
443 if (Pragma != NULL &&
444 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) {
445 llvm::Value *PragmaNameMDS = Pragma->getOperand(0);
446 llvm::Value *PragmaValueMDS = Pragma->getOperand(1);
447
448 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) &&
449 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) {
450 llvm::StringRef PragmaName =
451 static_cast<llvm::MDString*>(PragmaNameMDS)->getString();
452 llvm::StringRef PragmaValue =
453 static_cast<llvm::MDString*>(PragmaValueMDS)->getString();
454
455 pragmaList.push_back(
456 std::make_pair(std::string(PragmaName.data(),
457 PragmaName.size()),
458 std::string(PragmaValue.data(),
459 PragmaValue.size())));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700460#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700461 LOGD("compile(): Pragma: %s -> %s\n", pragmaList.back().first.c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800462 pragmaList.back().second.c_str());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700463#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800464 }
465 }
466 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800467 }
468
469 if (ObjectSlotMetadata) {
470 ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots;
471
472 for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) {
473 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i);
474 if (ObjectSlot != NULL &&
475 ObjectSlot->getNumOperands() == 1) {
476 llvm::Value *SlotMDS = ObjectSlot->getOperand(0);
477 if (SlotMDS->getValueID() == llvm::Value::MDStringVal) {
478 llvm::StringRef Slot =
479 static_cast<llvm::MDString*>(SlotMDS)->getString();
480 uint32_t USlot = 0;
481 if (Slot.getAsInteger(10, USlot)) {
482 setError("Non-integer object slot value '" + Slot.str() + "'");
483 goto on_bcc_compile_error;
484 }
485 objectSlotList.push_back(USlot);
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700486#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700487 LOGD("compile(): RefCount Slot: %s @ %u\n", Slot.str().c_str(), USlot);
488#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800489 }
490 }
491 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800492 }
493
494on_bcc_compile_error:
495 // LOGE("on_bcc_compiler_error");
496 if (TD) {
497 delete TD;
498 }
499
500 if (TM) {
501 delete TM;
502 }
503
504 if (mError.empty()) {
505 return 0;
506 }
507
508 // LOGE(getErrorMessage());
509 return 1;
510}
511
512
513#if USE_OLD_JIT
514int Compiler::runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
515 llvm::NamedMDNode const *ExportVarMetadata,
516 llvm::NamedMDNode const *ExportFuncMetadata) {
Logan1f028c02010-11-27 01:02:48 +0800517 // Create memory manager for creation of code emitter later.
518 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) {
519 setError("Failed to startup memory management for further compilation");
Logan Chienda5e0c32011-06-13 03:47:21 +0800520 return 1;
Logan1f028c02010-11-27 01:02:48 +0800521 }
Logan02286cb2011-01-07 00:30:47 +0800522
523 mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase());
Logan1f028c02010-11-27 01:02:48 +0800524
525 // Create code emitter
526 if (!mCodeEmitter.get()) {
527 if (!createCodeEmitter()) {
Logan Chienda5e0c32011-06-13 03:47:21 +0800528 setError("Failed to create machine code emitter for compilation");
529 return 1;
Logan1f028c02010-11-27 01:02:48 +0800530 }
531 } else {
532 // Reuse the code emitter
533 mCodeEmitter->reset();
534 }
535
536 mCodeEmitter->setTargetMachine(*TM);
537 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn,
538 mpSymbolLookupContext);
539
Logan1f028c02010-11-27 01:02:48 +0800540 // Create code-gen pass to run the code emitter
Logan Chienda5e0c32011-06-13 03:47:21 +0800541 llvm::OwningPtr<llvm::FunctionPassManager> CodeGenPasses(
542 new llvm::FunctionPassManager(mModule));
Logan1f028c02010-11-27 01:02:48 +0800543
Logan Chienda5e0c32011-06-13 03:47:21 +0800544 // Add TargetData to code generation pass manager
545 CodeGenPasses->add(TD);
546
547 // Add code emit passes
Logan1f028c02010-11-27 01:02:48 +0800548 if (TM->addPassesToEmitMachineCode(*CodeGenPasses,
549 *mCodeEmitter,
550 CodeGenOptLevel)) {
Logan Chienda5e0c32011-06-13 03:47:21 +0800551 setError("The machine code emission is not supported on '" + Triple + "'");
552 return 1;
Logan1f028c02010-11-27 01:02:48 +0800553 }
554
Logan Chienda5e0c32011-06-13 03:47:21 +0800555 // Run the code emitter on every non-declaration function in the module
Logan1f028c02010-11-27 01:02:48 +0800556 CodeGenPasses->doInitialization();
Logan Chienda5e0c32011-06-13 03:47:21 +0800557 for (llvm::Module::iterator
558 I = mModule->begin(), E = mModule->end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800559 if (!I->isDeclaration()) {
560 CodeGenPasses->run(*I);
561 }
562 }
563
564 CodeGenPasses->doFinalization();
Logan Chienb0ceca22011-06-12 13:34:49 +0800565
Logan1f028c02010-11-27 01:02:48 +0800566 // Copy the global address mapping from code emitter and remapping
567 if (ExportVarMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800568 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
569
Logan1f028c02010-11-27 01:02:48 +0800570 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
571 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
572 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
573 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
574 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
575 llvm::StringRef ExportVarName =
576 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
577
578 CodeEmitter::global_addresses_const_iterator I, E;
579 for (I = mCodeEmitter->global_address_begin(),
580 E = mCodeEmitter->global_address_end();
581 I != E; I++) {
582 if (I->first->getValueID() != llvm::Value::GlobalVariableVal)
583 continue;
584 if (ExportVarName == I->first->getName()) {
Logan2a6dc822011-01-06 04:05:20 +0800585 varList.push_back(I->second);
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700586#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700587 LOGD("runCodeGen(): Exported VAR: %s @ %p\n", ExportVarName.str().c_str(), I->second);
588#endif
Logan1f028c02010-11-27 01:02:48 +0800589 break;
590 }
591 }
592 if (I != mCodeEmitter->global_address_end())
593 continue; // found
Logan Chien7d1bf582011-06-13 23:22:40 +0800594
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700595#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700596 LOGD("runCodeGen(): Exported VAR: %s @ %p\n",
Logan Chien7d1bf582011-06-13 23:22:40 +0800597 ExportVarName.str().c_str(), (void *)0);
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700598#endif
Logan1f028c02010-11-27 01:02:48 +0800599 }
600 }
601 // if reaching here, we know the global variable record in metadata is
602 // not found. So we make an empty slot
Logan2a6dc822011-01-06 04:05:20 +0800603 varList.push_back(NULL);
Logan1f028c02010-11-27 01:02:48 +0800604 }
Logan2a6dc822011-01-06 04:05:20 +0800605
Stephen Hinesbbcef8a2011-05-04 19:40:10 -0700606 bccAssert((varList.size() == ExportVarMetadata->getNumOperands()) &&
607 "Number of slots doesn't match the number of export variables!");
Logan1f028c02010-11-27 01:02:48 +0800608 }
609
610 if (ExportFuncMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800611 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
612
Logan1f028c02010-11-27 01:02:48 +0800613 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
614 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
615 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
616 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
617 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
618 llvm::StringRef ExportFuncName =
619 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
Logan7dcaac92011-01-06 04:26:23 +0800620 funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700621#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700622 LOGD("runCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800623 funcList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700624#endif
Logan1f028c02010-11-27 01:02:48 +0800625 }
626 }
627 }
628 }
629
630 // Tell code emitter now can release the memory using during the JIT since
631 // we have done the code emission
632 mCodeEmitter->releaseUnnecessary();
633
Logan Chienda5e0c32011-06-13 03:47:21 +0800634 return 0;
Logan1f028c02010-11-27 01:02:48 +0800635}
Logan Chienda5e0c32011-06-13 03:47:21 +0800636#endif // USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800637
638
Logan Chienda5e0c32011-06-13 03:47:21 +0800639#if USE_MCJIT
640int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
641 llvm::NamedMDNode const *ExportVarMetadata,
642 llvm::NamedMDNode const *ExportFuncMetadata) {
643 // Decorate mEmittedELFExecutable with formatted ostream
644 llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable);
645
646 // Relax all machine instructions
647 TM->setMCRelaxAll(/* RelaxAll= */ true);
648
649 // Create MC code generation pass manager
650 llvm::PassManager MCCodeGenPasses;
651
652 // Add TargetData to MC code generation pass manager
653 MCCodeGenPasses.add(TD);
654
655 // Add MC code generation passes to pass manager
656 llvm::MCContext *Ctx;
657 if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS,
658 CodeGenOptLevel, false)) {
659 setError("Fail to add passes to emit file");
660 return 1;
661 }
662
663 MCCodeGenPasses.run(*mModule);
664 OutSVOS.flush();
665
666 // Load the ELF Object
667 mRSExecutable =
668 rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(),
669 mEmittedELFExecutable.size(),
670 &resolveSymbolAdapter, this);
671
672 if (!mRSExecutable) {
673 setError("Fail to load emitted ELF relocatable file");
674 return 1;
675 }
676
677#if !USE_OLD_JIT
678 // Note: If old JIT is compiled then we prefer the old version instead of the
679 // new version.
680
681 if (ExportVarMetadata) {
682 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
683
684 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
685 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
Logan Chien70dd9982011-06-13 23:21:39 +0800686 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
687 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
688 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
689 llvm::StringRef ExportVarName =
690 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
691
692 varList.push_back(
693 rsloaderGetSymbolAddress(mRSExecutable,
694 ExportVarName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700695#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700696 LOGD("runMCCodeGen(): Exported Var: %s @ %p\n", ExportVarName.str().c_str(),
Logan Chien70dd9982011-06-13 23:21:39 +0800697 varList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700698#endif
Logan Chien70dd9982011-06-13 23:21:39 +0800699 continue;
700 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800701 }
702
Logan Chien70dd9982011-06-13 23:21:39 +0800703 varList.push_back(NULL);
Logan Chienda5e0c32011-06-13 03:47:21 +0800704 }
705 }
706
707 if (ExportFuncMetadata) {
708 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
709
710 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
711 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
712 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
713 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
714 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
715 llvm::StringRef ExportFuncName =
716 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
717
718 funcList.push_back(
719 rsloaderGetSymbolAddress(mRSExecutable,
720 ExportFuncName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700721#if DEBUG_MCJIT_RELECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700722 LOGD("runMCCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800723 funcList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700724#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800725 }
726 }
727 }
728 }
729#endif // !USE_OLD_JIT
730
731#if USE_CACHE
732 // Write generated executable to file.
733 if (writeELFExecToFile() != 0) {
734 setError("Fail to write mcjit-ed executable to file");
735 return 1;
736 }
737#endif
738
739 return 0;
740}
741#endif // USE_MCJIT
742
743
744int Compiler::runLTO(llvm::TargetData *TD,
745 llvm::NamedMDNode const *ExportVarMetadata,
746 llvm::NamedMDNode const *ExportFuncMetadata) {
Logan Chien4cc00332011-06-12 14:00:46 +0800747 llvm::PassManager LTOPasses;
748
749 // Add TargetData to LTO passes
750 LTOPasses.add(TD);
751
752 // Collect All Exported Symbols
753 std::vector<const char*> ExportSymbols;
754
755 // Note: This is a workaround for getting export variable and function name.
756 // We should refine it soon.
757 if (ExportVarMetadata) {
758 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
759 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
760 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
761 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
762 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
763 llvm::StringRef ExportVarName =
764 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
765 ExportSymbols.push_back(ExportVarName.data());
766 }
767 }
768 }
769 }
770
771 if (ExportFuncMetadata) {
772 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
773 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
774 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
775 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
776 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
777 llvm::StringRef ExportFuncName =
778 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
779 ExportSymbols.push_back(ExportFuncName.data());
780 }
781 }
782 }
783 }
784
785 // root() and init() are born to be exported
786 ExportSymbols.push_back("root");
787 ExportSymbols.push_back("init");
788
789 // We now create passes list performing LTO. These are copied from
790 // (including comments) llvm::createStandardLTOPasses().
791
792 // Internalize all other symbols not listed in ExportSymbols
793 LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
794
795 // Propagate constants at call sites into the functions they call. This
796 // opens opportunities for globalopt (and inlining) by substituting
797 // function pointers passed as arguments to direct uses of functions.
798 LTOPasses.add(llvm::createIPSCCPPass());
799
800 // Now that we internalized some globals, see if we can hack on them!
801 LTOPasses.add(llvm::createGlobalOptimizerPass());
802
803 // Linking modules together can lead to duplicated global constants, only
804 // keep one copy of each constant...
805 LTOPasses.add(llvm::createConstantMergePass());
806
807 // Remove unused arguments from functions...
808 LTOPasses.add(llvm::createDeadArgEliminationPass());
809
810 // Reduce the code after globalopt and ipsccp. Both can open up
811 // significant simplification opportunities, and both can propagate
812 // functions through function pointers. When this happens, we often have
813 // to resolve varargs calls, etc, so let instcombine do this.
814 LTOPasses.add(llvm::createInstructionCombiningPass());
815
816 // Inline small functions
817 LTOPasses.add(llvm::createFunctionInliningPass());
818
819 // Remove dead EH info.
820 LTOPasses.add(llvm::createPruneEHPass());
821
822 // Internalize the globals again after inlining
823 LTOPasses.add(llvm::createGlobalOptimizerPass());
824
825 // Remove dead functions.
826 LTOPasses.add(llvm::createGlobalDCEPass());
827
828 // If we didn't decide to inline a function, check to see if we can
829 // transform it to pass arguments by value instead of by reference.
830 LTOPasses.add(llvm::createArgumentPromotionPass());
831
832 // The IPO passes may leave cruft around. Clean up after them.
833 LTOPasses.add(llvm::createInstructionCombiningPass());
834 LTOPasses.add(llvm::createJumpThreadingPass());
835
836 // Break up allocas
837 LTOPasses.add(llvm::createScalarReplAggregatesPass());
838
839 // Run a few AA driven optimizations here and now, to cleanup the code.
840 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture.
841 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
842
843 // Hoist loop invariants.
844 LTOPasses.add(llvm::createLICMPass());
845
846 // Remove redundancies.
847 LTOPasses.add(llvm::createGVNPass());
848
849 // Remove dead memcpys.
850 LTOPasses.add(llvm::createMemCpyOptPass());
851
852 // Nuke dead stores.
853 LTOPasses.add(llvm::createDeadStoreEliminationPass());
854
855 // Cleanup and simplify the code after the scalar optimizations.
856 LTOPasses.add(llvm::createInstructionCombiningPass());
857
858 LTOPasses.add(llvm::createJumpThreadingPass());
859
860 // Delete basic blocks, which optimization passes may have killed.
861 LTOPasses.add(llvm::createCFGSimplificationPass());
862
863 // Now that we have optimized the program, discard unreachable functions.
864 LTOPasses.add(llvm::createGlobalDCEPass());
865
866 LTOPasses.run(*mModule);
Logan Chienda5e0c32011-06-13 03:47:21 +0800867
868 return 0;
Logan Chien4cc00332011-06-12 14:00:46 +0800869}
870
871
Logan Chienda5e0c32011-06-13 03:47:21 +0800872#if USE_MCJIT
873int Compiler::writeELFExecToFile() {
874 std::string objPath(mCachePath);
875 if (!getObjPath(objPath)) {
876 LOGE("Fail to create objPath");
877 return 1;
878 }
879
880 FileHandle file;
881
882 int Fd = file.open(objPath.c_str(), OpenMode::Write);
883 if (Fd < 0) {
884 LOGE("Fail to open file '%s'", objPath.c_str());
885 return 1;
886 }
887
888 file.write(&*mEmittedELFExecutable.begin(), mEmittedELFExecutable.size());
889
890 return 0;
891}
892#endif
893
894
895#if USE_MCJIT
896void *Compiler::getSymbolAddress(char const *name) {
897 return rsloaderGetSymbolAddress(mRSExecutable, name);
898}
899#endif
900
901
902#if USE_MCJIT
903void *Compiler::resolveSymbolAdapter(void *context, char const *name) {
904 Compiler *self = reinterpret_cast<Compiler *>(context);
905
906 if (void *Addr = FindRuntimeFunction(name)) {
907 return Addr;
908 }
909
910 if (self->mpSymbolLookupFn) {
911 if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
912 return Addr;
913 }
914 }
915
916 LOGE("Unable to resolve symbol: %s\n", name);
917 return NULL;
918}
919#endif
920
921
Logan1f028c02010-11-27 01:02:48 +0800922Compiler::~Compiler() {
Logan1f028c02010-11-27 01:02:48 +0800923 delete mModule;
Logan1f028c02010-11-27 01:02:48 +0800924 delete mContext;
Logana4994f52010-11-27 14:06:02 +0800925
Logan Chienda5e0c32011-06-13 03:47:21 +0800926#if USE_MCJIT
927 rsloaderDisposeExec(mRSExecutable);
928#endif
929
Logana4994f52010-11-27 14:06:02 +0800930 // llvm::llvm_shutdown();
Logan1f028c02010-11-27 01:02:48 +0800931}
932
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700933#if USE_MCJIT && USE_DISASSEMBLER
934void Compiler::Disassemble(llvm::Target const *Target,
935 llvm::TargetMachine *TM,
936 std::string const &Name,
937 unsigned char const *Func,
938 size_t FuncSize) {
939 llvm::raw_ostream *OS;
940
941#if USE_DISASSEMBLER_FILE
942 std::string ErrorInfo;
943 OS = new llvm::raw_fd_ostream("/data/local/tmp/mcjit-dis.s", ErrorInfo,
944 llvm::raw_fd_ostream::F_Append);
945
946 if (!ErrorInfo.empty()) { // some errors occurred
947 // LOGE("Error in creating disassembly file");
948 delete OS;
949 return;
950 }
951#else
952 OS = &llvm::outs();
953#endif
954
955 *OS << "MC/ Disassembled code: " << Name << "\n";
956
957 const llvm::MCAsmInfo *AsmInfo;
958 const llvm::MCDisassembler *Disassmbler;
959 llvm::MCInstPrinter *IP;
960
961 AsmInfo = Target->createAsmInfo(Compiler::Triple);
962 Disassmbler = Target->createMCDisassembler();
963 IP = Target->createMCInstPrinter(*TM,
964 AsmInfo->getAssemblerDialect(),
965 *AsmInfo);
966
967 const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize);
968
969 uint64_t Size;
970 uint64_t Index;
971
972 for (Index = 0; Index < FuncSize; Index += Size) {
973 llvm::MCInst Inst;
974
975 if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
976 /* REMOVED */ llvm::nulls())) {
977 OS->indent(4);
978 OS->write("0x", 2);
979 OS->write_hex((uint32_t)Func + Index);
980 OS->write(": 0x", 4);
981 OS->write_hex(*(uint32_t *)(Func + Index));
982 IP->printInst(&Inst, *OS);
983 *OS << "\n";
984 } else {
985 if (Size == 0)
986 Size = 1; // skip illegible bytes
987 }
988 }
989
990 *OS << "\n";
991 delete BufferMObj;
992
993 delete AsmInfo;
994 delete Disassmbler;
995 delete IP;
996
997#if USE_DISASSEMBLER_FILE
998 // If you want the disassemble results write to file, uncomment this.
999 ((llvm::raw_fd_ostream*)OS)->close();
1000 delete OS;
1001#endif
1002}
1003#endif
1004
1005
Logan1f028c02010-11-27 01:02:48 +08001006} // namespace bcc