Mercurial > hg > truffle
changeset 22379:fdf6ad720cdc
Implementing the multiplyCode TCK method in SL. The language tries to recognize non-global parse and in such case it executes the defined method directly.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 18 Nov 2015 12:32:47 +0100 |
parents | 06bdf4a43126 |
children | b4e89154a774 |
files | truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java |
diffstat | 2 files changed, 25 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Nov 18 11:53:28 2015 +0100 +++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Nov 18 12:32:47 2015 +0100 @@ -44,10 +44,8 @@ import com.oracle.truffle.api.vm.PolyglotEngine; import com.oracle.truffle.sl.test.instrument.InstrumentationTestMode; import com.oracle.truffle.tck.TruffleTCK; - +import org.junit.After; import static org.junit.Assert.assertTrue; - -import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -161,6 +159,13 @@ } @Override + protected String multiplyCode(String firstName, String secondName) { + return "function multiply(" + firstName + ", " + secondName + ") {\n" + + " return " + firstName + " * " + secondName + ";\n" + + "}\n"; + } + + @Override protected String countInvocations() { return "count"; }
--- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java Wed Nov 18 11:53:28 2015 +0100 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java Wed Nov 18 12:32:47 2015 +0100 @@ -57,12 +57,16 @@ import com.oracle.truffle.api.dsl.NodeFactory; import com.oracle.truffle.api.dsl.UnsupportedSpecializationException; import com.oracle.truffle.api.frame.MaterializedFrame; +import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.instrument.Visualizer; import com.oracle.truffle.api.instrument.WrapperNode; +import com.oracle.truffle.api.interop.ForeignAccess; +import com.oracle.truffle.api.interop.Message; import com.oracle.truffle.api.nodes.GraphPrintVisitor; import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.nodes.NodeUtil; +import com.oracle.truffle.api.nodes.RootNode; import com.oracle.truffle.api.source.Source; import com.oracle.truffle.api.source.SourceSection; import com.oracle.truffle.api.vm.PolyglotEngine; @@ -410,7 +414,7 @@ } @Override - protected CallTarget parse(Source code, Node node, String... argumentNames) throws IOException { + protected CallTarget parse(Source code, final Node node, String... argumentNames) throws IOException { final SLContext c = new SLContext(this); final Exception[] failed = {null}; try { @@ -419,10 +423,10 @@ } catch (Exception e) { failed[0] = e; } - return new CallTarget() { + RootNode rootNode = new RootNode(SLLanguage.class, null, null) { @TruffleBoundary @Override - public Object call(Object... arguments) { + public Object execute(VirtualFrame frame) { if (failed[0] instanceof RuntimeException) { throw (RuntimeException) failed[0]; } @@ -432,17 +436,26 @@ Node n = createFindContextNode(); SLContext fillIn = findContext(n); final SLFunctionRegistry functionRegistry = fillIn.getFunctionRegistry(); + int oneAndCnt = 0; + SLFunction oneAndOnly = null; for (SLFunction f : c.getFunctionRegistry().getFunctions()) { RootCallTarget callTarget = f.getCallTarget(); if (callTarget == null) { continue; } - functionRegistry.lookup(f.getName()); + oneAndOnly = functionRegistry.lookup(f.getName()); + oneAndCnt++; functionRegistry.register(f.getName(), (SLRootNode) f.getCallTarget().getRootNode()); } + Object[] arguments = frame.getArguments(); + if (oneAndCnt == 1 && (arguments.length > 0 || node != null)) { + Node callNode = Message.createExecute(arguments.length).createNode(); + return ForeignAccess.execute(callNode, frame, oneAndOnly, arguments); + } return null; } }; + return Truffle.getRuntime().createCallTarget(rootNode); } @Override