Skip to content

Commit

Permalink
Add built-in luajit support for s390x
Browse files Browse the repository at this point in the history
Signed-off-by: YingJie Fu <[email protected]>
  • Loading branch information
YingJie Fu committed Jul 18, 2024
1 parent 12a9de5 commit 1995002
Show file tree
Hide file tree
Showing 17 changed files with 6,697 additions and 0 deletions.
546 changes: 546 additions & 0 deletions lib/luajit-04dca791/dynasm/dasm_s390x.h

Large diffs are not rendered by default.

1,634 changes: 1,634 additions & 0 deletions lib/luajit-04dca791/dynasm/dasm_s390x.lua

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/luajit-04dca791/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ else
ifneq (,$(findstring LJ_TARGET_ARM ,$(TARGET_TESTARCH)))
TARGET_LJARCH= arm
else
ifneq (,$(findstring LJ_TARGET_S390X ,$(TARGET_TESTARCH)))
TARGET_LJARCH= s390x
else
ifneq (,$(findstring LJ_TARGET_ARM64 ,$(TARGET_TESTARCH)))
ifneq (,$(findstring __AARCH64EB__ ,$(TARGET_TESTARCH)))
TARGET_ARCH= -D__AARCH64EB__=1
Expand Down Expand Up @@ -274,6 +277,7 @@ endif
endif
endif
endif
endif

ifneq (,$(findstring LJ_TARGET_PS3 1,$(TARGET_TESTARCH)))
TARGET_SYS= PS3
Expand Down
2 changes: 2 additions & 0 deletions lib/luajit-04dca791/src/host/buildvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);
#include "../dynasm/dasm_ppc.h"
#elif LJ_TARGET_MIPS
#include "../dynasm/dasm_mips.h"
#elif LJ_TARGET_S390X
#include "../dynasm/dasm_s390x.h"
#else
#error "No support for this architecture (yet)"
#endif
Expand Down
53 changes: 53 additions & 0 deletions lib/luajit-04dca791/src/host/buildvm_asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,54 @@ static void emit_asm_reloc_text(BuildCtx *ctx, uint8_t *cp, int n,
}
fprintf(ctx->fp, "\t%s %s\n", opname, sym);
}
#elif LJ_TARGET_S390X
/* Emit halfwords piecewise as assembler text. */
static void emit_asm_halfwords(BuildCtx *ctx, uint8_t *p, int n)
{
uint16_t *cp = (uint16_t*)p;
n /= 2;
int i;
for (i = 0; i < n; i++) {
if ((i & 7) == 0)
fprintf(ctx->fp, "\t.hword 0x%hx", cp[i]);
else
fprintf(ctx->fp, ",0x%hx", cp[i]);
if ((i & 7) == 7) putc('\n', ctx->fp);
}
if ((n & 7) != 0) putc('\n', ctx->fp);
}

/* Emit s390x text relocations. */
static void emit_asm_reloc_text(BuildCtx *ctx, uint8_t *cp, int n,
const char *sym)
{
if (n & 1 || n < 2) {
fprintf(stderr, "Error: instruction stream length invalid: %d.\n", n);
exit(1);
}
n -= 2;
const char *opname = NULL;
const char *argt = ""; /* Inserted before argument. */
int opcode = *(uint16_t*)(&cp[n]);
int arg = (opcode>>4) & 0xf;
switch (opcode & 0xff0f) {
case 0xa705: opname = "bras"; argt = "%r"; break;
case 0xc005: opname = "brasl"; argt = "%r"; break;
case 0xa704: opname = "brc"; break;
case 0xc004: opname = "brcl"; break;
default:
fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n",
sym);
exit(1);
}
emit_asm_halfwords(ctx, cp, n);
if (strncmp(sym+(*sym == '_'), LABEL_PREFIX, sizeof(LABEL_PREFIX)-1)) {
/* Various fixups for external symbols outside of our binary. */
fprintf(ctx->fp, "\t%s %s%d, %s@PLT\n", opname, argt, arg, sym);
return;
}
fprintf(ctx->fp, "\t%s %s%d, %s\n", opname, argt, arg, sym);
}
#else
/* Emit words piecewise as assembler text. */
static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n)
Expand Down Expand Up @@ -302,6 +350,9 @@ void emit_asm(BuildCtx *ctx)
emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);
}
ofs += n+4;
#elif LJ_TARGET_S390X
emit_asm_reloc_text(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
ofs += n+4;
#else
emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
ofs += n;
Expand All @@ -310,6 +361,8 @@ void emit_asm(BuildCtx *ctx)
}
#if LJ_TARGET_X86ORX64
emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);
#elif LJ_TARGET_S390X
emit_asm_halfwords(ctx, ctx->code+ofs, next-ofs);
#else
emit_asm_words(ctx, ctx->code+ofs, next-ofs);
#endif
Expand Down
1 change: 1 addition & 0 deletions lib/luajit-04dca791/src/jit/dis_s390x.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Not yet implemented.
2 changes: 2 additions & 0 deletions lib/luajit-04dca791/src/lib_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,8 @@ static uint32_t jit_cpudetect(void)
}
#endif

