X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=main.c;h=57e7a3065836f7d7ce59e031d4503068fc0b0f54;hb=b966cd4f4d10f1d9550401571e24a23e774ad6ce;hp=1c11b27f70cfb675568a4b0cb5040f2287baefbf;hpb=990450bfe02839007bf2364d918df5168e4b37a7;p=xonotic%2Fgmqcc.git diff --git a/main.c b/main.c index 1c11b27..57e7a30 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ /* * Copyright (C) 2012 * Dale Weiler + * Wolfgang Bumiller * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in @@ -24,18 +25,21 @@ #include "lexer.h" uint32_t opts_flags[1 + (COUNT_FLAGS / 32)]; -uint32_t opts_warn [1 + (COUNT_WARNINGS / 32)]; +uint32_t opts_optimization[1 + (COUNT_OPTIMIZATIONS / 32)]; + +/* counter increased in ir.c */ +unsigned int optimization_count[COUNT_OPTIMIZATIONS]; uint32_t opts_O = 1; const char *opts_output = "progs.dat"; int opts_standard = COMPILER_GMQCC; bool opts_debug = false; bool opts_memchk = false; +bool opts_dumpfin = false; bool opts_dump = false; -bool opts_werror = false; bool opts_forcecrc = false; bool opts_pp_only = false; -size_t opts_max_array_size = 1024; +size_t opts_max_array_size = (1024 << 3); uint16_t opts_forced_crc; @@ -111,6 +115,9 @@ static bool options_setflag(const char *name, bool on) { static bool options_setwarn(const char *name, bool on) { return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS); } +static bool options_setoptim(const char *name, bool on) { + return options_setflag_all(name, on, opts_optimization, opts_opt_list, COUNT_OPTIMIZATIONS); +} static bool options_witharg(int *argc_, char ***argv_, char **out) { int argc = *argc_; @@ -178,6 +185,13 @@ static void options_set(uint32_t *flags, size_t idx, bool on) #endif } +static void set_optimizations(unsigned int level) +{ + size_t i; + for (i = 0; i < COUNT_OPTIMIZATIONS; ++i) + options_set(opts_optimization, i, level >= opts_opt_oflag[i]); +} + static bool options_parse(int argc, char **argv) { bool argend = false; size_t itr; @@ -200,9 +214,13 @@ static bool options_parse(int argc, char **argv) { opts_standard = COMPILER_GMQCC; } else if (!strcmp(argarg, "qcc")) { options_set(opts_flags, ADJUST_VECTOR_FIELDS, false); + options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true); opts_standard = COMPILER_QCC; } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) { + options_set(opts_flags, FTEPP, true); + options_set(opts_flags, TRANSLATABLE_STRINGS, true); options_set(opts_flags, ADJUST_VECTOR_FIELDS, false); + options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true); opts_standard = COMPILER_FTEQCC; } else if (!strcmp(argarg, "qccx")) { options_set(opts_flags, ADJUST_VECTOR_FIELDS, false); @@ -219,12 +237,39 @@ static bool options_parse(int argc, char **argv) { continue; } if (options_long_gcc("redirout", &argc, &argv, &redirout)) { + con_change(redirout, redirerr); continue; } if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) { + con_change(redirout, redirerr); continue; } + /* show defaults (like pathscale) */ + if (!strcmp(argv[0]+1, "show-defaults")) { + size_t itr; + char buffer[1024]; + for (itr = 0; itr < COUNT_FLAGS; ++itr) { + if (!OPTS_FLAG(itr)) + continue; + + memset(buffer, 0, sizeof(buffer)); + util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1); + + con_out("-f%s ", buffer); + } + for (itr = 0; itr < COUNT_WARNINGS; ++itr) { + if (!OPTS_WARN(itr)) + continue; + + memset(buffer, 0, sizeof(buffer)); + util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1); + con_out("-W%s ", buffer); + } + con_out("\n"); + exit(0); + } + if (!strcmp(argv[0]+1, "debug")) { opts_debug = true; continue; @@ -233,6 +278,10 @@ static bool options_parse(int argc, char **argv) { opts_dump = true; continue; } + if (!strcmp(argv[0]+1, "dumpfin")) { + opts_dumpfin = true; + continue; + } if (!strcmp(argv[0]+1, "memchk")) { opts_memchk = true; continue; @@ -247,7 +296,7 @@ static bool options_parse(int argc, char **argv) { case 'h': usage(); exit(0); - break; + /* break; never reached because of exit(0) */ case 'E': opts_pp_only = true; @@ -256,7 +305,7 @@ static bool options_parse(int argc, char **argv) { /* handle all -fflags */ case 'f': util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1); - if (!strcmp(argv[0]+2, "HELP")) { + if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') { con_out("Possible flags:\n"); for (itr = 0; itr < COUNT_FLAGS; ++itr) { util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer)); @@ -277,7 +326,7 @@ static bool options_parse(int argc, char **argv) { break; case 'W': util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1); - if (!strcmp(argv[0]+2, "HELP")) { + if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') { con_out("Possible warnings:\n"); for (itr = 0; itr < COUNT_WARNINGS; ++itr) { util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer)); @@ -317,10 +366,37 @@ static bool options_parse(int argc, char **argv) { case 'O': if (!options_witharg(&argc, &argv, &argarg)) { - con_out("option -O requires a numerical argument\n"); + con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n"); return false; } - opts_O = atoi(argarg); + if (isdigit(argarg[0])) { + opts_O = atoi(argarg); + set_optimizations(opts_O); + } else { + util_strtocmd(argarg, argarg, strlen(argarg)+1); + if (!strcmp(argarg, "HELP")) { + con_out("Possible optimizations:\n"); + for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) { + util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer)); + con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]); + } + exit(0); + } + else if (!strcmp(argarg, "ALL")) + set_optimizations(opts_O = 9999); + else if (!strncmp(argarg, "NO_", 3)) { + if (!options_setoptim(argarg+3, false)) { + con_out("unknown optimization: %s\n", argarg+3); + return false; + } + } + else { + if (!options_setoptim(argarg, true)) { + con_out("unknown optimization: %s\n", argarg); + return false; + } + } + } break; case 'o': @@ -375,13 +451,11 @@ static bool options_parse(int argc, char **argv) { else { /* it's a QC filename */ - argitem item; item.filename = argv[0]; item.type = TYPE_QC; vec_push(items, item); } } - con_change(redirout, redirerr); return true; } @@ -415,6 +489,8 @@ int main(int argc, char **argv) { size_t itr; int retval = 0; bool opts_output_free = false; + bool progs_src = false; + FILE *outfile = NULL; app_name = argv[0]; con_init(); @@ -436,8 +512,13 @@ int main(int argc, char **argv) { options_set(opts_warn, WARN_END_SYS_FIELDS, true); options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true); options_set(opts_warn, WARN_PREPROCESSOR, true); + options_set(opts_warn, WARN_MULTIFILE_IF, true); + options_set(opts_warn, WARN_DOUBLE_DECLARATION, true); + options_set(opts_warn, WARN_CONST_VAR, true); + options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true); options_set(opts_flags, ADJUST_VECTOR_FIELDS, true); + options_set(opts_flags, FTEPP, false); if (!options_parse(argc, argv)) { return usage(); @@ -447,6 +528,9 @@ int main(int argc, char **argv) { if (opts_standard == COMPILER_GMQCC) { operators = c_operators; operator_count = c_operator_count; + } else if (opts_standard == COMPILER_FTEQCC) { + operators = fte_operators; + operator_count = fte_operator_count; } else { operators = qcc_operators; operator_count = qcc_operator_count; @@ -464,58 +548,53 @@ int main(int argc, char **argv) { con_out("standard = %i\n", opts_standard); } - if (!parser_init()) { - con_out("failed to initialize parser\n"); - retval = 1; - goto cleanup; - } - - util_debug("COM", "starting ...\n"); - - if (vec_size(items)) { - con_out("Mode: manual\n"); - con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items)); - for (itr = 0; itr < vec_size(items); ++itr) { - con_out(" item: %s (%s)\n", - items[itr].filename, - ( (items[itr].type == TYPE_QC ? "qc" : - (items[itr].type == TYPE_ASM ? "asm" : - (items[itr].type == TYPE_SRC ? "progs.src" : - ("unknown")))))); - - if (opts_pp_only) { - if (!ftepp_preprocess_file(items[itr].filename)) { - retval = 1; - goto cleanup; - } - } - else if (!parser_compile_file(items[itr].filename)) { + if (opts_pp_only) { + if (opts_output_wasset) { + outfile = util_fopen(opts_output, "wb"); + if (!outfile) { + con_err("failed to open `%s` for writing\n", opts_output); retval = 1; goto cleanup; } } + else + outfile = stdout; + } - if (!parser_finish(opts_output)) { + if (!opts_pp_only) { + if (!parser_init()) { + con_err("failed to initialize parser\n"); + retval = 1; + goto cleanup; + } + } + if (opts_pp_only || OPTS_FLAG(FTEPP)) { + if (!ftepp_init()) { + con_err("failed to initialize parser\n"); retval = 1; goto cleanup; } + } - } else { + util_debug("COM", "starting ...\n"); + + if (!vec_size(items)) { FILE *src; char *line; size_t linelen = 0; - con_out("Mode: progs.src\n"); + progs_src = true; + src = util_fopen("progs.src", "rb"); if (!src) { - con_out("failed to open `progs.src` for reading\n"); + con_err("failed to open `progs.src` for reading\n"); retval = 1; goto cleanup; } line = NULL; if (!progs_nextline(&line, &linelen, src) || !line[0]) { - con_out("illformatted progs.src file: expected output filename in first line\n"); + con_err("illformatted progs.src file: expected output filename in first line\n"); retval = 1; goto srcdone; } @@ -526,30 +605,105 @@ int main(int argc, char **argv) { } while (progs_nextline(&line, &linelen, src)) { + argitem item; if (!line[0] || (line[0] == '/' && line[1] == '/')) continue; - con_out(" src: %s\n", line); - if (!parser_compile_file(line)) { - retval = 1; - goto srcdone; - } + item.filename = util_strdup(line); + item.type = TYPE_QC; + vec_push(items, item); } - parser_finish(opts_output); - srcdone: fclose(src); mem_d(line); } + if (retval) + goto cleanup; + + if (vec_size(items)) { + if (!opts_pp_only) { + con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual")); + con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items)); + } + for (itr = 0; itr < vec_size(items); ++itr) { + if (!opts_pp_only) { + con_out(" item: %s (%s)\n", + items[itr].filename, + ( (items[itr].type == TYPE_QC ? "qc" : + (items[itr].type == TYPE_ASM ? "asm" : + (items[itr].type == TYPE_SRC ? "progs.src" : + ("unknown")))))); + } + + if (opts_pp_only) { + const char *out; + if (!ftepp_preprocess_file(items[itr].filename)) { + retval = 1; + goto cleanup; + } + out = ftepp_get(); + if (out) + fprintf(outfile, "%s", out); + ftepp_flush(); + } + else { + if (OPTS_FLAG(FTEPP)) { + const char *data; + if (!ftepp_preprocess_file(items[itr].filename)) { + retval = 1; + goto cleanup; + } + data = ftepp_get(); + if (vec_size(data)) { + if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) { + retval = 1; + goto cleanup; + } + } + ftepp_flush(); + } + else { + if (!parser_compile_file(items[itr].filename)) { + retval = 1; + goto cleanup; + } + } + } + + if (progs_src) { + mem_d(items[itr].filename); + items[itr].filename = NULL; + } + } + + ftepp_finish(); + if (!opts_pp_only) { + if (!parser_finish(opts_output)) { + retval = 1; + goto cleanup; + } + } + } + /* stuff */ + if (!opts_pp_only) { + for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) { + if (optimization_count[itr]) { + con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]); + } + } + } + cleanup: util_debug("COM", "cleaning ...\n"); + ftepp_finish(); con_close(); vec_free(items); - parser_cleanup(); + if (!opts_pp_only) + parser_cleanup(); if (opts_output_free) mem_d((char*)opts_output);