Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type visitor implimentation for computing the bitsize (issue #970) #998

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}