blob: c7dfa292da0fdfd26da522a1470a8181d79f115f [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
17#define LOG_TAG "bcc"
18#include <cutils/log.h>
19
Loganc4395232010-11-27 18:54:17 +080020#include "Compiler.h"
Logan1f028c02010-11-27 01:02:48 +080021
Logan35849002011-01-15 07:30:43 +080022#include "Config.h"
23
Loganeb3d12b2010-12-16 06:20:18 +080024#include "ContextManager.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
Logandf23afa2010-11-27 11:04:54 +080028#include "llvm/ADT/StringRef.h"
Logan1f028c02010-11-27 01:02:48 +080029
Logandf23afa2010-11-27 11:04:54 +080030#include "llvm/Analysis/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080031
Logan1f028c02010-11-27 01:02:48 +080032#include "llvm/Bitcode/ReaderWriter.h"
33
Logan1f028c02010-11-27 01:02:48 +080034#include "llvm/CodeGen/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080035#include "llvm/CodeGen/RegAllocRegistry.h"
36#include "llvm/CodeGen/SchedulerRegistry.h"
Logan1f028c02010-11-27 01:02:48 +080037
Logandf23afa2010-11-27 11:04:54 +080038#include "llvm/Transforms/IPO.h"
39#include "llvm/Transforms/Scalar.h"
40
41#include "llvm/Target/SubtargetFeature.h"
42#include "llvm/Target/TargetData.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/Target/TargetOptions.h"
45#include "llvm/Target/TargetRegistry.h"
46#include "llvm/Target/TargetSelect.h"
47
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MemoryBuffer.h"
50
51#include "llvm/GlobalValue.h"
52#include "llvm/Linker.h"
53#include "llvm/LLVMContext.h"
54#include "llvm/Metadata.h"
55#include "llvm/Module.h"
56#include "llvm/PassManager.h"
57#include "llvm/Value.h"
58
59#include <errno.h>
60#include <sys/file.h>
Logandf23afa2010-11-27 11:04:54 +080061#include <sys/stat.h>
62#include <sys/types.h>
63#include <unistd.h>
64
Logan75cc8a52011-01-07 06:06:52 +080065#include <string.h>
Logan8b77a772010-12-21 09:11:01 +080066
Logandf23afa2010-11-27 11:04:54 +080067#include <string>
68#include <vector>
Logan1f028c02010-11-27 01:02:48 +080069
Logan1f028c02010-11-27 01:02:48 +080070namespace bcc {
71
72//////////////////////////////////////////////////////////////////////////////
73// BCC Compiler Static Variables
74//////////////////////////////////////////////////////////////////////////////
75
76bool Compiler::GlobalInitialized = false;
77
Logan1f028c02010-11-27 01:02:48 +080078// Code generation optimization level for the compiler
79llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel;
80
81std::string Compiler::Triple;
82
83std::string Compiler::CPU;
84
85std::vector<std::string> Compiler::Features;
86
Stephen Hines071288a2011-01-27 14:38:26 -080087// Name of metadata node where pragma info resides (should be synced with
Logan1f028c02010-11-27 01:02:48 +080088// slang.cpp)
89const llvm::StringRef Compiler::PragmaMetadataName = "#pragma";
90
Stephen Hines071288a2011-01-27 14:38:26 -080091// Name of metadata node where exported variable names reside (should be
Logan1f028c02010-11-27 01:02:48 +080092// synced with slang_rs_metadata.h)
93const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var";
94
Stephen Hines071288a2011-01-27 14:38:26 -080095// Name of metadata node where exported function names reside (should be
Logan1f028c02010-11-27 01:02:48 +080096// synced with slang_rs_metadata.h)
97const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func";
98
Stephen Hines071288a2011-01-27 14:38:26 -080099// Name of metadata node where RS object slot info resides (should be
100// synced with slang_rs_metadata.h)
101const llvm::StringRef Compiler::ObjectSlotMetadataName = "#rs_object_slots";
Logan1f028c02010-11-27 01:02:48 +0800102
103//////////////////////////////////////////////////////////////////////////////
104// Compiler
105//////////////////////////////////////////////////////////////////////////////
106
107void Compiler::GlobalInitialization() {
108 if (GlobalInitialized)
109 return;
110
Logane1323992011-01-12 04:47:13 +0800111 LOGI("LIBBCC BUILD: %s\n", libbcc_build_time);
Logan87066272010-12-29 00:34:32 +0800112
Logan1f028c02010-11-27 01:02:48 +0800113 // if (!llvm::llvm_is_multithreaded())
114 // llvm::llvm_start_multithreaded();
115
116 // Set Triple, CPU and Features here
117 Triple = TARGET_TRIPLE_STRING;
118
Logan1f028c02010-11-27 01:02:48 +0800119 Features.push_back("+vfp3");
120 Features.push_back("+d16");
121
Logan4fe966f2011-02-27 08:26:40 +0800122 // NOTE: Currently, we have to turn off the support for NEON explicitly.
123 // Since the ARMCodeEmitter.cpp is not ready for JITing NEON
124 // instructions.
125 Features.push_back("-neon"); // TODO(sliao): NEON for JIT
126 Features.push_back("-neonfp");
127 Features.push_back("-vmlx");
128
Logan1f028c02010-11-27 01:02:48 +0800129#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
130 LLVMInitializeARMTargetInfo();
131 LLVMInitializeARMTarget();
Logan35849002011-01-15 07:30:43 +0800132#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800133 LLVMInitializeARMDisassembler();
134 LLVMInitializeARMAsmPrinter();
135#endif
136#endif
137
138#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
139 LLVMInitializeX86TargetInfo();
140 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800141#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800142 LLVMInitializeX86Disassembler();
143 LLVMInitializeX86AsmPrinter();
144#endif
145#endif
146
147#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
148 LLVMInitializeX86TargetInfo();
149 LLVMInitializeX86Target();
Logan35849002011-01-15 07:30:43 +0800150#if USE_DISASSEMBLER
Logan1f028c02010-11-27 01:02:48 +0800151 LLVMInitializeX86Disassembler();
152 LLVMInitializeX86AsmPrinter();
153#endif
154#endif
155
156 // -O0: llvm::CodeGenOpt::None
157 // -O1: llvm::CodeGenOpt::Less
158 // -O2: llvm::CodeGenOpt::Default
159 // -O3: llvm::CodeGenOpt::Aggressive
Shih-wei Liao72f67a62010-12-14 13:36:15 -0800160 CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
Logan1f028c02010-11-27 01:02:48 +0800161
162 // Below are the global settings to LLVM
163
164 // Disable frame pointer elimination optimization
165 llvm::NoFramePointerElim = false;
166
167 // Use hardfloat ABI
168 //
169 // TODO(all): Need to detect the CPU capability and decide whether to use
170 // softfp. To use softfp, change following 2 lines to
171 //
172 // llvm::FloatABIType = llvm::FloatABI::Soft;
173 // llvm::UseSoftFloat = true;
174 //
Shih-wei Liaoe728cb82010-12-15 15:20:47 -0800175 llvm::FloatABIType = llvm::FloatABI::Soft;
Logan1f028c02010-11-27 01:02:48 +0800176 llvm::UseSoftFloat = false;
177
178 // BCC needs all unknown symbols resolved at JIT/compilation time.
179 // So we don't need any dynamic relocation model.
180 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static);
181
182#if defined(DEFAULT_X64_CODEGEN)
183 // Data address in X86_64 architecture may reside in a far-away place
184 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium);
185#else
186 // This is set for the linker (specify how large of the virtual addresses
187 // we can access for all unknown symbols.)
188 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small);
189#endif
190
191 // Register the scheduler
192 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
193
194 // Register allocation policy:
195 // createFastRegisterAllocator: fast but bad quality
196 // createLinearScanRegisterAllocator: not so fast but good quality
197 llvm::RegisterRegAlloc::setDefault
198 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
199 llvm::createFastRegisterAllocator :
200 llvm::createLinearScanRegisterAllocator);
201
Logan35849002011-01-15 07:30:43 +0800202#if USE_CACHE
Logan75cc8a52011-01-07 06:06:52 +0800203 // Calculate the SHA1 checksum of libbcc and libRS.
Logan35849002011-01-15 07:30:43 +0800204#if USE_LIBBCC_SHA1SUM
Logan75cc8a52011-01-07 06:06:52 +0800205 calcFileSHA1(sha1LibBCC, pathLibBCC);
Logane1323992011-01-12 04:47:13 +0800206#endif
Logan75cc8a52011-01-07 06:06:52 +0800207 calcFileSHA1(sha1LibRS, pathLibRS);
Logan35849002011-01-15 07:30:43 +0800208#endif
Logan75cc8a52011-01-07 06:06:52 +0800209
Logan1f028c02010-11-27 01:02:48 +0800210 GlobalInitialized = true;
211}
212
213
214void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
215 std::string *Error = static_cast<std::string*>(UserData);
216 Error->assign(Message);
217 LOGE("%s", Message.c_str());
218 exit(1);
219}
220
221
222CodeMemoryManager *Compiler::createCodeMemoryManager() {
223 mCodeMemMgr.reset(new CodeMemoryManager());
224 return mCodeMemMgr.get();
225}
226
227
228CodeEmitter *Compiler::createCodeEmitter() {
Logan7dcaac92011-01-06 04:26:23 +0800229 mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get()));
Logan1f028c02010-11-27 01:02:48 +0800230 return mCodeEmitter.get();
231}
232
233
Logan2a6dc822011-01-06 04:05:20 +0800234Compiler::Compiler(ScriptCompiled *result)
235 : mpResult(result),
Logan1f028c02010-11-27 01:02:48 +0800236 mpSymbolLookupFn(NULL),
237 mpSymbolLookupContext(NULL),
238 mContext(NULL),
239 mModule(NULL),
240 mHasLinked(false) /* Turn off linker */ {
241 llvm::remove_fatal_error_handler();
242 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
243 mContext = new llvm::LLVMContext();
244 return;
245}
246
Logan1f028c02010-11-27 01:02:48 +0800247
Logan474cbd22011-01-31 01:47:44 +0800248llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) {
249 llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError);
Logan1f028c02010-11-27 01:02:48 +0800250
Logan474cbd22011-01-31 01:47:44 +0800251 if (!result) {
252 LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str());
253 return NULL;
Logan1f028c02010-11-27 01:02:48 +0800254 }
255
Logan474cbd22011-01-31 01:47:44 +0800256 return result;
Logan1f028c02010-11-27 01:02:48 +0800257}
258
259
Logan474cbd22011-01-31 01:47:44 +0800260int Compiler::linkModule(llvm::Module *moduleWith) {
261 if (llvm::Linker::LinkModules(mModule, moduleWith, &mError) != 0) {
Logan1f028c02010-11-27 01:02:48 +0800262 return hasError();
263 }
264
Logan1f028c02010-11-27 01:02:48 +0800265 // Everything for linking should be settled down here with no error occurs
266 mHasLinked = true;
267 return hasError();
268}
269
270
Logan1f028c02010-11-27 01:02:48 +0800271int Compiler::compile() {
272 llvm::TargetData *TD = NULL;
273
274 llvm::TargetMachine *TM = NULL;
275 const llvm::Target *Target;
276 std::string FeaturesStr;
277
278 llvm::FunctionPassManager *CodeGenPasses = NULL;
279
280 const llvm::NamedMDNode *PragmaMetadata;
281 const llvm::NamedMDNode *ExportVarMetadata;
282 const llvm::NamedMDNode *ExportFuncMetadata;
Stephen Hines071288a2011-01-27 14:38:26 -0800283 const llvm::NamedMDNode *ObjectSlotMetadata;
Logan1f028c02010-11-27 01:02:48 +0800284
285 if (mModule == NULL) // No module was loaded
286 return 0;
287
288 // Create TargetMachine
289 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
290 if (hasError())
291 goto on_bcc_compile_error;
292
293 if (!CPU.empty() || !Features.empty()) {
294 llvm::SubtargetFeatures F;
295 F.setCPU(CPU);
Logana4994f52010-11-27 14:06:02 +0800296
297 for (std::vector<std::string>::const_iterator
298 I = Features.begin(), E = Features.end(); I != E; I++) {
Logan1f028c02010-11-27 01:02:48 +0800299 F.AddFeature(*I);
Logana4994f52010-11-27 14:06:02 +0800300 }
301
Logan1f028c02010-11-27 01:02:48 +0800302 FeaturesStr = F.getString();
303 }
304
305 TM = Target->createTargetMachine(Triple, FeaturesStr);
306 if (TM == NULL) {
307 setError("Failed to create target machine implementation for the"
308 " specified triple '" + Triple + "'");
309 goto on_bcc_compile_error;
310 }
311
312 // Create memory manager for creation of code emitter later.
313 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) {
314 setError("Failed to startup memory management for further compilation");
315 goto on_bcc_compile_error;
316 }
Logan02286cb2011-01-07 00:30:47 +0800317
318 mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase());
Logan1f028c02010-11-27 01:02:48 +0800319
320 // Create code emitter
321 if (!mCodeEmitter.get()) {
322 if (!createCodeEmitter()) {
323 setError("Failed to create machine code emitter to complete"
324 " the compilation");
325 goto on_bcc_compile_error;
326 }
327 } else {
328 // Reuse the code emitter
329 mCodeEmitter->reset();
330 }
331
332 mCodeEmitter->setTargetMachine(*TM);
333 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn,
334 mpSymbolLookupContext);
335
336 // Get target data from Module
337 TD = new llvm::TargetData(mModule);
338
339 // Load named metadata
340 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName);
341 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName);
342 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName);
Stephen Hines071288a2011-01-27 14:38:26 -0800343 ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName);
Logan1f028c02010-11-27 01:02:48 +0800344
Shih-wei Liao644fcf22011-01-16 21:56:26 -0800345#if 0
346 mHasLinked = false;
347#endif
348
Logan1f028c02010-11-27 01:02:48 +0800349 // Create LTO passes and run them on the mModule
350 if (mHasLinked) {
351 llvm::TimePassesIsEnabled = true; // TODO(all)
352 llvm::PassManager LTOPasses;
353 LTOPasses.add(new llvm::TargetData(*TD));
354
355 std::vector<const char*> ExportSymbols;
356
357 // A workaround for getting export variable and function name. Will refine
358 // it soon.
359 if (ExportVarMetadata) {
360 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
361 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
362 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
363 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
364 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
365 llvm::StringRef ExportVarName =
366 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
367 ExportSymbols.push_back(ExportVarName.data());
368 }
369 }
370 }
371 }
372
373 if (ExportFuncMetadata) {
374 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
375 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
376 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
377 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
378 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
379 llvm::StringRef ExportFuncName =
380 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
381 ExportSymbols.push_back(ExportFuncName.data());
382 }
383 }
384 }
385 }
386 // root() and init() are born to be exported
387 ExportSymbols.push_back("root");
388 ExportSymbols.push_back("init");
389
390 // We now create passes list performing LTO. These are copied from
391 // (including comments) llvm::createStandardLTOPasses().
392
393 // Internalize all other symbols not listed in ExportSymbols
394 LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
395
396 // Propagate constants at call sites into the functions they call. This
397 // opens opportunities for globalopt (and inlining) by substituting
398 // function pointers passed as arguments to direct uses of functions.
399 LTOPasses.add(llvm::createIPSCCPPass());
400
401 // Now that we internalized some globals, see if we can hack on them!
402 LTOPasses.add(llvm::createGlobalOptimizerPass());
403
404 // Linking modules together can lead to duplicated global constants, only
405 // keep one copy of each constant...
406 LTOPasses.add(llvm::createConstantMergePass());
407
408 // Remove unused arguments from functions...
409 LTOPasses.add(llvm::createDeadArgEliminationPass());
410
411 // Reduce the code after globalopt and ipsccp. Both can open up
412 // significant simplification opportunities, and both can propagate
413 // functions through function pointers. When this happens, we often have
414 // to resolve varargs calls, etc, so let instcombine do this.
415 LTOPasses.add(llvm::createInstructionCombiningPass());
416
417 // Inline small functions
418 LTOPasses.add(llvm::createFunctionInliningPass());
419
420 // Remove dead EH info.
421 LTOPasses.add(llvm::createPruneEHPass());
422
423 // Internalize the globals again after inlining
424 LTOPasses.add(llvm::createGlobalOptimizerPass());
425
426 // Remove dead functions.
427 LTOPasses.add(llvm::createGlobalDCEPass());
428
429 // If we didn't decide to inline a function, check to see if we can
430 // transform it to pass arguments by value instead of by reference.
431 LTOPasses.add(llvm::createArgumentPromotionPass());
432
433 // The IPO passes may leave cruft around. Clean up after them.
434 LTOPasses.add(llvm::createInstructionCombiningPass());
435 LTOPasses.add(llvm::createJumpThreadingPass());
436
437 // Break up allocas
438 LTOPasses.add(llvm::createScalarReplAggregatesPass());
439
440 // Run a few AA driven optimizations here and now, to cleanup the code.
441 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture.
442 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
443
444 // Hoist loop invariants.
445 LTOPasses.add(llvm::createLICMPass());
446
447 // Remove redundancies.
448 LTOPasses.add(llvm::createGVNPass());
449
450 // Remove dead memcpys.
451 LTOPasses.add(llvm::createMemCpyOptPass());
452
453 // Nuke dead stores.
454 LTOPasses.add(llvm::createDeadStoreEliminationPass());
455
456 // Cleanup and simplify the code after the scalar optimizations.
457 LTOPasses.add(llvm::createInstructionCombiningPass());
458
459 LTOPasses.add(llvm::createJumpThreadingPass());
460
461 // Delete basic blocks, which optimization passes may have killed.
462 LTOPasses.add(llvm::createCFGSimplificationPass());
463
464 // Now that we have optimized the program, discard unreachable functions.
465 LTOPasses.add(llvm::createGlobalDCEPass());
466
467 LTOPasses.run(*mModule);
468 }
469
470 // Create code-gen pass to run the code emitter
471 CodeGenPasses = new llvm::FunctionPassManager(mModule);
472 CodeGenPasses->add(TD); // Will take the ownership of TD
473
474 if (TM->addPassesToEmitMachineCode(*CodeGenPasses,
475 *mCodeEmitter,
476 CodeGenOptLevel)) {
477 setError("The machine code emission is not supported by BCC on target '"
478 + Triple + "'");
479 goto on_bcc_compile_error;
480 }
481
482 // Run the pass (the code emitter) on every non-declaration function in the
483 // module
484 CodeGenPasses->doInitialization();
485 for (llvm::Module::iterator I = mModule->begin(), E = mModule->end();
486 I != E; I++) {
487 if (!I->isDeclaration()) {
488 CodeGenPasses->run(*I);
489 }
490 }
491
492 CodeGenPasses->doFinalization();
493
494 // Copy the global address mapping from code emitter and remapping
495 if (ExportVarMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800496 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
497
Logan1f028c02010-11-27 01:02:48 +0800498 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
499 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
500 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
501 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
502 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
503 llvm::StringRef ExportVarName =
504 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
505
506 CodeEmitter::global_addresses_const_iterator I, E;
507 for (I = mCodeEmitter->global_address_begin(),
508 E = mCodeEmitter->global_address_end();
509 I != E; I++) {
510 if (I->first->getValueID() != llvm::Value::GlobalVariableVal)
511 continue;
512 if (ExportVarName == I->first->getName()) {
Logan2a6dc822011-01-06 04:05:20 +0800513 varList.push_back(I->second);
Logan1f028c02010-11-27 01:02:48 +0800514 break;
515 }
516 }
517 if (I != mCodeEmitter->global_address_end())
518 continue; // found
519 }
520 }
521 // if reaching here, we know the global variable record in metadata is
522 // not found. So we make an empty slot
Logan2a6dc822011-01-06 04:05:20 +0800523 varList.push_back(NULL);
Logan1f028c02010-11-27 01:02:48 +0800524 }
Logan2a6dc822011-01-06 04:05:20 +0800525
526 assert((varList.size() == ExportVarMetadata->getNumOperands()) &&
Logan1f028c02010-11-27 01:02:48 +0800527 "Number of slots doesn't match the number of export variables!");
528 }
529
530 if (ExportFuncMetadata) {
Logan2a6dc822011-01-06 04:05:20 +0800531 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs;
532
Logan1f028c02010-11-27 01:02:48 +0800533 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
534 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
535 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
536 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
537 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
538 llvm::StringRef ExportFuncName =
539 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
Logan7dcaac92011-01-06 04:26:23 +0800540 funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str()));
Logan1f028c02010-11-27 01:02:48 +0800541 }
542 }
543 }
544 }
545
546 // Tell code emitter now can release the memory using during the JIT since
547 // we have done the code emission
548 mCodeEmitter->releaseUnnecessary();
549
550 // Finally, read pragma information from the metadata node of the @Module if
551 // any.
Logan2a6dc822011-01-06 04:05:20 +0800552 if (PragmaMetadata) {
553 ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas;
554
Logan1f028c02010-11-27 01:02:48 +0800555 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) {
556 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
557 if (Pragma != NULL &&
558 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) {
559 llvm::Value *PragmaNameMDS = Pragma->getOperand(0);
560 llvm::Value *PragmaValueMDS = Pragma->getOperand(1);
561
562 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) &&
563 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) {
564 llvm::StringRef PragmaName =
565 static_cast<llvm::MDString*>(PragmaNameMDS)->getString();
566 llvm::StringRef PragmaValue =
567 static_cast<llvm::MDString*>(PragmaValueMDS)->getString();
568
Logan2a6dc822011-01-06 04:05:20 +0800569 pragmaList.push_back(
Logan1f028c02010-11-27 01:02:48 +0800570 std::make_pair(std::string(PragmaName.data(),
571 PragmaName.size()),
572 std::string(PragmaValue.data(),
573 PragmaValue.size())));
574 }
575 }
576 }
Logan2a6dc822011-01-06 04:05:20 +0800577 }
Logan1f028c02010-11-27 01:02:48 +0800578
Stephen Hines071288a2011-01-27 14:38:26 -0800579 if (ObjectSlotMetadata) {
580 ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots;
581
582 for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) {
583 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i);
584 if (ObjectSlot != NULL &&
585 ObjectSlot->getNumOperands() == 1) {
586 llvm::Value *SlotMDS = ObjectSlot->getOperand(0);
587 if (SlotMDS->getValueID() == llvm::Value::MDStringVal) {
588 llvm::StringRef Slot =
589 static_cast<llvm::MDString*>(SlotMDS)->getString();
590 uint32_t USlot = 0;
591 if (Slot.getAsInteger(10, USlot)) {
592 setError("Non-integer object slot value '" + Slot.str() + "'");
593 goto on_bcc_compile_error;
594 }
595 objectSlotList.push_back(USlot);
596 }
597 }
598 }
599 }
600
Logan1f028c02010-11-27 01:02:48 +0800601on_bcc_compile_error:
602 // LOGE("on_bcc_compiler_error");
603 if (CodeGenPasses) {
604 delete CodeGenPasses;
605 } else if (TD) {
606 delete TD;
607 }
608 if (TM)
609 delete TM;
610
611 if (mError.empty()) {
Logan65719812011-01-07 11:17:14 +0800612 return 0;
Logan1f028c02010-11-27 01:02:48 +0800613 }
614
615 // LOGE(getErrorMessage());
Logan65719812011-01-07 11:17:14 +0800616 return 1;
Logan1f028c02010-11-27 01:02:48 +0800617}
618
619
Logan1f028c02010-11-27 01:02:48 +0800620Compiler::~Compiler() {
Logan1f028c02010-11-27 01:02:48 +0800621 delete mModule;
Logan1f028c02010-11-27 01:02:48 +0800622 delete mContext;
Logana4994f52010-11-27 14:06:02 +0800623
624 // llvm::llvm_shutdown();
Logan1f028c02010-11-27 01:02:48 +0800625}
626
Logan1f028c02010-11-27 01:02:48 +0800627} // namespace bcc