当前位置: 首页>>代码示例>>C++>>正文


C++ ArrayType::getLocation方法代码示例

本文整理汇总了C++中ArrayType::getLocation方法的典型用法代码示例。如果您正苦于以下问题:C++ ArrayType::getLocation方法的具体用法?C++ ArrayType::getLocation怎么用?C++ ArrayType::getLocation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ArrayType的用法示例。


在下文中一共展示了ArrayType::getLocation方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: convertLengthToSize

void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType, bool _pad) const
{
	if (_arrayType.getLocation() == ArrayType::Location::Storage)
	{
		if (_arrayType.isByteArray())
			m_context << u256(31) << eth::Instruction::ADD
				<< u256(32) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
		else if (_arrayType.getBaseType()->getStorageSize() <= 1)
		{
			unsigned baseBytes = _arrayType.getBaseType()->getStorageBytes();
			if (baseBytes == 0)
				m_context << eth::Instruction::POP << u256(1);
			else if (baseBytes <= 16)
			{
				unsigned itemsPerSlot = 32 / baseBytes;
				m_context
					<< u256(itemsPerSlot - 1) << eth::Instruction::ADD
					<< u256(itemsPerSlot) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
			}
		}
		else
			m_context << _arrayType.getBaseType()->getStorageSize() << eth::Instruction::MUL;
	}
	else
	{
		if (!_arrayType.isByteArray())
			m_context << _arrayType.getBaseType()->getCalldataEncodedSize() << eth::Instruction::MUL;
		else if (_pad)
			m_context << u256(31) << eth::Instruction::ADD
				<< u256(32) << eth::Instruction::DUP1
				<< eth::Instruction::SWAP2 << eth::Instruction::DIV << eth::Instruction::MUL;
	}
}
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:33,代码来源:ArrayUtils.cpp

示例2: clearDynamicArray

void ArrayUtils::clearDynamicArray(ArrayType const& _type) const
{
	solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
	solAssert(_type.isDynamicallySized(), "");

	unsigned stackHeightStart = m_context.getStackHeight();
	// fetch length
	m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD;
	// set length to zero
	m_context << u256(0) << eth::Instruction::DUP3 << eth::Instruction::SSTORE;
	// stack: ref old_length
	convertLengthToSize(_type);
	// compute data positions
	m_context << eth::Instruction::SWAP1;
	CompilerUtils(m_context).computeHashStatic();
	// stack: len data_pos
	m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2 << eth::Instruction::ADD
		<< eth::Instruction::SWAP1;
	// stack: data_pos_end data_pos
	if (_type.isByteArray() || _type.getBaseType()->getStorageBytes() < 32)
		clearStorageLoop(IntegerType(256));
	else
		clearStorageLoop(*_type.getBaseType());
	// cleanup
	m_context << eth::Instruction::POP;
	solAssert(m_context.getStackHeight() == stackHeightStart - 1, "");
}
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:27,代码来源:ArrayUtils.cpp

示例3: clearArray

