From c921f9d3e59e93a475f9c408e11a1e671fcbe80a Mon Sep 17 00:00:00 2001 From: Ludovic Apvrille <ludovic.apvrille@telecom-paristech.fr> Date: Thu, 31 Mar 2011 07:54:53 +0000 Subject: [PATCH] AVATAR: Update on code generator --- executablecode/generated_src/main.c | 12 -- executablecode/src/defs.h | 6 + .../toexecutable/AVATAR2CPOSIX.java | 120 +++++++++++++++++- .../toexecutable/TaskFile.java | 28 +++- 4 files changed, 149 insertions(+), 17 deletions(-) delete mode 100644 executablecode/generated_src/main.c create mode 100644 executablecode/src/defs.h diff --git a/executablecode/generated_src/main.c b/executablecode/generated_src/main.c deleted file mode 100644 index bce8999af4..0000000000 --- a/executablecode/generated_src/main.c +++ /dev/null @@ -1,12 +0,0 @@ -#include <stdio.h> -#include <pthread.h> -#include <unistd.h> -#include <stdlib.h> - -#include "request.h" -#include "syncchannel.h" -#include "request_manager.h" -#include "debug.h" - -int main(int argc, char *argv[]) { -} \ No newline at end of file diff --git a/executablecode/src/defs.h b/executablecode/src/defs.h new file mode 100644 index 0000000000..d04603858e --- /dev/null +++ b/executablecode/src/defs.h @@ -0,0 +1,6 @@ +#ifndef DEFS_H +#define DEFS_H + +#define bool int +#define true 1 +#define false 0 diff --git a/src/avatartranslator/toexecutable/AVATAR2CPOSIX.java b/src/avatartranslator/toexecutable/AVATAR2CPOSIX.java index 75cdcbb1bf..9fec99fd11 100755 --- a/src/avatartranslator/toexecutable/AVATAR2CPOSIX.java +++ b/src/avatartranslator/toexecutable/AVATAR2CPOSIX.java @@ -70,10 +70,19 @@ public class AVATAR2CPOSIX { public void saveInFiles(String path) throws FileException { + + TraceManager.addDev("Generating files"); + if (mainFile != null) { + TraceManager.addDev("Generating main files in " + path + mainFile.getName() + ".h"); FileUtils.saveFile(path + mainFile.getName() + ".h", mainFile.getHeaderCode()); FileUtils.saveFile(path + mainFile.getName() + ".c", mainFile.getMainCode()); } + + for(TaskFile taskFile: taskFiles) { + FileUtils.saveFile(path + taskFile.getName() + ".h", taskFile.getFullHeaderCode()); + FileUtils.saveFile(path + taskFile.getName() + ".c", taskFile.getMainCode()); + } } @@ -85,20 +94,38 @@ public class AVATAR2CPOSIX { public void generateCPOSIX(boolean _debug) { mainFile = new MainFile("main"); + taskFiles = new Vector<TaskFile>(); + + makeMainMutex(); + + makeSynchronousChannels(); + makeTasks(); + } + + public void makeMainMutex() { // Create a main mutex mainFile.appendToHCode("/* Main mutex */" + CR); mainFile.appendToBeforeMainCode("/* Main mutex */" + CR); mainFile.appendToHCode("extern pthread_mutex_t mainMutex;" + CR + CR); mainFile.appendToBeforeMainCode("pthread_mutex_t mainMutex;" + CR + CR); + } + + public void makeSynchronousChannels() { // Create a synchronous channel per relation/signal mainFile.appendToHCode("/* Synchronous channels */" + CR); mainFile.appendToBeforeMainCode("/* Synchronous channels */" + CR); + mainFile.appendToMainCode("/* Synchronous channels */" + CR); for(AvatarRelation ar: avspec.getRelations()) { if (!ar.isAsynchronous()) { - + for(int i=0; i<ar.nbOfSignals(); i++) { + mainFile.appendToHCode("extern syncchannel " + getChannelName(ar, i) + ";" + CR); + mainFile.appendToBeforeMainCode("syncchannel " + getChannelName(ar, i) + ";" + CR); + mainFile.appendToMainCode(getChannelName(ar, i) + ".inname =\"" + ar.getInSignal(i).getName() + "\";" + CR); + mainFile.appendToMainCode(getChannelName(ar, i) + ".outname =\"" + ar.getOutSignal(i).getName() + "\";" + CR); + } } } @@ -106,4 +133,95 @@ public class AVATAR2CPOSIX { } + public void makeTasks() { + for(AvatarBlock block: avspec.getListOfBlocks()) { + makeTask(block); + } + } + + public void makeTask(AvatarBlock block) { + TaskFile taskFile = new TaskFile(block.getName()); + + //taskFile.addToHeaderCode("#include \"main.h\"" + CR); + + //taskFile.addToMainCode("#include \"" + block.getName() + ".h\""); + + + defineAllStates(block, taskFile); + + defineAllMethods(block, taskFile); + + makeMainFunction(block, taskFile); + + taskFiles.add(taskFile); + } + + public void defineAllStates(AvatarBlock _block, TaskFile _taskFile) { + int id = 0; + for (AvatarStateMachineElement asme: _block.getStateMachine().getListOfElements()) { + if (asme instanceof AvatarState) { + _taskFile.addToMainCode("#define STATE__" + asme.getName() + " " + id + CR); + id ++; + } + } + } + + public void defineAllMethods(AvatarBlock _block, TaskFile _taskFile) { + String ret = ""; + LinkedList<AvatarAttribute> list; + + for (AvatarMethod am: _block.getMethods()) { + list = am.getListOfReturnAttributes(); + if (list.size() == 0) { + ret += "void"; + } else { + ret += getCTypeOf(list.get(0)); + } + ret += " " + am.getName() + "("; + list = am.getListOfAttributes(); + int cpt = 0; + for(AvatarAttribute aa: list) { + ret += getCTypeOf(aa) + " " + aa.getName() + } + + ret += ") {" + CR + "printf(\"Entering method " + am.getName() + "\");" + CR + "}" + CR + CR; + } + _taskFile.addToMainCode(ret + CR); + + } + + public void makeMainFunction(AvatarBlock _block, TaskFile _taskFile) { + String s = "void mainFunc_" + _block.getName() + "(void *arg)" + String sh = "extern " + s + ";" + CR; + s+= {" + CR; + + s += makeAttributesDeclaration(_block, _taskFile); + + s += "}\n" + CR; + _taskFile.addToMainCode(s + CR); + _taskFile.addToHeaderCode(sh + CR); + } + + + public String makeAttributesDeclaration(AvatarBlock _block, TaskFile _taskFile) { + String ret = ""; + for(AvatarAttribute aa: _block.getAttributes()) { + ret += getCTypeOf(aa) + " " + aa.getName() + " = " + aa.getInitialValue() + ";" + CR; + } + return ret; + } + + + public String getCTypeOf(AvatarAttribute _aa) { + String ret = "int"; + if (_aa.getType() == AvatarType.BOOLEAN) { + ret = "bool"; + } + return ret; + } + + public String getChannelName(AvatarRelation _ar, int _index) { + return _ar.block1.getName() + "_" + _ar.getSignal1(_index).getName() + "__" + _ar.block2.getName() + "_" + _ar.getSignal2(_index).getName(); + } + } \ No newline at end of file diff --git a/src/avatartranslator/toexecutable/TaskFile.java b/src/avatartranslator/toexecutable/TaskFile.java index e4c284b77d..cb1c5f5def 100755 --- a/src/avatartranslator/toexecutable/TaskFile.java +++ b/src/avatartranslator/toexecutable/TaskFile.java @@ -54,25 +54,45 @@ import avatartranslator.*; public class TaskFile { private final static String INCLUDE_HEADER = "#include <stdio.h>\n#include <pthread.h>\n#include <unistd.h>\n#include <stdlib.h>\n"; - private final static String LOCAL_INCLUDE_HEADER = "#include \"request.h\"\n#include \"syncchannel.h\"\n#include \"request_manager.h\"\n#include \"debug.h\""; + private final static String LOCAL_INCLUDE_HEADER = "#include \"request.h\"\n#include \"syncchannel.h\"\n#include \"request_manager.h\"\n#include \"debug.h\"\n#include \"defs.h\"\n#include \"main.h\""; + + private final static String CR = "\n"; private String name; + private String headerCode; + private String mainCode; + public TaskFile(String _name) { name = _name; + headerCode = ""; + mainCode = ""; } public String getName() { return name; } - public String getHeaderCode() { - return ""; + public String getFullHeaderCode() { + String s = "#ifndef " + name + "_H\n#define " + name + "_H\n"; + s += INCLUDE_HEADER + CR + LOCAL_INCLUDE_HEADER + CR + CR; + s += headerCode; + s += "#endif\n"; + return s; } public String getMainCode() { - return INCLUDE_HEADER + "\n" + LOCAL_INCLUDE_HEADER; + return "#include \"" + name + ".h\"" + CR + CR + mainCode; + } + + public void addToHeaderCode(String _code) { + headerCode += _code; } + public void addToMainCode(String _code) { + mainCode += _code; + } + + } \ No newline at end of file -- GitLab