blob: 715cb2c561ad7e6a5828cf6dca4b26b0f8bd15fa [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
Zonr Changade92772012-04-13 15:58:24 +080031} // end namespace llvm
Logan1f028c02010-11-27 01:02:48 +080032
33namespace bcc {
34
Zonr Changade92772012-04-13 15:58:24 +080035class CompilerConfig;
Zonr Changade92772012-04-13 15:58:24 +080036class Script;
Logan1f028c02010-11-27 01:02:48 +080037
Zonr Changade92772012-04-13 15:58:24 +080038//===----------------------------------------------------------------------===//
39// Design of Compiler
40//===----------------------------------------------------------------------===//
41// 1. A compiler instance can be constructed provided an "initial config."
42// 2. A compiler can later be re-configured using config().
43// 3. Once config() is invoked, it'll re-create TargetMachine instance (i.e.,
44// mTarget) according to the configuration supplied. TargetMachine instance
45// is *shared* across the different calls to compile() before the next call
46// to config().
47// 4. Once a compiler instance is created, you can use the compile() service
48// to compile the file over and over again. Each call uses TargetMachine
49// instance to construct the compilation passes.
50class Compiler {
51public:
52 enum ErrorCode {
53 kSuccess,
Logan1f028c02010-11-27 01:02:48 +080054
Zonr Changade92772012-04-13 15:58:24 +080055 kInvalidConfigNoTarget,
56 kErrCreateTargetMachine,
57 kErrSwitchTargetMachine,
58 kErrNoTargetMachine,
Zonr Changade92772012-04-13 15:58:24 +080059 kErrMaterialization,
60 kErrInvalidOutputFileState,
61 kErrPrepareOutput,
62 kPrepareCodeGenPass,
Logan1f028c02010-11-27 01:02:48 +080063
Chris Wailesb4447cd2014-08-19 16:22:20 -070064 kErrCustomPasses,
Logan1f028c02010-11-27 01:02:48 +080065
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080066 kErrInvalidSource,
67
Pirama Arumuga Nainard2d5ee32016-04-12 14:04:50 -070068 kIllegalGlobalFunction,
69
David Gross97e50992017-03-29 20:52:30 +000070 kErrInvalidTargetMachine,
71
72 kErrInvalidLayout
Zonr Changade92772012-04-13 15:58:24 +080073 };
Logan1f028c02010-11-27 01:02:48 +080074
Zonr Changade92772012-04-13 15:58:24 +080075 static const char *GetErrorString(enum ErrorCode pErrCode);
Logan1f028c02010-11-27 01:02:48 +080076
Zonr Changade92772012-04-13 15:58:24 +080077private:
78 llvm::TargetMachine *mTarget;
Chris Wailesb4447cd2014-08-19 16:22:20 -070079 // Optimization is enabled by default.
80 bool mEnableOpt;
Logan2a6dc822011-01-06 04:05:20 +080081
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -070082 enum ErrorCode runPasses(Script &pScript, llvm::raw_pwrite_stream &pResult);
Chris Wailesb4447cd2014-08-19 16:22:20 -070083
Stephen Hines1bd9f622015-03-18 14:53:10 -070084 bool addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM);
Matt Wala4e7a5062015-07-30 16:27:51 -070085 void addExpandKernelPass(llvm::legacy::PassManager &pPM);
Dean De Leo09c7a412015-11-25 12:45:45 +000086 void addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM);
David Gross5aefc982015-08-04 10:41:33 -070087 void addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM);
88 void addInvariantPass(llvm::legacy::PassManager &pPM);
89 void addInvokeHelperPass(llvm::legacy::PassManager &pPM);
Logan1f028c02010-11-27 01:02:48 +080090
Zonr Changade92772012-04-13 15:58:24 +080091public:
92 Compiler();
Chih-Hung Hsieh8a019dd2016-08-12 15:49:55 -070093 explicit 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
Shih-wei Liao0dbd4fb2012-07-27 03:09:47 -0700104 const llvm::TargetMachine& getTargetMachine() const
105 { return *mTarget; }
106
Chris Wailesb4447cd2014-08-19 16:22:20 -0700107 void enableOpt(bool pEnable = true)
108 { mEnableOpt = pEnable; }
Logan1f028c02010-11-27 01:02:48 +0800109
Chris Wailesb4447cd2014-08-19 16:22:20 -0700110 ~Compiler();
Pirama Arumuga Nainarebff2ea2015-05-21 15:45:05 -0700111
Stephen Hines4dc1d8e2020-07-24 16:18:16 -0700112 // Compare undefined external functions in pScript against a list of allowed
113 // RenderScript functions. Returns error if any external function that is
114 // not in this list is callable from the script.
Pirama Arumuga Nainarebff2ea2015-05-21 15:45:05 -0700115 enum ErrorCode screenGlobalFunctions(Script &pScript);
Pirama Arumuga Nainard2d5ee32016-04-12 14:04:50 -0700116
117 void translateGEPs(Script &pScript);
Zonr Changade92772012-04-13 15:58:24 +0800118};
Logan1f028c02010-11-27 01:02:48 +0800119
Zonr Changade92772012-04-13 15:58:24 +0800120} // end namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800121
Zonr Changc72c4dd2012-04-12 15:38:53 +0800122#endif // BCC_COMPILER_H