From e40d323fcef788123390038bce10a9845014e8ed Mon Sep 17 00:00:00 2001 From: Andrey Tvorozhkov Date: Wed, 14 Sep 2022 12:36:54 +0300 Subject: [PATCH] Add ~strdump and STRDUMP TVM OP (#452) * Add ~strdump and STRDUMP TVM OP Add ~strdump and STRDUMP TVM OP Add ~strdump and STRDUMP TVM OP * STRDUMP fixes Co-authored-by: Andrey Tvorozhkov --- crypto/func/builtins.cpp | 2 ++ crypto/vm/debugops.cpp | 41 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crypto/func/builtins.cpp b/crypto/func/builtins.cpp index 075d8f62..9a3c27e5 100644 --- a/crypto/func/builtins.cpp +++ b/crypto/func/builtins.cpp @@ -1063,6 +1063,8 @@ void define_builtins() { AsmOp::Nop()); define_builtin_func("~dump", TypeExpr::new_forall({X}, TypeExpr::new_map(X, TypeExpr::new_tensor({X, Unit}))), AsmOp::Custom("s0 DUMP", 1, 1), true); + define_builtin_func("~strdump", TypeExpr::new_forall({X}, TypeExpr::new_map(X, TypeExpr::new_tensor({X, Unit}))), + AsmOp::Custom("STRDUMP", 1, 1), true); define_builtin_func("run_method0", TypeExpr::new_map(Int, Unit), [](auto a, auto b, auto c) { return compile_run_method(a, b, c, 0, false); }, true); define_builtin_func("run_method1", TypeExpr::new_forall({X}, TypeExpr::new_map(TypeExpr::new_tensor({Int, X}), Unit)), diff --git a/crypto/vm/debugops.cpp b/crypto/vm/debugops.cpp index 0ec205a3..3f27de23 100644 --- a/crypto/vm/debugops.cpp +++ b/crypto/vm/debugops.cpp @@ -105,6 +105,43 @@ int exec_dump_value(VmState* st, unsigned arg) { return 0; } +int exec_dump_string(VmState* st) { + VM_LOG(st) << "execute STRDUMP"; + if (!vm_debug_enabled) { + return 0; + } + + Stack& stack = st->get_stack(); + + if (stack.depth() > 0){ + auto cs = stack[0].as_slice(); + + if (cs.not_null()) { // wanted t_slice + auto size = cs->size(); + + if (size % 8 == 0) { + auto cnt = size / 8; + + unsigned char tmp[128]; + cs.write().fetch_bytes(tmp, cnt); + std::string s{tmp, tmp + cnt}; + + std::cerr << "#DEBUG#: " << s << std::endl; + } + else { + std::cerr << "#DEBUG#: slice contains not valid bits count" << std::endl; + } + + } else { + std::cerr << "#DEBUG#: is not a slice" << std::endl; + } + } else { + std::cerr << "#DEBUG#: s0 is absent" << std::endl; + } + + return 0; +} + void register_debug_ops(OpcodeTable& cp0) { using namespace std::placeholders; if (!vm_debug_enabled) { @@ -113,7 +150,9 @@ void register_debug_ops(OpcodeTable& cp0) { } else { // NB: all non-redefined opcodes in fe00..feff should be redirected to dummy debug definitions cp0.insert(OpcodeInstr::mksimple(0xfe00, 16, "DUMPSTK", exec_dump_stack)) - .insert(OpcodeInstr::mkfixedrange(0xfe01, 0xfe20, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) + .insert(OpcodeInstr::mkfixedrange(0xfe01, 0xfe14, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) + .insert(OpcodeInstr::mksimple(0xfe14, 16,"STRDUMP", exec_dump_string)) + .insert(OpcodeInstr::mkfixedrange(0xfe15, 0xfe20, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) .insert(OpcodeInstr::mkfixed(0xfe2, 12, 4, instr::dump_1sr("DUMP"), exec_dump_value)) .insert(OpcodeInstr::mkfixedrange(0xfe30, 0xfef0, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) .insert(OpcodeInstr::mkext(0xfef, 12, 4, dump_dummy_debug_str, exec_dummy_debug_str, compute_len_debug_str));