Home | History | Annotate | Download | only in compiler
      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 // UnfoldSelect is an AST traverser to output the select operator ?: as if-else statements
      7 //
      8 
      9 #include "compiler/UnfoldSelect.h"
     10 
     11 #include "compiler/InfoSink.h"
     12 #include "compiler/OutputHLSL.h"
     13 
     14 namespace sh
     15 {
     16 UnfoldSelect::UnfoldSelect(TParseContext &context, OutputHLSL *outputHLSL) : mContext(context), mOutputHLSL(outputHLSL)
     17 {
     18     mTemporaryIndex = 0;
     19 }
     20 
     21 void UnfoldSelect::traverse(TIntermNode *node)
     22 {
     23     mTemporaryIndex++;
     24     node->traverse(this);
     25 }
     26 
     27 bool UnfoldSelect::visitSelection(Visit visit, TIntermSelection *node)
     28 {
     29     TInfoSinkBase &out = mOutputHLSL->getBodyStream();
     30 
     31     if (node->usesTernaryOperator())
     32     {
     33         int i = mTemporaryIndex++;
     34 
     35         out << mOutputHLSL->typeString(node->getType()) << " t" << i << ";\n";
     36 
     37         node->getCondition()->traverse(this);
     38         out << "if(";
     39         node->getCondition()->traverse(mOutputHLSL);
     40         out << ")\n"
     41                "{\n";
     42         node->getTrueBlock()->traverse(this);
     43         out << "    t" << i << " = ";
     44         node->getTrueBlock()->traverse(mOutputHLSL);
     45         out << ";\n"
     46                "}\n"
     47                "else\n"
     48                "{\n";
     49         node->getFalseBlock()->traverse(this);
     50         out << "    t" << i << " = ";
     51         node->getFalseBlock()->traverse(mOutputHLSL);
     52         out << ";\n"
     53                "}\n";
     54 
     55         mTemporaryIndex--;
     56     }
     57 
     58     return false;
     59 }
     60 
     61 int UnfoldSelect::getTemporaryIndex()
     62 {
     63     return mTemporaryIndex;
     64 }
     65 }
     66