aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/tests/fuzz_ops/lib/vm_defs.cpp
blob: 2a06e29f2274d502de1c4ade9f40fa2e21cf4e1b (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
#include "vm_defs.h"

#include <util/generic/xrange.h>
#include <util/string/builder.h>

namespace NSc::NUt {
    namespace {
        TStringBuf GetStringByIdx(ui32 idx) {
            static const TStringBuf strings[TIdx::ValueCount] {{}, "a", "aa", "aaa"};
            return strings[idx % TIdx::ValueCount];
        }
    }


    TVMState::TVMState(TStringBuf wire, ui32 memSz, ui32 pos)
        : Input(wire)
        , Memory(memSz)
        , Pos(pos)
    {}

    bool TVMState::TryPushBack() {
        if (MayAddMoreMemory()) {
            Memory.emplace_back();
            return true;
        } else {
            return false;
        }
    }

    bool TVMState::TryPushFront() {
        if (MayAddMoreMemory()) {
            Pos += 1;
            Memory.emplace_front();
            return true;
        } else {
            return false;
        }
    }

    bool TVMState::TryPopBack() {
        if (Memory.size() > 1 && Pos < Memory.size() - 1) {
            Memory.pop_back();
            return true;
        } else {
            return false;
        }
    }

    bool TVMState::TryPopFront() {
        if (Memory.size() > 1 && Pos > 0) {
            Memory.pop_front();
            Pos -= 1;
            return true;
        } else {
            return false;
        }
    }

    TString TVMState::ToString() const {
        TStringBuilder b;
        b << "pos=" << Pos << ";";
        for (const auto i : xrange(Memory.size())) {
            b << " " << i << (i == Pos ? "@" : ".") << Memory[i].ToJson().Quote();
        }

        return b;
    }

    TString TIdx::ToString() const {
        return TStringBuilder() << "IDX:" << Idx;
    }

    TString TPos::ToString() const {
        return TStringBuilder() << "POS:" << Pos;
    }

    template <class T>
    TString RenderToString(TStringBuf name, T type, ui32 pos) {
        TStringBuilder b;
        b << name << ":" << type;
        if ((ui32)-1 != pos) {
            b << "," << pos;
        }
        return b;
    }

    TString TRef::ToString() const {
        return RenderToString("REF", Type, Pos);
    }

    TString TSrc::ToString() const {
        return RenderToString("SRC", Type, Pos);
    }

    TString TDst::ToString() const {
        return RenderToString("DST", Type, Pos);
    }

    TString TPath::ToString() const {
        return TStringBuilder() << "PATH:" << Path.Quote();
    }


    TRef TVMAction::GetRef(ui32 arg) const noexcept {
        return std::get<TRef>(Arg[arg]); 
    }

    TSrc TVMAction::GetSrc(ui32 arg) const noexcept {
        return std::get<TSrc>(Arg[arg]); 
    }

    TDst TVMAction::GetDst(ui32 arg) const noexcept {
        return std::get<TDst>(Arg[arg]); 
    }

    ui32 TVMAction::GetPos(ui32 arg) const noexcept {
        return std::get<TPos>(Arg[arg]).Pos; 
    }

    ui32 TVMAction::GetIdx(ui32 arg) const noexcept {
        return std::get<TIdx>(Arg[arg]).Idx; 
    }

    TStringBuf TVMAction::GetKey(ui32 arg) const noexcept {
        return GetString(arg);
    }

    TStringBuf TVMAction::GetString(ui32 arg) const noexcept {
        return GetStringByIdx(GetIdx(arg));
    }

    i64 TVMAction::GetIntNumber(ui32 arg) const noexcept {
        return GetIdx(arg);
    }

    TStringBuf TVMAction::GetPath(ui32 arg) const noexcept {
        return std::get<TPath>(Arg[arg]).Path; 
    }


    struct TActionPrinter {
        TActionPrinter(IOutputStream& out)
            : Out(out)
        {}

        bool operator()(const std::monostate&) const { 
            return true;
        }
        //TIdx, TPos, TRef, TSrc, TDst, TPath
        template <class T>
        bool operator()(const T& t) const {
            Out << "; " << t.ToString();
            return false;
        }
        IOutputStream& Out;
    };

    TString TVMAction::ToString() const {
        TStringBuilder out;
        out << Type;
        for (const auto& arg : Arg) {
            if (std::visit(TActionPrinter(out.Out), arg)) { 
                break;
            }
        }
        return out;
    }
}