Search in sources :

Example 1 with ByteCodeClass

use of com.codename1.tools.translator.ByteCodeClass in project CodenameOne by codenameone.

the class CustomInvoke method appendExpression.

public boolean appendExpression(StringBuilder b) {
    // special case for clone on an array which isn't a real method invocation
    if (name.equals("clone") && owner.indexOf('[') > -1) {
        if (targetObjectLiteral != null) {
            b.append("cloneArray(").append(targetObjectLiteral).append(")");
        } else {
            b.append("cloneArray(POP_OBJ(1))");
        }
        return true;
    }
    StringBuilder bld = new StringBuilder();
    if (origOpcode == Opcodes.INVOKEINTERFACE || origOpcode == Opcodes.INVOKEVIRTUAL) {
        // b.append("    ");
        bld.append("virtual_");
    }
    if (origOpcode == Opcodes.INVOKESTATIC) {
        // find the actual class of the static method to workaround javac not defining it correctly
        ByteCodeClass bc = Parser.getClassObject(owner.replace('/', '_').replace('$', '_'));
        owner = findActualOwner(bc);
    }
    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    int numLiteralArgs = this.getNumLiteralArgs();
    if (numLiteralArgs > 0) {
        b.append("/* CustomInvoke */");
    }
    boolean noPop = false;
    b.append(bld);
    b.append("(threadStateData");
    if (origOpcode != Opcodes.INVOKESTATIC) {
        if (targetObjectLiteral == null) {
            // b.append("].data.o");
            return false;
        } else {
            b.append(", ").append(targetObjectLiteral);
            numLiteralArgs++;
        }
    }
    // int offset = args.size();
    // int numArgs = offset;
    int argIndex = 0;
    for (String a : args) {
        b.append(", ");
        if (literalArgs != null && literalArgs[argIndex] != null) {
            b.append(literalArgs[argIndex]);
        } else {
            return false;
        // b.append("SP[-");
        // b.append(offset);
        // b.append("].data.");
        // b.append(a);
        // offset--;
        }
        argIndex++;
    }
    if (returnVal == null) {
        return false;
    }
    b.append(")");
    return true;
}
Also used : ByteCodeClass(com.codename1.tools.translator.ByteCodeClass) ArrayList(java.util.ArrayList)

Example 2 with ByteCodeClass

use of com.codename1.tools.translator.ByteCodeClass in project CodenameOne by codenameone.

the class Invoke method appendInstruction.

