Skip to content
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

Implement emit_apply_fun_shared #12

Draft
wants to merge 18 commits into
base: arm32-jit
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 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
30 changes: 21 additions & 9 deletions erts/emulator/beam/jit/arm/32/beam_asm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,10 @@ struct BeamAssembler : public BeamAssemblerCommon {
const a32::Gp ARG3 = a32::r2;
const a32::Gp ARG4 = a32::r3;

const a32::Gp VAR = a32::r6;
const a32::Gp TMP = a32::r12;

#ifdef ERTS_MSACC_EXTENDED_STATES
const arm::Mem erts_msacc_cache = getSchedulerRegRef(
offsetof(ErtsSchedulerRegisters, aux_regs.d.erts_msacc_cache));
#endif
const a32::Gp VAR = a32::r6;

static const int num_register_backed_fregs = 8;
constexpr arm::Mem getSchedulerRegRef(int offset) const {
ASSERT((offset & (sizeof(Eterm) - 1)) == 0);
return arm::Mem(scheduler_registers, offset);
Expand Down Expand Up @@ -273,10 +269,14 @@ struct BeamAssembler : public BeamAssemblerCommon {
}

a32::Gp emit_ptr_val(a32::Gp Dst, a32::Gp Src) {
a32::Gp r;
#if !defined(TAG_LITERAL_PTR)
return Src;
#else
// TODO
// TAG_LITERAL_PTR is undefined in ARCH_32 and may be not needed
ASSERT(false);
return r;
return Dst;
#endif
}

void emit_untag_ptr(a32::Gp Dst, a32::Gp Src) {
Expand Down Expand Up @@ -326,7 +326,19 @@ struct BeamAssembler : public BeamAssemblerCommon {
template<typename T>
void mov_imm(a32::Gp to, T value) {
// TODO
ASSERT(false);
// implement this for emit_apply_fun_shared
static_assert(std::is_integral<T>::value || std::is_pointer<T>::value);
if (value) {
ASSERT(false);
a.mov(to, imm(value));
} else {
// arm64 uses the xzr register (ZERO) here and
// x86 uses some x86 specific instruction sequence
// to set the register to zero.
// We need to understand how one sets a register to zero in
// arm32
a.mov(to, 0);
}
}

void mov_imm(a32::Gp to, std::nullptr_t value) {
Expand Down
71 changes: 69 additions & 2 deletions erts/emulator/beam/jit/arm/32/instr_fun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,75 @@ void BeamModuleAssembler::emit_i_make_fun3(const ArgLambda &Lambda,
}

void BeamGlobalAssembler::emit_apply_fun_shared() {
// TODO
ASSERT(false);
// TODO this is the first called emitter
Label finished = a.newLabel();

/* Put the arity and fun into the right registers for `call_fun`, and stash
* the argument list in ARG2 for the error path. We'll bump the arity as
* we go through the argument list. */
mov_imm(ARG3, 0);
a.ldr(ARG4, getXRef(0));
a.ldr(ARG2, getXRef(1));
{
Label unpack_next = a.newLabel(), malformed_list = a.newLabel(),
raise_error = a.newLabel();

auto x_register = getXRef(0);

ASSERT(x_register.shift() == 0);
x_register.setIndex(ARG3);
x_register.setShift(2);

a.mov(ARG1, ARG2);
a.bind(unpack_next);
{
a.cmp(ARG1, imm(NIL));
a.b_eq(finished);

ERTS_CT_ASSERT(_TAG_PRIMARY_MASK - TAG_PRIMARY_LIST == (1 << 1));
a.tst(ARG1, imm(1)),
a.b_ne(malformed_list),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there commas and not semicolon at the end of the two lines above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omg... just my erlang braincells adding chaos


emit_ptr_val(ARG1, ARG1);
a.ldr(TMP, getCARRef(ARG1));
a.ldr(ARG1, getCDRRef(ARG1));
a.str(TMP, x_register);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fails emission....

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a Mem object is fine and emission succeeds wiht getXRef(0) but it fails if the operand has been setup with setIndex and setShift.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to ChatGPT we need another register to use STR:

    ; Load the address of 'var' into r1 (or any free register).
    LDR   r1, =var      ; r1 now has the address of var

    ; Store r0 (32 bits) into [r1].
    STR   r0, [r1]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this code the register used as base register is inside x_register memory object operator. It is defined at creation using one of the available constructors of class arm::Mem

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the steps anyway. The return of getCARRef is type arm:mem and x_register is type arm:mem. Can we just directly put the return of getCARRef in x_register?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails because getXReg adds an offset and we want to use a scale and an index register. We cannot use everything.
A correct initialization would be auto x_register = arm::Mem(scheduler_registers);


/* We bail at MAX_REG-1 rather than MAX_REG as the highest register
* is reserved for the loader. */
a.add(ARG3, ARG3, imm(1));
a.cmp(ARG3, imm(MAX_REG - 1));
a.b_lo(unpack_next);
}

a.mov(ARG1, imm(SYSTEM_LIMIT));
a.b(raise_error);

a.bind(malformed_list);
a.mov(ARG1, imm(BADARG));

a.bind(raise_error);
{
static const ErtsCodeMFA apply_mfa = {am_erlang, am_apply, 2};

a.str(ARG4, getXRef(0));
a.str(ARG2, getXRef(1));

a.str(ARG1, arm::Mem(c_p, offsetof(Process, freason)));
mov_imm(ARG4, &apply_mfa);
a.b(labels[raise_exception]);
}
}

a.bind(finished);
{
/* Make the lower 16 bits of ARG3 equal those of the header word of all
* funs with the same arity. */
a.lsl(ARG3, ARG3, imm(FUN_HEADER_ARITY_OFFS));
a.add(ARG3, ARG3, imm(FUN_SUBTAG));

a.bx(a32::lr);
}
}

void BeamModuleAssembler::emit_i_apply_fun() {
Expand Down