1 package dol.visitor.Epiphany;
3 import java.util.Vector;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
9 import java.util.jar.JarFile;
10 import dol.util.JarCopier;
11 import dol.util.Copier;
15 import dol.visitor.PNVisitor;
16 import dol.datamodel.pn.Resource;
17 import dol.datamodel.pn.ProcessNetwork;
18 import dol.datamodel.pn.Process;
19 import dol.datamodel.pn.Channel;
20 import dol.datamodel.pn.Connection;
21 import dol.datamodel.pn.Port;
23 public class EpiphanyVisitor extends PNVisitor {
24 private String _epiphanyHostName = "EpiphanyHost";
25 private String _packageName;
26 private Vector<EpiphanyBuffer> _buffers = new Vector<EpiphanyBuffer>();
27 private Integer _numShmBufs = 0;
30 public EpiphanyVisitor(String packageName) {
31 _packageName = packageName + _delimiter + "src";
34 /* ProcessNetwork Visitor */
35 public void visitComponent(ProcessNetwork pn) {
37 /* create list of buffers */
40 /* copy static files */
43 /* create process wrappers
44 TODO: fix _epiphanyHostName ?? */
45 for (Process p : pn.getProcessList()) {
46 if(!_epiphanyHostName.equals(p.getBasename()))
47 createProcessWrapper(p);
50 /* create and copy shared header */
53 /* create and copy host main application */
54 createHostApplication(pn);
56 /* create and copy Makefile */
59 } catch (Exception e) {
60 System.out.println("Exception: " + e.getMessage());
65 /* Search for all structures of type
66 "Process-Port-Connection-Port-Channel-Port-Connection-Port-Process"
67 and save src/dst Process, src/dst Port and Channel */
68 private void createBuffers(ProcessNetwork pn)
70 for(Connection c1 : pn.getConnectionList()) {
71 for(Connection c2 : pn.getConnectionList()) {
72 if(c1 == c2) continue;
74 if(c1.getTarget() == c2.getOrigin() &&
75 "fifo".equals(c1.getTarget().getType()) &&
76 "fifo".equals(c2.getOrigin().getType())) {
78 Process src = (Process)c1.getOrigin();
79 Port srcP = c1.getOriginPort();
80 Process dst = (Process)c2.getTarget();
81 Port dstP = c2.getTargetPort();
82 Channel ch = (Channel)c1.getTarget();
84 createBuffer(ch, src, srcP, dst, dstP);
90 /* create a buffer assignment structure */
91 private void createBuffer(Channel ch,
92 Process src, Port srcP,
93 Process dst, Port dstP)
95 // TODO: all buffers are SHM buffers for now
96 // if(src.getBasename().equals(_epiphanyHostName) ||
97 // dst.getBasename().equals(_epiphanyHostName)) {
98 /* this is an SHM buffer */
99 System.out.println("\tBuffer: " +
100 src.getName() + ":" + srcP.getName() +
102 dst.getName() + ":" + dstP.getName() +
103 " (size " + ch.getSize() + ")" +
104 " as SHM:" + _numShmBufs);
106 EpiphanyBuffer buf = new EpiphanyBuffer();
108 buf.srcProcess = src; buf.srcPort = srcP;
109 buf.dstProcess = dst; buf.dstPort = dstP;
110 buf.shmIndex = _numShmBufs; buf.size = ch.getSize();
115 /* this is an C2C buffer */
116 // System.out.println("C2CBUF: " +
117 // src.getBasename() + ":" + srcP.getBasename() +
119 // dst.getBasename() + ":" + dstP.getBasename() +
120 // " (size " + ch.getSize() + ")");
124 /* return buffer structure for a given process:port combination */
125 private EpiphanyBuffer getBuffer(Process process, Port port)
128 for(EpiphanyBuffer buf : _buffers) {
129 if(port == buf.srcPort ||
130 port == buf.dstPort) {
135 throw new Exception("No buffer for '" + port + "' found!");
138 /* Copy a file from the JAR-file to some destination.
139 Copier() supports JAR-sources for directories only,
140 although it has a copyFile method. */
141 private void copyFile(File src, File dst) throws IOException {
142 if(src.toString().contains("dol.jar!"))
144 String jarName = src.toString();
145 jarName = jarName.substring(jarName.indexOf("file:") + 5, jarName.lastIndexOf("!"));
146 String srcName = src.toString();
147 srcName = srcName.substring(src.toString().lastIndexOf("!") + 2);
148 srcName = srcName.replaceAll("\\\\", "/");
149 JarFile jar = new JarFile(jarName);
150 JarCopier copier = new JarCopier();
151 copier.copyFile(jar, srcName, dst.toString());
153 throw new IOException("Source file must be in JAR file.");
157 /* create directories, copy processes and library */
158 private void copyStaticFiles() throws IOException,FileNotFoundException
160 System.out.print("\tcopyStaticFiles(): ");
163 System.out.print("directories, ");
165 dir = new File(_packageName);
167 dir = new File(_packageName + _delimiter + "esrc");
169 dir = new File(_packageName + _delimiter + "eobj");
171 dir = new File(_packageName + _delimiter + "hsrc");
173 dir = new File(_packageName + _delimiter + "hobj");
175 dir = new File(_packageName + _delimiter + "lib");
177 dir = new File(_packageName + _delimiter + "bin");
181 System.out.print("processes, ");
182 File src = new File("src");
183 File dest = new File(_packageName + _delimiter + "esrc");
184 new Copier().copy(src, dest);
187 System.out.print("lib, ");
188 String libPath = _ui.getMySystemCLib();
189 libPath = libPath.replaceAll("systemC", "epiphany");
190 src = new File(libPath);
191 dest = new File(_packageName + _delimiter + "lib");
192 new Copier().copy(src, dest);
194 System.out.println("finished!");
197 /* create a process_Wrapper.c file */
198 private void createProcessWrapper(Process p) throws Exception
200 System.out.println("\tcreateProcessWrapper(): " +
203 /* copy template file */
204 String filename = _packageName + _delimiter + "esrc" +
205 _delimiter + p.getName() + "_Wrapper.c";
206 String template = _ui.getMySystemCLib() + _delimiter +
208 template = template.replaceAll("systemC", "epiphany");
209 template = template.replaceAll("lib", "template");
211 File fFilename = new File(filename);
212 File fTemplate = new File(template);
213 copyFile(fTemplate, fFilename);
215 /* handle this process' iterators */
216 String instance = "";
217 Vector<Integer> indices = p.getIteratorIndices();
218 switch(indices.size()) {
222 indices.elementAt(0) +
225 indices.elementAt(0) +
226 indices.elementAt(1) +
229 indices.elementAt(0) +
230 "," + indices.elementAt(1) +
231 "," + indices.elementAt(2) +
234 indices.elementAt(0) +
235 "," + indices.elementAt(1) +
236 "," + indices.elementAt(2) +
237 "," + indices.elementAt(3); break;
239 throw new Exception("Unsupported iterator dimension");
243 /* handle this process' ports */
245 String portMapping = "";
246 String portFunctions = "";
247 for (Port port : p.getPortList()) {
248 EpiphanyBuffer buf = getBuffer(p, port);
251 portMapping += "\t{ \"" + port.getName() + "\", size_" + portNum + ", level_" + portNum + ", ";
252 portMapping += port.isInPort() ? "read_" + portNum + ", " : "NULL, ";
253 portMapping += port.isOutPort() ? "write_" + portNum + ", " : "NULL, ";
254 portMapping += "},\n";
256 /* port access functions - TODO: c2c buffers */
257 if("shm".equals(buf.type)) {
258 Integer bufNum = buf.shmIndex;
260 portFunctions += "int size_" + portNum + "(void) { return(size_shm(&shm.buf" + bufNum + ")); }\n";
261 portFunctions += "int level_" + portNum + "(void) { return(level_shm(&shm.buf" + bufNum + ")); }\n";
262 if(port.isInPort()) {
263 portFunctions += "int read_" + portNum + "(void *buf, int len) { return(read_shm(&shm.buf" + bufNum + ", buf, len)); }\n";
266 if(port.isOutPort()) {
267 portFunctions += "int write_" + portNum + "(void *buf, int len) { return(write_shm(&shm.buf" + bufNum + ", buf, len)); }\n";
270 throw new Exception("unknown port type");
276 /* replace information */
278 sed.sed(filename, "@@PROCESSNAME@@", p.getBasename());
279 sed.sed(filename, "@@INSTANCE@@", instance);
280 sed.sed(filename, "@@NUM_PORTS@@", portNum.toString());
281 sed.sed(filename, "@@PORTMAPPING@@", portMapping);
282 sed.sed(filename, "@@PORTFUNCTIONS@@", portFunctions);
285 /* create the shared.h header file */
286 private void createSharedHeader() throws Exception
288 System.out.print("\tcreateSharedHeader(): ");
290 /* copy template file */
291 String filename = _packageName + _delimiter + "shared.h";
292 String template = _ui.getMySystemCLib() + _delimiter +
294 template = template.replaceAll("systemC", "epiphany");
295 template = template.replaceAll("lib", "template");
296 File fFilename = new File(filename);
297 File fTemplate = new File(template);
298 copyFile(fTemplate, fFilename);
300 /* generate replacement strings */
301 String shmBufTypes = "";
303 for(EpiphanyBuffer buf : _buffers) {
304 if("shm".equals(buf.type)) {
306 "\ntypedef struct {\n" +
309 "\tuint32_t size;\n" +
310 "\tchar buf[" + buf.size + "];\n" +
311 "} PACKED buf" + buf.shmIndex + "_t;\n";
314 "\tbuf" + buf.shmIndex + "_t" +
315 "\tbuf" + buf.shmIndex + ";\n";
321 sed.sed(filename, "@@SHM_BUF_STRUCTS@@", shmBufTypes);
322 sed.sed(filename, "@@SHM_BUFS@@", shmBufs);
323 System.out.println("done!");
326 /* create the host application */
327 private void createHostApplication(ProcessNetwork pn) throws Exception
329 System.out.print("\tcreateHostApplication(): ");
331 /* copy template file */
332 String filename = _packageName + _delimiter + "hsrc/main.c";
333 String template = _ui.getMySystemCLib() + _delimiter +
335 template = template.replaceAll("systemC", "epiphany");
336 template = template.replaceAll("lib", "template");
337 File fFilename = new File(filename);
338 File fTemplate = new File(template);
339 copyFile(fTemplate, fFilename);
341 /* generate srec filenames */
342 String srecFiles = "";
344 for(Process p : pn.getProcessList()) {
345 /* TODO: fix _epiphanyHostName */
346 if(!_epiphanyHostName.equals(p.getBasename())) {
347 srecFiles += "\t\"bin/" + p.getBasename() + ".srec\",\n";
352 /* generate shm buffer initialization code sequence */
353 String shmBufInit = "";
354 for(EpiphanyBuffer buf : _buffers) {
355 if("shm".equals(buf.type)) {
356 String tmp = "\tshm.buf" + buf.shmIndex;
359 tmp + ".size = " + buf.size + ";\n" +
367 sed.sed(filename, "@@SREC_FILES@@", srecFiles);
368 sed.sed(filename, "@@SREC_NUM@@", srecNum.toString());
369 sed.sed(filename, "@@SHM_BUF_INIT@@", shmBufInit);
370 System.out.println("done!");
373 /* create Makefile */
374 private void createMakefile(ProcessNetwork pn) throws Exception
376 System.out.print("\tcreateMakefile(): ");
378 /* copy template file */
379 String filename = _packageName + _delimiter + "Makefile";
380 String template = _ui.getMySystemCLib() + _delimiter +
382 template = template.replaceAll("systemC", "epiphany");
383 template = template.replaceAll("lib", "template");
384 File fFilename = new File(filename);
385 File fTemplate = new File(template);
386 copyFile(fTemplate, fFilename);
388 /* generate srec filenames */
389 String srecFiles = "";
390 for(Process p : pn.getProcessList()) {
391 /* TODO: fix _epiphanyHostName */
392 if(!_epiphanyHostName.equals(p.getBasename())) {
393 srecFiles += "\\$(DEST)/" +
394 p.getName() + ".srec ";
398 /* generate .elf rules, because wrapper file names are
399 non-obvious with iterated processes... */
400 String elfRules = "";
401 for(Process p : pn.getProcessList()) {
403 "\\$(EDEST)/" + p.getName() + ".elf: " +
404 "\\$(EDEST)/" + p.getBasename() + ".o " +
405 "\\$(EDEST)/" + p.getName() + "_Wrapper.o " +
408 "\t@\\$(ECHO) \"\\\\t(EPIPHANY) LINK\\\\t\\\\t\\$@\"\n" +
409 "\t@\\$(CC) -o \\$@ \\$^ \\$(LFLAGS)\n\n";
414 sed.sed(filename, "@@SREC_FILES@@", srecFiles);
415 sed.sed(filename, "@@ELF_RULES@@", elfRules);
416 System.out.println("done!");