mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6521 from ethereum/type_conv_yul-6479
Yul Generation: conversions
This commit is contained in:
commit
96688717a2
@ -90,15 +90,12 @@ bool IRGeneratorForStatements::visit(VariableDeclarationStatement const& _varDec
|
||||
|
||||
expression->accept(*this);
|
||||
|
||||
solUnimplementedAssert(
|
||||
*expression->annotation().type == *_varDeclStatement.declarations().front()->type(),
|
||||
"Type conversion not yet implemented"
|
||||
);
|
||||
VariableDeclaration const& varDecl = *_varDeclStatement.declarations().front();
|
||||
m_code <<
|
||||
"let " <<
|
||||
m_context.variableName(*_varDeclStatement.declarations().front()) <<
|
||||
m_context.variableName(varDecl) <<
|
||||
" := " <<
|
||||
m_context.variable(*expression) <<
|
||||
expressionAsType(*expression, *varDecl.type()) <<
|
||||
"\n";
|
||||
}
|
||||
else
|
||||
@ -115,18 +112,25 @@ bool IRGeneratorForStatements::visit(Assignment const& _assignment)
|
||||
|
||||
_assignment.rightHandSide().accept(*this);
|
||||
|
||||
// solUnimplementedAssert(
|
||||
// *_assignment.rightHandSide().annotation().type == *_assignment.leftHandSide().annotation().type,
|
||||
// "Type conversion not yet implemented"
|
||||
// );
|
||||
// TODO proper lvalue handling
|
||||
auto const& identifier = dynamic_cast<Identifier const&>(_assignment.leftHandSide());
|
||||
string varName = m_context.variableName(dynamic_cast<VariableDeclaration const&>(*identifier.annotation().referencedDeclaration));
|
||||
m_code << varName << " := " << m_context.variable(_assignment.rightHandSide()) << "\n";
|
||||
auto const& lvalue = dynamic_cast<Identifier const&>(_assignment.leftHandSide());
|
||||
string varName = m_context.variableName(dynamic_cast<VariableDeclaration const&>(*lvalue.annotation().referencedDeclaration));
|
||||
|
||||
m_code <<
|
||||
varName <<
|
||||
" := " <<
|
||||
expressionAsType(_assignment.rightHandSide(), *lvalue.annotation().type) <<
|
||||
"\n";
|
||||
m_code << "let " << m_context.variable(_assignment) << " := " << varName << "\n";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IRGeneratorForStatements::visit(Return const&)
|
||||
{
|
||||
solUnimplemented("Return not yet implemented in yul code generation");
|
||||
}
|
||||
|
||||
void IRGeneratorForStatements::endVisit(BinaryOperation const& _binOp)
|
||||
{
|
||||
solUnimplementedAssert(_binOp.getOperator() == Token::Add, "");
|
||||
@ -151,8 +155,31 @@ void IRGeneratorForStatements::endVisit(BinaryOperation const& _binOp)
|
||||
|
||||
bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
|
||||
{
|
||||
solUnimplementedAssert(_functionCall.annotation().kind == FunctionCallKind::FunctionCall, "");
|
||||
FunctionTypePointer functionType = dynamic_cast<FunctionType const*>(_functionCall.expression().annotation().type);
|
||||
solUnimplementedAssert(
|
||||
_functionCall.annotation().kind == FunctionCallKind::FunctionCall ||
|
||||
_functionCall.annotation().kind == FunctionCallKind::TypeConversion,
|
||||
"This type of function call is not yet implemented"
|
||||
);
|
||||
|
||||
TypePointer const funcType = _functionCall.expression().annotation().type;
|
||||
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::TypeConversion)
|
||||
{
|
||||
solAssert(funcType->category() == Type::Category::TypeType, "Expected category to be TypeType");
|
||||
solAssert(_functionCall.arguments().size() == 1, "Expected one argument for type conversion");
|
||||
_functionCall.arguments().front()->accept(*this);
|
||||
|
||||
m_code <<
|
||||
"let " <<
|
||||
m_context.variable(_functionCall) <<
|
||||
" := " <<
|
||||
expressionAsType(*_functionCall.arguments().front(), *_functionCall.annotation().type) <<
|
||||
"\n";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
FunctionTypePointer functionType = dynamic_cast<FunctionType const*>(funcType);
|
||||
|
||||
TypePointers parameterTypes = functionType->parameterTypes();
|
||||
vector<ASTPointer<Expression const>> const& callArguments = _functionCall.arguments();
|
||||
@ -185,9 +212,11 @@ bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
|
||||
for (unsigned i = 0; i < arguments.size(); ++i)
|
||||
{
|
||||
arguments[i]->accept(*this);
|
||||
// TODO convert
|
||||
//utils().convertType(*arguments[i]->annotation().type, *function.parameterTypes()[i]);
|
||||
args.emplace_back(m_context.variable(*arguments[i]));
|
||||
|
||||
if (functionType->takesArbitraryParameters())
|
||||
args.emplace_back(m_context.variable(*arguments[i]));
|
||||
else
|
||||
args.emplace_back(expressionAsType(*arguments[i], *parameterTypes[i]));
|
||||
}
|
||||
|
||||
if (auto identifier = dynamic_cast<Identifier const*>(&_functionCall.expression()))
|
||||
@ -273,3 +302,14 @@ bool IRGeneratorForStatements::visit(Literal const& _literal)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to)
|
||||
{
|
||||
Type const& from = *_expression.annotation().type;
|
||||
string varName = m_context.variable(_expression);
|
||||
|
||||
if (from == _to)
|
||||
return varName;
|
||||
else
|
||||
return m_utils.conversionFunction(from, _to) + "(" + std::move(varName) + ")";
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ public:
|
||||
|
||||
bool visit(VariableDeclarationStatement const& _variableDeclaration) override;
|
||||
bool visit(Assignment const& _assignment) override;
|
||||
bool visit(Return const& _return) override;
|
||||
void endVisit(BinaryOperation const& _binOp) override;
|
||||
bool visit(FunctionCall const& _funCall) override;
|
||||
bool visit(InlineAssembly const& _inlineAsm) override;
|
||||
@ -53,6 +54,10 @@ public:
|
||||
bool visit(Literal const& _literal) override;
|
||||
|
||||
private:
|
||||
/// @returns a Yul expression representing the current value of @a _expression,
|
||||
/// converted to type @a _to if it does not yet have that type.
|
||||
std::string expressionAsType(Expression const& _expression, Type const& _to);
|
||||
|
||||
std::ostringstream m_code;
|
||||
IRGenerationContext& m_context;
|
||||
YulUtilFunctions& m_utils;
|
||||
|
@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
function f() public pure returns (uint16 x) {
|
||||
uint8 y = uint8(0x12345678);
|
||||
x = y;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f() -> 0x78
|
@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
function f(bytes32 b) public pure returns (bytes32 x) {
|
||||
x = b;
|
||||
}
|
||||
function g() public pure returns (bytes32 x) {
|
||||
x = f(bytes4(uint32(0x12345678)));
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// g() -> 0x1234567800000000000000000000000000000000000000000000000000000000
|
@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
function f(uint a) public pure returns (uint8 x) {
|
||||
uint8 b = uint8(a);
|
||||
x = b;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f(uint256): 0x12345678 -> 0x78
|
@ -0,0 +1,14 @@
|
||||
// Tests IRGeneratorForStatements::visit(Assignment const& _assignment)
|
||||
contract C {
|
||||
function f() public pure returns (uint16 x) {
|
||||
uint8 y;
|
||||
assembly {
|
||||
y := 0x12345678
|
||||
}
|
||||
x = y;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f() -> 0x78
|
@ -0,0 +1,17 @@
|
||||
// IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
|
||||
contract C {
|
||||
function f(uint b) public pure returns (uint x) {
|
||||
x = b;
|
||||
}
|
||||
function g() public pure returns (uint x) {
|
||||
uint8 a;
|
||||
assembly {
|
||||
a := 0x12345678
|
||||
}
|
||||
x = f(a);
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// g() -> 0x78
|
@ -0,0 +1,13 @@
|
||||
// IRGeneratorForStatements::visit(VariableDeclarationStatement const& _varDeclStatement)
|
||||
contract C {
|
||||
function f() public pure returns (uint y) {
|
||||
uint8 a;
|
||||
assembly { a := 0x12345678 }
|
||||
uint z = a;
|
||||
y = z;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f() -> 0x78
|
Loading…
Reference in New Issue
Block a user