--- /dev/null
+/* $Id: CbeProcessVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.cbe;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+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.datamodel.pn.SourceCode;
+import dol.util.CodePrintStream;
+import dol.util.Copier;
+import dol.util.Sed;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate the main
+ * makefile for the application.
+ *
+ * @author lschor, 2008-10-30
+ *
+ * Revision:
+ * 2008-10-30: Updated the file for the CBE
+ * 2008-11-08: Add double buffering
+ */
+public class CbeProcessVisitor extends PNVisitor {
+
+ /**
+ * Constructor.
+ *
+ * @param dir target directory
+ */
+ public CbeProcessVisitor(String dir, HashMap<Port, Integer> portMap) {
+ _dir = dir;
+ _portMap = portMap;
+ }
+
+ /**
+ *
+ * @param x process network that needs to be processed
+ */
+ public void visitComponent(ProcessNetwork x) {
+ try {
+ Vector<String> pList = new Vector<String>();
+
+ for (Process p : x.getProcessList()) {
+ String basename = p.getBasename();
+ if (!pList.contains(basename)) {
+ pList.add(basename);
+ p.accept(this);
+ }
+ }
+ } catch (Exception e) {
+ System.out.println("CbeProcessVisitor: exception "
+ + "occured: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Visit process.
+ *
+ * @param p process that needs to be processed
+ */
+ public void visitComponent(Process p)
+ {
+ // Differ if the process is a source/sink or a normal process
+ // - Source/Sink: Mapped to the PPE
+ // - Normal process: Mapped to the SPE
+
+ if (p.getNumOfInports() > 0 && p.getNumOfOutports() > 0)
+ {
+ createSPEWrapper(p);
+ }
+ else
+ {
+ createPPEWrapper(p);
+ }
+ }
+
+ /**
+ * Creates a Wrapper for an SPE process
+ *
+ * @param p process that needs to be processed
+ */
+ protected void createSPEWrapper(Process p)
+ {
+ try {
+
+ // Create of each process an own folder
+ // add to this folder the source, the wrapper and a makefile
+ String processDir = _dir + _delimiter + "spu_"
+ + p.getBasename();
+ File dir = new File(processDir);
+ dir.mkdirs();
+
+ // Create the filename for the new wrapper
+ String filename = processDir + _delimiter + "spu_"
+ + p.getBasename() + "_wrapper.c";
+ File process_file = new File(filename);
+ File pattern_file = new File(_dir + _delimiter + _tempDirName
+ + _delimiter + "spu_process_wrapper_template.c");
+ new Copier().copyFile(pattern_file, process_file);
+
+ String includes = "";
+ for (SourceCode code : p.getSrcList()) {
+ includes += "#include \"" + code.getLocality() + "\""
+ + System.getProperty("line.separator");
+ }
+
+ Sed sed = new Sed();
+ // Replace the include of the main c-file
+ sed.sed(filename, "//#include \"@PROCESSNAME@.c\"", includes);
+ // Replace the process-name in the whole file
+ sed.sed(filename, "@PROCESSNAME@", p.getBasename());
+
+ // Go through all source files
+ for (SourceCode sourceCode : p.getSrcList()) {
+ // Copy the files to the new folder
+ String oldFilename = _dir
+ + _delimiter
+ + sourceCode.getLocality().replaceAll(
+ "(.*)\\.[cC][pP]*[pP]*", "$1\\.h");
+ filename = processDir
+ + _delimiter
+ + sourceCode.getLocality().replaceAll(
+ "(.*)\\.[cC][pP]*[pP]*", "$1\\.h");
+
+ if (new File(filename).exists()) {
+ new File(filename).delete();
+ }
+
+ new File(oldFilename).renameTo(new File(filename));
+
+ // Copy also the c-files
+ if ((oldFilename.substring(oldFilename.length() - 2))
+ .equals(".h")) {
+ String newFileNameC = filename.substring(0, filename
+ .length() - 2) + ".c";
+ if (new File(newFileNameC).exists()) {
+ new File(newFileNameC).delete();
+ }
+ new File(oldFilename.substring(
+ 0, oldFilename.length() - 2)
+ + ".c").renameTo(new File(newFileNameC));
+ }
+
+ // Update the files for the DOL project
+ sed.sed(filename, "<dol.h>", "\"dol.h\"");
+
+ // Create the port list
+ for (Port port : p.getPortList()) {
+ Integer portId = _portMap.get(port);
+ if (!port.getBasename().equals(port.getName())) {
+ for (Port port2 : p.getPortList()) {
+ if (port2.getBasename().equals(
+ port.getBasename())) {
+ if (_portMap.get(port2) <
+ _portMap.get(port)) {
+ portId = _portMap.get(port2);
+ }
+ }
+ }
+ }
+
+ // Update the port list with the base names
+ sed.sed(filename, "(#define[ ]+PORT_\\w*[ ]+)\"?"
+ + port.getBasename() + "\"?", "$1 " + portId);
+ }
+
+ // Create the makefile for the process
+ createMakefile(p);
+ }
+ } catch (Exception e) {
+ System.out.println("CbeProcessVisitor: exception "
+ + "occured: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Creates a Wrapper for an SPE process
+ *
+ * @param p process that needs to be processed
+ */
+ protected void createPPEWrapper(Process p)
+ {
+ try {
+ // Create of each process an own folder
+ // add to this folder the source, the wrapper and a makefile
+ String processDir = _dir + _delimiter + "ppu_"
+ + p.getBasename();
+ File dir = new File(processDir);
+ dir.mkdirs();
+
+ // Create the filename for the new wrapper
+ String filename = processDir + _delimiter + "ppu_"
+ + p.getBasename() + "_wrapper.c";
+ File process_file = new File(filename);
+ File pattern_file = new File(_dir + _delimiter + _tempDirName
+ + _delimiter + "ppu_process_wrapper_template.c");
+ new Copier().copyFile(pattern_file, process_file);
+
+ String includes = "";
+ for (SourceCode code : p.getSrcList()) {
+ includes += "#include \"" + code.getLocality() + "\""
+ + System.getProperty("line.separator");
+ }
+
+ Sed sed = new Sed();
+ // Replace the include of the main c-file
+ sed.sed(filename, "//#include \"@PROCESSNAME@.c\"", includes);
+ // Replace the process-name in the whole file
+ sed.sed(filename, "@PROCESSNAME@", p.getBasename());
+
+ // Go through all source files
+ for (SourceCode sourceCode : p.getSrcList()) {
+ // Copy the files to the new folder
+ String oldFilename = _dir
+ + _delimiter
+ + sourceCode.getLocality().replaceAll(
+ "(.*)\\.[cC][pP]*[pP]*", "$1\\.h");
+ filename = processDir
+ + _delimiter
+ + sourceCode.getLocality().replaceAll(
+ "(.*)\\.[cC][pP]*[pP]*", "$1\\.h");
+
+ if (new File(filename).exists()) {
+ new File(filename).delete();
+ }
+
+ new File(oldFilename).renameTo(new File(filename));
+
+ // Copy also the c-files
+ if ((oldFilename.substring(oldFilename.length() - 2))
+ .equals(".h")) {
+ String newFileNameC = filename.substring(0, filename
+ .length() - 2)
+ + ".c";
+ if (new File(newFileNameC).exists()) {
+ new File(newFileNameC).delete();
+ }
+ new File(oldFilename.substring(
+ 0, oldFilename.length() - 2)
+ + ".c").renameTo(new File(newFileNameC));
+ }
+
+ // Update the files for the DOL project
+ sed.sed(filename, "<dol.h>", "\"dol.h\"");
+
+ // Create the port list
+ for (Port port : p.getPortList()) {
+ Integer portId = _portMap.get(port);
+ if (!port.getBasename().equals(port.getName())) {
+ for (Port port2 : p.getPortList()) {
+ if (port2.getBasename().equals(
+ port.getBasename())) {
+ if (_portMap.get(port2) <
+ _portMap.get(port)) {
+ portId = _portMap.get(port2);
+ }
+ }
+ }
+ }
+
+ // Update the port list with the base names
+ sed.sed(filename, "(#define[ ]+PORT_\\w*[ ]+)\"?"
+ + port.getBasename() + "\"?", "$1 " + portId);
+ }
+
+ }
+ } catch (Exception e) {
+ System.out.println("CbeProcessVisitor: exception "
+ + "occured: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Create the makefile for a special process -> Each subprocess
+ * gets its own makefile
+ *
+ * @param p process for which the makefile should be created
+ */
+ protected void createMakefile(Process p) {
+ try {
+ // Directory of the process
+ String processDir = _dir + _delimiter + "spu_"
+ + p.getBasename();
+
+ // Create the filename for the new wrapper
+ String filename = processDir + _delimiter + "Makefile";
+ // File makefile = new File(filename);
+
+ OutputStream file;
+
+ file = new FileOutputStream(filename);
+
+ PrintStream _makefilePS = new CodePrintStream(file);
+
+ _makefilePS.println("# Makefile for process " + p.getName());
+ _makefilePS.println("");
+
+ String dependency = "all: spu_" + p.getBasename()
+ + "_wrapper.c ../lib/ProcessWrapper.h "
+ + "../lib/ProcessFifo.h";
+
+ for (SourceCode code: p.getSrcList())
+ {
+ dependency += " " + code.getLocality();
+ }
+
+ _makefilePS.println(dependency);
+ _makefilePS.println("\t spu-g++ -I .. -I ../lib -g -o spu_"
+ + p.getBasename() + "_wrapper spu_" + p.getBasename()
+ + "_wrapper.c -ftree-vectorize -lm -mtune=cell -O3 "
+ + "-fmodulo-sched -funroll-loops -ffast-math");
+ _makefilePS.println("clean: ");
+ _makefilePS.println("\t rm spu_" + p.getBasename()
+ + "_wrapper");
+ _makefilePS.println();
+
+ } catch (FileNotFoundException e) {
+ System.out.println("CbeProcessVisitor - Makefile: exception "
+ + "occured: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+
+ protected String _dir = null;
+ protected HashMap<Port, Integer> _portMap;
+
+ protected static String _libDirName = "lib";
+ protected static String _tempDirName = "template";
+}