1 // 2 // Copyright 2012 Francisco Jerez 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 // OTHER DEALINGS IN THE SOFTWARE. 21 // 22 23 #include <sstream> 24 25 #include "tgsi/invocation.hpp" 26 #include "core/error.hpp" 27 28 #include "tgsi/tgsi_parse.h" 29 #include "tgsi/tgsi_text.h" 30 #include "util/u_memory.h" 31 32 using namespace clover; 33 34 namespace { 35 void read_header(const std::string & header,module & m,std::string & r_log)36 read_header(const std::string &header, module &m, std::string &r_log) { 37 std::istringstream ls(header); 38 std::string line; 39 40 while (getline(ls, line)) { 41 std::istringstream ts(line); 42 std::string name, tok; 43 module::size_t offset; 44 std::vector<module::argument> args; 45 46 if (!(ts >> name)) 47 continue; 48 49 if (!(ts >> offset)) { 50 r_log = "invalid kernel start address"; 51 throw build_error(); 52 } 53 54 while (ts >> tok) { 55 if (tok == "scalar") 56 args.push_back({ module::argument::scalar, 4 }); 57 else if (tok == "global") 58 args.push_back({ module::argument::global, 4 }); 59 else if (tok == "local") 60 args.push_back({ module::argument::local, 4 }); 61 else if (tok == "constant") 62 args.push_back({ module::argument::constant, 4 }); 63 else if (tok == "image2d_rd") 64 args.push_back({ module::argument::image2d_rd, 4 }); 65 else if (tok == "image2d_wr") 66 args.push_back({ module::argument::image2d_wr, 4 }); 67 else if (tok == "image3d_rd") 68 args.push_back({ module::argument::image3d_rd, 4 }); 69 else if (tok == "image3d_wr") 70 args.push_back({ module::argument::image3d_wr, 4 }); 71 else if (tok == "sampler") 72 args.push_back({ module::argument::sampler, 0 }); 73 else { 74 r_log = "invalid kernel argument"; 75 throw build_error(); 76 } 77 } 78 79 m.syms.push_back({ name, 0, offset, args }); 80 } 81 } 82 83 void read_body(const char * source,module & m,std::string & r_log)84 read_body(const char *source, module &m, std::string &r_log) { 85 tgsi_token prog[1024]; 86 87 if (!tgsi_text_translate(source, prog, ARRAY_SIZE(prog))) { 88 r_log = "translate failed"; 89 throw build_error(); 90 } 91 92 unsigned sz = tgsi_num_tokens(prog) * sizeof(tgsi_token); 93 std::vector<char> data( (char *)prog, (char *)prog + sz ); 94 m.secs.push_back({ 0, module::section::text_executable, sz, data }); 95 } 96 } 97 98 module 99 clover::tgsi::compile_program(const std::string &source, std::string &r_log) { 100 const size_t body_pos = source.find("COMP\n"); 101 if (body_pos == std::string::npos) { 102 r_log = "invalid source"; 103 throw build_error(); 104 } 105 106 const char *body = &source[body_pos]; 107 module m; 108 109 read_header({ source.begin(), source.begin() + body_pos }, m, r_log); 110 read_body(body, m, r_log); 111 112 return m; 113 } 114 115 module 116 clover::tgsi::link_program(const std::vector<module> &modules) 117 { 118 assert(modules.size() == 1 && "Not implemented"); 119 return modules[0]; 120 } 121