Skip to content

Commit e6bef8f

Browse files
author
Elia Trachsel
committed
[GR-70543] Use getIntConstant in EspressoLibs
PullRequest: graal/22334
2 parents d9161d8 + 3e87d40 commit e6bef8f

File tree

3 files changed

+68
-58
lines changed

3 files changed

+68
-58
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/io/TruffleIO.java

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@
7070
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7171
import com.oracle.truffle.api.TruffleFile;
7272
import com.oracle.truffle.api.TruffleLanguage;
73-
import com.oracle.truffle.espresso.classfile.descriptors.Name;
74-
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
7573
import com.oracle.truffle.espresso.descriptors.EspressoSymbols;
7674
import com.oracle.truffle.espresso.descriptors.EspressoSymbols.Names;
7775
import com.oracle.truffle.espresso.descriptors.EspressoSymbols.Signatures;
@@ -81,7 +79,6 @@
8179
import com.oracle.truffle.espresso.impl.Method;
8280
import com.oracle.truffle.espresso.impl.ObjectKlass;
8381
import com.oracle.truffle.espresso.libs.LibsState;
84-
import com.oracle.truffle.espresso.meta.EspressoError;
8582
import com.oracle.truffle.espresso.meta.Meta;
8683
import com.oracle.truffle.espresso.runtime.EspressoContext;
8784
import com.oracle.truffle.espresso.runtime.OS;
@@ -143,10 +140,10 @@ public final class TruffleIO implements ContextAccess {
143140
public final Method sun_nio_fs_DefaultFileSystemProvider_instance;
144141

145142
public final ObjectKlass sun_nio_fs_FileAttributeParser;
146-
@CompilationFinal public FileAttributeParser_Sync fileAttributeParserSync;
143+
public final FileAttributeParser_Sync fileAttributeParserSync;
147144

148145
public final ObjectKlass sun_nio_ch_FileChannelImpl;
149-
@CompilationFinal public FileChannelImpl_Sync fileChannelImplSync;
146+
public final FileChannelImpl_Sync fileChannelImplSync;
150147

151148
public final ObjectKlass sun_nio_ch_IOStatus;
152149
public final IOStatus_Sync ioStatusSync;
@@ -158,7 +155,7 @@ public final class TruffleIO implements ContextAccess {
158155
public final InetAddressResolver_LookupPolicy_Sync inetAddressResolverLookupPolicySync;
159156

160157
public final ObjectKlass sun_nio_ch_Net;
161-
@CompilationFinal public Net_ShutFlags_Sync netShutFlagsSync;
158+
public final Net_ShutFlags_Sync netShutFlagsSync;
162159

163160
// Checkstyle: resume field name check
164161

@@ -1121,11 +1118,13 @@ public TruffleIO(EspressoContext context) {
11211118
sun_nio_fs_DefaultFileSystemProvider_instance = sun_nio_fs_DefaultFileSystemProvider.requireDeclaredMethod(Names.instance, Signatures.sun_nio_fs_TruffleFileSystemProvider);
11221119

11231120
sun_nio_fs_FileAttributeParser = meta.knownKlass(EspressoSymbols.Types.sun_nio_fs_FileAttributeParser);
1121+
this.fileAttributeParserSync = new FileAttributeParser_Sync(this);
11241122

11251123
sun_nio_ch_IOStatus = meta.knownKlass(EspressoSymbols.Types.sun_nio_ch_IOStatus);
11261124
ioStatusSync = new IOStatus_Sync(this);
11271125

11281126
sun_nio_ch_FileChannelImpl = meta.knownKlass(EspressoSymbols.Types.sun_nio_ch_FileChannelImpl);
1127+
this.fileChannelImplSync = new FileChannelImpl_Sync(this);
11291128

11301129
sun_nio_fs_TrufflePath = meta.knownKlass(Types.sun_nio_fs_TrufflePath);
11311130
sun_nio_fs_TrufflePath_HIDDEN_TRUFFLE_FILE = sun_nio_fs_TrufflePath.requireHiddenField(Names.HIDDEN_TRUFFLE_FILE);
@@ -1137,19 +1136,11 @@ public TruffleIO(EspressoContext context) {
11371136
inetAddressResolverLookupPolicySync = new InetAddressResolver_LookupPolicy_Sync(this);
11381137

11391138
sun_nio_ch_Net = meta.knownKlass(Types.sun_nio_ch_Net);
1139+
netShutFlagsSync = new Net_ShutFlags_Sync(this);
11401140

11411141
setEnv(context.getEnv());
11421142
}
11431143

1144-
/**
1145-
* See {@link Meta#postSystemInit()}.
1146-
*/
1147-
public void postSystemInit() {
1148-
this.fileAttributeParserSync = new FileAttributeParser_Sync(this);
1149-
this.fileChannelImplSync = new FileChannelImpl_Sync(this);
1150-
netShutFlagsSync = new Net_ShutFlags_Sync(this);
1151-
}
1152-
11531144
private void setEnv(TruffleLanguage.Env env) {
11541145
synchronized (files) {
11551146
files.get(FD_STDIN).setNewChannel(new DetachOnCloseInputStream(env.in()));
@@ -1391,13 +1382,6 @@ private SeekableByteChannel getSeekableChannel(int fd) {
13911382
throw Throw.throwNonSeekable(context);
13921383
}
13931384

1394-
private static int lookupSyncedValue(ObjectKlass klass, Symbol<Name> constant) {
1395-
Field f = klass.lookupDeclaredField(constant, Types._int);
1396-
EspressoError.guarantee(f != null, "Failed to sync " + klass.getExternalName() + " constants");
1397-
assert f.isStatic();
1398-
return f.getInt(klass.tryInitializeAndGetStatics());
1399-
}
1400-
14011385
// Checkstyle: stop field name check
14021386
public static final class RAF_Sync {
14031387
public final int O_RDONLY;
@@ -1407,11 +1391,11 @@ public static final class RAF_Sync {
14071391
public final int O_TEMPORARY;
14081392

14091393
public RAF_Sync(TruffleIO io) {
1410-
this.O_RDONLY = lookupSyncedValue(io.java_io_RandomAccessFile, Names.O_RDONLY);
1411-
this.O_RDWR = lookupSyncedValue(io.java_io_RandomAccessFile, Names.O_RDWR);
1412-
this.O_SYNC = lookupSyncedValue(io.java_io_RandomAccessFile, Names.O_SYNC);
1413-
this.O_DSYNC = lookupSyncedValue(io.java_io_RandomAccessFile, Names.O_DSYNC);
1414-
this.O_TEMPORARY = lookupSyncedValue(io.java_io_RandomAccessFile, Names.O_TEMPORARY);
1394+
this.O_RDONLY = Meta.getIntConstant(io.java_io_RandomAccessFile, Names.O_RDONLY);
1395+
this.O_RDWR = Meta.getIntConstant(io.java_io_RandomAccessFile, Names.O_RDWR);
1396+
this.O_SYNC = Meta.getIntConstant(io.java_io_RandomAccessFile, Names.O_SYNC);
1397+
this.O_DSYNC = Meta.getIntConstant(io.java_io_RandomAccessFile, Names.O_DSYNC);
1398+
this.O_TEMPORARY = Meta.getIntConstant(io.java_io_RandomAccessFile, Names.O_TEMPORARY);
14151399
}
14161400
}
14171401

@@ -1426,13 +1410,13 @@ public static final class FileSystem_Sync {
14261410
public final int ACCESS_EXECUTE;
14271411

14281412
public FileSystem_Sync(TruffleIO io) {
1429-
this.BA_EXISTS = lookupSyncedValue(io.java_io_FileSystem, Names.BA_EXISTS);
1430-
this.BA_REGULAR = lookupSyncedValue(io.java_io_FileSystem, Names.BA_REGULAR);
1431-
this.BA_DIRECTORY = lookupSyncedValue(io.java_io_FileSystem, Names.BA_DIRECTORY);
1432-
this.BA_HIDDEN = lookupSyncedValue(io.java_io_FileSystem, Names.BA_HIDDEN);
1433-
this.ACCESS_READ = lookupSyncedValue(io.java_io_FileSystem, Names.ACCESS_READ);
1434-
this.ACCESS_WRITE = lookupSyncedValue(io.java_io_FileSystem, Names.ACCESS_WRITE);
1435-
this.ACCESS_EXECUTE = lookupSyncedValue(io.java_io_FileSystem, Names.ACCESS_EXECUTE);
1413+
this.BA_EXISTS = Meta.getIntConstant(io.java_io_FileSystem, Names.BA_EXISTS);
1414+
this.BA_REGULAR = Meta.getIntConstant(io.java_io_FileSystem, Names.BA_REGULAR);
1415+
this.BA_DIRECTORY = Meta.getIntConstant(io.java_io_FileSystem, Names.BA_DIRECTORY);
1416+
this.BA_HIDDEN = Meta.getIntConstant(io.java_io_FileSystem, Names.BA_HIDDEN);
1417+
this.ACCESS_READ = Meta.getIntConstant(io.java_io_FileSystem, Names.ACCESS_READ);
1418+
this.ACCESS_WRITE = Meta.getIntConstant(io.java_io_FileSystem, Names.ACCESS_WRITE);
1419+
this.ACCESS_EXECUTE = Meta.getIntConstant(io.java_io_FileSystem, Names.ACCESS_EXECUTE);
14361420
}
14371421
}
14381422

@@ -1448,23 +1432,25 @@ public static final class FileAttributeParser_Sync {
14481432
public final int OTHERS_EXECUTE_VALUE;
14491433

14501434
public FileAttributeParser_Sync(TruffleIO io) {
1451-
this.OWNER_READ_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.OWNER_READ_VALUE);
1452-
this.OWNER_WRITE_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.OWNER_WRITE_VALUE);
1453-
this.OWNER_EXECUTE_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.OWNER_EXECUTE_VALUE);
1454-
this.GROUP_READ_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.GROUP_READ_VALUE);
1455-
this.GROUP_WRITE_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.GROUP_WRITE_VALUE);
1456-
this.GROUP_EXECUTE_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.GROUP_EXECUTE_VALUE);
1457-
this.OTHERS_READ_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.OTHERS_READ_VALUE);
1458-
this.OTHERS_WRITE_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.OTHERS_WRITE_VALUE);
1459-
this.OTHERS_EXECUTE_VALUE = lookupSyncedValue(io.sun_nio_fs_FileAttributeParser, Names.OTHERS_EXECUTE_VALUE);
1435+
// if this would fail we would need to load the class at post system init.
1436+
this.OWNER_READ_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.OWNER_READ_VALUE, false);
1437+
this.OWNER_WRITE_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.OWNER_WRITE_VALUE, false);
1438+
this.OWNER_EXECUTE_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.OWNER_EXECUTE_VALUE, false);
1439+
this.GROUP_READ_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.GROUP_READ_VALUE, false);
1440+
this.GROUP_WRITE_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.GROUP_WRITE_VALUE, false);
1441+
this.GROUP_EXECUTE_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.GROUP_EXECUTE_VALUE, false);
1442+
this.OTHERS_READ_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.OTHERS_READ_VALUE, false);
1443+
this.OTHERS_WRITE_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.OTHERS_WRITE_VALUE, false);
1444+
this.OTHERS_EXECUTE_VALUE = Meta.getIntConstant(io.sun_nio_fs_FileAttributeParser, Names.OTHERS_EXECUTE_VALUE, false);
14601445
}
14611446
}
14621447

