Browse Source

Coding style: Prefix for const variables.

cl-refactor
Christian 10 years ago
parent
commit
1e3929d5ad
  1. 31
      libsolidity/Compiler.cpp
  2. 62
      libsolidity/ExpressionCompiler.cpp
  3. 38
      test/SolidityEndToEndTest.cpp

31
libsolidity/Compiler.cpp

@ -154,14 +154,15 @@ unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, b
//@todo this can be done more efficiently, saving some CALLDATALOAD calls //@todo this can be done more efficiently, saving some CALLDATALOAD calls
for (ASTPointer<VariableDeclaration> const& var: _function.getParameters()) for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
{ {
unsigned const numBytes = var->getType()->getCalldataEncodedSize(); unsigned const c_numBytes = var->getType()->getCalldataEncodedSize();
if (numBytes > 32) if (c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError() BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(var->getLocation()) << errinfo_sourceLocation(var->getLocation())
<< errinfo_comment("Type " + var->getType()->toString() + " not yet supported.")); << errinfo_comment("Type " + var->getType()->toString() + " not yet supported."));
bool const leftAligned = var->getType()->getCategory() == Type::Category::STRING; bool const c_leftAligned = var->getType()->getCategory() == Type::Category::STRING;
bool const padToWords = true; bool const c_padToWords = true;
dataOffset += CompilerUtils(m_context).loadFromMemory(dataOffset, numBytes, leftAligned, !_fromMemory, padToWords); dataOffset += CompilerUtils(m_context).loadFromMemory(dataOffset, c_numBytes, c_leftAligned,
!_fromMemory, c_padToWords);
} }
return dataOffset; return dataOffset;
} }
@ -182,9 +183,9 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function)
<< errinfo_comment("Type " + paramType.toString() + " not yet supported.")); << errinfo_comment("Type " + paramType.toString() + " not yet supported."));
CompilerUtils(m_context).copyToStackTop(stackDepth, paramType); CompilerUtils(m_context).copyToStackTop(stackDepth, paramType);
ExpressionCompiler::appendTypeConversion(m_context, paramType, paramType, true); ExpressionCompiler::appendTypeConversion(m_context, paramType, paramType, true);
bool const leftAligned = paramType.getCategory() == Type::Category::STRING; bool const c_leftAligned = paramType.getCategory() == Type::Category::STRING;
bool const padToWords = true; bool const c_padToWords = true;
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned, padToWords); dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, c_leftAligned, c_padToWords);
stackDepth -= paramType.getSizeOnStack(); stackDepth -= paramType.getSizeOnStack();
} }
// note that the stack is not cleaned up here // note that the stack is not cleaned up here
@ -231,16 +232,16 @@ bool Compiler::visit(FunctionDefinition const& _function)
// Note that the fact that the return arguments are of increasing index is vital for this // Note that the fact that the return arguments are of increasing index is vital for this
// algorithm to work. // algorithm to work.
unsigned const argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters()); unsigned const c_argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters());
unsigned const returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters()); unsigned const c_returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters());
unsigned const localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables()); unsigned const c_localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables());
vector<int> stackLayout; vector<int> stackLayout;
stackLayout.push_back(returnValuesSize); // target of return address stackLayout.push_back(c_returnValuesSize); // target of return address
stackLayout += vector<int>(argumentsSize, -1); // discard all arguments stackLayout += vector<int>(c_argumentsSize, -1); // discard all arguments
for (unsigned i = 0; i < returnValuesSize; ++i) for (unsigned i = 0; i < c_returnValuesSize; ++i)
stackLayout.push_back(i); stackLayout.push_back(i);
stackLayout += vector<int>(localVariablesSize, -1); stackLayout += vector<int>(c_localVariablesSize, -1);
while (stackLayout.back() != int(stackLayout.size() - 1)) while (stackLayout.back() != int(stackLayout.size() - 1))
if (stackLayout.back() < 0) if (stackLayout.back() < 0)

62
libsolidity/ExpressionCompiler.cpp

