Skip to content

Commit 232d512

Browse files
committed
[Mono] added arm64 SwiftError support to interpreter
1 parent bad00cf commit 232d512

File tree

6 files changed

+91
-4
lines changed

6 files changed

+91
-4
lines changed

src/mono/mono/mini/interp/interp-internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ struct InterpMethod {
155155
MonoProfilerCallInstrumentationFlags prof_flags;
156156
InterpMethodCodeType code_type;
157157
int ref_slot_offset; // GC visible pointer slot
158+
int swift_error_offset; // swift error struct
158159
MonoBitSet *ref_slots;
159160
#ifdef ENABLE_EXPERIMENT_TIERED
160161
MiniTieredCounter tiered_counter;

src/mono/mono/mini/interp/interp.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -3176,6 +3176,20 @@ interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untype
31763176
/* Copy the args saved in the trampoline to the frame stack */
31773177
gpointer retp = mono_arch_get_native_call_context_args (ccontext, &frame, sig, call_info);
31783178

3179+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
3180+
int swift_error_arg_index = -1;
3181+
gpointer swift_error_data;
3182+
gpointer* swift_error_pointer;
3183+
if (mono_method_signature_has_ext_callconv (sig, MONO_EXT_CALLCONV_SWIFTCALL)) {
3184+
swift_error_data = mono_arch_get_swift_error (ccontext, sig, &swift_error_arg_index);
3185+
3186+
int swift_error_offset = frame.imethod->swift_error_offset;
3187+
if (swift_error_offset >= 0) {
3188+
swift_error_pointer = (gpointer*)((guchar*)frame.stack + swift_error_offset);
3189+
}
3190+
}
3191+
#endif
3192+
31793193
/* Allocate storage for value types */
31803194
stackval *newsp = sp;
31813195
/* FIXME we should reuse computation on imethod for this */
@@ -3195,6 +3209,11 @@ interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untype
31953209
} else {
31963210
size = MINT_STACK_SLOT_SIZE;
31973211
}
3212+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
3213+
if (swift_error_arg_index >= 0 && swift_error_arg_index == i) {
3214+
newsp->data.p = swift_error_pointer;
3215+
}
3216+
#endif
31983217
newsp = STACK_ADD_BYTES (newsp, size);
31993218
}
32003219
newsp = (stackval*)ALIGN_TO (newsp, MINT_STACK_ALIGNMENT);
@@ -3205,6 +3224,12 @@ interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untype
32053224
mono_interp_exec_method (&frame, context, NULL);
32063225
MONO_EXIT_GC_UNSAFE;
32073226

3227+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
3228+
if (swift_error_arg_index >= 0) {
3229+
*(gpointer*)swift_error_data = *(gpointer*)swift_error_pointer;
3230+
}
3231+
#endif
3232+
32083233
context->stack_pointer = (guchar*)sp;
32093234
g_assert (!context->has_resume_state);
32103235

@@ -3459,7 +3484,7 @@ interp_create_method_pointer (MonoMethod *method, gboolean compile, MonoError *e
34593484
return (gpointer)no_llvmonly_interp_method_pointer;
34603485
}
34613486

