Skip to content

Commit

Permalink
Cannot cache AtomConstructor in Node. Moving to Context.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Apr 14, 2022
1 parent 94221c2 commit e134001
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.enso.interpreter.node.callable;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.*;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
Expand All @@ -13,7 +11,6 @@
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.source.SourceSection;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.locks.Lock;

Expand All @@ -22,11 +19,8 @@
import org.enso.interpreter.node.callable.resolver.*;
import org.enso.interpreter.node.callable.thunk.ThunkExecutorNode;
import org.enso.interpreter.runtime.Context;
import org.enso.interpreter.runtime.Module;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
import org.enso.interpreter.runtime.callable.argument.CallArgumentInfo;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.data.ArrayRope;
import org.enso.interpreter.runtime.data.text.Text;
Expand Down Expand Up @@ -251,11 +245,11 @@ Stateful doConvertDate(
Object[] arguments,
@CachedLibrary(limit = "10") MethodDispatchLibrary methods,
@CachedLibrary(limit = "1") MethodDispatchLibrary dateDispatch,
@CachedLibrary(limit = "10") InteropLibrary interop,
@Cached("wrapDateInAtom(interop)") WrapDateInAtom wrapDate
@CachedLibrary(limit = "10") InteropLibrary interop
) {
try {
Atom date = wrapDate.newDate(_this);
var dateConstructor = Context.get(this).getDateConstructor();
Object date = dateConstructor.isPresent() ? dateConstructor.get().newInstance(_this) : _this;
Function function = dateDispatch.getFunctionalDispatch(date, symbol);
arguments[0] = date;
return invokeFunctionNode.execute(function, frame, state, arguments);
Expand All @@ -265,37 +259,6 @@ Stateful doConvertDate(
}
}

static final class WrapDateInAtom extends Node {
private final AtomConstructor dateConstructor;

WrapDateInAtom(AtomConstructor dateConstructor) {
this.dateConstructor = dateConstructor;
}

Atom newDate(Object date) {
return dateConstructor.newInstance(date);
}
}

final WrapDateInAtom wrapDateInAtom(InteropLibrary iop) {
CompilerAsserts.neverPartOfCompilation();
final Context ctx = Context.get(this);
ctx.ensureModuleIsLoaded("Standard.Base.Data.Time.Date");
Optional<Module> dateModule = ctx.findModule("Standard.Base.Data.Time.Date");
if (dateModule.isPresent()) {
try {
Object constrDate = iop.invokeMember(dateModule.get(), "get_constructor", "Date");
if (constrDate instanceof AtomConstructor) {
AtomConstructor c = (AtomConstructor) constrDate;
return new WrapDateInAtom(c);
}
} catch (InteropException ex) {
throw new IllegalStateException("Impossible, _this is guaranteed to be a string.");
}
}
return null;
}

@Specialization(
guards = {
"!methods.hasFunctionalDispatch(_this)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.enso.interpreter.runtime;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.compiler.Compiler;
import org.enso.compiler.PackageRepository;
Expand Down Expand Up @@ -64,6 +67,8 @@ public class Context {
private final DistributionManager distributionManager;
private final LockManager lockManager;
private final AtomicLong clock = new AtomicLong();
@CompilerDirectives.CompilationFinal
private Optional<AtomConstructor> date;

/**
* Creates a new Enso context.
Expand Down Expand Up @@ -463,4 +468,31 @@ public TruffleLogger getLogger(Class<?> klass) {
public long clockTick() {
return clock.getAndIncrement();
}

/** Return the {@code Standard.Base.Data.Time.Date} constructor.
*
* @return optional with {@link AtomConstructor} for the date, if it can be found
*/
public Optional<AtomConstructor> getDateConstructor() {
if (date == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
ensureModuleIsLoaded("Standard.Base.Data.Time.Date");
Optional<Module> dateModule = findModule("Standard.Base.Data.Time.Date");
if (dateModule.isPresent()) {
try {
InteropLibrary iop = InteropLibrary.getUncached();
Object constrDate = iop.invokeMember(dateModule.get(), "get_constructor", "Date");
if (constrDate instanceof AtomConstructor) {
date = Optional.of((AtomConstructor) constrDate);
}
} catch (InteropException ex) {
throw new IllegalStateException(ex);
}
if (date == null) {
date = Optional.empty();
}
}
}
return date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,22 @@ boolean hasFunctionalDispatch() {
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
boolean isDate() {
if ("Standard.Base.Data.Time.Date.Date".equals(constructor.getQualifiedName().toString())) {
return true;
boolean isDate(
@CachedLibrary("this") InteropLibrary iop
) {
var dateConstructor = Context.get(iop).getDateConstructor();
if (dateConstructor.isPresent()) {
return dateConstructor.get() == this.constructor;
} else {
return false;
}
return false;
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
LocalDate asDate() throws UnsupportedMessageException {
if (!isDate()) {
throw UnsupportedMessageException.create();
}
return InteropLibrary.getUncached().asDate(fields[0]);
LocalDate asDate(
@CachedLibrary(limit = "3") InteropLibrary iop
) throws UnsupportedMessageException {
return iop.asDate(fields[0]);
}

@ExportMessage
Expand Down

0 comments on commit e134001

Please sign in to comment.