10fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang/*
20fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * Copyright 2012, The Android Open Source Project
30fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang *
40fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
50fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * you may not use this file except in compliance with the License.
60fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * You may obtain a copy of the License at
70fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang *
80fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
90fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang *
100fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * Unless required by applicable law or agreed to in writing, software
110fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
120fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * See the License for the specific language governing permissions and
140fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang * limitations under the License.
150fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang */
160fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
17c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#ifndef BCC_RS_COMPILER_DRIVER_H
18c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#define BCC_RS_COMPILER_DRIVER_H
190fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
2009ca95426a7ec66def02e47048bb25289759c6fbShih-wei Liao#include "bcc/ExecutionEngine/CompilerRTSymbolResolver.h"
21c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/ExecutionEngine/SymbolResolvers.h"
22c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/ExecutionEngine/SymbolResolverProxy.h"
23e198abec6c5e3eab380ccf6897b0a0b9c2dd92ddStephen Hines#include "bcc/Renderscript/RSInfo.h"
24e198abec6c5e3eab380ccf6897b0a0b9c2dd92ddStephen Hines#include "bcc/Renderscript/RSCompiler.h"
2501f05d4b45cbde1e07d4707152908c1d843f1328Stephen Hines#include "bcc/Renderscript/RSScript.h"
260fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
270fffa7e4adc90079dc0f252359050fe872b360ddZonr Changnamespace bcc {
280fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
297bcec85228c1eceade6efc9cd7d02ef81c8d55e9Shih-wei Liaoclass BCCContext;
300fffa7e4adc90079dc0f252359050fe872b360ddZonr Changclass CompilerConfig;
31c3437f05c638f8befda59170ae788873db24dc1cStephen Hinesclass RSCompilerDriver;
320fffa7e4adc90079dc0f252359050fe872b360ddZonr Changclass RSExecutable;
330fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
34c3437f05c638f8befda59170ae788873db24dc1cStephen Hines// Type signature for dynamically loaded initialization of an RSCompilerDriver.
35c3437f05c638f8befda59170ae788873db24dc1cStephen Hinestypedef void (*RSCompilerDriverInit_t) (bcc::RSCompilerDriver *);
36c3437f05c638f8befda59170ae788873db24dc1cStephen Hines// Name of the function that we attempt to dynamically load/execute.
37c3437f05c638f8befda59170ae788873db24dc1cStephen Hines#define RS_COMPILER_DRIVER_INIT_FN rsCompilerDriverInit
38c3437f05c638f8befda59170ae788873db24dc1cStephen Hines
390fffa7e4adc90079dc0f252359050fe872b360ddZonr Changclass RSCompilerDriver {
400fffa7e4adc90079dc0f252359050fe872b360ddZonr Changprivate:
410fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  CompilerConfig *mConfig;
420fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  RSCompiler mCompiler;
430fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
441d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines  // Are we compiling under an RS debug context with additional checks?
451d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines  bool mDebugContext;
461d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines
47c3437f05c638f8befda59170ae788873db24dc1cStephen Hines  // Callback before linking with the runtime library.
48c3437f05c638f8befda59170ae788873db24dc1cStephen Hines  RSLinkRuntimeCallback mLinkRuntimeCallback;
49c3437f05c638f8befda59170ae788873db24dc1cStephen Hines
50c06cd06078a8c866d5cecbf897326092b55a8955Stephen Hines  // Do we merge global variables on ARM using LLVM's optimization pass?
51ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  // Disabling LLVM's global merge pass allows static globals to be correctly
52ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  // emitted to ELF. This can result in decreased performance due to increased
53ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  // register pressure, but it does make the resulting code easier to debug
54ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  // and work with.
55ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  bool mEnableGlobalMerge;
56ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines
570fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  // Setup the compiler config for the given script. Return true if mConfig has
580fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  // been changed and false if it remains unchanged.
590fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  bool setupConfig(const RSScript &pScript);
600fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
61f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // Compiles the provided bitcode, placing the binary at pOutputPath.
62f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // - If saveInfoFile is true, it also stores the RSInfo data in a file with a path derived from
63f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  //   pOutputPath.
64f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // - pSourceHash and commandLineToEmbed are values to embed in the RSInfo for future cache
65f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  //   invalidation decision.
66f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // - If pDumpIR is true, a ".ll" file will also be created.
67f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  Compiler::ErrorCode compileScript(RSScript& pScript, const char* pScriptName,
68f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet                                    const char* pOutputPath, const char* pRuntimePath,
69f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet                                    const RSInfo::DependencyHashTy& pSourceHash,
70f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet                                    const char* commandLineToEmbed, bool saveInfoFile, bool pDumpIR);
710fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
720fffa7e4adc90079dc0f252359050fe872b360ddZonr Changpublic:
733ab9da1e1b3d7794994393af086d096397fb84dbStephen Hines  RSCompilerDriver(bool pUseCompilerRT = true);
740fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  ~RSCompilerDriver();
750fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
76331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines  RSCompiler *getCompiler() {
77331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines    return &mCompiler;
78331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines  }
79331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines
80331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines  void setConfig(CompilerConfig *config) {
81331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines    mConfig = config;
82331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines  }
83331310e1f3f86a795f78e42b3f03558a43829f09Stephen Hines
841d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines  void setDebugContext(bool v) {
851d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines    mDebugContext = v;
861d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines  }
871d4a9e4646e8786623daa6ac3917782be56a8fb2Stephen Hines
88c3437f05c638f8befda59170ae788873db24dc1cStephen Hines  void setLinkRuntimeCallback(RSLinkRuntimeCallback c) {
89c3437f05c638f8befda59170ae788873db24dc1cStephen Hines    mLinkRuntimeCallback = c;
90c3437f05c638f8befda59170ae788873db24dc1cStephen Hines  }
91c3437f05c638f8befda59170ae788873db24dc1cStephen Hines
92c3437f05c638f8befda59170ae788873db24dc1cStephen Hines  RSLinkRuntimeCallback getLinkRuntimeCallback() const {
93c3437f05c638f8befda59170ae788873db24dc1cStephen Hines    return mLinkRuntimeCallback;
94c3437f05c638f8befda59170ae788873db24dc1cStephen Hines  }
95c3437f05c638f8befda59170ae788873db24dc1cStephen Hines
96c06cd06078a8c866d5cecbf897326092b55a8955Stephen Hines  // This function enables/disables merging of global static variables.
97c06cd06078a8c866d5cecbf897326092b55a8955Stephen Hines  // Note that it only takes effect on ARM architectures (other architectures
98c06cd06078a8c866d5cecbf897326092b55a8955Stephen Hines  // do not offer this option).
99ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  void setEnableGlobalMerge(bool v) {
100ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines    mEnableGlobalMerge = v;
101ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  }
102ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines
103ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  bool getEnableGlobalMerge() const {
104ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines    return mEnableGlobalMerge;
105ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines  }
106ad69476ec040ba9ee07176ccb758aabb5ff6d51cStephen Hines
10747f0d5a8d9ed629a6d58d3e69891f2c7fdee44d9Stephen Hines  // FIXME: This method accompany with loadScript and compileScript should
1080fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  //        all be const-methods. They're not now because the getAddress() in
1090fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang  //        SymbolResolverInterface is not a const-method.
11047f0d5a8d9ed629a6d58d3e69891f2c7fdee44d9Stephen Hines  // Returns true if script is successfully compiled.
111f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  bool build(BCCContext& pContext, const char* pCacheDir, const char* pResName,
112f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet             const char* pBitcode, size_t pBitcodeSize, const char* commandLine,
113f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet             const char* pRuntimePath, RSLinkRuntimeCallback pLinkRuntimeCallback = NULL,
1147b980e1717f3cf418f7bc4e40597004bc1139b8bTobias Grosser             bool pDumpIR = false);
11547f0d5a8d9ed629a6d58d3e69891f2c7fdee44d9Stephen Hines
11647f0d5a8d9ed629a6d58d3e69891f2c7fdee44d9Stephen Hines  // Returns true if script is successfully compiled.
117c5e607adff80a66bc5420baffd299862abdf368dJean-Luc Brouillet  bool buildForCompatLib(RSScript &pScript, const char *pOut, const char *pRuntimePath);
11847f0d5a8d9ed629a6d58d3e69891f2c7fdee44d9Stephen Hines
119f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // Tries to load the the compiled bit code at pCacheDir of the given name.  It checks that
120f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // the file has been compiled from the same bit code and with the same compile arguments as
121f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  // provided.
122f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet  static RSExecutable* loadScript(const char* pCacheDir, const char* pResName, const char* pBitcode,
123f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet                                  size_t pBitcodeSize, const char* expectedCompileCommandLine,
124f2ac3176c351cd80bce77fe1488f3de2d0789c1bJean-Luc Brouillet                                  SymbolResolverProxy& pResolver);
1250fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang};
1260fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
1270fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang} // end namespace bcc
1280fffa7e4adc90079dc0f252359050fe872b360ddZonr Chang
129c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#endif // BCC_RS_COMPILER_DRIVER_H
130