From patchwork Fri May 25 15:04:03 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Novillo X-Patchwork-Id: 161379 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 06824B6EEB for ; Sat, 26 May 2012 01:04:36 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1338563077; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Received:Received:To:Subject:Message-Id:Date: From:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=o2ZOOzV TUkLnWGdFmE/gbUyX4Tw=; b=sp9Yaf+PL1MrTKpDEK0rjoSsLCamx8LYMkQ+p2j v0rBBjvpkmmRubKQnw1ofUP69N5jRXh4Kr3ffIi2Yyjp0GlxhReGjy/b9KkNo1iW 3yO7rjlToBEukHFm3wxAepVTjeEus4rMMAdccXeFCrQIFy9iGET9QUBrvOhzVp59 oQJk= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:Received:Received:To:Subject:Message-Id:Date:From:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=fVhbx+Y5Sz0BGETXzEiXu4xNNkMukldzfEKzG7xkPWG/QIq9hEwF282yKjFVvN 4AUL56S8EwPZqqZztEx3VrYd+YEMo3WkQ/7Pg7SCtPSLuAS1r2MrH8p9ACxpAt7J SsN4szyz2FZsPjvsrPY6uOBh+SlAvGiLwTis26Zoh3y34=; Received: (qmail 8726 invoked by alias); 25 May 2012 15:04:27 -0000 Received: (qmail 8708 invoked by uid 22791); 25 May 2012 15:04:23 -0000 X-SWARE-Spam-Status: No, hits=-4.2 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-qc0-f201.google.com (HELO mail-qc0-f201.google.com) (209.85.216.201) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 25 May 2012 15:04:05 +0000 Received: by qcse14 with SMTP id e14so115093qcs.2 for ; Fri, 25 May 2012 08:04:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=to:subject:message-id:date:from:x-gm-message-state; bh=DURmWeUVGdcJbJshGFdY77OhxFemmuDrz03X1Sa7rvs=; b=YvqwuXBqem19nKhq564xGA8V+rTGaw9y4A7Qvr8R2qM4CjQtv2Qddr1PpAF672ZqZ0 s9bNOsrznu8jMu2l+U3lUnm5C21he8DPcf4Re3fxM4uIny3ujIjYfGzswR7Umru5EgRU 2I//KvnlQfY4XUVZ7u6MoH38/0KXK02epTX0FBxPy839yS2Dyx8YNDppGd0qV/JsxVmr x2VwjqrVHyQHMP6xr8+Q/9ZCCdIT1qTWoqYjAo0MQ6BEo568K+pZaK008MKkcEvgtrXs U2P8CLXg2c5S4BByBAk1wm9ji3hVpuOgMnJSu+ACKiCnvxoU9Kci/RuPiQitAgRwTjss vF7Q== Received: by 10.236.161.98 with SMTP id v62mr3916079yhk.4.1337958244684; Fri, 25 May 2012 08:04:04 -0700 (PDT) Received: by 10.236.161.98 with SMTP id v62mr3916071yhk.4.1337958244644; Fri, 25 May 2012 08:04:04 -0700 (PDT) Received: from wpzn4.hot.corp.google.com (216-239-44-65.google.com [216.239.44.65]) by gmr-mx.google.com with ESMTPS id v25si3483435yhm.0.2012.05.25.08.04.04 (version=TLSv1/SSLv3 cipher=AES128-SHA); Fri, 25 May 2012 08:04:04 -0700 (PDT) Received: from tobiano.tor.corp.google.com (tobiano.tor.corp.google.com [172.29.41.6]) by wpzn4.hot.corp.google.com (Postfix) with ESMTP id 7DBA91E004D; Fri, 25 May 2012 08:04:04 -0700 (PDT) Received: by tobiano.tor.corp.google.com (Postfix, from userid 54752) id 0FB26AE1DF; Fri, 25 May 2012 11:04:03 -0400 (EDT) To: reply@codereview.appspotmail.com, soni.sandeepb@gmail.com, gcc-patches@gcc.gnu.org Subject: [gimplefe] Fix parsing of assign_stmt (issue6247046) Message-Id: <20120525150404.0FB26AE1DF@tobiano.tor.corp.google.com> Date: Fri, 25 May 2012 11:04:03 -0400 (EDT) From: dnovillo@google.com (Diego Novillo) X-Gm-Message-State: ALoCoQlmh4PYZ3aabsGZG9AYpiZ6+cBvKp9+Sms++cNTLxgYNSW6HoUgifIjcqtllcmxE4Il8urTVyfeIbxciqcMtk4mYNPIZsLDUH7/edIQT3/c56oXV6jPOvQhownYdZZBF1wEENdRI7w+BatQG//SKlk/AvU5pA== X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Fix parser initialization and parsing of gimple_assign. This patch fixes the parser to use the new line map table initialization code. It was not setting the allocation function pointers properly. Additionally, we were only expecting binary RHS for gimple_assign. This was causing failures in the small test file I added yesterday. Sandeep, now that we have an initial harness for testing, could you please add a set of .gimple files that test the *whole* gimple grammar? (or at least a large chunk of it). An initial starting point would be to use -fdump-tree-gimple-raw on the compilation of several C/C++ source files. This will give you an initial set of test cases to put in the testsuite to exercise the recognizer. Tested on x86_64. 2012-05-25 Diego Novillo gimple/ChangeLog * parser.c (gimple_register_var_decl_in_symtab): Tidy. (gl_peek_token): New. (gl_consume_token): Call it. (gl_tree_code_for_token): Add FIXME note. (gl_gimple_code_for_token): Likewise. (gl_dump): Surround the current token in '[[[' ']]]'. (gl_consume_expected_token): Only emit an error if no other errors have been emitted. (gp_parse_expect_subcode): Return the recognized code. Return the read token in new argument *TOKEN_P. Update all callers. (gp_parse_expect_lhs): Call gl_peek_token. (gp_parse_expect_rhs_op): Rename from gp_parse_expect_rhs1. Call gl_peek_token. (gp_parse_expect_rhs2): Remove. (gp_parse_assign_stmt): Re-write to support all gimple RHS classes. (gp_parse_cond_stmt): Tidy. Emit an error if OPCODE is anything but a GIMPLE_BINARY_RHS. (gp_parse_goto_stmt): Tidy. (gp_parse_label_stmt): Tidy. (gp_parse_switch_stmt): Tidy. (gp_parse_stmt): Do not emit an error if one has been emitted already. (realloc_for_line_map): New. (gp_init): Use it. Update code to initialize the line table. (gimple_main): Tidy. testsuite/ChangeLog.gimplefe: * gimple.dg/20120523-1.gimple: Add expected error. * gimple.dg/20120525-1.gimple: New. --- This patch is available for review at http://codereview.appspot.com/6247046 diff --git a/gcc/gimple/parser.c b/gcc/gimple/parser.c index 4b29333..3f3eb96 100644 --- a/gcc/gimple/parser.c +++ b/gcc/gimple/parser.c @@ -152,10 +152,10 @@ gl_token_as_text (const gimple_token *token) static void gimple_register_var_decl_in_symtab (const gimple_token *name_token) { - const char *name = gl_token_as_text(name_token); - tree id = get_identifier(name); - tree decl = build_decl(name_token->location, VAR_DECL, get_identifier(name), void_type_node); - + const char *name = gl_token_as_text (name_token); + tree id = get_identifier (name); + tree decl = build_decl (name_token->location, VAR_DECL, + get_identifier(name), void_type_node); gimple_symtab_register_decl (decl,id); } @@ -168,15 +168,25 @@ gl_at_eof (gimple_lexer *lexer) } -/* Consume the next token from LEXER. */ +/* Peek into the next token from LEXER. */ static gimple_token * -gl_consume_token (gimple_lexer *lexer) +gl_peek_token (gimple_lexer *lexer) { if (gl_at_eof (lexer)) return &gl_eof_token; + return VEC_index (gimple_token, lexer->tokens, lexer->cur_token_ix); +} - return VEC_index (gimple_token, lexer->tokens, lexer->cur_token_ix++); + +/* Consume the next token from LEXER. */ + +static gimple_token * +gl_consume_token (gimple_lexer *lexer) +{ + gimple_token *tok = gl_peek_token (lexer); + lexer->cur_token_ix++; + return tok; } @@ -189,6 +199,7 @@ gl_tree_code_for_token (const gimple_token *token) size_t code; const char *s; + /* FIXME. Expensive linear scan, convert into a string->code map. */ s = gl_token_as_text (token); for (code = ERROR_MARK; code < LAST_AND_UNUSED_TREE_CODE; code++) if (strcasecmp (s, tree_code_name[code]) == 0) @@ -207,6 +218,7 @@ gl_gimple_code_for_token (const gimple_token *token) size_t code; const char *s; + /* FIXME. Expensive linear scan, convert into a string->code map. */ s = gl_token_as_text (token); for (code = GIMPLE_ERROR_MARK; code < LAST_AND_UNUSED_GIMPLE_CODE; code++) if (strcasecmp (s, gimple_code_name[code]) == 0) @@ -215,6 +227,7 @@ gl_gimple_code_for_token (const gimple_token *token) return (enum gimple_code) code; } + /* Return true if TOKEN is the start of a declaration. */ static bool @@ -272,7 +285,15 @@ gl_dump (FILE *file, gimple_lexer *lexer) lexer->cur_token_ix); for (i = 0; VEC_iterate (gimple_token, lexer->tokens, i, token); i++) - gl_dump_token (file, token); + { + if (i == lexer->cur_token_ix) + fprintf (file, "[[[ "); + gl_dump_token (file, token); + if (i == lexer->cur_token_ix) + fprintf (file, "]]]"); + } + + fprintf (file, "\n"); } @@ -293,7 +314,7 @@ static const gimple_token * gl_consume_expected_token (gimple_lexer *lexer, enum cpp_ttype expected) { const gimple_token *next_token = gl_consume_token (lexer); - if (next_token->type != expected) + if (next_token->type != expected && !errorcount) error_at (next_token->location, "token '%s' is not of the expected type '%s'", gl_token_as_text (next_token), cpp_type2name (expected, 0)); @@ -303,13 +324,15 @@ gl_consume_expected_token (gimple_lexer *lexer, enum cpp_ttype expected) /* Helper for gp_parse_assign_stmt and gp_parse_cond_stmt. - Peeks a token by reading from reader PARSER and looks it up to match - against the tree codes. */ + Consumes a token by reading from reader PARSER and looks it up to match + against the tree codes. Returns the tree code or emits a diagnostic + if no valid tree code was found. Additionally, if TOKEN_P is not + NULL, it returns the read token in *TOKEN_P. */ -static void -gp_parse_expect_subcode (gimple_parser *parser) +static enum tree_code +gp_parse_expect_subcode (gimple_parser *parser, gimple_token **token_p) { - const gimple_token *next_token; + gimple_token *next_token; enum tree_code code; gl_consume_expected_token (parser->lexer, CPP_LESS); @@ -327,8 +350,10 @@ gp_parse_expect_subcode (gimple_parser *parser) gl_consume_expected_token (parser->lexer, CPP_COMMA); - /* FIXME From this function we should return the tree code since it - can be used by the other helper functions to recognize precisely. */ + if (token_p) + *token_p = next_token; + + return code; } @@ -343,7 +368,7 @@ gp_parse_expect_lhs (gimple_parser *parser) /* Just before the name of the identifier we might get the symbol of dereference too. If we do get it then consume that token, else continue recognizing the name. */ - next_token = gl_consume_token (parser->lexer); + next_token = gl_peek_token (parser->lexer); if (next_token->type == CPP_MULT) next_token = gl_consume_token (parser->lexer); @@ -351,15 +376,16 @@ gp_parse_expect_lhs (gimple_parser *parser) gl_consume_expected_token (parser->lexer, CPP_COMMA); } + /* Helper for gp_parse_assign_stmt. The token read from reader PARSER should be the first operand in rhs of the tuple. */ static void -gp_parse_expect_rhs1 (gimple_parser *parser) +gp_parse_expect_rhs_op (gimple_parser *parser) { const gimple_token *next_token; - next_token = gl_consume_token (parser->lexer); + next_token = gl_peek_token (parser->lexer); /* Currently there is duplication in the following blocks but there would be more stuff added here as we go on. */ @@ -381,50 +407,58 @@ gp_parse_expect_rhs1 (gimple_parser *parser) default: break; } - - gl_consume_expected_token (parser->lexer, CPP_COMMA); } -/* Helper for gp_parse_assign_stmt. The token read from reader PARSER should - be the second operand in rhs of the tuple. */ +/* Parse a gimple_assign tuple that is read from the reader PARSER. + For now we only recognize the tuple. Refer gimple.def for the + format of this tuple. */ static void -gp_parse_expect_rhs2 (gimple_parser *parser) +gp_parse_assign_stmt (gimple_parser *parser) { - const gimple_token *next_token; - next_token = gl_consume_token (parser->lexer); + gimple_token *optoken; + enum tree_code opcode; + enum gimple_rhs_class rhs_class; - /* ??? Can there be more possibilities than these ? */ - switch (next_token->type) + opcode = gp_parse_expect_subcode (parser, &optoken); + gp_parse_expect_lhs (parser); + + rhs_class = get_gimple_rhs_class (opcode); + switch (rhs_class) { - case CPP_NAME: - /* Handle a special case, this can be NULL too. */ + case GIMPLE_INVALID_RHS: + error_at (optoken->location, "Invalid RHS for " + "gimple assignment: %s", tree_code_name[opcode]); + break; - case CPP_NUMBER: - break; + case GIMPLE_SINGLE_RHS: + case GIMPLE_UNARY_RHS: + case GIMPLE_BINARY_RHS: + case GIMPLE_TERNARY_RHS: + gp_parse_expect_rhs_op (parser); + if (rhs_class == GIMPLE_BINARY_RHS || rhs_class == GIMPLE_TERNARY_RHS) + { + gl_consume_expected_token (parser->lexer, CPP_COMMA); + gp_parse_expect_rhs_op (parser); + } + if (rhs_class == GIMPLE_TERNARY_RHS) + { + gl_consume_expected_token (parser->lexer, CPP_COMMA); + gp_parse_expect_rhs_op (parser); + } + break; - default: - break; + default: + gcc_unreachable (); } - gl_consume_expected_token (parser->lexer, CPP_GREATER); -} - -/* Parse a gimple_assign tuple that is read from the reader PARSER. For now we - only recognize the tuple. Refer gimple.def for the format of this tuple. */ - -static void -gp_parse_assign_stmt (gimple_parser *parser) -{ - gp_parse_expect_subcode (parser); - gp_parse_expect_lhs (parser); - gp_parse_expect_rhs1 (parser); - gp_parse_expect_rhs2 (parser); + gl_consume_expected_token (parser->lexer, CPP_GREATER); } /* Helper for gp_parse_cond_stmt. The token read from reader PARSER should be the first operand in the tuple. */ + static void gp_parse_expect_op1 (gimple_parser *parser) { @@ -498,21 +532,26 @@ gp_parse_expect_false_label (gimple_parser *parser) gl_consume_expected_token (parser->lexer, CPP_GREATER); } -/* Parse a gimple_cond tuple that is read from the reader PARSER. For now we only - recognize the tuple. Refer gimple.def for the format of this tuple. */ +/* Parse a gimple_cond tuple that is read from the reader PARSER. For + now we only recognize the tuple. Refer gimple.def for the format of + this tuple. */ static void gp_parse_cond_stmt (gimple_parser *parser) { - gp_parse_expect_subcode (parser); + gimple_token *optoken; + enum tree_code opcode = gp_parse_expect_subcode (parser, &optoken); + if (get_gimple_rhs_class (opcode) != GIMPLE_BINARY_RHS) + error_at (optoken->location, "Unsupported gimple_cond expression"); gp_parse_expect_op1 (parser); gp_parse_expect_op2 (parser); gp_parse_expect_true_label (parser); gp_parse_expect_false_label (parser); } -/* Parse a gimple_goto tuple that is read from the reader PARSER. For now we only - recognize the tuple. Refer gimple.def for the format of this tuple. */ +/* Parse a gimple_goto tuple that is read from the reader PARSER. For + now we only recognize the tuple. Refer gimple.def for the format of + this tuple. */ static void gp_parse_goto_stmt (gimple_parser *parser) @@ -524,8 +563,9 @@ gp_parse_goto_stmt (gimple_parser *parser) gl_consume_expected_token (parser->lexer, CPP_GREATER); } -/* Parse a gimple_label tuple that is read from the reader PARSER. For now we only - recognize the tuple. Refer gimple.def for the format of this tuple. */ +/* Parse a gimple_label tuple that is read from the reader PARSER. For + now we only recognize the tuple. Refer gimple.def for the format of + this tuple. */ static void gp_parse_label_stmt (gimple_parser *parser) @@ -537,8 +577,9 @@ gp_parse_label_stmt (gimple_parser *parser) gl_consume_expected_token (parser->lexer, CPP_GREATER); } -/* Parse a gimple_switch tuple that is read from the reader PARSER. For now we only - recognize the tuple. Refer gimple.def for the format of this tuple. */ +/* Parse a gimple_switch tuple that is read from the reader PARSER. + For now we only recognize the tuple. Refer gimple.def for the + format of this tuple. */ static void gp_parse_switch_stmt (gimple_parser *parser) @@ -575,6 +616,7 @@ gp_parse_switch_stmt (gimple_parser *parser) } } + /* Helper for gp_parse_call_stmt. The token read from reader PARSER should be the name of the function called. */ @@ -677,7 +719,7 @@ gp_parse_stmt (gimple_parser *parser, const gimple_token *token) { enum gimple_code code = gl_gimple_code_for_token (token); - if (code == LAST_AND_UNUSED_GIMPLE_CODE) + if (code == LAST_AND_UNUSED_GIMPLE_CODE && !errorcount) error_at (token->location, "Invalid gimple code used"); else { @@ -1077,6 +1119,16 @@ gl_init (gimple_parser *parser, const char *fname) } +/* A helper function; used as the reallocator function for cpp's line + table. */ + +static void * +realloc_for_line_map (void *ptr, size_t len) +{ + return GGC_RESIZEVAR (void, ptr, len); +} + + /* Initialize the parser data structures. FNAME is the name of the input gimple file being compiled. */ @@ -1084,10 +1136,11 @@ static gimple_parser * gp_init (const char *fname) { gimple_parser *parser = ggc_alloc_cleared_gimple_parser (); - line_table = parser->line_table = ggc_alloc_cleared_line_maps (); - parser->ident_hash = ident_hash; - + line_table = parser->line_table = ggc_alloc_line_maps (); linemap_init (parser->line_table); + parser->line_table->reallocator = realloc_for_line_map; + parser->line_table->round_alloc_size = ggc_round_alloc_size; + parser->ident_hash = ident_hash; parser->lexer = gl_init (parser, fname); return parser; @@ -1559,7 +1612,7 @@ gimple_main (void) if (parser->lexer->filename == NULL) return; - gimple_symtab_maybe_init_hash_table(); + gimple_symtab_maybe_init_hash_table (); gl_lex (parser->lexer); gp_parse (parser); gp_finish (parser); diff --git a/gcc/testsuite/gimple.dg/20120523-1.gimple b/gcc/testsuite/gimple.dg/20120523-1.gimple index 54c39ac..c73db3f 100644 --- a/gcc/testsuite/gimple.dg/20120523-1.gimple +++ b/gcc/testsuite/gimple.dg/20120523-1.gimple @@ -1,2 +1 @@ -gimple_assign - +gimple_assign /* { dg-error "Invalid RHS.*modify_expr" } */ diff --git a/gcc/testsuite/gimple.dg/20120525-1.gimple b/gcc/testsuite/gimple.dg/20120525-1.gimple new file mode 100644 index 0000000..0e21396 --- /dev/null +++ b/gcc/testsuite/gimple.dg/20120525-1.gimple @@ -0,0 +1 @@ +gimple_assign