Skip to content

Commit

Permalink
Merge pull request #2422 from thomaswue/master
Browse files Browse the repository at this point in the history
Truffle-related clean up and simplifications.
  • Loading branch information
chrisseaton committed Jan 4, 2015
2 parents 6327ad1 + 2db47b9 commit e0ea4e8
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 107 deletions.
Expand Up @@ -37,8 +37,7 @@ public AndNode(RubyContext context, SourceSection sourceSection, RubyNode left,
@Override
public Object execute(VirtualFrame frame) {
final Object leftValue = left.execute(frame);
boolean leftBoolean = leftCast.executeBoolean(frame, leftValue);
if (conditionProfile.profile(leftBoolean)) {
if (conditionProfile.profile(leftCast.executeBoolean(frame, leftValue))) {
// Right expression evaluated and returned if left expression returns true.
return right.execute(frame);
} else {
Expand Down
32 changes: 4 additions & 28 deletions core/src/main/java/org/jruby/truffle/nodes/control/IfNode.java
Expand Up @@ -9,10 +9,10 @@
*/
package org.jruby.truffle.nodes.control;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import com.oracle.truffle.api.utilities.ConditionProfile;

import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.cast.BooleanCastNode;
import org.jruby.truffle.runtime.RubyContext;
Expand All @@ -26,12 +26,7 @@ public class IfNode extends RubyNode {
@Child protected BooleanCastNode condition;
@Child protected RubyNode thenBody;
@Child protected RubyNode elseBody;

private final BranchProfile thenProfile = BranchProfile.create();
private final BranchProfile elseProfile = BranchProfile.create();

@CompilerDirectives.CompilationFinal private int thenCount;
@CompilerDirectives.CompilationFinal private int elseCount;
private final ConditionProfile conditionProfile = ConditionProfile.createCountingProfile();

public IfNode(RubyContext context, SourceSection sourceSection, BooleanCastNode condition, RubyNode thenBody, RubyNode elseBody) {
super(context, sourceSection);
Expand All @@ -47,29 +42,10 @@ public IfNode(RubyContext context, SourceSection sourceSection, BooleanCastNode

@Override
public Object execute(VirtualFrame frame) {
if (CompilerDirectives.injectBranchProbability(getBranchProbability(), condition.executeBoolean(frame))) {
if (CompilerDirectives.inInterpreter()) {
thenCount++;
}
thenProfile.enter();
if (conditionProfile.profile(condition.executeBoolean(frame))) {
return thenBody.execute(frame);
} else {
if (CompilerDirectives.inInterpreter()) {
elseCount++;
}
elseProfile.enter();
return elseBody.execute(frame);
}
}

private double getBranchProbability() {
final int totalCount = thenCount + elseCount;

if (totalCount == 0) {
return 0;
} else {
return (double) thenCount / (double) (thenCount + elseCount);
}
}

}
11 changes: 6 additions & 5 deletions core/src/main/java/org/jruby/truffle/nodes/control/OrNode.java
Expand Up @@ -9,6 +9,7 @@
*/
package org.jruby.truffle.nodes.control;

import com.oracle.truffle.api.utilities.ConditionProfile;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
Expand All @@ -24,6 +25,8 @@ public class OrNode extends RubyNode {
@Child protected RubyNode left;
@Child protected BooleanCastNode leftCast;
@Child protected RubyNode right;
private final ConditionProfile conditionProfile = ConditionProfile.createCountingProfile();


public OrNode(RubyContext context, SourceSection sourceSection, RubyNode left, RubyNode right) {
super(context, sourceSection);
Expand All @@ -35,12 +38,10 @@ public OrNode(RubyContext context, SourceSection sourceSection, RubyNode left, R
@Override
public Object execute(VirtualFrame frame) {
final Object leftValue = left.execute(frame);

if (leftCast.executeBoolean(frame, leftValue)) {
if (conditionProfile.profile(leftCast.executeBoolean(frame, leftValue))) {
return leftValue;
} else {
return right.execute(frame);
}

return right.execute(frame);
}

}
Expand Up @@ -62,7 +62,7 @@ public RubyArray geHeadLongFixnum(RubyArray array) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
} else {
final int size = array.getSize() - index;
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((long[]) array.getStore(), 0, array.getSize() - index), array.getSize() - index);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((long[]) array.getStore(), 0, size), size);
}
}

Expand All @@ -74,7 +74,7 @@ public RubyArray getHeadFloat(RubyArray array) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
} else {
final int size = array.getSize() - index;
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((double[]) array.getStore(), 0, array.getSize() - index), array.getSize() - index);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((double[]) array.getStore(), 0, size), size);
}
}

Expand All @@ -86,7 +86,7 @@ public RubyArray getHeadObject(RubyArray array) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
} else {
final int size = array.getSize() - index;
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((Object[]) array.getStore(), 0, array.getSize() - index), array.getSize() - index);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((Object[]) array.getStore(), 0, size), size);
}
}