void ArrayUtils::clearArray(ArrayType const& _type) const
{
	unsigned stackHeightStart = m_context.getStackHeight();
	solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
	if (_type.getBaseType()->getStorageBytes() < 32)
	{
		solAssert(_type.getBaseType()->isValueType(), "Invalid storage size for non-value type.");
		solAssert(_type.getBaseType()->getStorageSize() <= 1, "Invalid storage size for type.");
	}
	if (_type.getBaseType()->isValueType())
		solAssert(_type.getBaseType()->getStorageSize() <= 1, "Invalid size for value type.");

	m_context << eth::Instruction::POP; // remove byte offset
	if (_type.isDynamicallySized())
		clearDynamicArray(_type);
	else if (_type.getLength() == 0 || _type.getBaseType()->getCategory() == Type::Category::Mapping)
		m_context << eth::Instruction::POP;
	else if (_type.getBaseType()->isValueType() && _type.getStorageSize() <= 5)
	{
		// unroll loop for small arrays @todo choose a good value
		// Note that we loop over storage slots here, not elements.
		for (unsigned i = 1; i < _type.getStorageSize(); ++i)
			m_context
				<< u256(0) << eth::Instruction::DUP2 << eth::Instruction::SSTORE
				<< u256(1) << eth::Instruction::ADD;
		m_context << u256(0) << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
	}
	else if (!_type.getBaseType()->isValueType() && _type.getLength() <= 4)
	{
		// unroll loop for small arrays @todo choose a good value
		solAssert(_type.getBaseType()->getStorageBytes() >= 32, "Invalid storage size.");
		for (unsigned i = 1; i < _type.getLength(); ++i)
		{
			m_context << u256(0);
			StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), false);
			m_context
				<< eth::Instruction::POP
				<< u256(_type.getBaseType()->getStorageSize()) << eth::Instruction::ADD;
		}
		m_context << u256(0);
		StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), true);
	}
	else
	{
		m_context << eth::Instruction::DUP1 << _type.getLength();
		convertLengthToSize(_type);
		m_context << eth::Instruction::ADD << eth::Instruction::SWAP1;
		if (_type.getBaseType()->getStorageBytes() < 32)
			clearStorageLoop(IntegerType(256));
		else
			clearStorageLoop(*_type.getBaseType());
		m_context << eth::Instruction::POP;
	}
	solAssert(m_context.getStackHeight() == stackHeightStart - 2, "");
}
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:55,代码来源:ArrayUtils.cpp

示例4: resizeDynamicArray

void ArrayUtils::resizeDynamicArray(const ArrayType& _type) const
{
	solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
	solAssert(_type.isDynamicallySized(), "");
	if (!_type.isByteArray() && _type.getBaseType()->getStorageBytes() < 32)
		solAssert(_type.getBaseType()->isValueType(), "Invalid storage size for non-value type.");

	unsigned stackHeightStart = m_context.getStackHeight();
	eth::AssemblyItem resizeEnd = m_context.newTag();

	// stack: ref new_length
	// fetch old length
	m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
	// stack: ref new_length old_length
	// store new length
	m_context << eth::Instruction::DUP2 << eth::Instruction::DUP4 << eth::Instruction::SSTORE;
	// skip if size is not reduced
	m_context << eth::Instruction::DUP2 << eth::Instruction::DUP2
		<< eth::Instruction::ISZERO << eth::Instruction::GT;
	m_context.appendConditionalJumpTo(resizeEnd);

	// size reduced, clear the end of the array
	// stack: ref new_length old_length
	convertLengthToSize(_type);
	m_context << eth::Instruction::DUP2;
	convertLengthToSize(_type);
	// stack: ref new_length old_size new_size
	// compute data positions
	m_context << eth::Instruction::DUP4;
	CompilerUtils(m_context).computeHashStatic();
	// stack: ref new_length old_size new_size data_pos
	m_context << eth::Instruction::SWAP2 << eth::Instruction::DUP3 << eth::Instruction::ADD;
	// stack: ref new_length data_pos new_size delete_end
	m_context << eth::Instruction::SWAP2 << eth::Instruction::ADD;
	// stack: ref new_length delete_end delete_start
	if (_type.isByteArray() || _type.getBaseType()->getStorageBytes() < 32)
		clearStorageLoop(IntegerType(256));
	else
		clearStorageLoop(*_type.getBaseType());

	m_context << resizeEnd;
	// cleanup
	m_context << eth::Instruction::POP << eth::Instruction::POP << eth::Instruction::POP;
	solAssert(m_context.getStackHeight() == stackHeightStart - 2, "");
}
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:45,代码来源:ArrayUtils.cpp

示例5: retrieveLength

void ArrayUtils::retrieveLength(ArrayType const& _arrayType) const
{
	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;
		}
	}
}
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:21,代码来源:ArrayUtils.cpp

示例6: accessIndex

