Skip to content

Commit

Permalink
Moving LocalScope and FramePointer closer to compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Nov 1, 2023
1 parent a88e1ef commit 3b59881
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.enso.compiler.core.ir.expression.errors
import org.enso.compiler.core.ir.module.scope.definition
import org.enso.compiler.pass.PassManager
import org.enso.compiler.test.CompilerTest
import org.enso.interpreter.runtime.scope.LocalScope
import org.enso.compiler.context.LocalScope
import org.enso.text.buffer.Rope
import org.enso.text.editing.JavaEditorAdapter
import org.enso.text.editing.model.{Position, Range, TextEdit}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package org.enso.interpreter.instrument;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.debug.DebuggerTags;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.EventContext;
import com.oracle.truffle.api.instrumentation.ExecutionEventNode;
import com.oracle.truffle.api.instrumentation.Instrumenter;
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
import com.oracle.truffle.api.interop.InteropLibrary;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.enso.compiler.context.FramePointer;
import org.enso.interpreter.node.expression.builtin.debug.DebugBreakpointNode;
import org.enso.interpreter.node.expression.builtin.text.util.ToJavaStringNode;
import org.enso.interpreter.node.expression.debug.CaptureResultScopeNode;
Expand All @@ -25,14 +15,27 @@
import org.enso.interpreter.runtime.callable.CallerInfo;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.scope.FramePointer;
import org.enso.interpreter.runtime.state.State;
import org.enso.polyglot.debugger.DebugServerInfo;
import org.graalvm.options.OptionDescriptor;
import org.graalvm.options.OptionDescriptors;
import org.graalvm.options.OptionKey;
import org.graalvm.polyglot.io.MessageEndpoint;
import org.graalvm.polyglot.io.MessageTransport;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.debug.DebuggerTags;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.EventContext;
import com.oracle.truffle.api.instrumentation.ExecutionEventNode;
import com.oracle.truffle.api.instrumentation.Instrumenter;
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
import com.oracle.truffle.api.interop.InteropLibrary;

import scala.util.Either;
import scala.util.Left;
import scala.util.Right;
Expand Down Expand Up @@ -105,12 +108,12 @@ private ReplExecutionEventNodeImpl(
}

private Object getValue(MaterializedFrame frame, FramePointer ptr) {
return getProperFrame(frame, ptr).getValue(ptr.getFrameSlotIdx());
return getProperFrame(frame, ptr).getValue(ptr.frameSlotIdx());
}

