mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-20 03:36:44 +00:00
Implemented ADDMOD, MULMOD and GAS [Delivers #80566276]
This commit is contained in:
parent
9b3c446578
commit
6caff31697
7 changed files with 64 additions and 1 deletions
|
|
@ -434,6 +434,36 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
|||
break;
|
||||
}
|
||||
|
||||
case Instruction::ADDMOD:
|
||||
{
|
||||
auto val1 = stack.pop();
|
||||
auto val2 = stack.pop();
|
||||
auto sum = builder.CreateAdd(val1, val2);
|
||||
auto mod = stack.pop();
|
||||
|
||||
auto sum128 = builder.CreateTrunc(sum, Type::lowPrecision);
|
||||
auto mod128 = builder.CreateTrunc(mod, Type::lowPrecision);
|
||||
auto res128 = builder.CreateURem(sum128, mod128);
|
||||
auto res256 = builder.CreateZExt(res128, Type::i256);
|
||||
stack.push(res256);
|
||||
break;
|
||||
}
|
||||
|
||||
case Instruction::MULMOD:
|
||||
{
|
||||
auto val1 = stack.pop();
|
||||
auto val2 = stack.pop();
|
||||
auto prod = builder.CreateMul(val1, val2);
|
||||
auto mod = stack.pop();
|
||||
|
||||
auto prod128 = builder.CreateTrunc(prod, Type::lowPrecision);
|
||||
auto mod128 = builder.CreateTrunc(mod, Type::lowPrecision);
|
||||
auto res128 = builder.CreateURem(prod128, mod128);
|
||||
auto res256 = builder.CreateZExt(res128, Type::i256);
|
||||
stack.push(res256);
|
||||
break;
|
||||
}
|
||||
|
||||
case Instruction::SHA3:
|
||||
{
|
||||
auto inOff = stack.pop();
|
||||
|
|
|
|||
|
|
@ -130,4 +130,9 @@ void GasMeter::checkMemory(llvm::Value* _additionalMemoryInWords, llvm::IRBuilde
|
|||
_builder.CreateCall(m_gasCheckFunc, cost);
|
||||
}
|
||||
|
||||
}
|
||||
llvm::GlobalVariable* GasMeter::getLLVMGasVar()
|
||||
{
|
||||
return m_gas;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ public:
|
|||
/// Generate code that checks the cost of additional memory used by program
|
||||
void checkMemory(llvm::Value* _additionalMemoryInWords, llvm::IRBuilder<>& _builder);
|
||||
|
||||
llvm::GlobalVariable* getLLVMGasVar();
|
||||
|
||||
private:
|
||||
/// Cumulative gas cost of a block of instructions
|
||||
/// @TODO Handle overflow
|
||||
|
|
|
|||
1
evmcc/test/arith/addmod.evm
Normal file
1
evmcc/test/arith/addmod.evm
Normal file
|
|
@ -0,0 +1 @@
|
|||
60646107b760271460005560006001f2
|
||||
12
evmcc/test/arith/addmod.lll
Normal file
12
evmcc/test/arith/addmod.lll
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
;; Should return (1975 + 39) `mod` 100 = 14 = 0x0e
|
||||
(asm
|
||||
100
|
||||
1975
|
||||
39
|
||||
ADDMOD
|
||||
0
|
||||
MSTORE8
|
||||
0
|
||||
1
|
||||
RETURN
|
||||
)
|
||||
1
evmcc/test/arith/mulmod.evm
Normal file
1
evmcc/test/arith/mulmod.evm
Normal file
|
|
@ -0,0 +1 @@
|
|||
6064601b60251560005560006001f2
|
||||
12
evmcc/test/arith/mulmod.lll
Normal file
12
evmcc/test/arith/mulmod.lll
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
;; Should return (27 * 37) `mod` 100 = 99 = 0x63
|
||||
(asm
|
||||
100
|
||||
27
|
||||
37
|
||||
MULMOD
|
||||
0
|
||||
MSTORE8
|
||||
0
|
||||
1
|
||||
RETURN
|
||||
)
|
||||
Loading…
Reference in a new issue