Skip to content

Commit

Permalink
Cleanup asm a bit
Browse files Browse the repository at this point in the history
* Use a shared header file to deduplicate some directives
* Guarantee hidden visibility for functions
* Enable gc-sections on macOS x86_64
* Add `.type` annotations for ARM
  • Loading branch information
alexcrichton committed Feb 18, 2021
1 parent d8afceb commit b8871b3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 51 deletions.
18 changes: 11 additions & 7 deletions crates/fiber/src/arch/aarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
// Also at this time this file is heavily based off the x86_64 file, so you'll
// probably want to read that one as well.

#define GLOBL(fnname) .globl fnname
#define TYPE(fnname) .type fnname,@function
#define FUNCTION(fnname) fnname
#define SIZE(fnname) .size fnname,.-fnname
#include "header.h"

// fn(top_of_stack(%x0): *mut u8)
HIDDEN(wasmtime_fiber_switch)
GLOBL(wasmtime_fiber_switch)
.p2align 2
TYPE(wasmtime_fiber_switch)
Expand Down Expand Up @@ -58,11 +56,12 @@ SIZE(wasmtime_fiber_switch)
// entry_point(%x1): extern fn(*mut u8, *mut u8),
// entry_arg0(%x2): *mut u8,
// )
HIDDEN(wasmtime_fiber_init)
GLOBL(wasmtime_fiber_init)
.p2align 2
TYPE(wasmtime_fiber_init)
FUNCTION(wasmtime_fiber_init):
adr x8, wasmtime_fiber_start
adr x8, FUNCTION(wasmtime_fiber_start)
stp x0, x8, [x0, -0x28] // x0 => x19, x8 => lr
stp x2, x1, [x0, -0x38] // x1 => x20, x2 => x21

Expand Down Expand Up @@ -114,5 +113,10 @@ FUNCTION(wasmtime_fiber_start):
.cfi_endproc
SIZE(wasmtime_fiber_start)

.section .note.GNU-stack,"",%progbits

// This omits the `.subsections_via_symbols` directive on macOS which means we
// can't GC specific intrinsics from this file, but it enables usage of the
// `adr` instruction above in lieu of figuring out a slightly more complicated
// way of implementing that.
#ifndef CFG_TARGET_OS_macos
FOOTER
#endif
20 changes: 12 additions & 8 deletions crates/fiber/src/arch/arm.S
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
// Also at this time this file is heavily based off the x86_64 file, so you'll
// probably want to read that one as well.

#define SIZE(fnname) .size fnname,.-fnname
#include "header.h"

// fn(top_of_stack(%r0): *mut u8)
.globl wasmtime_fiber_switch
wasmtime_fiber_switch:
HIDDEN(wasmtime_fiber_switch)
GLOBL(wasmtime_fiber_switch)
TYPE(wasmtime_fiber_switch)
FUNCTION(wasmtime_fiber_switch):
// Save callee-saved registers
push {r4-r11,lr}

