Search in sources :

Example 1 with Pair

use of com.oracle.truffle.llvm.parser.util.Pair 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);
}
Also used : FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) ArrayList(java.util.ArrayList) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) StructureConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.StructureConstant) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) ArrayConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.ArrayConstant) Pair(com.oracle.truffle.llvm.parser.util.Pair)

Aggregations

SymbolImpl (com.oracle.truffle.llvm.parser.model.SymbolImpl)1 ArrayConstant (com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.ArrayConstant)1 StructureConstant (com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.StructureConstant)1 Pair (com.oracle.truffle.llvm.parser.util.Pair)1 LLVMExpressionNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode)1 FunctionType (com.oracle.truffle.llvm.runtime.types.FunctionType)1 PointerType (com.oracle.truffle.llvm.runtime.types.PointerType)1 StructureType (com.oracle.truffle.llvm.runtime.types.StructureType)1 ArrayList (java.util.ArrayList)1