alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 1 | // |
| 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.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 7 | #include "compiler/DetectRecursion.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 8 | #include "compiler/Initialize.h" |
| 9 | #include "compiler/ParseHelper.h" |
| 10 | #include "compiler/ShHandle.h" |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 11 | #include "compiler/ValidateLimitations.h" |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 12 | #include "compiler/MapLongVariableNames.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 13 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 14 | namespace { |
| 15 | bool InitializeSymbolTable( |
| 16 | const TBuiltInStrings& builtInStrings, |
| 17 | ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources, |
| 18 | TInfoSink& infoSink, TSymbolTable& symbolTable) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 19 | { |
| 20 | TIntermediate intermediate(infoSink); |
| 21 | TExtensionBehavior extBehavior; |
zmo@google.com | 09c323a | 2011-08-12 18:22:25 +0000 | [diff] [blame] | 22 | InitExtensionBehavior(resources, extBehavior); |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 23 | // The builtins deliberately don't specify precisions for the function |
| 24 | // arguments and return types. For that reason we don't try to check them. |
| 25 | TParseContext parseContext(symbolTable, extBehavior, intermediate, type, spec, 0, false, NULL, infoSink); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 26 | |
| 27 | GlobalParseContext = &parseContext; |
| 28 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 29 | assert(symbolTable.isEmpty()); |
| 30 | // |
| 31 | // Parse the built-ins. This should only happen once per |
| 32 | // language symbol table. |
| 33 | // |
| 34 | // Push the symbol table to give it an initial scope. This |
| 35 | // push should not have a corresponding pop, so that built-ins |
| 36 | // are preserved, and the test for an empty table fails. |
| 37 | // |
| 38 | symbolTable.push(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 39 | |
| 40 | for (TBuiltInStrings::const_iterator i = builtInStrings.begin(); i != builtInStrings.end(); ++i) |
| 41 | { |
alokp@chromium.org | 570bfc7 | 2010-09-24 17:19:25 +0000 | [diff] [blame] | 42 | const char* builtInShaders = i->c_str(); |
| 43 | int builtInLengths = static_cast<int>(i->size()); |
| 44 | if (builtInLengths <= 0) |
| 45 | continue; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 46 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 47 | if (PaParseStrings(1, &builtInShaders, &builtInLengths, &parseContext) != 0) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 48 | { |
| 49 | infoSink.info.message(EPrefixInternalError, "Unable to parse built-ins"); |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 54 | IdentifyBuiltIns(type, spec, resources, symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 55 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 59 | class TScopedPoolAllocator { |
| 60 | public: |
| 61 | TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop) |
| 62 | : mAllocator(allocator), mPushPopAllocator(pushPop) { |
| 63 | if (mPushPopAllocator) mAllocator->push(); |
| 64 | SetGlobalPoolAllocator(mAllocator); |
| 65 | } |
| 66 | ~TScopedPoolAllocator() { |
| 67 | SetGlobalPoolAllocator(NULL); |
| 68 | if (mPushPopAllocator) mAllocator->pop(); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | TPoolAllocator* mAllocator; |
| 73 | bool mPushPopAllocator; |
| 74 | }; |
| 75 | } // namespace |
| 76 | |
| 77 | TShHandleBase::TShHandleBase() { |
| 78 | allocator.push(); |
| 79 | SetGlobalPoolAllocator(&allocator); |
| 80 | } |
| 81 | |
| 82 | TShHandleBase::~TShHandleBase() { |
| 83 | SetGlobalPoolAllocator(NULL); |
| 84 | allocator.popAll(); |
| 85 | } |
| 86 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 87 | TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) |
| 88 | : shaderType(type), |
| 89 | shaderSpec(spec) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | TCompiler::~TCompiler() |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | bool TCompiler::Init(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 98 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 99 | TScopedPoolAllocator scopedAlloc(&allocator, false); |
| 100 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 101 | // Generate built-in symbol table. |
| 102 | if (!InitBuiltInSymbolTable(resources)) |
| 103 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 104 | InitExtensionBehavior(resources, extensionBehavior); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 105 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool TCompiler::compile(const char* const shaderStrings[], |
| 110 | const int numStrings, |
| 111 | int compileOptions) |
| 112 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 113 | TScopedPoolAllocator scopedAlloc(&allocator, true); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 114 | clearResults(); |
| 115 | |
| 116 | if (numStrings == 0) |
| 117 | return true; |
| 118 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 119 | // If compiling for WebGL, validate loop and indexing as well. |
| 120 | if (shaderSpec == SH_WEBGL_SPEC) |
| 121 | compileOptions |= SH_VALIDATE_LOOP_INDEXING; |
alokp@chromium.org | 1f29954 | 2010-11-12 15:50:23 +0000 | [diff] [blame] | 122 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 123 | // First string is path of source file if flag is set. The actual source follows. |
| 124 | const char* sourcePath = NULL; |
| 125 | int firstSource = 0; |
| 126 | if (compileOptions & SH_SOURCE_PATH) |
| 127 | { |
| 128 | sourcePath = shaderStrings[0]; |
| 129 | ++firstSource; |
| 130 | } |
| 131 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 132 | TIntermediate intermediate(infoSink); |
| 133 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 134 | shaderType, shaderSpec, compileOptions, true, |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 135 | sourcePath, infoSink); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 136 | GlobalParseContext = &parseContext; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 137 | |
| 138 | // We preserve symbols at the built-in level from compile-to-compile. |
| 139 | // Start pushing the user-defined symbols at global level. |
| 140 | symbolTable.push(); |
| 141 | if (!symbolTable.atGlobalLevel()) |
| 142 | infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); |
| 143 | |
| 144 | // Parse shader. |
| 145 | bool success = |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 146 | (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) && |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 147 | (parseContext.treeRoot != NULL); |
| 148 | if (success) { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 149 | TIntermNode* root = parseContext.treeRoot; |
| 150 | success = intermediate.postProcess(root); |
| 151 | |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 152 | if (success) |
| 153 | success = detectRecursion(root); |
| 154 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 155 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 156 | success = validateLimitations(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 157 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 158 | // Call mapLongVariableNames() before collectAttribsUniforms() so in |
| 159 | // collectAttribsUniforms() we already have the mapped symbol names and |
| 160 | // we could composite mapped and original variable names. |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 161 | if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES)) |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 162 | mapLongVariableNames(root); |
| 163 | |
| 164 | if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) |
| 165 | collectAttribsUniforms(root); |
| 166 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 167 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 168 | intermediate.outputTree(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 169 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 170 | if (success && (compileOptions & SH_OBJECT_CODE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 171 | translate(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Cleanup memory. |
| 175 | intermediate.remove(parseContext.treeRoot); |
| 176 | // Ensure symbol table is returned to the built-in level, |
| 177 | // throwing away all but the built-ins. |
| 178 | while (!symbolTable.atBuiltInLevel()) |
| 179 | symbolTable.pop(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 180 | |
| 181 | return success; |
| 182 | } |
| 183 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 184 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 185 | { |
| 186 | TBuiltIns builtIns; |
| 187 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 188 | builtIns.initialize(shaderType, shaderSpec, resources); |
| 189 | return InitializeSymbolTable(builtIns.getBuiltInStrings(), |
| 190 | shaderType, shaderSpec, resources, infoSink, symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void TCompiler::clearResults() |
| 194 | { |
| 195 | infoSink.info.erase(); |
| 196 | infoSink.obj.erase(); |
| 197 | infoSink.debug.erase(); |
| 198 | |
| 199 | attribs.clear(); |
| 200 | uniforms.clear(); |
| 201 | } |
| 202 | |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 203 | bool TCompiler::detectRecursion(TIntermNode* root) |
| 204 | { |
| 205 | DetectRecursion detect; |
| 206 | root->traverse(&detect); |
| 207 | switch (detect.detectRecursion()) { |
| 208 | case DetectRecursion::kErrorNone: |
| 209 | return true; |
| 210 | case DetectRecursion::kErrorMissingMain: |
| 211 | infoSink.info.message(EPrefixError, "Missing main()"); |
| 212 | return false; |
| 213 | case DetectRecursion::kErrorRecursion: |
| 214 | infoSink.info.message(EPrefixError, "Function recursion detected"); |
| 215 | return false; |
| 216 | default: |
| 217 | UNREACHABLE(); |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 222 | bool TCompiler::validateLimitations(TIntermNode* root) { |
| 223 | ValidateLimitations validate(shaderType, infoSink.info); |
| 224 | root->traverse(&validate); |
| 225 | return validate.numErrors() == 0; |
| 226 | } |
| 227 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 228 | void TCompiler::collectAttribsUniforms(TIntermNode* root) |
| 229 | { |
| 230 | CollectAttribsUniforms collect(attribs, uniforms); |
| 231 | root->traverse(&collect); |
| 232 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 233 | |
| 234 | void TCompiler::mapLongVariableNames(TIntermNode* root) |
| 235 | { |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 236 | MapLongVariableNames map(varyingLongNameMap); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 237 | root->traverse(&map); |
| 238 | } |
| 239 | |
| 240 | int TCompiler::getMappedNameMaxLength() const |
| 241 | { |
| 242 | return MAX_IDENTIFIER_NAME_SIZE + 1; |
| 243 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 244 | |
| 245 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 246 | { |
| 247 | return extensionBehavior; |
| 248 | } |