mirror of
				https://github.com/ton-blockchain/ton
				synced 2025-03-09 15:40:10 +00:00 
			
		
		
		
	* New TVM instructions * Remove PREVBLOCKS * Separate target ton_crypto into TVM-related and -unrelared code * Add fine for failed "send message"; rework SENDMSG * Fix include * Fix bugs, improve action fines * Disable fines for special accounts * Handle msg_balance_remaining.grams == null in transaction.cpp * Bugfixes in SENDMSG * Fix fee calculation in SENDMSG * Fix CellStorageStat and transaction.cpp after merge * SETBOUNCEONACTIONPHASEFAIL instruction * ADDDIVMOD instructions * RUNVM, RUNVMX instructions * Changes in RUNVM * Tests for adddiv and runvm * HASHEXT instruction * Improve opcode-timing More iterations Don't measure preliminary run Remove logs and other excessive operations Add "error" to output * Increase RUNVM gas price * Optimize HASHEXT, adjust gas price * Add "bounce of action fail" flag to actions * Stack operations with unlimited arguments * Ristretto255 instructions * Adjust gas consumption * Optional fixed number of return values in RUNVM, fix exception handling * Adjust gas consumption * Simplify gas consumption logic * Support of secp256k1 and sodium libraries in builds (#11) * add support of secp256k1 library to the builds (linux, win) * add support of secp256k1 library to the builds (linux, win) * install secp256k1 via brew * install libsodium via brew; change sodium to upper case in FindSodium.cmake * install libsodium via brew; change sodium to upper case in FindSodium.cmake * simplify FindSodium.cmake * bug fixing * bug fixing * bug fixing * add macro SODIUM_STATIC * adjust build command for windows * put back original FindSodium.cmake * put back original FindSodium.cmake * fix sodium unzipped path for windows; add ninja * fix sodium unzipped path for windows; add ninja * fix sodium unzipped path for windows; add ninja * Win32 github build for secp256k1 * x64 architecture github build for secp256k1 * fix sodium linking on linux * enable docker buildx arm64 builds from forked repos * enable docker buildx arm64 builds from forked repos * enable docker buildx arm64 builds from forked repos * adjust mac builds for secp2561k and sodium * fix tonlib jni generation * minor fix * sync fixes across platforms * add libsodium build script for android and precompiled static libraries * build tonlib for android (fails) * FindSodium uppercase * remove system libsodium for android, use precompiled instead; specify SECP256K1_INCLUDE_DIR fir mac 12.6 * uppercase sodium * simplify FindSodium * fix windows build sodium path; use ninja for windows * simplify sodium 2 * adjust windows sodium paths; add paths to android jni * add ninja build windows * add ninja build windows * add ninja build windows 2 * remove win ninja * fix 1 * fix 2 * fix win 3 * fix linux compile 3 * fix jni 1 * fix jni 2 and mac * fix jni 3 * fix jni 4 * fix jni 5 * fix mac 6 * fix mac 7 and jni paths * fix jni 8 * rework sodium for android * rework sodium for android * rework sodium for android 2 * fixed sodium for android 2 * fixed sodium for android 3 * static secp256k1 for android * add precompiled arm secp256k1 * add precompiled arm secp256k1 * build native-lib with secp256k1 x86-64 (non arm) * update precompiled with NDK libsecp256k1.a * update precompiled with NDK libsecp256k1.a * update precompiled with NDK libsecp256k1.a * refactor llvm-strip location * refactor llvm-strip location * add native-lib.so for armv7a, armv8a * add native-lib.so for armv7a, armv8a * test armv7a, armv8a * armv7a - fails linking on sodium, test -> armv8a * works x86-64, armv7a - fails linking on sodium, armv8a - fails linking secp256k1 (incompatible with aarch64linux) * update libpsec256k1, sodium static libs * test x86 android native-lib * test armv7 android native-lib * test armv8 android native-lib * x86_64 and arm64 android native-lib works * x86_64 and arm64 android native-lib works * x86_64 and arm64 android native-lib works * test armv7 android native-lib * test all android native-libs * test all android native-libs * test all android native-libs * test all android native-libs - without SodiumAndroid * test all android native-libs - with FindSodiumAndroid.cmake * win, with Sodium via SODIUM_DIR * win, with Sodium via SODIUM_DIR env * win, with Sodium via SODIUM_DIR env * win, with Sodium via SODIUM_DIR env and SODIUM_USE_STATIC_LIBS * win, with Sodium via SODIUM_DIR, SODIUM_USE_STATIC_LIBS and SODIUM_INCLUDE_DIR * android, with FindSodium * android, with FindSodium with SODIUM_USE_STATIC_LIBS * remove if not apple * target_link_libraries(ton_crypto_core PUBLIC secp256k1) * android SECP256K1_INCLUDE_DIRS * android SECP256K1_INCLUDE_DIR * add libsecp256k1.a/so pre-compiled with ubuntu 22 x86-64 * add libsecp256k1.a/so pre-compiled with ubuntu 22 x86-64 * sodium dirs * sodium dirs * sodium dirs * remove NOT APPLE and SodiumAndroid * add NOT APPLE and remove SodiumAndroid * add NOT APPLE and remove SodiumAndroid * remove build scripts for 18.04, reduce CMakeLists.txt * remove build scripts for 18.04, reduce CMakeLists.txt * Fix cas consumption during library load * Fix fetch_config_params after merge * Add all ADDDIVMOD ops to Asm.fif * Save unpaid storage fee to due_payment * Add "set prev blocks info" to emulator * Adjusted builds (#13) * Update flake.nix Add libsodium * add libsecp256k1-dev and libsodium-dev into wasm build * make back emulator a shared library; put emulator to artifacts; compile wasm artifacts with sodium and secp256k1. * add secp256k1 to nix * compile emulator statically with nix * compile emulator statically with nix * compile emulator lib statically with nix * compile emulator lib statically with nix * add libemulator to artifacts * add shared libemulator library to artifacts * minor release fix * update set-output commands; add recent_changelog.md * releases fixes * releases fixes, multiline * releases fixes, multiline * releases fixes, multiline * put back multiline changelog * put back multiline changelog * ConfigParam 19 (global-id) and GLOBALID instruction * Fix gas consumption in HASHEXT * Add blst library * Add bls instructions * Allow passing long code to opcode-timing * Add bls testcase * More BLS instructions * Fix tests, add bls tests * Add more bls tests * Improve some bls operations * Adjust some BLS gas prices * Adjust BLS gas prices * Enable __BLST_PORTABLE__ flag only if PORTABLE flag is set * Add tests for BLS_PAIRING * GASCONSUMED instruction * Fix compilation against docker with blst library; (#14) * fix compilation against docker with blst library; add precompiled libblst.a to android builds * minor fix * Adjust BLKSWX gas * Fix comparison with NAN * Allow arbitrary integers for scalars in ristretto multiplication, fix test * Adjust nix builds according to PR 694 (#15) * integrate and test PR-694 * integrate and test PR-694, test 2 * Add P256_CHKSIGN (secp256r1) --------- Co-authored-by: SpyCheese <mikle98@yandex.ru> Co-authored-by: neodiX42 <namlem@gmail.com>
		
			
				
	
	
		
			470 lines
		
	
	
	
		
			17 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			470 lines
		
	
	
	
		
			17 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
    This file is part of TON Blockchain Library.
 | 
						|
 | 
						|
    TON Blockchain Library is free software: you can redistribute it and/or modify
 | 
						|
    it under the terms of the GNU Lesser General Public License as published by
 | 
						|
    the Free Software Foundation, either version 2 of the License, or
 | 
						|
    (at your option) any later version.
 | 
						|
 | 
						|
    TON Blockchain Library is distributed in the hope that it will be useful,
 | 
						|
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
    GNU Lesser General Public License for more details.
 | 
						|
 | 
						|
    You should have received a copy of the GNU Lesser General Public License
 | 
						|
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
 | 
						|
 | 
						|
    Copyright 2017-2020 Telegram Systems LLP
 | 
						|
