Bug 1265461: Baldr: have if/else accept then/else plus an expression list; r=luke

MozReview-Commit-ID: B3vo0kFF6j9

--HG--
extra : rebase_source : b2bdc703eeba3deedcbbcaa2cdda2f1c5715aa7f
extra : histedit_source : 8eb9dede653dd7a3b042e67d5750a9c674322ce0
This commit is contained in:
Benjamin Bouvier 2016-05-10 12:37:53 +02:00
parent 27d6ab2516
commit 50e90e1537

View File

@ -392,22 +392,29 @@ class WasmAstReturn : public WasmAstExpr
class WasmAstIf : public WasmAstExpr
{
WasmAstExpr* cond_;
WasmAstExpr* thenBranch_;
WasmAstExpr* elseBranch_;
WasmName thenName_;
WasmAstExprVector thenExprs_;
WasmName elseName_;
WasmAstExprVector elseExprs_;
public:
static const WasmAstExprKind Kind = WasmAstExprKind::If;
WasmAstIf(WasmAstExpr* cond, WasmAstExpr* thenBranch, WasmAstExpr* elseBranch)
WasmAstIf(WasmAstExpr* cond, WasmName thenName, WasmAstExprVector&& thenExprs,
WasmName elseName, WasmAstExprVector&& elseExprs)
: WasmAstExpr(Kind),
cond_(cond),
thenBranch_(thenBranch),
elseBranch_(elseBranch)
thenName_(thenName),
thenExprs_(Move(thenExprs)),
elseName_(elseName),
elseExprs_(Move(elseExprs))
{}
WasmAstExpr& cond() const { return *cond_; }
WasmAstExpr& thenBranch() const { return *thenBranch_; }
bool hasElse() const { return !!elseBranch_; }
WasmAstExpr& elseBranch() const { MOZ_ASSERT(hasElse()); return *elseBranch_; }
const WasmAstExprVector& thenExprs() const { return thenExprs_; }
bool hasElse() const { return elseExprs_.length(); }
const WasmAstExprVector& elseExprs() const { MOZ_ASSERT(hasElse()); return elseExprs_; }
WasmName thenName() const { return thenName_; }
WasmName elseName() const { return elseName_; }
};
class WasmAstLoadStoreAddress
@ -806,6 +813,7 @@ class WasmToken
ComparisonOpcode,
Const,
ConversionOpcode,
Else,
EndOfFile,
Equal,
Error,
@ -837,6 +845,7 @@ class WasmToken
Table,
TernaryOpcode,
Text,
Then,
Type,
UnaryOpcode,
Unreachable,
@ -1475,6 +1484,8 @@ WasmTokenStream::next()
break;
case 'e':
if (consume(MOZ_UTF16("else")))
return WasmToken(WasmToken::Else, begin, cur_);
if (consume(MOZ_UTF16("export")))
return WasmToken(WasmToken::Export, begin, cur_);
break;
@ -2034,6 +2045,8 @@ WasmTokenStream::next()
case 't':
if (consume(MOZ_UTF16("table")))
return WasmToken(WasmToken::Table, begin, cur_);
if (consume(MOZ_UTF16("then")))
return WasmToken(WasmToken::Then, begin, cur_);
if (consume(MOZ_UTF16("type")))
return WasmToken(WasmToken::Type, begin, cur_);
break;
@ -2099,6 +2112,19 @@ ParseExpr(WasmParseContext& c)
return expr;
}
static bool
ParseExprList(WasmParseContext& c, WasmAstExprVector* exprs)
{
while (c.ts.getIf(WasmToken::OpenParen)) {
WasmAstExpr* expr = ParseExprInsideParens(c);
if (!expr || !exprs->append(expr))
return false;
if (!c.ts.match(WasmToken::CloseParen, c.error))
return false;
}
return true;
}
static WasmAstBlock*
ParseBlock(WasmParseContext& c, Expr expr)
{
@ -2110,13 +2136,8 @@ ParseBlock(WasmParseContext& c, Expr expr)
if (expr == Expr::Loop)
continueName = c.ts.getIfName();
while (c.ts.getIf(WasmToken::OpenParen)) {
WasmAstExpr* expr = ParseExprInsideParens(c);
if (!expr || !exprs.append(expr))
return nullptr;
if (!c.ts.match(WasmToken::CloseParen, c.error))
return nullptr;
}
if (!ParseExprList(c, &exprs))
return nullptr;
return new(c.lifo) WasmAstBlock(expr, breakName, continueName, Move(exprs));
}
@ -2637,20 +2658,40 @@ ParseIf(WasmParseContext& c)
if (!cond)
return nullptr;
WasmAstExpr* thenBranch = ParseExpr(c);
if (!thenBranch)
if (!c.ts.match(WasmToken::OpenParen, c.error))
return nullptr;
WasmAstExpr* elseBranch = nullptr;
if (c.ts.getIf(WasmToken::OpenParen)) {
elseBranch = ParseExprInsideParens(c);
if (!elseBranch)
WasmName thenName;
WasmAstExprVector thenExprs(c.lifo);
if (c.ts.getIf(WasmToken::Then)) {
thenName = c.ts.getIfName();
if (!ParseExprList(c, &thenExprs))
return nullptr;
} else {
WasmAstExpr* thenBranch = ParseExprInsideParens(c);
if (!thenBranch || !thenExprs.append(thenBranch))
return nullptr;
}
if (!c.ts.match(WasmToken::CloseParen, c.error))
return nullptr;
WasmName elseName;
WasmAstExprVector elseExprs(c.lifo);
if (c.ts.getIf(WasmToken::OpenParen)) {
if (c.ts.getIf(WasmToken::Else)) {
elseName = c.ts.getIfName();
if (!ParseExprList(c, &elseExprs))
return nullptr;
} else {
WasmAstExpr* elseBranch = ParseExprInsideParens(c);
if (!elseBranch || !elseExprs.append(elseBranch))
return nullptr;
}
if (!c.ts.match(WasmToken::CloseParen, c.error))
return nullptr;
}
return new(c.lifo) WasmAstIf(cond, thenBranch, elseBranch);
return new(c.lifo) WasmAstIf(cond, thenName, Move(thenExprs), elseName, Move(elseExprs));
}
static bool
@ -3359,6 +3400,16 @@ class Resolver
static bool
ResolveExpr(Resolver& r, WasmAstExpr& expr);
static bool
ResolveExprList(Resolver& r, const WasmAstExprVector& v)
{
for (size_t i = 0; i < v.length(); i++) {
if (!ResolveExpr(r, *v[i]))
return false;
}
return true;
}
static bool
ResolveBlock(Resolver& r, WasmAstBlock& b)
{
@ -3370,11 +3421,8 @@ ResolveBlock(Resolver& r, WasmAstBlock& b)
return false;
}
size_t numExprs = b.exprs().length();
for (size_t i = 0; i < numExprs; i++) {
if (!ResolveExpr(r, *b.exprs()[i]))
return false;
}
if (!ResolveExprList(r, b.exprs()))
return false;
if (b.expr() == Expr::Loop)
r.popTarget(b.continueName());
@ -3500,15 +3548,18 @@ ResolveIfElse(Resolver& r, WasmAstIf& i)
{
if (!ResolveExpr(r, i.cond()))
return false;
if (!r.pushTarget(WasmName()))
if (!r.pushTarget(i.thenName()))
return false;
if (!ResolveExpr(r, i.thenBranch()))
if (!ResolveExprList(r, i.thenExprs()))
return false;
r.popTarget(i.thenName());
if (i.hasElse()) {
if (!ResolveExpr(r, i.elseBranch()))
if (!r.pushTarget(i.elseName()))
return false;
if (!ResolveExprList(r, i.elseExprs()))
return false;
r.popTarget(i.elseName());
}
r.popTarget(WasmName());
return true;
}
@ -3678,18 +3729,24 @@ ResolveModule(LifoAlloc& lifo, WasmAstModule* module, UniqueChars* error)
static bool
EncodeExpr(Encoder& e, WasmAstExpr& expr);
static bool
EncodeExprList(Encoder& e, const WasmAstExprVector& v)
{
for (size_t i = 0; i < v.length(); i++) {
if (!EncodeExpr(e, *v[i]))
return false;
}
return true;
}
static bool
EncodeBlock(Encoder& e, WasmAstBlock& b)
{
if (!e.writeExpr(b.expr()))
return false;
size_t numExprs = b.exprs().length();
for (size_t i = 0; i < numExprs; i++) {
if (!EncodeExpr(e, *b.exprs()[i]))
return false;
}
if (!EncodeExprList(e, b.exprs()))
return false;
if (!e.writeExpr(Expr::End))
return false;
@ -3853,15 +3910,22 @@ EncodeConversionOperator(Encoder& e, WasmAstConversionOperator& b)
}
static bool
EmitIf(Encoder& e, WasmAstIf& i)
EncodeIf(Encoder& e, WasmAstIf& i)
{
return EncodeExpr(e, i.cond()) &&
e.writeExpr(Expr::If) &&
EncodeExpr(e, i.thenBranch()) &&
(!i.hasElse() ||
(e.writeExpr(Expr::Else) &&
EncodeExpr(e, i.elseBranch()))) &&
e.writeExpr(Expr::End);
if (!EncodeExpr(e, i.cond()) || !e.writeExpr(Expr::If))
return false;
if (!EncodeExprList(e, i.thenExprs()))
return false;
if (i.hasElse()) {
if (!e.writeExpr(Expr::Else))
return false;
if (!EncodeExprList(e, i.elseExprs()))
return false;
}
return e.writeExpr(Expr::End);
}
static bool
@ -3973,7 +4037,7 @@ EncodeExpr(Encoder& e, WasmAstExpr& expr)
case WasmAstExprKind::GetLocal:
return EncodeGetLocal(e, expr.as<WasmAstGetLocal>());
case WasmAstExprKind::If:
return EmitIf(e, expr.as<WasmAstIf>());
return EncodeIf(e, expr.as<WasmAstIf>());
case WasmAstExprKind::Load:
return EncodeLoad(e, expr.as<WasmAstLoad>());
case WasmAstExprKind::Return: