package com.oracle.truffle.llvm.parser.metadata;
import java.util.Arrays;
public final class MDExpression implements MDBaseNode {
public static final MDExpression EMPTY = new MDExpression(new long[0]);
private final long[] operands;
private MDExpression(long[] operands) {
this.operands = operands;
}
public long getOperand(int i) {
return operands[i];
}
public int getElementCount() {
return operands.length;
}
@Override
public void replace(MDBaseNode oldValue, MDBaseNode newValue) {
}
@Override
public void accept(MetadataVisitor visitor) {
visitor.visit(this);
}
private static final int INDEX_OPERANDSTART = 1;
private static final int INDEX_VERSION = 0;
private static final int VERSION_VALUE_SHIFT = 1;
public static MDExpression create(long[] record) {
final int version = (int) (record[INDEX_VERSION] >> VERSION_VALUE_SHIFT);
long[] ops = Arrays.copyOfRange(record, INDEX_OPERANDSTART, record.length);
ops = upgrade(version, ops);
return new MDExpression(ops);
}
private static final int CURRENT_VERSION = 3;
private static final int V0_FRAGMENT_OFFSET = 3;
private static final long DEFAULT_OPERAND = 0;
private static final class LongList {
private long[] array;
private int size;
LongList(int initialCapacity) {
this.array = new long[initialCapacity];
}
void add(long value) {
if (size >= array.length) {
array = Arrays.copyOf(array, Math.max(8, array.length * 2));
}
array[size++] = value;
}
long[] toArray() {
return array.length == size ? array : Arrays.copyOf(array, size);
}
}
private static long[] upgrade(int version, long[] originalOps) {
final int numOps = originalOps.length;
if (numOps == 0 || version >= CURRENT_VERSION) {
return originalOps;
}
long[] ops = originalOps;
if (version >= 0 && numOps >= V0_FRAGMENT_OFFSET && ops[numOps - V0_FRAGMENT_OFFSET] == DwarfOpcode.BIT_PIECE) {
ops[numOps - V0_FRAGMENT_OFFSET] = DwarfOpcode.LLVM_FRAGMENT;
}
if (version >= 1 && ops[0] == DwarfOpcode.DEREF) {
System.arraycopy(ops, 1, ops, 0, numOps - 1);
ops[numOps - 1] = DwarfOpcode.DEREF;
}
if (version >= 2) {
final LongList buffer = new LongList(numOps);
int i = 0;
while (i < numOps) {
final long op = ops[i++];
switch ((int) op) {
case (int) DwarfOpcode.PLUS:
buffer.add(DwarfOpcode.PLUS_UCONST);
buffer.add(i < numOps ? ops[i++] : DEFAULT_OPERAND);
break;
case (int) DwarfOpcode.MINUS:
buffer.add(DwarfOpcode.CONSTU);
buffer.add(i < numOps ? ops[i++] : DEFAULT_OPERAND);
buffer.add(DwarfOpcode.MINUS);
break;
case (int) DwarfOpcode.CONSTU:
buffer.add(op);
buffer.add(i < numOps ? ops[i++] : DEFAULT_OPERAND);
break;
case (int) DwarfOpcode.LLVM_FRAGMENT:
buffer.add(op);
buffer.add(i < numOps ? ops[i++] : DEFAULT_OPERAND);
buffer.add(i < numOps ? ops[i++] : DEFAULT_OPERAND);
break;
default:
buffer.add(op);
break;
}
}
return buffer.toArray();
}
return ops;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final MDExpression that = (MDExpression) o;
return Arrays.equals(operands, that.operands);
}
@Override
public int hashCode() {
return Arrays.hashCode(operands);
}
}