com.oracle.truffle.api.frame.Frame Java Examples

The following examples show how to use com.oracle.truffle.api.frame.Frame. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObjectGraphUtils.java    From trufflesqueak with MIT License 6 votes vote down vote up
private void addObjectsFromTruffleFrames() {
    CompilerAsserts.neverPartOfCompilation();
    Truffle.getRuntime().iterateFrames(frameInstance -> {
        final Frame current = frameInstance.getFrame(FrameInstance.FrameAccess.READ_ONLY);
        if (!FrameAccess.isTruffleSqueakFrame(current)) {
            return null;
        }
        for (final Object argument : current.getArguments()) {
            addIfUnmarked(argument);
        }
        final CompiledCodeObject blockOrMethod = FrameAccess.getBlockOrMethod(current);
        addIfUnmarked(FrameAccess.getContext(current, blockOrMethod));
        for (final FrameSlot slot : blockOrMethod.getStackSlotsUnsafe()) {
            if (slot == null) {
                return null; // Stop here, slot has not (yet) been created.
            }
            if (current.isObject(slot)) {
                addIfUnmarked(FrameUtil.getObjectSafe(current, slot));
            }
        }
        return null;
    });
}
 
Example #2
Source File: FrameAccess.java    From trufflesqueak with MIT License 6 votes vote down vote up
@TruffleBoundary
public static MaterializedFrame findFrameForMarker(final FrameMarker frameMarker) {
    CompilerDirectives.bailout("Finding materializable frames should never be part of compiled code as it triggers deopts");
    LogUtils.ITERATE_FRAMES.fine("Iterating frames to find a marker...");
    final Frame frame = Truffle.getRuntime().iterateFrames(frameInstance -> {
        final Frame current = frameInstance.getFrame(FrameInstance.FrameAccess.READ_ONLY);
        if (!isTruffleSqueakFrame(current)) {
            return null;
        }
        LogUtils.ITERATE_FRAMES.fine(() -> "..." + FrameAccess.getMethod(current).toString());
        if (frameMarker == getMarker(current)) {
            return frameInstance.getFrame(FrameInstance.FrameAccess.MATERIALIZE);
        }
        return null;
    });
    if (frame == null) {
        throw SqueakException.create("Could not find frame for:", frameMarker);
    } else {
        return frame.materialize();
    }
}
 
Example #3
Source File: FrameAccess.java    From trufflesqueak with MIT License 6 votes vote down vote up
/** Write to a frame slot (slow operation), prefer {@link FrameStackPushNode}. */
public static void setStackSlot(final Frame frame, final FrameSlot frameSlot, final Object value) {
    final FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
    assert frame.getFrameDescriptor().getSlots().contains(frameSlot);
    final FrameSlotKind frameSlotKind = frameDescriptor.getFrameSlotKind(frameSlot);
    final boolean isIllegal = frameSlotKind == FrameSlotKind.Illegal;
    if (value instanceof Boolean && (isIllegal || frameSlotKind == FrameSlotKind.Boolean)) {
        frameDescriptor.setFrameSlotKind(frameSlot, FrameSlotKind.Boolean);
        frame.setBoolean(frameSlot, (boolean) value);
    } else if (value instanceof Long && (isIllegal || frameSlotKind == FrameSlotKind.Long)) {
        frameDescriptor.setFrameSlotKind(frameSlot, FrameSlotKind.Long);
        frame.setLong(frameSlot, (long) value);
    } else if (value instanceof Double && (isIllegal || frameSlotKind == FrameSlotKind.Double)) {
        frameDescriptor.setFrameSlotKind(frameSlot, FrameSlotKind.Double);
        frame.setDouble(frameSlot, (double) value);
    } else {
        frameDescriptor.setFrameSlotKind(frameSlot, FrameSlotKind.Object);
        frame.setObject(frameSlot, value);
    }
}
 
Example #4
Source File: FrameSlotReadNode.java    From trufflesqueak with MIT License 6 votes vote down vote up
@Specialization(replaces = {"readBoolean", "readLong", "readDouble"})
protected final Object readAndClearObject(final Frame frame) {
    final Object value;
    if (!frame.isObject(getSlot())) {
        /*
         * The FrameSlotKind has been set to Object, so from now on all writes to the slot
         * will be Object writes. However, now we are in a frame that still has an old
         * non-Object value. This is a slow-path operation: we read the non-Object value,
         * and clear it immediately as an Object value so that we do not hit this path again
         * multiple times for the same slot of the same frame.
         */
        CompilerDirectives.transferToInterpreter();
        value = frame.getValue(getSlot());
    } else {
        value = FrameUtil.getObjectSafe(frame, getSlot());
    }
    frame.setObject(getSlot(), null);
    return value;
}
 