Expand Down
18 changes: 0 additions & 18 deletions core/src/main/java/org/jruby/truffle/nodes/core/ClassNodes.java
Expand Up @@ -86,24 +86,6 @@ private RubyBasicObject doNewInstance(VirtualFrame frame, RubyClass rubyClass, O
initialize.call(frame, instance, "initialize", block, args);
return instance;
}

private RubyClass cachedClass(RubyClass rubyClass) {
if (isCached) {
if (cachedClass == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
cachedClass = rubyClass;
}

if (rubyClass == cachedClass) {
return cachedClass;
} else {
CompilerDirectives.transferToInterpreterAndInvalidate();
isCached = false;
cachedClass = null;
}
}
return rubyClass;
}
}

@CoreMethod(names = "initialize", optional = 1, needsBlock = true)
Expand Down
Expand Up @@ -140,7 +140,6 @@ public RubyArray searchConvpath(RubyString source, RubyString destination) {
EncodingUtils.econvArgs(context, new IRubyObject[]{getContext().toJRuby(source), getContext().toJRuby(destination)}, encNames, encs, ecflags_p, ecopts_p);

TranscoderDB.searchPath(encNames[0], encNames[1], new TranscoderDB.SearchPathCallback() {
EncodingService es = runtime.getEncodingService();

public void call(byte[] source, byte[] destination, int depth) {
Object v;
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/jruby/truffle/nodes/core/IONodes.java
Expand Up @@ -75,9 +75,8 @@ public RubyArray readLines(RubyString file) {

final List<Object> lines = new ArrayList<>();

try {
final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.toString())));

try(final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.toString())))) {

while (true) {
final String line = reader.readLine();

Expand Down
Expand Up @@ -28,7 +28,7 @@

public abstract class CachedUnboxedDispatchNode extends CachedDispatchNode {

private final Class expectedClass;
private final Class<?> expectedClass;
private final Assumption unmodifiedAssumption;

private final Object value;
Expand All @@ -38,7 +38,7 @@ public abstract class CachedUnboxedDispatchNode extends CachedDispatchNode {
@Child protected IndirectCallNode indirectCallNode;

public CachedUnboxedDispatchNode(RubyContext context, Object cachedName, DispatchNode next,
Class expectedClass, Assumption unmodifiedAssumption, Object value,
Class<?> expectedClass, Assumption unmodifiedAssumption, Object value,
RubyMethod method, boolean indirect) {
super(context, cachedName, next, indirect);
this.expectedClass = expectedClass;
Expand Down
Expand Up @@ -13,4 +13,6 @@

public class UseMethodMissingException extends SlowPathException {

private static final long serialVersionUID = -4534089746905620428L;

}
Expand Up @@ -27,7 +27,7 @@ public GetFromThreadLocalNode(GetFromThreadLocalNode prev) {
}

@Specialization
public Object get(ThreadLocal threadLocal) {
public Object get(ThreadLocal<?> threadLocal) {
return threadLocal.get();
}

Expand All @@ -38,7 +38,7 @@ public Object get(Object value) {

public static Object get(RubyContext context, Object value) {
if (value instanceof ThreadLocal) {
return ((ThreadLocal) value).get();
return ((ThreadLocal<?>) value).get();
} else {
return value;
}
Expand Down
Expand Up @@ -27,14 +27,14 @@ public WrapInThreadLocalNode(WrapInThreadLocalNode prev) {
}

@Specialization
public ThreadLocal wrap(Object value) {
public ThreadLocal<?> wrap(Object value) {
return wrap(getContext(), value);
}

public static ThreadLocal wrap(RubyContext context, Object value) {
public static ThreadLocal<Object> wrap(RubyContext context, Object value) {
final RubyContext finalContext = context;

final ThreadLocal threadLocal = new ThreadLocal() {
final ThreadLocal<Object> threadLocal = new ThreadLocal<Object>() {

@Override
protected Object initialValue() {
Expand Down
Expand Up @@ -39,7 +39,7 @@ public Object dispatchWithModifiedBlock(VirtualFrame frame, RubyProc block, Ruby
}

@Override
public Object dispatchWithModifiedSelf(VirtualFrame frame, RubyProc block, Object self, Object[] argumentsObjects) {
public Object dispatchWithModifiedSelf(VirtualFrame frame, RubyProc block, Object self, Object... argumentsObjects) {
return callNode.call(frame, block.getCallTarget(), RubyArguments.pack(block, block.getDeclarationFrame(), self, block.getBlockCapturedInScope(), argumentsObjects));
}

Expand Down
Expand Up @@ -64,7 +64,7 @@ public Object dispatchWithModifiedBlock(VirtualFrame frame, RubyProc block, Ruby
}

@Override
public Object dispatchWithModifiedSelf(VirtualFrame frame, RubyProc block, Object self, Object[] argumentsObjects) {
public Object dispatchWithModifiedSelf(VirtualFrame frame, RubyProc block, Object self, Object... argumentsObjects) {
CompilerDirectives.transferToInterpreterAndInvalidate();

depth++;
Expand Down
Expand Up @@ -88,7 +88,7 @@ public RubyContext(Ruby runtime) {
safepointManager = new SafepointManager(this);

this.runtime = runtime;
translator = new TranslatorDriver(this);
translator = new TranslatorDriver();

warnings = new Warnings(this);

Expand Down
Expand Up @@ -11,6 +11,8 @@

public class TruffleFatalException extends RuntimeException {

private static final long serialVersionUID = -3119467647792546222L;

public TruffleFatalException(String message, Exception cause) {
super(message, cause);
}
Expand Down
Expand Up @@ -75,7 +75,6 @@ public void initialize(RubyClass superclass) {
public void initCopy(RubyModule other) {
super.initCopy(other);
assert other instanceof RubyClass;
final RubyClass otherClass = (RubyClass) other;
}

private RubyClass ensureSingletonConsistency() {
Expand Down
Expand Up @@ -24,7 +24,6 @@ public class RubyTime extends RubyBasicObject {

private long seconds;
private long nanoseconds;
private boolean isdst;

public RubyTime(RubyClass timeClass, long seconds, long nanoseconds) {
super(timeClass);
Expand Down
60 changes: 31 additions & 29 deletions core/src/main/java/org/jruby/truffle/translator/BodyTranslator.java
Expand Up @@ -86,6 +86,8 @@ public BodyTranslator(RubyNode currentNode, RubyContext context, BodyTranslator
this.parent = parent;
this.environment = environment;
this.topLevel = topLevel;
initGlobalVariableAliases();
initReadOnlyGlobalVariables();
}

@Override
Expand Down Expand Up @@ -797,7 +799,7 @@ public RubyNode visitDVarNode(org.jruby.ast.DVarNode node) {

TranslatorEnvironment e = environment;

for (int n = 0; n < node.getDepth(); n++) {
for (int n = 0; n < depth; n++) {
e = e.getParent();
}

Expand Down Expand Up @@ -1092,28 +1094,33 @@ private static org.jruby.ast.Node setRHS(org.jruby.ast.Node node, org.jruby.ast.
}
}

private final Set<String> readOnlyGlobalVariables = new HashSet<String>() {{
add("$:");
add("$LOAD_PATH");
add("$-I");
add("$\"");
add("$LOADED_FEATURES");
add("$<");
add("$FILENAME");
add("$?");
add("$-a");
add("$-l");
add("$-p");
}};

private final Map<String, String> globalVariableAliases = new HashMap<String, String>() {{
put("$-I", "$LOAD_PATH");
put("$:", "$LOAD_PATH");
put("$-d", "$DEBUG");
put("$-v", "$VERBOSE");
put("$-w", "$VERBOSE");
put("$-0", "$/");
}};
private final Set<String> readOnlyGlobalVariables = new HashSet<String>();
private final Map<String, String> globalVariableAliases = new HashMap<String, String>();

private void initReadOnlyGlobalVariables() {
Set<String> s = readOnlyGlobalVariables;
s.add("$:");
s.add("$LOAD_PATH");
s.add("$-I");
s.add("$\"");
s.add("$LOADED_FEATURES");
s.add("$<");
s.add("$FILENAME");
s.add("$?");
s.add("$-a");
s.add("$-l");
s.add("$-p");
}

private void initGlobalVariableAliases() {
Map<String, String> m = globalVariableAliases;
m.put("$-I", "$LOAD_PATH");
m.put("$:", "$LOAD_PATH");
m.put("$-d", "$DEBUG");
m.put("$-v", "$VERBOSE");
m.put("$-w", "$VERBOSE");
m.put("$-0", "$/");
}

@Override
public RubyNode visitGlobalAsgnNode(org.jruby.ast.GlobalAsgnNode node) {
Expand Down Expand Up @@ -1397,12 +1404,7 @@ public RubyNode visitLocalAsgnNode(org.jruby.ast.LocalAsgnNode node) {
}
}

RubyNode translated = ((ReadNode) lhs).makeWriteNode(rhs);

final SharedMethodInfo methodIdentifier = environment.findMethodForLocalVar(node.getName());

// return instrumenter.instrumentAsLocalAssignment(translated, methodIdentifier, node.getName());
return translated;
return ((ReadNode) lhs).makeWriteNode(rhs);
}

@Override
Expand Down
Expand Up @@ -19,7 +19,7 @@
/**
* Collects paramter names from a JRuby AST.
*/
public class ParameterCollector extends AbstractNodeVisitor {
public class ParameterCollector extends AbstractNodeVisitor<Object> {

private final List<String> parameters = new ArrayList<>();
private final List<String> keywords = new ArrayList<>();
Expand Down
Expand Up @@ -44,13 +44,8 @@ public static enum ParserContext {
TOP_LEVEL, SHELL, MODULE
}

private final RubyContext context;
private long nextReturnID = 0;

public TranslatorDriver(RubyContext context) {
this.context = context;
}

public MethodDefinitionNode parse(RubyContext context, org.jruby.ast.Node parseTree, org.jruby.ast.ArgsNode argsNode, org.jruby.ast.Node bodyNode, RubyNode currentNode) {
final SourceSection sourceSection = null;

Expand Down

0 comments on commit e0ea4e8

Please sign in to comment.