EVM Code for simple accessor function is properly generated

This commit is contained in:
Lefteris Karapetsas 2015-01-27 16:55:06 +01:00
parent ff91ab96ea
commit 2947e038d2
4 changed files with 20 additions and 15 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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