blob: 554222543ce8f88577ab99cfdfba32af3e398654 [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),
91 shaderSpec(spec)
92{
93}
94
95TCompiler::~TCompiler()
96{
97}
98
99bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000100{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000101 TScopedPoolAllocator scopedAlloc(&allocator, false);
102
alokp@chromium.org07620a52010-09-23 17:53:56 +0000103 // Generate built-in symbol table.
104 if (!InitBuiltInSymbolTable(resources))
105 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000106 InitExtensionBehavior(resources, extensionBehavior);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000107
alokp@chromium.org07620a52010-09-23 17:53:56 +0000108 return true;
109}
110
111bool TCompiler::compile(const char* const shaderStrings[],
112 const int numStrings,
113 int compileOptions)
114{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000115 TScopedPoolAllocator scopedAlloc(&allocator, true);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000116 clearResults();
117
118 if (numStrings == 0)
119 return true;
120
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000121 // If compiling for WebGL, validate loop and indexing as well.
122 if (shaderSpec == SH_WEBGL_SPEC)
123 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000124
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000125 // First string is path of source file if flag is set. The actual source follows.
126 const char* sourcePath = NULL;
127 int firstSource = 0;
128 if (compileOptions & SH_SOURCE_PATH)
129 {
130 sourcePath = shaderStrings[0];
131 ++firstSource;
132 }
133
alokp@chromium.org07620a52010-09-23 17:53:56 +0000134 TIntermediate intermediate(infoSink);
135 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000136 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000137 sourcePath, infoSink);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000138 GlobalParseContext = &parseContext;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000139
140 // We preserve symbols at the built-in level from compile-to-compile.
141 // Start pushing the user-defined symbols at global level.
142 symbolTable.push();
143 if (!symbolTable.atGlobalLevel())
144 infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
145
146 // Parse shader.
147 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000148 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000149 (parseContext.treeRoot != NULL);
150 if (success) {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000151 TIntermNode* root = parseContext.treeRoot;
152 success = intermediate.postProcess(root);
153
zmo@google.comb1762df2011-07-30 02:04:23 +0000154 if (success)
155 success = detectRecursion(root);
156
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000157 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
158 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000159
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000160 // Unroll for-loop markup needs to happen after validateLimitations pass.
161 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
162 ForLoopUnroll::MarkForLoopsWithIntegerIndicesForUnrolling(root);
163
zmo@google.com32e97312011-08-24 01:03:11 +0000164 // Built-in function emulation needs to happen after validateLimitations pass.
165 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
166 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
167
zmo@google.comfd747b82011-04-23 01:30:07 +0000168 // Call mapLongVariableNames() before collectAttribsUniforms() so in
169 // collectAttribsUniforms() we already have the mapped symbol names and
170 // we could composite mapped and original variable names.
zmo@google.comb1762df2011-07-30 02:04:23 +0000171 if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES))
zmo@google.comfd747b82011-04-23 01:30:07 +0000172 mapLongVariableNames(root);
173
174 if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS))
175 collectAttribsUniforms(root);
176
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000177 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000178 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000179
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000180 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000181 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000182 }
183
184 // Cleanup memory.
185 intermediate.remove(parseContext.treeRoot);
186 // Ensure symbol table is returned to the built-in level,
187 // throwing away all but the built-ins.
188 while (!symbolTable.atBuiltInLevel())
189 symbolTable.pop();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000190
191 return success;
192}
193
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000194bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195{
196 TBuiltIns builtIns;
197
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000198 builtIns.initialize(shaderType, shaderSpec, resources);
199 return InitializeSymbolTable(builtIns.getBuiltInStrings(),
200 shaderType, shaderSpec, resources, infoSink, symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000201}
202
203void TCompiler::clearResults()
204{
205 infoSink.info.erase();
206 infoSink.obj.erase();
207 infoSink.debug.erase();
208
209 attribs.clear();
210 uniforms.clear();
211}
212
zmo@google.comb1762df2011-07-30 02:04:23 +0000213bool TCompiler::detectRecursion(TIntermNode* root)
214{
215 DetectRecursion detect;
216 root->traverse(&detect);
217 switch (detect.detectRecursion()) {
218 case DetectRecursion::kErrorNone:
219 return true;
220 case DetectRecursion::kErrorMissingMain:
221 infoSink.info.message(EPrefixError, "Missing main()");
222 return false;
223 case DetectRecursion::kErrorRecursion:
224 infoSink.info.message(EPrefixError, "Function recursion detected");
225 return false;
226 default:
227 UNREACHABLE();
228 return false;
229 }
230}
231
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000232bool TCompiler::validateLimitations(TIntermNode* root) {
233 ValidateLimitations validate(shaderType, infoSink.info);
234 root->traverse(&validate);
235 return validate.numErrors() == 0;
236}
237
alokp@chromium.org07620a52010-09-23 17:53:56 +0000238void TCompiler::collectAttribsUniforms(TIntermNode* root)
239{
240 CollectAttribsUniforms collect(attribs, uniforms);
241 root->traverse(&collect);
242}
zmo@google.comfd747b82011-04-23 01:30:07 +0000243
244void TCompiler::mapLongVariableNames(TIntermNode* root)
245{
zmo@google.com24c08c42011-05-27 17:40:48 +0000246 MapLongVariableNames map(varyingLongNameMap);
zmo@google.comfd747b82011-04-23 01:30:07 +0000247 root->traverse(&map);
248}
249
250int TCompiler::getMappedNameMaxLength() const
251{
252 return MAX_IDENTIFIER_NAME_SIZE + 1;
253}
zmo@google.com5601ea02011-06-10 18:23:25 +0000254
255const TExtensionBehavior& TCompiler::getExtensionBehavior() const
256{
257 return extensionBehavior;
258}
zmo@google.com32e97312011-08-24 01:03:11 +0000259
260const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
261{
262 return builtInFunctionEmulator;
263}