Skip to content

Commit 767a4af

Browse files
committed
Introduce and use ZEND_JIT_TARGET_X86 and ZEND_JIT_TARGET_ARM64 macros.
Remove x86 specific code from ext/opcache/jit/zend_jit_arm64.dasc. Fixed ARM64 build without libcapstone.
1 parent 0123c99 commit 767a4af

File tree

6 files changed

+41
-136
lines changed

6 files changed

+41
-136
lines changed

ext/opcache/jit/zend_jit.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@
3939
#include "Optimizer/zend_call_graph.h"
4040
#include "Optimizer/zend_dump.h"
4141

42-
#if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
43-
#include "jit/zend_jit_x86.h"
44-
#elif defined (__aarch64__)
45-
#include "jit/zend_jit_arm64.h"
46-
#else
47-
#error "JIT not supported on this platform"
42+
#if ZEND_JIT_TARGET_X86
43+
# include "jit/zend_jit_x86.h"
44+
#elif ZEND_JIT_TARGET_ARM64
45+
# include "jit/zend_jit_arm64.h"
4846
#endif
4947

5048
#include "jit/zend_jit_internal.h"
@@ -206,12 +204,12 @@ static bool zend_is_commutative(zend_uchar opcode)
206204
#define OP2_RANGE() OP_RANGE(ssa_op, op2)
207205
#define OP1_DATA_RANGE() OP_RANGE(ssa_op + 1, op1)
208206

209-
#if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
210-
#include "dynasm/dasm_x86.h"
211-
#elif defined(__aarch64__)
207+
#if ZEND_JIT_TARGET_X86
208+
# include "dynasm/dasm_x86.h"
209+
#elif ZEND_JIT_TARGET_ARM64
212210
static int zend_jit_add_veneer(dasm_State *Dst, void *buffer, uint32_t ins, int *b, uint32_t *cp, ptrdiff_t offset);
213-
#define DASM_ADD_VENEER zend_jit_add_veneer
214-
#include "dynasm/dasm_arm64.h"
211+
# define DASM_ADD_VENEER zend_jit_add_veneer
212+
# include "dynasm/dasm_arm64.h"
215213
#endif
216214

217215
#include "jit/zend_jit_helpers.c"
@@ -224,11 +222,11 @@ static int zend_jit_add_veneer(dasm_State *Dst, void *buffer, uint32_t ins, int
224222
# include "jit/zend_jit_oprofile.c"
225223
#endif
226224

227-
#if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
228-
#include "jit/zend_jit_vtune.c"
229-
#include "jit/zend_jit_x86.c"
230-
#elif defined(__aarch64__)
231-
#include "jit/zend_jit_arm64.c"
225+
#if ZEND_JIT_TARGET_X86
226+
# include "jit/zend_jit_vtune.c"
227+
# include "jit/zend_jit_x86.c"
228+
#elif ZEND_JIT_TARGET_ARM64
229+
# include "jit/zend_jit_arm64.c"
232230
#endif
233231

234232
#if _WIN32
@@ -410,7 +408,7 @@ static void *dasm_link_and_encode(dasm_State **dasm_state,
410408
return NULL;
411409
}
412410

413-
#ifdef __aarch64__
411+
#if ZEND_JIT_TARGET_ARM64
414412
dasm_venners_size = 0;
415413
#endif
416414

@@ -422,7 +420,7 @@ static void *dasm_link_and_encode(dasm_State **dasm_state,
422420
return NULL;
423421
}
424422

425-
#ifdef __aarch64__
423+
#if ZEND_JIT_TARGET_ARM64
426424
size += dasm_venners_size;
427425
#endif
428426

@@ -4407,7 +4405,7 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, bool reattached)
44074405
}
44084406

44094407
zend_jit_unprotect();
4410-
#ifdef __aarch64__
4408+
#if ZEND_JIT_TARGET_ARM64
44114409
/* reserve space for global labels veneers */
44124410
dasm_labels_veneers = *dasm_ptr;
44134411
*dasm_ptr = (void**)*dasm_ptr + zend_lb_MAX;

ext/opcache/jit/zend_jit.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
#ifndef HAVE_JIT_H
2020
#define HAVE_JIT_H
2121

22+
#if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
23+
# define ZEND_JIT_TARGET_X86 1
24+
# define ZEND_JIT_TARGET_ARM64 0
25+
#elif defined (__aarch64__)
26+
# define ZEND_JIT_TARGET_X86 0
27+
# define ZEND_JIT_TARGET_ARM64 1
28+
#else
29+
# error "JIT not supported on this platform"
30+
#endif
31+
2232
#define ZEND_JIT_LEVEL_NONE 0 /* no JIT */
2333
#define ZEND_JIT_LEVEL_MINIMAL 1 /* minimal JIT (subroutine threading) */
2434
#define ZEND_JIT_LEVEL_INLINE 2 /* selective inline threading */

