blob: 9c6d3d76e66e83d43f79d95879a17fedfcc5bc74 [file] [log] [blame]
// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.cf.code;
import com.android.tools.r8.cf.CfPrinter;
import com.android.tools.r8.errors.Unreachable;
import com.android.tools.r8.ir.code.Cmp;
import com.android.tools.r8.ir.code.Cmp.Bias;
import com.android.tools.r8.ir.code.NumericType;
import com.android.tools.r8.naming.NamingLens;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class CfCmp extends CfInstruction {
private final Bias bias;
private final NumericType type;
public CfCmp(Bias bias, NumericType type) {
assert bias != null;
assert type != null;
assert type == NumericType.LONG || type == NumericType.FLOAT || type == NumericType.DOUBLE;
assert type != NumericType.LONG || bias == Cmp.Bias.NONE;
assert type == NumericType.LONG || bias != Cmp.Bias.NONE;
this.bias = bias;
this.type = type;
}
public static CfCmp fromAsm(int opcode) {
switch (opcode) {
case Opcodes.LCMP:
return new CfCmp(Bias.NONE, NumericType.LONG);
case Opcodes.FCMPL:
return new CfCmp(Bias.LT, NumericType.FLOAT);
case Opcodes.FCMPG:
return new CfCmp(Bias.GT, NumericType.FLOAT);
case Opcodes.DCMPL:
return new CfCmp(Bias.LT, NumericType.DOUBLE);
case Opcodes.DCMPG:
return new CfCmp(Bias.GT, NumericType.DOUBLE);
default:
throw new Unreachable("Wrong ASM opcode for CfCmp " + opcode);
}
}
public int getAsmOpcode() {
switch (type) {
case LONG:
return Opcodes.LCMP;
case FLOAT:
return bias == Cmp.Bias.LT ? Opcodes.FCMPL : Opcodes.FCMPG;
case DOUBLE:
return bias == Cmp.Bias.LT ? Opcodes.DCMPL : Opcodes.DCMPG;
default:
throw new Unreachable("CfCmp has unknown type " + type);
}
}
@Override
public void print(CfPrinter printer) {
printer.print(this);
}
@Override
public void write(MethodVisitor visitor, NamingLens lens) {
visitor.visitInsn(getAsmOpcode());
}
}