Browse Source

EVM Code for simple accessor function is properly generated

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
309ffb8948
  1. 12
      libsolidity/Compiler.cpp
  2. 2
      libsolidity/Compiler.h
  3. 15
      libsolidity/ExpressionCompiler.cpp
  4. 6
      libsolidity/ExpressionCompiler.h
  5. 12
      test/SolidityEndToEndTest.cpp

12
libsolidity/Compiler.cpp

@ -292,21 +292,19 @@ void Compiler::registerStateVariables(ContractDefinition const& _contract)
m_context.addStateVariable(*variable);
}
bool Compiler::generateAccessorCode(VariableDeclaration const& _varDecl)
void Compiler::generateAccessorCode(VariableDeclaration const& _varDecl)
{
m_context.startNewFunction();
m_returnTag = m_context.newTag();
m_breakTags.clear();
m_continueTags.clear();
// TODO: Work in progress
m_context << m_context.getFunctionEntryLabel(_varDecl);
// CompilerUtils(m_context).moveToStackVariable(firstVariable);
m_context.appendJumpTo(m_returnTag);
m_context << m_returnTag;
ExpressionCompiler::appendStateVariableAccessor(m_context, &_varDecl);
// TODO: perhaps return void if there are no checks?
return true;
uint64_t foo = uint64_t(_varDecl.getType()->getStorageSize());
m_context << eth::dupInstruction(foo + 1);
m_context << eth::Instruction::JUMP;
}
bool Compiler::visit(FunctionDefinition const& _function)

2
libsolidity/Compiler.h

@ -63,7 +63,7 @@ private:
void registerStateVariables(ContractDefinition const& _contract);
bool generateAccessorCode(VariableDeclaration const& _varDecl);
void generateAccessorCode(VariableDeclaration const& _varDecl);
virtual bool visit(FunctionDefinition const& _function) override;
virtual bool visit(IfStatement const& _ifStatement) override;

15
libsolidity/ExpressionCompiler.cpp

@ -48,6 +48,12 @@ void ExpressionCompiler::appendTypeConversion(CompilerContext& _context, Type co
compiler.appendTypeConversion(_typeOnStack, _targetType, _cleanupNeeded);
}
void ExpressionCompiler::appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const* _varDecl, bool _optimize)
{
ExpressionCompiler compiler(_context, _optimize);
compiler.appendStateVariableAccessor(_varDecl);
}
bool ExpressionCompiler::visit(Assignment const& _assignment)
{
_assignment.getRightHandSide().accept(*this);
@ -792,8 +798,7 @@ unsigned ExpressionCompiler::appendArgumentCopyToMemory(TypePointers const& _typ
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const* _varDecl)
{
m_currentLValue.fromStateVariable(*_varDecl, _varDecl->getType());
// TODO
// m_currentLValue.retrieveValueFromStorage();
m_currentLValue.retrieveValueFromStorage(_varDecl->getType(), true);
}
ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType,
@ -823,7 +828,7 @@ void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bo
break;
}
case STORAGE:
retrieveValueFromStorage(_expression, _remove);
retrieveValueFromStorage(_expression.getType(), _remove);
break;
case MEMORY:
if (!_expression.getType()->isValueType())
@ -838,9 +843,9 @@ void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bo
}
}
void ExpressionCompiler::LValue::retrieveValueFromStorage(Expression const& _expression, bool _remove) const
void ExpressionCompiler::LValue::retrieveValueFromStorage(std::shared_ptr<Type const> const& _type, bool _remove) const
{
if (!_expression.getType()->isValueType())
if (!_type->isValueType())
return; // no distinction between value and reference for non-value types
if (!_remove)
*m_context << eth::Instruction::DUP1;

6
libsolidity/ExpressionCompiler.h

@ -53,6 +53,8 @@ public:
/// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type.
static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack,
Type const& _targetType, bool _cleanupNeeded = false);
/// Appends code for a State Variable accessor function
static void appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const* _varDecl, bool _optimize = false);
private:
explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false):
@ -130,8 +132,8 @@ private:
/// also removes the reference from the stack (note that is does not reset the type to @a NONE).
/// @a _expression is the current expression, used for error reporting.
void retrieveValue(Expression const& _expression, bool _remove = false) const;
/// Convenience function to retrive Value from Storage. Specific version of @ref retrieveValue
void retrieveValueFromStorage(Expression const& _expression, bool _remove = false) const;
/// Convenience function to retrieve Value from Storage. Specific version of @ref retrieveValue
void retrieveValueFromStorage(std::shared_ptr<Type const> const& _type, bool _remove = false) const;
/// Stores a value (from the stack directly beneath the reference, which is assumed to
/// be on the top of the stack, if any) in the lvalue and removes the reference.
/// Also removes the stored value from the stack if @a _move is

12
test/SolidityEndToEndTest.cpp

@ -882,6 +882,18 @@ BOOST_AUTO_TEST_CASE(constructor)
testSolidityAgainstCpp("get(uint256)", get, u256(7));
}
BOOST_AUTO_TEST_CASE(simple_accessor)
{
char const* sourceCode = "contract test {\n"
" uint256 data;\n"
" function test() {\n"
" data = 8;\n"
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("data()") == encodeArgs(8));
}
BOOST_AUTO_TEST_CASE(balance)
{
char const* sourceCode = "contract test {\n"

Loading…
Cancel
Save