parser: Remove the unused attribute alignment from struct declaration_specifiers_t.
[cparser] / main.c
diff --git a/main.c b/main.c
index 289d825..21d82e9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,21 +1,6 @@
 /*
  * This file is part of cparser.
- * 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
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
  */
 #include <config.h>
 
@@ -59,7 +44,9 @@
 
 #include <libfirm/firm.h>
 #include <libfirm/be.h>
+#include <libfirm/statev.h>
 
+#include "ast_t.h"
 #include "preprocessor.h"
 #include "token_t.h"
 #include "types.h"
 #include "adt/error.h"
 #include "adt/strutil.h"
 #include "adt/array.h"
+#include "symbol_table.h"
 #include "wrappergen/write_fluffy.h"
 #include "wrappergen/write_jna.h"
+#include "wrappergen/write_compoundsizes.h"
 #include "revision.h"
 #include "warning.h"
 #include "help.h"
@@ -85,7 +74,7 @@
 
 #ifndef PREPROCESSOR
 #ifndef __WIN32__
-#define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
+#define PREPROCESSOR "gcc -E -U__STRICT_ANSI__ -U__BLOCKS__"
 #else
 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
 #endif
@@ -120,10 +109,10 @@ static int               verbose;
 static struct obstack    cppflags_obst;
 static struct obstack    ldflags_obst;
 static struct obstack    asflags_obst;
-static char              dep_target[1024];
 static const char       *outname;
 static bool              define_intmax_types;
 static const char       *input_encoding;
