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

Implement Poly2Kanon #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions truffle/sl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

/usr/lib64/openjdk-11/bin/java --add-modules=org.graalvm.truffle --module-path=/home/pois/Programs/graal/graal/truffle/mxbuild/dists/jdk11/truffle-api.jar:$GRAAL_PRJ_HOME/graal/sdk/mxbuild/linux-amd64/GRAALVM_591BD358E0_JAVA11/graalvm-591bd358e0-java11-21.0.0-dev/lib/:$GRAAL_PRJ_HOME/graal/sdk/mxbuild/dists/jdk11/: -cp /home/pois/.mx/cache/ANTLR4_e27d8ab4f984f9d186f54da984a6ab -cp /home/pois/Programs/graal/graal/sdk/mxbuild/dists/jdk11/graal-sdk.jar -cp $GRAAL_PRJ_HOME/graal/truffle/mxbuild/src:/home/pois/Programs/graal/graal/truffle/mxbuild/src/com.oracle.truffle.sl/bin:/home/pois/Programs/graal/graal/truffle/mxbuild/src/com.oracle.truffle.sl.launcher/bin:/home/pois/.mx/cache/ANTLR4_e27d8ab4f984f9d186f54da984a6ab1cccac755e/antlr4.jar com.oracle.truffle.sl.launcher.SLMain
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,29 @@ AllocationEventFilter getAllocationFilter() {

}

static final class ObjectChange<T> extends EventBinding<T> {
private final AllocationEventFilter filterObjectChange;

ObjectChange(AbstractInstrumenter instrumenter, AllocationEventFilter filter, T lister) {
super(instrumenter, lister);
this.filterObjectChange = filter;
}

AllocationEventFilter getAllocationFilter() {
return filterObjectChange;
}
}

