From: Peizhao Ou Date: Tue, 29 Oct 2013 03:52:05 +0000 (-0700) Subject: add more X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=efb1d30419ff984d1c06d338ec453fd8b0780d74;p=cdsspec-compiler.git add more --- diff --git a/notes/generated_code_examples.txt b/notes/generated_code_examples.txt index 95e649a..1658001 100644 --- a/notes/generated_code_examples.txt +++ b/notes/generated_code_examples.txt @@ -12,7 +12,7 @@ Global Variable Declaration #include /* All other user-defined functions */ -ALL_USER_DEFINED_FUNCTIONS +ALL_USER_DEFINED_FUNCTIONS // Make them static /* Definition of interface info struct (per interface) */ typedef struct Put_info { @@ -29,7 +29,7 @@ typedef struct Get_info { /* ID functions of interface */ static id_t Put_id() { - id_t id = PUT_ID; + id_t id = PUT_ID; // Default ID == 0; return id; } @@ -39,10 +39,7 @@ static id_t Get_id() { } /* End of ID functions */ -/* Initialization of interface<->function_ptr table */ -#define INTERFACE_SIZE 2 -void* func_ptr_table[INTERFACE_SIZE * 2] = { - CLASS + /* Check_action function of interfaces */ bool Put_check_action(void *info, id_t __ID__) { @@ -78,6 +75,9 @@ bool Get_check_action(void *info, id_t __ID__) { } /* End of check_action function definitions */ +/* Initialization of interface<->function_ptr table */ +#define INTERFACE_SIZE 2 +void* func_ptr_table[INTERFACE_SIZE * 2]; /* Beginning of other user-defined variables */ bool lock_acquired; @@ -87,6 +87,8 @@ int reader_lock_cnt; /* Define function for sequential code initialization */ void __sequential_init() { + /* Init func_ptr_table */ + /* Init user-defined variables */ lock_acquired = false; reader_lock_cnt = 0; diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java index e2ae18b..2900601 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java @@ -4,10 +4,14 @@ import java.util.ArrayList; import java.util.HashSet; import java.io.File; +import edu.uci.eecs.specCompiler.grammerParser.ParseException; +import edu.uci.eecs.specCompiler.grammerParser.SpecParser; import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface; +import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader; import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct; import edu.uci.eecs.specCompiler.specExtraction.IDExtractor; import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ParserUtils; import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct; import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor; @@ -28,6 +32,7 @@ public class CodeVariables { public static final String HEADER_CDSTRACE = ""; public static final String CDSAnnotate = "cdsannotate"; public static final String CDSAnnotateType = "SPEC_ANALYSIS"; + public static final String IDType = "id_t"; public static final String SPEC_ANNO_TYPE = "spec_anno_type"; public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT"; @@ -50,6 +55,7 @@ public class CodeVariables { // Specification variables public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_"; + public static final String DEFAULT_ID = "0"; // Specification library public static final String HEADER_SPEC_LIB = ""; @@ -106,6 +112,11 @@ public class CodeVariables { return type + " " + name + ";"; } + private static String DECLARE(VariableDeclaration varDecl) { + String type = varDecl.type, name = varDecl.name; + return type + " " + name + ";"; + } + private static String DECLARE_DEFINE(String type, String var, String val) { return type + " " + var + " = " + val + ";"; } @@ -114,6 +125,32 @@ public class CodeVariables { return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");"; } + private static ArrayList DEFINE_INFO_STRUCT(String interfaceName, + FunctionHeader funcDecl) { + ArrayList code = new ArrayList(); + code.add("typedef struct " + interfaceName + "_info {"); + code.add(DECLARE(funcDecl.returnType, MACRO_RETURN)); + for (int i = 0; i < funcDecl.args.size(); i++) { + code.add(DECLARE(funcDecl.args.get(i))); + } + code.add("} " + interfaceName + "_info {"); + return code; + } + + private static ArrayList DEFINE_ID_FUNC(String interfaceName, + String idCode) { + ArrayList code = new ArrayList(); + code.add("static " + IDType + " " + interfaceName + "_id() {"); + if (idCode != null) { + code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode)); + } else { + code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID)); + } + code.add("return " + MACRO_ID + ";"); + code.add("}"); + return code; + } + private static HashSet getAllHeaders(SemanticsChecker semantics) { HashSet headers = new HashSet(); for (String interfaceName : semantics.interfaceName2Construct.keySet()) { @@ -123,6 +160,32 @@ public class CodeVariables { return headers; } + private static void makeFunctionStatic(ArrayList funcDefine) { + String headLine = funcDefine.get(0); + headLine = "static " + headLine; + funcDefine.set(0, headLine); + } + + private static void makeVariablesStatic(ArrayList varDecls) { + for (int i = 0; i < varDecls.size(); i++) { + String varDecl = varDecls.get(i); + varDecl = "static " + varDecl; + varDecls.set(i, varDecl); + } + } + + private static FunctionHeader getFunctionHeader(SemanticsChecker semantics, + InterfaceConstruct construct) { + ArrayList content = semantics.srcFilesInfo.get(construct.file).content; + String headerLine = content.get(construct.beginLineNum); + try { + return SpecParser.parseFuncHeader(headerLine); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + public static ArrayList generateGlobalVarDeclaration( SemanticsChecker semantics, GlobalConstruct construct) { ArrayList newCode = new ArrayList(); @@ -145,13 +208,35 @@ public class CodeVariables { newCode.add(COMMENT("All other user-defined functions")); ArrayList> defineFuncs = code.defineFuncs; for (int i = 0; i < defineFuncs.size(); i++) { - newCode.addAll(defineFuncs.get(i)); + ArrayList defineFunc = defineFuncs.get(i); + makeFunctionStatic(defineFunc); + newCode.addAll(defineFunc); newCode.add(""); } - // Define necessary info structure - newCode.add(COMMENT("Definition of interface info struct (per interface)")); - + for (String interfaceName : semantics.interfaceName2Construct.keySet()) { + InterfaceConstruct iConstruct = semantics.interfaceName2Construct + .get(interfaceName); + FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct); + // Define necessary info structure + newCode.add(COMMENT("Definition of interface info struct: " + + interfaceName)); + newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader)); + newCode.add(COMMENT("End of info struct definition: " + + interfaceName)); + newCode.add(""); + + // Define ID function + newCode.add(COMMENT("ID functions of interface: " + interfaceName)); + newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode)); + newCode.add(""); + newCode.add(COMMENT("End of ID function: + " + interfaceName)); + newCode.add(""); + + // Define check_action function + + + } // User-defined variables ArrayList varDecls = code.declareVar; diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java b/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java index c70aa94..0ce5332 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java @@ -2,6 +2,9 @@ package edu.uci.eecs.specCompiler.specExtraction; import java.util.ArrayList; +import edu.uci.eecs.specCompiler.grammerParser.ParseException; +import edu.uci.eecs.specCompiler.grammerParser.SpecParser; + public class ParserUtils { public static String trimSpace(String line) { int i, j;