1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
7 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
8 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
9 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
10 import edu.uci.eecs.specCompiler.specExtraction.Construct;
11 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
14 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
15 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
17 public class SemanticsChecker {
18 public final ArrayList<Construct> constructs;
19 public final HashMap<String, Construct> CPLabel2Construct;
20 public final HashMap<String, Construct> potentialCPLabel2Construct;
21 public final HashMap<String, Construct> interfaceName2Construct;
22 public final HashMap<String, Construct> interfaceName2DefineConstruct;
23 public final HashMap<String, ArrayList<InterfaceConstruct>> CPLabel2InterfaceConstruct;
25 public final HashMap<String, Integer> interface2Num;
26 public final HashMap<String, Integer> hbLabel2Num;
27 public final HashMap<String, Integer> commitPointLabel2Num;
29 private HashMap<String, String> options;
30 private HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions;
31 private Construct entryPointConstruct;
33 private int _interfaceNum;
34 private int _hbLabelNum;
35 private int _commitPointNum;
37 public SemanticsChecker(ArrayList<Construct> constructs) {
38 this.constructs = constructs;
39 this.CPLabel2Construct = new HashMap<String, Construct>();
40 this.potentialCPLabel2Construct = new HashMap<String, Construct>();
41 this.interfaceName2Construct = new HashMap<String, Construct>();
42 this.interfaceName2DefineConstruct = new HashMap<String, Construct>();
43 this.CPLabel2InterfaceConstruct = new HashMap<String, ArrayList<InterfaceConstruct>>();
44 this.entryPointConstruct = null;
46 this.interface2Num = new HashMap<String, Integer>();
47 this.hbLabel2Num = new HashMap<String, Integer>();
48 // Immediately init the true HB-condition to be 0
49 hbLabel2Num.put("", 0);
51 this.commitPointLabel2Num = new HashMap<String, Integer>();
58 public HashMap<ConditionalInterface, HashSet<ConditionalInterface>> getHBConditions() {
59 return this.hbConditions;
62 public String getOption(String key) {
63 return options.get(key);
66 private void checkHBLabelConsistency(ConditionalInterface inst)
67 throws SemanticsCheckerException {
68 String interfaceName = inst.interfaceName, label = inst.hbConditionLabel;
69 if (!interfaceName2Construct.containsKey(interfaceName)) {
70 throw new SemanticsCheckerException(
71 "In global construct, no interface \"" + interfaceName
73 } else if (!label.equals("")) {
74 InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
76 if (!iConstruct.hbConditions.containsKey(label)) {
77 throw new SemanticsCheckerException("Interface "
78 + interfaceName + " doesn't contain HB_codition: "
82 // No HB condition label can duplicate!
83 if (hbLabel2Num.containsKey(label)) {
84 throw new SemanticsCheckerException("Happens-before label: "
85 + label + " duplicates!");
88 // Number the HB-condition label
89 hbLabel2Num.put(label, _hbLabelNum++);
93 private void checkLabelDuplication(Construct construct, String label)
94 throws SemanticsCheckerException {
95 if (potentialCPLabel2Construct.containsKey(label)
96 || CPLabel2Construct.containsKey(label))
97 throw new SemanticsCheckerException("In construct: " + construct
98 + "\"" + label + "\" has duplication.");
101 private void checkOptions() throws SemanticsCheckerException {
102 // FIXME: We don't have any check here
105 private void postCheck() throws SemanticsCheckerException {
106 // This is a C program, must provide the entry point
107 if (getOption("LANG").equals("C") && entryPointConstruct == null) {
108 throw new SemanticsCheckerException(
109 "C program must provide the entry point!");
112 // Check if interface define construct labels are correct
113 for (String name : interfaceName2DefineConstruct.keySet()) {
114 if (!interfaceName2Construct.containsKey(name)) {
115 throw new SemanticsCheckerException(
116 "Label \"" + name + "\" does not have interface declaration!");
121 public void check() throws SemanticsCheckerException {
122 boolean hasGlobalConstruct = false;
123 // First grab the information from the interface
124 for (int i = 0; i < constructs.size(); i++) {
125 Construct inst = constructs.get(i);
126 if (inst instanceof InterfaceConstruct) {
127 InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
128 if (interfaceName2Construct.containsKey(iConstruct.name)) {
129 throw new SemanticsCheckerException("Interface name: "
130 + iConstruct.name + " duplicates!");
132 // Number the interface label
133 interface2Num.put(iConstruct.name, _interfaceNum++);
135 interfaceName2Construct.put(iConstruct.name, constructs.get(i));
137 for (int j = 0; j < iConstruct.commitPointSet.size(); j++) {
138 String label = iConstruct.commitPointSet.get(j);
139 if (!CPLabel2InterfaceConstruct.containsKey(label)) {
140 CPLabel2InterfaceConstruct.put(label,
141 new ArrayList<InterfaceConstruct>());
143 CPLabel2InterfaceConstruct.get(label).add(iConstruct);
149 for (int i = 0; i < constructs.size(); i++) {
150 Construct construct = constructs.get(i);
151 if (construct instanceof GlobalConstruct) {
152 GlobalConstruct theConstruct = (GlobalConstruct) construct;
153 if (!hasGlobalConstruct)
154 hasGlobalConstruct = true;
156 throw new SemanticsCheckerException(
157 "More than one global construct!");
159 // Record the options and check them
160 options = theConstruct.options;
162 // Record the HB conditions and check it
163 hbConditions = theConstruct.hbRelations;
164 for (ConditionalInterface left : hbConditions.keySet()) {
165 HashSet<ConditionalInterface> set = hbConditions.get(left);
166 checkHBLabelConsistency(left);
168 for (ConditionalInterface right : set) {
169 checkHBLabelConsistency(right);
172 } else if (construct instanceof PotentialCPDefineConstruct) {
173 PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
174 label = theConstruct.label;
175 checkLabelDuplication(construct, label);
176 // Number the commit_point label
177 commitPointLabel2Num.put(label, _commitPointNum++);
179 potentialCPLabel2Construct.put(label, construct);
180 } else if (construct instanceof CPDefineCheckConstruct) {
181 CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
182 label = theConstruct.label;
183 checkLabelDuplication(construct, label);
184 // Number the commit_point label
185 commitPointLabel2Num.put(label, _commitPointNum++);
187 CPLabel2Construct.put(label, construct);
188 } else if (construct instanceof CPDefineConstruct) {
189 CPDefineConstruct theConstruct = (CPDefineConstruct) construct;
190 label = theConstruct.label;
191 checkLabelDuplication(construct, label);
192 // Number the commit_point label
193 commitPointLabel2Num.put(label, _commitPointNum++);
195 CPLabel2Construct.put(label, construct);
196 } else if (construct instanceof EntryPointConstruct) {
197 if (entryPointConstruct != null) {
198 throw new SemanticsCheckerException(
199 "More than one entry point!");
201 entryPointConstruct = construct;
202 } else if (construct instanceof InterfaceDefineConstruct) {
203 InterfaceDefineConstruct theConstruct = (InterfaceDefineConstruct) construct;
204 String name = theConstruct.name;
205 if (interfaceName2DefineConstruct.containsKey(name)) {
206 throw new SemanticsCheckerException(
207 "Interface define label duplicates!");
209 interfaceName2DefineConstruct.put(name, construct);
214 public String toString() {
215 StringBuilder sb = new StringBuilder();
216 if (entryPointConstruct == null) {
217 sb.append("Entry point is not specified!");
219 sb.append("@Entry_point:\n" + entryPointConstruct);
222 sb.append("Interface name 2 Construct:\n");
223 for (String interfaceName : interfaceName2Construct.keySet()) {
224 sb.append(interfaceName + "\t"
225 + interfaceName2Construct.get(interfaceName) + "\n");
228 sb.append("Interface name 2 define construct:\n");
229 for (String interfaceName : interfaceName2DefineConstruct.keySet()) {
230 sb.append(interfaceName + "\t"
231 + interfaceName2DefineConstruct.get(interfaceName) + "\n");
234 sb.append("Potential commit point label 2 Construct:\n");
235 for (String label : potentialCPLabel2Construct.keySet()) {
236 sb.append(label + "\t" + potentialCPLabel2Construct.get(label)
240 sb.append("Commit point label 2 Construct:\n");
241 for (String label : CPLabel2Construct.keySet()) {
242 sb.append(label + "\t" + CPLabel2Construct.get(label) + "\n");
244 return sb.toString();