Skip to content

Tail call VM [2] #18720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Zend/Zend.m4
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ ZEND_CHECK_STACK_DIRECTION
ZEND_CHECK_FLOAT_PRECISION
ZEND_DLSYM_CHECK
ZEND_CHECK_GLOBAL_REGISTER_VARIABLES
ZEND_CHECK_PRESERVE_NONE
ZEND_CHECK_CPUID_COUNT

AC_MSG_CHECKING([whether to enable thread safety])
Expand Down Expand Up @@ -471,3 +472,103 @@ AS_VAR_IF([ZEND_MAX_EXECUTION_TIMERS], [yes],
AC_MSG_CHECKING([whether to enable Zend max execution timers])
AC_MSG_RESULT([$ZEND_MAX_EXECUTION_TIMERS])
])

dnl
dnl ZEND_CHECK_PRESERVE_NONE
dnl
dnl Check if the preserve_none calling convention is supported and matches our
dnl expectations.
dnl
AC_DEFUN([ZEND_CHECK_PRESERVE_NONE], [dnl
AC_CACHE_CHECK([for preserve_none calling convention],
[php_cv_preverve_none],
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <stdio.h>
#include <stdint.h>

const char * const1 = "str1";
const char * const2 = "str2";
const char * const3 = "str3";
uint64_t key = UINT64_C(0x9d7f71d2bd296364);

uintptr_t _a = 0;
uintptr_t _b = 0;

uintptr_t __attribute__((preserve_none)) fun(uintptr_t a, uintptr_t b) {
_a = a;
_b = b;
return (uintptr_t)const3;
}

uintptr_t __attribute__((preserve_none)) test(void) {
uintptr_t ret;

#if defined(__x86_64__)
__asm__ __volatile__(
/* XORing to make it unlikely the value exists in any other register */
"movq %1, %%r12\n"
"xorq %3, %%r12\n"
"movq %2, %%r13\n"
"xorq %3, %%r13\n"
"xorq %%rax, %%rax\n"
"call fun\n"
: "=a" (ret)
: "r" (const1), "r" (const2), "r" (key)
: "r12", "r13"
);
#elif defined(__aarch64__)
__asm__ __volatile__(
/* XORing to make it unlikely the value exists in any other register */
"eor x20, %1, %3\n"
"eor x21, %2, %3\n"
"eor x0, x0, x0\n"
"bl fun\n"
"mov %0, x0\n"
: "=r" (ret)
: "r" (const1), "r" (const2), "r" (key)
: "x0", "x21", "x22", "x30"
);
#else
# error
#endif

return ret;
}

int main(void) {

/* JIT is making the following expectations about preserve_none:
* - The registers used for integer args 1 and 2
* - The register used for a single integer return value
*
* We check these expectations here:
*/

uintptr_t ret = test();

if (_a != ((uintptr_t)const1 ^ key)) {
fprintf(stderr, "arg1 mismatch\n");
return 1;
}
if (_b != ((uintptr_t)const2 ^ key)) {
fprintf(stderr, "arg2 mismatch\n");
return 2;
}
if (ret != (uintptr_t)const3) {
fprintf(stderr, "ret mismatch\n");
return 3;
}

fprintf(stderr, "OK\n");

return 0;
}]])],
[php_cv_preserve_none=yes],
[php_cv_preserve_none=no],
[php_cv_preserve_none=no])
])
AS_VAR_IF([php_cv_preserve_none], [yes], [
AC_DEFINE([HAVE_PRESERVE_NONE], [1],
[Define to 1 if you have preserve_none support.])
])
])
14 changes: 14 additions & 0 deletions Zend/zend_portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,20 @@ char *alloca();
# define ZEND_FASTCALL
#endif

#ifdef HAVE_PRESERVE_NONE
# define ZEND_PRESERVE_NONE __attribute__((preserve_none))
#endif

#if __has_attribute(musttail)
# define HAVE_MUSTTAIL
# define ZEND_MUSTTAIL __attribute__((musttail))
#endif

#if __has_attribute(sysv_abi)
# define HAVE_SYSV_ABI
# define ZEND_SYSV_ABI __attribute__((sysv_abi))
#endif

#if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(__APPLE__) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) || __has_attribute(noreturn)
# define HAVE_NORETURN
# define ZEND_NORETURN __attribute__((noreturn))
Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4917,7 +4917,7 @@ ZEND_VM_HOT_SEND_HANDLER(116, ZEND_SEND_VAL_EX, CONST|TMP, CONST|UNUSED|NUM, SPE
ZEND_VM_C_GOTO(send_val_by_ref);
}
} else if (ARG_MUST_BE_SENT_BY_REF(EX(call)->func, arg_num)) {
ZEND_VM_C_LABEL(send_val_by_ref):
ZEND_VM_C_LABEL(send_val_by_ref):;
ZEND_VM_DISPATCH_TO_HELPER(zend_cannot_pass_by_ref_helper, _arg_num, arg_num, _arg, arg);
}
value = GET_OP1_ZVAL_PTR(BP_VAR_R);
Expand Down Expand Up @@ -8235,9 +8235,9 @@ ZEND_VM_HANDLER(150, ZEND_USER_OPCODE, ANY, ANY)
case ZEND_USER_OPCODE_LEAVE:
ZEND_VM_LEAVE();
case ZEND_USER_OPCODE_DISPATCH:
ZEND_VM_DISPATCH(opline->opcode, opline);
ZEND_VM_DISPATCH_OPCODE(opline->opcode, opline);
default:
ZEND_VM_DISPATCH((uint8_t)(ret & 0xff), opline);
ZEND_VM_DISPATCH_OPCODE((uint8_t)(ret & 0xff), opline);
}
}

Expand Down
Loading