package org.graalvm.compiler.lir;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.graalvm.compiler.asm.Label;
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
import org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph;
import org.graalvm.compiler.core.common.cfg.BlockMap;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.lir.StandardOp.BlockEndOp;
import org.graalvm.compiler.lir.StandardOp.LabelHoldingOp;
import org.graalvm.compiler.lir.StandardOp.LabelOp;
import org.graalvm.compiler.lir.gen.LIRGenerator;
import org.graalvm.compiler.options.OptionValues;
public final class LIR extends LIRGenerator.VariableProvider {
private final AbstractControlFlowGraph<?> cfg;
private final AbstractBlockBase<?>[] linearScanOrder;
private final AbstractBlockBase<?>[] codeEmittingOrder;
private final BlockMap<ArrayList<LIRInstruction>> lirInstructions;
private boolean hasArgInCallerFrame;
private final OptionValues options;
private final DebugContext debug;
public LIR(AbstractControlFlowGraph<?> cfg,
AbstractBlockBase<?>[] linearScanOrder,
AbstractBlockBase<?>[] codeEmittingOrder,
OptionValues options,
DebugContext debug) {
this.cfg = cfg;
this.codeEmittingOrder = codeEmittingOrder;
this.linearScanOrder = linearScanOrder;
this.lirInstructions = new BlockMap<>(cfg);
this.options = options;
this.debug = debug;
}
public AbstractControlFlowGraph<?> getControlFlowGraph() {
return cfg;
}
public OptionValues getOptions() {
return options;
}
public DebugContext getDebug() {
return debug;
}
public boolean hasDebugInfo() {
for (AbstractBlockBase<?> b : linearScanOrder()) {
for (LIRInstruction op : getLIRforBlock(b)) {
if (op.hasState()) {
return true;
}
}
}
return false;
}
public ArrayList<LIRInstruction> getLIRforBlock(AbstractBlockBase<?> block) {
return lirInstructions.get(block);
}
public void setLIRforBlock(AbstractBlockBase<?> block, ArrayList<LIRInstruction> list) {
assert getLIRforBlock(block) == null : "lir instruction list should only be initialized once";
lirInstructions.put(block, list);
}
public AbstractBlockBase<?>[] linearScanOrder() {
return linearScanOrder;
}
public AbstractBlockBase<?>[] codeEmittingOrder() {
return codeEmittingOrder;
}
public void setHasArgInCallerFrame() {
hasArgInCallerFrame = true;
}
public boolean hasArgInCallerFrame() {
return hasArgInCallerFrame;
}
public static AbstractBlockBase<?> getNextBlock(AbstractBlockBase<?>[] blocks, int blockIndex) {
for (int nextIndex = blockIndex + 1; nextIndex > 0 && nextIndex < blocks.length; nextIndex++) {
AbstractBlockBase<?> nextBlock = blocks[nextIndex];
if (nextBlock != null) {
return nextBlock;
}
}
return null;
}
public static LabelRef getExceptionEdge(LIRInstruction op) {
final LabelRef[] exceptionEdge = {null};
op.forEachState(state -> {
if (state.exceptionEdge != null) {
assert exceptionEdge[0] == null;
exceptionEdge[0] = state.exceptionEdge;
}
});
return exceptionEdge[0];
}
public static final int MAX_EXCEPTION_EDGE_OP_DISTANCE_FROM_END = 3;
public static boolean verifyBlock(LIR lir, AbstractBlockBase<?> block) {
ArrayList<LIRInstruction> ops = lir.getLIRforBlock(block);
if (ops.size() == 0) {
return false;
}
assert ops.get(0) instanceof LabelOp : String.format("Not a Label %s (Block %s)", ops.get(0).getClass(), block);
LIRInstruction opWithExceptionEdge = null;
int index = 0;
int lastIndex = ops.size() - 1;
for (LIRInstruction op : ops.subList(0, lastIndex)) {
assert !(op instanceof BlockEndOp) : String.format("BlockEndOp %s (Block %s)", op.getClass(), block);
LabelRef exceptionEdge = getExceptionEdge(op);
if (exceptionEdge != null) {
assert opWithExceptionEdge == null : "multiple ops with an exception edge not allowed";
opWithExceptionEdge = op;
int distanceFromEnd = lastIndex - index;
assert distanceFromEnd <= MAX_EXCEPTION_EDGE_OP_DISTANCE_FROM_END;
}
index++;
}
LIRInstruction end = ops.get(lastIndex);
assert end instanceof BlockEndOp : String.format("Not a BlockEndOp %s (Block %s)", end.getClass(), block);
return true;
}
public static boolean verifyBlocks(LIR lir, AbstractBlockBase<?>[] blocks) {
for (AbstractBlockBase<?> block : blocks) {
if (block == null) {
continue;
}
for (AbstractBlockBase<?> sux : block.getSuccessors()) {