package com.oracle.truffle.llvm.runtime.nodes.cast;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.llvm.runtime.LLVMIVarBit;
import com.oracle.truffle.llvm.runtime.LLVMIVarBitLarge;
import com.oracle.truffle.llvm.runtime.LLVMIVarBitSmall;
import com.oracle.truffle.llvm.runtime.floating.LLVM80BitFloat;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMToNativeNode;
import com.oracle.truffle.llvm.runtime.nodes.cast.LLVMToVarINodeGen.LLVMBitcastToIVarNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.cast.LLVMToVarINodeGen.LLVMSignedCastToIVarNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.cast.LLVMToVarINodeGen.LLVMUnsignedCastToIVarNodeGen;
import com.oracle.truffle.llvm.runtime.pointer.LLVMPointer;
@NodeChild(value = "fromNode", type = LLVMExpressionNode.class)
@NodeField(type = int.class, name = "bits")
public abstract class LLVMToVarINode extends LLVMExpressionNode {
public abstract int getBits();
protected abstract LLVMIVarBit executeWith(long value);
protected LLVMToVarINode createRecursive() {
throw new IllegalStateException("abstract node LLVMToVarINode used");
}
@Specialization
protected LLVMIVarBit doPointer(LLVMPointer from,
@Cached("createToNativeWithTarget()") LLVMToNativeNode toNative,
@Cached("createRecursive()") LLVMToVarINode recursive) {
long ptr = toNative.executeWithTarget(from).asNative();
return recursive.executeWith(ptr);
}
public abstract static class LLVMSignedCastToIVarNode extends LLVMToVarINode {
@Override
protected LLVMToVarINode createRecursive() {
return LLVMSignedCastToIVarNodeGen.create(null, getBits());
}
@Specialization
protected LLVMIVarBit doI8(byte from) {
return LLVMIVarBit.fromByte(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI16(short from) {
return LLVMIVarBit.fromShort(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI32(int from) {
return LLVMIVarBit.fromInt(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI64(long from) {
return LLVMIVarBit.fromLong(getBits(), from);
}
@Specialization
protected LLVMIVarBit doIVarBit(LLVMIVarBitLarge from) {
return LLVMIVarBit.create(getBits(), from.getSignExtendedBytes(), from.getBitSize(), true);
}
@Specialization
protected LLVMIVarBit doIVarBit(LLVMIVarBitSmall from) {
return LLVMIVarBit.create(getBits(), from.getCleanedValue(true), from.getBitSize(), true);
}
@Specialization
protected LLVMIVarBit do80BitFloat(LLVM80BitFloat from) {
return LLVMIVarBit.create(getBits(), from.getBytesBigEndian(), LLVM80BitFloat.BIT_WIDTH, true);
}
}
public abstract static class LLVMUnsignedCastToIVarNode extends LLVMToVarINode {
@Override
protected LLVMToVarINode createRecursive() {
return LLVMUnsignedCastToIVarNodeGen.create(null, getBits());
}
@Specialization
protected LLVMIVarBit doI8(byte from) {
return LLVMIVarBit.createZeroExt(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI16(short from) {
return LLVMIVarBit.createZeroExt(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI32(int from) {
return LLVMIVarBit.createZeroExt(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI64(long from) {
return LLVMIVarBit.createZeroExt(getBits(), from);
}
@Specialization
protected LLVMIVarBit doIVarBit(LLVMIVarBitLarge from) {
return LLVMIVarBit.create(getBits(), from.getBytes(), from.getBitSize(), false);
}
@Specialization
protected LLVMIVarBit doIVarBit(LLVMIVarBitSmall from) {
return LLVMIVarBit.create(getBits(), from.getCleanedValue(false), from.getBitSize(), false);
}
}
public abstract static class LLVMBitcastToIVarNode extends LLVMToVarINode {
@Override
protected LLVMToVarINode createRecursive() {
return LLVMBitcastToIVarNodeGen.create(null, getBits());
}
@Specialization
protected LLVMIVarBit doI8(byte from) {
return LLVMIVarBit.fromByte(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI16(short from) {
return LLVMIVarBit.fromShort(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI32(int from) {
return LLVMIVarBit.fromInt(getBits(), from);
}
@Specialization
protected LLVMIVarBit doI64(long from) {
return LLVMIVarBit.fromLong(getBits(), from);
}
@Specialization
protected LLVMIVarBit doIVarBit(LLVMIVarBit from) {
return from;
}
@Specialization
protected LLVMIVarBit doFloat(float from) {
return LLVMIVarBit.fromInt(getBits(), Float.floatToIntBits(from));
}
@Specialization
protected LLVMIVarBit doDouble(double from) {
return LLVMIVarBit.fromLong(getBits(), Double.doubleToLongBits(from));
}
@Specialization
protected LLVMIVarBit do80BitFloat(LLVM80BitFloat from) {
assert getBits() == LLVM80BitFloat.BIT_WIDTH;
return LLVMIVarBit.create(getBits(), from.getBytesBigEndian(), LLVM80BitFloat.BIT_WIDTH, true);
}
}
}