14631448
public static final class FileChannelImpl_Sync {
14641449
public final int MAP_RW;
14651450

14661451
public FileChannelImpl_Sync(TruffleIO io) {
1467-
this.MAP_RW = lookupSyncedValue(io.sun_nio_ch_FileChannelImpl, Names.MAP_RW);
1452+
// if this would fail we would need to load the class at post system init.
1453+
this.MAP_RW = Meta.getIntConstant(io.sun_nio_ch_FileChannelImpl, Names.MAP_RW, false);
14681454
}
14691455
}
14701456

@@ -1477,12 +1463,12 @@ public static final class IOStatus_Sync {
14771463
public final int UNSUPPORTED_CASE;
14781464

14791465
public IOStatus_Sync(TruffleIO io) {
1480-
this.EOF = lookupSyncedValue(io.sun_nio_ch_IOStatus, Names.EOF);
1481-
this.UNAVAILABLE = lookupSyncedValue(io.sun_nio_ch_IOStatus, Names.UNAVAILABLE);
1482-
this.INTERRUPTED = lookupSyncedValue(io.sun_nio_ch_IOStatus, Names.INTERRUPTED);
1483-
this.UNSUPPORTED = lookupSyncedValue(io.sun_nio_ch_IOStatus, Names.UNSUPPORTED);
1484-
this.THROWN = lookupSyncedValue(io.sun_nio_ch_IOStatus, Names.THROWN);
1485-
this.UNSUPPORTED_CASE = lookupSyncedValue(io.sun_nio_ch_IOStatus, Names.UNSUPPORTED_CASE);
1466+
this.EOF = Meta.getIntConstant(io.sun_nio_ch_IOStatus, Names.EOF);
1467+
this.UNAVAILABLE = Meta.getIntConstant(io.sun_nio_ch_IOStatus, Names.UNAVAILABLE);
1468+
this.INTERRUPTED = Meta.getIntConstant(io.sun_nio_ch_IOStatus, Names.INTERRUPTED);
1469+
this.UNSUPPORTED = Meta.getIntConstant(io.sun_nio_ch_IOStatus, Names.UNSUPPORTED);
1470+
this.THROWN = Meta.getIntConstant(io.sun_nio_ch_IOStatus, Names.THROWN);
1471+
this.UNSUPPORTED_CASE = Meta.getIntConstant(io.sun_nio_ch_IOStatus, Names.UNSUPPORTED_CASE);
14861472
}
14871473
}
14881474

