1 /* $Id: CbeVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.cbe;
5 import java.util.HashMap;
6 import java.util.Vector;
8 import dol.datamodel.pn.Port;
9 import dol.datamodel.pn.Process;
10 import dol.datamodel.pn.ProcessNetwork;
11 import dol.util.Copier;
12 import dol.visitor.PNVisitor;
15 * This class is a class for a visitor that is used to generate
18 public class CbeVisitor extends PNVisitor {
23 * @param packageName name of the Cbe directory
25 public CbeVisitor(String packageName) {
26 _packageName = packageName;
30 * Visit process network.
32 * @param pn process network that needs to be rendered.
34 public void visitComponent(ProcessNetwork pn) {
36 File dir = new File(_packageName);
40 File lib = new File(_packageName + _delimiter + "lib");
44 File source = new File(_ui.getMySystemCLib().
45 replaceAll("systemC", "cbe").replace("%20", " "));
46 new Copier().copy(source, lib);
48 // Create the template
49 File template = new File(_packageName + _delimiter
54 source = new File(_ui.getMySystemCLib().
55 replaceAll("systemC", "cbe").
56 replace("lib", "template").replace("%20", " "));
57 new Copier().copy(source, template);
59 // Some library files must be copied to the main directory
60 (new File(lib.getPath() + _delimiter + "ppu_main.h")).
61 renameTo(new File (dir.getPath() + _delimiter
64 //copy process source code
65 source = new File(_srcDirName.replace("%20", " "));
66 new Copier().copy(source, dir);
69 pn.accept(new CbeMakefileVisitor(_packageName));
70 pn.accept(new CbeBuildFileVisitor(_packageName));
71 pn.accept(new CbeProcessVisitor(_packageName, _portMap));
72 pn.accept(new CbeModuleVisitor(_packageName, _portMap));
73 pn.accept(new CbeConstantVisitor(_packageName, _portMap));
76 System.out.println("CbeVisitor: exception occured: "
83 * Create a hashmap which maps each port of the given process network
84 * to an integer. For each process, ports are numbered with integers
87 * @param pn process network for which the map should be generated
89 protected void createPortMap(ProcessNetwork pn) {
90 _portMap = new HashMap<Port, Integer>();
92 for (Process process : pn.getProcessList()) {
94 Vector<Port> portList = process.getPortList();
95 Vector<String> portNameList = new Vector<String>();
97 HashMap<String, Integer> portMap =
98 new HashMap<String, Integer>();
101 for (int i = 0; i < portList.size(); i++) {
102 //treat single ports differently than iterated ports
103 String portName = portList.elementAt(i).getName();
104 String baseName = portList.elementAt(i).getBasename();
106 if (portName.equals(baseName)) {
107 portNameList.add(portName);
108 portMap.put(portName, portCount++);
110 String range_indices =
111 portList.elementAt(i).getRange();
112 Vector<Integer> range_indices_values =
113 getIndex(range_indices, ";");
115 String port_indices = portName;
116 port_indices.replaceAll(baseName, "");
117 Vector<Integer> port_indices_values =
118 getIndex(port_indices, "_");
120 if (!portNameList.contains(baseName)) {
121 portNameList.add(baseName);
122 portMap.put(baseName, portCount);
126 j < range_indices_values.size(); j++) {
127 size *= range_indices_values.elementAt(j);
132 int portId = portMap.get(baseName);
133 for (int j = 0; j < port_indices_values.size(); j++) {
136 k < range_indices_values.size(); k++) {
137 weight *= range_indices_values.elementAt(k);
139 portId += port_indices_values.elementAt(j)
142 portMap.put(portName, portId);
146 for (int i = 0; i < portList.size(); i++) {
147 _portMap.put(portList.elementAt(i),
148 portMap.get(portList.elementAt(i).getName()));
154 * Gets vector of indices of a string, where the index must be
155 * separated by the specified separator.
157 * getIndex("name_1_2", "_", 0) will return 1.
158 * getIndex("name_1_2", "_", 1) will return 2.
160 * @param range string to parse
161 * @param separator delimiter of indices
162 * @return vector of indices
164 protected Vector<Integer> getIndex(String range, String separator) {
165 Vector<Integer> indices = new Vector<Integer>();
166 String[] subranges = range.split(separator);
167 for (int i = 0; i < subranges.length; i++) {
169 int value = Integer.valueOf(subranges[i]);
171 } catch (Exception e) {
178 protected HashMap<Port, Integer> _portMap;
179 protected String _packageName = null;
181 protected String _srcDir = "";
182 protected static String _srcDirName = "src";