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