@@ -1493,10 +1479,10 @@ public static final class InetAddressResolver_LookupPolicy_Sync {
14931479
public final int IPV6_FIRST;
14941480

14951481
public InetAddressResolver_LookupPolicy_Sync(TruffleIO io) {
1496-
this.IPV4 = lookupSyncedValue(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV4);
1497-
this.IPV6 = lookupSyncedValue(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV6);
1498-
this.IPV4_FIRST = lookupSyncedValue(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV4_FIRST);
1499-
this.IPV6_FIRST = lookupSyncedValue(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV6_FIRST);
1482+
this.IPV4 = Meta.getIntConstant(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV4);
1483+
this.IPV6 = Meta.getIntConstant(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV6);
1484+
this.IPV4_FIRST = Meta.getIntConstant(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV4_FIRST);
1485+
this.IPV6_FIRST = Meta.getIntConstant(io.java_net_spi_InetAddressResolver$LookupPolicy, Names.IPV6_FIRST);
15001486
}
15011487
}
15021488

@@ -1506,9 +1492,10 @@ public static final class Net_ShutFlags_Sync {
15061492
public final int SHUT_RDWR;
15071493

15081494
public Net_ShutFlags_Sync(TruffleIO io) {
1509-
this.SHUT_RD = lookupSyncedValue(io.sun_nio_ch_Net, Names.SHUT_RD);
1510-
this.SHUT_WR = lookupSyncedValue(io.sun_nio_ch_Net, Names.SHUT_WR);
1511-
this.SHUT_RDWR = lookupSyncedValue(io.sun_nio_ch_Net, Names.SHUT_RDWR);
1495+
// if this would fail we would need to load the class at post system init.
1496+
this.SHUT_RD = Meta.getIntConstant(io.sun_nio_ch_Net, Names.SHUT_RD, false);
1497+
this.SHUT_WR = Meta.getIntConstant(io.sun_nio_ch_Net, Names.SHUT_WR, false);
1498+
this.SHUT_RDWR = Meta.getIntConstant(io.sun_nio_ch_Net, Names.SHUT_RDWR, false);
15121499
}
15131500
}
15141501
// Checkstyle: resume field name check

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/meta/Meta.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,7 +3015,28 @@ public Klass resolveSymbolAndAccessCheck(Symbol<Type> type, ObjectKlass accessin
30153015
return klass;
30163016
}
30173017

