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" |
| 11 | #include "ShHandle.h" |
| 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 | |
| 34 | TShHandleBase::TShHandleBase() { |
| 35 | allocator.push(); |
| 36 | SetGlobalPoolAllocator(&allocator); |
| 37 | } |
| 38 | |
| 39 | TShHandleBase::~TShHandleBase() { |
| 40 | SetGlobalPoolAllocator(NULL); |
| 41 | allocator.popAll(); |
| 42 | } |
| 43 | |
| 44 | TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) |
| 45 | : shaderType(type), |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 46 | shaderSpec(spec), |
| 47 | maxCallStackDepth(UINT_MAX) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | TCompiler::~TCompiler() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | bool TCompiler::Init(const ShBuiltInResources& resources) |
| 56 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 57 | maxCallStackDepth = resources.MaxCallStackDepth; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 58 | 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 | |
| 68 | bool 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 Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 95 | SetGlobalParseContext(&parseContext); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 96 | |
| 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 Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 112 | success = validateCallDepth(root, infoSink); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 113 | |
| 114 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 115 | success = validateLimitations(root); |
| 116 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 117 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
| 118 | intermediate.outputTree(root); |
| 119 | |
| 120 | if (success && (compileOptions & SH_OBJECT_CODE)) |
Nicolas Capens | 014b9a6 | 2014-10-15 10:28:29 -0400 | [diff] [blame] | 121 | success = translate(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 122 | } |
| 123 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 124 | // 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 Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 132 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 133 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 134 | 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 Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 142 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 143 | 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 Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void TCompiler::clearResults() |
| 175 | { |
| 176 | infoSink.info.erase(); |
| 177 | infoSink.obj.erase(); |
| 178 | infoSink.debug.erase(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 179 | } |
| 180 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 181 | bool TCompiler::validateCallDepth(TIntermNode *root, TInfoSink &infoSink) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 182 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 183 | 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 Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool TCompiler::validateLimitations(TIntermNode* root) { |
| 210 | ValidateLimitations validate(shaderType, infoSink.info); |
| 211 | root->traverse(&validate); |
| 212 | return validate.numErrors() == 0; |
| 213 | } |
| 214 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 215 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 216 | { |
| 217 | return extensionBehavior; |
| 218 | } |