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_SCRIPT_H
18 #define BCC_SCRIPT_H
19 
20 #include "slang_version.h"
21 
22 #include <llvm/Support/CodeGen.h>
23 #include "bcc/Source.h"
24 
25 namespace llvm {
26 class Module;
27 }
28 
29 namespace bcc {
30 
31 class Script;
32 class Source;
33 class CompilerConfig;
34 
35 typedef llvm::Module *(*RSLinkRuntimeCallback)(bcc::Script *, llvm::Module *,
36                                                llvm::Module *);
37 
38 class Script {
39 private:
40   // This is the source associated with this object and is going to be
41   // compiled.
42   // TODO(jeanluc) Verify that the lifetime is managed correctly.
43   Source *mSource;
44 
45   llvm::CodeGenOpt::Level mOptimizationLevel;
46 
47   RSLinkRuntimeCallback mLinkRuntimeCallback;
48 
49   bool mEmbedInfo;
50 
51   // Specifies whether we should embed global variable information in the
52   // code via special RS variables that can be examined later by the driver.
53   bool mEmbedGlobalInfo;
54 
55   // Specifies whether we should skip constant (immutable) global variables
56   // when potentially embedding information about globals.
57   bool mEmbedGlobalInfoSkipConstant;
58 
59 public:
60   explicit Script(Source *pSource);
61 
~Script()62   ~Script() {}
63 
64   bool LinkRuntime(const char *rt_path);
65 
getCompilerVersion()66   unsigned getCompilerVersion() const {
67     return getSource().getCompilerVersion();
68   }
69 
isStructExplicitlyPaddedBySlang()70   bool isStructExplicitlyPaddedBySlang() const {
71     return getCompilerVersion() >= SlangVersion::N_STRUCT_EXPLICIT_PADDING;
72   }
73 
setOptimizationLevel(llvm::CodeGenOpt::Level pOptimizationLevel)74   void setOptimizationLevel(llvm::CodeGenOpt::Level pOptimizationLevel) {
75     mOptimizationLevel = pOptimizationLevel;
76   }
77 
getOptimizationLevel()78   llvm::CodeGenOpt::Level getOptimizationLevel() const {
79     return mOptimizationLevel;
80   }
81 
setLinkRuntimeCallback(RSLinkRuntimeCallback fn)82   void setLinkRuntimeCallback(RSLinkRuntimeCallback fn) {
83     mLinkRuntimeCallback = fn;
84   }
85 
setEmbedInfo(bool pEnable)86   void setEmbedInfo(bool pEnable) { mEmbedInfo = pEnable; }
87 
getEmbedInfo()88   bool getEmbedInfo() const { return mEmbedInfo; }
89 
90   // Set to true if we should embed global variable information in the code.
setEmbedGlobalInfo(bool pEnable)91   void setEmbedGlobalInfo(bool pEnable) { mEmbedGlobalInfo = pEnable; }
92 
93   // Returns true if we should embed global variable information in the code.
getEmbedGlobalInfo()94   bool getEmbedGlobalInfo() const { return mEmbedGlobalInfo; }
95 
96   // Set to true if we should skip constant (immutable) global variables when
97   // potentially embedding information about globals.
setEmbedGlobalInfoSkipConstant(bool pEnable)98   void setEmbedGlobalInfoSkipConstant(bool pEnable) {
99     mEmbedGlobalInfoSkipConstant = pEnable;
100   }
101 
102   // Returns true if we should skip constant (immutable) global variables when
103   // potentially embedding information about globals.
getEmbedGlobalInfoSkipConstant()104   inline bool getEmbedGlobalInfoSkipConstant() const {
105     return mEmbedGlobalInfoSkipConstant;
106   }
107 
108   // Merge (or link) another source into the current source associated with
109   // this Script object. Return false on error.
110   //
111   // This is equivalent to the call to Script::merge(...) on mSource.
112   bool mergeSource(Source &pSource);
113 
getSource()114   inline Source &getSource() { return *mSource; }
getSource()115   inline const Source &getSource() const { return *mSource; }
116 };
117 
118 } // end namespace bcc
119 
120 #endif // BCC_SCRIPT_H
121