Skip to content

Commit

Permalink
[Truffle] Start to refactor local nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed May 10, 2015
1 parent 5409335 commit 999f19e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 166 deletions.
Expand Up @@ -19,10 +19,10 @@
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.globals.GetFromThreadLocalNode;
import org.jruby.truffle.nodes.globals.WrapInThreadLocalNode;
import org.jruby.truffle.nodes.locals.ReadAbstractFrameSlotNode;
import org.jruby.truffle.nodes.locals.ReadAbstractFrameSlotNodeGen;
import org.jruby.truffle.nodes.locals.WriteAbstractFrameSlotNode;
import org.jruby.truffle.nodes.locals.WriteAbstractFrameSlotNodeGen;
import org.jruby.truffle.nodes.locals.ReadFrameSlotNode;
import org.jruby.truffle.nodes.locals.ReadFrameSlotNodeGen;
import org.jruby.truffle.nodes.locals.WriteFrameSlotNode;
import org.jruby.truffle.nodes.locals.WriteFrameSlotNodeGen;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
Expand Down Expand Up @@ -84,7 +84,7 @@ public Object localVariableGetCached(RubyBinding binding, RubySymbol symbol,
@Cached("symbol") RubySymbol cachedSymbol,
@Cached("getFrameDescriptor(binding)") FrameDescriptor cachedFrameDescriptor,
@Cached("findFrameSlot(binding, symbol)") FrameSlot cachedFrameSlot,
@Cached("createReadNode(cachedFrameSlot)") ReadAbstractFrameSlotNode readLocalVariableNode) {
@Cached("createReadNode(cachedFrameSlot)") ReadFrameSlotNode readLocalVariableNode) {
if (cachedFrameSlot == null) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().nameErrorLocalVariableNotDefined(symbol.toString(), binding, this));
Expand Down Expand Up @@ -142,11 +142,11 @@ protected FrameSlot findFrameSlot(RubyBinding binding, RubySymbol symbol) {
return null;
}

