blob: 396b390dcb8bd8de1f75a33e598ee8c828a52b5f [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 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
Dmitry Skiba01971112015-07-10 14:54:00 -04007#include "compiler/translator/Cache.h"
Jamie Madilld4a3a312014-06-25 16:04:56 -04008#include "compiler/translator/Compiler.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -08009#include "compiler/translator/CallDAG.h"
Geoff Lang17732822013-08-29 13:46:49 -040010#include "compiler/translator/ForLoopUnroll.h"
11#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080013#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040014#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030015#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070016#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030017#include "compiler/translator/RemovePow.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/RenameFunction.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070019#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070020#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040021#include "compiler/translator/ValidateLimitations.h"
22#include "compiler/translator/ValidateOutputs.h"
23#include "compiler/translator/VariablePacker.h"
24#include "compiler/translator/depgraph/DependencyGraph.h"
25#include "compiler/translator/depgraph/DependencyGraphOutput.h"
26#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
27#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000028#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040029#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040030#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000031
Jamie Madill5508f392014-02-20 13:31:36 -050032bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000033{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070034 return (spec == SH_WEBGL_SPEC ||
35 spec == SH_CSS_SHADERS_SPEC ||
36 spec == SH_WEBGL2_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000037}
38
Qingqing Dengad0d0792015-04-08 14:25:06 -070039bool IsGLSL130OrNewer(ShShaderOutput output)
40{
41 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070042 output == SH_GLSL_140_OUTPUT ||
43 output == SH_GLSL_150_CORE_OUTPUT ||
44 output == SH_GLSL_330_CORE_OUTPUT ||
45 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070046 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070047 output == SH_GLSL_420_CORE_OUTPUT ||
48 output == SH_GLSL_430_CORE_OUTPUT ||
49 output == SH_GLSL_440_CORE_OUTPUT ||
50 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070051}
52
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070053size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050054{
Jamie Madill88f6e942014-02-19 10:27:53 -050055 // WebGL defines a max token legnth of 256, while ES2 leaves max token
56 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070057 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050058 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070059 case SH_WEBGL_SPEC:
60 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050061 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070062 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050063 return 1024;
64 }
65}
66
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000067namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070068
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080069class TScopedPoolAllocator
70{
71 public:
72 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
73 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040074 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000075 SetGlobalPoolAllocator(mAllocator);
76 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080077 ~TScopedPoolAllocator()
78 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000079 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040080 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000081 }
82
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080083 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040085};
86
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080087class TScopedSymbolTableLevel
88{
89 public:
90 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
91 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040092 ASSERT(mTable->atBuiltInLevel());
93 mTable->push();
94 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080095 ~TScopedSymbolTableLevel()
96 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040097 while (!mTable->atBuiltInLevel())
98 mTable->pop();
99 }
100
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800101 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400102 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000103};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700104
105int MapSpecToShaderVersion(ShShaderSpec spec)
106{
107 switch (spec)
108 {
109 case SH_GLES2_SPEC:
110 case SH_WEBGL_SPEC:
111 case SH_CSS_SHADERS_SPEC:
112 return 100;
113 case SH_GLES3_SPEC:
114 case SH_WEBGL2_SPEC:
115 return 300;
116 default:
117 UNREACHABLE();
118 return 0;
119 }
120}
121
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000122} // namespace
123
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800124TShHandleBase::TShHandleBase()
125{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000126 allocator.push();
127 SetGlobalPoolAllocator(&allocator);
128}
129
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800130TShHandleBase::~TShHandleBase()
131{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000132 SetGlobalPoolAllocator(NULL);
133 allocator.popAll();
134}
135
Jamie Madill183bde52014-07-02 15:31:19 -0400136TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000137 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000138 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400139 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400140 maxUniformVectors(0),
141 maxExpressionComplexity(0),
142 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000143 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000144 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200145 builtInFunctionEmulator(),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200146 mSourcePath(NULL)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000147{
148}
149
150TCompiler::~TCompiler()
151{
152}
153
154bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000155{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000156 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400157 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000158 resources.MaxVertexUniformVectors :
159 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400160 maxExpressionComplexity = resources.MaxExpressionComplexity;
161 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400162
163 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000164
alokp@chromium.org07620a52010-09-23 17:53:56 +0000165 // Generate built-in symbol table.
166 if (!InitBuiltInSymbolTable(resources))
167 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000169 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000170
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000171 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
172 clampingStrategy = resources.ArrayIndexClampingStrategy;
173
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000174 hashFunction = resources.HashFunction;
175
alokp@chromium.org07620a52010-09-23 17:53:56 +0000176 return true;
177}
178
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200179TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
180 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000181{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200182 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
183}
184
185TIntermNode *TCompiler::compileTreeImpl(const char* const shaderStrings[],
186 size_t numStrings, int compileOptions)
187{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000188 clearResults();
189
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200190 ASSERT(numStrings > 0);
191 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000192
David Yen0fbd1282015-02-02 14:46:09 -0800193 // Reset the extension behavior for each compilation unit.
194 ResetExtensionBehavior(extensionBehavior);
195
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000196 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500197 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000198 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000199
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000200 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000201 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000202 if (compileOptions & SH_SOURCE_PATH)
203 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200204 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000205 ++firstSource;
206 }
207
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200208 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000209 TIntermediate intermediate(infoSink);
210 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000211 shaderType, shaderSpec, compileOptions, true,
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200212 infoSink, debugShaderPrecision);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200213
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400214 parseContext.setFragmentPrecisionHigh(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400215 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000216
217 // We preserve symbols at the built-in level from compile-to-compile.
218 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400219 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000220
221 // Parse shader.
222 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400223 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
224 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000225
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000226 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700227 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
228 {
229 infoSink.info.prefix(EPrefixError);
230 infoSink.info << "unsupported shader version";
231 success = false;
232 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000233
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400234 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200235
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800236 if (success)
237 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700238 mPragma = parseContext.pragma();
239 if (mPragma.stdgl.invariantAll)
240 {
241 symbolTable.setGlobalInvariant();
242 }
243
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400244 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300245 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000246
Jamie Madill6654bc92014-03-26 14:01:57 -0400247 // Disallow expressions deemed too complex.
248 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
249 success = limitExpressionComplexity(root);
250
Corentin Wallez71d147f2015-02-11 11:15:24 -0800251 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000252 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800253 success = initCallDag(root);
254
255 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
256 success = checkCallDepth();
257
258 // Checks which functions are used and if "main" exists
259 if (success)
260 {
261 functionMetadata.clear();
262 functionMetadata.resize(mCallDag.size());
263 success = tagUsedFunctions();
264 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000265
Corentin Walleza094a8a2015-04-07 11:53:06 -0700266 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
267 success = pruneUnusedFunctions(root);
268
Olli Etuahoc6833112015-04-22 15:15:54 +0300269 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
270 if (success)
271 PruneEmptyDeclarations(root);
272
Jamie Madill183bde52014-07-02 15:31:19 -0400273 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400274 success = validateOutputs(root);
275
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000276 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
277 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000278
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000279 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000280 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000281
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000282 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
283 rewriteCSSShader(root);
284
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000285 // Unroll for-loop markup needs to happen after validateLimitations pass.
286 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800287 {
288 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800289 root->traverse(&marker);
290 }
291 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800292 {
293 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800294 root->traverse(&marker);
295 if (marker.samplerArrayIndexIsFloatLoopIndex())
296 {
297 infoSink.info.prefix(EPrefixError);
298 infoSink.info << "sampler array index is float loop index";
299 success = false;
300 }
301 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000302
zmo@google.com32e97312011-08-24 01:03:11 +0000303 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200304 if (success)
305 {
306 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000307 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200308 }
zmo@google.com32e97312011-08-24 01:03:11 +0000309
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000310 // Clamping uniform array bounds needs to happen after validateLimitations pass.
311 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
312 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
313
Jamie Madill183bde52014-07-02 15:31:19 -0400314 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800315 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400316
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800317 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
318 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700319 UnfoldShortCircuitAST unfoldShortCircuit;
320 root->traverse(&unfoldShortCircuit);
321 unfoldShortCircuit.updateTree();
322 }
323
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300324 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
325 {
326 RemovePow(root);
327 }
328
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800329 if (success && (compileOptions & SH_VARIABLES))
330 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400331 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800332 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
333 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000334 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800335 if (!success)
336 {
Jamie Madill075edd82013-07-08 13:30:19 -0400337 infoSink.info.prefix(EPrefixError);
338 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000339 }
340 }
Jamie Madill183bde52014-07-02 15:31:19 -0400341 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800342 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
343 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000344 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000345
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700346 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
347 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700348 ScalarizeVecAndMatConstructorArgs scalarizer(
349 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700350 root->traverse(&scalarizer);
351 }
352
Zhenyao Moe740add2014-07-18 17:01:01 -0700353 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
354 {
355 RegenerateStructNames gen(symbolTable, shaderVersion);
356 root->traverse(&gen);
357 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000358 }
359
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700360 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200361 if (success)
362 return root;
363
364 return NULL;
365}
366
367bool TCompiler::compile(const char* const shaderStrings[],
368 size_t numStrings, int compileOptions)
369{
370 if (numStrings == 0)
371 return true;
372
373 TScopedPoolAllocator scopedAlloc(&allocator);
374 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
375
376 if (root)
377 {
378 if (compileOptions & SH_INTERMEDIATE_TREE)
379 TIntermediate::outputTree(root, infoSink.info);
380
381 if (compileOptions & SH_OBJECT_CODE)
382 translate(root, compileOptions);
383
384 // The IntermNode tree doesn't need to be deleted here, since the
385 // memory will be freed in a big chunk by the PoolAllocator.
386 return true;
387 }
388 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000389}
390
Nicolas Capens49a88872013-06-20 09:54:03 -0400391bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000392{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000393 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400394 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000395
Nicolas Capens49a88872013-06-20 09:54:03 -0400396 assert(symbolTable.isEmpty());
397 symbolTable.push(); // COMMON_BUILTINS
398 symbolTable.push(); // ESSL1_BUILTINS
399 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000400
Nicolas Capens49a88872013-06-20 09:54:03 -0400401 TPublicType integer;
402 integer.type = EbtInt;
403 integer.primarySize = 1;
404 integer.secondarySize = 1;
405 integer.array = false;
406
407 TPublicType floatingPoint;
408 floatingPoint.type = EbtFloat;
409 floatingPoint.primarySize = 1;
410 floatingPoint.secondarySize = 1;
411 floatingPoint.array = false;
412
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400413 TPublicType sampler;
414 sampler.primarySize = 1;
415 sampler.secondarySize = 1;
416 sampler.array = false;
417
Nicolas Capens49a88872013-06-20 09:54:03 -0400418 switch(shaderType)
419 {
Jamie Madill183bde52014-07-02 15:31:19 -0400420 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400421 symbolTable.setDefaultPrecision(integer, EbpMedium);
422 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400423 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400424 symbolTable.setDefaultPrecision(integer, EbpHigh);
425 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
426 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800427 default:
428 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400429 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400430 // We set defaults for all the sampler types, even those that are
431 // only available if an extension exists.
432 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800433 samplerType < EbtGuardSamplerEnd; ++samplerType)
434 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400435 sampler.type = static_cast<TBasicType>(samplerType);
436 symbolTable.setDefaultPrecision(sampler, EbpLow);
437 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400438
Jamie Madill1b452142013-07-12 14:51:11 -0400439 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400440
441 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
442
443 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000444}
445
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400446void TCompiler::setResourceString()
447{
448 std::ostringstream strstream;
449 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
450 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
451 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
452 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
453 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
454 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
455 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
456 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
457 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
458 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
459 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
460 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
461 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
462 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
463 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300464 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400465 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
466 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100467 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
468 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
469 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400470 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
471 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
472 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300473 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300474 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200475 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
476 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400477
478 builtInResourcesString = strstream.str();
479}
480
alokp@chromium.org07620a52010-09-23 17:53:56 +0000481void TCompiler::clearResults()
482{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000483 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000484 infoSink.info.erase();
485 infoSink.obj.erase();
486 infoSink.debug.erase();
487
Jamie Madilled27c722014-07-02 15:31:23 -0400488 attributes.clear();
489 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000490 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400491 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400492 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400493 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000494
495 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000496
497 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200498
499 mSourcePath = NULL;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000500}
501
Corentin Wallez71d147f2015-02-11 11:15:24 -0800502bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000503{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800504 mCallDag.clear();
505
506 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800507 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800508 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800509 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800510 case CallDAG::INITDAG_RECURSION:
511 infoSink.info.prefix(EPrefixError);
512 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800513 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800514 case CallDAG::INITDAG_UNDEFINED:
515 infoSink.info.prefix(EPrefixError);
516 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800517 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800518 }
519
520 UNREACHABLE();
521 return true;
522}
523
524bool TCompiler::checkCallDepth()
525{
526 std::vector<int> depths(mCallDag.size());
527
528 for (size_t i = 0; i < mCallDag.size(); i++)
529 {
530 int depth = 0;
531 auto &record = mCallDag.getRecordFromIndex(i);
532
533 for (auto &calleeIndex : record.callees)
534 {
535 depth = std::max(depth, depths[calleeIndex] + 1);
536 }
537
538 depths[i] = depth;
539
540 if (depth >= maxCallStackDepth)
541 {
542 // Trace back the function chain to have a meaningful info log.
543 infoSink.info.prefix(EPrefixError);
544 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
545 << ") with the following call chain: " << record.name;
546
Jamie Madillb1956432015-08-12 17:35:20 +0000547 int currentFunction = i;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800548 int currentDepth = depth;
549
550 while (currentFunction != -1)
551 {
552 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
553
554 int nextFunction = -1;
555 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
556 {
557 if (depths[calleeIndex] == currentDepth - 1)
558 {
559 currentDepth--;
560 nextFunction = calleeIndex;
561 }
562 }
563
564 currentFunction = nextFunction;
565 }
566
567 return false;
568 }
569 }
570
571 return true;
572}
573
574bool TCompiler::tagUsedFunctions()
575{
576 // Search from main, starting from the end of the DAG as it usually is the root.
Jamie Madillb1956432015-08-12 17:35:20 +0000577 for (int i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800578 {
579 if (mCallDag.getRecordFromIndex(i).name == "main(")
580 {
581 internalTagUsedFunction(i);
582 return true;
583 }
584 }
585
586 infoSink.info.prefix(EPrefixError);
587 infoSink.info << "Missing main()";
588 return false;
589}
590
591void TCompiler::internalTagUsedFunction(size_t index)
592{
593 if (functionMetadata[index].used)
594 {
595 return;
596 }
597
598 functionMetadata[index].used = true;
599
600 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
601 {
602 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000603 }
604}
605
Corentin Walleza094a8a2015-04-07 11:53:06 -0700606// A predicate for the stl that returns if a top-level node is unused
607class TCompiler::UnusedPredicate
608{
609 public:
610 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
611 : mCallDag(callDag),
612 mMetadatas(metadatas)
613 {
614 }
615
616 bool operator ()(TIntermNode *node)
617 {
618 const TIntermAggregate *asAggregate = node->getAsAggregate();
619
620 if (asAggregate == nullptr)
621 {
622 return false;
623 }
624
625 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
626 {
627 return false;
628 }
629
630 size_t callDagIndex = mCallDag->findIndex(asAggregate);
631 if (callDagIndex == CallDAG::InvalidIndex)
632 {
633 // This happens only for unimplemented prototypes which are thus unused
634 ASSERT(asAggregate->getOp() == EOpPrototype);
635 return true;
636 }
637
638 ASSERT(callDagIndex < mMetadatas->size());
639 return !(*mMetadatas)[callDagIndex].used;
640 }
641
642 private:
643 const CallDAG *mCallDag;
644 const std::vector<FunctionMetadata> *mMetadatas;
645};
646
647bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
648{
649 TIntermAggregate *rootNode = root->getAsAggregate();
650 ASSERT(rootNode != nullptr);
651
652 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
653 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700654
655 if (!sequence->empty())
656 {
657 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
658 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700659
660 return true;
661}
662
Jamie Madill05a80ce2013-06-20 11:55:49 -0400663bool TCompiler::validateOutputs(TIntermNode* root)
664{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300665 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400666 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300667 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400668}
669
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000670void TCompiler::rewriteCSSShader(TIntermNode* root)
671{
672 RenameFunction renamer("main(", "css_main(");
673 root->traverse(&renamer);
674}
675
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800676bool TCompiler::validateLimitations(TIntermNode* root)
677{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000678 ValidateLimitations validate(shaderType, infoSink.info);
679 root->traverse(&validate);
680 return validate.numErrors() == 0;
681}
682
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000683bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000684{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800685 if (shaderSpec != SH_WEBGL_SPEC)
686 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000687 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
688 return false;
689 }
690
Jamie Madill183bde52014-07-02 15:31:19 -0400691 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800692 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000693 TDependencyGraph graph(root);
694
695 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000696 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500697
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000698 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800699 if (outputGraph)
700 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000701 TDependencyGraphOutput output(infoSink.info);
702 output.outputAllSpanningTrees(graph);
703 }
Jamie Madill5508f392014-02-20 13:31:36 -0500704
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000705 return success;
706 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800707 else
708 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000709 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000710 }
711}
712
Jamie Madilleb1a0102013-07-08 13:31:38 -0400713bool TCompiler::limitExpressionComplexity(TIntermNode* root)
714{
Jamie Madill6654bc92014-03-26 14:01:57 -0400715 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400716 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400717
718 if (traverser.getMaxDepth() > maxExpressionComplexity)
719 {
720 infoSink.info << "Expression too complex.";
721 return false;
722 }
723
Jamie Madilleb1a0102013-07-08 13:31:38 -0400724 TDependencyGraph graph(root);
725
726 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
727 iter != graph.endUserDefinedFunctionCalls();
728 ++iter)
729 {
730 TGraphFunctionCall* samplerSymbol = *iter;
731 TDependencyGraphTraverser graphTraverser;
732 samplerSymbol->traverse(&graphTraverser);
733 }
734
Jamie Madilleb1a0102013-07-08 13:31:38 -0400735 return true;
736}
737
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000738bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000739{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000740 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000741 restrictor.enforceRestrictions(graph);
742 return restrictor.numErrors() == 0;
743}
744
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000745bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000746{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000747 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000748 restrictor.enforceRestrictions(root);
749 return restrictor.numErrors() == 0;
750}
751
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400752void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000753{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400754 sh::CollectVariables collect(&attributes,
755 &outputVariables,
756 &uniforms,
757 &varyings,
758 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700759 hashFunction,
760 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000761 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400762
Zhenyao Mo409078f2014-10-28 13:23:18 -0700763 // This is for enforcePackingRestriction().
764 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000765}
zmo@google.comfd747b82011-04-23 01:30:07 +0000766
gman@chromium.org8d804792012-10-17 21:33:48 +0000767bool TCompiler::enforcePackingRestrictions()
768{
769 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400770 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000771}
772
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800773void TCompiler::initializeGLPosition(TIntermNode* root)
774{
775 InitializeVariables::InitVariableInfoList variables;
776 InitializeVariables::InitVariableInfo var(
777 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
778 variables.push_back(var);
779 InitializeVariables initializer(variables);
780 root->traverse(&initializer);
781}
782
783void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
784{
785 InitializeVariables::InitVariableInfoList variables;
786 for (size_t ii = 0; ii < varyings.size(); ++ii)
787 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400788 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800789 if (varying.staticUse)
790 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400791 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
792 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400793 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800794 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400795 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800796 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400797 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800798 name = name.substr(0, name.find_first_of('['));
799 }
800
801 InitializeVariables::InitVariableInfo var(name, type);
802 variables.push_back(var);
803 }
804 InitializeVariables initializer(variables);
805 root->traverse(&initializer);
806}
807
zmo@google.com5601ea02011-06-10 18:23:25 +0000808const TExtensionBehavior& TCompiler::getExtensionBehavior() const
809{
810 return extensionBehavior;
811}
zmo@google.com32e97312011-08-24 01:03:11 +0000812
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200813const char *TCompiler::getSourcePath() const
814{
815 return mSourcePath;
816}
817
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000818const ShBuiltInResources& TCompiler::getResources() const
819{
820 return compileResources;
821}
822
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000823const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
824{
825 return arrayBoundsClamper;
826}
827
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000828ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
829{
830 return clampingStrategy;
831}
832
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200833const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000834{
835 return builtInFunctionEmulator;
836}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700837
838void TCompiler::writePragma()
839{
840 TInfoSinkBase &sink = infoSink.obj;
841 if (mPragma.stdgl.invariantAll)
842 sink << "#pragma STDGL invariant(all)\n";
843}