/
Branch.java
85 lines (72 loc) · 2.12 KB
/
Branch.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
85
/*
* This file is part of the Jikes RVM project (http://jikesrvm.org).
*
* This file is licensed to You under the Common Public License (CPL);
* You may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.php
*
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership.
*/
package org.mmtk.harness.lang.pcode;
import org.mmtk.harness.lang.Env;
import org.mmtk.harness.lang.ast.AST;
import org.mmtk.harness.lang.compiler.Register;
import org.mmtk.harness.lang.runtime.StackFrame;
/*
* A simple branch construct
*/
public final class Branch extends UnaryOp {
public int target;
public final boolean branchOn;
/**
* Create a branch instruction. Jump to 'target' if cond.loadBoolean() == branchOn
* @param cond Branch condition
* @param branchOn Sense of the comparison
* @param target Jump target
*/
public Branch(AST source, Register cond, boolean branchOn, int target) {
super(source, "if"+branchOn,cond);
this.target = target;
this.branchOn = branchOn;
}
/**
* Create a branch instruction without a target. Target will be filled in
* later by the compiler. Jump to 'target' if cond.loadBoolean() == branchOn
* @param cond Branch condition
* @param branchOn Sense of the comparison
* @param target Jump target
*/
public Branch(AST source, Register cond, boolean branchOn) {
super(source,"if"+branchOn,cond);
this.branchOn = branchOn;
}
public void setBranchTarget(int target) {
this.target = target;
}
public int getBranchTarget() {
return target;
}
@Override
public boolean affectsControlFlow() {
return true;
}
@Override
public void exec(Env env) {
}
@Override
public boolean isBranch() {
return true;
}
@Override
public boolean isTaken(Env env) {
StackFrame frame = env.top();
return frame.get(operand).getBoolValue() == branchOn;
}
@Override
public String toString() {
return String.format("if(%st%d) goto %d", branchOn ? "" : "!", operand, target);
}
}