Browse Source

Byte size checked for zero; coding style.

cl-refactor
chriseth 10 years ago
parent
commit
d2077b8962
  1. 27
      libsolidity/ArrayUtils.cpp
  2. 2
      libsolidity/ArrayUtils.h
  3. 1
      libsolidity/ExpressionCompiler.cpp

27
libsolidity/ArrayUtils.cpp

@ -440,9 +440,10 @@ void ArrayUtils::retrieveLength(ArrayType const& _arrayType) const
} }
} }
void ArrayUtils::incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosition, unsigned storageOffsetPosition) const void ArrayUtils::incrementByteOffset(unsigned _byteSize, unsigned _byteOffsetPosition, unsigned _storageOffsetPosition) const
{ {
solAssert(byteSize < 32, ""); solAssert(_byteSize < 32, "");
solAssert(_byteSize != 0, "");
// We do the following, but avoiding jumps: // We do the following, but avoiding jumps:
// byteOffset += byteSize // byteOffset += byteSize
// if (byteOffset + byteSize > 32) // if (byteOffset + byteSize > 32)
@ -450,28 +451,28 @@ void ArrayUtils::incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosit
// storageOffset++; // storageOffset++;
// byteOffset = 0; // byteOffset = 0;
// } // }
if (byteOffsetPosition > 1) if (_byteOffsetPosition > 1)
m_context << eth::swapInstruction(byteOffsetPosition - 1); m_context << eth::swapInstruction(_byteOffsetPosition - 1);
m_context << u256(byteSize) << eth::Instruction::ADD; m_context << u256(_byteSize) << eth::Instruction::ADD;
if (byteOffsetPosition > 1) if (_byteOffsetPosition > 1)
m_context << eth::swapInstruction(byteOffsetPosition - 1); m_context << eth::swapInstruction(_byteOffsetPosition - 1);
// compute, X := (byteOffset + byteSize - 1) / 32, should be 1 iff byteOffset + bytesize > 32 // compute, X := (byteOffset + byteSize - 1) / 32, should be 1 iff byteOffset + bytesize > 32
m_context m_context
<< u256(32) << eth::dupInstruction(1 + byteOffsetPosition) << u256(byteSize - 1) << u256(32) << eth::dupInstruction(1 + _byteOffsetPosition) << u256(_byteSize - 1)
<< eth::Instruction::ADD << eth::Instruction::DIV; << eth::Instruction::ADD << eth::Instruction::DIV;
// increment storage offset if X == 1 (just add X to it) // increment storage offset if X == 1 (just add X to it)
// stack: X // stack: X
m_context m_context
<< eth::swapInstruction(storageOffsetPosition) << eth::dupInstruction(storageOffsetPosition + 1) << eth::swapInstruction(_storageOffsetPosition) << eth::dupInstruction(_storageOffsetPosition + 1)
<< eth::Instruction::ADD << eth::swapInstruction(storageOffsetPosition); << eth::Instruction::ADD << eth::swapInstruction(_storageOffsetPosition);
// stack: X // stack: X
// set source_byte_offset to zero if X == 1 (using source_byte_offset *= 1 - X) // set source_byte_offset to zero if X == 1 (using source_byte_offset *= 1 - X)
m_context << u256(1) << eth::Instruction::SUB; m_context << u256(1) << eth::Instruction::SUB;
// stack: 1 - X // stack: 1 - X
if (byteOffsetPosition == 1) if (_byteOffsetPosition == 1)
m_context << eth::Instruction::MUL; m_context << eth::Instruction::MUL;
else else
m_context m_context
<< eth::dupInstruction(byteOffsetPosition + 1) << eth::Instruction::MUL << eth::dupInstruction(_byteOffsetPosition + 1) << eth::Instruction::MUL
<< eth::swapInstruction(byteOffsetPosition) << eth::Instruction::POP; << eth::swapInstruction(_byteOffsetPosition) << eth::Instruction::POP;
} }

2
libsolidity/ArrayUtils.h

@ -76,7 +76,7 @@ private:
/// the storage offset if adding this number again would increase the counter over 32. /// the storage offset if adding this number again would increase the counter over 32.
/// @param byteOffsetPosition the stack offset of the storage byte offset /// @param byteOffsetPosition the stack offset of the storage byte offset
/// @param storageOffsetPosition the stack offset of the storage slot offset /// @param storageOffsetPosition the stack offset of the storage slot offset
void incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosition, unsigned storageOffsetPosition) const; void incrementByteOffset(unsigned _byteSize, unsigned _byteOffsetPosition, unsigned _storageOffsetPosition) const;
CompilerContext& m_context; CompilerContext& m_context;
}; };

1
libsolidity/ExpressionCompiler.cpp

@ -839,6 +839,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
// goal: // goal:
// <ref> <byte_number> = <base_ref + index / itemsPerSlot> <(index % itemsPerSlot) * byteSize> // <ref> <byte_number> = <base_ref + index / itemsPerSlot> <(index % itemsPerSlot) * byteSize>
unsigned byteSize = arrayType.getBaseType()->getStorageBytes(); unsigned byteSize = arrayType.getBaseType()->getStorageBytes();
solAssert(byteSize != 0, "");
unsigned itemsPerSlot = 32 / byteSize; unsigned itemsPerSlot = 32 / byteSize;
m_context << u256(itemsPerSlot) << eth::Instruction::SWAP2; m_context << u256(itemsPerSlot) << eth::Instruction::SWAP2;
// stack: itemsPerSlot index data_ref // stack: itemsPerSlot index data_ref

Loading…
Cancel
Save