3018+
/**
3019+
* Works as specified by {@link Meta#getIntConstant(ObjectKlass, Symbol, boolean)} with
3020+
* allowClassInit set to true.
3021+
*/
30183022
public static int getIntConstant(ObjectKlass klass, Symbol<Name> constant) {
3023+
return getIntConstant(klass, constant, true);
3024+
}
3025+
3026+
/**
3027+
* Retrieves the int constant from the given guest class. Used for synchronizing constants
3028+
* between the host and guest world.
3029+
* </p>
3030+
* First the method tries to retrieve the constant from the constantPool (which does not trigger
3031+
* class initialization). If this fails and allowClassInit is true, it triggers class
3032+
* initialization and gets the constant from the loaded class.
3033+
*
3034+
* @param klass the guest class which has the constant as a field.
3035+
* @param constant the symbol of the int constant to retrieve.
3036+
* @param allowClassInit whether to allow class initialization
3037+
* @return the int constant
3038+
*/
3039+
public static int getIntConstant(ObjectKlass klass, Symbol<Name> constant, boolean allowClassInit) {
30193040
Field f = klass.lookupDeclaredField(constant, Types._int);
30203041
if (f == null || !f.isStatic() || !f.isFinalFlagSet()) {
30213042
throw EspressoError.fatal("Cannot find " + constant + " int constant in class " + klass.getName());
@@ -3024,6 +3045,9 @@ public static int getIntConstant(ObjectKlass klass, Symbol<Name> constant) {
30243045
if (constantValueIndex != 0) {
30253046
return klass.getConstantPool().intAt(constantValueIndex);
30263047
}
3048+
if (!allowClassInit) {
3049+
throw EspressoError.shouldNotReachHere("Without classInit, cannot find " + constant + " int constant in class " + klass.getName());
3050+
}
30273051
return f.getInt(klass.tryInitializeAndGetStatics());
30283052
}
30293053

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/runtime/EspressoContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,6 @@ private void spawnVM() throws ContextPatchingException {
597597
meta.postSystemInit();
598598
if (language.useEspressoLibs()) {
599599
libsMeta.postSystemInit();
600-
truffleIO.postSystemInit();
601600
}
602601

603602
// class redefinition will be enabled if debug mode or if any redefine or retransform

0 commit comments

Comments
 (0)