started working on correct initializer support (still unfinished and buggy!)
authorMatthias Braun <matze@braunis.de>
Mon, 19 Nov 2007 23:19:13 +0000 (23:19 +0000)
committerMatthias Braun <matze@braunis.de>
Mon, 19 Nov 2007 23:19:13 +0000 (23:19 +0000)
[r18492]

12 files changed:
Makefile
TODO
adt/hashset.c
ast.c
ast.h
ast2firm.c
ast_t.h
main.c
parser.c
parsetest/declarator_special.c
parsetest/initializers.c
write_fluffy.c

index aeda011..145da96 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ FIRM_CFLAGS = -I$(FIRM_HOME)/libfirm/include -I$(FIRM_HOME)/obstack -I$(FIRM_HOM
 FIRM_LIBS = -L$(FIRM_BUILD) -lfirm -llpp -lcore -lm -ldl
 
 CFLAGS += -Wall -W -Wstrict-prototypes -Wmissing-prototypes -Werror -std=c99 -pedantic
-CFLAGS += -DHAVE_CONFIG_H
+CFLAGS += -DHAVE_CONFIG_H -DFIRM_BACKEND
 CFLAGS += -I .
 CFLAGS += -O0 -g3
 CFLAGS += $(FIRM_CFLAGS)
diff --git a/TODO b/TODO
index e5a2b11..a68ab5e 100644 (file)
--- a/TODO
+++ b/TODO
@@ -12,6 +12,7 @@ Parser:
 - outermost typequalifiers can differ between function declarations and
   implementations...
 - fix semantic to ignore type qualifiers (const double is the same as double)
+- label: declaration; is no valid C99 but we parse it anyway
 
 ast2firm:
 - output source file positions for panics...
index 70dce5b..381f165 100644 (file)
@@ -536,7 +536,7 @@ void hashset_init_size(HashSet *this, size_t expected_elements)
                abort();
        }
 
-       needed_size = expected_elements * (1.0 / HT_OCCUPANCY_FLT);
+       needed_size = (size_t) (expected_elements * (1.0 / HT_OCCUPANCY_FLT));
        po2size = ceil_po2(needed_size);
        init_size(this, po2size);
 }
diff --git a/ast.c b/ast.c
index d61ff38..77821bc 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -161,7 +161,7 @@ static void print_unary_expression(const unary_expression_t *unexpr)
                fputs(")", out);
                break;
        case UNEXPR_INVALID:
-               fprintf(out, "unop%d", unexpr->type);
+               fprintf(out, "unop%d", (int) unexpr->type);
                break;
        }
        fputs("(", out);
@@ -294,7 +294,7 @@ void print_expression(const expression_t *expression)
        case EXPR_OFFSETOF:
        case EXPR_STATEMENT:
                /* TODO */
-               fprintf(out, "some expression of type %d", expression->type);
+               fprintf(out, "some expression of type %d", (int) expression->type);
                break;
        }
 }
@@ -512,10 +512,11 @@ static void print_storage_class(storage_class_t storage_class)
 void print_initializer(const initializer_t *initializer)
 {
        if(initializer->type == INITIALIZER_VALUE) {
-               print_expression(initializer->v.value);
+               //print_expression(initializer->v.value);
                return;
        }
 
+#if 0
        assert(initializer->type == INITIALIZER_LIST);
        fputs("{ ", out);
        initializer_t *iter = initializer->v.list;
@@ -526,6 +527,7 @@ void print_initializer(const initializer_t *initializer)
                }
        }
        fputs("}", out);
+#endif
 }
 
 static void print_normal_declaration(const declaration_t *declaration)
diff --git a/ast.h b/ast.h
index 1b1f8d6..ef9ae43 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -29,6 +29,8 @@ typedef struct builtin_symbol_expression_t  builtin_symbol_expression_t;
 typedef struct classify_type_expression_t   classify_type_expression_t;
 
 typedef struct initializer_t                initializer_t;
