Parsing of labels with stack info.

This commit is contained in:
chriseth 2017-02-01 21:40:50 +01:00
parent fd62adebf3
commit 98e343b3fc
4 changed files with 40 additions and 3 deletions

View File

@ -91,6 +91,7 @@ public:
void operator()(T const& /*_item*/) { } void operator()(T const& /*_item*/) { }
void operator()(Label const& _item) void operator()(Label const& _item)
{ {
solAssert(_item.stackInfo.empty(), "Labels with stack info not yet supported.");
if (m_state.labels.count(_item.name)) if (m_state.labels.count(_item.name))
//@TODO secondary location //@TODO secondary location
m_state.addError( m_state.addError(

View File

@ -42,8 +42,8 @@ struct Literal { SourceLocation location; bool isNumber; std::string value; };
/// External / internal identifier or label reference /// External / internal identifier or label reference
struct Identifier { SourceLocation location; std::string name; }; struct Identifier { SourceLocation location; std::string name; };
struct FunctionalInstruction; struct FunctionalInstruction;
/// Jump label ("name:") /// Jump label ("name:" or "name [x, y, z]:" or "name [-3]:")
struct Label { SourceLocation location; std::string name; }; struct Label { SourceLocation location; std::string name; std::vector<std::string> stackInfo; };
/// Assignemnt (":= x", moves stack top into x, potentially multiple slots) /// Assignemnt (":= x", moves stack top into x, potentially multiple slots)
struct Assignment { SourceLocation location; Identifier variableName; }; struct Assignment { SourceLocation location; Identifier variableName; };
struct FunctionalAssignment; struct FunctionalAssignment;

View File

@ -121,6 +121,38 @@ assembly::Statement Parser::parseStatement()
return label; return label;
} }
} }
case Token::LBrack:
{
if (statement.type() != typeid(assembly::Identifier))
fatalParserError("Label name must precede \"[\".");
assembly::Identifier const& identifier = boost::get<assembly::Identifier>(statement);
Label label = createWithLocation<Label>(identifier.location);
label.name = identifier.name;
m_scanner->next();
if (m_scanner->currentToken() == Token::Number)
{
label.stackInfo.push_back(m_scanner->currentLiteral());
m_scanner->next();
}
else if (m_scanner->currentToken() == Token::Sub)
{
m_scanner->next();
label.stackInfo.push_back("-" + m_scanner->currentLiteral());
expectToken(Token::Number);
}
else
while (true)
{
label.stackInfo.push_back(expectAsmIdentifier());
if (m_scanner->currentToken() == Token::RBrack)
break;
expectToken(Token::Comma);
}
expectToken(Token::RBrack);
label.location.end = endPosition();
expectToken(Token::Colon);
return label;
}
default: default:
break; break;
} }

View File

@ -94,7 +94,11 @@ string AsmPrinter::operator()(assembly::FunctionalInstruction const& _functional
string AsmPrinter::operator()(assembly::Label const& _label) string AsmPrinter::operator()(assembly::Label const& _label)
{ {
return _label.name + ":"; return _label.name + (
!_label.stackInfo.empty() ?
"[" + boost::algorithm::join(_label.stackInfo, ", ") + "]" :
string()
) + ":";
} }
string AsmPrinter::operator()(assembly::Assignment const& _assignment) string AsmPrinter::operator()(assembly::Assignment const& _assignment)