main: remove "pass-by-reference" editing in parse_options()
[cdsspec-compiler.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6
7 #include <threads.h>
8 #include "common.h"
9 #include "threads-model.h"
10 #include "output.h"
11
12 #include "datarace.h"
13
14 /* global "model" object */
15 #include "model.h"
16 #include "snapshot-interface.h"
17
18 static void param_defaults(struct model_params * params) {
19         params->maxreads = 0;
20         params->maxfuturedelay = 100;
21         params->fairwindow = 0;
22         params->enabledcount = 1;
23         params->bound = 0;
24         params->maxfuturevalues = 0;
25         params->expireslop = 10;
26         params->verbose = 0;
27 }
28
29 static void print_usage(struct model_params *params) {
30         /* Reset defaults before printing */
31         param_defaults(params);
32
33         model_print(
34 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
35 "\n"
36 "Options:\n"
37 "-h                    Display this help message and exit\n"
38 "-m                    Maximum times a thread can read from the same write\n"
39 "                      while other writes exist. Default: %d\n"
40 "-M                    Maximum number of future values that can be sent to\n"
41 "                      the same read. Default: %d\n"
42 "-s                    Maximum actions that the model checker will wait for\n"
43 "                      a write from the future past the expected number of\n"
44 "                      actions. Default: %d\n"
45 "-S                    Future value expiration sloppiness. Default: %u\n"
46 "-f                    Specify a fairness window in which actions that are\n"
47 "                      enabled sufficiently many times should receive\n"
48 "                      priority for execution. Default: %d\n"
49 "-e                    Enabled count. Default: %d\n"
50 "-b                    Upper length bound. Default: %d\n"
51 "-v                    Print verbose execution information.\n"
52 "--                    Program arguments follow.\n\n",
53 params->maxreads, params->maxfuturevalues, params->maxfuturedelay, params->expireslop, params->fairwindow, params->enabledcount, params->bound);
54         exit(EXIT_SUCCESS);
55 }
56
57 static void parse_options(struct model_params *params, int argc, char **argv) {
58         const char *shortopts = "hm:M:s:S:f:e:b:v";
59         int opt;
60         bool error = false;
61         while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
62                 switch (opt) {
63                 case 'h':
64                         print_usage(params);
65                         break;
66                 case 's':
67                         params->maxfuturedelay = atoi(optarg);
68                         break;
69                 case 'S':
70                         params->expireslop = atoi(optarg);
71                         break;
72                 case 'f':
73                         params->fairwindow = atoi(optarg);
74                         break;
75                 case 'e':
76                         params->enabledcount = atoi(optarg);
77                         break;
78                 case 'b':
79                         params->bound = atoi(optarg);
80                         break;
81                 case 'm':
82                         params->maxreads = atoi(optarg);
83                         break;
84                 case 'M':
85                         params->maxfuturevalues = atoi(optarg);
86                         break;
87                 case 'v':
88                         params->verbose = 1;
89                         break;
90                 default: /* '?' */
91                         error = true;
92                         break;
93                 }
94         }
95
96         /* Pass remaining arguments to user program */
97         params->argc = argc - (optind - 1);
98         params->argv = argv + (optind - 1);
99
100         /* Reset program name */
101         params->argv[0] = argv[0];
102
103         /* Reset (global) optind for potential use by user program */
104         optind = 1;
105
106         if (error)
107                 print_usage(params);
108 }
109
110 int main_argc;
111 char **main_argv;
112
113 /** The model_main function contains the main model checking loop. */
114 static void model_main() {
115         struct model_params params;
116
117         param_defaults(&params);
118
119         parse_options(&params, main_argc, main_argv);
120
121         //Initialize race detector
122         initRaceDetector();
123
124         //Create the singleton SnapshotStack object
125         snapshotObject = new SnapshotStack();
126
127         model = new ModelChecker(params);
128         snapshotObject->snapshotStep(0);
129         model->run();
130         delete model;
131
132         DEBUG("Exiting\n");
133 }
134
135 /**
136  * Main function.  Just initializes snapshotting library and the
137  * snapshotting library calls the model_main function.
138  */
139 int main(int argc, char ** argv) {
140         main_argc = argc;
141         main_argv = argv;
142
143         /* Configure output redirection for the model-checker */
144         redirect_output();
145
146         /* Let's jump in quickly and start running stuff */
147         initSnapshotLibrary(10000, 1024, 1024, 4000, &model_main);
148 }