Browse Source

Merge branch 'develop' of github.com:ethereum/cpp-ethereum into develop

cl-refactor
Gav Wood 10 years ago
parent
commit
13fdd2e6e0
  1. 1
      libsolidity/CompilerContext.h
  2. 8
      libsolidity/CompilerUtils.cpp
  3. 2
      libsolidity/CompilerUtils.h
  4. 63
      libsolidity/ExpressionCompiler.cpp
  5. 25
      libsolidity/Types.cpp
  6. 1
      libtestutils/StateLoader.cpp
  7. 54
      test/SolidityEndToEndTest.cpp

1
libsolidity/CompilerContext.h

@ -83,6 +83,7 @@ public:
/// Converts an offset relative to the current stack height to a value that can be used later
/// with baseToCurrentStackOffset to point to the same stack element.
unsigned currentToBaseStackOffset(unsigned _offset) const;
/// @returns pair of slot and byte offset of the value inside this slot.
std::pair<u256, unsigned> getStorageLocationOfVariable(Declaration const& _declaration) const;
/// Appends a JUMPI instruction to a new tag and @returns the tag

8
libsolidity/CompilerUtils.cpp

@ -157,8 +157,12 @@ void CompilerUtils::copyToStackTop(unsigned _stackDepth, unsigned _itemSize)
void CompilerUtils::popStackElement(Type const& _type)
{
unsigned const size = _type.getSizeOnStack();
for (unsigned i = 0; i < size; ++i)
popStackSlots(_type.getSizeOnStack());
}
void CompilerUtils::popStackSlots(size_t _amount)
{
for (size_t i = 0; i < _amount; ++i)
m_context << eth::Instruction::POP;
}

2
libsolidity/CompilerUtils.h

@ -79,6 +79,8 @@ public:
void copyToStackTop(unsigned _stackDepth, unsigned _itemSize);
/// Removes the current value from the top of the stack.
void popStackElement(Type const& _type);
/// Removes element from the top of the stack _amount times.
void popStackSlots(size_t _amount);
template <class T>
static unsigned getSizeOnStack(std::vector<T> const& _variables);

63
libsolidity/ExpressionCompiler.cpp

@ -60,57 +60,82 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
CompilerContext::LocationSetter locationSetter(m_context, _varDecl);
FunctionType accessorType(_varDecl);
unsigned length = 0;
TypePointers const& paramTypes = accessorType.getParameterTypes();
// move arguments to memory
for (TypePointer const& paramType: boost::adaptors::reverse(paramTypes))
length += CompilerUtils(m_context).storeInMemory(length, *paramType, true);
// retrieve the position of the variable
auto const& location = m_context.getStorageLocationOfVariable(_varDecl);
m_context << location.first;
m_context << location.first << u256(location.second);
TypePointer returnType = _varDecl.getType();
for (TypePointer const& paramType: paramTypes)
{
// move offset to memory
CompilerUtils(m_context).storeInMemory(length);
unsigned argLen = paramType->getCalldataEncodedSize();
length -= argLen;
m_context << u256(argLen + 32) << u256(length) << eth::Instruction::SHA3;
returnType = dynamic_cast<MappingType const&>(*returnType).getValueType();
for (size_t i = 0; i < paramTypes.size(); ++i)
{
if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
{
// pop offset
m_context << eth::Instruction::POP;
// move storage offset to memory.
CompilerUtils(m_context).storeInMemory(32);
// move key to memory.
CompilerUtils(m_context).copyToStackTop(paramTypes.size() - i, 1);
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(64) << u256(0) << eth::Instruction::SHA3;
// push offset
m_context << u256(0);
returnType = mappingType->getValueType();
}
else if (auto arrayType = dynamic_cast<ArrayType const*>(returnType.get()))
{
// pop offset
m_context << eth::Instruction::POP;
CompilerUtils(m_context).copyToStackTop(paramTypes.size() - i + 1, 1);
ArrayUtils(m_context).accessIndex(*arrayType);
returnType = arrayType->getBaseType();
}
else
solAssert(false, "Index access is allowed only for \"mapping\" and \"array\" types.");
}
// remove index arguments.
if (paramTypes.size() == 1)
m_context << eth::Instruction::SWAP2 << eth::Instruction::POP << eth::Instruction::SWAP1;
else if (paramTypes.size() >= 2)
{
m_context << eth::swapInstruction(paramTypes.size());
m_context << eth::Instruction::POP;
m_context << eth::swapInstruction(paramTypes.size());
CompilerUtils(m_context).popStackSlots(paramTypes.size() - 1);
}
unsigned retSizeOnStack = 0;
solAssert(accessorType.getReturnParameterTypes().size() >= 1, "");
if (StructType const* structType = dynamic_cast<StructType const*>(returnType.get()))
{
// remove offset
m_context << eth::Instruction::POP;
auto const& names = accessorType.getReturnParameterNames();
auto const& types = accessorType.getReturnParameterTypes();
// struct
for (size_t i = 0; i < names.size(); ++i)
{
if (types[i]->getCategory() == Type::Category::Mapping)
if (types[i]->getCategory() == Type::Category::Mapping || types[i]->getCategory() == Type::Category::Array)
continue;
pair<u256, unsigned> const& offsets = structType->getStorageOffsetsOfMember(names[i]);
m_context << eth::Instruction::DUP1 << u256(offsets.first) << eth::Instruction::ADD << u256(offsets.second);
StorageItem(m_context, *types[i]).retrieveValue(SourceLocation(), true);
solAssert(types[i]->getSizeOnStack() == 1, "Returning struct elements with stack size != 1 not yet implemented.");
solAssert(types[i]->getSizeOnStack() == 1, "Returning struct elements with stack size != 1 is not yet implemented.");
m_context << eth::Instruction::SWAP1;
retSizeOnStack += types[i]->getSizeOnStack();
}
// remove slot
m_context << eth::Instruction::POP;
}
else
{
// simple value
solAssert(accessorType.getReturnParameterTypes().size() == 1, "");
m_context << u256(location.second);
StorageItem(m_context, *returnType).retrieveValue(SourceLocation(), true);
retSizeOnStack = returnType->getSizeOnStack();
}
solAssert(retSizeOnStack <= 15, "Stack too deep.");
solAssert(retSizeOnStack <= 15, "Stack is too deep.");
m_context << eth::dupInstruction(retSizeOnStack + 1);
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
}
@ -749,7 +774,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
appendTypeMoveToMemory(IntegerType(256));
m_context << u256(0) << eth::Instruction::SHA3;
m_context << u256(0);
setLValueToStorageItem( _indexAccess);
setLValueToStorageItem(_indexAccess);
}
else if (baseType.getCategory() == Type::Category::Array)
{

25
libsolidity/Types.cpp

@ -981,15 +981,26 @@ FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal
FunctionType::FunctionType(VariableDeclaration const& _varDecl):
m_location(Location::External), m_isConstant(true), m_declaration(&_varDecl)
{
TypePointers params;
TypePointers paramTypes;
vector<string> paramNames;
auto returnType = _varDecl.getType();
while (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
while (true)
{
params.push_back(mappingType->getKeyType());
paramNames.push_back("");
returnType = mappingType->getValueType();
if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
{
paramTypes.push_back(mappingType->getKeyType());
paramNames.push_back("");
returnType = mappingType->getValueType();
}
else if (auto arrayType = dynamic_cast<ArrayType const*>(returnType.get()))
{
returnType = arrayType->getBaseType();
paramNames.push_back("");
paramTypes.push_back(make_shared<IntegerType>(256));
}
else
break;
}
TypePointers retParams;
@ -997,7 +1008,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl):
if (auto structType = dynamic_cast<StructType const*>(returnType.get()))
{
for (pair<string, TypePointer> const& member: structType->getMembers())
if (member.second->canLiveOutsideStorage())
if (member.second->getCategory() != Category::Mapping && member.second->getCategory() != Category::Array)
{
retParamNames.push_back(member.first);
retParams.push_back(member.second);
@ -1009,7 +1020,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl):
retParamNames.push_back("");
}
swap(params, m_parameterTypes);
swap(paramTypes, m_parameterTypes);
swap(paramNames, m_parameterNames);
swap(retParams, m_returnParameterTypes);
swap(retParamNames, m_returnParameterNames);

1
libtestutils/StateLoader.cpp

@ -54,5 +54,4 @@ StateLoader::StateLoader(Json::Value const& _json, std::string const& _dbPath):
}
m_state.commit();
m_state.db().commit();
}

