Skip to content

Commit

Permalink
Replace requiresContext with WithContext
Browse files Browse the repository at this point in the history
- avoids duplicating the same knowledge
- gives us common interface for init, avoiding constructor
- add corresponding classes AritySystemOperation
- make sure to initialize nodes in the right order
  1. set source section
  2. optionally, set the eager primitive flag
  3. set VM, i.e., context
  With this order, initializations of breakpoints and other things that
  require source sections and the VM, are only safe in the last step,
  after setting the VM.

Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Aug 11, 2017
1 parent 1bdbd21 commit bc4e21b
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 245 deletions.
18 changes: 13 additions & 5 deletions src/som/interpreter/actors/AbstractPromiseResolutionNode.java
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.ForkJoinPool;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.Instrumentable;
Expand All @@ -12,24 +13,31 @@
import som.interpreter.actors.SPromise.SResolver;
import som.interpreter.nodes.nary.QuaternaryExpressionNode;
import som.interpreter.nodes.nary.UnaryExpressionNode;
import som.primitives.WithContext;
import som.vm.VmSettings;
import tools.concurrency.ActorExecutionTrace;


@Instrumentable(factory = AbstractPromiseResolutionNodeWrapper.class)
public abstract class AbstractPromiseResolutionNode extends QuaternaryExpressionNode {
private final ForkJoinPool actorPool;
public abstract class AbstractPromiseResolutionNode extends QuaternaryExpressionNode
implements WithContext<AbstractPromiseResolutionNode> {
@CompilationFinal private ForkJoinPool actorPool;

@Child protected WrapReferenceNode wrapper = WrapReferenceNodeGen.create();
@Child protected UnaryExpressionNode haltNode;

protected AbstractPromiseResolutionNode(final ForkJoinPool actorPool) {
this.actorPool = actorPool;
protected AbstractPromiseResolutionNode() {
haltNode = insert(SuspendExecutionNodeGen.create(2, null));
}

protected AbstractPromiseResolutionNode(final AbstractPromiseResolutionNode node) {
this(node.actorPool);
this();
}

@Override
public AbstractPromiseResolutionNode initialize(final VM vm) {
actorPool = vm.getActorPool();
return this;
}

@Override
Expand Down
8 changes: 1 addition & 7 deletions src/som/interpreter/actors/ErrorPromiseNode.java
Expand Up @@ -4,20 +4,14 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;

import som.VM;
import som.interpreter.actors.SPromise.Resolution;
import som.interpreter.actors.SPromise.SResolver;
import som.primitives.Primitive;


@GenerateNodeFactory
@Primitive(primitive = "actorsError:with:isBPResolver:isBPResolution:", requiresContext = true)
@Primitive(primitive = "actorsError:with:isBPResolver:isBPResolution:")
public abstract class ErrorPromiseNode extends AbstractPromiseResolutionNode {

protected ErrorPromiseNode(final VM vm) {
super(vm.getActorPool());
}

/**
* Standard error case, when the promise is errored with a value that's not a promise.
*/
Expand Down
12 changes: 4 additions & 8 deletions src/som/interpreter/actors/ReceivedRootNode.java
@@ -1,7 +1,5 @@
package som.interpreter.actors;

import java.util.concurrent.ForkJoinPool;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.VirtualFrame;
Expand Down Expand Up @@ -51,7 +49,8 @@ protected final void resolvePromise(final VirtualFrame frame,
if (resolver == null) {
this.resolve = insert(new NullResolver());
} else {
this.resolve = insert(ResolvePromiseNodeFactory.create(vm, null, null, null, null));
this.resolve = insert(
ResolvePromiseNodeFactory.create(null, null, null, null).initialize(vm));
}
}

Expand All @@ -68,7 +67,8 @@ protected final void errorPromise(final VirtualFrame frame,
if (resolver == null) {
this.error = insert(new NullResolver());
} else {
this.error = insert(ErrorPromiseNodeFactory.create(vm, null, null, null, null));
this.error = insert(
ErrorPromiseNodeFactory.create(null, null, null, null).initialize(vm));
}
}

Expand All @@ -80,10 +80,6 @@ protected final void errorPromise(final VirtualFrame frame,
* Promise resolver for the case that the actual promise has been optimized out.
*/
public final class NullResolver extends AbstractPromiseResolutionNode {
public NullResolver() {
super((ForkJoinPool) null);
}

@Override
public Object executeEvaluated(final VirtualFrame frame,
final SResolver receiver, final Object argument,
Expand Down
9 changes: 1 addition & 8 deletions src/som/interpreter/actors/ResolvePromiseNode.java
Expand Up @@ -4,21 +4,14 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;

import som.VM;
import som.interpreter.actors.SPromise.Resolution;
import som.interpreter.actors.SPromise.SResolver;
import som.primitives.Primitive;


@GenerateNodeFactory
@Primitive(primitive = "actorsResolve:with:isBPResolver:isBPResolution:",
requiresContext = true)
@Primitive(primitive = "actorsResolve:with:isBPResolver:isBPResolution:")
public abstract class ResolvePromiseNode extends AbstractPromiseResolutionNode {

protected ResolvePromiseNode(final VM vm) {
super(vm.getActorPool());
}

/**
* Normal case, when the promise is resolved with a value that's not a promise.
* Here we need to distinguish the explicit promises to ask directly to the promise
Expand Down
16 changes: 16 additions & 0 deletions src/som/interpreter/nodes/nary/BinaryComplexOperation.java
@@ -1,5 +1,9 @@
package som.interpreter.nodes.nary;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;

import som.VM;
import som.primitives.WithContext;
import tools.dym.Tags.ComplexPrimitiveOperation;


Expand All @@ -18,4 +22,16 @@ protected boolean isTaggedWithIgnoringEagerness(final Class<?> tag) {
return super.isTaggedWithIgnoringEagerness(tag);
}
}

public abstract static class BinarySystemOperation extends BinaryComplexOperation
implements WithContext<BinarySystemOperation> {
@CompilationFinal protected VM vm;

@Override
public BinarySystemOperation initialize(final VM vm) {
assert this.vm == null && vm != null;
this.vm = vm;
return this;
}
}
}
15 changes: 15 additions & 0 deletions src/som/interpreter/nodes/nary/TernaryExpressionNode.java
@@ -1,11 +1,14 @@
package som.interpreter.nodes.nary;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.Instrumentable;

import som.VM;
import som.interpreter.nodes.ExpressionNode;
import som.primitives.WithContext;
import som.vmobjects.SSymbol;


Expand Down Expand Up @@ -37,4 +40,16 @@ public EagerPrimitive wrapInEagerWrapper(final SSymbol selector,
result.initialize(sourceSection);
return result;
}

public abstract static class TernarySystemOperation extends TernaryExpressionNode
implements WithContext<TernaryExpressionNode> {
@CompilationFinal protected VM vm;

@Override
public TernaryExpressionNode initialize(final VM vm) {
assert this.vm == null && vm != null;
this.vm = vm;
return this;
}
}
}
15 changes: 15 additions & 0 deletions src/som/interpreter/nodes/nary/UnaryExpressionNode.java
@@ -1,10 +1,13 @@
package som.interpreter.nodes.nary;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.Instrumentable;

import som.VM;
import som.interpreter.nodes.ExpressionNode;
import som.primitives.WithContext;
import som.vmobjects.SSymbol;


Expand All @@ -31,4 +34,16 @@ public EagerPrimitive wrapInEagerWrapper(final SSymbol selector,
result.initialize(sourceSection);
return result;
}

public abstract static class UnarySystemOperation extends UnaryExpressionNode
implements WithContext<UnarySystemOperation> {
@CompilationFinal protected VM vm;

@Override
public UnarySystemOperation initialize(final VM vm) {
assert this.vm == null && vm != null;
this.vm = vm;
return this;
}
}
}
69 changes: 28 additions & 41 deletions src/som/primitives/ActivitySpawn.java
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.ForkJoinPool;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
Expand All @@ -12,8 +13,8 @@
import com.oracle.truffle.api.source.SourceSection;

import som.VM;
import som.interpreter.nodes.nary.BinaryComplexOperation;
import som.interpreter.nodes.nary.TernaryExpressionNode;
import som.interpreter.nodes.nary.BinaryComplexOperation.BinarySystemOperation;
import som.interpreter.nodes.nary.TernaryExpressionNode.TernarySystemOperation;
import som.primitives.ObjectPrims.IsValue;
import som.primitives.arrays.ToArgumentsArrayNode;
import som.primitives.arrays.ToArgumentsArrayNodeFactory;
Expand Down Expand Up @@ -90,31 +91,25 @@ public static IsValue createIsValue() {

@GenerateNodeFactory
@ImportStatic({ThreadingModule.class, ChannelPrimitives.class, ActivitySpawn.class})
@Primitive(primitive = "threading:threadSpawn:", requiresContext = true)
@Primitive(primitive = "threading:taskSpawn:", requiresContext = true)
@Primitive(selector = "spawn:", requiresContext = true)
public abstract static class SpawnPrim extends BinaryComplexOperation {
private final ForkJoinPool forkJoinPool;
private final ForkJoinPool processesPool;
private final ForkJoinPool threadPool;
@Primitive(primitive = "threading:threadSpawn:")
@Primitive(primitive = "threading:taskSpawn:")
@Primitive(selector = "spawn:")
public abstract static class SpawnPrim extends BinarySystemOperation {
@CompilationFinal private ForkJoinPool forkJoinPool;
@CompilationFinal private ForkJoinPool processesPool;
@CompilationFinal private ForkJoinPool threadPool;

/** Breakpoint info for triggering suspension on first execution of code in activity. */
@Child protected AbstractBreakpointNode onExec;

private final VM vm;

public SpawnPrim(final VM vm) {
this.vm = vm;
@Override
public final SpawnPrim initialize(final VM vm) {
super.initialize(vm);
this.onExec = insert(
Breakpoints.create(sourceSection, BreakpointType.ACTIVITY_ON_EXEC, vm));
this.forkJoinPool = vm.getForkJoinPool();
this.processesPool = vm.getProcessPool();
this.threadPool = vm.getThreadPool();
}

@Override
@SuppressWarnings("unchecked")
public final SpawnPrim initialize(final SourceSection source) {
super.initialize(source);
this.onExec = insert(Breakpoints.create(source, BreakpointType.ACTIVITY_ON_EXEC, vm));
return this;
}

Expand Down Expand Up @@ -169,35 +164,27 @@ protected boolean isTaggedWithIgnoringEagerness(final Class<?> tag) {
@NodeChild(value = "argArr", type = ToArgumentsArrayNode.class,
executeWith = {"secondArg", "firstArg"})
@Primitive(primitive = "threading:threadSpawn:with:",
extraChild = ToArgumentsArrayNodeFactory.class, requiresContext = true)
extraChild = ToArgumentsArrayNodeFactory.class)
@Primitive(primitive = "threading:taskSpawn:with:",
extraChild = ToArgumentsArrayNodeFactory.class, requiresContext = true)
@Primitive(primitive = "proc:spawn:with:",
extraChild = ToArgumentsArrayNodeFactory.class, requiresContext = true)
@Primitive(selector = "spawn:with:",
extraChild = ToArgumentsArrayNodeFactory.class, requiresContext = true)
public abstract static class SpawnWithPrim extends TernaryExpressionNode {
private final ForkJoinPool forkJoinPool;
private final ForkJoinPool processesPool;
private final ForkJoinPool threadPool;
extraChild = ToArgumentsArrayNodeFactory.class)
@Primitive(primitive = "proc:spawn:with:", extraChild = ToArgumentsArrayNodeFactory.class)
@Primitive(selector = "spawn:with:", extraChild = ToArgumentsArrayNodeFactory.class)
public abstract static class SpawnWithPrim extends TernarySystemOperation {
@CompilationFinal private ForkJoinPool forkJoinPool;
@CompilationFinal private ForkJoinPool processesPool;
@CompilationFinal private ForkJoinPool threadPool;

/** Breakpoint info for triggering suspension on first execution of code in activity. */
@Child protected AbstractBreakpointNode onExec;

private final VM vm;

public SpawnWithPrim(final VM vm) {
this.vm = vm;
@Override
public final SpawnWithPrim initialize(final VM vm) {
super.initialize(vm);
this.onExec = insert(
Breakpoints.create(sourceSection, BreakpointType.ACTIVITY_ON_EXEC, vm));
this.forkJoinPool = vm.getForkJoinPool();
this.processesPool = vm.getProcessPool();
this.threadPool = vm.getThreadPool();
}

@Override
@SuppressWarnings("unchecked")
public final SpawnWithPrim initialize(final SourceSection source) {
super.initialize(source);
this.onExec = insert(Breakpoints.create(source, BreakpointType.ACTIVITY_ON_EXEC, vm));
return this;
}

Expand Down
3 changes: 0 additions & 3 deletions src/som/primitives/Primitive.java
Expand Up @@ -45,9 +45,6 @@
/** Pass array of evaluated arguments to node constructor. */
boolean requiresArguments() default false;

/** Pass VM object, i.e., execution context to node constructor. */
boolean requiresContext() default false;

/** Disabled for Dynamic Metrics. */
boolean disabled() default false;

Expand Down

0 comments on commit bc4e21b

Please sign in to comment.