use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.
the class ConditionalJumpStmt method toCode.
@Override
public void toCode(MethodVisitor visitor, ControlFlowGraph cfg) {
Type opType = TypeUtils.resolveBinOpType(left.getType(), right.getType());
if (TypeUtils.isObjectRef(opType)) {
boolean isNull = right instanceof ConstantExpr && ((ConstantExpr) right).getConstant() == null;
if (type != ComparisonType.EQ && type != ComparisonType.NE) {
throw new IllegalArgumentException(type.toString());
}
left.toCode(visitor, cfg);
if (isNull) {
visitor.visitJumpInsn(type == ComparisonType.EQ ? Opcodes.IFNULL : Opcodes.IFNONNULL, trueSuccessor.getLabel());
} else {
right.toCode(visitor, cfg);
visitor.visitJumpInsn(type == ComparisonType.EQ ? Opcodes.IF_ACMPEQ : Opcodes.IF_ACMPNE, trueSuccessor.getLabel());
}
} else if (opType == Type.INT_TYPE) {
boolean canShorten = right instanceof ConstantExpr && ((ConstantExpr) right).getConstant() instanceof Number && ((Number) ((ConstantExpr) right).getConstant()).intValue() == 0;
left.toCode(visitor, cfg);
int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
if (canShorten) {
visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
} else {
right.toCode(visitor, cfg);
cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
visitor.visitJumpInsn(Opcodes.IF_ICMPEQ + type.ordinal(), trueSuccessor.getLabel());
}
} else if (opType == Type.LONG_TYPE) {
left.toCode(visitor, cfg);
int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
right.toCode(visitor, cfg);
cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
visitor.visitInsn(Opcodes.LCMP);
visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
} else if (opType == Type.FLOAT_TYPE) {
left.toCode(visitor, cfg);
int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
right.toCode(visitor, cfg);
cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
visitor.visitInsn((type == ComparisonType.LT || type == ComparisonType.LE) ? Opcodes.FCMPL : Opcodes.FCMPG);
visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
} else if (opType == Type.DOUBLE_TYPE) {
left.toCode(visitor, cfg);
int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
right.toCode(visitor, cfg);
cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
for (int i = 0; i < cast.length; i++) {
visitor.visitInsn(cast[i]);
}
visitor.visitInsn((type == ComparisonType.LT || type == ComparisonType.LE) ? Opcodes.DCMPL : Opcodes.DCMPG);
visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
} else {
throw new IllegalArgumentException(opType.toString());
}
}
use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.
the class ConstantParameterPass method inlineConstant.
private void inlineConstant(ControlFlowGraph cfg, int argLocalIndex, Object o) {
/* we don't actually demote the synthetic copy
* here as we would also need to change the
* method desc and we can't do that until
* later so we defer it. */
LocalsPool pool = cfg.getLocals();
/* create the spill variable but not the
* actual definition yet. */
VersionedLocal argLocal = pool.get(argLocalIndex, 0, false);
VersionedLocal spill = pool.makeLatestVersion(argLocal);
AbstractCopyStmt synthParamCopy = pool.defs.get(argLocal);
ConstantExpr rhsVal = new ConstantExpr(o, synthParamCopy.getType() == Type.BOOLEAN_TYPE ? Type.BYTE_TYPE : synthParamCopy.getType());
/* we have to maintain local references in
* phis as opposed to direct constant refs,
* so we go through every use of the argLocal
* and either replace it with the constant or
* a reference to the spill local if it is in
* a phi. */
Set<VarExpr> spillUses = new HashSet<>();
boolean requireSpill = false;
Iterator<VarExpr> it = pool.uses.get(argLocal).iterator();
while (it.hasNext()) {
VarExpr v = it.next();
if (v.getParent() == null) {
/* the use is in a phi, we can't
* remove the def.
*
* we also replace the old var
* with the new spill one so we
* have to add this as a use of
* the new spill local. */
spillUses.add(v);
v.setLocal(spill);
requireSpill = true;
} else {
CodeUnit par = v.getParent();
par.overwrite(rhsVal.copy(), par.indexOf(v));
}
/* this use is no longer associated
* with the old argLocal. */
it.remove();
}
if (pool.uses.get(argLocal).size() != 0) {
throw new IllegalStateException(String.format("l:%s, uses:%s", argLocal, pool.uses.get(argLocal)));
}
if (requireSpill) {
/* generate the copy for the spill (v = const) */
CopyVarStmt spillCopy = new CopyVarStmt(new VarExpr(spill, synthParamCopy.getVariable().getType()), rhsVal);
synthParamCopy.getBlock().add(spillCopy);
/* initialise data entries for the new spill
* variable. */
pool.defs.put(spill, spillCopy);
pool.uses.put(spill, spillUses);
}
}
use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.
the class FieldRSADecryptionPass method handleFss.
private void handleFss(FieldStoreStmt fss) {
if (!isIntField(fss.getDesc())) {
return;
}
List<CodeUnit> list = getInsns(fss, new HashSet<>());
boolean other = containsOther(list, key(fss));
for (CodeUnit u : list) {
if (u.getOpcode() == CONST_LOAD) {
ConstantExpr c = (ConstantExpr) u;
Object cst = c.getConstant();
if (cst instanceof Integer || cst instanceof Long) {
if (large((Number) cst, cst instanceof Long)) {
if (other) {
dangerConstants.getNonNull(key(fss)).add((Number) cst);
} else {
constants.getNonNull(key(fss)).add((Number) cst);
}
}
}
}
}
}
use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.
the class FieldRSADecryptionPass method handleFle.
private void handleFle(Stmt stmt, FieldLoadExpr fle) {
if (!isIntField(fle.getDesc())) {
return;
}
List<CodeUnit> list = getInsns(fle, new HashSet<>());
boolean other = containsOther(list, key(fle));
for (CodeUnit u : list) {
if (u.getOpcode() == CONST_LOAD) {
ConstantExpr c = (ConstantExpr) u;
Object cst = c.getConstant();
if (cst instanceof Integer || cst instanceof Long) {
if (large((Number) cst, cst instanceof Long)) {
if (other) {
dangerConstants.getNonNull(key(fle)).add((Number) cst);
} else {
constants.getNonNull(key(fle)).add((Number) cst);
}
}
}
}
}
}
use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.
the class ExpressionEvaluator method evaluatePrimitiveConditional.
public Boolean evaluatePrimitiveConditional(ConditionalJumpStmt cond, TaintableSet<ConstantExpr> leftSet, TaintableSet<ConstantExpr> rightSet) {
Boolean val = null;
Iterator<Pair<ConstantExpr, ConstantExpr>> it = leftSet.product(rightSet);
while (it.hasNext()) {
Pair<ConstantExpr, ConstantExpr> lcrc = it.next();
ConstantExpr lc = lcrc.getKey();
ConstantExpr rc = lcrc.getValue();
if (TypeUtils.isPrimitive(lc.getType()) && TypeUtils.isPrimitive(rc.getType())) {
EvaluationFunctor<Boolean> bridge = factory.branch(lc.getType(), rc.getType(), cond.getComparisonType());
/*System.out.println("eval: " + bridge.method + " " + lc.getConstant().getClass() + " " + rc.getConstant().getClass());
System.out.println(" actual: " + lc.getType() + ", " + rc.getType());
System.out.println(" " + lc.getConstant() +" " + rc.getConstant());*/
boolean branchVal = bridge.eval(lc.getConstant(), rc.getConstant());
if (val != null) {
/* inconsistent branch results */
if (val != branchVal) {
return null;
}
} else {
val = branchVal;
}
} else {
/*System.err.println("something::");
System.err.println(" " + cond);
System.err.println(" leftset: " + leftSet);
System.err.println(" rightSet: " + rightSet);|
return;*/
throw new UnsupportedOperationException();
}
}
return val;
}
Aggregations