blob: c5b4fe75974ce34fd2146450fd4baa0500ed4ee0 [file] [log] [blame]
Zonr Chang0fffa7e2012-04-12 19:43:53 +08001/*
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
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#ifndef BCC_RS_COMPILER_DRIVER_H
18#define BCC_RS_COMPILER_DRIVER_H
Zonr Chang0fffa7e2012-04-12 19:43:53 +080019
Chris Wailesb4447cd2014-08-19 16:22:20 -070020#include "bcc/Compiler.h"
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080021#include "bcc/Script.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080022
Yang Nia4ded132014-11-17 17:44:08 -080023#include "bcinfo/MetadataExtractor.h"
24
Yang Ni0ab50b82015-02-16 12:31:56 -080025#include <list>
Pirama Arumuga Nainar55253792015-03-24 16:22:09 -070026#include <string>
Yang Nia4ded132014-11-17 17:44:08 -080027#include <vector>
28
Zonr Chang0fffa7e2012-04-12 19:43:53 +080029namespace bcc {
30
Shih-wei Liao7bcec852012-04-25 04:07:09 -070031class BCCContext;
Zonr Chang0fffa7e2012-04-12 19:43:53 +080032class CompilerConfig;
Stephen Hinesc3437f02014-01-30 17:57:21 -080033class RSCompilerDriver;
Yang Nia4ded132014-11-17 17:44:08 -080034class Source;
Zonr Chang0fffa7e2012-04-12 19:43:53 +080035
Stephen Hinesc3437f02014-01-30 17:57:21 -080036// Type signature for dynamically loaded initialization of an RSCompilerDriver.
37typedef void (*RSCompilerDriverInit_t) (bcc::RSCompilerDriver *);
38// Name of the function that we attempt to dynamically load/execute.
39#define RS_COMPILER_DRIVER_INIT_FN rsCompilerDriverInit
40
Zonr Chang0fffa7e2012-04-12 19:43:53 +080041class RSCompilerDriver {
42private:
43 CompilerConfig *mConfig;
Chris Wailesb4447cd2014-08-19 16:22:20 -070044 Compiler mCompiler;
Zonr Chang0fffa7e2012-04-12 19:43:53 +080045
Stephen Hines1d4a9e42013-04-18 01:03:59 -070046 // Are we compiling under an RS debug context with additional checks?
47 bool mDebugContext;
48
Stephen Hinesc3437f02014-01-30 17:57:21 -080049 // Callback before linking with the runtime library.
50 RSLinkRuntimeCallback mLinkRuntimeCallback;
51
Stephen Hinesc06cd062013-07-12 10:51:29 -070052 // Do we merge global variables on ARM using LLVM's optimization pass?
Stephen Hinesad694762013-04-29 18:59:47 -070053 // Disabling LLVM's global merge pass allows static globals to be correctly
54 // emitted to ELF. This can result in decreased performance due to increased
55 // register pressure, but it does make the resulting code easier to debug
56 // and work with.
57 bool mEnableGlobalMerge;
58
Stephen Hines750ee652015-04-16 16:24:18 -070059 // Specifies whether we should embed global variable information in the
60 // code via special RS variables that can be examined later by the driver.
61 bool mEmbedGlobalInfo;
62
63 // Specifies whether we should skip constant (immutable) global variables
64 // when potentially embedding information about globals.
65 bool mEmbedGlobalInfoSkipConstant;
66
Zonr Chang0fffa7e2012-04-12 19:43:53 +080067 // Setup the compiler config for the given script. Return true if mConfig has
68 // been changed and false if it remains unchanged.
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080069 bool setupConfig(const Script &pScript);
Zonr Chang0fffa7e2012-04-12 19:43:53 +080070
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -070071 // Compiles the provided bitcode, placing the binary at pOutputPath.
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -070072 // - If pDumpIR is true, a ".ll" file will also be created.
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080073 Compiler::ErrorCode compileScript(Script& pScript, const char* pScriptName,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -080074 const char* pOutputPath,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -080075 const char* pRuntimePath,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -080076 const char* pBuildChecksum,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -080077 bool pDumpIR);
Zonr Chang0fffa7e2012-04-12 19:43:53 +080078
79public:
David Grossefe14342016-02-18 11:46:57 -080080 RSCompilerDriver();
Zonr Chang0fffa7e2012-04-12 19:43:53 +080081 ~RSCompilerDriver();
82
Chris Wailesb4447cd2014-08-19 16:22:20 -070083 Compiler *getCompiler() {
Stephen Hines331310e2012-10-26 19:27:55 -070084 return &mCompiler;
85 }
86
87 void setConfig(CompilerConfig *config) {
88 mConfig = config;
89 }
90
Stephen Hines1d4a9e42013-04-18 01:03:59 -070091 void setDebugContext(bool v) {
92 mDebugContext = v;
93 }
94
Stephen Hinesc3437f02014-01-30 17:57:21 -080095 void setLinkRuntimeCallback(RSLinkRuntimeCallback c) {
96 mLinkRuntimeCallback = c;
97 }
98
99 RSLinkRuntimeCallback getLinkRuntimeCallback() const {
100 return mLinkRuntimeCallback;
101 }
102
Stephen Hinesc06cd062013-07-12 10:51:29 -0700103 // This function enables/disables merging of global static variables.
104 // Note that it only takes effect on ARM architectures (other architectures
105 // do not offer this option).
Stephen Hinesad694762013-04-29 18:59:47 -0700106 void setEnableGlobalMerge(bool v) {
107 mEnableGlobalMerge = v;
108 }
109
110 bool getEnableGlobalMerge() const {
111 return mEnableGlobalMerge;
112 }
113
Stephen McGroarty656325b2015-07-17 15:37:06 +0100114 const CompilerConfig * getConfig() const {
115 return mConfig;
116 }
117
Stephen Hines750ee652015-04-16 16:24:18 -0700118 // Set to true if we should embed global variable information in the code.
119 void setEmbedGlobalInfo(bool v) {
120 mEmbedGlobalInfo = v;
121 }
122
123 // Returns true if we should embed global variable information in the code.
124 bool getEmbedGlobalInfo() const {
125 return mEmbedGlobalInfo;
126 }
127
128 // Set to true if we should skip constant (immutable) global variables when
129 // potentially embedding information about globals.
130 void setEmbedGlobalInfoSkipConstant(bool v) {
131 mEmbedGlobalInfoSkipConstant = v;
132 }
133
134 // Returns true if we should skip constant (immutable) global variables when
135 // potentially embedding information about globals.
136 bool getEmbedGlobalInfoSkipConstant() const {
137 return mEmbedGlobalInfoSkipConstant;
138 }
139
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700140 // FIXME: This method accompany with loadScript and compileScript should
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800141 // all be const-methods. They're not now because the getAddress() in
142 // SymbolResolverInterface is not a const-method.
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700143 // Returns true if script is successfully compiled.
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700144 bool build(BCCContext& pContext, const char* pCacheDir, const char* pResName,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800145 const char* pBitcode, size_t pBitcodeSize,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800146 const char *pBuildChecksum, const char* pRuntimePath,
Chris Wailes900c6c12014-08-13 15:40:00 -0700147 RSLinkRuntimeCallback pLinkRuntimeCallback = nullptr,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700148 bool pDumpIR = false);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700149
Yang Nia4ded132014-11-17 17:44:08 -0800150 bool buildScriptGroup(
151 BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
Yang Ni186d2f32015-04-07 14:51:47 -0700152 const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum,
Yang Ni6da4e252015-03-11 10:00:42 -0700153 const std::vector<Source*>& sources,
Yang Ni0ab50b82015-02-16 12:31:56 -0800154 const std::list<std::list<std::pair<int, int>>>& toFuse,
155 const std::list<std::string>& fused,
156 const std::list<std::list<std::pair<int, int>>>& invokes,
157 const std::list<std::string>& invokeBatchNames);
Yang Nia4ded132014-11-17 17:44:08 -0800158
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700159 // Returns true if script is successfully compiled.
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800160 bool buildForCompatLib(Script &pScript, const char *pOut,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800161 const char *pBuildChecksum, const char *pRuntimePath,
162 bool pDumpIR);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800163};
164
165} // end namespace bcc
166
Zonr Changc72c4dd2012-04-12 15:38:53 +0800167#endif // BCC_RS_COMPILER_DRIVER_H