aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/tests/fuzz_ops/lib/vm_defs.h
blob: 6bc74c96c6ce7271f48b0ba71dbf750605717849 (plain) (blame)
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
#pragma once

#include <library/cpp/bit_io/bitinput.h>
#include <library/cpp/scheme/scheme.h>

#include <util/generic/deque.h>
#include <util/generic/maybe.h>
#include <util/generic/variant.h>
#include <util/string/builder.h>

namespace NSc::NUt {

    enum EVMAction {
        VMA_JMP__POS                    /* "pos=POS" */,

        VMA_CREATE_BACK                 /* "TValue()->emplace_back" */,
        VMA_CREATE_BACK__SRC            /* "TValue(SRC)->emplace_back" */,
        VMA_CREATE_FRONT                /* "TValue()->emplace_front,pos+=1" */,
        VMA_CREATE_FRONT__SRC           /* "TValue(SRC)->emplace_front,pos+=1" */,

        VMA_DESTROY_BACK                /* "pop_back" */,
        VMA_DESTROY_FRONT               /* "pop_front,pos-=1" */,

        VMA_ASSIGN__SRC                 /* "operator=(SRC)" */,

        VMA_SET_STRING__IDX             /* "SetString(str[IDX])" */,
        VMA_SET_INT_NUMBER__IDX         /* "SetIntNumber(IDX)" */,
        VMA_SET_DICT                    /* "SetDict()" */,
        VMA_SET_ARRAY                   /* "SetArray()" */,
        VMA_SET_NULL                    /* "SetNull()" */,

        VMA_GET_JSON                    /* "ToJson()" */,

        VMA_ARRAY_CLEAR                 /* "ClearArray()" */,
        VMA_ARRAY_PUSH                  /* "Push()" */,
        VMA_ARRAY_PUSH__SRC             /* "Push()=SRC" */,
        VMA_ARRAY_PUSH__DST             /* "Push()->DST" */,
        VMA_ARRAY_POP                   /* "Pop()" */,
        VMA_ARRAY_POP__REF              /* "Pop()->REF" */,
        VMA_ARRAY_INSERT__IDX           /* "Insert(IDX)" */,
        VMA_ARRAY_INSERT__IDX_SRC       /* "Insert(IDX)=SRC" */,
        VMA_ARRAY_INSERT__IDX_DST       /* "Insert(IDX)->DST" */,
        VMA_ARRAY_DELETE__IDX           /* "Delete(IDX)" */,
        VMA_ARRAY_DELETE__IDX_REF       /* "Delete(IDX)->REF" */,
        VMA_ARRAY_GET__IDX_REF          /* "Get(IDX)->REF" */,
        VMA_ARRAY_GET_OR_ADD__IDX       /* "GetOrAdd(IDX)" */,
        VMA_ARRAY_GET_OR_ADD__IDX_SRC   /* "GetOrAdd(IDX)=SRC" */,
        VMA_ARRAY_GET_OR_ADD__IDX_DST   /* "GetOrAdd(IDX)->DST" */,
        VMA_ARRAY_GET_NO_ADD__IDX_REF   /* "GetNoAdd(IDX)->REF" */,

        VMA_DICT_CLEAR                  /* "ClearDict()" */,
        VMA_DICT_DELETE__IDX            /* "Delete(str[IDX])" */,
        VMA_DICT_DELETE__IDX_REF        /* "Delete(str[IDX])->REF" */,
        VMA_DICT_GET__IDX_REF           /* "Get(str[IDX])->REF" */,
        VMA_DICT_GET_OR_ADD__IDX        /* "GetOrAdd(str[IDX])" */,
        VMA_DICT_GET_OR_ADD__IDX_SRC    /* "GetOrAdd(str[IDX])=SRC" */,
        VMA_DICT_GET_OR_ADD__IDX_DST    /* "GetOrAdd(str[IDX])->DST" */,
        VMA_DICT_GET_NO_ADD__IDX_REF    /* "GetNoAdd(str[IDX])->REF" */,

        VMA_MERGE_UPDATE__POS           /* "MergeUpdate(POS)" */,
        VMA_MERGE_REVERSE__POS          /* "ReverseMerge(POS)" */,
        VMA_MERGE_COPY_FROM__POS        /* "CopyFrom(POS)" */,

        VMA_SWAP__POS                   /* "Swap(POS)" */,
        VMA_EQUAL__POS_POS              /* "Equal(POS, POS)" */,

        VMA_SELECT_NO_ADD__PATH_REF      /* "TrySelect(PATH)->REF" */,
        VMA_SELECT_OR_ADD__PATH_REF      /* "TrySelectOrAdd(PATH)->REF" */,
        VMA_SELECT_AND_DELETE__PATH_REF  /* "TrySelectAndDelete(PATH)->REF" */,

        VMA_COUNT,
    };


    struct TVMState {
        NBitIO::TBitInput Input;
        TDeque<TValue> Memory { 1 };
        ui32 Pos = 0;

        static const ui32 MaxMemory = 16;