#elif LJ_TARGET_S390X
/* No optional CPU features to detect (for now). */
#else
#error "Missing CPU detection for this architecture"
#endif
Expand Down
19 changes: 19 additions & 0 deletions lib/luajit-04dca791/src/lj_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define LUAJIT_ARCH_mips32 6
#define LUAJIT_ARCH_MIPS64 7
#define LUAJIT_ARCH_mips64 7
#define LUAJIT_ARCH_S390X 8
#define LUAJIT_ARCH_s390x 8

/* Target OS. */
#define LUAJIT_OS_OTHER 0
Expand Down Expand Up @@ -59,6 +61,8 @@
#define LUAJIT_TARGET LUAJIT_ARCH_ARM
#elif defined(__aarch64__) || defined(_M_ARM64)
#define LUAJIT_TARGET LUAJIT_ARCH_ARM64
#elif defined(__s390x__) || defined(__s390x)
#define LUAJIT_TARGET LUAJIT_ARCH_S390X
#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC)
#define LUAJIT_TARGET LUAJIT_ARCH_PPC
#elif defined(__mips64__) || defined(__mips64) || defined(__MIPS64__) || defined(__MIPS64)
Expand Down Expand Up @@ -439,6 +443,21 @@
#define LJ_ARCH_VERSION 10
#endif

#elif LUAJIT_TARGET == LUAJIT_ARCH_S390X

#define LJ_ARCH_NAME "s390x"
#define LJ_ARCH_BITS 64
#define LJ_ARCH_ENDIAN LUAJIT_BE
#define LJ_TARGET_S390X 1
#define LJ_TARGET_EHRETREG 0xe
#define LJ_TARGET_JUMPRANGE 32 /* +-2^32 = +-4GB (32-bit, halfword aligned) */
#define LJ_TARGET_MASKSHIFT 1
#define LJ_TARGET_MASKROT 1
#define LJ_TARGET_UNALIGNED 1
#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL
#define LJ_TARGET_GC64 1
#define LJ_ARCH_NOJIT 1 /* NYI */

#else
#error "No target architecture defined"
#endif
Expand Down
2 changes: 2 additions & 0 deletions lib/luajit-04dca791/src/lj_asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,8 @@ static void asm_loop(ASMState *as)
#include "lj_asm_ppc.h"
#elif LJ_TARGET_MIPS
#include "lj_asm_mips.h"
#elif LJ_TARGET_S390X
#include "lj_asm_s390x.h"
#else
#error "Missing assembler for target CPU"
#endif
Expand Down
50 changes: 50 additions & 0 deletions lib/luajit-04dca791/src/lj_ccall.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,40 @@
goto done; \
}

#elif LJ_TARGET_S390X
/* -- POSIX/s390x calling conventions --------------------------------------- */

#define CCALL_HANDLE_STRUCTRET \
cc->retref = 1; /* Return all structs by reference. */ \
cc->gpr[ngpr++] = (GPRArg)dp;

#define CCALL_HANDLE_COMPLEXRET \
cc->retref = 1; /* Return all complex values by reference. */ \
cc->gpr[ngpr++] = (GPRArg)dp;

