Remove use of ppc string instructions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
diff --git a/ChangeLog b/ChangeLog
index d193641..148af6d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2011-11-12 Kyle Moffett <Kyle.D.Moffett@boeing.com>
+ Anthony Green <green@moxielogic.com>
+
+ * src/ppc/sysv.S, src/ppc/ffi.c: Remove use of ppc string
+ instructions (not available on some cores, like the PPC440).
+
2011-11-12 Kimura Wataru <kimuraw@i.nifty.jp>
* m4/ax_enable_builddir: Change from string comparison to numeric
diff --git a/patches/powerpc-sysv-without-string-ops b/patches/powerpc-sysv-without-string-ops
new file mode 100644
index 0000000..a1261b9
--- /dev/null
+++ b/patches/powerpc-sysv-without-string-ops
@@ -0,0 +1,171 @@
+Index: libffi/ChangeLog
+===================================================================
+--- libffi.orig/ChangeLog
++++ libffi/ChangeLog
+@@ -1,3 +1,9 @@
++2011-11-12 Kyle Moffett <Kyle.D.Moffett@boeing.com>
++ Anthony Green <green@moxielogic.com>
++
++ * src/ppc/sysv.S, src/ppc/ffi.c: Remove use of ppc string
++ instructions (not available on some cores, like the PPC440).
++
+ 2011-11-12 Kimura Wataru <kimuraw@i.nifty.jp>
+
+ * m4/ax_enable_builddir: Change from string comparison to numeric
+Index: libffi/src/powerpc/ffi.c
+===================================================================
+--- libffi.orig/src/powerpc/ffi.c
++++ libffi/src/powerpc/ffi.c
+@@ -1,5 +1,6 @@
+ /* -----------------------------------------------------------------------
+ ffi.c - Copyright (C) 2011 Anthony Green
++ Copyright (C) 2011 Kyle Moffett
+ Copyright (C) 2008 Red Hat, Inc
+ Copyright (C) 2007, 2008 Free Software Foundation, Inc
+ Copyright (c) 1998 Geoffrey Keating
+@@ -44,11 +45,6 @@ enum {
+ FLAG_RETURNS_64BITS = 1 << (31-28),
+
+ FLAG_RETURNS_128BITS = 1 << (31-27), /* cr6 */
+- FLAG_SYSV_SMST_R4 = 1 << (31-26), /* use r4 for FFI_SYSV 8 byte
+- structs. */
+- FLAG_SYSV_SMST_R3 = 1 << (31-25), /* use r3 for FFI_SYSV 4 byte
+- structs. */
+- /* Bits (31-24) through (31-19) store shift value for SMST */
+
+ FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
+ FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
+@@ -672,34 +668,19 @@ ffi_prep_cif_machdep (ffi_cif *cif)
+ break;
+
+ case FFI_TYPE_STRUCT:
+- if (cif->abi == FFI_SYSV)
+- {
+- /* The final SYSV ABI says that structures smaller or equal 8 bytes
+- are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
+- in memory. */
+-
+- /* Treat structs with size <= 8 bytes. */
+- if (size <= 8)
+- {
+- flags |= FLAG_RETURNS_SMST;
+- /* These structs are returned in r3. We pack the type and the
+- precalculated shift value (needed in the sysv.S) into flags.
+- The same applies for the structs returned in r3/r4. */
+- if (size <= 4)
+- {
+- flags |= FLAG_SYSV_SMST_R3;
+- flags |= 8 * (4 - size) << 8;
+- break;
+- }
+- /* These structs are returned in r3 and r4. See above. */
+- if (size <= 8)
+- {
+- flags |= FLAG_SYSV_SMST_R3 | FLAG_SYSV_SMST_R4;
+- flags |= 8 * (8 - size) << 8;
+- break;
+- }
+- }
+- }
++ /*
++ * The final SYSV ABI says that structures smaller or equal 8 bytes
++ * are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
++ * in memory.
++ *
++ * NOTE: The assembly code can safely assume that it just needs to
++ * store both r3 and r4 into a 8-byte word-aligned buffer, as
++ * we allocate a temporary buffer in ffi_call() if this flag is
++ * set.
++ */
++ if (cif->abi == FFI_SYSV && size <= 8)
++ flags |= FLAG_RETURNS_SMST;
++
+ #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
+ byref:
+ #endif
+@@ -887,21 +868,32 @@ extern void FFI_HIDDEN ffi_call_LINUX64(
+ void
+ ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
+ {
++ /*
++ * The final SYSV ABI says that structures smaller or equal 8 bytes
++ * are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
++ * in memory.
++ *
++ * Just to keep things simple for the assembly code, we will always
++ * bounce-buffer struct return values less than or equal to 8 bytes.
++ * This allows the ASM to handle SYSV small structures by directly
++ * writing r3 and r4 to memory without worrying about struct size.
++ */
++ unsigned int smst_buffer[2];
+ extended_cif ecif;
++ unsigned int rsize;
+
+ ecif.cif = cif;
+ ecif.avalue = avalue;
+
+- /* If the return value is a struct and we don't have a return */
+- /* value address then we need to make one */
+-
+- if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
+- {
+- ecif.rvalue = alloca(cif->rtype->size);
+- }
+- else
+- ecif.rvalue = rvalue;
+-
++ /* Ensure that we have a valid struct return value */
++ ecif.rvalue = rvalue;
++ if (cif->rtype->type == FFI_TYPE_STRUCT) {
++ rsize = cif->rtype->size;
++ if (rsize <= 8)
++ ecif.rvalue = smst_buffer;
++ else if (!rvalue)
++ ecif.rvalue = alloca(rsize);
++ }
+
+ switch (cif->abi)
+ {
+@@ -921,6 +913,10 @@ ffi_call(ffi_cif *cif, void (*fn)(void),
+ FFI_ASSERT (0);
+ break;
+ }
++
++ /* Check for a bounce-buffered return value */
++ if (rvalue && ecif.rvalue == smst_buffer)
++ memcpy(rvalue, smst_buffer, rsize);
+ }
+
+
+Index: libffi/src/powerpc/sysv.S
+===================================================================
+--- libffi.orig/src/powerpc/sysv.S
++++ libffi/src/powerpc/sysv.S
+@@ -136,19 +136,14 @@ L(float_return_value):
+ b L(done_return_value)
+
+ L(small_struct_return_value):
+- extrwi %r6,%r31,2,19 /* number of bytes padding = shift/8 */
+- mtcrf 0x02,%r31 /* copy flags to cr[24:27] (cr6) */
+- extrwi %r5,%r31,5,19 /* r5 <- number of bits of padding */
+- subfic %r6,%r6,4 /* r6 <- number of useful bytes in r3 */
+- bf- 25,L(done_return_value) /* struct in r3 ? if not, done. */
+-/* smst_one_register: */
+- slw %r3,%r3,%r5 /* Left-justify value in r3 */
+- mtxer %r6 /* move byte count to XER ... */
+- stswx %r3,0,%r30 /* ... and store that many bytes */
+- bf+ 26,L(done_return_value) /* struct in r3:r4 ? */
+- add %r6,%r6,%r30 /* adjust pointer */
+- stswi %r4,%r6,4 /* store last four bytes */
+- b L(done_return_value)
++ /*
++ * The C code always allocates a properly-aligned 8-byte bounce
++ * buffer to make this assembly code very simple. Just write out
++ * r3 and r4 to the buffer to allow the C code to handle the rest.
++ */
++ stw %r3, 0(%r30)
++ stw %r4, 4(%r30)
++ b L(done_return_value)
+
+ .LFE1:
+ END(ffi_call_SYSV)
diff --git a/patches/series b/patches/series
index 3ebccee..86a000e 100644
--- a/patches/series
+++ b/patches/series
@@ -28,6 +28,7 @@ darwin-EH-fix
more-openbsd
more-openbsd-mips
darwin11
+powerpc-sysv-without-string-ops
minix
interix-patch
diff --git a/src/powerpc/ffi.c b/src/powerpc/ffi.c
index fb2a39f..53c7e8e 100644
--- a/src/powerpc/ffi.c
+++ b/src/powerpc/ffi.c
@@ -1,5 +1,6 @@
/* -----------------------------------------------------------------------
ffi.c - Copyright (C) 2011 Anthony Green
+ Copyright (C) 2011 Kyle Moffett
Copyright (C) 2008 Red Hat, Inc
Copyright (C) 2007, 2008 Free Software Foundation, Inc
Copyright (c) 1998 Geoffrey Keating
@@ -44,11 +45,6 @@ enum {
FLAG_RETURNS_64BITS = 1 << (31-28),
FLAG_RETURNS_128BITS = 1 << (31-27), /* cr6 */
- FLAG_SYSV_SMST_R4 = 1 << (31-26), /* use r4 for FFI_SYSV 8 byte
- structs. */
- FLAG_SYSV_SMST_R3 = 1 << (31-25), /* use r3 for FFI_SYSV 4 byte
- structs. */
- /* Bits (31-24) through (31-19) store shift value for SMST */
FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
@@ -672,34 +668,19 @@ ffi_prep_cif_machdep (ffi_cif *cif)
break;
case FFI_TYPE_STRUCT:
- if (cif->abi == FFI_SYSV)
- {
- /* The final SYSV ABI says that structures smaller or equal 8 bytes
- are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
- in memory. */
-
- /* Treat structs with size <= 8 bytes. */
- if (size <= 8)
- {
- flags |= FLAG_RETURNS_SMST;
- /* These structs are returned in r3. We pack the type and the
- precalculated shift value (needed in the sysv.S) into flags.
- The same applies for the structs returned in r3/r4. */
- if (size <= 4)
- {
- flags |= FLAG_SYSV_SMST_R3;
- flags |= 8 * (4 - size) << 8;
- break;
- }
- /* These structs are returned in r3 and r4. See above. */
- if (size <= 8)
- {
- flags |= FLAG_SYSV_SMST_R3 | FLAG_SYSV_SMST_R4;
- flags |= 8 * (8 - size) << 8;
- break;
- }
- }
- }
+ /*
+ * The final SYSV ABI says that structures smaller or equal 8 bytes
+ * are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
+ * in memory.
+ *
+ * NOTE: The assembly code can safely assume that it just needs to
+ * store both r3 and r4 into a 8-byte word-aligned buffer, as
+ * we allocate a temporary buffer in ffi_call() if this flag is
+ * set.
+ */
+ if (cif->abi == FFI_SYSV && size <= 8)
+ flags |= FLAG_RETURNS_SMST;
+
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
byref:
#endif
@@ -887,21 +868,32 @@ extern void FFI_HIDDEN ffi_call_LINUX64(extended_cif *, unsigned long,
void
ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
{
+ /*
+ * The final SYSV ABI says that structures smaller or equal 8 bytes
+ * are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
+ * in memory.
+ *
+ * Just to keep things simple for the assembly code, we will always
+ * bounce-buffer struct return values less than or equal to 8 bytes.
+ * This allows the ASM to handle SYSV small structures by directly
+ * writing r3 and r4 to memory without worrying about struct size.
+ */
+ unsigned int smst_buffer[2];
extended_cif ecif;
+ unsigned int rsize;
ecif.cif = cif;
ecif.avalue = avalue;
- /* If the return value is a struct and we don't have a return */
- /* value address then we need to make one */
-
- if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
- {
- ecif.rvalue = alloca(cif->rtype->size);
- }
- else
- ecif.rvalue = rvalue;
-
+ /* Ensure that we have a valid struct return value */
+ ecif.rvalue = rvalue;
+ if (cif->rtype->type == FFI_TYPE_STRUCT) {
+ rsize = cif->rtype->size;
+ if (rsize <= 8)
+ ecif.rvalue = smst_buffer;
+ else if (!rvalue)
+ ecif.rvalue = alloca(rsize);
+ }
switch (cif->abi)
{
@@ -921,6 +913,10 @@ ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
FFI_ASSERT (0);
break;
}
+
+ /* Check for a bounce-buffered return value */
+ if (rvalue && ecif.rvalue == smst_buffer)
+ memcpy(rvalue, smst_buffer, rsize);
}
diff --git a/src/powerpc/sysv.S b/src/powerpc/sysv.S
index 96ea22b..9ad8d9f 100644
--- a/src/powerpc/sysv.S
+++ b/src/powerpc/sysv.S
@@ -136,19 +136,14 @@ L(float_return_value):
b L(done_return_value)
L(small_struct_return_value):
- extrwi %r6,%r31,2,19 /* number of bytes padding = shift/8 */
- mtcrf 0x02,%r31 /* copy flags to cr[24:27] (cr6) */
- extrwi %r5,%r31,5,19 /* r5 <- number of bits of padding */
- subfic %r6,%r6,4 /* r6 <- number of useful bytes in r3 */
- bf- 25,L(done_return_value) /* struct in r3 ? if not, done. */
-/* smst_one_register: */
- slw %r3,%r3,%r5 /* Left-justify value in r3 */
- mtxer %r6 /* move byte count to XER ... */
- stswx %r3,0,%r30 /* ... and store that many bytes */
- bf+ 26,L(done_return_value) /* struct in r3:r4 ? */
- add %r6,%r6,%r30 /* adjust pointer */
- stswi %r4,%r6,4 /* store last four bytes */
- b L(done_return_value)
+ /*
+ * The C code always allocates a properly-aligned 8-byte bounce
+ * buffer to make this assembly code very simple. Just write out
+ * r3 and r4 to the buffer to allow the C code to handle the rest.
+ */
+ stw %r3, 0(%r30)
+ stw %r4, 4(%r30)
+ b L(done_return_value)
.LFE1:
END(ffi_call_SYSV)