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++;
}
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;
}
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;
}
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;
}
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));
}
Aggregations