dol: add command line options and empty class
[jump.git] / dol / src / dol / main / Main.java
1 /* $Id: Main.java 203 2010-10-11 08:59:47Z dchokshi $ */
2 package dol.main;
3
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6
7 import dol.check.SanityCheck;
8 import dol.datamodel.architecture.Architecture;
9 import dol.datamodel.mapping.Mapping;
10 import dol.datamodel.pn.ProcessNetwork;
11 import dol.helper.profiler.Profiler;
12 import dol.helper.profiler.VSPLogFileProfiler;
13 import dol.helper.profiler.WorkloadAnnotator;
14 import dol.parser.xml.archischema.ArchiXmlParser;
15 import dol.parser.xml.mapschema.MapXmlParser;
16 import dol.parser.xml.pnschema.PNXmlParser;
17 import dol.util.CodePrintStream;
18 import dol.visitor.PipeAndFilter.PipeAndFilterVisitor;
19 import dol.visitor.cell.CellVisitor;
20 import dol.visitor.dot.ArchDotVisitor;
21 import dol.visitor.dot.MapDotVisitor;
22 import dol.visitor.dot.PNDotVisitor;
23 import dol.visitor.hds.HdsVisitor;
24 import dol.visitor.hdsd.HdsdVisitor;
25 import dol.visitor.Epiphany.EpiphanyVisitor;
26 import dol.visitor.protothread.ProtothreadVisitor;
27 import dol.visitor.rtems.RtemsVisitor;
28 import dol.visitor.systemC.PNSystemCVisitor;
29 import dol.visitor.xml.PNXmlVisitor;
30 import dol.visitor.yapi.YapiVisitor;
31
32 /**
33  * Distributed Operating Layer (DOL)
34  *
35  * This class is the main controlling part of DOL. In this class, the
36  * command line options are processed, the input and output files are set,
37  * and the complete compilation cycle is done. Also, this class is the
38  * final responder to exceptions occurring within the DOL.
39  */
40 public class Main {
41
42     /**
43      * The main method of this class
44      *
45      * @param args The arguments to provide to DOL.
46      */
47     public static void main(String[] args) {
48
49         _ui = UserInterface.getInstance();
50
51         try {
52             new Options(args);
53         } catch (NumberFormatException e) {
54             System.out.println("Error in Command line options: "
55                                + " the numerial format for an argument is"
56                                + " incorrect. Message: "
57                                + e.getMessage());
58             System.exit(-1);
59         } catch (IllegalArgumentException e) {
60             System.out.println("Error in Command line option: "
61                                + e.getMessage());
62             System.exit(-1);
63         } catch (Exception e) {
64             System.out.println(e.getMessage());
65             System.exit(-1);
66         }
67
68         try {
69             //loader
70
71             //load process network specification from XML
72             if (_ui.getNetworkFileName() != null) {
73                 PNXmlParser parserPN = new PNXmlParser();
74                 _pn = parserPN.doParse(_ui.getNetworkFileName());
75             }
76
77             //load architecture specification from XML
78             if (_ui.getPlatformFileName() != null) {
79                 ArchiXmlParser parserArch = new ArchiXmlParser();
80                 _architecture = parserArch.doParse(
81                         _ui.getPlatformFileName());
82
83                 //not useful (archiPathFinderVisitor not functional)
84                 //_architecture.setArchiConnections();
85                 //_architecture.accept(new dol.visitor.pathFinder.
86                 //        archiPathFinderVisitor());
87                 //_architecture.accept(new dol.visitor.pathFinder.
88                 //        archiPathPrinterVisitor());
89             }
90
91             //load mapping specification from XML
92             if ((_pn != null) && (_architecture != null)
93                     && _ui.getMappingFileName() != null) {
94                 MapXmlParser parserMap = new MapXmlParser(_pn, _architecture);
95                 _mapping = parserMap.doParse(_ui.getMappingFileName());
96             }
97
98             //sanity check
99             if (_ui.getCheckFlag()){
100                 System.out.println("Consistency check:");
101                 //check process network
102                 if (_pn != null)
103                     SanityCheck.getInstance().checkPN(_pn);
104
105                 //check architecture
106                 if (_architecture != null)
107                     SanityCheck.getInstance().checkArch(_architecture);
108
109                 //check mapping
110                 if (_mapping != null)
111                     SanityCheck.getInstance().checkMap(_mapping);
112
113                 System.out.println(" -- Consistency check [Finished]");
114                 System.out.println();
115             }
116
117             // analyze profiling data and fill into pn
118             if (_ui.getProfilingFlag() && (_pn != null)) {
119                 System.out.println(" -- ProcessNetwork profiling");
120                 Profiler p = new Profiler();
121                 p.profilePN(_ui.getTraceName(), _pn);
122                 System.out.println(" -- Profiling [Finished]");
123                 System.out.println();
124             }
125
126             if (_ui.getVspLogFlag() && (_pn != null)) {
127                 System.out.println("VSP log file back-annotation:");
128                 VSPLogFileProfiler.annotateProcessNetwork(
129                         _ui.getVspLogFileName(), _pn);
130                 System.out.println(" -- Back-annotation [Finished]");
131                 System.out.println();
132             }
133
134             if (_ui.getWorkloadFlag() && (_pn != null)) {
135                 System.out.println("Workload file back-annotation:");
136                 WorkloadAnnotator.annotateProcessNetwork(
137                         _ui.getWorkloadFileName(), _pn);
138                 System.out.println(" -- Back-annotation [Finished]");
139                 System.out.println();
140             }
141
142             //Generator
143             CodePrintStream printStream;
144
145             if (_ui.getDottyFlag()) {
146                 OutputStream file = new FileOutputStream(_ui.getDottyFileName());
147                 printStream = new CodePrintStream(file);
148                 if (_mapping != null)
149                 {
150                     System.out.println("Generating Mapping in Dotty format:");
151                     _mapping.accept(new MapDotVisitor(printStream));
152                 }
153                 else if (_pn != null)
154                 {
155                     System.out.println("Generating ProcessNetwork in Dotty format:");
156                     _pn.accept(new PNDotVisitor(printStream));
157                 }
158                 else if (_architecture!=null) {
159                     System.out.println("Generating Architecture in Dotty format:");
160                     _architecture.registerRWPath2Resource();
161                     _architecture.accept(new ArchDotVisitor(printStream));
162                 }
163                 System.out.println(" -- Generation [Finished]");
164                 System.out.println();
165             }
166
167             if (_ui.getXmlGenFlag() && (_pn != null)) {
168                 System.out.println("Generating ProcessNetwork in XML format:");
169                 StringBuffer buffer = new StringBuffer();
170                 _pn.accept(new PNXmlVisitor(buffer));
171                 try {
172                     java.io.BufferedWriter writer =
173                             new java.io.BufferedWriter(
174                             new java.io.FileWriter(_ui.getOutputFileName()));
175                     writer.write(buffer.toString());
176                     writer.close();
177                 } catch (java.io.IOException e) {
178                     System.out.println(" -- DOL caught an exception while "
179                             + "creating XML file: " + e.getMessage());
180                     e.printStackTrace(System.out);
181                 }
182                 System.out.println(" -- Generation [Finished]");
183                 System.out.println();
184             }
185
186             if (_ui.getSystemCFlag() && (_pn != null)) {
187                 System.out.println("Generating SystemC package:");
188                 _pn.accept(new PNSystemCVisitor(_ui.getCodeDirectoryName()));
189                 System.out.println(" -- Generation [Finished]");
190                 System.out.println();
191             }
192
193             if (_ui.getPipeAndFilterFlag() && (_pn != null)) {
194                 System.out.println("Generating PipeAndFilter package:");
195                 _pn.accept(new PipeAndFilterVisitor(
196                         _ui.getPipeAndFilterCodeDirectoryName()));
197                 System.out.println(" -- Generation [Finished]");
198                 System.out.println();
199             }
200
201             if (_ui.getEpiphanyFlag() && (_pn != null)) {
202                 System.out.println("Generating Epiphany package:");
203                 _pn.accept(new EpiphanyVisitor(
204                         _ui.getEpiphanyCodeDirectoryName()));
205                 System.out.println(" -- Generation [Finished]");
206                 System.out.println();
207             }
208
209             if (_ui.getProtothreadFlag() && (_pn != null)) {
210                 System.out.println("Generating protothread package:");
211                 _pn.accept(new ProtothreadVisitor(
212                         _ui.getProtothreadCodeDirectoryName()));
213                 System.out.println(" -- Generation [Finished]");
214                 System.out.println();
215             }
216
217             if (_ui.getRtemsFlag() && (_pn != null)) {
218                 String bsp = _ui.getRtemsBSP();
219                 System.out.println("Generating RTEMS-" + bsp + " package:");
220                 _pn.accept(new RtemsVisitor(
221                     _ui.getRtemsCodeDirectoryName()));
222                 System.out.println(" -- Generation [Finished]");
223                 System.out.println();
224             }
225
226             if (_ui.getCbeFlag() && (_pn != null)) {
227                 System.out.println("Generating Cell-package:");
228                 _pn.accept(new CellVisitor(
229                     _ui.getCbeCodeDirectoryName()));
230                 System.out.println(" -- Generation [Finished]");
231                 System.out.println();
232             }
233
234             if (_ui.getYapiFlag() && (_pn != null)) {
235                 System.out.println("Generating YAPI-package:");
236                 _pn.accept(new YapiVisitor(
237                     _ui.getYapiCodeDirectoryName()));
238                 System.out.println(" -- Generation [Finished]");
239                 System.out.println();
240             }
241             
242             if (_ui.getHdsFlag() && (_pn != null)) {
243                 System.out.println("Generating HdS package:");
244                 if ((_pn!=null) && (_architecture!=null) && (_mapping!=null)) {
245                     System.out.println("Generating distributed HdS package:");
246                     // Hds with networking supportSAXException
247                     _mapping.accept(new HdsdVisitor(_ui.getHdsCodeDirectoryName()));
248
249                 } else {
250                     // Hds without networking support
251                     _pn.accept(new HdsVisitor(_ui.getHdsCodeDirectoryName()));
252                 }
253                 System.out.println(" -- Generation [Finished]");
254                 System.out.println();
255             }
256
257         }
258         catch (NumberFormatException e) {
259             System.out.println(" ERROR Occured in DOL: " + e.getMessage());
260             e.printStackTrace(System.out);
261         }
262         catch (Exception e) {
263             System.out.println(" DOL caught an exception: " + e.getMessage());
264             e.printStackTrace(System.out);
265         }
266     }
267
268     protected static ProcessNetwork _pn = null;
269     protected static Architecture _architecture = null;
270     protected static Mapping _mapping = null;
271     protected static UserInterface _ui = null;
272 }