@@ -3711,7 +3711,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
3711
3711
swift_error_args ++ ;
3712
3712
} else if (param_klass == swift_self ) {
3713
3713
swift_self_args ++ ;
3714
- } else if (!m_class_is_blittable ( param_klass ) || m_class_is_simd_type (param_klass )) {
3714
+ } else if (!type_is_blittable ( method -> signature -> params [ i ] ) || m_class_is_simd_type (param_klass )) {
3715
3715
swift_error_args = swift_self_args = 0 ;
3716
3716
mono_error_set_generic_error (emitted_error , "System" , "InvalidProgramException" , "Passing non-blittable types to a P/Invoke with the Swift calling convention is unsupported." );
3717
3717
break ;
@@ -6693,7 +6693,8 @@ typedef enum {
6693
6693
SWIFT_DOUBLE ,
6694
6694
} SwiftPhysicalLoweringKind ;
6695
6695
6696
- static int get_swift_lowering_alignment (SwiftPhysicalLoweringKind kind ) {
6696
+ static int get_swift_lowering_alignment (SwiftPhysicalLoweringKind kind )
6697
+ {
6697
6698
switch (kind ) {
6698
6699
case SWIFT_INT64 :
6699
6700
case SWIFT_DOUBLE :
@@ -6705,7 +6706,8 @@ static int get_swift_lowering_alignment (SwiftPhysicalLoweringKind kind) {
6705
6706
}
6706
6707
}
6707
6708
6708
- static void set_lowering_range (guint8 * lowered_bytes , guint32 offset , guint32 size , SwiftPhysicalLoweringKind kind ) {
6709
+ static void set_lowering_range (guint8 * lowered_bytes , guint32 offset , guint32 size , SwiftPhysicalLoweringKind kind )
6710
+ {
6709
6711
bool force_opaque = false;
6710
6712
6711
6713
if (offset != ALIGN_TO (offset , get_swift_lowering_alignment (kind ))) {
@@ -6736,7 +6738,8 @@ static void set_lowering_range(guint8* lowered_bytes, guint32 offset, guint32 si
6736
6738
6737
6739
static void record_struct_field_physical_lowering (guint8 * lowered_bytes , MonoType * type , guint32 offset );
6738
6740
6739
- static void record_inlinearray_struct_physical_lowering (guint8 * lowered_bytes , MonoClass * klass , guint32 offset ) {
6741
+ static void record_inlinearray_struct_physical_lowering (guint8 * lowered_bytes , MonoClass * klass , guint32 offset )
6742
+ {
6740
6743
// Get the first field and record its physical lowering N times
6741
6744
MonoClassField * field = mono_class_get_fields_internal (klass , NULL );
6742
6745
MonoType * fieldType = field -> type ;
@@ -6755,17 +6758,21 @@ static void record_struct_physical_lowering (guint8* lowered_bytes, MonoClass* k
6755
6758
// For each field, we need to record the physical lowering of it.
6756
6759
gpointer iter = NULL ;
6757
6760
MonoClassField * field ;
6761
+ int type_offset = MONO_ABI_SIZEOF (MonoObject );
6758
6762
while ((field = mono_class_get_fields_internal (klass , & iter ))) {
6759
6763
if (field -> type -> attrs & FIELD_ATTRIBUTE_STATIC )
6760
6764
continue ;
6761
6765
if (mono_field_is_deleted (field ))
6762
6766
continue ;
6763
6767
6764
- record_struct_field_physical_lowering (lowered_bytes , field -> type , offset + m_field_get_offset (field ));
6768
+ record_struct_field_physical_lowering (lowered_bytes , field -> type , ( offset + m_field_get_offset (field )) - type_offset );
6765
6769
}
6766
6770
}
6767
6771
6768
- static void record_struct_field_physical_lowering (guint8 * lowered_bytes , MonoType * type , guint32 offset ) {
6772
+ static void record_struct_field_physical_lowering (guint8 * lowered_bytes , MonoType * type , guint32 offset )
6773
+ {
6774
+ int align ;
6775
+
6769
6776
// Normalize pointer types to IntPtr and resolve generic classes.
6770
6777
// We don't need to care about specific pointer types at this ABI level.
6771
6778
if (type -> type == MONO_TYPE_PTR || type -> type == MONO_TYPE_FNPTR ) {
@@ -6793,7 +6800,7 @@ static void record_struct_field_physical_lowering (guint8* lowered_bytes, MonoTy
6793
6800
kind = SWIFT_DOUBLE ;
6794
6801
}
6795
6802
6796
- set_lowering_range (lowered_bytes , offset , mono_type_size (type , NULL ), kind );
6803
+ set_lowering_range (lowered_bytes , offset , mono_type_size (type , & align ), kind );
6797
6804
}
6798
6805
}
6799
6806
@@ -6820,13 +6827,13 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
6820
6827
}
6821
6828
6822
6829
MonoClass * klass = mono_class_from_mono_type_internal (type );
6823
-
6830
+ int vtype_size = mono_class_value_size ( klass , NULL );
6824
6831
// TODO: We currently don't support vector types, so we can say that the maximum size of a non-by_reference struct
6825
6832
// is 4 * PointerSize.
6826
6833
// Strictly, this is inaccurate in the case where a struct has a fully-empty 8 bytes of padding using explicit layout,
6827
6834
// but that's not possible in the Swift layout algorithm.
6828
6835
6829
- if (m_class_get_instance_size ( klass ) > 4 * TARGET_SIZEOF_VOID_P ) {
6836
+ if (vtype_size > 4 * TARGET_SIZEOF_VOID_P ) {
6830
6837
lowering .by_reference = TRUE;
6831
6838
return lowering ;
6832
6839
}
@@ -6845,8 +6852,7 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
6845
6852
GArray * intervals = g_array_new (FALSE, TRUE, sizeof (struct _SwiftInterval ));
6846
6853
6847
6854
// Now we'll build the intervals from the lowered_bytes array
6848
- int instance_size = m_class_get_instance_size (klass );
6849
- for (int i = 0 ; i < instance_size ; ++ i ) {
6855
+ for (int i = 0 ; i < vtype_size ; ++ i ) {
6850
6856
// Don't create an interval for empty bytes
6851
6857
if (lowered_bytes [i ] == SWIFT_EMPTY ) {
6852
6858
continue ;
@@ -6874,19 +6880,23 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
6874
6880
}
6875
6881
6876
6882
// Merge opaque intervals that are in the same pointer-sized block
6877
- for (int i = 0 ; i < intervals -> len - 1 ; ++ i ) {
6878
- struct _SwiftInterval current = g_array_index (intervals , struct _SwiftInterval , i );
6879
- struct _SwiftInterval next = g_array_index (intervals , struct _SwiftInterval , i + 1 );
6880
-
6881
- if (current .kind == SWIFT_OPAQUE && next .kind == SWIFT_OPAQUE && current .start / TARGET_SIZEOF_VOID_P == next .start / TARGET_SIZEOF_VOID_P ) {
6882
- current .size = next .start + next .size - current .start ;
6883
- g_array_remove_index (intervals , i + 1 );
6884
- i -- ;
6883
+ for (int i = 0 ; i < intervals -> len ; ++ i ) {
6884
+ struct _SwiftInterval interval = g_array_index (intervals , struct _SwiftInterval , i );
6885
+
6886
+ if (i != 0 && interval .kind == SWIFT_OPAQUE ) {
6887
+ // Merge two opaque intervals when the previous interval ends in the same pointer-sized block
6888
+ struct _SwiftInterval prevInterval = g_array_index (intervals , struct _SwiftInterval , i - 1 );
6889
+ if (prevInterval .kind == SWIFT_OPAQUE && (prevInterval .start + prevInterval .size ) / TARGET_SIZEOF_VOID_P == interval .start / TARGET_SIZEOF_VOID_P ) {
6890
+ (g_array_index (intervals , struct _SwiftInterval , i - 1 )).size = interval .start + interval .size - prevInterval .start ;
6891
+ g_array_remove_index (intervals , i );
6892
+ -- i ;
6893
+ continue ;
6894
+ }
6885
6895
}
6886
6896
}
6887
6897
6888
6898
// Now that we have the intervals, we can calculate the lowering
6889
- MonoTypeEnum lowered_types [4 ];
6899
+ MonoType * lowered_types [4 ];
6890
6900
guint32 offsets [4 ];
6891
6901
guint32 num_lowered_types = 0 ;
6892
6902
@@ -6904,13 +6914,13 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
6904
6914
6905
6915
switch (interval .kind ) {
6906
6916
case SWIFT_INT64 :
6907
- lowered_types [num_lowered_types ++ ] = MONO_TYPE_I8 ;
6917
+ lowered_types [num_lowered_types ++ ] = m_class_get_byval_arg ( mono_defaults . int64_class ) ;
6908
6918
break ;
6909
6919
case SWIFT_FLOAT :
6910
- lowered_types [num_lowered_types ++ ] = MONO_TYPE_R4 ;
6920
+ lowered_types [num_lowered_types ++ ] = m_class_get_byval_arg ( mono_defaults . single_class ) ;
6911
6921
break ;
6912
6922
case SWIFT_DOUBLE :
6913
- lowered_types [num_lowered_types ++ ] = MONO_TYPE_R8 ;
6923
+ lowered_types [num_lowered_types ++ ] = m_class_get_byval_arg ( mono_defaults . double_class ) ;
6914
6924
break ;
6915
6925
case SWIFT_OPAQUE :
6916
6926
{
@@ -6943,20 +6953,20 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
6943
6953
6944
6954
offsets [num_lowered_types ] = opaque_interval_start ;
6945
6955
6946
- if (remaining_interval_size > 8 && (opaque_interval_start % 8 == 0 )) {
6947
- lowered_types [num_lowered_types ] = MONO_TYPE_I8 ;
6956
+ if (remaining_interval_size > 4 && (opaque_interval_start % 8 == 0 )) {
6957
+ lowered_types [num_lowered_types ] = m_class_get_byval_arg ( mono_defaults . int64_class ) ;
6948
6958
remaining_interval_size -= 8 ;
6949
6959
opaque_interval_start += 8 ;
6950
- } else if (remaining_interval_size > 4 && (opaque_interval_start % 4 == 0 )) {
6951
- lowered_types [num_lowered_types ] = MONO_TYPE_I4 ;
6960
+ } else if (remaining_interval_size > 2 && (opaque_interval_start % 4 == 0 )) {
6961
+ lowered_types [num_lowered_types ] = m_class_get_byval_arg ( mono_defaults . int32_class ) ;
6952
6962
remaining_interval_size -= 4 ;
6953
6963
opaque_interval_start += 4 ;
6954
- } else if (remaining_interval_size > 2 && (opaque_interval_start % 2 == 0 )) {
6955
- lowered_types [num_lowered_types ] = MONO_TYPE_I2 ;
6964
+ } else if (remaining_interval_size > 1 && (opaque_interval_start % 2 == 0 )) {
6965
+ lowered_types [num_lowered_types ] = m_class_get_byval_arg ( mono_defaults . int16_class ) ;
6956
6966
remaining_interval_size -= 2 ;
6957
6967
opaque_interval_start += 2 ;
6958
6968
} else {
6959
- lowered_types [num_lowered_types ] = MONO_TYPE_U1 ;
6969
+ lowered_types [num_lowered_types ] = m_class_get_byval_arg ( mono_defaults . byte_class ) ;
6960
6970
remaining_interval_size -= 1 ;
6961
6971
opaque_interval_start += 1 ;
6962
6972
}
@@ -6967,7 +6977,7 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
6967
6977
}
6968
6978
}
6969
6979
6970
- memcpy (lowering .lowered_elements , lowered_types , num_lowered_types * sizeof (MonoTypeEnum ));
6980
+ memcpy (lowering .lowered_elements , lowered_types , num_lowered_types * sizeof (MonoType * ));
6971
6981
memcpy (lowering .offsets , offsets , num_lowered_types * sizeof (guint32 ));
6972
6982
lowering .num_lowered_elements = num_lowered_types ;
6973
6983
lowering .by_reference = FALSE;
0 commit comments