ControlCplusControlV / Scribe

Minimal Yul Transpilation to the Miden VM
GNU General Public License v3.0
51 stars 4 forks source link

For Loop to Repeat Statement #44

Open ControlCplusControlV opened 1 year ago

ControlCplusControlV commented 1 year ago

For Loop to Repeat Statement

Given a loop statement like

    for { let i:u32 := 0 } lt(i, 10) { i := add(i, 1)}
    {
        c := add(a,b)
        a := b
        b := c
    }

Should be transpiled down to a statement which utilizes the Miden Assembly instruction

repeat.{}

Rather than the current while based implementation of for loops

Testcases

  1. Input Yul
    for { let i:u32 := 0 } lt(i, 10) { i := add(i, 1)}
    {
        c := add(a,b)
        a := b
        b := c
    }
  1. Assembly Output
use.std::math::u256

begin
    # Assigning to a #
        # u32 literal 10 #
        push.10

    # Assigning to b #
        # u32 literal 5 #
        push.5

    # Assigning to c #
        # u32 literal 0 #
        push.0

    repeat.10
        # -- interior block -- #
            # Assigning to c #
                # add() #
                # pushing a to the top #
                    dup.3
                # pushing b to the top #
                    dup.3
                add
            # Assigning to a #
                # pushing b to the top #
                    dup.3
            # Assigning to b #
                # pushing c to the top #
                    dup.1

    end

end
  1. Input Yul
{
    let a:u32 := 10
    let b:u32 := 5
    let c:u32 := 0

for { let i:u32 := 10 } gt(i, 0) { i := sub(i, 5)}
    {
        let c := add(a,b)
        let a := b
        let b := c
    }
}
  1. Assembly Output

use.std::math::u256

begin
    # Assigning to a #
        # u32 literal 10 #
        push.10

    # Assigning to b #
        # u32 literal 5 #
        push.5

    # Assigning to c #
        # u32 literal 0 #
        push.0

repeat.2
    # -- interior block -- #
        # Assigning to c #
            # add() #
            # pushing a to the top #
                dup.3
            # pushing b to the top #
                dup.3
            add
        # Assigning to a #
            # pushing b to the top #
                dup.3
        # Assigning to b #
            # pushing c to the top #
                dup.1

    end
end
  1. Input Yul
{
    let a:u32 := 10
    let b:u32 := 5
    let c:u32 := 0

for { let i:u32 := 10 } lt(i, 0) { i := sub(i, 5)}
    {
        let c := add(a,b)
        let a := b
        let b := c
    }
}
  1. Assembly Output
//Dead Code, No Assembly Generated