From 6ca84ad4ccb1f135f5961acb014fb31228c70c6c Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Sat, 19 Oct 2024 13:04:51 +0200 Subject: [PATCH] Cleanups related to object and code headers --- .../test/AbstractSqueakTestCase.java | 2 +- .../test/SqueakMiscellaneousTest.java | 2 +- .../image/SqueakImageConstants.java | 30 ++++++++----------- .../AbstractSqueakObjectWithClassAndHash.java | 21 +++++++------ .../model/CompiledCodeObject.java | 25 +++++++--------- .../impl/MiscellaneousPrimitives.java | 2 +- .../hpi/swa/trufflesqueak/util/MiscUtils.java | 4 --- 7 files changed, 35 insertions(+), 51 deletions(-) diff --git a/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/AbstractSqueakTestCase.java b/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/AbstractSqueakTestCase.java index d8e35dff4..b26c8e062 100644 --- a/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/AbstractSqueakTestCase.java +++ b/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/AbstractSqueakTestCase.java @@ -50,7 +50,7 @@ protected static final CompiledCodeObject makeMethod(final long header, final Ob bytes[i] = (byte) intbytes[i]; } bytes[intbytes.length] = 0; // Set flagByte = 0 for no method trailer. - int numLiterals = (int) header & 0x7FFF; + final int numLiterals = (int) header & 0x7FFF; final Object[] allLiterals = Arrays.copyOf(literals, numLiterals); allLiterals[numLiterals - 2] = image.asByteString("DoIt"); // compiledInSelector allLiterals[numLiterals - 1] = nilClassBinding; // methodClassAssociation diff --git a/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/SqueakMiscellaneousTest.java b/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/SqueakMiscellaneousTest.java index 43ee7d8f6..49f317ebe 100644 --- a/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/SqueakMiscellaneousTest.java +++ b/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/SqueakMiscellaneousTest.java @@ -125,8 +125,8 @@ public void testSource() { @Test public void testSourceAllBytecodes() { - final long header = 17235971L; final Object[] literals = {image.asByteString("someSelector"), image.asByteString("someOtherSelector"), 63, nilClassBinding}; + final long header = makeHeader(0, 0, literals.length, false, true); final CompiledCodeObject code = makeMethod(header, literals, 139, 31, 0, 31, 32, 67, 96, 97, 98, 99, 103, 111, 112, 113, 114, 115, 116, diff --git a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/image/SqueakImageConstants.java b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/image/SqueakImageConstants.java index 2441eab7b..e0de45bfe 100644 --- a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/image/SqueakImageConstants.java +++ b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/image/SqueakImageConstants.java @@ -6,8 +6,6 @@ */ package de.hpi.swa.trufflesqueak.image; -import de.hpi.swa.trufflesqueak.util.MiscUtils; - public final class SqueakImageConstants { /** General. */ @@ -22,6 +20,7 @@ public final class SqueakImageConstants { /** Object Header. */ public static final long OVERFLOW_SLOTS = 255; public static final long SLOTS_MASK = 0xFFL << 56; + public static final int IDENTITY_HASH_HALF_WORD_MASK = (1 << 22) - 1; /** Object Header Tag Bits. */ public static final int NUM_TAG_BITS = 3; @@ -99,33 +98,28 @@ public static int classTableIndexFor(final int majorIndex, final int minorIndex) * */ public static final class ObjectHeader { - private static final int NUM_SLOTS_SIZE = 1 << 8; - public static final int HASH_AND_CLASS_INDEX_SIZE = 1 << 22; - private static final int FORMAT_SIZE = 1 << 5; - private static final int PINNED_BIT_SHIFT = 30; - - public static int getClassIndex(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 0, HASH_AND_CLASS_INDEX_SIZE); + public static int getClassIndex(final long header) { + return (int) header & CLASS_INDEX_MASK; } - public static int getFormat(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 24, FORMAT_SIZE); + public static int getFormat(final long header) { + return (int) (header >> 24) & 0x1f; } - public static int getHash(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 32, HASH_AND_CLASS_INDEX_SIZE); + public static int getHash(final long header) { + return (int) (header >> 32) & IDENTITY_HASH_HALF_WORD_MASK; } - public static int getNumSlots(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 56, NUM_SLOTS_SIZE); + public static int getNumSlots(final long header) { + return (int) (header >> 56) & 255; } - public static boolean isPinned(final long headerWord) { - return (headerWord >> PINNED_BIT_SHIFT & 1) == 1; + public static boolean isPinned(final long header) { + return (header >> 30 & 1) == 1; } public static long getHeader(final long numSlots, final long identityHash, final long format, final long classIndex) { - assert numSlots < NUM_SLOTS_SIZE && identityHash < HASH_AND_CLASS_INDEX_SIZE && format < FORMAT_SIZE && classIndex < HASH_AND_CLASS_INDEX_SIZE; + assert numSlots < 0x100 && identityHash < 0x400000 && format < 0x20 && classIndex < 0x400000; return numSlots << 56 | identityHash << 32 | format << 24 | classIndex; } diff --git a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/AbstractSqueakObjectWithClassAndHash.java b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/AbstractSqueakObjectWithClassAndHash.java index 1e15c7601..31333a8df 100644 --- a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/AbstractSqueakObjectWithClassAndHash.java +++ b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/AbstractSqueakObjectWithClassAndHash.java @@ -23,7 +23,6 @@ import de.hpi.swa.trufflesqueak.util.ObjectGraphUtils.ObjectTracer; public abstract class AbstractSqueakObjectWithClassAndHash extends AbstractSqueakObject { - public static final int SQUEAK_HASH_MASK = ObjectHeader.HASH_AND_CLASS_INDEX_SIZE - 1; private static final int MARK_BIT = 1 << 24; /* Generate new hash if hash is 0 (see SpurMemoryManager>>#hashBitsOf:). */ private static final int HASH_UNINITIALIZED = 0; @@ -38,7 +37,7 @@ public abstract class AbstractSqueakObjectWithClassAndHash extends AbstractSquea * can be represented by just one 64-bit word. */ private ClassObject squeakClass; - private int squeahHashAndBits; + private int squeakHashAndBits; // For special/well-known objects only. protected AbstractSqueakObjectWithClassAndHash() { @@ -46,7 +45,7 @@ protected AbstractSqueakObjectWithClassAndHash() { } protected AbstractSqueakObjectWithClassAndHash(final long header, final ClassObject klass) { - squeahHashAndBits = ObjectHeader.getHash(header); + squeakHashAndBits = ObjectHeader.getHash(header); squeakClass = klass; // mark bit zero when loading image } @@ -56,7 +55,7 @@ protected AbstractSqueakObjectWithClassAndHash(final SqueakImageContext image, f } private AbstractSqueakObjectWithClassAndHash(final boolean markingFlag, final ClassObject klass) { - squeahHashAndBits = AbstractSqueakObjectWithClassAndHash.HASH_UNINITIALIZED; + squeakHashAndBits = AbstractSqueakObjectWithClassAndHash.HASH_UNINITIALIZED; squeakClass = klass; if (markingFlag) { toggleMarkingFlag(); @@ -64,7 +63,7 @@ private AbstractSqueakObjectWithClassAndHash(final boolean markingFlag, final Cl } protected AbstractSqueakObjectWithClassAndHash(final AbstractSqueakObjectWithClassAndHash original) { - squeahHashAndBits = original.squeahHashAndBits; + squeakHashAndBits = original.squeakHashAndBits; setSqueakHash(HASH_UNINITIALIZED); squeakClass = original.squeakClass; } @@ -112,13 +111,13 @@ public final long getOrCreateSqueakHash(final InlinedBranchProfile needsHashProf if (needsSqueakHash()) { /** Lazily initialize squeakHash and derive value from hashCode. */ needsHashProfile.enter(node); - setSqueakHash(System.identityHashCode(this) & SQUEAK_HASH_MASK); + setSqueakHash(System.identityHashCode(this) & SqueakImageConstants.IDENTITY_HASH_HALF_WORD_MASK); } return getSqueakHash(); } public long getSqueakHash() { - return squeahHashAndBits & SQUEAK_HASH_MASK; + return squeakHashAndBits & SqueakImageConstants.IDENTITY_HASH_HALF_WORD_MASK; } public final boolean needsSqueakHash() { @@ -126,16 +125,16 @@ public final boolean needsSqueakHash() { } public final void setSqueakHash(final int newHash) { - assert newHash <= SQUEAK_HASH_MASK; - squeahHashAndBits = (squeahHashAndBits & ~SQUEAK_HASH_MASK) + newHash; + assert newHash <= SqueakImageConstants.IDENTITY_HASH_HALF_WORD_MASK; + squeakHashAndBits = (squeakHashAndBits & ~SqueakImageConstants.IDENTITY_HASH_HALF_WORD_MASK) + newHash; } public final boolean getMarkingFlag() { - return (squeahHashAndBits & MARK_BIT) != 0; + return (squeakHashAndBits & MARK_BIT) != 0; } private void toggleMarkingFlag() { - squeahHashAndBits ^= MARK_BIT; + squeakHashAndBits ^= MARK_BIT; } public final boolean isMarked(final boolean currentMarkingFlag) { diff --git a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/CompiledCodeObject.java b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/CompiledCodeObject.java index b1ce6d960..dfd977ce4 100644 --- a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/CompiledCodeObject.java +++ b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/model/CompiledCodeObject.java @@ -48,7 +48,6 @@ import de.hpi.swa.trufflesqueak.shared.SqueakLanguageConfig; import de.hpi.swa.trufflesqueak.util.ArrayUtils; import de.hpi.swa.trufflesqueak.util.FrameAccess; -import de.hpi.swa.trufflesqueak.util.MiscUtils; import de.hpi.swa.trufflesqueak.util.ObjectGraphUtils.ObjectTracer; import de.hpi.swa.trufflesqueak.util.UnsafeUtils; @@ -681,28 +680,24 @@ public CompiledCodeObject getMethodUnsafe() { * */ private static final class CompiledCodeHeaderDecoder { - private static final int NUM_LITERALS_SIZE = 1 << 15; - private static final int NUM_TEMPS_TEMPS_SIZE = 1 << 6; - private static final int NUM_ARGUMENTS_SIZE = 1 << 4; - - private static int getNumLiterals(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 0, NUM_LITERALS_SIZE); + private static int getNumLiterals(final long header) { + return (int) (header & 0x7FFF); } - private static boolean getHasPrimitive(final long headerWord) { - return (headerWord & 1 << 16) != 0; + private static boolean getHasPrimitive(final long header) { + return (header & 0x10000) != 0; } - private static boolean getNeedsLargeFrame(final long headerWord) { - return (headerWord & 1 << 17) != 0; + private static boolean getNeedsLargeFrame(final long header) { + return (header & 0x20000) != 0; } - private static int getNumTemps(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 18, NUM_TEMPS_TEMPS_SIZE); + private static int getNumTemps(final long header) { + return (int) (header >> 18) & 0x3F; } - private static int getNumArguments(final long headerWord) { - return MiscUtils.bitSplit(headerWord, 24, NUM_ARGUMENTS_SIZE); + private static int getNumArguments(final long header) { + return (int) (header >> 24) & 0x0F; } private static boolean getSignFlag(final long headerWord) { diff --git a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/primitives/impl/MiscellaneousPrimitives.java b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/primitives/impl/MiscellaneousPrimitives.java index ef7f00755..27eff9fa4 100644 --- a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/primitives/impl/MiscellaneousPrimitives.java +++ b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/primitives/impl/MiscellaneousPrimitives.java @@ -803,7 +803,7 @@ protected static final AbstractSqueakObject doCopy(final AbstractSqueakObjectWit public static final class PrimMaxIdentityHashNode extends AbstractSingletonPrimitiveNode { @Override public Object execute() { - return (long) AbstractSqueakObjectWithClassAndHash.SQUEAK_HASH_MASK; + return (long) SqueakImageConstants.IDENTITY_HASH_HALF_WORD_MASK; } } diff --git a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/util/MiscUtils.java b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/util/MiscUtils.java index 9a65e227c..9a6aae196 100644 --- a/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/util/MiscUtils.java +++ b/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/util/MiscUtils.java @@ -53,10 +53,6 @@ public final class MiscUtils { private MiscUtils() { } - public static int bitSplit(final long value, final int offset, final int size) { - return (int) (value >> offset & size - 1); - } - public static long clamp(final long value, final long min, final long max) { return Math.max(min, Math.min(value, max)); }