54
test/SolidityEndToEndTest.cpp

@ -966,6 +966,60 @@ BOOST_AUTO_TEST_CASE(simple_accessor)
BOOST_CHECK(callContractFunction("data()") == encodeArgs(8));
}
BOOST_AUTO_TEST_CASE(array_accessor)
{
char const* sourceCode = R"(
contract test {
uint[8] public data;
uint[] public dynamicData;
uint24[] public smallTypeData;
struct st { uint a; uint[] finalArray; }
mapping(uint256 => mapping(uint256 => st[5])) public multiple_map;
function test() {
data[0] = 8;
dynamicData.length = 3;
dynamicData[2] = 8;
smallTypeData.length = 128;
smallTypeData[1] = 22;
smallTypeData[127] = 2;
multiple_map[2][1][2].a = 3;
multiple_map[2][1][2].finalArray.length = 4;
multiple_map[2][1][2].finalArray[3] = 5;
}
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("data(uint256)", 0) == encodeArgs(8));
BOOST_CHECK(callContractFunction("data(uint256)", 8) == encodeArgs());
BOOST_CHECK(callContractFunction("dynamicData(uint256)", 2) == encodeArgs(8));
BOOST_CHECK(callContractFunction("dynamicData(uint256)", 8) == encodeArgs());
BOOST_CHECK(callContractFunction("smallTypeData(uint256)", 1) == encodeArgs(22));
BOOST_CHECK(callContractFunction("smallTypeData(uint256)", 127) == encodeArgs(2));
BOOST_CHECK(callContractFunction("smallTypeData(uint256)", 128) == encodeArgs());
BOOST_CHECK(callContractFunction("multiple_map(uint256,uint256,uint256)", 2, 1, 2) == encodeArgs(3));
}
BOOST_AUTO_TEST_CASE(accessors_mapping_for_array)
{
char const* sourceCode = R"(
contract test {
mapping(uint => uint[8]) public data;
mapping(uint => uint[]) public dynamicData;
function test() {
data[2][2] = 8;
dynamicData[2].length = 3;
dynamicData[2][2] = 8;
}
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("data(uint256,uint256)", 2, 2) == encodeArgs(8));
BOOST_CHECK(callContractFunction("data(uint256, 256)", 2, 8) == encodeArgs());
BOOST_CHECK(callContractFunction("dynamicData(uint256,uint256)", 2, 2) == encodeArgs(8));
BOOST_CHECK(callContractFunction("dynamicData(uint256,uint256)", 2, 8) == encodeArgs());
}
BOOST_AUTO_TEST_CASE(multiple_elementary_accessors)
{
char const* sourceCode = "contract test {\n"

Loading…
Cancel
Save