correct
[repair.git] / Repair / RepairCompiler / MCC / CLI.java
1 package MCC;
2
3 import java.util.*;
4 import MCC.IR.DebugItem;
5 import MCC.IR.RepairGenerator;
6
7 /**
8  * A generic command-line interface for 6.035 compilers.  This class
9  * provides command-line parsing for student projects.  It recognizes
10  * the required <tt>-target</tt>, <tt>-debug</tt>, <tt>-opt</tt>, and
11  * <tt>-o</tt> switches, and generates a name for input and output
12  * files.
13  *
14  * @author  le01, 6.035 Staff (<tt>6.035-staff@mit.edu</tt>)
15  * @version <tt>$Id: CLI.java,v 1.21 2005/11/09 21:06:29 bdemsky Exp $</tt>
16  */
17 public class CLI {
18     /**
19      * Array indicating which optimizations should be performed.  If
20      * a particular element is true, it indicates that the optimization
21      * named in the optnames[] parameter to parse with the same index
22      * should be performed.
23      */
24     public boolean opts[];
25
26     /**
27      * Vector of String containing the command-line arguments which could
28      * not otherwise be parsed.
29      */
30     public Vector extras;
31
32     /**
33      * Vector of String containing the optimizations which could not be
34      * parsed.  It is okay to complain about anything in this list, even
35      * without the <tt>-debug</tt> flag.
36      */
37     public Vector extraopts;
38
39     /**
40      * Name of the file to put the output in.
41      */
42     public String outfile;
43
44     /**
45      * Name of the file to get input from.  This is null if the user didn't
46      * provide a file name.
47      */
48     public String infile;
49
50     /**
51      * The debug flag.  This is true if <tt>-debug</tt> was passed on
52      * the command line, requesting debugging output.
53      */
54     public boolean debug;
55
56     /**
57      * Verbose output
58      */
59     public int verbose;
60
61     /**
62      * Public constructor.  Sets up default values for all of the
63      * result fields.  Specifically, sets the input and output files
64      * to null, the target to DEFAULT, and the extras and extraopts
65      * arrays to new empty Vectors.
66      */
67     public CLI() {
68         outfile = null;
69         infile = null;
70         extras = new Vector();
71         extraopts = new Vector();
72         verbose = 0;
73     }
74
75     /**
76      * Parse the command-line arguments.  Sets all of the result fields
77      * accordingly. <BR>
78      * <ul>
79      * <li><TT>-target <I>target</I></TT> sets the CLI.target field based
80      * on the <I>target</I> specified. </li>
81      * <li><TT>scan</TT> or <TT>scanner</TT> specifies CLI.SCAN</li>
82      * <li><TT>parse</TT> specifies CLI.PARSE</li>
83      * <li><TT>inter</TT> specifies CLI.INTER</li>
84      * <li><TT>lowir</TT> specifies CLI.LOWIR</li>
85      * <TT>assembly</TT> or <TT>codegen</TT> specifies CLI.ASSEMBLY</li>
86      * </ul>
87      * The boolean array opts[] indicates which, if any, of the
88      * optimizations in optnames[] should be performed; these arrays
89      * are in the same order.
90      *
91      * @param args Array of arguments passed in to the program's Main
92      *   function.
93      * @param optnames Ordered array of recognized optimization names.  */
94     public void parse(String args[]) {
95
96         String optnames[] = {};
97         int context = 0;
98         String ext = ".out";
99
100         opts = new boolean[optnames.length];
101
102         if (args.length==0) {
103             System.out.println("-debugcompiler -- print out debug messages");
104             System.out.println("-depth depthnum constraintnum -- generate dependency graph from constraintnum with depth of depthnum");
105             System.out.println("-depthconj depthnum constraintnum conjunctionnum -- generate dependency graph from constraintnum with depth of depthnum");
106             System.out.println("-instrument -- generate instrumentation code");
107             System.out.println("-removeconj constr,conj");
108             System.out.println("-aggressivesearch -- search for one repair per constraint");
109             System.out.println("-prunequantifiernodes -- prune nodes that satisfy constraint by decreasing scope");
110             System.out.println("-cplusplus -- properly set up c++ classes");
111             System.out.println("-time -- generate timing code");
112             System.out.println("-omitcomp -- omit compensation updates");
113             System.out.println("-mergenodes -- omit nodes for simpler role dependence graphs");
114             System.out.println("-debuggraph -- add edge labels and support to debug graph");
115             System.out.println("-rejectlengthchanges -- reject all updates which change the length of an array");
116             System.out.println("-printrepairs -- print log of repair actions");
117             System.out.println("-exactallocation -- application calls malloc for each struct and");
118             System.out.println("                    allocates exactly the right amount of space.");
119             System.out.println("-name -- set name");
120             System.exit(-1);
121         }
122
123         for (int i = 0; i < args.length; i++) {
124             if (args[i].equals("-debugcompiler")) {
125                 context = 0;
126                 debug = true;
127             } else if (args[i].equals("-checkonly")) {
128                 Compiler.REPAIR=false;
129             } else if (args[i].equals("-removeconj")) {
130                 if ((i+1)==args.length) {
131                     throw new Error("No fieldname");
132                 }
133                 Compiler.removeconj.add(args[i+1]);
134                 i++;
135             } else if (args[i].equals("-exactallocation")) {
136                 Compiler.EXACTALLOCATION=true;
137             } else if (args[i].equals("-omitcomp")) {
138                 Compiler.OMITCOMP=true;
139             } else if (args[i].equals("-debuggraph")) {
140                 Compiler.DEBUGGRAPH=true;
141             } else if (args[i].equals("-mergenodes")) {
142                 Compiler.MERGENODES=true;
143             } else if (args[i].equals("-printrepairs")) {
144                 Compiler.PRINTREPAIRS=true;
145             } else if (args[i].equals("-depth")) {
146                 Compiler.debuggraphs.add(new DebugItem(Integer.parseInt(args[i+1]),Integer.parseInt(args[i+2])));
147                 i+=2;
148             } else if (args[i].equals("-depthconj")) {
149                 Compiler.debuggraphs.add(new DebugItem(Integer.parseInt(args[i+1]),Integer.parseInt(args[i+2]),Integer.parseInt(args[i+3])));
150                 i+=3;
151             } else if (args[i].equals("-rejectlengthchanges")) {
152                 Compiler.REJECTLENGTH=true;
153             } else if (args[i].equals("-debug")) {
154                 Compiler.GENERATEDEBUGHOOKS=true;
155             } else if (args[i].equals("-time")) {
156                 Compiler.TIME=true;
157             } else if (args[i].equals("-instrument")) {
158                 Compiler.GENERATEINSTRUMENT=true;
159             } else if (args[i].equals("-aggressivesearch")) {
160                 Compiler.AGGRESSIVESEARCH=true;
161             } else if (args[i].equals("-prunequantifiernodes")) {
162                 Compiler.PRUNEQUANTIFIERS=true;
163             } else if (args[i].equals("-cplusplus")) {
164                 Compiler.ALLOCATECPLUSPLUS=true;
165             } else if (args[i].equals("-name")) {
166                 i++;
167                 RepairGenerator.name=args[i];
168                 RepairGenerator.postfix=args[i];
169             } else if (args[i].equals("-verbose") || args[i].equals("-v")) {
170                 context = 0;
171                 verbose++;
172             } else if (args[i].equals("-opt"))
173                 context = 1;
174             else if (args[i].equals("-o"))
175                 context = 2;
176             else if (context == 1) {
177                 boolean hit = false;
178                 for (int j = 0; j < optnames.length; j++) {
179                     if (args[i].equals("all") ||
180                         (args[i].equals(optnames[j]))) {
181                         hit = true;
182                         opts[j] = true;
183                     }
184                     if (args[i].equals("-" + optnames[j])) {
185                         hit = true;
186                         opts[j] = false;
187                     }
188                 }
189                 if (!hit)
190                     extraopts.addElement(args[i]);
191             } else if (context == 2) {
192                 outfile = args[i];
193                 context = 0;
194             } else {
195                 boolean hit = false;
196                 for (int j = 0; j < optnames.length; j++) {
197                     if (args[i].equals("-" + optnames[j])) {
198                         hit = true;
199                         opts[j] = true;
200                     }
201                 }
202                 if (!hit) {
203                     extras.addElement(args[i]);
204                 }
205             }
206         }
207
208         // grab infile and lose extra args
209         int i = 0;
210         while (infile == null && i < extras.size()) {
211             String fn = (String) extras.elementAt(i);
212
213             if (fn.charAt(0) != '-')
214             {
215                 infile = fn;
216                 extras.removeElementAt(i);
217             }
218             i++;
219         }
220     }
221 }