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_SCRIPT_H
18#define BCC_RS_SCRIPT_H
19
20#include "bcc/Script.h"
21#include "bcc/Support/Sha1Util.h"
22
23namespace llvm {
24  class Module;
25}
26
27namespace bcc {
28
29class RSScript;
30class Source;
31class CompilerConfig;
32
33typedef llvm::Module* (*RSLinkRuntimeCallback) (bcc::RSScript *, llvm::Module *, llvm::Module *);
34
35
36class RSScript : public Script {
37public:
38  // This is one-one mapping with the llvm::CodeGenOpt::Level in
39  // llvm/Support/CodeGen.h. Therefore, value of this type can safely cast
40  // to llvm::CodeGenOpt::Level. This makes RSScript LLVM-free.
41  enum OptimizationLevel {
42    kOptLvl0, // -O0
43    kOptLvl1, // -O1
44    kOptLvl2, // -O2, -Os
45    kOptLvl3  // -O3
46  };
47
48private:
49  unsigned mCompilerVersion;
50
51  OptimizationLevel mOptimizationLevel;
52
53  RSLinkRuntimeCallback mLinkRuntimeCallback;
54
55  bool mEmbedInfo;
56
57  // Specifies whether we should embed global variable information in the
58  // code via special RS variables that can be examined later by the driver.
59  bool mEmbedGlobalInfo;
60
61  // Specifies whether we should skip constant (immutable) global variables
62  // when potentially embedding information about globals.
63  bool mEmbedGlobalInfoSkipConstant;
64
65private:
66  // This will be invoked when the containing source has been reset.
67  virtual bool doReset();
68
69public:
70  static bool LinkRuntime(RSScript &pScript, const char *rt_path = nullptr);
71
72  RSScript(Source &pSource);
73
74  // Passing in the CompilerConfig allows the optimization level to
75  // be derived rather than defaulted to aggressive (-O3)
76  RSScript(Source &pSource, const CompilerConfig * pCompilerConfig);
77
78  virtual ~RSScript() { }
79
80  void setCompilerVersion(unsigned pCompilerVersion) {
81    mCompilerVersion = pCompilerVersion;
82  }
83
84  unsigned getCompilerVersion() const {
85    return mCompilerVersion;
86  }
87
88  void setOptimizationLevel(OptimizationLevel pOptimizationLevel) {
89    mOptimizationLevel = pOptimizationLevel;
90  }
91
92  OptimizationLevel getOptimizationLevel() const {
93    return mOptimizationLevel;
94  }
95
96  void setLinkRuntimeCallback(RSLinkRuntimeCallback fn){
97    mLinkRuntimeCallback = fn;
98  }
99
100  void setEmbedInfo(bool pEnable) {
101    mEmbedInfo = pEnable;
102  }
103
104  bool getEmbedInfo() const {
105    return mEmbedInfo;
106  }
107
108  // Set to true if we should embed global variable information in the code.
109  void setEmbedGlobalInfo(bool pEnable) {
110    mEmbedGlobalInfo = pEnable;
111  }
112
113  // Returns true if we should embed global variable information in the code.
114  bool getEmbedGlobalInfo() const {
115    return mEmbedGlobalInfo;
116  }
117
118  // Set to true if we should skip constant (immutable) global variables when
119  // potentially embedding information about globals.
120  void setEmbedGlobalInfoSkipConstant(bool pEnable) {
121    mEmbedGlobalInfoSkipConstant = pEnable;
122  }
123
124  // Returns true if we should skip constant (immutable) global variables when
125  // potentially embedding information about globals.
126  bool getEmbedGlobalInfoSkipConstant() const {
127    return mEmbedGlobalInfoSkipConstant;
128  }
129};
130
131} // end namespace bcc
132
133#endif // BCC_RS_SCRIPT_H
134