Skip to content

feat: support vm jump table instruction #1835

@DavePearce

Description

@DavePearce

Summary

Currently, switch statements at the source level are translated into if-else chains. Whilst this is fine for constraint generation, it does have an impact on tracing / execution performance. Hence, it would make sense to introduce a vm instruction for implement switch statements, and this should be some variation on the concept of a "jump table". A useful reference point here would be the approach taken in Java bytecode where tableswitch and lookupswitch are used.

Example

A simple example would be the following:

fn f(x:u16) -> (r:u16) {
  switch x {
  case 0,1: {
    r = 1
  }
  default: {
    r = 0
  }
  }
}

This currently generates the following vectored IR:

fn f(u16 x) -> (u16 r) {
        u16 $2
        u16 $3
[0]     $2 = 0x0 ; skip_if x == $2 5 ; $3 = 0x1 ; skip_if x == $3 2 ; r = 0x0 ; skip 2 ; skip 0 ; r = 0x1 ; ret
}

The purpose of this would be to introduce a multi-way skip, something like this:

fn f(u16 x) -> (u16 r) {
        u16 $2
        u16 $3
[0]    switch x [0=>2, 1=>2] $3 = 0x0 ; ret ; r = 0x1 ; ret
}

Here, 0 => 2 means: when x == 0 then skip 2 instructions, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions