Search in sources :

Example 6 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class SLLanguage method parse.

@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
    Source source = request.getSource();
    Map<TruffleString, RootCallTarget> functions;
    /*
         * Parse the provided source. At this point, we do not have a SLContext yet. Registration of
         * the functions with the SLContext happens lazily in SLEvalRootNode.
         */
    if (request.getArgumentNames().isEmpty()) {
        functions = SimpleLanguageParser.parseSL(this, source);
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("function main(");
        String sep = "";
        for (String argumentName : request.getArgumentNames()) {
            sb.append(sep);
            sb.append(argumentName);
            sep = ",";
        }
        sb.append(") { return ");
        sb.append(source.getCharacters());
        sb.append(";}");
        String language = source.getLanguage() == null ? ID : source.getLanguage();
        Source decoratedSource = Source.newBuilder(language, sb.toString(), source.getName()).build();
        functions = SimpleLanguageParser.parseSL(this, decoratedSource);
    }
    RootCallTarget main = functions.get(SLStrings.MAIN);
    RootNode evalMain;
    if (main != null) {
        /*
             * We have a main function, so "evaluating" the parsed source means invoking that main
             * function. However, we need to lazily register functions into the SLContext first, so
             * we cannot use the original SLRootNode for the main function. Instead, we create a new
             * SLEvalRootNode that does everything we need.
             */
        evalMain = new SLEvalRootNode(this, main, functions);
    } else {
        /*
             * Even without a main function, "evaluating" the parsed source needs to register the
             * functions into the SLContext.
             */
        evalMain = new SLEvalRootNode(this, null, functions);
    }
    return evalMain.getCallTarget();
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) SLRootNode(com.oracle.truffle.sl.nodes.SLRootNode) SLUndefinedFunctionRootNode(com.oracle.truffle.sl.nodes.SLUndefinedFunctionRootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) TruffleString(com.oracle.truffle.api.strings.TruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Source(com.oracle.truffle.api.source.Source)

Example 7 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString 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
 * @param argumentIndex null or index of the argument the assignment is assigning
 * @return An SLExpressionNode for the given parameters. null if nameNode or valueNode is null.
 */
public SLExpressionNode createAssignment(SLExpressionNode nameNode, SLExpressionNode valueNode, Integer argumentIndex) {
    if (nameNode == null || valueNode == null) {
        return null;
    }
    TruffleString name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
    Integer frameSlot = lexicalScope.find(name);
    boolean newVariable = false;
    if (frameSlot == null) {
        frameSlot = frameDescriptorBuilder.addSlot(FrameSlotKind.Illegal, name, argumentIndex);
        lexicalScope.locals.put(name, frameSlot);
        newVariable = true;
    }
    final SLExpressionNode result = SLWriteLocalVariableNodeGen.create(valueNode, frameSlot, nameNode, newVariable);
    if (valueNode.hasSource()) {
        final int start = nameNode.getSourceCharIndex();
        final int length = valueNode.getSourceEndIndex() - start;
        result.setSourceSection(start, length);
    }
    if (argumentIndex == null) {
        result.addExpressionTag();
    }
    return result;
}
Also used : BigInteger(java.math.BigInteger) TruffleString(com.oracle.truffle.api.strings.TruffleString) SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLStringLiteralNode(com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)

Example 8 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString 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;
    }
    TruffleString name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
    final SLExpressionNode result;
    final Integer frameSlot = lexicalScope.find(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(name);
    }
    result.setSourceSection(nameNode.getSourceCharIndex(), nameNode.getSourceLength());
    result.addExpressionTag();
    return result;
}
Also used : BigInteger(java.math.BigInteger) TruffleString(com.oracle.truffle.api.strings.TruffleString) SLFunctionLiteralNode(com.oracle.truffle.sl.nodes.expression.SLFunctionLiteralNode) SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLStringLiteralNode(com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)

Example 9 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class SLStackTraceBuiltin method createStackTrace.

@TruffleBoundary
private static TruffleString createStackTrace() {
    final TruffleStringBuilder str = TruffleStringBuilder.create(SLLanguage.STRING_ENCODING);
    Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {

        // skip stack trace builtin
        private int skip = 1;

        @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.byteLength() > 0) {
                str.appendStringUncached(SLStrings.fromJavaString(System.getProperty("line.separator")));
            }
            str.appendStringUncached(FRAME);
            str.appendStringUncached(getRootNodeName(rn));
            FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
            int count = frameDescriptor.getNumberOfSlots();
            for (int i = 0; i < count; i++) {
                str.appendStringUncached(SEPARATOR);
                str.appendStringUncached((TruffleString) frameDescriptor.getSlotName(i));
                str.appendStringUncached(EQUALS);
                str.appendStringUncached(SLStrings.fromObject(frame.getValue(i)));
            }
            return null;
        }
    });
    return str.toStringUncached();
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) SLRootNode(com.oracle.truffle.sl.nodes.SLRootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Frame(com.oracle.truffle.api.frame.Frame) TruffleString(com.oracle.truffle.api.strings.TruffleString) RootCallTarget(com.oracle.truffle.api.RootCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 10 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringSwitchEncodingTest method testAll.

@Test
public void testAll() throws Exception {
    EnumSet<TruffleString.Encoding> reducedEncodingSet = EnumSet.allOf(TruffleString.Encoding.class);
    reducedEncodingSet.removeIf(e -> e.name().startsWith("IBM") || e.name().startsWith("Windows") || e.name().startsWith("ISO_8859_"));
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        if (encoding == TruffleString.Encoding.UTF8_SoftBank || encoding == TruffleString.Encoding.CP51932) {
            // https://github.com/jruby/jcodings/issues/42
            return;
        }
        for (TruffleString.Encoding targetEncoding : reducedEncodingSet) {
            TruffleString b = node.execute(a, targetEncoding);
            MutableTruffleString bMutable = nodeMutable.execute(a, targetEncoding);
            if (a instanceof TruffleString && (encoding == targetEncoding || !isDebugStrictEncodingChecks() && codeRange == TruffleString.CodeRange.ASCII && isAsciiCompatible(targetEncoding))) {
                Assert.assertSame(a, b);
            }
            if (a instanceof MutableTruffleString && encoding == targetEncoding) {
                Assert.assertSame(a, bMutable);
            }
            if (isUTF(encoding) && isUTF(targetEncoding) && isValid) {
                assertCodePointsEqual(b, targetEncoding, codepoints);
            }
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Aggregations

TruffleString (com.oracle.truffle.api.strings.TruffleString)39 Test (org.junit.Test)26 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)16 AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)9 RootCallTarget (com.oracle.truffle.api.RootCallTarget)3 SLExpressionNode (com.oracle.truffle.sl.nodes.SLExpressionNode)3 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)3 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)3 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)2 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)2 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)2 SLStringLiteralNode (com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)2 BigInteger (java.math.BigInteger)2 CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 Specialization (com.oracle.truffle.api.dsl.Specialization)1 Frame (com.oracle.truffle.api.frame.Frame)1 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)1