Skip to content

Commit

Permalink
Change alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Nov 16, 2016
1 parent 679ea28 commit ff3553a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ TypePointer FunctionType::encodingType() const
TypePointer FunctionType::interfaceType(bool /*_inLibrary*/) const
{
if (m_location == Location::External)
return make_shared<IntegerType>(8 * storageBytes());
return make_shared<FixedBytesType>(storageBytes());
else
return TypePointer();
}
Expand Down
58 changes: 35 additions & 23 deletions libsolidity/codegen/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound
)
{
solAssert(_padToWordBoundaries, "Non-padded store for function not implemented.");
combineExternalFunctionType();
combineExternalFunctionType(true);
m_context << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(_padToWordBoundaries ? 32 : 24) << Instruction::ADD;
}
Expand Down Expand Up @@ -315,19 +315,29 @@ void CompilerUtils::memoryCopy()
m_context << Instruction::POP; // ignore return value
}

void CompilerUtils::splitExternalFunctionType()
void CompilerUtils::splitExternalFunctionType(bool _leftAligned)
{
// We have to split the right-aligned <function identifier><address> into two stack slots:
// We have to split the left-aligned <function identifier><address> into two stack slots:
// address (right aligned), function identifier (right aligned)
if (_leftAligned)
m_context << (u256(1) << 64) << Instruction::SWAP1 << Instruction::DIV;
m_context << Instruction::DUP1 << ((u256(1) << 160) - 1) << Instruction::AND << Instruction::SWAP1;
m_context << (u256(1) << 160) << Instruction::SWAP1 << Instruction::DIV;
m_context << u256(0xffffffffUL) << Instruction::AND;
if (!_leftAligned)
m_context << u256(0xffffffffUL) << Instruction::AND;
}

void CompilerUtils::combineExternalFunctionType()
void CompilerUtils::combineExternalFunctionType(bool _leftAligned)
{
m_context << u256(0xffffffffUL) << Instruction::AND << (u256(1) << 160) << Instruction::MUL << Instruction::SWAP1;
m_context << ((u256(1) << 160) - 1) << Instruction::AND << Instruction::OR;
if (_leftAligned)
m_context << (u256(1) << 224);
else
m_context << u256(0xffffffffUL) << Instruction::AND << (u256(1) << 160);
m_context << Instruction::MUL << Instruction::SWAP1;
m_context << ((u256(1) << 160) - 1) << Instruction::AND;
if (_leftAligned)
m_context << (u256(1) << 64) << Instruction::MUL;
m_context << Instruction::OR;
}

void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
Expand Down Expand Up @@ -856,27 +866,29 @@ void CompilerUtils::storeStringData(bytesConstRef _data)
unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCalldata, bool _padToWordBoundaries)
{
unsigned numBytes = _type.calldataEncodedSize(_padToWordBoundaries);
bool leftAligned = _type.category() == Type::Category::FixedBytes;
bool isExternalFunctionType = false;
if (auto const* funType = dynamic_cast<FunctionType const*>(&_type))
if (funType->location() == FunctionType::Location::External)
isExternalFunctionType = true;
if (numBytes == 0)
{
m_context << Instruction::POP << u256(0);
else
return numBytes;
}
solAssert(numBytes <= 32, "Static memory load of more than 32 bytes requested.");
m_context << (_fromCalldata ? Instruction::CALLDATALOAD : Instruction::MLOAD);
if (isExternalFunctionType)
splitExternalFunctionType(true);
else if (numBytes != 32)
{
solAssert(numBytes <= 32, "Static memory load of more than 32 bytes requested.");
m_context << (_fromCalldata ? Instruction::CALLDATALOAD : Instruction::MLOAD);
if (numBytes != 32)
{
// add leading or trailing zeros by dividing/multiplying depending on alignment
u256 shiftFactor = u256(1) << ((32 - numBytes) * 8);
m_context << shiftFactor << Instruction::SWAP1 << Instruction::DIV;
if (leftAligned)
m_context << shiftFactor << Instruction::MUL;
}
bool leftAligned = _type.category() == Type::Category::FixedBytes;
// add leading or trailing zeros by dividing/multiplying depending on alignment
u256 shiftFactor = u256(1) << ((32 - numBytes) * 8);
m_context << shiftFactor << Instruction::SWAP1 << Instruction::DIV;
if (leftAligned)
m_context << shiftFactor << Instruction::MUL;
}

if (auto const* funType = dynamic_cast<FunctionType const*>(&_type))
if (funType->location() == FunctionType::Location::External)
splitExternalFunctionType();

return numBytes;
}

Expand Down
10 changes: 5 additions & 5 deletions libsolidity/codegen/CompilerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ class CompilerUtils
/// Stack post:
void memoryCopy();

/// Converts the combined and right-aligned external function type
/// <function identifier><address> into two stack slots:
/// Converts the combined and left-aligned (right-aligned if @a _rightAligned is true)
/// external function type <function identifier><address> into two stack slots:
/// address (right aligned), function identifier (right aligned)
void splitExternalFunctionType();
/// Performs the opposite operation of splitExternalFunctionType()
void combineExternalFunctionType();
void splitExternalFunctionType(bool _rightAligned);
/// Performs the opposite operation of splitExternalFunctionType(_rightAligned)
void combineExternalFunctionType(bool _rightAligned);

/// Appends code for an implicit or explicit type conversion. This includes erasing higher
/// order bits (@see appendHighBitCleanup) when widening integer but also copy to memory
Expand Down
4 changes: 2 additions & 2 deletions libsolidity/codegen/LValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const
m_dataType->category() == Type::Category::Function &&
dynamic_cast<FunctionType const&>(*m_dataType).location() == FunctionType::Location::External
)
CompilerUtils(m_context).splitExternalFunctionType();
CompilerUtils(m_context).splitExternalFunctionType(false);
else
{
solAssert(m_dataType->sizeOnStack() == 1, "");
Expand Down Expand Up @@ -241,7 +241,7 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc
dynamic_cast<FunctionType const&>(*m_dataType).location() == FunctionType::Location::External
)
// Combine the two-item function type into a single stack slot.
utils.combineExternalFunctionType();
utils.combineExternalFunctionType(false);
else if (m_dataType->category() == Type::Category::FixedBytes)
m_context
<< (u256(0x1) << (256 - 8 * dynamic_cast<FixedBytesType const&>(*m_dataType).numBytes()))
Expand Down

0 comments on commit ff3553a

Please sign in to comment.