Path: blob/21.2-virgl/src/gallium/frontends/clover/core/module.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include <type_traits>23#include <iostream>2425#include "core/module.hpp"2627using namespace clover;2829namespace {30template<typename T, typename = void>31struct _serializer;3233/// Serialize the specified object.34template<typename T>35void36_proc(std::ostream &os, const T &x) {37_serializer<T>::proc(os, x);38}3940/// Deserialize the specified object.41template<typename T>42void43_proc(std::istream &is, T &x) {44_serializer<T>::proc(is, x);45}4647template<typename T>48T49_proc(std::istream &is) {50T x;51_serializer<T>::proc(is, x);52return x;53}5455/// Calculate the size of the specified object.56template<typename T>57void58_proc(module::size_t &sz, const T &x) {59_serializer<T>::proc(sz, x);60}6162/// (De)serialize a scalar value.63template<typename T>64struct _serializer<T, typename std::enable_if<65std::is_scalar<T>::value>::type> {66static void67proc(std::ostream &os, const T &x) {68os.write(reinterpret_cast<const char *>(&x), sizeof(x));69}7071static void72proc(std::istream &is, T &x) {73is.read(reinterpret_cast<char *>(&x), sizeof(x));74}7576static void77proc(module::size_t &sz, const T &x) {78sz += sizeof(x);79}80};8182/// (De)serialize a vector.83template<typename T>84struct _serializer<std::vector<T>,85typename std::enable_if<86!std::is_scalar<T>::value>::type> {87static void88proc(std::ostream &os, const std::vector<T> &v) {89_proc<uint32_t>(os, v.size());9091for (size_t i = 0; i < v.size(); i++)92_proc<T>(os, v[i]);93}9495static void96proc(std::istream &is, std::vector<T> &v) {97v.resize(_proc<uint32_t>(is));9899for (size_t i = 0; i < v.size(); i++)100new(&v[i]) T(_proc<T>(is));101}102103static void104proc(module::size_t &sz, const std::vector<T> &v) {105sz += sizeof(uint32_t);106107for (size_t i = 0; i < v.size(); i++)108_proc<T>(sz, v[i]);109}110};111112template<typename T>113struct _serializer<std::vector<T>,114typename std::enable_if<115std::is_scalar<T>::value>::type> {116static void117proc(std::ostream &os, const std::vector<T> &v) {118_proc<uint32_t>(os, v.size());119os.write(reinterpret_cast<const char *>(&v[0]),120v.size() * sizeof(T));121}122123static void124proc(std::istream &is, std::vector<T> &v) {125v.resize(_proc<uint32_t>(is));126is.read(reinterpret_cast<char *>(&v[0]),127v.size() * sizeof(T));128}129130static void131proc(module::size_t &sz, const std::vector<T> &v) {132sz += sizeof(uint32_t) + sizeof(T) * v.size();133}134};135136/// (De)serialize a string.137template<>138struct _serializer<std::string> {139static void140proc(std::ostream &os, const std::string &s) {141_proc<uint32_t>(os, s.size());142os.write(&s[0], s.size() * sizeof(std::string::value_type));143}144145static void146proc(std::istream &is, std::string &s) {147s.resize(_proc<uint32_t>(is));148is.read(&s[0], s.size() * sizeof(std::string::value_type));149}150151static void152proc(module::size_t &sz, const std::string &s) {153sz += sizeof(uint32_t) + sizeof(std::string::value_type) * s.size();154}155};156157/// (De)serialize a printf format158template<>159struct _serializer<module::printf_info> {160template<typename S, typename QT>161static void162proc(S & s, QT &x) {163_proc(s, x.arg_sizes);164_proc(s, x.strings);165}166};167168/// (De)serialize a module::section.169template<>170struct _serializer<module::section> {171template<typename S, typename QT>172static void173proc(S &s, QT &x) {174_proc(s, x.id);175_proc(s, x.type);176_proc(s, x.size);177_proc(s, x.data);178}179};180181/// (De)serialize a module::argument.182template<>183struct _serializer<module::argument> {184template<typename S, typename QT>185static void186proc(S &s, QT &x) {187_proc(s, x.type);188_proc(s, x.size);189_proc(s, x.target_size);190_proc(s, x.target_align);191_proc(s, x.ext_type);192_proc(s, x.semantic);193}194};195196/// (De)serialize a module::symbol.197template<>198struct _serializer<module::symbol> {199template<typename S, typename QT>200static void201proc(S &s, QT &x) {202_proc(s, x.name);203_proc(s, x.attributes);204_proc(s, x.reqd_work_group_size);205_proc(s, x.section);206_proc(s, x.offset);207_proc(s, x.args);208}209};210211/// (De)serialize a module.212template<>213struct _serializer<module> {214template<typename S, typename QT>215static void216proc(S &s, QT &x) {217_proc(s, x.syms);218_proc(s, x.secs);219_proc(s, x.printf_infos);220_proc(s, x.printf_strings_in_buffer);221}222};223};224225namespace clover {226void227module::serialize(std::ostream &os) const {228_proc(os, *this);229}230231module232module::deserialize(std::istream &is) {233return _proc<module>(is);234}235236module::size_t237module::size() const {238size_t sz = 0;239_proc(sz, *this);240return sz;241}242}243244245