dol: initial dol commit
[jump.git] / dol / src / dol / visitor / rtems / RtemsProcessVisitor.java
diff --git a/dol/src/dol/visitor/rtems/RtemsProcessVisitor.java b/dol/src/dol/visitor/rtems/RtemsProcessVisitor.java
new file mode 100644 (file)
index 0000000..27b02a1
--- /dev/null
@@ -0,0 +1,168 @@
+/* $Id: RtemsProcessVisitor.java 213 2010-10-20 09:40:59Z khuang $ */
+package dol.visitor.rtems;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Vector;
+
+import dol.datamodel.pn.Configuration;
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.datamodel.pn.SourceCode;
+import dol.util.Copier;
+import dol.util.Sed;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate
+ * a wrapper class for a process.
+ */
+public class RtemsProcessVisitor extends PNVisitor {
+
+    /**
+     * Constructor.
+     *
+     * @param dir target directory
+     */
+    public RtemsProcessVisitor(String dir,
+                               HashMap<Port, Integer> portMap,
+                               HashMap<Process, Integer> sinkMap) {
+        _dir = dir;
+        _portMap = portMap;
+        _sinkMap = sinkMap;
+    }
+
+    /**
+     *
+     * @param x process network that needs to be processed
+     */
+    public void visitComponent(ProcessNetwork x) {
+        try {
+            Vector<String> pList = new Vector<String>();
+
+            for (Process p : x.getProcessList()) {
+                String basename = p.getBasename();
+                if (!pList.contains(basename)) {
+                    pList.add(basename);
+                    p.accept(this);
+                }
+            }
+        }
+        catch (Exception e) {
+            System.out.println("RtemsProcessVisitor: exception "
+                    + "occured: " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Visit process.
+     *
+     * @param p process that needs to be processed
+     */
+    public void visitComponent(Process p) {
+        try {
+            String filename = _dir + _delimiter + p.getBasename()
+                    + "_wrapper.c";
+            File process_file = new File(filename);
+            File pattern_file = new File(_dir + _delimiter
+                    + "process_wrapper_template.c");
+            new Copier().copyFile(pattern_file, process_file);
+
+            String includes = "";
+            for (SourceCode code : p.getSrcList()) {
+                includes += "#include \"" + code.getLocality() + "\""
+                        + System.getProperty("line.separator");
+            }
+
+            Sed sed = new Sed();
+            sed.sed(filename, "//#include \"@PROCESSNAME@.c\"", includes);
+            sed.sed(filename, "@PROCESSNAME@", p.getBasename());
+            sed.sed(filename, "@Processname@",
+                    p.getBasename().substring(0, 1).toUpperCase() +
+                    p.getBasename().substring(1));
+
+            String triggerPeriod = null;
+            String burstSize = null;
+            String burstPosition = null;
+            for (Configuration c : p.getCfgList()) {
+                if (c.getName().equals("triggerPeriod")) {
+                    triggerPeriod = c.getValue();
+                } else if (c.getName().equals("burstSize")) {
+                    burstSize = c.getValue();
+                } else if (c.getName().equals("burstPosition")) {
+                    burstPosition = c.getValue();
+                }
+            }
+            if (triggerPeriod != null) {
+                if (burstSize == null) {
+                    burstSize = "0";
+                }
+                if (burstPosition == null) {
+                    burstPosition = "0";
+                }
+
+                String periodicTrigger =
+                        "//limits the number of process activations\n"
+                        + "        while ((((int)get_cycle() - "
+                        + "1511576) / " + triggerPeriod + ") "
+                        + "< number_of_activations - (((int)get_cycle()"
+                    //  + " - 1511576) > " + burstPosition + " ? "
+                        + " ) > " + burstPosition + " ? "
+                        + burstSize + " : 0)) {\n"
+                        + "            rtems_task_set_priority("
+                        + "RTEMS_SELF, 127, &old_priority);\n"
+                        + "            rtems_task_wake_after("
+                        + "RTEMS_YIELD_PROCESSOR);\n"
+                        + "        }\n"
+                        + "        rtems_task_set_priority(RTEMS_SELF, "
+                        + "wrapper->priority, &old_priority);\n";
+
+                sed.sed(filename,
+                        "//placeholder for periodic trigger",
+                        periodicTrigger);
+            }
+
+            if (!p.hasOutPorts()) {
+                sed.sed(filename, "@ENDING_SHAPER@",
+                        "ss_sem_signal(get_sem_terminate("
+                        + _sinkMap.get(p) + "));");
+            } else {
+                sed.sed(filename, "@ENDING_SHAPER@", "");
+            }
+
+            for(SourceCode sourceCode : p.getSrcList()) {
+                filename = _dir + _delimiter
+                        + sourceCode.getLocality().replaceAll(
+                        "(.*)\\.[cC][pP]*[pP]*", "$1\\.h");
+                sed.sed(filename, "<dol.h>", "\"dol.h\"");
+
+                for (Port port : p.getPortList()) {
+                    Integer portId = _portMap.get(port);
+                    if (!port.getBasename().equals(port.getName())) {
+                        for (Port port2 : p.getPortList()) {
+                            if (port2.getBasename().equals(port.getBasename())) {
+                                if (_portMap.get(port2) < _portMap.get(port)) {
+                                    portId = _portMap.get(port2);
+                                }
+                            }
+                        }
+                    }
+                    sed.sed(filename, "(#define[ ]+PORT_\\w*[ ]+)\"?"
+                            + port.getBasename() + "\"?",
+                            "$1 " + portId);
+                }
+            }
+        }
+        catch (Exception e) {
+            System.out.println("RtemsProcessVisitor: exception "
+                    + "occured: " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    protected String _dir = null;
+    protected HashMap<Port, Integer> _portMap;
+    protected HashMap<Process, Integer> _sinkMap;
+}