Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify BlackDiamonds code: Avoid Unnecessary Generics #209

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/trufflesom/src/trufflesom/bdt/inlining/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import trufflesom.compiler.Variable;


/**
Expand Down Expand Up @@ -30,11 +31,9 @@ public interface Scope<This extends Scope<This, MethodT>, MethodT> {
* The set excludes variables that are defined in other scopes, even if they might be
* logically part of the set from language perspective, perhaps because of nesting scopes.
*
* @param <T> the type of the {@link Variable} implementation
*
* @return the set of variables defined in this scope
*/
<T extends Variable<? extends Node>> T[] getVariables();
Variable[] getVariables();

/**
* The scope directly enclosing the current scope.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import trufflesom.bdt.inlining.nodes.ScopeReference;
import trufflesom.bdt.inlining.nodes.WithSource;
import trufflesom.compiler.Variable;
import trufflesom.compiler.Variable.Local;
import trufflesom.interpreter.nodes.ExpressionNode;


/**
Expand Down Expand Up @@ -92,20 +94,18 @@ public boolean outerScopeChanged() {

/**
* The result of a lookup in the scope chain to find a variable and its context level.
*
* @param <N> the type of node used for node access, can be very unprecise
*/
public static final class ScopeElement<N extends Node> {
public static final class ScopeElement {

/** The variable found by the lookup. */
public final Variable<N> var;
public final Variable var;

/**
* The context level at which the variable is defined, relative to the start of the lookup.
*/
public final int contextLevel;

private ScopeElement(final Variable<N> var, final int contextLevel) {
private ScopeElement(final Variable var, final int contextLevel) {
this.var = var;
this.contextLevel = contextLevel;
}
Expand All @@ -117,11 +117,11 @@ public String toString() {
}

@SuppressWarnings("unchecked")
private <N extends Node> ScopeElement<N> getSplitVar(final Variable<N> var,
private <N extends Node> ScopeElement getSplitVar(final Variable var,
final Scope<?, ?> scope, final int lvl) {
for (Variable<? extends Node> v : scope.getVariables()) {
for (Variable v : scope.getVariables()) {
if (v.equals(var)) {
return new ScopeElement<>((Variable<N>) v, lvl);
return new ScopeElement(v, lvl);
}
}

Expand All @@ -139,7 +139,7 @@ private <N extends Node> ScopeElement<N> getSplitVar(final Variable<N> var,
* @param var in the un-adapted node
* @return the adapted version of the variable
*/
public <N extends Node> ScopeElement<N> getAdaptedVar(final Variable<N> var) {
public ScopeElement getAdaptedVar(final Variable var) {
return getSplitVar(var, newScope, 0);
}

Expand All @@ -165,12 +165,12 @@ public Local getAdaptedLocal(final int localIdx, final int contextLvl,
newCtxLevel -= 1;
}

Variable<?>[] old = oldS.getVariables();
for (Variable<?> v : old) {
Variable[] old = oldS.getVariables();
for (Variable v : old) {
if (v instanceof Local) {
Local l = (Local) v;
if (l.getIndex() == localIdx) {
for (Variable<?> newV : newS.getVariables()) {
for (Variable newV : newS.getVariables()) {
if (newV.equals(l)) {
return (Local) newV;
}
Expand Down Expand Up @@ -233,9 +233,9 @@ public boolean visit(final Node node) {
* @param node the read node
* @param ctxLevel the context level of the node
*/
public void updateRead(final Variable<?> var, final WithSource node,
public void updateRead(final Variable var, final WithSource node,
final int ctxLevel) {
ScopeElement<? extends Node> se = getAdaptedVar(var);
ScopeElement se = getAdaptedVar(var);
if (se.var != var || se.contextLevel < ctxLevel) {
((Node) node).replace(se.var.getReadNode(se.contextLevel, node.getSourceCoordinate()));
} else {
Expand All @@ -246,17 +246,15 @@ public void updateRead(final Variable<?> var, final WithSource node,
/**
* Factory method to update a write node with an appropriate version for the adapted scope.
*
* @param <N> the type of the node to be returned
*
* @param var the variable accessed by {@code node}
* @param node the write node
* @param valExpr the expression that is evaluated to determine the value to be written to
* the variable
* @param ctxLevel the context level of the node
*/
public <N extends Node> void updateWrite(final Variable<N> var, final WithSource node,
final N valExpr, final int ctxLevel) {
ScopeElement<N> se = getAdaptedVar(var);
public void updateWrite(final Variable var, final WithSource node,
final ExpressionNode valExpr, final int ctxLevel) {
ScopeElement se = getAdaptedVar(var);
if (se.var != var || se.contextLevel < ctxLevel) {
((Node) node).replace(
se.var.getWriteNode(se.contextLevel, valExpr, node.getSourceCoordinate()));
Expand Down
3 changes: 2 additions & 1 deletion src/trufflesom/src/trufflesom/bdt/inlining/ScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import trufflesom.bdt.basic.ProgramDefinitionError;
import trufflesom.bdt.inlining.nodes.Inlinable;
import trufflesom.compiler.Variable;


/**
Expand Down Expand Up @@ -32,7 +33,7 @@ public interface ScopeBuilder<This extends ScopeBuilder<This>> {
* the variable. This is supposed to be used to indicate errors in the user
* program.
*/
Variable<?> introduceTempForInlinedVersion(Inlinable<This> node, long coord)
Variable introduceTempForInlinedVersion(Inlinable<This> node, long coord)
throws ProgramDefinitionError;

Source getSource();
Expand Down
56 changes: 0 additions & 56 deletions src/trufflesom/src/trufflesom/bdt/inlining/Variable.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static <Context, Id, ExprT> void addAll(
* <p>
* This methods should be called when the constructor completes.
*/
protected void initialize() {
protected final void initialize() {
List<Specializer<ExprT, Id>> specializers = getSpecializers();
for (Specializer<ExprT, Id> s : specializers) {
// TODO: figure out whether we really want it like this with a VmSetting, or whether
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public void mergeIntoScope(final LexicalScope scope, final SMethod toBeInlined)
}

@Override
public trufflesom.bdt.inlining.Variable<?> introduceTempForInlinedVersion(
public Variable introduceTempForInlinedVersion(
final Inlinable<MethodGenerationContext> blockOrVal, final long coord)
throws ProgramDefinitionError {
Local loopIdx;
Expand Down
2 changes: 1 addition & 1 deletion src/trufflesom/src/trufflesom/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected Parser(final String content, final Source source,
getSymbolFromLexer();
}

protected Lexer createLexer(final String content) {
protected static Lexer createLexer(final String content) {
return new Lexer(content);
}

Expand Down
8 changes: 4 additions & 4 deletions src/trufflesom/src/trufflesom/compiler/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import trufflesom.vmobjects.SSymbol;


public abstract class Variable implements trufflesom.bdt.inlining.Variable<ExpressionNode> {
public abstract class Variable {
public final SSymbol name;
public final long coord;

Expand All @@ -52,9 +52,11 @@ public String toString() {
return getClass().getSimpleName() + "(" + name + ")";
}

@Override
public abstract ExpressionNode getReadNode(int contextLevel, long coordinate);

public abstract ExpressionNode getWriteNode(
int contextLevel, ExpressionNode valueExpr, long coord);

protected abstract void emitPop(BytecodeMethodGenContext mgenc);

protected abstract void emitPush(BytecodeMethodGenContext mgenc);
Expand Down Expand Up @@ -122,7 +124,6 @@ public ExpressionNode getReadNode(final int contextLevel, final long coordinate)
}
}

@Override
public ExpressionNode getWriteNode(final int contextLevel,
final ExpressionNode valueExpr, final long coordinate) {
if (contextLevel == 0) {
Expand Down Expand Up @@ -187,7 +188,6 @@ public Local splitToMergeIntoOuterScope(final int newSlotIndex) {
return new Local(name, coord, newSlotIndex);
}

@Override
public ExpressionNode getWriteNode(final int contextLevel,
final ExpressionNode valueExpr, final long coordinate) {
if (contextLevel > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Object executeGeneric(final VirtualFrame frame) {

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<ExpressionNode> se = inliner.getAdaptedVar(onStackMarkerVar);
ScopeElement se = inliner.getAdaptedVar(onStackMarkerVar);

if (se.var != onStackMarkerVar || se.contextLevel < contextLevel) {
ExpressionNode node;
Expand Down Expand Up @@ -125,7 +125,7 @@ public Object executeGeneric(final VirtualFrame frame) {

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<ExpressionNode> se = inliner.getAdaptedVar(onStackMarkerVar);
ScopeElement se = inliner.getAdaptedVar(onStackMarkerVar);
if (se.var != onStackMarkerVar) {
ReturnLocalNode node = new ReturnLocalNode(expression, (Internal) se.var);
node.initialize(sourceCoord);
Expand Down Expand Up @@ -183,7 +183,7 @@ public Object executeGeneric(final VirtualFrame frame) {

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<ExpressionNode> se = inliner.getAdaptedVar(onStackMarkerVar);
ScopeElement se = inliner.getAdaptedVar(onStackMarkerVar);
if (se.var != onStackMarkerVar) {
replace(new CatchNonLocalReturnNode(
methodBody, (Internal) se.var).initialize(sourceCoord));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void reportLoopCount(final long count) {

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<ExpressionNode> se = inliner.getAdaptedVar(loopIdxVar);
ScopeElement se = inliner.getAdaptedVar(loopIdxVar);
IntDownToDoInlinedLiteralsNode node =
IntDownToDoInlinedLiteralsNodeFactory.create(bodyActualNode,
body, (Local) se.var, getFrom(), getTo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void reportLoopCount(final long count) {

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<ExpressionNode> se = inliner.getAdaptedVar(loopIdxVar);
ScopeElement se = inliner.getAdaptedVar(loopIdxVar);
IntToDoInlinedLiteralsNode node = IntToDoInlinedLiteralsNodeFactory.create(bodyActualNode,
body, (Local) se.var, getFrom(), getTo());
node.initialize(sourceCoord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public GenericMessageSendNode makeGenericSend(

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<? extends Node> se = inliner.getAdaptedVar(arg);
ScopeElement se = inliner.getAdaptedVar(arg);
if (se.var != arg || se.contextLevel < 0) {
Node newNode;
if (se.contextLevel == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public final GenericMessageSendNode makeGenericSend(

@Override
public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) {
ScopeElement<? extends Node> se = inliner.getAdaptedVar(arg);
ScopeElement se = inliner.getAdaptedVar(arg);
if (se.var != arg || se.contextLevel < contextLevel) {
Node newNode;
if (se.contextLevel == 0) {
Expand Down
4 changes: 2 additions & 2 deletions tests/trufflesom/bdt/inlining/TScope.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package trufflesom.bdt.inlining;

import com.oracle.truffle.api.nodes.Node;
import trufflesom.compiler.Variable;


public class TScope implements Scope<TScope, Void> {

@Override
@SuppressWarnings("unchecked")
public <T extends Variable<? extends Node>> T[] getVariables() {
public Variable[] getVariables() {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/trufflesom/bdt/inlining/TScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import trufflesom.bdt.basic.ProgramDefinitionError;
import trufflesom.bdt.inlining.nodes.Inlinable;
import trufflesom.compiler.Variable;


public class TScopeBuilder implements ScopeBuilder<TScopeBuilder> {

@Override
public Variable<?> introduceTempForInlinedVersion(final Inlinable<TScopeBuilder> node,
public Variable introduceTempForInlinedVersion(final Inlinable<TScopeBuilder> node,
final long coord) throws ProgramDefinitionError {
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/trufflesom/bdt/primitives/Primitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@


public class Primitives extends PrimitiveLoader<ExprNode, String> {

@SuppressWarnings("this-escape")
protected Primitives() {
super(new StringId());
initialize();
Expand Down
Loading