+static bool              construct_dep_target;
 
 typedef enum lang_standard_t {
        STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
@@ -223,7 +212,7 @@ static void do_parsing(compilation_unit_t *unit)
 {
        ir_timer_t *t_parsing = ir_timer_new();
        timer_register(t_parsing, "Frontend: Parsing");
-       timer_push(t_parsing);
+       timer_start(t_parsing);
 
        start_parsing();
 
@@ -238,7 +227,10 @@ static void do_parsing(compilation_unit_t *unit)
 
        unit->type         = COMPILATION_UNIT_AST;
        unit->parse_errors = error_count > 0 || !res;
-       timer_pop(t_parsing);
+       timer_stop(t_parsing);
+       if (stat_ev_enabled) {
+               stat_ev_dbl("time_parsing", ir_timer_elapsed_sec(t_parsing));
+       }
 }
 
 static void add_flag(struct obstack *obst, const char *format, ...)
@@ -261,7 +253,7 @@ static void add_flag(struct obstack *obst, const char *format, ...)
 #else
        /* escape stuff... */
        for (char *c = buf; *c != '\0'; ++c) {
-               switch(*c) {
+               switch (*c) {
                case ' ':
                case '"':
                case '$':
@@ -314,38 +306,8 @@ static char const* str_lang_standard(lang_standard_t const standard)
 
 static bool run_external_preprocessor(compilation_unit_t *unit)
 {
-       static const char *common_flags = NULL;
-
-       if (common_flags == NULL) {
-               obstack_1grow(&cppflags_obst, '\0');
-               const char *flags = obstack_finish(&cppflags_obst);
-
-               /* setup default defines */
-               add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
-               add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
-               add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
-               add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
-
-               add_flag(&cppflags_obst, "-U__VERSION__");
-               add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
-
-               if (define_intmax_types) {
-                       add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
-                       add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
-                       add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
-                       add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
-               }
-
-               if (flags[0] != '\0') {
-                       size_t len = strlen(flags);
-                       obstack_1grow(&cppflags_obst, ' ');
-                       obstack_grow(&cppflags_obst, flags, len);
-               }
-               obstack_1grow(&cppflags_obst, '\0');
-               common_flags = obstack_finish(&cppflags_obst);
-       }
-
-       assert(obstack_object_size(&cppflags_obst) == 0);
+       obstack_1grow(&cppflags_obst, '\0');
+       const char *flags = obstack_finish(&cppflags_obst);
 
        const char *preprocessor = getenv("CPARSER_PP");
        if (preprocessor != NULL) {
@@ -366,18 +328,56 @@ static bool run_external_preprocessor(compilation_unit_t *unit)
        if (lang)
                add_flag(&cppflags_obst, "-x%s", lang);
 
-       add_flag(&cppflags_obst, "-std=%s", str_lang_standard(unit->standard));
+       if (unit->type == COMPILATION_UNIT_C
+        || unit->type == COMPILATION_UNIT_CXX) {
+               add_flag(&cppflags_obst, "-std=%s", str_lang_standard(unit->standard));
+
+               /* setup default defines */
+               add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
+               add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
+               add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
+               add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
+
+               add_flag(&cppflags_obst, "-U__VERSION__");
+               add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
 
-       obstack_printf(&cppflags_obst, "%s", common_flags);
+               if (define_intmax_types) {
+                       add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
+                       add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
+                       add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
+                       add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
+               }
+       }
+       if (flags[0] != '\0') {
+               size_t len = strlen(flags);
+               obstack_1grow(&cppflags_obst, ' ');
+               obstack_grow(&cppflags_obst, flags, len);
+       }
 
        /* handle dependency generation */
-       if (dep_target[0] != '\0') {
+       if (construct_dep_target) {
+               static char dep_target[4096];
+               if (outname != 0) {
+                       size_t len = strlen(outname);
+                       if (len > sizeof(dep_target)-4) /* leave room for .d extension */
+                               len = sizeof(dep_target)-4;
+                       memcpy(dep_target, outname, len);
+                       /* replace extension with .d if found */
+                       char *dot = &dep_target[len-1];
+                       for ( ; dot >= dep_target && *dot != '/'; --dot) {
+                               if (*dot == '.') {
+                                       dot[1] = 'd';
+                                       len = (dot-dep_target)+2;
+                                       break;
+                               }
+                       }
+                       dep_target[len] = '\0';
+               } else {
+                       get_output_name(dep_target, sizeof(dep_target), unit->name, ".d");
+               }
+
                add_flag(&cppflags_obst, "-MF");
                add_flag(&cppflags_obst, dep_target);
-               if (outname != NULL) {
-                       add_flag(&cppflags_obst, "-MQ");
-                       add_flag(&cppflags_obst, outname);
-               }
        }
        assert(unit->input == NULL);
        add_flag(&cppflags_obst, unit->name);
@@ -389,7 +389,8 @@ static bool run_external_preprocessor(compilation_unit_t *unit)
        }
        FILE *f = popen(commandline, "r");
        if (f == NULL) {
-               fprintf(stderr, "invoking preprocessor failed\n");
+               position_t const pos = { unit->name, 0, 0, 0 };
+               errorf(&pos, "invoking preprocessor failed");
                return false;
        }
        /* we do not really need that anymore */
@@ -440,7 +441,8 @@ static void assemble(const char *out, const char *in)
        }
        int err = system(commandline);
        if (err != EXIT_SUCCESS) {
-               fprintf(stderr, "assembler reported an error\n");
+               position_t const pos = { in, 0, 0, 0 };
+               errorf(&pos, "assembler reported an error");
                exit(EXIT_FAILURE);
        }
        obstack_free(&asflags_obst, commandline);
@@ -472,7 +474,8 @@ static void print_file_name(const char *file)
        }
        int err = system(commandline);
        if (err != EXIT_SUCCESS) {
-               fprintf(stderr, "linker reported an error\n");
+               position_t const pos = { file, 0, 0, 0 };
+               errorf(&pos, "linker reported an error");
                exit(EXIT_FAILURE);
        }
        obstack_free(&ldflags_obst, commandline);
@@ -542,13 +545,14 @@ static FILE *make_temp_file(const char *prefix, const char **name_result)
        char *name = obstack_finish(&file_obst);
        int fd = mkstemp(name);
        if (fd == -1) {
-               fprintf(stderr, "could not create temporary file: %s\n",
-                       strerror(errno));
+               position_t const pos = { name, 0, 0, 0 };
+               errorf(&pos, "could not create temporary file: %s", strerror(errno));
                return NULL;
        }
        FILE *out = fdopen(fd, "w");
        if (out == NULL) {
-               fprintf(stderr, "could not open temporary file as FILE*\n");
+               position_t const pos = { name, 0, 0, 0 };
+               errorf(&pos, "could not open temporary file as FILE*");
                return NULL;
        }
 
@@ -559,6 +563,9 @@ static FILE *make_temp_file(const char *prefix, const char **name_result)
 
 static void free_temp_files(void)
 {
+       if (temp_files == NULL)
+               return;
+
        size_t n_temp_files = ARR_LEN(temp_files);
        size_t i;
        for (i = 0; i < n_temp_files; ++i) {
@@ -580,7 +587,8 @@ typedef enum compile_mode_t {
        CompileAssembleLink,
        PrintAst,
        PrintFluffy,
-       PrintJna
+       PrintJna,
+       PrintCompoundSizes,
 } compile_mode_t;
 
 static void usage(const char *argv0)
@@ -654,6 +662,7 @@ static void print_help_preprocessor(void)
        put_help("-D SYMBOL[=value]",        "");
        put_help("-U SYMBOL",                "");
        put_help("-Wp,OPTION",               "Pass option directly to preprocessor");
+       put_help("-Xpreprocessor OPTION",    "Pass option directly to preprocessor");
        put_help("-M",                       "");
        put_help("-MD",                      "");
        put_help("-MMD",                     "");
@@ -766,7 +775,10 @@ static void print_help_linker(void)
        put_help("-s",                       "Do not produce symbol table and relocation information");
        put_help("-shared",                  "Produce a shared library");
        put_help("-static",                  "Produce statically linked binary");
+       put_help("-Wa,OPTION",               "Pass option directly to assembler");
+       put_help("-Xassembler OPTION",       "Pass option directly to assembler");
        put_help("-Wl,OPTION",               "Pass option directly to linker");
+       put_help("-Xlinker OPTION",          "Pass option directly to linker");
 }
 
 static void print_help_debug(void)
@@ -776,6 +788,8 @@ static void print_help_debug(void)
        put_help("--print-parenthesis",      "");
        put_help("--benchmark",              "Preprocess and parse, produces no output");
        put_help("--time",                   "Measure time of compiler passes");
+       put_help("--statev",                 "Produce statev output");
+       put_help("--filtev=filter",          "Set statev filter regex");
        put_help("--dump-function func",     "Preprocess, parse and output vcg graph of func");
        put_help("--export-ir",              "Preprocess, parse and output compiler intermediate representation");
 }
@@ -875,7 +889,7 @@ static bool parse_target_triple(const char *arg)
 {
        machine_triple_t *triple = firm_parse_machine_triple(arg);
        if (triple == NULL) {
-               fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
+               errorf(NULL, "target-triple '%s' is not in the form 'cpu_type-manufacturer-operating_system'", arg);
                return false;
        }
        target_machine = triple;
@@ -913,7 +927,7 @@ static const char *setup_isa_from_tripel(const machine_triple_t *machine)
        } else if (streq(cpu, "arm")) {
                return "arm";
        } else {
-               fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
+               errorf(NULL, "unknown cpu '%s' in target-triple", cpu);
                return NULL;
        }
 }
@@ -964,23 +978,23 @@ static void init_types_and_adjust(void)
        atomic_type_properties_t *props = atomic_type_properties;
 
        /* adjust types as requested by target architecture */
-       ir_type *type_long_double = be_params->type_long_double;
-       if (type_long_double != NULL) {
-               set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_long_double);
-               atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_long_double);
+       ir_type *const type_ld = be_params->type_long_double;
+       if (type_ld) {
+               set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_ld);
+               atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_ld);
        }
 