Example #5
Source File: FrameSlotReadNode.java    From trufflesqueak with MIT License 6 votes vote down vote up
@Specialization(replaces = {"readBoolean", "readLong", "readDouble"})
protected final Object readObject(final Frame frame) {
    if (!frame.isObject(getSlot())) {
        /*
         * The FrameSlotKind has been set to Object, so from now on all writes to the slot
         * will be Object writes. However, now we are in a frame that still has an old
         * non-Object value. This is a slow-path operation: we read the non-Object value,
         * and write it immediately as an Object value so that we do not hit this path again
         * multiple times for the same slot of the same frame.
         */
        CompilerDirectives.transferToInterpreter();
        final Object value = frame.getValue(getSlot());
        assert value != null : "Unexpected `null` value";
        frame.setObject(getSlot(), value);
        return value;
    } else {
        return FrameUtil.getObjectSafe(frame, getSlot());
    }
}
 
Example #6
Source File: FrameSlotReadNode.java    From trufflesqueak with MIT License 6 votes vote down vote up
public static FrameSlotReadNode create(final Frame frame, final int index) {
    // Only clear stack values, not receiver, arguments, or temporary variables.
    final CompiledCodeObject code;
    final int initialSP;
    final BlockClosureObject closure = FrameAccess.getClosure(frame);
    if (closure == null) {
        code = FrameAccess.getMethod(frame);
        initialSP = code.getNumTemps();
    } else {
        code = closure.getCompiledBlock();
        initialSP = code.getNumArgsAndCopied();
    }
    if (index >= initialSP) {
        return FrameSlotReadClearNodeGen.create(code.getStackSlot(index));
    } else {
        return FrameSlotReadNoClearNodeGen.create(code.getStackSlot(index));
    }
}
 
Example #7
Source File: ClosureSymbolNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
@Specialization(replaces = { "readLong", "readBoolean", "readObject" })
protected Object read(VirtualFrame virtualFrame) {
    try {
        return this.readUpStack(Frame::getValue, virtualFrame);
    } catch (FrameSlotTypeException e) {
        // FrameSlotTypeException not thrown
    }
    return null;
}
 
Example #8
Source File: FrameSlotWriteNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(replaces = {"writeBool", "writeLong", "writeDouble"})
protected final void writeObject(final Frame frame, final Object value) {
    /* Initialize type on first write. No-op if kind is already Object. */
    frame.getFrameDescriptor().setFrameSlotKind(getSlot(), FrameSlotKind.Object);

    assert !isNullOrIllegalPrimitive(value) : "Propagation of illegal value detected: " + value;
    frame.setObject(getSlot(), value);
}
 
Example #9
Source File: FrameSlotWriteNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "isDoubleOrIllegal(frame)")
protected final void writeDouble(final Frame frame, final double value) {
    /* Initialize type on first write. No-op if kind is already Double. */
    frame.getFrameDescriptor().setFrameSlotKind(getSlot(), FrameSlotKind.Double);

    frame.setDouble(getSlot(), value);
}
 
Example #10
Source File: FrameSlotWriteNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "isLongOrIllegal(frame)")
protected final void writeLong(final Frame frame, final long value) {
    /* Initialize type on first write. No-op if kind is already Long. */
    frame.getFrameDescriptor().setFrameSlotKind(getSlot(), FrameSlotKind.Long);

    frame.setLong(getSlot(), value);
}
 
Example #11
Source File: HashemLexicalScope.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
public Object getArguments(Frame frame) {
    if (root == null) {
        // No arguments for block scope
        return null;
    }
    // The slots give us names of the arguments:
    Map<String, FrameSlot> argSlots = collectArgs(block);
    // The frame's arguments array give us the argument values:
    Object[] args = (frame != null) ? frame.getArguments() : null;
    // Create a TruffleObject having the arguments as properties:
    return new VariablesMapObject(argSlots, args, frame);
}
 
Example #12
Source File: HashemStackTraceBuiltin.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@TruffleBoundary
private static String createStackTrace() {
    final StringBuilder str = new StringBuilder();

    Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {
        private int skip = 1; // skip stack trace builtin

        @Override
        public Integer visitFrame(FrameInstance frameInstance) {
            if (skip > 0) {
                skip--;
                return null;
            }
            CallTarget callTarget = frameInstance.getCallTarget();
            Frame frame = frameInstance.getFrame(FrameAccess.READ_ONLY);
            RootNode rn = ((RootCallTarget) callTarget).getRootNode();
            // ignore internal or interop stack frames
            if (rn.isInternal() || rn.getLanguageInfo() == null) {
                return 1;
            }
            if (str.length() > 0) {
                str.append(System.getProperty("line.separator"));
            }
            str.append("Frame: ").append(rn.toString());
            FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
            for (FrameSlot s : frameDescriptor.getSlots()) {
                str.append(", ").append(s.getIdentifier()).append("=").append(frame.getValue(s));
            }
            return null;
        }
    });
    return str.toString();
}
 