#define CCALL_HANDLE_COMPLEXRET2 \
UNUSED(dp); /* Nothing to do. */

#define CCALL_HANDLE_STRUCTARG \
/* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \
if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \
rp = cdataptr(lj_cdata_new(cts, did, sz)); \
sz = CTSIZE_PTR; /* Pass all other structs by reference. */ \
}

#define CCALL_HANDLE_COMPLEXARG \
/* Pass complex numbers by reference. */ \
/* TODO: not sure why this is different to structs. */ \
rp = cdataptr(lj_cdata_new(cts, did, sz)); \
sz = CTSIZE_PTR; \

#define CCALL_HANDLE_REGARG \
if (isfp) { \
if (nfpr < CCALL_NARG_FPR) { dp = &cc->fpr[nfpr++]; goto done; } \
} else { \
if (ngpr < maxgpr) { dp = &cc->gpr[ngpr++]; goto done; } \
}

#else
#error "Missing calling convention definitions for this architecture"
#endif
Expand Down Expand Up @@ -999,6 +1033,9 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
CType *d;
CTSize sz;
MSize n, isfp = 0, isva = 0;
#if LJ_TARGET_S390X
MSize onstack = 0;
#endif
void *dp, *rp = NULL;

if (fid) { /* Get argument type from field. */
Expand Down Expand Up @@ -1037,6 +1074,9 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
CCALL_HANDLE_REGARG /* Handle register arguments. */

/* Otherwise pass argument on stack. */
#if LJ_TARGET_S390X
onstack = 1;
#endif
if (CCALL_ALIGN_STACKARG) { /* Align argument on stack. */
MSize align = (1u << ctype_align(d->info)) - 1;
if (rp || (CCALL_PACK_STACKARG && isva && align < CTSIZE_PTR-1))
Expand Down Expand Up @@ -1086,6 +1126,16 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
*(int64_t *)dp = (int64_t)*(int32_t *)dp; /* Sign-extend to 64 bit. */
}
#endif
#if LJ_TARGET_S390X
/* Arguments need to be sign-/zero-extended to 64-bits. */
if ((ctype_isinteger_or_bool(d->info) || ctype_isenum(d->info) ||
(isfp && onstack)) && d->size <= 4) {
if (d->info & CTF_UNSIGNED || isfp)
*(uint64_t *)dp = (uint64_t)*(uint32_t *)dp;
else
*(int64_t *)dp = (int64_t)*(int32_t *)dp;
}
#endif
#if LJ_TARGET_X64 && LJ_ABI_WIN
if (isva) { /* Windows/x64 mirrors varargs in both register sets. */
if (nfpr == ngpr)
Expand Down
15 changes: 15 additions & 0 deletions lib/luajit-04dca791/src/lj_ccall.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ typedef union FPRArg {
struct { LJ_ENDIAN_LOHI(float f; , float g;) };
} FPRArg;

#elif LJ_TARGET_S390X

#define CCALL_NARG_GPR 5 /* GPR 2,3,4,5,6 */
#define CCALL_NARG_FPR 4 /* FPR 0,2,4,8 */
#define CCALL_NRET_GPR 1 /* GPR 2 */
#define CCALL_NRET_FPR 1 /* FPR 0 */
#define CCALL_SPS_EXTRA 20 /* 160-byte callee save area (not sure if this is the right place) */
#define CCALL_SPS_FREE 0

typedef intptr_t GPRArg;
typedef union FPRArg {
double d;
float f;
} FPRArg;

#else
#error "Missing calling convention definitions for this architecture"
#endif
Expand Down
9 changes: 9 additions & 0 deletions lib/luajit-04dca791/src/lj_ccallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,15 @@ void lj_ccallback_mcode_free(CTState *cts)
if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
((float *)dp)[1] = *(float *)dp;

#elif LJ_TARGET_S390X

#define CALLBACK_HANDLE_REGARG \
if (isfp) { \
if (nfpr < CCALL_NARG_FPR) { sp = &cts->cb.fpr[nfpr++]; goto done; } \
} else { \
if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \
}

#else
#error "Missing calling convention definitions for this architecture"
#endif
Expand Down
3 changes: 3 additions & 0 deletions lib/luajit-04dca791/src/lj_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,
if (version != 1)
return _URC_FATAL_PHASE1_ERROR;
cf = (void *)_Unwind_GetCFA(ctx);
#ifdef LJ_TARGET_S390X
cf -= 160; /* CFA points 160 bytes above r15. */
#endif
L = cframe_L(cf);
if ((actions & _UA_SEARCH_PHASE)) {
#if LJ_UNWIND_EXT
Expand Down
14 changes: 14 additions & 0 deletions lib/luajit-04dca791/src/lj_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,20 @@ enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
#endif
#define CFRAME_OFS_MULTRES 0
#define CFRAME_SHIFT_MULTRES 3
#elif LJ_TARGET_S390X
#define CFRAME_OFS_ERRF 280
#define CFRAME_OFS_NRES 272
#define CFRAME_OFS_PREV 264
#define CFRAME_OFS_L 256
#define CFRAME_OFS_PC 168
#define CFRAME_OFS_MULTRES 160
#define CFRAME_SIZE 240
/*
** TODO: it would be good if we always decoded param*8 like
** the RISC architectures do. If so then SHIFT_MULTRES will
** need to change to 3.
*/
#define CFRAME_SHIFT_MULTRES 0
#else
#error "Missing CFRAME_* definitions for this architecture"
#endif
Expand Down
2 changes: 2 additions & 0 deletions lib/luajit-04dca791/src/lj_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ typedef uint32_t RegCost;
#include "lj_target_ppc.h"
#elif LJ_TARGET_MIPS
#include "lj_target_mips.h"
#elif LJ_TARGET_S390X
#include "lj_target_s390x.h"
#else
#error "Missing include for target CPU"
#endif
Expand Down
80 changes: 80 additions & 0 deletions lib/luajit-04dca791/src/lj_target_s390x.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
** Definitions for IBM z/Architecture (s390x) CPUs.
** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
*/

#ifndef _LJ_TARGET_S390X_H
#define _LJ_TARGET_S390X_H

/* -- Registers IDs ------------------------------------------------------- */

#define GPRDEF(_) \
_(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \
_(R8) _(R9) _(R10) _(R11) _(R12) _(R13) _(R14) _(R15)
#define FPRDEF(_) \
_(F0) _(F1) _(F2) _(F3) \
_(F4) _(F5) _(F6) _(F7) \
_(F8) _(F9) _(F10) _(F11) \
_(F12) _(F13) _(F14) _(F15)
// TODO: VREG?

#define RIDENUM(name) RID_##name,

enum {
GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */
FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */
RID_MAX,

/* Calling conventions. */
RID_SP = RID_R15,
RID_RET = RID_R2,
RID_FPRET = RID_F0,

/* These definitions must match with the *.dasc file(s): */
RID_BASE = RID_R7, /* Interpreter BASE. */
RID_LPC = RID_R9, /* Interpreter PC. */
RID_DISPATCH = RID_R10, /* Interpreter DISPATCH table. */

/* Register ranges [min, max) and number of registers. */
RID_MIN_GPR = RID_R0,
RID_MIN_FPR = RID_F0,
RID_MAX_GPR = RID_MIN_FPR,
RID_MAX_FPR = RID_MAX,
RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,
RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR,
};

/* -- Register sets ------------------------------------------------------- */

/* -- Spill slots --------------------------------------------------------- */

/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.
**
** SPS_FIXED: Available fixed spill slots in interpreter frame.
** This definition must match with the *.dasc file(s).
**
** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots.
*/
#define SPS_FIXED 2
#define SPS_FIRST 2

#define SPOFS_TMP 0

#define sps_scale(slot) (4 * (int32_t)(slot))
#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1)

/* -- Exit state ---------------------------------------------------------- */

/* This definition must match with the *.dasc file(s). */
typedef struct {
lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */
int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */
int32_t spill[256]; /* Spill slots. */
} ExitState;

#define EXITSTUB_SPACING 4
#define EXITSTUBS_PER_GROUP 32

/* -- Instructions -------------------------------------------------------- */

#endif
Loading

0 comments on commit 1995002

Please sign in to comment.