*/
 | 
						|
#include <cassert>
 | 
						|
#include <iterator>
 | 
						|
#include "vm/opctable.h"
 | 
						|
#include "vm/cellslice.h"
 | 
						|
#include "vm/excno.hpp"
 | 
						|
#include "vm/vm.h"
 | 
						|
#include <iostream>
 | 
						|
#include <iomanip>
 | 
						|
#include <sstream>
 | 
						|
#include <functional>
 | 
						|
 | 
						|
#include "td/utils/format.h"
 | 
						|
 | 
						|
namespace vm {
 | 
						|
 | 
						|
DispatchTable* OpcodeTable::finalize() {
 | 
						|
  if (final) {
 | 
						|
    return this;
 | 
						|
  }
 | 
						|
  instruction_list.clear();
 | 
						|
  instruction_list.reserve(instructions.size() * 2 + 1);
 | 
						|
 | 
						|
  unsigned upto = 0;
 | 
						|
  for (const auto& x : instructions) {
 | 
						|
    auto range = x.second->get_opcode_range();
 | 
						|
    assert(range.first == x.first);
 | 
						|
    assert(range.first < range.second);
 | 
						|
    assert(range.first >= upto);
 | 
						|
    assert(range.second <= top_opcode);
 | 
						|
    if (range.first > upto) {
 | 
						|
      instruction_list.emplace_back(upto, new OpcodeInstrDummy{upto, range.first});
 | 
						|
    }
 | 
						|
    instruction_list.emplace_back(x);
 | 
						|
    upto = range.second;
 | 
						|
  }
 | 
						|
 | 
						|
  if (upto < top_opcode) {
 | 
						|
    instruction_list.emplace_back(upto, new OpcodeInstrDummy{upto, top_opcode});
 | 
						|
  }
 | 
						|
 | 
						|
  instruction_list.shrink_to_fit();
 | 
						|
  final = true;
 | 
						|
  return this;
 | 
						|
}
 | 
						|
 | 
						|
OpcodeTable& OpcodeTable::insert(const OpcodeInstr* instr) {
 | 
						|
  LOG_IF(FATAL, !insert_bool(instr)) << td::format::lambda([&](auto& sb) {
 | 
						|
    sb << "cannot insert instruction into table " << name << ": ";
 | 
						|
    if (!instr) {
 | 
						|
      sb << "instruction is null";
 | 
						|
    } else if (final) {
 | 
						|
      sb << "instruction table already finalized";
 | 
						|
    } else {
 | 
						|
      auto range = instr->get_opcode_range();
 | 
						|
      sb << "opcode range " << td::format::as_hex(range.first) << ".." << td::format::as_hex(range.second - 1)
 | 
						|
         << " already occupied or invalid";
 | 
						|
    }
 | 
						|
  });
 | 
						|
  return *this;
 | 
						|
}
 | 
						|
 | 
						|
bool OpcodeTable::insert_bool(const OpcodeInstr* instr) {
 | 
						|
  if (!instr || final) {
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
  auto range = instr->get_opcode_range();
 | 
						|
  assert(range.first < range.second);
 | 
						|
  assert(range.second <= top_opcode);
 | 
						|
  auto it = instructions.lower_bound(range.first);
 | 
						|
  if (it != instructions.end() && it->first < range.second) {
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
  if (it != instructions.begin()) {
 | 
						|
    auto prev_range = std::prev(it)->second->get_opcode_range();
 | 
						|
    assert(prev_range.first < prev_range.second);
 | 
						|
    assert(prev_range.first == std::prev(it)->first);
 | 
						|
    if (prev_range.second > range.first) {
 | 
						|
      return false;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  instructions.emplace_hint(it, range.first, instr);
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
const OpcodeInstr* OpcodeTable::lookup_instr(unsigned opcode, unsigned bits) const {
 | 
						|
  std::size_t i = 0, j = instruction_list.size();
 | 
						|
  assert(j);
 | 
						|
  while (j - i > 1) {
 | 
						|
    auto k = ((j + i) >> 1);
 | 
						|
    if (instruction_list[k].first <= opcode) {
 | 
						|
      i = k;
 | 
						|
    } else {
 | 
						|
      j = k;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return instruction_list[i].second;
 | 
						|
}
 | 
						|
 | 
						|
const OpcodeInstr* OpcodeTable::lookup_instr(const CellSlice& cs, unsigned& opcode, unsigned& bits) const {
 | 
						|
  bits = max_opcode_bits;
 | 
						|
  unsigned long long prefetch = cs.prefetch_ulong_top(bits);
 | 
						|
  opcode = (unsigned)(prefetch >> (64 - max_opcode_bits));
 | 
						|
  opcode &= (static_cast<int32_t>(static_cast<td::uint32>(-1) << max_opcode_bits) >> bits);
 | 
						|
  return lookup_instr(opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeTable::dispatch(VmState* st, CellSlice& cs) const {
 | 
						|
  assert(final);
 | 
						|
  unsigned bits, opcode;
 | 
						|
  auto instr = lookup_instr(cs, opcode, bits);
 | 
						|
  //std::cerr << "lookup_instr: cs.size()=" << cs.size() << "; bits=" << bits << "; opcode=" << std::setw(6) << std::setfill('0') << std::hex << opcode << std::dec << std::endl;
 | 
						|
  return instr->dispatch(st, cs, opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeTable::dump_instr(CellSlice& cs) const {
 | 
						|
  assert(final);
 | 
						|
  unsigned bits, opcode;
 | 
						|
  auto instr = lookup_instr(cs, opcode, bits);
 | 
						|
  return instr->dump(cs, opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeTable::instr_len(const CellSlice& cs) const {
 | 
						|
  assert(final);
 | 
						|
  unsigned bits, opcode;
 | 
						|
  auto instr = lookup_instr(cs, opcode, bits);
 | 
						|
  return instr->instr_len(cs, opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstr::OpcodeInstr(unsigned _opcode, unsigned _bits, bool)
 | 
						|
    : min_opcode(_opcode << (max_opcode_bits - _bits)), max_opcode((_opcode + 1) << (max_opcode_bits - _bits)) {
 | 
						|
  assert(_opcode < (1U << _bits) && _bits <= max_opcode_bits);
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrDummy::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  st->consume_gas(gas_per_instr);
 | 
						|
  throw VmError{Excno::inv_opcode, "invalid opcode", opcode};
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeInstr::dump(CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  return "";
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstr::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstrSimple::OpcodeInstrSimple(unsigned opcode, unsigned _opc_bits, std::string _name, exec_instr_func_t exec)
 | 
						|
    : OpcodeInstr(opcode, _opc_bits, false)
 | 
						|
    , opc_bits(static_cast<unsigned char>(_opc_bits))
 | 
						|
    , name(_name)
 | 
						|
    , exec_instr(exec) {
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrSimple::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  st->consume_gas(gas_per_instr + opc_bits * gas_per_bit);
 | 
						|
  if (bits < opc_bits) {
 | 
						|
    throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)};
 | 
						|
  }
 | 
						|
  cs.advance(opc_bits);
 | 
						|
  return exec_instr(st, cs, opcode >> (max_opcode_bits - opc_bits), opc_bits);
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeInstrSimple::dump(CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < opc_bits) {
 | 
						|
    return "";
 | 
						|
  }
 | 
						|
  cs.advance(opc_bits);
 | 
						|
  return name;
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrSimple::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < opc_bits) {
 | 
						|
    return 0;
 | 
						|
  } else {
 | 
						|
    return opc_bits;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstrSimplest::OpcodeInstrSimplest(unsigned opcode, unsigned _opc_bits, std::string _name,
 | 
						|
                                         exec_simple_instr_func_t exec)
 | 
						|
    : OpcodeInstr(opcode, _opc_bits, false)
 | 
						|
    , opc_bits(static_cast<unsigned char>(_opc_bits))
 | 
						|
    , name(_name)
 | 
						|
    , exec_instr(exec) {
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrSimplest::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  st->consume_gas(gas_per_instr + opc_bits * gas_per_bit);
 | 
						|
  if (bits < opc_bits) {
 | 
						|
    throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)};
 | 
						|
  }
 | 
						|
  cs.advance(opc_bits);
 | 
						|
  return exec_instr(st);
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeInstrSimplest::dump(CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < opc_bits) {
 | 
						|
    return "";
 | 
						|
  }
 | 
						|
  cs.advance(opc_bits);
 | 
						|
  return name;
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrSimplest::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < opc_bits) {
 | 
						|
    return 0;
 | 
						|
  } else {
 | 
						|
    return opc_bits;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstrFixed::OpcodeInstrFixed(unsigned opcode, unsigned _opc_bits, unsigned _arg_bits, dump_arg_instr_func_t dump,
 | 
						|
                                   exec_arg_instr_func_t exec)
 | 
						|
    : OpcodeInstr(opcode, _opc_bits, false)
 | 
						|
    , opc_bits(static_cast<unsigned char>(_opc_bits))
 | 
						|
    , tot_bits(static_cast<unsigned char>(_opc_bits + _arg_bits))
 | 
						|
    , dump_instr(dump)
 | 
						|
    , exec_instr(exec) {
 | 
						|
  assert(_arg_bits <= max_opcode_bits && _opc_bits <= max_opcode_bits && _arg_bits + _opc_bits <= max_opcode_bits);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstrFixed::OpcodeInstrFixed(unsigned opcode_min, unsigned opcode_max, unsigned _tot_bits, unsigned _arg_bits,
 | 
						|
                                   dump_arg_instr_func_t dump, exec_arg_instr_func_t exec)
 | 
						|
    : OpcodeInstr(opcode_min << (max_opcode_bits - _tot_bits), opcode_max << (max_opcode_bits - _tot_bits))
 | 
						|
    , opc_bits(static_cast<unsigned char>(_tot_bits - _arg_bits))
 | 
						|
    , tot_bits(static_cast<unsigned char>(_tot_bits))
 | 
						|
    , dump_instr(dump)
 | 
						|
    , exec_instr(exec) {
 | 
						|
  assert(_arg_bits <= _tot_bits && _tot_bits <= max_opcode_bits);
 | 
						|
  assert(opcode_min < opcode_max && opcode_max <= (1U << _tot_bits));
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrFixed::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  st->consume_gas(gas_per_instr + tot_bits * gas_per_bit);
 | 
						|
  if (bits < tot_bits) {
 | 
						|
    throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)};
 | 
						|
  }
 | 
						|
  cs.advance(tot_bits);
 | 
						|
  return exec_instr(st, opcode >> (max_opcode_bits - tot_bits));
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeInstrFixed::dump(CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < tot_bits) {
 | 
						|
    return "";
 | 
						|
  }
 | 
						|
  cs.advance(tot_bits);
 | 
						|
  return dump_instr(cs, opcode >> (max_opcode_bits - tot_bits));
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrFixed::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < tot_bits) {
 | 
						|
    return 0;
 | 
						|
  } else {
 | 
						|
    return tot_bits;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstrExt::OpcodeInstrExt(unsigned opcode, unsigned _opc_bits, unsigned _arg_bits, dump_instr_func_t dump,
 | 
						|
                               exec_instr_func_t exec, compute_instr_len_func_t comp_len)
 | 
						|
    : OpcodeInstr(opcode, _opc_bits, false)
 | 
						|
    , opc_bits(static_cast<unsigned char>(_opc_bits))
 | 
						|
    , tot_bits(static_cast<unsigned char>(_opc_bits + _arg_bits))
 | 
						|
    , dump_instr(dump)
 | 
						|
    , exec_instr(exec)
 | 
						|
    , compute_instr_len(comp_len) {
 | 
						|
  assert(_arg_bits <= max_opcode_bits && _opc_bits <= max_opcode_bits && _arg_bits + _opc_bits <= max_opcode_bits);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstrExt::OpcodeInstrExt(unsigned opcode_min, unsigned opcode_max, unsigned _tot_bits, unsigned _arg_bits,
 | 
						|
                               dump_instr_func_t dump, exec_instr_func_t exec, compute_instr_len_func_t comp_len)
 | 
						|
    : OpcodeInstr(opcode_min << (max_opcode_bits - _tot_bits), opcode_max << (max_opcode_bits - _tot_bits))
 | 
						|
    , opc_bits(static_cast<unsigned char>(_tot_bits - _arg_bits))
 | 
						|
    , tot_bits(static_cast<unsigned char>(_tot_bits))
 | 
						|
    , dump_instr(dump)
 | 
						|
    , exec_instr(exec)
 | 
						|
    , compute_instr_len(comp_len) {
 | 
						|
  assert(_arg_bits <= _tot_bits && _tot_bits <= max_opcode_bits);
 | 
						|
  assert(opcode_min < opcode_max && opcode_max <= (1U << _tot_bits));
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrExt::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  st->consume_gas(gas_per_instr + tot_bits * gas_per_bit);
 | 
						|
  if (bits < tot_bits) {
 | 
						|
    throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)};
 | 
						|
  }
 | 
						|
  return exec_instr(st, cs, opcode >> (max_opcode_bits - tot_bits), tot_bits);
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeInstrExt::dump(CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < tot_bits) {
 | 
						|
    return "";
 | 
						|
  }
 | 
						|
  return dump_instr(cs, opcode >> (max_opcode_bits - tot_bits), (int)tot_bits);
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrExt::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (bits < tot_bits) {
 | 
						|
    return 0;
 | 
						|
  } else {
 | 
						|
    return compute_instr_len(cs, opcode >> (max_opcode_bits - tot_bits), (int)tot_bits);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/*
 | 
						|
OpcodeInstr* OpcodeInstr::mksimple(unsigned opcode, unsigned opc_bits, std::string _name, exec_instr_func_t exec) {
 | 
						|
  return new OpcodeInstrSimple(opcode, opc_bits, _name, exec);
 | 
						|
}
 | 
						|
*/
 | 
						|
 | 
						|
OpcodeInstr* OpcodeInstr::mksimple(unsigned opcode, unsigned opc_bits, std::string _name,
 | 
						|
                                   exec_simple_instr_func_t exec) {
 | 
						|
  return new OpcodeInstrSimplest(opcode, opc_bits, _name, exec);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstr* OpcodeInstr::mkfixed(unsigned opcode, unsigned opc_bits, unsigned arg_bits, dump_arg_instr_func_t dump,
 | 
						|
                                  exec_arg_instr_func_t exec) {
 | 
						|
  return new OpcodeInstrFixed(opcode, opc_bits, arg_bits, dump, exec);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstr* OpcodeInstr::mkfixedrange(unsigned opcode_min, unsigned opcode_max, unsigned tot_bits, unsigned arg_bits,
 | 
						|
                                       dump_arg_instr_func_t dump, exec_arg_instr_func_t exec) {
 | 
						|
  return new OpcodeInstrFixed(opcode_min, opcode_max, tot_bits, arg_bits, dump, exec);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstr* OpcodeInstr::mkext(unsigned opcode, unsigned opc_bits, unsigned arg_bits, dump_instr_func_t dump,
 | 
						|
                                exec_instr_func_t exec, compute_instr_len_func_t comp_len) {
 | 
						|
  return new OpcodeInstrExt(opcode, opc_bits, arg_bits, dump, exec, comp_len);
 | 
						|
}
 | 
						|
 | 
						|
OpcodeInstr* OpcodeInstr::mkextrange(unsigned opcode_min, unsigned opcode_max, unsigned tot_bits, unsigned arg_bits,
 | 
						|
                                     dump_instr_func_t dump, exec_instr_func_t exec,
 | 
						|
                                     compute_instr_len_func_t comp_len) {
 | 
						|
  return new OpcodeInstrExt(opcode_min, opcode_max, tot_bits, arg_bits, dump, exec, comp_len);
 | 
						|
}
 | 
						|
 | 
						|
namespace instr {
 | 
						|
 | 
						|
using namespace std::placeholders;
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_1sr(std::string prefix, std::string suffix) {
 | 
						|
  return [prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << 's' << (args & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_1sr_l(std::string prefix, std::string suffix) {
 | 
						|
  return [prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << 's' << (args & 255) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_2sr(std::string prefix, std::string suffix) {
 | 
						|
  return [prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << 's' << ((args >> 4) & 15) << ",s" << (args & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_2sr_adj(unsigned adj, std::string prefix, std::string suffix) {
 | 
						|
  return [adj, prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << 's' << (int)((args >> 4) & 15) - (int)((adj >> 4) & 15) << ",s" << (int)(args & 15) - (int)(adj & 15)
 | 
						|
       << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_3sr(std::string prefix, std::string suffix) {
 | 
						|
  return [prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << 's' << ((args >> 8) & 15) << ",s" << ((args >> 4) & 15) << ",s" << (args & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_3sr_adj(unsigned adj, std::string prefix, std::string suffix) {
 | 
						|
  return [adj, prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << 's' << (int)((args >> 8) & 15) - (int)((adj >> 8) & 15) << ",s"
 | 
						|
       << (int)((args >> 4) & 15) - (int)((adj >> 4) & 15) << ",s" << (int)(args & 15) - (int)(adj & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_1c(std::string prefix, std::string suffix) {
 | 
						|
  return [prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << (args & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_1c_l_add(int adj, std::string prefix, std::string suffix) {
 | 
						|
  return [adj, prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << (int)(args & 255) + adj << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_1c_and(unsigned mask, std::string prefix, std::string suffix) {
 | 
						|
  return [mask, prefix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << (args & mask) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_2c(std::string prefix, std::string interfix, std::string suffix) {
 | 
						|
  return [prefix, interfix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << ((args >> 4) & 15) << interfix << (args & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
dump_arg_instr_func_t dump_2c_add(unsigned add, std::string prefix, std::string interfix, std::string suffix) {
 | 
						|
  return [add, prefix, interfix, suffix](CellSlice&, unsigned args) -> std::string {
 | 
						|
    std::ostringstream os;
 | 
						|
    os << prefix << ((args >> 4) & 15) + ((add >> 4) & 15) << interfix << (args & 15) + (add & 15) << suffix;
 | 
						|
    return os.str();
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
}  // namespace instr
 | 
						|
 | 
						|
OpcodeInstr* OpcodeInstr::require_version(int required_version) {
 | 
						|
  return new OpcodeInstrWithVersion(this, required_version);
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrWithVersion::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  if (st->get_global_version() < required_version) {
 | 
						|
    st->consume_gas(gas_per_instr);
 | 
						|
    throw VmError{Excno::inv_opcode, "invalid opcode", opcode};
 | 
						|
  }
 | 
						|
  return instr->dispatch(st, cs, opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
std::string OpcodeInstrWithVersion::dump(CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  return instr->dump(cs, opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
int OpcodeInstrWithVersion::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const {
 | 
						|
  return instr->instr_len(cs, opcode, bits);
 | 
						|
}
 | 
						|
 | 
						|
}  // namespace vm
 |