Example #13
Source File: TruffleAST.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Object[] getRawSlots() {
    Frame frame = frameInstance.getFrame(FrameInstance.FrameAccess.MATERIALIZE);
    List<? extends FrameSlot> slots = frame.getFrameDescriptor().getSlots();
    int n = slots.size();
    Object[] slotInfo = new Object[2*n];
    for (int i = 0; i < n; i++) {
        FrameSlot slot = slots.get(i);
        slotInfo[2*i] = slot.getIdentifier();
        slotInfo[2*i + 1] = frame.getValue(slot);
    }
    return slotInfo;
}
 
Example #14
Source File: FrameSlotWriteNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "isBooleanOrIllegal(frame)")
protected final void writeBool(final Frame frame, final boolean value) {
    /* Initialize type on first write. No-op if kind is already Boolean. */
    frame.getFrameDescriptor().setFrameSlotKind(getSlot(), FrameSlotKind.Boolean);

    frame.setBoolean(getSlot(), value);
}
 
Example #15
Source File: SqueakLanguage.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Override
protected Iterable<Scope> findLocalScopes(final SqueakImageContext context, final Node node, final Frame frame) {
    // TODO: support access at parse time (frame == null).
    if (!FrameAccess.isTruffleSqueakFrame(frame)) {
        return super.findLocalScopes(context, node, frame);
    }
    final CompiledCodeObject blockOrMethod = FrameAccess.getBlockOrMethod(frame);
    final String name = blockOrMethod.toString();
    final Object receiver = FrameAccess.getReceiver(frame);
    final ContextObjectInfo variables = new ContextObjectInfo(frame);
    final InteropArray arguments = new InteropArray(frame.getArguments());
    return Collections.singletonList(Scope.newBuilder(name, variables).node(node).receiver(receiver.toString(), receiver).arguments(arguments).build());
}
 
Example #16
Source File: ContextObjectInfo.java    From trufflesqueak with MIT License 5 votes vote down vote up
public ContextObjectInfo(final Frame frame) {
    this.frame = frame;
    final CompiledCodeObject code = FrameAccess.getBlockOrMethod(frame);
    for (final FrameSlot slot : code.getStackSlotsUnsafe()) {
        if (slot == null) {
            break;
        }
        slots.put(Objects.toString(slot.getIdentifier()), slot);
    }
}
 
Example #17
Source File: HashemLexicalScope.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
public Object getVariables(Frame frame) {
    Map<String, FrameSlot> vars = getVars();
    Object[] args = null;
    // Use arguments when the current node is above the block
    if (current == null) {
        args = (frame != null) ? frame.getArguments() : null;
    }
    return new VariablesMapObject(vars, args, frame);
}
 
Example #18
Source File: GetOrCreateContextNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
public static final ContextObject getOrCreateFromActiveProcessUncached(final Frame frame) {
    CompilerAsserts.neverPartOfCompilation();
    final CompiledCodeObject code = FrameAccess.getBlockOrMethod(frame);
    final ContextObject context = FrameAccess.getContext(frame, code);
    if (context != null) {
        return context;
    } else {
        final ContextObject result = ContextObject.create(frame.materialize(), code);
        result.setProcess(GetActiveProcessNode.getUncached().execute());
        return result;
    }
}
 
Example #19
Source File: FrameStackTopNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
public Object execute(final Frame frame) {
    if (readNode == null) {
        CompilerDirectives.transferToInterpreterAndInvalidate();
        final int stackPointer = FrameAccess.getStackPointerSlow(frame) - 1;
        readNode = FrameSlotReadNode.create(FrameAccess.getStackSlot(frame, stackPointer));
    }
    return readNode.executeRead(frame);
}
 
Example #20
Source File: HashemLanguage.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Override
public Iterable<Scope> findLocalScopes(HashemContext context, Node node, Frame frame) {
    final HashemLexicalScope scope = HashemLexicalScope.createScope(node);
    return new Iterable<Scope>() {
        @Override
        public Iterator<Scope> iterator() {
            return new Iterator<Scope>() {
                private HashemLexicalScope previousScope;
                private HashemLexicalScope nextScope = scope;

                @Override
                public boolean hasNext() {
                    if (nextScope == null) {
                        nextScope = previousScope.findParent();
                    }
                    return nextScope != null;
                }

                @Override
                public Scope next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    Object functionObject = findFunctionObject();
                    Scope vscope = Scope.newBuilder(nextScope.getName(), nextScope.getVariables(frame)).node(nextScope.getNode()).arguments(nextScope.getArguments(frame)).rootInstance(
                            functionObject).build();
                    previousScope = nextScope;
                    nextScope = null;
                    return vscope;
                }

                private Object findFunctionObject() {
                    String name = node.getRootNode().getName();
                    return context.getFunctionRegistry().getFunction(name);
                }
            };
        }
    };
}
 
