Revert "Make libbcc public."
This reverts commit 80232dd16c0affb2afae01cde6c94abf23ac1ba8.
diff --git a/lib/ExecutionEngine/Android.mk b/lib/ExecutionEngine/Android.mk
index d5a4573..f3b2c3d 100644
--- a/lib/ExecutionEngine/Android.mk
+++ b/lib/ExecutionEngine/Android.mk
@@ -22,14 +22,36 @@
#=====================================================================
libbcc_executionengine_SRC_FILES := \
- BCCRuntimeStub.c \
+ BCCContext.cpp \
+ BCCContextImpl.cpp \
BCCRuntimeSymbolResolver.cpp \
+ Compiler.cpp \
+ CompilerConfig.cpp \
ELFObjectLoaderImpl.cpp \
+ FileBase.cpp \
GDBJIT.cpp \
GDBJITRegistrar.cpp \
+ Initialization.cpp \
+ InputFile.cpp \
ObjectLoader.cpp \
+ OutputFile.cpp \
+ RSCompiler.cpp \
+ RSCompilerDriver.cpp \
+ RSExecutable.cpp \
+ RSForEachExpand.cpp \
+ RSInfo.cpp \
+ RSInfoExtractor.cpp \
+ RSInfoReader.cpp \
+ RSInfoWriter.cpp \
+ RSScript.cpp \
+ BCCRuntimeStub.c \
+ Script.cpp \
+ Sha1Helper.cpp \
+ Source.cpp \
SymbolResolverProxy.cpp \
- SymbolResolvers.cpp
+ SymbolResolvers.cpp \
+ TargetCompilerConfigs.cpp \
+ bcc.cpp
#=====================================================================
# Device Static Library: libbccExecutionEngine
@@ -42,11 +64,11 @@
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_C_INCLUDES := \
- $(LIBBCC_ROOT_PATH) \
$(RSLOADER_ROOT_PATH) \
$(RSLOADER_ROOT_PATH)/include
LOCAL_SRC_FILES := $(libbcc_executionengine_SRC_FILES)
+LOCAL_SHARED_LIBRARIES := libbcinfo
include $(LIBBCC_DEVICE_BUILD_MK)
include $(LIBBCC_GEN_CONFIG_MK)
@@ -65,11 +87,11 @@
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_C_INCLUDES := \
- $(LIBBCC_ROOT_PATH) \
$(RSLOADER_ROOT_PATH) \
$(RSLOADER_ROOT_PATH)/include
LOCAL_SRC_FILES := $(libbcc_executionengine_SRC_FILES)
+LOCAL_SHARED_LIBRARIES := libbcinfo
include $(LIBBCC_HOST_BUILD_MK)
include $(LIBBCC_GEN_CONFIG_MK)
diff --git a/lib/ExecutionEngine/BCCContext.cpp b/lib/ExecutionEngine/BCCContext.cpp
new file mode 100644
index 0000000..62995ed
--- /dev/null
+++ b/lib/ExecutionEngine/BCCContext.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BCCContext.h"
+
+#include <new>
+
+#include "BCCContextImpl.h"
+#include "DebugHelper.h"
+#include "Source.h"
+
+using namespace bcc;
+
+static BCCContext *GlobalContext = NULL;
+
+BCCContext *BCCContext::GetOrCreateGlobalContext() {
+ if (GlobalContext == NULL) {
+ GlobalContext = new (std::nothrow) BCCContext();
+ if (GlobalContext == NULL) {
+ ALOGE("Out of memory when allocate global BCCContext!");
+ }
+ }
+ return GlobalContext;
+}
+
+void BCCContext::DestroyGlobalContext() {
+ delete GlobalContext;
+ GlobalContext = NULL;
+}
+
+BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { }
+
+BCCContext::~BCCContext() {
+ delete mImpl;
+ if (this == GlobalContext) {
+ // We're deleting the context returned from GetOrCreateGlobalContext().
+ // Reset the GlobalContext.
+ GlobalContext = NULL;
+ }
+}
+
+void BCCContext::addSource(Source &pSource)
+{ mImpl->mOwnSources.insert(&pSource); }
+
+void BCCContext::removeSource(Source &pSource)
+{ mImpl->mOwnSources.erase(&pSource); }
+
+llvm::LLVMContext &BCCContext::getLLVMContext()
+{ return mImpl->mLLVMContext; }
+
+const llvm::LLVMContext &BCCContext::getLLVMContext() const
+{ return mImpl->mLLVMContext; }
diff --git a/lib/ExecutionEngine/BCCContext.h b/lib/ExecutionEngine/BCCContext.h
new file mode 100644
index 0000000..56962fe
--- /dev/null
+++ b/lib/ExecutionEngine/BCCContext.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_CONTEXT_H
+#define BCC_EXECUTION_ENGINE_CONTEXT_H
+
+namespace llvm {
+ class LLVMContext;
+}
+
+namespace bcc {
+
+class BCCContextImpl;
+class Source;
+
+/*
+ * class BCCContext manages the global data across the libbcc infrastructure.
+ */
+class BCCContext {
+public:
+ BCCContextImpl *const mImpl;
+
+ BCCContext();
+ ~BCCContext();
+
+ llvm::LLVMContext &getLLVMContext();
+ const llvm::LLVMContext &getLLVMContext() const;
+
+ void addSource(Source &pSource);
+ void removeSource(Source &pSource);
+
+ // Global BCCContext
+ static BCCContext *GetOrCreateGlobalContext();
+ static void DestroyGlobalContext();
+};
+
+} // namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_CONTEXT_H
diff --git a/lib/ExecutionEngine/BCCContextImpl.cpp b/lib/ExecutionEngine/BCCContextImpl.cpp
new file mode 100644
index 0000000..ec865fa
--- /dev/null
+++ b/lib/ExecutionEngine/BCCContextImpl.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BCCContextImpl.h"
+
+#include <vector>
+
+#include <llvm/ADT/STLExtras.h>
+
+#include "Source.h"
+
+using namespace bcc;
+
+BCCContextImpl::~BCCContextImpl() {
+ // Another temporary container is needed to store the Source objects that we
+ // are going to detroy. Since the destruction of Source object will call
+ // removeSource() and change the content of OwnSources.
+ std::vector<Source *> Sources(mOwnSources.begin(), mOwnSources.end());
+ llvm::DeleteContainerPointers(Sources);
+}
diff --git a/lib/ExecutionEngine/BCCContextImpl.h b/lib/ExecutionEngine/BCCContextImpl.h
new file mode 100644
index 0000000..446eb36
--- /dev/null
+++ b/lib/ExecutionEngine/BCCContextImpl.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_CONTEXT_IMPL_H
+#define BCC_EXECUTION_ENGINE_CONTEXT_IMPL_H
+
+#include <llvm/ADT/SmallPtrSet.h>
+#include <llvm/LLVMContext.h>
+
+namespace bcc {
+
+class BCCContext;
+class Source;
+
+/*
+ * class BCCContextImpl contains the implementation of BCCCotext.
+ */
+class BCCContextImpl {
+public:
+ llvm::LLVMContext mLLVMContext;
+
+ // The set of sources that initialized in this context. They will be destroyed
+ // automatically when this context is gone.
+ llvm::SmallPtrSet<Source *, 2> mOwnSources;
+
+ BCCContextImpl(BCCContext &pContext) { }
+ ~BCCContextImpl();
+};
+
+} // namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_CONTEXT_IMPL_H
diff --git a/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp b/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp
index ddc06fc..1059d85 100644
--- a/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp
+++ b/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h"
+#include "BCCRuntimeSymbolResolver.h"
// int_lib.h included by BCCRuntimeStub.h has the following line:
//
diff --git a/lib/ExecutionEngine/BCCRuntimeSymbolResolver.h b/lib/ExecutionEngine/BCCRuntimeSymbolResolver.h
new file mode 100644
index 0000000..e48a4d8
--- /dev/null
+++ b/lib/ExecutionEngine/BCCRuntimeSymbolResolver.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_BCC_RUNTIME_SYMBOL_RESOLVERS_H
+#define BCC_EXECUTION_ENGINE_BCC_RUNTIME_SYMBOL_RESOLVERS_H
+
+#include "SymbolResolvers.h"
+
+namespace bcc {
+
+class BCCRuntimeSymbolResolver :
+ public ArraySymbolResolver<BCCRuntimeSymbolResolver> {
+ typedef ArraySymbolResolver<BCCRuntimeSymbolResolver> super;
+public:
+ // SymbolArray and NumSymbols are required to be define in the
+ // subclass of ArraySymbolResolver.
+ static const SymbolMap SymbolArray[];
+ static const size_t NumSymbols;
+
+ BCCRuntimeSymbolResolver() : super(/* pSorted */true) { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_BCC_RUNTIME_SYMBOL_RESOLVERS_H
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
new file mode 100644
index 0000000..2348d56
--- /dev/null
+++ b/lib/ExecutionEngine/Compiler.cpp
@@ -0,0 +1,382 @@
+/*
+ * Copyright 2010-2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Compiler.h"
+
+#include <llvm/Analysis/Passes.h>
+#include <llvm/CodeGen/RegAllocRegistry.h>
+#include <llvm/Module.h>
+#include <llvm/PassManager.h>
+#include <llvm/Support/TargetRegistry.h>
+#include <llvm/Support/raw_ostream.h>
+#include <llvm/Target/TargetData.h>
+#include <llvm/Target/TargetMachine.h>
+#include <llvm/Transforms/IPO.h>
+#include <llvm/Transforms/Scalar.h>
+
+#include "CompilerConfig.h"
+#include "DebugHelper.h"
+#include "OutputFile.h"
+#include "Script.h"
+#include "Source.h"
+
+using namespace bcc;
+
+const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
+ static const char *ErrorString[] = {
+ /* kSuccess */
+ "Successfully compiled.",
+ /* kInvalidConfigNoTarget */
+ "Invalid compiler config supplied (getTarget() returns NULL.) "
+ "(missing call to CompilerConfig::initialize()?)",
+ /* kErrCreateTargetMachine */
+ "Failed to create llvm::TargetMachine.",
+ /* kErrSwitchTargetMachine */
+ "Failed to switch llvm::TargetMachine.",
+ /* kErrNoTargetMachine */
+ "Failed to compile the script since there's no available TargetMachine."
+ " (missing call to Compiler::config()?)",
+ /* kErrTargetDataNoMemory */
+ "Out of memory when create TargetData during compilation.",
+ /* kErrMaterialization */
+ "Failed to materialize the module.",
+ /* kErrInvalidOutputFileState */
+ "Supplied output file was invalid (in the error state.)",
+ /* kErrPrepareOutput */
+ "Failed to prepare file for output.",
+ /* kPrepareCodeGenPass */
+ "Failed to construct pass list for code-generation.",
+
+ /* kErrHookBeforeAddLTOPasses */
+ "Error occurred during beforeAddLTOPasses() in subclass.",
+ /* kErrHookAfterAddLTOPasses */
+ "Error occurred during afterAddLTOPasses() in subclass.",
+ /* kErrHookBeforeExecuteLTOPasses */
+ "Error occurred during beforeExecuteLTOPasses() in subclass.",
+ /* kErrHookAfterExecuteLTOPasses */
+ "Error occurred during afterExecuteLTOPasses() in subclass.",
+
+ /* kErrHookBeforeAddCodeGenPasses */
+ "Error occurred during beforeAddCodeGenPasses() in subclass.",
+ /* kErrHookAfterAddCodeGenPasses */
+ "Error occurred during afterAddCodeGenPasses() in subclass.",
+ /* kErrHookBeforeExecuteCodeGenPasses */
+ "Error occurred during beforeExecuteCodeGenPasses() in subclass.",
+ /* kErrHookAfterExecuteCodeGenPasses */
+ "Error occurred during afterExecuteCodeGenPasses() in subclass.",
+
+ /* kMaxErrorCode */
+ "(Unknown error code)"
+ };
+
+ if (pErrCode > kMaxErrorCode) {
+ pErrCode = kMaxErrorCode;
+ }
+
+ return ErrorString[ static_cast<size_t>(pErrCode) ];
+}
+
+//===----------------------------------------------------------------------===//
+// Instance Methods
+//===----------------------------------------------------------------------===//
+Compiler::Compiler() : mTarget(NULL), mEnableLTO(true) {
+ return;
+}
+
+Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(NULL),
+ mEnableLTO(true) {
+ const std::string &triple = pConfig.getTriple();
+
+ enum ErrorCode err = config(pConfig);
+ if (err != kSuccess) {
+ ALOGE("%s (%s, features: %s)", GetErrorString(err),
+ triple.c_str(), pConfig.getFeatureString().c_str());
+ return;
+ }
+
+ return;
+}
+
+enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) {
+ if (pConfig.getTarget() == NULL) {
+ return kInvalidConfigNoTarget;
+ }
+
+ llvm::TargetMachine *new_target =
+ (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(),
+ pConfig.getCPU(),
+ pConfig.getFeatureString(),
+ pConfig.getTargetOptions(),
+ pConfig.getRelocationModel(),
+ pConfig.getCodeModel(),
+ pConfig.getOptimizationLevel());
+
+ if (new_target == NULL) {
+ return ((mTarget != NULL) ? kErrSwitchTargetMachine :
+ kErrCreateTargetMachine);
+ }
+
+ // Replace the old TargetMachine.
+ delete mTarget;
+ mTarget = new_target;
+
+ // Adjust register allocation policy according to the optimization level.
+ // createFastRegisterAllocator: fast but bad quality
+ // createLinearScanRegisterAllocator: not so fast but good quality
+ if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) {
+ llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator);
+ } else {
+ llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator);
+ }
+
+ // Relax all machine instructions.
+ mTarget->setMCRelaxAll(true);
+
+ return kSuccess;
+}
+
+Compiler::~Compiler() {
+ delete mTarget;
+}
+
+enum Compiler::ErrorCode Compiler::runLTO(Script &pScript) {
+ llvm::TargetData *target_data = NULL;
+
+ // Pass manager for link-time optimization
+ llvm::PassManager lto_passes;
+
+ // Prepare TargetData target data from Module
+ target_data = new (std::nothrow) llvm::TargetData(*mTarget->getTargetData());
+ if (target_data == NULL) {
+ return kErrTargetDataNoMemory;
+ }
+
+ // Add TargetData to the pass manager.
+ lto_passes.add(target_data);
+
+ // Invokde "beforeAddLTOPasses" before adding the first pass.
+ if (!beforeAddLTOPasses(pScript, lto_passes)) {
+ return kErrHookBeforeAddLTOPasses;
+ }
+
+ // We now create passes list performing LTO. These are copied from
+ // (including comments) llvm::PassManagerBuilder::populateLTOPassManager().
+ // Only a subset of these LTO passes are enabled in optimization level 0 as
+ // they interfere with interactive debugging.
+ //
+ // FIXME: Figure out which passes (if any) makes sense for levels 1 and 2.
+ //if ( != llvm::CodeGenOpt::None) {
+ if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
+ lto_passes.add(llvm::createGlobalOptimizerPass());
+ lto_passes.add(llvm::createConstantMergePass());
+ } else {
+ // Propagate constants at call sites into the functions they call. This
+ // opens opportunities for globalopt (and inlining) by substituting
+ // function pointers passed as arguments to direct uses of functions.
+ lto_passes.add(llvm::createIPSCCPPass());
+
+ // Now that we internalized some globals, see if we can hack on them!
+ lto_passes.add(llvm::createGlobalOptimizerPass());
+
+ // Linking modules together can lead to duplicated global constants, only
+ // keep one copy of each constant...
+ lto_passes.add(llvm::createConstantMergePass());
+
+ // Remove unused arguments from functions...
+ lto_passes.add(llvm::createDeadArgEliminationPass());
+
+ // Reduce the code after globalopt and ipsccp. Both can open up
+ // significant simplification opportunities, and both can propagate
+ // functions through function pointers. When this happens, we often have
+ // to resolve varargs calls, etc, so let instcombine do this.
+ lto_passes.add(llvm::createInstructionCombiningPass());
+
+ // Inline small functions
+ lto_passes.add(llvm::createFunctionInliningPass());
+
+ // Remove dead EH info.
+ lto_passes.add(llvm::createPruneEHPass());
+
+ // Internalize the globals again after inlining
+ lto_passes.add(llvm::createGlobalOptimizerPass());
+
+ // Remove dead functions.
+ lto_passes.add(llvm::createGlobalDCEPass());
+
+ // If we didn't decide to inline a function, check to see if we can
+ // transform it to pass arguments by value instead of by reference.
+ lto_passes.add(llvm::createArgumentPromotionPass());
+
+ // The IPO passes may leave cruft around. Clean up after them.
+ lto_passes.add(llvm::createInstructionCombiningPass());
+ lto_passes.add(llvm::createJumpThreadingPass());
+
+ // Break up allocas
+ lto_passes.add(llvm::createScalarReplAggregatesPass());
+
+ // Run a few AA driven optimizations here and now, to cleanup the code.
+ lto_passes.add(llvm::createFunctionAttrsPass()); // Add nocapture.
+ lto_passes.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
+
+ // Hoist loop invariants.
+ lto_passes.add(llvm::createLICMPass());
+
+ // Remove redundancies.
+ lto_passes.add(llvm::createGVNPass());
+
+ // Remove dead memcpys.
+ lto_passes.add(llvm::createMemCpyOptPass());
+
+ // Nuke dead stores.
+ lto_passes.add(llvm::createDeadStoreEliminationPass());
+
+ // Cleanup and simplify the code after the scalar optimizations.
+ lto_passes.add(llvm::createInstructionCombiningPass());
+
+ lto_passes.add(llvm::createJumpThreadingPass());
+
+ // Delete basic blocks, which optimization passes may have killed.
+ lto_passes.add(llvm::createCFGSimplificationPass());
+
+ // Now that we have optimized the program, discard unreachable functions.
+ lto_passes.add(llvm::createGlobalDCEPass());
+ }
+
+ // Invokde "afterAddLTOPasses" after pass manager finished its
+ // construction.
+ if (!afterAddLTOPasses(pScript, lto_passes)) {
+ return kErrHookAfterAddLTOPasses;
+ }
+
+ // Invokde "beforeExecuteLTOPasses" before executing the passes.
+ if (!beforeExecuteLTOPasses(pScript, lto_passes)) {
+ return kErrHookBeforeExecuteLTOPasses;
+ }
+
+ lto_passes.run(pScript.getSource().getModule());
+
+ // Invokde "afterExecuteLTOPasses" before returning.
+ if (!afterExecuteLTOPasses(pScript)) {
+ return kErrHookAfterExecuteLTOPasses;
+ }
+
+ return kSuccess;
+}
+
+enum Compiler::ErrorCode Compiler::runCodeGen(Script &pScript,
+ llvm::raw_ostream &pResult) {
+ llvm::TargetData *target_data;
+ llvm::MCContext *mc_context = NULL;
+
+ // Create pass manager for MC code generation.
+ llvm::PassManager codegen_passes;
+
+ // Prepare TargetData target data from Module
+ target_data = new (std::nothrow) llvm::TargetData(*mTarget->getTargetData());
+ if (target_data == NULL) {
+ return kErrTargetDataNoMemory;
+ }
+
+ // Add TargetData to the pass manager.
+ codegen_passes.add(target_data);
+
+ // Invokde "beforeAddCodeGenPasses" before adding the first pass.
+ if (!beforeAddCodeGenPasses(pScript, codegen_passes)) {
+ return kErrHookBeforeAddCodeGenPasses;
+ }
+
+ // Add passes to the pass manager to emit machine code through MC layer.
+ if (mTarget->addPassesToEmitMC(codegen_passes, mc_context, pResult,
+ /* DisableVerify */false)) {
+ return kPrepareCodeGenPass;
+ }
+
+ // Invokde "afterAddCodeGenPasses" after pass manager finished its
+ // construction.
+ if (!afterAddCodeGenPasses(pScript, codegen_passes)) {
+ return kErrHookAfterAddCodeGenPasses;
+ }
+
+ // Invokde "beforeExecuteCodeGenPasses" before executing the passes.
+ if (!beforeExecuteCodeGenPasses(pScript, codegen_passes)) {
+ return kErrHookBeforeExecuteCodeGenPasses;
+ }
+
+ // Execute the pass.
+ codegen_passes.run(pScript.getSource().getModule());
+
+ // Invokde "afterExecuteCodeGenPasses" before returning.
+ if (!afterExecuteCodeGenPasses(pScript)) {
+ return kErrHookAfterExecuteCodeGenPasses;
+ }
+
+ return kSuccess;
+}
+
+enum Compiler::ErrorCode Compiler::compile(Script &pScript,
+ llvm::raw_ostream &pResult) {
+ llvm::Module &module = pScript.getSource().getModule();
+ enum ErrorCode err;
+
+ if (mTarget == NULL) {
+ return kErrNoTargetMachine;
+ }
+
+ // Materialize the bitcode module.
+ if (module.getMaterializer() != NULL) {
+ std::string error;
+ // A module with non-null materializer means that it is a lazy-load module.
+ // Materialize it now via invoking MaterializeAllPermanently(). This
+ // function returns false when the materialization is successful.
+ if (module.MaterializeAllPermanently(&error)) {
+ ALOGE("Failed to materialize the module `%s'! (%s)",
+ module.getModuleIdentifier().c_str(), error.c_str());
+ return kErrMaterialization;
+ }
+ }
+
+ if (mEnableLTO && ((err = runLTO(pScript)) != kSuccess)) {
+ return err;
+ }
+
+ if ((err = runCodeGen(pScript, pResult)) != kSuccess) {
+ return err;
+ }
+
+ return kSuccess;
+}
+
+enum Compiler::ErrorCode Compiler::compile(Script &pScript,
+ OutputFile &pResult) {
+ // Check the state of the specified output file.
+ if (pResult.hasError()) {
+ return kErrInvalidOutputFileState;
+ }
+
+ // Open the output file decorated in llvm::raw_ostream.
+ llvm::raw_ostream *out = pResult.dup();
+ if (out == NULL) {
+ return kErrPrepareOutput;
+ }
+
+ // Delegate the request.
+ enum Compiler::ErrorCode err = compile(pScript, *out);
+
+ // Close the output before return.
+ delete out;
+
+ return err;
+}
diff --git a/lib/ExecutionEngine/Compiler.h b/lib/ExecutionEngine/Compiler.h
new file mode 100644
index 0000000..5e37925
--- /dev/null
+++ b/lib/ExecutionEngine/Compiler.h
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2010-2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_COMPILER_H
+#define BCC_EXECUTION_ENGINE_COMPILER_H
+
+namespace llvm {
+
+class raw_ostream;
+class PassManager;
+class TargetData;
+class TargetMachine;
+
+} // end namespace llvm
+
+namespace bcc {
+
+class CompilerConfig;
+class OutputFile;
+class Script;
+
+//===----------------------------------------------------------------------===//
+// Design of Compiler
+//===----------------------------------------------------------------------===//
+// 1. A compiler instance can be constructed provided an "initial config."
+// 2. A compiler can later be re-configured using config().
+// 3. Once config() is invoked, it'll re-create TargetMachine instance (i.e.,
+// mTarget) according to the configuration supplied. TargetMachine instance
+// is *shared* across the different calls to compile() before the next call
+// to config().
+// 4. Once a compiler instance is created, you can use the compile() service
+// to compile the file over and over again. Each call uses TargetMachine
+// instance to construct the compilation passes.
+class Compiler {
+public:
+ enum ErrorCode {
+ kSuccess,
+
+ kInvalidConfigNoTarget,
+ kErrCreateTargetMachine,
+ kErrSwitchTargetMachine,
+ kErrNoTargetMachine,
+ kErrTargetDataNoMemory,
+ kErrMaterialization,
+ kErrInvalidOutputFileState,
+ kErrPrepareOutput,
+ kPrepareCodeGenPass,
+
+ kErrHookBeforeAddLTOPasses,
+ kErrHookAfterAddLTOPasses,
+ kErrHookBeforeExecuteLTOPasses,
+ kErrHookAfterExecuteLTOPasses,
+
+ kErrHookBeforeAddCodeGenPasses,
+ kErrHookAfterAddCodeGenPasses,
+ kErrHookBeforeExecuteCodeGenPasses,
+ kErrHookAfterExecuteCodeGenPasses,
+
+ kMaxErrorCode,
+ };
+
+ static const char *GetErrorString(enum ErrorCode pErrCode);
+
+private:
+ llvm::TargetMachine *mTarget;
+ // LTO is enabled by default.
+ bool mEnableLTO;
+
+ enum ErrorCode runLTO(Script &pScript);
+ enum ErrorCode runCodeGen(Script &pScript, llvm::raw_ostream &pResult);
+
+public:
+ Compiler();
+ Compiler(const CompilerConfig &pConfig);
+
+ enum ErrorCode config(const CompilerConfig &pConfig);
+
+ // Compile a script and output the result to a LLVM stream.
+ enum ErrorCode compile(Script &pScript, llvm::raw_ostream &pResult);
+
+ // Compile a script and output the result to a file.
+ enum ErrorCode compile(Script &pScript, OutputFile &pResult);
+
+ void enableLTO(bool pEnable = true)
+ { mEnableLTO = pEnable; }
+
+ virtual ~Compiler();
+
+protected:
+ //===--------------------------------------------------------------------===//
+ // Plugin callbacks for sub-class.
+ //===--------------------------------------------------------------------===//
+ // Called before adding first pass to code-generation passes.
+ virtual bool beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM)
+ { return true; }
+
+ // Called after adding last pass to code-generation passes.
+ virtual bool afterAddLTOPasses(Script &pScript, llvm::PassManager &pPM)
+ { return true; }
+
+ // Called before executing code-generation passes.
+ virtual bool beforeExecuteLTOPasses(Script &pScript,
+ llvm::PassManager &pPM)
+ { return true; }
+
+ // Called after executing code-generation passes.
+ virtual bool afterExecuteLTOPasses(Script &pScript)
+ { return true; }
+
+ // Called before adding first pass to code-generation passes.
+ virtual bool beforeAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
+ { return true; }
+
+ // Called after adding last pass to code-generation passes.
+ virtual bool afterAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
+ { return true; }
+
+ // Called before executing code-generation passes.
+ virtual bool beforeExecuteCodeGenPasses(Script &pScript,
+ llvm::PassManager &pPM)
+ { return true; }
+
+ // Called after executing code-generation passes.
+ virtual bool afterExecuteCodeGenPasses(Script &pScript)
+ { return true; }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_COMPILER_H
diff --git a/lib/ExecutionEngine/CompilerConfig.cpp b/lib/ExecutionEngine/CompilerConfig.cpp
new file mode 100644
index 0000000..faaaa97
--- /dev/null
+++ b/lib/ExecutionEngine/CompilerConfig.cpp
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "CompilerConfig.h"
+
+#include <llvm/CodeGen/SchedulerRegistry.h>
+#include <llvm/MC/SubtargetFeature.h>
+#include <llvm/Support/TargetRegistry.h>
+
+#include "DebugHelper.h"
+#include "TargetCompilerConfigs.h"
+
+using namespace bcc;
+
+CompilerConfig::CompilerConfig(const std::string &pTriple)
+ : mTriple(pTriple), mTarget(NULL) {
+ //===--------------------------------------------------------------------===//
+ // Default setting of register sheduler
+ //===--------------------------------------------------------------------===//
+ llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
+
+ //===--------------------------------------------------------------------===//
+ // Default setting of target options
+ //===--------------------------------------------------------------------===//
+ // Use hardfloat ABI by default.
+ //
+ // TODO(all): Need to detect the CPU capability and decide whether to use
+ // softfp. To use softfp, change the following 2 lines to
+ //
+ // options.FloatABIType = llvm::FloatABI::Soft;
+ // options.UseSoftFloat = true;
+ mTargetOpts.FloatABIType = llvm::FloatABI::Soft;
+ mTargetOpts.UseSoftFloat = false;
+
+ // Enable frame pointer elimination optimization by default.
+ mTargetOpts.NoFramePointerElim = false;
+
+ //===--------------------------------------------------------------------===//
+ // Default setting for code model
+ //===--------------------------------------------------------------------===//
+ mCodeModel = llvm::CodeModel::Small;
+
+ //===--------------------------------------------------------------------===//
+ // Default setting for relocation model
+ //===--------------------------------------------------------------------===//
+ mRelocModel = llvm::Reloc::Default;
+
+ //===--------------------------------------------------------------------===//
+ // Default setting for optimization level (-O2)
+ //===--------------------------------------------------------------------===//
+ mOptLevel = llvm::CodeGenOpt::Default;
+
+ //===--------------------------------------------------------------------===//
+ // Default setting for architecture type
+ //===--------------------------------------------------------------------===//
+ mArchType = llvm::Triple::UnknownArch;
+
+ initializeTarget();
+ initializeArch();
+
+ return;
+}
+
+bool CompilerConfig::initializeTarget() {
+ std::string error;
+ mTarget = llvm::TargetRegistry::lookupTarget(mTriple, error);
+ if (mTarget != NULL) {
+ return true;
+ } else {
+ ALOGE("Cannot initialize llvm::Target for given triple '%s'! (%s)",
+ mTriple.c_str(), error.c_str());
+ return false;
+ }
+}
+
+void CompilerConfig::initializeArch() {
+ if (mTarget != NULL) {
+ mArchType = llvm::Triple::getArchTypeForLLVMName(mTarget->getName());
+ } else {
+ mArchType = llvm::Triple::UnknownArch;
+ }
+ return;
+}
+
+void CompilerConfig::setFeatureString(const std::vector<std::string> &pAttrs) {
+ llvm::SubtargetFeatures f;
+
+ for (std::vector<std::string>::const_iterator attr_iter = pAttrs.begin(),
+ attr_end = pAttrs.end();
+ attr_iter != attr_end; attr_iter++) {
+ f.AddFeature(*attr_iter);
+ }
+
+ mFeatureString = f.getString();
+ return;
+}
diff --git a/lib/ExecutionEngine/CompilerConfig.h b/lib/ExecutionEngine/CompilerConfig.h
new file mode 100644
index 0000000..d83086a
--- /dev/null
+++ b/lib/ExecutionEngine/CompilerConfig.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_COMPILER_CONFIG_H
+#define BCC_EXECUTION_ENGINE_COMPILER_CONFIG_H
+
+#include <string>
+#include <vector>
+
+#include <llvm/ADT/Triple.h>
+#include <llvm/Support/CodeGen.h>
+#include <llvm/Target/TargetOptions.h>
+
+namespace llvm {
+
+class Target;
+
+} // end namespace llvm
+
+namespace bcc {
+
+class CompilerConfig {
+private:
+ //===--------------------------------------------------------------------===//
+ // Available Configurations
+ //===--------------------------------------------------------------------===//
+ std::string mTriple;
+
+ // Optional. If given, the name of the target CPU to generate code for.
+ std::string mCPU;
+
+ llvm::TargetOptions mTargetOpts;
+
+ llvm::CodeModel::Model mCodeModel;
+
+ llvm::CodeGenOpt::Level mOptLevel;
+
+ llvm::Reloc::Model mRelocModel;
+
+ // The list of target specific features to enable or disable -- this should
+ // be a list of strings starting with '+' (enable) or '-' (disable).
+ std::string mFeatureString;
+
+private:
+ //===--------------------------------------------------------------------===//
+ // These are generated by CompilerConfig during initialize().
+ //===--------------------------------------------------------------------===//
+ const llvm::Target *mTarget;
+ bool initializeTarget();
+
+ llvm::Triple::ArchType mArchType;
+ void initializeArch();
+
+public:
+ //===--------------------------------------------------------------------===//
+ // Getters
+ //===--------------------------------------------------------------------===//
+ inline const std::string &getTriple() const
+ { return mTriple; }
+
+ inline const std::string &getCPU() const
+ { return mCPU; }
+ inline void setCPU(const std::string &pCPU)
+ { mCPU = pCPU; }
+
+ inline const llvm::TargetOptions &getTargetOptions() const
+ { return mTargetOpts; }
+ inline llvm::TargetOptions &getTargetOptions()
+ { return mTargetOpts; }
+
+ inline llvm::CodeModel::Model getCodeModel() const
+ { return mCodeModel; }
+ inline void setCodeModel(llvm::CodeModel::Model pCodeMode)
+ { mCodeModel = pCodeMode; }
+
+ inline llvm::CodeGenOpt::Level getOptimizationLevel() const
+ { return mOptLevel; }
+ inline void setOptimizationLevel(llvm::CodeGenOpt::Level pOptLvl)
+ { mOptLevel = pOptLvl; }
+
+ inline llvm::Reloc::Model getRelocationModel() const
+ { return mRelocModel; }
+ inline void setRelocationModel(llvm::Reloc::Model pRelocModel)
+ { mRelocModel = pRelocModel; }
+
+ inline const llvm::Target *getTarget() const
+ { return mTarget; }
+
+ inline llvm::Triple::ArchType getArchType() const
+ { return mArchType; }
+
+ inline const std::string &getFeatureString() const
+ { return mFeatureString; }
+ void setFeatureString(const std::vector<std::string> &pAttrs);
+
+public:
+ CompilerConfig(const std::string &pTriple);
+
+ virtual ~CompilerConfig() { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_COMPILER_CONFIG_H
diff --git a/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp b/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
index 82eac5f..01f613e 100644
--- a/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
+++ b/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
@@ -24,8 +24,8 @@
#include "ELFSymbol.h"
#include "utils/serialize.h"
-#include "bcc/ExecutionEngine/SymbolResolverInterface.h"
-#include "bcc/Support/DebugHelper.h"
+#include "DebugHelper.h"
+#include "SymbolResolverInterface.h"
using namespace bcc;
diff --git a/lib/ExecutionEngine/File.h b/lib/ExecutionEngine/File.h
new file mode 100644
index 0000000..4b7ad4d
--- /dev/null
+++ b/lib/ExecutionEngine/File.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_FILE_H
+#define BCC_EXECUTION_ENGINE_FILE_H
+
+#include "FileBase.h"
+
+namespace bcc {
+
+template<enum FileBase::OpenModeEnum OpenMode>
+struct FileAttribute {
+ // The flags to the 2nd argument in ::open().
+ enum { kOpenFlags };
+
+ // Default value of LockMode.
+ enum { kDefaultLockMode };
+};
+
+// FileAttribute for accessing read-only file
+template<>
+struct FileAttribute<FileBase::kReadMode> {
+ enum { kOpenFlags = O_RDONLY };
+ enum { kDefaultLockMode = FileBase::kReadLock };
+};
+
+// FileAttribute for accessing writable file
+template<>
+struct FileAttribute<FileBase::kWriteMode> {
+ enum { kOpenFlags = O_RDWR | O_CREAT | O_TRUNC };
+ enum { kDefaultLockMode = FileBase::kWriteLock };
+};
+
+template<enum FileBase::OpenModeEnum OpenMode>
+class File : public FileBase {
+public:
+ File(const std::string &pFilename, unsigned pFlags)
+ : FileBase(pFilename, FileAttribute<OpenMode>::kOpenFlags, pFlags) { }
+
+ inline bool lock(enum LockModeEnum pMode = static_cast<enum LockModeEnum>(
+ FileAttribute<OpenMode>::kDefaultLockMode),
+ bool pNonblocking = true,
+ unsigned pMaxRetry = FileBase::kDefaultMaxRetryLock,
+ useconds_t pRetryInterval =
+ FileBase::kDefaultRetryLockInterval) {
+ return FileBase::lock(pMode, pNonblocking, pMaxRetry, pRetryInterval);
+ }
+
+ inline android::FileMap *createMap(off_t pOffset, size_t pLength,
+ bool pIsReadOnly =
+ (OpenMode == FileBase::kReadMode)) {
+ return FileBase::createMap(pOffset, pLength, pIsReadOnly);
+ }
+};
+
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_FILE_H
diff --git a/lib/ExecutionEngine/FileBase.cpp b/lib/ExecutionEngine/FileBase.cpp
new file mode 100644
index 0000000..379406d
--- /dev/null
+++ b/lib/ExecutionEngine/FileBase.cpp
@@ -0,0 +1,279 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "FileBase.h"
+
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <cerrno>
+#include <new>
+
+#include <utils/FileMap.h>
+
+using namespace bcc;
+
+FileBase::FileBase(const std::string &pFilename,
+ unsigned pOpenFlags,
+ unsigned pFlags)
+ : mFD(-1),
+ mError(),
+ mName(pFilename), mOpenFlags(pOpenFlags),
+ mShouldUnlock(false) {
+ // Process pFlags
+#ifdef O_BINARY
+ if (pFlags & kBinary) {
+ mOpenFlags |= O_BINARY;
+ }
+#endif
+
+ // Open the file.
+ open();
+
+ return;
+}
+
+FileBase::~FileBase() {
+ close();
+}
+
+bool FileBase::open() {
+ do {
+ // FIXME: Hard-coded permissions (0644) for newly created file should be
+ // removed and provide a way to let the user configure the value.
+ mFD = ::open(mName.c_str(), mOpenFlags, 0644);
+ if (mFD > 0) {
+ return true;
+ }
+
+ // Some errors occurred ...
+ if (errno != EINTR) {
+ detectError();
+ return false;
+ }
+ } while (true);
+ // unreachable
+}
+
+
+bool FileBase::checkFileIntegrity() {
+ // Check the file integrity by examine whether the inode referred to the mFD
+ // and to the file mName are the same.
+ struct stat fd_stat, file_stat;
+
+ // Get the file status of file descriptor mFD.
+ do {
+ if (::fstat(mFD, &fd_stat) == 0) {
+ break;
+ } else if (errno != EINTR) {
+ detectError();
+ return false;
+ }
+ } while (true);
+
+ // Get the file status of file mName.
+ do {
+ if (::stat(mName.c_str(), &file_stat) == 0) {
+ break;
+ } else if (errno != EINTR) {
+ detectError();
+ return false;
+ }
+ } while (true);
+
+ return ((fd_stat.st_dev == file_stat.st_dev) &&
+ (fd_stat.st_ino == file_stat.st_ino));
+}
+
+void FileBase::detectError() {
+ // Read error from errno.
+ mError.assign(errno, llvm::posix_category());
+}
+
+bool FileBase::lock(enum LockModeEnum pMode,
+ bool pNonblocking,
+ unsigned pMaxRetry,
+ useconds_t pRetryInterval) {
+ int lock_operation;
+ unsigned retry = 0;
+
+ // Check the state.
+ if ((mFD < 0) || hasError()) {
+ return false;
+ }
+
+ // Return immediately if it's already locked.
+ if (mShouldUnlock) {
+ return true;
+ }
+
+ // Determine the lock operation (2nd argument) to the flock().
+ if (pMode == kReadLock) {
+ lock_operation = LOCK_SH;
+ } else if (pMode == kWriteLock) {
+ lock_operation = LOCK_EX;
+ } else {
+ mError.assign(llvm::errc::invalid_argument, llvm::posix_category());
+ return false;
+ }
+
+ if (pNonblocking) {
+ lock_operation |= LOCK_NB;
+ }
+
+ do {
+ if (::flock(mFD, lock_operation) == 0) {
+ mShouldUnlock = true;
+ // Here we got a lock but we need to check whether the mFD still
+ // "represents" the filename (mName) we opened in the contructor. This
+ // check may failed when another process deleted the original file mFD
+ // mapped when we were trying to obtain the lock on the file.
+ if (!checkFileIntegrity()) {
+ if (hasError() || !reopen()) {
+ // Error occurred when check the file integrity or re-open the file.
+ return false;
+ } else {
+ // Wait a while before the next try.
+ ::usleep(pRetryInterval);
+ retry++;
+ continue;
+ }
+ }
+
+ return true;
+ }
+
+ // flock() was not performed successfully. Check the errno to see whether
+ // it's retry-able.
+ if (errno == EINTR) {
+ // flock() was interrupted by delivery of a signal. Restart without
+ // decrement the retry counter.
+ continue;
+ } else if (errno == EWOULDBLOCK) {
+ // The file descriptor was locked by others, wait for a while before next
+ // retry.
+ retry++;
+ ::usleep(pRetryInterval);
+ } else {
+ // There's a fatal error occurs when perform flock(). Return immediately
+ // without further retry.
+ detectError();
+ return false;
+ }
+ } while (retry <= pMaxRetry);
+
+ return false;
+}
+
+void FileBase::unlock() {
+ if (mFD < 0) {
+ return;
+ }
+
+ do {
+ if (::flock(mFD, LOCK_UN) == 0) {
+ mShouldUnlock = false;
+ return;
+ }
+ } while (errno == EINTR);
+
+ detectError();
+ return;
+}
+
+android::FileMap *FileBase::createMap(off_t pOffset, size_t pLength,
+ bool pIsReadOnly) {
+ if (mFD < 0 || hasError()) {
+ return NULL;
+ }
+
+ android::FileMap *map = new (std::nothrow) android::FileMap();
+ if (map == NULL) {
+ mError.assign(llvm::errc::not_enough_memory, llvm::system_category());
+ return NULL;
+ }
+
+ if (!map->create(NULL, mFD, pOffset, pLength, pIsReadOnly)) {
+ detectError();
+ map->release();
+ return NULL;
+ }
+
+ return map;
+}
+
+size_t FileBase::getSize() {
+ if (mFD < 0 || hasError()) {
+ return static_cast<size_t>(-1);
+ }
+
+ struct stat file_stat;
+ do {
+ if (::fstat(mFD, &file_stat) == 0) {
+ break;
+ } else if (errno != EINTR) {
+ detectError();
+ return static_cast<size_t>(-1);
+ }
+ } while (true);
+
+ return file_stat.st_size;
+}
+
+off_t FileBase::seek(off_t pOffset) {
+ if ((mFD < 0) || hasError()) {
+ return static_cast<off_t>(-1);
+ }
+
+ do {
+ off_t result = ::lseek(mFD, pOffset, SEEK_SET);
+ if (result == pOffset) {
+ return result;
+ }
+ } while (errno == EINTR);
+
+ detectError();
+ return static_cast<off_t>(-1);
+}
+
+off_t FileBase::tell() {
+ if ((mFD < 0) || hasError()) {
+ return static_cast<off_t>(-1);
+ }
+
+ do {
+ off_t result = ::lseek(mFD, 0, SEEK_CUR);
+ if (result != static_cast<off_t>(-1)) {
+ return result;
+ }
+ } while (errno == EINTR);
+
+ detectError();
+ return static_cast<off_t>(-1);
+}
+
+void FileBase::close() {
+ if (mShouldUnlock) {
+ unlock();
+ mShouldUnlock = false;
+ }
+ if (mFD > 0) {
+ ::close(mFD);
+ mFD = -1;
+ }
+ return;
+}
diff --git a/lib/ExecutionEngine/FileBase.h b/lib/ExecutionEngine/FileBase.h
new file mode 100644
index 0000000..62d6504
--- /dev/null
+++ b/lib/ExecutionEngine/FileBase.h
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_FILE_BASE_H
+#define BCC_EXECUTION_ENGINE_FILE_BASE_H
+
+#include <fcntl.h>
+
+#include <string>
+
+#include <llvm/Support/system_error.h>
+
+namespace android {
+ class FileMap;
+}
+
+namespace bcc {
+
+class FileBase {
+public:
+ enum OpenModeEnum {
+ kReadMode = 1 << 0,
+ kWriteMode = 1 << 1,
+ kReadWriteMode = (kReadMode | kWriteMode),
+ };
+
+ enum FlagEnum {
+ // The openning file is a binary file.
+ kBinary = 1 << 0,
+ };
+
+ enum LockModeEnum {
+ // The shared resource behind the Stream locked in ReadLock mode can be
+ // locked by other processes at the same time.
+ kReadLock,
+
+ // The shared resource behind the Stream locked in WriteLock mode can only
+ // be locked by one process. It's exclusive. That is, the shared resource
+ // cannot have both ReadLock and WriteLock simultaneously.
+ kWriteLock
+ };
+
+ // Default configuration to the lock().
+ enum {
+ kDefaultMaxRetryLock = 4,
+ kDefaultRetryLockInterval = 200000UL,
+ };
+
+protected:
+ // Grant direct access of the internal file descriptor to the sub-class and
+ // error message such that they can implement their own I/O functionality.
+ int mFD;
+
+ llvm::error_code mError;
+
+private:
+ std::string mName;
+
+ // The 2nd argument to the POSIX open().
+ unsigned mOpenFlags;
+
+ // True true if we should call unlock() in destructor.
+ bool mShouldUnlock;
+
+ // Open mName with flag mOpenFlags (using POSIX open().)
+ bool open();
+
+ // Return true if mFD is the corresponded file descriptor to the file named
+ // mName on the filesystem. This check may returns failed, for example,
+ // someone re-create the file with the same name after we openning the file.
+ bool checkFileIntegrity();
+
+ inline bool reopen() {
+ // Don't need to check the object state since this is a private method.
+ close();
+ return open();
+ }
+
+private:
+ FileBase(FileBase &); // Do not implement.
+ void operator=(const FileBase &); // Do not implement.
+
+protected:
+ // pOpenFlags is the 2nd argument to the POSIX open(). pFlags are the flags to
+ // FileBase. It's a bit set composed by the value defined in
+ // FileBase::FlagEnum.
+ FileBase(const std::string &pFilename, unsigned pOpenFlags, unsigned pFlags);
+
+ void detectError();
+
+public:
+ // Lock the file descriptor in given pMode. If pNonblocking is true, the lock
+ // request issued will return immediately when the shared resource is locked.
+ // In this case, it retries pMaxRetry times, each wait pRetryInterval (in
+ // usecs) before the previous retry getting done.
+ //
+ // Only file is allowed to use this API.
+ bool lock(enum LockModeEnum pMode, bool pNonblocking = true,
+ unsigned pMaxRetry = kDefaultMaxRetryLock,
+ useconds_t pRetryInterval = kDefaultRetryLockInterval);
+
+ void unlock();
+
+ // Map the file content to the memory.
+ //
+ // One who gets non-null android::FileMap returned from this API is resposible
+ // for destroying it after the use.
+ android::FileMap *createMap(off_t pOffset, size_t pLength, bool pIsReadOnly);
+
+ size_t getSize();
+
+ off_t seek(off_t pOffset);
+ off_t tell();
+
+ inline bool hasError() const
+ { return (mError.value() != llvm::errc::success); }
+
+ inline const llvm::error_code &getError() const
+ { return mError; }
+
+ // The return value of llvm::error_code::message() is obtained upon the call
+ // and is passed by value (that is, it's not a member of llvm::error_code.)
+ inline std::string getErrorMessage() const
+ { return mError.message(); }
+
+ inline const std::string &getName() const
+ { return mName; }
+
+ void close();
+
+ virtual ~FileBase();
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_FILE_BASE_H
diff --git a/lib/ExecutionEngine/FileMutex.h b/lib/ExecutionEngine/FileMutex.h
new file mode 100644
index 0000000..85c0e84
--- /dev/null
+++ b/lib/ExecutionEngine/FileMutex.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_FILE_MUTEX_H
+#define BCC_EXECUTION_ENGINE_FILE_MUTEX_H
+
+#include <string>
+
+#include "FileBase.h"
+
+namespace bcc {
+
+template<enum FileBase::LockModeEnum LockMode>
+class FileMutex : public FileBase {
+public:
+ FileMutex(const std::string &pFileToLock)
+ : FileBase(pFileToLock + ".lock", O_RDONLY | O_CREAT, 0) { }
+
+ // Provide a lock() interface filled with default configuration.
+ inline bool lock(bool pNonblocking = true,
+ unsigned pMaxRetry = FileBase::kDefaultMaxRetryLock,
+ useconds_t pRetryInterval =
+ FileBase::kDefaultRetryLockInterval) {
+ return FileBase::lock(LockMode, pNonblocking, pMaxRetry, pRetryInterval);
+ }
+};
+
+} // namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_FILE_MUTEX_H
diff --git a/lib/ExecutionEngine/GDBJIT.cpp b/lib/ExecutionEngine/GDBJIT.cpp
index 29944dd..4038098 100644
--- a/lib/ExecutionEngine/GDBJIT.cpp
+++ b/lib/ExecutionEngine/GDBJIT.cpp
@@ -29,7 +29,7 @@
//
//===----------------------------------------------------------------------===//
-#include <llvm/Support/Compiler.h>
+#include "llvm/Support/Compiler.h"
// This interface must be kept in sync with gdb/gdb/jit.h .
extern "C" {
diff --git a/lib/ExecutionEngine/GDBJIT.h b/lib/ExecutionEngine/GDBJIT.h
new file mode 100644
index 0000000..442b666
--- /dev/null
+++ b/lib/ExecutionEngine/GDBJIT.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// This file contains portions derived from LLVM, with the original copyright
+// header below:
+//===-------------- GDBJIT.h - Register debug symbols for JIT -------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the data structures used by JIT engines to register object
+// files (ideally containing debug info) with GDB.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef BCC_EXECUTION_ENGINE_GDB_JIT_H
+#define BCC_EXECUTION_ENGINE_GDB_JIT_H
+
+#include "llvm/Support/DataTypes.h"
+#include "llvm/Support/Compiler.h"
+
+// This must be kept in sync with gdb/gdb/jit.h .
+extern "C" {
+
+ typedef enum {
+ JIT_NOACTION = 0,
+ JIT_REGISTER_FN,
+ JIT_UNREGISTER_FN
+ } jit_actions_t;
+
+ struct jit_code_entry {
+ struct jit_code_entry *next_entry;
+ struct jit_code_entry *prev_entry;
+ const char *symfile_addr;
+ uint64_t symfile_size;
+ };
+
+ struct jit_descriptor {
+ uint32_t version;
+ // This should be jit_actions_t, but we want to be specific about the
+ // bit-width.
+ uint32_t action_flag;
+ struct jit_code_entry *relevant_entry;
+ struct jit_code_entry *first_entry;
+ };
+
+ // GDB 7.0+ puts a (silent) breakpoint in this function.
+ LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code();
+
+}
+
+#endif // BCC_EXECUTION_ENGINE_GDB_JIT_H
diff --git a/lib/ExecutionEngine/GDBJITRegistrar.cpp b/lib/ExecutionEngine/GDBJITRegistrar.cpp
index 68d6833..5d8428a 100644
--- a/lib/ExecutionEngine/GDBJITRegistrar.cpp
+++ b/lib/ExecutionEngine/GDBJITRegistrar.cpp
@@ -30,15 +30,13 @@
//
//===----------------------------------------------------------------------===//
-#include "bcc/ExecutionEngine/GDBJITRegistrar.h"
-
-#include <llvm/ADT/DenseMap.h>
-#include <llvm/Support/ErrorHandling.h>
-#include <llvm/Support/Memory.h>
-#include <llvm/Support/Mutex.h>
-#include <llvm/Support/MutexGuard.h>
-
-#include "bcc/ExecutionEngine/GDBJIT.h"
+#include "GDBJITRegistrar.h"
+#include "llvm/ADT/DenseMap.h"
+#include "GDBJIT.h"
+#include "llvm/Support/Memory.h"
+#include "llvm/Support/MutexGuard.h"
+#include "llvm/Support/Mutex.h"
+#include "llvm/Support/ErrorHandling.h"
#include <fstream>
diff --git a/lib/ExecutionEngine/GDBJITRegistrar.h b/lib/ExecutionEngine/GDBJITRegistrar.h
new file mode 100644
index 0000000..debf503
--- /dev/null
+++ b/lib/ExecutionEngine/GDBJITRegistrar.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// This file contains portions derived from LLVM, with the original copyright
+// header below:
+//===-- GDBJITRegistrar.h - Common Implementation shared by GDB-JIT users --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains declarations of the interface an ExecutionEngine would use
+// to register an in-memory object file with GDB.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef BCC_GDBJITREGISTRAR_H
+#define BCC_GDBJITREGISTRAR_H
+
+#include <cstddef>
+
+// Buffer for an in-memory object file in executable memory
+typedef char ObjectBuffer;
+
+void registerObjectWithGDB(const ObjectBuffer* Object, std::size_t Size);
+void deregisterObjectWithGDB(const ObjectBuffer* Object);
+
+#endif // BCC_GDBJITREGISTRAR_H
diff --git a/lib/ExecutionEngine/Initialization.cpp b/lib/ExecutionEngine/Initialization.cpp
new file mode 100644
index 0000000..7cdb938
--- /dev/null
+++ b/lib/ExecutionEngine/Initialization.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Initialization.h"
+
+#include <cstdlib>
+
+#include <llvm/Support/ErrorHandling.h>
+#include <llvm/Support/TargetSelect.h>
+
+#include "Config.h"
+#include "DebugHelper.h"
+
+namespace {
+
+void llvm_error_handler(void *pUserData, const std::string &pMessage) {
+ ALOGE("%s", pMessage.c_str());
+ ::exit(1);
+}
+
+} // end anonymous namespace
+
+void bcc::init::Initialize() {
+ static bool is_initialized = false;
+
+ if (is_initialized) {
+ return;
+ }
+
+ // Setup error handler for LLVM.
+ llvm::remove_fatal_error_handler();
+ llvm::install_fatal_error_handler(llvm_error_handler, NULL);
+
+#if defined(PROVIDE_ARM_CODEGEN)
+ LLVMInitializeARMAsmPrinter();
+ LLVMInitializeARMTargetMC();
+ LLVMInitializeARMTargetInfo();
+ LLVMInitializeARMTarget();
+#endif
+
+#if defined(PROVIDE_MIPS_CODEGEN)
+ LLVMInitializeMipsAsmPrinter();
+ LLVMInitializeMipsTargetMC();
+ LLVMInitializeMipsTargetInfo();
+ LLVMInitializeMipsTarget();
+#endif
+
+#if defined(PROVIDE_X86_CODEGEN)
+ LLVMInitializeX86AsmPrinter();
+ LLVMInitializeX86TargetMC();
+ LLVMInitializeX86TargetInfo();
+ LLVMInitializeX86Target();
+#endif
+
+#if USE_DISASSEMBLER
+ InitializeDisassembler();
+#endif
+
+ is_initialized = true;
+
+ return;
+}
diff --git a/lib/ExecutionEngine/Initialization.h b/lib/ExecutionEngine/Initialization.h
new file mode 100644
index 0000000..9a357bc
--- /dev/null
+++ b/lib/ExecutionEngine/Initialization.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_INITIALIZATION_H
+#define BCC_EXECUTION_ENGINE_INITIALIZATION_H
+
+namespace bcc {
+
+namespace init {
+
+void Initialize();
+
+} // end namespace init
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_INITIALIZATION_H
diff --git a/lib/ExecutionEngine/InputFile.cpp b/lib/ExecutionEngine/InputFile.cpp
new file mode 100644
index 0000000..a51b09e
--- /dev/null
+++ b/lib/ExecutionEngine/InputFile.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "InputFile.h"
+
+#include "DebugHelper.h"
+
+using namespace bcc;
+
+InputFile::InputFile(const std::string &pFilename, unsigned pFlags)
+ : super(pFilename, pFlags) { }
+
+ssize_t InputFile::read(void *pBuf, size_t count) {
+ if ((mFD < 0) || hasError()) {
+ return -1;
+ }
+
+ if ((count <= 0) || (pBuf == NULL)) {
+ // Keep safe and issue a warning.
+ ALOGW("InputFile::read: count = %zu, buffer = %p", count, pBuf);
+ return 0;
+ }
+
+ while (count > 0) {
+ ssize_t read_size = ::read(mFD, pBuf, count);
+
+ if (read_size >= 0) {
+ return read_size;
+ } else if ((errno == EAGAIN) || (errno == EINTR)) {
+ // If the errno is EAGAIN or EINTR, then we try to read again.
+ //
+ // Fall-through
+ } else {
+ detectError();
+ return -1;
+ }
+ }
+ // unreachable
+ return 0;
+}
diff --git a/lib/ExecutionEngine/InputFile.h b/lib/ExecutionEngine/InputFile.h
new file mode 100644
index 0000000..a7c22d2
--- /dev/null
+++ b/lib/ExecutionEngine/InputFile.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_INPUT_FILE_H
+#define BCC_EXECUTION_ENGINE_INPUT_FILE_H
+
+#include "File.h"
+#include "FileBase.h"
+
+namespace bcc {
+
+class InputFile : public File<FileBase::kReadMode> {
+ typedef File<FileBase::kReadMode> super;
+public:
+ InputFile(const std::string &pFilename, unsigned pFlags = 0);
+
+ ssize_t read(void *pBuf, size_t count);
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_INPUT_FILE_H
diff --git a/lib/ExecutionEngine/ObjectLoader.cpp b/lib/ExecutionEngine/ObjectLoader.cpp
index 1277e13..708ded3 100644
--- a/lib/ExecutionEngine/ObjectLoader.cpp
+++ b/lib/ExecutionEngine/ObjectLoader.cpp
@@ -14,15 +14,14 @@
* limitations under the License.
*/
-#include "bcc/ExecutionEngine/ObjectLoader.h"
+#include "ObjectLoader.h"
#include <utils/FileMap.h>
-#include "bcc/ExecutionEngine/GDBJITRegistrar.h"
-#include "bcc/Support/DebugHelper.h"
-#include "bcc/Support/FileBase.h"
-
+#include "DebugHelper.h"
#include "ELFObjectLoaderImpl.h"
+#include "FileBase.h"
+#include "GDBJITRegistrar.h"
using namespace bcc;
diff --git a/lib/ExecutionEngine/ObjectLoader.h b/lib/ExecutionEngine/ObjectLoader.h
new file mode 100644
index 0000000..6c2bc8c
--- /dev/null
+++ b/lib/ExecutionEngine/ObjectLoader.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_OBJECT_LOADER_H
+#define BCC_EXECUTION_ENGINE_OBJECT_LOADER_H
+
+#include <cstddef>
+
+namespace bcc {
+
+class FileBase;
+class ObjectLoaderImpl;
+class SymbolResolverInterface;
+
+class ObjectLoader {
+private:
+ ObjectLoaderImpl *mImpl;
+
+ void *mDebugImage;
+
+ ObjectLoader() : mImpl(NULL), mDebugImage(0) { }
+
+public:
+ // Load from a in-memory object. pName is a descriptive name of this memory.
+ static ObjectLoader *Load(void *pMemStart, size_t pMemSize, const char *pName,
+ SymbolResolverInterface &pResolver,
+ bool pEnableGDBDebug);
+
+ // Load from a file.
+ static ObjectLoader *Load(FileBase &pFile,
+ SymbolResolverInterface &pResolver,
+ bool pEnableGDBDebug);
+
+ void *getSymbolAddress(const char *pName) const;
+
+ ~ObjectLoader();
+};
+
+} // namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_OBJECT_LOADER_H
diff --git a/lib/ExecutionEngine/ObjectLoaderImpl.h b/lib/ExecutionEngine/ObjectLoaderImpl.h
index 0e0f2c1..81756f8 100644
--- a/lib/ExecutionEngine/ObjectLoaderImpl.h
+++ b/lib/ExecutionEngine/ObjectLoaderImpl.h
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-#ifndef OBJECT_LOADER_IMPL_H
-#define OBJECT_LOADER_IMPL_H
+#ifndef BCC_EXECUTION_ENGINE_OBJECT_LOADER_IMPL_H
+#define BCC_EXECUTION_ENGINE_OBJECT_LOADER_IMPL_H
#include <cstring>
@@ -40,4 +40,4 @@
} // namespace bcc
-#endif // OBJECT_LOADER_IMPL_H
+#endif // BCC_EXECUTION_ENGINE_OBJECT_LOADER_IMPL_H
diff --git a/lib/ExecutionEngine/OutputFile.cpp b/lib/ExecutionEngine/OutputFile.cpp
new file mode 100644
index 0000000..6200617
--- /dev/null
+++ b/lib/ExecutionEngine/OutputFile.cpp
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "OutputFile.h"
+
+#include <cstdlib>
+
+#include <llvm/Support/raw_ostream.h>
+
+#include "DebugHelper.h"
+
+using namespace bcc;
+
+OutputFile *OutputFile::CreateTemporary(const std::string &pFileTemplate,
+ unsigned pFlags) {
+ char *tmp_filename = NULL;
+ int tmp_fd;
+ OutputFile *result = NULL;
+
+ // Allocate memory to hold the generated unique temporary filename.
+ tmp_filename =
+ new (std::nothrow) char [ pFileTemplate.length() + /* .XXXXXX */7 + 1 ];
+ if (tmp_filename == NULL) {
+ ALOGE("Out of memory when allocates memory for filename %s in "
+ "OutputFile::CreateTemporary()!", pFileTemplate.c_str());
+ return NULL;
+ }
+
+ // Construct filename template for mkstemp().
+ if (pFileTemplate.length() > 0)
+ ::memcpy(tmp_filename, pFileTemplate.c_str(), pFileTemplate.length());
+ ::strncpy(tmp_filename + pFileTemplate.length(), ".XXXXXX", 7);
+
+ // POSIX mkstemp() never returns EINTR.
+ tmp_fd = ::mkstemp(tmp_filename);
+ if (tmp_fd < 0) {
+ llvm::error_code err(errno, llvm::posix_category());
+ ALOGE("Failed to create temporary file using mkstemp() for %s! (%s)",
+ tmp_filename, err.message().c_str());
+ delete [] tmp_filename;
+ return NULL;
+ }
+
+ // Create result OutputFile.
+ result = new (std::nothrow) OutputFile(tmp_filename, pFlags);
+ if (result == NULL) {
+ ALOGE("Out of memory when creates OutputFile for %s!", tmp_filename);
+ // Fall through to the clean-up codes.
+ } else {
+ if (result->hasError()) {
+ ALOGE("Failed to open temporary output file %s! (%s)",
+ result->getName().c_str(), result->getErrorMessage().c_str());
+ delete result;
+ result = NULL;
+ // Fall through to the clean-up codes.
+ }
+ }
+
+ // Clean up.
+ delete [] tmp_filename;
+ ::close(tmp_fd);
+
+ return result;
+}
+
+OutputFile::OutputFile(const std::string &pFilename, unsigned pFlags)
+ : super(pFilename, pFlags) { }
+
+ssize_t OutputFile::write(const void *pBuf, size_t count) {
+ if ((mFD < 0) || hasError()) {
+ return -1;
+ }
+
+ if ((count <= 0) || (pBuf == NULL)) {
+ // Keep safe and issue a warning.
+ ALOGW("OutputFile::write: count = %zu, buffer = %p", count, pBuf);
+ return 0;
+ }
+
+ while (count > 0) {
+ ssize_t write_size = ::write(mFD, pBuf, count);
+
+ if (write_size > 0) {
+ return write_size;
+ } else if ((errno == EAGAIN) || (errno == EINTR)) {
+ // If the errno is EAGAIN or EINTR, then we try to write again.
+ //
+ // Fall-through
+ } else {
+ detectError();
+ return -1;
+ }
+ }
+ // unreachable
+ return 0;
+}
+
+void OutputFile::truncate() {
+ if (mFD < 0) {
+ return;
+ }
+
+ do {
+ if (::ftruncate(mFD, 0) == 0) {
+ return;
+ }
+ } while (errno == EINTR);
+ detectError();
+
+ return;
+}
+
+llvm::raw_fd_ostream *OutputFile::dup() {
+ int newfd;
+
+ do {
+ newfd = ::dup(mFD);
+ if (newfd < 0) {
+ if (errno != EINTR) {
+ detectError();
+ return NULL;
+ }
+ // EINTR
+ continue;
+ }
+ // dup() returns ok.
+ break;
+ } while (true);
+
+ llvm::raw_fd_ostream *result =
+ new (std::nothrow) llvm::raw_fd_ostream(newfd, /* shouldClose */true);
+
+ if (result == NULL) {
+ mError.assign(llvm::errc::not_enough_memory, llvm::system_category());
+ }
+
+ return result;
+}
diff --git a/lib/ExecutionEngine/OutputFile.h b/lib/ExecutionEngine/OutputFile.h
new file mode 100644
index 0000000..4de8863
--- /dev/null
+++ b/lib/ExecutionEngine/OutputFile.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_OUTPUT_FILE_H
+#define BCC_EXECUTION_ENGINE_OUTPUT_FILE_H
+
+#include "File.h"
+#include "FileBase.h"
+
+namespace llvm {
+ class raw_fd_ostream;
+}
+
+namespace bcc {
+
+class OutputFile : public File<FileBase::kWriteMode> {
+ typedef File<FileBase::kWriteMode> super;
+public:
+ // Generate a unique temporary filename from pFileTemplate and open it in
+ // an OutputFile returned. The filename will be pFileTemplate with
+ // a dot ('.') plus six random characters appended. Return NULL on error.
+ static OutputFile *CreateTemporary(const std::string &pFileTemplate,
+ unsigned pFlags);
+
+ OutputFile(const std::string &pFilename, unsigned pFlags = 0);
+
+ ssize_t write(const void *pBuf, size_t count);
+
+ void truncate();
+
+ // This is similar to the system call dup(). It creates a copy of the file
+ // descriptor it contains and wrap it in llvm::raw_fd_ostream object. It
+ // returns a non-NULL object if everything goes well and user should later
+ // use delete operator to destroy it by itself.
+ llvm::raw_fd_ostream *dup();
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_OUTPUT_FILE_H
diff --git a/lib/ExecutionEngine/RSCompiler.cpp b/lib/ExecutionEngine/RSCompiler.cpp
new file mode 100644
index 0000000..e431086
--- /dev/null
+++ b/lib/ExecutionEngine/RSCompiler.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RSCompiler.h"
+
+#include <llvm/Module.h>
+#include <llvm/PassManager.h>
+#include <llvm/Transforms/IPO.h>
+
+#include "DebugHelper.h"
+#include "RSExecutable.h"
+#include "RSInfo.h"
+#include "RSScript.h"
+#include "RSTransforms.h"
+#include "Source.h"
+
+using namespace bcc;
+
+bool RSCompiler::beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM) {
+ // Add a pass to internalize the symbols that don't need to have global
+ // visibility.
+ RSScript &script = static_cast<RSScript &>(pScript);
+ const RSInfo *info = script.getInfo();
+
+ // The vector contains the symbols that should not be internalized.
+ std::vector<const char *> export_symbols;
+
+ // Special RS functions should always be global symbols.
+ const char **special_functions = RSExecutable::SpecialFunctionNames;
+ while (*special_functions != NULL) {
+ export_symbols.push_back(*special_functions);
+ special_functions++;
+ }
+
+ // Visibility of symbols appeared in rs_export_var and rs_export_func should
+ // also be preserved.
+ const RSInfo::ExportVarNameListTy &export_vars = info->getExportVarNames();
+ const RSInfo::ExportFuncNameListTy &export_funcs = info->getExportFuncNames();
+
+ for (RSInfo::ExportVarNameListTy::const_iterator
+ export_var_iter = export_vars.begin(),
+ export_var_end = export_vars.end();
+ export_var_iter != export_var_end; export_var_iter++) {
+ export_symbols.push_back(*export_var_iter);
+ }
+
+ for (RSInfo::ExportFuncNameListTy::const_iterator
+ export_func_iter = export_funcs.begin(),
+ export_func_end = export_funcs.end();
+ export_func_iter != export_func_end; export_func_iter++) {
+ export_symbols.push_back(*export_func_iter);
+ }
+
+ // Expanded foreach functions should not be internalized, too.
+ const RSInfo::ExportForeachFuncListTy &export_foreach_func =
+ info->getExportForeachFuncs();
+ std::vector<std::string> expanded_foreach_funcs;
+ for (RSInfo::ExportForeachFuncListTy::const_iterator
+ foreach_func_iter = export_foreach_func.begin(),
+ foreach_func_end = export_foreach_func.end();
+ foreach_func_iter != foreach_func_end; foreach_func_iter++) {
+ std::string name(foreach_func_iter->first);
+ expanded_foreach_funcs.push_back(name.append(".expand"));
+ }
+
+ // Need to wait until ForEachExpandList is fully populated to fill in
+ // exported symbols.
+ for (size_t i = 0; i < expanded_foreach_funcs.size(); i++) {
+ export_symbols.push_back(expanded_foreach_funcs[i].c_str());
+ }
+
+ pPM.add(llvm::createInternalizePass(export_symbols));
+
+ return true;
+}
+
+bool RSCompiler::beforeExecuteLTOPasses(Script &pScript,
+ llvm::PassManager &pPM) {
+ // Execute a pass to expand foreach-able functions
+ llvm::PassManager rs_passes;
+
+ // Script passed to RSCompiler must be a RSScript.
+ RSScript &script = static_cast<RSScript &>(pScript);
+ const RSInfo *info = script.getInfo();
+ llvm::Module &module = script.getSource().getModule();
+
+ if (info == NULL) {
+ ALOGE("Missing RSInfo in RSScript to run the pass for foreach expansion on "
+ "%s!", module.getModuleIdentifier().c_str());
+ return false;
+ }
+
+ // Expand ForEach on CPU path to reduce launch overhead.
+ rs_passes.add(createRSForEachExpandPass(info->getExportForeachFuncs()));
+
+ // Execute the pass.
+ rs_passes.run(module);
+
+ return true;
+}
diff --git a/lib/ExecutionEngine/RSCompiler.h b/lib/ExecutionEngine/RSCompiler.h
new file mode 100644
index 0000000..227727b
--- /dev/null
+++ b/lib/ExecutionEngine/RSCompiler.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_RS_COMPILER_H
+#define BCC_EXECUTION_ENGINE_RS_COMPILER_H
+
+#include "Compiler.h"
+
+namespace bcc {
+
+class RSCompiler : public Compiler {
+private:
+ virtual bool beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM);
+ virtual bool beforeExecuteLTOPasses(Script &pScript, llvm::PassManager &pPM);
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_COMPILER_H
diff --git a/lib/ExecutionEngine/RSCompilerDriver.cpp b/lib/ExecutionEngine/RSCompilerDriver.cpp
new file mode 100644
index 0000000..ceb4f93
--- /dev/null
+++ b/lib/ExecutionEngine/RSCompilerDriver.cpp
@@ -0,0 +1,295 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RSCompilerDriver.h"
+
+#include "CompilerConfig.h"
+#include "TargetCompilerConfigs.h"
+#include "DebugHelper.h"
+#include "FileMutex.h"
+#include "InputFile.h"
+#include "Initialization.h"
+#include "OutputFile.h"
+#include "RSExecutable.h"
+
+#include <cutils/properties.h>
+#include <utils/String8.h>
+
+using namespace bcc;
+
+namespace {
+
+bool is_force_recompile() {
+ char buf[PROPERTY_VALUE_MAX];
+
+ property_get("debug.rs.forcerecompile", buf, "0");
+ if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+} // end anonymous namespace
+
+RSCompilerDriver::RSCompilerDriver() : mConfig(NULL), mCompiler() {
+ init::Initialize();
+ // Chain the symbol resolvers for BCC runtimes and RS runtimes.
+ mResolver.chainResolver(mBCCRuntime);
+ mResolver.chainResolver(mRSRuntime);
+}
+
+RSCompilerDriver::~RSCompilerDriver() {
+ delete mConfig;
+}
+
+RSExecutable *RSCompilerDriver::loadScriptCache(const RSScript &pScript,
+ const std::string &pOutputPath){
+ RSExecutable *result = NULL;
+
+ if (is_force_recompile())
+ return NULL;
+
+ //===--------------------------------------------------------------------===//
+ // Acquire the read lock for reading output object file.
+ //===--------------------------------------------------------------------===//
+ FileMutex<FileBase::kReadLock> read_output_mutex(pOutputPath);
+
+ if (read_output_mutex.hasError() || !read_output_mutex.lock()) {
+ ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath.c_str(),
+ read_output_mutex.getErrorMessage().c_str());
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Read the output object file.
+ //===--------------------------------------------------------------------===//
+ InputFile *output_file = new (std::nothrow) InputFile(pOutputPath);
+
+ if ((output_file == NULL) || output_file->hasError()) {
+ ALOGE("Unable to open the %s for read! (%s)", pOutputPath.c_str(),
+ output_file->getErrorMessage().c_str());
+ delete output_file;
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Acquire the read lock on output_file for reading its RS info file.
+ //===--------------------------------------------------------------------===//
+ android::String8 info_path = RSInfo::GetPath(*output_file);
+
+ if (!output_file->lock()) {
+ ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)",
+ pOutputPath.c_str(), info_path.string(),
+ output_file->getErrorMessage().c_str());
+ delete output_file;
+ return NULL;
+ }
+
+ //===---------------------------------------------------------------------===//
+ // Open and load the RS info file.
+ //===--------------------------------------------------------------------===//
+ InputFile info_file(info_path.string());
+ RSInfo *info = RSInfo::ReadFromFile(info_file,
+ pScript.getSourceDependencies());
+
+ // Release the lock on output_file.
+ output_file->unlock();
+
+ if (info == NULL) {
+ delete output_file;
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Create the RSExecutable.
+ //===--------------------------------------------------------------------===//
+ result = RSExecutable::Create(*info, *output_file, mResolver);
+ if (result == NULL) {
+ delete output_file;
+ delete info;
+ return NULL;
+ }
+
+ // TODO: Dirty hack for libRS. This can be removed once RSExecutable is public
+ // to libRS.
+ if (!result->isThreadable()) {
+ mRSRuntime.getAddress("__clearThreadable");
+ }
+
+ return result;
+}
+
+bool RSCompilerDriver::setupConfig(const RSScript &pScript) {
+ bool changed = false;
+
+ const llvm::CodeGenOpt::Level script_opt_level =
+ static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel());
+
+ if (mConfig != NULL) {
+ // Renderscript bitcode may have their optimization flag configuration
+ // different than the previous run of RS compilation.
+ if (mConfig->getOptimizationLevel() != script_opt_level) {
+ mConfig->setOptimizationLevel(script_opt_level);
+ changed = true;
+ }
+ } else {
+ // Haven't run the compiler ever.
+ mConfig = new (std::nothrow) DefaultCompilerConfig();
+ if (mConfig == NULL) {
+ // Return false since mConfig remains NULL and out-of-memory.
+ return false;
+ }
+ mConfig->setOptimizationLevel(script_opt_level);
+ changed = true;
+ }
+
+#if defined(DEFAULT_ARM_CODEGEN)
+ // NEON should be disable when full-precision floating point is required.
+ assert((pScript.getInfo() != NULL) && "NULL RS info!");
+ if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::Full) {
+ // Must be ARMCompilerConfig.
+ ARMCompilerConfig *arm_config = static_cast<ARMCompilerConfig *>(mConfig);
+ changed |= arm_config->enableNEON(/* pEnable */false);
+ }
+#endif
+
+ return changed;
+}
+
+RSExecutable *RSCompilerDriver::compileScript(RSScript &pScript,
+ const std::string &pOutputPath) {
+ RSExecutable *result = NULL;
+ RSInfo *info = NULL;
+
+ //===--------------------------------------------------------------------===//
+ // Extract RS-specific information from source bitcode.
+ //===--------------------------------------------------------------------===//
+ // RS info may contains configuration (such as #optimization_level) to the
+ // compiler therefore it should be extracted before compilation.
+ info = RSInfo::ExtractFromSource(pScript.getSource(),
+ pScript.getSourceDependencies());
+ if (info == NULL) {
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Associate script with its info
+ //===--------------------------------------------------------------------===//
+ // This is required since RS compiler may need information in the info file
+ // to do some transformation (e.g., expand foreach-able function.)
+ pScript.setInfo(info);
+
+ //===--------------------------------------------------------------------===//
+ // Acquire the write lock for writing output object file.
+ //===--------------------------------------------------------------------===//
+ FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
+
+ if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
+ ALOGE("Unable to acquire the lock for writing %s! (%s)",
+ pOutputPath.c_str(), write_output_mutex.getErrorMessage().c_str());
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Open the output file for write.
+ //===--------------------------------------------------------------------===//
+ OutputFile *output_file = new (std::nothrow) OutputFile(pOutputPath);
+
+ if ((output_file == NULL) || output_file->hasError()) {
+ ALOGE("Unable to open the %s for write! (%s)", pOutputPath.c_str(),
+ output_file->getErrorMessage().c_str());
+ delete info;
+ delete output_file;
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Setup the config to the compiler.
+ //===--------------------------------------------------------------------===//
+ bool compiler_need_reconfigure = setupConfig(pScript);
+
+ if (mConfig == NULL) {
+ ALOGE("Failed to setup config for RS compiler to compile %s!",
+ pOutputPath.c_str());
+ delete info;
+ delete output_file;
+ return NULL;
+ }
+
+ // Compiler need to re-config if it's haven't run the config() yet or the
+ // configuration it referenced is changed.
+ if (compiler_need_reconfigure) {
+ Compiler::ErrorCode err = mCompiler.config(*mConfig);
+ if (err != Compiler::kSuccess) {
+ ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath.c_str(),
+ Compiler::GetErrorString(err));
+ delete info;
+ delete output_file;
+ return NULL;
+ }
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Run the compiler.
+ //===--------------------------------------------------------------------===//
+ Compiler::ErrorCode compile_result = mCompiler.compile(pScript, *output_file);
+ if (compile_result != Compiler::kSuccess) {
+ ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath.c_str(),
+ Compiler::GetErrorString(compile_result));
+ delete info;
+ delete output_file;
+ return NULL;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Create the RSExecutable.
+ //===--------------------------------------------------------------------===//
+ result = RSExecutable::Create(*info, *output_file, mResolver);
+ if (result == NULL) {
+ delete info;
+ delete output_file;
+ return NULL;
+ }
+
+ // TODO: Dirty hack for libRS. This can be removed once RSExecutable is public
+ // to libRS.
+ result->setThreadable(mRSRuntime.getAddress("__isThreadable") != NULL);
+
+ //===--------------------------------------------------------------------===//
+ // Write out the RS info file.
+ //===--------------------------------------------------------------------===//
+ // Note that write failure only results in a warning since the source is
+ // successfully compiled and loaded.
+ if (!result->syncInfo(/* pForce */true)) {
+ ALOGW("%s was successfully compiled and loaded but its RS info file failed "
+ "to write out!", pOutputPath.c_str());
+ }
+
+ return result;
+}
+
+RSExecutable *RSCompilerDriver::build(RSScript &pScript,
+ const std::string &pOutputPath) {
+ RSExecutable *result = loadScriptCache(pScript, pOutputPath);
+
+ if (result != NULL) {
+ // Cache hit
+ return result;
+ }
+
+ return compileScript(pScript, pOutputPath);
+}
diff --git a/lib/ExecutionEngine/RSCompilerDriver.h b/lib/ExecutionEngine/RSCompilerDriver.h
new file mode 100644
index 0000000..8bc9298
--- /dev/null
+++ b/lib/ExecutionEngine/RSCompilerDriver.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_RS_COMPILER_DRIVER_H
+#define BCC_EXECUTION_ENGINE_RS_COMPILER_DRIVER_H
+
+#include <string>
+
+#include "BCCRuntimeSymbolResolver.h"
+#include "RSCompiler.h"
+#include "SymbolResolvers.h"
+#include "SymbolResolverProxy.h"
+
+namespace bcc {
+
+class CompilerConfig;
+class RSExecutable;
+class RSScript;
+
+class RSCompilerDriver {
+private:
+ CompilerConfig *mConfig;
+ RSCompiler mCompiler;
+
+ BCCRuntimeSymbolResolver mBCCRuntime;
+ LookupFunctionSymbolResolver<void*> mRSRuntime;
+ SymbolResolverProxy mResolver;
+
+ RSExecutable *loadScriptCache(const RSScript &pScript,
+ const std::string &pOutputPath);
+
+ // Setup the compiler config for the given script. Return true if mConfig has
+ // been changed and false if it remains unchanged.
+ bool setupConfig(const RSScript &pScript);
+
+ RSExecutable *compileScript(RSScript &pScript,
+ const std::string &pOutputPath);
+
+public:
+ RSCompilerDriver();
+ ~RSCompilerDriver();
+
+ inline void setRSRuntimeLookupFunction(
+ LookupFunctionSymbolResolver<>::LookupFunctionTy pLookupFunc)
+ { mRSRuntime.setLookupFunction(pLookupFunc); }
+ inline void setRSRuntimeLookupContext(void *pContext)
+ { mRSRuntime.setContext(pContext); }
+
+ // FIXME: This method accompany with loadScriptCache and compileScript should
+ // all be const-methods. They're not now because the getAddress() in
+ // SymbolResolverInterface is not a const-method.
+ RSExecutable *build(RSScript &pScript,
+ const std::string &pOutputPath);
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_COMPILER_DRIVER_H
diff --git a/lib/ExecutionEngine/RSExecutable.cpp b/lib/ExecutionEngine/RSExecutable.cpp
new file mode 100644
index 0000000..46ec726
--- /dev/null
+++ b/lib/ExecutionEngine/RSExecutable.cpp
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RSExecutable.h"
+
+#include "DebugHelper.h"
+#include "FileBase.h"
+#include "OutputFile.h"
+#include "SymbolResolverProxy.h"
+
+#include <utils/String8.h>
+
+using namespace bcc;
+
+const char *RSExecutable::SpecialFunctionNames[] = {
+ "root",
+ "init",
+ ".rs.dtor",
+ // Must be NULL-terminated.
+ NULL
+};
+
+RSExecutable *RSExecutable::Create(RSInfo &pInfo,
+ FileBase &pObjFile,
+ SymbolResolverProxy &pResolver) {
+ // Load the object file. Enable the GDB's JIT debugging if the script contains
+ // debug information.
+ ObjectLoader *loader = ObjectLoader::Load(pObjFile,
+ pResolver,
+ pInfo.hasDebugInformation());
+ if (loader == NULL) {
+ return NULL;
+ }
+
+ // Now, all things required to build a RSExecutable object are ready.
+ RSExecutable *result = new (std::nothrow) RSExecutable(pInfo,
+ pObjFile,
+ *loader);
+ if (result == NULL) {
+ ALOGE("Out of memory when create object to hold RS result file for %s!",
+ pObjFile.getName().c_str());
+ return NULL;
+ }
+
+ unsigned idx;
+ // Resolve addresses of RS export vars.
+ idx = 0;
+ const RSInfo::ExportVarNameListTy &export_var_names =
+ pInfo.getExportVarNames();
+ for (RSInfo::ExportVarNameListTy::const_iterator
+ var_iter = export_var_names.begin(),
+ var_end = export_var_names.end(); var_iter != var_end;
+ var_iter++, idx++) {
+ const char *name = *var_iter;
+ void *addr = result->getSymbolAddress(name);
+ if (addr == NULL) {
+ ALOGW("RS export var at entry #%u named %s cannot be found in the result "
+ "object!", idx, name);
+ }
+ result->mExportVarAddrs.push_back(addr);
+ }
+
+ // Resolve addresses of RS export functions.
+ idx = 0;
+ const RSInfo::ExportFuncNameListTy &export_func_names =
+ pInfo.getExportFuncNames();
+ for (RSInfo::ExportFuncNameListTy::const_iterator
+ func_iter = export_func_names.begin(),
+ func_end = export_func_names.end(); func_iter != func_end;
+ func_iter++, idx++) {
+ const char *name = *func_iter;
+ void *addr = result->getSymbolAddress(name);
+ if (addr == NULL) {
+ ALOGW("RS export func at entry #%u named %s cannot be found in the result"
+ " object!", idx, name);
+ }
+ result->mExportFuncAddrs.push_back(addr);
+ }
+
+ // Resolve addresses of expanded RS foreach function.
+ idx = 0;
+ const RSInfo::ExportForeachFuncListTy &export_foreach_funcs =
+ pInfo.getExportForeachFuncs();
+ for (RSInfo::ExportForeachFuncListTy::const_iterator
+ foreach_iter = export_foreach_funcs.begin(),
+ foreach_end = export_foreach_funcs.end();
+ foreach_iter != foreach_end; foreach_iter++, idx++) {
+ const char *func_name = foreach_iter->first;
+ android::String8 expanded_func_name(func_name);
+ expanded_func_name.append(".expand");
+ void *addr = result->getSymbolAddress(expanded_func_name.string());
+ if (addr == NULL) {
+ ALOGW("Expanded RS foreach at entry #%u named %s cannot be found in the "
+ "result object!", idx, expanded_func_name.string());
+ }
+ result->mExportForeachFuncAddrs.push_back(addr);
+ }
+
+ // Copy pragma key/value pairs from RSInfo::getPragmas() into mPragmaKeys and
+ // mPragmaValues, respectively.
+ const RSInfo::PragmaListTy &pragmas = pInfo.getPragmas();
+ for (RSInfo::PragmaListTy::const_iterator pragma_iter = pragmas.begin(),
+ pragma_end = pragmas.end(); pragma_iter != pragma_end;
+ pragma_iter++){
+ result->mPragmaKeys.push_back(pragma_iter->first);
+ result->mPragmaValues.push_back(pragma_iter->second);
+ }
+
+ return result;
+}
+
+bool RSExecutable::syncInfo(bool pForce) {
+ if (!pForce && !mIsInfoDirty) {
+ return true;
+ }
+
+ android::String8 info_path = RSInfo::GetPath(*mObjFile);
+ OutputFile info_file(info_path.string());
+
+ if (info_file.hasError()) {
+ ALOGE("Failed to open the info file %s for write! (%s)", info_path.string(),
+ info_file.getErrorMessage().c_str());
+ return false;
+ }
+
+ // Operation to the RS info file need to acquire the lock on the output file
+ // first.
+ if (!mObjFile->lock(FileBase::kWriteLock)) {
+ ALOGE("Write to RS info file %s required the acquisition of the write lock "
+ "on %s but got failure! (%s)", info_path.string(),
+ mObjFile->getName().c_str(), info_file.getErrorMessage().c_str());
+ return false;
+ }
+
+ // Perform the write.
+ if (!mInfo->write(info_file)) {
+ ALOGE("Failed to sync the RS info file %s!", info_path.string());
+ mObjFile->unlock();
+ return false;
+ }
+
+ mObjFile->unlock();
+ mIsInfoDirty = false;
+ return true;
+}
+
+RSExecutable::~RSExecutable() {
+ syncInfo();
+ delete mInfo;
+ delete mObjFile;
+ delete mLoader;
+}
diff --git a/lib/ExecutionEngine/RSExecutable.h b/lib/ExecutionEngine/RSExecutable.h
new file mode 100644
index 0000000..12e0f0a
--- /dev/null
+++ b/lib/ExecutionEngine/RSExecutable.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_RS_EXECUTABLE_H
+#define BCC_EXECUTION_ENGINE_RS_EXECUTABLE_H
+
+#include <cstddef>
+
+#include "DebugHelper.h"
+#include "ObjectLoader.h"
+#include "RSInfo.h"
+
+#include <utils/Vector.h>
+
+namespace bcc {
+
+class FileBase;
+class SymbolResolverProxy;
+
+/*
+ * RSExecutable holds the build results of a RSScript.
+ */
+class RSExecutable {
+private:
+ RSInfo *mInfo;
+ bool mIsInfoDirty;
+
+ FileBase *mObjFile;
+
+ ObjectLoader *mLoader;
+
+ // Memory address of rs export stuffs
+ android::Vector<void *> mExportVarAddrs;
+ android::Vector<void *> mExportFuncAddrs;
+ android::Vector<void *> mExportForeachFuncAddrs;
+
+ // FIXME: These are designed for RenderScript HAL and is initialized in
+ // RSExecutable::Create(). Both of them come from RSInfo::getPragmas().
+ // If possible, read the pragma key/value pairs directly from RSInfo.
+ android::Vector<const char *> mPragmaKeys;
+ android::Vector<const char *> mPragmaValues;
+
+ RSExecutable(RSInfo &pInfo, FileBase &pObjFile, ObjectLoader &pLoader)
+ : mInfo(&pInfo), mIsInfoDirty(false), mObjFile(&pObjFile), mLoader(&pLoader)
+ { }
+
+public:
+ // This is a NULL-terminated string array which specifies "Special" functions
+ // in RenderScript (e.g., root().)
+ static const char *SpecialFunctionNames[];
+
+ // Return NULL on error. If the return object is non-NULL, it claims the
+ // ownership of pInfo and pObjFile.
+ static RSExecutable *Create(RSInfo &pInfo,
+ FileBase &pObjFile,
+ SymbolResolverProxy &pResolver);
+
+ inline const RSInfo &getInfo() const
+ { return *mInfo; }
+
+ // Interfaces to RSInfo
+ inline bool isThreadable() const
+ { return mInfo->isThreadable(); }
+
+ inline void setThreadable(bool pThreadable = true) {
+ if (mInfo->isThreadable() != pThreadable) {
+ mInfo->setThreadable(pThreadable);
+ mIsInfoDirty = true;
+ }
+ return;
+ }
+
+ // Interfaces to ObjectLoader
+ inline void *getSymbolAddress(const char *pName) const
+ { return mLoader->getSymbolAddress(pName); }
+
+ bool syncInfo(bool pForce = false);
+
+ inline const android::Vector<void *> &getExportVarAddrs() const
+ { return mExportVarAddrs; }
+ inline const android::Vector<void *> &getExportFuncAddrs() const
+ { return mExportFuncAddrs; }
+ inline const android::Vector<void *> &getExportForeachFuncAddrs() const
+ { return mExportForeachFuncAddrs; }
+
+ inline const android::Vector<const char *> &getPragmaKeys() const
+ { return mPragmaKeys; }
+ inline const android::Vector<const char *> &getPragmaValues() const
+ { return mPragmaValues; }
+
+ ~RSExecutable();
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_EXECUTABLE_H
diff --git a/lib/ExecutionEngine/RSForEachExpand.cpp b/lib/ExecutionEngine/RSForEachExpand.cpp
new file mode 100644
index 0000000..b30dd23
--- /dev/null
+++ b/lib/ExecutionEngine/RSForEachExpand.cpp
@@ -0,0 +1,390 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define RS_FOREACH_EXPAND_PASS_NDEBUG 0
+#include "RSTransforms.h"
+
+#include <cstdlib>
+
+#include "Config.h"
+
+#include "DebugHelper.h"
+#include "RSInfo.h"
+
+#include "llvm/DerivedTypes.h"
+#include "llvm/Function.h"
+#include "llvm/Instructions.h"
+#include "llvm/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Type.h"
+#include "llvm/Support/IRBuilder.h"
+
+using namespace bcc;
+
+namespace {
+
+/* RSForEachExpandPass - This pass operates on functions that are able to be
+ * called via rsForEach() or "foreach_<NAME>". We create an inner loop for the
+ * ForEach-able function to be invoked over the appropriate data cells of the
+ * input/output allocations (adjusting other relevant parameters as we go). We
+ * support doing this for any ForEach-able compute kernels. The new function
+ * name is the original function name followed by ".expand". Note that we
+ * still generate code for the original function.
+ */
+class RSForEachExpandPass : public llvm::ModulePass {
+private:
+ static char ID;
+
+ llvm::Module *M;
+ llvm::LLVMContext *C;
+
+ const RSInfo::ExportForeachFuncListTy &mFuncs;
+
+ uint32_t getRootSignature(llvm::Function *F) {
+ const llvm::NamedMDNode *ExportForEachMetadata =
+ M->getNamedMetadata("#rs_export_foreach");
+
+ if (!ExportForEachMetadata) {
+ llvm::SmallVector<llvm::Type*, 8> RootArgTys;
+ for (llvm::Function::arg_iterator B = F->arg_begin(),
+ E = F->arg_end();
+ B != E;
+ ++B) {
+ RootArgTys.push_back(B->getType());
+ }
+
+ // For pre-ICS bitcode, we may not have signature information. In that
+ // case, we use the size of the RootArgTys to select the number of
+ // arguments.
+ return (1 << RootArgTys.size()) - 1;
+ }
+
+#if !RS_FOREACH_EXPAND_PASS_NDEBUG
+ if (ExportForEachMetadata->getNumOperands() <= 0) {
+ ALOGE("Assert failed at %s:%d: Invalid #rs_export_foreach metadata in "
+ " '%s'!", __FILE__, __LINE__, M->getModuleIdentifier().c_str());
+ ::abort();
+ }
+#endif
+
+ // We only handle the case for legacy root() functions here, so this is
+ // hard-coded to look at only the first such function.
+ llvm::MDNode *SigNode = ExportForEachMetadata->getOperand(0);
+ if (SigNode != NULL && SigNode->getNumOperands() == 1) {
+ llvm::Value *SigVal = SigNode->getOperand(0);
+ if (SigVal->getValueID() == llvm::Value::MDStringVal) {
+ llvm::StringRef SigString =
+ static_cast<llvm::MDString*>(SigVal)->getString();
+ uint32_t Signature = 0;
+ if (SigString.getAsInteger(10, Signature)) {
+ ALOGE("Non-integer signature value '%s'", SigString.str().c_str());
+ return 0;
+ }
+ return Signature;
+ }
+ }
+
+ return 0;
+ }
+
+ static bool hasIn(uint32_t Signature) {
+ return Signature & 1;
+ }
+
+ static bool hasOut(uint32_t Signature) {
+ return Signature & 2;
+ }
+
+ static bool hasUsrData(uint32_t Signature) {
+ return Signature & 4;
+ }
+
+ static bool hasX(uint32_t Signature) {
+ return Signature & 8;
+ }
+
+ static bool hasY(uint32_t Signature) {
+ return Signature & 16;
+ }
+
+public:
+ RSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs)
+ : ModulePass(ID), M(NULL), C(NULL), mFuncs(pForeachFuncs) {
+ }
+
+ /* Performs the actual optimization on a selected function. On success, the
+ * Module will contain a new function of the name "<NAME>.expand" that
+ * invokes <NAME>() in a loop with the appropriate parameters.
+ */
+ bool ExpandFunction(llvm::Function *F, uint32_t Signature) {
+ ALOGV("Expanding ForEach-able Function %s", F->getName().str().c_str());
+
+ if (!Signature) {
+ Signature = getRootSignature(F);
+ if (!Signature) {
+ // We couldn't determine how to expand this function based on its
+ // function signature.
+ return false;
+ }
+ }
+
+ llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*C);
+ llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
+ llvm::Type *SizeTy = Int32Ty;
+
+ /* Defined in frameworks/base/libs/rs/rs_hal.h:
+ *
+ * struct RsForEachStubParamStruct {
+ * const void *in;
+ * void *out;
+ * const void *usr;
+ * size_t usr_len;
+ * uint32_t x;
+ * uint32_t y;
+ * uint32_t z;
+ * uint32_t lod;
+ * enum RsAllocationCubemapFace face;
+ * uint32_t ar[16];
+ * };
+ */
+ llvm::SmallVector<llvm::Type*, 9> StructTys;
+ StructTys.push_back(VoidPtrTy); // const void *in
+ StructTys.push_back(VoidPtrTy); // void *out
+ StructTys.push_back(VoidPtrTy); // const void *usr
+ StructTys.push_back(SizeTy); // size_t usr_len
+ StructTys.push_back(Int32Ty); // uint32_t x
+ StructTys.push_back(Int32Ty); // uint32_t y
+ StructTys.push_back(Int32Ty); // uint32_t z
+ StructTys.push_back(Int32Ty); // uint32_t lod
+ StructTys.push_back(Int32Ty); // enum RsAllocationCubemapFace
+ StructTys.push_back(llvm::ArrayType::get(Int32Ty, 16)); // uint32_t ar[16]
+
+ llvm::Type *ForEachStubPtrTy = llvm::StructType::create(
+ StructTys, "RsForEachStubParamStruct")->getPointerTo();
+
+ /* Create the function signature for our expanded function.
+ * void (const RsForEachStubParamStruct *p, uint32_t x1, uint32_t x2,
+ * uint32_t instep, uint32_t outstep)
+ */
+ llvm::SmallVector<llvm::Type*, 8> ParamTys;
+ ParamTys.push_back(ForEachStubPtrTy); // const RsForEachStubParamStruct *p
+ ParamTys.push_back(Int32Ty); // uint32_t x1
+ ParamTys.push_back(Int32Ty); // uint32_t x2
+ ParamTys.push_back(Int32Ty); // uint32_t instep
+ ParamTys.push_back(Int32Ty); // uint32_t outstep
+
+ llvm::FunctionType *FT =
+ llvm::FunctionType::get(llvm::Type::getVoidTy(*C), ParamTys, false);
+ llvm::Function *ExpandedFunc =
+ llvm::Function::Create(FT,
+ llvm::GlobalValue::ExternalLinkage,
+ F->getName() + ".expand", M);
+
+ // Create and name the actual arguments to this expanded function.
+ llvm::SmallVector<llvm::Argument*, 8> ArgVec;
+ for (llvm::Function::arg_iterator B = ExpandedFunc->arg_begin(),
+ E = ExpandedFunc->arg_end();
+ B != E;
+ ++B) {
+ ArgVec.push_back(B);
+ }
+
+ if (ArgVec.size() != 5) {
+ ALOGE("Incorrect number of arguments to function: %zu",
+ ArgVec.size());
+ return false;
+ }
+ llvm::Value *Arg_p = ArgVec[0];
+ llvm::Value *Arg_x1 = ArgVec[1];
+ llvm::Value *Arg_x2 = ArgVec[2];
+ llvm::Value *Arg_instep = ArgVec[3];
+ llvm::Value *Arg_outstep = ArgVec[4];
+
+ Arg_p->setName("p");
+ Arg_x1->setName("x1");
+ Arg_x2->setName("x2");
+ Arg_instep->setName("instep");
+ Arg_outstep->setName("outstep");
+
+ // Construct the actual function body.
+ llvm::BasicBlock *Begin =
+ llvm::BasicBlock::Create(*C, "Begin", ExpandedFunc);
+ llvm::IRBuilder<> Builder(Begin);
+
+ // uint32_t X = x1;
+ llvm::AllocaInst *AX = Builder.CreateAlloca(Int32Ty, 0, "AX");
+ Builder.CreateStore(Arg_x1, AX);
+
+ // Collect and construct the arguments for the kernel().
+ // Note that we load any loop-invariant arguments before entering the Loop.
+ llvm::Function::arg_iterator Args = F->arg_begin();
+
+ llvm::Type *InTy = NULL;
+ llvm::AllocaInst *AIn = NULL;
+ if (hasIn(Signature)) {
+ InTy = Args->getType();
+ AIn = Builder.CreateAlloca(InTy, 0, "AIn");
+ Builder.CreateStore(Builder.CreatePointerCast(Builder.CreateLoad(
+ Builder.CreateStructGEP(Arg_p, 0)), InTy), AIn);
+ Args++;
+ }
+
+ llvm::Type *OutTy = NULL;
+ llvm::AllocaInst *AOut = NULL;
+ if (hasOut(Signature)) {
+ OutTy = Args->getType();
+ AOut = Builder.CreateAlloca(OutTy, 0, "AOut");
+ Builder.CreateStore(Builder.CreatePointerCast(Builder.CreateLoad(
+ Builder.CreateStructGEP(Arg_p, 1)), OutTy), AOut);
+ Args++;
+ }
+
+ llvm::Value *UsrData = NULL;
+ if (hasUsrData(Signature)) {
+ llvm::Type *UsrDataTy = Args->getType();
+ UsrData = Builder.CreatePointerCast(Builder.CreateLoad(
+ Builder.CreateStructGEP(Arg_p, 2)), UsrDataTy);
+ UsrData->setName("UsrData");
+ Args++;
+ }
+
+ if (hasX(Signature)) {
+ Args++;
+ }
+
+ llvm::Value *Y = NULL;
+ if (hasY(Signature)) {
+ Y = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 5), "Y");
+ Args++;
+ }
+
+#if !RS_FOREACH_EXPAND_PASS_NDEBUG
+ if (Args != F->arg_end()) {
+ ALOGE("Assert failed at %s:%d: Invalid signature to the foreach function "
+ "'%s'!", __FILE__, __LINE__, F->getName().str().c_str());
+ ::abort();
+ }
+#endif
+
+ llvm::BasicBlock *Loop = llvm::BasicBlock::Create(*C, "Loop", ExpandedFunc);
+ llvm::BasicBlock *Exit = llvm::BasicBlock::Create(*C, "Exit", ExpandedFunc);
+
+ // if (x1 < x2) goto Loop; else goto Exit;
+ llvm::Value *Cond = Builder.CreateICmpSLT(Arg_x1, Arg_x2);
+ Builder.CreateCondBr(Cond, Loop, Exit);
+
+ // Loop:
+ Builder.SetInsertPoint(Loop);
+
+ // Populate the actual call to kernel().
+ llvm::SmallVector<llvm::Value*, 8> RootArgs;
+
+ llvm::Value *In = NULL;
+ llvm::Value *Out = NULL;
+
+ if (AIn) {
+ In = Builder.CreateLoad(AIn, "In");
+ RootArgs.push_back(In);
+ }
+
+ if (AOut) {
+ Out = Builder.CreateLoad(AOut, "Out");
+ RootArgs.push_back(Out);
+ }
+
+ if (UsrData) {
+ RootArgs.push_back(UsrData);
+ }
+
+ // We always have to load X, since it is used to iterate through the loop.
+ llvm::Value *X = Builder.CreateLoad(AX, "X");
+ if (hasX(Signature)) {
+ RootArgs.push_back(X);
+ }
+
+ if (Y) {
+ RootArgs.push_back(Y);
+ }
+
+ Builder.CreateCall(F, RootArgs);
+
+ if (In) {
+ // In += instep
+ llvm::Value *NewIn = Builder.CreateIntToPtr(Builder.CreateNUWAdd(
+ Builder.CreatePtrToInt(In, Int32Ty), Arg_instep), InTy);
+ Builder.CreateStore(NewIn, AIn);
+ }
+
+ if (Out) {
+ // Out += outstep
+ llvm::Value *NewOut = Builder.CreateIntToPtr(Builder.CreateNUWAdd(
+ Builder.CreatePtrToInt(Out, Int32Ty), Arg_outstep), OutTy);
+ Builder.CreateStore(NewOut, AOut);
+ }
+
+ // X++;
+ llvm::Value *XPlusOne =
+ Builder.CreateNUWAdd(X, llvm::ConstantInt::get(Int32Ty, 1));
+ Builder.CreateStore(XPlusOne, AX);
+
+ // If (X < x2) goto Loop; else goto Exit;
+ Cond = Builder.CreateICmpSLT(XPlusOne, Arg_x2);
+ Builder.CreateCondBr(Cond, Loop, Exit);
+
+ // Exit:
+ Builder.SetInsertPoint(Exit);
+ Builder.CreateRetVoid();
+
+ return true;
+ }
+
+ virtual bool runOnModule(llvm::Module &M) {
+ bool Changed = false;
+ this->M = &M;
+ C = &M.getContext();
+
+ for (RSInfo::ExportForeachFuncListTy::const_iterator
+ func_iter = mFuncs.begin(), func_end = mFuncs.end();
+ func_iter != func_end; func_iter++) {
+ const char *name = func_iter->first;
+ uint32_t signature = func_iter->second;
+ llvm::Function *kernel = M.getFunction(name);
+ if (kernel && kernel->getReturnType()->isVoidTy()) {
+ Changed |= ExpandFunction(kernel, signature);
+ }
+ }
+
+ return Changed;
+ }
+
+ virtual const char *getPassName() const {
+ return "ForEach-able Function Expansion";
+ }
+
+}; // end RSForEachExpandPass
+
+} // end anonymous namespace
+
+char RSForEachExpandPass::ID = 0;
+
+namespace bcc {
+
+llvm::ModulePass *
+createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs){
+ return new RSForEachExpandPass(pForeachFuncs);
+}
+
+} // end namespace bcc
diff --git a/lib/ExecutionEngine/RSInfo.cpp b/lib/ExecutionEngine/RSInfo.cpp
new file mode 100644
index 0000000..d351d36
--- /dev/null
+++ b/lib/ExecutionEngine/RSInfo.cpp
@@ -0,0 +1,295 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#include "RSInfo.h"
+
+#include <cstring>
+#include <new>
+
+#include "FileBase.h"
+#include "DebugHelper.h"
+#include "Sha1Helper.h"
+
+using namespace bcc;
+
+const char RSInfo::LibBCCPath[] = "/system/lib/libbcc.so";
+const char RSInfo::LibRSPath[] = "/system/lib/libRS.so";
+uint8_t RSInfo::LibBCCSHA1[20];
+uint8_t RSInfo::LibRSSHA1[20];
+
+void RSInfo::LoadBuiltInSHA1Information() {
+ static bool loaded = false;
+
+ if (loaded) {
+ return;
+ }
+
+ // Read SHA-1 checksum of libbcc from hard-coded patch
+ // /system/lib/libbcc.so.sha1.
+ readSHA1(LibBCCSHA1, 20, "/system/lib/libbcc.so.sha1");
+
+ // Calculate the SHA-1 checksum of libRS.so.
+ calcFileSHA1(LibRSSHA1, LibRSPath);
+
+ loaded = true;
+
+ return;
+}
+
+android::String8 RSInfo::GetPath(const FileBase &pFile) {
+ android::String8 result(pFile.getName().c_str());
+ result.append(".info");
+ return result;
+}
+
+#define PRINT_DEPENDENCY(PREFIX, N, X) \
+ ALOGV("\t" PREFIX "Source name: %s, " \
+ "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
+ "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", \
+ (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5], \
+ (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11], \
+ (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17], \
+ (X)[18], (X)[19]);
+
+bool RSInfo::CheckDependency(const RSInfo &pInfo,
+ const char *pInputFilename,
+ const RSScript::SourceDependencyListTy &pDeps) {
+ // Built-in dependencies are libbcc.so and libRS.so.
+ static const unsigned NumBuiltInDependencies = 2;
+
+ LoadBuiltInSHA1Information();
+
+ if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
+ ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
+ static_cast<unsigned long>(pInfo.mDependencyTable.size()),
+ static_cast<unsigned long>(pDeps.size()), pInputFilename);
+ return false;
+ } else {
+ // Built-in dependencies always go first.
+ const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
+ pInfo.mDependencyTable[0];
+ const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
+ pInfo.mDependencyTable[1];
+
+ // Check libbcc.so.
+ if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, 20) != 0) {
+ ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+ LibBCCPath);
+ PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
+ PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
+ cache_libbcc_dep.second);
+ return false;
+ }
+
+ // Check libRS.so.
+ if (::memcmp(cache_libRS_dep.second, LibRSSHA1, 20) != 0) {
+ ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+ LibRSPath);
+ PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
+ PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
+ cache_libRS_dep.second);
+ return false;
+ }
+
+ for (unsigned i = 0; i < pDeps.size(); i++) {
+ const RSScript::SourceDependency &in_dep = *(pDeps[i]);
+ const std::pair<const char *, const uint8_t *> &cache_dep =
+ pInfo.mDependencyTable[i + NumBuiltInDependencies];
+
+ if ((::strncmp(in_dep.getSourceName().c_str(),
+ cache_dep.first,
+ in_dep.getSourceName().length()) != 0) ||
+ (::memcmp(in_dep.getSHA1Checksum(), cache_dep.second, 20) != 0)) {
+ ALOGD("Cache %s is dirty due to the source it dependends on has been "
+ "changed:", pInputFilename);
+ PRINT_DEPENDENCY("given - ", in_dep.getSourceName().c_str(),
+ in_dep.getSHA1Checksum());
+ PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
+ ::memset(&mHeader, 0, sizeof(mHeader));
+
+ ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
+ ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
+
+ mHeader.headerSize = sizeof(mHeader);
+
+ mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
+ mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
+ mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
+ mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
+ mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
+ mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
+
+ if (pStringPoolSize > 0) {
+ mHeader.strPoolSize = pStringPoolSize;
+ mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
+ if (mStringPool == NULL) {
+ ALOGE("Out of memory when allocate memory for string pool in RSInfo "
+ "constructor (size: %u)!", mHeader.strPoolSize);
+ }
+ }
+}
+
+RSInfo::~RSInfo() {
+ delete [] mStringPool;
+}
+
+bool RSInfo::layout(off_t initial_offset) {
+ mHeader.dependencyTable.offset = initial_offset +
+ mHeader.headerSize +
+ mHeader.strPoolSize;
+ mHeader.dependencyTable.count = mDependencyTable.size();
+
+#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
+ mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
+ mHeader.pragmaList.count = mPragmas.size();
+
+ mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
+ mHeader.objectSlotList.count = mObjectSlots.size();
+
+ mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
+ mHeader.exportVarNameList.count = mExportVarNames.size();
+
+ mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
+ mHeader.exportFuncNameList.count = mExportFuncNames.size();
+
+ mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
+ mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
+#undef AFTER
+
+ return true;
+}
+
+void RSInfo::dump() const {
+ // Hide the codes to save the code size when debugging is disabled.
+#if !LOG_NDEBUG
+
+ // Dump header
+ ALOGV("RSInfo Header:");
+ ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
+ ALOGV("\tHeader size: %u", mHeader.headerSize);
+ ALOGV("\tString pool size: %u", mHeader.strPoolSize);
+
+#define DUMP_LIST_HEADER(_name, _header) do { \
+ ALOGV(_name ":"); \
+ ALOGV("\toffset: %u", (_header).offset); \
+ ALOGV("\t# of item: %u", (_header).count); \
+ ALOGV("\tsize of each item: %u", (_header).itemSize); \
+} while (false)
+ DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
+ for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
+ dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
+ PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
+ }
+
+ DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
+ for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
+ pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
+ ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
+ }
+
+ DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
+ for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
+ slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
+ ALOGV("slot: %u", *slot_iter);
+ }
+
+ DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
+ for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
+ var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
+ ALOGV("name: %s", *var_iter);
+ }
+
+ DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
+ for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
+ func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
+ ALOGV("name: %s", *func_iter);
+ }
+
+ DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
+ for (ExportForeachFuncListTy::const_iterator
+ foreach_iter = mExportForeachFuncs.begin(),
+ foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
+ foreach_iter++) {
+ ALOGV("name: %s, signature: %05x", foreach_iter->first,
+ foreach_iter->second);
+ }
+#undef DUMP_LIST_HEADER
+
+#endif // LOG_NDEBUG
+ return;
+}
+
+const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
+ // String pool uses direct indexing. Ensure that the pStrIdx is within the
+ // range.
+ if (pStrIdx >= mHeader.strPoolSize) {
+ ALOGE("String index #%u is out of range in string pool (size: %u)!",
+ pStrIdx, mHeader.strPoolSize);
+ return NULL;
+ }
+ return &mStringPool[ pStrIdx ];
+}
+
+rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
+ // Assume we are on the flat memory architecture (i.e., the memory space is
+ // continuous.)
+ if ((mStringPool + mHeader.strPoolSize) < pStr) {
+ ALOGE("String %s does not in the string pool!", pStr);
+ return rsinfo::gInvalidStringIndex;
+ }
+ return (pStr - mStringPool);
+}
+
+enum RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
+ // Check to see if we have any FP precision-related pragmas.
+ static const char relaxed_pragma[] = "rs_fp_relaxed";
+ static const char imprecise_pragma[] = "rs_fp_imprecise";
+ bool relaxed_pragma_seen = false;
+
+ for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
+ pragma_end = mPragmas.end(); pragma_iter != pragma_end;
+ pragma_iter++) {
+ const char *pragma_key = pragma_iter->first;
+ if (::strcmp(pragma_key, relaxed_pragma) == 0) {
+ relaxed_pragma_seen = true;
+ } else if (::strcmp(pragma_key, imprecise_pragma) == 0) {
+ if (relaxed_pragma_seen) {
+ ALOGW("Multiple float precision pragmas specified!");
+ }
+ // Fast return when there's rs_fp_imprecise specified.
+ return Imprecise;
+ }
+ }
+
+ // Imprecise is selected over Relaxed precision.
+ // In the absence of both, we stick to the default Full precision.
+ if (relaxed_pragma_seen) {
+ return Relaxed;
+ } else {
+ return Full;
+ }
+ // unreachable
+}
diff --git a/lib/ExecutionEngine/RSInfo.h b/lib/ExecutionEngine/RSInfo.h
new file mode 100644
index 0000000..d69d15e
--- /dev/null
+++ b/lib/ExecutionEngine/RSInfo.h
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_RS_INFO_FILE_H
+#define BCC_EXECUTION_ENGINE_RS_INFO_FILE_H
+
+#include <stdint.h>
+
+#include <utility>
+
+#include "RSScript.h"
+#include "DebugHelper.h"
+
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+namespace bcc {
+
+// Forward declarations
+class FileBase;
+class InputFile;
+class OutputFile;
+class Source;
+
+namespace rsinfo {
+
+/* RS info file magic */
+#define RSINFO_MAGIC "\0rsinfo\n"
+
+/* RS info file version, encoded in 4 bytes of ASCII */
+#define RSINFO_VERSION "003\0"
+
+struct __attribute__((packed)) ListHeader {
+ // The offset from the beginning of the file of data
+ uint32_t offset;
+ // Number of item in the list
+ uint32_t count;
+ // Size of each item
+ uint8_t itemSize;
+};
+
+/* RS info file header */
+struct __attribute__((packed)) Header {
+ // Magic versus version
+ uint8_t magic[8];
+ uint8_t version[4];
+
+ uint8_t isThreadable;
+ uint8_t hasDebugInformation;
+
+ uint16_t headerSize;
+
+ uint32_t strPoolSize;
+
+ struct ListHeader dependencyTable;
+ struct ListHeader pragmaList;
+ struct ListHeader objectSlotList;
+ struct ListHeader exportVarNameList;
+ struct ListHeader exportFuncNameList;
+ struct ListHeader exportForeachFuncList;
+};
+
+typedef uint32_t StringIndexTy;
+// Use value -1 as an invalid string index marker. No need to declare with
+// 'static' modifier since 'const' variable has internal linkage by default.
+const StringIndexTy gInvalidStringIndex = static_cast<StringIndexTy>(-1);
+
+struct __attribute__((packed)) DependencyTableItem {
+ StringIndexTy id;
+ // SHA-1 checksum is stored as a string in string pool (and has fixed-length
+ // 20 bytes)
+ StringIndexTy sha1;
+};
+
+struct __attribute__((packed)) PragmaItem {
+ // Pragma is a key-value pair.
+ StringIndexTy key;
+ StringIndexTy value;
+};
+
+struct __attribute__((packed)) ObjectSlotItem {
+ uint32_t slot;
+};
+
+struct __attribute__((packed)) ExportVarNameItem {
+ StringIndexTy name;
+};
+
+struct __attribute__((packed)) ExportFuncNameItem {
+ StringIndexTy name;
+};
+
+struct __attribute__((packed)) ExportForeachFuncItem {
+ StringIndexTy name;
+ uint32_t signature;
+};
+
+// Return the human-readable name of the given rsinfo::*Item in the template
+// parameter. This is for debugging and error message.
+template<typename Item>
+inline const char *GetItemTypeName();
+
+template<>
+inline const char *GetItemTypeName<DependencyTableItem>()
+{ return "rs dependency info"; }
+
+template<>
+inline const char *GetItemTypeName<PragmaItem>()
+{ return "rs pragma"; }
+
+template<>
+inline const char *GetItemTypeName<ObjectSlotItem>()
+{ return "rs object slot"; }
+
+template<>
+inline const char *GetItemTypeName<ExportVarNameItem>()
+{ return "rs export var"; }
+
+template<>
+inline const char *GetItemTypeName<ExportFuncNameItem>()
+{ return "rs export func"; }
+
+template<>
+inline const char *GetItemTypeName<ExportForeachFuncItem>()
+{ return "rs export foreach"; }
+
+} // end namespace rsinfo
+
+class RSInfo {
+public:
+ typedef android::Vector<std::pair<const char *,
+ const uint8_t *> > DependencyTableTy;
+ typedef android::Vector<std::pair<const char*, const char*> > PragmaListTy;
+ typedef android::Vector<uint32_t> ObjectSlotListTy;
+ typedef android::Vector<const char *> ExportVarNameListTy;
+ typedef android::Vector<const char *> ExportFuncNameListTy;
+ typedef android::Vector<std::pair<const char *,
+ uint32_t> > ExportForeachFuncListTy;
+
+public:
+ // Calculate or load the SHA-1 information of the built-in dependencies.
+ static void LoadBuiltInSHA1Information();
+
+ // Return the path of the RS info file corresponded to the given output
+ // executable file.
+ static android::String8 GetPath(const FileBase &pFile);
+
+ static const char LibBCCPath[];
+ static const char LibRSPath[];
+
+private:
+ // SHA-1 of the built-in dependencies. Will be initialized in
+ // LoadBuiltInSHA1Information().
+ static uint8_t LibBCCSHA1[20];
+ static uint8_t LibRSSHA1[20];
+
+ static bool CheckDependency(const RSInfo &pInfo,
+ const char *pInputFilename,
+ const RSScript::SourceDependencyListTy &pDeps);
+ static bool AddBuiltInDependencies(RSInfo &pInfo);
+
+ rsinfo::Header mHeader;
+
+ char *mStringPool;
+
+ // In most of the time, there're 4 source dependencies stored (libbcc.so,
+ // libRS.so, libclcore and the input bitcode itself.)
+ DependencyTableTy mDependencyTable;
+ PragmaListTy mPragmas;
+ ObjectSlotListTy mObjectSlots;
+ ExportVarNameListTy mExportVarNames;
+ ExportFuncNameListTy mExportFuncNames;
+ ExportForeachFuncListTy mExportForeachFuncs;
+
+ // Initialize an empty RSInfo with its size of string pool is pStringPoolSize.
+ RSInfo(size_t pStringPoolSize);
+
+ // layout() assigns value of offset in each ListHeader (i.e., it decides where
+ // data should go in the file.) It also updates fields other than offset to
+ // reflect the current RSInfo object states to mHeader.
+ bool layout(off_t initial_offset);
+
+public:
+ ~RSInfo();
+
+ // Implemented in RSInfoExtractor.cpp.
+ static RSInfo *ExtractFromSource(
+ const Source &pSource, const RSScript::SourceDependencyListTy &pDeps);
+
+ // Implemented in RSInfoReader.cpp.
+ static RSInfo *ReadFromFile(InputFile &pInput,
+ const RSScript::SourceDependencyListTy &pDeps);
+
+ // Implemneted in RSInfoWriter.cpp
+ bool write(OutputFile &pOutput);
+
+ void dump() const;
+
+ // const getter
+ inline bool isThreadable() const
+ { return mHeader.isThreadable; }
+ inline bool hasDebugInformation() const
+ { return mHeader.hasDebugInformation; }
+ inline const DependencyTableTy &getDependencyTable() const
+ { return mDependencyTable; }
+ inline const PragmaListTy &getPragmas() const
+ { return mPragmas; }
+ inline const ObjectSlotListTy &getObjectSlots() const
+ { return mObjectSlots; }
+ inline const ExportVarNameListTy &getExportVarNames() const
+ { return mExportVarNames; }
+ inline const ExportFuncNameListTy &getExportFuncNames() const
+ { return mExportFuncNames; }
+ inline const ExportForeachFuncListTy &getExportForeachFuncs() const
+ { return mExportForeachFuncs; }
+
+ const char *getStringFromPool(rsinfo::StringIndexTy pStrIdx) const;
+ rsinfo::StringIndexTy getStringIdxInPool(const char *pStr) const;
+
+ // setter
+ inline void setThreadable(bool pThreadable = true)
+ { mHeader.isThreadable = pThreadable; }
+
+public:
+ enum FloatPrecision {
+ Full,
+ Relaxed,
+ Imprecise,
+ };
+
+ // Return the minimal floating point precision required for the associated
+ // script.
+ enum FloatPrecision getFloatPrecisionRequirement() const;
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_INFO_FILE_H
diff --git a/lib/ExecutionEngine/RSInfoExtractor.cpp b/lib/ExecutionEngine/RSInfoExtractor.cpp
new file mode 100644
index 0000000..a90becc
--- /dev/null
+++ b/lib/ExecutionEngine/RSInfoExtractor.cpp
@@ -0,0 +1,404 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//===----------------------------------------------------------------------===//
+// This file implements RSInfo::ExtractFromSource()
+//===----------------------------------------------------------------------===//
+#include "RSInfo.h"
+
+#include <llvm/Constants.h>
+#include <llvm/Metadata.h>
+#include <llvm/Module.h>
+
+#include "DebugHelper.h"
+#include "Source.h"
+
+using namespace bcc;
+
+namespace {
+
+// Name of metadata node where pragma info resides (should be synced with
+// slang.cpp)
+const llvm::StringRef pragma_metadata_name("#pragma");
+
+/*
+ * The following names should be synced with the one appeared in
+ * slang_rs_metadata.h.
+ */
+// Name of metadata node where exported variable names reside
+const llvm::StringRef export_var_metadata_name("#rs_export_var");
+
+// Name of metadata node where exported function names reside
+const llvm::StringRef export_func_metadata_name("#rs_export_func");
+
+// Name of metadata node where exported ForEach name information resides
+const llvm::StringRef export_foreach_name_metadata_name("#rs_export_foreach_name");
+
+// Name of metadata node where exported ForEach signature information resides
+const llvm::StringRef export_foreach_metadata_name("#rs_export_foreach");
+
+// Name of metadata node where RS object slot info resides (should be
+const llvm::StringRef object_slot_metadata_name("#rs_object_slots");
+
+inline llvm::StringRef getStringFromOperand(const llvm::Value *pString) {
+ if ((pString != NULL) && (pString->getValueID() == llvm::Value::MDStringVal)) {
+ return static_cast<const llvm::MDString *>(pString)->getString();
+ }
+ return llvm::StringRef();
+}
+
+template<size_t NumOperands>
+inline size_t getMetadataStringLength(const llvm::NamedMDNode *pMetadata) {
+ if (pMetadata == NULL) {
+ return 0;
+ }
+
+ size_t string_size = 0;
+ for (unsigned i = 0, e = pMetadata->getNumOperands(); i < e; i++) {
+ llvm::MDNode *node = pMetadata->getOperand(i);
+ if ((node != NULL) && (node->getNumOperands() >= NumOperands)) {
+ // Compiler try its best to unroll this loop since NumOperands is a
+ // template parameter (therefore the number of iteration can be determined
+ // at compile-time and it's usually small.)
+ for (unsigned j = 0; j < NumOperands; j++) {
+ llvm::StringRef s = getStringFromOperand(node->getOperand(j));
+ if (s.size() > 0) {
+ // +1 is for the null-terminator at the end of string.
+ string_size += (s.size() + 1);
+ }
+ }
+ }
+ }
+
+ return string_size;
+}
+
+// Write a string pString to the string pool pStringPool at offset pWriteStart.
+// Return the pointer the pString resides within the string pool.
+const char *writeString(const llvm::StringRef &pString, char *pStringPool,
+ off_t *pWriteStart) {
+ if (pString.empty()) {
+ return pStringPool;
+ }
+
+ char *pStringWriteStart = pStringPool + *pWriteStart;
+ // Copy the string.
+ ::memcpy(pStringWriteStart, pString.data(), pString.size());
+ // Write null-terminator at the end of the string.
+ pStringWriteStart[ pString.size() ] = '\0';
+ // Update pWriteStart.
+ *pWriteStart += (pString.size() + 1);
+
+ return pStringWriteStart;
+}
+
+bool writeDependency(const std::string &pSourceName, const uint8_t *pSHA1,
+ char *pStringPool, off_t *pWriteStart,
+ RSInfo::DependencyTableTy &pDepTable) {
+ const char *source_name = writeString(pSourceName, pStringPool, pWriteStart);
+
+ uint8_t *sha1 = reinterpret_cast<uint8_t *>(pStringPool + *pWriteStart);
+
+ // SHA-1 is special. It's 20-bytes long without null-terminator.
+ ::memcpy(sha1, pSHA1, 20);
+ // Record in the result RSInfo object.
+ pDepTable.push(std::make_pair(source_name, sha1));
+ // Update the string pool pointer.
+ *pWriteStart += 20;
+
+ return true;
+}
+
+} // end anonymous namespace
+
+RSInfo *RSInfo::ExtractFromSource(const Source &pSource,
+ const RSScript::SourceDependencyListTy &pDeps)
+{
+ const llvm::Module &module = pSource.getModule();
+ const char *module_name = module.getModuleIdentifier().c_str();
+
+ const llvm::NamedMDNode *pragma =
+ module.getNamedMetadata(pragma_metadata_name);
+ const llvm::NamedMDNode *export_var =
+ module.getNamedMetadata(export_var_metadata_name);
+ const llvm::NamedMDNode *export_func =
+ module.getNamedMetadata(export_func_metadata_name);
+ const llvm::NamedMDNode *export_foreach_name =
+ module.getNamedMetadata(export_foreach_name_metadata_name);
+ const llvm::NamedMDNode *export_foreach_signature =
+ module.getNamedMetadata(export_foreach_metadata_name);
+ const llvm::NamedMDNode *object_slots =
+ module.getNamedMetadata(object_slot_metadata_name);
+
+ // Always write a byte 0x0 at the beginning of the string pool.
+ size_t string_pool_size = 1;
+ off_t cur_string_pool_offset = 0;
+
+ RSInfo *result = NULL;
+
+ // Handle legacy case for pre-ICS bitcode that doesn't contain a metadata
+ // section for ForEach. We generate a full signature for a "root" function.
+ if ((export_foreach_name == NULL) || (export_foreach_signature == NULL)) {
+ export_foreach_name = NULL;
+ export_foreach_signature = NULL;
+ string_pool_size += 5; // insert "root\0" for #rs_export_foreach_name
+ }
+
+ string_pool_size += getMetadataStringLength<2>(pragma);
+ string_pool_size += getMetadataStringLength<1>(export_var);
+ string_pool_size += getMetadataStringLength<1>(export_func);
+ string_pool_size += getMetadataStringLength<1>(export_foreach_name);
+
+ // Don't forget to reserve the space for the dependency informationin string
+ // pool.
+ string_pool_size += ::strlen(LibBCCPath) + 1 + 20;
+ string_pool_size += ::strlen(LibRSPath) + 1 + 20;
+ for (unsigned i = 0, e = pDeps.size(); i != e; i++) {
+ const RSScript::SourceDependency *source_dep = pDeps[i];
+ if (source_dep != NULL) {
+ // +1 for null-terminator
+ string_pool_size += source_dep->getSourceName().length() + 1;
+ // +20 for SHA-1 checksum
+ string_pool_size += 20;
+ }
+ }
+
+ // Allocate result object
+ result = new (std::nothrow) RSInfo(string_pool_size);
+ if (result == NULL) {
+ ALOGE("Out of memory when create RSInfo object for %s!", module_name);
+ goto bail;
+ }
+
+ // Check string pool.
+ if (result->mStringPool == NULL) {
+ ALOGE("Out of memory when allocate string pool in RSInfo object for %s!",
+ module_name);
+ goto bail;
+ }
+
+ // First byte of string pool should be an empty string
+ result->mStringPool[ cur_string_pool_offset++ ] = '\0';
+
+ // Populate all the strings and data.
+#define FOR_EACH_NODE_IN(_metadata, _node) \
+ for (unsigned i = 0, e = (_metadata)->getNumOperands(); i != e; i++) \
+ if (((_node) = (_metadata)->getOperand(i)) != NULL)
+ //===--------------------------------------------------------------------===//
+ // #pragma
+ //===--------------------------------------------------------------------===//
+ // Pragma is actually a key-value pair. The value can be an empty string while
+ // the key cannot.
+ if (pragma != NULL) {
+ llvm::MDNode *node;
+ FOR_EACH_NODE_IN(pragma, node) {
+ llvm::StringRef key = getStringFromOperand(node->getOperand(0));
+ llvm::StringRef val = getStringFromOperand(node->getOperand(1));
+ if (key.empty()) {
+ ALOGW("%s contains pragma metadata with empty key (skip)!",
+ module_name);
+ } else {
+ result->mPragmas.push(std::make_pair(
+ writeString(key, result->mStringPool, &cur_string_pool_offset),
+ writeString(val, result->mStringPool, &cur_string_pool_offset)));
+ } // key.empty()
+ } // FOR_EACH_NODE_IN
+ } // pragma != NULL
+
+ //===--------------------------------------------------------------------===//
+ // #rs_export_var
+ //===--------------------------------------------------------------------===//
+ if (export_var != NULL) {
+ llvm::MDNode *node;
+ FOR_EACH_NODE_IN(export_var, node) {
+ llvm::StringRef name = getStringFromOperand(node->getOperand(0));
+ if (name.empty()) {
+ ALOGW("%s contains empty entry in #rs_export_var metadata (skip)!",
+ module_name);
+ } else {
+ result->mExportVarNames.push(
+ writeString(name, result->mStringPool, &cur_string_pool_offset));
+ }
+ }
+ }
+
+ //===--------------------------------------------------------------------===//
+ // #rs_export_func
+ //===--------------------------------------------------------------------===//
+ if (export_func != NULL) {
+ llvm::MDNode *node;
+ FOR_EACH_NODE_IN(export_func, node) {
+ llvm::StringRef name = getStringFromOperand(node->getOperand(0));
+ if (name.empty()) {
+ ALOGW("%s contains empty entry in #rs_export_func metadata (skip)!",
+ module_name);
+ } else {
+ result->mExportFuncNames.push(
+ writeString(name, result->mStringPool, &cur_string_pool_offset));
+ }
+ }
+ }
+
+ //===--------------------------------------------------------------------===//
+ // #rs_export_foreach and #rs_export_foreach_name
+ //===--------------------------------------------------------------------===//
+ // It's a little bit complicated to deal with #rs_export_foreach (the
+ // signature of foreach-able function) and #rs_export_foreach_name (the name
+ // of function which is foreach-able). We have to maintain a legacy case:
+ //
+ // In pre-ICS bitcode, forEach feature only supports non-graphic root()
+ // function and only one signature corresponded to that non-graphic root()
+ // was written to the #rs_export_foreach metadata section. There's no
+ // #rs_export_foreach_name metadata section.
+ //
+ // Currently, not only non-graphic root() is supported but also other
+ // functions that are exportable. Therefore, a new metadata section
+ // #rs_export_foreach_name is added to specify which functions are
+ // for-eachable. In this case, #rs_export_foreach (the function name) and
+ // #rs_export_foreach metadata (the signature) is one-to-one mapping among
+ // their entries.
+ if ((export_foreach_name != NULL) && (export_foreach_signature != NULL)) {
+ unsigned num_foreach_function;
+
+ // Should be one-to-one mapping.
+ if (export_foreach_name->getNumOperands() !=
+ export_foreach_signature->getNumOperands()) {
+ ALOGE("Mismatch number of foreach-able function names (%u) in "
+ "#rs_export_foreach_name and number of signatures (%u) "
+ "in %s!", export_foreach_name->getNumOperands(),
+ export_foreach_signature->getNumOperands(), module_name);
+ goto bail;
+ }
+
+ num_foreach_function = export_foreach_name->getNumOperands();
+ for (unsigned i = 0; i < num_foreach_function; i++) {
+ llvm::MDNode *name_node = export_foreach_name->getOperand(i);
+ llvm::MDNode *signature_node = export_foreach_signature->getOperand(i);
+
+ llvm::StringRef name, signature_string;
+ if (name_node != NULL) {
+ name = getStringFromOperand(name_node->getOperand(0));
+ }
+ if (signature_node != NULL) {
+ signature_string = getStringFromOperand(signature_node->getOperand(0));
+ }
+
+ if (!name.empty() && !signature_string.empty()) {
+ // Both name_node and signature_node are not NULL nodes.
+ uint32_t signature;
+ if (signature_string.getAsInteger(10, signature)) {
+ ALOGE("Non-integer signature value '%s' for function %s found in %s!",
+ signature_string.str().c_str(), name.str().c_str(), module_name);
+ goto bail;
+ }
+ result->mExportForeachFuncs.push(std::make_pair(
+ writeString(name, result->mStringPool, &cur_string_pool_offset),
+ signature));
+ } else {
+ // One or both of the name and signature value are empty. It's safe only
+ // if both of them are empty.
+ if (name.empty() && signature_string.empty()) {
+ ALOGW("Entries #%u at #rs_export_foreach_name and #rs_export_foreach"
+ " are both NULL in %s! (skip)", i, module_name);
+ continue;
+ } else {
+ ALOGE("Entries #%u at %s is NULL in %s! (skip)", i,
+ (name.empty() ? "#rs_export_foreach_name" :
+ "#rs_export_foreach"), module_name);
+ goto bail;
+ }
+ }
+ } // end for
+ } else {
+ // To handle the legacy case, we generate a full signature for a "root"
+ // function which means that we need to set the bottom 5 bits (0x1f) in the
+ // mask.
+ result->mExportForeachFuncs.push(std::make_pair(
+ writeString(llvm::StringRef("root"), result->mStringPool,
+ &cur_string_pool_offset), 0x1f));
+ }
+
+ //===--------------------------------------------------------------------===//
+ // #rs_object_slots
+ //===--------------------------------------------------------------------===//
+ if (object_slots != NULL) {
+ llvm::MDNode *node;
+ FOR_EACH_NODE_IN(object_slots, node) {
+ llvm::StringRef val = getStringFromOperand(node->getOperand(0));
+ if (val.empty()) {
+ ALOGW("%s contains empty entry in #rs_object_slots (skip)!",
+ module.getModuleIdentifier().c_str());
+ } else {
+ uint32_t slot;
+ if (val.getAsInteger(10, slot)) {
+ ALOGE("Non-integer object slot value '%s' in %s!", val.str().c_str(),
+ module.getModuleIdentifier().c_str());
+ goto bail;
+ }
+ }
+ }
+ }
+#undef FOR_EACH_NODE_IN
+
+ //===--------------------------------------------------------------------===//
+ // Record built-in dependency information.
+ //===--------------------------------------------------------------------===//
+ LoadBuiltInSHA1Information();
+
+ if (!writeDependency(LibBCCPath, LibBCCSHA1,
+ result->mStringPool, &cur_string_pool_offset,
+ result->mDependencyTable)) {
+ goto bail;
+ }
+
+ if (!writeDependency(LibRSPath, LibRSSHA1,
+ result->mStringPool, &cur_string_pool_offset,
+ result->mDependencyTable)) {
+ goto bail;
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Record dependency information.
+ //===--------------------------------------------------------------------===//
+ for (unsigned i = 0, e = pDeps.size(); i != e; i++) {
+ const RSScript::SourceDependency *source_dep = pDeps[i];
+ if (source_dep != NULL) {
+ if (!writeDependency(source_dep->getSourceName(),
+ source_dep->getSHA1Checksum(),
+ result->mStringPool, &cur_string_pool_offset,
+ result->mDependencyTable)) {
+ goto bail;
+ }
+ }
+ }
+
+ //===--------------------------------------------------------------------===//
+ // Determine whether the bitcode contains debug information
+ //===--------------------------------------------------------------------===//
+ // The root context of the debug information in the bitcode is put under
+ // the metadata named "llvm.dbg.cu".
+ result->mHeader.hasDebugInformation =
+ static_cast<uint8_t>(module.getNamedMetadata("llvm.dbg.cu") != NULL);
+
+ assert((reinterpret_cast<size_t>(cur_string_pool_offset) ==
+ string_pool_size) && "Unexpected string pool size!");
+
+ return result;
+
+bail:
+ delete result;
+ return NULL;
+}
diff --git a/lib/ExecutionEngine/RSInfoReader.cpp b/lib/ExecutionEngine/RSInfoReader.cpp
new file mode 100644
index 0000000..a3eca5a
--- /dev/null
+++ b/lib/ExecutionEngine/RSInfoReader.cpp
@@ -0,0 +1,331 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//===----------------------------------------------------------------------===//
+// This file implements RSInfo::ReadFromFile()
+//===----------------------------------------------------------------------===//
+
+#include "RSInfo.h"
+
+#include <new>
+
+#include <utils/FileMap.h>
+
+#include "DebugHelper.h"
+#include "InputFile.h"
+
+using namespace bcc;
+
+namespace {
+
+template<typename ItemType, typename ItemContainer>
+inline bool helper_read_list_item(const ItemType &pItem,
+ const RSInfo &pInfo,
+ ItemContainer &pResult);
+
+// Process DependencyTableItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::DependencyTableItem, RSInfo::DependencyTableTy>(
+ const rsinfo::DependencyTableItem &pItem,
+ const RSInfo &pInfo,
+ RSInfo::DependencyTableTy &pResult)
+{
+ const char *id = pInfo.getStringFromPool(pItem.id);
+ const uint8_t *sha1 =
+ reinterpret_cast<const uint8_t *>(pInfo.getStringFromPool(pItem.sha1));
+
+ if (id == NULL) {
+ ALOGE("Invalid string index %d for source id in RS dependenct table.",
+ pItem.id);
+ return false;
+ }
+
+ if (sha1 == NULL) {
+ ALOGE("Invalid string index %d for SHA-1 checksum in RS dependenct table.",
+ pItem.id);
+ return false;
+ }
+
+ pResult.push(std::make_pair(id, sha1));
+ return true;
+}
+
+// Process PragmaItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::PragmaItem, RSInfo::PragmaListTy>(
+ const rsinfo::PragmaItem &pItem,
+ const RSInfo &pInfo,
+ RSInfo::PragmaListTy &pResult)
+{
+ const char *key = pInfo.getStringFromPool(pItem.key);
+ const char *value =pInfo.getStringFromPool(pItem.value);
+
+ if (key == NULL) {
+ ALOGE("Invalid string index %d for key in RS pragma list.", pItem.key);
+ return false;
+ }
+
+ if (value == NULL) {
+ ALOGE("Invalid string index %d for value in RS pragma list.", pItem.value);
+ return false;
+ }
+
+ pResult.push(std::make_pair(key, value));
+ return true;
+}
+
+// Procee ObjectSlotItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ObjectSlotItem, RSInfo::ObjectSlotListTy>(
+ const rsinfo::ObjectSlotItem &pItem,
+ const RSInfo &pInfo,
+ RSInfo::ObjectSlotListTy &pResult)
+{
+ pResult.push(pItem.slot);
+ return true;
+}
+
+// Procee ExportVarNameItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ExportVarNameItem, RSInfo::ExportVarNameListTy>(
+ const rsinfo::ExportVarNameItem &pItem,
+ const RSInfo &pInfo,
+ RSInfo::ExportVarNameListTy &pResult)
+{
+ const char *name = pInfo.getStringFromPool(pItem.name);
+
+ if (name == NULL) {
+ ALOGE("Invalid string index %d for name in RS export vars.", pItem.name);
+ return false;
+ }
+
+ pResult.push(name);
+ return true;
+}
+
+// Procee ExportFuncNameItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ExportFuncNameItem, RSInfo::ExportFuncNameListTy>(
+ const rsinfo::ExportFuncNameItem &pItem,
+ const RSInfo &pInfo,
+ RSInfo::ExportFuncNameListTy &pResult)
+{
+ const char *name = pInfo.getStringFromPool(pItem.name);
+
+ if (name == NULL) {
+ ALOGE("Invalid string index %d for name in RS export funcs.", pItem.name);
+ return false;
+ }
+
+ pResult.push(name);
+ return true;
+}
+
+// Procee ExportForeachFuncItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ExportForeachFuncItem, RSInfo::ExportForeachFuncListTy>(
+ const rsinfo::ExportForeachFuncItem &pItem,
+ const RSInfo &pInfo,
+ RSInfo::ExportForeachFuncListTy &pResult)
+{
+ const char *name = pInfo.getStringFromPool(pItem.name);
+
+ if (name == NULL) {
+ ALOGE("Invalid string index %d for name in RS export foreachs.", pItem.name);
+ return false;
+ }
+
+ pResult.push(std::make_pair(name, pItem.signature));
+ return true;
+}
+
+template<typename ItemType, typename ItemContainer>
+inline bool helper_read_list(const uint8_t *pData,
+ const RSInfo &pInfo,
+ const rsinfo::ListHeader &pHeader,
+ ItemContainer &pResult) {
+ const ItemType *item;
+
+ // Out-of-range exception has been checked.
+ for (uint32_t i = 0; i < pHeader.count; i++) {
+ item = reinterpret_cast<const ItemType *>(pData +
+ pHeader.offset +
+ i * pHeader.itemSize);
+ if (!helper_read_list_item<ItemType, ItemContainer>(*item, pInfo, pResult)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+} // end anonymous namespace
+
+RSInfo *RSInfo::ReadFromFile(InputFile &pInput,
+ const RSScript::SourceDependencyListTy &pDeps) {
+ android::FileMap *map = NULL;
+ RSInfo *result = NULL;
+ const uint8_t *data;
+ const rsinfo::Header *header;
+ size_t filesize;
+ const char *input_filename = pInput.getName().c_str();
+ const off_t cur_input_offset = pInput.tell();
+
+ if (pInput.hasError()) {
+ ALOGE("Invalid RS info file %s! (%s)", input_filename,
+ pInput.getErrorMessage().c_str());
+ goto bail;
+ }
+
+ filesize = pInput.getSize();
+ if (pInput.hasError()) {
+ ALOGE("Failed to get the size of RS info file %s! (%s)",
+ input_filename, pInput.getErrorMessage().c_str());
+ goto bail;
+ }
+
+ // Create memory map for the file.
+ map = pInput.createMap(/* pOffset */cur_input_offset,
+ /* pLength */filesize - cur_input_offset);
+ if (map == NULL) {
+ ALOGE("Failed to map RS info file %s to the memory! (%s)",
+ input_filename, pInput.getErrorMessage().c_str());
+ goto bail;
+ }
+
+ data = reinterpret_cast<const uint8_t *>(map->getDataPtr());
+
+ // Header starts at the beginning of the file.
+ header = reinterpret_cast<const rsinfo::Header *>(data);
+
+ // Check the magic.
+ if (::memcmp(header->magic, RSINFO_MAGIC, sizeof(header->magic)) != 0) {
+ ALOGV("Wrong magic found in the RS info file %s. Treat it as a dirty "
+ "cache.", input_filename);
+ goto bail;
+ }
+
+ // Check the version.
+ if (::memcmp(header->version,
+ RSINFO_VERSION,
+ sizeof((header->version)) != 0)) {
+ ALOGV("Mismatch the version of RS info file %s: (current) %s v.s. (file) "
+ "%s. Treat it as as a dirty cache.", input_filename, RSINFO_VERSION,
+ header->version);
+ goto bail;
+ }
+
+ // Check the size.
+ if ((header->headerSize != sizeof(rsinfo::Header)) ||
+ (header->dependencyTable.itemSize != sizeof(rsinfo::DependencyTableItem)) ||
+ (header->pragmaList.itemSize != sizeof(rsinfo::PragmaItem)) ||
+ (header->objectSlotList.itemSize != sizeof(rsinfo::ObjectSlotItem)) ||
+ (header->exportVarNameList.itemSize != sizeof(rsinfo::ExportVarNameItem)) ||
+ (header->exportFuncNameList.itemSize != sizeof(rsinfo::ExportFuncNameItem)) ||
+ (header->exportForeachFuncList.itemSize != sizeof(rsinfo::ExportForeachFuncItem))) {
+ ALOGW("Corrupted RS info file %s! (unexpected size found)", input_filename);
+ goto bail;
+ }
+
+ // Check the range.
+#define LIST_DATA_RANGE(_list_header) \
+ ((_list_header).offset + (_list_header).count * (_list_header).itemSize)
+ if (((header->headerSize + header->strPoolSize) > filesize) ||
+ (LIST_DATA_RANGE(header->dependencyTable) > filesize) ||
+ (LIST_DATA_RANGE(header->pragmaList) > filesize) ||
+ (LIST_DATA_RANGE(header->objectSlotList) > filesize) ||
+ (LIST_DATA_RANGE(header->exportVarNameList) > filesize) ||
+ (LIST_DATA_RANGE(header->exportFuncNameList) > filesize) ||
+ (LIST_DATA_RANGE(header->exportForeachFuncList) > filesize)) {
+ ALOGW("Corrupted RS info file %s! (data out of the range)", input_filename);
+ goto bail;
+ }
+#undef LIST_DATA_RANGE
+
+ // File seems ok, create result RSInfo object.
+ result = new (std::nothrow) RSInfo(header->strPoolSize);
+ if (result == NULL) {
+ ALOGE("Out of memory when create RSInfo object for %s!", input_filename);
+ goto bail;
+ }
+
+ // Make advice on our access pattern.
+ map->advise(android::FileMap::SEQUENTIAL);
+
+ // Copy the header.
+ ::memcpy(&result->mHeader, header, sizeof(rsinfo::Header));
+
+ if (header->strPoolSize > 0) {
+ // Copy the string pool. The string pool is immediately after the header at
+ // the offset header->headerSize.
+ if (result->mStringPool == NULL) {
+ ALOGE("Out of memory when allocate string pool for RS info file %s!",
+ input_filename);
+ goto bail;
+ }
+ ::memcpy(result->mStringPool, data + result->mHeader.headerSize,
+ result->mHeader.strPoolSize);
+ }
+
+ // Populate all the data to the result object.
+ if (!helper_read_list<rsinfo::DependencyTableItem, DependencyTableTy>
+ (data, *result, header->dependencyTable, result->mDependencyTable)) {
+ goto bail;
+ }
+
+ // Check dependency to see whether the cache is dirty or not.
+ if (!CheckDependency(*result, pInput.getName().c_str(), pDeps)) {
+ goto bail;
+ }
+
+ if (!helper_read_list<rsinfo::PragmaItem, PragmaListTy>
+ (data, *result, header->pragmaList, result->mPragmas)) {
+ goto bail;
+ }
+
+ if (!helper_read_list<rsinfo::ObjectSlotItem, ObjectSlotListTy>
+ (data, *result, header->objectSlotList, result->mObjectSlots)) {
+ goto bail;
+ }
+
+ if (!helper_read_list<rsinfo::ExportVarNameItem, ExportVarNameListTy>
+ (data, *result, header->exportVarNameList, result->mExportVarNames)) {
+ goto bail;
+ }
+
+ if (!helper_read_list<rsinfo::ExportFuncNameItem, ExportFuncNameListTy>
+ (data, *result, header->exportFuncNameList, result->mExportFuncNames)) {
+ goto bail;
+ }
+
+ if (!helper_read_list<rsinfo::ExportForeachFuncItem, ExportForeachFuncListTy>
+ (data, *result, header->exportForeachFuncList, result->mExportForeachFuncs)) {
+ goto bail;
+ }
+
+ // Clean up.
+ map->release();
+
+ return result;
+
+bail:
+ if (map != NULL) {
+ map->release();
+ }
+
+ delete result;
+
+ return NULL;
+} // RSInfo::ReadFromFile
diff --git a/lib/ExecutionEngine/RSInfoWriter.cpp b/lib/ExecutionEngine/RSInfoWriter.cpp
new file mode 100644
index 0000000..d54fb41
--- /dev/null
+++ b/lib/ExecutionEngine/RSInfoWriter.cpp
@@ -0,0 +1,234 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//===----------------------------------------------------------------------===//
+// This file implements RSInfo::write()
+//===----------------------------------------------------------------------===//
+
+#include "RSInfo.h"
+
+#include "DebugHelper.h"
+#include "OutputFile.h"
+
+using namespace bcc;
+
+namespace {
+
+template<typename ItemType, typename ItemContainer> inline bool
+helper_adapt_list_item(ItemType &pResult, const RSInfo &pInfo,
+ const typename ItemContainer::const_iterator &pItem);
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::DependencyTableItem, RSInfo::DependencyTableTy>(
+ rsinfo::DependencyTableItem &pResult,
+ const RSInfo &pInfo,
+ const RSInfo::DependencyTableTy::const_iterator &pItem) {
+ pResult.id = pInfo.getStringIdxInPool(pItem->first);
+ pResult.sha1 =
+ pInfo.getStringIdxInPool(reinterpret_cast<const char *>(pItem->second));
+
+ if (pResult.id == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS dependency table contains invalid source id string '%s'.",
+ pItem->first);
+ return false;
+ }
+
+ if (pResult.sha1 == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS dependency table contains invalid SHA-1 checksum string in '%s'.",
+ pItem->first);
+ return false;
+ }
+
+ return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::PragmaItem, RSInfo::PragmaListTy>(
+ rsinfo::PragmaItem &pResult,
+ const RSInfo &pInfo,
+ const RSInfo::PragmaListTy::const_iterator &pItem) {
+ pResult.key = pInfo.getStringIdxInPool(pItem->first);
+ pResult.value = pInfo.getStringIdxInPool(pItem->second);
+
+ if (pResult.key == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS pragma list contains invalid string '%s' for key.", pItem->first);
+ return false;
+ }
+
+ if (pResult.value == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS pragma list contains invalid string '%s' for value.",
+ pItem->second);
+ return false;
+ }
+
+ return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ObjectSlotItem, RSInfo::ObjectSlotListTy>(
+ rsinfo::ObjectSlotItem &pResult,
+ const RSInfo &pInfo,
+ const RSInfo::ObjectSlotListTy::const_iterator &pItem) {
+ pResult.slot = *pItem;
+ return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ExportVarNameItem, RSInfo::ExportVarNameListTy>(
+ rsinfo::ExportVarNameItem &pResult,
+ const RSInfo &pInfo,
+ const RSInfo::ExportVarNameListTy::const_iterator &pItem) {
+ pResult.name = pInfo.getStringIdxInPool(*pItem);
+
+ if (pResult.name == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS export vars contains invalid string '%s' for name.", *pItem);
+ return false;
+ }
+
+ return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ExportFuncNameItem,
+ RSInfo::ExportFuncNameListTy>(
+ rsinfo::ExportFuncNameItem &pResult,
+ const RSInfo &pInfo,
+ const RSInfo::ExportFuncNameListTy::const_iterator &pItem) {
+ pResult.name = pInfo.getStringIdxInPool(*pItem);
+
+ if (pResult.name == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS export funcs contains invalid string '%s' for name.", *pItem);
+ return false;
+ }
+
+ return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ExportForeachFuncItem,
+ RSInfo::ExportForeachFuncListTy>(
+ rsinfo::ExportForeachFuncItem &pResult,
+ const RSInfo &pInfo,
+ const RSInfo::ExportForeachFuncListTy::const_iterator &pItem) {
+ pResult.name = pInfo.getStringIdxInPool(pItem->first);
+ pResult.signature = pItem->second;
+
+ if (pResult.name == rsinfo::gInvalidStringIndex) {
+ ALOGE("RS export foreach contains invalid string '%s' for name.",
+ pItem->first);
+ return false;
+ }
+
+ return true;
+}
+
+template<typename ItemType, typename ItemContainer>
+inline bool helper_write_list(OutputFile &pOutput,
+ const RSInfo &pInfo,
+ const rsinfo::ListHeader &pHeader,
+ ItemContainer &pList) {
+ ItemType item;
+
+ for (typename ItemContainer::const_iterator item_iter = pList.begin(),
+ item_end = pList.end(); item_iter != item_end; item_iter++) {
+ // Convert each entry in the pList to ItemType.
+ if (!helper_adapt_list_item<ItemType, ItemContainer>(item,
+ pInfo,
+ item_iter)) {
+ return false;
+ }
+ // And write out an item.
+ if (pOutput.write(&item, sizeof(item)) != sizeof(item)) {
+ ALOGE("Cannot write out item of %s for RSInfo file %s! (%s)",
+ rsinfo::GetItemTypeName<ItemType>(), pOutput.getName().c_str(),
+ pOutput.getErrorMessage().c_str());
+ return false;
+ }
+ }
+
+ return true;
+}
+
+} // end anonymous namespace
+
+bool RSInfo::write(OutputFile &pOutput) {
+ off_t initial_offset = pOutput.tell();
+ const char *output_filename = pOutput.getName().c_str();
+
+ if (pOutput.hasError()) {
+ ALOGE("Invalid RS info file %s for output! (%s)",
+ output_filename, pOutput.getErrorMessage().c_str());
+ return false;
+ }
+
+ // Layout.
+ if (!layout(initial_offset)) {
+ return false;
+ }
+
+ // Write header.
+ if (pOutput.write(&mHeader, sizeof(mHeader)) != sizeof(mHeader)) {
+ ALOGE("Cannot write out the header for RSInfo file %s! (%s)",
+ output_filename, pOutput.getErrorMessage().c_str());
+ return false;
+ }
+
+ // Write string pool.
+ if (static_cast<size_t>(pOutput.write(mStringPool, mHeader.strPoolSize))
+ != mHeader.strPoolSize) {
+ ALOGE("Cannot write out the string pool for RSInfo file %s! (%s)",
+ output_filename, pOutput.getErrorMessage().c_str());
+ return false;
+ }
+
+ // Write dependencyTable.
+ if (!helper_write_list<rsinfo::DependencyTableItem, DependencyTableTy>
+ (pOutput, *this, mHeader.dependencyTable, mDependencyTable)) {
+ return false;
+ }
+
+ // Write pragmaList.
+ if (!helper_write_list<rsinfo::PragmaItem, PragmaListTy>
+ (pOutput, *this, mHeader.pragmaList, mPragmas)) {
+ return false;
+ }
+
+ // Write objectSlotList.
+ if (!helper_write_list<rsinfo::ObjectSlotItem, ObjectSlotListTy>
+ (pOutput, *this, mHeader.objectSlotList, mObjectSlots)) {
+ return false;
+ }
+
+ // Write exportVarNameList.
+ if (!helper_write_list<rsinfo::ExportVarNameItem, ExportVarNameListTy>
+ (pOutput, *this, mHeader.exportVarNameList, mExportVarNames)) {
+ return false;
+ }
+
+ // Write exportFuncNameList.
+ if (!helper_write_list<rsinfo::ExportFuncNameItem, ExportFuncNameListTy>
+ (pOutput, *this, mHeader.exportFuncNameList, mExportFuncNames)) {
+ return false;
+ }
+
+ // Write exportForeachFuncList.
+ if (!helper_write_list<rsinfo::ExportForeachFuncItem, ExportForeachFuncListTy>
+ (pOutput, *this, mHeader.exportForeachFuncList, mExportForeachFuncs)) {
+ return false;
+ }
+
+ return true;
+}
diff --git a/lib/ExecutionEngine/RSScript.cpp b/lib/ExecutionEngine/RSScript.cpp
new file mode 100644
index 0000000..7ec84d9
--- /dev/null
+++ b/lib/ExecutionEngine/RSScript.cpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RSScript.h"
+
+#include <cstring>
+
+#include <llvm/ADT/STLExtras.h>
+
+#include "DebugHelper.h"
+
+using namespace bcc;
+
+RSScript::SourceDependency::SourceDependency(const std::string &pSourceName,
+ const uint8_t *pSHA1)
+ : mSourceName(pSourceName) {
+ ::memcpy(mSHA1, pSHA1, sizeof(mSHA1));
+ return;
+}
+
+RSScript::RSScript(Source &pSource)
+ : Script(pSource), mInfo(NULL), mCompilerVersion(0),
+ mOptimizationLevel(kOptLvl3) { }
+
+RSScript::~RSScript() {
+ llvm::DeleteContainerPointers(mSourceDependencies);
+}
+
+bool RSScript::doReset() {
+ mInfo = NULL;
+ mCompilerVersion = 0;
+ mOptimizationLevel = kOptLvl3;
+ llvm::DeleteContainerPointers(mSourceDependencies);
+ return true;
+}
+
+bool RSScript::addSourceDependency(const std::string &pSourceName,
+ const uint8_t *pSHA1) {
+ SourceDependency *source_dep =
+ new (std::nothrow) SourceDependency(pSourceName, pSHA1);
+ if (source_dep == NULL) {
+ ALOGE("Out of memory when record dependency information of `%s'!",
+ pSourceName.c_str());
+ return false;
+ }
+
+ mSourceDependencies.push_back(source_dep);
+ return true;
+}
diff --git a/lib/ExecutionEngine/RSScript.h b/lib/ExecutionEngine/RSScript.h
new file mode 100644
index 0000000..1ae9c9d
--- /dev/null
+++ b/lib/ExecutionEngine/RSScript.h
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_RS_SCRIPT_H
+#define BCC_EXECUTION_ENGINE_RS_SCRIPT_H
+
+#include <string>
+
+#include <llvm/ADT/SmallVector.h>
+#include <llvm/Support/CodeGen.h>
+
+#include "Script.h"
+
+namespace bcc {
+
+class RSInfo;
+class Source;
+
+class RSScript : public Script {
+public:
+ class SourceDependency {
+ private:
+ std::string mSourceName;
+ uint8_t mSHA1[20];
+
+ public:
+ SourceDependency(const std::string &pSourceName,
+ const uint8_t *pSHA1);
+
+ inline const std::string &getSourceName() const
+ { return mSourceName; }
+
+ inline const uint8_t *getSHA1Checksum() const
+ { return mSHA1; }
+ };
+ typedef llvm::SmallVectorImpl<SourceDependency *> SourceDependencyListTy;
+
+ // This is one-one mapping with the llvm::CodeGenOpt::Level in
+ // llvm/Support/CodeGen.h. Therefore, value of this type can safely cast
+ // to llvm::CodeGenOpt::Level. This makes RSScript LLVM-free.
+ enum OptimizationLevel {
+ kOptLvl0, // -O0
+ kOptLvl1, // -O1
+ kOptLvl2, // -O2, -Os
+ kOptLvl3 // -O3
+ };
+
+private:
+ llvm::SmallVector<SourceDependency *, 4> mSourceDependencies;
+
+ const RSInfo *mInfo;
+
+ unsigned mCompilerVersion;
+
+ OptimizationLevel mOptimizationLevel;
+
+private:
+ // This will be invoked when the containing source has been reset.
+ virtual bool doReset();
+
+public:
+ RSScript(Source &pSource);
+
+ // Add dependency information for this script given the source named
+ // pSourceName. pSHA1 is the SHA-1 checksum of the given source. Return
+ // false on error.
+ bool addSourceDependency(const std::string &pSourceName,
+ const uint8_t *pSHA1);
+
+ const SourceDependencyListTy &getSourceDependencies() const
+ { return mSourceDependencies; }
+
+ // Set the associated RSInfo of the script.
+ void setInfo(const RSInfo *pInfo)
+ { mInfo = pInfo; }
+
+ const RSInfo *getInfo() const
+ { return mInfo; }
+
+ void setCompilerVersion(unsigned pCompilerVersion)
+ { mCompilerVersion = pCompilerVersion; }
+
+ unsigned getCompilerVersion() const
+ { return mCompilerVersion; }
+
+ void setOptimizationLevel(OptimizationLevel pOptimizationLevel)
+ { mOptimizationLevel = pOptimizationLevel; }
+
+ OptimizationLevel getOptimizationLevel() const
+ { return mOptimizationLevel; }
+
+ ~RSScript();
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_SCRIPT_H
diff --git a/lib/ExecutionEngine/RSTransforms.h b/lib/ExecutionEngine/RSTransforms.h
new file mode 100644
index 0000000..ec5f690
--- /dev/null
+++ b/lib/ExecutionEngine/RSTransforms.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_RS_TRANSFORMS_H
+#define BCC_EXECUTION_ENGINE_RS_TRANSFORMS_H
+
+#include "RSInfo.h"
+
+namespace llvm {
+ class ModulePass;
+}
+
+namespace bcc {
+
+llvm::ModulePass *
+createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs);
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_TRANSFORMS_H
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
new file mode 100644
index 0000000..785915c
--- /dev/null
+++ b/lib/ExecutionEngine/Script.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2010-2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Script.h"
+
+#include "Source.h"
+
+using namespace bcc;
+
+bool Script::reset(Source &pSource, bool pPreserveCurrent) {
+ if (mSource == &pSource) {
+ return false;
+ }
+
+ if (!pPreserveCurrent) {
+ delete mSource;
+ }
+ mSource = &pSource;
+ return doReset();
+}
+
+bool Script::mergeSource(Source &pSource, bool pPreserveSource) {
+ return mSource->merge(pSource, pPreserveSource);
+}
diff --git a/lib/ExecutionEngine/Script.h b/lib/ExecutionEngine/Script.h
new file mode 100644
index 0000000..aaa5eba
--- /dev/null
+++ b/lib/ExecutionEngine/Script.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2010-2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SCRIPT_H
+#define BCC_EXECUTION_ENGINE_SCRIPT_H
+
+namespace bcc {
+
+class Source;
+
+class Script {
+private:
+ // This is the source associated with this object and is going to be
+ // compiled.
+ Source *mSource;
+
+protected:
+ // This hook will be invoked after the script object is succssfully reset
+ // itself.
+ virtual bool doReset()
+ { return true; }
+
+public:
+ Script(Source &pSource) : mSource(&pSource) { }
+
+ virtual ~Script() { }
+
+ // Reset this object with the new source supplied. Return false if this
+ // object remains unchanged after the call (e.g., the supplied source is
+ // the same with the one contain in this object.) If pPreserveCurrent is
+ // false, the current containing source will be destroyed after successfully
+ // reset.
+ bool reset(Source &pSource, bool pPreserveCurrent = false);
+
+ // Merge (or link) another source into the current source associated with
+ // this Script object. Return false on error.
+ //
+ // This is equivalent to the call to Script::merge(...) on mSource.
+ bool mergeSource(Source &pSource, bool pPreserveSource = false);
+
+ inline Source &getSource()
+ { return *mSource; }
+ inline const Source &getSource() const
+ { return *mSource; }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SCRIPT_H
diff --git a/lib/ExecutionEngine/Sha1Helper.cpp b/lib/ExecutionEngine/Sha1Helper.cpp
new file mode 100644
index 0000000..07a995f
--- /dev/null
+++ b/lib/ExecutionEngine/Sha1Helper.cpp
@@ -0,0 +1,97 @@
+/*
+ * copyright 2010, the android open source project
+ *
+ * licensed under the apache license, version 2.0 (the "license");
+ * you may not use this file except in compliance with the license.
+ * you may obtain a copy of the license at
+ *
+ * http://www.apache.org/licenses/license-2.0
+ *
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the license is distributed on an "as is" basis,
+ * without warranties or conditions of any kind, either express or implied.
+ * see the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+#include "Sha1Helper.h"
+
+#include "Config.h"
+
+#include "DebugHelper.h"
+#include "InputFile.h"
+
+#include <string.h>
+
+#include <utils/StopWatch.h>
+
+#include <sha1.h>
+
+namespace bcc {
+
+unsigned char sha1LibBCC_SHA1[20];
+char const *pathLibBCC_SHA1 = "/system/lib/libbcc.so.sha1";
+
+unsigned char sha1LibRS[20];
+char const *pathLibRS = "/system/lib/libRS.so";
+
+void calcSHA1(unsigned char *result, char const *data, size_t size) {
+ SHA1_CTX hashContext;
+
+ SHA1Init(&hashContext);
+ SHA1Update(&hashContext,
+ reinterpret_cast<const unsigned char *>(data),
+ static_cast<unsigned long>(size));
+
+ SHA1Final(result, &hashContext);
+}
+
+
+void calcFileSHA1(unsigned char *result, char const *filename) {
+ android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
+
+ InputFile file(filename);
+
+ if (file.hasError()) {
+ ALOGE("Unable to open the file %s before SHA-1 checksum "
+ "calculation! (%s)", filename, file.getErrorMessage().c_str());
+ memset(result, '\0', 20);
+ return;
+ }
+
+ SHA1_CTX hashContext;
+ SHA1Init(&hashContext);
+
+ char buf[256];
+ while (true) {
+ ssize_t nread = file.read(buf, sizeof(buf));
+
+ if (nread < 0) {
+ break;
+ }
+
+ SHA1Update(&hashContext,
+ reinterpret_cast<unsigned char *>(buf),
+ static_cast<unsigned long>(nread));
+
+ if ((size_t)nread < sizeof(buf)) {
+ // finished.
+ break;
+ }
+ }
+
+ SHA1Final(result, &hashContext);
+}
+
+void readSHA1(unsigned char *result, int result_size, char const *filename) {
+ InputFile file(filename);
+ if (file.hasError()) {
+ ALOGE("Unable to open the binary sha1 file %s! (%s)", filename,
+ file.getErrorMessage().c_str());
+ memset(result, '\0', result_size);
+ return;
+ }
+ file.read((char *)result, result_size);
+}
+
+} // namespace bcc
diff --git a/lib/ExecutionEngine/Sha1Helper.h b/lib/ExecutionEngine/Sha1Helper.h
new file mode 100644
index 0000000..fe13b7a
--- /dev/null
+++ b/lib/ExecutionEngine/Sha1Helper.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_SHA1HELPER_H
+#define BCC_SHA1HELPER_H
+
+#include "Config.h"
+
+#include <stddef.h>
+
+namespace bcc {
+ extern unsigned char sha1LibBCC_SHA1[20];
+ extern char const *pathLibBCC_SHA1;
+
+ extern unsigned char sha1LibRS[20];
+ extern char const *pathLibRS;
+
+ void calcSHA1(unsigned char *result, char const *data, size_t size);
+
+ void calcFileSHA1(unsigned char *result, char const *filename);
+
+ // Read binary representation of sha1 from filename.
+ void readSHA1(unsigned char *result, int resultsize, char const *filename);
+}
+
+#endif // BCC_SHA1HELPER_H
diff --git a/lib/ExecutionEngine/Source.cpp b/lib/ExecutionEngine/Source.cpp
new file mode 100644
index 0000000..754f3e5
--- /dev/null
+++ b/lib/ExecutionEngine/Source.cpp
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Source.h"
+
+#include <new>
+
+#include <llvm/Bitcode/ReaderWriter.h>
+#include <llvm/LLVMContext.h>
+#include <llvm/Linker.h>
+#include <llvm/Module.h>
+#include <llvm/Support/MemoryBuffer.h>
+#include <llvm/Support/system_error.h>
+
+#include "BCCContext.h"
+#include "BCCContextImpl.h"
+#include "DebugHelper.h"
+
+namespace {
+
+// Helper function to load the bitcode. This uses "bitcode lazy load" feature to
+// reduce the startup time. On success, return the LLVM module object created
+// and take the ownership of input memory buffer (i.e., pInput). On error,
+// return NULL and will NOT take the ownership of pInput.
+static inline llvm::Module *helper_load_bitcode(llvm::LLVMContext &pContext,
+ llvm::MemoryBuffer *pInput) {
+ std::string error;
+ llvm::Module *module = llvm::getLazyBitcodeModule(pInput, pContext, &error);
+
+ if (module == NULL) {
+ ALOGE("Unable to parse the given bitcode file `%s'! (%s)",
+ pInput->getBufferIdentifier(), error.c_str());
+ }
+
+ return module;
+}
+
+} // end anonymous namespace
+
+namespace bcc {
+
+Source *Source::CreateFromBuffer(BCCContext &pContext,
+ const char *pName,
+ const char *pBitcode,
+ size_t pBitcodeSize) {
+ llvm::StringRef input_data(pBitcode, pBitcodeSize);
+ llvm::MemoryBuffer *input_memory =
+ llvm::MemoryBuffer::getMemBuffer(input_data, pName);
+
+ if (input_memory == NULL) {
+ ALOGE("Unable to load bitcode `%s' from buffer!", pName);
+ return NULL;
+ }
+
+ llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+ input_memory);
+ if (module == NULL) {
+ delete input_memory;
+ return NULL;
+ }
+
+ Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+ if (result == NULL) {
+ delete module;
+ }
+
+ return result;
+}
+
+
+Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) {
+ llvm::OwningPtr<llvm::MemoryBuffer> input_data;
+
+ llvm::error_code ec = llvm::MemoryBuffer::getFile(pPath, input_data);
+ if (ec != llvm::error_code::success()) {
+ ALOGE("Failed to load bitcode from path %s! (%s)", pPath.c_str(),
+ ec.message().c_str());
+ return NULL;
+ }
+
+ llvm::MemoryBuffer *input_memory = input_data.take();
+ llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+ input_memory);
+ if (module == NULL) {
+ delete input_memory;
+ return NULL;
+ }
+
+ Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+ if (result == NULL) {
+ delete module;
+ }
+
+ return result;
+}
+
+
+Source *Source::CreateFromModule(BCCContext &pContext, llvm::Module &pModule,
+ bool pNoDelete) {
+ Source *result = new (std::nothrow) Source(pContext, pModule, pNoDelete);
+ if (result == NULL) {
+ ALOGE("Out of memory during Source object allocation for `%s'!",
+ pModule.getModuleIdentifier().c_str());
+ }
+ return result;
+}
+
+Source::Source(BCCContext &pContext, llvm::Module &pModule, bool pNoDelete)
+ : mContext(pContext), mModule(&pModule), mNoDelete(pNoDelete) {
+ pContext.addSource(*this);
+}
+
+Source::~Source() {
+ mContext.removeSource(*this);
+ if (!mNoDelete)
+ delete mModule;
+}
+
+bool Source::merge(Source &pSource, bool pPreserveSource) {
+ std::string error;
+ llvm::Linker::LinkerMode mode =
+ ((pPreserveSource) ? llvm::Linker::PreserveSource :
+ llvm::Linker::DestroySource);
+
+ if (llvm::Linker::LinkModules(mModule, &pSource.getModule(),
+ mode, &error) != 0) {
+ ALOGE("Failed to link source `%s' with `%s' (%s)!",
+ getIdentifier().c_str(),
+ pSource.getIdentifier().c_str(),
+ error.c_str());
+ return false;
+ }
+
+ if (!pPreserveSource) {
+ pSource.mNoDelete = true;
+ delete &pSource;
+ }
+
+ return true;
+}
+
+Source *Source::CreateEmpty(BCCContext &pContext, const std::string &pName) {
+ // Create an empty module
+ llvm::Module *module =
+ new (std::nothrow) llvm::Module(pName, pContext.mImpl->mLLVMContext);
+
+ if (module == NULL) {
+ ALOGE("Out of memory when creating empty LLVM module `%s'!", pName.c_str());
+ return NULL;
+ }
+
+ Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+ if (result == NULL) {
+ delete module;
+ }
+
+ return result;
+}
+
+const std::string &Source::getIdentifier() const {
+ return mModule->getModuleIdentifier();
+}
+
+} // namespace bcc
diff --git a/lib/ExecutionEngine/Source.h b/lib/ExecutionEngine/Source.h
new file mode 100644
index 0000000..b9a98e8
--- /dev/null
+++ b/lib/ExecutionEngine/Source.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SOURCE_H
+#define BCC_EXECUTION_ENGINE_SOURCE_H
+
+#include <string>
+
+namespace llvm {
+ class Module;
+}
+
+namespace bcc {
+
+class BCCContext;
+
+class Source {
+private:
+ BCCContext &mContext;
+ llvm::Module *mModule;
+
+ // If true, destructor won't destroy the mModule.
+ bool mNoDelete;
+
+private:
+ Source(BCCContext &pContext, llvm::Module &pModule, bool pNoDelete = false);
+
+public:
+ static Source *CreateFromBuffer(BCCContext &pContext,
+ const char *pName,
+ const char *pBitcode,
+ size_t pBitcodeSize);
+
+ static Source *CreateFromFile(BCCContext &pContext,
+ const std::string &pPath);
+
+ // Create a Source object from an existing module. If pNoDelete
+ // is true, destructor won't call delete on the given module.
+ static Source *CreateFromModule(BCCContext &pContext,
+ llvm::Module &pModule,
+ bool pNoDelete = false);
+
+ static Source *CreateEmpty(BCCContext &pContext, const std::string &pName);
+
+ // Merge the current source with pSource. If pPreserveSource is false, pSource
+ // will be destroyed after successfully merged. Return false on error.
+ bool merge(Source &pSource, bool pPreserveSource = false);
+
+ inline BCCContext &getContext()
+ { return mContext; }
+ inline const BCCContext &getContext() const
+ { return mContext; }
+
+ inline llvm::Module &getModule()
+ { return *mModule; }
+ inline const llvm::Module &getModule() const
+ { return *mModule; }
+
+ // Get the "identifier" of the bitcode. This will return the value of pName
+ // when it's created using CreateFromBuffer and pPath if CreateFromFile().
+ const std::string &getIdentifier() const;
+
+ ~Source();
+};
+
+} // namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SOURCE_H
diff --git a/lib/ExecutionEngine/SymbolResolverInterface.h b/lib/ExecutionEngine/SymbolResolverInterface.h
new file mode 100644
index 0000000..c80073d
--- /dev/null
+++ b/lib/ExecutionEngine/SymbolResolverInterface.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_INTERFACE_H
+#define BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_INTERFACE_H
+
+#include <cstddef>
+
+namespace bcc {
+
+class SymbolResolverInterface {
+public:
+ static void *LookupFunction(void *pResolver, const char *pName) {
+ SymbolResolverInterface *resolver =
+ reinterpret_cast<SymbolResolverInterface*>(pResolver);
+ return ((resolver != NULL) ? resolver->getAddress(pName) : NULL);
+ }
+
+ // Should this be a const method?
+ virtual void *getAddress(const char *pName) = 0;
+
+ virtual ~SymbolResolverInterface() { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_INTERFACE_H
diff --git a/lib/ExecutionEngine/SymbolResolverProxy.cpp b/lib/ExecutionEngine/SymbolResolverProxy.cpp
index 5f3e7dc..29929d9 100644
--- a/lib/ExecutionEngine/SymbolResolverProxy.cpp
+++ b/lib/ExecutionEngine/SymbolResolverProxy.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "bcc/ExecutionEngine/SymbolResolverProxy.h"
+#include "SymbolResolverProxy.h"
using namespace bcc;
diff --git a/lib/ExecutionEngine/SymbolResolverProxy.h b/lib/ExecutionEngine/SymbolResolverProxy.h
new file mode 100644
index 0000000..7bbdac5
--- /dev/null
+++ b/lib/ExecutionEngine/SymbolResolverProxy.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_PROXY_H
+#define BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_PROXY_H
+
+#include "DebugHelper.h"
+#include "SymbolResolverInterface.h"
+
+#include <utils/Vector.h>
+
+namespace bcc {
+
+class SymbolResolverProxy : public SymbolResolverInterface {
+private:
+ android::Vector<SymbolResolverInterface *> mChain;
+
+public:
+ SymbolResolverProxy() { }
+
+ void chainResolver(SymbolResolverInterface &pResolver);
+
+ virtual void *getAddress(const char *pName);
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_PROXY_H
diff --git a/lib/ExecutionEngine/SymbolResolvers.cpp b/lib/ExecutionEngine/SymbolResolvers.cpp
index 62914ac..26941f2 100644
--- a/lib/ExecutionEngine/SymbolResolvers.cpp
+++ b/lib/ExecutionEngine/SymbolResolvers.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "bcc/ExecutionEngine/SymbolResolvers.h"
+#include "SymbolResolvers.h"
#include <dlfcn.h>
diff --git a/lib/ExecutionEngine/SymbolResolvers.h b/lib/ExecutionEngine/SymbolResolvers.h
new file mode 100644
index 0000000..909211f
--- /dev/null
+++ b/lib/ExecutionEngine/SymbolResolvers.h
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SYMBOL_RESOLVERS_H
+#define BCC_EXECUTION_ENGINE_SYMBOL_RESOLVERS_H
+
+#include <cstdlib>
+#include <cstring>
+
+#include "SymbolResolverInterface.h"
+
+namespace bcc {
+
+/*
+ * Symbol lookup via dlopen()/dlsym().
+ */
+class DyldSymbolResolver : public SymbolResolverInterface {
+public:
+ typedef void *HandleTy;
+
+private:
+ HandleTy mHandle;
+ char *mError;
+
+public:
+ // If pFileName is NULL, it will search symbol in the current process image.
+ DyldSymbolResolver(const char *pFileName, bool pLazyBinding = true);
+
+ virtual void *getAddress(const char *pName);
+
+ inline bool hasError() const
+ { return (mError != NULL); }
+ inline const char *getError() const
+ { return mError; }
+
+ ~DyldSymbolResolver();
+};
+
+/*
+ * Symbol lookup by searching through an array of SymbolMap.
+ */
+template<typename Subclass>
+class ArraySymbolResolver : public SymbolResolverInterface {
+public:
+ typedef struct {
+ // Symbol name
+ const char *mName;
+ // Symbol address
+ void *mAddr;
+ } SymbolMap;
+
+private:
+ // True if the symbol name is sorted in the array.
+ bool mSorted;
+
+ static int CompareSymbolName(const void *pA, const void *pB) {
+ return ::strcmp(reinterpret_cast<const SymbolMap *>(pA)->mName,
+ reinterpret_cast<const SymbolMap *>(pB)->mName);
+ }
+
+public:
+ ArraySymbolResolver(bool pSorted = false) : mSorted(pSorted) { }
+
+ virtual void *getAddress(const char *pName) {
+ const SymbolMap *result = NULL;
+
+ if (mSorted) {
+ // Use binary search.
+ const SymbolMap key = { pName, NULL };
+
+ result = reinterpret_cast<SymbolMap *>(
+ ::bsearch(&key, Subclass::SymbolArray,
+ Subclass::NumSymbols,
+ sizeof(SymbolMap),
+ CompareSymbolName));
+ } else {
+ // Use linear search.
+ for (size_t i = 0; i < Subclass::NumSymbols; i++) {
+ if (::strcmp(Subclass::SymbolArray[i].mName, pName) == 0) {
+ result = &Subclass::SymbolArray[i];
+ break;
+ }
+ }
+ }
+
+ return ((result != NULL) ? result->mAddr : NULL);
+ }
+};
+
+template<typename ContextTy = void *>
+class LookupFunctionSymbolResolver : public SymbolResolverInterface {
+public:
+ typedef void *(*LookupFunctionTy)(ContextTy pContext, const char *pName);
+
+private:
+ LookupFunctionTy mLookupFunc;
+ ContextTy mContext;
+
+public:
+ LookupFunctionSymbolResolver(LookupFunctionTy pLookupFunc = NULL,
+ ContextTy pContext = NULL)
+ : mLookupFunc(pLookupFunc), mContext(pContext) { }
+
+ virtual void *getAddress(const char *pName) {
+ return ((mLookupFunc != NULL) ? mLookupFunc(mContext, pName) : NULL);
+ }
+
+ inline LookupFunctionTy getLookupFunction() const
+ { return mLookupFunc; }
+ inline ContextTy getContext() const
+ { return mContext; }
+
+ inline void setLookupFunction(LookupFunctionTy pLookupFunc)
+ { mLookupFunc = pLookupFunc; }
+ inline void setContext(ContextTy pContext)
+ { mContext = pContext; }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SYMBOL_RESOLVERS_H
diff --git a/lib/ExecutionEngine/TargetCompilerConfigs.cpp b/lib/ExecutionEngine/TargetCompilerConfigs.cpp
new file mode 100644
index 0000000..2e2f5da
--- /dev/null
+++ b/lib/ExecutionEngine/TargetCompilerConfigs.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "TargetCompilerConfigs.h"
+
+using namespace bcc;
+
+//===----------------------------------------------------------------------===//
+// ARM
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_ARM_CODEGEN)
+
+void ARMCompilerConfig::GetFeatureVector(std::vector<std::string> &pAttributes,
+ bool pEnableNEON) {
+#if defined(ARCH_ARM_HAVE_VFP)
+ pAttributes.push_back("+vfp3");
+# if !defined(ARCH_ARM_HAVE_VFP_D32)
+ pAttributes.push_back("+d16");
+# endif
+#endif
+
+#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
+ if (pEnableNEON) {
+ // FIXME(all): Turn NEON back on after debugging the rebase.
+ //attributes.push_back("+neon");
+ //attributes.push_back("+neonfp");
+ pAttributes.push_back("-neon");
+ pAttributes.push_back("-neonfp");
+ } else {
+ pAttributes.push_back("-neon");
+ pAttributes.push_back("-neonfp");
+ }
+#else
+ pAttributes.push_back("-neon");
+ pAttributes.push_back("-neonfp");
+#endif
+
+ return;
+}
+
+ARMCompilerConfig::ARMCompilerConfig()
+ : CompilerConfig(DEFAULT_ARM_TRIPLE_STRING) {
+
+ // Enable NEON by default.
+ mEnableNEON = true;
+
+ std::vector<std::string> attributes;
+ GetFeatureVector(attributes, /* pEnableNEON */mEnableNEON);
+ setFeatureString(attributes);
+
+ return;
+}
+
+bool ARMCompilerConfig::enableNEON(bool pEnable) {
+#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
+ if (mEnableNEON != pEnable) {
+ std::vector<std::string> attributes;
+ GetFeatureVector(attributes, pEnable);
+ setFeatureString(attributes);
+ mEnableNEON = pEnable;
+ return true;
+ }
+ // Fall-through
+#endif
+ return false;
+}
+#endif // defined(PROVIDE_ARM_CODEGEN)
diff --git a/lib/ExecutionEngine/TargetCompilerConfigs.h b/lib/ExecutionEngine/TargetCompilerConfigs.h
new file mode 100644
index 0000000..bf10a7a
--- /dev/null
+++ b/lib/ExecutionEngine/TargetCompilerConfigs.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_TARGET_COMPILER_CONFIGS_H
+#define BCC_EXECUTION_ENGINE_TARGET_COMPILER_CONFIGS_H
+
+#include "CompilerConfig.h"
+#include "Config.h"
+
+namespace bcc {
+
+//===----------------------------------------------------------------------===//
+// ARM
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_ARM_CODEGEN)
+class ARMCompilerConfig : public CompilerConfig {
+private:
+ bool mEnableNEON;
+
+ static void GetFeatureVector(std::vector<std::string> &pAttributes,
+ bool pEnableNEON);
+
+public:
+ ARMCompilerConfig();
+
+ // Return true if config has been changed after returning from this function.
+ bool enableNEON(bool pEnable = true);
+};
+#endif // defined(PROVIDE_ARM_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// MIPS
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_MIPS_CODEGEN)
+class MipsCompilerConfig : public CompilerConfig {
+public:
+ MipsCompilerConfig() : CompilerConfig(DEFAULT_MIPS_TRIPLE_STRING) {}
+};
+#endif // defined(PROVIDE_MIPS_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// X86 and X86_64
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_X86_CODEGEN)
+class X86FamilyCompilerConfigBase : public CompilerConfig {
+protected:
+ X86FamilyCompilerConfigBase(const std::string &pTriple)
+ : CompilerConfig(pTriple) {
+ // Disable frame pointer elimination optimization on x86 family.
+ getTargetOptions().NoFramePointerElim = true;
+ return;
+ }
+};
+
+class X86_32CompilerConfig : public X86FamilyCompilerConfigBase {
+public:
+ X86_32CompilerConfig() :
+ X86FamilyCompilerConfigBase(DEFAULT_X86_TRIPLE_STRING) { }
+};
+
+class X86_64CompilerConfig : public X86FamilyCompilerConfigBase {
+public:
+ X86_64CompilerConfig() :
+ X86FamilyCompilerConfigBase(DEFAULT_X86_64_TRIPLE_STRING) {
+ setCodeModel(llvm::CodeModel::Medium);
+ }
+};
+#endif // defined(PROVIDE_X86_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// Default target
+//===----------------------------------------------------------------------===//
+class DefaultCompilerConfig : public
+#if defined(DEFAULT_ARM_CODEGEN)
+ ARMCompilerConfig
+#elif defined(DEFAULT_MIPS_CODEGEN)
+ MipsCompilerConfig
+#elif defined(DEFAULT_X86_CODEGEN)
+ X86_32CompilerConfig
+#elif defined(DEFAULT_X86_64_CODEGEN)
+ X86_64CompilerConfig
+#else
+# error "Unsupported Default Target!"
+#endif
+{ };
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_TARGET_COMPILER_CONFIGS_H
diff --git a/lib/ExecutionEngine/bcc.cpp b/lib/ExecutionEngine/bcc.cpp
new file mode 100644
index 0000000..b830ec2
--- /dev/null
+++ b/lib/ExecutionEngine/bcc.cpp
@@ -0,0 +1,442 @@
+/*
+ * Copyright 2010-2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Bitcode compiler (bcc) for Android:
+// This is an eager-compilation JIT running on Android.
+
+#include <bcc/bcc.h>
+
+#include <llvm/Support/CodeGen.h>
+
+#include <utils/StopWatch.h>
+
+#include "DebugHelper.h"
+#include "Initialization.h"
+#include "RSExecutable.h"
+#include "RSScript.h"
+#include "Sha1Helper.h"
+#include "Source.h"
+
+#include "bcc_internal.h"
+#include <bcinfo/BitcodeWrapper.h>
+
+using namespace bcc;
+
+namespace llvm {
+ class Module;
+}
+
+static bool bccBuildStampPrinted = false;
+
+static void bccPrintBuildStamp() {
+ if (!bccBuildStampPrinted) {
+ ALOGI("LIBBCC build time: %s", bccGetBuildTime());
+ ALOGI("LIBBCC build revision: %s", bccGetBuildRev());
+ bccBuildStampPrinted = true;
+ }
+}
+
+extern "C" BCCScriptRef bccCreateScript() {
+ BCC_FUNC_LOGGER();
+ bccPrintBuildStamp();
+ init::Initialize();
+ RSScriptContext *rsctx = new (std::nothrow) RSScriptContext();
+ if (rsctx != NULL) {
+ rsctx->script = NULL;
+ rsctx->result = NULL;
+ }
+ return wrap(rsctx);
+}
+
+
+extern "C" void bccDisposeScript(BCCScriptRef script) {
+ BCC_FUNC_LOGGER();
+ RSScriptContext *rsctx = unwrap(script);
+ if (rsctx != NULL) {
+ delete rsctx->script;
+ delete rsctx->result;
+ }
+ delete rsctx;
+}
+
+
+extern "C" int bccRegisterSymbolCallback(BCCScriptRef script,
+ BCCSymbolLookupFn pFn,
+ void *pContext) {
+ BCC_FUNC_LOGGER();
+ unwrap(script)->driver.setRSRuntimeLookupFunction(pFn);
+ unwrap(script)->driver.setRSRuntimeLookupContext(pContext);
+ return BCC_NO_ERROR;
+}
+
+
+extern "C" int bccGetError(BCCScriptRef script) {
+ BCC_FUNC_LOGGER();
+ return BCC_DEPRECATED_API;
+}
+
+static bool helper_add_source(RSScriptContext *pCtx,
+ char const *pName,
+ char const *pBitcode,
+ size_t pBitcodeSize,
+ unsigned long pFlags,
+ bool pIsLink) {
+ bool need_dependency_check = !(pFlags & BCC_SKIP_DEP_SHA1);
+ if (!pName && need_dependency_check) {
+ pFlags |= BCC_SKIP_DEP_SHA1;
+
+ ALOGW("It is required to give resName for sha1 dependency check.\n");
+ ALOGW("Sha1sum dependency check will be skipped.\n");
+ ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n");
+ }
+
+ Source *source = Source::CreateFromBuffer(pCtx->context, pName,
+ pBitcode, pBitcodeSize);
+ if (source == NULL) {
+ return false;
+ }
+
+ if (pCtx->script == NULL) {
+ pCtx->script = new (std::nothrow) RSScript(*source);
+ if (pCtx->script == NULL) {
+ ALOGE("Out of memory during script creation.");
+ return false;
+ }
+ } else {
+ bool result;
+ if (pIsLink) {
+ result = pCtx->script->mergeSource(*source);
+ } else {
+ result = pCtx->script->reset(*source);
+ }
+ if (!result) {
+ return false;
+ } else {
+ bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
+ pCtx->script->setCompilerVersion(wrapper.getCompilerVersion());
+ pCtx->script->setOptimizationLevel(
+ static_cast<RSScript::OptimizationLevel>(
+ wrapper.getOptimizationLevel()));
+ }
+ }
+
+ if (need_dependency_check) {
+ uint8_t sha1[20];
+ calcSHA1(sha1, pBitcode, pBitcodeSize);
+ if (!pCtx->script->addSourceDependency(pName, sha1)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool helper_add_source(RSScriptContext *pCtx,
+ llvm::Module *pModule,
+ bool pIsLink) {
+ if (pModule == NULL) {
+ ALOGE("Cannot add null module to script!");
+ return false;
+ }
+
+ Source *source = Source::CreateFromModule(pCtx->context, *pModule, true);
+ if (source == NULL) {
+ return false;
+ }
+
+ if (pCtx->script == NULL) {
+ pCtx->script = new (std::nothrow) RSScript(*source);
+ if (pCtx->script == NULL) {
+ ALOGE("Out of memory during script creation.");
+ return false;
+ }
+ } else {
+ bool result;
+ if (pIsLink) {
+ result = pCtx->script->mergeSource(*source);
+ } else {
+ result = pCtx->script->reset(*source);
+ }
+ if (!result) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool helper_add_source(RSScriptContext *pCtx,
+ char const *pPath,
+ unsigned long pFlags,
+ bool pIsLink) {
+ bool need_dependency_check = !(pFlags & BCC_SKIP_DEP_SHA1);
+
+ Source *source = Source::CreateFromFile(pCtx->context, pPath);
+ if (source == NULL) {
+ return false;
+ }
+
+ if (pCtx->script == NULL) {
+ pCtx->script = new (std::nothrow) RSScript(*source);
+ if (pCtx->script == NULL) {
+ ALOGE("Out of memory during script creation.");
+ return false;
+ }
+ } else {
+ bool result;
+ if (pIsLink) {
+ result = pCtx->script->mergeSource(*source);
+ } else {
+ result = pCtx->script->reset(*source);
+ }
+ if (!result) {
+ return false;
+ }
+ }
+
+ if (need_dependency_check) {
+ uint8_t sha1[20];
+ calcFileSHA1(sha1, pPath);
+ if (!pCtx->script->addSourceDependency(pPath, sha1)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+extern "C" int bccReadBC(BCCScriptRef script,
+ char const *resName,
+ char const *bitcode,
+ size_t bitcodeSize,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return (helper_add_source(unwrap(script), resName,
+ bitcode, bitcodeSize,
+ flags, /* pIsLink */false) == false);
+}
+
+
+extern "C" int bccReadModule(BCCScriptRef script,
+ char const *resName /* deprecated */,
+ LLVMModuleRef module,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return (helper_add_source(unwrap(script), unwrap(module),
+ /* pIsLink */false) == false);
+}
+
+
+extern "C" int bccReadFile(BCCScriptRef script,
+ char const *path,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return (helper_add_source(unwrap(script), path,
+ flags, /* pIsLink */false) == false);
+}
+
+
+extern "C" int bccLinkBC(BCCScriptRef script,
+ char const *resName,
+ char const *bitcode,
+ size_t bitcodeSize,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return (helper_add_source(unwrap(script), resName,
+ bitcode, bitcodeSize,
+ flags, /* pIsLink */true) == false);
+}
+
+
+extern "C" int bccLinkFile(BCCScriptRef script,
+ char const *path,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return (helper_add_source(unwrap(script), path,
+ flags, /* pIsLink */true) == false);
+}
+
+
+extern "C" void bccMarkExternalSymbol(BCCScriptRef script, char const *name) {
+ BCC_FUNC_LOGGER();
+ return /* BCC_DEPRECATED_API */;
+}
+
+
+extern "C" int bccPrepareRelocatable(BCCScriptRef script,
+ char const *objPath,
+ bccRelocModelEnum RelocModel,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return BCC_DEPRECATED_API;
+}
+
+
+extern "C" int bccPrepareSharedObject(BCCScriptRef script,
+ char const *objPath,
+ char const *dsoPath,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+ return BCC_DEPRECATED_API;
+}
+
+
+extern "C" int bccPrepareExecutable(BCCScriptRef script,
+ char const *cacheDir,
+ char const *cacheName,
+ unsigned long flags) {
+ BCC_FUNC_LOGGER();
+
+ android::StopWatch compileTimer("bcc: PrepareExecutable time");
+
+ RSScriptContext *rsctx = unwrap(script);
+
+ if (rsctx->script == NULL) {
+ return 1;
+ }
+
+ // Construct the output path.
+ std::string output_path(cacheDir);
+ if (!output_path.empty() && (*output_path.rbegin() != '/')) {
+ output_path.append(1, '/');
+ }
+ output_path.append(cacheName);
+ output_path.append(".o");
+
+ // Make sure the result container is clean.
+ if (rsctx->result != NULL) {
+ delete rsctx->result;
+ rsctx->result = NULL;
+ }
+
+ rsctx->result = rsctx->driver.build(*rsctx->script, output_path);
+
+ return (rsctx->result == NULL);
+}
+
+
+extern "C" void *bccGetFuncAddr(BCCScriptRef script, char const *funcname) {
+ BCC_FUNC_LOGGER();
+
+ RSScriptContext *rsctx = unwrap(script);
+
+ void *addr = NULL;
+ if (rsctx->result != NULL) {
+ addr = rsctx->result->getSymbolAddress(funcname);
+ }
+
+#if DEBUG_BCC_REFLECT
+ ALOGD("Function Address: %s --> %p\n", funcname, addr);
+#endif
+
+ return addr;
+}
+
+
+extern "C" void bccGetExportVarList(BCCScriptRef script,
+ size_t varListSize,
+ void **varList) {
+ BCC_FUNC_LOGGER();
+
+ const RSScriptContext *rsctx = unwrap(script);
+ if (varList && rsctx->result) {
+ const android::Vector<void *> &export_var_addrs =
+ rsctx->result->getExportVarAddrs();
+ size_t count = export_var_addrs.size();
+
+ if (count > varListSize) {
+ count = varListSize;
+ }
+
+ for (size_t i = 0; i < count; ++i) {
+ varList[i] = export_var_addrs[i];
+ }
+
+#if DEBUG_BCC_REFLECT
+ ALOGD("ExportVarCount = %lu\n",
+ static_cast<unsigned long>(export_var_addrs.size()));
+
+ for (size_t i = 0; i < count; ++i) {
+ ALOGD("ExportVarList[%lu] = %p\n", static_cast<unsigned long>(i),
+ varList[i]);
+ }
+#endif
+ }
+}
+
+
+extern "C" void bccGetExportFuncList(BCCScriptRef script,
+ size_t funcListSize,
+ void **funcList) {
+ BCC_FUNC_LOGGER();
+
+ const RSScriptContext *rsctx = unwrap(script);
+ if (funcList && rsctx->result) {
+ const android::Vector<void *> &export_func_addrs =
+ rsctx->result->getExportFuncAddrs();
+ size_t count = export_func_addrs.size();
+
+ if (count > funcListSize) {
+ count = funcListSize;
+ }
+
+ for (size_t i = 0; i < count; ++i) {
+ funcList[i] = export_func_addrs[i];
+ }
+
+#if DEBUG_BCC_REFLECT
+ ALOGD("ExportFuncCount = %lu\n",
+ static_cast<unsigned long>(export_var_addrs.size()));
+
+ for (size_t i = 0; i < count; ++i) {
+ ALOGD("ExportFuncList[%lu] = %p\n", static_cast<unsigned long>(i),
+ varList[i]);
+ }
+#endif
+ }
+}
+
+
+extern "C" void bccGetExportForEachList(BCCScriptRef script,
+ size_t forEachListSize,
+ void **forEachList) {
+ BCC_FUNC_LOGGER();
+
+ const RSScriptContext *rsctx = unwrap(script);
+ if (forEachList && rsctx->result) {
+ const android::Vector<void *> &export_foreach_func_addrs =
+ rsctx->result->getExportForeachFuncAddrs();
+ size_t count = export_foreach_func_addrs.size();
+
+ if (count > forEachListSize) {
+ count = forEachListSize;
+ }
+
+ for (size_t i = 0; i < count; ++i) {
+ forEachList[i] = export_foreach_func_addrs[i];
+ }
+
+#if DEBUG_BCC_REFLECT
+ ALOGD("ExportForEachCount = %lu\n",
+ static_cast<unsigned long>(export_foreach_func_addrs.size()));
+
+ for (size_t i = 0; i < count; ++i) {
+ ALOGD("ExportForEachList[%lu] = %p\n", static_cast<unsigned long>(i),
+ forEachList[i]);
+ }
+#endif
+ }
+}
diff --git a/lib/ExecutionEngine/bcc_internal.h b/lib/ExecutionEngine/bcc_internal.h
new file mode 100644
index 0000000..c003d6c
--- /dev/null
+++ b/lib/ExecutionEngine/bcc_internal.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2010-2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_INTERNAL_H
+#define BCC_INTERNAL_H
+
+#include <bcc/bcc.h>
+
+#include "BCCContext.h"
+#include "RSCompilerDriver.h"
+
+#if defined(__cplusplus)
+
+#define BCC_OPAQUE_TYPE_CONVERSION(TRANSPARENT_TYPE, OPAQUE_TYPE) \
+ inline OPAQUE_TYPE wrap(TRANSPARENT_TYPE ptr) { \
+ return reinterpret_cast<OPAQUE_TYPE>(ptr); \
+ } \
+ \
+ inline TRANSPARENT_TYPE unwrap(OPAQUE_TYPE ptr) { \
+ return reinterpret_cast<TRANSPARENT_TYPE>(ptr); \
+ }
+
+namespace llvm {
+ class Module;
+}
+
+namespace bcc {
+
+class RSScript;
+class RSExecutable;
+
+struct RSScriptContext {
+ // The context required in libbcc.
+ BCCContext context;
+ // The compiler driver
+ RSCompilerDriver driver;
+ // The script hold the source which is about to compile.
+ RSScript *script;
+ // The compilation result.
+ RSExecutable *result;
+};
+
+BCC_OPAQUE_TYPE_CONVERSION(bcc::RSScriptContext *, BCCScriptRef);
+BCC_OPAQUE_TYPE_CONVERSION(llvm::Module *, LLVMModuleRef);
+
+} // namespace bcc
+
+#undef BCC_OPAQUE_TYPE_CONVERSION
+
+#endif // defined(__cplusplus)
+
+#endif // BCC_INTERNAL_H