    public:
        explicit TVMState(TStringBuf wire, ui32 memSz, ui32 pos);

        TValue& Current() {
            return Memory[Pos];
        }

        TValue& LRef(ui32 pos) {
            return Memory[pos];
        }

        const TValue& CRef(ui32 pos) {
            return Memory[pos];
        }

        TValue&& RRef(ui32 pos) {
            return std::move(Memory[pos]);
        }

        [[nodiscard]]
        bool TryPushBack();

        template <class T>
        [[nodiscard]]
        bool TryPushBack(T&& t) {
            if (MayAddMoreMemory()) {
                Memory.emplace_back(std::forward<T>(t));
                return true;
            } else {
                return false;
            }
        }

        [[nodiscard]]
        bool TryPushFront();

        template <class T>
        [[nodiscard]]
        bool TryPushFront(T&& t) {
            if (MayAddMoreMemory()) {
                Pos += 1;
                Memory.emplace_front(std::forward<T>(t));
                return true;
            } else {
                return false;
            }
        }

        [[nodiscard]]
        bool TryPopBack();

        [[nodiscard]]
        bool TryPopFront();

        TString ToString() const;

    private:
        [[nodiscard]]
        bool MayAddMoreMemory() const noexcept {
            return Memory.size() < MaxMemory;
        }
    };


    struct TIdx {
        ui32 Idx = -1;
        static const ui32 ValueCount = 4;

    public:
        TString ToString() const;
    };


    struct TPos {
        ui32 Pos = -1;
        static const ui32 ValueCount = TVMState::MaxMemory;

    public:
        TString ToString() const;
    };


    struct TRef : public TPos {
        enum EType {
            T_CREATE_FRONT  /* "emplace_front(RES),pos+=1" */,
            T_CREATE_BACK   /* "emplace_back(RES)" */,
            T_REF__POS      /* "pos@(RES)" */,
            T_COUNT
        };

        EType Type = T_COUNT;
        static const ui32 TypeCount = T_COUNT;

    public:
        TString ToString() const;
    };


    struct TSrc : public TPos {
        enum EType {
            T_LREF__POS     /* "pos@(TValue&)" */,
            T_CREF__POS     /* "pos@(const TValue&)" */,
            T_RREF__POS     /* "pos@(TValue&&)" */,
            T_COUNT
        };

        EType Type = T_COUNT;
        static const ui32 TypeCount = T_COUNT;

    public:
        TString ToString() const;
    };


    struct TDst : public TPos {
        enum EType {
            T_CREATE_FRONT_LREF     /* "emplace_front(TValue&),pos+=1" */,
            T_CREATE_FRONT_CREF     /* "emplace_front(const TValue&),pos+=1" */,
            T_CREATE_FRONT_RREF     /* "emplace_front(TValue&&),pos+=1" */,
            T_CREATE_BACK_LREF      /* "emplace_back(TValue&)" */,
            T_CREATE_BACK_CREF      /* "emplace_back(TValue&),pos+=1" */,
            T_CREATE_BACK_RREF      /* "emplace_back(TValue&),pos+=1" */,
            T_LREF__POS             /* "pos@(TValue&)" */,
            T_CREF__POS             /* "pos@(const TValue&)" */,
            T_RREF__POS             /* "pos@(TValue&&)" */,
            T_COUNT
        };

        EType Type = T_COUNT;
        static const ui32 TypeCount = T_COUNT;

    public:
        TString ToString() const;
    };


    struct TPath {
        TString Path;
        static const ui32 MaxLength = 32;

    public:
        TString ToString() const;
    };

    using TArg = std::variant<std::monostate, TIdx, TPos, TRef, TSrc, TDst, TPath>;

    struct TVMAction {
        using EType = EVMAction;
        EVMAction Type = VMA_COUNT;
        static const ui32 TypeCount = VMA_COUNT;

    public:
        template <class T>
        bool SetArg(TMaybe<T> arg) {
            Y_ABORT_UNLESS(CurrArg < Y_ARRAY_SIZE(Arg));
            if (arg) {
                Arg[CurrArg++] = *arg;
                return true;
            } else {
                return false;
            }
        }

    public:
        TRef GetRef(ui32 arg) const noexcept;

        TSrc GetSrc(ui32 arg) const noexcept;

        TDst GetDst(ui32 arg) const noexcept;


        ui32 GetPos(ui32 arg) const noexcept;

        ui32 GetIdx(ui32 arg) const noexcept;

        TStringBuf GetKey(ui32 arg) const noexcept;

        TStringBuf GetString(ui32 arg) const noexcept;

        i64 GetIntNumber(ui32 arg) const noexcept;

        TStringBuf GetPath(ui32 arg) const noexcept;

        TString ToString() const;

    private:
        TArg Arg[2];
        ui32 CurrArg = 0;
    };

    [[nodiscard]]
    inline ui32 GetCountWidth(ui32 cnt) {
        return MostSignificantBit(cnt - 1) + 1;
    }

}