3462-
#ifndef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
3487+
#if !defined(MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE) && !defined(MONO_ARCH_HAVE_SWIFTCALL)
34633488
/*
34643489
* Interp in wrappers get the argument in the rgctx register. If
34653490
* MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE is defined it means that

src/mono/mono/mini/interp/transform.c

+37
Original file line numberDiff line numberDiff line change
@@ -4417,6 +4417,43 @@ interp_method_compute_offsets (TransformData *td, InterpMethod *imethod, MonoMet
44174417
td->il_locals_size = offset - td->il_locals_offset;
44184418
td->total_locals_size = offset;
44194419

4420+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
4421+
// Allocate SwiftError
4422+
if (mono_method_signature_has_ext_callconv (sig, MONO_EXT_CALLCONV_SWIFTCALL)) {
4423+
imethod->swift_error_offset = -1;
4424+
4425+
MonoClass *swift_error = mono_class_try_get_swift_error_class ();
4426+
MonoClass *swift_error_ptr = mono_class_create_ptr (m_class_get_this_arg (swift_error));
4427+
4428+
int swift_error_index = -1;
4429+
for (int i = 0; i < sig->param_count; i++) {
4430+
MonoClass *klass = mono_class_from_mono_type_internal (sig->params [i]);
4431+
if (klass == swift_error_ptr) {
4432+
swift_error_index = i;
4433+
break;
4434+
}
4435+
}
4436+
4437+
if (swift_error_index >= 0)
4438+
{
4439+
MonoType* type = mono_method_signature_internal (td->method)->params [swift_error_index - sig->hasthis];
4440+
int index = num_args + num_il_locals;
4441+
int mt = mono_mint_type (type);
4442+
4443+
td->vars [index].type = type;
4444+
td->vars [index].global = TRUE;
4445+
td->vars [index].offset = offset;
4446+
size = mono_interp_type_size (type, mt, &align);
4447+
td->vars [index].size = size;
4448+
interp_mark_ref_slots_for_var (td, index);
4449+
4450+
offset += size;
4451+
offset = ALIGN_TO (offset, MINT_STACK_ALIGNMENT);
4452+
imethod->swift_error_offset = td->vars [index].offset;
4453+
}
4454+
}
4455+
#endif
4456+
44204457
imethod->clause_data_offsets = (guint32*)g_malloc (header->num_clauses * sizeof (guint32));
44214458
td->clause_vars = (int*)mono_mempool_alloc (td->mempool, sizeof (int) * header->num_clauses);
44224459
for (guint i = 0; i < header->num_clauses; i++) {

src/mono/mono/mini/mini-arm64.c

+14
Original file line numberDiff line numberDiff line change
@@ -2081,10 +2081,24 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo
20812081
storage = alloca (temp_size);
20822082
else
20832083
storage = arg_get_storage (ccontext, ainfo);
2084+
2085+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
2086+
int swift_error_preserved_val = 0;
2087+
if (ccontext->gregs [PARAM_REGS + 2]) {
2088+
swift_error_preserved_val = ccontext->gregs [PARAM_REGS + 2];
2089+
}
2090+
#endif
2091+
20842092
memset (ccontext, 0, sizeof (CallContext)); // FIXME
20852093
interp_cb->frame_arg_to_data ((MonoInterpFrameHandle)frame, sig, -1, storage);
20862094
if (temp_size)
20872095
arg_set_val (ccontext, ainfo, storage);
2096+
2097+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
2098+
if (swift_error_preserved_val > 0) {
2099+
ccontext->gregs [PARAM_REGS + 2] = swift_error_preserved_val;
2100+
}
2101+
#endif
20882102
}
20892103
}
20902104

src/mono/mono/mini/tramp-arm64.c

+13
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,13 @@ mono_arch_get_native_to_interp_trampoline (MonoTrampInfo **info)
846846
for (i = 0; i < FP_PARAM_REGS; i++)
847847
arm_strfpx (code, i, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, fregs) + i * sizeof (double));
848848

849+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
850+
/* set context registers to CallContext */
851+
for (i = 0; i < CTX_REGS; i++) {
852+
arm_strx (code, i + CTX_REGS_OFFSET, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, gregs) + (i + PARAM_REGS + 1) * sizeof (host_mgreg_t));
853+
}
854+
#endif
855+
849856
/* set the stack pointer to the value at call site */
850857
arm_addx_imm (code, ARMREG_R0, ARMREG_FP, framesize);
851858
arm_strp (code, ARMREG_R0, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, stack));
@@ -863,6 +870,12 @@ mono_arch_get_native_to_interp_trampoline (MonoTrampInfo **info)
863870
for (i = 0; i < FP_PARAM_REGS; i++)
864871
arm_ldrfpx (code, i, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, fregs) + i * sizeof (double));
865872

873+
#ifdef MONO_ARCH_HAVE_SWIFTCALL
874+
/* set the context registers from CallContext */
875+
for (i = 0; i < CTX_REGS; i++) {
876+
arm_ldrx (code, i + CTX_REGS_OFFSET, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, gregs) + (i + PARAM_REGS + 1) * sizeof (host_mgreg_t));
877+
}
878+
#endif
866879
/* reset stack and return */
867880
arm_ldpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
868881
arm_addx_imm (code, ARMREG_SP, ARMREG_SP, framesize);

src/tests/issues.targets

-3
Original file line numberDiff line numberDiff line change
@@ -2145,9 +2145,6 @@
21452145
<ExcludeList Include = "$(XUnitTestBinBase)/JIT/Directed/Arrays/nintindexoutofrange/**">
21462146
<Issue>https://github.com/dotnet/runtime/issues/71656</Issue>
21472147
</ExcludeList>
2148-
<ExcludeList Include="$(XunitTestBinBase)/Interop/Swift/SwiftErrorHandling/**">
2149-
<Issue>Reverse P/Invokes not supported yet</Issue>
2150-
</ExcludeList>
21512148
<!-- End interpreter issues -->
21522149
</ItemGroup>
21532150

0 commit comments

Comments
 (0)