runtime: extend internal atomics to comply with sync/atomic

This is the gofrontend version of https://golang.org/cl/289152.

Change-Id: I28e231e7a72e3d2d05af6b03ae4aed4d7e5f431e
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/339690
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
diff --git a/go/expressions.cc b/go/expressions.cc
index 3e433d6..33177a7 100644
--- a/go/expressions.cc
+++ b/go/expressions.cc
@@ -11590,12 +11590,10 @@
       // sync/atomic functions and runtime/internal/atomic functions
       // are very similar. In order not to duplicate code, we just
       // redirect to the latter and let the code below to handle them.
-      // In case there is no equivalent functions (slight variance
-      // in types), we just make an artificial name (begin with '$').
       // Note: no StorePointer, SwapPointer, and CompareAndSwapPointer,
       // as they need write barriers.
       if (name == "LoadInt32")
-        name = "$Loadint32";
+        name = "Loadint32";
       else if (name == "LoadInt64")
         name = "Loadint64";
       else if (name == "LoadUint32")
@@ -11607,9 +11605,9 @@
       else if (name == "LoadPointer")
         name = "Loadp";
       else if (name == "StoreInt32")
-        name = "$Storeint32";
+        name = "Storeint32";
       else if (name == "StoreInt64")
-        name = "$Storeint64";
+        name = "Storeint64";
       else if (name == "StoreUint32")
         name = "Store";
       else if (name == "StoreUint64")
@@ -11617,7 +11615,7 @@
       else if (name == "StoreUintptr")
         name = "Storeuintptr";
       else if (name == "AddInt32")
-        name = "$Xaddint32";
+        name = "Xaddint32";
       else if (name == "AddInt64")
         name = "Xaddint64";
       else if (name == "AddUint32")
@@ -11627,9 +11625,9 @@
       else if (name == "AddUintptr")
         name = "Xadduintptr";
       else if (name == "SwapInt32")
-        name = "$Xchgint32";
+        name = "Xchgint32";
       else if (name == "SwapInt64")
-        name = "$Xchgint64";
+        name = "Xchgint64";
       else if (name == "SwapUint32")
         name = "Xchg";
       else if (name == "SwapUint64")
@@ -11637,9 +11635,9 @@
       else if (name == "SwapUintptr")
         name = "Xchguintptr";
       else if (name == "CompareAndSwapInt32")
-        name = "$Casint32";
+        name = "Casint32";
       else if (name == "CompareAndSwapInt64")
-        name = "$Casint64";
+        name = "Casint64";
       else if (name == "CompareAndSwapUint32")
         name = "Cas";
       else if (name == "CompareAndSwapUint64")
@@ -11875,7 +11873,7 @@
 
       if ((name == "Load" || name == "Load64" || name == "Loadint64" || name == "Loadp"
            || name == "Loaduint" || name == "Loaduintptr" || name == "LoadAcq"
-           || name == "$Loadint32")
+           || name == "Loadint32")
           && this->args_ != NULL && this->args_->size() == 1)
         {
           if (int_size < 8 && (name == "Load64" || name == "Loadint64"))
@@ -11895,7 +11893,7 @@
               code = Runtime::ATOMIC_LOAD_8;
               res_type = uint64_type;
             }
-          else if (name == "$Loadint32")
+          else if (name == "Loadint32")
             {
               code = Runtime::ATOMIC_LOAD_4;
               res_type = int32_type;
@@ -11942,10 +11940,10 @@
 
       if ((name == "Store" || name == "Store64" || name == "StorepNoWB"
            || name == "Storeuintptr" || name == "StoreRel"
-           || name == "$Storeint32" || name == "$Storeint64")
+           || name == "Storeint32" || name == "Storeint64")
           && this->args_ != NULL && this->args_->size() == 2)
         {
-          if (int_size < 8 && (name == "Store64" || name == "$Storeint64"))
+          if (int_size < 8 && (name == "Store64" || name == "Storeint64"))
             return NULL;
 
           Runtime::Function code;
@@ -11955,9 +11953,9 @@
             code = Runtime::ATOMIC_STORE_4;
           else if (name == "Store64")
             code = Runtime::ATOMIC_STORE_8;
-          else if (name == "$Storeint32")
+          else if (name == "Storeint32")
             code = Runtime::ATOMIC_STORE_4;
-          else if (name == "$Storeint64")
+          else if (name == "Storeint64")
             code = Runtime::ATOMIC_STORE_8;
           else if (name == "Storeuintptr")
             code = (ptr_size == 8 ? Runtime::ATOMIC_STORE_8 : Runtime::ATOMIC_STORE_4);
@@ -11979,7 +11977,7 @@
         }
 
       if ((name == "Xchg" || name == "Xchg64" || name == "Xchguintptr"
-           || name == "$Xchgint32" || name == "$Xchgint64")
+           || name == "Xchgint32" || name == "Xchgint64")
           && this->args_ != NULL && this->args_->size() == 2)
         {
           if (int_size < 8 && (name == "Xchg64" || name == "Xchgint64"))
@@ -11997,12 +11995,12 @@
               code = Runtime::ATOMIC_EXCHANGE_8;
               res_type = uint64_type;
             }
