Skip to content

Commit 1b95428

Browse files
authored
Implement invokeNative asm code for armasm64 assembler on ARM64 Windows (#4636)
* Implement invokeNative asm code for armasm64 assembler on ARM64 Windows * Use more solid wrapper for armasm64 executable Signed-off-by: Hiroshi Hatake <cosmo0920.oucc@gmail.com>
1 parent 635576c commit 1b95428

File tree

3 files changed

+212
-2
lines changed

3 files changed

+212
-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: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,42 @@
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+
# Detect host tool dir
10+
set(_ARMASM64_CANDIDATES
11+
"$ENV{VCToolsInstallDir}/bin/HostX64/ARM64/armasm64.exe"
12+
"$ENV{VCToolsInstallDir}/bin/HostARM64/arm64/armasm64.exe")
13+
set(_ARMASM64_EXE "")
14+
foreach(_p IN LISTS _ARMASM64_CANDIDATES)
15+
if (EXISTS "${_p}")
16+
set(_ARMASM64_EXE "${_p}")
17+
break()
18+
endif()
19+
endforeach()
20+
if (_ARMASM64_EXE STREQUAL "")
21+
message(FATAL_ERROR "armasm64.exe not found under VCToolsInstallDir")
22+
endif()
23+
24+
# Wrapper without spaces to avoid quoting hell on NMake/cmd.exe
25+
set(_WRAP "${CMAKE_BINARY_DIR}/armasm64_wrapper.bat")
26+
file(WRITE "${_WRAP}"
27+
"@echo off\r\n\"${_ARMASM64_EXE}\" %*\r\n")
28+
29+
# Use wrapper as compiler (no spaces in path)
30+
set(CMAKE_ASM_MASM_COMPILER
31+
"${_WRAP}"
32+
CACHE FILEPATH "" FORCE)
33+
34+
# Quote ONLY object and source (compiler path has no spaces now)
35+
set(CMAKE_ASM_MASM_COMPILE_OBJECT
36+
"<CMAKE_ASM_MASM_COMPILER> /nologo -o \"<OBJECT>\" \"<SOURCE>\""
37+
CACHE STRING "" FORCE)
38+
39+
else()
40+
message(FATAL_ERROR "VCToolsInstallDir is not defined. Please run from a Developer Command Prompt or specify armasm64.exe manually.")
41+
endif()
42+
endif()
743

844
add_definitions(-DBH_MALLOC=wasm_runtime_malloc)
945
add_definitions(-DBH_FREE=wasm_runtime_free)
@@ -79,9 +115,37 @@ elseif (WAMR_BUILD_TARGET MATCHES "THUMB.*")
79115
endif ()
80116
elseif (WAMR_BUILD_TARGET MATCHES "AARCH64.*")
81117
if (NOT WAMR_BUILD_SIMD EQUAL 1)
82-
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64.s)
118+
if (WAMR_BUILD_PLATFORM STREQUAL "windows")
119+
if (MSVC)
120+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64.asm)
121+
set(_WAMR_ARM64_MASM_SOURCES ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64.asm)
122+
set_source_files_properties(${_WAMR_ARM64_MASM_SOURCES}
123+
PROPERTIES
124+
LANGUAGE ASM_MASM
125+
COMPILE_DEFINITIONS ""
126+
INCLUDE_DIRECTORIES ""
127+
COMPILE_OPTIONS "/nologo"
128+
)
129+
endif ()
130+
else ()
131+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64.s)
132+
endif ()
83133
else()
84-
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64_simd.s)
134+
if (WAMR_BUILD_PLATFORM STREQUAL "windows")
135+
if (MSVC)
136+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64_simd.asm)
137+
set(_WAMR_ARM64_MASM_SOURCES_SIMD ${IWASM_COMMON_DIR}/arch/invokeNative_armasm64_simd.asm)
138+
set_source_files_properties(${_WAMR_ARM64_MASM_SOURCES_SIMD}
139+
PROPERTIES
140+
LANGUAGE ASM_MASM
141+
COMPILE_DEFINITIONS ""
142+
INCLUDE_DIRECTORIES ""
143+
COMPILE_OPTIONS "/nologo"
144+
)
145+
endif ()
146+
else ()
147+
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_aarch64_simd.s)
148+
endif ()
85149
endif()
86150
elseif (WAMR_BUILD_TARGET STREQUAL "MIPS")
87151
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_mips.s)

0 commit comments

Comments
 (0)