blob: b1bd093212e296402287a461badc0ab38a4da9e5 [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"
Logan2a6dc822011-01-06 04:05:20 +080023#include "ScriptCompiled.h"
Logan75cc8a52011-01-07 06:06:52 +080024#include "Sha1Helper.h"
Loganeb3d12b2010-12-16 06:20:18 +080025
Logandf23afa2010-11-27 11:04:54 +080026#include "llvm/ADT/StringRef.h"
Logan1f028c02010-11-27 01:02:48 +080027
Logandf23afa2010-11-27 11:04:54 +080028#include "llvm/Analysis/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080029
Logan1f028c02010-11-27 01:02:48 +080030#include "llvm/Bitcode/ReaderWriter.h"
31
Logan1f028c02010-11-27 01:02:48 +080032#include "llvm/CodeGen/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080033#include "llvm/CodeGen/RegAllocRegistry.h"
34#include "llvm/CodeGen/SchedulerRegistry.h"
Logan1f028c02010-11-27 01:02:48 +080035
Logandf23afa2010-11-27 11:04:54 +080036#include "llvm/Transforms/IPO.h"
37#include "llvm/Transforms/Scalar.h"
38
39#include "llvm/Target/SubtargetFeature.h"
40#include "llvm/Target/TargetData.h"
41#include "llvm/Target/TargetMachine.h"
42#include "llvm/Target/TargetOptions.h"
43#include "llvm/Target/TargetRegistry.h"
44#include "llvm/Target/TargetSelect.h"
45
46#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/MemoryBuffer.h"
48
49#include "llvm/GlobalValue.h"
50#include "llvm/Linker.h"
51#include "llvm/LLVMContext.h"
52#include "llvm/Metadata.h"
53#include "llvm/Module.h"
54#include "llvm/PassManager.h"
55#include "llvm/Value.h"
56
57#include <errno.h>
58#include <sys/file.h>
Logandf23afa2010-11-27 11:04:54 +080059#include <sys/stat.h>
60#include <sys/types.h>
61#include <unistd.h>
62
Logan75cc8a52011-01-07 06:06:52 +080063#include <string.h>
Logan8b77a772010-12-21 09:11:01 +080064
Logandf23afa2010-11-27 11:04:54 +080065#include <string>
66#include <vector>
Logan1f028c02010-11-27 01:02:48 +080067
Logan1f028c02010-11-27 01:02:48 +080068namespace bcc {
69
70//////////////////////////////////////////////////////////////////////////////
71// BCC Compiler Static Variables
72//////////////////////////////////////////////////////////////////////////////
73
74bool Compiler::GlobalInitialized = false;
75
Logan1f028c02010-11-27 01:02:48 +080076// Code generation optimization level for the compiler
77llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel;
78
79std::string Compiler::Triple;
80
81std::string Compiler::CPU;
82
83std::vector<std::string> Compiler::Features;
84
Stephen Hines071288a2011-01-27 14:38:26 -080085// Name of metadata node where pragma info resides (should be synced with
Logan1f028c02010-11-27 01:02:48 +080086// slang.cpp)
87const llvm::StringRef Compiler::PragmaMetadataName = "#pragma";
88
Stephen Hines071288a2011-01-27 14:38:26 -080089// Name of metadata node where exported variable names reside (should be
Logan1f028c02010-11-27 01:02:48 +080090// synced with slang_rs_metadata.h)
91const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var";
92
Stephen Hines071288a2011-01-27 14:38:26 -080093// Name of metadata node where exported function names reside (should be
Logan1f028c02010-11-27 01:02:48 +080094// synced with slang_rs_metadata.h)
95const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func";
96
Stephen Hines071288a2011-01-27 14:38:26 -080097// Name of metadata node where RS object slot info resides (should be
98// synced with slang_rs_metadata.h)
99const llvm::StringRef Compiler::ObjectSlotMetadataName = "#rs_object_slots";
Logan1f028c02010-11-27 01:02:48 +0800100
101//////////////////////////////////////////////////////////////////////////////
102// Compiler
103//////////////////////////////////////////////////////////////////////////////
104
105void Compiler::GlobalInitialization() {
106 if (GlobalInitialized)
107 return;
108
Logane1323992011-01-12 04:47:13 +0800109 LOGI("LIBBCC BUILD: %s\n", libbcc_build_time);
Logan87066272010-12-29 00:34:32 +0800110
Logan1f028c02010-11-27 01:02:48 +0800111 // if (!llvm::llvm_is_multithreaded())
112 // llvm::llvm_start_multithreaded();
113
114 // Set Triple, CPU and Features here
115 Triple = TARGET_TRIPLE_STRING;
116
Logan1f028c02010-11-27 01:02:48 +0800117 Features.push_back("+vfp3");
118 Features.push_back("+d16");
119
Logan4fe966f2011-02-27 08:26:40 +0800120 // NOTE: Currently, we have to turn off the support for NEON explicitly.
121 // Since the ARMCodeEmitter.cpp is not ready for JITing NEON
122 // instructions.
123 Features.push_back("-neon"); // TODO(sliao): NEON for JIT
124 Features.push_back("-neonfp");
125 Features.push_back("-vmlx");
126
Logan1f028c02010-11-27 01:02:48 +0800127#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
128 LLVMInitializeARMTargetInfo();
129 LLVMInitializeARMTarget();
Logan35849002011-01-15 07:30:43 +0800130#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800131 LLVMInitializeARMDisassembler();
132 LLVMInitializeARMAsmPrinter();
133#endif
134#endif
135
136#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
137 LLVMInitializeX86TargetInfo();
138 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800139#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800140 LLVMInitializeX86Disassembler();
141 LLVMInitializeX86AsmPrinter();
142#endif
143#endif
144
145#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
146 LLVMInitializeX86TargetInfo();
147 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800148#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800149 LLVMInitializeX86Disassembler();
150 LLVMInitializeX86AsmPrinter();
151#endif
152#endif
153
154 // -O0: llvm::CodeGenOpt::None
155 // -O1: llvm::CodeGenOpt::Less
156 // -O2: llvm::CodeGenOpt::Default
157 // -O3: llvm::CodeGenOpt::Aggressive
Shih-wei Liao72f67a62010-12-14 13:36:15 -0800158 CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
Logan1f028c02010-11-27 01:02:48 +0800159
160 // Below are the global settings to LLVM
161
162 // Disable frame pointer elimination optimization
163 llvm::NoFramePointerElim = false;
164
165 // Use hardfloat ABI
166 //
167 // TODO(all): Need to detect the CPU capability and decide whether to use
168 // softfp. To use softfp, change following 2 lines to
169 //
170 // llvm::FloatABIType = llvm::FloatABI::Soft;
171 // llvm::UseSoftFloat = true;
172 //
Shih-wei Liaoe728cb82010-12-15 15:20:47 -0800173 llvm::FloatABIType = llvm::FloatABI::Soft;
Logan1f028c02010-11-27 01:02:48 +0800174 llvm::UseSoftFloat = false;
175
176 // BCC needs all unknown symbols resolved at JIT/compilation time.
177 // So we don't need any dynamic relocation model.
178 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static);
179
180#if defined(DEFAULT_X64_CODEGEN)
181 // Data address in X86_64 architecture may reside in a far-away place
182 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium);
183#else
184 // This is set for the linker (specify how large of the virtual addresses
185 // we can access for all unknown symbols.)
186 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small);
187#endif
188
189 // Register the scheduler
190 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
191
192 // Register allocation policy:
193 // createFastRegisterAllocator: fast but bad quality
194 // createLinearScanRegisterAllocator: not so fast but good quality
195 llvm::RegisterRegAlloc::setDefault
196 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
197 llvm::createFastRegisterAllocator :
198 llvm::createLinearScanRegisterAllocator);
199
Logan35849002011-01-15 07:30:43 +0800200#if USE_CACHE
Logan75cc8a52011-01-07 06:06:52 +0800201 // Calculate the SHA1 checksum of libbcc and libRS.
Logan35849002011-01-15 07:30:43 +0800202#if USE_LIBBCC_SHA1SUM
Logan75cc8a52011-01-07 06:06:52 +0800203 calcFileSHA1(sha1LibBCC, pathLibBCC);
Logane1323992011-01-12 04:47:13 +0800204#endif
Logan75cc8a52011-01-07 06:06:52 +0800205 calcFileSHA1(sha1LibRS, pathLibRS);
Logan35849002011-01-15 07:30:43 +0800206#endif
Logan75cc8a52011-01-07 06:06:52 +0800207
Logan1f028c02010-11-27 01:02:48 +0800208 GlobalInitialized = true;
209}
210
211
212void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
213 std::string *Error = static_cast<std::string*>(UserData);
214 Error->assign(Message);
215 LOGE("%s", Message.c_str());
216 exit(1);
217}
218
219
220CodeMemoryManager *Compiler::createCodeMemoryManager() {
221 mCodeMemMgr.reset(new CodeMemoryManager());
222 return mCodeMemMgr.get();
223}
224
225
226CodeEmitter *Compiler::createCodeEmitter() {
Logan7dcaac92011-01-06 04:26:23 +0800227 mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get()));
Logan1f028c02010-11-27 01:02:48 +0800228 return mCodeEmitter.get();
229}
230
231
Logan2a6dc822011-01-06 04:05:20 +0800232Compiler::Compiler(ScriptCompiled *result)
233 : mpResult(result),
Logan1f028c02010-11-27 01:02:48 +0800234 mpSymbolLookupFn(NULL),
235 mpSymbolLookupContext(NULL),
236 mContext(NULL),
237 mModule(NULL),
238 mHasLinked(false) /* Turn off linker */ {
239 llvm::remove_fatal_error_handler();
240 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
241 mContext = new llvm::LLVMContext();
242 return;
243}
244
Logan1f028c02010-11-27 01:02:48 +0800245
Logan474cbd22011-01-31 01:47:44 +0800246llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) {
247 llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError);
Logan1f028c02010-11-27 01:02:48 +0800248
Logan474cbd22011-01-31 01:47:44 +0800249 if (!result) {
250 LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str());
251 return NULL;
Logan1f028c02010-11-27 01:02:48 +0800252 }
253
Logan474cbd22011-01-31 01:47:44 +0800254 return result;
Logan1f028c02010-11-27 01:02:48 +0800255}
256
257
Logan474cbd22011-01-31 01:47:44 +0800258int Compiler::linkModule(llvm::Module *moduleWith) {
259 if (llvm::Linker::LinkModules(mModule, moduleWith, &mError) != 0) {
Logan1f028c02010-11-27 01:02:48 +0800260 return hasError();
261 }
262
Logan1f028c02010-11-27 01:02:48 +0800263 // Everything for linking should be settled down here with no error occurs
264 mHasLinked = true;
265 return hasError();
266}
267
268
Logan1f028c02010-11-27 01:02:48 +0800269int Compiler::compile() {
270 llvm::TargetData *TD = NULL;
271
272 llvm::TargetMachine *TM = NULL;
273 const llvm::Target *Target;
274 std::string FeaturesStr;
275
276 llvm::FunctionPassManager *CodeGenPasses = NULL;
277
278 const llvm::NamedMDNode *PragmaMetadata;
279 const llvm::NamedMDNode *ExportVarMetadata;
280 const llvm::NamedMDNode *ExportFuncMetadata;
Stephen Hines071288a2011-01-27 14:38:26 -0800281 const llvm::NamedMDNode *ObjectSlotMetadata;
Logan1f028c02010-11-27 01:02:48 +0800282
283 if (mModule == NULL) // No module was loaded
284 return 0;
285
286 // Create TargetMachine
287 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
288 if (hasError())
289 goto on_bcc_compile_error;
290
291 if (!CPU.empty() || !Features.empty()) {
292 llvm::SubtargetFeatures F;
293 F.setCPU(CPU);
Logana4994f52010-11-27 14:06:02 +0800294
295 for (std::vector<std::string>::const_iterator
296 I = Features.begin(), E = Features.end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800297 F.AddFeature(*I);
Logana4994f52010-11-27 14:06:02 +0800298 }
299
Logan1f028c02010-11-27 01:02:48 +0800300 FeaturesStr = F.getString();
301 }
302
303 TM = Target->createTargetMachine(Triple, FeaturesStr);
304 if (TM == NULL) {
305 setError("Failed to create target machine implementation for the"
306 " specified triple '" + Triple + "'");
307 goto on_bcc_compile_error;
308 }
309
310 // Create memory manager for creation of code emitter later.
311 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) {
312 setError("Failed to startup memory management for further compilation");
313 goto on_bcc_compile_error;
314 }
Logan02286cb2011-01-07 00:30:47 +0800315
316 mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase());
Logan1f028c02010-11-27 01:02:48 +0800317
318 // Create code emitter
319 if (!mCodeEmitter.get()) {
320 if (!createCodeEmitter()) {
321 setError("Failed to create machine code emitter to complete"
322 " the compilation");
323 goto on_bcc_compile_error;
324 }
325 } else {
326 // Reuse the code emitter
327 mCodeEmitter->reset();
328 }
329
330 mCodeEmitter->setTargetMachine(*TM);
331 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn,
332 mpSymbolLookupContext);
333
334 // Get target data from Module
335 TD = new llvm::TargetData(mModule);
336
337 // Load named metadata
338 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName);
339 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName);
340 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName);
Stephen Hines071288a2011-01-27 14:38:26 -0800341 ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName);
Logan1f028c02010-11-27 01:02:48 +0800342
Shih-wei Liao644fcf22011-01-16 21:56:26 -0800343#if 0
344 mHasLinked = false;
345#endif
346
Logan1f028c02010-11-27 01:02:48 +0800347 // Create LTO passes and run them on the mModule
348 if (mHasLinked) {
349 llvm::TimePassesIsEnabled = true; // TODO(all)
350 llvm::PassManager LTOPasses;
351 LTOPasses.add(new llvm::TargetData(*TD));
352
353 std::vector<const char*> ExportSymbols;
354
355 // A workaround for getting export variable and function name. Will refine
356 // it soon.
357 if (ExportVarMetadata) {
358 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
359 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
360 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
361 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
362 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
363 llvm::StringRef ExportVarName =
364 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
365 ExportSymbols.push_back(ExportVarName.data());
366 }
367 }
368 }
369 }
370
371 if (ExportFuncMetadata) {
372 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
373 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
374 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
375 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
376 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
377 llvm::StringRef ExportFuncName =
378 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
379 ExportSymbols.push_back(ExportFuncName.data());
380 }
381 }
382 }
383 }
384 // root() and init() are born to be exported
385 ExportSymbols.push_back("root");
386 ExportSymbols.push_back("init");
387
388 // We now create passes list performing LTO. These are copied from
389 // (including comments) llvm::createStandardLTOPasses().
390
391 // Internalize all other symbols not listed in ExportSymbols
392 LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
393
394 // Propagate constants at call sites into the functions they call. This
395 // opens opportunities for globalopt (and inlining) by substituting
396 // function pointers passed as arguments to direct uses of functions.
397 LTOPasses.add(llvm::createIPSCCPPass());
398
399 // Now that we internalized some globals, see if we can hack on them!
400 LTOPasses.add(llvm::createGlobalOptimizerPass());
401
402 // Linking modules together can lead to duplicated global constants, only
403 // keep one copy of each constant...
404 LTOPasses.add(llvm::createConstantMergePass());
405
406 // Remove unused arguments from functions...
407 LTOPasses.add(llvm::createDeadArgEliminationPass());
408
409 // Reduce the code after globalopt and ipsccp. Both can open up
410 // significant simplification opportunities, and both can propagate
411 // functions through function pointers. When this happens, we often have
412 // to resolve varargs calls, etc, so let instcombine do this.
413 LTOPasses.add(llvm::createInstructionCombiningPass());
414
415 // Inline small functions
416 LTOPasses.add(llvm::createFunctionInliningPass());
417
418 // Remove dead EH info.
419 LTOPasses.add(llvm::createPruneEHPass());
420
421 // Internalize the globals again after inlining
422 LTOPasses.add(llvm::createGlobalOptimizerPass());
423
424 // Remove dead functions.
425 LTOPasses.add(llvm::createGlobalDCEPass());
426
427 // If we didn't decide to inline a function, check to see if we can
428 // transform it to pass arguments by value instead of by reference.
429 LTOPasses.add(llvm::createArgumentPromotionPass());
430
431 // The IPO passes may leave cruft around. Clean up after them.
432 LTOPasses.add(llvm::createInstructionCombiningPass());
433 LTOPasses.add(llvm::createJumpThreadingPass());
434
435 // Break up allocas
436 LTOPasses.add(llvm::createScalarReplAggregatesPass());
437
438 // Run a few AA driven optimizations here and now, to cleanup the code.
439 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture.
440 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
441
442 // Hoist loop invariants.
443 LTOPasses.add(llvm::createLICMPass());
444
445 // Remove redundancies.
446 LTOPasses.add(llvm::createGVNPass());
447
448 // Remove dead memcpys.
449 LTOPasses.add(llvm::createMemCpyOptPass());
450
451 // Nuke dead stores.
452 LTOPasses.add(llvm::createDeadStoreEliminationPass());
453
454 // Cleanup and simplify the code after the scalar optimizations.
455 LTOPasses.add(llvm::createInstructionCombiningPass());
456
457 LTOPasses.add(llvm::createJumpThreadingPass());
458
459 // Delete basic blocks, which optimization passes may have killed.
460 LTOPasses.add(llvm::createCFGSimplificationPass());
461
462 // Now that we have optimized the program, discard unreachable functions.
463 LTOPasses.add(llvm::createGlobalDCEPass());
464
465 LTOPasses.run(*mModule);
466 }
467
468 // Create code-gen pass to run the code emitter
469 CodeGenPasses = new llvm::FunctionPassManager(mModule);
470 CodeGenPasses->add(TD); // Will take the ownership of TD
471
472 if (TM->addPassesToEmitMachineCode(*CodeGenPasses,
473 *mCodeEmitter,
474 CodeGenOptLevel)) {
475 setError("The machine code emission is not supported by BCC on target '"
476 + Triple + "'");
477 goto on_bcc_compile_error;
478 }
479
480 // Run the pass (the code emitter) on every non-declaration function in the
481 // module
482 CodeGenPasses->doInitialization();
483 for (llvm::Module::iterator I = mModule->begin(), E = mModule->end();
484 I != E; I++) {
485 if (!I->isDeclaration()) {
486 CodeGenPasses->run(*I);
487 }
488 }
489
490 CodeGenPasses->doFinalization();
491
492 // Copy the global address mapping from code emitter and remapping
493 if (ExportVarMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800494 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
495
Logan1f028c02010-11-27 01:02:48 +0800496 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
497 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
498 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
499 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
500 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
501 llvm::StringRef ExportVarName =
502 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
503
504 CodeEmitter::global_addresses_const_iterator I, E;
505 for (I = mCodeEmitter->global_address_begin(),
506 E = mCodeEmitter->global_address_end();
507 I != E; I++) {
508 if (I->first->getValueID() != llvm::Value::GlobalVariableVal)
509 continue;
510 if (ExportVarName == I->first->getName()) {
Logan2a6dc822011-01-06 04:05:20 +0800511 varList.push_back(I->second);
Logan1f028c02010-11-27 01:02:48 +0800512 break;
513 }
514 }
515 if (I != mCodeEmitter->global_address_end())
516 continue; // found
517 }
518 }
519 // if reaching here, we know the global variable record in metadata is
520 // not found. So we make an empty slot
Logan2a6dc822011-01-06 04:05:20 +0800521 varList.push_back(NULL);
Logan1f028c02010-11-27 01:02:48 +0800522 }
Logan2a6dc822011-01-06 04:05:20 +0800523
524 assert((varList.size() == ExportVarMetadata->getNumOperands()) &&
Logan1f028c02010-11-27 01:02:48 +0800525 "Number of slots doesn't match the number of export variables!");
526 }
527
528 if (ExportFuncMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800529 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
530
Logan1f028c02010-11-27 01:02:48 +0800531 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
532 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
533 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
534 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
535 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
536 llvm::StringRef ExportFuncName =
537 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
Logan7dcaac92011-01-06 04:26:23 +0800538 funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str()));
Logan1f028c02010-11-27 01:02:48 +0800539 }
540 }
541 }
542 }
543
544 // Tell code emitter now can release the memory using during the JIT since
545 // we have done the code emission
546 mCodeEmitter->releaseUnnecessary();
547
548 // Finally, read pragma information from the metadata node of the @Module if
549 // any.
Logan2a6dc822011-01-06 04:05:20 +0800550 if (PragmaMetadata) {
551 ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas;
552
Logan1f028c02010-11-27 01:02:48 +0800553 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) {
554 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
555 if (Pragma != NULL &&
556 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) {
557 llvm::Value *PragmaNameMDS = Pragma->getOperand(0);
558 llvm::Value *PragmaValueMDS = Pragma->getOperand(1);
559
560 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) &&
561 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) {
562 llvm::StringRef PragmaName =
563 static_cast<llvm::MDString*>(PragmaNameMDS)->getString();
564 llvm::StringRef PragmaValue =
565 static_cast<llvm::MDString*>(PragmaValueMDS)->getString();
566
Logan2a6dc822011-01-06 04:05:20 +0800567 pragmaList.push_back(
Logan1f028c02010-11-27 01:02:48 +0800568 std::make_pair(std::string(PragmaName.data(),
569 PragmaName.size()),
570 std::string(PragmaValue.data(),
571 PragmaValue.size())));
572 }
573 }
574 }
Logan2a6dc822011-01-06 04:05:20 +0800575 }
Logan1f028c02010-11-27 01:02:48 +0800576
Stephen Hines071288a2011-01-27 14:38:26 -0800577 if (ObjectSlotMetadata) {
578 ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots;
579
580 for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) {
581 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i);
582 if (ObjectSlot != NULL &&
583 ObjectSlot->getNumOperands() == 1) {
584 llvm::Value *SlotMDS = ObjectSlot->getOperand(0);
585 if (SlotMDS->getValueID() == llvm::Value::MDStringVal) {
586 llvm::StringRef Slot =
587 static_cast<llvm::MDString*>(SlotMDS)->getString();
588 uint32_t USlot = 0;
589 if (Slot.getAsInteger(10, USlot)) {
590 setError("Non-integer object slot value '" + Slot.str() + "'");
591 goto on_bcc_compile_error;
592 }
593 objectSlotList.push_back(USlot);
594 }
595 }
596 }
597 }
598
Logan1f028c02010-11-27 01:02:48 +0800599on_bcc_compile_error:
600 // LOGE("on_bcc_compiler_error");
601 if (CodeGenPasses) {
602 delete CodeGenPasses;
603 } else if (TD) {
604 delete TD;
605 }
606 if (TM)
607 delete TM;
608
609 if (mError.empty()) {
Logan65719812011-01-07 11:17:14 +0800610 return 0;
Logan1f028c02010-11-27 01:02:48 +0800611 }
612
613 // LOGE(getErrorMessage());
Logan65719812011-01-07 11:17:14 +0800614 return 1;
Logan1f028c02010-11-27 01:02:48 +0800615}
616
617
Logan1f028c02010-11-27 01:02:48 +0800618Compiler::~Compiler() {
Logan1f028c02010-11-27 01:02:48 +0800619 delete mModule;
Logan1f028c02010-11-27 01:02:48 +0800620 delete mContext;
Logana4994f52010-11-27 14:06:02 +0800621
622 // llvm::llvm_shutdown();
Logan1f028c02010-11-27 01:02:48 +0800623}
624
Logan1f028c02010-11-27 01:02:48 +0800625} // namespace bcc