dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cbe / CbeVisitor.java
1 /* $Id: CbeVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.cbe;
3
4 import java.io.File;
5 import java.util.HashMap;
6 import java.util.Vector;
7
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;
13
14 /**
15  * This class is a class for a visitor that is used to generate
16  * a CBE package.
17  */
18 public class CbeVisitor extends PNVisitor {
19
20     /**
21      * Constructor.
22      *
23      * @param packageName name of the Cbe directory
24      */
25     public CbeVisitor(String packageName) {
26         _packageName = packageName;
27     }
28
29     /**
30      * Visit process network.
31      *
32      * @param pn process network that needs to be rendered.
33      */
34     public void visitComponent(ProcessNetwork pn) {
35         try {
36             File dir = new File(_packageName);
37             dir.mkdirs();
38
39             // Create the library
40             File lib = new File(_packageName + _delimiter + "lib");
41             lib.mkdirs();
42
43             //copy library files
44             File source = new File(_ui.getMySystemCLib().
45                     replaceAll("systemC", "cbe").replace("%20", " "));
46             new Copier().copy(source, lib);
47
48             // Create the template
49             File template = new File(_packageName + _delimiter
50                     + "template");
51             template.mkdirs();
52
53             //copy the templates
54             source = new File(_ui.getMySystemCLib().
55                     replaceAll("systemC", "cbe").
56                     replace("lib", "template").replace("%20", " "));
57             new Copier().copy(source, template);
58
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
62                     + "ppu_main.h"));
63
64             //copy process source code
65             source = new File(_srcDirName.replace("%20", " "));
66             new Copier().copy(source, dir);
67
68             createPortMap(pn);
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));
74         }
75         catch (Exception e) {
76             System.out.println("CbeVisitor: exception occured: "
77                     + e.getMessage());
78             e.printStackTrace();
79         }
80     }
81
82     /**
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
85      * starting from 0.
86      *
87      * @param pn process network for which the map should be generated
88      */
89     protected void createPortMap(ProcessNetwork pn) {
90         _portMap = new HashMap<Port, Integer>();
91
92         for (Process process : pn.getProcessList()) {
93             int portCount = 0;
94             Vector<Port> portList = process.getPortList();
95             Vector<String> portNameList = new Vector<String>();
96             portNameList.clear();
97             HashMap<String, Integer> portMap =
98                 new HashMap<String, Integer>();
99             portMap.clear();
100
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();
105
106                 if (portName.equals(baseName)) {
107                     portNameList.add(portName);
108                     portMap.put(portName, portCount++);
109                 } else {
110                     String range_indices =
111                             portList.elementAt(i).getRange();
112                     Vector<Integer> range_indices_values =
113                             getIndex(range_indices, ";");
114
115                     String port_indices = portName;
116                     port_indices.replaceAll(baseName, "");
117                     Vector<Integer> port_indices_values =
118                             getIndex(port_indices, "_");
119
120                     if (!portNameList.contains(baseName)) {
121                         portNameList.add(baseName);
122                         portMap.put(baseName, portCount);
123
124                         int size = 1;
125                         for (int j = 0;
126                                 j < range_indices_values.size(); j++) {
127                             size *= range_indices_values.elementAt(j);
128                         }
129                         portCount += size;
130                     }
131
132                     int portId = portMap.get(baseName);
133                     for (int j = 0; j < port_indices_values.size(); j++) {
134                         int weight = 1;
135                         for (int k = j + 1;
136                                 k < range_indices_values.size(); k++) {
137                             weight *= range_indices_values.elementAt(k);
138                         }
139                         portId += port_indices_values.elementAt(j)
140                                 * weight;
141                     }
142                     portMap.put(portName, portId);
143                 }
144             }
145
146             for (int i = 0; i < portList.size(); i++) {
147                 _portMap.put(portList.elementAt(i),
148                         portMap.get(portList.elementAt(i).getName()));
149             }
150         }
151     }
152
153     /**
154      * Gets vector of indices of a string, where the index must be
155      * separated by the specified separator.
156      * examples:
157      * getIndex("name_1_2", "_", 0) will return 1.
158      * getIndex("name_1_2", "_", 1) will return 2.
159      *
160      * @param range string to parse
161      * @param separator delimiter of indices
162      * @return vector of indices
163      */
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++) {
168             try {
169                 int value = Integer.valueOf(subranges[i]);
170                 indices.add(value);
171             } catch (Exception e) {
172                 continue;
173             }
174         }
175         return indices;
176     }
177
178     protected HashMap<Port, Integer> _portMap;
179     protected String _packageName = null;
180
181     protected String _srcDir = "";
182     protected static String _srcDirName = "src";
183 }