1 /* $Id: ProtothreadModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.protothread;
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.util.StringTokenizer;
7 import java.util.Vector;
9 import dol.datamodel.pn.Channel;
10 import dol.datamodel.pn.Port;
11 import dol.datamodel.pn.Process;
12 import dol.datamodel.pn.ProcessNetwork;
13 import dol.util.CodePrintStream;
14 import dol.visitor.PNVisitor;
17 * This class is a class for a visitor that is used to generate
20 public class ProtothreadModuleVisitor extends PNVisitor {
25 public ProtothreadModuleVisitor(String dir) {
29 public void visitComponent(ProcessNetwork x) {
31 String filename = _dir + _delimiter + "sc_application.cpp";
32 OutputStream file = new FileOutputStream(filename);
33 _code = new CodePrintStream(file);
35 _code.printPrefixln("#include \"pt.h\"");
36 _code.printPrefixln("#include \"Fifo.h\"");
37 _code.printPrefixln("#include \"WindowedFifo.h\"");
38 for (String basename : x.getProcessBasenames()) {
39 _code.printPrefixln("#include \"" + basename
44 _code.printPrefixln("int main(void)");
45 _code.printLeftBracket();
47 //instantiate channels
48 for (Channel c : x.getChannelList()) {
49 if (c.getType().equals("fifo")) {
50 _code.printPrefixln("Fifo " + c.getName() + "("
51 + c.getSize() * c.getTokenSize() + ");");
52 } else if (c.getType().equals("wfifo")) {
53 _code.printPrefixln("WindowedFifo " + c.getName() + "("
54 + c.getSize() * c.getTokenSize() + ");");
59 //instantiate processes
60 for (Process p : x.getProcessList()) {
61 _code.printPrefix("int " + p.getName()
63 Vector<Integer> iteratorIndex =
64 p.getIteratorIndices();
65 if (iteratorIndex.size() < 4) {
66 while (iteratorIndex.size() < 4) {
67 iteratorIndex.add(-1);
69 } else if (iteratorIndex.size() > 4) {
70 new RuntimeException("Error: Currently not more than "
71 + "4 iterator dimensions are supported."
72 + "Consider revising " + p.getBasename()
75 for (int i = 0; i < 4; i++) {
77 _code.print(iteratorIndex.elementAt(i) + ", ");
79 _code.println(iteratorIndex.elementAt(i) + " };");
82 _code.printPrefixln(p.getBasename() + "Wrapper *"
83 + p.getName() + " = new "
84 + p.getBasename() + "Wrapper(\""
85 + p.getName() + "\", "
86 + p.getName() + "Indices);");
91 for (Process p : x.getProcessList()) {
92 for (Port port : p.getPortList()) {
93 if (port.getName().equals(port.getBasename())) {
94 _code.printPrefixln(p.getName()
95 + "->_port" + port.getName() + "Fifo = &"
96 + port.getPeerResource().getName() + ";");
98 _code.printPrefix(p.getName()
99 + "->_port" + port.getBasename()
101 StringTokenizer tokenizer =
102 new StringTokenizer(port.getName().
103 replaceFirst(port.getBasename(), ""), "_");
104 while (tokenizer.hasMoreTokens()) {
105 _code.print("[" + tokenizer.nextToken()
109 + port.getPeerResource().getName() + ";");
115 //initialize processes
116 for (Process p : x.getProcessList()) {
117 _code.printPrefixln(p.getName() + "->init();");
122 _code.printPrefix("while(");
124 for (Process p : x.getProcessList()) {
126 _code.printPrefix(" ");
128 _code.print("!" + p.getName() + "->isDetached()");
129 if (counter++ < x.getProcessList().size() - 1) {
130 _code.println(" ||");
136 _code.printPrefixln("bool allBlocked = false;");
137 _code.printPrefixln("while(!allBlocked)");
138 _code.printLeftBracket();
139 _code.printPrefixln("allBlocked = true;");
140 for (Process p : x.getProcessList()) {
141 _code.printPrefixln("if (!" + p.getName()
142 + "->isDetached()) {");
143 //_code.printPrefixln(" " + p.getName() + "->fire();");
144 _code.printPrefixln(" if (" + p.getName()
145 + "->fire() == PT_ENDED) {");
146 _code.printPrefixln(" allBlocked = false;");
147 _code.printPrefixln(" }");
148 _code.printPrefixln("}");
150 _code.printRightBracket();
153 for (Process p : x.getProcessList()) {
154 _code.printPrefixln("delete " + p.getName() + ";");
157 _code.printPrefixln("return 0;");
158 _code.printRightBracket();
160 catch (Exception e) {
161 System.out.println("ProtothreadModuleVisitor: "
162 + "exception occured: " + e.getMessage());
167 protected CodePrintStream _code = null;
168 protected String _dir = null;