Skip to content

Commit

Permalink
TRegex: fix implementation of AbstractConstantKeysObject.IsMemberRead…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
djoooooe committed Jul 21, 2022
1 parent d0bce49 commit 2d76333
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
package com.oracle.truffle.regex;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
Expand All @@ -54,6 +55,8 @@ public abstract class AbstractConstantKeysObject extends AbstractRegexObject {

public abstract TruffleReadOnlyKeysArray getKeys();

public abstract boolean isMemberReadableImpl(String symbol);

public abstract Object readMemberImpl(String symbol) throws UnknownIdentifierException;

@ExportMessage
Expand All @@ -70,25 +73,32 @@ public Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
public abstract static class IsMemberReadable {

@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "result"}, limit = "8")
@Specialization(guards = {"symbol == cachedSymbol", "isExact(receiver, cachedClass)", "result"}, limit = "8")
public static boolean cacheIdentity(AbstractConstantKeysObject receiver, String symbol,
@Cached("symbol") String cachedSymbol,
@Cached("isReadable(receiver, cachedSymbol)") boolean result) {
@Cached("receiver.getClass()") Class<?> cachedClass,
@Cached("receiver.isMemberReadableImpl(cachedSymbol)") boolean result) {
return result;
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "result"}, limit = "8", replaces = "cacheIdentity")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "isExact(receiver, cachedClass)", "result"}, limit = "8", replaces = "cacheIdentity")
public static boolean cacheEquals(AbstractConstantKeysObject receiver, String symbol,
@Cached("symbol") String cachedSymbol,
@Cached("isReadable(receiver, cachedSymbol)") boolean result) {
@Cached("receiver.getClass()") Class<?> cachedClass,
@Cached("receiver.isMemberReadableImpl(cachedSymbol)") boolean result) {
return result;
}

@SuppressWarnings("unused")
@Specialization(replaces = "cacheEquals")
public static boolean isReadable(AbstractConstantKeysObject receiver, String symbol) {
return receiver.getKeys().contains(symbol);
public static boolean isReadable(AbstractConstantKeysObject receiver, String symbol,
@Cached("createClassProfile()") @Cached.Shared("classProfile") ValueProfile classProfile) {
return classProfile.profile(receiver).isMemberReadableImpl(symbol);
}

static boolean isExact(AbstractConstantKeysObject receiver, Class<?> cachedClass) {
return CompilerDirectives.isExact(receiver, cachedClass);
}
}

