add more on code generation
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / SemanticsChecker.java
1 package edu.uci.eecs.specCompiler.codeGenerator;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6
7 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
8 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
9 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
10 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
11 import edu.uci.eecs.specCompiler.specExtraction.Construct;
12 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
14 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
15 import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
16
17 public class SemanticsChecker {
18         public final ArrayList<SpecConstruct> constructs;
19         public final HashMap<String, SpecConstruct> CPLabel2Construct;
20         public final HashMap<String, SpecConstruct> potentialCPLabel2Construct;
21         public final HashMap<String, SpecConstruct> interfaceName2Construct;
22         public final HashSet<DefineVar> defineVars;
23
24         public SemanticsChecker(ArrayList<SpecConstruct> constructs) {
25                 this.constructs = constructs;
26                 this.CPLabel2Construct = new HashMap<String, SpecConstruct>();
27                 this.potentialCPLabel2Construct = new HashMap<String, SpecConstruct>();
28                 this.interfaceName2Construct = new HashMap<String, SpecConstruct>();
29                 this.defineVars = new HashSet<DefineVar>();
30         }
31
32         private void checkHBLabelConsistency(ConditionalInterface inst)
33                         throws SemanticsCheckerException {
34                 String interfaceName = inst.interfaceName,
35                                 label = inst.hbConditionLabel;
36                 if (!interfaceName2Construct.containsKey(interfaceName)) {
37                         throw new SemanticsCheckerException(
38                                         "In global construct, no interface \"" + interfaceName
39                                                         + "\"!");
40                 } else if (!label.equals("")){
41                         InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
42                                         .get(interfaceName).construct;
43                         if (!iConstruct.hbConditions.containsKey(label)) {
44                                 throw new SemanticsCheckerException("Interface "
45                                                 + interfaceName + " doesn't contain HB_codition: "
46                                                 + label + "!");
47                         }
48                 }
49         }
50
51         private void checkLabelDuplication(Construct construct, String label)
52                         throws SemanticsCheckerException {
53                 if (potentialCPLabel2Construct.containsKey(label) ||
54                                 CPLabel2Construct.containsKey(label))
55                         throw new SemanticsCheckerException("In construct: " + construct
56                                         + "\"" + label + "\" has duplication.");
57         }
58
59         public void check() throws SemanticsCheckerException {
60                 boolean hasGlobalConstruct = false;
61                 // First grab the information from the interface
62                 for (int i = 0; i < constructs.size(); i++) {
63                         Construct inst = constructs.get(i).construct;
64                         if (inst instanceof InterfaceConstruct) {
65                                 InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
66                                 interfaceName2Construct.put(iConstruct.name, constructs.get(i));
67                                 for (int j = 0; j < iConstruct.action.defineVars.size(); j++) {
68                                         DefineVar var = iConstruct.action.defineVars.get(j);
69                                         var.renameVarName("__" + iConstruct.name + "_" + var.varName + "__");
70                                 }
71                         }
72                 }
73
74                 String label;
75                 for (int i = 0; i < constructs.size(); i++) {
76                         SpecConstruct inst = constructs.get(i);
77                         Construct construct = inst.construct;
78                         if (construct instanceof GlobalConstruct) {
79                                 GlobalConstruct theConstruct = (GlobalConstruct) construct;
80                                 if (!hasGlobalConstruct)
81                                         hasGlobalConstruct = true;
82                                 else {
83                                         throw new SemanticsCheckerException(
84                                                         "More than one global construct!");
85                                 }
86                                 HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions = theConstruct.hbRelations;
87                                 for (ConditionalInterface left : hbConditions.keySet()) {
88                                         HashSet<ConditionalInterface> set = hbConditions.get(left);
89                                         checkHBLabelConsistency(left);
90                                         for (ConditionalInterface right : set) {
91                                                 checkHBLabelConsistency(right);
92                                         }
93                                 }
94                         } else if (construct instanceof PotentialCPDefineConstruct) {
95                                 PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
96                                 label = theConstruct.label;
97                                 checkLabelDuplication(construct, label);
98                                 potentialCPLabel2Construct.put(label, inst);
99                         } else if (construct instanceof CPDefineCheckConstruct) {
100                                 CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
101                                 label = theConstruct.label;
102                                 checkLabelDuplication(construct, label);
103                                 CPLabel2Construct.put(label, inst);
104                         } else if (construct instanceof CPDefineConstruct) {
105                                 CPDefineConstruct theConstruct = (CPDefineConstruct) construct;
106                                 label = theConstruct.label;
107                                 checkLabelDuplication(construct, label);
108                                 CPLabel2Construct.put(label, inst);
109                         }
110                 }
111         }
112         
113         public String toString() {
114                 StringBuilder sb = new StringBuilder();
115                 sb.append("Interface name 2 Construct:\n");
116                 for (String interfaceName : interfaceName2Construct.keySet()) {
117                         sb.append(interfaceName + "\t" + interfaceName2Construct.get(interfaceName) + "\n");
118                 }
119                 
120                 sb.append("Potential commit point label 2 Construct:\n");
121                 for (String label : potentialCPLabel2Construct.keySet()) {
122                         sb.append(label + "\t" + potentialCPLabel2Construct.get(label) + "\n");
123                 }
124                 
125                 sb.append("Commit point label 2 Construct:\n");
126                 for (String label : CPLabel2Construct.keySet()) {
127                         sb.append(label + "\t" + CPLabel2Construct.get(label) + "\n");
128                 }
129                 return sb.toString();
130         }
131 }