summaryrefslogtreecommitdiff
path: root/src/hir/serialise_lowlevel.hpp
blob: b3633d54ce3391a08c559376c60aee3f76c3e888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * hir/serialise_lowlevel.hpp
 * - HIR (De)Serialisation low-level "protocol"
 */
#pragma once

#include <vector>
#include <string>
#include <map>
#include <stddef.h>
#include <assert.h>
#include <rc_string.hpp>

namespace HIR {
namespace serialise {

class WriterInner;
class ReaderInner;

class Writer
{
    WriterInner*    m_inner;
    ::std::map<RcString, unsigned>  m_istring_cache;
public:
    Writer();
    Writer(const Writer&) = delete;
    Writer(Writer&&) = delete;
    ~Writer();

    void open(const ::std::string& filename);
    void write(const void* data, size_t count);

    void write_u8(uint8_t v) {
        write(reinterpret_cast<const char*>(&v), 1);
    }
    void write_u16(uint16_t v) {
        uint8_t buf[] = { static_cast<uint8_t>(v & 0xFF), static_cast<uint8_t>(v >> 8) };
        this->write(buf, 2);
    }
    void write_u32(uint32_t v) {
        uint8_t buf[] = {
            static_cast<uint8_t>(v & 0xFF), static_cast<uint8_t>(v >> 8),
            static_cast<uint8_t>(v >> 16), static_cast<uint8_t>(v >> 24) };
        this->write(buf, 4);
    }
    void write_u64(uint64_t v) {
        uint8_t buf[] = {
            static_cast<uint8_t>(v & 0xFF), static_cast<uint8_t>(v >> 8), static_cast<uint8_t>(v >> 16), static_cast<uint8_t>(v >> 24),
            static_cast<uint8_t>(v >> 32), static_cast<uint8_t>(v >> 40), static_cast<uint8_t>(v >> 48), static_cast<uint8_t>(v >> 56)
            };
        this->write(buf, 8);
    }
    void write_i64(int64_t v) {
        write_u64(static_cast<uint64_t>(v));
    }
    // Variable-length encoded u64 (for array sizes)
    void write_u64c(uint64_t v) {
        if( v < (1<<7) ) {
            write_u8(static_cast<uint8_t>(v));
        }
        else if( v < (1<<(6+16)) ) {
            uint8_t buf[] = {
                static_cast<uint8_t>(0x80 + (v >> 16)),   // 0x80 -- 0xBF
                static_cast<uint8_t>(v >> 8),
                static_cast<uint8_t>(v & 0xFF)
                };
            this->write(buf, sizeof buf);
        }
        else if( v < (1ull << (5 + 32)) ) {
            uint8_t buf[] = {
                static_cast<uint8_t>(0xC0 + (v >> 32)), // 0xC0 -- 0xDF
                static_cast<uint8_t>(v >> 24),
                static_cast<uint8_t>(v >> 16),
                static_cast<uint8_t>(v >> 8),
                static_cast<uint8_t>(v)
                };
            this->write(buf, sizeof buf);
        }
        else {
            uint8_t buf[] = {
                0xFF,
                static_cast<uint8_t>(v & 0xFF), static_cast<uint8_t>(v >> 8), static_cast<uint8_t>(v >> 16), static_cast<uint8_t>(v >> 24),
                static_cast<uint8_t>(v >> 32), static_cast<uint8_t>(v >> 40), static_cast<uint8_t>(v >> 48), static_cast<uint8_t>(v >> 56)
                };
            this->write(buf, sizeof buf);
        }
    }
    void write_i64c(int64_t v) {
        // Convert from 2's completement
        bool sign = (v < 0);
        uint64_t va = (v < 0 ? -v : v);
        va <<= 1;
        va |= (sign ? 1 : 0);
        write_u64c(va);
    }
    void write_double(double v) {
        // - Just raw-writes the double
        this->write(&v, sizeof v);
    }
    void write_tag(unsigned int t) {
        assert(t < 256);
        write_u8( static_cast<uint8_t>(t) );
    }
    void write_count(size_t c) {
        //DEBUG("c = " << c);
        if(c < 0xFE) {
            write_u8( static_cast<uint8_t>(c) );
        }
        else if( c == ~0u ) {
            write_u8( 0xFF );
        }
        else {
            assert(c < (1u<<16));
            write_u8( 0xFE );
            write_u16( static_cast<uint16_t>(c) );
        }
    }
    void write_string(const RcString& v);
    void write_string(size_t len, const char* s) {
        if(len < 128) {
            write_u8( static_cast<uint8_t>(len) );
        }
        else {
            assert(len < (1u<<(16+7)));
            write_u8( static_cast<uint8_t>(128 + (len >> 16)) );
            write_u16( static_cast<uint16_t>(len & 0xFFFF) );
        }
        this->write(s, len);
    }
    void write_string(const ::std::string& v) {
        write_string(v.size(), v.c_str());
    }
    void write_bool(bool v) {
        write_u8(v ? 0xFF : 0x00);
    }
};


class ReadBuffer
{
    ::std::vector<uint8_t>  m_backing;
    unsigned int    m_ofs;
public:
    ReadBuffer(size_t size);

