Search in sources :

Example 11 with FrameSlot

use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.

the class DefaultScope method getVariables.

private static Object getVariables(RootNode root, Frame frame) {
    List<? extends FrameSlot> slots;
    if (frame == null) {
        slots = root.getFrameDescriptor().getSlots();
    } else {
        slots = frame.getFrameDescriptor().getSlots();
        // Filter out slots with null values:
        List<FrameSlot> nonNulls = null;
        int lastI = 0;
        for (int i = 0; i < slots.size(); i++) {
            FrameSlot slot = slots.get(i);
            if (frame.getValue(slot) == null || isInternal(slot)) {
                if (nonNulls == null) {
                    nonNulls = new ArrayList<>(slots.size());
                }
                nonNulls.addAll(slots.subList(lastI, i));
                lastI = i + 1;
            }
        }
        if (nonNulls != null) {
            if (lastI < slots.size()) {
                nonNulls.addAll(slots.subList(lastI, slots.size()));
            }
            slots = nonNulls;
        }
    }
    Map<String, FrameSlot> slotsMap;
    if (slots.isEmpty()) {
        slotsMap = Collections.emptyMap();
    } else if (slots.size() == 1) {
        FrameSlot slot = slots.get(0);
        slotsMap = Collections.singletonMap(Objects.toString(slot.getIdentifier()), slot);
    } else {
        slotsMap = new LinkedHashMap<>(slots.size());
        for (FrameSlot slot : slots) {
            slotsMap.put(Objects.toString(slot.getIdentifier()), slot);
        }
    }
    return new VariablesMapObject(slotsMap, frame);
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with FrameSlot

use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.

the class SLHelloEqualsWorldBuiltin method change.

@Specialization
@TruffleBoundary
public String change() {
    FrameInstance frameInstance = Truffle.getRuntime().getCallerFrame();
    Frame frame = frameInstance.getFrame(FrameAccess.READ_WRITE);
    FrameSlot slot = frame.getFrameDescriptor().findOrAddFrameSlot("hello");
    frame.setObject(slot, "world");
    return "world";
}
Also used : Frame(com.oracle.truffle.api.frame.Frame) FrameSlot(com.oracle.truffle.api.frame.FrameSlot) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) Specialization(com.oracle.truffle.api.dsl.Specialization) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 13 with FrameSlot

use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.

the class SLNodeFactory method createRead.

/**
 * Returns a {@link SLReadLocalVariableNode} if this read is a local variable or a
 * {@link SLFunctionLiteralNode} if this read is global. In SL, the only global names are
 * functions.
 *
 * @param nameNode The name of the variable/function being read
 * @return either:
 *         <ul>
 *         <li>A SLReadLocalVariableNode representing the local variable being read.</li>
 *         <li>A SLFunctionLiteralNode representing the function definition.</li>
 *         <li>null if nameNode is null.</li>
 *         </ul>
 */
public SLExpressionNode createRead(SLExpressionNode nameNode) {
    if (nameNode == null) {
        return null;
    }
    String name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
    final SLExpressionNode result;
    final FrameSlot frameSlot = lexicalScope.locals.get(name);
    if (frameSlot != null) {
        /* Read of a local variable. */
        result = SLReadLocalVariableNodeGen.create(frameSlot);
    } else {
        /* Read of a global name. In our language, the only global names are functions. */
        result = new SLFunctionLiteralNode(language, name);
    }
    result.setSourceSection(nameNode.getSourceCharIndex(), nameNode.getSourceLength());
    return result;
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) SLFunctionLiteralNode(com.oracle.truffle.sl.nodes.expression.SLFunctionLiteralNode) SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLStringLiteralNode(com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)

Example 14 with FrameSlot

use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.

the class SLNodeFactory method createAssignment.

/**
 * Returns an {@link SLWriteLocalVariableNode} for the given parameters.
 *
 * @param nameNode The name of the variable being assigned
 * @param valueNode The value to be assigned
 * @return An SLExpressionNode for the given parameters. null if nameNode or valueNode is null.
 */
public SLExpressionNode createAssignment(SLExpressionNode nameNode, SLExpressionNode valueNode) {
    if (nameNode == null || valueNode == null) {
        return null;
    }
    String name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
    FrameSlot frameSlot = frameDescriptor.findOrAddFrameSlot(name);
    lexicalScope.locals.put(name, frameSlot);
    final SLExpressionNode result = SLWriteLocalVariableNodeGen.create(valueNode, frameSlot);
    if (valueNode.hasSource()) {
        final int start = nameNode.getSourceCharIndex();
        final int length = valueNode.getSourceEndIndex() - start;
        result.setSourceSection(start, length);
    }
    return result;
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLStringLiteralNode(com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)

Example 15 with FrameSlot

use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.

the class SLLexicalScope method collectArgs.

private static Map<String, FrameSlot> collectArgs(Node block) {
    // Arguments are pushed to frame slots at the beginning of the function block.
    // To collect argument slots, search for SLReadArgumentNode inside of
    // SLWriteLocalVariableNode.
    Map<String, FrameSlot> args = new LinkedHashMap<>(4);
    NodeUtil.forEachChild(block, new NodeVisitor() {

        // The current write node containing a slot
        private SLWriteLocalVariableNode wn;

        @Override
        public boolean visit(Node node) {
            // When there is a write node, search for SLReadArgumentNode among its children:
            if (node instanceof SLWriteLocalVariableNode) {
                wn = (SLWriteLocalVariableNode) node;
                boolean all = NodeUtil.forEachChild(node, this);
                wn = null;
                return all;
            } else if (wn != null && (node instanceof SLReadArgumentNode)) {
                FrameSlot slot = wn.getSlot();
                String name = Objects.toString(slot.getIdentifier());
                assert !args.containsKey(name) : name + " argument exists already.";
                args.put(name, slot);
                return true;
            } else if (wn == null && (node instanceof SLStatementNode)) {
                // A different SL node - we're done.
                return false;
            } else {
                return NodeUtil.forEachChild(node, this);
            }
        }
    });
    return args;
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode) LinkedHashMap(java.util.LinkedHashMap) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor)

Aggregations

FrameSlot (com.oracle.truffle.api.frame.FrameSlot)47 LLVMExpressionNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode)12 PrimitiveType (com.oracle.truffle.llvm.runtime.types.PrimitiveType)11 PointerType (com.oracle.truffle.llvm.runtime.types.PointerType)10 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)7 TypeDescriptor (cz.cuni.mff.d3s.trupple.parser.identifierstable.types.TypeDescriptor)7 StructureType (com.oracle.truffle.llvm.runtime.types.StructureType)6 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Type (com.oracle.truffle.llvm.runtime.types.Type)5 CallTarget (com.oracle.truffle.api.CallTarget)4 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)3 RootNode (com.oracle.truffle.api.nodes.RootNode)3 Phi (com.oracle.truffle.llvm.parser.LLVMPhiManager.Phi)3 LLVMArithmeticInstructionType (com.oracle.truffle.llvm.parser.instructions.LLVMArithmeticInstructionType)3 LLVMConversionType (com.oracle.truffle.llvm.parser.instructions.LLVMConversionType)3 SymbolImpl (com.oracle.truffle.llvm.parser.model.SymbolImpl)3 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 Frame (com.oracle.truffle.api.frame.Frame)2 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2