package org.jruby.ir.operands;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.persistence.IRReaderDecoder;
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.ir.transformations.inlining.CloneInfo;
import java.util.List;
public class Label extends Operand {
public static final Label UNRESCUED_REGION_LABEL = new Label("UNRESCUED_REGION", 0);
private static final Label GLOBAL_ENSURE_BLOCK_LABEL = new Label("_GLOBAL_ENSURE_BLOCK_", 0);
public final String prefix;
public final int id;
private int targetPC = -1;
public static Label getGlobalEnsureBlockLabel() { return GLOBAL_ENSURE_BLOCK_LABEL.clone(); }
public Label(String prefix, int id) {
super();
this.prefix = prefix;
this.id = id;
}
@Override
public OperandType getOperandType() {
return OperandType.LABEL;
}
@Override
public String toString() {
return prefix + '_' + id + ':' + targetPC;
}
@Override
public void addUsedVariables(List<Variable> l) {
}
@Override
public boolean canCopyPropagate() {
return true;
}
@Override
public int hashCode() {
return 11 * (77 + System.identityHashCode(prefix)) + id;
}
@Override
public boolean equals(Object o) {
return (o instanceof Label) && id == ((Label) o).id && prefix.equals(((Label)o).prefix);
}
public boolean isGlobalEnsureBlockLabel() {
return this.equals(GLOBAL_ENSURE_BLOCK_LABEL);
}
public Label clone() {
Label newL = new Label(prefix, id);
newL.setTargetPC(getTargetPC());
return newL;
}
@Override
public Operand cloneForInlining(CloneInfo ii) {
return ii.getRenamedLabel(this);
}
public void setTargetPC(int i) {
this.targetPC = i;
}
public int getTargetPC() {
return this.targetPC;
}
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(prefix);
e.encode(id);
}
public static Label decode(IRReaderDecoder d) {
String prefix = d.decodeString();
int id = d.decodeInt();
if ("_GLOBAL_ENSURE_BLOCK".equals(prefix)) return new Label("_GLOBAL_ENSURE_BLOCK", 0);
String fullLabel = prefix + '_' + id;
if (d.getVars().containsKey(fullLabel)) {
return (Label) d.getVars().get(fullLabel);
}
Label newLabel = new Label(prefix, id);
d.getVars().put(fullLabel, newLabel);
return newLabel;
}
@Override
public void visit(IRVisitor visitor) {
visitor.Label(this);
}
}