blob: 75cde3791d085a7c8784c69192b2fd318bf04994 [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
Stephen Hinesdb169182012-01-05 18:46:36 -08002 * Copyright 2010-2012, The Android Open Source Project
Logan1f028c02010-11-27 01:02:48 +08003 *
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_COMPILER_H
18#define BCC_COMPILER_H
Logan1f028c02010-11-27 01:02:48 +080019
Logan1f028c02010-11-27 01:02:48 +080020namespace llvm {
Logan1f028c02010-11-27 01:02:48 +080021
Zonr Changade92772012-04-13 15:58:24 +080022class raw_ostream;
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -070023class raw_pwrite_stream;
Stephen Hinesb730e232013-01-09 15:31:36 -080024class DataLayout;
Zonr Changade92772012-04-13 15:58:24 +080025class TargetMachine;
26
Stephen Hinesdee928b2014-02-10 17:19:46 -080027namespace legacy {
28class PassManager;
29} // end namespace legacy
30
31using legacy::PassManager;
32
Zonr Changade92772012-04-13 15:58:24 +080033} // end namespace llvm
Logan1f028c02010-11-27 01:02:48 +080034
35namespace bcc {
36
Zonr Changade92772012-04-13 15:58:24 +080037class CompilerConfig;
38class OutputFile;
39class Script;
Logan1f028c02010-11-27 01:02:48 +080040
Zonr Changade92772012-04-13 15:58:24 +080041//===----------------------------------------------------------------------===//
42// Design of Compiler
43//===----------------------------------------------------------------------===//
44// 1. A compiler instance can be constructed provided an "initial config."
45// 2. A compiler can later be re-configured using config().
46// 3. Once config() is invoked, it'll re-create TargetMachine instance (i.e.,
47// mTarget) according to the configuration supplied. TargetMachine instance
48// is *shared* across the different calls to compile() before the next call
49// to config().
50// 4. Once a compiler instance is created, you can use the compile() service
51// to compile the file over and over again. Each call uses TargetMachine
52// instance to construct the compilation passes.
53class Compiler {
54public:
55 enum ErrorCode {
56 kSuccess,
Logan1f028c02010-11-27 01:02:48 +080057
Zonr Changade92772012-04-13 15:58:24 +080058 kInvalidConfigNoTarget,
59 kErrCreateTargetMachine,
60 kErrSwitchTargetMachine,
61 kErrNoTargetMachine,
Zonr Changade92772012-04-13 15:58:24 +080062 kErrMaterialization,
63 kErrInvalidOutputFileState,
64 kErrPrepareOutput,
65 kPrepareCodeGenPass,
Logan1f028c02010-11-27 01:02:48 +080066
Chris Wailesb4447cd2014-08-19 16:22:20 -070067 kErrCustomPasses,
Logan1f028c02010-11-27 01:02:48 +080068
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080069 kErrInvalidSource,
70
71 kIllegalGlobalFunction
Zonr Changade92772012-04-13 15:58:24 +080072 };
Logan1f028c02010-11-27 01:02:48 +080073
Zonr Changade92772012-04-13 15:58:24 +080074 static const char *GetErrorString(enum ErrorCode pErrCode);
Logan1f028c02010-11-27 01:02:48 +080075
Zonr Changade92772012-04-13 15:58:24 +080076private:
77 llvm::TargetMachine *mTarget;
Chris Wailesb4447cd2014-08-19 16:22:20 -070078 // Optimization is enabled by default.
79 bool mEnableOpt;
Logan2a6dc822011-01-06 04:05:20 +080080
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -070081 enum ErrorCode runPasses(Script &pScript, llvm::raw_pwrite_stream &pResult);
Chris Wailesb4447cd2014-08-19 16:22:20 -070082
Stephen Hines1bd9f622015-03-18 14:53:10 -070083 bool addCustomPasses(Script &pScript, llvm::legacy::PassManager &pPM);
84 bool addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM);
85 bool addExpandForEachPass(Script &pScript, llvm::legacy::PassManager &pPM);
Stephen Hines750ee652015-04-16 16:24:18 -070086 bool addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM);
Stephen Hines1bd9f622015-03-18 14:53:10 -070087 bool addInvariantPass(llvm::legacy::PassManager &pPM);
88 bool addInvokeHelperPass(llvm::legacy::PassManager &pPM);
Pirama Arumuga Nainar8c24f8d2015-03-17 13:11:25 -070089 bool addPostLTOCustomPasses(llvm::legacy::PassManager &pPM);
Logan1f028c02010-11-27 01:02:48 +080090
Zonr Changade92772012-04-13 15:58:24 +080091public:
92 Compiler();
93 Compiler(const CompilerConfig &pConfig);
Logan Chienda5e0c32011-06-13 03:47:21 +080094
Zonr Changade92772012-04-13 15:58:24 +080095 enum ErrorCode config(const CompilerConfig &pConfig);
Logan1f028c02010-11-27 01:02:48 +080096
Zonr Changade92772012-04-13 15:58:24 +080097 // Compile a script and output the result to a LLVM stream.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -070098 //
99 // @param IRStream If not NULL, the LLVM-IR that is fed to code generation
100 // will be written to IRStream.
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700101 enum ErrorCode compile(Script &pScript, llvm::raw_pwrite_stream &pResult,
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700102 llvm::raw_ostream *IRStream);
Logan1f028c02010-11-27 01:02:48 +0800103
Zonr Changade92772012-04-13 15:58:24 +0800104 // Compile a script and output the result to a file.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700105 enum ErrorCode compile(Script &pScript, OutputFile &pResult,
106 llvm::raw_ostream *IRStream = 0);
Logan1f028c02010-11-27 01:02:48 +0800107
Shih-wei Liao0dbd4fb2012-07-27 03:09:47 -0700108 const llvm::TargetMachine& getTargetMachine() const
109 { return *mTarget; }
110
Chris Wailesb4447cd2014-08-19 16:22:20 -0700111 void enableOpt(bool pEnable = true)
112 { mEnableOpt = pEnable; }
Logan1f028c02010-11-27 01:02:48 +0800113
Chris Wailesb4447cd2014-08-19 16:22:20 -0700114 ~Compiler();
Pirama Arumuga Nainarebff2ea2015-05-21 15:45:05 -0700115
116 // Compare undefined external functions in pScript against a 'whitelist' of
117 // all RenderScript functions. Returns error if any external function that is
118 // not in this whitelist is callable from the script.
119 enum ErrorCode screenGlobalFunctions(Script &pScript);
Zonr Changade92772012-04-13 15:58:24 +0800120};
Logan1f028c02010-11-27 01:02:48 +0800121
Zonr Changade92772012-04-13 15:58:24 +0800122} // end namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800123
Zonr Changc72c4dd2012-04-12 15:38:53 +0800124#endif // BCC_COMPILER_H