mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 04:06:44 +00:00
Generate gas checking function
This commit is contained in:
parent
97644d660a
commit
04cf0cfcea
4 changed files with 17 additions and 4 deletions
|
|
@ -57,15 +57,23 @@ GasMeter::GasMeter(llvm::IRBuilder<>& _builder, llvm::Module* _module):
|
||||||
m_gas = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(Type::i256), "gas");
|
m_gas = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(Type::i256), "gas");
|
||||||
m_gas->setUnnamedAddr(true); // Address is not important
|
m_gas->setUnnamedAddr(true); // Address is not important
|
||||||
|
|
||||||
//llvm::Function::Create()
|
auto pt = m_builder.GetInsertPoint();
|
||||||
|
auto bb = m_builder.GetInsertBlock();
|
||||||
|
m_gasCheckFunc = llvm::Function::Create(llvm::FunctionType::get(Type::Void, Type::i256, false), llvm::Function::PrivateLinkage, "gas.check", _module);
|
||||||
|
auto gasCheckBB = llvm::BasicBlock::Create(_builder.getContext(), {}, m_gasCheckFunc);
|
||||||
|
m_builder.SetInsertPoint(gasCheckBB);
|
||||||
|
llvm::Value* cost = m_gasCheckFunc->arg_begin();
|
||||||
|
llvm::Value* gas = m_builder.CreateLoad(m_gas);
|
||||||
|
gas = m_builder.CreateSub(gas, cost);
|
||||||
|
m_builder.CreateStore(gas, m_gas);
|
||||||
|
m_builder.CreateRetVoid();
|
||||||
|
m_builder.SetInsertPoint(bb, pt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GasMeter::check(Instruction _inst)
|
void GasMeter::check(Instruction _inst)
|
||||||
{
|
{
|
||||||
auto stepCost = getStepCost(_inst);
|
auto stepCost = getStepCost(_inst);
|
||||||
auto before = m_builder.CreateLoad(m_gas, "gas.before");
|
m_builder.CreateCall(m_gasCheckFunc, m_builder.getIntN(256, stepCost));
|
||||||
auto after = m_builder.CreateSub(before, m_builder.getIntN(256, stepCost));
|
|
||||||
m_builder.CreateStore(after, m_gas);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -21,6 +21,7 @@ public:
|
||||||
private:
|
private:
|
||||||
llvm::IRBuilder<>& m_builder;
|
llvm::IRBuilder<>& m_builder;
|
||||||
llvm::GlobalVariable* m_gas;
|
llvm::GlobalVariable* m_gas;
|
||||||
|
llvm::Function* m_gasCheckFunc;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -8,11 +8,13 @@ namespace evmcc
|
||||||
|
|
||||||
llvm::Type* Type::i256;
|
llvm::Type* Type::i256;
|
||||||
llvm::Type* Type::lowPrecision;
|
llvm::Type* Type::lowPrecision;
|
||||||
|
llvm::Type* Type::Void;
|
||||||
|
|
||||||
void Type::init(llvm::LLVMContext& _context)
|
void Type::init(llvm::LLVMContext& _context)
|
||||||
{
|
{
|
||||||
i256 = llvm::Type::getIntNTy(_context, 256);
|
i256 = llvm::Type::getIntNTy(_context, 256);
|
||||||
lowPrecision = llvm::Type::getInt64Ty(_context);
|
lowPrecision = llvm::Type::getInt64Ty(_context);
|
||||||
|
Void = llvm::Type::getVoidTy(_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +14,8 @@ struct Type
|
||||||
/// @TODO: Use 64-bit for now. In 128-bit compiler-rt library functions are required
|
/// @TODO: Use 64-bit for now. In 128-bit compiler-rt library functions are required
|
||||||
static llvm::Type* lowPrecision;
|
static llvm::Type* lowPrecision;
|
||||||
|
|
||||||
|
static llvm::Type* Void;
|
||||||
|
|
||||||
static void init(llvm::LLVMContext& _context);
|
static void init(llvm::LLVMContext& _context);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue