Skip to content

Commit

Permalink
Merge pull request #16 in G/truffle from ~MATTHIAS.G.GRIMMER_ORACLE.C…
Browse files Browse the repository at this point in the history
…OM/truffle:master to master

* commit 'feec4a84e5157b24c01329c341c69b389ecdd065':
  Factor language check into guard helper method in CachedObjectAccessNode
  Rename LanguageCheck annotation to CanResolve
  Use deep copy of can-resolve node of interop DSL
  Add tests for @LanguageCheck annotation
  Extend Interop DSL and provide an @LanguageCheck annotation, which allows expressing the language check as a node
  • Loading branch information
Matthias Grimmer committed Apr 21, 2016
2 parents d0afa11 + feec4a8 commit bb4a24b
Show file tree
Hide file tree
Showing 20 changed files with 697 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.oracle.truffle.api.dsl.test.interop;

import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
Expand Down Expand Up @@ -89,6 +90,14 @@ protected static int access(ExampleTruffleObject receiver,
}
}

@CanResolve
public abstract static class Check extends Node {

protected static boolean test(TruffleObject receiver) {
return receiver instanceof ExampleTruffleObject;
}
}

}
// END: com.oracle.truffle.api.dsl.test.interop.Snippets.ExampleTruffleObjectMR
//@formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public static boolean isInstance(TruffleObject obj) {

