blob: f27cb75d515e77af2d19283bdfc20d6e45e33ba9 [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
zmo@google.comb9f64aa2012-01-20 00:35:15 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// 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"
zmo@google.comb9f64aa2012-01-20 00:35:15 +000011#include "compiler/MapLongVariableNames.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000012#include "compiler/ParseHelper.h"
13#include "compiler/ShHandle.h"
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000014#include "compiler/ValidateLimitations.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),
zmo@google.com9996b8e2012-01-19 01:43:55 +000092 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000093{
zmo@google.comb9f64aa2012-01-20 00:35:15 +000094 longNameMap = LongNameMap::GetInstance();
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000095}
96
97TCompiler::~TCompiler()
98{
zmo@google.comb9f64aa2012-01-20 00:35:15 +000099 ASSERT(longNameMap);
100 longNameMap->Release();
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000101}
102
103bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000104{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000105 TScopedPoolAllocator scopedAlloc(&allocator, false);
106
alokp@chromium.org07620a52010-09-23 17:53:56 +0000107 // Generate built-in symbol table.
108 if (!InitBuiltInSymbolTable(resources))
109 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000110 InitExtensionBehavior(resources, extensionBehavior);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000111
alokp@chromium.org07620a52010-09-23 17:53:56 +0000112 return true;
113}
114
115bool TCompiler::compile(const char* const shaderStrings[],
116 const int numStrings,
117 int compileOptions)
118{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000119 TScopedPoolAllocator scopedAlloc(&allocator, true);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000120 clearResults();
121
122 if (numStrings == 0)
123 return true;
124
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000125 // If compiling for WebGL, validate loop and indexing as well.
126 if (shaderSpec == SH_WEBGL_SPEC)
127 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000128
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000129 // First string is path of source file if flag is set. The actual source follows.
130 const char* sourcePath = NULL;
131 int firstSource = 0;
132 if (compileOptions & SH_SOURCE_PATH)
133 {
134 sourcePath = shaderStrings[0];
135 ++firstSource;
136 }
137
alokp@chromium.org07620a52010-09-23 17:53:56 +0000138 TIntermediate intermediate(infoSink);
139 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000140 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000141 sourcePath, infoSink);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000142 GlobalParseContext = &parseContext;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000143
144 // We preserve symbols at the built-in level from compile-to-compile.
145 // Start pushing the user-defined symbols at global level.
146 symbolTable.push();
147 if (!symbolTable.atGlobalLevel())
148 infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
149
150 // Parse shader.
151 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000152 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000153 (parseContext.treeRoot != NULL);
154 if (success) {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000155 TIntermNode* root = parseContext.treeRoot;
156 success = intermediate.postProcess(root);
157
zmo@google.comb1762df2011-07-30 02:04:23 +0000158 if (success)
159 success = detectRecursion(root);
160
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000161 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
162 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000163
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000164 // Unroll for-loop markup needs to happen after validateLimitations pass.
165 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
166 ForLoopUnroll::MarkForLoopsWithIntegerIndicesForUnrolling(root);
167
zmo@google.com32e97312011-08-24 01:03:11 +0000168 // Built-in function emulation needs to happen after validateLimitations pass.
169 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
170 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
171
zmo@google.comfd747b82011-04-23 01:30:07 +0000172 // Call mapLongVariableNames() before collectAttribsUniforms() so in
173 // collectAttribsUniforms() we already have the mapped symbol names and
174 // we could composite mapped and original variable names.
zmo@google.comb1762df2011-07-30 02:04:23 +0000175 if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES))
zmo@google.comfd747b82011-04-23 01:30:07 +0000176 mapLongVariableNames(root);
177
178 if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS))
179 collectAttribsUniforms(root);
180
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000181 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000182 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000183
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000184 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000185 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000186 }
187
188 // Cleanup memory.
189 intermediate.remove(parseContext.treeRoot);
190 // Ensure symbol table is returned to the built-in level,
191 // throwing away all but the built-ins.
192 while (!symbolTable.atBuiltInLevel())
193 symbolTable.pop();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000194
195 return success;
196}
197
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000198bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199{
200 TBuiltIns builtIns;
201
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000202 builtIns.initialize(shaderType, shaderSpec, resources);
203 return InitializeSymbolTable(builtIns.getBuiltInStrings(),
204 shaderType, shaderSpec, resources, infoSink, symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000205}
206
207void TCompiler::clearResults()
208{
209 infoSink.info.erase();
210 infoSink.obj.erase();
211 infoSink.debug.erase();
212
213 attribs.clear();
214 uniforms.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000215
216 builtInFunctionEmulator.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000217}
218
zmo@google.comb1762df2011-07-30 02:04:23 +0000219bool TCompiler::detectRecursion(TIntermNode* root)
220{
221 DetectRecursion detect;
222 root->traverse(&detect);
223 switch (detect.detectRecursion()) {
224 case DetectRecursion::kErrorNone:
225 return true;
226 case DetectRecursion::kErrorMissingMain:
227 infoSink.info.message(EPrefixError, "Missing main()");
228 return false;
229 case DetectRecursion::kErrorRecursion:
230 infoSink.info.message(EPrefixError, "Function recursion detected");
231 return false;
232 default:
233 UNREACHABLE();
234 return false;
235 }
236}
237
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000238bool TCompiler::validateLimitations(TIntermNode* root) {
239 ValidateLimitations validate(shaderType, infoSink.info);
240 root->traverse(&validate);
241 return validate.numErrors() == 0;
242}
243
alokp@chromium.org07620a52010-09-23 17:53:56 +0000244void TCompiler::collectAttribsUniforms(TIntermNode* root)
245{
246 CollectAttribsUniforms collect(attribs, uniforms);
247 root->traverse(&collect);
248}
zmo@google.comfd747b82011-04-23 01:30:07 +0000249
250void TCompiler::mapLongVariableNames(TIntermNode* root)
251{
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000252 ASSERT(longNameMap);
253 MapLongVariableNames map(longNameMap);
zmo@google.com9996b8e2012-01-19 01:43:55 +0000254 root->traverse(&map);
zmo@google.comfd747b82011-04-23 01:30:07 +0000255}
256
257int TCompiler::getMappedNameMaxLength() const
258{
kbr@chromium.org22152112011-10-26 01:18:28 +0000259 return MAX_SHORTENED_IDENTIFIER_SIZE + 1;
zmo@google.comfd747b82011-04-23 01:30:07 +0000260}
zmo@google.com5601ea02011-06-10 18:23:25 +0000261
262const TExtensionBehavior& TCompiler::getExtensionBehavior() const
263{
264 return extensionBehavior;
265}
zmo@google.com32e97312011-08-24 01:03:11 +0000266
267const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
268{
269 return builtInFunctionEmulator;
270}