+typedef struct initializer_list_t           initializer_list_t;
+typedef struct initializer_value_t          initializer_value_t;
 typedef struct declaration_t                declaration_t;
 
 typedef struct statement_t                  statement_t;
index 71c8e78..e5eb0d6 100644 (file)
@@ -106,7 +106,7 @@ static ident *unique_ident(const char *tag)
 {
        char buf[256];
 
-       snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
+       snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
        unique_id++;
        return new_id_from_str(buf);
 }
@@ -1885,9 +1885,9 @@ static void create_initializer(declaration_t *declaration)
                return;
 
        if(initializer->type == INITIALIZER_VALUE) {
-               assert(initializer->designator == NULL);
-               assert(initializer->next == NULL);
-               ir_node *init_node = expression_to_firm(initializer->v.value);
+               initializer_value_t *value = (initializer_value_t*) initializer;
+
+               ir_node *init_node = expression_to_firm(value->value);
 
                if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
                        set_value(declaration->v.value_number, init_node);
diff --git a/ast_t.h b/ast_t.h
index 7d3fc37..ccb2cea 100644 (file)
--- a/ast_t.h
+++ b/ast_t.h
@@ -220,13 +220,19 @@ typedef enum {
 } initializer_type_t;
 
 struct initializer_t {
-       initializer_type_t type;
-       designator_t      *designator;
-       union {
-               initializer_t *list;
-               expression_t  *value;
-       } v;
-       initializer_t *next;
+       initializer_type_t  type;
+       initializer_t      *next;
+};
+
+struct initializer_value_t {
+       initializer_t initializer;
+       expression_t *value;
+};
+
+struct initializer_list_t {
+       initializer_t  initializer;
+       size_t         len;
+       initializer_t *initializers[];
 };
 
 struct declaration_t {
diff --git a/main.c b/main.c
index 79807dd..6b240ad 100644 (file)
--- a/main.c
+++ b/main.c
@@ -325,6 +325,36 @@ int main(int argc, char **argv)
                        mode = PrintFluffy;
                } else if(strcmp(arg, "-v") == 0) {
                        verbose = 1;
+               } else if(arg[0] == '-' && arg[1] == 'f') {
+                       const char *opt = &arg[2];
+                       if(opt[0] == 0) {
+                               ++i;
+                               if(i >= argc) {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                               opt = argv[i];
+                               if(opt[0] == '-') {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                       }
+                       //firm_option(opt);
+               } else if(arg[0] == '-' && arg[1] == 'b') {
+                       const char *opt = &arg[2];
+                       if(opt[0] == 0) {
+                               ++i;
+                               if(i >= argc) {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                               opt = argv[i];
+                               if(opt[0] == '-') {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                       }
+                       //firm_be_option(opt);
                } else if(arg[0] == '-') {
                        if (arg[1] == 'D' ||
                                        arg[1] == 'O' ||
index 1d9a9a7..702cdcf 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -341,6 +341,14 @@ static void eat_brace(void)
     }                                              \
     next_token();
 
+#define expect_block(expected)                     \
+    if(UNLIKELY(token.type != (expected))) {       \
+        parse_error_expected(NULL, (expected), 0); \
+        eat_block();                               \
+        return NULL;                               \
+    }                                              \
+    next_token();
+
 #define expect_void(expected)                      \
     if(UNLIKELY(token.type != (expected))) {       \
         parse_error_expected(NULL, (expected), 0); \
@@ -844,6 +852,7 @@ attributes_finished:
        ;
 }
 
+#if 0
 static designator_t *parse_designation(void)
 {
        if(token.type != '[' && token.type != '.')
@@ -886,69 +895,225 @@ static designator_t *parse_designation(void)
                last = designator;
        }
 }
+#endif
 
-static initializer_t *parse_initializer_list(type_t *type);
-
-static initializer_t *parse_initializer(type_t *type)
+static initializer_t *initializer_from_expression(type_t *type,
+                                                  expression_t *expression)
 {
-       designator_t *designator = parse_designation();
+       initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
 
-       initializer_t *result;
-       if(token.type == '{') {
-               result = parse_initializer_list(type);
-       } else {
-               result          = allocate_ast_zero(sizeof(result[0]));
-               result->type    = INITIALIZER_VALUE;
-               result->v.value = parse_assignment_expression();
+       /* TODO check that expression is a constant expression */
 
-               if(type != NULL) {
-                       semantic_assign(type, &result->v.value, "initializer");
+       /* § 6.7.8.14/15 char array may be initialized by string literals */
+       if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
+               array_type_t *array_type   = (array_type_t*) type;
+               type_t       *element_type = array_type->element_type;
+
+               if(element_type->type == TYPE_ATOMIC) {
+                       atomic_type_t      *atomic_type = (atomic_type_t*) element_type;
+                       atomic_type_type_t  atype       = atomic_type->atype;
+
+                       /* TODO handle wide strings */
+                       if(atype == ATOMIC_TYPE_CHAR
+                                       || atype == ATOMIC_TYPE_SCHAR
+                                       || atype == ATOMIC_TYPE_UCHAR) {
+                               /* it's fine TODO: check for length of string array... */
+                               goto initializer_from_expression_finished;
+                       }
                }
        }
-       result->designator = designator;
 
-       return result;
+       semantic_assign(type, &expression, "initializer");
+
+initializer_from_expression_finished:
+       result->initializer.type = INITIALIZER_VALUE;
+       result->value            = expression;
+
+       return (initializer_t*) result;
 }
 
-static initializer_t *parse_initializer_list(type_t *type)
+static initializer_t *parse_sub_initializer(type_t *type,
+                                            expression_t *expression,
+                                            type_t *expression_type);
+
+static initializer_t *parse_sub_initializer_elem(type_t *type)
 {
-       eat('{');
+       if(token.type == '{') {
+               return parse_sub_initializer(type, NULL, NULL);
+       }
+
+       expression_t *expression      = parse_assignment_expression();
+       type_t       *expression_type = skip_typeref(expression->datatype);
 
-       /* TODO: semantic */
-       (void) type;
+       return parse_sub_initializer(type, expression, expression_type);
+}
 
-       initializer_t *result = allocate_ast_zero(sizeof(result[0]));
-       result->type = INITIALIZER_LIST;
+static bool had_initializer_brace_warning;
 
-       initializer_t *last = NULL;
-       while(1) {
-               initializer_t *initializer = parse_initializer(NULL);
-               if(last != NULL) {
-                       last->next = initializer;
+static initializer_t *parse_sub_initializer(type_t *type,
+                                            expression_t *expression,
+                                            type_t *expression_type)
+{
+       if(is_type_scalar(type)) {
+               /* there might be extra {} hierarchies */
+               if(token.type == '{') {
+                       next_token();
+                       if(!had_initializer_brace_warning) {
+                               parse_warning("braces around scalar initializer");
+                               had_initializer_brace_warning = true;
+                       }
+                       initializer_t *result = parse_sub_initializer(type, NULL, NULL);
+                       if(token.type == ',') {
+                               next_token();
+                               /* TODO: warn about excessive elements */
+                       }
+                       expect_block('}');
+                       return result;
+               }
+
+               if(expression == NULL) {
+                       expression = parse_assignment_expression();
+               }
+               return initializer_from_expression(type, expression);
+       }
+
+       /* TODO: ignore qualifiers, comparing pointers is probably
+        * not correct */
+       if(expression != NULL && expression_type == type) {
+               initializer_t *result = allocate_ast_zero(sizeof(result[0]));
+               result->type          = INITIALIZER_VALUE;
+
+               if(type != NULL) {
+                       semantic_assign(type, &expression, "initializer");
+               }
+               //result->v.value = expression;
+
+               return result;
+       }
+
+       bool read_paren = false;
+       if(token.type == '{') {
+               next_token();
+               read_paren = true;
+       }
+
+       /* descend into subtype */
+       initializer_t *result = NULL;
+       if(type->type == TYPE_ARRAY) {
+               array_type_t *array_type   = (array_type_t*) type;
+               type_t       *element_type = array_type->element_type;
+               element_type               = skip_typeref(element_type);
+
+               result
+                       = parse_sub_initializer(element_type, expression, expression_type);
+       } else {
+               assert(type->type == TYPE_COMPOUND_STRUCT
+                               || type->type == TYPE_COMPOUND_UNION);
+               compound_type_t *compound_type = (compound_type_t*) type;
+               context_t       *context       = & compound_type->declaration->context;
+
+               declaration_t *first = context->declarations;
+               if(first == NULL)
+                       return NULL;
+               type_t *first_type = first->type;
+               first_type         = skip_typeref(first_type);
+
+               initializer_t *sub;
+               had_initializer_brace_warning = false;
+               if(expression == NULL) {
+                       sub = parse_sub_initializer_elem(first_type);
                } else {
-                       result->v.list = initializer;
+                       sub = parse_sub_initializer(first_type, expression,expression_type);
                }
-               last = initializer;
 
-               if(token.type == '}')
-                       break;
+               /* didn't match the subtypes -> try our parent type */
+               if(sub == NULL) {
+                       assert(!read_paren);
+                       return NULL;
+               }
 
-               if(token.type != ',') {
-                       parse_error_expected("while parsing initializer list", ',', '}', 0);
-                       eat_block();
-                       return result;
+               initializer_t **elems = NEW_ARR_F(initializer_t*, 0);
+               ARR_APP1(initializer_t*, elems, sub);
+
+               declaration_t *iter  = first->next;
+               for( ; iter != NULL; iter = iter->next) {
+                       if(iter->symbol == NULL)
+                               continue;
+                       if(iter->namespc != NAMESPACE_NORMAL)
+                               continue;
+
+                       if(token.type == '}')
+                               break;
+                       expect_block(',');
+
+                       type_t *iter_type = iter->type;
+                       iter_type         = skip_typeref(iter_type);
+
+                       /* read next token */
+                       initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
+                       if(sub == NULL) {
+                               /* TODO error, do nicer cleanup*/
+                               parse_error("member initializer didn't match");
+                               DEL_ARR_F(elems);
+                               return NULL;
+                       }
+                       ARR_APP1(initializer_t*, elems, sub);
                }
-               eat(',');
 
-               if(token.type == '}')
-                       break;
+               int    len        = ARR_LEN(elems);
+               size_t elems_size = sizeof(initializer_t*) * len;
+
+               initializer_list_t *init
+                       = allocate_ast_zero(sizeof(init[0]) + elems_size);
+
+               init->initializer.type = INITIALIZER_LIST;
+               init->len              = len;
+               memcpy(init->initializers, elems, elems_size);
+               DEL_ARR_F(elems);
+
+               result = (initializer_t*) init;
        }
 
-       expect('}');
+       if(read_paren) {
+               if(token.type == ',')
+                       next_token();
+               expect('}');
+       }
+       return result;
+}
+
+static initializer_t *parse_initializer(type_t *type)
+{
+       initializer_t *result;
+
+       type = skip_typeref(type);
+
+       if(token.type != '{') {
+               expression_t *expression = parse_assignment_expression();
+               return initializer_from_expression(type, expression);
+       }
+
+       if(is_type_scalar(type)) {
+               /* § 6.7.8.11 */
+               eat('{');
+
+               expression_t *expression = parse_assignment_expression();
+               result = initializer_from_expression(type, expression);
+
+               if(token.type == ',')
+                       next_token();
+
+               expect('}');
+               return result;
+       } else {
+               result = parse_sub_initializer(type, NULL, NULL);
+       }
 
        return result;
 }
 
+
+
 static declaration_t *parse_compound_type_specifier(bool is_struct)
 {
        if(is_struct) {
@@ -1762,6 +1927,14 @@ static construct_type_t *parse_inner_declarator(declaration_t *declaration,
                if(may_be_abstract)
                        break;
                parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
+               /* avoid a loop in the outermost scope, because eat_statement doesn't
+                * eat '}' */
+               if(token.type == '}' && current_function == NULL) {
+                       next_token();
+               } else {
+                       eat_statement();
+               }
+               return NULL;
        }
 
        while(true) {
@@ -1918,8 +2091,7 @@ static void parser_error_multiple_definition(declaration_t *previous,
        fprintf(stderr, "multiple definition of symbol '%s'\n",
                declaration->symbol->string);
        parser_print_error_prefix_pos(previous->source_position);
-       fprintf(stderr, "this is the location of the previous "
-               "definition.\n");
+       fprintf(stderr, "this is the location of the previous definition.\n");
 }
 
 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
@@ -1933,7 +2105,7 @@ static void parse_init_declarators(const declaration_specifiers_t *specifiers)
                type_t *type = declaration->type;
                if(type->type != TYPE_FUNCTION && declaration->is_inline) {
                        parser_print_warning_prefix_pos(declaration->source_position);
-                       fprintf(stderr, "variable ‘%s’ declared ‘inline’\n",
+                       fprintf(stderr, "variable '%s' declared 'inline'\n",
                                declaration->symbol->string);
                }
 
@@ -2791,12 +2963,11 @@ static expression_t *parse_conditional_expression(unsigned precedence,
        conditional->condition = expression;
 
        /* 6.5.15.2 */
-       type_t *condition_type = conditional->condition->datatype;
-       if(condition_type != NULL) {
-               if(!is_type_scalar(skip_typeref(condition_type))) {
-                       type_error("expected a scalar type", expression->source_position,
-                                  condition_type);
-               }
+       type_t *condition_type_orig = conditional->condition->datatype;
+       type_t *condition_type      = skip_typeref(condition_type_orig);
+       if(condition_type != NULL && !is_type_scalar(condition_type)) {
+               type_error("expected a scalar type", expression->source_position,
+                          condition_type_orig);
        }
 
        conditional->true_expression = parse_expression();
index 75964ad..f9639a4 100644 (file)
@@ -9,6 +9,7 @@ int (* const (fip) (void))(void)
 }
 
 int main(void) {
-       int(*func)(void) = fip();
+       int(*func)(void);
+       func = fip();
        return func() == 42 ? 0 : 1;
 }
index 7c473b0..0df6188 100644 (file)
@@ -22,18 +22,20 @@ struct foo {
 };
 
 struct foo f1 = { 1, 2 };
-struct foo f2 = { { 1, }, 2 }; /* produces a warning on icc and gcc... */
+struct foo f2 = { { 1,
+                                       }
+                                       , 2 }; /* produces a warning on icc and gcc... */
 struct foo f3 = { { { 1, } }, 2 }; /* produces a warning on icc and gcc... */
 
 struct foob {
        int a;
        struct foobb {
                float c, d;
-       };
+       } f;
        int e;
 };
 
-struct foob ff2 = { 1, 2.5, 4, 2 };
+struct foob ff2 = { 1, 2.5, 4.4, 2 };
 
 union foou {
        int a;
index e8ccbd7..e2d9aac 100644 (file)
@@ -240,7 +240,7 @@ static void write_initializer(const initializer_t *initializer)
                panic("list initializers not supported yet");
        }
 
-       write_expression(initializer->v.value);
+       //write_expression(initializer->v.value);
 }
 
 static void write_enum(const symbol_t *symbol, const enum_type_t *type)