Skip to content

Commit 1e7fe71

Browse files
[WASM] Support SE-0283 for WebAssembly
Ref: swiftlang#28833
1 parent 62e3c93 commit 1e7fe71

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

stdlib/public/runtime/BuiltinProtocolConformances.cpp

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ using StaticInfixWitness = SWIFT_CC(swift) bool(OpaqueValue *, OpaqueValue *,
3333
#define INDIRECT_RELREF_GOTPCREL(SYMBOL) SYMBOL "@GOTPCREL + 1"
3434
#endif
3535

36+
#if defined(__wasm__)
37+
// Wasm doesn't support pc relative relocation, so use absolute ptr
38+
#define INDIRECT_RELREF_GOTPCREL(SYMBOL) SYMBOL
39+
#endif
40+
3641
// MachO indirect symbol references.
3742
#if defined(__MACH__)
3843

@@ -105,6 +110,16 @@ __asm(
105110
" .lcomm __swift_tupleEquatable_private, 128, 16\n"
106111
" .section .rdata, \"dr\"\n"
107112
#pragma comment(linker, "/EXPORT:_swift_tupleEquatable_conf,DATA")
113+
#elif defined(__wasm__)
114+
" .section .rodata,\"\",@\n"
115+
"__swift_tupleEquatable_private:\n"
116+
" .type __swift_tupleEquatable_private, @object\n"
117+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
118+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
119+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
120+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
121+
" .size __swift_tupleEquatable_private, 128\n"
122+
" .type " TUPLE_EQUATABLE_CONF ", @object\n"
108123
#endif
109124
" .globl " TUPLE_EQUATABLE_CONF "\n"
110125
" .p2align 2\n"
@@ -128,7 +143,11 @@ __asm(
128143
// Equatable == method descriptor.
129144
" .long " INDIRECT_RELREF_GOTPCREL(EQUATABLE_EE_METHOD_DESCRIPTOR) "\n"
130145
// This is a direct relative reference to the equals witness defined below.
146+
#if defined(__wasm__)
147+
" .long (" TUPLE_EQUATABLE_EQUALS ")\n"
148+
#else
131149
" .long (" TUPLE_EQUATABLE_EQUALS ") - .\n"
150+
#endif
132151
// The witness table size in words.
133152
" .short 0\n"
134153
// The witness table private size in words & requires instantiation.
@@ -137,8 +156,12 @@ __asm(
137156
" .long 0\n"
138157
// This is a direct relative reference to the private data for the
139158
// conformance.
159+
#if defined(__wasm__)
160+
" .long __swift_tupleEquatable_private\n"
161+
#else
140162
" .long __swift_tupleEquatable_private - .\n"
141-
#if defined(__ELF__)
163+
#endif
164+
#if defined(__ELF__) || defined(__wasm__)
142165
" .size " TUPLE_EQUATABLE_CONF ", 40\n"
143166
#endif
144167
);
@@ -236,17 +259,23 @@ __asm(
236259
#elif defined(_WIN32)
237260
" .section .sw5tyrf$B, \"dr\", discard, \"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\"\n"
238261
" .globl \"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\"\n"
262+
#elif defined(__wasm__)
263+
" .section .rodata.__swift5_typeref,\"\",@\n"
239264
#endif
240265
" .p2align 1\n"
241266
"\"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\":\n"
242267
" .byte 255\n"
243268
" .byte 7\n"
244269
// This is a direct relative reference to the base accessor for Equatable
245270
// defined below.
271+
#if defined(__wasm__)
272+
" .long (" TUPLE_COMPARABLE_BASEACCESSOREQUATABLE ")\n"
273+
#else
246274
" .long (" TUPLE_COMPARABLE_BASEACCESSOREQUATABLE ") - .\n"
275+
#endif
247276
// This 0 is our null terminator.
248277
" .byte 0\n"
249-
#if defined (__ELF__)
278+
#if defined (__ELF__) || defined(__wasm__)
250279
" .size \"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\", 7\n"
251280
#endif
252281
);
@@ -268,6 +297,16 @@ __asm(
268297
" .lcomm __swift_tupleComparable_private, 128, 16\n"
269298
" .section .rdata, \"dr\"\n"
270299
#pragma comment(linker, "/EXPORT:_swift_tupleComparable_conf,DATA")
300+
#elif defined(__wasm__)
301+
" .section .rodata,\"\",@\n"
302+
"__swift_tupleComparable_private:\n"
303+
" .type __swift_tupleComparable_private, @object\n"
304+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
305+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
306+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
307+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
308+
" .size __swift_tupleComparable_private, 128\n"
309+
" .type " TUPLE_COMPARABLE_CONF ", @object\n"
271310
#endif
272311
" .globl " TUPLE_COMPARABLE_CONF "\n"
273312
" .p2align 2\n"
@@ -293,30 +332,50 @@ __asm(
293332
// This is a direct relative reference to the associated conformance for
294333
// Equatable defined above in assembly. NOTE: We + 1 here because the
295334
// associated conformance structure is 1 aligned.
335+
#if defined(__wasm__)
336+
" .long (\"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\") + 1\n"
337+
#else
296338
" .long (\"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\") - . + 1\n"
339+
#endif
297340
// This is an indirectable relative reference to the GOT entry for the
298341
// Comparable.< method descriptor.
299342
" .long " INDIRECT_RELREF_GOTPCREL(COMPARABLE_LT_METHOD_DESCRIPTOR) "\n"
300343
// This is a direct relative reference to the less than witness defined below.
344+
#if defined(__wasm__)
345+
" .long (" TUPLE_COMPARABLE_LESSTHAN ")\n"
346+
#else
301347
" .long (" TUPLE_COMPARABLE_LESSTHAN ") - .\n"
348+
#endif
302349
// This is an indirectable relative reference to the GOT entry for the
303350
// Comparable.<= method descriptor.
304351
" .long " INDIRECT_RELREF_GOTPCREL(COMPARBALE_LTE_METHOD_DESCRIPTOR) "\n"
305352
// This is a direct relative reference to the less than or equal witness
306353
// defined below.
354+
#if defined(__wasm__)
355+
" .long (" TUPLE_COMPARABLE_LESSTHANOREQUAL ")\n"
356+
#else
307357
" .long (" TUPLE_COMPARABLE_LESSTHANOREQUAL ") - .\n"
358+
#endif
308359
// This is an indirectable relative reference to the GOT entry for the
309360
// Comparable.>= method descriptor.
310361
" .long " INDIRECT_RELREF_GOTPCREL(COMPARABLE_GTE_METHOD_DESCRIPTOR) "\n"
311362
// This is a direct relative reference to the greater than or equal witness
312363
// defined below.
364+
#if defined(__wasm__)
365+
" .long (" TUPLE_COMPARABLE_GREATERTHANOREQUAL ")\n"
366+
#else
313367
" .long (" TUPLE_COMPARABLE_GREATERTHANOREQUAL ") - .\n"
368+
#endif
314369
// This is an indirectable relative reference to the GOT entry for the
315370
// Comparable.> method descriptor.
316371
" .long " INDIRECT_RELREF_GOTPCREL(COMPARABLE_GT_METHOD_DESCRIPTOR) "\n"
317372
// This is a direct relative reference to the greater than witness defined
318373
// below.
374+
#if defined(__wasm__)
375+
" .long (" TUPLE_COMPARABLE_GREATERTHAN ")\n"
376+
#else
319377
" .long (" TUPLE_COMPARABLE_GREATERTHAN ") - .\n"
378+
#endif
320379
// The witness table size in words.
321380
" .short 0\n"
322381
// The witness table private size in words & requires instantiation.
@@ -325,8 +384,12 @@ __asm(
325384
" .long 0\n"
326385
// This is a direct relative reference to the private data for the
327386
// conformance.
387+
#if defined(__wasm__)
388+
" .long __swift_tupleComparable_private\n"
389+
#else
328390
" .long __swift_tupleComparable_private - .\n"
329-
#if defined(__ELF__)
391+
#endif
392+
#if defined(__ELF__) || defined(__wasm__)
330393
" .size " TUPLE_COMPARABLE_CONF ", 72\n"
331394
#endif
332395
);
@@ -634,6 +697,16 @@ __asm(
634697
" .lcomm __swift_tupleHashable_private, 128, 16\n"
635698
" .section .rdata, \"dr\"\n"
636699
#pragma comment(linker, "/EXPORT:_swift_tupleHashable_conf,DATA")
700+
#elif defined(__wasm__)
701+
" .section .rodata,\"\",@\n"
702+
"__swift_tupleHashable_private:\n"
703+
" .type __swift_tupleHashable_private, @object\n"
704+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
705+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
706+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
707+
" .8byte 0\n .8byte 0\n .8byte 0\n .8byte 0\n"
708+
" .size __swift_tupleHashable_private, 128\n"
709+
" .type " TUPLE_HASHABLE_CONF ", @object\n"
637710
#endif
638711
" .globl " TUPLE_HASHABLE_CONF "\n"
639712
" .p2align 2\n"
@@ -663,17 +736,29 @@ __asm(
663736
// from its elements whose witness table is located in the same place for both
664737
// protocols. NOTE: We + 1 here because the associated conformance
665738
// structure is 1 aligned.
739+
#if defined(__wasm__)
740+
" .long (\"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\") + 1\n"
741+
#else
666742
" .long (\"" TUPLE_COMPARABLE_ASSOCIATEDCONFORMANCE "\") - . + 1\n"
743+
#endif
667744
// This is an indirectable relative reference to the GOT entry for the
668745
// Hashable.hashValue method descriptor.
669746
" .long " INDIRECT_RELREF_GOTPCREL(HASHABLE_HASHVALUE_METHOD_DESCRIPTOR) "\n"
670747
// This is a direct relative reference to the hashValue witness defined below.
748+
#if defined(__wasm__)
749+
" .long (" TUPLE_HASHABLE_HASHVALUE ")\n"
750+
#else
671751
" .long (" TUPLE_HASHABLE_HASHVALUE ") - .\n"
752+
#endif
672753
// This is an indirectable relative reference to the GOT entry for the
673754
// Hashable.hash(into:) method descriptor.
674755
" .long " INDIRECT_RELREF_GOTPCREL(HASHABLE_HASH_METHOD_DESCRIPTOR) "\n"
675756
// This is a direct relative reference to the hash(into:) witness defined below.
757+
#if defined(__wasm__)
758+
" .long (" TUPLE_HASHABLE_HASH ")\n"
759+
#else
676760
" .long (" TUPLE_HASHABLE_HASH ") - .\n"
761+
#endif
677762
// This is an indirectable relative reference to the GOT equivalent for the
678763
// Hashable._rawHashValue method descriptor.
679764
" .long " INDIRECT_RELREF_GOTPCREL(HASHABLE_RAWHASHVALUE_METHOD_DESCRIPTOR) "\n"
@@ -688,8 +773,12 @@ __asm(
688773
" .long 0\n"
689774
// This is a direct relative reference to the private data for the
690775
// conformance.
776+
#if defined(__wasm__)
777+
" .long __swift_tupleHashable_private\n"
778+
#else
691779
" .long __swift_tupleHashable_private - .\n"
692-
#if defined(__ELF__)
780+
#endif
781+
#if defined(__ELF__) || defined(__wasm__)
693782
" .size " TUPLE_HASHABLE_CONF ", 64\n"
694783
#endif
695784
);

0 commit comments

Comments
 (0)