Skip to content

Commit

Permalink
This change addresses the issue soot-oss#970.
Browse files Browse the repository at this point in the history
Implemented a type visitor for computing the bitsize of a given type.
  • Loading branch information
skmuduli92 committed Jul 29, 2024
1 parent b7ba8cd commit d1840d9
Showing 1 changed file with 51 additions and 14 deletions.
65 changes: 51 additions & 14 deletions sootup.core/src/main/java/sootup/core/types/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import javax.annotation.Nonnull;
import sootup.core.jimple.visitor.AbstractTypeVisitor;
import sootup.core.jimple.visitor.Acceptor;
import sootup.core.jimple.visitor.TypeVisitor;

Expand Down Expand Up @@ -63,24 +64,60 @@ public static ArrayType createArrayType(@Nonnull Type type, int dim) {
}

public static int getValueBitSize(Type type) {
// TODO: ms: make use of the typevisitor to get O(1)
if (type instanceof PrimitiveType.BooleanType) {
return 1;
BitSizeVisitor visitor = new BitSizeVisitor();
type.accept(visitor);
return visitor.getResult().intValue();
}

private static class BitSizeVisitor extends AbstractTypeVisitor<Integer> {
@Override
public void caseBooleanType() {
setResult(Integer.valueOf(1));
}

@Override
public void caseByteType() {
setResult(Integer.valueOf(8));
}

@Override
public void caseShortType() {
setResult(Integer.valueOf(16));
}

@Override
public void caseCharType() {
setResult(Integer.valueOf(16));
}
if (type instanceof PrimitiveType.ByteType) {
return 8;

@Override
public void caseIntType() {
setResult(Integer.valueOf(32));
}

@Override
public void caseFloatType() {
setResult(Integer.valueOf(32));
}

@Override
public void caseLongType() {
setResult(Integer.valueOf(64));
}
if (type instanceof PrimitiveType.ShortType || type instanceof PrimitiveType.CharType) {
return 16;

@Override
public void caseDoubleType() {
setResult(Integer.valueOf(64));
}
if (type instanceof PrimitiveType.IntType || type instanceof PrimitiveType.FloatType) {
return 32;

@Override
public void caseClassType(@Nonnull ClassType classType) {
setResult(Integer.valueOf(64));
}
if (type instanceof PrimitiveType.LongType
|| type instanceof PrimitiveType.DoubleType
|| type instanceof ClassType) {
return 64;

@Override
public void defaultCaseType() {
setResult(Integer.valueOf(0));
}
return 0;
}
}

0 comments on commit d1840d9

Please sign in to comment.