use of com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.StructureConstant in project sulong by graalvm.
the class LLVMParserRuntime method resolveStructor.
private LLVMExpressionNode[] resolveStructor(GlobalValueSymbol globalVar, Comparator<Pair<Integer, ?>> priorityComparator) {
if (!(globalVar.getValue() instanceof ArrayConstant)) {
// array globals of length 0 may be initialized with scalar null
return LLVMExpressionNode.NO_EXPRESSIONS;
}
final Object globalVariableDescriptor = scope.getGlobalVariable(globalVar.getName());
final ArrayConstant arrayConstant = (ArrayConstant) globalVar.getValue();
final int elemCount = arrayConstant.getElementCount();
final StructureType elementType = (StructureType) arrayConstant.getType().getElementType();
final int structSize = getContext().getByteSize(elementType);
final FunctionType functionType = (FunctionType) ((PointerType) elementType.getElementType(1)).getPointeeType();
final int indexedTypeLength = getContext().getByteAlignment(functionType);
final ArrayList<Pair<Integer, LLVMExpressionNode>> structors = new ArrayList<>(elemCount);
for (int i = 0; i < elemCount; i++) {
final LLVMExpressionNode globalVarAddress = nodeFactory.createLiteral(this, globalVariableDescriptor, new PointerType(globalVar.getType()));
final LLVMExpressionNode iNode = nodeFactory.createLiteral(this, i, PrimitiveType.I32);
final LLVMExpressionNode structPointer = nodeFactory.createTypedElementPointer(this, globalVarAddress, iNode, structSize, elementType);
final LLVMExpressionNode loadedStruct = nodeFactory.createLoad(this, elementType, structPointer);
final LLVMExpressionNode oneLiteralNode = nodeFactory.createLiteral(this, 1, PrimitiveType.I32);
final LLVMExpressionNode functionLoadTarget = nodeFactory.createTypedElementPointer(this, loadedStruct, oneLiteralNode, indexedTypeLength, functionType);
final LLVMExpressionNode loadedFunction = nodeFactory.createLoad(this, functionType, functionLoadTarget);
final LLVMExpressionNode[] argNodes = new LLVMExpressionNode[] { nodeFactory.createFrameRead(this, PointerType.VOID, rootFrame.findFrameSlot(LLVMStack.FRAME_ID)) };
final LLVMExpressionNode functionCall = nodeFactory.createFunctionCall(this, loadedFunction, argNodes, functionType, null);
final StructureConstant structorDefinition = (StructureConstant) arrayConstant.getElement(i);
final SymbolImpl prioritySymbol = structorDefinition.getElement(0);
final Integer priority = LLVMSymbolReadResolver.evaluateIntegerConstant(prioritySymbol);
structors.add(new Pair<>(priority != null ? priority : LEAST_CONSTRUCTOR_PRIORITY, functionCall));
}
return structors.stream().sorted(priorityComparator).map(Pair::getSecond).toArray(LLVMExpressionNode[]::new);
}
Aggregations