Skip to content

Commit 42d8c43

Browse files
author
Andreas Woess
committed
[Truffle] Adopt OM API.
1 parent 5460e11 commit 42d8c43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+706
-1074
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/ClassNodes.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public abstract static class NewNode extends CoreMethodNode {
5353

5454
@Child protected AllocateNode allocateNode;
5555
@Child protected DispatchHeadNode initialize;
56+
@CompilerDirectives.CompilationFinal private boolean isCached = true;
57+
@CompilerDirectives.CompilationFinal private RubyClass cachedClass;
5658

5759
public NewNode(RubyContext context, SourceSection sourceSection) {
5860
super(context, sourceSection);
@@ -82,6 +84,23 @@ private RubyBasicObject doNewInstance(VirtualFrame frame, RubyClass rubyClass, O
8284
return instance;
8385
}
8486

87+
private RubyClass cachedClass(RubyClass rubyClass) {
88+
if (isCached) {
89+
if (cachedClass == null) {
90+
CompilerDirectives.transferToInterpreterAndInvalidate();
91+
cachedClass = rubyClass;
92+
}
93+
94+
if (rubyClass == cachedClass) {
95+
return cachedClass;
96+
} else {
97+
CompilerDirectives.transferToInterpreterAndInvalidate();
98+
isCached = false;
99+
cachedClass = null;
100+
}
101+
}
102+
return rubyClass;
103+
}
85104
}
86105

87106
@CoreMethod(names = "class_variables")

core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public Object clone(VirtualFrame frame, RubyBasicObject self) {
421421
newObject.getSingletonClass(this).initCopy(self.getMetaClass());
422422
}
423423

424-
newObject.setInstanceVariables(self.getFields());
424+
newObject.setInstanceVariables(self.getInstanceVariables());
425425
initializeCloneNode.call(frame, newObject, "initialize_clone", null, self);
426426

427427
return newObject;
@@ -450,8 +450,7 @@ public Object dup(VirtualFrame frame, RubyBasicObject self) {
450450
// This method is pretty crappy for compilation - it should improve with the OM
451451

452452
final RubyBasicObject newObject = self.getLogicalClass().newInstance(this);
453-
454-
newObject.setInstanceVariables(self.getFields());
453+
newObject.setInstanceVariables(self.getInstanceVariables());
455454
initializeDupNode.call(frame, newObject, "initialize_dup", null, self);
456455

457456
return newObject;

core/src/main/java/org/jruby/truffle/nodes/objects/ReadInstanceVariableNode.java

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,20 @@
99
*/
1010
package org.jruby.truffle.nodes.objects;
1111

12-
import com.oracle.truffle.api.*;
12+
import com.oracle.truffle.api.CompilerDirectives;
13+
import com.oracle.truffle.api.object.Property;
14+
import com.oracle.truffle.api.object.Shape;
1315
import com.oracle.truffle.api.source.*;
1416
import com.oracle.truffle.api.frame.*;
1517
import com.oracle.truffle.api.nodes.UnexpectedResultException;
1618
import com.oracle.truffle.api.utilities.BranchProfile;
1719
import org.jruby.truffle.nodes.*;
1820
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
19-
import org.jruby.truffle.nodes.objectstorage.RespecializeHook;
2021
import org.jruby.truffle.runtime.*;
2122
import org.jruby.truffle.runtime.core.RubyBasicObject;
22-
import org.jruby.truffle.runtime.objectstorage.*;
2323

2424
public class ReadInstanceVariableNode extends RubyNode implements ReadNode {
2525

26-
private final RespecializeHook hook = new RespecializeHook() {
27-
28-
@Override
29-
public void hookRead(ObjectStorage object, String name) {
30-
final RubyBasicObject rubyObject = (RubyBasicObject) object;
31-
32-
if (!rubyObject.hasPrivateLayout()) {
33-
rubyObject.updateLayoutToMatchClass();
34-
}
35-
}
36-
37-
@Override
38-
public void hookWrite(ObjectStorage object, String name, Object value) {
39-
final RubyBasicObject rubyObject = (RubyBasicObject) object;
40-
41-
if (!rubyObject.hasPrivateLayout()) {
42-
rubyObject.updateLayoutToMatchClass();
43-
}
44-
45-
rubyObject.setInstanceVariable(name, value);
46-
}
47-
48-
};
49-
5026
@Child protected RubyNode receiver;
5127
@Child protected ReadHeadObjectFieldNode readNode;
5228
private final boolean isGlobal;
@@ -57,7 +33,7 @@ public void hookWrite(ObjectStorage object, String name, Object value) {
5733
public ReadInstanceVariableNode(RubyContext context, SourceSection sourceSection, String name, RubyNode receiver, boolean isGlobal) {
5834
super(context, sourceSection);
5935
this.receiver = receiver;
60-
readNode = new ReadHeadObjectFieldNode(name, hook);
36+
readNode = new ReadHeadObjectFieldNode(name);
6137
this.isGlobal = isGlobal;
6238
}
6339

@@ -142,10 +118,10 @@ public Object isDefined(VirtualFrame frame) {
142118
if (receiverObject instanceof RubyBasicObject) {
143119
final RubyBasicObject receiverRubyObject = (RubyBasicObject) receiverObject;
144120

145-
final ObjectLayout layout = receiverRubyObject.getObjectLayout();
146-
final StorageLocation storageLocation = layout.findStorageLocation(readNode.getName());
121+
final Shape layout = receiverRubyObject.getDynamicObject().getShape();
122+
final Property storageLocation = layout.getProperty(readNode.getName());
147123

148-
if (storageLocation.isSet(receiverRubyObject)) {
124+
if (storageLocation != null) {
149125
return context.makeString("instance-variable");
150126
} else {
151127
return getContext().getCoreLibrary().getNilObject();

core/src/main/java/org/jruby/truffle/nodes/objects/WriteInstanceVariableNode.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
package org.jruby.truffle.nodes.objects;
1111

12-
import com.oracle.truffle.api.*;
12+
import com.oracle.truffle.api.CompilerDirectives;
1313
import com.oracle.truffle.api.source.*;
1414
import com.oracle.truffle.api.frame.VirtualFrame;
1515
import com.oracle.truffle.api.nodes.UnexpectedResultException;
@@ -18,35 +18,9 @@
1818
import org.jruby.truffle.runtime.*;
1919
import org.jruby.truffle.runtime.control.RaiseException;
2020
import org.jruby.truffle.runtime.core.RubyBasicObject;
21-
import org.jruby.truffle.runtime.core.RubyMatchData;
22-
import org.jruby.truffle.runtime.objectstorage.*;
2321

2422
public class WriteInstanceVariableNode extends RubyNode implements WriteNode {
2523

26-
private final RespecializeHook hook = new RespecializeHook() {
27-
28-
@Override
29-
public void hookRead(ObjectStorage object, String name) {
30-
final RubyBasicObject rubyObject = (RubyBasicObject) object;
31-
32-
if (!rubyObject.hasPrivateLayout()) {
33-
rubyObject.updateLayoutToMatchClass();
34-
}
35-
}
36-
37-
@Override
38-
public void hookWrite(ObjectStorage object, String name, Object value) {
39-
final RubyBasicObject rubyObject = (RubyBasicObject) object;
40-
41-
if (!rubyObject.hasPrivateLayout()) {
42-
rubyObject.updateLayoutToMatchClass();
43-
}
44-
45-
rubyObject.setInstanceVariable(name, value);
46-
}
47-
48-
};
49-
5024
@Child protected RubyNode receiver;
5125
@Child protected RubyNode rhs;
5226
@Child protected WriteHeadObjectFieldNode writeNode;
@@ -56,7 +30,7 @@ public WriteInstanceVariableNode(RubyContext context, SourceSection sourceSectio
5630
super(context, sourceSection);
5731
this.receiver = receiver;
5832
this.rhs = rhs;
59-
writeNode = new WriteHeadObjectFieldNode(name, hook);
33+
writeNode = new WriteHeadObjectFieldNode(name);
6034
this.isGlobal = isGlobal;
6135
}
6236

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 1.0
7+
* GNU General Public License version 2
8+
* GNU Lesser General Public License version 2.1
9+
*/
10+
package org.jruby.truffle.nodes.objectstorage;
11+
12+
import com.oracle.truffle.api.object.Shape;
13+
import org.jruby.truffle.runtime.core.RubyBasicObject;
14+
15+
public class MigrateNode extends WriteObjectFieldChainNode {
16+
17+
private final Shape expectedShape;
18+
19+
public MigrateNode(Shape expectedShape, WriteObjectFieldNode next) {
20+
super(next);
21+
this.expectedShape = expectedShape;
22+
}
23+
24+
@Override
25+
public void execute(RubyBasicObject object, Object value) {
26+
if (object.getObjectLayout() == expectedShape) {
27+
object.getDynamicObject().updateShape();
28+
}
29+
30+
next.execute(object, value);
31+
}
32+
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 1.0
7+
* GNU General Public License version 2
8+
* GNU Lesser General Public License version 2.1
9+
*/
10+
package org.jruby.truffle.nodes.objectstorage;
11+
12+
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
13+
import com.oracle.truffle.api.nodes.NodeCost;
14+
import com.oracle.truffle.api.nodes.NodeInfo;
15+
import com.oracle.truffle.api.nodes.UnexpectedResultException;
16+
import com.oracle.truffle.api.object.BooleanLocation;
17+
import com.oracle.truffle.api.object.Shape;
18+
import org.jruby.truffle.runtime.core.RubyBasicObject;
19+
20+
@NodeInfo(cost = NodeCost.POLYMORPHIC)
21+
public class ReadBooleanObjectFieldNode extends ReadObjectFieldChainNode {
22+
23+
private final Shape objectLayout;
24+
private final BooleanLocation storageLocation;
25+
26+
public ReadBooleanObjectFieldNode(Shape objectLayout, BooleanLocation storageLocation, ReadObjectFieldNode next) {
27+
super(next);
28+
this.objectLayout = objectLayout;
29+
this.storageLocation = storageLocation;
30+
}
31+
32+
@Override
33+
public boolean executeBoolean(RubyBasicObject object) throws UnexpectedResultException {
34+
try {
35+
objectLayout.getValidAssumption().check();
36+
} catch (InvalidAssumptionException e) {
37+
replace(next);
38+
return next.executeBoolean(object);
39+
}
40+
41+
final boolean condition = object.getObjectLayout() == objectLayout;
42+
43+
if (condition) {
44+
return storageLocation.getBoolean(object.getDynamicObject(), objectLayout);
45+
} else {
46+
return next.executeBoolean(object);
47+
}
48+
}
49+
50+
@Override
51+
public Object execute(RubyBasicObject object) {
52+
try {
53+
objectLayout.getValidAssumption().check();
54+
} catch (InvalidAssumptionException e) {
55+
replace(next);
56+
return next.execute(object);
57+
}
58+
59+
final boolean condition = object.getObjectLayout() == objectLayout;
60+
61+
if (condition) {
62+
return storageLocation.get(object.getDynamicObject(), objectLayout);
63+
} else {
64+
return next.execute(object);
65+
}
66+
}
67+
68+
}

core/src/main/java/org/jruby/truffle/nodes/objectstorage/ReadDoubleObjectFieldNode.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,57 @@
99
*/
1010
package org.jruby.truffle.nodes.objectstorage;
1111

12+
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
1213
import com.oracle.truffle.api.nodes.NodeCost;
1314
import com.oracle.truffle.api.nodes.NodeInfo;
1415
import com.oracle.truffle.api.nodes.UnexpectedResultException;
15-
import org.jruby.truffle.runtime.objectstorage.DoubleStorageLocation;
16-
import org.jruby.truffle.runtime.objectstorage.ObjectLayout;
17-
import org.jruby.truffle.runtime.objectstorage.ObjectStorage;
16+
import com.oracle.truffle.api.object.DoubleLocation;
17+
import com.oracle.truffle.api.object.Shape;
18+
import org.jruby.truffle.runtime.core.RubyBasicObject;
1819

1920
@NodeInfo(cost = NodeCost.POLYMORPHIC)
2021
public class ReadDoubleObjectFieldNode extends ReadObjectFieldChainNode {
2122

22-
private final ObjectLayout objectLayout;
23-
private final DoubleStorageLocation storageLocation;
23+
private final Shape objectLayout;
24+
private final DoubleLocation storageLocation;
2425

25-
public ReadDoubleObjectFieldNode(ObjectLayout objectLayout, DoubleStorageLocation storageLocation, ReadObjectFieldNode next) {
26+
public ReadDoubleObjectFieldNode(Shape objectLayout, DoubleLocation storageLocation, ReadObjectFieldNode next) {
2627
super(next);
2728
this.objectLayout = objectLayout;
2829
this.storageLocation = storageLocation;
2930
}
3031

3132
@Override
32-
public double executeDouble(ObjectStorage object) throws UnexpectedResultException {
33+
public double executeDouble(RubyBasicObject object) throws UnexpectedResultException {
34+
try {
35+
objectLayout.getValidAssumption().check();
36+
} catch (InvalidAssumptionException e) {
37+
replace(next);
38+
return next.executeDouble(object);
39+
}
40+
3341
final boolean condition = object.getObjectLayout() == objectLayout;
3442

3543
if (condition) {
36-
return storageLocation.readDouble(object, condition);
44+
return storageLocation.getDouble(object.getDynamicObject(), objectLayout);
3745
} else {
3846
return next.executeDouble(object);
3947
}
4048
}
4149

4250
@Override
43-
public Object execute(ObjectStorage object) {
51+
public Object execute(RubyBasicObject object) {
52+
try {
53+
objectLayout.getValidAssumption().check();
54+
} catch (InvalidAssumptionException e) {
55+
replace(next);
56+
return next.execute(object);
57+
}
58+
4459
final boolean condition = object.getObjectLayout() == objectLayout;
4560

4661
if (condition) {
47-
return storageLocation.read(object, condition);
62+
return storageLocation.get(object.getDynamicObject(), objectLayout);
4863
} else {
4964
return next.execute(object);
5065
}

core/src/main/java/org/jruby/truffle/nodes/objectstorage/ReadHeadObjectFieldNode.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@
1111

1212
import com.oracle.truffle.api.nodes.Node;
1313
import com.oracle.truffle.api.nodes.UnexpectedResultException;
14-
import org.jruby.truffle.runtime.objectstorage.ObjectStorage;
14+
import org.jruby.truffle.runtime.core.RubyBasicObject;
1515

1616
public class ReadHeadObjectFieldNode extends Node {
1717

1818
private final String name;
1919
@Child protected ReadObjectFieldNode first;
2020

21-
public ReadHeadObjectFieldNode(String name, RespecializeHook hook) {
21+
public ReadHeadObjectFieldNode(String name) {
2222
this.name = name;
23-
first = new UninitializedReadObjectFieldNode(name, hook);
23+
first = new UninitializedReadObjectFieldNode(name);
2424
}
2525

26-
public int executeInteger(ObjectStorage object) throws UnexpectedResultException {
26+
public int executeInteger(RubyBasicObject object) throws UnexpectedResultException {
2727
return first.executeInteger(object);
2828
}
2929

30-
public long executeLong(ObjectStorage object) throws UnexpectedResultException {
30+
public long executeLong(RubyBasicObject object) throws UnexpectedResultException {
3131
return first.executeLong(object);
3232
}
3333

34-
public double executeDouble(ObjectStorage object) throws UnexpectedResultException {
34+
public double executeDouble(RubyBasicObject object) throws UnexpectedResultException {
3535
return first.executeDouble(object);
3636
}
3737

38-
public Object execute(ObjectStorage object) {
38+
public Object execute(RubyBasicObject object) {
3939
return first.execute(object);
4040
}
4141

4242
public String getName() {
4343
return name;
4444
}
4545

46-
public boolean isSet(ObjectStorage object) {
46+
public boolean isSet(RubyBasicObject object) {
4747
return first.isSet(object);
4848
}
4949

0 commit comments

Comments
 (0)