From patchwork Tue Dec 1 08:13:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kito Cheng X-Patchwork-Id: 1408656 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=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=fvpTbe97; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4ClZc86sZXz9s1l for ; Tue, 1 Dec 2020 19:13:26 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8F9F3854810; Tue, 1 Dec 2020 08:13:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by sourceware.org (Postfix) with ESMTPS id C059A3857000 for ; Tue, 1 Dec 2020 08:13:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C059A3857000 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kito.cheng@sifive.com Received: by mail-pg1-x52c.google.com with SMTP id g18so764440pgk.1 for ; Tue, 01 Dec 2020 00:13:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=H31joAjlShkJES7jT+x8v+NzE53KC0S4MgWz4k0h/GM=; b=fvpTbe97rZmDRKiwmAZsgSKfbjK/+HjKm7BKgEnH8e1R9MVZEX+wepfBITfpDc+MSM DXjWS2F0SMbX5FAMilYDKS1B+/Hu1xJ6jiry6JBu5mTJacRrV63TdjlatZi3rg8Bb6vy QiIX2Sm9u+MuJx6Y84XKwPD2jCabr7R+melH2Y4Rc3+skQsiA56XUIX6TZlEFKf7wfIi Y+qxf0T/aNE8O+TfzP6PnqCLDkF0UVIiCTbh10SHGs2itviDJWZDuQF82eJg63mcp32K nYvApzq35GoRJWe1YePhROZGDHz83Qp03vtVEYR2stfhJDdvGmCwVwNnMOIdEkyxonHp ilHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=H31joAjlShkJES7jT+x8v+NzE53KC0S4MgWz4k0h/GM=; b=ekqoAksgk+FjLszphFapX9fXX5jpY9G/C05od7pq/sCnW5RrLXLFOwSxJMJwWnUdur twfyeP//IbDpy08+8N9P2aXlNik+ecsqs7kOl53/lyn3OFkRItiI/evArPeZ6TYI6lYu B/slcevGQZiqpRZBSliKTX8YoZw4kQGbUr0/vKf9ZAuFnHLAmfh5gJBhZQB6Oktwv5UR k5C+9b5jlOhpQkIkGbcaJZ0xwnvGAy3+D4MBrLGVDGijG+ptvW5w/Zf71sLREYMZO8ke PRaBGp5D0PFrR1c6OFyZOPaUR8dKqBytlDd+46w0P3wWYcaEEWx2EsRhB48ENBdR8NYA +XCQ== X-Gm-Message-State: AOAM532ce7Ecn1GF695OjUVn6CSkQ03DqZVI4ojifIEAieN5UePViTAM ylimXQUgjTaqwG4/ZXBoUAFYsDhUdTNs/R3R X-Google-Smtp-Source: ABdhPJwIwmvoJw8rL8whORyuOH3dG5hrJ1JfMTXY6yZ4NRIaVEt1XTTNI7Sa87V6l8AfzY5BUa7t9g== X-Received: by 2002:a62:1d4b:0:b029:18a:df98:515f with SMTP id d72-20020a621d4b0000b029018adf98515fmr1541846pfd.30.1606810398359; Tue, 01 Dec 2020 00:13:18 -0800 (PST) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id r7sm1420556pjd.8.2020.12.01.00.13.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 00:13:17 -0800 (PST) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, jimw@sifive.com Subject: [PATCH] RISC-V: Canonicalize --with-arch Date: Tue, 1 Dec 2020 16:13:13 +0800 Message-Id: <20201201081313.68385-1-kito.cheng@sifive.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kito Cheng Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" - We would like to canonicalize the arch string for --with-arch for easier handling multilib, so split canonicalization part to a stand along script to shared the logic. gcc/ChangeLog: * config/riscv/multilib-generator (arch_canonicalize): Move code to arch-canonicalize, and call that script to canonicalize arch string. (canonical_order): Move code to arch-canonicalize. (LONG_EXT_PREFIXES): Ditto. (IMPLIED_EXT): Ditto. * config/riscv/arch-canonicalize: New. * config.gcc (riscv*-*-*): Canonicalize --with-arch. --- gcc/config.gcc | 1 + gcc/config/riscv/arch-canonicalize | 102 ++++++++++++++++++++++++++++ gcc/config/riscv/multilib-generator | 83 +++------------------- 3 files changed, 111 insertions(+), 75 deletions(-) create mode 100755 gcc/config/riscv/arch-canonicalize diff --git a/gcc/config.gcc b/gcc/config.gcc index 4808b698f3a..c348596b1ac 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -4615,6 +4615,7 @@ case "${target}" in exit 1 ;; esac + with_arch=`${srcdir}/config/riscv/arch-canonicalize ${with_arch}` tm_defines="${tm_defines} TARGET_RISCV_DEFAULT_ARCH=${with_arch}" # Make sure --with-abi is valid. If it was not specified, diff --git a/gcc/config/riscv/arch-canonicalize b/gcc/config/riscv/arch-canonicalize new file mode 100755 index 00000000000..2b4289e320d --- /dev/null +++ b/gcc/config/riscv/arch-canonicalize @@ -0,0 +1,102 @@ +#!/usr/bin/env python + +# Tool for canonical RISC-V architecture string. +# Copyright (C) 2011-2020 Free Software Foundation, Inc. +# Contributed by Andrew Waterman (andrew@sifive.com). +# +# This file is part of GCC. +# +# GCC 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 3, or (at your option) +# any later version. +# +# GCC 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 GCC; see the file COPYING3. If not see +# . + + +from __future__ import print_function +import sys +import collections +import itertools +from functools import reduce + + +CANONICAL_ORDER = "mafdgqlcbjtpvn" +LONG_EXT_PREFIXES = ['z', 's', 'h', 'x'] + +# +# IMPLIED_EXT(ext) -> implied extension list. +# +IMPLIED_EXT = { + "d" : ["f"], +} + +def arch_canonicalize(arch): + # TODO: Support extension version. + new_arch = "" + if arch[:5] in ['rv32e', 'rv32i', 'rv32g', 'rv64i', 'rv64g']: + # TODO: We should expand g to imad_zifencei once we support newer spec. + new_arch = arch[:5].replace("g", "imafd") + else: + raise Exception("Unexpected arch: `%s`" % arch[:5]) + + # Find any Z, S, H or X + long_ext_prefixes_idx = map(lambda x: arch.find(x), LONG_EXT_PREFIXES) + + # Filter out any non-existent index. + long_ext_prefixes_idx = list(filter(lambda x: x != -1, long_ext_prefixes_idx)) + if long_ext_prefixes_idx: + first_long_ext_idx = min(long_ext_prefixes_idx) + long_exts = arch[first_long_ext_idx:].split("_") + std_exts = list(arch[5:first_long_ext_idx]) + else: + long_exts = [] + std_exts = list(arch[5:]) + + # + # Handle implied extensions. + # + for ext in std_exts + long_exts: + if ext in IMPLIED_EXT: + implied_exts = IMPLIED_EXT[ext] + for implied_ext in implied_exts: + if implied_ext not in std_exts + long_exts: + long_exts.append(implied_ext) + + # Single letter extension might appear in the long_exts list, + # becasue we just append extensions list to the arch string. + std_exts += list(filter(lambda x:len(x) == 1, long_exts)) + + # Multi-letter extension must be in lexicographic order. + long_exts = list(sorted(filter(lambda x:len(x) != 1, long_exts))) + + # Put extensions in canonical order. + for ext in CANONICAL_ORDER: + if ext in std_exts: + new_arch += ext + + # Check every extension is processed. + for ext in std_exts: + if ext == '_': + continue + if ext not in CANONICAL_ORDER: + raise Exception("Unsupported extension `%s`" % ext) + + # Concat rest of the multi-char extensions. + if long_exts: + new_arch += "_" + "_".join(long_exts) + return new_arch + +if len(sys.argv) < 2: + print ("Usage: %s [*]" % sys.argv) + sys.exit(1) + +for arg in sys.argv[1:]: + print (arch_canonicalize(arg)) diff --git a/gcc/config/riscv/multilib-generator b/gcc/config/riscv/multilib-generator index 0d9ebcb321f..53c51dfa53f 100755 --- a/gcc/config/riscv/multilib-generator +++ b/gcc/config/riscv/multilib-generator @@ -35,9 +35,11 @@ from __future__ import print_function import sys +import os import collections import itertools from functools import reduce +import subprocess # # TODO: Add test for this script. @@ -48,82 +50,13 @@ abis = collections.OrderedDict() required = [] reuse = [] -canonical_order = "mafdgqlcbjtpvn" -LONG_EXT_PREFIXES = ['z', 's', 'h', 'x'] - -# -# IMPLIED_EXT(ext) -> implied extension list. -# -IMPLIED_EXT = { - "d" : ["f"], -} - def arch_canonicalize(arch): - # TODO: Support extension version. - new_arch = "" - if arch[:5] in ['rv32e', 'rv32i', 'rv32g', 'rv64i', 'rv64g']: - # TODO: We should expand g to imad_zifencei once we support newer spec. - new_arch = arch[:5].replace("g", "imafd") - else: - raise Exception("Unexpected arch: `%s`" % arch[:5]) - - # Find any Z, S, H or X - long_ext_prefixes_idx = map(lambda x: arch.find(x), LONG_EXT_PREFIXES) - - # Filter out any non-existent index. - long_ext_prefixes_idx = list(filter(lambda x: x != -1, long_ext_prefixes_idx)) - if long_ext_prefixes_idx: - first_long_ext_idx = min(long_ext_prefixes_idx) - long_exts = arch[first_long_ext_idx:].split("_") - std_exts = list(arch[5:first_long_ext_idx]) - else: - long_exts = [] - std_exts = list(arch[5:]) - - # - # Handle implied extensions. - # - for ext in std_exts + long_exts: - if ext in IMPLIED_EXT: - implied_exts = IMPLIED_EXT[ext] - for implied_ext in implied_exts: - if implied_ext not in std_exts + long_exts: - long_exts.append(implied_ext) - - # Single letter extension might appear in the long_exts list, - # becasue we just append extensions list to the arch string. - std_exts += list(filter(lambda x:len(x) == 1, long_exts)) - - # Multi-letter extension must be in lexicographic order. - long_exts = list(sorted(filter(lambda x:len(x) != 1, long_exts))) - - # Put extensions in canonical order. - for ext in canonical_order: - if ext in std_exts: - new_arch += ext - - # Check every extension is processed. - for ext in std_exts: - if ext == '_': - continue - if ext not in canonical_order: - raise Exception("Unsupported extension `%s`" % ext) - - # Concat rest of the multi-char extensions. - if long_exts: - new_arch += "_" + "_".join(long_exts) - return new_arch - -# -# add underline for each multi-char extensions. -# e.g. ["a", "zfh"] -> ["a", "_zfh"] -# -def add_underline_prefix(ext): - for long_ext_prefix in LONG_EXT_PREFIXES: - if ext.startswith(long_ext_prefix): - return "_" + ext - - return ext + this_file = os.path.abspath(os.path.join( __file__)) + arch_can_script = \ + os.path.join(os.path.dirname(this_file), "arch-canonicalize") + proc = subprocess.Popen([arch_can_script, arch], stdout=subprocess.PIPE) + out, err = proc.communicate() + return out.strip() # # Handle expansion operation.