1 package edu.uci.eecs.specCompiler.codeGenerator;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.HashSet;
8 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
9 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
10 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
14 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
15 import edu.uci.eecs.specCompiler.specExtraction.Construct;
16 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
21 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
22 import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
23 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
25 public class SemanticsChecker {
26 public final HashMap<File, SourceFileInfo> srcFilesInfo;
27 public final ArrayList<Construct> constructs;
28 public final HashMap<String, Construct> CPLabel2Construct;
29 public final HashMap<String, PotentialCPDefineConstruct> potentialCPLabel2Construct;
30 public final HashMap<String, InterfaceConstruct> interfaceName2Construct;
31 public final HashMap<String, InterfaceDefineConstruct> interfaceName2DefineConstruct;
32 public final HashMap<String, ArrayList<InterfaceConstruct>> CPLabel2InterfaceConstruct;
34 public final HashMap<String, Integer> interface2Num;
35 public final HashMap<String, Integer> hbLabel2Num;
36 public final HashMap<String, Integer> commitPointLabel2Num;
38 private HashMap<String, String> options;
39 private HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions;
40 private ArrayList<EntryPointConstruct> entryPointConstructs;
41 private ClassBeginConstruct classBeginConstruct;
42 private ClassEndConstruct classEndConstruct;
44 private String templateStr;
45 private String templateFullStr;
46 private String className;
48 private int _interfaceNum;
49 private int _hbLabelNum;
50 private int _commitPointNum;
52 public SemanticsChecker(SpecExtractor extractor) {
53 this.srcFilesInfo = extractor.srcFilesInfo;
54 this.constructs = extractor.getConstructs();
55 this.CPLabel2Construct = new HashMap<String, Construct>();
56 this.potentialCPLabel2Construct = new HashMap<String, PotentialCPDefineConstruct>();
57 this.interfaceName2Construct = new HashMap<String, InterfaceConstruct>();
58 this.interfaceName2DefineConstruct = new HashMap<String, InterfaceDefineConstruct>();
59 this.CPLabel2InterfaceConstruct = new HashMap<String, ArrayList<InterfaceConstruct>>();
60 this.entryPointConstructs = new ArrayList<EntryPointConstruct>();
61 this.classBeginConstruct = null;
62 this.classEndConstruct = null;
64 this.interface2Num = new HashMap<String, Integer>();
65 this.hbLabel2Num = new HashMap<String, Integer>();
66 // Immediately init the true HB-condition to be 0
67 hbLabel2Num.put("", 0);
69 this.commitPointLabel2Num = new HashMap<String, Integer>();
76 templateFullStr = null;
80 public ClassBeginConstruct getClassBeginConstruct() {
81 return this.classBeginConstruct;
84 public ClassEndConstruct getClassEndConstruct() {
85 return this.classEndConstruct;
88 public String getTemplateFullStr() {
89 return this.templateFullStr;
92 public String getTemplateStr() {
93 return this.templateStr;
96 public String getClassName() {
97 return this.className;
100 public HashMap<ConditionalInterface, HashSet<ConditionalInterface>> getHBConditions() {
101 return this.hbConditions;
104 public String getOption(String key) {
105 return options.get(key);
108 private void checkHBLabelConsistency(ConditionalInterface inst)
109 throws SemanticsCheckerException {
110 String interfaceName = inst.interfaceName, label = inst.hbConditionLabel;
111 if (!interfaceName2Construct.containsKey(interfaceName)) {
112 throw new SemanticsCheckerException(
113 "In global construct, no interface \"" + interfaceName
115 } else if (!label.equals("")) {
116 InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
118 if (!iConstruct.hbConditions.containsKey(label)) {
119 throw new SemanticsCheckerException("Interface "
120 + interfaceName + " doesn't contain HB_codition: "
124 // No HB condition label can duplicate!
125 if (hbLabel2Num.containsKey(label)) {
126 throw new SemanticsCheckerException("Happens-before label: "
127 + label + " duplicates!");
130 // Number the HB-condition label
131 hbLabel2Num.put(label, _hbLabelNum++);
135 private void checkLabelDuplication(Construct construct, String label)
136 throws SemanticsCheckerException {
137 if (potentialCPLabel2Construct.containsKey(label)
138 || CPLabel2Construct.containsKey(label))
139 throw new SemanticsCheckerException("In construct: " + construct
140 + "\"" + label + "\" has duplication.");
143 private void checkOptions() throws SemanticsCheckerException {
144 // FIXME: We don't have any check here
147 private void postCheck() throws SemanticsCheckerException {
148 // C++ data structure with Class must provide the beginning and ending
149 // of its declaration
150 if (getOption("Class") != null) {
151 if (classBeginConstruct == null || classEndConstruct == null) {
152 throw new SemanticsCheckerException(
153 "Class must provide the boundary explicitly!");
156 // It must provide the entry point
157 if (entryPointConstructs.size() == 0) {
158 throw new SemanticsCheckerException(
159 "The program must have at least one entry point!");
162 // Check if interface define construct labels are correct
163 for (String name : interfaceName2DefineConstruct.keySet()) {
164 if (!interfaceName2Construct.containsKey(name)) {
165 throw new SemanticsCheckerException("Label \"" + name
166 + "\" does not have interface declaration!");
171 public void check() throws SemanticsCheckerException {
172 boolean hasGlobalConstruct = false;
173 // First grab the information from the interface
174 for (int i = 0; i < constructs.size(); i++) {
175 Construct inst = constructs.get(i);
176 if (inst instanceof InterfaceConstruct) {
177 InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
178 if (interfaceName2Construct.containsKey(iConstruct.name)) {
179 throw new SemanticsCheckerException("Interface name: "
180 + iConstruct.name + " duplicates!");
182 // Number the interface label
183 interface2Num.put(iConstruct.name, _interfaceNum++);
185 interfaceName2Construct.put(iConstruct.name,
186 (InterfaceConstruct) constructs.get(i));
188 for (int j = 0; j < iConstruct.commitPointSet.size(); j++) {
189 String label = iConstruct.commitPointSet.get(j);
190 if (!CPLabel2InterfaceConstruct.containsKey(label)) {
191 CPLabel2InterfaceConstruct.put(label,
192 new ArrayList<InterfaceConstruct>());
194 CPLabel2InterfaceConstruct.get(label).add(iConstruct);
200 for (int i = 0; i < constructs.size(); i++) {
201 Construct construct = constructs.get(i);
202 if (construct instanceof GlobalConstruct) {
203 GlobalConstruct theConstruct = (GlobalConstruct) construct;
204 if (!hasGlobalConstruct)
205 hasGlobalConstruct = true;
207 throw new SemanticsCheckerException(
208 "More than one global construct!");
210 // Record the options and check them
211 options = theConstruct.options;
213 // Record the HB conditions and check it
214 hbConditions = theConstruct.hbRelations;
215 for (ConditionalInterface left : hbConditions.keySet()) {
216 HashSet<ConditionalInterface> set = hbConditions.get(left);
217 checkHBLabelConsistency(left);
219 for (ConditionalInterface right : set) {
220 checkHBLabelConsistency(right);
223 } else if (construct instanceof PotentialCPDefineConstruct) {
224 PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
225 label = theConstruct.label;
226 checkLabelDuplication(construct, label);
227 // Number the commit_point label
228 commitPointLabel2Num.put(label, _commitPointNum++);
230 potentialCPLabel2Construct.put(label,
231 (PotentialCPDefineConstruct) construct);
232 } else if (construct instanceof CPDefineCheckConstruct) {
233 CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
234 label = theConstruct.label;
235 checkLabelDuplication(construct, label);
236 // Number the commit_point label
237 commitPointLabel2Num.put(label, _commitPointNum++);
239 CPLabel2Construct.put(label, construct);
240 } else if (construct instanceof CPDefineConstruct) {
241 CPDefineConstruct theConstruct = (CPDefineConstruct) construct;
242 label = theConstruct.label;
243 checkLabelDuplication(construct, label);
244 // Number the commit_point label
245 commitPointLabel2Num.put(label, _commitPointNum++);
247 CPLabel2Construct.put(label, construct);
248 } else if (construct instanceof EntryPointConstruct) {
249 entryPointConstructs.add((EntryPointConstruct) construct);
250 } else if (construct instanceof InterfaceDefineConstruct) {
251 InterfaceDefineConstruct theConstruct = (InterfaceDefineConstruct) construct;
252 String name = theConstruct.name;
253 if (interfaceName2DefineConstruct.containsKey(name)) {
254 throw new SemanticsCheckerException(
255 "Interface define label duplicates!");
257 interfaceName2DefineConstruct.put(name, theConstruct);
258 } else if (construct instanceof ClassBeginConstruct) {
259 classBeginConstruct = (ClassBeginConstruct) construct;
260 ArrayList<String> content = srcFilesInfo.get(classBeginConstruct.file).content;
261 String firstLine = content.get(classBeginConstruct.beginLineNum), secondLine;
262 if (firstLine.startsWith("template")) {
263 secondLine = content.get(classBeginConstruct.beginLineNum + 1);
264 templateFullStr = firstLine;
265 templateStr = ParserUtils.getTemplateStr(firstLine);
266 className = ParserUtils.getClassName(secondLine);
268 className = ParserUtils.getClassName(firstLine);
271 } else if (construct instanceof ClassEndConstruct) {
272 classEndConstruct = (ClassEndConstruct) construct;
277 public String toString() {
278 StringBuilder sb = new StringBuilder();
280 sb.append("Interface name 2 Construct:\n");
281 for (String interfaceName : interfaceName2Construct.keySet()) {
282 sb.append(interfaceName + "\t"
283 + interfaceName2Construct.get(interfaceName) + "\n");
286 sb.append("Interface name 2 define construct:\n");
287 for (String interfaceName : interfaceName2DefineConstruct.keySet()) {
288 sb.append(interfaceName + "\t"
289 + interfaceName2DefineConstruct.get(interfaceName) + "\n");
292 sb.append("Potential commit point label 2 Construct:\n");
293 for (String label : potentialCPLabel2Construct.keySet()) {
294 sb.append(label + "\t" + potentialCPLabel2Construct.get(label)
298 sb.append("Commit point label 2 Construct:\n");
299 for (String label : CPLabel2Construct.keySet()) {
300 sb.append(label + "\t" + CPLabel2Construct.get(label) + "\n");
302 return sb.toString();