ext/opcache/jit/zend_jit_arm64.dasc

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,115 +2642,8 @@ static int zend_jit_setup(void)
26422642
allowed_opt_flags = 0;
26432643

26442644
#if ZTS
2645-
# ifdef _WIN64
2646-
tsrm_tls_index = _tls_index * sizeof(void*);
2647-
2648-
/* To find offset of "_tsrm_ls_cache" in TLS segment we perform a linear scan of local TLS memory */
2649-
/* Probably, it might be better solution */
2650-
do {
2651-
void ***tls_mem = ((void**)__readgsqword(0x58))[_tls_index];
2652-
void *val = _tsrm_ls_cache;
2653-
size_t offset = 0;
2654-
size_t size = (char*)&_tls_end - (char*)&_tls_start;
2655-
2656-
while (offset < size) {
2657-
if (*tls_mem == val) {
2658-
tsrm_tls_offset = offset;
2659-
break;
2660-
}
2661-
tls_mem++;
2662-
offset += sizeof(void*);
2663-
}
2664-
if (offset >= size) {
2665-
// TODO: error message ???
2666-
return FAILURE;
2667-
}
2668-
} while(0);
2669-
# elif ZEND_WIN32
2670-
tsrm_tls_index = _tls_index * sizeof(void*);
2671-
2672-
/* To find offset of "_tsrm_ls_cache" in TLS segment we perform a linear scan of local TLS memory */
2673-
/* Probably, it might be better solution */
2674-
do {
2675-
void ***tls_mem = ((void***)__readfsdword(0x2c))[_tls_index];
2676-
void *val = _tsrm_ls_cache;
2677-
size_t offset = 0;
2678-
size_t size = (char*)&_tls_end - (char*)&_tls_start;
2679-
2680-
while (offset < size) {
2681-
if (*tls_mem == val) {
2682-
tsrm_tls_offset = offset;
2683-
break;
2684-
}
2685-
tls_mem++;
2686-
offset += sizeof(void*);
2687-
}
2688-
if (offset >= size) {
2689-
// TODO: error message ???
2690-
return FAILURE;
2691-
}
2692-
} while(0);
2693-
# elif defined(__APPLE__) && defined(__x86_64__)
2694-
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
2695-
if (tsrm_ls_cache_tcb_offset == 0) {
2696-
size_t *ti;
2697-
__asm__(
2698-
"leaq __tsrm_ls_cache(%%rip),%0"
2699-
: "=r" (ti));
2700-
tsrm_tls_offset = ti[2];
2701-
tsrm_tls_index = ti[1] * 8;
2702-
}
2703-
# elif defined(__GNUC__) && defined(__x86_64__)
2704-
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
2705-
if (tsrm_ls_cache_tcb_offset == 0) {
2706-
#if defined(__has_attribute) && __has_attribute(tls_model) && !defined(__FreeBSD__)
2707-
size_t ret;
2708-
2709-
asm ("movq _tsrm_ls_cache@gottpoff(%%rip),%0"
2710-
: "=r" (ret));
2711-
tsrm_ls_cache_tcb_offset = ret;
2712-
#else
2713-
size_t *ti;
2714-
2715-
__asm__(
2716-
"leaq _tsrm_ls_cache@tlsgd(%%rip), %0\n"
2717-
: "=a" (ti));
2718-
tsrm_tls_offset = ti[1];
2719-
tsrm_tls_index = ti[0] * 16;
2720-
#endif
2721-
}
2722-
# elif defined(__GNUC__) && defined(__i386__)
2723-
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
2724-
if (tsrm_ls_cache_tcb_offset == 0) {
2725-
#if !defined(__FreeBSD__)
2726-
size_t ret;
2727-
2728-
asm ("leal _tsrm_ls_cache@ntpoff,%0\n"
2729-
: "=a" (ret));
2730-
tsrm_ls_cache_tcb_offset = ret;
2731-
#else
2732-
size_t *ti, _ebx, _ecx, _edx;
2733-
2734-
__asm__(
2735-
"call 1f\n"
2736-
".subsection 1\n"
2737-
"1:\tmovl (%%esp), %%ebx\n\t"
2738-
"ret\n"
2739-
".previous\n\t"
2740-
"addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t"
2741-
"leal _tsrm_ls_cache@tlsldm(%%ebx), %0\n\t"
2742-
"call ___tls_get_addr@plt\n\t"
2743-
"leal _tsrm_ls_cache@tlsldm(%%ebx), %0\n"
2744-
: "=a" (ti), "=&b" (_ebx), "=&c" (_ecx), "=&d" (_edx));
2745-
tsrm_tls_offset = ti[1];
2746-
tsrm_tls_index = ti[0] * 8;
2747-
#endif
2748-
}
2749-
# elif defined(__aarch64__)
27502645
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
27512646
ZEND_ASSERT(tsrm_ls_cache_tcb_offset != 0);
2752-
# elif
2753-
# endif
27542647
#endif
27552648