@Override
public void appendInstruction(StringBuilder b) {
    // special case for clone on an array which isn't a real method invocation
    if (name.equals("clone") && owner.indexOf('[') > -1) {
        b.append("    POP_MANY_AND_PUSH_OBJ(cloneArray(PEEK_OBJ(1)), 1);\n");
        return;
    }
    StringBuilder bld = new StringBuilder();
    if (opcode == Opcodes.INVOKEINTERFACE || opcode == Opcodes.INVOKEVIRTUAL) {
        b.append("    ");
        bld.append("virtual_");
    } else {
        b.append("    ");
    }
    if (opcode == Opcodes.INVOKESTATIC) {
        // find the actual class of the static method to workaround javac not defining it correctly
        ByteCodeClass bc = Parser.getClassObject(owner.replace('/', '_').replace('$', '_'));
        owner = findActualOwner(bc);
    }
    // if(owner.replace('/', '_').replace('$', '_').equals("java_lang_System_1") && name.equals("sleep")) {
    // System.out.println("Break");
    // }
    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    boolean noPop = false;
    if (returnVal == null) {
        b.append(bld);
    } else {
        if (args.size() == 0 && opcode == Opcodes.INVOKESTATIC) {
            // special case for static method
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("PUSH_OBJ");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("PUSH_INT");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("PUSH_LONG");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("PUSH_DOUBLE");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("PUSH_FLOAT");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
            // b.append(returnVal);
            noPop = true;
            b.append("(");
        } else {
            // b.append("POP_MANY_AND_");
            // b.append(returnVal);
            b.append("{ ");
            b.append(returnVal);
            b.append(" tmpResult = ");
        }
        b.append(bld);
    }
    b.append("(threadStateData");
    if (opcode != Opcodes.INVOKESTATIC) {
        b.append(", SP[-");
        b.append(args.size() + 1);
        b.append("].data.o");
    }
    int offset = args.size();
    // int numArgs = offset;
    int argIndex = 0;
    for (String a : args) {
        b.append(", ");
        b.append("SP[-");
        b.append(offset);
        b.append("].data.");
        b.append(a);
        offset--;
        argIndex++;
    }
    if (noPop) {
        b.append("));\n");
        return;
    }
    if (returnVal != null) {
        b.append(");\n");
        if (opcode != Opcodes.INVOKESTATIC) {
            if (args.size() > 0) {
                b.append("    SP-=");
                b.append(args.size());
                b.append(";\n");
            }
        } else {
            if (args.size() > 1) {
                b.append("    SP-=");
                b.append(args.size() - 1);
                b.append(";\n");
            }
        }
        if (returnVal.equals("JAVA_OBJECT")) {
            b.append("    SP[-1].data.o = tmpResult; SP[-1].type = CN1_TYPE_OBJECT; }\n");
        } else {
            if (returnVal.equals("JAVA_INT")) {
                b.append("    SP[-1].data.i = tmpResult; SP[-1].type = CN1_TYPE_INT; }\n");
            } else {
                if (returnVal.equals("JAVA_LONG")) {
                    b.append("    SP[-1].data.l = tmpResult; SP[-1].type = CN1_TYPE_LONG; }\n");
                } else {
                    if (returnVal.equals("JAVA_DOUBLE")) {
                        b.append("    SP[-1].data.d = tmpResult; SP[-1].type = CN1_TYPE_DOUBLE; }\n");
                    } else {
                        if (returnVal.equals("JAVA_FLOAT")) {
                            b.append("    SP[-1].data.f = tmpResult; SP[-1].type = CN1_TYPE_FLOAT; }\n");
                        } else {
                            throw new UnsupportedOperationException("Unknown type: " + returnVal);
                        }
                    }
                }
            }
        }
        return;
    }
    b.append("); ");
    int val;
    if (opcode != Opcodes.INVOKESTATIC) {
        val = args.size() + 1;
    } else {
        val = args.size();
    }
    if (val > 0) {
        /*b.append("popMany(threadStateData, ");            
            b.append(val);
            b.append(", stack, &stackPointer); \n"); */
        b.append("    SP-= ");
        b.append(val);
        b.append(";\n");
    } else {
        b.append("\n");
    }
}
Also used : ByteCodeClass(com.codename1.tools.translator.ByteCodeClass) ArrayList(java.util.ArrayList)

Example 3 with ByteCodeClass

use of com.codename1.tools.translator.ByteCodeClass in project CodenameOne by codenameone.

the class CustomInvoke method appendInstruction.