void ArrayUtils::accessIndex(ArrayType const& _arrayType) const
{
	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;

	// retrieve length
	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;
	eth::AssemblyItem legalAccess = m_context.appendConditionalJump();
	// out-of-bounds access throws exception (just STOP for now)
	m_context << eth::Instruction::STOP;

	m_context << legalAccess;
	// stack: <base_ref> <index>
	if (_arrayType.isByteArray())
		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;
			break;
		case ArrayType::Location::CallData:
			// no lvalue, just retrieve the value
			m_context
				<< eth::Instruction::ADD << eth::Instruction::CALLDATALOAD
				<< ((u256(0xff) << (256 - 8)))  << eth::Instruction::AND;
			break;
		case ArrayType::Location::Memory:
			solAssert(false, "Memory lvalues not yet implemented.");
		}
	else
	{
		// stack: <base_ref> <index>
		m_context << eth::Instruction::SWAP1;
		if (_arrayType.isDynamicallySized())
		{
			if (location == ArrayType::Location::Storage)
				CompilerUtils(m_context).computeHashStatic();
			else if (location == ArrayType::Location::Memory)
				m_context << u256(32) << eth::Instruction::ADD;
		}
		// stack: <index> <data_ref>
		switch (location)
		{
		case ArrayType::Location::CallData:
			m_context
				<< eth::Instruction::SWAP1 << _arrayType.getBaseType()->getCalldataEncodedSize()
				<< eth::Instruction::MUL << eth::Instruction::ADD;
			if (_arrayType.getBaseType()->isValueType())
				CompilerUtils(m_context).loadFromMemoryDynamic(*_arrayType.getBaseType(), true, true, false);
			break;
		case ArrayType::Location::Storage:
			m_context << eth::Instruction::SWAP1;
			if (_arrayType.getBaseType()->getStorageBytes() <= 16)
			{
				// stack: <data_ref> <index>
				// goal:
				// <ref> <byte_number> = <base_ref + index / itemsPerSlot> <(index % itemsPerSlot) * byteSize>
				unsigned byteSize = _arrayType.getBaseType()->getStorageBytes();
				solAssert(byteSize != 0, "");
				unsigned itemsPerSlot = 32 / byteSize;
				m_context << u256(itemsPerSlot) << eth::Instruction::SWAP2;
				// stack: itemsPerSlot index data_ref
				m_context
					<< eth::Instruction::DUP3 << eth::Instruction::DUP3
					<< eth::Instruction::DIV << eth::Instruction::ADD
				// stack: itemsPerSlot index (data_ref + index / itemsPerSlot)
					<< eth::Instruction::SWAP2 << eth::Instruction::SWAP1
					<< eth::Instruction::MOD
					<< u256(byteSize) << eth::Instruction::MUL;
			}
			else
			{
				if (_arrayType.getBaseType()->getStorageSize() != 1)
					m_context << _arrayType.getBaseType()->getStorageSize() << eth::Instruction::MUL;
				m_context << eth::Instruction::ADD << u256(0);
			}
			break;
		case ArrayType::Location::Memory:
			solAssert(false, "Memory lvalues not yet implemented.");
//.........这里部分代码省略.........
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:101,代码来源:ArrayUtils.cpp

示例7: copyArrayToStorage

void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType const& _sourceType) const
{
	// this copies source to target and also clears target if it was larger
	// need to leave "target_ref target_byte_off" on the stack at the end

	// stack layout: [source_ref] [source_byte_off] [source length] target_ref target_byte_off (top)
	solAssert(_targetType.getLocation() == ArrayType::Location::Storage, "");
	solAssert(
		_sourceType.getLocation() == ArrayType::Location::CallData ||
			_sourceType.getLocation() == ArrayType::Location::Storage,
		"Given array location not implemented."
	);

	IntegerType uint256(256);
	Type const* targetBaseType = _targetType.isByteArray() ? &uint256 : &(*_targetType.getBaseType());
	Type const* sourceBaseType = _sourceType.isByteArray() ? &uint256 : &(*_sourceType.getBaseType());

	// TODO unroll loop for small sizes

	bool sourceIsStorage = _sourceType.getLocation() == ArrayType::Location::Storage;
	bool directCopy = sourceIsStorage && sourceBaseType->isValueType() && *sourceBaseType == *targetBaseType;
	bool haveByteOffsetSource = !directCopy && sourceIsStorage && sourceBaseType->getStorageBytes() <= 16;
	bool haveByteOffsetTarget = !directCopy && targetBaseType->getStorageBytes() <= 16;
	unsigned byteOffsetSize = (haveByteOffsetSource ? 1 : 0) + (haveByteOffsetTarget ? 1 : 0);

	// stack: source_ref [source_byte_off] [source_length] target_ref target_byte_off
	// store target_ref
	// arrays always start at zero byte offset, pop offset
	m_context << eth::Instruction::POP;
	for (unsigned i = _sourceType.getSizeOnStack(); i > 0; --i)
		m_context << eth::swapInstruction(i);
	// stack: target_ref source_ref [source_byte_off] [source_length]
	if (sourceIsStorage)
		// arrays always start at zero byte offset, pop offset
		m_context << eth::Instruction::POP;
	// stack: target_ref source_ref [source_length]
	// retrieve source length
	if (_sourceType.getLocation() != ArrayType::Location::CallData || !_sourceType.isDynamicallySized())
		retrieveLength(_sourceType); // otherwise, length is already there
	// stack: target_ref source_ref source_length
	m_context << eth::Instruction::DUP3;
	// stack: target_ref source_ref source_length target_ref
	retrieveLength(_targetType);
	// stack: target_ref source_ref source_length target_ref target_length
	if (_targetType.isDynamicallySized())
		// store new target length
		m_context << eth::Instruction::DUP3 << eth::Instruction::DUP3 << eth::Instruction::SSTORE;
	if (sourceBaseType->getCategory() == Type::Category::Mapping)
	{
		solAssert(targetBaseType->getCategory() == Type::Category::Mapping, "");
		solAssert(_sourceType.getLocation() == ArrayType::Location::Storage, "");
		// nothing to copy
		m_context
			<< eth::Instruction::POP << eth::Instruction::POP
			<< eth::Instruction::POP << eth::Instruction::POP;
		m_context << u256(0);
		return;
	}
	// compute hashes (data positions)
	m_context << eth::Instruction::SWAP1;
	if (_targetType.isDynamicallySized())
		CompilerUtils(m_context).computeHashStatic();
	// stack: target_ref source_ref source_length target_length target_data_pos
	m_context << eth::Instruction::SWAP1;
	convertLengthToSize(_targetType);
	m_context << eth::Instruction::DUP2 << eth::Instruction::ADD;
	// stack: target_ref source_ref source_length target_data_pos target_data_end
	m_context << eth::Instruction::SWAP3;
	// stack: target_ref target_data_end source_length target_data_pos source_ref
	// skip copying if source length is zero
	m_context << eth::Instruction::DUP3 << eth::Instruction::ISZERO;
	eth::AssemblyItem copyLoopEndWithoutByteOffset = m_context.newTag();
	m_context.appendConditionalJumpTo(copyLoopEndWithoutByteOffset);

	if (_sourceType.getLocation() == ArrayType::Location::Storage && _sourceType.isDynamicallySized())
		CompilerUtils(m_context).computeHashStatic();
	// stack: target_ref target_data_end source_length target_data_pos source_data_pos
	m_context << eth::Instruction::SWAP2;
	convertLengthToSize(_sourceType);
	m_context << eth::Instruction::DUP3 << eth::Instruction::ADD;
	// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end
	if (haveByteOffsetTarget)
		m_context << u256(0);
	if (haveByteOffsetSource)
		m_context << u256(0);
	// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
	eth::AssemblyItem copyLoopStart = m_context.newTag();
	m_context << copyLoopStart;
	// check for loop condition
	m_context
		<< eth::dupInstruction(3 + byteOffsetSize) << eth::dupInstruction(2 + byteOffsetSize)
		<< eth::Instruction::GT << eth::Instruction::ISZERO;
	eth::AssemblyItem copyLoopEnd = m_context.newTag();
	m_context.appendConditionalJumpTo(copyLoopEnd);
	// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
	// copy
	if (sourceBaseType->getCategory() == Type::Category::Array)
	{
		solAssert(byteOffsetSize == 0, "Byte offset for array as base type.");
		m_context << eth::Instruction::DUP3;
//.........这里部分代码省略.........
开发者ID:gluk256,项目名称:cpp-ethereum,代码行数:101,代码来源:ArrayUtils.cpp


注:本文中的ArrayType::getLocation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。