dol: initial dol commit
[jump.git] / dol / src / dol / visitor / protothread / ProtothreadModuleVisitor.java
1 /* $Id: ProtothreadModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.protothread;
3
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.util.StringTokenizer;
7 import java.util.Vector;
8
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;
15
16 /**
17  * This class is a class for a visitor that is used to generate
18  * the main program.
19  */
20 public class ProtothreadModuleVisitor extends PNVisitor {
21
22     /**
23      * Constructor.
24      */
25     public ProtothreadModuleVisitor(String dir) {
26         _dir = dir;
27     }
28
29     public void visitComponent(ProcessNetwork x) {
30         try {
31             String filename = _dir + _delimiter + "sc_application.cpp";
32             OutputStream file = new FileOutputStream(filename);
33             _code = new CodePrintStream(file);
34
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
40                         + "Wrapper.h\"");
41             }
42             
43             _code.println();
44             _code.printPrefixln("int main(void)");
45             _code.printLeftBracket();
46
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() + ");");
55                 }
56             }
57             _code.println();
58             
59             //instantiate processes
60             for (Process p : x.getProcessList()) {
61                 _code.printPrefix("int " + p.getName()
62                         + "Indices[] = { ");
63                 Vector<Integer> iteratorIndex =
64                         p.getIteratorIndices();
65                 if (iteratorIndex.size() < 4) {
66                     while (iteratorIndex.size() < 4) {
67                         iteratorIndex.add(-1);
68                     }
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()
73                             + ".");
74                 }
75                 for (int i = 0; i < 4; i++) {
76                     if (i < 3) {
77                         _code.print(iteratorIndex.elementAt(i) + ", ");
78                     } else {
79                         _code.println(iteratorIndex.elementAt(i) + " };");
80                     }
81                 }
82                 _code.printPrefixln(p.getBasename() + "Wrapper *"
83                         + p.getName() + " = new "
84                         + p.getBasename() + "Wrapper(\""
85                         + p.getName() + "\", "
86                         + p.getName() + "Indices);");
87             }
88             _code.println();
89
90             //connect the network
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() + ";");
97                     } else {
98                         _code.printPrefix(p.getName()
99                                 + "->_port" + port.getBasename()
100                                 + "Fifo");
101                         StringTokenizer tokenizer =
102                                 new StringTokenizer(port.getName().
103                                 replaceFirst(port.getBasename(), ""), "_");
104                         while (tokenizer.hasMoreTokens()) {
105                             _code.print("[" + tokenizer.nextToken()
106                                     + "]");
107                         }
108                         _code.println(" = &"
109                                 + port.getPeerResource().getName() + ";");
110                     }
111                 }
112             }
113             _code.println();
114             
115             //initialize processes
116             for (Process p : x.getProcessList()) {
117                 _code.printPrefixln(p.getName() + "->init();");
118             }
119             _code.println();
120             
121             /*
122             _code.printPrefix("while(");
123             int counter = 0;
124             for (Process p : x.getProcessList()) {
125                 if (counter > 0) {
126                     _code.printPrefix("      ");
127                 }
128                 _code.print("!" + p.getName() + "->isDetached()");
129                 if (counter++ < x.getProcessList().size() - 1) {
130                     _code.println(" ||");
131                 } else {
132                     _code.println(")");
133                 }
134             }
135             */
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("}");
149             }
150             _code.printRightBracket();
151             _code.println();
152             
153             for (Process p : x.getProcessList()) {
154                 _code.printPrefixln("delete " + p.getName() + ";");
155             }
156             _code.println();
157             _code.printPrefixln("return 0;");
158             _code.printRightBracket();
159         }
160         catch (Exception e) {
161             System.out.println("ProtothreadModuleVisitor: "
162                     + "exception occured: " + e.getMessage());
163             e.printStackTrace();
164         }
165     }
166
167     protected CodePrintStream _code = null;
168     protected String _dir = null;
169 }
170