Search in sources :

Example 1 with IInstruction

use of dyvilx.tools.compiler.ast.bytecode.IInstruction in project Dyvil by Dyvil.

the class InlineIntrinsicData method preProcess.

private void preProcess() {
    final ParameterList parameterList = this.method.getParameters();
    int parameterSlots = 0;
    for (int i = 0, parameterCount = parameterList.size(); i < parameterCount; i++) {
        parameterSlots += parameterList.get(i).getCovariantType().getLocalSlots();
    }
    this.parameterSlots = parameterSlots;
    final int[] accessCounts = new int[this.maxLocals];
    int lastStoredIndex = -1;
    for (int i = 0; i < this.instructionCount; i++) {
        final IInstruction instruction = this.instructions[i];
        final int opcode = instruction.getOpcode();
        if (Opcodes.isLoadOpcode(opcode)) {
            final int varIndex = ((VarInstruction) instruction).getIndex();
            if (accessCounts[varIndex]++ == 0) {
                // Local Variable loaded for the first time -> might not need to store it
                continue;
            }
            // Local Variable loaded at least two times -> need to store it and all parameters before
            if (varIndex > lastStoredIndex && varIndex < parameterSlots) {
                lastStoredIndex = varIndex;
            }
        } else if (Opcodes.isReturnOpcode(opcode)) {
            this.returnIndex = i;
        }
    }
    this.storedParameters = lastStoredIndex + 1;
}
Also used : VarInstruction(dyvilx.tools.compiler.ast.bytecode.VarInstruction) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) IInstruction(dyvilx.tools.compiler.ast.bytecode.IInstruction)

Example 2 with IInstruction

use of dyvilx.tools.compiler.ast.bytecode.IInstruction in project Dyvil by Dyvil.

the class InlineIntrinsicData method writeInvIntrinsic.

@Override
public void writeInvIntrinsic(MethodWriter writer, Label dest, IValue receiver, ArgumentList arguments, int lineNumber) throws BytecodeException {
    final int localCount = writer.localCount();
    this.preWrite(writer, receiver, arguments, localCount);
    // Write all except the last Instruction
    final int lastIndex = this.returnIndex - 1;
    for (int i = 0; i < lastIndex; i++) {
        this.writeInstruction(this.instructions[i], writer, receiver, arguments, localCount);
    }
    // Write the last Instruction as an inverse jump instruction
    final IInstruction lastInstruction = this.instructions[lastIndex];
    final int jumpOpcode = Opcodes.getInvJumpOpcode(lastInstruction.getOpcode());
    if (jumpOpcode >= 0) {
        writer.visitJumpInsn(jumpOpcode, dest);
    } else {
        this.writeInstruction(lastInstruction, writer, receiver, arguments, localCount);
        writer.visitJumpInsn(Opcodes.IFEQ, dest);
    }
    writer.resetLocals(localCount);
}
Also used : IInstruction(dyvilx.tools.compiler.ast.bytecode.IInstruction)

Example 3 with IInstruction

use of dyvilx.tools.compiler.ast.bytecode.IInstruction in project Dyvil by Dyvil.

the class InlineIntrinsicData method writeIntrinsic.

@Override
public void writeIntrinsic(MethodWriter writer, Label dest, IValue receiver, ArgumentList arguments, int lineNumber) throws BytecodeException {
    final int localCount = writer.localCount();
    this.preWrite(writer, receiver, arguments, localCount);
    // Write all except the last Instruction
    final int lastIndex = this.returnIndex - 1;
    for (int i = 0; i < lastIndex; i++) {
        this.writeInstruction(this.instructions[i], writer, receiver, arguments, localCount);
    }
    // Write the last Instruction as a jump instruction
    final IInstruction lastInstruction = this.instructions[lastIndex];
    final int jumpOpcode = Opcodes.getJumpOpcode(lastInstruction.getOpcode());
    if (jumpOpcode >= 0) {
        writer.visitJumpInsn(jumpOpcode, dest);
    } else {
        this.writeInstruction(lastInstruction, writer, receiver, arguments, localCount);
        writer.visitJumpInsn(Opcodes.IFNE, dest);
    }
    writer.resetLocals(localCount);
}
Also used : IInstruction(dyvilx.tools.compiler.ast.bytecode.IInstruction)

Aggregations

IInstruction (dyvilx.tools.compiler.ast.bytecode.IInstruction)3 VarInstruction (dyvilx.tools.compiler.ast.bytecode.VarInstruction)1 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)1