1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashSet;
7 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
8 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
9 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
10 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
11 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
13 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
14 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
15 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
16 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
18 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
20 public class CodeVariables {
21 // C++ code or library
22 public static final String HEADER_THREADS = "<threads.h>";
23 public static final String HEADER_STDINT = "<stdint.h>";
24 public static final String ThreadIDType = "thrd_t";
25 public static final String GET_THREAD_ID = "thrd_current";
26 public static final String BOOLEAN = "bool";
27 public static final String UINT64 = "uint64_t";
30 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
31 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
32 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
33 public static final String CDSAnnotate = "cdsannotate";
34 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
35 public static final String IDType = "id_t";
37 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
38 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
39 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
40 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
41 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
42 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
43 public static final String SPEC_ANNOTATION = "spec_annotation";
44 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
45 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
47 public static final String ANNO_HB_INIT = "anno_hb_init";
48 public static final String ANNO_INTERFACE_BOUNDARY = "anno_interface_boundary";
49 public static final String ANNO_ID = "anno_id";
50 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potentail_cp_define";
51 public static final String ANNO_CP_DEFINE = "anno_cp_define";
52 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
53 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
54 public static final String ANNO_POST_CHECK = "anno_post_check";
56 // Specification variables
57 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
58 public static final String DEFAULT_ID = "0";
60 // Specification library
61 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
62 public static final String SPEC_QUEUE = "spec_queue";
63 public static final String SPEC_STACK = "spec_stack";
64 public static final String SPEC_DEQUE = "spec_deque";
65 public static final String SPEC_HASHTABLE = "spec_hashtable";
66 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
67 public static final String SPEC_TAG = "spec_tag";
68 public static final String SPEC_TAG_CURRENT = "current";
69 public static final String SPEC_TAG_NEXT = "next";
72 public static final String MACRO_ID = "__ID__";
73 public static final String MACRO_COND = "__COND_SAT__";
74 public static final String MACRO_RETURN = "__RET__";
75 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
77 public static void printCode(ArrayList<String> code) {
78 for (int i = 0; i < code.size(); i++) {
79 System.out.println(code.get(i));
83 private static String COMMENT(String comment) {
84 return "/* " + comment + " */";
87 private static String INCLUDE(String header) {
88 return "#include " + header;
91 private static String DEFINE(String left, String right) {
92 return "#define " + left + " " + right;
95 private static String UNDEFINE(String macro) {
96 return "#undef " + macro;
99 private static String BRACE(String val) {
100 return "(" + val + ")";
103 private static String ASSIGN(String structName, String field, String val) {
104 return structName + "." + field + " = " + val + ";";
107 private static String ASSIGN_PTR(String structName, String field, String val) {
108 return structName + "." + field + " = &" + val + ";";
111 private static String DECLARE(String type, String name) {
112 return type + " " + name + ";";
115 private static String DECLARE(VariableDeclaration varDecl) {
116 String type = varDecl.type, name = varDecl.name;
117 return type + " " + name + ";";
120 private static String DECLARE_DEFINE(String type, String var, String val) {
121 return type + " " + var + " = " + val + ";";
124 private static String ANNOTATE(String structName) {
125 return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");";
128 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
129 FunctionHeader funcDecl) {
130 ArrayList<String> code = new ArrayList<String>();
131 code.add("typedef struct " + interfaceName + "_info {");
132 code.add(DECLARE(funcDecl.returnType, MACRO_RETURN));
133 for (int i = 0; i < funcDecl.args.size(); i++) {
134 code.add(DECLARE(funcDecl.args.get(i)));
136 code.add("} " + interfaceName + "_info {");
140 private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
142 ArrayList<String> code = new ArrayList<String>();
143 code.add("static " + IDType + " " + interfaceName + "_id() {");
144 if (idCode != null) {
145 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
147 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
149 code.add("return " + MACRO_ID + ";");
154 private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
155 HashSet<String> headers = new HashSet<String>();
156 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
157 File f = semantics.interfaceName2Construct.get(interfaceName).file;
158 headers.addAll(semantics.srcFilesInfo.get(f).headers);
163 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
164 String headLine = funcDefine.get(0);
165 headLine = "static " + headLine;
166 funcDefine.set(0, headLine);
169 private static void makeVariablesStatic(ArrayList<String> varDecls) {
170 for (int i = 0; i < varDecls.size(); i++) {
171 String varDecl = varDecls.get(i);
172 varDecl = "static " + varDecl;
173 varDecls.set(i, varDecl);
177 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
178 InterfaceConstruct construct) {
179 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
180 String headerLine = content.get(construct.beginLineNum);
182 return SpecParser.parseFuncHeader(headerLine);
183 } catch (ParseException e) {
189 public static ArrayList<String> generateGlobalVarDeclaration(
190 SemanticsChecker semantics, GlobalConstruct construct) {
191 ArrayList<String> newCode = new ArrayList<String>();
192 HashSet<String> allHeaders = getAllHeaders(semantics);
194 // All headers needed by the interface decalration
195 newCode.add(COMMENT("/* Include all the header files that contains the interface declaration */"));
196 for (String header : allHeaders) {
197 newCode.add(INCLUDE(header));
200 // Other necessary headers
201 newCode.add(INCLUDE(HEADER_STDINT));
202 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
203 newCode.add(INCLUDE(HEADER_SPEC_LIB));
206 SequentialDefineSubConstruct code = construct.code;
207 // User-defined functions
208 newCode.add(COMMENT("All other user-defined functions"));
209 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
210 for (int i = 0; i < defineFuncs.size(); i++) {
211 ArrayList<String> defineFunc = defineFuncs.get(i);
212 makeFunctionStatic(defineFunc);
213 newCode.addAll(defineFunc);
217 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
218 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
220 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
221 // Define necessary info structure
222 newCode.add(COMMENT("Definition of interface info struct: "
224 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
225 newCode.add(COMMENT("End of info struct definition: "
229 // Define ID function
230 newCode.add(COMMENT("ID functions of interface: " + interfaceName));
231 newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
233 newCode.add(COMMENT("End of ID function: + " + interfaceName));
236 // Define check_action function
241 // User-defined variables
242 ArrayList<VariableDeclaration> varDecls = code.declareVar;
243 for (int i = 0; i < varDecls.size(); i++) {
244 VariableDeclaration varDecl = varDecls.get(i);
245 newCode.add(DECLARE(varDecl.type, varDecl.name));
248 // printCode(newCode);
252 private static ArrayList<String> generateHBInitAnnotation(
253 SemanticsChecker semantics) {
254 ArrayList<String> newCode = new ArrayList<String>();
255 int hbConditionInitIdx = 0;
256 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
257 for (ConditionalInterface right : semantics.getHBConditions().get(
259 String structVarName = "hbConditionInit" + hbConditionInitIdx;
260 String annotationVarName = "hb_init" + hbConditionInitIdx;
261 hbConditionInitIdx++;
262 String interfaceNumBefore = Integer
263 .toString(semantics.interface2Num
264 .get(left.interfaceName)), hbLabelNumBefore = Integer
265 .toString(semantics.hbLabel2Num
266 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
267 .toString(semantics.interface2Num
268 .get(right.interfaceName)), hbLabelNumAfter = Integer
269 .toString(semantics.hbLabel2Num
270 .get(right.hbConditionLabel));
271 newCode.add(COMMENT(left + " -> " + right));
273 newCode.add(ANNO_HB_INIT + " " + structVarName + ";");
274 newCode.add(ASSIGN(structVarName, "interface_num_before",
275 interfaceNumBefore));
276 newCode.add(ASSIGN(structVarName, "hb_condition_num_before",
278 newCode.add(ASSIGN(structVarName, "interface_num_after",
280 newCode.add(ASSIGN(structVarName, "hb_condition_num_after",
283 newCode.add(DECLARE(SPEC_ANNOTATION, annotationVarName));
284 newCode.add(ASSIGN(annotationVarName,
285 SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
286 newCode.add(ASSIGN_PTR(annotationVarName,
287 SPEC_ANNOTATION_FIELD_ANNO, structVarName));
288 newCode.add(ANNOTATE(annotationVarName));
294 public static ArrayList<String> generateInterfaceWrapper(
295 SemanticsChecker semantics, InterfaceConstruct construct) {
296 ArrayList<String> newCode = new ArrayList<String>();
298 // Generate necessary header file (might be redundant but never mind)
300 // Generate wrapper header
302 // Wrapper function body
306 // Call original renamed function
312 // End of the wrapper function
314 // printCode(newCode);
318 public static ArrayList<String> generatePotentialCPDefine(
319 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
320 ArrayList<String> newCode = new ArrayList<String>();
322 // Generate redundant header files
323 newCode.add(COMMENT("Automatically generated code for potential commit point: "
325 newCode.add(COMMENT("Include redundant headers"));
326 newCode.add(INCLUDE(HEADER_THREADS));
327 newCode.add(INCLUDE(HEADER_CDSTRACE));
329 // Some necessary function calls