Skip to content

Commit 093c788

Browse files
committed
Implement invokeNative asm code for armasm64 assembler on ARM64 Windows
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent a6a9f1f commit 093c788

File tree

3 files changed

+199
-2
lines changed

3 files changed

+199
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
AREA |.text|, CODE, READONLY, ALIGN=2
5+
6+
EXPORT invokeNative
7+
8+
; ------------------------ direct call path ------------------------
9+
10+
call_func
11+
mov x20, x30 ; save x30(lr)
12+
blr x19
13+
mov sp, x22 ; restore sp saved before function call
14+
15+
return_label
16+
mov x30, x20 ; restore x30(lr)
17+
ldp x19, x20, [sp, #0x20]
18+
ldp x21, x22, [sp, #0x10]
19+
ldp x23, x24, [sp, #0x0]
20+
add sp, sp, #0x30
21+
ret
22+
23+
; ------------------------ stack-args path ------------------------
24+
25+
handle_stack
26+
; Reserve aligned stack space for stack arguments and copy them
27+
mov x23, sp
28+
bic sp, x23, #15 ; Ensure 16-byte alignment
29+
lsl x23, x21, #3 ; x23 = nstacks * 8
30+
add x23, x23, #15
31+
bic x23, x23, #15
32+
sub sp, sp, x23
33+
mov x23, sp
34+
35+
copy_loop
36+
cmp x21, #0
37+
b.eq call_func ; when done, branch back to call path
38+
ldr x24, [x20], #8
39+
str x24, [x23], #8
40+
sub x21, x21, #1
41+
b copy_loop
42+
43+
; ------------------------ function entry ------------------------
44+
45+
invokeNative
46+
sub sp, sp, #0x30
47+
stp x19, x20, [sp, #0x20] ; save the registers
48+
stp x21, x22, [sp, #0x10]
49+
stp x23, x24, [sp, #0x0]
50+
51+
mov x19, x0 ; x19 = function ptr
52+
mov x20, x1 ; x20 = argv
53+
mov x21, x2 ; x21 = nstacks
54+
mov x22, sp ; save the sp before call function
55+
56+
; Fill in floating-point registers
57+
ldp d0, d1, [x20], #16
58+
ldp d2, d3, [x20], #16
59+
ldp d4, d5, [x20], #16
60+
ldp d6, d7, [x20], #16
61+
62+
; Fill integer registers
63+
ldp x0, x1, [x20], #16 ; x0 = argv[8] = exec_env, x1 = argv[9]
64+
ldp x2, x3, [x20], #16
65+
ldp x4, x5, [x20], #16
66+
ldp x6, x7, [x20], #16
67+
68+
; Now x20 points to stack args
69+
cmp x21, #0
70+
b.ne handle_stack ; backward: there are stack args
71+
b call_func ; backward: no stack args
72+
73+
END
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
AREA |.text|, CODE, READONLY, ALIGN=2
5+
6+
EXPORT invokeNative
7+
8+
; ------------------------ direct call path ------------------------
9+
10+
call_func
11+
mov x20, x30 ; save x30(lr)
12+
blr x19
13+
mov sp, x22 ; restore sp saved before function call
14+
15+
return_label
16+
mov x30, x20 ; restore x30(lr)
17+
ldp x19, x20, [sp, #0x20]
18+
ldp x21, x22, [sp, #0x10]
19+
ldp x23, x24, [sp, #0x0]
20+
add sp, sp, #0x30
21+
ret
22+
23+
; ------------------------ stack-args path ------------------------
24+
25+
handle_stack
26+
; Reserve aligned stack space for stack arguments and copy them
27+
mov x23, sp
28+
bic sp, x23, #15 ; Ensure 16-byte alignment
29+
lsl x23, x21, #3 ; x23 = nstacks * 8
30+
add x23, x23, #15
31+
bic x23, x23, #15
32+
sub sp, sp, x23
33+
mov x23, sp
34+
35+
copy_loop
36+
cmp x21, #0
37+
b.eq call_func ; when done, branch back to call path
38+
ldr x24, [x20], #8
39+
str x24, [x23], #8
40+
sub x21, x21, #1
41+
b copy_loop
42+
43+
; ------------------------ function entry ------------------------
44+
45+
invokeNative
46+
sub sp, sp, #0x30
47+
stp x19, x20, [sp, #0x20] ; save the registers
48+
stp x21, x22, [sp, #0x10]
49+
stp x23, x24, [sp, #0x0]
50+
51+
mov x19, x0 ; x19 = function ptr
52+
mov x20, x1 ; x20 = argv
53+
mov x21, x2 ; x21 = nstacks
54+
mov x22, sp ; save the sp before call function
55+
56+
; Fill in floating-point registers
57+
; v0 = argv[0], v1 = argv[1], v2 = argv[2], v3 = argv[3]
58+
ld1 {v0.2D, v1.2D, v2.2D, v3.2D}, [x20], #64
59+
; v4 = argv[4], v5 = argv[5], v6 = argv[6], v7 = argv[7]
60+
ld1 {v4.2D, v5.2D, v6.2D, v7.2D}, [x20], #64
61+
62+
; Fill integer registers
63+
ldp x0, x1, [x20], #16 ; x0 = argv[8] = exec_env, x1 = argv[9]
64+
ldp x2, x3, [x20], #16
65+
ldp x4, x5, [x20], #16
66+
ldp x6, x7, [x20], #16
67+
68+
; Now x20 points to stack args
69+
cmp x21, #0
70+
b.ne handle_stack ; (backward) there are stack args
71+
b call_func ; (backward) no stack args
72+
73+
END

core/iwasm/common/iwasm_common.cmake

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
set (IWASM_COMMON_DIR ${CMAKE_CURRENT_LIST_DIR})
55

66
include_directories (${IWASM_COMMON_DIR})
7+
if (MSVC AND WAMR_BUILD_PLATFORM STREQUAL "windows" AND WAMR_BUILD_TARGET MATCHES "AARCH64.*")
8+
if (DEFINED ENV{VCToolsInstallDir})
9+
set(CMAKE_ASM_MASM_COMPILER
10+
"$ENV{VCToolsInstallDir}/bin/HostARM64/arm64/armasm64.exe"
11+
CACHE FILEPATH "" FORCE)
12+
set(CMAKE_ASM_MASM_COMPILE_OBJECT
13+
"<CMAKE_ASM_MASM_COMPILER> /nologo -o <OBJECT> <SOURCE>"
14+
CACHE STRING "" FORCE)
15+
set(_WAMR_ARM64_MASM_SOURCES
16+
${IWASM_COMMON_DIR}/arch/invokeNative_armarm64.asm
17+
${IWASM_COMMON_DIR}/arch/invokeNative_armarm64_simd.asm)
18+
foreach(_s IN LISTS _WAMR_ARM64_MASM_SOURCES)
19+
if (EXISTS "${_s}")
20+
set_source_files_properties("${_s}" PROPERTIES
21+
LANGUAGE ASM_MASM
22+
COMPILE_OPTIONS "/nologo"
23+
)
24+
endif()
25+
endforeach()
26+
else()
27+
message(FATAL_ERROR "VCToolsInstallDir is not defined. Please run from a Developer Command Prompt or specify armasm64.exe manually.")
28+
endif()
29+
endif()
730

831
add_definitions(-DBH_MALLOC=wasm_runtime_malloc)
932
add_definitions(-DBH_FREE=wasm_runtime_free)
@@ -79,9 +102,37 @@ elseif (WAMR_BUILD_TARGET MATCHES "THUMB.*")
79102
endif ()
80103
elseif (WAMR_BUILD_TARGET MATCHES "AARCH64.*")
81104
if (NOT WAMR_BUILD_SIMD EQUAL 1)
82-
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64.s)
105+
if (WAMR_BUILD_PLATFORM STREQUAL "windows")
106+
if (MSVC)
107+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64.asm)
108+
set(_WAMR_ARM64_MASM_SOURCES ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64.asm)
109+
set_source_files_properties(${_WAMR_ARM64_MASM_SOURCES}
110+
PROPERTIES
111+
LANGUAGE ASM_MASM
112+
COMPILE_DEFINITIONS ""
113+
INCLUDE_DIRECTORIES ""
114+
COMPILE_OPTIONS "/nologo"
115+
)
116+
endif ()
117+
else ()
118+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64.s)
119+
endif ()
83120
else()
84-
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64_simd.s)
121+
if (WAMR_BUILD_PLATFORM STREQUAL "windows")
122+
if (MSVC)
123+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64_simd.asm)
124+
set(_WAMR_ARM64_MASM_SOURCES_SIMD ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64_simd.asm)
125+
set_source_files_properties(${_WAMR_ARM64_MASM_SOURCES_SIMD}
126+
PROPERTIES
127+
LANGUAGE ASM_MASM
128+
COMPILE_DEFINITIONS ""
129+
INCLUDE_DIRECTORIES ""
130+
COMPILE_OPTIONS "/nologo"
131+
)
132+
endif ()
133+
else ()
134+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64_simd.s)
135+
endif ()
85136
endif()
86137
elseif (WAMR_BUILD_TARGET STREQUAL "MIPS")
87138
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_mips.s)

0 commit comments

Comments
 (0)