protected ReadAbstractFrameSlotNode createReadNode(FrameSlot frameSlot) {
protected ReadFrameSlotNode createReadNode(FrameSlot frameSlot) {
if (frameSlot == null) {
return null;
} else {
return ReadAbstractFrameSlotNodeGen.create(frameSlot);
return ReadFrameSlotNodeGen.create(frameSlot);
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public LocalVariableSetNode(RubyContext context, SourceSection sourceSection) {
public Object localVariableSetCached(RubyBinding binding, RubySymbol symbol, Object value,
@Cached("symbol") RubySymbol cachedSymbol,
@Cached("getFrameDescriptor(binding)") FrameDescriptor cachedFrameDescriptor,
@Cached("createWriteNode(findFrameSlot(binding, symbol))") WriteAbstractFrameSlotNode writeLocalVariableNode) {
@Cached("createWriteNode(findFrameSlot(binding, symbol))") WriteFrameSlotNode writeLocalVariableNode) {
return writeLocalVariableNode.executeWrite(binding.getFrame(), value);
}

Expand Down Expand Up @@ -218,8 +218,8 @@ protected FrameSlot findFrameSlot(RubyBinding binding, RubySymbol symbol) {
return binding.getFrame().getFrameDescriptor().addFrameSlot(symbolString);
}

protected WriteAbstractFrameSlotNode createWriteNode(FrameSlot frameSlot) {
return WriteAbstractFrameSlotNodeGen.create(frameSlot);
protected WriteFrameSlotNode createWriteNode(FrameSlot frameSlot) {
return WriteFrameSlotNodeGen.create(frameSlot);
}

protected boolean isLastLine(RubySymbol symbol) {
Expand Down
Expand Up @@ -14,11 +14,11 @@
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.Node;

public abstract class ReadAbstractFrameSlotNode extends Node {
public abstract class ReadFrameSlotNode extends Node {

protected final FrameSlot frameSlot;

public ReadAbstractFrameSlotNode(FrameSlot slot) {
public ReadFrameSlotNode(FrameSlot slot) {
assert slot != null;
this.frameSlot = slot;
}
Expand Down Expand Up @@ -60,26 +60,14 @@ public final FrameSlot getFrameSlot() {
}

@Override
public ReadAbstractFrameSlotNode copy() {
return (ReadAbstractFrameSlotNode) super.copy();
public ReadFrameSlotNode copy() {
return (ReadFrameSlotNode) super.copy();
}

protected final void setBoolean(Frame frame, boolean value) {
frame.setBoolean(frameSlot, value);
}

protected final void setFixnum(Frame frame, int value) {
frame.setInt(frameSlot, value);
}

protected final void setLongFixnum(Frame frame, long value) {
frame.setLong(frameSlot, value);
}

protected final void setFloat(Frame frame, double value) {
frame.setDouble(frameSlot, value);
}

protected final void setObject(Frame frame, Object value) {
frame.setObject(frameSlot, value);
}
Expand Down Expand Up @@ -108,41 +96,4 @@ protected final Object getValue(Frame frame) {
return frame.getValue(frameSlot);
}

protected final boolean isBooleanKind(Frame frame) {
return isKind(FrameSlotKind.Boolean);
}

protected final boolean isFixnumKind(Frame frame) {
return isKind(FrameSlotKind.Int);
}

protected final boolean isLongFixnumKind(Frame frame) {
return isKind(FrameSlotKind.Long);
}

protected final boolean isFloatKind(Frame frame) {
return isKind(FrameSlotKind.Double);
}

protected final boolean isObjectKind(Frame frame) {
if (frameSlot.getKind() != FrameSlotKind.Object) {
CompilerDirectives.transferToInterpreter();
frameSlot.setKind(FrameSlotKind.Object);
}
return true;
}

private boolean isKind(FrameSlotKind kind) {
return frameSlot.getKind() == kind || initialSetKind(kind);
}

private boolean initialSetKind(FrameSlotKind kind) {
if (frameSlot.getKind() == FrameSlotKind.Illegal) {
CompilerDirectives.transferToInterpreter();
frameSlot.setKind(kind);
return true;
}
return false;
}

}
Expand Up @@ -23,54 +23,32 @@
import java.util.HashSet;
import java.util.Set;

public abstract class ReadLocalVariableNode extends FrameSlotNode implements ReadNode {
public class ReadLocalVariableNode extends RubyNode implements ReadNode {

public ReadLocalVariableNode(RubyContext context, SourceSection sourceSection, FrameSlot slot) {
super(context, sourceSection, slot);
}

@Specialization(rewriteOn = {FrameSlotTypeException.class})
public boolean doBoolean(VirtualFrame frame) throws FrameSlotTypeException {
return getBoolean(frame);
}

@Specialization(rewriteOn = {FrameSlotTypeException.class})
public int doFixnum(VirtualFrame frame) throws FrameSlotTypeException {
return getFixnum(frame);
}
@Child private ReadFrameSlotNode readFrameSlotNode;

@Specialization(rewriteOn = {FrameSlotTypeException.class})
public long doLongFixnum(VirtualFrame frame) throws FrameSlotTypeException {
return getLongFixnum(frame);
}

@Specialization(rewriteOn = {FrameSlotTypeException.class})
public double doFloat(VirtualFrame frame) throws FrameSlotTypeException {
return getFloat(frame);
}

@Specialization(rewriteOn = {FrameSlotTypeException.class})
public Object doObject(VirtualFrame frame) throws FrameSlotTypeException {
return getObject(frame);
public ReadLocalVariableNode(RubyContext context, SourceSection sourceSection, FrameSlot slot) {
super(context, sourceSection);
readFrameSlotNode = ReadFrameSlotNodeGen.create(slot);
}

@Specialization
public Object doValue(VirtualFrame frame) {
return getValue(frame);
@Override
public Object execute(VirtualFrame frame) {
return readFrameSlotNode.executeRead(frame);
}

@Override
public RubyNode makeWriteNode(RubyNode rhs) {
return WriteLocalVariableNodeGen.create(getContext(), getSourceSection(), frameSlot, rhs);
return new WriteLocalVariableNode(getContext(), getSourceSection(), rhs, readFrameSlotNode.getFrameSlot());
}

public static final Set<String> ALWAYS_DEFINED_GLOBALS = new HashSet<>(Arrays.asList("$~"));

@Override
public Object isDefined(VirtualFrame frame) {
// TODO(CS): copy and paste of ReadLevelVariableNode
if (BodyTranslator.FRAME_LOCAL_GLOBAL_VARIABLES.contains(frameSlot.getIdentifier())) {
if (ALWAYS_DEFINED_GLOBALS.contains(frameSlot.getIdentifier()) || doValue(frame) != nil()) {
if (BodyTranslator.FRAME_LOCAL_GLOBAL_VARIABLES.contains(readFrameSlotNode.getFrameSlot().getIdentifier())) {
if (ALWAYS_DEFINED_GLOBALS.contains(readFrameSlotNode.getFrameSlot().getIdentifier()) || readFrameSlotNode.executeRead(frame) != nil()) {
return getContext().makeString("global-variable");
} else {
return nil();
Expand Down
Expand Up @@ -14,11 +14,11 @@
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.Node;

public abstract class WriteAbstractFrameSlotNode extends Node {
public abstract class WriteFrameSlotNode extends Node {

protected final FrameSlot frameSlot;

public WriteAbstractFrameSlotNode(FrameSlot frameSlot) {
public WriteFrameSlotNode(FrameSlot frameSlot) {
assert frameSlot != null;
this.frameSlot = frameSlot;
}
Expand Down Expand Up @@ -79,22 +79,6 @@ protected final void setObject(Frame frame, Object value) {
frame.setObject(frameSlot, value);
}

protected final boolean getBoolean(Frame frame) throws FrameSlotTypeException {
return frame.getBoolean(frameSlot);
}

protected final int getFixnum(Frame frame) throws FrameSlotTypeException {
return frame.getInt(frameSlot);
}

protected final long getLongFixnum(Frame frame) throws FrameSlotTypeException {
return frame.getLong(frameSlot);
}

protected final double getFloat(Frame frame) throws FrameSlotTypeException {
return frame.getDouble(frameSlot);
}

protected final Object getObject(Frame frame) throws FrameSlotTypeException {
return frame.getObject(frameSlot);
}
Expand Down
Expand Up @@ -18,46 +18,25 @@
import org.jruby.truffle.translator.WriteNode;
import org.jruby.truffle.runtime.RubyContext;

@NodeChild(value = "rhs", type = RubyNode.class)
public abstract class WriteLocalVariableNode extends FrameSlotNode implements WriteNode {
public class WriteLocalVariableNode extends RubyNode implements WriteNode {

public WriteLocalVariableNode(RubyContext context, SourceSection sourceSection, FrameSlot frameSlot) {
super(context, sourceSection, frameSlot);
}

@Specialization(guards = "isBooleanKind(frame)")
public boolean doFixnum(VirtualFrame frame, boolean value) {
setBoolean(frame, value);
return value;
}

@Specialization(guards = "isFixnumKind(frame)")
public int doFixnum(VirtualFrame frame, int value) {
setFixnum(frame, value);
return value;
}

@Specialization(guards = "isLongFixnumKind(frame)")
public long doLongFixnum(VirtualFrame frame, long value) {
setLongFixnum(frame, value);
return value;
}
@Child private RubyNode valueNode;
@Child private WriteFrameSlotNode writeFrameSlotNode;

@Specialization(guards = "isFloatKind(frame)")
public double doFloat(VirtualFrame frame, double value) {
setFloat(frame, value);
return value;
public WriteLocalVariableNode(RubyContext context, SourceSection sourceSection, RubyNode valueNode, FrameSlot frameSlot) {
super(context, sourceSection);
this.valueNode = valueNode;
writeFrameSlotNode = WriteFrameSlotNodeGen.create(frameSlot);
}

@Specialization(guards = "isObjectKind(frame)")
public Object doObject(VirtualFrame frame, Object value) {
setObject(frame, value);
return value;
@Override
public Object execute(VirtualFrame frame) {
return writeFrameSlotNode.executeWrite(frame, valueNode.execute(frame));
}

@Override
public RubyNode makeReadNode() {
return ReadLocalVariableNodeGen.create(getContext(), getSourceSection(), frameSlot);
return new ReadLocalVariableNode(getContext(), getSourceSection(), writeFrameSlotNode.getFrameSlot());
}

@Override
Expand Down
Expand Up @@ -240,7 +240,7 @@ public RubyNode visitAttrAssignNodeExtraArgument(org.jruby.ast.AttrAssignNode no
argChildNodes.remove(argChildNodes.size() - 1);

// Evaluate the value and store it in a local variable
writeValue = WriteLocalVariableNodeGen.create(context, sourceSection, frameSlot, valueNode.accept(this));
writeValue = new WriteLocalVariableNode(context, sourceSection, valueNode.accept(this), frameSlot);

// Recreate the arguments array, reading that local instead of including the RHS for the last argument
argChildNodes.add(new ReadLocalDummyNode(node.getPosition(), sourceSection, frameSlot));
Expand All @@ -253,7 +253,7 @@ public RubyNode visitAttrAssignNodeExtraArgument(org.jruby.ast.AttrAssignNode no
final RubyNode valueNode = extraArgument;

// Evaluate the value and store it in a local variable
writeValue = WriteLocalVariableNodeGen.create(context, sourceSection, frameSlot, valueNode);
writeValue = new WriteLocalVariableNode(context, sourceSection, valueNode, frameSlot);

// Recreate the arguments array, reading that local instead of including the RHS for the last argument
final List<org.jruby.ast.Node> argChildNodes = new ArrayList<>();
Expand Down Expand Up @@ -305,7 +305,7 @@ public RubyNode visitAttrAssignNodeExtraArgument(org.jruby.ast.AttrAssignNode no
return SequenceNode.sequence(context, sourceSection,
writeValue,
actualCall,
ReadLocalVariableNodeGen.create(context, sourceSection, frameSlot));
new ReadLocalVariableNode(context, sourceSection, frameSlot));
}

@Override
Expand Down Expand Up @@ -2886,7 +2886,7 @@ protected String getIdentifier() {
public RubyNode visitOther(Node node) {
if (node instanceof ReadLocalDummyNode) {
final ReadLocalDummyNode readLocal = (ReadLocalDummyNode) node;
return ReadLocalVariableNodeGen.create(context, readLocal.getSourceSection(), readLocal.getFrameSlot());
return new ReadLocalVariableNode(context, readLocal.getSourceSection(), readLocal.getFrameSlot());
} else {
throw new UnsupportedOperationException();
}
Expand Down

0 comments on commit 999f19e

Please sign in to comment.