From 1008c70a14c2ad6a6ec70e9233914daa63c8a95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 30 Oct 2014 19:17:55 +0100 Subject: [PATCH] Create dedicated function for pushdata reading --- libevmjit/Utils.cpp | 17 +++++++++++++++++ libevmjit/Utils.h | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/libevmjit/Utils.cpp b/libevmjit/Utils.cpp index 0fd9c0e41..966dc69bd 100644 --- a/libevmjit/Utils.cpp +++ b/libevmjit/Utils.cpp @@ -35,6 +35,23 @@ i256 eth2llvm(u256 _u) return i; } +u256 readPushData(const byte*& _curr, const byte* _end) +{ + auto pushInst = *_curr; + assert(Instruction(pushInst) >= Instruction::PUSH1 && Instruction(pushInst) <= Instruction::PUSH32); + auto numBytes = pushInst - static_cast(Instruction::PUSH1) + 1; + u256 value; + ++_curr; // Point the data + for (decltype(numBytes) i = 0; i < numBytes; ++i) + { + byte b = (_curr != _end) ? *_curr++ : 0; + value <<= 8; + value |= b; + } + --_curr; // Point the last real byte read + return value; +} + } } } diff --git a/libevmjit/Utils.h b/libevmjit/Utils.h index 7a3afda64..9fa9f050e 100644 --- a/libevmjit/Utils.h +++ b/libevmjit/Utils.h @@ -5,6 +5,7 @@ #include #include +#include namespace dev { @@ -29,6 +30,11 @@ static_assert(sizeof(i256) == 32, "Wrong i265 size"); u256 llvm2eth(i256); i256 eth2llvm(u256); +/// Reads PUSH data from pointed fragment of bytecode and constructs number out of it +/// Reading out of bytecode means reading 0 +/// @param _curr is updates and points the last real byte read +u256 readPushData(const byte*& _curr, const byte* _end); + #define ANY_PUSH PUSH1: \ case Instruction::PUSH2: \ case Instruction::PUSH3: \