use of com.oracle.truffle.llvm.runtime.types.AggregateType in project sulong by graalvm.
the class LLVMSymbolReadResolver method resolveElementPointer.
public LLVMExpressionNode resolveElementPointer(SymbolImpl base, List<SymbolImpl> indices) {
LLVMExpressionNode currentAddress = resolve(base);
Type currentType = base.getType();
for (int i = 0, indicesSize = indices.size(); i < indicesSize; i++) {
final SymbolImpl indexSymbol = indices.get(i);
final Type indexType = indexSymbol.getType();
final Long indexInteger = evaluateLongIntegerConstant(indexSymbol);
if (indexInteger == null) {
// the index is determined at runtime
if (currentType instanceof StructureType) {
// according to http://llvm.org/docs/LangRef.html#getelementptr-instruction
throw new IllegalStateException("Indices on structs must be constant integers!");
}
AggregateType aggregate = (AggregateType) currentType;
final long indexedTypeLength = runtime.getContext().getIndexOffset(1, aggregate);
currentType = aggregate.getElementType(1);
final LLVMExpressionNode indexNode = resolve(indexSymbol);
currentAddress = runtime.getNodeFactory().createTypedElementPointer(runtime, currentAddress, indexNode, indexedTypeLength, currentType);
} else {
// the index is a constant integer
AggregateType aggregate = (AggregateType) currentType;
final long addressOffset = runtime.getContext().getIndexOffset(indexInteger, aggregate);
currentType = aggregate.getElementType(indexInteger);
// computed by getelementptr even if it is the same as the basepointer
if (addressOffset != 0 || i == indicesSize - 1) {
final LLVMExpressionNode indexNode;
if (indexType == PrimitiveType.I32) {
indexNode = runtime.getNodeFactory().createLiteral(runtime, 1, PrimitiveType.I32);
} else if (indexType == PrimitiveType.I64) {
indexNode = runtime.getNodeFactory().createLiteral(runtime, 1L, PrimitiveType.I64);
} else {
throw new AssertionError(indexType);
}
currentAddress = runtime.getNodeFactory().createTypedElementPointer(runtime, currentAddress, indexNode, addressOffset, currentType);
}
}
}
return currentAddress;
}
use of com.oracle.truffle.llvm.runtime.types.AggregateType in project sulong by graalvm.
the class Function method createExtractValue.
private void createExtractValue(long[] args) {
int i = 0;
int aggregate = getIndex(args[i++]);
Type aggregateType = null;
if (scope.isValueForwardRef(aggregate)) {
aggregateType = types.get(args[i++]);
}
int index = (int) args[i++];
if (aggregateType == null) {
aggregateType = scope.getValueType(aggregate);
}
if (i != args.length) {
throw new UnsupportedOperationException("Multiple indices are not yet supported!");
}
final Type elementType = ((AggregateType) aggregateType).getElementType(index);
emit(ExtractValueInstruction.fromSymbols(scope.getSymbols(), elementType, aggregate, index));
}
use of com.oracle.truffle.llvm.runtime.types.AggregateType in project sulong by graalvm.
the class LLVMBitcodeInstructionVisitor method visit.
@Override
public void visit(ExtractValueInstruction extract) {
if (!(extract.getAggregate().getType() instanceof ArrayType || extract.getAggregate().getType() instanceof StructureType || extract.getAggregate().getType() instanceof PointerType)) {
throw new IllegalStateException("\'extractvalue\' can only extract elements of arrays and structs!");
}
final LLVMExpressionNode baseAddress = symbols.resolve(extract.getAggregate());
final Type baseType = extract.getAggregate().getType();
final int targetIndex = extract.getIndex();
final Type resultType = extract.getType();
LLVMExpressionNode targetAddress = baseAddress;
final AggregateType aggregateType = (AggregateType) baseType;
long offset = runtime.getContext().getIndexOffset(targetIndex, aggregateType);
final Type targetType = aggregateType.getElementType(targetIndex);
if (targetType != null && !((targetType instanceof StructureType) && (((StructureType) targetType).isPacked()))) {
offset += runtime.getContext().getBytePadding(offset, targetType);
}
if (offset != 0) {
final LLVMExpressionNode oneLiteralNode = nodeFactory.createLiteral(runtime, 1, PrimitiveType.I32);
targetAddress = nodeFactory.createTypedElementPointer(runtime, targetAddress, oneLiteralNode, offset, extract.getType());
}
final LLVMExpressionNode result = nodeFactory.createExtractValue(runtime, resultType, targetAddress);
createFrameWrite(result, extract);
}
use of com.oracle.truffle.llvm.runtime.types.AggregateType in project sulong by graalvm.
the class LLVMBitcodeInstructionVisitor method visit.
@Override
public void visit(InsertValueInstruction insert) {
if (!(insert.getAggregate().getType() instanceof StructureType || insert.getAggregate().getType() instanceof ArrayType)) {
throw new IllegalStateException("\'insertvalue\' can only insert values into arrays and structs!");
}
final AggregateType sourceType = (AggregateType) insert.getAggregate().getType();
final LLVMExpressionNode sourceAggregate = symbols.resolve(insert.getAggregate());
final LLVMExpressionNode valueToInsert = symbols.resolve(insert.getValue());
final Type valueType = insert.getValue().getType();
final int targetIndex = insert.getIndex();
final LLVMExpressionNode resultAggregate = nodeFactory.createAlloca(runtime, sourceType);
final long offset = runtime.getContext().getIndexOffset(targetIndex, sourceType);
final LLVMExpressionNode result = nodeFactory.createInsertValue(runtime, resultAggregate, sourceAggregate, runtime.getContext().getByteSize(sourceType), offset, valueToInsert, valueType);
createFrameWrite(result, insert);
}
Aggregations