Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Latest commit

 

History

History
58 lines (47 loc) · 1.51 KB

README.md

File metadata and controls

58 lines (47 loc) · 1.51 KB

Bytecode Builder

BCB is a framework for generating java bytecode. The goal of this project is to simplify java bytecode generation.

Project Layout

  • bcb - the core bcb project

  • examples - bcb code generation examples

Fused-Multiple-Add (FMA) Example

public class Test {
    public static void main(String[] args) {
        BCBMethod method = BCBMethod.builder()
                .name("Test")
                .declaringClass("TestClass")
                .returnType(Type.FLOAT_TYPE)
                .arg("a", Type.FLOAT_TYPE)
                .arg("b", Type.FLOAT_TYPE)
                .arg("c", Type.FLOAT_TYPE)
                .body(
                        ret(
                                add(
                                        mul(
                                                load("a"),
                                                load("b")
                                        ),
                                        load("c")
                                )
                        )
                ).build();

        final MethodHandle fmaMethodHandle = method.toMethodHandle();

        System.out.println(fmaMethodHandle.invoke(2, 2, 6));
    }
}

Generated bytecode (from javap)

public class TestClass {
  public static float Test(float, float);
    Code:
       0: fload_0
       1: fload_1
       2: fmul
       3: fload_0
       4: fload_1
       5: fmul
       6: fadd
       7: freturn
}