From addedc26751431b5384d22f44352adf8be798a41 Mon Sep 17 00:00:00 2001
From: Stuart Langley <slangley@google.com>
Date: Thu, 13 Sep 2012 17:37:37 +0200
Subject: [PATCH 1/4] Support building PHP with the native client toolchain.

The native client compiler defines the C macro __x86_64__, but the size
of an unsigned int is only 4 bytes. This causes the compile to fail
because the inline asm code compiled is for an 8 byte unsigned int. Add
the correct defines to fix this.

As detailed in:
http://code.google.com/p/nativeclient/issues/detail?id=2255
---
 Zend/zend_alloc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c
index 66cd23c7cbb5d..0b0e8632c1634 100644
--- a/Zend/zend_alloc.c
+++ b/Zend/zend_alloc.c
@@ -664,7 +664,7 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
 
 static inline unsigned int zend_mm_high_bit(size_t _size)
 {
-#if defined(__GNUC__) && defined(i386)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(i386))
 	unsigned int n;
 
 	__asm__("bsrl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
@@ -690,7 +690,7 @@ static inline unsigned int zend_mm_high_bit(size_t _size)
 
 static inline unsigned int zend_mm_low_bit(size_t _size)
 {
-#if defined(__GNUC__) && defined(i386)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(i386))
 	unsigned int n;
 
 	__asm__("bsfl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
@@ -2454,7 +2454,7 @@ ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_
 	return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
 }
 
-#if defined(__GNUC__) && defined(i386)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(i386))
 
 static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
 {

From d856881a6a6e611c57cebdb9100a517b80810139 Mon Sep 17 00:00:00 2001
From: Stuart Langley <slangley@google.com>
Date: Mon, 24 Sep 2012 14:08:31 +1000
Subject: [PATCH 2/4] Support building PHP with the NaCl toolchain.

---
 TSRM/threads.m4      | 4 ++++
 Zend/zend_multiply.h | 2 +-
 acinclude.m4         | 8 +++++++-
 config.sub           | 6 ++++++
 4 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/TSRM/threads.m4 b/TSRM/threads.m4
index 38494ce7cae9b..c36b5034e6ce2 100644
--- a/TSRM/threads.m4
+++ b/TSRM/threads.m4
@@ -90,6 +90,10 @@ int main() {
   case $host_alias in
   *netware*)
     pthreads_working=yes
+    ;;
+  *nacl*)
+    pthreads_working=yes
+    ;;
   esac
 ]
 ) ] )dnl
diff --git a/Zend/zend_multiply.h b/Zend/zend_multiply.h
index 33a86d7dd81e9..8ac2b9f533683 100644
--- a/Zend/zend_multiply.h
+++ b/Zend/zend_multiply.h
@@ -19,7 +19,7 @@
 
 /* $Id$ */
 
-#if defined(__i386__) && defined(__GNUC__)
+#if (defined(__i386__) || defined(__native_client__)) && defined(__GNUC__)
 
 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
 	long __tmpvar; 													\
diff --git a/acinclude.m4 b/acinclude.m4
index adb9599ce4887..fd02be2ff2a29 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -1124,9 +1124,15 @@ dnl
 dnl PHP_CHECK_TYPES(type-list, include-file [, extra-headers])
 dnl
 AC_DEFUN([PHP_CHECK_TYPES], [
+  cross_compile_value=0
+  case $host_alias in
+  *nacl*)
+    cross_compile_value=1
+    ;;
+  esac
   for php_typename in $1; do
     AC_MSG_CHECKING([whether $php_typename exists])
-    _PHP_CHECK_SIZEOF($php_typename, 0, $3, [
+    _PHP_CHECK_SIZEOF($php_typename, cross_compile_value, $3, [
       _PHP_DEF_HAVE_FILE($php_typename, $2)
       AC_MSG_RESULT([yes])
     ], [
diff --git a/config.sub b/config.sub
index 6759825a5b7fc..f9b96ef7dc70c 100644
--- a/config.sub
+++ b/config.sub
@@ -739,6 +739,10 @@ case $basic_machine in
 		basic_machine=i370-ibm
 		os=-mvs
 		;;
+        nacl)
+                basic_machine=1e32-unknown
+                os=-nacl
+                ;;
 	ncr3000)
 		basic_machine=i486-ncr
 		os=-sysv4
@@ -1388,6 +1392,8 @@ case $os in
 	-zvmoe)
 		os=-zvmoe
 		;;
+        -nacl*)
+                ;;
 	-none)
 		;;
 	*)

From 10b7fe6f34d4ea1a9420b5fcc85b18f7755068a6 Mon Sep 17 00:00:00 2001
From: Stuart Langley <slangley@google.com>
Date: Wed, 26 Sep 2012 11:34:22 +1000
Subject: [PATCH 3/4] Update config.sub

Use the correct basic_machine for nacl.
---
 config.sub | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/config.sub b/config.sub
index f9b96ef7dc70c..7071fff552dd7 100644
--- a/config.sub
+++ b/config.sub
@@ -740,7 +740,7 @@ case $basic_machine in
 		os=-mvs
 		;;
         nacl)
-                basic_machine=1e32-unknown
+                basic_machine=i686-pc
                 os=-nacl
                 ;;
 	ncr3000)

From 738ef0b835e92b78cc4db675ce0953f2874f695b Mon Sep 17 00:00:00 2001
From: Stuart Langley <slangley@google.com>
Date: Wed, 26 Sep 2012 14:42:54 +1000
Subject: [PATCH 4/4] Update Zend/zend_operators.h

More wrapping of inline __asm__ with defined(__native_client__)
---
 Zend/zend_operators.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h
index 08a6b19bd44b9..96d6228054282 100644
--- a/Zend/zend_operators.h
+++ b/Zend/zend_operators.h
@@ -477,7 +477,7 @@ ZEND_API void zend_update_current_locale(void);
 static zend_always_inline int fast_increment_function(zval *op1)
 {
 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
-#if defined(__GNUC__) && defined(__i386__)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(__i386__))
 		__asm__(
 			"incl (%0)\n\t"
 			"jno  0f\n\t"
@@ -514,7 +514,7 @@ static zend_always_inline int fast_increment_function(zval *op1)
 static zend_always_inline int fast_decrement_function(zval *op1)
 {
 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
-#if defined(__GNUC__) && defined(__i386__)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(__i386__))
 		__asm__(
 			"decl (%0)\n\t"
 			"jno  0f\n\t"
@@ -552,7 +552,7 @@ static zend_always_inline int fast_add_function(zval *result, zval *op1, zval *o
 {
 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
 		if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
-#if defined(__GNUC__) && defined(__i386__)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(__i386__))
 		__asm__(
 			"movl	(%1), %%eax\n\t"
 			"addl   (%2), %%eax\n\t"
@@ -627,7 +627,7 @@ static zend_always_inline int fast_sub_function(zval *result, zval *op1, zval *o
 {
 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
 		if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
-#if defined(__GNUC__) && defined(__i386__)
+#if defined(__GNUC__) && (defined(__native_client__) || defined(__i386__))
 		__asm__(
 			"movl	(%1), %%eax\n\t"
 			"subl   (%2), %%eax\n\t"