Skip to content

Commit

Permalink
Fix enum (#5)
Browse files Browse the repository at this point in the history
* Use Java 13.

* Fix resolution of references to enum.

* Fix the initialization of enum variables.
  • Loading branch information
mht208 authored Jul 31, 2023
1 parent 5f26298 commit b281e44
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SRC_PATH = ${PSS_HOME}/src
ANTLR4 = java -jar $(ANTLR4_JAR_PATH)
GRUN = java -classpath $(PSSGEN_JAR_PATH):$(ANTLR4_JAR_PATH) org.antlr.v4.gui.TestRig PSS model -gui -f

JAVAC = javac -classpath $(SRC_PATH):$(ANTLR4_JAR_PATH)
JAVAC = javac --release 13 -classpath $(SRC_PATH):$(ANTLR4_JAR_PATH)

PSSGEN = java -classpath $(PSSGEN_JAR_PATH):$(ANTLR4_JAR_PATH) PSSGenMain

Expand Down
15 changes: 13 additions & 2 deletions src/PSSEnumInst.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import java.util.*;
import java.math.BigInteger;

public class PSSEnumInst extends PSSIntInst {

public PSSEnumInst(String id, String type, PSSModel type_decl, boolean rand) {
public PSSEnumInst(String id, String type, PSSEnumModel type_decl, boolean rand) {
super(id, rand, 32, true);

/*
* PSS 2.0, Section 8.4 Enumeration types:
* When not initialized, the default value of an enum field shall be the first
* enum_item in the list. This is not necessarily the value 0 nor the enum_item
* with the minimum value.
*/
String[] items = type_decl.getEnumItems();
if (items.length > 0) {
m_val = new PSSIntVal(BigInteger.valueOf(type_decl.getEnumItemValue(items[0])));
}
}
}
23 changes: 21 additions & 2 deletions src/PSSEnumModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@

public class PSSEnumModel extends PSSModel {

private HashMap<String, Integer> m_items;
private LinkedHashMap<String, Integer> m_items;

private Integer m_default_val;

public PSSEnumModel(String id) {
super(id);
m_items = new HashMap<String, Integer>();
m_items = new LinkedHashMap<String, Integer>();
m_default_val = 0;
}

/**
* Returns items in this enum.
*
* @return items in this enum.
*/
public String[] getEnumItems() {
return m_items.keySet().toArray(new String[0]);
}

/**
* Returns the value of a specified item in this enum.
*
* @param item an item in this enum
* @return the value of item in this enum
*/
public int getEnumItemValue(String item) {
return m_items.get(item);
}

public void addEnumItem(String id) {
m_items.put(id, m_default_val);
m_default_val++;
Expand Down
10 changes: 6 additions & 4 deletions src/PSSInst.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ public PSSInst findInstanceUnder(String hierarchy_id) {

public PSSInst findStaticInst(String hierarchiy_id) {
// Currently, find enum items only.
PSSInst res = null;
PSSModel m = getTypeModel();
while (!(m instanceof PSSComponentModel) && m != null)
while (m != null) {
res = m.findStaticInst(hierarchiy_id);
if (res != null)
break;
m = m.m_parent;
if (m != null) {
return m.findStaticInst(hierarchiy_id);
}
return null;
return res;
}

public PSSInst findInstance(String hierarchy_id, boolean local_scope) {
Expand Down
25 changes: 22 additions & 3 deletions src/PSSMemberPathElemExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ private PSSExecKind getContextExecKind(PSSInst inst) {
*/
private PSSInst getInstOne(PSSModel pkg, PSSInst ctx, PSSInst parent) {
PSSInst inst = null;

if (m_function_parameter_list == null) {
inst = m_parent == null ? ctx.findInstance(m_id) : parent.findInstanceUnder(m_id);
if (pkg == null || m_parent != null) {
inst = m_parent == null ? ctx.findInstance(m_id) : parent.findInstanceUnder(m_id);
} else {
inst = pkg.findStaticInst(m_id);
}
if (inst == null)
PSSMessage.Error("PSSMemberPathElemExpression", getUpperHierarchicalID() + " is not defined.");
} else if (m_parent == null || parent instanceof PSSComponentInst) {
Expand Down Expand Up @@ -188,7 +191,8 @@ private PSSInst getInstOne(PSSModel pkg, PSSInst ctx, PSSInst parent) {
PSSMessage.Error("PSS 2.0 Section 22.4.1.3",
"The function call " + getUpperHierarchicalID() + "("
+ String.join(", ",
m_function_parameter_list.stream().map(p -> p.getText()).collect(Collectors.toList()))
m_function_parameter_list.stream().map(p -> p.getText())
.collect(Collectors.toList()))
+ ") is not available in its platform "
+ k + ".");
}
Expand Down Expand Up @@ -255,6 +259,21 @@ private PSSInst getInst(PSSModel pkg, PSSInst ctx, PSSInst parent) {
return inst;
}

/**
* Resolve the whole hierarchical reference path.
*
* @param pkg an optional package containing m_id (m_parent != null implies
* pkg == null)
* @param var the containing instance
* @return the resolved instance of the whole hierarchical reference path
*/
public PSSInst getInst(PSSModel pkg, PSSInst var) {
if (m_parent != null)
PSSMessage.Error("PSSMemberPathElemExpression",
"Cannot evaluate a partial member path reference: " + getLowerHierarchicalID());
return getInst(pkg, var, var);
}

@Override
public PSSInst getInst(PSSInst var) {
if (m_parent != null)
Expand Down
6 changes: 3 additions & 3 deletions src/PSSModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public void declEnumInst(PSSInst inst) {

child.declEnumItem(inst);
}
// if (m_parent != null) {
// m_parent.declEnumInst(inst);
// }
if (m_parent != null) {
m_parent.declEnumInst(inst);
}
}

public void init_up(PSSInst inst) {
Expand Down
7 changes: 5 additions & 2 deletions src/PSSRefPathExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ public static PSSRefPathExpression fromString(PSSModel root, String str) {

@Override
public PSSInst getInst(PSSInst var) {
PSSInst inst = null;
if (m_type_identifier_elems != null && !m_type_identifier_elems.equals("")) {
PSSMessage.Fatal("[" + getClass().getName() + "] type_identifier_elems is not implemented");
PSSModel m = var.getTypeModel().findDeclaration(m_type_identifier_elems);
inst = m_ref_path.getInst(m, var);
} else {
inst = m_ref_path.getInst(var);
}

PSSInst inst = m_ref_path.getInst(var);
if (m_bit_slice_from != null && m_bit_slice_to != null)
PSSMessage.Fatal("[" + getClass().getName() + "] bit_slice is not implemented");

Expand Down

0 comments on commit b281e44

Please sign in to comment.