@ -144,23 +144,23 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
Expression const& leftExpression = _binaryOperation.getLeftExpression(); Expression const& leftExpression = _binaryOperation.getLeftExpression();
Expression const& rightExpression = _binaryOperation.getRightExpression(); Expression const& rightExpression = _binaryOperation.getRightExpression();
Type const& commonType = _binaryOperation.getCommonType(); Type const& commonType = _binaryOperation.getCommonType();
Token::Value const op = _binaryOperation.getOperator(); Token::Value const c_op = _binaryOperation.getOperator();
if (op == Token::AND || op == Token::OR) // special case: short-circuiting if (c_op == Token::AND || c_op == Token::OR) // special case: short-circuiting
appendAndOrOperatorCode(_binaryOperation); appendAndOrOperatorCode(_binaryOperation);
else if (commonType.getCategory() == Type::Category::INTEGER_CONSTANT) else if (commonType.getCategory() == Type::Category::INTEGER_CONSTANT)
m_context << commonType.literalValue(nullptr); m_context << commonType.literalValue(nullptr);
else else
{ {
bool cleanupNeeded = commonType.getCategory() == Type::Category::INTEGER && bool cleanupNeeded = commonType.getCategory() == Type::Category::INTEGER &&
(Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD); (Token::isCompareOp(c_op) || c_op == Token::DIV || c_op == Token::MOD);
// for commutative operators, push the literal as late as possible to allow improved optimization // for commutative operators, push the literal as late as possible to allow improved optimization
auto isLiteral = [](Expression const& _e) auto isLiteral = [](Expression const& _e)
{ {
return dynamic_cast<Literal const*>(&_e) || _e.getType()->getCategory() == Type::Category::INTEGER_CONSTANT; return dynamic_cast<Literal const*>(&_e) || _e.getType()->getCategory() == Type::Category::INTEGER_CONSTANT;
}; };
bool swap = m_optimize && Token::isCommutativeOp(op) && isLiteral(rightExpression) && !isLiteral(leftExpression); bool swap = m_optimize && Token::isCommutativeOp(c_op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
if (swap) if (swap)
{ {
leftExpression.accept(*this); leftExpression.accept(*this);
@ -175,10 +175,10 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
leftExpression.accept(*this); leftExpression.accept(*this);
appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded); appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded);
} }
if (Token::isCompareOp(op)) if (Token::isCompareOp(c_op))
appendCompareOperatorCode(op, commonType); appendCompareOperatorCode(c_op, commonType);
else else
appendOrdinaryBinaryOperatorCode(op, commonType); appendOrdinaryBinaryOperatorCode(c_op, commonType);
} }
// do not visit the child nodes, we already did that explicitly // do not visit the child nodes, we already did that explicitly
@ -327,14 +327,15 @@ bool ExpressionCompiler::visit(NewExpression const& _newExpression)
{ {
arguments[i]->accept(*this); arguments[i]->accept(*this);
appendTypeConversion(*arguments[i]->getType(), *types[i], true); appendTypeConversion(*arguments[i]->getType(), *types[i], true);
unsigned const numBytes = types[i]->getCalldataEncodedSize(); unsigned const c_numBytes = types[i]->getCalldataEncodedSize();
if (numBytes > 32) if (c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError() BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(arguments[i]->getLocation()) << errinfo_sourceLocation(arguments[i]->getLocation())
<< errinfo_comment("Type " + types[i]->toString() + " not yet supported.")); << errinfo_comment("Type " + types[i]->toString() + " not yet supported."));
bool const leftAligned = types[i]->getCategory() == Type::Category::STRING; bool const c_leftAligned = types[i]->getCategory() == Type::Category::STRING;
bool const padToWords = true; bool const c_padToWords = true;
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned, padToWords); dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, c_numBytes,
c_leftAligned, c_padToWords);
} }
// size, offset, endowment // size, offset, endowment
m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE; m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE;
@ -462,12 +463,12 @@ void ExpressionCompiler::endVisit(Literal const& _literal)
void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation) void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation)
{ {
Token::Value const op = _binaryOperation.getOperator(); Token::Value const c_op = _binaryOperation.getOperator();
solAssert(op == Token::OR || op == Token::AND, ""); solAssert(c_op == Token::OR || c_op == Token::AND, "");
_binaryOperation.getLeftExpression().accept(*this); _binaryOperation.getLeftExpression().accept(*this);
m_context << eth::Instruction::DUP1; m_context << eth::Instruction::DUP1;
if (op == Token::AND) if (c_op == Token::AND)
m_context << eth::Instruction::ISZERO; m_context << eth::Instruction::ISZERO;
eth::AssemblyItem endLabel = m_context.appendConditionalJump(); eth::AssemblyItem endLabel = m_context.appendConditionalJump();
m_context << eth::Instruction::POP; m_context << eth::Instruction::POP;
@ -486,23 +487,23 @@ void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type
else else
{ {
IntegerType const& type = dynamic_cast<IntegerType const&>(_type); IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
bool const isSigned = type.isSigned(); bool const c_isSigned = type.isSigned();
switch (_operator) switch (_operator)
{ {
case Token::GTE: case Token::GTE:
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT) m_context << (c_isSigned ? eth::Instruction::SLT : eth::Instruction::LT)
<< eth::Instruction::ISZERO; << eth::Instruction::ISZERO;
break; break;
case Token::LTE: case Token::LTE:
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT) m_context << (c_isSigned ? eth::Instruction::SGT : eth::Instruction::GT)
<< eth::Instruction::ISZERO; << eth::Instruction::ISZERO;
break; break;
case Token::GT: case Token::GT:
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT); m_context << (c_isSigned ? eth::Instruction::SGT : eth::Instruction::GT);
break; break;
case Token::LT: case Token::LT:
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT); m_context << (c_isSigned ? eth::Instruction::SLT : eth::Instruction::LT);
break; break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown comparison operator.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown comparison operator."));
@ -525,7 +526,7 @@ void ExpressionCompiler::appendOrdinaryBinaryOperatorCode(Token::Value _operator
void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Type const& _type) void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Type const& _type)
{ {
IntegerType const& type = dynamic_cast<IntegerType const&>(_type); IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
bool const isSigned = type.isSigned(); bool const c_isSigned = type.isSigned();
switch (_operator) switch (_operator)
{ {
@ -539,10 +540,10 @@ void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Ty
m_context << eth::Instruction::MUL; m_context << eth::Instruction::MUL;
break; break;
case Token::DIV: case Token::DIV:
m_context << (isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV); m_context << (c_isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV);
break; break;
case Token::MOD: case Token::MOD:
m_context << (isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD); m_context << (c_isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD);
break; break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown arithmetic operator.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown arithmetic operator."));
@ -634,14 +635,15 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
_arguments[i]->accept(*this); _arguments[i]->accept(*this);
Type const& type = *_functionType.getParameterTypes()[i]; Type const& type = *_functionType.getParameterTypes()[i];
appendTypeConversion(*_arguments[i]->getType(), type, true); appendTypeConversion(*_arguments[i]->getType(), type, true);
unsigned const numBytes = type.getCalldataEncodedSize(); unsigned const c_numBytes = type.getCalldataEncodedSize();
if (numBytes == 0 || numBytes > 32) if (c_numBytes == 0 || c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError() BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_arguments[i]->getLocation()) << errinfo_sourceLocation(_arguments[i]->getLocation())
<< errinfo_comment("Type " + type.toString() + " not yet supported.")); << errinfo_comment("Type " + type.toString() + " not yet supported."));
bool const leftAligned = type.getCategory() == Type::Category::STRING; bool const c_leftAligned = type.getCategory() == Type::Category::STRING;
bool const padToWords = true; bool const c_padToWords = true;
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned, padToWords); dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, c_numBytes,
c_leftAligned, c_padToWords);
} }
//@todo only return the first return value for now //@todo only return the first return value for now
Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr : Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
@ -662,8 +664,8 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
if (retSize > 0) if (retSize > 0)
{ {
bool const leftAligned = firstType->getCategory() == Type::Category::STRING; bool const c_leftAligned = firstType->getCategory() == Type::Category::STRING;
CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned, false, true); CompilerUtils(m_context).loadFromMemory(0, retSize, c_leftAligned, false, true);
} }
} }

