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