Class JavaMethodWasmCodeBuilder


  • class JavaMethodWasmCodeBuilder
    extends WasmCodeBuilder
    Convert Java Byte Code to a list of WasmInstruction.
    • Constructor Detail

      • JavaMethodWasmCodeBuilder

        JavaMethodWasmCodeBuilder​(@Nonnull
                                  WasmCodeBuilder codeBuilder)
        Create an instance
        Parameters:
        codeBuilder - other code builder for shred references
    • Method Detail

      • init

        void init​(WasmOptions options,
                  ClassFileLoader classFileLoader)
        Initialize the code builder;
        Overrides:
        init in class WasmCodeBuilder
        Parameters:
        options - compiler properties
        classFileLoader - for loading the class files
      • buildCode

        void buildCode​(@Nonnull
                       Code code,
                       MethodInfo method)
        Build the wasm instructions
        Parameters:
        code - the Java method code
        method - the method with signature as fallback for a missing variable table
        Throws:
        WasmException - if some Java code can't converted
      • writeCode

        private void writeCode​(CodeInputStream byteCode,
                               ConstantPool constantPool,
                               ClassFile classFile,
                               AnyType returnType)
                        throws WasmException
        Write the byte code of a method.
        Parameters:
        byteCode - a stream of byte code
        constantPool - the constant pool of the the current class
        classFile - the declaring class file
        returnType - the return type of the method
        Throws:
        WasmException - if some Java code can't converted
      • writeSwitchCode

        private void writeSwitchCode​(CodeInputStream byteCode,
                                     boolean isLookupSwitch)
                              throws java.io.IOException
        Write the both switch operation codes
        Parameters:
        byteCode - the current stream with a position after the operation code
        isLookupSwitch - true, if the operation was a loopupswitch; false, if the operation was a tableswitch
        Throws:
        java.io.IOException - if any I/O error occur
      • findNext

        private static int findNext​(int current,
                                    int[] values)
        Find the next higher value.
        Parameters:
        current - the current value
        values - the unordered list of values
        Returns:
        the next value or current value if not found.
      • opIfCondition

        private void opIfCondition​(NumericOperator compareOp,
                                   CodeInputStream byteCode,
                                   int codePos,
                                   int lineNumber)
                            throws java.io.IOException
        Handle the if of the Java byte code. This Java instruction compare the first stack value with value 0. Important: In the Java IF expression the condition for the jump to the else block is saved. In WebAssembler we need to use condition for the if block. The caller of the method must already negate this
        Parameters:
        compareOp - The condition for the continue of a loop.
        byteCode - current byte code stream to read the target offset.
        codePos - the code position/offset in the Java method
        lineNumber - the line number in the Java source code
        Throws:
        java.io.IOException - if any I/O errors occur.
      • opIfCompareCondition

        private void opIfCompareCondition​(NumericOperator compareOp,
                                          CodeInputStream byteCode,
                                          int codePos,
                                          int lineNumber)
                                   throws java.io.IOException
        Handle the if of the Java byte code. This Java instruction compare 2 values from stack. Important: In the Java IF expression the condition for the jump to the else block is saved. In WebAssembler we need to use condition for the if block. The caller of the method must already negate this.
        Parameters:
        compareOp - The condition for the continue of a loop.
        byteCode - current byte code stream to read the target offset.
        codePos - the code position/offset in the Java method
        lineNumber - the line number in the Java source code
        Throws:
        java.io.IOException - if any I/O errors occur.
      • opCompare

        private void opCompare​(ValueType valueType,
                               CodeInputStream byteCode,
                               int codePos,
                               int lineNumber)
                        throws java.io.IOException
        Handle the different compare operator. The compare operator returns the integer values -1, 0 or 1. There is no equivalent in WebAssembly. That we need to read the next operation to find an equivalent.
        Parameters:
        valueType - the value type of the compared
        byteCode - current byte code stream to read the next operation.
        codePos - the code position/offset in the Java method
        lineNumber - the line number in the Java source code
        Throws:
        java.io.IOException - if any I/O errors occur.
      • patchTypeOfNullConst

        private void patchTypeOfNullConst()
        NULL const has no type in Java. In WebAssembly currently.