mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge remote-tracking branch 'upstream/develop' into evmjit
This commit is contained in:
commit
5c713d1429
58
AST.cpp
58
AST.cpp
@ -328,8 +328,30 @@ bool VariableDeclaration::isLValue() const
|
||||
|
||||
void VariableDeclaration::checkTypeRequirements()
|
||||
{
|
||||
if (m_value)
|
||||
// Variables can be declared without type (with "var"), in which case the first assignment
|
||||
// sets the type.
|
||||
// Note that assignments before the first declaration are legal because of the special scoping
|
||||
// rules inherited from JavaScript.
|
||||
if (!m_value)
|
||||
return;
|
||||
if (m_type)
|
||||
m_value->expectType(*m_type);
|
||||
else
|
||||
{
|
||||
// no type declared and no previous assignment, infer the type
|
||||
m_value->checkTypeRequirements();
|
||||
TypePointer type = m_value->getType();
|
||||
if (type->getCategory() == Type::Category::IntegerConstant)
|
||||
{
|
||||
auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
|
||||
if (!intType)
|
||||
BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString() + "."));
|
||||
type = intType;
|
||||
}
|
||||
else if (type->getCategory() == Type::Category::Void)
|
||||
BOOST_THROW_EXCEPTION(createTypeError("Variable cannot have void type."));
|
||||
m_type = type;
|
||||
}
|
||||
}
|
||||
|
||||
bool VariableDeclaration::isExternalFunctionParameter() const
|
||||
@ -445,32 +467,9 @@ void Return::checkTypeRequirements()
|
||||
|
||||
void VariableDeclarationStatement::checkTypeRequirements()
|
||||
{
|
||||
// Variables can be declared without type (with "var"), in which case the first assignment
|
||||
// sets the type.
|
||||
// Note that assignments before the first declaration are legal because of the special scoping
|
||||
// rules inherited from JavaScript.
|
||||
if (m_variable->getValue())
|
||||
{
|
||||
if (m_variable->getType())
|
||||
m_variable->getValue()->expectType(*m_variable->getType());
|
||||
else
|
||||
{
|
||||
// no type declared and no previous assignment, infer the type
|
||||
m_variable->getValue()->checkTypeRequirements();
|
||||
TypePointer type = m_variable->getValue()->getType();
|
||||
if (type->getCategory() == Type::Category::IntegerConstant)
|
||||
{
|
||||
auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
|
||||
if (!intType)
|
||||
BOOST_THROW_EXCEPTION(m_variable->getValue()->createTypeError("Invalid integer constant " + type->toString()));
|
||||
type = intType;
|
||||
}
|
||||
else if (type->getCategory() == Type::Category::Void)
|
||||
BOOST_THROW_EXCEPTION(m_variable->createTypeError("var cannot be void type"));
|
||||
m_variable->setType(type);
|
||||
}
|
||||
}
|
||||
m_variable->checkTypeRequirements();
|
||||
}
|
||||
|
||||
void Assignment::checkTypeRequirements()
|
||||
{
|
||||
m_leftHandSide->checkTypeRequirements();
|
||||
@ -671,8 +670,11 @@ void IndexAccess::checkTypeRequirements()
|
||||
if (!m_index)
|
||||
BOOST_THROW_EXCEPTION(createTypeError("Index expression cannot be omitted."));
|
||||
m_index->expectType(IntegerType(256));
|
||||
m_type = type.getBaseType();
|
||||
m_isLValue = true;
|
||||
if (type.isByteArray())
|
||||
m_type = make_shared<IntegerType>(8, IntegerType::Modifier::Hash);
|
||||
else
|
||||
m_type = type.getBaseType();
|
||||
m_isLValue = type.getLocation() != ArrayType::Location::CallData;
|
||||
break;
|
||||
}
|
||||
case Type::Category::Mapping:
|
||||
|
9
AST.h
9
AST.h
@ -460,7 +460,6 @@ public:
|
||||
|
||||
virtual bool isLValue() const override;
|
||||
|
||||
/// Calls checkTypeRequirments for all state variables.
|
||||
void checkTypeRequirements();
|
||||
bool isLocalVariable() const { return !!dynamic_cast<FunctionDefinition const*>(getScope()); }
|
||||
bool isExternalFunctionParameter() const;
|
||||
@ -1197,7 +1196,13 @@ public:
|
||||
Wei = Token::SubWei,
|
||||
Szabo = Token::SubSzabo,
|
||||
Finney = Token::SubFinney,
|
||||
Ether = Token::SubEther
|
||||
Ether = Token::SubEther,
|
||||
Second = Token::SubSecond,
|
||||
Minute = Token::SubMinute,
|
||||
Hour = Token::SubHour,
|
||||
Day = Token::SubDay,
|
||||
Week = Token::SubWeek,
|
||||
Year = Token::SubYear
|
||||
};
|
||||
Literal(SourceLocation const& _location, Token::Value _token,
|
||||
ASTPointer<ASTString> const& _value,
|
||||
|
@ -70,22 +70,25 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
||||
m_context << eth::Instruction::ISZERO;
|
||||
eth::AssemblyItem copyLoopEnd = m_context.newTag();
|
||||
m_context.appendConditionalJumpTo(copyLoopEnd);
|
||||
// add length to source offset
|
||||
m_context << eth::Instruction::DUP5 << eth::Instruction::DUP5 << eth::Instruction::ADD;
|
||||
// stack now: source_offset source_len target_ref target_data_end target_data_ref source_end
|
||||
// store start offset
|
||||
m_context << eth::Instruction::DUP5;
|
||||
// stack now: source_offset source_len target_ref target_data_end target_data_ref calldata_offset
|
||||
m_context << eth::Instruction::DUP6;
|
||||
// stack now: source_offset source_len target_ref target_data_end target_data_ref source_end calldata_offset
|
||||
eth::AssemblyItem copyLoopStart = m_context.newTag();
|
||||
m_context << copyLoopStart
|
||||
// copy from calldata and store
|
||||
<< eth::Instruction::DUP1 << eth::Instruction::CALLDATALOAD
|
||||
<< eth::Instruction::DUP3 << eth::Instruction::SSTORE
|
||||
<< eth::Instruction::DUP4 << eth::Instruction::SSTORE
|
||||
// increment target_data_ref by 1
|
||||
<< eth::Instruction::SWAP1 << u256(1) << eth::Instruction::ADD
|
||||
<< eth::Instruction::SWAP2 << u256(1) << eth::Instruction::ADD
|
||||
// increment calldata_offset by 32
|
||||
<< eth::Instruction::SWAP1 << u256(32) << eth::Instruction::ADD
|
||||
<< eth::Instruction::SWAP2 << u256(32) << eth::Instruction::ADD
|
||||
// check for loop condition
|
||||
<< eth::Instruction::DUP1 << eth::Instruction::DUP6 << eth::Instruction::GT;
|
||||
<< eth::Instruction::DUP1 << eth::Instruction::DUP3 << eth::Instruction::GT;
|
||||
m_context.appendConditionalJumpTo(copyLoopStart);
|
||||
m_context << eth::Instruction::POP;
|
||||
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
||||
m_context << copyLoopEnd;
|
||||
|
||||
// now clear leftover bytes of the old value
|
||||
@ -179,6 +182,7 @@ void ArrayUtils::clearArray(ArrayType const& _type) const
|
||||
m_context << eth::Instruction::POP;
|
||||
else if (_type.getLength() < 5) // unroll loop for small arrays @todo choose a good value
|
||||
{
|
||||
solAssert(!_type.isByteArray(), "");
|
||||
for (unsigned i = 1; i < _type.getLength(); ++i)
|
||||
{
|
||||
StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), false);
|
||||
@ -188,6 +192,7 @@ void ArrayUtils::clearArray(ArrayType const& _type) const
|
||||
}
|
||||
else
|
||||
{
|
||||
solAssert(!_type.isByteArray(), "");
|
||||
m_context
|
||||
<< eth::Instruction::DUP1 << u256(_type.getLength())
|
||||
<< u256(_type.getBaseType()->getStorageSize())
|
||||
@ -296,9 +301,23 @@ void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType) const
|
||||
|
||||
void ArrayUtils::retrieveLength(ArrayType const& _arrayType) const
|
||||
{
|
||||
if (_arrayType.isDynamicallySized())
|
||||
m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD;
|
||||
else
|
||||
if (!_arrayType.isDynamicallySized())
|
||||
m_context << _arrayType.getLength();
|
||||
else
|
||||
{
|
||||
m_context << eth::Instruction::DUP1;
|
||||
switch (_arrayType.getLocation())
|
||||
{
|
||||
case ArrayType::Location::CallData:
|
||||
// length is stored on the stack
|
||||
break;
|
||||
case ArrayType::Location::Memory:
|
||||
m_context << eth::Instruction::MLOAD;
|
||||
break;
|
||||
case ArrayType::Location::Storage:
|
||||
m_context << eth::Instruction::SLOAD;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -260,7 +260,7 @@ void Compiler::appendReturnValuePacker(TypePointers const& _typeParameters)
|
||||
|
||||
for (TypePointer const& type: _typeParameters)
|
||||
{
|
||||
CompilerUtils(m_context).copyToStackTop(stackDepth, *type);
|
||||
CompilerUtils(m_context).copyToStackTop(stackDepth, type->getSizeOnStack());
|
||||
ExpressionCompiler(m_context, m_optimize).appendTypeConversion(*type, *type, true);
|
||||
bool const c_padToWords = true;
|
||||
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, *type, c_padToWords);
|
||||
|
@ -41,8 +41,11 @@ public:
|
||||
std::map<ContractDefinition const*, bytes const*> const& _contracts);
|
||||
bytes getAssembledBytecode() { return m_context.getAssembledBytecode(m_optimize); }
|
||||
bytes getRuntimeBytecode() { return m_runtimeContext.getAssembledBytecode(m_optimize);}
|
||||
void streamAssembly(std::ostream& _stream) const { m_context.streamAssembly(_stream); }
|
||||
|
||||
/// @arg _sourceCodes is the map of input files to source code strings
|
||||
void streamAssembly(std::ostream& _stream, StringMap const& _sourceCodes = StringMap()) const
|
||||
{
|
||||
m_context.streamAssembly(_stream, _sourceCodes);
|
||||
}
|
||||
/// @returns Assembly items of the normal compiler context
|
||||
eth::AssemblyItems const& getAssemblyItems() const { return m_context.getAssembly().getItems(); }
|
||||
/// @returns Assembly items of the runtime compiler context
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include <libevmcore/Assembly.h>
|
||||
#include <libsolidity/ASTForward.h>
|
||||
#include <libsolidity/Types.h>
|
||||
#include <libdevcore/Common.h>
|
||||
|
||||
namespace dev {
|
||||
namespace solidity {
|
||||
@ -118,7 +119,9 @@ public:
|
||||
CompilerContext& operator<<(bytes const& _data);
|
||||
|
||||
eth::Assembly const& getAssembly() const { return m_asm; }
|
||||
void streamAssembly(std::ostream& _stream) const { _stream << m_asm; }
|
||||
/// @arg _sourceCodes is the map of input files to source code strings
|
||||
void streamAssembly(std::ostream& _stream, StringMap const& _sourceCodes = StringMap()) const { m_asm.streamRLP(_stream, "", _sourceCodes); }
|
||||
|
||||
bytes getAssembledBytecode(bool _optimize = false) { return m_asm.optimise(_optimize).assemble(); }
|
||||
|
||||
/**
|
||||
|
@ -180,9 +180,9 @@ dev::h256 CompilerStack::getContractCodeHash(string const& _contractName) const
|
||||
return dev::sha3(getRuntimeBytecode(_contractName));
|
||||
}
|
||||
|
||||
void CompilerStack::streamAssembly(ostream& _outStream, string const& _contractName) const
|
||||
void CompilerStack::streamAssembly(ostream& _outStream, string const& _contractName, StringMap _sourceCodes) const
|
||||
{
|
||||
getContract(_contractName).compiler->streamAssembly(_outStream);
|
||||
getContract(_contractName).compiler->streamAssembly(_outStream, _sourceCodes);
|
||||
}
|
||||
|
||||
string const& CompilerStack::getInterface(string const& _contractName) const
|
||||
|
@ -59,8 +59,6 @@ enum class DocumentationType: uint8_t
|
||||
ABISolidityInterface
|
||||
};
|
||||
|
||||
extern const std::map<std::string, std::string> StandardSources;
|
||||
|
||||
/**
|
||||
* Easy to use and self-contained Solidity compiler with as few header dependencies as possible.
|
||||
* It holds state and can be used to either step through the compilation stages (and abort e.g.
|
||||
@ -74,7 +72,7 @@ public:
|
||||
|
||||
/// Adds a source object (e.g. file) to the parser. After this, parse has to be called again.
|
||||
/// @returns true if a source object by the name already existed and was replaced.
|
||||
void addSources(std::map<std::string, std::string> const& _nameContents, bool _isLibrary = false) { for (auto const& i: _nameContents) addSource(i.first, i.second, _isLibrary); }
|
||||
void addSources(StringMap const& _nameContents, bool _isLibrary = false) { for (auto const& i: _nameContents) addSource(i.first, i.second, _isLibrary); }
|
||||
bool addSource(std::string const& _name, std::string const& _content, bool _isLibrary = false);
|
||||
void setSource(std::string const& _sourceCode);
|
||||
/// Parses all source units that were added
|
||||
@ -103,8 +101,9 @@ public:
|
||||
dev::h256 getContractCodeHash(std::string const& _contractName = "") const;
|
||||
|
||||
/// Streams a verbose version of the assembly to @a _outStream.
|
||||
/// @arg _sourceCodes is the map of input files to source code strings
|
||||
/// Prerequisite: Successful compilation.
|
||||
void streamAssembly(std::ostream& _outStream, std::string const& _contractName = "") const;
|
||||
void streamAssembly(std::ostream& _outStream, std::string const& _contractName = "", StringMap _sourceCodes = StringMap()) const;
|
||||
|
||||
/// Returns a string representing the contract interface in JSON.
|
||||
/// Prerequisite: Successful call to parse or compile.
|
||||
|
@ -33,26 +33,38 @@ namespace solidity
|
||||
|
||||
const unsigned int CompilerUtils::dataStartOffset = 4;
|
||||
|
||||
unsigned CompilerUtils::loadFromMemory(unsigned _offset, Type const& _type,
|
||||
bool _fromCalldata, bool _padToWordBoundaries)
|
||||
unsigned CompilerUtils::loadFromMemory(
|
||||
unsigned _offset,
|
||||
Type const& _type,
|
||||
bool _fromCalldata,
|
||||
bool _padToWordBoundaries
|
||||
)
|
||||
{
|
||||
solAssert(_type.getCategory() != Type::Category::Array, "Unable to statically load dynamic type.");
|
||||
m_context << u256(_offset);
|
||||
return loadFromMemoryHelper(_type, _fromCalldata, _padToWordBoundaries);
|
||||
}
|
||||
|
||||
void CompilerUtils::loadFromMemoryDynamic(Type const& _type, bool _fromCalldata, bool _padToWordBoundaries)
|
||||
void CompilerUtils::loadFromMemoryDynamic(
|
||||
Type const& _type,
|
||||
bool _fromCalldata,
|
||||
bool _padToWordBoundaries,
|
||||
bool _keepUpdatedMemoryOffset
|
||||
)
|
||||
{
|
||||
solAssert(_type.getCategory() != Type::Category::Array, "Arrays not yet implemented.");
|
||||
m_context << eth::Instruction::DUP1;
|
||||
if (_keepUpdatedMemoryOffset)
|
||||
m_context << eth::Instruction::DUP1;
|
||||
unsigned numBytes = loadFromMemoryHelper(_type, _fromCalldata, _padToWordBoundaries);
|
||||
// update memory counter
|
||||
for (unsigned i = 0; i < _type.getSizeOnStack(); ++i)
|
||||
m_context << eth::swapInstruction(1 + i);
|
||||
m_context << u256(numBytes) << eth::Instruction::ADD;
|
||||
if (_keepUpdatedMemoryOffset)
|
||||
{
|
||||
// update memory counter
|
||||
for (unsigned i = 0; i < _type.getSizeOnStack(); ++i)
|
||||
m_context << eth::swapInstruction(1 + i);
|
||||
m_context << u256(numBytes) << eth::Instruction::ADD;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned CompilerUtils::storeInMemory(unsigned _offset, Type const& _type, bool _padToWordBoundaries)
|
||||
{
|
||||
solAssert(_type.getCategory() != Type::Category::Array, "Unable to statically store dynamic type.");
|
||||
@ -134,12 +146,11 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable)
|
||||
m_context << eth::swapInstruction(stackPosition - size + 1) << eth::Instruction::POP;
|
||||
}
|
||||
|
||||
void CompilerUtils::copyToStackTop(unsigned _stackDepth, Type const& _type)
|
||||
void CompilerUtils::copyToStackTop(unsigned _stackDepth, unsigned _itemSize)
|
||||
{
|
||||
if (_stackDepth > 16)
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Stack too deep."));
|
||||
unsigned const size = _type.getSizeOnStack();
|
||||
for (unsigned i = 0; i < size; ++i)
|
||||
for (unsigned i = 0; i < _itemSize; ++i)
|
||||
m_context << eth::dupInstruction(_stackDepth);
|
||||
}
|
||||
|
||||
|
@ -41,19 +41,31 @@ public:
|
||||
/// @param _fromCalldata if true, load from calldata, not from memory
|
||||
/// @param _padToWordBoundaries if true, assume the data is padded to word (32 byte) boundaries
|
||||
/// @returns the number of bytes consumed in memory.
|
||||
unsigned loadFromMemory(unsigned _offset, Type const& _type = IntegerType(256),
|
||||
bool _fromCalldata = false, bool _padToWordBoundaries = false);
|
||||
unsigned loadFromMemory(
|
||||
unsigned _offset,
|
||||
Type const& _type = IntegerType(256),
|
||||
bool _fromCalldata = false,
|
||||
bool _padToWordBoundaries = false
|
||||
);
|
||||
/// Dynamic version of @see loadFromMemory, expects the memory offset on the stack.
|
||||
/// Stack pre: memory_offset
|
||||
/// Stack post: value... (memory_offset+length)
|
||||
void loadFromMemoryDynamic(Type const& _type, bool _fromCalldata = false, bool _padToWordBoundaries = true);
|
||||
void loadFromMemoryDynamic(
|
||||
Type const& _type,
|
||||
bool _fromCalldata = false,
|
||||
bool _padToWordBoundaries = true,
|
||||
bool _keepUpdatedMemoryOffset = true
|
||||
);
|
||||
/// Stores data from stack in memory.
|
||||
/// @param _offset offset in memory
|
||||
/// @param _type type of the data on the stack
|
||||
/// @param _padToWordBoundaries if true, pad the data to word (32 byte) boundaries
|
||||
/// @returns the number of bytes written to memory (can be different from _bytes if
|
||||
/// _padToWordBoundaries is true)
|
||||
unsigned storeInMemory(unsigned _offset, Type const& _type = IntegerType(256), bool _padToWordBoundaries = false);
|
||||
unsigned storeInMemory(unsigned _offset,
|
||||
Type const& _type = IntegerType(256),
|
||||
bool _padToWordBoundaries = false
|
||||
);
|
||||
/// Dynamic version of @see storeInMemory, expects the memory offset below the value on the stack
|
||||
/// and also updates that.
|
||||
/// Stack pre: memory_offset value...
|
||||
@ -65,8 +77,9 @@ public:
|
||||
|
||||
/// Moves the value that is at the top of the stack to a stack variable.
|
||||
void moveToStackVariable(VariableDeclaration const& _variable);
|
||||
/// Copies a variable of type @a _type from a stack depth of @a _stackDepth to the top of the stack.
|
||||
void copyToStackTop(unsigned _stackDepth, Type const& _type);
|
||||
/// Copies an item that occupies @a _itemSize stack slots from a stack depth of @a _stackDepth
|
||||
/// to the top of the stack.
|
||||
void copyToStackTop(unsigned _stackDepth, unsigned _itemSize);
|
||||
/// Removes the current value from the top of the stack.
|
||||
void popStackElement(Type const& _type);
|
||||
|
||||
|
@ -215,12 +215,20 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
|
||||
if (op != Token::Assign) // compound assignment
|
||||
{
|
||||
solAssert(_assignment.getType()->isValueType(), "Compound operators not implemented for non-value types.");
|
||||
if (m_currentLValue->storesReferenceOnStack())
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
|
||||
unsigned lvalueSize = m_currentLValue->sizeOnStack();
|
||||
unsigned itemSize = _assignment.getType()->getSizeOnStack();
|
||||
if (lvalueSize > 0)
|
||||
{
|
||||
CompilerUtils(m_context).copyToStackTop(lvalueSize + itemSize, itemSize);
|
||||
CompilerUtils(m_context).copyToStackTop(itemSize + lvalueSize, lvalueSize);
|
||||
// value lvalue_ref value lvalue_ref
|
||||
}
|
||||
m_currentLValue->retrieveValue(_assignment.getLocation(), true);
|
||||
appendOrdinaryBinaryOperatorCode(Token::AssignmentToBinaryOp(op), *_assignment.getType());
|
||||
if (m_currentLValue->storesReferenceOnStack())
|
||||
m_context << eth::Instruction::SWAP1;
|
||||
if (lvalueSize > 0)
|
||||
// value [lvalue_ref] updated_value
|
||||
for (unsigned i = 0; i < itemSize; ++i)
|
||||
m_context << eth::swapInstruction(itemSize + lvalueSize) << eth::Instruction::POP;
|
||||
}
|
||||
m_currentLValue->storeValue(*_assignment.getRightHandSide().getType(), _assignment.getLocation());
|
||||
m_currentLValue.reset();
|
||||
@ -250,6 +258,9 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
|
||||
case Token::BitNot: // ~
|
||||
m_context << eth::Instruction::NOT;
|
||||
break;
|
||||
case Token::After: // after
|
||||
m_context << eth::Instruction::TIMESTAMP << eth::Instruction::ADD;
|
||||
break;
|
||||
case Token::Delete: // delete
|
||||
solAssert(!!m_currentLValue, "LValue not retrieved.");
|
||||
m_currentLValue->setToZero(_unaryOperation.getLocation());
|
||||
@ -259,9 +270,10 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
|
||||
case Token::Dec: // -- (pre- or postfix)
|
||||
solAssert(!!m_currentLValue, "LValue not retrieved.");
|
||||
m_currentLValue->retrieveValue(_unaryOperation.getLocation());
|
||||
solAssert(m_currentLValue->sizeOnStack() <= 1, "Not implemented.");
|
||||
if (!_unaryOperation.isPrefixOperation())
|
||||
{
|
||||
if (m_currentLValue->storesReferenceOnStack())
|
||||
if (m_currentLValue->sizeOnStack() == 1)
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
|
||||
else
|
||||
m_context << eth::Instruction::DUP1;
|
||||
@ -273,7 +285,7 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::SUB; // @todo avoid the swap
|
||||
// Stack for prefix: [ref] (*ref)+-1
|
||||
// Stack for postfix: *ref [ref] (*ref)+-1
|
||||
if (m_currentLValue->storesReferenceOnStack())
|
||||
if (m_currentLValue->sizeOnStack() == 1)
|
||||
m_context << eth::Instruction::SWAP1;
|
||||
m_currentLValue->storeValue(
|
||||
*_unaryOperation.getType(), _unaryOperation.getLocation(),
|
||||
@ -714,18 +726,24 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
||||
}
|
||||
else if (baseType.getCategory() == Type::Category::Array)
|
||||
{
|
||||
// stack layout: <base_ref> [<length>] <index>
|
||||
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(baseType);
|
||||
solAssert(arrayType.getLocation() == ArrayType::Location::Storage,
|
||||
"TODO: Index acces only implemented for storage arrays.");
|
||||
solAssert(!arrayType.isByteArray(), "TODO: Index acces not implemented for byte arrays.");
|
||||
solAssert(_indexAccess.getIndexExpression(), "Index expression expected.");
|
||||
ArrayType::Location location = arrayType.getLocation();
|
||||
eth::Instruction load =
|
||||
location == ArrayType::Location::Storage ? eth::Instruction::SLOAD :
|
||||
location == ArrayType::Location::Memory ? eth::Instruction::MLOAD :
|
||||
eth::Instruction::CALLDATALOAD;
|
||||
|
||||
_indexAccess.getIndexExpression()->accept(*this);
|
||||
// retrieve length
|
||||
if (arrayType.isDynamicallySized())
|
||||
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
|
||||
else
|
||||
if (!arrayType.isDynamicallySized())
|
||||
m_context << arrayType.getLength();
|
||||
else if (location == ArrayType::Location::CallData)
|
||||
// length is stored on the stack
|
||||
m_context << eth::Instruction::SWAP1;
|
||||
else
|
||||
m_context << eth::Instruction::DUP2 << load;
|
||||
// stack: <base_ref> <index> <length>
|
||||
// check out-of-bounds access
|
||||
m_context << eth::Instruction::DUP2 << eth::Instruction::LT;
|
||||
@ -735,14 +753,64 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
||||
|
||||
m_context << legalAccess;
|
||||
// stack: <base_ref> <index>
|
||||
m_context << arrayType.getBaseType()->getStorageSize() << eth::Instruction::MUL;
|
||||
if (arrayType.isDynamicallySized())
|
||||
if (arrayType.isByteArray())
|
||||
// byte array is packed differently, especially in storage
|
||||
switch (location)
|
||||
{
|
||||
case ArrayType::Location::Storage:
|
||||
// byte array index storage lvalue on stack (goal):
|
||||
// <ref> <byte_number> = <base_ref + index / 32> <index % 32>
|
||||
m_context << u256(32) << eth::Instruction::SWAP2;
|
||||
CompilerUtils(m_context).computeHashStatic();
|
||||
// stack: 32 index data_ref
|
||||
m_context
|
||||
<< eth::Instruction::DUP3 << eth::Instruction::DUP3
|
||||
<< eth::Instruction::DIV << eth::Instruction::ADD
|
||||
// stack: 32 index (data_ref + index / 32)
|
||||
<< eth::Instruction::SWAP2 << eth::Instruction::SWAP1 << eth::Instruction::MOD;
|
||||
setLValue<StorageByteArrayElement>(_indexAccess);
|
||||
break;
|
||||
case ArrayType::Location::CallData:
|
||||
// no lvalue, just retrieve the value
|
||||
m_context
|
||||
<< eth::Instruction::ADD << eth::Instruction::CALLDATALOAD
|
||||
<< u256(0) << eth::Instruction::BYTE;
|
||||
break;
|
||||
case ArrayType::Location::Memory:
|
||||
solAssert(false, "Memory lvalues not yet implemented.");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_context << eth::Instruction::SWAP1;
|
||||
CompilerUtils(m_context).computeHashStatic();
|
||||
u256 elementSize =
|
||||
location == ArrayType::Location::Storage ? arrayType.getBaseType()->getStorageSize() :
|
||||
CompilerUtils::getPaddedSize(arrayType.getBaseType()->getCalldataEncodedSize());
|
||||
solAssert(elementSize != 0, "Invalid element size.");
|
||||
if (elementSize > 1)
|
||||
m_context << elementSize << eth::Instruction::MUL;
|
||||
if (arrayType.isDynamicallySized())
|
||||
{
|
||||
if (location == ArrayType::Location::Storage)
|
||||
{
|
||||
m_context << eth::Instruction::SWAP1;
|
||||
CompilerUtils(m_context).computeHashStatic();
|
||||
}
|
||||
else if (location == ArrayType::Location::Memory)
|
||||
m_context << u256(32) << eth::Instruction::ADD;
|
||||
}
|
||||
m_context << eth::Instruction::ADD;
|
||||
switch (location)
|
||||
{
|
||||
case ArrayType::Location::CallData:
|
||||
// no lvalue
|
||||
CompilerUtils(m_context).loadFromMemoryDynamic(*arrayType.getBaseType(), true, true, false);
|
||||
break;
|
||||
case ArrayType::Location::Storage:
|
||||
setLValueToStorageItem(_indexAccess);
|
||||
break;
|
||||
case ArrayType::Location::Memory:
|
||||
solAssert(false, "Memory lvalues not yet implemented.");
|
||||
}
|
||||
}
|
||||
m_context << eth::Instruction::ADD;
|
||||
setLValueToStorageItem(_indexAccess);
|
||||
}
|
||||
else
|
||||
solAssert(false, "Index access only allowed for mappings or arrays.");
|
||||
|
59
LValue.cpp
59
LValue.cpp
@ -232,6 +232,64 @@ void StorageItem::setToZero(SourceLocation const&, bool _removeReference) const
|
||||
}
|
||||
}
|
||||
|
||||
/// Used in StorageByteArrayElement
|
||||
static IntegerType byteType(8, IntegerType::Modifier::Hash);
|
||||
|
||||
StorageByteArrayElement::StorageByteArrayElement(CompilerContext& _compilerContext):
|
||||
LValue(_compilerContext, byteType)
|
||||
{
|
||||
}
|
||||
|
||||
void StorageByteArrayElement::retrieveValue(SourceLocation const&, bool _remove) const
|
||||
{
|
||||
// stack: ref byte_number
|
||||
if (_remove)
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::SLOAD
|
||||
<< eth::Instruction::SWAP1 << eth::Instruction::BYTE;
|
||||
else
|
||||
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD
|
||||
<< eth::Instruction::DUP2 << eth::Instruction::BYTE;
|
||||
}
|
||||
|
||||
void StorageByteArrayElement::storeValue(Type const&, SourceLocation const&, bool _move) const
|
||||
{
|
||||
//@todo optimize this
|
||||
|
||||
// stack: value ref byte_number
|
||||
m_context << u256(31) << eth::Instruction::SUB << u256(0x100) << eth::Instruction::EXP;
|
||||
// stack: value ref (1<<(8*(31-byte_number)))
|
||||
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
|
||||
// stack: value ref (1<<(8*(31-byte_number))) old_full_value
|
||||
// clear byte in old value
|
||||
m_context << eth::Instruction::DUP2 << u256(0xff) << eth::Instruction::MUL
|
||||
<< eth::Instruction::NOT << eth::Instruction::AND;
|
||||
// stack: value ref (1<<(32-byte_number)) old_full_value_with_cleared_byte
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP4 << eth::Instruction::MUL
|
||||
<< eth::Instruction::OR;
|
||||
// stack: value ref new_full_value
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
||||
if (_move)
|
||||
m_context << eth::Instruction::POP;
|
||||
}
|
||||
|
||||
void StorageByteArrayElement::setToZero(SourceLocation const&, bool _removeReference) const
|
||||
{
|
||||
// stack: ref byte_number
|
||||
if (!_removeReference)
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
|
||||
m_context << u256(31) << eth::Instruction::SUB << u256(0x100) << eth::Instruction::EXP;
|
||||
// stack: ref (1<<(8*(31-byte_number)))
|
||||
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
|
||||
// stack: ref (1<<(8*(31-byte_number))) old_full_value
|
||||
// clear byte in old value
|
||||
m_context << eth::Instruction::SWAP1 << u256(0xff) << eth::Instruction::MUL << eth::Instruction::AND;
|
||||
// stack: ref old_full_value_with_cleared_byte
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
||||
if (!_removeReference)
|
||||
m_context << eth::Instruction::SWAP1;
|
||||
else
|
||||
m_context << eth::Instruction::POP;
|
||||
}
|
||||
|
||||
StorageArrayLength::StorageArrayLength(CompilerContext& _compilerContext, const ArrayType& _arrayType):
|
||||
LValue(_compilerContext, *_arrayType.getMemberType("length")),
|
||||
@ -262,4 +320,3 @@ void StorageArrayLength::setToZero(SourceLocation const&, bool _removeReference)
|
||||
m_context << eth::Instruction::DUP1;
|
||||
ArrayUtils(m_context).clearDynamicArray(m_arrayType);
|
||||
}
|
||||
|
||||
|
67
LValue.h
67
LValue.h
@ -46,8 +46,8 @@ protected:
|
||||
m_context(_compilerContext), m_dataType(_dataType) {}
|
||||
|
||||
public:
|
||||
/// @returns true if this lvalue reference type occupies a slot on the stack.
|
||||
virtual bool storesReferenceOnStack() const = 0;
|
||||
/// @returns the number of stack slots occupied by the lvalue reference
|
||||
virtual unsigned sizeOnStack() const { return 1; }
|
||||
/// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true,
|
||||
/// also removes the reference from the stack.
|
||||
/// @a _location source location of the current expression, used for error reporting.
|
||||
@ -61,7 +61,9 @@ public:
|
||||
/// Stores zero in the lvalue. Removes the reference from the stack if @a _removeReference is true.
|
||||
/// @a _location is the source location of the requested operation
|
||||
virtual void setToZero(
|
||||
SourceLocation const& _location = SourceLocation(), bool _removeReference = true) const = 0;
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _removeReference = true
|
||||
) const = 0;
|
||||
|
||||
protected:
|
||||
CompilerContext& m_context;
|
||||
@ -76,12 +78,17 @@ class StackVariable: public LValue
|
||||
public:
|
||||
StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration);
|
||||
|
||||
virtual bool storesReferenceOnStack() const { return false; }
|
||||
virtual unsigned sizeOnStack() const override { return 0; }
|
||||
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
|
||||
virtual void storeValue(Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(), bool _move = false) const override;
|
||||
virtual void storeValue(
|
||||
Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _move = false
|
||||
) const override;
|
||||
virtual void setToZero(
|
||||
SourceLocation const& _location = SourceLocation(), bool _removeReference = true) const override;
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _removeReference = true
|
||||
) const override;
|
||||
|
||||
private:
|
||||
/// Base stack offset (@see CompilerContext::getBaseStackOffsetOfVariable) of the local variable.
|
||||
@ -100,12 +107,16 @@ public:
|
||||
StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration);
|
||||
/// Constructs the LValue and assumes that the storage reference is already on the stack.
|
||||
StorageItem(CompilerContext& _compilerContext, Type const& _type);
|
||||
virtual bool storesReferenceOnStack() const { return true; }
|
||||
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
|
||||
virtual void storeValue(Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(), bool _move = false) const override;
|
||||
virtual void storeValue(
|
||||
Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _move = false
|
||||
) const override;
|
||||
virtual void setToZero(
|
||||
SourceLocation const& _location = SourceLocation(), bool _removeReference = true) const override;
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _removeReference = true
|
||||
) const override;
|
||||
|
||||
private:
|
||||
/// Number of stack elements occupied by the value (not the reference).
|
||||
@ -113,6 +124,28 @@ private:
|
||||
unsigned m_size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reference to a single byte inside a storage byte array.
|
||||
* Stack: <storage_ref> <byte_number>
|
||||
*/
|
||||
class StorageByteArrayElement: public LValue
|
||||
{
|
||||
public:
|
||||
/// Constructs the LValue and assumes that the storage reference is already on the stack.
|
||||
StorageByteArrayElement(CompilerContext& _compilerContext);
|
||||
virtual unsigned sizeOnStack() const override { return 2; }
|
||||
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
|
||||
virtual void storeValue(
|
||||
Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _move = false
|
||||
) const override;
|
||||
virtual void setToZero(
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _removeReference = true
|
||||
) const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reference to the "length" member of a dynamically-sized array. This is an LValue with special
|
||||
* semantics since assignments to it might reduce its length and thus arrays members have to be
|
||||
@ -123,12 +156,16 @@ class StorageArrayLength: public LValue
|
||||
public:
|
||||
/// Constructs the LValue, assumes that the reference to the array head is already on the stack.
|
||||
StorageArrayLength(CompilerContext& _compilerContext, ArrayType const& _arrayType);
|
||||
virtual bool storesReferenceOnStack() const { return true; }
|
||||
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
|
||||
virtual void storeValue(Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(), bool _move = false) const override;
|
||||
virtual void storeValue(
|
||||
Type const& _sourceType,
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _move = false
|
||||
) const override;
|
||||
virtual void setToZero(
|
||||
SourceLocation const& _location = SourceLocation(), bool _removeReference = true) const override;
|
||||
SourceLocation const& _location = SourceLocation(),
|
||||
bool _removeReference = true
|
||||
) const override;
|
||||
|
||||
private:
|
||||
ArrayType const& m_arrayType;
|
||||
|
@ -822,6 +822,15 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
|
||||
expression = nodeFactory.createNode<Literal>(token, literal, subdenomination);
|
||||
break;
|
||||
}
|
||||
if (Token::isTimeSubdenomination(m_scanner->peekNextToken()))
|
||||
{
|
||||
ASTPointer<ASTString> literal = getLiteralAndAdvance();
|
||||
nodeFactory.markEndPosition();
|
||||
Literal::SubDenomination subdenomination = static_cast<Literal::SubDenomination>(m_scanner->getCurrentToken());
|
||||
m_scanner->next();
|
||||
expression = nodeFactory.createNode<Literal>(token, literal, subdenomination);
|
||||
break;
|
||||
}
|
||||
// fall-through
|
||||
case Token::StringLiteral:
|
||||
nodeFactory.markEndPosition();
|
||||
|
24
Token.h
24
Token.h
@ -163,7 +163,7 @@ namespace solidity
|
||||
K(New, "new", 0) \
|
||||
K(Public, "public", 0) \
|
||||
K(Private, "private", 0) \
|
||||
K(Internal, "internal", 0) \
|
||||
K(Internal, "internal", 0) \
|
||||
K(Return, "return", 0) \
|
||||
K(Returns, "returns", 0) \
|
||||
K(Struct, "struct", 0) \
|
||||
@ -172,11 +172,18 @@ namespace solidity
|
||||
K(While, "while", 0) \
|
||||
K(Enum, "enum", 0) \
|
||||
\
|
||||
/* Ether subdenominations */ \
|
||||
K(SubWei, "wei", 0) \
|
||||
K(SubSzabo, "szabo", 0) \
|
||||
K(SubFinney, "finney", 0) \
|
||||
K(SubEther, "ether", 0) \
|
||||
/* Ether subdenominations */ \
|
||||
K(SubWei, "wei", 0) \
|
||||
K(SubSzabo, "szabo", 0) \
|
||||
K(SubFinney, "finney", 0) \
|
||||
K(SubEther, "ether", 0) \
|
||||
K(SubSecond, "seconds", 0) \
|
||||
K(SubMinute, "minutes", 0) \
|
||||
K(SubHour, "hours", 0) \
|
||||
K(SubDay, "days", 0) \
|
||||
K(SubWeek, "weeks", 0) \
|
||||
K(SubYear, "years", 0) \
|
||||
K(After, "after", 0) \
|
||||
/* type keywords, keep them in this order, keep int as first keyword
|
||||
* the implementation in Types.cpp has to be synced to this here */\
|
||||
K(Int, "int", 0) \
|
||||
@ -377,12 +384,13 @@ public:
|
||||
}
|
||||
|
||||
static bool isBitOp(Value op) { return (BitOr <= op && op <= SHR) || op == BitNot; }
|
||||
static bool isUnaryOp(Value op) { return (Not <= op && op <= Delete) || op == Add || op == Sub; }
|
||||
static bool isUnaryOp(Value op) { return (Not <= op && op <= Delete) || op == Add || op == Sub || op == After; }
|
||||
static bool isCountOp(Value op) { return op == Inc || op == Dec; }
|
||||
static bool isShiftOp(Value op) { return (SHL <= op) && (op <= SHR); }
|
||||
static bool isVisibilitySpecifier(Value op) { return isVariableVisibilitySpecifier(op) || op == External; }
|
||||
static bool isVariableVisibilitySpecifier(Value op) { return op == Public || op == Private || op == Internal; }
|
||||
static bool isEtherSubdenomination(Value op) { return op == SubWei || op == SubSzabo || op == SubFinney || op == Token::SubEther; }
|
||||
static bool isEtherSubdenomination(Value op) { return op == SubWei || op == SubSzabo || op == SubFinney || op == SubEther; }
|
||||
static bool isTimeSubdenomination(Value op) { return op == SubSecond || op == SubMinute || op == SubHour || op == SubDay || op == SubWeek || op == SubYear; }
|
||||
|
||||
// Returns a string corresponding to the JS token string
|
||||
// (.e., "<" for the token LT) or NULL if the token doesn't
|
||||
|
21
Types.cpp
21
Types.cpp
@ -195,7 +195,8 @@ TypePointer IntegerType::unaryOperatorResult(Token::Value _operator) const
|
||||
return TypePointer();
|
||||
// for non-hash integers, we allow +, -, ++ and --
|
||||
else if (_operator == Token::Add || _operator == Token::Sub ||
|
||||
_operator == Token::Inc || _operator == Token::Dec)
|
||||
_operator == Token::Inc || _operator == Token::Dec ||
|
||||
_operator == Token::After)
|
||||
return shared_from_this();
|
||||
else
|
||||
return TypePointer();
|
||||
@ -251,6 +252,7 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal)
|
||||
switch (_literal.getSubDenomination())
|
||||
{
|
||||
case Literal::SubDenomination::Wei:
|
||||
case Literal::SubDenomination::Second:
|
||||
case Literal::SubDenomination::None:
|
||||
break;
|
||||
case Literal::SubDenomination::Szabo:
|
||||
@ -262,6 +264,21 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal)
|
||||
case Literal::SubDenomination::Ether:
|
||||
m_value *= bigint("1000000000000000000");
|
||||
break;
|
||||
case Literal::SubDenomination::Minute:
|
||||
m_value *= bigint("60");
|
||||
break;
|
||||
case Literal::SubDenomination::Hour:
|
||||
m_value *= bigint("3600");
|
||||
break;
|
||||
case Literal::SubDenomination::Day:
|
||||
m_value *= bigint("86400");
|
||||
break;
|
||||
case Literal::SubDenomination::Week:
|
||||
m_value *= bigint("604800");
|
||||
break;
|
||||
case Literal::SubDenomination::Year:
|
||||
m_value *= bigint("31536000");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -291,6 +308,8 @@ TypePointer IntegerConstantType::unaryOperatorResult(Token::Value _operator) con
|
||||
case Token::Sub:
|
||||
value = -m_value;
|
||||
break;
|
||||
case Token::After:
|
||||
return shared_from_this();
|
||||
default:
|
||||
return TypePointer();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user