1 // Copyright 2017-2019 VMware, Inc.
2 // SPDX-License-Identifier: BSD-2-Clause
3 //
4 // The BSD-2 license (the License) set forth below applies to all parts of the
5 // Cascade project.  You may not use this file except in compliance with the
6 // License.
7 //
8 // BSD-2 License
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this
14 // list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include "target/state.h"
32 
33 using namespace std;
34 
35 namespace cascade {
36 
read(istream & is,size_t base)37 void State::read(istream& is, size_t base) {
38   state_.clear();
39 
40   size_t n = 0;
41   is >> n;
42 
43   for (size_t i = 0; i < n; ++i) {
44     VId id;
45     is >> id;
46     size_t arity = 0;
47     is >> arity;
48     size_t width = 0;
49     is >> width;
50     size_t type = 0;
51     is >> type;
52 
53     for (size_t j = 0; j < arity; ++j) {
54       Bits bits;
55       bits.read(is, base);
56       bits.resize(width);
57       bits.reinterpret_type(static_cast<Bits::Type>(type));
58       state_[id].push_back(bits);
59     }
60   }
61 }
62 
write(ostream & os,size_t base) const63 void State::write(ostream& os, size_t base) const {
64   os << state_.size() << endl;
65   for (const auto& s : state_) {
66     os << "  " << s.first << " " << s.second.size() << " " << s.second[0].size() << " " << static_cast<size_t>(s.second[0].get_type()) << endl;
67     for (const auto& b : s.second) {
68       os << "    ";
69       b.write(os, base);
70       os << endl;
71     }
72   }
73 }
74 
deserialize(istream & is)75 size_t State::deserialize(istream& is) {
76   state_.clear();
77 
78   // How many elements are in this state?
79   uint32_t n = 0;
80   is.read(reinterpret_cast<char*>(&n), 4);
81   size_t res = 4;
82 
83   // Read that many id / bit pairs
84   for (size_t i = 0; i < n; ++i) {
85     VId id;
86     is.read(reinterpret_cast<char*>(&id), 4);
87     res += 4;
88 
89     uint32_t arity = 0;
90     is.read(reinterpret_cast<char*>(&arity), 4);
91     res += 4;
92 
93     for (size_t j = 0; j < arity; ++j) {
94       Bits bits;
95       res += bits.deserialize(is);
96       state_[id].push_back(bits);
97     }
98   }
99   return res;
100 }
101 
serialize(ostream & os) const102 size_t State::serialize(ostream& os) const {
103   // How many elements are in this state?
104   uint32_t n = state_.size();
105   os.write(reinterpret_cast<char*>(&n), 4);
106   size_t res = 4;
107 
108   // Write that many id / bit pairs
109   for (const auto& s : state_) {
110     os.write(const_cast<char*>(reinterpret_cast<const char*>(&s.first)), 4);
111     res += 4;
112 
113     uint32_t arity = s.second.size();
114     os.write(reinterpret_cast<char*>(&arity), 4);
115     res += 4;
116 
117     for (const auto& b : s.second) {
118       res += b.serialize(os);
119     }
120   }
121   return res;
122 }
123 
124 } // namespace cascade
125 
126