vm_defs.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include <library/cpp/bit_io/bitinput.h>
  3. #include <library/cpp/scheme/scheme.h>
  4. #include <util/generic/deque.h>
  5. #include <util/generic/maybe.h>
  6. #include <util/generic/variant.h>
  7. #include <util/string/builder.h>
  8. namespace NSc::NUt {
  9. enum EVMAction {
  10. VMA_JMP__POS /* "pos=POS" */,
  11. VMA_CREATE_BACK /* "TValue()->emplace_back" */,
  12. VMA_CREATE_BACK__SRC /* "TValue(SRC)->emplace_back" */,
  13. VMA_CREATE_FRONT /* "TValue()->emplace_front,pos+=1" */,
  14. VMA_CREATE_FRONT__SRC /* "TValue(SRC)->emplace_front,pos+=1" */,
  15. VMA_DESTROY_BACK /* "pop_back" */,
  16. VMA_DESTROY_FRONT /* "pop_front,pos-=1" */,
  17. VMA_ASSIGN__SRC /* "operator=(SRC)" */,
  18. VMA_SET_STRING__IDX /* "SetString(str[IDX])" */,
  19. VMA_SET_INT_NUMBER__IDX /* "SetIntNumber(IDX)" */,
  20. VMA_SET_DICT /* "SetDict()" */,
  21. VMA_SET_ARRAY /* "SetArray()" */,
  22. VMA_SET_NULL /* "SetNull()" */,
  23. VMA_GET_JSON /* "ToJson()" */,
  24. VMA_ARRAY_CLEAR /* "ClearArray()" */,
  25. VMA_ARRAY_PUSH /* "Push()" */,
  26. VMA_ARRAY_PUSH__SRC /* "Push()=SRC" */,
  27. VMA_ARRAY_PUSH__DST /* "Push()->DST" */,
  28. VMA_ARRAY_POP /* "Pop()" */,
  29. VMA_ARRAY_POP__REF /* "Pop()->REF" */,
  30. VMA_ARRAY_INSERT__IDX /* "Insert(IDX)" */,
  31. VMA_ARRAY_INSERT__IDX_SRC /* "Insert(IDX)=SRC" */,
  32. VMA_ARRAY_INSERT__IDX_DST /* "Insert(IDX)->DST" */,
  33. VMA_ARRAY_DELETE__IDX /* "Delete(IDX)" */,
  34. VMA_ARRAY_DELETE__IDX_REF /* "Delete(IDX)->REF" */,
  35. VMA_ARRAY_GET__IDX_REF /* "Get(IDX)->REF" */,
  36. VMA_ARRAY_GET_OR_ADD__IDX /* "GetOrAdd(IDX)" */,
  37. VMA_ARRAY_GET_OR_ADD__IDX_SRC /* "GetOrAdd(IDX)=SRC" */,
  38. VMA_ARRAY_GET_OR_ADD__IDX_DST /* "GetOrAdd(IDX)->DST" */,
  39. VMA_ARRAY_GET_NO_ADD__IDX_REF /* "GetNoAdd(IDX)->REF" */,
  40. VMA_DICT_CLEAR /* "ClearDict()" */,
  41. VMA_DICT_DELETE__IDX /* "Delete(str[IDX])" */,
  42. VMA_DICT_DELETE__IDX_REF /* "Delete(str[IDX])->REF" */,
  43. VMA_DICT_GET__IDX_REF /* "Get(str[IDX])->REF" */,
  44. VMA_DICT_GET_OR_ADD__IDX /* "GetOrAdd(str[IDX])" */,
  45. VMA_DICT_GET_OR_ADD__IDX_SRC /* "GetOrAdd(str[IDX])=SRC" */,
  46. VMA_DICT_GET_OR_ADD__IDX_DST /* "GetOrAdd(str[IDX])->DST" */,
  47. VMA_DICT_GET_NO_ADD__IDX_REF /* "GetNoAdd(str[IDX])->REF" */,
  48. VMA_MERGE_UPDATE__POS /* "MergeUpdate(POS)" */,
  49. VMA_MERGE_REVERSE__POS /* "ReverseMerge(POS)" */,
  50. VMA_MERGE_COPY_FROM__POS /* "CopyFrom(POS)" */,
  51. VMA_SWAP__POS /* "Swap(POS)" */,
  52. VMA_EQUAL__POS_POS /* "Equal(POS, POS)" */,
  53. VMA_SELECT_NO_ADD__PATH_REF /* "TrySelect(PATH)->REF" */,
  54. VMA_SELECT_OR_ADD__PATH_REF /* "TrySelectOrAdd(PATH)->REF" */,
  55. VMA_SELECT_AND_DELETE__PATH_REF /* "TrySelectAndDelete(PATH)->REF" */,
  56. VMA_COUNT,
  57. };
  58. struct TVMState {
  59. NBitIO::TBitInput Input;
  60. TDeque<TValue> Memory { 1 };
  61. ui32 Pos = 0;
  62. static const ui32 MaxMemory = 16;
  63. public:
  64. explicit TVMState(TStringBuf wire, ui32 memSz, ui32 pos);
  65. TValue& Current() {
  66. return Memory[Pos];
  67. }
  68. TValue& LRef(ui32 pos) {
  69. return Memory[pos];
  70. }
  71. const TValue& CRef(ui32 pos) {
  72. return Memory[pos];
  73. }
  74. TValue&& RRef(ui32 pos) {
  75. return std::move(Memory[pos]);
  76. }
  77. [[nodiscard]]
  78. bool TryPushBack();
  79. template <class T>
  80. [[nodiscard]]
  81. bool TryPushBack(T&& t) {
  82. if (MayAddMoreMemory()) {
  83. Memory.emplace_back(std::forward<T>(t));
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. [[nodiscard]]
  90. bool TryPushFront();
  91. template <class T>
  92. [[nodiscard]]
  93. bool TryPushFront(T&& t) {
  94. if (MayAddMoreMemory()) {
  95. Pos += 1;
  96. Memory.emplace_front(std::forward<T>(t));
  97. return true;
  98. } else {
  99. return false;
  100. }
  101. }
  102. [[nodiscard]]
  103. bool TryPopBack();
  104. [[nodiscard]]
  105. bool TryPopFront();
  106. TString ToString() const;
  107. private:
  108. [[nodiscard]]
  109. bool MayAddMoreMemory() const noexcept {
  110. return Memory.size() < MaxMemory;
  111. }
  112. };
  113. struct TIdx {
  114. ui32 Idx = -1;
  115. static const ui32 ValueCount = 4;
  116. public:
  117. TString ToString() const;
  118. };
  119. struct TPos {
  120. ui32 Pos = -1;
  121. static const ui32 ValueCount = TVMState::MaxMemory;
  122. public:
  123. TString ToString() const;
  124. };
  125. struct TRef : public TPos {
  126. enum EType {
  127. T_CREATE_FRONT /* "emplace_front(RES),pos+=1" */,
  128. T_CREATE_BACK /* "emplace_back(RES)" */,
  129. T_REF__POS /* "pos@(RES)" */,
  130. T_COUNT
  131. };
  132. EType Type = T_COUNT;
  133. static const ui32 TypeCount = T_COUNT;
  134. public:
  135. TString ToString() const;
  136. };
  137. struct TSrc : public TPos {
  138. enum EType {
  139. T_LREF__POS /* "pos@(TValue&)" */,
  140. T_CREF__POS /* "pos@(const TValue&)" */,
  141. T_RREF__POS /* "pos@(TValue&&)" */,
  142. T_COUNT
  143. };
  144. EType Type = T_COUNT;
  145. static const ui32 TypeCount = T_COUNT;
  146. public:
  147. TString ToString() const;
  148. };
  149. struct TDst : public TPos {
  150. enum EType {
  151. T_CREATE_FRONT_LREF /* "emplace_front(TValue&),pos+=1" */,
  152. T_CREATE_FRONT_CREF /* "emplace_front(const TValue&),pos+=1" */,
  153. T_CREATE_FRONT_RREF /* "emplace_front(TValue&&),pos+=1" */,
  154. T_CREATE_BACK_LREF /* "emplace_back(TValue&)" */,
  155. T_CREATE_BACK_CREF /* "emplace_back(TValue&),pos+=1" */,
  156. T_CREATE_BACK_RREF /* "emplace_back(TValue&),pos+=1" */,
  157. T_LREF__POS /* "pos@(TValue&)" */,
  158. T_CREF__POS /* "pos@(const TValue&)" */,
  159. T_RREF__POS /* "pos@(TValue&&)" */,
  160. T_COUNT
  161. };
  162. EType Type = T_COUNT;
  163. static const ui32 TypeCount = T_COUNT;
  164. public:
  165. TString ToString() const;
  166. };
  167. struct TPath {
  168. TString Path;
  169. static const ui32 MaxLength = 32;
  170. public:
  171. TString ToString() const;
  172. };
  173. using TArg = std::variant<std::monostate, TIdx, TPos, TRef, TSrc, TDst, TPath>;
  174. struct TVMAction {
  175. using EType = EVMAction;
  176. EVMAction Type = VMA_COUNT;
  177. static const ui32 TypeCount = VMA_COUNT;
  178. public:
  179. template <class T>
  180. bool SetArg(TMaybe<T> arg) {
  181. Y_ABORT_UNLESS(CurrArg < Y_ARRAY_SIZE(Arg));
  182. if (arg) {
  183. Arg[CurrArg++] = *arg;
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. public:
  190. TRef GetRef(ui32 arg) const noexcept;
  191. TSrc GetSrc(ui32 arg) const noexcept;
  192. TDst GetDst(ui32 arg) const noexcept;
  193. ui32 GetPos(ui32 arg) const noexcept;
  194. ui32 GetIdx(ui32 arg) const noexcept;
  195. TStringBuf GetKey(ui32 arg) const noexcept;
  196. TStringBuf GetString(ui32 arg) const noexcept;
  197. i64 GetIntNumber(ui32 arg) const noexcept;
  198. TStringBuf GetPath(ui32 arg) const noexcept;
  199. TString ToString() const;
  200. private:
  201. TArg Arg[2];
  202. ui32 CurrArg = 0;
  203. };
  204. [[nodiscard]]
  205. inline ui32 GetCountWidth(ui32 cnt) {
  206. return MostSignificantBit(cnt - 1) + 1;
  207. }
  208. }