blob: b01e992fabf8fc565aacb817e412f1e662c9c37e [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
zmo@google.com32e97312011-08-24 01:03:11 +00007#include "compiler/BuiltInFunctionEmulator.h"
zmo@google.comb1762df2011-07-30 02:04:23 +00008#include "compiler/DetectRecursion.h"
zmo@google.com0c6bb7a2011-08-17 19:39:58 +00009#include "compiler/ForLoopUnroll.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000010#include "compiler/Initialize.h"
11#include "compiler/ParseHelper.h"
12#include "compiler/ShHandle.h"
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000013#include "compiler/ValidateLimitations.h"
zmo@google.comfd747b82011-04-23 01:30:07 +000014#include "compiler/MapLongVariableNames.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000015
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000016namespace {
17bool InitializeSymbolTable(
18 const TBuiltInStrings& builtInStrings,
19 ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources,
20 TInfoSink& infoSink, TSymbolTable& symbolTable)
alokp@chromium.org07620a52010-09-23 17:53:56 +000021{
22 TIntermediate intermediate(infoSink);
23 TExtensionBehavior extBehavior;
zmo@google.com09c323a2011-08-12 18:22:25 +000024 InitExtensionBehavior(resources, extBehavior);
zmo@google.comdc4b4f82011-06-17 00:42:53 +000025 // The builtins deliberately don't specify precisions for the function
26 // arguments and return types. For that reason we don't try to check them.
27 TParseContext parseContext(symbolTable, extBehavior, intermediate, type, spec, 0, false, NULL, infoSink);
alokp@chromium.org07620a52010-09-23 17:53:56 +000028
29 GlobalParseContext = &parseContext;
30
alokp@chromium.org07620a52010-09-23 17:53:56 +000031 assert(symbolTable.isEmpty());
32 //
33 // Parse the built-ins. This should only happen once per
34 // language symbol table.
35 //
36 // Push the symbol table to give it an initial scope. This
37 // push should not have a corresponding pop, so that built-ins
38 // are preserved, and the test for an empty table fails.
39 //
40 symbolTable.push();
alokp@chromium.org07620a52010-09-23 17:53:56 +000041
42 for (TBuiltInStrings::const_iterator i = builtInStrings.begin(); i != builtInStrings.end(); ++i)
43 {
alokp@chromium.org570bfc72010-09-24 17:19:25 +000044 const char* builtInShaders = i->c_str();
45 int builtInLengths = static_cast<int>(i->size());
46 if (builtInLengths <= 0)
47 continue;
alokp@chromium.org07620a52010-09-23 17:53:56 +000048
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000049 if (PaParseStrings(1, &builtInShaders, &builtInLengths, &parseContext) != 0)
alokp@chromium.org07620a52010-09-23 17:53:56 +000050 {
51 infoSink.info.message(EPrefixInternalError, "Unable to parse built-ins");
52 return false;
53 }
54 }
55
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000056 IdentifyBuiltIns(type, spec, resources, symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +000057
alokp@chromium.org07620a52010-09-23 17:53:56 +000058 return true;
59}
60
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000061class TScopedPoolAllocator {
62public:
63 TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop)
64 : mAllocator(allocator), mPushPopAllocator(pushPop) {
65 if (mPushPopAllocator) mAllocator->push();
66 SetGlobalPoolAllocator(mAllocator);
67 }
68 ~TScopedPoolAllocator() {
69 SetGlobalPoolAllocator(NULL);
70 if (mPushPopAllocator) mAllocator->pop();
71 }
72
73private:
74 TPoolAllocator* mAllocator;
75 bool mPushPopAllocator;
76};
77} // namespace
78
79TShHandleBase::TShHandleBase() {
80 allocator.push();
81 SetGlobalPoolAllocator(&allocator);
82}
83
84TShHandleBase::~TShHandleBase() {
85 SetGlobalPoolAllocator(NULL);
86 allocator.popAll();
87}
88
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000089TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
90 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +000091 shaderSpec(spec),
92 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000093{
94}
95
96TCompiler::~TCompiler()
97{
98}
99
100bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000101{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000102 TScopedPoolAllocator scopedAlloc(&allocator, false);
103
alokp@chromium.org07620a52010-09-23 17:53:56 +0000104 // Generate built-in symbol table.
105 if (!InitBuiltInSymbolTable(resources))
106 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000107 InitExtensionBehavior(resources, extensionBehavior);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000108
alokp@chromium.org07620a52010-09-23 17:53:56 +0000109 return true;
110}
111
112bool TCompiler::compile(const char* const shaderStrings[],
113 const int numStrings,
114 int compileOptions)
115{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000116 TScopedPoolAllocator scopedAlloc(&allocator, true);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000117 clearResults();
118
119 if (numStrings == 0)
120 return true;
121
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000122 // If compiling for WebGL, validate loop and indexing as well.
123 if (shaderSpec == SH_WEBGL_SPEC)
124 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000125
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000126 // First string is path of source file if flag is set. The actual source follows.
127 const char* sourcePath = NULL;
128 int firstSource = 0;
129 if (compileOptions & SH_SOURCE_PATH)
130 {
131 sourcePath = shaderStrings[0];
132 ++firstSource;
133 }
134
alokp@chromium.org07620a52010-09-23 17:53:56 +0000135 TIntermediate intermediate(infoSink);
136 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000137 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000138 sourcePath, infoSink);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000139 GlobalParseContext = &parseContext;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000140
141 // We preserve symbols at the built-in level from compile-to-compile.
142 // Start pushing the user-defined symbols at global level.
143 symbolTable.push();
144 if (!symbolTable.atGlobalLevel())
145 infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
146
147 // Parse shader.
148 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000149 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000150 (parseContext.treeRoot != NULL);
151 if (success) {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000152 TIntermNode* root = parseContext.treeRoot;
153 success = intermediate.postProcess(root);
154
zmo@google.comb1762df2011-07-30 02:04:23 +0000155 if (success)
156 success = detectRecursion(root);
157
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000158 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
159 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000160
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000161 // Unroll for-loop markup needs to happen after validateLimitations pass.
162 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
163 ForLoopUnroll::MarkForLoopsWithIntegerIndicesForUnrolling(root);
164
zmo@google.com32e97312011-08-24 01:03:11 +0000165 // Built-in function emulation needs to happen after validateLimitations pass.
166 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
167 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
168
zmo@google.comfd747b82011-04-23 01:30:07 +0000169 // Call mapLongVariableNames() before collectAttribsUniforms() so in
170 // collectAttribsUniforms() we already have the mapped symbol names and
171 // we could composite mapped and original variable names.
zmo@google.comb1762df2011-07-30 02:04:23 +0000172 if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES))
zmo@google.comfd747b82011-04-23 01:30:07 +0000173 mapLongVariableNames(root);
174
175 if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS))
176 collectAttribsUniforms(root);
177
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000178 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000179 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000180
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000181 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000182 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000183 }
184
185 // Cleanup memory.
186 intermediate.remove(parseContext.treeRoot);
187 // Ensure symbol table is returned to the built-in level,
188 // throwing away all but the built-ins.
189 while (!symbolTable.atBuiltInLevel())
190 symbolTable.pop();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000191
192 return success;
193}
194
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000195bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000196{
197 TBuiltIns builtIns;
198
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000199 builtIns.initialize(shaderType, shaderSpec, resources);
200 return InitializeSymbolTable(builtIns.getBuiltInStrings(),
201 shaderType, shaderSpec, resources, infoSink, symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000202}
203
204void TCompiler::clearResults()
205{
206 infoSink.info.erase();
207 infoSink.obj.erase();
208 infoSink.debug.erase();
209
210 attribs.clear();
211 uniforms.clear();
212}
213
zmo@google.comb1762df2011-07-30 02:04:23 +0000214bool TCompiler::detectRecursion(TIntermNode* root)
215{
216 DetectRecursion detect;
217 root->traverse(&detect);
218 switch (detect.detectRecursion()) {
219 case DetectRecursion::kErrorNone:
220 return true;
221 case DetectRecursion::kErrorMissingMain:
222 infoSink.info.message(EPrefixError, "Missing main()");
223 return false;
224 case DetectRecursion::kErrorRecursion:
225 infoSink.info.message(EPrefixError, "Function recursion detected");
226 return false;
227 default:
228 UNREACHABLE();
229 return false;
230 }
231}
232
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000233bool TCompiler::validateLimitations(TIntermNode* root) {
234 ValidateLimitations validate(shaderType, infoSink.info);
235 root->traverse(&validate);
236 return validate.numErrors() == 0;
237}
238
alokp@chromium.org07620a52010-09-23 17:53:56 +0000239void TCompiler::collectAttribsUniforms(TIntermNode* root)
240{
241 CollectAttribsUniforms collect(attribs, uniforms);
242 root->traverse(&collect);
243}
zmo@google.comfd747b82011-04-23 01:30:07 +0000244
245void TCompiler::mapLongVariableNames(TIntermNode* root)
246{
zmo@google.com24c08c42011-05-27 17:40:48 +0000247 MapLongVariableNames map(varyingLongNameMap);
zmo@google.comfd747b82011-04-23 01:30:07 +0000248 root->traverse(&map);
249}
250
251int TCompiler::getMappedNameMaxLength() const
252{
253 return MAX_IDENTIFIER_NAME_SIZE + 1;
254}
zmo@google.com5601ea02011-06-10 18:23:25 +0000255
256const TExtensionBehavior& TCompiler::getExtensionBehavior() const
257{
258 return extensionBehavior;
259}
zmo@google.com32e97312011-08-24 01:03:11 +0000260
261const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
262{
263 return builtInFunctionEmulator;
264}