blob: 0086577d1562881c84db418271388459b84545a5 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Nicolas Capenscc863da2015-01-21 15:50:55 -05007#include "AnalyzeCallDepth.h"
8#include "Initialize.h"
9#include "InitializeParseContext.h"
10#include "ParseHelper.h"
11#include "ShHandle.h"
12#include "ValidateLimitations.h"
John Bauman66b8ab22014-05-06 15:57:45 -040013
John Baumand4ae8632014-05-06 16:18:33 -040014namespace
John Bauman66b8ab22014-05-06 15:57:45 -040015{
John Bauman66b8ab22014-05-06 15:57:45 -040016class TScopedPoolAllocator {
17public:
18 TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop)
19 : mAllocator(allocator), mPushPopAllocator(pushPop) {
20 if (mPushPopAllocator) mAllocator->push();
21 SetGlobalPoolAllocator(mAllocator);
22 }
23 ~TScopedPoolAllocator() {
24 SetGlobalPoolAllocator(NULL);
25 if (mPushPopAllocator) mAllocator->pop();
26 }
27
28private:
29 TPoolAllocator* mAllocator;
30 bool mPushPopAllocator;
31};
32} // namespace
33
34TShHandleBase::TShHandleBase() {
35 allocator.push();
36 SetGlobalPoolAllocator(&allocator);
37}
38
39TShHandleBase::~TShHandleBase() {
40 SetGlobalPoolAllocator(NULL);
41 allocator.popAll();
42}
43
44TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
45 : shaderType(type),
John Baumand4ae8632014-05-06 16:18:33 -040046 shaderSpec(spec),
47 maxCallStackDepth(UINT_MAX)
John Bauman66b8ab22014-05-06 15:57:45 -040048{
49}
50
51TCompiler::~TCompiler()
52{
53}
54
55bool TCompiler::Init(const ShBuiltInResources& resources)
56{
John Baumand4ae8632014-05-06 16:18:33 -040057 maxCallStackDepth = resources.MaxCallStackDepth;
John Bauman66b8ab22014-05-06 15:57:45 -040058 TScopedPoolAllocator scopedAlloc(&allocator, false);
59
60 // Generate built-in symbol table.
61 if (!InitBuiltInSymbolTable(resources))
62 return false;
63 InitExtensionBehavior(resources, extensionBehavior);
64
65 return true;
66}
67
68bool TCompiler::compile(const char* const shaderStrings[],
69 const int numStrings,
70 int compileOptions)
71{
72 TScopedPoolAllocator scopedAlloc(&allocator, true);
73 clearResults();
74
75 if (numStrings == 0)
76 return true;
77
78 // If compiling for WebGL, validate loop and indexing as well.
79 if (shaderSpec == SH_WEBGL_SPEC)
80 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
81
82 // First string is path of source file if flag is set. The actual source follows.
83 const char* sourcePath = NULL;
84 int firstSource = 0;
85 if (compileOptions & SH_SOURCE_PATH)
86 {
87 sourcePath = shaderStrings[0];
88 ++firstSource;
89 }
90
91 TIntermediate intermediate(infoSink);
92 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
93 shaderType, shaderSpec, compileOptions, true,
94 sourcePath, infoSink);
Nicolas Capens978ddc52014-11-11 12:42:08 -050095 SetGlobalParseContext(&parseContext);
John Bauman66b8ab22014-05-06 15:57:45 -040096
97 // We preserve symbols at the built-in level from compile-to-compile.
98 // Start pushing the user-defined symbols at global level.
99 symbolTable.push();
100 if (!symbolTable.atGlobalLevel())
101 infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
102
103 // Parse shader.
104 bool success =
105 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
106 (parseContext.treeRoot != NULL);
107 if (success) {
108 TIntermNode* root = parseContext.treeRoot;
109 success = intermediate.postProcess(root);
110
111 if (success)
John Baumand4ae8632014-05-06 16:18:33 -0400112 success = validateCallDepth(root, infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400113
114 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
115 success = validateLimitations(root);
116
John Bauman66b8ab22014-05-06 15:57:45 -0400117 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
118 intermediate.outputTree(root);
119
120 if (success && (compileOptions & SH_OBJECT_CODE))
Nicolas Capens014b9a62014-10-15 10:28:29 -0400121 success = translate(root);
John Bauman66b8ab22014-05-06 15:57:45 -0400122 }
123
John Bauman66b8ab22014-05-06 15:57:45 -0400124 // Ensure symbol table is returned to the built-in level,
125 // throwing away all but the built-ins.
126 while (!symbolTable.atBuiltInLevel())
127 symbolTable.pop();
128
129 return success;
130}
131
John Baumand4ae8632014-05-06 16:18:33 -0400132bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
John Bauman66b8ab22014-05-06 15:57:45 -0400133{
John Baumand4ae8632014-05-06 16:18:33 -0400134 assert(symbolTable.isEmpty());
135
136 //
137 // Push the symbol table to give it an initial scope. This
138 // push should not have a corresponding pop, so that built-ins
139 // are preserved, and the test for an empty table fails.
140 //
141 symbolTable.push();
John Bauman66b8ab22014-05-06 15:57:45 -0400142
John Baumand4ae8632014-05-06 16:18:33 -0400143 TPublicType integer;
144 integer.type = EbtInt;
145 integer.size = 1;
146 integer.matrix = false;
147 integer.array = false;
148
149 TPublicType floatingPoint;
150 floatingPoint.type = EbtFloat;
151 floatingPoint.size = 1;
152 floatingPoint.matrix = false;
153 floatingPoint.array = false;
154
155 switch(shaderType)
156 {
157 case SH_FRAGMENT_SHADER:
158 symbolTable.setDefaultPrecision(integer, EbpMedium);
159 break;
160 case SH_VERTEX_SHADER:
161 symbolTable.setDefaultPrecision(integer, EbpHigh);
162 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
163 break;
164 default: assert(false && "Language not supported");
165 }
166
167 InsertBuiltInFunctions(shaderType, resources, symbolTable);
168
169 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
170
171 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400172}
173
174void TCompiler::clearResults()
175{
176 infoSink.info.erase();
177 infoSink.obj.erase();
178 infoSink.debug.erase();
John Bauman66b8ab22014-05-06 15:57:45 -0400179}
180
John Baumand4ae8632014-05-06 16:18:33 -0400181bool TCompiler::validateCallDepth(TIntermNode *root, TInfoSink &infoSink)
John Bauman66b8ab22014-05-06 15:57:45 -0400182{
John Baumand4ae8632014-05-06 16:18:33 -0400183 AnalyzeCallDepth validator(root);
184
185 unsigned int depth = validator.analyzeCallDepth();
186
187 if(depth == 0)
188 {
189 infoSink.info.prefix(EPrefixError);
190 infoSink.info << "Missing main()";
191 return false;
192 }
193 else if(depth == UINT_MAX)
194 {
195 infoSink.info.prefix(EPrefixError);
196 infoSink.info << "Function recursion detected";
197 return false;
198 }
199 else if(depth > maxCallStackDepth)
200 {
201 infoSink.info.prefix(EPrefixError);
202 infoSink.info << "Function call stack too deep";
203 return false;
204 }
205
206 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400207}
208
209bool TCompiler::validateLimitations(TIntermNode* root) {
210 ValidateLimitations validate(shaderType, infoSink.info);
211 root->traverse(&validate);
212 return validate.numErrors() == 0;
213}
214
John Bauman66b8ab22014-05-06 15:57:45 -0400215const TExtensionBehavior& TCompiler::getExtensionBehavior() const
216{
217 return extensionBehavior;
218}