static final class StackTracking<T> extends EventBinding<T> {
private final AllocationEventFilter filterObjectChange;

StackTracking(AbstractInstrumenter instrumenter, AllocationEventFilter filter, T lister) {
super(instrumenter, lister);
this.filterObjectChange = filter;
}

AllocationEventFilter getAllocationFilter() {
return filterObjectChange;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ public void collectEnvServices(Set<Object> collectTo, Object polyglotLanguage, T
Instrumenter instrumenter = instrumentationHandler.forLanguage(language);
collectTo.add(instrumenter);
AllocationReporter allocationReporter = instrumentationHandler.getAllocationReporter(InstrumentAccessor.langAccess().getLanguageInfo(language));
ObjectTracker objectTracker = instrumentationHandler.getObjectTracker(InstrumentAccessor.langAccess().getLanguageInfo(language));
StackTracker stackTracker = instrumentationHandler.getStackTracker(InstrumentAccessor.langAccess().getLanguageInfo(language));
collectTo.add(allocationReporter);
collectTo.add(objectTracker);
collectTo.add(stackTracker);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ final class InstrumentationHandler {
final Collection<RootNode> loadedRoots = new WeakAsyncList<>(256);
private final Collection<RootNode> executedRoots = new WeakAsyncList<>(64);
private final Collection<AllocationReporter> allocationReporters = new WeakAsyncList<>(16);
private final Collection<ObjectTracker> objectTrackers = new WeakAsyncList<>(16);
private final Collection<StackTracker> stackTrackers = new WeakAsyncList<>(16);


private volatile boolean hasLoadOrExecutionBinding = false;
private final CopyOnWriteList<EventBinding.Source<?>> executionBindings = new CopyOnWriteList<>(new EventBinding.Source<?>[0]);
Expand All @@ -153,6 +156,8 @@ final class InstrumentationHandler {
private final Collection<EventBinding<? extends OutputStream>> outputStdBindings = new EventBindingList<>(1);
private final Collection<EventBinding<? extends OutputStream>> outputErrBindings = new EventBindingList<>(1);
private final Collection<EventBinding.Allocation<? extends AllocationListener>> allocationBindings = new EventBindingList<>(2);
private final Collection<EventBinding.ObjectChange<? extends ObjectChangeListener>> objectChangeBindings = new EventBindingList<>(2);
private final Collection<EventBinding.StackTracking<? extends StackListener>> stackTrackingBindings = new EventBindingList<>(2);
private final Collection<EventBinding<? extends ContextsListener>> contextsBindings = new EventBindingList<>(8);
private final Collection<EventBinding<? extends ThreadsListener>> threadsBindings = new EventBindingList<>(8);
private final Collection<EventBinding<? extends ThreadsActivationListener>> threadsActivationBindings = new EventBindingList<>(8);
Expand Down Expand Up @@ -543,6 +548,42 @@ private <T extends AllocationListener> EventBinding<T> addAllocationBinding(Even
return binding;
}

private <T extends ObjectChangeListener> EventBinding<T> addObjectChangeBinding(EventBinding.ObjectChange<T> binding) {
if (TRACE) {
trace("BEGIN: Adding allocation binding %s%n", binding.getElement());
}

this.objectChangeBindings.add(binding);
for (ObjectTracker objectTracker : objectTrackers) {
if (binding.getAllocationFilter().contains(objectTracker.language)) {
objectTracker.addListener(binding.getElement());
}
}

if (TRACE) {
trace("END: Added allocation binding %s%n", binding.getElement());
}
return binding;
}

private <T extends StackListener> EventBinding<T> addStackTrackingBinding(EventBinding.StackTracking<T> binding) {
if (TRACE) {
trace("BEGIN: Adding allocation binding %s%n", binding.getElement());
}

this.stackTrackingBindings.add(binding);
for (StackTracker stackTracker : stackTrackers) {
if (binding.getAllocationFilter().contains(stackTracker.language)) {
stackTracker.addListener(binding.getElement());
}
}

if (TRACE) {
trace("END: Added allocation binding %s%n", binding.getElement());
}
return binding;
}

private <T extends ContextsListener> EventBinding<T> addContextsBinding(EventBinding<T> binding, boolean includeActiveContexts) {
if (TRACE) {
trace("BEGIN: Adding contexts binding %s%n", binding.getElement());
Expand Down Expand Up @@ -683,6 +724,22 @@ void disposeBinding(EventBinding<?> binding) {
allocationReporter.removeListener(l);
}
}
} else if (binding instanceof EventBinding.ObjectChange) {
final EventBinding.ObjectChange<?> objectChangeBinding = (EventBinding.ObjectChange<?>) binding;
final ObjectChangeListener l = (ObjectChangeListener) binding.getElement();
for (ObjectTracker objectTracker : objectTrackers) {
if (objectChangeBinding.getAllocationFilter().contains(objectTracker.language)) {
objectTracker.removeListener(l);
}
}
} else if (binding instanceof EventBinding.StackTracking) {
final EventBinding.StackTracking<?> stackTrackingBinding = (EventBinding.StackTracking<?>) binding;
final StackListener l = (StackListener) binding.getElement();
for (StackTracker stackTracker : stackTrackers) {
if (stackTrackingBinding.getAllocationFilter().contains(stackTracker.language)) {
stackTracker.removeListener(l);
}
}
} else {
Object elm = binding.getElement();
if (elm instanceof OutputStream) {
Expand Down Expand Up @@ -989,6 +1046,14 @@ private <T extends AllocationListener> EventBinding<T> attachAllocationListener(
return addAllocationBinding(new EventBinding.Allocation<>(instrumenter, filter, listener));
}

private <T extends ObjectChangeListener> EventBinding<T> attachObjectChangeListener(AbstractInstrumenter instrumenter, AllocationEventFilter filter, T listener) {
return addObjectChangeBinding(new EventBinding.ObjectChange<>(instrumenter, filter, listener));
}

private <T extends StackListener> EventBinding<T> attachStackListener(AbstractInstrumenter instrumenter, AllocationEventFilter filter, T listener) {
return addStackTrackingBinding(new EventBinding.StackTracking<>(instrumenter, filter, listener));
}

private <T extends ContextsListener> EventBinding<T> attachContextsListener(AbstractInstrumenter instrumenter, T listener, boolean includeActiveContexts) {
assert listener != null;
return addContextsBinding(new EventBinding<>(instrumenter, listener), includeActiveContexts);
Expand Down Expand Up @@ -1266,6 +1331,28 @@ AllocationReporter getAllocationReporter(LanguageInfo info) {
return allocationReporter;
}

ObjectTracker getObjectTracker(LanguageInfo info) {
final ObjectTracker objectTracker = new ObjectTracker(info);
objectTrackers.add(objectTracker);
for (EventBinding.ObjectChange<? extends ObjectChangeListener> binding : objectChangeBindings) {
if (binding.getAllocationFilter().contains(info)) {
objectTracker.addListener(binding.getElement());
}
}
return objectTracker;
}

StackTracker getStackTracker(LanguageInfo info) {
final StackTracker stackTracker = new StackTracker(info);
stackTrackers.add(stackTracker);
for (EventBinding.StackTracking<? extends StackListener> binding : stackTrackingBindings) {
if (binding.getAllocationFilter().contains(info)) {
stackTracker.addListener(binding.getElement());
}
}
return stackTracker;
}

void finalizeStore() {
this.out = null;
this.err = null;
Expand Down Expand Up @@ -2502,6 +2589,16 @@ public <T extends AllocationListener> EventBinding<T> attachAllocationListener(A
return InstrumentationHandler.this.attachAllocationListener(this, filter, listener);
}

@Override
public <T extends ObjectChangeListener> EventBinding<T> attachObjectChangeListener(AllocationEventFilter filter, T listener) {
return InstrumentationHandler.this.attachObjectChangeListener(this, filter, listener);
}

@Override
public <T extends StackListener> EventBinding<T> attachStackListener(AllocationEventFilter filter, T listener) {
return InstrumentationHandler.this.attachStackListener(this, filter, listener);
}

@Override
public <T extends OutputStream> EventBinding<T> attachOutConsumer(T stream) {
return InstrumentationHandler.this.attachOutputConsumer(this, stream, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ public final <T extends ExecutionEventNodeFactory> EventBinding<T> attachExecuti
*/
public abstract <T extends AllocationListener> EventBinding<T> attachAllocationListener(AllocationEventFilter filter, T listener);

public abstract <T extends ObjectChangeListener> EventBinding<T> attachObjectChangeListener(AllocationEventFilter filter, T listener);

public abstract <T extends StackListener> EventBinding<T> attachStackListener(AllocationEventFilter filter, T listener);

/**
* Attach a {@link ContextsListener listener} to be notified about changes in contexts in guest
* language application. This is supported in {@link TruffleInstrument.Env#getInstrumenter()}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.oracle.truffle.api.instrumentation;

public class ObjectChangeEvent {
private final Object object;
private final Object key;
private final Object value;

ObjectChangeEvent(Object object, Object key, Object value) {
this.object = object;
this.key = key;
this.value = value;
}

public Object getObject() {
return object;
}

public Object getKey() {
return key;
}

public Object getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.oracle.truffle.api.instrumentation;

public interface ObjectChangeListener {
void onFieldAssigned(ObjectChangeEvent e);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.oracle.truffle.api.instrumentation;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.LanguageInfo;

import java.util.Arrays;

public class ObjectTracker {
@CompilerDirectives.CompilationFinal(dimensions = 1) private volatile ObjectChangeListener[] listeners = null;

final LanguageInfo language;

public ObjectTracker(LanguageInfo language) {
this.language = language;
}

public void addListener(ObjectChangeListener l) {
CompilerAsserts.neverPartOfCompilation();
synchronized (this) {
if (listeners == null) {
listeners = new ObjectChangeListener[]{l};
} else {
int i = listeners.length;
ObjectChangeListener[] newListeners = Arrays.copyOf(listeners, i + 1);
newListeners[i] = l;
listeners = newListeners;
}
}
}

public void removeListener(ObjectChangeListener l) {
CompilerAsserts.neverPartOfCompilation();
synchronized (this) {
final int len = listeners.length;
if (len == 1) {
if (listeners[0] == l) {
listeners = null;
}
} else {
for (int i = 0; i < len; i++) {
if (listeners[i] == l) {
if (i == (len - 1)) {
listeners = Arrays.copyOf(listeners, i);
} else if (i == 0) {
listeners = Arrays.copyOfRange(listeners, 1, len);
} else {
ObjectChangeListener[] newListeners = new ObjectChangeListener[len - 1];
System.arraycopy(listeners, 0, newListeners, 0, i);
System.arraycopy(listeners, i + 1, newListeners, i, len - i - 1);
listeners = newListeners;
}
break;
}
}
}
}
}

@ExplodeLoop
public void notifyAssignment(Object object, Object key, Object value) {
CompilerAsserts.partialEvaluationConstant(this);

if (listeners != null) {
ObjectChangeListener[] ls = listeners;
ObjectChangeEvent e = new ObjectChangeEvent(object, key, value);
for (ObjectChangeListener listener : ls) {
listener.onFieldAssigned(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.oracle.truffle.api.instrumentation;

import com.oracle.truffle.api.frame.FrameSlot;

public interface StackListener {
public void onObjectSet(FrameSlot slot, Object value);

public void onBooleanSet(FrameSlot slot, boolean value);

public void onByteSet(FrameSlot slot, byte value);

public void onIntSet(FrameSlot slot, int value);

public void onLongSet(FrameSlot slot, long value);

public void onFloatSet(FrameSlot slot, float value);

public void onDoubleSet(FrameSlot slot, double value);
}
Loading