Changes to allow unique names
[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.20 2005/11/09 16:47:42 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("-aggressivesearch -- search for one repair per constraint");
108             System.out.println("-prunequantifiernodes -- prune nodes that satisfy constraint by decreasing scope");
109             System.out.println("-cplusplus -- properly set up c++ classes");
110             System.out.println("-time -- generate timing code");
111             System.out.println("-omitcomp -- omit compensation updates");
112             System.out.println("-mergenodes -- omit nodes for simpler role dependence graphs");
113             System.out.println("-debuggraph -- add edge labels and support to debug graph");
114             System.out.println("-rejectlengthchanges -- reject all updates which change the length of an array");
115             System.out.println("-printrepairs -- print log of repair actions");
116             System.out.println("-exactallocation -- application calls malloc for each struct and");
117             System.out.println("                    allocates exactly the right amount of space.");
118             System.out.println("-name -- set name");
119             System.exit(-1);
120         }
121
122         for (int i = 0; i < args.length; i++) {
123             if (args[i].equals("-debugcompiler")) {
124                 context = 0;
125                 debug = true;
126             } else if (args[i].equals("-checkonly")) {
127                 Compiler.REPAIR=false;
128             } else if (args[i].equals("-exactallocation")) {
129                 Compiler.EXACTALLOCATION=true;
130             } else if (args[i].equals("-omitcomp")) {
131                 Compiler.OMITCOMP=true;
132             } else if (args[i].equals("-debuggraph")) {
133                 Compiler.DEBUGGRAPH=true;
134             } else if (args[i].equals("-mergenodes")) {
135                 Compiler.MERGENODES=true;
136             } else if (args[i].equals("-printrepairs")) {
137                 Compiler.PRINTREPAIRS=true;
138             } else if (args[i].equals("-depth")) {
139                 Compiler.debuggraphs.add(new DebugItem(Integer.parseInt(args[i+1]),Integer.parseInt(args[i+2])));
140                 i+=2;
141             } else if (args[i].equals("-depthconj")) {
142                 Compiler.debuggraphs.add(new DebugItem(Integer.parseInt(args[i+1]),Integer.parseInt(args[i+2]),Integer.parseInt(args[i+3])));
143                 i+=3;
144             } else if (args[i].equals("-rejectlengthchanges")) {
145                 Compiler.REJECTLENGTH=true;
146             } else if (args[i].equals("-debug")) {
147                 Compiler.GENERATEDEBUGHOOKS=true;
148             } else if (args[i].equals("-time")) {
149                 Compiler.TIME=true;
150             } else if (args[i].equals("-instrument")) {
151                 Compiler.GENERATEINSTRUMENT=true;
152             } else if (args[i].equals("-aggressivesearch")) {
153                 Compiler.AGGRESSIVESEARCH=true;
154             } else if (args[i].equals("-prunequantifiernodes")) {
155                 Compiler.PRUNEQUANTIFIERS=true;
156             } else if (args[i].equals("-cplusplus")) {
157                 Compiler.ALLOCATECPLUSPLUS=true;
158             } else if (args[i].equals("-name")) {
159                 i++;
160                 RepairGenerator.name=args[i];
161                 RepairGenerator.postfix=args[i];
162             } else if (args[i].equals("-verbose") || args[i].equals("-v")) {
163                 context = 0;
164                 verbose++;
165             } else if (args[i].equals("-opt"))
166                 context = 1;
167             else if (args[i].equals("-o"))
168                 context = 2;
169             else if (context == 1) {
170                 boolean hit = false;
171                 for (int j = 0; j < optnames.length; j++) {
172                     if (args[i].equals("all") ||
173                         (args[i].equals(optnames[j]))) {
174                         hit = true;
175                         opts[j] = true;
176                     }
177                     if (args[i].equals("-" + optnames[j])) {
178                         hit = true;
179                         opts[j] = false;
180                     }
181                 }
182                 if (!hit)
183                     extraopts.addElement(args[i]);
184             } else if (context == 2) {
185                 outfile = args[i];
186                 context = 0;
187             } else {
188                 boolean hit = false;
189                 for (int j = 0; j < optnames.length; j++) {
190                     if (args[i].equals("-" + optnames[j])) {
191                         hit = true;
192                         opts[j] = true;
193                     }
194                 }
195                 if (!hit) {
196                     extras.addElement(args[i]);
197                 }
198             }
199         }
200
201         // grab infile and lose extra args
202         int i = 0;
203         while (infile == null && i < extras.size()) {
204             String fn = (String) extras.elementAt(i);
205
206             if (fn.charAt(0) != '-')
207             {
208                 infile = fn;
209                 extras.removeElementAt(i);
210             }
211             i++;
212         }
213     }
214 }