Nearly CC
An educational compiler skeleton
options.h
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef OPTIONS_H
22 #define OPTIONS_H
23 
24 #include <string>
25 #include <map>
26 
27 // Command line options handling
28 
29 // Enumeration of IR kinds
30 enum class IRKind {
31  TOKENS=0,
32  AST, // implies parsing
33  SYMBOL_TABLE, // implies semantic analysis
34  HIGHLEVEL_CODE, // implies high-level codegen
35  LOWLEVEL_CODE, // implies low-level codegen
36 };
37 
38 // Code IR output formats
39 // (these need to have higher ordinal values than IRKind members)
40 enum class CodeFormat {
41  ASSEMBLY=100, // normal assembly code output
42  CFG, // print code as function CFGs
43  DATAFLOW_CFG, // options will indicate which dataflow analysis
44 };
45 
46 
47 class Options {
48 private:
49  std::map<std::string, std::string> m_opts;
50  IRKind m_ir_kind_goal;
51  CodeFormat m_code_format_goal;
52 
53 public:
54  // names of command line options
55  static constexpr const char *PRINT_TOKENS = "-l";
56  static constexpr const char *PRINT_AST = "-p";
57  static constexpr const char *PRINT_SYMTAB = "-a";
58  static constexpr const char *OPTIMIZE = "-o";
59  static constexpr const char *PRINT_CFG = "-C";
60  static constexpr const char *HIGHLEVEL = "-h";
61  static constexpr const char *PRINT_DATAFLOW = "-D";
62 #ifdef SOLUTION
63  static constexpr const char *INCR_STRUCT_TYPE = "-i";
64  static constexpr const char *GRADUAL_FN_TYPE = "-g";
65  static constexpr const char *DEBUG_OPT = "--debug-opts";
66  static constexpr const char *ENABLE_PEEPHOLE_LL = "--enable-peephole-ll";
67 #endif
68 
69  Options();
70  ~Options();
71 
72  // Throws RuntimeError if provided command line options
73  // aren't valid. Returns the index of the first command line
74  // argument that isn't an option (which in theory should be the
75  // filename.)
76  int parse(int argc, char **argv);
77 
78  // Return true if the named option was specified
79  // on the command line, false otherwise
80  bool has_option(const std::string &opt_name) const;
81 
82  // Get the argument specified for the given option
83  // (if the option requires an argument). Should only be called
84  // if has_option indicates that the option was specified
85  // on the command line.
86  std::string get_arg(const std::string &opt_name) const;
87 
88  // Get the goal indicating what kind of intermediate representation
89  // the compiler is being asked to produce.
90  IRKind get_ir_kind_goal() const { return m_ir_kind_goal; }
91 
92  // Get the goal indicating the code format (relevant if the
93  // IR goal is high-level code or low-level code)
94  CodeFormat get_code_format_goal() const { return m_code_format_goal; }
95 
96  std::string get_usage() const;
97 };
98 
99 
100 #endif // OPTIONS_H
Definition: options.h:47