changeset 440:bbcb7f4bafe5

Pass structures through ?:, test for function pointers too (grischka-2005-09-25 case_1) This patch was originally posted in grischka's 2005-09-25 email as required fix case_1 to compile gcc 2.95. This adds tests to ensure that both structures and function pointers can pass through the conditional operator ?:. The tests were tweaked by David A. Wheeler to merge the test cases into the standard tcc test suite. This patch also modifies tcc so that structures can be passed through conditional operators. Note that this patch does NOT modify tcc to permit function pointers to pass through ?:, because although such a patch WAS in grischka's patch, an equivalent change was already in tcc by the time this version of the patch was created.
author Rob Landley <rob@landley.net>
date Thu, 03 May 2007 16:01:35 -0400
parents 025b81f7e70b
children 6ed28bf1ff35
files tcc.c tests/tcctest.c
diffstat 2 files changed, 36 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/tcc.c	Thu May 03 15:29:49 2007 -0400
+++ b/tcc.c	Thu May 03 16:01:35 2007 -0400
@@ -7043,6 +7043,8 @@
                 
             /* now we convert second operand */
             gen_cast(&type);
+            if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
+                gaddrof();
             rc = RC_INT;
             if (is_float(type.t)) {
                 rc = RC_FLOAT;
@@ -7060,6 +7062,8 @@
             /* put again first value and cast it */
             *vtop = sv;
             gen_cast(&type);
+            if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
+                gaddrof();
             r1 = gv(rc);
             move_reg(r2, r1);
             vtop->r = r2;
--- a/tests/tcctest.c	Thu May 03 15:29:49 2007 -0400
+++ b/tests/tcctest.c	Thu May 03 16:01:35 2007 -0400
@@ -285,6 +285,16 @@
 
 }
 
+int some_fn(int x)
+{
+    return x;
+}
+
+int other_fn(int x)
+{
+    return x*2;
+}
+
 int op(a,b)
 {
     return a / b;
@@ -929,6 +939,28 @@
         }
     }
 
+    /* Test passing structs & function pointers though conditional
+     * operator ? :.  This is bug grischka-20050929 case_1 */
+    {
+        struct test1 { int a, b, c; };
+        struct test1 t0 = {10,20,30};
+        struct test1 t1 = {11,21,31};
+        struct test1 tx = {0,0,0};
+        int (*pfn)(int);
+        int f = 0;
+
+        tx = f==0 ? t0 : t1;
+        printf("case_1.1: 10,20,30 -> %d,%d,%d\n", tx.a, tx.b, tx.c);
+
+        /* This tests to see that function pointers correctly pass through
+           the conditional operator ?:.  This tests for
+           grischka-20050929 case_1.2.  Note that this was already FIXED
+           in rl-1.0.0, but we want to TEST for it too. */
+        pfn = f ? some_fn : other_fn;
+        printf("case_1.2: other -> %s\n", 1==pfn(1)?"some":"other");
+    }
+
+
     /* test ? : GCC extension */
     {
         static int v1 = 34 ? : -1; /* constant case */