38
test/SolidityEndToEndTest.cpp

@ -1054,10 +1054,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls)
} }
})"; })";
compileAndRun(sourceCode, 0, "Helper"); compileAndRun(sourceCode, 0, "Helper");
u160 const helperAddress = m_contractAddress; u160 const c_helperAddress = m_contractAddress;
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
u256 a(3456789); u256 a(3456789);
u256 b("0x282837623374623234aa74"); u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b)); BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b));
@ -1084,10 +1084,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters)
} }
})"; })";
compileAndRun(sourceCode, 0, "Helper"); compileAndRun(sourceCode, 0, "Helper");
u160 const helperAddress = m_contractAddress; u160 const c_helperAddress = m_contractAddress;
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
u256 a(3456789); u256 a(3456789);
u256 b("0x282837623374623234aa74"); u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3)); BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3));
@ -1115,11 +1115,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this)
} }
})"; })";
compileAndRun(sourceCode, 0, "Helper"); compileAndRun(sourceCode, 0, "Helper");
u160 const helperAddress = m_contractAddress; u160 const c_helperAddress = m_contractAddress;
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(c_helperAddress));
} }
BOOST_AUTO_TEST_CASE(calls_to_this) BOOST_AUTO_TEST_CASE(calls_to_this)
@ -1146,10 +1146,10 @@ BOOST_AUTO_TEST_CASE(calls_to_this)
} }
})"; })";
compileAndRun(sourceCode, 0, "Helper"); compileAndRun(sourceCode, 0, "Helper");
u160 const helperAddress = m_contractAddress; u160 const c_helperAddress = m_contractAddress;
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
u256 a(3456789); u256 a(3456789);
u256 b("0x282837623374623234aa74"); u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10)); BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10));
@ -1181,10 +1181,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars)
} }
})"; })";
compileAndRun(sourceCode, 0, "Helper"); compileAndRun(sourceCode, 0, "Helper");
u160 const helperAddress = m_contractAddress; u160 const c_helperAddress = m_contractAddress;
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
u256 a(3456789); u256 a(3456789);
u256 b("0x282837623374623234aa74"); u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9)); BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9));
@ -1211,10 +1211,10 @@ BOOST_AUTO_TEST_CASE(strings_in_calls)
} }
})"; })";
compileAndRun(sourceCode, 0, "Helper"); compileAndRun(sourceCode, 0, "Helper");
u160 const helperAddress = m_contractAddress; u160 const c_helperAddress = m_contractAddress;
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
BOOST_CHECK(callContractFunction("callHelper(string2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5))); BOOST_CHECK(callContractFunction("callHelper(string2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5)));
} }

Loading…
Cancel
Save