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