blob: ffa73113254b1bf95a04ac54b86f04a283c0442f [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
Logan1f028c02010-11-27 01:02:48 +0800112namespace bcc {
113
114//////////////////////////////////////////////////////////////////////////////
115// BCC Compiler Static Variables
116//////////////////////////////////////////////////////////////////////////////
117
118bool Compiler::GlobalInitialized = false;
119
Logan1f028c02010-11-27 01:02:48 +0800120// Code generation optimization level for the compiler
121llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel;
122
123std::string Compiler::Triple;
124
125std::string Compiler::CPU;
126
127std::vector<std::string> Compiler::Features;
128
Stephen Hines071288a2011-01-27 14:38:26 -0800129// Name of metadata node where pragma info resides (should be synced with
Logan1f028c02010-11-27 01:02:48 +0800130// slang.cpp)
131const llvm::StringRef Compiler::PragmaMetadataName = "#pragma";
132
Stephen Hines071288a2011-01-27 14:38:26 -0800133// Name of metadata node where exported variable names reside (should be
Logan1f028c02010-11-27 01:02:48 +0800134// synced with slang_rs_metadata.h)
135const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var";
136
Stephen Hines071288a2011-01-27 14:38:26 -0800137// Name of metadata node where exported function names reside (should be
Logan1f028c02010-11-27 01:02:48 +0800138// synced with slang_rs_metadata.h)
139const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func";
140
Stephen Hines071288a2011-01-27 14:38:26 -0800141// Name of metadata node where RS object slot info resides (should be
142// synced with slang_rs_metadata.h)
143const llvm::StringRef Compiler::ObjectSlotMetadataName = "#rs_object_slots";
Logan1f028c02010-11-27 01:02:48 +0800144
145//////////////////////////////////////////////////////////////////////////////
146// Compiler
147//////////////////////////////////////////////////////////////////////////////
148
149void Compiler::GlobalInitialization() {
150 if (GlobalInitialized)
151 return;
Logan1f028c02010-11-27 01:02:48 +0800152 // if (!llvm::llvm_is_multithreaded())
153 // llvm::llvm_start_multithreaded();
154
155 // Set Triple, CPU and Features here
156 Triple = TARGET_TRIPLE_STRING;
157
Logan1f028c02010-11-27 01:02:48 +0800158 Features.push_back("+vfp3");
Logan1f028c02010-11-27 01:02:48 +0800159
Logan4fe966f2011-02-27 08:26:40 +0800160 // NOTE: Currently, we have to turn off the support for NEON explicitly.
161 // Since the ARMCodeEmitter.cpp is not ready for JITing NEON
162 // instructions.
Joseph Wen51001b82011-06-23 18:56:45 -0700163#if ARCH_ARM_HAVE_NEON
164 Features.push_back("+d32");
165 Features.push_back("+neon");
166 Features.push_back("+neonfp");
167#else
168 Features.push_back("+d16");
169 Features.push_back("-neon");
Logan4fe966f2011-02-27 08:26:40 +0800170 Features.push_back("-neonfp");
Joseph Wen51001b82011-06-23 18:56:45 -0700171#endif
172
Logan4fe966f2011-02-27 08:26:40 +0800173 Features.push_back("-vmlx");
174
Logan1f028c02010-11-27 01:02:48 +0800175#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
176 LLVMInitializeARMTargetInfo();
177 LLVMInitializeARMTarget();
Logan35849002011-01-15 07:30:43 +0800178#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800179 LLVMInitializeARMDisassembler();
180 LLVMInitializeARMAsmPrinter();
181#endif
182#endif
183
184#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
185 LLVMInitializeX86TargetInfo();
186 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800187#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800188 LLVMInitializeX86Disassembler();
189 LLVMInitializeX86AsmPrinter();
190#endif
191#endif
192
193#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
194 LLVMInitializeX86TargetInfo();
195 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800196#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800197 LLVMInitializeX86Disassembler();
198 LLVMInitializeX86AsmPrinter();
199#endif
200#endif
201
202 // -O0: llvm::CodeGenOpt::None
203 // -O1: llvm::CodeGenOpt::Less
204 // -O2: llvm::CodeGenOpt::Default
205 // -O3: llvm::CodeGenOpt::Aggressive
Shih-wei Liao72f67a62010-12-14 13:36:15 -0800206 CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
Logan1f028c02010-11-27 01:02:48 +0800207
208 // Below are the global settings to LLVM
209
210 // Disable frame pointer elimination optimization
211 llvm::NoFramePointerElim = false;
212
213 // Use hardfloat ABI
214 //
215 // TODO(all): Need to detect the CPU capability and decide whether to use
216 // softfp. To use softfp, change following 2 lines to
217 //
218 // llvm::FloatABIType = llvm::FloatABI::Soft;
219 // llvm::UseSoftFloat = true;
220 //
Shih-wei Liaoe728cb82010-12-15 15:20:47 -0800221 llvm::FloatABIType = llvm::FloatABI::Soft;
Logan1f028c02010-11-27 01:02:48 +0800222 llvm::UseSoftFloat = false;
223
224 // BCC needs all unknown symbols resolved at JIT/compilation time.
225 // So we don't need any dynamic relocation model.
226 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static);
227
228#if defined(DEFAULT_X64_CODEGEN)
229 // Data address in X86_64 architecture may reside in a far-away place
230 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium);
231#else
232 // This is set for the linker (specify how large of the virtual addresses
233 // we can access for all unknown symbols.)
234 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small);
235#endif
236
237 // Register the scheduler
238 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
239
240 // Register allocation policy:
241 // createFastRegisterAllocator: fast but bad quality
242 // createLinearScanRegisterAllocator: not so fast but good quality
243 llvm::RegisterRegAlloc::setDefault
244 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
245 llvm::createFastRegisterAllocator :
246 llvm::createLinearScanRegisterAllocator);
247
Logan35849002011-01-15 07:30:43 +0800248#if USE_CACHE
Logan75cc8a52011-01-07 06:06:52 +0800249 // Calculate the SHA1 checksum of libbcc and libRS.
Joseph Wen2ca6e572011-06-24 14:12:23 -0700250 calcFileSHA1(sha1LibBCC_SHA1, pathLibBCC_SHA1);
Logan35849002011-01-15 07:30:43 +0800251#endif
Logan75cc8a52011-01-07 06:06:52 +0800252
Logan1f028c02010-11-27 01:02:48 +0800253 GlobalInitialized = true;
254}
255
256
257void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
258 std::string *Error = static_cast<std::string*>(UserData);
259 Error->assign(Message);
260 LOGE("%s", Message.c_str());
261 exit(1);
262}
263
264
Logan Chienda5e0c32011-06-13 03:47:21 +0800265#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800266CodeMemoryManager *Compiler::createCodeMemoryManager() {
267 mCodeMemMgr.reset(new CodeMemoryManager());
268 return mCodeMemMgr.get();
269}
Logan Chienda5e0c32011-06-13 03:47:21 +0800270#endif
Logan1f028c02010-11-27 01:02:48 +0800271
272
Logan Chienda5e0c32011-06-13 03:47:21 +0800273#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800274CodeEmitter *Compiler::createCodeEmitter() {
Logan7dcaac92011-01-06 04:26:23 +0800275 mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get()));
Logan1f028c02010-11-27 01:02:48 +0800276 return mCodeEmitter.get();
277}
Logan Chienda5e0c32011-06-13 03:47:21 +0800278#endif
Logan1f028c02010-11-27 01:02:48 +0800279
280
Logan2a6dc822011-01-06 04:05:20 +0800281Compiler::Compiler(ScriptCompiled *result)
282 : mpResult(result),
Logan Chienda5e0c32011-06-13 03:47:21 +0800283#if USE_MCJIT
284 mRSExecutable(NULL),
285#endif
Logan1f028c02010-11-27 01:02:48 +0800286 mpSymbolLookupFn(NULL),
287 mpSymbolLookupContext(NULL),
288 mContext(NULL),
289 mModule(NULL),
290 mHasLinked(false) /* Turn off linker */ {
291 llvm::remove_fatal_error_handler();
292 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
293 mContext = new llvm::LLVMContext();
294 return;
295}
296
Logan1f028c02010-11-27 01:02:48 +0800297
Logan Chienda5e0c32011-06-13 03:47:21 +0800298#if USE_MCJIT
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700299// input objPath: For example,
300// /data/user/0/com.example.android.rs.fountain/cache/
301// @com.example.android.rs.fountain:raw@fountain.oBCC
302// output objPath: /data/user/0/com.example.android.rs.fountain/cache/
303// fountain.o
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700304//
305bool Compiler::getObjPath(std::string &objPath) {
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700306 size_t found0 = objPath.find("@");
307 size_t found1 = objPath.rfind("@");
308
309 if (found0 == found1 ||
Nowar Gu09b6c1c2011-05-24 23:49:07 +0800310 found0 == std::string::npos ||
311 found1 == std::string::npos) {
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700312 LOGE("Ill formatted resource name '%s'. The name should contain 2 @s",
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700313 objPath.c_str());
314 return false;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700315 }
316
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700317 objPath.replace(found0, found1 - found0 + 1, "", 0);
318 objPath.resize(objPath.length() - 3);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700319
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700320 LOGV("objPath = %s", objPath.c_str());
321 return true;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700322}
Logan Chienda5e0c32011-06-13 03:47:21 +0800323#endif
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700324
325
Logan474cbd22011-01-31 01:47:44 +0800326llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) {
327 llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError);
Logan1f028c02010-11-27 01:02:48 +0800328
Logan474cbd22011-01-31 01:47:44 +0800329 if (!result) {
330 LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str());
331 return NULL;
Logan1f028c02010-11-27 01:02:48 +0800332 }
333
Logan474cbd22011-01-31 01:47:44 +0800334 return result;
Logan1f028c02010-11-27 01:02:48 +0800335}
336
337
Logan474cbd22011-01-31 01:47:44 +0800338int Compiler::linkModule(llvm::Module *moduleWith) {
339 if (llvm::Linker::LinkModules(mModule, moduleWith, &mError) != 0) {
Logan1f028c02010-11-27 01:02:48 +0800340 return hasError();
341 }
342
Logan1f028c02010-11-27 01:02:48 +0800343 // Everything for linking should be settled down here with no error occurs
344 mHasLinked = true;
345 return hasError();
346}
347
348
Logan1f028c02010-11-27 01:02:48 +0800349int Compiler::compile() {
Logan Chienda5e0c32011-06-13 03:47:21 +0800350 llvm::Target const *Target = NULL;
Logan1f028c02010-11-27 01:02:48 +0800351 llvm::TargetData *TD = NULL;
Logan1f028c02010-11-27 01:02:48 +0800352 llvm::TargetMachine *TM = NULL;
Logan Chienda5e0c32011-06-13 03:47:21 +0800353
Logan1f028c02010-11-27 01:02:48 +0800354 std::string FeaturesStr;
355
Logan Chienda5e0c32011-06-13 03:47:21 +0800356 llvm::NamedMDNode const *PragmaMetadata;
357 llvm::NamedMDNode const *ExportVarMetadata;
358 llvm::NamedMDNode const *ExportFuncMetadata;
359 llvm::NamedMDNode const *ObjectSlotMetadata;
Logan1f028c02010-11-27 01:02:48 +0800360
Logan1f028c02010-11-27 01:02:48 +0800361 if (mModule == NULL) // No module was loaded
362 return 0;
363
364 // Create TargetMachine
365 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
366 if (hasError())
367 goto on_bcc_compile_error;
368
369 if (!CPU.empty() || !Features.empty()) {
370 llvm::SubtargetFeatures F;
371 F.setCPU(CPU);
Logana4994f52010-11-27 14:06:02 +0800372
373 for (std::vector<std::string>::const_iterator
374 I = Features.begin(), E = Features.end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800375 F.AddFeature(*I);
Logana4994f52010-11-27 14:06:02 +0800376 }
377
Logan1f028c02010-11-27 01:02:48 +0800378 FeaturesStr = F.getString();
379 }
380
381 TM = Target->createTargetMachine(Triple, FeaturesStr);
382 if (TM == NULL) {
383 setError("Failed to create target machine implementation for the"
384 " specified triple '" + Triple + "'");
385 goto on_bcc_compile_error;
386 }
387
Logan Chienda5e0c32011-06-13 03:47:21 +0800388 // Get target data from Module
389 TD = new llvm::TargetData(mModule);
390
391 // Load named metadata
392 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName);
393 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName);
394 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName);
395 ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName);
396
397 // Perform link-time optimization if we have multiple modules
398 if (mHasLinked) {
399 runLTO(new llvm::TargetData(*TD), ExportVarMetadata, ExportFuncMetadata);
400 }
401
402 // Perform code generation
403#if USE_OLD_JIT
404 if (runCodeGen(new llvm::TargetData(*TD), TM,
405 ExportVarMetadata, ExportFuncMetadata) != 0) {
406 goto on_bcc_compile_error;
407 }
408#endif
409
410#if USE_MCJIT
411 if (runMCCodeGen(new llvm::TargetData(*TD), TM,
412 ExportVarMetadata, ExportFuncMetadata) != 0) {
413 goto on_bcc_compile_error;
414 }
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700415#if USE_DISASSEMBLER && DEBUG_MCJIT_DISASSEMBLE
416 {
Shih-wei Liaod3c551f2011-07-01 04:28:27 -0700417 // Get MC codegen emitted function name list
418 size_t func_list_size = rsloaderGetFuncCount(mRSExecutable);
419 std::vector<char const *> func_list(func_list_size, NULL);
420 rsloaderGetFuncNameList(mRSExecutable, func_list_size, &*func_list.begin());
Shih-wei Liao320b5492011-06-20 22:53:33 -0700421
Shih-wei Liaod3c551f2011-07-01 04:28:27 -0700422 // Disassemble each function
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700423 for (size_t i = 0; i < func_list_size; ++i) {
424 void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]);
425 if (func) {
426 size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]);
427 Disassemble(Target, TM, func_list[i], (unsigned char const *)func, size);
428 }
429 }
430 }
431#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800432#endif
433
434 // Read pragma information from the metadata node of the module.
435 if (PragmaMetadata) {
436 ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas;
437
438 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) {
439 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
440 if (Pragma != NULL &&
441 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) {
442 llvm::Value *PragmaNameMDS = Pragma->getOperand(0);
443 llvm::Value *PragmaValueMDS = Pragma->getOperand(1);
444
445 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) &&
446 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) {
447 llvm::StringRef PragmaName =
448 static_cast<llvm::MDString*>(PragmaNameMDS)->getString();
449 llvm::StringRef PragmaValue =
450 static_cast<llvm::MDString*>(PragmaValueMDS)->getString();
451
452 pragmaList.push_back(
453 std::make_pair(std::string(PragmaName.data(),
454 PragmaName.size()),
455 std::string(PragmaValue.data(),
456 PragmaValue.size())));
Shih-wei Liao9f73de02011-07-01 04:40:24 -0700457#if DEBUG_BCC_REFLECT
458 LOGD("compile(): Pragma: %s -> %s\n",
459 pragmaList.back().first.c_str(),
460 pragmaList.back().second.c_str());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700461#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800462 }
463 }
464 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800465 }
466
467 if (ObjectSlotMetadata) {
468 ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots;
469
470 for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) {
471 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i);
472 if (ObjectSlot != NULL &&
473 ObjectSlot->getNumOperands() == 1) {
474 llvm::Value *SlotMDS = ObjectSlot->getOperand(0);
475 if (SlotMDS->getValueID() == llvm::Value::MDStringVal) {
476 llvm::StringRef Slot =
477 static_cast<llvm::MDString*>(SlotMDS)->getString();
478 uint32_t USlot = 0;
479 if (Slot.getAsInteger(10, USlot)) {
480 setError("Non-integer object slot value '" + Slot.str() + "'");
481 goto on_bcc_compile_error;
482 }
483 objectSlotList.push_back(USlot);
Shih-wei Liao9f73de02011-07-01 04:40:24 -0700484#if DEBUG_BCC_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700485 LOGD("compile(): RefCount Slot: %s @ %u\n", Slot.str().c_str(), USlot);
486#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800487 }
488 }
489 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800490 }
491
492on_bcc_compile_error:
493 // LOGE("on_bcc_compiler_error");
494 if (TD) {
495 delete TD;
496 }
497
498 if (TM) {
499 delete TM;
500 }
501
502 if (mError.empty()) {
503 return 0;
504 }
505
506 // LOGE(getErrorMessage());
507 return 1;
508}
509
510
511#if USE_OLD_JIT
512int Compiler::runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
513 llvm::NamedMDNode const *ExportVarMetadata,
514 llvm::NamedMDNode const *ExportFuncMetadata) {
Logan1f028c02010-11-27 01:02:48 +0800515 // Create memory manager for creation of code emitter later.
516 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) {
517 setError("Failed to startup memory management for further compilation");
Logan Chienda5e0c32011-06-13 03:47:21 +0800518 return 1;
Logan1f028c02010-11-27 01:02:48 +0800519 }
Logan02286cb2011-01-07 00:30:47 +0800520
521 mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase());
Logan1f028c02010-11-27 01:02:48 +0800522
523 // Create code emitter
524 if (!mCodeEmitter.get()) {
525 if (!createCodeEmitter()) {
Logan Chienda5e0c32011-06-13 03:47:21 +0800526 setError("Failed to create machine code emitter for compilation");
527 return 1;
Logan1f028c02010-11-27 01:02:48 +0800528 }
529 } else {
530 // Reuse the code emitter
531 mCodeEmitter->reset();
532 }
533
534 mCodeEmitter->setTargetMachine(*TM);
535 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn,
536 mpSymbolLookupContext);
537
Logan1f028c02010-11-27 01:02:48 +0800538 // Create code-gen pass to run the code emitter
Logan Chienda5e0c32011-06-13 03:47:21 +0800539 llvm::OwningPtr<llvm::FunctionPassManager> CodeGenPasses(
540 new llvm::FunctionPassManager(mModule));
Logan1f028c02010-11-27 01:02:48 +0800541
Logan Chienda5e0c32011-06-13 03:47:21 +0800542 // Add TargetData to code generation pass manager
543 CodeGenPasses->add(TD);
544
545 // Add code emit passes
Logan1f028c02010-11-27 01:02:48 +0800546 if (TM->addPassesToEmitMachineCode(*CodeGenPasses,
547 *mCodeEmitter,
548 CodeGenOptLevel)) {
Logan Chienda5e0c32011-06-13 03:47:21 +0800549 setError("The machine code emission is not supported on '" + Triple + "'");
550 return 1;
Logan1f028c02010-11-27 01:02:48 +0800551 }
552
Logan Chienda5e0c32011-06-13 03:47:21 +0800553 // Run the code emitter on every non-declaration function in the module
Logan1f028c02010-11-27 01:02:48 +0800554 CodeGenPasses->doInitialization();
Logan Chienda5e0c32011-06-13 03:47:21 +0800555 for (llvm::Module::iterator
556 I = mModule->begin(), E = mModule->end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800557 if (!I->isDeclaration()) {
558 CodeGenPasses->run(*I);
559 }
560 }
561
562 CodeGenPasses->doFinalization();
Logan Chienb0ceca22011-06-12 13:34:49 +0800563
Logan1f028c02010-11-27 01:02:48 +0800564 // Copy the global address mapping from code emitter and remapping
565 if (ExportVarMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800566 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
567
Logan1f028c02010-11-27 01:02:48 +0800568 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
569 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
570 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
571 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
572 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
573 llvm::StringRef ExportVarName =
574 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
575
576 CodeEmitter::global_addresses_const_iterator I, E;
577 for (I = mCodeEmitter->global_address_begin(),
578 E = mCodeEmitter->global_address_end();
579 I != E; I++) {
580 if (I->first->getValueID() != llvm::Value::GlobalVariableVal)
581 continue;
582 if (ExportVarName == I->first->getName()) {
Logan2a6dc822011-01-06 04:05:20 +0800583 varList.push_back(I->second);
Shih-wei Liao9f73de02011-07-01 04:40:24 -0700584#if DEBUG_BCC_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700585 LOGD("runCodeGen(): Exported VAR: %s @ %p\n", ExportVarName.str().c_str(), I->second);
586#endif
Logan1f028c02010-11-27 01:02:48 +0800587 break;
588 }
589 }
590 if (I != mCodeEmitter->global_address_end())
591 continue; // found
Logan Chien7d1bf582011-06-13 23:22:40 +0800592
Shih-wei Liao9f73de02011-07-01 04:40:24 -0700593#if DEBUG_BCC_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700594 LOGD("runCodeGen(): Exported VAR: %s @ %p\n",
Logan Chien7d1bf582011-06-13 23:22:40 +0800595 ExportVarName.str().c_str(), (void *)0);
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700596#endif
Logan1f028c02010-11-27 01:02:48 +0800597 }
598 }
599 // if reaching here, we know the global variable record in metadata is
600 // not found. So we make an empty slot
Logan2a6dc822011-01-06 04:05:20 +0800601 varList.push_back(NULL);
Logan1f028c02010-11-27 01:02:48 +0800602 }
Logan2a6dc822011-01-06 04:05:20 +0800603
Stephen Hinesbbcef8a2011-05-04 19:40:10 -0700604 bccAssert((varList.size() == ExportVarMetadata->getNumOperands()) &&
605 "Number of slots doesn't match the number of export variables!");
Logan1f028c02010-11-27 01:02:48 +0800606 }
607
608 if (ExportFuncMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800609 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
610
Logan1f028c02010-11-27 01:02:48 +0800611 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
612 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
613 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
614 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
615 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
616 llvm::StringRef ExportFuncName =
617 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
Logan7dcaac92011-01-06 04:26:23 +0800618 funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str()));
Shih-wei Liao9f73de02011-07-01 04:40:24 -0700619#if DEBUG_BCC_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700620 LOGD("runCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800621 funcList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700622#endif
Logan1f028c02010-11-27 01:02:48 +0800623 }
624 }
625 }
626 }
627
628 // Tell code emitter now can release the memory using during the JIT since
629 // we have done the code emission
630 mCodeEmitter->releaseUnnecessary();
631
Logan Chienda5e0c32011-06-13 03:47:21 +0800632 return 0;
Logan1f028c02010-11-27 01:02:48 +0800633}
Logan Chienda5e0c32011-06-13 03:47:21 +0800634#endif // USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +0800635
636
Logan Chienda5e0c32011-06-13 03:47:21 +0800637#if USE_MCJIT
638int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
639 llvm::NamedMDNode const *ExportVarMetadata,
640 llvm::NamedMDNode const *ExportFuncMetadata) {
641 // Decorate mEmittedELFExecutable with formatted ostream
642 llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable);
643
644 // Relax all machine instructions
645 TM->setMCRelaxAll(/* RelaxAll= */ true);
646
647 // Create MC code generation pass manager
648 llvm::PassManager MCCodeGenPasses;
649
650 // Add TargetData to MC code generation pass manager
651 MCCodeGenPasses.add(TD);
652
653 // Add MC code generation passes to pass manager
654 llvm::MCContext *Ctx;
655 if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS,
656 CodeGenOptLevel, false)) {
657 setError("Fail to add passes to emit file");
658 return 1;
659 }
660
661 MCCodeGenPasses.run(*mModule);
662 OutSVOS.flush();
663
664 // Load the ELF Object
665 mRSExecutable =
666 rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(),
667 mEmittedELFExecutable.size(),
668 &resolveSymbolAdapter, this);
669
670 if (!mRSExecutable) {
671 setError("Fail to load emitted ELF relocatable file");
672 return 1;
673 }
674
675#if !USE_OLD_JIT
676 // Note: If old JIT is compiled then we prefer the old version instead of the
677 // new version.
678
679 if (ExportVarMetadata) {
680 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
681
682 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
683 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
Logan Chien70dd9982011-06-13 23:21:39 +0800684 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
685 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
686 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
687 llvm::StringRef ExportVarName =
688 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
689
690 varList.push_back(
691 rsloaderGetSymbolAddress(mRSExecutable,
692 ExportVarName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700693#if DEBUG_MCJIT_REFLECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700694 LOGD("runMCCodeGen(): Exported Var: %s @ %p\n", ExportVarName.str().c_str(),
Logan Chien70dd9982011-06-13 23:21:39 +0800695 varList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700696#endif
Logan Chien70dd9982011-06-13 23:21:39 +0800697 continue;
698 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800699 }
700
Logan Chien70dd9982011-06-13 23:21:39 +0800701 varList.push_back(NULL);
Logan Chienda5e0c32011-06-13 03:47:21 +0800702 }
703 }
704
705 if (ExportFuncMetadata) {
706 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
707
708 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
709 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
710 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
711 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
712 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
713 llvm::StringRef ExportFuncName =
714 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
715
716 funcList.push_back(
717 rsloaderGetSymbolAddress(mRSExecutable,
718 ExportFuncName.str().c_str()));
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700719#if DEBUG_MCJIT_RELECT
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700720 LOGD("runMCCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(),
Logan Chien7d1bf582011-06-13 23:22:40 +0800721 funcList.back());
Shih-wei Liao749a51c2011-06-17 16:02:18 -0700722#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800723 }
724 }
725 }
726 }
727#endif // !USE_OLD_JIT
Logan Chienda5e0c32011-06-13 03:47:21 +0800728 return 0;
729}
730#endif // USE_MCJIT
731
732
733int Compiler::runLTO(llvm::TargetData *TD,
734 llvm::NamedMDNode const *ExportVarMetadata,
735 llvm::NamedMDNode const *ExportFuncMetadata) {
Logan Chien4cc00332011-06-12 14:00:46 +0800736 llvm::PassManager LTOPasses;
737
738 // Add TargetData to LTO passes
739 LTOPasses.add(TD);
740
741 // Collect All Exported Symbols
742 std::vector<const char*> ExportSymbols;
743
744 // Note: This is a workaround for getting export variable and function name.
745 // We should refine it soon.
746 if (ExportVarMetadata) {
747 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
748 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
749 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
750 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
751 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
752 llvm::StringRef ExportVarName =
753 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
754 ExportSymbols.push_back(ExportVarName.data());
755 }
756 }
757 }
758 }
759
760 if (ExportFuncMetadata) {
761 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
762 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
763 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
764 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
765 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
766 llvm::StringRef ExportFuncName =
767 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
768 ExportSymbols.push_back(ExportFuncName.data());
769 }
770 }
771 }
772 }
773
774 // root() and init() are born to be exported
775 ExportSymbols.push_back("root");
776 ExportSymbols.push_back("init");
777
778 // We now create passes list performing LTO. These are copied from
779 // (including comments) llvm::createStandardLTOPasses().
780
781 // Internalize all other symbols not listed in ExportSymbols
782 LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
783
784 // Propagate constants at call sites into the functions they call. This
785 // opens opportunities for globalopt (and inlining) by substituting
786 // function pointers passed as arguments to direct uses of functions.
787 LTOPasses.add(llvm::createIPSCCPPass());
788
789 // Now that we internalized some globals, see if we can hack on them!
790 LTOPasses.add(llvm::createGlobalOptimizerPass());
791
792 // Linking modules together can lead to duplicated global constants, only
793 // keep one copy of each constant...
794 LTOPasses.add(llvm::createConstantMergePass());
795
796 // Remove unused arguments from functions...
797 LTOPasses.add(llvm::createDeadArgEliminationPass());
798
799 // Reduce the code after globalopt and ipsccp. Both can open up
800 // significant simplification opportunities, and both can propagate
801 // functions through function pointers. When this happens, we often have
802 // to resolve varargs calls, etc, so let instcombine do this.
803 LTOPasses.add(llvm::createInstructionCombiningPass());
804
805 // Inline small functions
806 LTOPasses.add(llvm::createFunctionInliningPass());
807
808 // Remove dead EH info.
809 LTOPasses.add(llvm::createPruneEHPass());
810
811 // Internalize the globals again after inlining
812 LTOPasses.add(llvm::createGlobalOptimizerPass());
813
814 // Remove dead functions.
815 LTOPasses.add(llvm::createGlobalDCEPass());
816
817 // If we didn't decide to inline a function, check to see if we can
818 // transform it to pass arguments by value instead of by reference.
819 LTOPasses.add(llvm::createArgumentPromotionPass());
820
821 // The IPO passes may leave cruft around. Clean up after them.
822 LTOPasses.add(llvm::createInstructionCombiningPass());
823 LTOPasses.add(llvm::createJumpThreadingPass());
824
825 // Break up allocas
826 LTOPasses.add(llvm::createScalarReplAggregatesPass());
827
828 // Run a few AA driven optimizations here and now, to cleanup the code.
829 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture.
830 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
831
832 // Hoist loop invariants.
833 LTOPasses.add(llvm::createLICMPass());
834
835 // Remove redundancies.
836 LTOPasses.add(llvm::createGVNPass());
837
838 // Remove dead memcpys.
839 LTOPasses.add(llvm::createMemCpyOptPass());
840
841 // Nuke dead stores.
842 LTOPasses.add(llvm::createDeadStoreEliminationPass());
843
844 // Cleanup and simplify the code after the scalar optimizations.
845 LTOPasses.add(llvm::createInstructionCombiningPass());
846
847 LTOPasses.add(llvm::createJumpThreadingPass());
848
849 // Delete basic blocks, which optimization passes may have killed.
850 LTOPasses.add(llvm::createCFGSimplificationPass());
851
852 // Now that we have optimized the program, discard unreachable functions.
853 LTOPasses.add(llvm::createGlobalDCEPass());
854
855 LTOPasses.run(*mModule);
Logan Chienda5e0c32011-06-13 03:47:21 +0800856
857 return 0;
Logan Chien4cc00332011-06-12 14:00:46 +0800858}
859
860
Logan Chienda5e0c32011-06-13 03:47:21 +0800861#if USE_MCJIT
Logan Chienda5e0c32011-06-13 03:47:21 +0800862void *Compiler::getSymbolAddress(char const *name) {
863 return rsloaderGetSymbolAddress(mRSExecutable, name);
864}
865#endif
866
867
868#if USE_MCJIT
869void *Compiler::resolveSymbolAdapter(void *context, char const *name) {
870 Compiler *self = reinterpret_cast<Compiler *>(context);
871
872 if (void *Addr = FindRuntimeFunction(name)) {
873 return Addr;
874 }
875
876 if (self->mpSymbolLookupFn) {
877 if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
878 return Addr;
879 }
880 }
881
882 LOGE("Unable to resolve symbol: %s\n", name);
883 return NULL;
884}
885#endif
886
887
Logan1f028c02010-11-27 01:02:48 +0800888Compiler::~Compiler() {
Logan1f028c02010-11-27 01:02:48 +0800889 delete mModule;
Logan1f028c02010-11-27 01:02:48 +0800890 delete mContext;
Logana4994f52010-11-27 14:06:02 +0800891
Logan Chienda5e0c32011-06-13 03:47:21 +0800892#if USE_MCJIT
893 rsloaderDisposeExec(mRSExecutable);
894#endif
895
Logana4994f52010-11-27 14:06:02 +0800896 // llvm::llvm_shutdown();
Logan1f028c02010-11-27 01:02:48 +0800897}
898
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700899#if USE_MCJIT && USE_DISASSEMBLER
900void Compiler::Disassemble(llvm::Target const *Target,
901 llvm::TargetMachine *TM,
902 std::string const &Name,
903 unsigned char const *Func,
904 size_t FuncSize) {
905 llvm::raw_ostream *OS;
906
907#if USE_DISASSEMBLER_FILE
908 std::string ErrorInfo;
909 OS = new llvm::raw_fd_ostream("/data/local/tmp/mcjit-dis.s", ErrorInfo,
910 llvm::raw_fd_ostream::F_Append);
911
912 if (!ErrorInfo.empty()) { // some errors occurred
913 // LOGE("Error in creating disassembly file");
914 delete OS;
915 return;
916 }
917#else
918 OS = &llvm::outs();
919#endif
920
921 *OS << "MC/ Disassembled code: " << Name << "\n";
922
923 const llvm::MCAsmInfo *AsmInfo;
924 const llvm::MCDisassembler *Disassmbler;
925 llvm::MCInstPrinter *IP;
926
927 AsmInfo = Target->createAsmInfo(Compiler::Triple);
928 Disassmbler = Target->createMCDisassembler();
929 IP = Target->createMCInstPrinter(*TM,
930 AsmInfo->getAssemblerDialect(),
931 *AsmInfo);
932
933 const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize);
934
935 uint64_t Size;
936 uint64_t Index;
937
938 for (Index = 0; Index < FuncSize; Index += Size) {
939 llvm::MCInst Inst;
940
941 if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
942 /* REMOVED */ llvm::nulls())) {
943 OS->indent(4);
944 OS->write("0x", 2);
945 OS->write_hex((uint32_t)Func + Index);
946 OS->write(": 0x", 4);
947 OS->write_hex(*(uint32_t *)(Func + Index));
948 IP->printInst(&Inst, *OS);
949 *OS << "\n";
950 } else {
951 if (Size == 0)
952 Size = 1; // skip illegible bytes
953 }
954 }
955
956 *OS << "\n";
957 delete BufferMObj;
958
959 delete AsmInfo;
960 delete Disassmbler;
961 delete IP;
962
963#if USE_DISASSEMBLER_FILE
964 // If you want the disassemble results write to file, uncomment this.
965 ((llvm::raw_fd_ostream*)OS)->close();
966 delete OS;
967#endif
968}
969#endif
970
971
Logan1f028c02010-11-27 01:02:48 +0800972} // namespace bcc