|
@ -59,6 +59,7 @@ void ExpressionCompiler::appendStateVariableAccessor(CompilerContext& _context, |
|
|
bool ExpressionCompiler::visit(Assignment const& _assignment) |
|
|
bool ExpressionCompiler::visit(Assignment const& _assignment) |
|
|
{ |
|
|
{ |
|
|
_assignment.getRightHandSide().accept(*this); |
|
|
_assignment.getRightHandSide().accept(*this); |
|
|
|
|
|
if (_assignment.getType()->isValueType()) |
|
|
appendTypeConversion(*_assignment.getRightHandSide().getType(), *_assignment.getType()); |
|
|
appendTypeConversion(*_assignment.getRightHandSide().getType(), *_assignment.getType()); |
|
|
_assignment.getLeftHandSide().accept(*this); |
|
|
_assignment.getLeftHandSide().accept(*this); |
|
|
solAssert(m_currentLValue.isValid(), "LValue not retrieved."); |
|
|
solAssert(m_currentLValue.isValid(), "LValue not retrieved."); |
|
@ -66,6 +67,7 @@ bool ExpressionCompiler::visit(Assignment const& _assignment) |
|
|
Token::Value op = _assignment.getAssignmentOperator(); |
|
|
Token::Value op = _assignment.getAssignmentOperator(); |
|
|
if (op != Token::Assign) // compound assignment
|
|
|
if (op != Token::Assign) // compound assignment
|
|
|
{ |
|
|
{ |
|
|
|
|
|
solAssert(_assignment.getType()->isValueType(), "Compound operators not implemented for non-value types."); |
|
|
if (m_currentLValue.storesReferenceOnStack()) |
|
|
if (m_currentLValue.storesReferenceOnStack()) |
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2; |
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2; |
|
|
m_currentLValue.retrieveValue(_assignment.getType(), _assignment.getLocation(), true); |
|
|
m_currentLValue.retrieveValue(_assignment.getType(), _assignment.getLocation(), true); |
|
@ -73,7 +75,7 @@ bool ExpressionCompiler::visit(Assignment const& _assignment) |
|
|
if (m_currentLValue.storesReferenceOnStack()) |
|
|
if (m_currentLValue.storesReferenceOnStack()) |
|
|
m_context << eth::Instruction::SWAP1; |
|
|
m_context << eth::Instruction::SWAP1; |
|
|
} |
|
|
} |
|
|
m_currentLValue.storeValue(_assignment); |
|
|
m_currentLValue.storeValue(_assignment, *_assignment.getRightHandSide().getType()); |
|
|
m_currentLValue.reset(); |
|
|
m_currentLValue.reset(); |
|
|
|
|
|
|
|
|
return false; |
|
|
return false; |
|
@ -126,7 +128,7 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation) |
|
|
// Stack for postfix: *ref [ref] (*ref)+-1
|
|
|
// Stack for postfix: *ref [ref] (*ref)+-1
|
|
|
if (m_currentLValue.storesReferenceOnStack()) |
|
|
if (m_currentLValue.storesReferenceOnStack()) |
|
|
m_context << eth::Instruction::SWAP1; |
|
|
m_context << eth::Instruction::SWAP1; |
|
|
m_currentLValue.storeValue(_unaryOperation, !_unaryOperation.isPrefixOperation()); |
|
|
m_currentLValue.storeValue(_unaryOperation, *_unaryOperation.getType(), !_unaryOperation.isPrefixOperation()); |
|
|
m_currentLValue.reset(); |
|
|
m_currentLValue.reset(); |
|
|
break; |
|
|
break; |
|
|
case Token::Add: // +
|
|
|
case Token::Add: // +
|
|
@ -472,6 +474,10 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) |
|
|
m_context << eth::Instruction::GAS; |
|
|
m_context << eth::Instruction::GAS; |
|
|
else if (member == "gasprice") |
|
|
else if (member == "gasprice") |
|
|
m_context << eth::Instruction::GASPRICE; |
|
|
m_context << eth::Instruction::GASPRICE; |
|
|
|
|
|
else if (member == "data") |
|
|
|
|
|
{ |
|
|
|
|
|
// nothing to store on the stack
|
|
|
|
|
|
} |
|
|
else |
|
|
else |
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown magic member.")); |
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown magic member.")); |
|
|
break; |
|
|
break; |
|
@ -1014,7 +1020,7 @@ void ExpressionCompiler::LValue::retrieveValueFromStorage(TypePointer const& _ty |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool _move) const |
|
|
void ExpressionCompiler::LValue::storeValue(Expression const& _expression, Type const& _sourceType, bool _move) const |
|
|
{ |
|
|
{ |
|
|
switch (m_type) |
|
|
switch (m_type) |
|
|
{ |
|
|
{ |
|
@ -1032,9 +1038,9 @@ void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
case LValueType::Storage: |
|
|
case LValueType::Storage: |
|
|
if (!_expression.getType()->isValueType()) |
|
|
|
|
|
break; // no distinction between value and reference for non-value types
|
|
|
|
|
|
// stack layout: value value ... value ref
|
|
|
// stack layout: value value ... value ref
|
|
|
|
|
|
if (_expression.getType()->isValueType()) |
|
|
|
|
|
{ |
|
|
if (!_move) // copy values
|
|
|
if (!_move) // copy values
|
|
|
{ |
|
|
{ |
|
|
if (m_size + 1 > 16) |
|
|
if (m_size + 1 > 16) |
|
@ -1055,6 +1061,23 @@ void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool |
|
|
<< eth::Instruction::SSTORE |
|
|
<< eth::Instruction::SSTORE |
|
|
<< u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB; |
|
|
<< u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
solAssert(!_move, "Move assign for non-value types not implemented."); |
|
|
|
|
|
solAssert(_sourceType.getCategory() == _expression.getType()->getCategory(), ""); |
|
|
|
|
|
if (_expression.getType()->getCategory() == Type::Category::ByteArray) |
|
|
|
|
|
copyByteArrayToStorage(dynamic_cast<ByteArrayType const&>(*_expression.getType()), |
|
|
|
|
|
dynamic_cast<ByteArrayType const&>(_sourceType)); |
|
|
|
|
|
else if (_expression.getType()->getCategory() == Type::Category::Struct) |
|
|
|
|
|
{ |
|
|
|
|
|
//@todo
|
|
|
|
|
|
solAssert(false, "Struct copy not yet implemented."); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation()) |
|
|
|
|
|
<< errinfo_comment("Invalid non-value type for assignment.")); |
|
|
|
|
|
} |
|
|
break; |
|
|
break; |
|
|
case LValueType::Memory: |
|
|
case LValueType::Memory: |
|
|
if (!_expression.getType()->isValueType()) |
|
|
if (!_expression.getType()->isValueType()) |
|
@ -1145,5 +1168,60 @@ void ExpressionCompiler::LValue::fromIdentifier(Identifier const& _identifier, D |
|
|
<< errinfo_comment("Identifier type not supported or identifier not found.")); |
|
|
<< errinfo_comment("Identifier type not supported or identifier not found.")); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ExpressionCompiler::LValue::copyByteArrayToStorage(ByteArrayType const& _targetType, |
|
|
|
|
|
ByteArrayType const& _sourceType) const |
|
|
|
|
|
{ |
|
|
|
|
|
// stack layout: [source_ref] target_ref (head)
|
|
|
|
|
|
// need to leave target_ref on the stack at the end
|
|
|
|
|
|
solAssert(m_type == LValueType::Storage, ""); |
|
|
|
|
|
solAssert(_targetType.getLocation() == ByteArrayType::Location::Storage, ""); |
|
|
|
|
|
switch (_sourceType.getLocation()) |
|
|
|
|
|
{ |
|
|
|
|
|
case ByteArrayType::Location::CallData: |
|
|
|
|
|
{ |
|
|
|
|
|
// @todo this does not take length into account. It also assumes that after "CALLDATALENGTH" we only have zeros.
|
|
|
|
|
|
// add some useful constants
|
|
|
|
|
|
*m_context << u256(32) << u256(1); |
|
|
|
|
|
// stack here: target_ref 32 1
|
|
|
|
|
|
// store length (in bytes)
|
|
|
|
|
|
if (_sourceType.getOffset() == 0) |
|
|
|
|
|
*m_context << eth::Instruction::CALLDATASIZE; |
|
|
|
|
|
else |
|
|
|
|
|
*m_context << _sourceType.getOffset() << eth::Instruction::CALLDATASIZE << eth::Instruction::SUB; |
|
|
|
|
|
*m_context << eth::Instruction::DUP1 << eth::Instruction::DUP5 << eth::Instruction::SSTORE; |
|
|
|
|
|
// jump to end if length is zero
|
|
|
|
|
|
*m_context << eth::Instruction::ISZERO; |
|
|
|
|
|
eth::AssemblyItem loopEnd = m_context->newTag(); |
|
|
|
|
|
m_context->appendConditionalJumpTo(loopEnd); |
|
|
|
|
|
// actual array data is stored at SHA3(storage_offset)
|
|
|
|
|
|
*m_context << eth::Instruction::DUP3; |
|
|
|
|
|
CompilerUtils(*m_context).storeInMemory(0); |
|
|
|
|
|
*m_context << u256(32) << u256(0) << eth::Instruction::SHA3; |
|
|
|
|
|
|
|
|
|
|
|
*m_context << _sourceType.getOffset(); |
|
|
|
|
|
// stack now: target_ref 32 1 target_data_ref calldata_offset
|
|
|
|
|
|
eth::AssemblyItem loopStart = m_context->newTag(); |
|
|
|
|
|
*m_context << loopStart |
|
|
|
|
|
// copy from calldata and store
|
|
|
|
|
|
<< eth::Instruction::DUP1 << eth::Instruction::CALLDATALOAD |
|
|
|
|
|
<< eth::Instruction::DUP3 << eth::Instruction::SSTORE |
|
|
|
|
|
// increment target_data_ref by 1
|
|
|
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::DUP3 << eth::Instruction::ADD |
|
|
|
|
|
// increment calldata_offset by 32
|
|
|
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::DUP4 << eth::Instruction::ADD |
|
|
|
|
|
// check for loop condition
|
|
|
|
|
|
<< eth::Instruction::DUP1 << eth::Instruction::CALLDATASIZE << eth::Instruction::GT; |
|
|
|
|
|
m_context->appendConditionalJumpTo(loopStart); |
|
|
|
|
|
*m_context << eth::Instruction::POP << eth::Instruction::POP; |
|
|
|
|
|
*m_context << loopEnd << eth::Instruction::POP << eth::Instruction::POP; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case ByteArrayType::Location::Storage: |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
solAssert(false, "Byte array location not implemented."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|