Bug 148934

Summary: Implement control flow statements in WebAssembly
Product: WebKit Reporter: Sukolsak Sakshuwong <sukolsak>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: commit-queue, fpizlo, ggaren, saam, sukolsak, ysuzuki
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 146064    
Attachments:
Description Flags
Patch
none
Patch none

Description Sukolsak Sakshuwong 2015-09-07 10:53:27 PDT
This patch implements if, while, do, label, break, and continue statements in WebAssembly. Switches will be implemented in a subsequent patch.
Comment 1 Sukolsak Sakshuwong 2015-09-07 10:58:52 PDT
Created attachment 260741 [details]
Patch
Comment 2 Sukolsak Sakshuwong 2015-09-07 11:10:12 PDT
Created attachment 260742 [details]
Patch
Comment 3 Sukolsak Sakshuwong 2015-09-08 07:27:02 PDT
When we implement an LLVM IR generator for WebAssembly, JumpTarget will be just an LLVM Basic Block. The code will look like this:

typedef LBasicBlock JumpTarget;

LBasicBlock newTarget()
{
    return m_out.newBlock();
}

void linkTarget(LBasicBlock target)
{
    m_out.jump(target);
    m_out.appendTo(target);
}
    
void jumpToTarget(LBasicBlock target)
{
    m_out.jump(target);
}
    
void jumpToTargetIf(JumpCondition condition, LValue expression, LBasicBlock target)
{
    LBasicBlock notTaken = m_out.newBlock();
    m_out.branch((condition == JumpCondition::Zero) ? m_out.isZero32(expression) : m_out.notZero32(expression), FTL::unsure(target), FTL::unsure(notTaken));
    m_out.appendTo(notTaken);
}
Comment 4 Geoffrey Garen 2015-09-08 11:13:19 PDT
Comment on attachment 260742 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=260742&action=review

r=me

> Source/JavaScriptCore/wasm/WASMFunctionCompiler.h:407
> +    void endLabel()
> +    {
> +        linkTarget(m_breakLabelTargets.last());
> +
> +        m_breakLabelTargets.removeLast();
> +        m_continueLabelTargets.removeLast();
> +    }

Is it guaranteed that WASM can't have arbitrary gotos? (An arbitrary goto would make the push/pop model of labels invalid.)
Comment 5 Sukolsak Sakshuwong 2015-09-08 11:36:15 PDT
Thanks!

(In reply to comment #4)
> Comment on attachment 260742 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=260742&action=review
> 
> r=me
> 
> > Source/JavaScriptCore/wasm/WASMFunctionCompiler.h:407
> > +    void endLabel()
> > +    {
> > +        linkTarget(m_breakLabelTargets.last());
> > +
> > +        m_breakLabelTargets.removeLast();
> > +        m_continueLabelTargets.removeLast();
> > +    }
> 
> Is it guaranteed that WASM can't have arbitrary gotos? (An arbitrary goto
> would make the push/pop model of labels invalid.)

goto is not part of the MVP, but it is under consideration for a future version. <https://github.com/WebAssembly/design/blob/master/FutureFeatures.md#more-expressive-control-flow> The WASM format that we are using doesn't have goto.
Comment 6 WebKit Commit Bot 2015-09-08 16:05:08 PDT
Comment on attachment 260742 [details]
Patch

Clearing flags on attachment: 260742

Committed r189514: <http://trac.webkit.org/changeset/189514>
Comment 7 WebKit Commit Bot 2015-09-08 16:05:12 PDT
All reviewed patches have been landed.  Closing bug.