@Override
public void appendInstruction(StringBuilder b) {
    // special case for clone on an array which isn't a real method invocation
    if (name.equals("clone") && owner.indexOf('[') > -1) {
        if (targetObjectLiteral != null) {
            b.append("    PUSH_OBJ(cloneArray(").append(targetObjectLiteral).append("));\n");
        } else {
            b.append("    POP_MANY_AND_PUSH_OBJ(cloneArray(PEEK_OBJ(1)), 1);\n");
        }
        return;
    }
    StringBuilder bld = new StringBuilder();
    if (origOpcode == Opcodes.INVOKEINTERFACE || origOpcode == Opcodes.INVOKEVIRTUAL) {
        b.append("    ");
        bld.append("virtual_");
    } else {
        b.append("    ");
    }
    if (origOpcode == Opcodes.INVOKESTATIC) {
        // find the actual class of the static method to workaround javac not defining it correctly
        ByteCodeClass bc = Parser.getClassObject(owner.replace('/', '_').replace('$', '_'));
        owner = findActualOwner(bc);
    }
    // if(owner.replace('/', '_').replace('$', '_').equals("java_lang_System_1") && name.equals("sleep")) {
    // System.out.println("Break");
    // }
    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    int numLiteralArgs = this.getNumLiteralArgs();
    if (numLiteralArgs > 0) {
        b.append("/* CustomInvoke */");
    }
    boolean noPop = false;
    if (returnVal == null || noReturn) {
        b.append(bld);
    } else {
        if (args.size() - numLiteralArgs == 0 && origOpcode == Opcodes.INVOKESTATIC) {
            // special case for static method
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("PUSH_OBJ");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("PUSH_INT");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("PUSH_LONG");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("PUSH_DOUBLE");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("PUSH_FLOAT");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
            // b.append(returnVal);
            noPop = true;
            b.append("(");
        } else {
            // b.append("POP_MANY_AND_");
            // b.append(returnVal);
            b.append("{ ");
            b.append(returnVal);
            b.append(" tmpResult = ");
        }
        b.append(bld);
    }
    b.append("(threadStateData");
    if (origOpcode != Opcodes.INVOKESTATIC) {
        if (targetObjectLiteral == null) {
            b.append(", SP[-");
            b.append(args.size() + 1 - numLiteralArgs);
            b.append("].data.o");
        } else {
            b.append(", " + targetObjectLiteral);
            numLiteralArgs++;
        }
    }
    int offset = args.size();
    // int numArgs = offset;
    int argIndex = 0;
    for (String a : args) {
        b.append(", ");
        if (literalArgs != null && literalArgs[argIndex] != null) {
            b.append(literalArgs[argIndex]);
        } else {
            b.append("SP[-");
            b.append(offset);
            b.append("].data.");
            b.append(a);
            offset--;
        }
        argIndex++;
    }
    if (noPop) {
        b.append("));\n");
        return;
    }
    if (returnVal != null && !noReturn) {
        b.append(");\n");
        if (origOpcode != Opcodes.INVOKESTATIC) {
            if (args.size() - numLiteralArgs > 0) {
                b.append("    SP -= ");
                b.append(args.size() - numLiteralArgs);
                b.append(";\n");
            }
        } else {
            if (args.size() - numLiteralArgs > 1) {
                b.append("    SP -= ");
                b.append(args.size() - numLiteralArgs - 1);
                b.append(";\n");
            }
        }
        if (targetObjectLiteral == null) {
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("    SP[-1].data.o = tmpResult; SP[-1].type = CN1_TYPE_OBJECT; }\n");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("    SP[-1].data.i = tmpResult; SP[-1].type = CN1_TYPE_INT; }\n");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("    SP[-1].data.l = tmpResult; SP[-1].type = CN1_TYPE_LONG; }\n");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("    SP[-1].data.d = tmpResult; SP[-1].type = CN1_TYPE_DOUBLE; }\n");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("    SP[-1].data.f = tmpResult; SP[-1].type = CN1_TYPE_FLOAT; }\n");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
        } else {
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("    PUSH_OBJ(tmpResult); }\n");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("    PUSH_INT(tmpResult); }\n");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("    PUSH_LONG(tmpResult); }\n");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("    PUSH_DOUBLE(tmpResult); }\n");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("    PUSH_FLOAT(tmpResult); }\n");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
        }
        return;
    }
    b.append("); ");
    int val;
    if (origOpcode != Opcodes.INVOKESTATIC) {
        val = args.size() + 1 - numLiteralArgs;
    } else {
        val = args.size() - numLiteralArgs;
    }
    if (val > 0) {
        b.append("    SP -= ");
        b.append(val);
        b.append(";\n");
    } else {
        b.append("\n");
    }
}
Also used : ByteCodeClass(com.codename1.tools.translator.ByteCodeClass) ArrayList(java.util.ArrayList)

Aggregations

ByteCodeClass (com.codename1.tools.translator.ByteCodeClass)3 ArrayList (java.util.ArrayList)3