private MaterializedFrame getProperFrame(MaterializedFrame frame, FramePointer ptr) {
MaterializedFrame currentFrame = frame;
for (int i = 0; i < ptr.getParentLevel(); i++) {
for (int i = 0; i < ptr.parentLevel(); i++) {
currentFrame = Function.ArgumentsHelper.getLocalScope(currentFrame.getArguments());
}
return currentFrame;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.enso.compiler.context;

/**
* A representation of a pointer into a stack frame at a given number of levels above the current.
*/
public record FramePointer(int parentLevel, int frameSlotIdx) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.enso.interpreter.node.ProgramRootNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.IrToTruffle;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.runtime.state.ExecutionEnvironment;
import org.enso.interpreter.runtime.tag.AvoidIdInstrumentationTag;
import org.enso.interpreter.runtime.tag.IdentifiedTag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.source.SourceSection;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.util.ScalaConversions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.util.function.Supplier;

import org.enso.compiler.core.CompilerError;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;

import com.oracle.truffle.api.CompilerDirectives;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.NodeInfo;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.node.EnsoRootNode;
import org.enso.interpreter.runtime.callable.CallerInfo;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.NodeInfo;
import org.enso.compiler.context.InlineContext;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.Constants;
import org.enso.interpreter.node.BaseNode;
import org.enso.interpreter.node.ClosureRootNode;
Expand All @@ -18,7 +19,6 @@
import org.enso.interpreter.runtime.callable.CallerInfo;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.state.State;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.NodeInfo;
import org.enso.compiler.context.FramePointer;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.scope.FramePointer;

/**
* Reads from a local target (variable or call target).
Expand Down Expand Up @@ -45,10 +45,10 @@ public static ReadLocalVariableNode build(FramePointer pointer) {
*/
@Specialization(rewriteOn = FrameSlotTypeException.class)
protected long readLong(VirtualFrame frame) throws FrameSlotTypeException {
if (getFramePointer().getParentLevel() == 0)
return frame.getLong(getFramePointer().getFrameSlotIdx());
if (getFramePointer().parentLevel() == 0)
return frame.getLong(getFramePointer().frameSlotIdx());
MaterializedFrame currentFrame = getProperFrame(frame);
return currentFrame.getLong(getFramePointer().getFrameSlotIdx());
return currentFrame.getLong(getFramePointer().frameSlotIdx());
}

/**
Expand All @@ -61,18 +61,18 @@ protected long readLong(VirtualFrame frame) throws FrameSlotTypeException {
*/
@Specialization(rewriteOn = FrameSlotTypeException.class)
protected Object readGeneric(VirtualFrame frame) throws FrameSlotTypeException {
if (getFramePointer().getParentLevel() == 0)
return frame.getObject(getFramePointer().getFrameSlotIdx());
if (getFramePointer().parentLevel() == 0)
return frame.getObject(getFramePointer().frameSlotIdx());
MaterializedFrame currentFrame = getProperFrame(frame);
return currentFrame.getObject(getFramePointer().getFrameSlotIdx());
return currentFrame.getObject(getFramePointer().frameSlotIdx());
}

@Specialization
protected Object readGenericValue(VirtualFrame frame) {
if (getFramePointer().getParentLevel() == 0)
return frame.getValue(getFramePointer().getFrameSlotIdx());
if (getFramePointer().parentLevel() == 0)
return frame.getValue(getFramePointer().frameSlotIdx());
MaterializedFrame currentFrame = getProperFrame(frame);
return currentFrame.getValue(getFramePointer().getFrameSlotIdx());
return currentFrame.getValue(getFramePointer().frameSlotIdx());
}

/**
Expand All @@ -97,7 +97,7 @@ public MaterializedFrame getParentFrame(Frame frame) {
@ExplodeLoop
public MaterializedFrame getProperFrame(Frame frame) {
MaterializedFrame currentFrame = getParentFrame(frame);
for (int i = 1; i < getFramePointer().getParentLevel(); i++) {
for (int i = 1; i < getFramePointer().parentLevel(); i++) {
currentFrame = getParentFrame(currentFrame);
}
return currentFrame;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.WeakHashMap;
import java.util.logging.Level;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.context.LocalScope;
import org.enso.compiler.context.SimpleUpdate;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.Expression;
Expand All @@ -37,7 +38,6 @@
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.type.Types;
import org.enso.pkg.Package;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.enso.interpreter.runtime.callable;

import com.oracle.truffle.api.frame.MaterializedFrame;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.oracle.truffle.api.source.SourceSection;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.node.ClosureRootNode;
import org.enso.interpreter.node.ExpressionNode;
Expand All @@ -29,7 +30,6 @@
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.pkg.QualifiedName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.enso.compiler.context.FramePointer;
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.node.EnsoRootNode;
import org.enso.interpreter.runtime.callable.function.Function;
Expand Down Expand Up @@ -100,17 +101,17 @@ private static List<List<String>> gatherBindingsByLevels(Map<String, FramePointe

int maxParentLevel =
bindings.values().stream()
.max(Comparator.comparingInt(FramePointer::getParentLevel))
.max(Comparator.comparingInt(FramePointer::parentLevel))
.orElseThrow()
.getParentLevel();
.parentLevel();

// Get all binding names for a particular parent level
List<List<String>> bindingsByLevels = new ArrayList<>(maxParentLevel + 1);
for (int level = 0; level < maxParentLevel + 1; level++) {
final int finalLevel = level;
List<String> levelBindings =
bindings.entrySet().stream()
.filter(entry -> entry.getValue().getParentLevel() == finalLevel)
.filter(entry -> entry.getValue().parentLevel() == finalLevel)
.map(Entry::getKey)
.collect(Collectors.toList());
bindingsByLevels.add(levelBindings);
Expand Down Expand Up @@ -253,18 +254,18 @@ public String toString() {
}

private Object getValue(MaterializedFrame frame, FramePointer ptr) {
return ptr == null ? null : getProperFrame(frame, ptr).getValue(ptr.getFrameSlotIdx());
return ptr == null ? null : getProperFrame(frame, ptr).getValue(ptr.frameSlotIdx());
}

private void setValue(MaterializedFrame frame, FramePointer ptr, Object value) {
assert ptr != null;
MaterializedFrame properFrame = getProperFrame(frame, ptr);
properFrame.setObject(ptr.getFrameSlotIdx(), value);
properFrame.setObject(ptr.frameSlotIdx(), value);
}

private MaterializedFrame getProperFrame(MaterializedFrame frame, FramePointer ptr) {
MaterializedFrame currentFrame = frame;
for (int i = 0; i < ptr.getParentLevel(); i++) {
for (int i = 0; i < ptr.parentLevel(); i++) {
currentFrame = Function.ArgumentsHelper.getLocalScope(currentFrame.getArguments());
}
return currentFrame;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.enso.compiler.context

import org.enso.compiler.PackageRepository
import org.enso.compiler.context.LocalScope
import org.enso.compiler.data.CompilerConfig
import org.enso.compiler.pass.PassConfiguration
import org.enso.interpreter.runtime.scope.LocalScope

/** A type containing the information about the execution context for an inline
* expression.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.enso.interpreter.runtime.scope
package org.enso.compiler.context

import org.enso.compiler.pass.analyse.AliasAnalysis.Graph
import org.enso.compiler.pass.analyse.AliasAnalysis.Graph.{
Expand Down Expand Up @@ -158,8 +158,7 @@ class LocalScope(
}
object LocalScope {

/** Constructs a local scope for an
* [[org.enso.interpreter.node.EnsoRootNode]].
/** Constructs a local scope for an [[EnsoRootNode]].
*
* @return a defaulted local scope
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.enso.interpreter.runtime
import com.oracle.truffle.api.source.{Source, SourceSection}
import com.oracle.truffle.api.interop.InteropLibrary
import org.enso.compiler.context.CompilerContext
import org.enso.compiler.context.FramePointer
import org.enso.compiler.context.LocalScope
import org.enso.compiler.core.CompilerError
import org.enso.compiler.core.ConstantsNames
import org.enso.compiler.core.Implicits.AsMetadata
Expand Down Expand Up @@ -104,11 +106,7 @@ import org.enso.interpreter.runtime.callable.{
}
import org.enso.interpreter.runtime.data.Type
import org.enso.interpreter.runtime.data.text.Text
import org.enso.interpreter.runtime.scope.{
FramePointer,
LocalScope,
ModuleScope
}
import org.enso.interpreter.runtime.scope.{ModuleScope}
import org.enso.interpreter.{Constants, EnsoLanguage}

import java.math.BigInteger
Expand Down
Loading

0 comments on commit 3b59881

Please sign in to comment.