dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / HdsdModulePNVisitor.java
1 package dol.visitor.hdsd;
2
3 import dol.datamodel.architecture.Processor;
4 import dol.datamodel.mapping.Mapping;
5 import dol.datamodel.pn.Channel;
6 import dol.datamodel.pn.Port;
7 import dol.datamodel.pn.Process;
8 import dol.datamodel.pn.Resource;
9 import dol.util.CodePrintStream;
10 import dol.visitor.PNVisitor;
11
12 /**
13  * Visitor that generates the main program thread of a process
14  * and the process network in the constructor.
15  */
16 public class HdsdModulePNVisitor extends PNVisitor {
17
18     /**
19      * Constructor.
20      */
21     public HdsdModulePNVisitor(Processor processor, CodePrintStream stream) {
22         _processor = processor;
23         _mainPS = stream;
24     }
25
26     /**
27      * Generates the thread for this process.
28      *
29      * @param x process that needs to be rendered
30      */
31     public void visitComponent(Process x) {
32         _mainPS.printPrefixln("void thread_" + x.getName() + "()");
33         _mainPS.printLeftBracket();
34         _mainPS.printPrefixln("while (!" + x.getName()
35                 + "_ins.isDetached())");
36         _mainPS.printLeftBracket();
37
38         /* begin of profiling: start event */
39         _mainPS.printPrefixln("#ifdef INCLUDE_PROFILER");
40         _mainPS.printPrefixln("if (profiler_output_file != NULL) fprintf(profiler_output_file, \"%u "+ x.getName() + " started.\\n\", profiler_event_counter++);");
41         _mainPS.printPrefixln("#endif");
42         /* end of profiling */
43
44         _mainPS.printPrefixln(x.getName() + "_ins.fire();");
45
46         /* begin of profiling: stop event */
47         _mainPS.printPrefixln("#ifdef INCLUDE_PROFILER");
48         _mainPS.printPrefixln("if (profiler_output_file != NULL) fprintf(profiler_output_file, \"%u "+ x.getName() + " stopped.\\n\", profiler_event_counter++);");
49         _mainPS.printPrefixln("#endif");
50         /* end of profiling */
51
52         _mainPS.printPrefixln("eventList.push_back(&" + x.getName()
53                               + "_event);");
54         _mainPS.printPrefixln("sched_event.notify(SC_ZERO_TIME);");
55         _mainPS.printPrefixln("wait(" + x.getName() + "_event);");
56
57         _mainPS.printRightBracket();
58         _mainPS.printRightBracket();
59     }
60
61     /**
62      * Generates the process network. This is the channel content
63      * in the model constructor.
64      * @param x channel that needs to be rendered
65      */
66     public void visitComponent(Channel x) {
67         for (Port p : x.getPortList()) {
68             Port peerPort = (Port)(p.getPeerPort());
69             Resource peerResource = p.getPeerResource();
70
71             if (!_processor.hasProcess(peerResource.getName()))
72                 continue;
73
74             if (peerPort.getRange() != null) {
75                 if (p.isOutPort()) {
76                     _mainPS.printPrefixln(peerResource.getName()
77                             + "_ins.INPORT_"
78                             + peerPort.getBasename()
79                             + peerPort.getName().replace(
80                             peerPort.getBasename(), "").replaceAll(
81                             "_([0-9]+)", "[$1]") + "(" + x.getName()
82                             + "_ins);");
83                 }
84                 else if (p.isInPort()) {
85                     _mainPS.printPrefixln(peerResource.getName()
86                             + "_ins.OUTPORT_"
87                             + peerPort.getBasename()
88                             + peerPort.getName().replace(
89                             peerPort.getBasename(), "").replaceAll(
90                             "_([0-9]+)", "[$1]") + "(" + x.getName()
91                             + "_ins);");
92                 }
93             }
94             else {
95                 if (p.isOutPort()) {
96                     _mainPS.printPrefixln(peerResource.getName()
97                             + "_ins.INPORT_" + peerPort.getName()
98                             + "(" + x.getName() + "_ins);");
99                 }
100                 else if (p.isInPort()) {
101                     _mainPS.printPrefixln(peerResource.getName()
102                             + "_ins.OUTPORT_" + peerPort.getName()
103                             + "(" + x.getName() + "_ins);");
104                 }
105             }
106         }
107     }
108
109     protected CodePrintStream _mainPS = null;
110     protected String _dir = null;
111     protected Mapping _map = null;
112     protected Processor _processor = null;
113 }