From patchwork Tue Feb 4 02:26:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Schmidt X-Patchwork-Id: 1233106 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-518833-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=ydId17Up; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48BT9w47D2z9sX3 for ; Tue, 4 Feb 2020 13:27:32 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; q=dns; s=default; b=xhYOEh5yqeWxxh4gf9f XBvyJHFa4VepJ4mKCntYC/JZ1REgWzErt9Ht+tu5DDfLWa8DeP3vxTxg66zpoDEE KW7rPsajSVSQOmQKCtmBPAwqS1w4abYEF14XNGxNgLMLYVeFBLYewz3dVNXr/xK/ vxmZtbQOB5puLeop2x7mMAo4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; s=default; bh=i+NCdHo6Man8cW9Ip4GILQhnm zc=; b=ydId17Up0IG+iIzxxMYkOOUei9R/p6ks/H8uTAwgGbqhIH7F7zKMb7gOI fosVhNVZTVPPHO6i2ql2ue7f4nP6JNovdg3hsBDdX7mePBJd6UIJvhkIQ69vOPWe 0YzFG/vq8LHm5eMWUA9tSQbcVc4khEufK84/7Cowb0+R3xxwzQ= Received: (qmail 65211 invoked by alias); 4 Feb 2020 02:27:10 -0000 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 Received: (qmail 65130 invoked by uid 89); 4 Feb 2020 02:27:09 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 04 Feb 2020 02:27:07 +0000 Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0142PMFN037688; Mon, 3 Feb 2020 21:27:06 -0500 Received: from ppma04wdc.us.ibm.com (1a.90.2fa9.ip4.static.sl-reverse.com [169.47.144.26]) by mx0a-001b2d01.pphosted.com with ESMTP id 2xxm9daf2y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 03 Feb 2020 21:27:06 -0500 Received: from pps.filterd (ppma04wdc.us.ibm.com [127.0.0.1]) by ppma04wdc.us.ibm.com (8.16.0.27/8.16.0.27) with SMTP id 0142KpMM020906; Tue, 4 Feb 2020 02:27:05 GMT Received: from b03cxnp08027.gho.boulder.ibm.com (b03cxnp08027.gho.boulder.ibm.com [9.17.130.19]) by ppma04wdc.us.ibm.com with ESMTP id 2xw0y6dsm5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 04 Feb 2020 02:27:05 +0000 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp08027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0142R4w956426774 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 4 Feb 2020 02:27:04 GMT Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2AB3FC6057; Tue, 4 Feb 2020 02:27:04 +0000 (GMT) Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 055BBC605B; Tue, 4 Feb 2020 02:27:04 +0000 (GMT) Received: from localhost (unknown [9.40.194.84]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 4 Feb 2020 02:27:03 +0000 (GMT) From: Bill Schmidt To: gcc-patches@gcc.gnu.org Cc: segher@kernel.crashing.org Subject: [PATCH 04/14] Support functions to parse whitespace, lines, identifiers, integers. Date: Mon, 3 Feb 2020 20:26:05 -0600 Message-Id: <5c6c10ada96553341a5eae5d68f84cdf003f994f.1580782131.git.wschmidt@linux.ibm.com> In-Reply-To: References: In-Reply-To: References: 2020-02-03 Bill Schmidt * config/rs6000/rs6000-genbif.c (MININT): New defined constant. (exit_codes): New enum. (consume_whitespace): New function. (advance_line): New function. (safe_inc_pos): New function. (match_identifier): New function. (match_integer): New function. --- gcc/config/rs6000/rs6000-genbif.c | 99 +++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/gcc/config/rs6000/rs6000-genbif.c b/gcc/config/rs6000/rs6000-genbif.c index 3fb13cb11d6..197059cc2d2 100644 --- a/gcc/config/rs6000/rs6000-genbif.c +++ b/gcc/config/rs6000/rs6000-genbif.c @@ -123,6 +123,10 @@ along with GCC; see the file COPYING3. If not see #include #include +/* Used as a sentinel for range constraints on integer fields. No field can + be 32 bits wide, so this is a safe sentinel value. */ +#define MININT INT32_MIN + /* Input and output file descriptors and pathnames. */ static FILE *bif_file; static FILE *ovld_file; @@ -145,6 +149,11 @@ static char linebuf[LINELEN]; static int line; static int pos; +/* Exit codes for the shell. */ +enum exit_codes { + EC_INTERR +}; + /* Pointer to a diagnostic function. */ void (*diag) (const char *, ...) __attribute__ ((format (printf, 1, 2))) = NULL; @@ -169,3 +178,93 @@ ovld_diag (const char * fmt, ...) vfprintf (stderr, fmt, args); va_end (args); } + +/* Pass over unprintable characters and whitespace (other than a newline, + which terminates the scan). */ +static void +consume_whitespace () +{ + while (pos < LINELEN && isspace(linebuf[pos]) && linebuf[pos] != '\n') + pos++; + return; +} + +/* Get the next nonblank line, returning 0 on EOF, 1 otherwise. */ +static int +advance_line (FILE *file) +{ + while (1) + { + /* Read ahead one line and check for EOF. */ + if (!fgets (linebuf, sizeof(linebuf), file)) + return 0; + line++; + pos = 0; + consume_whitespace (); + if (linebuf[pos] != '\n') + return 1; + } +} + +static inline void +safe_inc_pos () +{ + if (pos++ >= LINELEN) + { + (*diag) ("line length overrun.\n"); + exit (EC_INTERR); + } +} + +/* Match an identifier, returning NULL on failure, else a pointer to a + buffer containing the identifier. */ +static char * +match_identifier () +{ + int lastpos = pos - 1; + while (isalnum (linebuf[lastpos + 1]) || linebuf[lastpos + 1] == '_') + if (++lastpos >= LINELEN - 1) + { + (*diag) ("line length overrun.\n"); + exit (EC_INTERR); + } + + if (lastpos < pos) + return 0; + + char *buf = (char *) malloc (lastpos - pos + 2); + memcpy (buf, &linebuf[pos], lastpos - pos + 1); + buf[lastpos - pos + 1] = '\0'; + + pos = lastpos + 1; + return buf; +} + +/* Match an integer and return its value, or MININT on failure. */ +static int +match_integer () +{ + int startpos = pos; + if (linebuf[pos] == '-') + safe_inc_pos (); + + int lastpos = pos - 1; + while (isdigit (linebuf[lastpos + 1])) + if (++lastpos >= LINELEN - 1) + { + (*diag) ("line length overrun in match_integer.\n"); + exit (EC_INTERR); + } + + if (lastpos < pos) + return MININT; + + pos = lastpos + 1; + char *buf = (char *) malloc (lastpos - startpos + 2); + memcpy (buf, &linebuf[startpos], lastpos - startpos + 1); + buf[lastpos - startpos + 1] = '\0'; + + int x; + sscanf (buf, "%d", &x); + return x; +}