-          else if (name == "$Xchgint32")
+          else if (name == "Xchgint32")
             {
               code = Runtime::ATOMIC_EXCHANGE_4;
               res_type = int32_type;
             }
-          else if (name == "$Xchgint64")
+          else if (name == "Xchgint64")
             {
               code = Runtime::ATOMIC_EXCHANGE_8;
               res_type = int64_type;
@@ -12025,10 +12023,10 @@
 
       if ((name == "Cas" || name == "Cas64" || name == "Casuintptr"
            || name == "Casp1" || name == "CasRel"
-           || name == "$Casint32" || name == "$Casint64")
+           || name == "Casint32" || name == "Casint64")
           && this->args_ != NULL && this->args_->size() == 3)
         {
-          if (int_size < 8 && (name == "Cas64" || name == "$Casint64"))
+          if (int_size < 8 && (name == "Cas64" || name == "Casint64"))
             return NULL;
 
           Runtime::Function code;
@@ -12047,9 +12045,9 @@
             code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
           else if (name == "Cas64")
             code = Runtime::ATOMIC_COMPARE_EXCHANGE_8;
-          else if (name == "$Casint32")
+          else if (name == "Casint32")
             code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
-          else if (name == "$Casint64")
+          else if (name == "Casint64")
             code = Runtime::ATOMIC_COMPARE_EXCHANGE_8;
           else if (name == "Casuintptr")
             code = (ptr_size == 8
@@ -12077,7 +12075,7 @@
         }
 
       if ((name == "Xadd" || name == "Xadd64" || name == "Xaddint64"
-           || name == "Xadduintptr" || name == "$Xaddint32")
+           || name == "Xadduintptr" || name == "Xaddint32")
           && this->args_ != NULL && this->args_->size() == 2)
         {
           if (int_size < 8 && (name == "Xadd64" || name == "Xaddint64"))
@@ -12095,7 +12093,7 @@
               code = Runtime::ATOMIC_ADD_FETCH_8;
               res_type = uint64_type;
             }
-          else if (name == "$Xaddint32")
+          else if (name == "Xaddint32")
             {
               code = Runtime::ATOMIC_ADD_FETCH_4;
               res_type = int32_type;
diff --git a/libgo/go/runtime/internal/atomic/atomic.c b/libgo/go/runtime/internal/atomic/atomic.c
index 569e56e..b5a0940 100644
--- a/libgo/go/runtime/internal/atomic/atomic.c
+++ b/libgo/go/runtime/internal/atomic/atomic.c
@@ -104,6 +104,16 @@
   return __atomic_load_n (ptr, __ATOMIC_SEQ_CST);
 }
 
+int32_t Loadint32 (int32_t *ptr)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Loadint32")
+  __attribute__ ((no_split_stack));
+
+int32_t
+Loadint32 (int32_t *ptr)
+{
+  return __atomic_load_n (ptr, __ATOMIC_SEQ_CST);
+}
+
 int64_t Loadint64 (int64_t *ptr)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Loadint64")
   __attribute__ ((no_split_stack));
@@ -126,6 +136,16 @@
   return __atomic_add_fetch (ptr, (uint32_t) delta, __ATOMIC_SEQ_CST);
 }
 
+int32_t Xaddint32 (int32_t *ptr, int32_t delta)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xaddint32")
+  __attribute__ ((no_split_stack));
+
+int32_t
+Xaddint32 (int32_t *ptr, int32_t delta)
+{
+  return __atomic_add_fetch (ptr, delta, __ATOMIC_SEQ_CST);
+}
+
 uint64_t Xadd64 (uint64_t *ptr, int64_t delta)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xadd64")
   __attribute__ ((no_split_stack));
@@ -170,6 +190,16 @@
   return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
 }
 
