mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-20 11:46:44 +00:00
fixed implementation of JUMPI (cond casted to bool)
This commit is contained in:
parent
1d17da1e5f
commit
ad2c4c4274
1 changed files with 37 additions and 39 deletions
|
|
@ -169,6 +169,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
// Create the basic blocks.
|
// Create the basic blocks.
|
||||||
auto entryBlock = BasicBlock::Create(context, "entry", mainFunc);
|
auto entryBlock = BasicBlock::Create(context, "entry", mainFunc);
|
||||||
basicBlocks[0] = entryBlock;
|
basicBlocks[0] = entryBlock;
|
||||||
|
builder.SetInsertPoint(entryBlock);
|
||||||
createBasicBlocks(bytecode);
|
createBasicBlocks(bytecode);
|
||||||
|
|
||||||
// Init runtime structures.
|
// Init runtime structures.
|
||||||
|
|
@ -179,7 +180,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
auto userRet = false;
|
auto userRet = false;
|
||||||
auto finished = false;
|
auto finished = false;
|
||||||
|
|
||||||
BasicBlock* currentBlock = nullptr;
|
BasicBlock* currentBlock = entryBlock;
|
||||||
|
|
||||||
for (auto pc = bytecode.cbegin(); pc != bytecode.cend(); ++pc)
|
for (auto pc = bytecode.cbegin(); pc != bytecode.cend(); ++pc)
|
||||||
{
|
{
|
||||||
|
|
@ -188,17 +189,13 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
ProgramCounter currentPC = pc - bytecode.cbegin();
|
ProgramCounter currentPC = pc - bytecode.cbegin();
|
||||||
|
|
||||||
auto blockIter = basicBlocks.find(currentPC);
|
auto blockIter = basicBlocks.find(currentPC);
|
||||||
if (blockIter != basicBlocks.end())
|
if (currentPC > 0 && blockIter != basicBlocks.end())
|
||||||
{
|
{
|
||||||
auto nextBlock = blockIter->second;
|
auto nextBlock = blockIter->second;
|
||||||
if (currentBlock != nullptr)
|
// Terminate the current block by jumping to the next one.
|
||||||
{
|
builder.CreateBr(nextBlock);
|
||||||
// Terminate the current block by jumping to the next one.
|
|
||||||
builder.CreateBr(nextBlock);
|
|
||||||
}
|
|
||||||
// Insert the next block into the main function.
|
// Insert the next block into the main function.
|
||||||
if (nextBlock != entryBlock)
|
mainFunc->getBasicBlockList().push_back(nextBlock);
|
||||||
mainFunc->getBasicBlockList().push_back(nextBlock);
|
|
||||||
builder.SetInsertPoint(nextBlock);
|
builder.SetInsertPoint(nextBlock);
|
||||||
currentBlock = nextBlock;
|
currentBlock = nextBlock;
|
||||||
}
|
}
|
||||||
|
|
@ -209,36 +206,6 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
switch (inst)
|
switch (inst)
|
||||||
{
|
{
|
||||||
|
|
||||||
case Instruction::JUMP:
|
|
||||||
{
|
|
||||||
// The target address is computed at compile time,
|
|
||||||
// just pop it without looking...
|
|
||||||
stack.pop();
|
|
||||||
|
|
||||||
auto targetBlock = jumpTargets[currentPC];
|
|
||||||
builder.CreateBr(targetBlock);
|
|
||||||
|
|
||||||
currentBlock = nullptr;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Instruction::JUMPI:
|
|
||||||
{
|
|
||||||
assert(pc + 1 < bytecode.cend());
|
|
||||||
|
|
||||||
// The target address is computed at compile time,
|
|
||||||
// just pop it without looking...
|
|
||||||
stack.pop();
|
|
||||||
|
|
||||||
auto cond = stack.pop();
|
|
||||||
auto targetBlock = jumpTargets[currentPC];
|
|
||||||
auto followBlock = basicBlocks[currentPC + 1];
|
|
||||||
builder.CreateCondBr(cond, targetBlock, followBlock);
|
|
||||||
|
|
||||||
currentBlock = nullptr;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Instruction::ADD:
|
case Instruction::ADD:
|
||||||
{
|
{
|
||||||
auto lhs = stack.pop();
|
auto lhs = stack.pop();
|
||||||
|
|
@ -464,6 +431,37 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case Instruction::JUMP:
|
||||||
|
{
|
||||||
|
// The target address is computed at compile time,
|
||||||
|
// just pop it without looking...
|
||||||
|
stack.pop();
|
||||||
|
|
||||||
|
auto targetBlock = jumpTargets[currentPC];
|
||||||
|
builder.CreateBr(targetBlock);
|
||||||
|
|
||||||
|
currentBlock = nullptr;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Instruction::JUMPI:
|
||||||
|
{
|
||||||
|
assert(pc + 1 < bytecode.cend());
|
||||||
|
|
||||||
|
// The target address is computed at compile time,
|
||||||
|
// just pop it without looking...
|
||||||
|
stack.pop();
|
||||||
|
|
||||||
|
auto top = stack.pop();
|
||||||
|
auto cond = builder.CreateTrunc(top, builder.getInt1Ty(), "cond");
|
||||||
|
auto targetBlock = jumpTargets[currentPC];
|
||||||
|
auto followBlock = basicBlocks[currentPC + 1];
|
||||||
|
builder.CreateCondBr(cond, targetBlock, followBlock);
|
||||||
|
|
||||||
|
currentBlock = nullptr;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case Instruction::ADDRESS:
|
case Instruction::ADDRESS:
|
||||||
{
|
{
|
||||||
auto value = ext.address();
|
auto value = ext.address();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue