fix int->bool conversion
[cparser] / main.c
diff --git a/main.c b/main.c
index d6cba01..84ff443 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,6 +1,6 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
+ * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -141,6 +141,7 @@ typedef enum filetype_t {
        FILETYPE_ASSEMBLER,
        FILETYPE_PREPROCESSED_ASSEMBLER,
        FILETYPE_OBJECT,
+       FILETYPE_IR,
        FILETYPE_UNKNOWN
 } filetype_t;
 
@@ -232,7 +233,7 @@ static void lextest(FILE *in, const char *fname)
 
 static void add_flag(struct obstack *obst, const char *format, ...)
 {
-       char buf[4096];
+       char buf[65536];
        va_list ap;
 
        va_start(ap, format);
@@ -318,7 +319,9 @@ static FILE *preprocess(const char *fname)
                }
        }
        if (flags[0] != '\0') {
-               obstack_printf(&cppflags_obst, " %s", flags);
+               size_t len = strlen(flags);
+               obstack_1grow(&cppflags_obst, ' ');
+               obstack_grow(&cppflags_obst, flags, len);
        }
        add_flag(&cppflags_obst, fname);
 
@@ -339,7 +342,7 @@ static FILE *preprocess(const char *fname)
 
 static void assemble(const char *out, const char *in)
 {
-       char buf[4096];
+       char buf[65536];
 
        snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
        if (verbose) {
@@ -498,6 +501,7 @@ typedef enum compile_mode_t {
        ParseOnly,
        Compile,
        CompileDump,
+       CompileExportIR,
        CompileAssemble,
        CompileAssembleLink,
        LexTest,
@@ -512,7 +516,8 @@ static void usage(const char *argv0)
        fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
 }
 
-static void print_cparser_version(void) {
+static void print_cparser_version(void)
+{
        printf("cparser (%s) using libFirm (%u.%u",
               cparser_REVISION, ir_get_version_major(),
               ir_get_version_minor());
@@ -528,7 +533,9 @@ static void print_cparser_version(void) {
                putchar(' ');
                fputs(build, stdout);
        }
-       puts(")\n");
+       puts(")");
+       puts("This is free software; see the source for copying conditions.  There is NO\n"
+            "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
 }
 
 static void set_be_option(const char *arg)
@@ -838,19 +845,23 @@ int main(int argc, char **argv)
                                                opt += 3;
                                        }
 
-                                       if (streq(opt, "dollars-in-identifiers")) {
-                                               allow_dollar_in_symbol = truth_value;
-                                       } if (streq(opt, "builtins")) {
+                                       if (streq(opt, "builtins")) {
                                                use_builtins = truth_value;
+                                       } else if (streq(opt, "dollars-in-identifiers")) {
+                                               allow_dollar_in_symbol = truth_value;
+                                       } else if (streq(opt, "omit-frame-pointer")) {
+                                               set_be_option(truth_value ? "omitfp" : "omitfp=no");
                                        } else if (streq(opt, "short-wchar")) {
                                                wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
                                                        : ATOMIC_TYPE_INT;
-                                       } else if (streq(opt, "syntax-only")) {
-                                               mode = truth_value ? ParseOnly : CompileAssembleLink;
-                                       } else if (streq(opt, "omit-frame-pointer")) {
-                                               set_be_option(truth_value ? "omitfp" : "omitfp=no");
+                                       } else if (streq(opt, "signed-char")) {
+                                               char_is_signed = truth_value;
                                        } else if (streq(opt, "strength-reduce")) {
                                                firm_option(truth_value ? "strength-red" : "no-strength-red");
+                                       } else if (streq(opt, "syntax-only")) {
+                                               mode = truth_value ? ParseOnly : CompileAssembleLink;
+                                       } else if (streq(opt, "unsigned-char")) {
+                                               char_is_signed = !truth_value;
                                        } else if (streq(opt, "fast-math")               ||
                                                   streq(opt, "jump-tables")             ||
                                                   streq(opt, "unroll-loops")            ||
@@ -959,6 +970,8 @@ int main(int argc, char **argv)
                                        set_be_option("omitleaffp=1");
                                } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
                                        set_be_option("omitleaffp=0");
+                               } else if (streq(opt, "rtd")) {
+                                       default_calling_convention = CC_STDCALL;
                                } else {
                                        char *endptr;
                                        long int value = strtol(opt, &endptr, 10);
@@ -1017,10 +1030,6 @@ int main(int argc, char **argv)
                                } else if (streq(option, "no-ms")) {
                                        features_on  &= ~_MS;
                                        features_off |=  _MS;
-                               } else if (streq(option, "signed-chars")) {
-                                       char_is_signed = true;
-                               } else if (streq(option, "unsigned-chars")) {
-                                       char_is_signed = false;
                                } else if (streq(option, "strict")) {
                                        strict_mode = true;
                                } else if (streq(option, "lextest")) {
@@ -1052,6 +1061,8 @@ int main(int argc, char **argv)
                                        }
                                        dumpfunction = argv[i];
                                        mode         = CompileDump;
+                               } else if (streq(option, "export-ir")) {
+                                       mode = CompileExportIR;
                                } else {
                                        fprintf(stderr, "error: unknown argument '%s'\n", arg);
                                        argument_errors = true;
@@ -1079,6 +1090,7 @@ int main(int argc, char **argv)
                                                        streq(suffix, "cpp") ? FILETYPE_CXX                    :
                                                        streq(suffix, "cxx") ? FILETYPE_CXX                    :
                                                        streq(suffix, "h")   ? FILETYPE_C                      :
+                                                       streq(suffix, "ir")  ? FILETYPE_IR                     :
                                                        streq(suffix, "o")   ? FILETYPE_OBJECT                 :
                                                        streq(suffix, "s")   ? FILETYPE_PREPROCESSED_ASSEMBLER :
                                                        streq(suffix, "so")  ? FILETYPE_OBJECT                 :
@@ -1128,6 +1140,10 @@ int main(int argc, char **argv)
        /* we do the lowering in ast2firm */
        firm_opt.lower_bitfields = FALSE;
 
+       /* set the c_mode here, types depends on it */
+       c_mode |= features_on;
+       c_mode &= ~features_off;
+
        gen_firm_init();
        init_symbol_table();
        init_types();
@@ -1177,6 +1193,10 @@ int main(int argc, char **argv)
                                        ".vcg");
                        outname = outnamebuf;
                        break;
+               case CompileExportIR:
+                       get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
+                       outname = outnamebuf;
+                       break;
                case CompileAssembleLink:
 #ifdef _WIN32
                        outname = "a.exe";
@@ -1272,7 +1292,7 @@ preprocess:
                        switch (standard) {
                                case STANDARD_ANSI:
                                case STANDARD_C89:   c_mode = _C89;                break;
-                               /* TODO ^v determine difference between these two */
+                               /* TODO determine difference between these two */
                                case STANDARD_C90:   c_mode = _C89;                break;
                                case STANDARD_C99:   c_mode = _C89 | _C99;         break;
                                case STANDARD_GNU89: c_mode = _C89 |        _GNUC; break;
@@ -1312,6 +1332,7 @@ default_cxx_warn:
 do_parsing:
                        c_mode |= features_on;
                        c_mode &= ~features_off;
+
                        init_tokens();
                        translation_unit_t *const unit = do_parsing(in, filename);
 
@@ -1357,6 +1378,7 @@ do_parsing:
 
                        translation_unit_to_firm(unit);
 
+graph_built:
                        if (mode == ParseOnly) {
                                continue;
                        }
@@ -1386,11 +1408,21 @@ do_parsing:
                                exit(0);
                        }
 
+                       if (mode == CompileExportIR) {
+                               fclose(out);
+                               ir_export(outname);
+                               exit(0);
+                       }
+
                        gen_firm_finish(asm_out, filename, /*c_mode=*/1,
                                        have_const_functions);
                        if (asm_out != out) {
                                fclose(asm_out);
                        }
+               } else if (filetype == FILETYPE_IR) {
+                       fclose(in);
+                       ir_import(filename);
+                       goto graph_built;
                } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
                        copy_file(asm_out, in);
                        if (in == preprocessed_in) {