Search in sources :

Example 6 with SLExpressionNode

use of com.oracle.truffle.sl.nodes.SLExpressionNode in project graal by oracle.

the class SLNodeFactory method addFormalParameter.

public void addFormalParameter(Token nameToken) {
    /*
         * Method parameters are assigned to local variables at the beginning of the method. This
         * ensures that accesses to parameters are specialized the same way as local variables are
         * specialized.
         */
    final SLReadArgumentNode readArg = new SLReadArgumentNode(parameterCount);
    SLExpressionNode assignment = createAssignment(createStringLiteral(nameToken, false), readArg);
    methodNodes.add(assignment);
    parameterCount++;
}
Also used : SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLReadArgumentNode(com.oracle.truffle.sl.nodes.local.SLReadArgumentNode)

Example 7 with SLExpressionNode

use of com.oracle.truffle.sl.nodes.SLExpressionNode 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 8 with SLExpressionNode

use of com.oracle.truffle.sl.nodes.SLExpressionNode 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 9 with SLExpressionNode

use of com.oracle.truffle.sl.nodes.SLExpressionNode in project graal by oracle.

the class SLNodeFactory method createReadProperty.

/**
 * Returns an {@link SLReadPropertyNode} for the given parameters.
 *
 * @param receiverNode The receiver of the property access
 * @param nameNode The name of the property being accessed
 * @return An SLExpressionNode for the given parameters. null if receiverNode or nameNode is
 *         null.
 */
public SLExpressionNode createReadProperty(SLExpressionNode receiverNode, SLExpressionNode nameNode) {
    if (receiverNode == null || nameNode == null) {
        return null;
    }
    final SLExpressionNode result = SLReadPropertyNodeGen.create(receiverNode, nameNode);
    final int startPos = receiverNode.getSourceCharIndex();
    final int endPos = nameNode.getSourceEndIndex();
    result.setSourceSection(startPos, endPos - startPos);
    return result;
}
Also used : SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode)

Example 10 with SLExpressionNode

use of com.oracle.truffle.sl.nodes.SLExpressionNode in project graal by oracle.

the class SLContext method installBuiltin.

public void installBuiltin(NodeFactory<? extends SLBuiltinNode> factory) {
    /*
         * The builtin node factory is a class that is automatically generated by the Truffle DSL.
         * The signature returned by the factory reflects the signature of the @Specialization
         *
         * methods in the builtin classes.
         */
    int argumentCount = factory.getExecutionSignature().size();
    SLExpressionNode[] argumentNodes = new SLExpressionNode[argumentCount];
    /*
         * Builtin functions are like normal functions, i.e., the arguments are passed in as an
         * Object[] array encapsulated in SLArguments. A SLReadArgumentNode extracts a parameter
         * from this array.
         */
    for (int i = 0; i < argumentCount; i++) {
        argumentNodes[i] = new SLReadArgumentNode(i);
    }
    /* Instantiate the builtin node. This node performs the actual functionality. */
    SLBuiltinNode builtinBodyNode = factory.createNode((Object) argumentNodes);
    builtinBodyNode.addRootTag();
    /* The name of the builtin function is specified via an annotation on the node class. */
    String name = lookupNodeInfo(builtinBodyNode.getClass()).shortName();
    builtinBodyNode.setUnavailableSourceSection();
    /* Wrap the builtin in a RootNode. Truffle requires all AST to start with a RootNode. */
    SLRootNode rootNode = new SLRootNode(language, new FrameDescriptor(), builtinBodyNode, BUILTIN_SOURCE.createUnavailableSection(), name);
    /* Register the builtin function in our function registry. */
    getFunctionRegistry().register(name, Truffle.getRuntime().createCallTarget(rootNode));
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLReadArgumentNode(com.oracle.truffle.sl.nodes.local.SLReadArgumentNode) SLRootNode(com.oracle.truffle.sl.nodes.SLRootNode) SLBuiltinNode(com.oracle.truffle.sl.builtins.SLBuiltinNode)

Aggregations

SLExpressionNode (com.oracle.truffle.sl.nodes.SLExpressionNode)19 SLStatementNode (com.oracle.truffle.sl.nodes.SLStatementNode)3 FrameSlot (com.oracle.truffle.api.frame.FrameSlot)2 SLStringLiteralNode (com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)2 SLReadArgumentNode (com.oracle.truffle.sl.nodes.local.SLReadArgumentNode)2 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)1 SLBuiltinNode (com.oracle.truffle.sl.builtins.SLBuiltinNode)1 SLBinaryNode (com.oracle.truffle.sl.nodes.SLBinaryNode)1 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)1 SLInvokeNode (com.oracle.truffle.sl.nodes.call.SLInvokeNode)1 SLBigIntegerLiteralNode (com.oracle.truffle.sl.nodes.expression.SLBigIntegerLiteralNode)1 SLFunctionLiteralNode (com.oracle.truffle.sl.nodes.expression.SLFunctionLiteralNode)1 SLLogicalAndNode (com.oracle.truffle.sl.nodes.expression.SLLogicalAndNode)1 SLLogicalOrNode (com.oracle.truffle.sl.nodes.expression.SLLogicalOrNode)1 SLLongLiteralNode (com.oracle.truffle.sl.nodes.expression.SLLongLiteralNode)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1