@Override
public ForeignAccess getForeignAccess() {
return ValidTruffleObject1MRForeign.ACCESS;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@
import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.TruffleObject;

public class ValidTruffleObject10 implements TruffleObject {
public class ValidTruffleObject11 implements TruffleObject {

public ForeignAccess getForeignAccess() {
return ValidTruffleObject10Foreign.ACCESS;
return ValidTruffleObject11MRForeign.ACCESS;
}

public static boolean isInstance(TruffleObject obj) {
return obj instanceof ValidTruffleObject10;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api.dsl.test.interop;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

@SuppressWarnings("unused")
@MessageResolution(receiverType = ValidTruffleObject11.class, language = TestTruffleLanguage.class)
public class ValidTruffleObject11MR {

@Resolve(message = "READ")
public abstract static class ReadNode11 extends Node {

protected Object access(VirtualFrame frame, ValidTruffleObject1 receiver, Object name) {
return 0;
}
}

@CanResolve
public abstract static class LanguageCheck1 extends Node {

protected boolean test(VirtualFrame frame, TruffleObject receiver) {
return receiver instanceof ValidTruffleObject11;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
*/
package com.oracle.truffle.api.dsl.test.interop;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.TruffleObject;

@SuppressWarnings("deprecation")
@com.oracle.truffle.api.interop.AcceptMessage(value = "WRITE", receiverType = ValidTruffleObject10.class, language = TestTruffleLanguage.class)
public final class AcceptMessageTest extends AcceptMessageTestBase {
public class ValidTruffleObject12 implements TruffleObject {

@Override
protected int access(VirtualFrame frame, Object receiver, Object name, Object value) {
return 0;
public ForeignAccess getForeignAccess() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api.dsl.test.interop;

import com.oracle.truffle.api.dsl.test.ExpectError;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

@SuppressWarnings("unused")
@MessageResolution(receiverType = ValidTruffleObject12.class, language = TestTruffleLanguage.class)
@ExpectError("Only one @LanguageCheck element allowed")
public class ValidTruffleObject12MR {

@Resolve(message = "READ")
public abstract static class ReadNode12 extends Node {

protected Object access(VirtualFrame frame, ValidTruffleObject1 receiver, Object name) {
return 0;
}
}

@CanResolve
public abstract static class LanguageCheck1 extends Node {

protected boolean test(VirtualFrame frame, TruffleObject receiver) {
return receiver instanceof ValidTruffleObject11;
}
}

@CanResolve
public abstract static class LanguageCheck2 extends Node {

protected boolean test(VirtualFrame frame, TruffleObject receiver) {
return receiver instanceof ValidTruffleObject11;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api.dsl.test.interop;

import com.oracle.truffle.api.dsl.test.ExpectError;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

@SuppressWarnings("unused")
@MessageResolution(receiverType = ValidTruffleObject12.class, language = TestTruffleLanguage.class)
public class ValidTruffleObject13MR {

@Resolve(message = "READ")
public abstract static class ReadNode13 extends Node {

protected Object access(VirtualFrame frame, ValidTruffleObject1 receiver, Object name) {
return 0;
}
}

@CanResolve
public abstract static class LanguageCheck1 extends Node {

@ExpectError("Method must return a boolean value")
protected Object test(VirtualFrame frame, TruffleObject receiver) {
return receiver instanceof ValidTruffleObject11;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api.dsl.test.interop;

import com.oracle.truffle.api.dsl.test.ExpectError;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

@SuppressWarnings("unused")
@MessageResolution(receiverType = ValidTruffleObject12.class, language = TestTruffleLanguage.class)
public class ValidTruffleObject14MR {

@Resolve(message = "READ")
public abstract static class ReadNode14 extends Node {

protected Object access(VirtualFrame frame, ValidTruffleObject1 receiver, Object name) {
return 0;
}
}

@CanResolve
public abstract static class LanguageCheck1 extends Node {

@ExpectError("The receiver type must be TruffleObject")
protected boolean test(VirtualFrame frame, ValidTruffleObject12 receiver) {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class ValidTruffleObject6 implements TruffleObject {

public ForeignAccess getForeignAccess() {
return ValidTruffleObject6MRForeign.ACCESS;
return null;
}

public static boolean isInstance(TruffleObject obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class ValidTruffleObject7 implements TruffleObject {

public ForeignAccess getForeignAccess() {
return ValidTruffleObject8MRForeign.ACCESS;
return null;
}

public static boolean isInstance(TruffleObject obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class ValidTruffleObject8 implements TruffleObject {

public ForeignAccess getForeignAccess() {
return ValidTruffleObject8MRForeign.ACCESS;
return null;
}

public static boolean isInstance(TruffleObject obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,50 @@
import com.oracle.truffle.api.nodes.DirectCallNode;

final class CachedObjectAccessNode extends ObjectAccessNode {
@Child private DirectCallNode callNode;
@Child private DirectCallNode callTarget;
@Child private ObjectAccessNode next;

@Child private DirectCallNode languageCheckAsNode;
private final ForeignAccess languageCheck;

@Child private ForeignAccessArguments accessArguments = new ForeignAccessArguments();

protected CachedObjectAccessNode(DirectCallNode callNode, ObjectAccessNode next, ForeignAccess languageCheck) {
this.callNode = callNode;
protected CachedObjectAccessNode(DirectCallNode callTarget, ObjectAccessNode next, ForeignAccess languageCheck, DirectCallNode languageCheckAsNode) {
this.callTarget = callTarget;
this.next = next;
this.languageCheck = languageCheck;
this.callNode.forceInlining();
}

protected CachedObjectAccessNode(CachedObjectAccessNode prev) {
this(prev.callNode, prev.next, prev.languageCheck);
this.languageCheckAsNode = languageCheckAsNode;
if (this.languageCheckAsNode != null) {
this.languageCheckAsNode.forceInlining();
}
this.callTarget.forceInlining();
}

@Override
public Object executeWith(VirtualFrame frame, TruffleObject receiver, Object[] arguments) {
if (languageCheck.canHandle(receiver)) {
return callNode.call(frame, accessArguments.executeCreate(receiver, arguments));
return doAccess(frame, receiver, arguments);
}

private Object doAccess(VirtualFrame frame, TruffleObject receiver, Object[] arguments) {
if (accept(frame, receiver)) {
return callTarget.call(frame, accessArguments.executeCreate(receiver, arguments));
} else {
return next.executeWith(frame, receiver, arguments);
return doNext(frame, receiver, arguments);
}
}

private boolean accept(VirtualFrame frame, TruffleObject receiver) {
if ((languageCheckAsNode != null && (boolean) languageCheckAsNode.call(frame, new Object[]{receiver}))) {
return true;
} else if (languageCheckAsNode == null && languageCheck.canHandle(receiver)) {
return true;
} else {
return false;
}
}

private Object doNext(VirtualFrame frame, TruffleObject receiver, Object[] arguments) {
return next.executeWith(frame, receiver, arguments);
}

}
Loading

0 comments on commit bb4a24b

Please sign in to comment.