dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cell / CellVisitor.java
1 package dol.visitor.cell;
2
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.Vector;
6
7 import dol.datamodel.pn.Port;
8 import dol.datamodel.pn.Process;
9 import dol.datamodel.pn.ProcessNetwork;
10 import dol.main.UserInterface;
11 import dol.visitor.PNVisitor;
12 import dol.util.Copier;
13
14 /**
15  * This class is a class for a visitor that is used to generate
16  * a CELL package.
17  */
18 public class CellVisitor extends PNVisitor {
19
20     /**
21      * Constructor.
22      *
23      * @param packageName name of the Cell directory
24      */
25     public CellVisitor(String packageName) {
26         _packageName = packageName;
27         _ui = UserInterface.getInstance();
28     }
29
30     /**
31      * Visit process network.
32      *
33      * @param pn process network that needs to be rendered.
34      */
35     public void visitComponent(ProcessNetwork pn) {
36         try {
37             File dir = new File(_packageName);
38             dir.mkdirs();
39
40             // Create the library
41             File lib = new File(_packageName + _delimiter + "lib");
42             lib.mkdirs();
43
44             //copy library files
45             File source = new File(_ui.getMySystemCLib().
46                     replaceAll("systemC", "cell").replace("%20", " "));
47             new Copier().copy(source, lib);
48
49             // Create the template
50             File template = new File(_packageName + _delimiter + "template");
51             template.mkdirs();
52
53             //copy the templates
54             source = new File(_ui.getMySystemCLib().replaceAll("systemC", "cell").replace("lib", "template").replace("%20", " "));
55             new Copier().copy(source, template);
56
57             // Some library files must be copied to the main directory
58             (new File(lib.getPath() + _delimiter + "ppu_main.h")).renameTo(new File (dir.getPath() + _delimiter + "ppu_main.h"));
59
60             //copy process source code
61             source = new File(_srcDirName.replace("%20", " "));
62             new Copier().copy(source, dir);
63
64             createPortMap(pn);
65
66             // Create the mapping for this process network on the cell
67             CellMapping mapping = new CellMapping(pn, "predefined", true, MAX_SPUS);
68
69             pn.accept(new CellMakefileVisitor(_packageName, mapping));
70             pn.accept(new CellProcessVisitor(_packageName, _portMap, mapping));
71             pn.accept(new CellModuleVisitor(_packageName, _portMap, mapping));
72             pn.accept(new CellConstantVisitor(_packageName, mapping));
73             pn.accept(new CellSPEVisitor(_packageName, mapping));
74             pn.accept(new CellPPEVisitor(_packageName, mapping));
75         }
76         catch (Exception e) {
77             System.out.println("CellVisitor: exception occured: "
78                     + e.getMessage());
79             e.printStackTrace();
80         }
81     }
82
83     /**
84      * Create a hashmap which maps each port of the given process network
85      * to an integer. For each process, ports are numbered with integers
86      * starting from 0.
87      *
88      * @param pn process network for which the map should be generated
89      */
90     protected void createPortMap(ProcessNetwork pn) {
91         _portMap = new HashMap<Port, Integer>();
92
93         for (Process process : pn.getProcessList()) {
94             int portCount = 0;
95             Vector<Port> portList = process.getPortList();
96             Vector<String> portNameList = new Vector<String>();
97             portNameList.clear();
98             HashMap<String, Integer> portMap =
99                 new HashMap<String, Integer>();
100             portMap.clear();
101
102             for (int i = 0; i < portList.size(); i++) {
103                 //treat single ports differently than iterated ports
104                 String portName = portList.elementAt(i).getName();
105                 String baseName = portList.elementAt(i).getBasename();
106
107                 if (portName.equals(baseName)) {
108                     portNameList.add(portName);
109                     portMap.put(portName, portCount++);
110                 } else {
111                     String range_indices = portList.elementAt(i).getRange();
112                     Vector<Integer> range_indices_values = getIndex(range_indices, ";");
113
114                     String port_indices = portName;
115                     port_indices.replaceAll(baseName, "");
116                     Vector<Integer> port_indices_values = getIndex(port_indices, "_");
117
118                     if (!portNameList.contains(baseName)) {
119                         portNameList.add(baseName);
120                         portMap.put(baseName, portCount);
121
122                         int size = 1;
123                         for (int j = 0; j < range_indices_values.size(); j++) {
124                             size *= range_indices_values.elementAt(j);
125                         }
126                         portCount += size;
127                     }
128
129                     int portId = portMap.get(baseName);
130                     for (int j = 0; j < port_indices_values.size(); j++) {
131                         int weight = 1;
132                         for (int k = j + 1; k < range_indices_values.size(); k++) {
133                             weight *= range_indices_values.elementAt(k);
134                         }
135                         portId += port_indices_values.elementAt(j) * weight;
136                     }
137                     portMap.put(portName, portId);
138                 }
139             }
140
141             for (int i = 0; i < portList.size(); i++) {
142                 _portMap.put(portList.elementAt(i),
143                         portMap.get(portList.elementAt(i).getName()));
144             }
145         }
146     }
147
148     /**
149      * Gets vector of indices of a string, where the index must be
150      * separated by the specified separator.
151      * examples:
152      * getIndex("name_1_2", "_", 0) will return 1.
153      * getIndex("name_1_2", "_", 1) will return 2.
154      *
155      * @param range string to parse
156      * @param separator delimiter of indices
157      * @return vector of indices
158      */
159     protected Vector<Integer> getIndex(String range, String separator) {
160         Vector<Integer> indices = new Vector<Integer>();
161         String[] subranges = range.split(separator);
162         for (int i = 0; i < subranges.length; i++) {
163             try {
164                 int value = Integer.valueOf(subranges[i]);
165                 indices.add(value);
166             } catch (Exception e) {
167                 continue;
168             }
169         }
170         return indices;
171     }
172
173     public final static int MAX_SPUS = 6;
174     protected HashMap<Port, Integer> _portMap;
175     protected String _packageName = null;
176
177     protected String _srcDir = "";
178     protected static String _srcDirName = "src";
179 }