Skip to content

Commit dbcaba6

Browse files
Synthesize invokeNative.c
1 parent e51f96d commit dbcaba6

File tree

6 files changed

+247
-78
lines changed

6 files changed

+247
-78
lines changed
Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
2+
/* DO NOT MODIFY THIS FILE! IT IS AUTOMATICALLY GENERATED BY synthesize-invoke-native */
3+
4+
#if defined(__x86_64__)
5+
/* Copied from https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/common/arch/invokeNative_em64.s */
6+
7+
__asm__(
18
/*
29
* Copyright (C) 2019 Intel Corporation. All rights reserved.
310
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
411
*/
5-
__asm__(
612
" .text\n"
713
" .align 2\n"
814
#ifndef BH_PLATFORM_DARWIN
@@ -13,13 +19,13 @@ __asm__(
1319
".globl _invokeNative\n"
1420
"_invokeNative:\n"
1521
#endif /* end of BH_PLATFORM_DARWIN */
16-
/* rdi - function ptr */
17-
/* rsi - argv */
18-
/* rdx - n_stacks */
19-
"\n"
22+
" \n"/* rdi - function ptr */
23+
" \n"/* rsi - argv */
24+
" \n"/* rdx - n_stacks */
25+
2026
" push %rbp\n"
2127
" mov %rsp, %rbp\n"
22-
"\n"
28+
2329
" mov %rdx, %r10\n"
2430
" mov %rsp, %r11 \n"/* Check that stack is aligned on */
2531
" and $8, %r11 \n"/* 16 bytes. This code may be removed */
@@ -30,7 +36,7 @@ __asm__(
3036
" and $1, %r11 \n"/* stack arguments in case we have an odd */
3137
" shl $3, %r11 \n"/* number of stack arguments */
3238
" sub %r11, %rsp\n"
33-
/* store memory args */
39+
" \n"/* store memory args */
3440
" movq %rdi, %r11 \n"/* func ptr */
3541
" movq %r10, %rcx \n"/* counter */
3642
" lea 64+48-8(%rsi,%rcx,8), %r10\n"
@@ -41,7 +47,7 @@ __asm__(
4147
" push 0(%rsp,%r10)\n"
4248
" loop push_args\n"
4349
"push_args_end:\n"
44-
/* fill all fp args */
50+
" \n"/* fill all fp args */
4551
" movq 0x00(%rsi), %xmm0\n"
4652
" movq 0x08(%rsi), %xmm1\n"
4753
" movq 0x10(%rsi), %xmm2\n"
@@ -50,16 +56,106 @@ __asm__(
5056
" movq 0x28(%rsi), %xmm5\n"
5157
" movq 0x30(%rsi), %xmm6\n"
5258
" movq 0x38(%rsi), %xmm7\n"
53-
"\n"
54-
/* fill all int args */
59+
60+
" \n"/* fill all int args */
5561
" movq 0x40(%rsi), %rdi\n"
5662
" movq 0x50(%rsi), %rdx\n"
5763
" movq 0x58(%rsi), %rcx\n"
5864
" movq 0x60(%rsi), %r8\n"
5965
" movq 0x68(%rsi), %r9\n"
6066
" movq 0x48(%rsi), %rsi\n"
61-
"\n"
67+
6268
" call *%r11\n"
6369
" leave\n"
6470
" ret\n"
71+
72+
73+
);
74+
#elif defined(__aarch64__)
75+
/* Copied from https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/common/arch/invokeNative_aarch64.s */
76+
77+
__asm__(
78+
/*
79+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
80+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
81+
*/
82+
" .text\n"
83+
" .align 2\n"
84+
#ifndef BH_PLATFORM_DARWIN
85+
" .globl invokeNative\n"
86+
" .type invokeNative, function\n"
87+
"invokeNative:\n"
88+
#else
89+
" .globl _invokeNative\n"
90+
"_invokeNative:\n"
91+
#endif /* end of BH_PLATFORM_DARWIN */
92+
93+
/*
94+
* Arguments passed in:
95+
*
96+
* x0 function ptr
97+
* x1 argv
98+
* x2 nstacks
99+
*/
100+
101+
" sub sp, sp, #0x30\n"
102+
" stp x19, x20, [sp, #0x20] \n"/* save the registers */
103+
" stp x21, x22, [sp, #0x10]\n"
104+
" stp x23, x24, [sp, #0x0]\n"
105+
106+
" mov x19, x0 \n"/* x19 = function ptr */
107+
" mov x20, x1 \n"/* x20 = argv */
108+
" mov x21, x2 \n"/* x21 = nstacks */
109+
" mov x22, sp \n"/* save the sp before call function */
110+
111+
" \n"/* Fill in float-point registers */
112+
" ldp d0, d1, [x20], #16 \n"/* d0 = argv[0], d1 = argv[1] */
113+
" ldp d2, d3, [x20], #16 \n"/* d2 = argv[2], d3 = argv[3] */
114+
" ldp d4, d5, [x20], #16 \n"/* d4 = argv[4], d5 = argv[5] */
115+
" ldp d6, d7, [x20], #16 \n"/* d6 = argv[6], d7 = argv[7] */
116+
117+
" \n"/* Fill inteter registers */
118+
" ldp x0, x1, [x20], #16 \n"/* x0 = argv[8] = exec_env, x1 = argv[9] */
119+
" ldp x2, x3, [x20], #16 \n"/* x2 = argv[10], x3 = argv[11] */
120+
" ldp x4, x5, [x20], #16 \n"/* x4 = argv[12], x5 = argv[13] */
121+
" ldp x6, x7, [x20], #16 \n"/* x6 = argv[14], x7 = argv[15] */
122+
123+
" \n"/* Now x20 points to stack args */
124+
125+
" \n"/* Directly call the fucntion if no args in stack */
126+
" cmp x21, #0\n"
127+
" beq call_func\n"
128+
129+
" \n"/* Fill all stack args: reserve stack space and fill one by one */
130+
" mov x23, sp\n"
131+
" bic sp, x23, #15 \n"/* Ensure stack is 16 bytes aligned */
132+
" lsl x23, x21, #3 \n"/* x23 = nstacks * 8 */
133+
" add x23, x23, #15 \n"/* x23 = (x23 + 15) & ~15 */
134+
" bic x23, x23, #15\n"
135+
" sub sp, sp, x23 \n"/* reserved stack space for stack arguments */
136+
" mov x23, sp\n"
137+
138+
"loop_stack_args: \n"/* copy stack arguments to stack */
139+
" cmp x21, #0\n"
140+
" beq call_func\n"
141+
" ldr x24, [x20], #8\n"
142+
" str x24, [x23], #8\n"
143+
" sub x21, x21, #1\n"
144+
" b loop_stack_args\n"
145+
146+
"call_func:\n"
147+
" mov x20, x30 \n"/* save x30(lr) */
148+
" blr x19\n"
149+
" mov sp, x22 \n"/* restore sp which is saved before calling fuction*/
150+
151+
"return:\n"
152+
" mov x30, x20 \n"/* restore x30(lr) */
153+
" ldp x19, x20, [sp, #0x20] \n"/* restore the registers in stack */
154+
" ldp x21, x22, [sp, #0x10]\n"
155+
" ldp x23, x24, [sp, #0x0]\n"
156+
" add sp, sp, #0x30 \n"/* restore sp */
157+
" ret\n"
158+
159+
65160
);
161+
#endif

Sources/wamr-core-linux/invokeNative.c

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../wamr-core-darwin/invokeNative.c

Tools/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/

Tools/Package.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// swift-tools-version:5.3
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Tools",
7+
products: [
8+
.executable(name: "synthesize-invoke-native", targets: ["SynthesizeInvokeNative"]),
9+
],
10+
targets: [
11+
.target(name: "SynthesizeInvokeNative", dependencies: []),
12+
]
13+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class AsmParser {
2+
let contents: String
3+
var output: String
4+
5+
var cursor: String.Index
6+
var startComment: Bool {
7+
contents[cursor] == "/" && contents[contents.index(after: cursor)] == "*"
8+
}
9+
var endComment: Bool {
10+
contents[cursor] == "*" && contents[contents.index(after: cursor)] == "/"
11+
}
12+
13+
var isMacroLine: Bool {
14+
contents[cursor] == "#"
15+
}
16+
17+
init(contents: String) {
18+
self.contents = contents
19+
self.cursor = contents.startIndex
20+
self.output = ""
21+
}
22+
23+
func advanceCursor(offset: Int = 1) {
24+
cursor = contents.index(cursor, offsetBy: offset)
25+
}
26+
func writeOutput<S: StringProtocol>(_ outContent: S) {
27+
output += outContent
28+
}
29+
func writeOutput(_ outContent: Character) {
30+
writeOutput(String(outContent))
31+
}
32+
33+
func parse() {
34+
while cursor < contents.endIndex {
35+
if isMacroLine {
36+
while !contents[cursor].isNewline {
37+
writeOutput(contents[cursor])
38+
advanceCursor(offset: 1)
39+
}
40+
writeOutput(contents[cursor])
41+
advanceCursor(offset: 1) // Skip '\n'
42+
continue
43+
} else if startComment {
44+
while !endComment {
45+
writeOutput(contents[cursor])
46+
advanceCursor(offset: 1)
47+
}
48+
writeOutput(contents[cursor..<contents.index(cursor, offsetBy: 2)])
49+
advanceCursor(offset: 2) // Skip '*/'
50+
continue
51+
} else if contents[cursor].isNewline {
52+
advanceCursor(offset: 1)
53+
writeOutput("\n")
54+
} else {
55+
writeOutput("\"")
56+
while !contents[cursor].isNewline && !startComment {
57+
writeOutput(contents[cursor])
58+
advanceCursor(offset: 1)
59+
}
60+
writeOutput("\\n\"")
61+
if startComment { continue }
62+
advanceCursor(offset: 1)
63+
writeOutput("\n")
64+
}
65+
}
66+
}
67+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Foundation
2+
3+
let supportArchs = [
4+
(macroDef: "__x86_64__", asmFile: "invokeNative_em64.s"),
5+
(macroDef: "__aarch64__", asmFile: "invokeNative_aarch64.s"),
6+
]
7+
8+
let projectDir = URL(fileURLWithPath: #file)
9+
.deletingLastPathComponent()
10+
.deletingLastPathComponent()
11+
.deletingLastPathComponent()
12+
.deletingLastPathComponent()
13+
let iwasmCommonArchDir = projectDir
14+
.appendingPathComponent("third-party/wasm-micro-runtime/core/iwasm/common/arch")
15+
let outputInvokeNativeFile = projectDir
16+
.appendingPathComponent("Sources/wamr-core-darwin/invokeNative.c")
17+
18+
19+
var output = """
20+
21+
/* DO NOT MODIFY THIS FILE! IT IS AUTOMATICALLY GENERATED BY synthesize-invoke-native */
22+
23+
24+
"""
25+
for (index, arch) in supportArchs.enumerated() {
26+
let file = iwasmCommonArchDir.appendingPathComponent(arch.asmFile)
27+
let contents = try String(contentsOf: file)
28+
let parser = AsmParser(contents: contents)
29+
parser.parse()
30+
31+
// prologue
32+
switch index {
33+
case 0:
34+
output += "#if defined(\(arch.macroDef))\n"
35+
default:
36+
output += "#elif defined(\(arch.macroDef))\n"
37+
}
38+
output += """
39+
/* Copied from https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/common/arch/\(arch.asmFile) */
40+
41+
__asm__(
42+
\(parser.output)
43+
);
44+
45+
"""
46+
// epilogue
47+
switch index {
48+
case supportArchs.endIndex - 1:
49+
output += "#endif\n"
50+
default: break
51+
}
52+
}
53+
54+
try output.write(to: outputInvokeNativeFile, atomically: true, encoding: .utf8)

0 commit comments

Comments
 (0)