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