+int32_t Xchgint32 (int32_t *ptr, int32_t new)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchgint32")
+  __attribute__ ((no_split_stack));
+
+int32_t
+Xchgint32 (int32_t *ptr, int32_t new)
+{
+  return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
+}
+
 uint64_t Xchg64 (uint64_t *ptr, uint64_t new)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchg64")
   __attribute__ ((no_split_stack));
@@ -182,6 +212,16 @@
   return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
 }
 
+int64_t Xchgint64 (int64_t *ptr, int64_t new)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchgint64")
+  __attribute__ ((no_split_stack));
+
+int64_t
+Xchgint64 (int64_t *ptr, int64_t new)
+{
+  return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
+}
+
 uintptr_t Xchguintptr (uintptr_t *ptr, uintptr_t new)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchguintptr")
   __attribute__ ((no_split_stack));
@@ -264,6 +304,26 @@
   return __atomic_compare_exchange_n (ptr, &old, new, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 }
 
+_Bool Casint32 (int32_t *ptr, int32_t old, int32_t new)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Casint32")
+  __attribute__ ((no_split_stack));
+
+_Bool
+Casint32 (int32_t *ptr, int32_t old, int32_t new)
+{
+  return __atomic_compare_exchange_n (ptr, &old, new, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
+}
+
+_Bool Casint64 (int64_t *ptr, int64_t old, int64_t new)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Casint64")
+  __attribute__ ((no_split_stack));
+
+_Bool
+Casint64 (int64_t *ptr, int64_t old, int64_t new)
+{
+  return __atomic_compare_exchange_n (ptr, &old, new, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
+}
+
 _Bool Casp1 (void **ptr, void *old, void *new)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Casp1")
   __attribute__ ((no_split_stack));
@@ -304,6 +364,16 @@
   __atomic_store_n (ptr, val, __ATOMIC_SEQ_CST);
 }
 
+void Storeint32 (int32_t *ptr, int32_t val)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Storeint32")
+  __attribute__ ((no_split_stack));
+
+void
+Storeint32 (int32_t *ptr, int32_t val)
+{
+  __atomic_store_n (ptr, val, __ATOMIC_SEQ_CST);
+}
+
 void Store64 (uint64_t *ptr, uint64_t val)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Store64")
   __attribute__ ((no_split_stack));
@@ -338,6 +408,16 @@
   __atomic_store_n (ptr, val, __ATOMIC_RELEASE);
 }
 
+void Storeint64 (int64_t *ptr, int64_t val)
+  __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Storeint64")
+  __attribute__ ((no_split_stack));
+
+void
+Storeint64 (int64_t *ptr, int64_t val)
+{
+  __atomic_store_n (ptr, val, __ATOMIC_SEQ_CST);
+}
+
 void StoreReluintptr (uintptr_t *ptr, uintptr_t val)
   __asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.StoreReluintptr")
   __attribute__ ((no_split_stack));
diff --git a/libgo/go/runtime/internal/atomic/stubs.go b/libgo/go/runtime/internal/atomic/stubs.go
index 62e30d1..e7544ba 100644
--- a/libgo/go/runtime/internal/atomic/stubs.go
+++ b/libgo/go/runtime/internal/atomic/stubs.go
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+//go:build !wasm
 // +build !wasm
 
 package atomic
@@ -15,9 +16,21 @@
 func Casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
 
 //go:noescape
+func Casint32(ptr *int32, old, new int32) bool
+
+//go:noescape
+func Casint64(ptr *int64, old, new int64) bool
+
+//go:noescape
 func Casuintptr(ptr *uintptr, old, new uintptr) bool
 
 //go:noescape
+func Storeint32(ptr *int32, new int32)
+
+//go:noescape
+func Storeint64(ptr *int64, new int64)
+
+//go:noescape
 func Storeuintptr(ptr *uintptr, new uintptr)
 
 //go:noescape
@@ -29,7 +42,19 @@
 // TODO(matloob): Should these functions have the go:noescape annotation?
 
 //go:noescape
+func Loadint32(ptr *int32) int32
+
+//go:noescape
 func Loadint64(ptr *int64) int64
 
 //go:noescape
+func Xaddint32(ptr *int32, delta int32) int32
+
+//go:noescape
 func Xaddint64(ptr *int64, delta int64) int64
+
+//go:noescape
+func Xchgint32(ptr *int32, new int32) int32
+
+//go:noescape
+func Xchgint64(ptr *int64, new int64) int64