-       ir_type *type_long_long = be_params->type_long_long;
-       if (type_long_long != NULL)
-               set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_long_long);
+       ir_type *const type_ll = be_params->type_long_long;
+       if (type_ll)
+               set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_ll);
 
-       ir_type *type_unsigned_long_long = be_params->type_unsigned_long_long;
-       if (type_unsigned_long_long != NULL)
-               set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
+       ir_type *const type_ull = be_params->type_unsigned_long_long;
+       if (type_ull)
+               set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_ull);
 
        /* operating system ABI specifics */
        if (firm_is_darwin_os(target_machine)) {
-               if (machine_size == 32) {
+               if (is_ia32_cpu(target_machine->cpu_type)) {
                        props[ATOMIC_TYPE_LONGLONG].struct_alignment    =  4;
                        props[ATOMIC_TYPE_ULONGLONG].struct_alignment   =  4;
                        props[ATOMIC_TYPE_DOUBLE].struct_alignment      =  4;
@@ -989,7 +1003,11 @@ static void init_types_and_adjust(void)
                        props[ATOMIC_TYPE_LONG_DOUBLE].struct_alignment = 16;
                }
        } else if (firm_is_windows_os(target_machine)) {
-               if (machine_size == 64) {
+               if (is_ia32_cpu(target_machine->cpu_type)) {
+                       props[ATOMIC_TYPE_LONGLONG].struct_alignment    =  8;
+                       props[ATOMIC_TYPE_ULONGLONG].struct_alignment   =  8;
+                       props[ATOMIC_TYPE_DOUBLE].struct_alignment      =  8;
+               } else if (machine_size == 64) {
                        /* to ease porting of old c-code microsoft decided to use 32bits
                         * even for long */
                        props[ATOMIC_TYPE_LONG]  = props[ATOMIC_TYPE_INT];
@@ -1000,8 +1018,6 @@ static void init_types_and_adjust(void)
                props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
        } else if (firm_is_unixish_os(target_machine)) {
                if (is_ia32_cpu(target_machine->cpu_type)) {
-                       /* System V has a broken alignment for double so we have to add
-                        * a hack here */
                        props[ATOMIC_TYPE_DOUBLE].struct_alignment    = 4;
                        props[ATOMIC_TYPE_LONGLONG].struct_alignment  = 4;
                        props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
@@ -1019,14 +1035,14 @@ static void init_types_and_adjust(void)
        props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
 
        /* initialize defaults for unsupported types */
-       if (type_long_long == NULL) {
+       if (!type_ll) {
                copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
        }
-       if (type_unsigned_long_long == NULL) {
+       if (!type_ull) {
                copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
                               &props[ATOMIC_TYPE_ULONG]);
        }
-       if (type_long_double == NULL) {
+       if (!type_ld) {
                copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
                               &props[ATOMIC_TYPE_DOUBLE]);
        }
@@ -1182,6 +1198,20 @@ static bool output_preprocessor_tokens(compilation_unit_t *unit, FILE *out)
        return res && error_count == 0;
 }
 
+static void copy_file(FILE *dest, FILE *input)
+{
+       char buf[16384];
+
+       for (;;) {
+               size_t read = fread(buf, 1, sizeof(buf), input);
+               if (read == 0)
+                       break;
+               if (fwrite(buf, 1, read, dest) != read) {
+                       perror("could not write output");
+               }
+       }
+}
+
 static bool open_input(compilation_unit_t *unit)
 {
        /* input already available as FILE? */
@@ -1195,14 +1225,33 @@ static bool open_input(compilation_unit_t *unit)
        } else {
                unit->input = fopen(inputname, "r");
                if (unit->input == NULL) {
-                       fprintf(stderr, "Could not open '%s': %s\n", inputname,
-                                       strerror(errno));
+                       position_t const pos = { inputname, 0, 0, 0 };
+                       errorf(&pos, "could not open: %s", strerror(errno));
                        return false;
                }
        }
        return true;
 }
 
+static void node_counter(ir_node *node, void *env)
+{
+       (void)node;
+       unsigned long long *count = (unsigned long long*)env;
+       ++(*count);
+}
+
+static unsigned long long count_firm_nodes(void)
+{
+       unsigned long long count = 0;
+
+       int n_irgs = get_irp_n_irgs();
+       for (int i = 0; i < n_irgs; ++i) {
+               ir_graph *irg = get_irp_irg(i);
+               irg_walk_graph(irg, node_counter, NULL, &count);
+       }
+       return count;
+}
+
 static int compilation_loop(compile_mode_t mode, compilation_unit_t *units,
                                                        lang_standard_t standard, FILE *out)
 {
@@ -1214,33 +1263,53 @@ static int compilation_loop(compile_mode_t mode, compilation_unit_t *units,
                determine_unit_standard(unit, standard);
                setup_cmode(unit);
 
+               stat_ev_ctx_push_str("compilation_unit", inputname);
+
 again:
                switch (unit->type) {
                case COMPILATION_UNIT_IR: {
-                       bool res = open_input(unit);
-                       if (!res) {
+                       if (!open_input(unit)) {
                                result = EXIT_FAILURE;
-                               continue;
+                               break;
                        }
-                       res = !ir_import_file(unit->input, unit->name);
-                       if (!res) {
-                               fprintf(stderr, "Import of firm graph from '%s' failed\n",
-                                       inputname);
+                       if (ir_import_file(unit->input, unit->name)) {
+                               position_t const pos = { inputname, 0, 0, 0 };
+                               errorf(&pos, "import of firm graph failed");
                                result = EXIT_FAILURE;
-                               continue;
+                               break;
                        }
                        unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
                        goto again;
                }
-               case COMPILATION_UNIT_ASSEMBLER:
-                       panic("TODO: preprocess for assembler");
+               case COMPILATION_UNIT_ASSEMBLER: {
+                       if (external_preprocessor == NULL) {
+                               panic("preprocessed assembler not possible with internal preprocessor yet");
+                       }
+                       if (!run_external_preprocessor(unit)) {
+                               result = EXIT_FAILURE;
+                               break;
+                       }
+                       /* write file to output... */
+                       FILE *asm_out;
+                       if (mode == PreprocessOnly) {
+                               asm_out = out;
+                       } else {
+                               asm_out = make_temp_file("ccs", &unit->name);
+                       }
+                       assert(unit->input != NULL);
+                       assert(unit->input_is_pipe);
+                       copy_file(asm_out, unit->input);
+                       if (asm_out != out)
+                               fclose(asm_out);
+                       unit->type = COMPILATION_UNIT_PREPROCESSED_ASSEMBLER;
+                       goto again;
+               }
                case COMPILATION_UNIT_C:
                case COMPILATION_UNIT_CXX:
                        if (external_preprocessor != NULL) {
-                               bool res = run_external_preprocessor(unit);
-                               if (!res) {
+                               if (!run_external_preprocessor(unit)) {
                                        result = EXIT_FAILURE;
-                                       continue;
+                                       break;
                                }
                                goto again;
                        }
@@ -1248,20 +1317,18 @@ again:
 
                case COMPILATION_UNIT_PREPROCESSED_C:
                case COMPILATION_UNIT_PREPROCESSED_CXX: {
-                       bool res = open_input(unit);
-                       if (!res) {
+                       if (!open_input(unit)) {
                                result = EXIT_FAILURE;
-                               continue;
+                               break;
                        }
                        init_tokens();
 
                        if (mode == PreprocessOnly) {
-                               bool res = output_preprocessor_tokens(unit, out);
-                               if (!res) {
+                               if (!output_preprocessor_tokens(unit, out)) {
                                        result = EXIT_FAILURE;
-                                       continue;
+                                       break;
                                }
-                               continue;
+                               break;
                        }
 
                        /* do the actual parsing */
@@ -1287,23 +1354,33 @@ again:
                        } else if (mode == PrintJna) {
                                write_jna_decls(out, unit->ast);
                                break;
+                       } else if (mode == PrintCompoundSizes) {
+                               write_compoundsizes(out, unit->ast);
+                               break;
                        }
 
                        /* build the firm graph */
                        ir_timer_t *t_construct = ir_timer_new();
                        timer_register(t_construct, "Frontend: Graph construction");
-                       timer_push(t_construct);
+                       timer_start(t_construct);
                        if (already_constructed_firm) {
                                panic("compiling multiple files/translation units not possible");
                        }
                        init_implicit_optimizations();
                        translation_unit_to_firm(unit->ast);
                        already_constructed_firm = true;
-                       timer_pop(t_construct);
+                       timer_stop(t_construct);
+                       if (stat_ev_enabled) {
+                               stat_ev_dbl("time_graph_construction", ir_timer_elapsed_sec(t_construct));
+                               stat_ev_int("size_graph_construction", count_firm_nodes());
+                       }
                        unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
                        goto again;
 
                case COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION:
+                       if (mode == ParseOnly)
+                               break;
+
                        if (mode == CompileDump) {
                                /* find irg */
                                ident    *id     = new_id_from_str(dumpfunction);
@@ -1319,8 +1396,7 @@ again:
                                }
 
                                if (irg == NULL) {
-                                       fprintf(stderr, "No graph for function '%s' found\n",
-                                               dumpfunction);
+                                       errorf(NULL, "no graph for function '%s' found", dumpfunction);
                                        return EXIT_FAILURE;
                                }
 
@@ -1332,7 +1408,7 @@ again:
                        if (mode == CompileExportIR) {
                                ir_export_file(out);
                                if (ferror(out) != 0) {
-                                       fprintf(stderr, "Error while writing to output\n");
+                                       errorf(NULL, "writing to output failed");
                                        return EXIT_FAILURE;
                                }
                                return EXIT_SUCCESS;
@@ -1344,7 +1420,14 @@ again:
                        } else {
                                asm_out = make_temp_file("ccs", &unit->name);
                        }
+                       ir_timer_t *t_opt_codegen = ir_timer_new();
+                       timer_register(t_opt_codegen, "Optimization and Codegeneration");
+                       timer_start(t_opt_codegen);
                        generate_code(asm_out, inputname);
+                       timer_stop(t_opt_codegen);
+                       if (stat_ev_enabled) {
+                               stat_ev_dbl("time_opt_codegen", ir_timer_elapsed_sec(t_opt_codegen));
+                       }
                        if (asm_out != out) {
                                fclose(asm_out);
                        }
@@ -1374,6 +1457,8 @@ again:
                case COMPILATION_UNIT_OBJECT:
                        break;
                }
+
+               stat_ev_ctx_pop("compilation_unit");
        }
        return result;
 }
@@ -1412,7 +1497,7 @@ static int link_program(compilation_unit_t *units)
        }
        int err = system(commandline);
        if (err != EXIT_SUCCESS) {
-               fprintf(stderr, "linker reported an error\n");
+               errorf(NULL, "linker reported an error");
                return EXIT_FAILURE;
        }
        return EXIT_SUCCESS;
@@ -1426,10 +1511,12 @@ int main(int argc, char **argv)
        char                cpu_arch[16]         = "ia32";
        compilation_unit_t *units                = NULL;
        compilation_unit_t *last_unit            = NULL;
-       bool                construct_dep_target = false;
-       bool                do_timing            = false;
+       bool                produce_statev       = false;
+       const char         *filtev               = NULL;
        bool                profile_generate     = false;
        bool                profile_use          = false;
+       bool                do_timing            = false;
+       bool                print_timing         = false;
 
        /* hack for now... */
        if (strstr(argv[0], "pptest") != NULL) {
@@ -1452,13 +1539,13 @@ int main(int argc, char **argv)
        if (def[0] == '\0') {                                                    \
                ++i;                                                                 \
                if (i >= argc) {                                                     \
-                       fprintf(stderr, "error: expected argument after '" args "'\n");  \
+                       errorf(NULL, "expected argument after '" args "'"); \
                        argument_errors = true;                                          \
                        break;                                                           \
                }                                                                    \
                def = argv[i];                                                       \
                if (def[0] == '-' && def[1] != '\0') {                               \
-                       fprintf(stderr, "error: expected argument after '" args "'\n");  \
+                       errorf(NULL, "expected argument after '" args "'"); \
                        argument_errors = true;                                          \
                        continue;                                                        \
                }                                                                    \
@@ -1501,9 +1588,9 @@ int main(int argc, char **argv)
                        if (option[0] == 'o') {
                                GET_ARG_AFTER(outname, "-o");
                        } else if (option[0] == 'g') {
-                               /* TODO: parse -gX with 0<=x<=3... */
+                               /* TODO: parse -gX with 0<=X<=3... */
                                set_be_option("debug=frameinfo");
-                               set_be_option("ia32-nooptcc=yes");
+                               set_be_option("ia32-optcc=false");
                        } else if (SINGLE_OPTION('c')) {
                                mode = CompileAssemble;
                        } else if (SINGLE_OPTION('E')) {
@@ -1545,15 +1632,15 @@ int main(int argc, char **argv)
                                GET_ARG_AFTER(opt, "-x");
                                forced_unittype = get_unit_type_from_string(opt);
                                if (forced_unittype == COMPILATION_UNIT_UNKNOWN) {
-                                       fprintf(stderr, "Unknown language '%s'\n", opt);
+                                       errorf(NULL, "unknown language '%s'", opt);
                                        argument_errors = true;
                                }
-                       } else if (streq(option, "M")) {
+                       } else if (SINGLE_OPTION('M')) {
                                mode = PreprocessOnly;
                                add_flag(&cppflags_obst, "-M");
                        } else if (streq(option, "MMD") ||
                                   streq(option, "MD")) {
-                           construct_dep_target = true;
+                               construct_dep_target = true;
                                add_flag(&cppflags_obst, "-%s", option);
                        } else if (streq(option, "MM")  ||
                                   streq(option, "MP")) {
@@ -1619,8 +1706,7 @@ int main(int argc, char **argv)
                                        elf_visibility_tag_t visibility
                                                = get_elf_visibility_from_string(val);
                                        if (visibility == ELF_VISIBILITY_ERROR) {
-                                               fprintf(stderr, "invalid visibility '%s' specified\n",
-                                                       val);
+                                               errorf(NULL, "invalid visibility '%s' specified", val);
                                                argument_errors = true;
                                        } else {
                                                set_default_visibility(visibility);
@@ -1687,16 +1773,13 @@ int main(int argc, char **argv)
                                                   streq(opt, "align-loops")             ||
                                                   streq(opt, "align-jumps")             ||
                                                   streq(opt, "align-functions")         ||
-                                                  streq(opt, "unroll-loops")            ||
                                                   streq(opt, "PIC")                     ||
                                                   streq(opt, "stack-protector")         ||
                                                   streq(opt, "stack-protector-all")) {
                                                fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
                                        } else {
-                                               int res = firm_option(orig_opt);
-                                               if (res == 0) {
-                                                       fprintf(stderr, "error: unknown Firm option '-f%s'\n",
-                                                               orig_opt);
+                                               if (firm_option(orig_opt) == 0) {
+                                                       errorf(NULL, "unknown Firm option '-f%s'", orig_opt);
                                                        argument_errors = true;
                                                        continue;
                                                }
@@ -1710,17 +1793,19 @@ int main(int argc, char **argv)
                                        fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
                                        help |= HELP_FIRM;
                                } else {
-                                       int res = be_parse_arg(opt);
-                                       if (res == 0) {
-                                               fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
-                                                               opt);
+                                       if (be_parse_arg(opt) == 0) {
+                                               errorf(NULL, "unknown Firm backend option '-b %s'", opt);
                                                argument_errors = true;
                                        } else if (strstart(opt, "isa=")) {
                                                strncpy(cpu_arch, opt, sizeof(cpu_arch));
                                        }
                                }
                        } else if (option[0] == 'W') {
-                               if (strstart(option + 1, "p,")) {
+                               if (strstart(option + 1, "a,")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Wa,");
+                                       add_flag(&asflags_obst, "-Wa,%s", opt);
+                               } else if (strstart(option + 1, "p,")) {
                                        // pass options directly to the preprocessor
                                        const char *opt;
                                        GET_ARG_AFTER(opt, "-Wp,");
@@ -1741,7 +1826,7 @@ int main(int argc, char **argv)
                                           || streq(option+1, "format-security")
                                           || streq(option+1, "old-style-declaration")
                                           || streq(option+1, "type-limits")) {
-                                   /* ignore (gcc compatibility) */
+                                       /* ignore (gcc compatibility) */
                                } else {
                                        set_warning_opt(&option[1]);
                                }
@@ -1777,20 +1862,18 @@ int main(int argc, char **argv)
                                        res &= be_parse_arg(arch_opt);
 
                                        if (res == 0) {
-                                               fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
+                                               errorf(NULL, "unknown architecture '%s'", arch_opt);
                                                argument_errors = true;
                                        }
                                } else if (strstart(opt, "tune=")) {
                                        GET_ARG_AFTER(opt, "-mtune=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (strstart(opt, "cpu=")) {
                                        GET_ARG_AFTER(opt, "-mcpu=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (strstart(opt, "fpmath=")) {
                                        GET_ARG_AFTER(opt, "-mfpmath=");
@@ -1799,31 +1882,28 @@ int main(int argc, char **argv)
                                        else if (streq(opt, "sse"))
                                                opt = "sse2";
                                        else {
-                                               fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
+                                               errorf(NULL, "option -mfpmath supports only 387 or sse");
                                                argument_errors = true;
                                        }
                                        if (!argument_errors) {
                                                snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
-                                               int res = be_parse_arg(arch_opt);
-                                               if (res == 0)
+                                               if (be_parse_arg(arch_opt) == 0)
                                                        argument_errors = true;
                                        }
                                } else if (strstart(opt, "preferred-stack-boundary=")) {
                                        GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (streq(opt, "rtd")) {
                                        default_calling_convention = CC_STDCALL;
                                } else if (strstart(opt, "regparm=")) {
-                                       fprintf(stderr, "error: regparm convention not supported yet\n");
+                                       errorf(NULL, "regparm convention not supported yet");
                                        argument_errors = true;
                                } else if (streq(opt, "soft-float")) {
                                        add_flag(&ldflags_obst, "-msoft-float");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (streq(opt, "sse2")) {
                                        /* ignore for now, our x86 backend always uses sse when
@@ -1831,10 +1911,10 @@ int main(int argc, char **argv)
                                } else {
                                        long int value = strtol(opt, NULL, 10);
                                        if (value == 0) {
-                                               fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
+                                               errorf(NULL, "wrong option '-m %s'",  opt);
                                                argument_errors = true;
                                        } else if (value != 16 && value != 32 && value != 64) {
-                                               fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
+                                               errorf(NULL, "option -m supports only 16, 32 or 64");
                                                argument_errors = true;
                                        } else {
                                                unsigned machine_size = (unsigned)value;
@@ -1844,6 +1924,23 @@ int main(int argc, char **argv)
                                                add_flag(&ldflags_obst, "-m%u", machine_size);
                                        }
                                }
+                       } else if (option[0] == 'X') {
+                               if (streq(option + 1, "assembler")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Xassembler");
+                                       add_flag(&asflags_obst, "-Xassembler");
+                                       add_flag(&asflags_obst, opt);
+                               } else if (streq(option + 1, "preprocessor")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Xpreprocessor");
+                                       add_flag(&cppflags_obst, "-Xpreprocessor");
+                                       add_flag(&cppflags_obst, opt);
+                               } else if (streq(option + 1, "linker")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Xlinker");
+                                       add_flag(&ldflags_obst, "-Xlinker");
+                                       add_flag(&ldflags_obst, opt);
+                               }
                        } else if (streq(option, "pg")) {
                                set_be_option("gprof");
                                add_flag(&ldflags_obst, "-pg");
@@ -1910,13 +2007,14 @@ int main(int argc, char **argv)
                                        print_parenthesis = true;
                                } else if (streq(option, "print-fluffy")) {
                                        mode = PrintFluffy;
+                               } else if (streq(option, "print-compound-sizes")) {
+                                       mode = PrintCompoundSizes;
                                } else if (streq(option, "print-jna")) {
                                        mode = PrintJna;
                                } else if (streq(option, "jna-limit")) {
                                        ++i;
                                        if (i >= argc) {
-                                               fprintf(stderr, "error: "
-                                                       "expected argument after '--jna-limit'\n");
+                                               errorf(NULL, "expected argument after '--jna-limit'");
                                                argument_errors = true;
                                                break;
                                        }
@@ -1924,8 +2022,7 @@ int main(int argc, char **argv)
                                } else if (streq(option, "jna-libname")) {
                                        ++i;
                                        if (i >= argc) {
-                                               fprintf(stderr, "error: "
-                                                       "expected argument after '--jna-libname'\n");
+                                               errorf(NULL, "expected argument after '--jna-libname'");
                                                argument_errors = true;
                                                break;
                                        }
@@ -1940,7 +2037,13 @@ int main(int argc, char **argv)
                                } else if (streq(option, "no-external-pp")) {
                                        external_preprocessor = NULL;
                                } else if (streq(option, "time")) {
-                                       do_timing = true;
+                                       do_timing    = true;
+                                       print_timing = true;
+                               } else if (streq(option, "statev")) {
+                                       do_timing      = true;
+                                       produce_statev = true;
+                               } else if (strstart(option, "filtev=")) {
+                                       GET_ARG_AFTER(filtev, "--filtev=");
                                } else if (streq(option, "version")) {
                                        print_cparser_version();
                                        return EXIT_SUCCESS;
@@ -1967,8 +2070,7 @@ int main(int argc, char **argv)
                                } else if (streq(option, "dump-function")) {
                                        ++i;
                                        if (i >= argc) {
-                                               fprintf(stderr, "error: "
-                                                       "expected argument after '--dump-function'\n");
+                                               errorf(NULL, "expected argument after '--dump-function'");
                                                argument_errors = true;
                                                break;
                                        }
@@ -1979,11 +2081,11 @@ int main(int argc, char **argv)
                                } else if (streq(option, "unroll-loops")) {
                                        /* ignore (gcc compatibility) */
                                } else {
-                                       fprintf(stderr, "error: unknown argument '%s'\n", arg);
+                                       errorf(NULL, "unknown argument '%s'", arg);
                                        argument_errors = true;
                                }
                        } else {
-                               fprintf(stderr, "error: unknown argument '%s'\n", arg);
+                               errorf(NULL, "unknown argument '%s'", arg);
                                argument_errors = true;
                        }
                } else {
@@ -2043,7 +2145,7 @@ int main(int argc, char **argv)
                return EXIT_SUCCESS;
        }
        if (units == NULL) {
-               fprintf(stderr, "error: no input files specified\n");
+               errorf(NULL, "no input files specified");
                argument_errors = true;
        }
 
@@ -2081,25 +2183,16 @@ int main(int argc, char **argv)
        if (do_timing)
                timer_init();
 
-       if (construct_dep_target) {
-               if (outname != 0 && strlen(outname) >= 2) {
-                       get_output_name(dep_target, sizeof(dep_target), outname, ".d");
-               } else {
-                       get_output_name(dep_target, sizeof(dep_target), units->name, ".d");
-               }
-       } else {
-               dep_target[0] = '\0';
-       }
-
        char outnamebuf[4096];
        if (outname == NULL) {
                const char *filename = units->name;
 
-               switch(mode) {
+               switch (mode) {
                case BenchmarkParser:
                case PrintAst:
                case PrintFluffy:
                case PrintJna:
+               case PrintCompoundSizes:
                case PreprocessOnly:
                case ParseOnly:
                        outname = "-";
@@ -2139,13 +2232,31 @@ int main(int argc, char **argv)
        } else {
                out = fopen(outname, "w");
                if (out == NULL) {
-                       fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
-                                       strerror(errno));
+                       position_t const pos = { outname, 0, 0, 0 };
+                       errorf(&pos, "could not open for writing: %s", strerror(errno));
                        return EXIT_FAILURE;
                }
        }
 
+       if (produce_statev && units != NULL) {
+               /* attempt to guess a good name for the file */
+               const char *first_cup = units->name;
+               if (first_cup != NULL) {
+                       const char *dot = strrchr(first_cup, '.');
+                       const char *pos = dot ? dot : first_cup + strlen(first_cup);
+                       char        buf[pos-first_cup+1];
+                       strncpy(buf, first_cup, pos-first_cup);
+                       buf[pos-first_cup] = '\0';
+
+                       stat_ev_begin(buf, filtev);
+               }
+       }
+
        int result = compilation_loop(mode, units, standard, out);
+       if (stat_ev_enabled) {
+               stat_ev_end();
+       }
+
        if (result != EXIT_SUCCESS) {
                if (out != stdout)
                        unlink(outname);
@@ -2154,17 +2265,18 @@ int main(int argc, char **argv)
 
        /* link program file */
        if (mode == CompileAssembleLink) {
-               int result = link_program(units);
-               if (result != EXIT_SUCCESS) {
+               int const link_result = link_program(units);
+               if (link_result != EXIT_SUCCESS) {
                        if (out != stdout)
                                unlink(outname);
-                       return result;
+                       return link_result;
                }
        }
 
        if (do_timing)
-               timer_term(stderr);
+               timer_term(print_timing ? stderr : NULL);
 
+       free_temp_files();
        obstack_free(&cppflags_obst, NULL);
        obstack_free(&ldflags_obst, NULL);
        obstack_free(&asflags_obst, NULL);