Example #21
Source File: DebugUtils.java    From trufflesqueak with MIT License 5 votes vote down vote up
public static void printSqStackTrace() {
    CompilerAsserts.neverPartOfCompilation("For debugging purposes only");
    final boolean isCIBuild = System.getenv().containsKey("GITHUB_ACTIONS");
    final int[] depth = new int[1];
    final Object[] lastSender = new Object[]{null};
    final PrintWriter err = SqueakLanguage.getContext().getError();
    err.println("== Truffle stack trace ===========================================================");
    Truffle.getRuntime().iterateFrames(frameInstance -> {
        if (depth[0]++ > 50 && isCIBuild) {
            return null;
        }
        final Frame current = frameInstance.getFrame(FrameInstance.FrameAccess.READ_ONLY);
        if (!FrameAccess.isTruffleSqueakFrame(current)) {
            return null;
        }
        final CompiledMethodObject method = FrameAccess.getMethod(current);
        lastSender[0] = FrameAccess.getSender(current);
        final Object marker = FrameAccess.getMarker(current, method);
        final Object context = FrameAccess.getContext(current, method);
        final String prefix = FrameAccess.getClosure(current) == null ? "" : "[] in ";
        final String argumentsString = ArrayUtils.toJoinedString(", ", FrameAccess.getReceiverAndArguments(current));
        err.println(MiscUtils.format("%s%s #(%s) [marker: %s, context: %s, sender: %s]", prefix, method, argumentsString, marker, context, lastSender[0]));
        return null;
    });
    if (lastSender[0] instanceof ContextObject) {
        err.println("== Squeak frames ================================================================");
        printSqStackTrace((ContextObject) lastSender[0]);
    }
}
 
Example #22
Source File: MumblerNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
protected static MaterializedFrame getLexicalScope(Frame frame) {
    Object[] args = frame.getArguments();
    if (args.length > 0) {
        return (MaterializedFrame) frame.getArguments()[0];
    } else {
        return null;
    }
}
 
Example #23
Source File: ClosureSymbolNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
@ExplodeLoop
public <T> T readUpStack(FrameGet<T> getter, Frame frame)
        throws FrameSlotTypeException {

    Frame lookupFrame = frame;
    for (int i = 0; i < this.getDepth(); i++) {
        lookupFrame = getLexicalScope(lookupFrame);
    }
    return getter.get(lookupFrame, this.getSlot());
}
 
Example #24
Source File: FrameAccess.java    From trufflesqueak with MIT License 5 votes vote down vote up
public static void setArgumentIfInRange(final Frame frame, final int index, final Object value) {
    assert index >= 0;
    final Object[] frameArguments = frame.getArguments();
    final int argumentIndex = ArgumentIndicies.ARGUMENTS_START.ordinal() + index;
    if (argumentIndex < frameArguments.length) {
        frameArguments[argumentIndex] = value;
    }
}
 
Example #25
Source File: FrameAccess.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static FrameSlot getContextSlot(final Frame frame) {
    CompilerAsserts.neverPartOfCompilation();
    return frame.getFrameDescriptor().findFrameSlot(SLOT_IDENTIFIER.THIS_CONTEXT);
}
 
Example #26
Source File: FrameAccess.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static int getStackPointerSlow(final Frame frame) {
    return getStackPointer(frame, getStackPointerSlot(frame));
}
 
Example #27
Source File: FrameAccess.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static void setInstructionPointer(final Frame frame, final FrameSlot instructionPointerSlot, final int value) {
    frame.setInt(instructionPointerSlot, value);
}
 
Example #28
Source File: FrameAccess.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static void setInstructionPointer(final Frame frame, final CompiledCodeObject code, final int value) {
    setInstructionPointer(frame, code.getInstructionPointerSlot(), value);
}
 
Example #29
Source File: FrameAccess.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static FrameSlot getStackPointerSlot(final Frame frame) {
    CompilerAsserts.neverPartOfCompilation();
    return frame.getFrameDescriptor().findFrameSlot(SLOT_IDENTIFIER.STACK_POINTER);
}
 
Example #30
Source File: FrameAccess.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static ContextObject getContext(final Frame frame, final FrameSlot thisContextSlot) {
    return (ContextObject) FrameUtil.getObjectSafe(frame, thisContextSlot);
}