1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_RS_COMPILER_DRIVER_H
18#define BCC_RS_COMPILER_DRIVER_H
19
20#include "bcc/ExecutionEngine/CompilerRTSymbolResolver.h"
21#include "bcc/ExecutionEngine/SymbolResolvers.h"
22#include "bcc/ExecutionEngine/SymbolResolverProxy.h"
23#include "bcc/Renderscript/RSInfo.h"
24#include "bcc/Renderscript/RSCompiler.h"
25#include "bcc/Renderscript/RSScript.h"
26
27namespace bcc {
28
29class BCCContext;
30class CompilerConfig;
31class RSExecutable;
32
33class RSCompilerDriver {
34private:
35  CompilerConfig *mConfig;
36  RSCompiler mCompiler;
37
38  CompilerRTSymbolResolver *mCompilerRuntime;
39  LookupFunctionSymbolResolver<void*> mRSRuntime;
40  SymbolResolverProxy mResolver;
41
42  // Are we compiling under an RS debug context with additional checks?
43  bool mDebugContext;
44
45  // Do we merge global variables on ARM using LLVM's optimization pass?
46  // Disabling LLVM's global merge pass allows static globals to be correctly
47  // emitted to ELF. This can result in decreased performance due to increased
48  // register pressure, but it does make the resulting code easier to debug
49  // and work with.
50  bool mEnableGlobalMerge;
51
52  // Setup the compiler config for the given script. Return true if mConfig has
53  // been changed and false if it remains unchanged.
54  bool setupConfig(const RSScript &pScript);
55
56  Compiler::ErrorCode compileScript(RSScript &pScript,
57                                    const char* pScriptName,
58                                    const char *pOutputPath,
59                                    const char *pRuntimePath,
60                                    const RSInfo::DependencyTableTy &pDeps,
61                                    bool pSkipLoad, bool pDumpIR = false);
62
63public:
64  RSCompilerDriver(bool pUseCompilerRT = true);
65  ~RSCompilerDriver();
66
67  inline void setRSRuntimeLookupFunction(
68      LookupFunctionSymbolResolver<>::LookupFunctionTy pLookupFunc)
69  { mRSRuntime.setLookupFunction(pLookupFunc); }
70  inline void setRSRuntimeLookupContext(void *pContext)
71  { mRSRuntime.setContext(pContext); }
72
73  RSCompiler *getCompiler() {
74    return &mCompiler;
75  }
76
77  void setConfig(CompilerConfig *config) {
78    mConfig = config;
79  }
80
81  void setDebugContext(bool v) {
82    mDebugContext = v;
83  }
84
85  // This function enables/disables merging of global static variables.
86  // Note that it only takes effect on ARM architectures (other architectures
87  // do not offer this option).
88  void setEnableGlobalMerge(bool v) {
89    mEnableGlobalMerge = v;
90  }
91
92  bool getEnableGlobalMerge() const {
93    return mEnableGlobalMerge;
94  }
95
96  // FIXME: This method accompany with loadScript and compileScript should
97  //        all be const-methods. They're not now because the getAddress() in
98  //        SymbolResolverInterface is not a const-method.
99  // Returns true if script is successfully compiled.
100  bool build(BCCContext &pContext, const char *pCacheDir, const char *pResName,
101             const char *pBitcode, size_t pBitcodeSize,
102             const char *pRuntimePath,
103             RSLinkRuntimeCallback pLinkRuntimeCallback = NULL,
104             bool pDumpIR = false);
105
106  // Returns true if script is successfully compiled.
107  bool build(RSScript &pScript, const char *pOut, const char *pRuntimePath);
108
109  RSExecutable *loadScript(const char *pCacheDir, const char *pResName,
110                           const char *pBitcode, size_t pBitcodeSize);
111};
112
113} // end namespace bcc
114
115#endif // BCC_RS_COMPILER_DRIVER_H
116