Expand All @@ -98,20 +108,20 @@ public abstract static class ReadMember {
@Specialization(guards = "symbol == cachedSymbol", limit = "8")
public static Object readIdentity(AbstractConstantKeysObject receiver, @SuppressWarnings("unused") String symbol,
@Cached("symbol") String cachedSymbol,
@Cached("createClassProfile()") ValueProfile classProfile) throws UnknownIdentifierException {
@Cached("createClassProfile()") @Cached.Exclusive ValueProfile classProfile) throws UnknownIdentifierException {
return read(receiver, cachedSymbol, classProfile);
}

@Specialization(guards = "symbol.equals(cachedSymbol)", limit = "8", replaces = "readIdentity")
public static Object readEquals(AbstractConstantKeysObject receiver, @SuppressWarnings("unused") String symbol,
@Cached("symbol") String cachedSymbol,
@Cached("createClassProfile()") ValueProfile classProfile) throws UnknownIdentifierException {
@Cached("createClassProfile()") @Cached.Exclusive ValueProfile classProfile) throws UnknownIdentifierException {
return read(receiver, cachedSymbol, classProfile);
}

@Specialization(replaces = "readEquals")
public static Object read(AbstractConstantKeysObject receiver, String symbol,
@Cached("createClassProfile()") ValueProfile classProfile) throws UnknownIdentifierException {
@Cached("createClassProfile()") @Cached.Shared("classProfile") ValueProfile classProfile) throws UnknownIdentifierException {
return classProfile.profile(receiver).readMemberImpl(symbol);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,24 @@
@ExportLibrary(InteropLibrary.class)
public final class RegexFlags extends AbstractConstantKeysObject implements JsonConvertible {

private static final TruffleReadOnlyKeysArray KEYS = new TruffleReadOnlyKeysArray("source", "ignoreCase", "multiline", "sticky", "global", "unicode", "dotAll", "hasIndices");
private static final String PROP_SOURCE = "source";
private static final String PROP_IGNORE_CASE = "ignoreCase";
private static final String PROP_MULTILINE = "multiline";
private static final String PROP_STICKY = "sticky";
private static final String PROP_GLOBAL = "global";
private static final String PROP_UNICODE = "unicode";
private static final String PROP_DOT_ALL = "dotAll";
private static final String PROP_HAS_INDICES = "hasIndices";

private static final TruffleReadOnlyKeysArray KEYS = new TruffleReadOnlyKeysArray(
PROP_SOURCE,
PROP_IGNORE_CASE,
PROP_MULTILINE,
PROP_STICKY,
PROP_GLOBAL,
PROP_UNICODE,
PROP_DOT_ALL,
PROP_HAS_INDICES);

private static final int NONE = 0;
private static final int IGNORE_CASE = 1;
Expand Down Expand Up @@ -183,38 +200,55 @@ public boolean equals(Object obj) {
@TruffleBoundary
@Override
public JsonValue toJson() {
return Json.obj(Json.prop("ignoreCase", isIgnoreCase()),
Json.prop("multiline", isMultiline()),
Json.prop("global", isGlobal()),
Json.prop("sticky", isSticky()),
Json.prop("unicode", isUnicode()),
Json.prop("dotAll", isDotAll()),
Json.prop("hasIndices", hasIndices()));
return Json.obj(Json.prop(PROP_IGNORE_CASE, isIgnoreCase()),
Json.prop(PROP_MULTILINE, isMultiline()),
Json.prop(PROP_GLOBAL, isGlobal()),
Json.prop(PROP_STICKY, isSticky()),
Json.prop(PROP_UNICODE, isUnicode()),
Json.prop(PROP_DOT_ALL, isDotAll()),
Json.prop(PROP_HAS_INDICES, hasIndices()));
}

@Override
public TruffleReadOnlyKeysArray getKeys() {
return KEYS;
}

@Override
public boolean isMemberReadableImpl(String symbol) {
switch (symbol) {
case PROP_SOURCE:
case PROP_IGNORE_CASE:
case PROP_MULTILINE:
case PROP_STICKY:
case PROP_GLOBAL:
case PROP_UNICODE:
case PROP_DOT_ALL:
case PROP_HAS_INDICES:
return true;
default:
return false;
}
}

@Override
public Object readMemberImpl(String symbol) throws UnknownIdentifierException {
switch (symbol) {
case "source":
case PROP_SOURCE:
return getSource();
case "ignoreCase":
case PROP_IGNORE_CASE:
return isIgnoreCase();
case "multiline":
case PROP_MULTILINE:
return isMultiline();
case "sticky":
case PROP_STICKY:
return isSticky();
case "global":
case PROP_GLOBAL:
return isGlobal();
case "unicode":
case PROP_UNICODE:
return isUnicode();
case "dotAll":
case PROP_DOT_ALL:
return isDotAll();
case "hasIndices":
case PROP_HAS_INDICES:
return hasIndices();
default:
CompilerDirectives.transferToInterpreterAndInvalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ public TruffleReadOnlyKeysArray getKeys() {
return KEYS;
}

@Override
public boolean isMemberReadableImpl(String symbol) {
switch (symbol) {
case PROP_EXEC:
case PROP_EXEC_BOOLEAN:
case PROP_EXEC_BYTES:
case PROP_PATTERN:
case PROP_FLAGS:
case PROP_GROUP_COUNT:
case PROP_GROUPS:
case PROP_IS_BACKTRACKING:
return true;
default:
return false;
}
}

@Override
public Object readMemberImpl(String symbol) throws UnknownIdentifierException {
switch (symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@ public TruffleReadOnlyKeysArray getKeys() {
return KEYS;
}

@Override
public boolean isMemberReadableImpl(String symbol) {
switch (symbol) {
case PROP_IS_MATCH:
case PROP_GET_START:
case PROP_GET_END:
case PROP_LAST_GROUP:
return true;
default:
return false;
}
}

@Override
public Object readMemberImpl(String symbol) throws UnknownIdentifierException {
switch (symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,23 @@
@ExportLibrary(InteropLibrary.class)
public final class PythonFlags extends AbstractConstantKeysObject {

private static final TruffleReadOnlyKeysArray KEYS = new TruffleReadOnlyKeysArray("ASCII", "DOTALL", "IGNORECASE", "LOCALE", "MULTILINE", "TEMPLATE", "UNICODE", "VERBOSE");
private static final String PROP_ASCII = "ASCII";
private static final String PROP_DOTALL = "DOTALL";
private static final String PROP_IGNORECASE = "IGNORECASE";
private static final String PROP_LOCALE = "LOCALE";
private static final String PROP_MULTILINE = "MULTILINE";
private static final String PROP_TEMPLATE = "TEMPLATE";
private static final String PROP_UNICODE = "UNICODE";
private static final String PROP_VERBOSE = "VERBOSE";
private static final TruffleReadOnlyKeysArray KEYS = new TruffleReadOnlyKeysArray(
PROP_ASCII,
PROP_DOTALL,
PROP_IGNORECASE,
PROP_LOCALE,
PROP_MULTILINE,
PROP_TEMPLATE,
PROP_UNICODE,
PROP_VERBOSE);

private final int value;

Expand Down Expand Up @@ -258,24 +274,41 @@ public TruffleReadOnlyKeysArray getKeys() {
return KEYS;
}

@Override
public boolean isMemberReadableImpl(String symbol) {
switch (symbol) {
case PROP_ASCII:
case PROP_DOTALL:
case PROP_IGNORECASE:
case PROP_LOCALE:
case PROP_MULTILINE:
case PROP_TEMPLATE:
case PROP_UNICODE:
case PROP_VERBOSE:
return true;
default:
return false;
}
}

@Override
public Object readMemberImpl(String symbol) throws UnknownIdentifierException {
switch (symbol) {
case "ASCII":
case PROP_ASCII:
return isAscii();
case "DOTALL":
case PROP_DOTALL:
return isDotAll();
case "IGNORECASE":
case PROP_IGNORECASE:
return isIgnoreCase();
case "LOCALE":
case PROP_LOCALE:
return isLocale();
case "MULTILINE":
case PROP_MULTILINE:
return isMultiLine();
case "TEMPLATE":
case PROP_TEMPLATE:
return isTemplate();
case "UNICODE":
case PROP_UNICODE:
return isUnicodeExplicitlySet();
case "VERBOSE":
case PROP_VERBOSE:
return isVerbose();
default:
CompilerDirectives.transferToInterpreterAndInvalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
@ExportLibrary(InteropLibrary.class)
public final class RubyFlags extends AbstractConstantKeysObject {

private static final TruffleReadOnlyKeysArray KEYS = new TruffleReadOnlyKeysArray("EXTENDED", "IGNORECASE", "MULTILINE");
private static final String PROP_EXTENDED = "EXTENDED";
private static final String PROP_IGNORECASE = "IGNORECASE";
private static final String PROP_MULTILINE = "MULTILINE";
private static final TruffleReadOnlyKeysArray KEYS = new TruffleReadOnlyKeysArray(PROP_EXTENDED, PROP_IGNORECASE, PROP_MULTILINE);

private final int value;
private final Mode mode;
Expand Down Expand Up @@ -194,14 +197,26 @@ public TruffleReadOnlyKeysArray getKeys() {
return KEYS;
}

@Override
public boolean isMemberReadableImpl(String symbol) {
switch (symbol) {
case PROP_EXTENDED:
case PROP_IGNORECASE:
case PROP_MULTILINE:
return true;
default:
return false;
}
}

@Override
public Object readMemberImpl(String symbol) throws UnknownIdentifierException {
switch (symbol) {
case "EXTENDED":
case PROP_EXTENDED:
return isExtended();
case "IGNORECASE":
case PROP_IGNORECASE:
return isIgnoreCase();
case "MULTILINE":
case PROP_MULTILINE:
return isMultiline();
default:
CompilerDirectives.transferToInterpreterAndInvalidate();
Expand Down

0 comments on commit 2d76333

Please sign in to comment.