]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
the -std=qcc set of operators, still have to verify if it's the original
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24 #include "lexer.h"
25
26 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
27 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
28
29 uint32_t    opts_O        = 1;
30 const char *opts_output   = "progs.dat";
31 int         opts_standard = COMPILER_GMQCC;
32 bool        opts_debug    = false;
33 bool        opts_memchk   = false;
34 bool        opts_dump     = false;
35 bool        opts_werror   = false;
36 bool        opts_forcecrc = false;
37
38 uint16_t    opts_forced_crc;
39
40 static bool opts_output_wasset = false;
41
42 /* set by the standard */
43 const oper_info *operators      = NULL;
44 size_t           operator_count = 0;
45
46 typedef struct { char *filename; int type; } argitem;
47 VECTOR_MAKE(argitem, items);
48
49 #define TYPE_QC  0
50 #define TYPE_ASM 1
51 #define TYPE_SRC 2
52
53 static const char *app_name;
54
55 static int usage() {
56     printf("usage: %s [options] [files...]", app_name);
57     printf("options:\n"
58            "  -h, --help             show this help message\n"
59            "  -debug                 turns on compiler debug messages\n"
60            "  -memchk                turns on compiler memory leak check\n");
61     printf("  -o, --output=file      output file, defaults to progs.dat\n"
62            "  -a filename            add an asm file to be assembled\n"
63            "  -s filename            add a progs.src file to be used\n");
64     printf("  -f<flag>               enable a flag\n"
65            "  -fno-<flag>            disable a flag\n"
66            "  -std standard          select one of the following standards\n"
67            "       -std=qcc          original QuakeC\n"
68            "       -std=fteqcc       fteqcc QuakeC\n"
69            "       -std=gmqcc        this compiler (default)\n");
70     printf("  -W<warning>            enable a warning\n"
71            "  -Wno-<warning>         disable a warning\n"
72            "  -Wall                  enable all warnings\n");
73     printf("  -force-crc=num         force a specific checksum into the header\n");
74     printf("\n");
75     printf("flags:\n"
76            "  -fdarkplaces-string-table-bug\n"
77            "            patch the string table to work with some bugged darkplaces versions\n"
78            "  -fomit-nullbytes\n"
79            "            omits certain null-bytes for a smaller output - requires a patched engine\n"
80            );
81     return -1;
82 }
83
84 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
85     size_t i;
86
87     for (i = 0; i < listsize; ++i) {
88         if (!strcmp(name, list[i].name)) {
89             longbit lb = list[i].bit;
90 #if 0
91             if (on)
92                 flags[lb.idx] |= (1<<(lb.bit));
93             else
94                 flags[lb.idx] &= ~(1<<(lb.bit));
95 #else
96             if (on)
97                 flags[0] |= (1<<lb);
98             else
99                 flags[0] &= ~(1<<(lb));
100 #endif
101             return true;
102         }
103     }
104     return false;
105 }
106 static bool options_setflag(const char *name, bool on) {
107     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
108 }
109 static bool options_setwarn(const char *name, bool on) {
110     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
111 }
112
113 static bool options_witharg(int *argc_, char ***argv_, char **out) {
114     int  argc   = *argc_;
115     char **argv = *argv_;
116
117     if (argv[0][2]) {
118         *out = argv[0]+2;
119         return true;
120     }
121     /* eat up the next */
122     if (argc < 2) /* no parameter was provided */
123         return false;
124
125     *out = argv[1];
126     --*argc_;
127     ++*argv_;
128     return true;
129 }
130
131 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
132     int  argc   = *argc_;
133     char **argv = *argv_;
134
135     size_t len = strlen(optname);
136
137     if (strncmp(argv[0]+ds, optname, len))
138         return false;
139
140     /* it's --optname, check how the parameter is supplied */
141     if (argv[0][ds+len] == '=') {
142         /* using --opt=param */
143         *out = argv[0]+ds+len+1;
144         return true;
145     }
146
147     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
148         return false;
149
150     /* using --opt param */
151     *out = argv[1];
152     --*argc_;
153     ++*argv_;
154     return true;
155 }
156 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
157     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
158 }
159 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
160     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
161 }
162
163 static bool options_parse(int argc, char **argv) {
164     bool argend = false;
165     size_t itr;
166     char buffer[1024];
167     while (!argend && argc > 1) {
168         char *argarg;
169         argitem item;
170
171         ++argv;
172         --argc;
173
174         if (argv[0][0] == '-') {
175     /* All gcc-type long options */
176             if (options_long_gcc("std", &argc, &argv, &argarg)) {
177                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
178                     opts_standard = COMPILER_GMQCC;
179                 else if (!strcmp(argarg, "qcc"))
180                     opts_standard = COMPILER_QCC;
181                 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
182                     opts_standard = COMPILER_FTEQCC;
183                 else if (!strcmp(argarg, "qccx"))
184                     opts_standard = COMPILER_QCCX;
185                 else {
186                     printf("Unknown standard: %s\n", argarg);
187                     return false;
188                 }
189                 continue;
190             }
191             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
192                 opts_forcecrc = true;
193                 opts_forced_crc = strtol(argarg, NULL, 0);
194                 continue;
195             }
196             if (!strcmp(argv[0]+1, "debug")) {
197                 opts_debug = true;
198                 continue;
199             }
200             if (!strcmp(argv[0]+1, "dump")) {
201                 opts_dump = true;
202                 continue;
203             }
204             if (!strcmp(argv[0]+1, "memchk")) {
205                 opts_memchk = true;
206                 continue;
207             }
208
209             switch (argv[0][1]) {
210                 /* -h, show usage but exit with 0 */
211                 case 'h':
212                     usage();
213                     exit(0);
214                     break;
215
216                 /* handle all -fflags */
217                 case 'f':
218                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
219                     if (!strcmp(argv[0]+2, "HELP")) {
220                         printf("Possible flags:\n");
221                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
222                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
223                             printf(" -f%s\n", buffer);
224                         }
225                         exit(0);
226                     }
227                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
228                         if (!options_setflag(argv[0]+5, false)) {
229                             printf("unknown flag: %s\n", argv[0]+2);
230                             return false;
231                         }
232                     }
233                     else if (!options_setflag(argv[0]+2, true)) {
234                         printf("unknown flag: %s\n", argv[0]+2);
235                         return false;
236                     }
237                     break;
238                 case 'W':
239                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
240                     if (!strcmp(argv[0]+2, "HELP")) {
241                         printf("Possible warnings:\n");
242                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
243                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
244                             printf(" -W%s\n", buffer);
245                         }
246                         exit(0);
247                     }
248                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
249                         opts_werror = false;
250                         break;
251                     }
252                     else if (!strcmp(argv[0]+2, "ERROR")) {
253                         opts_werror = true;
254                         break;
255                     }
256                     else if (!strcmp(argv[0]+2, "ALL")) {
257                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
258                             opts_warn[itr] = 0xFFFFFFFFL;
259                         break;
260                     }
261                     if (!strncmp(argv[0]+2, "NO_", 3)) {
262                         if (!options_setwarn(argv[0]+5, false)) {
263                             printf("unknown warning: %s\n", argv[0]+2);
264                             return false;
265                         }
266                     }
267                     else if (!options_setwarn(argv[0]+2, true)) {
268                         printf("unknown warning: %s\n", argv[0]+2);
269                         return false;
270                     }
271                     break;
272
273                 case 'O':
274                     if (!options_witharg(&argc, &argv, &argarg)) {
275                         printf("option -O requires a numerical argument\n");
276                         return false;
277                     }
278                     opts_O = atoi(argarg);
279                     break;
280
281                 case 'o':
282                     if (!options_witharg(&argc, &argv, &argarg)) {
283                         printf("option -o requires an argument: the output file name\n");
284                         return false;
285                     }
286                     opts_output = argarg;
287                     opts_output_wasset = true;
288                     break;
289
290                 case 'a':
291                 case 's':
292                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
293                     if (!options_witharg(&argc, &argv, &argarg)) {
294                         printf("option -a requires a filename %s\n",
295                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
296                         return false;
297                     }
298                     item.filename = argarg;
299                     items_add(item);
300                     break;
301
302                 case '-':
303                     if (!argv[0][2]) {
304                         /* anything following -- is considered a non-option argument */
305                         argend = true;
306                         break;
307                     }
308             /* All long options without arguments */
309                     else if (!strcmp(argv[0]+2, "help")) {
310                         usage();
311                         exit(0);
312                     }
313                     else {
314             /* All long options with arguments */
315                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
316                             opts_output = argarg;
317                             opts_output_wasset = true;
318                         } else {
319                             printf("Unknown parameter: %s\n", argv[0]);
320                             return false;
321                         }
322                     }
323                     break;
324
325                 default:
326                     printf("Unknown parameter: %s\n", argv[0]);
327                     return false;
328             }
329         }
330         else
331         {
332             /* it's a QC filename */
333             argitem item;
334             item.filename = argv[0];
335             item.type     = TYPE_QC;
336             items_add(item);
337         }
338     }
339     return true;
340 }
341
342 static void options_set(uint32_t *flags, size_t idx, bool on)
343 {
344     longbit lb = LONGBIT(idx);
345 #if 0
346     if (on)
347         flags[lb.idx] |= (1<<(lb.bit));
348     else
349         flags[lb.idx] &= ~(1<<(lb.bit));
350 #else
351     if (on)
352         flags[0] |= (1<<(lb));
353     else
354         flags[0] &= ~(1<<(lb));
355 #endif
356 }
357
358 /* returns the line number, or -1 on error */
359 static bool progs_nextline(char **out, size_t *alen,FILE *src)
360 {
361     int    len;
362     char  *line;
363     char  *start;
364     char  *end;
365
366     line = *out;
367     len = util_getline(&line, alen, src);
368     if (len == -1)
369         return false;
370
371     /* start at first non-blank */
372     for (start = line; isspace(*start); ++start) {}
373     /* end at the first non-blank */
374     for (end = start;  *end && !isspace(*end);  ++end)   {}
375
376     *out = line;
377     /* move the actual filename to the beginning */
378     while (start != end) {
379         *line++ = *start++;
380     }
381     *line = 0;
382     return true;
383 }
384
385 int main(int argc, char **argv) {
386     size_t itr;
387     int retval = 0;
388     bool opts_output_free = false;
389
390     app_name = argv[0];
391
392     /* default options / warn flags */
393     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
394     options_set(opts_warn, WARN_EXTENSIONS, true);
395     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
396     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
397     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
398     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
399     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
400     options_set(opts_warn, WARN_VOID_VARIABLES, true);
401     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
402     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
403
404     if (!options_parse(argc, argv)) {
405         return usage();
406     }
407
408     /* the standard decides which set of operators to use */
409     if (opts_standard == COMPILER_GMQCC) {
410         operators = c_operators;
411         operator_count = c_operator_count;
412     } else {
413         operators = qcc_operators;
414         operator_count = qcc_operator_count;
415     }
416
417     if (opts_dump) {
418         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
419             printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
420         }
421         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
422             printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
423         }
424         printf("output = %s\n", opts_output);
425         printf("optimization level = %i\n", (int)opts_O);
426         printf("standard = %i\n", opts_standard);
427     }
428
429     if (!parser_init()) {
430         printf("failed to initialize parser\n");
431         retval = 1;
432         goto cleanup;
433     }
434
435     util_debug("COM", "starting ...\n");
436
437     if (items_elements) {
438         printf("Mode: manual\n");
439         printf("There are %lu items to compile:\n", (unsigned long)items_elements);
440         for (itr = 0; itr < items_elements; ++itr) {
441             printf("  item: %s (%s)\n",
442                    items_data[itr].filename,
443                    ( (items_data[itr].type == TYPE_QC ? "qc" :
444                      (items_data[itr].type == TYPE_ASM ? "asm" :
445                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
446                      ("unknown"))))));
447
448             if (!parser_compile(items_data[itr].filename)) {
449                 retval = 1;
450                 goto cleanup;
451             }
452         }
453
454         parser_finish(opts_output);
455     } else {
456         FILE *src;
457         char *line;
458         size_t linelen = 0;
459
460         printf("Mode: progs.src\n");
461         src = util_fopen("progs.src", "rb");
462         if (!src) {
463             printf("failed to open `progs.src` for reading\n");
464             retval = 1;
465             goto cleanup;
466         }
467
468         line = NULL;
469         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
470             printf("illformatted progs.src file: expected output filename in first line\n");
471             retval = 1;
472             goto srcdone;
473         }
474
475         if (!opts_output_wasset) {
476             opts_output = util_strdup(line);
477             opts_output_free = true;
478         }
479
480         while (progs_nextline(&line, &linelen, src)) {
481             if (!line[0] || (line[0] == '/' && line[1] == '/'))
482                 continue;
483             printf("  src: %s\n", line);
484             if (!parser_compile(line)) {
485                 retval = 1;
486                 goto srcdone;
487             }
488         }
489
490         parser_finish(opts_output);
491
492 srcdone:
493         fclose(src);
494         mem_d(line);
495     }
496
497     /* stuff */
498
499 cleanup:
500     util_debug("COM", "cleaning ...\n");
501
502     mem_d(items_data);
503
504     parser_cleanup();
505     if (opts_output_free)
506         mem_d((char*)opts_output);
507
508     lex_cleanup();
509     util_meminfo();
510     return retval;
511 }