/
NotInstr.java
84 lines (67 loc) · 2.32 KB
/
NotInstr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.jruby.ir.instructions;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.IRScope;
import org.jruby.ir.Operation;
import org.jruby.ir.operands.UnboxedBoolean;
import org.jruby.ir.operands.Operand;
import org.jruby.ir.operands.Variable;
import org.jruby.ir.transformations.inlining.InlinerInfo;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.util.Map;
public class NotInstr extends Instr implements ResultInstr, FixedArityInstr {
private Operand arg;
private Variable result;
public NotInstr(Variable result, Operand arg) {
super(Operation.NOT);
assert result != null: "NotInstr result is null";
this.arg = arg;
this.result = result;
}
public Operand getArg() {
return arg;
}
@Override
public Operand[] getOperands() {
return new Operand[]{arg};
}
@Override
public Variable getResult() {
return result;
}
@Override
public void updateResult(Variable v) {
this.result = v;
}
@Override
public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
arg = arg.getSimplifiedOperand(valueMap, force);
}
@Override
public String toString() {
return super.toString() + "(" + arg + ")";
}
@Override
public Instr cloneForInlining(InlinerInfo ii) {
return new NotInstr(ii.getRenamedVariable(result), arg.cloneForInlining(ii));
}
private Operand flipLogical(IRScope scope, UnboxedBoolean value) {
return value.isTrue() ? scope.getManager().getFalse() : scope.getManager().getTrue();
}
@Override
public Operand simplifyAndGetResult(IRScope scope, Map<Operand, Operand> valueMap) {
simplifyOperands(valueMap, false);
return arg instanceof UnboxedBoolean ? flipLogical(scope, (UnboxedBoolean) arg) : null;
}
@Override
public Object interpret(ThreadContext context, DynamicScope currDynScope, IRubyObject self, Object[] temp, Block block) {
boolean not = !((IRubyObject) arg.retrieve(context, self, currDynScope, temp)).isTrue();
return context.runtime.newBoolean(not);
}
@Override
public void visit(IRVisitor visitor) {
visitor.NotInstr(this);
}
}