birbs

refutable

  • 19, he/they


birbs
@birbs

I managed to successfully get my JVM to compile the following Java code

public class Main {
    public Main() {
    }

    public static int return_4() {
        int var0 = 0;

        for(int var1 = 0; var1 < 1024; ++var1) {
            var0 += var1;
        }

        return var0;
    }
}

into the following WASM code (represented in the text format for legibility because I don't think you can read raw wasm modules by eye and if you can you're cool)

(module
  (type (;0;) (func (result i32)))
  (type (;1;) (func))
  (type (;2;) (func (param i32 i32)))
  (func (;0;) (type 0) (result i32)
    (local i32 i32 i32)
    i32.const 0
    local.set 0
    i32.const 0
    local.set 1
    loop ;; label = @1
      local.get 1
      i32.const 1024
      block (type 2) (param i32 i32) ;; label = @2
        i32.ge_s
        br_if 0 (;@2;)
        local.get 0
        local.get 1
        i32.add
        local.set 0
        local.get 1
        i32.const 1
        i32.add
        local.set 1
        br 1 (;@1;)
      end
    end
    local.get 0
    return
  )
  (export "java" (func 0))
)

and when I run it with wasmtime I get

> 523776

birbs
@birbs

meaning in theory i implemented the relooper algorithm correctly which is great. unfortunately i am just focusing on the interpreter side of the jvm but it is very cool that i got it working because i have wanted to do that for years


You must log in to comment.

in reply to @birbs's post:

Are using using that java to native code thing (I think it's grall-vm) and using a WASM background, or are you somehow translating JVM byte code to WASM? Seems cool either way. Now we can have an unholy mess of enterprise architecture patterns and flavor of the week js frameworks running side by side!

in reply to @birbs's post: