Skip to content

Commit

Permalink
Change PrimitiveType from enum to interface, implement PrimitiveType …
Browse files Browse the repository at this point in the history
…(one enum for each primitive type)
  • Loading branch information
silverbullettt committed Jan 20, 2024
1 parent 591ecc8 commit 93df4f5
Show file tree
Hide file tree
Showing 25 changed files with 390 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import pascal.taie.language.classes.JClass;
import pascal.taie.language.type.ArrayType;
import pascal.taie.language.type.ClassType;
import pascal.taie.language.type.PrimitiveType;
import pascal.taie.language.type.IntType;
import pascal.taie.language.type.ReferenceType;
import pascal.taie.language.type.Type;

Expand All @@ -60,7 +60,7 @@ public List<Stmt> compareAndSwapObject(Invoke invoke) {
if (o.getType() instanceof ArrayType) { // if o is of ArrayType
// generate o[i] = x;
Var i = new Var(invoke.getContainer(),
"%unsafe-index" + counter++, PrimitiveType.INT, -1);
"%unsafe-index" + counter++, IntType.INT, -1);
stmts.add(new StoreArray(new ArrayAccess(o, i), x));
} else { // otherwise, o is of ClassType
// generate o.f = x; for field f that has the same type of x.
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/pascal/taie/frontend/soot/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import pascal.taie.language.generics.MethodGSignature;
import pascal.taie.language.generics.ReferenceTypeGSignature;
import pascal.taie.language.type.ClassType;
import pascal.taie.language.type.PrimitiveType;
import pascal.taie.language.type.Type;
import pascal.taie.language.type.TypeSystem;
import pascal.taie.util.collection.Lists;
Expand Down Expand Up @@ -93,7 +92,15 @@
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import static pascal.taie.language.type.BooleanType.BOOLEAN;
import static pascal.taie.language.type.BottomType.BOTTOM;
import static pascal.taie.language.type.ByteType.BYTE;
import static pascal.taie.language.type.CharType.CHAR;
import static pascal.taie.language.type.DoubleType.DOUBLE;
import static pascal.taie.language.type.FloatType.FLOAT;
import static pascal.taie.language.type.IntType.INT;
import static pascal.taie.language.type.LongType.LONG;
import static pascal.taie.language.type.ShortType.SHORT;
import static pascal.taie.language.type.VoidType.VOID;
import static pascal.taie.util.collection.Maps.newConcurrentMap;

Expand Down Expand Up @@ -128,21 +135,21 @@ class Converter {
Type convertType(soot.Type sootType) {
if (sootType instanceof PrimType) {
if (sootType instanceof ByteType) {
return PrimitiveType.BYTE;
return BYTE;
} else if (sootType instanceof ShortType) {
return PrimitiveType.SHORT;
return SHORT;
} else if (sootType instanceof IntType) {
return PrimitiveType.INT;
return INT;
} else if (sootType instanceof LongType) {
return PrimitiveType.LONG;
return LONG;
} else if (sootType instanceof FloatType) {
return PrimitiveType.FLOAT;
return FLOAT;
} else if (sootType instanceof DoubleType) {
return PrimitiveType.DOUBLE;
return DOUBLE;
} else if (sootType instanceof CharType) {
return PrimitiveType.CHAR;
return CHAR;
} else if (sootType instanceof BooleanType) {
return PrimitiveType.BOOLEAN;
return BOOLEAN;
}
} else if (sootType instanceof RefType) {
return typeSystem.getClassType(loader, sootType.toString());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/ArrayLengthExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package pascal.taie.ir.exp;

import pascal.taie.language.type.ArrayType;
import pascal.taie.language.type.IntType;
import pascal.taie.language.type.PrimitiveType;

/**
Expand All @@ -48,7 +49,7 @@ public Var getOperand() {

@Override
public PrimitiveType getType() {
return PrimitiveType.INT;
return IntType.INT;
}

@Override
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/pascal/taie/ir/exp/ComparisonExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.DoubleType;
import pascal.taie.language.type.FloatType;
import pascal.taie.language.type.IntType;
import pascal.taie.language.type.LongType;
import pascal.taie.language.type.PrimitiveType;
import pascal.taie.language.type.Type;

Expand Down Expand Up @@ -60,9 +64,9 @@ public ComparisonExp(Op op, Var value1, Var value2) {
protected void validate() {
Type v1type = operand1.getType();
assert v1type.equals(operand2.getType());
assert v1type.equals(PrimitiveType.LONG) ||
v1type.equals(PrimitiveType.FLOAT) ||
v1type.equals(PrimitiveType.DOUBLE);
assert v1type.equals(LongType.LONG) ||
v1type.equals(FloatType.FLOAT) ||
v1type.equals(DoubleType.DOUBLE);
}

@Override
Expand All @@ -72,7 +76,7 @@ public Op getOperator() {

@Override
public PrimitiveType getType() {
return PrimitiveType.INT;
return IntType.INT;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/ConditionExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.IntType;
import pascal.taie.language.type.PrimitiveType;

/**
Expand Down Expand Up @@ -71,7 +72,7 @@ public Op getOperator() {

@Override
public PrimitiveType getType() {
return PrimitiveType.INT;
return IntType.INT;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/DoubleLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.DoubleType;
import pascal.taie.language.type.PrimitiveType;

public class DoubleLiteral implements FloatingPointLiteral {
Expand All @@ -43,7 +44,7 @@ public static DoubleLiteral get(double value) {

@Override
public PrimitiveType getType() {
return PrimitiveType.DOUBLE;
return DoubleType.DOUBLE;
}

public double getValue() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/Exps.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.LongType;
import pascal.taie.language.type.PrimitiveType;
import pascal.taie.language.type.ReferenceType;

Expand Down Expand Up @@ -54,7 +55,7 @@ public static boolean holdsInt(Exp exp) {
* @return {@code true} if {@code exp} can hold long values.
*/
public static boolean holdsLong(Exp exp) {
return exp.getType().equals(PrimitiveType.LONG);
return exp.getType().equals(LongType.LONG);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/FloatLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.FloatType;
import pascal.taie.language.type.PrimitiveType;

public class FloatLiteral implements FloatingPointLiteral {
Expand All @@ -43,7 +44,7 @@ public static FloatLiteral get(float value) {

@Override
public PrimitiveType getType() {
return PrimitiveType.FLOAT;
return FloatType.FLOAT;
}

public float getValue() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/InstanceOfExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.BooleanType;
import pascal.taie.language.type.PrimitiveType;
import pascal.taie.language.type.ReferenceType;

Expand Down Expand Up @@ -54,7 +55,7 @@ public ReferenceType getCheckedType() {

@Override
public PrimitiveType getType() {
return PrimitiveType.BOOLEAN;
return BooleanType.BOOLEAN;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/IntLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.IntType;
import pascal.taie.language.type.PrimitiveType;

/**
Expand Down Expand Up @@ -59,7 +60,7 @@ public static IntLiteral get(int value) {

@Override
public PrimitiveType getType() {
return PrimitiveType.INT;
return IntType.INT;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/LongLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package pascal.taie.ir.exp;

import pascal.taie.language.type.LongType;
import pascal.taie.language.type.PrimitiveType;

/**
Expand Down Expand Up @@ -59,7 +60,7 @@ public static LongLiteral get(long value) {

@Override
public PrimitiveType getType() {
return PrimitiveType.LONG;
return LongType.LONG;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pascal/taie/ir/exp/NegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package pascal.taie.ir.exp;


import pascal.taie.language.type.IntType;
import pascal.taie.language.type.PrimitiveType;

/**
Expand All @@ -49,7 +50,7 @@ public Var getOperand() {
@Override
public PrimitiveType getType() {
PrimitiveType type = (PrimitiveType) value.getType();
return type.asInt() ? PrimitiveType.INT : type;
return type.asInt() ? IntType.INT : type;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
import pascal.taie.language.classes.JField;
import pascal.taie.language.classes.JMethod;
import pascal.taie.language.type.ArrayType;
import pascal.taie.language.type.BooleanType;
import pascal.taie.language.type.ClassType;
import pascal.taie.language.type.PrimitiveType;
import pascal.taie.language.type.IntType;
import pascal.taie.language.type.Type;
import pascal.taie.language.type.TypeSystem;

Expand Down Expand Up @@ -263,7 +264,7 @@ private void initModels() {
Var arr = helper.getReturnVar();
// here n is just a placeholder of array-related statements,
// thus is value is irrelevant.
Var n = helper.newTempVar(PrimitiveType.INT);
Var n = helper.newTempVar(IntType.INT);
List<Stmt> stmts = new ArrayList<>();
stmts.add(new New(m, str, new NewInstance(string)));
stmts.add(new New(m, arr, new NewArray(stringArray, n)));
Expand Down Expand Up @@ -329,7 +330,7 @@ private void initModels() {
allocateObject(m, "<java.security.AccessControlContext: void <init>(java.security.ProtectionDomain[],boolean)>", b -> {
Var context = b.newTempVar(typeSystem.getArrayType(
typeSystem.getClassType("java.security.ProtectionDomain"), 1));
Var isPrivileged = b.newTempVar(PrimitiveType.BOOLEAN);
Var isPrivileged = b.newTempVar(BooleanType.BOOLEAN);
return List.of(context, isPrivileged);
}));

Expand All @@ -354,7 +355,7 @@ private void initModels() {
register("<sun.misc.Unsafe: boolean compareAndSwapObject(java.lang.Object,long,java.lang.Object,java.lang.Object)>", m -> {
IRBuildHelper helper = new IRBuildHelper(m);
Var array = helper.newTempVar(objArrayType);
Var i = helper.newTempVar(PrimitiveType.INT);
Var i = helper.newTempVar(IntType.INT);
List<Stmt> stmts = List.of(
new Cast(array, new CastExp(helper.getParam(0), objArrayType)),
new StoreArray(new ArrayAccess(array, i), helper.getParam(3)),
Expand All @@ -366,7 +367,7 @@ private void initModels() {
Function<JMethod, IR> unsafePut = m -> {
IRBuildHelper helper = new IRBuildHelper(m);
Var array = helper.newTempVar(objArrayType);
Var i = helper.newTempVar(PrimitiveType.INT);
Var i = helper.newTempVar(IntType.INT);
List<Stmt> stmts = List.of(
new Cast(array, new CastExp(helper.getParam(0), objArrayType)),
new StoreArray(new ArrayAccess(array, i), helper.getParam(2)),
Expand All @@ -389,7 +390,7 @@ private void initModels() {
Function<JMethod, IR> unsafeGet = m -> {
IRBuildHelper helper = new IRBuildHelper(m);
Var array = helper.newTempVar(objArrayType);
Var i = helper.newTempVar(PrimitiveType.INT);
Var i = helper.newTempVar(IntType.INT);
List<Stmt> stmts = List.of(
new Cast(array, new CastExp(helper.getParam(0), objArrayType)),
new LoadArray(helper.getReturnVar(), new ArrayAccess(array, i)),
Expand Down Expand Up @@ -481,7 +482,7 @@ private IR allocateObject(
private IR allocateArray(JMethod method, Type elemType) {
IRBuildHelper helper = new IRBuildHelper(method);
List<Stmt> stmts = new ArrayList<>();
Var len = helper.newTempVar(PrimitiveType.INT);
Var len = helper.newTempVar(IntType.INT);
ArrayType arrayType = typeSystem.getArrayType(elemType, 1);
Var array = helper.getReturnVar();
stmts.add(new New(method, array, new NewArray(arrayType, len)));
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/pascal/taie/language/type/BooleanType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Tai-e: A Static Analysis Framework for Java
*
* Copyright (C) 2022 Tian Tan <[email protected]>
* Copyright (C) 2022 Yue Li <[email protected]>
*
* This file is part of Tai-e.
*
* Tai-e is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
*/

package pascal.taie.language.type;

public enum BooleanType implements PrimitiveType {

BOOLEAN;

@Override
public String toString() {
return "boolean";
}

@Override
public boolean asInt() {
return true;
}
}
Loading

0 comments on commit 93df4f5

Please sign in to comment.