Helper function to define the value of expressions.

This commit is contained in:
chriseth 2019-04-25 00:21:00 +02:00
parent 0eef51ffa4
commit e66ab6c036
2 changed files with 14 additions and 23 deletions

View File

@ -122,7 +122,7 @@ bool IRGeneratorForStatements::visit(Assignment const& _assignment)
" := " << " := " <<
expressionAsType(_assignment.rightHandSide(), *lvalue.annotation().type) << expressionAsType(_assignment.rightHandSide(), *lvalue.annotation().type) <<
"\n"; "\n";
m_code << "let " << m_context.variable(_assignment) << " := " << varName << "\n"; defineExpression(_assignment) << varName << "\n";
return false; return false;
} }
@ -197,10 +197,7 @@ void IRGeneratorForStatements::endVisit(BinaryOperation const& _binOp)
// special case: short-circuiting // special case: short-circuiting
solUnimplementedAssert(false, ""); solUnimplementedAssert(false, "");
else if (commonType->category() == Type::Category::RationalNumber) else if (commonType->category() == Type::Category::RationalNumber)
m_code << defineExpression(_binOp) <<
"let " <<
m_context.variable(_binOp) <<
" := " <<
toCompactHexWithPrefix(commonType->literalValue(nullptr)) << toCompactHexWithPrefix(commonType->literalValue(nullptr)) <<
"\n"; "\n";
else else
@ -209,10 +206,7 @@ void IRGeneratorForStatements::endVisit(BinaryOperation const& _binOp)
if (IntegerType const* type = dynamic_cast<IntegerType const*>(commonType)) if (IntegerType const* type = dynamic_cast<IntegerType const*>(commonType))
{ {
solUnimplementedAssert(!type->isSigned(), ""); solUnimplementedAssert(!type->isSigned(), "");
m_code << defineExpression(_binOp) <<
"let " <<
m_context.variable(_binOp) <<
" := " <<
m_utils.overflowCheckedUIntAddFunction(type->numBits()) << m_utils.overflowCheckedUIntAddFunction(type->numBits()) <<
"(" << "(" <<
expressionAsType(_binOp.leftExpression(), *commonType) << expressionAsType(_binOp.leftExpression(), *commonType) <<
@ -241,10 +235,7 @@ bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
solAssert(_functionCall.arguments().size() == 1, "Expected one argument for type conversion"); solAssert(_functionCall.arguments().size() == 1, "Expected one argument for type conversion");
_functionCall.arguments().front()->accept(*this); _functionCall.arguments().front()->accept(*this);
m_code << defineExpression(_functionCall) <<
"let " <<
m_context.variable(_functionCall) <<
" := " <<
expressionAsType(*_functionCall.arguments().front(), *_functionCall.annotation().type) << expressionAsType(*_functionCall.arguments().front(), *_functionCall.annotation().type) <<
"\n"; "\n";
@ -297,10 +288,7 @@ bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
if (auto functionDef = dynamic_cast<FunctionDefinition const*>(identifier->annotation().referencedDeclaration)) if (auto functionDef = dynamic_cast<FunctionDefinition const*>(identifier->annotation().referencedDeclaration))
{ {
// @TODO The function can very well return multiple vars. // @TODO The function can very well return multiple vars.
m_code << defineExpression(_functionCall) <<
"let " <<
m_context.variable(_functionCall) <<
" := " <<
m_context.virtualFunctionName(*functionDef) << m_context.virtualFunctionName(*functionDef) <<
"(" << "(" <<
joinHumanReadable(args) << joinHumanReadable(args) <<
@ -313,10 +301,7 @@ bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
// @TODO The function can very well return multiple vars. // @TODO The function can very well return multiple vars.
args = vector<string>{m_context.variable(_functionCall.expression())} + args; args = vector<string>{m_context.variable(_functionCall.expression())} + args;
m_code << defineExpression(_functionCall) <<
"let " <<
m_context.variable(_functionCall) <<
" := " <<
m_context.internalDispatch(functionType->parameterTypes().size(), functionType->returnParameterTypes().size()) << m_context.internalDispatch(functionType->parameterTypes().size(), functionType->returnParameterTypes().size()) <<
"(" << "(" <<
joinHumanReadable(args) << joinHumanReadable(args) <<
@ -351,7 +336,7 @@ bool IRGeneratorForStatements::visit(Identifier const& _identifier)
value = m_context.variableName(*varDecl); value = m_context.variableName(*varDecl);
else else
solUnimplemented(""); solUnimplemented("");
m_code << "let " << m_context.variable(_identifier) << " := " << value << "\n"; defineExpression(_identifier) << value << "\n";
return false; return false;
} }
@ -364,7 +349,7 @@ bool IRGeneratorForStatements::visit(Literal const& _literal)
case Type::Category::RationalNumber: case Type::Category::RationalNumber:
case Type::Category::Bool: case Type::Category::Bool:
case Type::Category::Address: case Type::Category::Address:
m_code << "let " << m_context.variable(_literal) << " := " << toCompactHexWithPrefix(type->literalValue(&_literal)) << "\n"; defineExpression(_literal) << toCompactHexWithPrefix(type->literalValue(&_literal)) << "\n";
break; break;
case Type::Category::StringLiteral: case Type::Category::StringLiteral:
solUnimplemented(""); solUnimplemented("");
@ -385,3 +370,8 @@ string IRGeneratorForStatements::expressionAsType(Expression const& _expression,
else else
return m_utils.conversionFunction(from, _to) + "(" + std::move(varName) + ")"; return m_utils.conversionFunction(from, _to) + "(" + std::move(varName) + ")";
} }
ostream& IRGeneratorForStatements::defineExpression(Expression const& _expression)
{
return m_code << "let " << m_context.variable(_expression) << " := ";
}

View File

@ -60,6 +60,7 @@ private:
/// @returns a Yul expression representing the current value of @a _expression, /// @returns a Yul expression representing the current value of @a _expression,
/// converted to type @a _to if it does not yet have that type. /// converted to type @a _to if it does not yet have that type.
std::string expressionAsType(Expression const& _expression, Type const& _to); std::string expressionAsType(Expression const& _expression, Type const& _to);
std::ostream& defineExpression(Expression const& _expression);
std::ostringstream m_code; std::ostringstream m_code;
IRGenerationContext& m_context; IRGenerationContext& m_context;