Skip to content

Commit

Permalink
Cleanups related to object and code headers
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 19, 2024
1 parent 49a46bc commit 6ca84ad
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package de.hpi.swa.trufflesqueak.image;

import de.hpi.swa.trufflesqueak.util.MiscUtils;

public final class SqueakImageConstants {

/** General. */
Expand All @@ -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;
Expand Down Expand Up @@ -99,33 +98,28 @@ public static int classTableIndexFor(final int majorIndex, final int minorIndex)
* </pre>
*/
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,15 +37,15 @@ 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() {
this(HASH_UNINITIALIZED, null);
}

protected AbstractSqueakObjectWithClassAndHash(final long header, final ClassObject klass) {
squeahHashAndBits = ObjectHeader.getHash(header);
squeakHashAndBits = ObjectHeader.getHash(header);
squeakClass = klass;
// mark bit zero when loading image
}
Expand All @@ -56,15 +55,15 @@ 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();
}
}

protected AbstractSqueakObjectWithClassAndHash(final AbstractSqueakObjectWithClassAndHash original) {
squeahHashAndBits = original.squeahHashAndBits;
squeakHashAndBits = original.squeakHashAndBits;
setSqueakHash(HASH_UNINITIALIZED);
squeakClass = original.squeakClass;
}
Expand Down Expand Up @@ -112,30 +111,30 @@ 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() {
return getSqueakHash() == HASH_UNINITIALIZED;
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -681,28 +680,24 @@ public CompiledCodeObject getMethodUnsafe() {
* </pre>
*/
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 6ca84ad

Please sign in to comment.