From a9a6d92cbf3371de82aa81595564c23986b4da89 Mon Sep 17 00:00:00 2001
From: xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>
Date: Sat, 17 Apr 2010 07:41:39 +0000
Subject: [PATCH] __builtin_memcpy, continued.

git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@1320 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
---
 cfrontend/C2Clight.ml           |  6 ++--
 cparser/StructAssign.ml         | 27 ++++++++++-----
 powerpc/PrintAsm.ml             | 16 ++++-----
 test/regression/Results/struct7 |  4 +++
 test/regression/Results/struct8 |  2 ++
 test/regression/struct7.c       | 59 +++++++++++++++++++++++++++++++++
 test/regression/struct8.c       | 23 +++++++++++++
 7 files changed, 117 insertions(+), 20 deletions(-)
 create mode 100644 test/regression/Results/struct7
 create mode 100644 test/regression/Results/struct8
 create mode 100644 test/regression/struct7.c
 create mode 100644 test/regression/struct8.c

diff --git a/cfrontend/C2Clight.ml b/cfrontend/C2Clight.ml
index 614ad7707..6fc9b5cab 100644
--- a/cfrontend/C2Clight.ml
+++ b/cfrontend/C2Clight.ml
@@ -840,16 +840,16 @@ let builtins_generic = {
     "__builtin_volatile_write_float64",
          (TVoid [], [TPtr(TVoid [], []); TFloat(FDouble, [])], false);
     "__builtin_volatile_write_pointer",
-         (TVoid [], [TPtr(TVoid [], []); TPtr(TVoid [], [])], false)
+         (TVoid [], [TPtr(TVoid [], []); TPtr(TVoid [], [])], false);
     (* Block copy *)
     "__builtin_memcpy",
-         (TPtr(TVoid [], []),
+         (TVoid [],
            [TPtr(TVoid [], []); 
             TPtr(TVoid [AConst], []); 
             TInt(Cutil.size_t_ikind, [])],
           false);
     "__builtin_memcpy_words",
-         (TPtr(TVoid [], []),
+         (TVoid [],
            [TPtr(TVoid [], []); 
             TPtr(TVoid [AConst], []); 
             TInt(Cutil.size_t_ikind, [])],
diff --git a/cparser/StructAssign.ml b/cparser/StructAssign.ml
index 3be917e8f..725c136c8 100644
--- a/cparser/StructAssign.ml
+++ b/cparser/StructAssign.ml
@@ -35,17 +35,26 @@ let memcpy_type =
              (Env.fresh_ident "", TInt(size_t_ikind, []))],
        false, [])
 
+let lookup_function env name =
+  match Env.lookup_ident env name with
+  | (id, II_ident(sto, ty)) -> (id, ty)
+  | (id, II_enum _) -> raise (Env.Error(Env.Unbound_identifier name))
+
 let memcpy_ident env =
-  try fst (Env.lookup_ident env "__builtin_memcpy")
+  try lookup_function env "__builtin_memcpy"
   with Env.Error _ ->
-  try fst (Env.lookup_ident env "memcpy")
+  try lookup_function env "memcpy"
   with Env.Error _ ->
   match !memcpy_decl with
-  | Some id -> id
-  | None -> let id = Env.fresh_ident "memcpy" in memcpy_decl := Some id; id
+  | Some id ->
+      (id, memcpy_type)
+  | None ->
+      let id = Env.fresh_ident "memcpy" in
+      memcpy_decl := Some id;
+      (id, memcpy_type)
 
 let memcpy_words_ident env =
-  try fst (Env.lookup_ident env "__builtin_memcpy_words")
+  try lookup_function env "__builtin_memcpy_words"
   with Env.Error _ -> memcpy_ident env
 
 let transf_assign env loc lhs rhs =
@@ -98,11 +107,11 @@ let transf_assign env loc lhs rhs =
       match Cutil.sizeof env lhs.etyp with
       | Some n -> n mod !config.sizeof_ptr = 0
       | None -> false in
-    let ident =
+    let (ident, ty) =
       if by_words
-      then memcpy_word_ident()
-      else memcpy_ident() in
-    let memcpy = {edesc = EVar(ident); etyp = memcpy_type} in
+      then memcpy_words_ident env
+      else memcpy_ident env in
+    let memcpy = {edesc = EVar(ident); etyp = ty} in
     let e_lhs = {edesc = EUnop(Oaddrof, lhs); etyp = TPtr(lhs.etyp, [])} in
     let e_rhs = {edesc = EUnop(Oaddrof, rhs); etyp = TPtr(rhs.etyp, [])} in
     let e_size = {edesc = ESizeof(lhs.etyp); etyp = TInt(size_t_ikind, [])} in
diff --git a/powerpc/PrintAsm.ml b/powerpc/PrintAsm.ml
index 4c898790a..91449913e 100644
--- a/powerpc/PrintAsm.ml
+++ b/powerpc/PrintAsm.ml
@@ -275,25 +275,25 @@ let print_builtin_function oc s =
   | "__builtin_memcpy" ->
       let lbl1 = new_label() in
       let lbl2 = new_label() in
-      fprintf oc "	cmplwi	%a, %a, 0\n" creg CR0 ireg GPR5;
-      fprintf oc "	beq	%a, %a\n" creg CR0 label lbl1;
+      fprintf oc "	cmplwi	%a, %a, 0\n" creg 0 ireg GPR5;
+      fprintf oc "	beq	%a, %a\n" creg 0 label lbl1;
       fprintf oc "	mtctr	%a\n" ireg GPR5;
-      fprintf oc "	addi	%a, %a, -1\n" ireg GPR6 ireg GPR3;
+      fprintf oc "	addi	%a, %a, -1\n" ireg GPR3 ireg GPR3;
       fprintf oc "	addi	%a, %a, -1\n" ireg GPR4 ireg GPR4;
       fprintf oc "%a:	lbzu	%a, 1(%a)\n" label lbl2 ireg GPR0 ireg GPR4;
-      fprintf oc "	stbu	%a, 1(%a)\n" ireg GPR0 ireg GPR6;
+      fprintf oc "	stbu	%a, 1(%a)\n" ireg GPR0 ireg GPR3;
       fprintf oc "	bdnz	%a\n" label lbl2;
       fprintf oc "%a:\n" label lbl1
-  | "__builtin_memcpy_word" ->
+  | "__builtin_memcpy_words" ->
       let lbl1 = new_label() in
       let lbl2 = new_label() in
       fprintf oc "	rlwinm.	%a, %a, 30, 2, 31\n" ireg GPR5 ireg GPR5;
-      fprintf oc "	beq	%a, %a\n" creg CR0 label lbl1;
+      fprintf oc "	beq	%a, %a\n" creg 0 label lbl1;
       fprintf oc "	mtctr	%a\n" ireg GPR5;
-      fprintf oc "	addi	%a, %a, -4\n" ireg GPR6 ireg GPR3;
+      fprintf oc "	addi	%a, %a, -4\n" ireg GPR3 ireg GPR3;
       fprintf oc "	addi	%a, %a, -4\n" ireg GPR4 ireg GPR4;
       fprintf oc "%a:	lwzu	%a, 4(%a)\n" label lbl2 ireg GPR0 ireg GPR4;
-      fprintf oc "	stwu	%a, 4(%a)\n" ireg GPR0 ireg GPR6;
+      fprintf oc "	stwu	%a, 4(%a)\n" ireg GPR0 ireg GPR3;
       fprintf oc "	bdnz	%a\n" label lbl2;
       fprintf oc "%a:\n" label lbl1
   (* Integer arithmetic *)
diff --git a/test/regression/Results/struct7 b/test/regression/Results/struct7
new file mode 100644
index 000000000..ae630bf1a
--- /dev/null
+++ b/test/regression/Results/struct7
@@ -0,0 +1,4 @@
+A2 = { 1234, 3.141590, { 'H', ... , 'o' } }
+B2 = { 1, ..., 5, ..., 0 }
+C2.c = 'z'
+D2.v = { 0, ..., 4, ..., 0 }
diff --git a/test/regression/Results/struct8 b/test/regression/Results/struct8
new file mode 100644
index 000000000..a2151938c
--- /dev/null
+++ b/test/regression/Results/struct8
@@ -0,0 +1,2 @@
+a = { 123, 2.718000, 'a' }
+b = { 125, 5.436000, 'f' }
diff --git a/test/regression/struct7.c b/test/regression/struct7.c
new file mode 100644
index 000000000..136602bf9
--- /dev/null
+++ b/test/regression/struct7.c
@@ -0,0 +1,59 @@
+/* Assignment between structs and unions */
+
+#include <stdio.h>
+
+struct small {
+  int x;
+  double d;
+  char c[5];
+};
+
+struct big {
+  int x[100];
+};
+
+union u1 {
+  char c;
+  short s;
+};
+
+union u2 {
+  struct small u;
+  struct big v;
+};
+
+struct small A = { 1234, 3.14159, { 'H', 'e', 'l', 'l', 'o' }};
+struct big B = { 1, 2, 3, 4, 5 };
+union u1 C;
+union u2 D;
+
+int main()
+{
+  struct small A2;
+  struct big B2;
+  union u1 C2;
+  union u2 D2;
+  int i;
+
+  C.c = 'z';
+  for (i = 0; i < 99; i++) D.v.x[i] = i;
+
+  A2 = A;
+  printf("A2 = { %d, %f, { '%c', ... , '%c' } }\n",
+         A2.x, A2.d, A2.c[0], A2.c[4]);
+
+  B2 = B;
+  printf("B2 = { %d, ..., %d, ..., %d }\n",
+         B2.x[0], B2.x[4], B2.x[99]);
+
+  C2 = C;
+  printf("C2.c = '%c'\n", C2.c);
+
+  D2 = D;
+  printf("D2.v = { %d, ..., %d, ..., %d }\n",
+         D2.v.x[0], D2.v.x[4], D2.v.x[99]);
+
+  return 0;
+}
+
+
diff --git a/test/regression/struct8.c b/test/regression/struct8.c
new file mode 100644
index 000000000..989c3524a
--- /dev/null
+++ b/test/regression/struct8.c
@@ -0,0 +1,23 @@
+/* Passing structs by value */
+
+#include <stdio.h>
+
+struct S { int x; double d; char c; };
+
+struct S f(struct S s, int scale)
+{
+  struct S r;
+  r.x = s.x + scale;
+  r.d = s.d * scale;
+  r.c = 'f';
+  return r;
+}
+
+int main()
+{
+  struct S a = { 123, 2.718, 'a' };
+  struct S b = f(a, 2);
+  printf("a = { %d, %f, '%c' }\n", a.x, a.d, a.c);
+  printf("b = { %d, %f, '%c' }\n", b.x, b.d, b.c);
+  return 0;
+}
-- 
GitLab