    size_t capacity() const { return m_backing.capacity(); }
    size_t read(void* dst, size_t len);
    void populate(ReaderInner& is);
};

class Reader
{
    ReaderInner*    m_inner;
    ReadBuffer  m_buffer;
    size_t  m_pos;
    ::std::vector<RcString> m_strings;
public:
    Reader(const ::std::string& path);
    Reader(const Writer&) = delete;
    Reader(Writer&&) = delete;
    ~Reader();

    size_t get_pos() const { return m_pos; }
    void read(void* dst, size_t count);

    uint8_t read_u8() {
        uint8_t v;
        read(&v, sizeof v);
        return v;
    }
    uint16_t read_u16() {
        uint8_t buf[2];
        read(buf, sizeof buf);
        return static_cast<uint16_t>(buf[0]) | (static_cast<uint16_t>(buf[1]) << 8);
    }
    uint32_t read_u32() {
        uint8_t buf[4];
        read(buf, sizeof buf);
        return static_cast<uint32_t>(buf[0])
            | (static_cast<uint32_t>(buf[1]) << 8)
            | (static_cast<uint32_t>(buf[2]) << 16)
            | (static_cast<uint32_t>(buf[3]) << 24)
            ;
    }
    uint64_t read_u64() {
        uint8_t buf[8];
        read(buf, sizeof buf);
        return static_cast<uint64_t>(buf[0])
            | (static_cast<uint64_t>(buf[1]) << 8)
            | (static_cast<uint64_t>(buf[2]) << 16)
            | (static_cast<uint64_t>(buf[3]) << 24)
            | (static_cast<uint64_t>(buf[4]) << 32)
            | (static_cast<uint64_t>(buf[5]) << 40)
            | (static_cast<uint64_t>(buf[6]) << 48)
            | (static_cast<uint64_t>(buf[7]) << 56)
            ;
    }
    int64_t read_i64() {
        return static_cast<int64_t>(read_u64());
    }
    // Variable-length encoded u64 (for array sizes)
    uint64_t read_u64c() {
        auto v = read_u8();
        if( v < (1<<7) ) {
            return static_cast<uint64_t>(v);
        }
        else if( v < 0xC0 ) {
            uint64_t    rv = static_cast<uint64_t>(v & 0x3F) << 16;
            rv |= static_cast<uint64_t>(read_u8()) << 8;
            rv |= static_cast<uint64_t>(read_u8());
            return rv;
        }
        else if( v < 0xFF ) {
            uint64_t    rv = static_cast<uint64_t>(v & 0x3F) << 32;
            rv |= static_cast<uint64_t>(read_u8()) << 24;
            rv |= static_cast<uint64_t>(read_u8()) << 16;
            rv |= static_cast<uint64_t>(read_u8()) << 8;
            rv |= static_cast<uint64_t>(read_u8());
            return rv;
        }
        else {
            return read_u64();
        }
    }
    int64_t read_i64c() {
        uint64_t va = read_u64c();
        bool sign = (va & 0x1) != 0;
        va >>= 1;

        if( va == 0 && sign ) {
            return INT64_MIN;
        }
        else if( sign ) {
            return -static_cast<int64_t>(va);
        }
        else {
            return static_cast<int64_t>(va);
        }
    }
    double read_double() {
        double v;
        read(reinterpret_cast<char*>(&v), sizeof v);
        return v;
    }
    unsigned int read_tag() {
        return static_cast<unsigned int>( read_u8() );
    }
    size_t read_count() {
        auto v = read_u8();
        if( v < 0xFE ) {
            return v;
        }
        else if( v == 0xFE ) {
            return read_u16( );
        }
        else /*if( v == 0xFF )*/ {
            return ~0u;
        }
    }
    RcString read_istring() {
        size_t idx = read_count();
        return m_strings.at(idx);
    }
    ::std::string read_string() {
        size_t len = read_u8();
        if( len < 128 ) {
        }
        else {
            len = (len & 0x7F) << 16;
            len |= read_u16();
        }
        ::std::string   rv(len, '\0');
        read( const_cast<char*>(rv.data()), len);
        return rv;
    }
    bool read_bool() {
        return read_u8() != 0x00;
    }
};

}   // namespace serialise
}   // namespace HIR