From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id 187C53A19807 for ; Fri, 13 Nov 2020 07:29:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 187C53A19807 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-x535.google.com with SMTP id f18so6393398pgi.8 for ; Thu, 12 Nov 2020 23:29:17 -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:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=a7gFhbIN0f91wjpfvtp95P0m13pVCCTFocx0Vg1eL9M=; b=ngPcIFtVQmBUEH9CihROK4Qxs/VMHDWnIy8d12aASga4oVuUBHzIy8gPMtUerZQ7IH W78r++LXrisbfHIikY4oOKXYE80Db5FqsdlF4KKjntmBkWswa30aRA723GRV6r+2B3Tv PZB2dk82hd4T+fClg1tlQUOut3bnKflhtqBY4B8dT3FIeJCip2MQp2AJcchRZ6TEqhH1 MwU8x7K4yiHy9OiaDr9bQhdUojPHHzW9MvBMnVxzHoBWvtVr7UmlYhSYYfqOaXlPBk7n rZywa6mT/K6kHt5RVKJcVO5R88P5ZHTQhCuav/zJyW2SqSRsjh31VBV49WA2tnjkLeEO uuYA== 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:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=a7gFhbIN0f91wjpfvtp95P0m13pVCCTFocx0Vg1eL9M=; b=l3UXlkkFnPl6pUkn8t2N7VKee3tOJruPdlX7p5w+L8mAh60ZOvwNAMEsAPyRGXjrWF FOoH1wM0qy/llUO91Ui7QonkN2Dn54bYI6aBQllS4iuG44FVanPWmqsj3/AfgTX0qfgF fPRJXKt+5gMMOpyqX5lxTuuiwnMN81QZJaSo2TsrVYk8Z44sN+UI4zqOI9KS03vMNfLL GNSWJCniXi/mJGH8KJw9Wji9ow9yNPfQEpeqA1sjfl/Ig9xtICTnSENYtaW2Q7f5CxTl nVp7xoyL0tPOq+hbNMnWAt8iiQTvpMPbheJZa6iXgCVpHmFYXE1g2bbpNVm/OVioq4DE 0C6w== X-Gm-Message-State: AOAM530t7f1Av0T9SB/Tm9nLRGn5ftMMZ5b9K51SsrVzpX6iVJX6zbbE TlRr14KO/C6CivmkbyZ6W/YIY9gBv2ZyXA== X-Google-Smtp-Source: ABdhPJxECTZQblnySKDKJl3ei2dKTQ2Q683TiUqQa+LCJoNAgX6cmWml3Uw4qPZbtRtb5ElQ9wbn3Q== X-Received: by 2002:aa7:9acd:0:b029:18b:bda:9a0d with SMTP id x13-20020aa79acd0000b029018b0bda9a0dmr941450pfp.10.1605252555780; Thu, 12 Nov 2020 23:29:15 -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 17sm8790712pfu.160.2020.11.12.23.29.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Nov 2020 23:29:15 -0800 (PST) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com Cc: Kito Cheng Subject: [PATCH 1/3] RISC-V: Handle implied extension in canonical ordering. Date: Fri, 13 Nov 2020 15:29:08 +0800 Message-Id: <20201113072910.42353-2-kito.cheng@sifive.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201113072910.42353-1-kito.cheng@sifive.com> References: <20201113072910.42353-1-kito.cheng@sifive.com> Reply-To: Kito Cheng MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: , X-List-Received-Date: Fri, 13 Nov 2020 07:29:18 -0000 - ISA spec has specify the order between multi-letter extensions, implied extension also need to follow store in canonical ordering, so most easy way is we keep that in-order during insertion. gcc/ChangeLog: * common/config/riscv/riscv-common.c (single_letter_subset_rank): New. (multi_letter_subset_rank): Ditto. (subset_cmp): Ditto. (riscv_subset_list::add): Insert subext in canonical ordering. (riscv_subset_list::parse_std_ext): Move handle_implied_ext to ... (riscv_subset_list::parse): ... here. --- gcc/common/config/riscv/riscv-common.c | 177 ++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 5 deletions(-) diff --git a/gcc/common/config/riscv/riscv-common.c b/gcc/common/config/riscv/riscv-common.c index 9a576eb689b..f5f7be3cfff 100644 --- a/gcc/common/config/riscv/riscv-common.c +++ b/gcc/common/config/riscv/riscv-common.c @@ -145,6 +145,129 @@ riscv_subset_list::~riscv_subset_list () } } +/* Get the rank for single-letter subsets, lower value meaning higher + priority. */ + +static int +single_letter_subset_rank (char ext) +{ + int rank; + + switch (ext) + { + case 'i': + return 0; + case 'e': + return 1; + default: + break; + } + + const char *all_ext = riscv_supported_std_ext (); + const char *ext_pos = strchr (all_ext, ext); + if (ext_pos == NULL) + /* If got an unknown extension letter, then give it an alphabetical + order, but after all known standard extension. */ + rank = strlen (all_ext) + ext - 'a'; + else + rank = (int)(ext_pos - all_ext) + 2 /* e and i has higher rank. */; + + return rank; +} + +/* Get the rank for multi-letter subsets, lower value meaning higher + priority. */ + +static int +multi_letter_subset_rank (const std::string &subset) +{ + gcc_assert (subset.length () >= 2); + int high_order = -1; + int low_order = 0; + /* The order between multi-char extensions: s -> h -> z -> x. */ + char multiletter_class = subset[0]; + switch (multiletter_class) + { + case 's': + high_order = 0; + break; + case 'h': + high_order = 1; + break; + case 'z': + gcc_assert (subset.length () > 2); + high_order = 2; + break; + case 'x': + high_order = 3; + break; + default: + gcc_unreachable (); + return -1; + } + + if (multiletter_class == 'z') + /* Order for z extension on spec: If multiple "Z" extensions are named, they + should be ordered first by category, then alphabetically within a + category - for example, "Zicsr_Zifencei_Zam". */ + low_order = single_letter_subset_rank (subset[1]); + else + low_order = 0; + + return (high_order << 8) + low_order; +} + +/* subset compare + + Returns an integral value indicating the relationship between the subsets: + Return value indicates + -1 B has higher order than A. + 0 A and B are same subset. + 1 A has higher order than B. + +*/ + +static int +subset_cmp (const std::string &a, const std::string &b) +{ + if (a == b) + return 0; + + size_t a_len = a.length (); + size_t b_len = b.length (); + + /* Single-letter extension always get higher order than + multi-letter extension. */ + if (a_len == 1 && b_len != 1) + return 1; + + if (a_len != 1 && b_len == 1) + return -1; + + if (a_len == 1 && b_len == 1) + { + int rank_a = single_letter_subset_rank (a[0]); + int rank_b = single_letter_subset_rank (b[0]); + + if (rank_a < rank_b) + return 1; + else + return -1; + } + else + { + int rank_a = multi_letter_subset_rank(a); + int rank_b = multi_letter_subset_rank(b); + + /* Using alphabetical/lexicographical order if they have same rank. */ + if (rank_a == rank_b) + /* The return value of strcmp has opposite meaning. */ + return -strcmp (a.c_str (), b.c_str ()); + else + return (rank_a < rank_b) ? 1 : -1; + } +} + /* Add new subset to list. */ void @@ -152,6 +275,7 @@ riscv_subset_list::add (const char *subset, int major_version, int minor_version, bool explicit_version_p) { riscv_subset_t *s = new riscv_subset_t (); + riscv_subset_t *itr; if (m_head == NULL) m_head = s; @@ -162,9 +286,45 @@ riscv_subset_list::add (const char *subset, int major_version, s->explicit_version_p = explicit_version_p; s->next = NULL; - if (m_tail != NULL) - m_tail->next = s; + if (m_tail == NULL) + { + m_tail = s; + return; + } + + /* e, i or g should be first subext, never come here. */ + gcc_assert (subset[0] != 'e' + && subset[0] != 'i' + && subset[0] != 'g'); + + if (m_tail == m_head) + { + gcc_assert (m_head->next == NULL); + m_head->next = s; + m_tail = s; + return; + } + + gcc_assert (m_head->next != NULL); + + /* Subset list must in canonical order, but implied subset won't + add in canonical order. */ + for (itr = m_head; itr->next != NULL; itr = itr->next) + { + riscv_subset_t *next = itr->next; + int cmp = subset_cmp (s->name, next->name); + gcc_assert (cmp != 0); + + if (cmp > 0) + { + s->next = next; + itr->next = s; + return; + } + } + /* Insert at tail of the list. */ + itr->next = s; m_tail = s; } @@ -441,9 +601,6 @@ riscv_subset_list::parse_std_ext (const char *p) subset[0] = std_ext; - handle_implied_ext (subset, major_version, - minor_version, explicit_version_p); - add (subset, major_version, minor_version, explicit_version_p); } return p; @@ -553,6 +710,7 @@ riscv_subset_list * riscv_subset_list::parse (const char *arch, location_t loc) { riscv_subset_list *subset_list = new riscv_subset_list (arch, loc); + riscv_subset_t *itr; const char *p = arch; if (strncmp (p, "rv32", 4) == 0) { @@ -608,6 +766,15 @@ riscv_subset_list::parse (const char *arch, location_t loc) goto fail; } + for (itr = subset_list->m_head; itr != NULL; itr = itr->next) + { + subset_list->handle_implied_ext ( + itr->name.c_str (), + itr->major_version, + itr->minor_version, + itr->explicit_version_p); + } + return subset_list; fail: -- 2.29.2