Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShSyntax.cpp

00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 00027 #include <iostream> 00028 #include <fstream> 00029 #include <cassert> 00030 #include "ShSyntax.hpp" 00031 #include "ShEnvironment.hpp" 00032 #include "ShContext.hpp" 00033 #include "ShTokenizer.hpp" 00034 #include "ShToken.hpp" 00035 #include "ShProgram.hpp" 00036 #include "ShBackend.hpp" 00037 #include "ShOptimizations.hpp" 00038 00039 namespace SH { 00040 00041 ShProgram shBeginShader(const std::string& target) 00042 { 00043 ShProgram prg(target); 00044 ShContext::current()->enter(prg.node()); 00045 return prg; 00046 } 00047 00048 void shEndShader() 00049 { 00050 ShContext* context = ShContext::current(); 00051 00052 ShProgramNodePtr parsing = context->parsing(); 00053 assert(parsing); 00054 00055 parsing->ctrlGraph = new ShCtrlGraph(parsing->tokenizer.blockList()); 00056 00057 optimize(parsing); 00058 00059 parsing->collectVariables(); 00060 00061 context->exit(); 00062 00063 parsing->finish(); 00064 00065 // TODO. See issue129. 00066 // if (!ShEnvironment::shader->target().empty()) { 00067 // shCompile(ShEnvironment::shader); 00068 // } 00069 } 00070 00071 void shCompile(ShProgram& prg) 00072 { 00073 if (!ShEnvironment::backend) return; 00074 prg.compile(ShEnvironment::backend); 00075 } 00076 00077 void shCompile(ShProgram& prg, const std::string& target) 00078 { 00079 if (!ShEnvironment::backend) return; 00080 prg.compile(target, ShEnvironment::backend); 00081 } 00082 00083 void shCompileShader(ShProgram& prg) 00084 { 00085 shCompile(prg); 00086 } 00087 00088 void shCompileShader(const std::string& target, ShProgram& prg) 00089 { 00090 shCompile(prg, target); 00091 } 00092 00093 void shBind(ShProgram& prg) 00094 { 00095 if (!ShEnvironment::backend) return; 00096 prg.code(ShEnvironment::backend)->bind(); 00097 } 00098 00099 void shBind(ShProgram& prg, const std::string& target) 00100 { 00101 if (!ShEnvironment::backend) return; 00102 prg.code(target, ShEnvironment::backend)->bind(); 00103 } 00104 00105 void shBindShader(ShProgram& shader) 00106 { 00107 shBind(shader); 00108 } 00109 00110 void shBindShader(const std::string& target, ShProgram& shader) 00111 { 00112 shBind(shader, target); 00113 } 00114 00115 bool shSetBackend(const std::string& name) 00116 { 00117 ShBackendPtr backend = SH::ShBackend::lookup(name); 00118 if (!backend) return false; 00119 SH::ShEnvironment::backend = backend; 00120 return true; 00121 } 00122 00123 void shInit() 00124 { 00125 // TODO: Initialize backends 00126 } 00127 00128 void shIf(bool) 00129 { 00130 ShPointer<ShToken> token = new ShToken(SH_TOKEN_IF); 00131 00132 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument()); 00133 ShContext::current()->parsing()->tokenizer.popArgQueue(); 00134 00135 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token); 00136 } 00137 00138 void shEndIf() 00139 { 00140 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_ENDIF)); 00141 } 00142 00143 void shElse() 00144 { 00145 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_ELSE)); 00146 } 00147 00148 void shWhile(bool) 00149 { 00150 ShPointer<ShToken> token = new ShToken(SH_TOKEN_WHILE); 00151 00152 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument()); 00153 ShContext::current()->parsing()->tokenizer.popArgQueue(); 00154 00155 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token); 00156 } 00157 00158 void shEndWhile() 00159 { 00160 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_ENDWHILE)); 00161 } 00162 00163 void shDo() 00164 { 00165 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_DO)); 00166 } 00167 00168 void shUntil(bool) 00169 { 00170 ShPointer<ShToken> token = new ShToken(SH_TOKEN_UNTIL); 00171 00172 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument()); 00173 ShContext::current()->parsing()->tokenizer.popArgQueue(); 00174 00175 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token); 00176 } 00177 00178 void shFor(bool) 00179 { 00180 ShPointer<ShToken> token = new ShToken(SH_TOKEN_FOR); 00181 00182 for (int i = 0; i < 3; i++) { 00183 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument()); 00184 } 00185 ShContext::current()->parsing()->tokenizer.popArgQueue(); 00186 00187 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token); 00188 } 00189 00190 void shEndFor() 00191 { 00192 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_ENDFOR)); 00193 } 00194 00195 void ShBreak() 00196 { 00197 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_BREAK)); 00198 } 00199 00200 void ShContinue() 00201 { 00202 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(new ShToken(SH_TOKEN_CONTINUE)); 00203 } 00204 00205 }

Generated on Mon Oct 18 14:17:40 2004 for Sh by doxygen 1.3.7