Expand All @@ -31,9 +33,11 @@ SIZE(wasmtime_fiber_switch)
// entry_point(%r1): extern fn(*mut u8, *mut u8),
// entry_arg0(%r2): *mut u8,
// )
.globl wasmtime_fiber_init
wasmtime_fiber_init:
adr r3, wasmtime_fiber_start
HIDDEN(wasmtime_fiber_init)
GLOBL(wasmtime_fiber_init)
TYPE(wasmtime_fiber_init)
FUNCTION(wasmtime_fiber_init):
adr r3, FUNCTION(wasmtime_fiber_start)
str r3, [r0, #-0x0c] // => lr
str r0, [r0, #-0x10] // => r11
str r1, [r0, #-0x14] // => r10
Expand All @@ -44,7 +48,7 @@ wasmtime_fiber_init:
bx lr
SIZE(wasmtime_fiber_init)

wasmtime_fiber_start:
FUNCTION(wasmtime_fiber_start):
.cfi_startproc simple
// See the x86_64 file for more commentary on what these CFI directives are
// doing. Like over there note that the relative offsets to registers here
Expand Down Expand Up @@ -76,4 +80,4 @@ wasmtime_fiber_start:
.cfi_endproc
SIZE(wasmtime_fiber_start)

.section .note.GNU-stack,"",%progbits
FOOTER
36 changes: 36 additions & 0 deletions crates/fiber/src/arch/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __wasmtime_common_h
#define __wasmtime_common_h

#if CFG_TARGET_OS_macos

.section __TEXT,__text,regular,pure_instructions

#define GLOBL(fnname) .globl _##fnname
#define HIDDEN(fnname) .private_extern _##fnname
#define TYPE(fnname)
#define FUNCTION(fnname) _##fnname
#define SIZE(fnname)

// Tells the linker it's safe to gc symbols away if not used.
#define FOOTER .subsections_via_symbols

#else

.text

#define GLOBL(fnname) .globl fnname
#define HIDDEN(fnname) .hidden fnname
#ifdef CFG_TARGET_ARCH_arm
#define TYPE(fnname) .type fnname,%function
#else
#define TYPE(fnname) .type fnname,@function
#endif
#define FUNCTION(fnname) fnname
#define SIZE(fnname) .size fnname,.-fnname

// Mark that we don't need executable stack.
#define FOOTER .section .note.GNU-stack,"",%progbits

#endif

#endif // __wasmtime_common_h
28 changes: 13 additions & 15 deletions crates/fiber/src/arch/x86.S
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
// different so the reserved space at the top of the stack is 8 bytes, not 16
// bytes. Still two pointers though.

.text

#define SIZE(fnname) .size fnname,.-fnname
#include "header.h"

// fn(top_of_stack: *mut u8)
.globl wasmtime_fiber_switch
.type wasmtime_fiber_switch,@function
wasmtime_fiber_switch:
HIDDEN(wasmtime_fiber_switch)
GLOBL(wasmtime_fiber_switch)
TYPE(wasmtime_fiber_switch)
FUNCTION(wasmtime_fiber_switch):
// Load our stack-to-use
mov 0x4(%esp), %eax
mov -0x8(%eax), %ecx
Expand Down Expand Up @@ -45,9 +44,10 @@ SIZE(wasmtime_fiber_switch)
// entry_point: extern fn(*mut u8, *mut u8),
// entry_arg0: *mut u8,
// )
.globl wasmtime_fiber_init
.type wasmtime_fiber_init,@function
wasmtime_fiber_init:
HIDDEN(wasmtime_fiber_init)
GLOBL(wasmtime_fiber_init)
TYPE(wasmtime_fiber_init)
FUNCTION(wasmtime_fiber_init):
mov 4(%esp), %eax

// move top_of_stack to the 2nd argument
Expand All @@ -59,7 +59,7 @@ wasmtime_fiber_init:

// Move our start function to the return address which the `ret` in
// `wasmtime_fiber_start` will return to.
lea wasmtime_fiber_start, %ecx
lea FUNCTION(wasmtime_fiber_start), %ecx
mov %ecx, -0x14(%eax)

// And move `entry_point` to get loaded into `%ebp` through the context
Expand All @@ -82,8 +82,8 @@ wasmtime_fiber_init:
ret
SIZE(wasmtime_fiber_init)

.type wasmtime_fiber_start,@function
wasmtime_fiber_start:
TYPE(wasmtime_fiber_start)
FUNCTION(wasmtime_fiber_start):
.cfi_startproc simple
.cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \
5, /* the byte length of this expression */ \
Expand All @@ -104,6 +104,4 @@ wasmtime_fiber_start:
.cfi_endproc
SIZE(wasmtime_fiber_start)

// Mark that we don't need executable stack.
.section .note.GNU-stack,"",%progbits

FOOTER
26 changes: 5 additions & 21 deletions crates/fiber/src/arch/x86_64.S
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,10 @@
// all the other bits. Documentation tries to reference various bits here and
// there but try to make sure to read over everything before tweaking things!

.text

#if CFG_TARGET_OS_macos

#define GLOBL(fnname) .globl _##fnname
#define TYPE(fnname)
#define FUNCTION(fnname) _##fnname
#define SIZE(fnname)

#else

#define GLOBL(fnname) .globl fnname
#define TYPE(fnname) .type fnname,@function
#define FUNCTION(fnname) fnname
#define SIZE(fnname) .size fnname,.-fnname

#endif
#include "header.h"

// fn(top_of_stack(%rdi): *mut u8)
HIDDEN(wasmtime_fiber_switch)
GLOBL(wasmtime_fiber_switch)
.align 16
TYPE(wasmtime_fiber_switch)
Expand Down Expand Up @@ -63,6 +48,7 @@ SIZE(wasmtime_fiber_switch)
// entry_point(%rsi): extern fn(*mut u8, *mut u8),
// entry_arg0(%rdx): *mut u8,
// )
HIDDEN(wasmtime_fiber_init)
GLOBL(wasmtime_fiber_init)
.align 16
TYPE(wasmtime_fiber_init)
Expand Down Expand Up @@ -169,7 +155,5 @@ FUNCTION(wasmtime_fiber_start):
.cfi_endproc
SIZE(wasmtime_fiber_start)

// Mark that we don't need executable stack.
#if defined(__ELF__)
.section .note.GNU-stack,"",%progbits
#endif
FOOTER

0 comments on commit b8871b3

Please sign in to comment.