Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong native locations in optimized IR AST #15228

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bugfixes:
* SMTChecker: Fix formatting of unary minus expressions in invariants.
* SMTChecker: Fix internal compiler error when reporting proved targets for BMC engine.
* TypeChecker: Fix segfault when assigning nested tuple to tuple.
* Yul AST: Fix ``nativeSrc`` attributes in optimized IR AST referring to locations in unoptimized IR.
nikola-matic marked this conversation as resolved.
Show resolved Hide resolved
* Yul IR Code Generation: Deterministic order of Yul subobjects.
* Yul Optimizer: Name simplification could lead to forbidden identifiers with a leading and/or trailing dot, e.g., ``x._`` would get simplified into ``x.``.
* Yul Parser: Fix segfault when parsing very long location comments.
Expand Down
47 changes: 29 additions & 18 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,25 +1464,36 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
);
}

yul::YulStack stack(
m_evmVersion,
m_eofVersion,
yul::YulStack::Language::StrictAssembly,
m_optimiserSettings,
m_debugInfoSelection
);
bool yulAnalysisSuccessful = stack.parseAndAnalyze("", compiledContract.yulIR);
solAssert(
yulAnalysisSuccessful,
compiledContract.yulIR + "\n\n"
"Invalid IR generated:\n" +
langutil::SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack) + "\n"
);
auto const parseYul = [&](std::string const& _irSource) {
YulStack stack(
m_evmVersion,
m_eofVersion,
YulStack::Language::StrictAssembly,
m_optimiserSettings,
m_debugInfoSelection
);
bool yulAnalysisSuccessful = stack.parseAndAnalyze("", _irSource);
solAssert(
yulAnalysisSuccessful,
_irSource + "\n\n"
"Invalid IR generated:\n" +
langutil::SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack) + "\n"
);
return stack;
};

compiledContract.yulIRAst = stack.astJson();
stack.optimize();
compiledContract.yulIROptimized = stack.print(this);
compiledContract.yulIROptimizedAst = stack.astJson();
{
YulStack stack = parseYul(compiledContract.yulIR);
compiledContract.yulIRAst = stack.astJson();
stack.optimize();
compiledContract.yulIROptimized = stack.print(this);
}
{
// Optimizer does not maintain correct native source locations in the AST.
// We can work around it by regenerating the AST from scratch from optimized IR.
YulStack stack = parseYul(compiledContract.yulIROptimized);
compiledContract.yulIROptimizedAst = stack.astJson();
nikola-matic marked this conversation as resolved.
Show resolved Hide resolved
}
}

void CompilerStack::generateEVMFromIR(ContractDefinition const& _contract)
Expand Down
4 changes: 2 additions & 2 deletions libyul/YulStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::yul;
using namespace solidity::langutil;
using namespace solidity::util;

namespace
{
Expand Down Expand Up @@ -257,8 +258,7 @@ MachineAssemblyObject YulStack::assemble(Machine _machine)
case Machine::EVM:
return assembleWithDeployed().first;
}
// unreachable
return MachineAssemblyObject();
unreachable();
}

std::pair<MachineAssemblyObject, MachineAssemblyObject>
Expand Down
2 changes: 0 additions & 2 deletions libyul/YulStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ class YulStack: public langutil::CharStreamProvider
std::shared_ptr<yul::Object> m_parserResult;
langutil::ErrorList m_errors;
langutil::ErrorReporter m_errorReporter;

std::unique_ptr<std::string> m_sourceMappings;
};

}
Loading