/ libevmjit / Compiler.cpp
Compiler.cpp
   1  #include "Compiler.h"
   2  
   3  #include <fstream>
   4  #include <chrono>
   5  #include <sstream>
   6  
   7  #include "preprocessor/llvm_includes_start.h"
   8  #include <llvm/IR/CFG.h>
   9  #include <llvm/IR/Module.h>
  10  #include <llvm/IR/IntrinsicInst.h>
  11  #include "preprocessor/llvm_includes_end.h"
  12  
  13  #include "JIT.h"
  14  #include "Instruction.h"
  15  #include "Type.h"
  16  #include "Memory.h"
  17  #include "Ext.h"
  18  #include "GasMeter.h"
  19  #include "Utils.h"
  20  #include "Endianness.h"
  21  #include "Arith256.h"
  22  #include "RuntimeManager.h"
  23  
  24  #ifndef __has_cpp_attribute
  25    #define __has_cpp_attribute(x) 0
  26  #endif
  27  
  28  #if __has_cpp_attribute(fallthrough)
  29    #define FALLTHROUGH [[fallthrough]]
  30  #elif __has_cpp_attribute(clang::fallthrough)
  31    #define FALLTHROUGH [[clang::fallthrough]]
  32  #elif __has_cpp_attribute(gnu::fallthrough)
  33    #define FALLTHROUGH [[gnu::fallthrough]]
  34  #else
  35    #define FALLTHROUGH
  36  #endif
  37  
  38  namespace dev
  39  {
  40  namespace eth
  41  {
  42  namespace jit
  43  {
  44  
  45  static const auto c_destIdxLabel = "destIdx";
  46  
  47  Compiler::Compiler(Options const& _options, evmc_revision _rev, bool _staticCall, llvm::LLVMContext& _llvmContext):
  48  	m_options(_options),
  49  	m_rev(_rev),
  50  	m_staticCall(_staticCall),
  51  	m_builder(_llvmContext)
  52  {
  53  	Type::init(m_builder.getContext());
  54  }
  55  
  56  std::vector<BasicBlock> Compiler::createBasicBlocks(code_iterator _codeBegin, code_iterator _codeEnd)
  57  {
  58  	/// Helper function that skips push data and finds next iterator (can be the end)
  59  	auto skipPushDataAndGetNext = [](code_iterator _curr, code_iterator _end)
  60  	{
  61  		static const auto push1  = static_cast<size_t>(Instruction::PUSH1);
  62  		static const auto push32 = static_cast<size_t>(Instruction::PUSH32);
  63  		size_t offset = 1;
  64  		if (*_curr >= push1 && *_curr <= push32)
  65  			offset += std::min<size_t>(*_curr - push1 + 1, (_end - _curr) - 1);
  66  		return _curr + offset;
  67  	};
  68  
  69  	std::vector<BasicBlock> blocks;
  70  
  71  	bool isDead = false;
  72  	auto begin = _codeBegin; // begin of current block
  73  	for (auto curr = begin, next = begin; curr != _codeEnd; curr = next)
  74  	{
  75  		next = skipPushDataAndGetNext(curr, _codeEnd);
  76  
  77  		if (isDead)
  78  		{
  79  			if (Instruction(*curr) == Instruction::JUMPDEST)
  80  			{
  81  				isDead = false;
  82  				begin = curr;
  83  			}
  84  			else
  85  				continue;
  86  		}
  87  
  88  		bool isEnd = false;
  89  		switch (Instruction(*curr))
  90  		{
  91  		case Instruction::JUMP:
  92  		case Instruction::RETURN:
  93  		case Instruction::REVERT:
  94  		case Instruction::STOP:
  95  		case Instruction::SUICIDE:
  96  			isDead = true;
  97  			FALLTHROUGH;
  98  		case Instruction::JUMPI:
  99  			isEnd = true;
 100  			break;
 101  
 102  		default:
 103  			break;
 104  		}
 105  
 106  		assert(next <= _codeEnd);
 107  		if (next == _codeEnd || Instruction(*next) == Instruction::JUMPDEST)
 108  			isEnd = true;
 109  
 110  		if (isEnd)
 111  		{
 112  			auto beginIdx = begin - _codeBegin;
 113  			blocks.emplace_back(beginIdx, begin, next, m_mainFunc);
 114  			begin = next;
 115  		}
 116  	}
 117  
 118  	return blocks;
 119  }
 120  
 121  void Compiler::resolveJumps()
 122  {
 123  	auto jumpTable = llvm::cast<llvm::SwitchInst>(m_jumpTableBB->getTerminator());
 124  	auto jumpTableInput = llvm::cast<llvm::PHINode>(m_jumpTableBB->begin());
 125  
 126  	// Iterate through all EVM instructions blocks (skip first one and last 4 - special blocks).
 127  	for (auto it = std::next(m_mainFunc->begin()), end = std::prev(m_mainFunc->end(), 4); it != end; ++it)
 128  	{
 129  		auto nextBlockIter = it;
 130  		++nextBlockIter; // If the last code block, that will be "stop" block.
 131  		auto currentBlockPtr = &(*it);
 132  		auto nextBlockPtr = &(*nextBlockIter);
 133  		
 134  		auto term = it->getTerminator();
 135  		llvm::BranchInst* jump = nullptr;
 136  
 137  		if (!term) // Block may have no terminator if the next instruction is a jump destination.
 138  			IRBuilder{currentBlockPtr}.CreateBr(nextBlockPtr);
 139  		else if ((jump = llvm::dyn_cast<llvm::BranchInst>(term)) && jump->getSuccessor(0) == m_jumpTableBB)
 140  		{
 141  			auto destIdx = llvm::cast<llvm::ValueAsMetadata>(jump->getMetadata(c_destIdxLabel)->getOperand(0))->getValue();
 142  			if (auto constant = llvm::dyn_cast<llvm::ConstantInt>(destIdx))
 143  			{
 144  				// If destination index is a constant do direct jump to the destination block.
 145  				auto bb = jumpTable->findCaseValue(constant)->getCaseSuccessor();
 146  				jump->setSuccessor(0, bb);
 147  			}
 148  			else
 149  				jumpTableInput->addIncoming(destIdx, currentBlockPtr); // Fill up PHI node
 150  
 151  			if (jump->isConditional())
 152  				jump->setSuccessor(1, &(*nextBlockIter)); // Set next block for conditional jumps
 153  		}
 154  	}
 155  
 156  	auto simplifiedInput = jumpTableInput->getNumIncomingValues() == 0 ?
 157  			llvm::UndefValue::get(jumpTableInput->getType()) :
 158  			jumpTableInput->hasConstantValue();
 159  	if (simplifiedInput)
 160  	{
 161  		jumpTableInput->replaceAllUsesWith(simplifiedInput);
 162  		jumpTableInput->eraseFromParent();
 163  	}
 164  }
 165  
 166  std::unique_ptr<llvm::Module> Compiler::compile(code_iterator _begin, code_iterator _end, std::string const& _id)
 167  {
 168  	auto module = llvm::make_unique<llvm::Module>(_id, m_builder.getContext()); // TODO: Provide native DataLayout
 169  
 170  	// Create main function
 171  	auto mainFuncType = llvm::FunctionType::get(Type::MainReturn, Type::RuntimePtr, false);
 172  	m_mainFunc = llvm::Function::Create(mainFuncType, llvm::Function::ExternalLinkage, _id, module.get());
 173  	m_mainFunc->args().begin()->setName("rt");
 174  
 175  	// Create entry basic block
 176  	auto entryBB = llvm::BasicBlock::Create(m_builder.getContext(), "Entry", m_mainFunc);
 177  
 178  	auto blocks = createBasicBlocks(_begin, _end);
 179  
 180   	// Special "Stop" block. Guarantees that there exists a next block after the code blocks (also when there are no code blocks).
 181  	auto stopBB = llvm::BasicBlock::Create(m_mainFunc->getContext(), "Stop", m_mainFunc);
 182  	m_jumpTableBB = llvm::BasicBlock::Create(m_mainFunc->getContext(), "JumpTable", m_mainFunc);
 183  	auto abortBB = llvm::BasicBlock::Create(m_mainFunc->getContext(), "Abort", m_mainFunc);
 184  
 185  	m_builder.SetInsertPoint(m_jumpTableBB); // Must be before basic blocks compilation
 186  	auto target = m_builder.CreatePHI(Type::Word, 16, "target");
 187  	m_builder.CreateSwitch(target, abortBB);
 188  
 189  	m_builder.SetInsertPoint(entryBB);
 190  
 191  
 192  	// Init runtime structures.
 193  	RuntimeManager runtimeManager(m_builder, _begin, _end);
 194  	GasMeter gasMeter(m_builder, runtimeManager, m_rev);
 195  	Memory memory(runtimeManager, gasMeter);
 196  	Ext ext(runtimeManager, memory);
 197  	Arith256 arith(m_builder);
 198  
 199  	auto jmpBufWords = m_builder.CreateAlloca(Type::BytePtr, m_builder.getInt64(3), "jmpBuf.words");
 200  	auto frameaddress = llvm::Intrinsic::getDeclaration(module.get(), llvm::Intrinsic::frameaddress);
 201  	auto fp = m_builder.CreateCall(frameaddress, m_builder.getInt32(0), "fp");
 202  	m_builder.CreateStore(fp, jmpBufWords);
 203  	auto stacksave = llvm::Intrinsic::getDeclaration(module.get(), llvm::Intrinsic::stacksave);
 204  	auto sp = m_builder.CreateCall(stacksave, {}, "sp");
 205  	auto jmpBufSp = m_builder.CreateConstInBoundsGEP1_64(jmpBufWords, 2, "jmpBuf.sp");
 206  	m_builder.CreateStore(sp, jmpBufSp);
 207  	auto setjmp = llvm::Intrinsic::getDeclaration(module.get(), llvm::Intrinsic::eh_sjlj_setjmp);
 208  	auto jmpBuf = m_builder.CreateBitCast(jmpBufWords, Type::BytePtr, "jmpBuf");
 209  	auto r = m_builder.CreateCall(setjmp, jmpBuf);
 210  	auto normalFlow = m_builder.CreateICmpEQ(r, m_builder.getInt32(0));
 211  	runtimeManager.setJmpBuf(jmpBuf);
 212  	m_builder.CreateCondBr(normalFlow, entryBB->getNextNode(), abortBB, Type::expectTrue);
 213  
 214  	for (auto& block: blocks)
 215  		compileBasicBlock(block, runtimeManager, arith, memory, ext, gasMeter);
 216  
 217  	// Code for special blocks:
 218  	m_builder.SetInsertPoint(stopBB);
 219  	runtimeManager.exit(ReturnCode::Stop);
 220  
 221  	m_builder.SetInsertPoint(abortBB);
 222  	runtimeManager.exit(ReturnCode::OutOfGas);
 223  
 224  	resolveJumps();
 225  
 226  	return module;
 227  }
 228  
 229  
 230  void Compiler::compileBasicBlock(BasicBlock& _basicBlock, RuntimeManager& _runtimeManager,
 231  								 Arith256& _arith, Memory& _memory, Ext& _ext, GasMeter& _gasMeter)
 232  {
 233  	m_builder.SetInsertPoint(_basicBlock.llvm());
 234  	LocalStack stack{m_builder, _runtimeManager};
 235  
 236  	for (auto it = _basicBlock.begin(); it != _basicBlock.end(); ++it)
 237  	{
 238  		auto inst = Instruction(*it);
 239  
 240  		_gasMeter.count(inst);
 241  
 242  		switch (inst)
 243  		{
 244  
 245  		case Instruction::ADD:
 246  		{
 247  			auto lhs = stack.pop();
 248  			auto rhs = stack.pop();
 249  			auto result = m_builder.CreateAdd(lhs, rhs);
 250  			stack.push(result);
 251  			break;
 252  		}
 253  
 254  		case Instruction::SUB:
 255  		{
 256  			auto lhs = stack.pop();
 257  			auto rhs = stack.pop();
 258  			auto result = m_builder.CreateSub(lhs, rhs);
 259  			stack.push(result);
 260  			break;
 261  		}
 262  
 263  		case Instruction::MUL:
 264  		{
 265  			auto lhs = stack.pop();
 266  			auto rhs = stack.pop();
 267  			auto res = m_builder.CreateMul(lhs, rhs);
 268  			stack.push(res);
 269  			break;
 270  		}
 271  
 272  		case Instruction::DIV:
 273  		{
 274  			auto d = stack.pop();
 275  			auto n = stack.pop();
 276  			auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0));
 277  			n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal
 278  			auto r = m_builder.CreateUDiv(d, n);
 279  			r = m_builder.CreateSelect(divByZero, Constant::get(0), r);
 280  			stack.push(r);
 281  			break;
 282  		}
 283  
 284  		case Instruction::SDIV:
 285  		{
 286  			auto d = stack.pop();
 287  			auto n = stack.pop();
 288  			auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0));
 289  			auto divByMinusOne = m_builder.CreateICmpEQ(n, Constant::get(-1));
 290  			n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal
 291  			auto r = m_builder.CreateSDiv(d, n);
 292  			r = m_builder.CreateSelect(divByZero, Constant::get(0), r);
 293  			auto dNeg = m_builder.CreateSub(Constant::get(0), d);
 294  			r = m_builder.CreateSelect(divByMinusOne, dNeg, r); // protect against undef i256.min / -1
 295  			stack.push(r);
 296  			break;
 297  		}
 298  
 299  		case Instruction::MOD:
 300  		{
 301  			auto d = stack.pop();
 302  			auto n = stack.pop();
 303  			auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0));
 304  			n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal
 305  			auto r = m_builder.CreateURem(d, n);
 306  			r = m_builder.CreateSelect(divByZero, Constant::get(0), r);
 307  			stack.push(r);
 308  			break;
 309  		}
 310  
 311  		case Instruction::SMOD:
 312  		{
 313  			auto d = stack.pop();
 314  			auto n = stack.pop();
 315  			auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0));
 316  			auto divByMinusOne = m_builder.CreateICmpEQ(n, Constant::get(-1));
 317  			n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal
 318  			auto r = m_builder.CreateSRem(d, n);
 319  			r = m_builder.CreateSelect(divByZero, Constant::get(0), r);
 320  			r = m_builder.CreateSelect(divByMinusOne, Constant::get(0), r); // protect against undef i256.min / -1
 321  			stack.push(r);
 322  			break;
 323  		}
 324  
 325  		case Instruction::ADDMOD:
 326  		{
 327  			auto i512Ty = m_builder.getIntNTy(512);
 328  			auto a = stack.pop();
 329  			auto b = stack.pop();
 330  			auto m = stack.pop();
 331  			auto divByZero = m_builder.CreateICmpEQ(m, Constant::get(0));
 332  			a = m_builder.CreateZExt(a, i512Ty);
 333  			b = m_builder.CreateZExt(b, i512Ty);
 334  			m = m_builder.CreateZExt(m, i512Ty);
 335  			auto s = m_builder.CreateNUWAdd(a, b);
 336  			s = m_builder.CreateURem(s, m);
 337  			s = m_builder.CreateTrunc(s, Type::Word);
 338  			s = m_builder.CreateSelect(divByZero, Constant::get(0), s);
 339  			stack.push(s);
 340  			break;
 341  		}
 342  
 343  		case Instruction::MULMOD:
 344  		{
 345  			auto i512Ty = m_builder.getIntNTy(512);
 346  			auto a = stack.pop();
 347  			auto b = stack.pop();
 348  			auto m = stack.pop();
 349  			auto divByZero = m_builder.CreateICmpEQ(m, Constant::get(0));
 350  			a = m_builder.CreateZExt(a, i512Ty);
 351  			b = m_builder.CreateZExt(b, i512Ty);
 352  			m = m_builder.CreateZExt(m, i512Ty);
 353  			auto p = m_builder.CreateNUWMul(a, b);
 354  			p = m_builder.CreateURem(p, m);
 355  			p = m_builder.CreateTrunc(p, Type::Word);
 356  			p = m_builder.CreateSelect(divByZero, Constant::get(0), p);
 357  			stack.push(p);
 358  			break;
 359  		}
 360  
 361  		case Instruction::EXP:
 362  		{
 363  			auto base = stack.pop();
 364  			auto exponent = stack.pop();
 365  			_gasMeter.countExp(exponent);
 366  			auto ret = _arith.exp(base, exponent);
 367  			stack.push(ret);
 368  			break;
 369  		}
 370  
 371  		case Instruction::NOT:
 372  		{
 373  			auto value = stack.pop();
 374  			auto ret = m_builder.CreateXor(value, Constant::get(-1), "bnot");
 375  			stack.push(ret);
 376  			break;
 377  		}
 378  
 379  		case Instruction::LT:
 380  		{
 381  			auto lhs = stack.pop();
 382  			auto rhs = stack.pop();
 383  			auto res1 = m_builder.CreateICmpULT(lhs, rhs);
 384  			auto res256 = m_builder.CreateZExt(res1, Type::Word);
 385  			stack.push(res256);
 386  			break;
 387  		}
 388  
 389  		case Instruction::GT:
 390  		{
 391  			auto lhs = stack.pop();
 392  			auto rhs = stack.pop();
 393  			auto res1 = m_builder.CreateICmpUGT(lhs, rhs);
 394  			auto res256 = m_builder.CreateZExt(res1, Type::Word);
 395  			stack.push(res256);
 396  			break;
 397  		}
 398  
 399  		case Instruction::SLT:
 400  		{
 401  			auto lhs = stack.pop();
 402  			auto rhs = stack.pop();
 403  			auto res1 = m_builder.CreateICmpSLT(lhs, rhs);
 404  			auto res256 = m_builder.CreateZExt(res1, Type::Word);
 405  			stack.push(res256);
 406  			break;
 407  		}
 408  
 409  		case Instruction::SGT:
 410  		{
 411  			auto lhs = stack.pop();
 412  			auto rhs = stack.pop();
 413  			auto res1 = m_builder.CreateICmpSGT(lhs, rhs);
 414  			auto res256 = m_builder.CreateZExt(res1, Type::Word);
 415  			stack.push(res256);
 416  			break;
 417  		}
 418  
 419  		case Instruction::EQ:
 420  		{
 421  			auto lhs = stack.pop();
 422  			auto rhs = stack.pop();
 423  			auto res1 = m_builder.CreateICmpEQ(lhs, rhs);
 424  			auto res256 = m_builder.CreateZExt(res1, Type::Word);
 425  			stack.push(res256);
 426  			break;
 427  		}
 428  
 429  		case Instruction::ISZERO:
 430  		{
 431  			auto top = stack.pop();
 432  			auto iszero = m_builder.CreateICmpEQ(top, Constant::get(0), "iszero");
 433  			auto result = m_builder.CreateZExt(iszero, Type::Word);
 434  			stack.push(result);
 435  			break;
 436  		}
 437  
 438  		case Instruction::AND:
 439  		{
 440  			auto lhs = stack.pop();
 441  			auto rhs = stack.pop();
 442  			auto res = m_builder.CreateAnd(lhs, rhs);
 443  			stack.push(res);
 444  			break;
 445  		}
 446  
 447  		case Instruction::OR:
 448  		{
 449  			auto lhs = stack.pop();
 450  			auto rhs = stack.pop();
 451  			auto res = m_builder.CreateOr(lhs, rhs);
 452  			stack.push(res);
 453  			break;
 454  		}
 455  
 456  		case Instruction::XOR:
 457  		{
 458  			auto lhs = stack.pop();
 459  			auto rhs = stack.pop();
 460  			auto res = m_builder.CreateXor(lhs, rhs);
 461  			stack.push(res);
 462  			break;
 463  		}
 464  
 465  		case Instruction::BYTE:
 466  		{
 467  			const auto idx = stack.pop();
 468  			auto value = Endianness::toBE(m_builder, stack.pop());
 469  
 470  			auto idxValid = m_builder.CreateICmpULT(idx, Constant::get(32), "idxValid");
 471  			auto bytes = m_builder.CreateBitCast(value, llvm::VectorType::get(Type::Byte, 32), "bytes");
 472  			// TODO: Workaround for LLVM bug. Using big value of index causes invalid memory access.
 473  			auto safeIdx = m_builder.CreateTrunc(idx, m_builder.getIntNTy(5));
 474  			// TODO: Workaround for LLVM bug. DAG Builder used sext on index instead of zext
 475  			safeIdx = m_builder.CreateZExt(safeIdx, Type::Size);
 476  			auto byte = m_builder.CreateExtractElement(bytes, safeIdx, "byte");
 477  			value = m_builder.CreateZExt(byte, Type::Word);
 478  			value = m_builder.CreateSelect(idxValid, value, Constant::get(0));
 479  			stack.push(value);
 480  			break;
 481  		}
 482  
 483  		case Instruction::SIGNEXTEND:
 484  		{
 485  			auto idx = stack.pop();
 486  			auto word = stack.pop();
 487  
 488  			auto k32_ = m_builder.CreateTrunc(idx, m_builder.getIntNTy(5), "k_32");
 489  			auto k32 = m_builder.CreateZExt(k32_, Type::Size);
 490  			auto k32x8 = m_builder.CreateMul(k32, m_builder.getInt64(8), "kx8");
 491  
 492  			// test for word >> (k * 8 + 7)
 493  			auto bitpos = m_builder.CreateAdd(k32x8, m_builder.getInt64(7), "bitpos");
 494  			auto bitposEx = m_builder.CreateZExt(bitpos, Type::Word);
 495  			auto bitval = m_builder.CreateLShr(word, bitposEx, "bitval");
 496  			auto bittest = m_builder.CreateTrunc(bitval, Type::Bool, "bittest");
 497  
 498  			auto mask_ = m_builder.CreateShl(Constant::get(1), bitposEx);
 499  			auto mask = m_builder.CreateSub(mask_, Constant::get(1), "mask");
 500  
 501  			auto negmask = m_builder.CreateXor(mask, llvm::ConstantInt::getAllOnesValue(Type::Word), "negmask");
 502  			auto val1 = m_builder.CreateOr(word, negmask);
 503  			auto val0 = m_builder.CreateAnd(word, mask);
 504  
 505  			auto kInRange = m_builder.CreateICmpULE(idx, llvm::ConstantInt::get(Type::Word, 30));
 506  			auto result = m_builder.CreateSelect(kInRange,
 507  												 m_builder.CreateSelect(bittest, val1, val0),
 508  												 word);
 509  			stack.push(result);
 510  			break;
 511  		}
 512  
 513  		case Instruction::SHA3:
 514  		{
 515  			auto inOff = stack.pop();
 516  			auto inSize = stack.pop();
 517  			_memory.require(inOff, inSize);
 518  			_gasMeter.countSha3Data(inSize);
 519  			auto hash = _ext.sha3(inOff, inSize);
 520  			stack.push(hash);
 521  			break;
 522  		}
 523  
 524  		case Instruction::POP:
 525  		{
 526  			stack.pop();
 527  			break;
 528  		}
 529  
 530  		case Instruction::ANY_PUSH:
 531  		{
 532  			auto value = readPushData(it, _basicBlock.end());
 533  			stack.push(Constant::get(value));
 534  			break;
 535  		}
 536  
 537  		case Instruction::ANY_DUP:
 538  		{
 539  			auto index = static_cast<size_t>(inst) - static_cast<size_t>(Instruction::DUP1);
 540  			stack.dup(index);
 541  			break;
 542  		}
 543  
 544  		case Instruction::ANY_SWAP:
 545  		{
 546  			auto index = static_cast<size_t>(inst) - static_cast<size_t>(Instruction::SWAP1) + 1;
 547  			stack.swap(index);
 548  			break;
 549  		}
 550  
 551  		case Instruction::MLOAD:
 552  		{
 553  			auto addr = stack.pop();
 554  			auto word = _memory.loadWord(addr);
 555  			stack.push(word);
 556  			break;
 557  		}
 558  
 559  		case Instruction::MSTORE:
 560  		{
 561  			auto addr = stack.pop();
 562  			auto word = stack.pop();
 563  			_memory.storeWord(addr, word);
 564  			break;
 565  		}
 566  
 567  		case Instruction::MSTORE8:
 568  		{
 569  			auto addr = stack.pop();
 570  			auto word = stack.pop();
 571  			_memory.storeByte(addr, word);
 572  			break;
 573  		}
 574  
 575  		case Instruction::MSIZE:
 576  		{
 577  			auto word = _memory.getSize();
 578  			stack.push(word);
 579  			break;
 580  		}
 581  
 582  		case Instruction::SLOAD:
 583  		{
 584  			auto index = stack.pop();
 585  			auto value = _ext.sload(index);
 586  			stack.push(value);
 587  			break;
 588  		}
 589  
 590  		case Instruction::SSTORE:
 591  		{
 592  			if (m_staticCall)
 593  				goto invalidInstruction;
 594  
 595  			auto index = stack.pop();
 596  			auto value = stack.pop();
 597  			_gasMeter.countSStore(_ext, index, value);
 598  			_ext.sstore(index, value);
 599  			break;
 600  		}
 601  
 602  		case Instruction::JUMP:
 603  		case Instruction::JUMPI:
 604  		{
 605  			auto destIdx = llvm::MDNode::get(m_builder.getContext(), llvm::ValueAsMetadata::get(stack.pop()));
 606  
 607  			// Create branch instruction, initially to jump table.
 608  			// Destination will be optimized with direct jump during jump resolving if destination index is a constant.
 609  			auto jumpInst = (inst == Instruction::JUMP) ?
 610  					m_builder.CreateBr(m_jumpTableBB) :
 611  					m_builder.CreateCondBr(m_builder.CreateICmpNE(stack.pop(), Constant::get(0), "jump.check"), m_jumpTableBB, nullptr);
 612  
 613  			// Attach medatada to branch instruction with information about destination index.
 614  			jumpInst->setMetadata(c_destIdxLabel, destIdx);
 615  			break;
 616  		}
 617  
 618  		case Instruction::JUMPDEST:
 619  		{
 620  			// Add the basic block to the jump table.
 621  			assert(it == _basicBlock.begin() && "JUMPDEST must be the first instruction of a basic block");
 622  			auto jumpTable = llvm::cast<llvm::SwitchInst>(m_jumpTableBB->getTerminator());
 623  			jumpTable->addCase(Constant::get(_basicBlock.firstInstrIdx()), _basicBlock.llvm());
 624  			break;
 625  		}
 626  
 627  		case Instruction::PC:
 628  		{
 629  			auto value = Constant::get(it - _basicBlock.begin() + _basicBlock.firstInstrIdx());
 630  			stack.push(value);
 631  			break;
 632  		}
 633  
 634  		case Instruction::GAS:
 635  		{
 636  			_gasMeter.commitCostBlock();
 637  			stack.push(m_builder.CreateZExt(_runtimeManager.getGas(), Type::Word));
 638  			break;
 639  		}
 640  
 641  		case Instruction::ADDRESS:
 642  			stack.push(Endianness::toNative(m_builder, _runtimeManager.getAddress()));
 643  			break;
 644  		case Instruction::CALLER:
 645  			stack.push(Endianness::toNative(m_builder, _runtimeManager.getSender()));
 646  			break;
 647  		case Instruction::ORIGIN:
 648  			stack.push(m_builder.CreateZExt(Endianness::toNative(m_builder, _runtimeManager.getTxContextItem(1)), Type::Word));
 649  			break;
 650  		case Instruction::COINBASE:
 651  			stack.push(m_builder.CreateZExt(Endianness::toNative(m_builder, _runtimeManager.getTxContextItem(2)), Type::Word));
 652  			break;
 653  
 654  		case Instruction::GASPRICE:
 655  			stack.push(Endianness::toNative(m_builder, _runtimeManager.getTxContextItem(0)));
 656  			break;
 657  
 658  		case Instruction::DIFFICULTY:
 659  			stack.push(Endianness::toNative(m_builder, _runtimeManager.getTxContextItem(6)));
 660  			break;
 661  
 662  		case Instruction::GASLIMIT:
 663  			stack.push(m_builder.CreateZExt(_runtimeManager.getTxContextItem(5), Type::Word));
 664  			break;
 665  
 666  		case Instruction::NUMBER:
 667  			stack.push(m_builder.CreateZExt(_runtimeManager.getTxContextItem(3), Type::Word));
 668  			break;
 669  
 670  		case Instruction::TIMESTAMP:
 671  			stack.push(m_builder.CreateZExt(_runtimeManager.getTxContextItem(4), Type::Word));
 672  			break;
 673  
 674  		case Instruction::CALLVALUE:
 675  		{
 676  			auto beValue = _runtimeManager.getValue();
 677  			stack.push(Endianness::toNative(m_builder, beValue));
 678  			break;
 679  		}
 680  
 681  		case Instruction::CODESIZE:
 682  			stack.push(_runtimeManager.getCodeSize());
 683  			break;
 684  
 685  		case Instruction::CALLDATASIZE:
 686  			stack.push(_runtimeManager.getCallDataSize());
 687  			break;
 688  
 689  		case Instruction::RETURNDATASIZE:
 690  		{
 691  			if (m_rev < EVMC_BYZANTIUM)
 692  				goto invalidInstruction;
 693  
 694  			auto returnBufSizePtr = _runtimeManager.getReturnBufSizePtr();
 695  			auto returnBufSize = m_builder.CreateLoad(returnBufSizePtr);
 696  			stack.push(m_builder.CreateZExt(returnBufSize, Type::Word));
 697  			break;
 698  		}
 699  
 700  		case Instruction::BLOCKHASH:
 701  		{
 702  			auto number = stack.pop();
 703  			// If number bigger than int64 assume the result is 0.
 704  			auto limitC = m_builder.getInt64(std::numeric_limits<int64_t>::max());
 705  			auto limit = m_builder.CreateZExt(limitC, Type::Word);
 706  			auto isBigNumber = m_builder.CreateICmpUGT(number, limit);
 707  			auto hash = _ext.blockHash(number);
 708  			// TODO: Try to eliminate the call if the number is invalid.
 709  			hash = m_builder.CreateSelect(isBigNumber, Constant::get(0), hash);
 710  			stack.push(hash);
 711  			break;
 712  		}
 713  
 714  		case Instruction::BALANCE:
 715  		{
 716  			auto address = stack.pop();
 717  			auto value = _ext.balance(address);
 718  			stack.push(value);
 719  			break;
 720  		}
 721  
 722  		case Instruction::EXTCODESIZE:
 723  		{
 724  			auto addr = stack.pop();
 725  			auto codesize = _ext.extcodesize(addr);
 726  			stack.push(codesize);
 727  			break;
 728  		}
 729  
 730  		case Instruction::CALLDATACOPY:
 731  		{
 732  			auto destMemIdx = stack.pop();
 733  			auto srcIdx = stack.pop();
 734  			auto reqBytes = stack.pop();
 735  
 736  			auto srcPtr = _runtimeManager.getCallData();
 737  			auto srcSize = _runtimeManager.getCallDataSize();
 738  
 739  			_memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
 740  			break;
 741  		}
 742  
 743  		case Instruction::RETURNDATACOPY:
 744  		{
 745  			if (m_rev < EVMC_BYZANTIUM)
 746  				goto invalidInstruction;
 747  
 748  			auto destMemIdx = stack.pop();
 749  			auto srcIdx = stack.pop();
 750  			auto reqBytes = stack.pop();
 751  
 752  			auto srcPtr = m_builder.CreateLoad(_runtimeManager.getReturnBufDataPtr());
 753  			auto srcSize = m_builder.CreateLoad(_runtimeManager.getReturnBufSizePtr());
 754  
 755  			_memory.copyBytesNoPadding(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
 756  			break;
 757  		}
 758  
 759  		case Instruction::CODECOPY:
 760  		{
 761  			auto destMemIdx = stack.pop();
 762  			auto srcIdx = stack.pop();
 763  			auto reqBytes = stack.pop();
 764  
 765  			auto srcPtr = _runtimeManager.getCode();    // TODO: Code & its size are constants, feature #80814234
 766  			auto srcSize = _runtimeManager.getCodeSize();
 767  
 768  			_memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
 769  			break;
 770  		}
 771  
 772  		case Instruction::EXTCODECOPY:
 773  		{
 774  			auto addr = stack.pop();
 775  			auto destMemIdx = stack.pop();
 776  			auto srcIdx = stack.pop();
 777  			auto reqBytes = stack.pop();
 778  
 779  			auto codeRef = _ext.extcode(addr);
 780  
 781  			_memory.copyBytes(codeRef.ptr, codeRef.size, srcIdx, destMemIdx, reqBytes);
 782  			break;
 783  		}
 784  
 785  		case Instruction::CALLDATALOAD:
 786  		{
 787  			auto idx = stack.pop();
 788  			auto value = _ext.calldataload(idx);
 789  			stack.push(value);
 790  			break;
 791  		}
 792  
 793  		case Instruction::CREATE:
 794  		{
 795  			if (m_staticCall)
 796  				goto invalidInstruction;
 797  
 798  			auto endowment = stack.pop();
 799  			auto initOff = stack.pop();
 800  			auto initSize = stack.pop();
 801  			_memory.require(initOff, initSize);
 802  
 803  			_gasMeter.commitCostBlock();
 804  			auto gas = _runtimeManager.getGas();
 805  			llvm::Value* gasKept = (m_rev >= EVMC_TANGERINE_WHISTLE) ?
 806  			                       m_builder.CreateLShr(gas, 6) :
 807  			                       m_builder.getInt64(0);
 808  			auto createGas = m_builder.CreateSub(gas, gasKept, "create.gas", true, true);
 809  			llvm::Value* r = nullptr;
 810  			llvm::Value* pAddr = nullptr;
 811  			std::tie(r, pAddr) = _ext.create(createGas, endowment, initOff, initSize);
 812  
 813  			auto ret =
 814  				m_builder.CreateICmpSGE(r, m_builder.getInt64(0), "create.ret");
 815  			auto rmagic = m_builder.CreateSelect(
 816  				ret, m_builder.getInt64(0), m_builder.getInt64(EVM_CALL_FAILURE),
 817  				"call.rmagic");
 818  			// TODO: optimize
 819  			auto gasLeft = m_builder.CreateSub(r, rmagic, "create.gasleft");
 820  			gas = m_builder.CreateAdd(gasLeft, gasKept);
 821  			_runtimeManager.setGas(gas);
 822  
 823  			llvm::Value* addr = m_builder.CreateLoad(pAddr);
 824  			addr = Endianness::toNative(m_builder, addr);
 825  			addr = m_builder.CreateZExt(addr, Type::Word);
 826  			addr = m_builder.CreateSelect(ret, addr, Constant::get(0));
 827  			stack.push(addr);
 828  			break;
 829  		}
 830  
 831  		case Instruction::CALL:
 832  		case Instruction::CALLCODE:
 833  		case Instruction::DELEGATECALL:
 834  		case Instruction::STATICCALL:
 835  		{
 836  			// Handle invalid instructions.
 837  			if (inst == Instruction::DELEGATECALL && m_rev < EVMC_HOMESTEAD)
 838  				goto invalidInstruction;
 839  
 840  			if (inst == Instruction::STATICCALL && m_rev < EVMC_BYZANTIUM)
 841  				goto invalidInstruction;
 842  
 843  			auto callGas = stack.pop();
 844  			auto address = stack.pop();
 845  			bool hasValue = inst == Instruction::CALL || inst == Instruction::CALLCODE;
 846  			auto value = hasValue ? stack.pop() : Constant::get(0);
 847  
 848  			auto inOff = stack.pop();
 849  			auto inSize = stack.pop();
 850  			auto outOff = stack.pop();
 851  			auto outSize = stack.pop();
 852  
 853  			_gasMeter.commitCostBlock();
 854  
 855  			// Require memory for in and out buffers
 856  			_memory.require(outOff, outSize);  // Out buffer first as we guess
 857  											   // it will be after the in one
 858  			_memory.require(inOff, inSize);
 859  
 860  			auto noTransfer = m_builder.CreateICmpEQ(value, Constant::get(0));
 861  
 862  			// For static call mode, select infinite penalty for CALL with
 863  			// value transfer.
 864  			auto const transferGas = (inst == Instruction::CALL && m_staticCall) ?
 865  				std::numeric_limits<int64_t>::max() :
 866  				JITSchedule::valueTransferGas::value;
 867  
 868  			auto transferCost = m_builder.CreateSelect(
 869  					noTransfer, m_builder.getInt64(0),
 870  					m_builder.getInt64(transferGas));
 871  			_gasMeter.count(transferCost, _runtimeManager.getJmpBuf(),
 872  							_runtimeManager.getGasPtr());
 873  
 874  			if (inst == Instruction::CALL)
 875  			{
 876  				auto accountExists = _ext.exists(address);
 877  				auto noPenaltyCond = accountExists;
 878  				if (m_rev >= EVMC_SPURIOUS_DRAGON)
 879  					noPenaltyCond = m_builder.CreateOr(accountExists, noTransfer);
 880  				auto penalty = m_builder.CreateSelect(noPenaltyCond,
 881  				                                      m_builder.getInt64(0),
 882  				                                      m_builder.getInt64(JITSchedule::callNewAccount::value));
 883  				_gasMeter.count(penalty, _runtimeManager.getJmpBuf(),
 884  				                _runtimeManager.getGasPtr());
 885  			}
 886  
 887  			if (m_rev >= EVMC_TANGERINE_WHISTLE)
 888  			{
 889  				auto gas = _runtimeManager.getGas();
 890  				auto gas64th = m_builder.CreateLShr(gas, 6);
 891  				auto gasMaxAllowed = m_builder.CreateZExt(
 892  						m_builder.CreateSub(gas, gas64th, "gas.maxallowed",
 893  						                    true, true), Type::Word);
 894  				auto cmp = m_builder.CreateICmpUGT(callGas, gasMaxAllowed);
 895  				callGas = m_builder.CreateSelect(cmp, gasMaxAllowed, callGas);
 896  			}
 897  
 898  			_gasMeter.count(callGas, _runtimeManager.getJmpBuf(),
 899  							_runtimeManager.getGasPtr());
 900  			auto stipend = m_builder.CreateSelect(
 901  					noTransfer, m_builder.getInt64(0),
 902  					m_builder.getInt64(JITSchedule::callStipend::value));
 903  			auto gas = m_builder.CreateTrunc(callGas, Type::Gas, "call.gas.declared");
 904  			gas = m_builder.CreateAdd(gas, stipend, "call.gas", true, true);
 905  			int kind = [inst]() -> int
 906  			{
 907  				switch (inst)
 908  				{
 909  				case Instruction::CALL: return EVMC_CALL;
 910  				case Instruction::CALLCODE: return EVMC_CALLCODE;
 911  				case Instruction::DELEGATECALL: return EVMC_DELEGATECALL;
 912  				case Instruction::STATICCALL: return EVM_STATICCALL;
 913  				default: LLVM_BUILTIN_UNREACHABLE;
 914  				}
 915  			}();
 916  			auto r = _ext.call(kind, gas, address, value, inOff, inSize, outOff,
 917  							   outSize);
 918  			auto ret =
 919  				m_builder.CreateICmpSGE(r, m_builder.getInt64(0), "call.ret");
 920  			auto rmagic = m_builder.CreateSelect(
 921  				ret, m_builder.getInt64(0), m_builder.getInt64(EVM_CALL_FAILURE),
 922  				"call.rmagic");
 923  			// TODO: optimize
 924  			auto finalGas = m_builder.CreateSub(r, rmagic, "call.finalgas");
 925  			_gasMeter.giveBack(finalGas);
 926  			stack.push(m_builder.CreateZExt(ret, Type::Word));
 927  			break;
 928  		}
 929  
 930  		case Instruction::RETURN:
 931  		case Instruction::REVERT:
 932  		{
 933  			auto const isRevert = inst == Instruction::REVERT;
 934  			if (isRevert && m_rev < EVMC_BYZANTIUM)
 935  				goto invalidInstruction;
 936  
 937  			auto index = stack.pop();
 938  			auto size = stack.pop();
 939  
 940  			_memory.require(index, size);
 941  			_runtimeManager.registerReturnData(index, size);
 942  
 943  			_runtimeManager.exit(isRevert ? ReturnCode::Revert : ReturnCode::Return);
 944  			break;
 945  		}
 946  
 947  		case Instruction::SUICIDE:
 948  		{
 949  			if (m_staticCall)
 950  				goto invalidInstruction;
 951  
 952  			auto dest = stack.pop();
 953  			if (m_rev >= EVMC_TANGERINE_WHISTLE)
 954  			{
 955  				auto destExists = _ext.exists(dest);
 956  				auto noPenaltyCond = destExists;
 957  				if (m_rev >= EVMC_SPURIOUS_DRAGON)
 958  				{
 959  					auto addr = Endianness::toNative(m_builder, _runtimeManager.getAddress());
 960  					auto balance = _ext.balance(addr);
 961  					auto noTransfer = m_builder.CreateICmpEQ(balance,
 962  					                                         Constant::get(0));
 963  					noPenaltyCond = m_builder.CreateOr(destExists, noTransfer);
 964  				}
 965  				auto penalty = m_builder.CreateSelect(
 966  						noPenaltyCond, m_builder.getInt64(0),
 967  						m_builder.getInt64(JITSchedule::callNewAccount::value));
 968  				_gasMeter.count(penalty, _runtimeManager.getJmpBuf(),
 969  				                _runtimeManager.getGasPtr());
 970  			}
 971  			_ext.selfdestruct(dest);
 972  		}
 973  			FALLTHROUGH;
 974  		case Instruction::STOP:
 975  			_runtimeManager.exit(ReturnCode::Stop);
 976  			break;
 977  
 978  		case Instruction::LOG0:
 979  		case Instruction::LOG1:
 980  		case Instruction::LOG2:
 981  		case Instruction::LOG3:
 982  		case Instruction::LOG4:
 983  		{
 984  			if (m_staticCall)
 985  				goto invalidInstruction;
 986  
 987  			auto beginIdx = stack.pop();
 988  			auto numBytes = stack.pop();
 989  			_memory.require(beginIdx, numBytes);
 990  
 991  			// This will commit the current cost block
 992  			_gasMeter.countLogData(numBytes);
 993  
 994  			llvm::SmallVector<llvm::Value*, 4> topics;
 995  			auto numTopics = static_cast<size_t>(inst) - static_cast<size_t>(Instruction::LOG0);
 996  			for (size_t i = 0; i < numTopics; ++i)
 997  				topics.emplace_back(stack.pop());
 998  
 999  			_ext.log(beginIdx, numBytes, topics);
1000  			break;
1001  		}
1002  
1003  		invalidInstruction:
1004  		default: // Invalid instruction - abort
1005  			_runtimeManager.exit(ReturnCode::OutOfGas);
1006  			it = _basicBlock.end() - 1; // finish block compilation
1007  		}
1008  	}
1009  
1010  	_gasMeter.commitCostBlock();
1011  
1012  	stack.finalize();
1013  }
1014  
1015  
1016  }
1017  }
1018  }