https://gcc.gnu.org/g:4d5fd1176d1a5d387dbc53f00324f86a1afa2ecb

commit r14-11545-g4d5fd1176d1a5d387dbc53f00324f86a1afa2ecb
Author: Yang Yujie <yangyu...@loongson.cn>
Date:   Mon Apr 7 10:31:46 2025 +0800

    LoongArch: Fix awk / sed usage for compatibility
    
    Tested with nawk, mawk, and gawk.
    
    gcc/ChangeLog:
    
            * config/loongarch/genopts/gen-evolution.awk: remove
            usage of "asort".
            * config/loongarch/genopts/genstr.sh: replace sed with awk.
    
    (cherry picked from commit 6ed8c17c2bce631ae370d93164ceb6c1b5adf925)

Diff:
---
 gcc/config/loongarch/genopts/gen-evolution.awk | 12 +++++-
 gcc/config/loongarch/genopts/genstr.sh         | 57 ++++++++++++++------------
 2 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/gcc/config/loongarch/genopts/gen-evolution.awk 
b/gcc/config/loongarch/genopts/gen-evolution.awk
index a2ecd9a8bcf5..97f7b0a33c29 100644
--- a/gcc/config/loongarch/genopts/gen-evolution.awk
+++ b/gcc/config/loongarch/genopts/gen-evolution.awk
@@ -101,10 +101,18 @@ function gen_cpucfg_useful_idx()
         idx_list[j++] = i+0
     delete idx_bucket
 
-    asort (idx_list)
+    for (i = 1; i < j; i++) {
+        t = i
+        for (k = i + 1; k < j; k++)
+            t = idx_list[k] < idx_list[t] ? k : t
+
+        k = idx_list[t]
+        idx_list[t] = idx_list[i]
+        idx_list[i] = k
+    }
 
     print "static constexpr int cpucfg_useful_idx[] = {"
-    for (i in idx_list)
+    for (i = 1; i < j; i++)
         printf("  %d,\n", idx_list[i])
     print "};"
 
diff --git a/gcc/config/loongarch/genopts/genstr.sh 
b/gcc/config/loongarch/genopts/genstr.sh
index 5b3bf7fdf0c0..501546d19bc9 100755
--- a/gcc/config/loongarch/genopts/genstr.sh
+++ b/gcc/config/loongarch/genopts/genstr.sh
@@ -51,18 +51,18 @@ along with GCC; see the file COPYING3.  If not see
 #define LOONGARCH_STR_H
 EOF
 
-    sed -e '/^$/n' -e 's@#.*$@@' -e '/^$/d' \
-       -e 's@^\([^ \t]\+\)[ \t]*\([^ \t]*\)@#define \1 "\2"@' \
-       loongarch-strings
+    awk '/^#.*$/ { next } /^$/ { print; next }
+         { printf ("#define %s \"%s\"\n", $1, $2) }' \
+         loongarch-strings
 
     echo
 
-       # Generate the strings from isa-evolution.in.
-       awk '{
-         a=$3
-         gsub(/-/, "_", a)
-         print("#define OPTSTR_"toupper(a)"\t\""$3"\"")
-       }' isa-evolution.in
+    # Generate the strings from isa-evolution.in.
+    awk '{
+      a=$3
+      gsub(/-/, "_", a)
+      print("#define OPTSTR_"toupper(a)"\t\""$3"\"")
+    }' isa-evolution.in
 
     echo
     echo "#endif /* LOONGARCH_STR_H */"
@@ -73,18 +73,8 @@ EOF
 # according to the key-value pairs defined in loongarch-strings.
 
 gen_options() {
-
-    sed -e '/^$/n' -e 's@#.*$@@' -e '/^$/d' \
-       -e 's@^\([^ \t]\+\)[ \t]*\([^ \t]*\)@\1="\2"@' \
-       loongarch-strings | { \
-
-       # read the definitions
-       while read -r line; do
-           eval "$line"
-       done
-
-       # print a header
-       cat << EOF
+    # print a header
+    cat << EOF
 ; Generated by "genstr" from the template "loongarch.opt.in"
 ; and definitions from "loongarch-strings" and "isa-evolution.in".
 ;
@@ -95,12 +85,25 @@ gen_options() {
 ;
 EOF
 
-       # make the substitutions
-       sed -e 's@"@\\"@g' -e 's/@@\([^@]\+\)@@/${\1}/g' loongarch.opt.in | \
-           while read -r line; do
-               eval "echo \"$line\""
-           done
-    }
+    # Generate loongarch.opt.
+    awk 'BEGIN {
+            delete strtab
+            while (getline < "loongarch-strings" > 0) {
+                if ($0 ~ /^#.*$/ || $0 ~ /^$/) continue
+                strtab[$1] = $2
+            }
+        }
+        {
+            n = split($0, tmp, "@@")
+            for (i = 2; i <= n; i += 2)
+                tmp[i] = strtab[tmp[i]]
+
+            for (i = 1; i <= n; i++)
+                printf("%s", tmp[i])
+            printf ("\n")
+
+        }' loongarch.opt.in
+
 
     # Generate the strings from isa-evolution.in.
     awk '{

Reply via email to