dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cbe / CbeVisitor.java
diff --git a/dol/src/dol/visitor/cbe/CbeVisitor.java b/dol/src/dol/visitor/cbe/CbeVisitor.java
new file mode 100644 (file)
index 0000000..d70b347
--- /dev/null
@@ -0,0 +1,183 @@
+/* $Id: CbeVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.cbe;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Vector;
+
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.util.Copier;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate
+ * a CBE package.
+ */
+public class CbeVisitor extends PNVisitor {
+
+    /**
+     * Constructor.
+     *
+     * @param packageName name of the Cbe directory
+     */
+    public CbeVisitor(String packageName) {
+        _packageName = packageName;
+    }
+
+    /**
+     * Visit process network.
+     *
+     * @param pn process network that needs to be rendered.
+     */
+    public void visitComponent(ProcessNetwork pn) {
+        try {
+            File dir = new File(_packageName);
+            dir.mkdirs();
+
+            // Create the library
+            File lib = new File(_packageName + _delimiter + "lib");
+            lib.mkdirs();
+
+            //copy library files
+            File source = new File(_ui.getMySystemCLib().
+                    replaceAll("systemC", "cbe").replace("%20", " "));
+            new Copier().copy(source, lib);
+
+            // Create the template
+            File template = new File(_packageName + _delimiter
+                    + "template");
+            template.mkdirs();
+
+            //copy the templates
+            source = new File(_ui.getMySystemCLib().
+                    replaceAll("systemC", "cbe").
+                    replace("lib", "template").replace("%20", " "));
+            new Copier().copy(source, template);
+
+            // Some library files must be copied to the main directory
+            (new File(lib.getPath() + _delimiter + "ppu_main.h")).
+                    renameTo(new File (dir.getPath() + _delimiter
+                    + "ppu_main.h"));
+
+            //copy process source code
+            source = new File(_srcDirName.replace("%20", " "));
+            new Copier().copy(source, dir);
+
+            createPortMap(pn);
+            pn.accept(new CbeMakefileVisitor(_packageName));
+            pn.accept(new CbeBuildFileVisitor(_packageName));
+            pn.accept(new CbeProcessVisitor(_packageName, _portMap));
+            pn.accept(new CbeModuleVisitor(_packageName, _portMap));
+            pn.accept(new CbeConstantVisitor(_packageName, _portMap));
+        }
+        catch (Exception e) {
+            System.out.println("CbeVisitor: exception occured: "
+                    + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Create a hashmap which maps each port of the given process network
+     * to an integer. For each process, ports are numbered with integers
+     * starting from 0.
+     *
+     * @param pn process network for which the map should be generated
+     */
+    protected void createPortMap(ProcessNetwork pn) {
+        _portMap = new HashMap<Port, Integer>();
+
+        for (Process process : pn.getProcessList()) {
+            int portCount = 0;
+            Vector<Port> portList = process.getPortList();
+            Vector<String> portNameList = new Vector<String>();
+            portNameList.clear();
+            HashMap<String, Integer> portMap =
+                new HashMap<String, Integer>();
+            portMap.clear();
+
+            for (int i = 0; i < portList.size(); i++) {
+                //treat single ports differently than iterated ports
+                String portName = portList.elementAt(i).getName();
+                String baseName = portList.elementAt(i).getBasename();
+
+                if (portName.equals(baseName)) {
+                    portNameList.add(portName);
+                    portMap.put(portName, portCount++);
+                } else {
+                    String range_indices =
+                            portList.elementAt(i).getRange();
+                    Vector<Integer> range_indices_values =
+                            getIndex(range_indices, ";");
+
+                    String port_indices = portName;
+                    port_indices.replaceAll(baseName, "");
+                    Vector<Integer> port_indices_values =
+                            getIndex(port_indices, "_");
+
+                    if (!portNameList.contains(baseName)) {
+                        portNameList.add(baseName);
+                        portMap.put(baseName, portCount);
+
+                        int size = 1;
+                        for (int j = 0;
+                                j < range_indices_values.size(); j++) {
+                            size *= range_indices_values.elementAt(j);
+                        }
+                        portCount += size;
+                    }
+
+                    int portId = portMap.get(baseName);
+                    for (int j = 0; j < port_indices_values.size(); j++) {
+                        int weight = 1;
+                        for (int k = j + 1;
+                                k < range_indices_values.size(); k++) {
+                            weight *= range_indices_values.elementAt(k);
+                        }
+                        portId += port_indices_values.elementAt(j)
+                                * weight;
+                    }
+                    portMap.put(portName, portId);
+                }
+            }
+
+            for (int i = 0; i < portList.size(); i++) {
+                _portMap.put(portList.elementAt(i),
+                        portMap.get(portList.elementAt(i).getName()));
+            }
+        }
+    }
+
+    /**
+     * Gets vector of indices of a string, where the index must be
+     * separated by the specified separator.
+     * examples:
+     * getIndex("name_1_2", "_", 0) will return 1.
+     * getIndex("name_1_2", "_", 1) will return 2.
+     *
+     * @param range string to parse
+     * @param separator delimiter of indices
+     * @return vector of indices
+     */
+    protected Vector<Integer> getIndex(String range, String separator) {
+        Vector<Integer> indices = new Vector<Integer>();
+        String[] subranges = range.split(separator);
+        for (int i = 0; i < subranges.length; i++) {
+            try {
+                int value = Integer.valueOf(subranges[i]);
+                indices.add(value);
+            } catch (Exception e) {
+                continue;
+            }
+        }
+        return indices;
+    }
+
+    protected HashMap<Port, Integer> _portMap;
+    protected String _packageName = null;
+
+    protected String _srcDir = "";
+    protected static String _srcDirName = "src";
+}