27562649
return SUCCESS;

ext/opcache/jit/zend_jit_disasm.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# define HAVE_DISASM 1
2424
# include <capstone/capstone.h>
2525
# define HAVE_CAPSTONE_ITER 1
26-
#else
26+
#elif ZEND_JIT_TARGET_X86
2727
# define HAVE_DISASM 1
2828
# define DISASM_INTEL_SYNTAX 0
2929

@@ -38,6 +38,8 @@
3838
# include "jit/libudis86/udis86.c"
3939
#endif /* HAVE_CAPSTONE */
4040

41+
#ifdef HAVE_DISASM
42+
4143
static void zend_jit_disasm_add_symbol(const char *name,
4244
uint64_t addr,
4345
uint64_t size);
@@ -225,15 +227,15 @@ static uint64_t zend_jit_disasm_branch_target(csh cs, const cs_insn *insn)
225227
{
226228
unsigned int i;
227229

228-
#if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
230+
#if ZEND_JIT_TARGET_X86
229231
if (cs_insn_group(cs, insn, X86_GRP_JUMP)) {
230232
for (i = 0; i < insn->detail->x86.op_count; i++) {
231233
if (insn->detail->x86.operands[i].type == X86_OP_IMM) {
232234
return insn->detail->x86.operands[i].imm;
233235
}
234236
}
235237
}
236-
#elif defined(__aarch64__)
238+
#elif ZEND_JIT_TARGET_ARM64
237239
if (cs_insn_group(cs, insn, ARM64_GRP_JUMP)
238240
|| insn->id == ARM64_INS_BL
239241
|| insn->id == ARM64_INS_ADR) {
@@ -321,7 +323,7 @@ static int zend_jit_disasm(const char *name,
321323
#endif
322324

323325
#ifdef HAVE_CAPSTONE
324-
# if defined(__x86_64__) || defined(_WIN64) || defined(ZEND_WIN32)
326+
# if ZEND_JIT_TARGET_X86
325327
if (cs_open(CS_ARCH_X86, CS_MODE_64, &cs) != CS_ERR_OK)
326328
return 0;
327329
cs_option(cs, CS_OPT_DETAIL, CS_OPT_ON);
@@ -330,7 +332,7 @@ static int zend_jit_disasm(const char *name,
330332
# else
331333
cs_option(cs, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
332334
# endif
333-
# elif defined(__aarch64__)
335+
# elif ZEND_JIT_TARGET_ARM64
334336
if (cs_open(CS_ARCH_ARM64, CS_MODE_ARM, &cs) != CS_ERR_OK)
335337
return 0;
336338
cs_option(cs, CS_OPT_DETAIL, CS_OPT_ON);
@@ -768,3 +770,5 @@ static void zend_jit_disasm_shutdown(void)
768770
JIT_G(symbols) = NULL;
769771
}
770772
}
773+
774+
#endif /* HAVE_DISASM */

ext/opcache/jit/zend_jit_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static zend_always_inline bool zend_jit_may_be_polymorphic_call(const zend_op *o
727727

728728
/* Instruction cache flush */
729729
#ifndef JIT_CACHE_FLUSH
730-
# if defined (__aarch64__)
730+
# if ZEND_JIT_TARGET_ARM64
731731
# if ((defined(__GNUC__) && ZEND_GCC_VERSION >= 4003) || __has_builtin(__builtin___clear_cache))
732732
# define JIT_CACHE_FLUSH(from, to) __builtin___clear_cache((char*)(from), (char*)(to))
733733
# else

ext/opcache/jit/zend_jit_vm_helpers.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
#include "Optimizer/zend_func_info.h"
2929
#include "Optimizer/zend_call_graph.h"
3030
#include "zend_jit.h"
31-
#if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
32-
#include "zend_jit_x86.h"
33-
#elif defined(__aarch64__)
34-
#include "zend_jit_arm64.h"
31+
#if ZEND_JIT_TARGET_X86
32+
# include "zend_jit_x86.h"
33+
#elif ZEND_JIT_TARGET_ARM64
34+
# include "zend_jit_arm64.h"
3535
#endif
3636

3737
#include "zend_jit_internal.h"

0 commit comments

Comments
 (0)