Skip to content

Commit 1a1a276

Browse files
committed
assembly for masm
1 parent c8cf5e1 commit 1a1a276

File tree

4 files changed

+239
-1
lines changed

4 files changed

+239
-1
lines changed

.github/workflows/build-lib.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build the library
2+
3+
on:
4+
push:
5+
branches:
6+
- "**"
7+
pull_request:
8+
branches:
9+
- "**"
10+
11+
jobs:
12+
build:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
config:
17+
- name: Windows
18+
os: windows-latest
19+
20+
- name: macOS
21+
os: macos-latest
22+
23+
- name: Ubuntu
24+
os: ubuntu-latest
25+
26+
name: ${{ matrix.config.name }}
27+
runs-on: ${{ matrix.config.os }}
28+
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
33+
- name: Configure CMake
34+
run: cmake -B build -S .
35+
36+
- name: Build
37+
run: cmake --build build

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 3.21)
2+
23
project(discord-rpc)
34

45
set(CMAKE_CXX_STANDARD 23)
@@ -10,6 +11,24 @@ target_include_directories(${PROJECT_NAME} PUBLIC include)
1011
# Windows has some extra code
1112
if (WIN32)
1213
target_sources(${PROJECT_NAME} PRIVATE src/platform/windows.cpp)
14+
15+
if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
16+
set(TARGET_MASM_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/platform/wine_masm.asm")
17+
set(TARGET_OBJ_FILE "${CMAKE_CURRENT_BINARY_DIR}/wine_masm.obj")
18+
add_custom_command(
19+
OUTPUT "${TARGET_OBJ_FILE}"
20+
COMMAND ml64 /c /Fo${TARGET_OBJ_FILE} "${TARGET_MASM_FILE}"
21+
DEPENDS "${TARGET_MASM_FILE}"
22+
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
23+
COMMENT "Assembling ${TARGET_MASM_FILE}"
24+
VERBATIM
25+
)
26+
27+
add_custom_target(assemble_wine_masm ALL DEPENDS "${TARGET_OBJ_FILE}")
28+
add_dependencies(${PROJECT_NAME} assemble_wine_masm)
29+
30+
target_link_libraries(${PROJECT_NAME} PRIVATE "${TARGET_OBJ_FILE}")
31+
endif()
1332
endif()
1433

1534
CPMAddPackage("gh:fmtlib/fmt#11.0.2")

src/platform/windows.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
#elif defined(__GNUC__) || defined(__clang__) // x86_64 (GCC/Clang)
88
#include "wine_gcc.hpp"
99
#elif defined(_MSC_VER) // x86_64 (MSVC)
10+
extern "C" {
1011
int wine_close(uint32_t fd);
1112
int wine_open(const char* path, int flags, int mode);
1213
ssize_t wine_read(uint32_t fd, void* buffer, size_t count);
1314
ssize_t wine_write(uint32_t fd, const void* buf, size_t count);
1415
int wine_socket(int domain, int type, int protocol);
1516
int wine_connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
16-
int wine_fcntl(int fd, int cmd, ...);
17+
int wine_fcntl(int fd, int cmd, int arg);
18+
}
1719
#endif
1820
#endif
1921

src/platform/wine_masm.asm

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
; Assembly for Wine on x86_64 using MASM syntax
2+
3+
public wine_close
4+
public wine_open
5+
public wine_read
6+
public wine_write
7+
public wine_socket
8+
public wine_connect
9+
public wine_fcntl
10+
11+
.code
12+
13+
wine_close proc ; int wine_close(uint32_t fd);
14+
push rdi
15+
push rax
16+
17+
mov [rsp+4], ecx
18+
mov edi, [rsp+4]
19+
20+
mov rax, 3
21+
syscall
22+
23+
mov [rsp], eax
24+
mov eax, [rsp]
25+
add rsp, 8
26+
pop rdi
27+
ret
28+
wine_close endp
29+
30+
wine_open proc ; int wine_open(const char* path, int flags, int mode);
31+
push rsi
32+
push rdi
33+
34+
sub rsp, 18h
35+
mov [rsp+14h], r8d
36+
mov [rsp+10h], edx
37+
mov [rsp+8], rcx
38+
39+
mov rdi, [rsp+8]
40+
mov esi, [rsp+10h]
41+
mov edx, [rsp+14h]
42+
43+
mov rax, 2
44+
syscall
45+
46+
mov [rsp+4], eax
47+
mov eax, [rsp+4]
48+
add rsp, 18h
49+
50+
pop rdi
51+
pop rsi
52+
ret
53+
wine_open endp
54+
55+
wine_read proc ; ssize_t wine_read(uint32_t fd, void* buffer, size_t count);
56+
push rsi
57+
push rdi
58+
59+
sub rsp, 20h
60+
mov [rsp+18h], r8
61+
mov [rsp+10h], rdx
62+
mov [rsp+8], ecx
63+
64+
mov edi, [rsp+8]
65+
mov rsi, [rsp+10h]
66+
mov rdx, [rsp+18h]
67+
68+
mov rax, 0
69+
syscall
70+
71+
mov [rsp], rax
72+
mov rax, [rsp]
73+
add rsp, 20h
74+
75+
pop rdi
76+
pop rsi
77+
ret
78+
wine_read endp
79+
80+
wine_write proc ; ssize_t wine_write(uint32_t fd, const void* buf, size_t count);
81+
push rsi
82+
push rdi
83+
84+
sub rsp, 20h
85+
mov [rsp+18h], r8
86+
mov [rsp+10h], rdx
87+
mov [rsp+8], ecx
88+
89+
mov edi, [rsp+8]
90+
mov rsi, [rsp+10h]
91+
mov rdx, [rsp+18h]
92+
93+
mov rax, 1
94+
syscall
95+
96+
mov [rsp], rax
97+
mov rax, [rsp]
98+
add rsp, 20h
99+
100+
pop rdi
101+
pop rsi
102+
ret
103+
wine_write endp
104+
105+
wine_socket proc ; int wine_socket(int domain, int type, int protocol);
106+
push rsi
107+
push rdi
108+
109+
sub rsp, 18h
110+
mov [rsp+14h], r8d
111+
mov [rsp+10h], edx
112+
mov [rsp+8], ecx
113+
114+
mov edi, [rsp+8]
115+
mov esi, [rsp+10h]
116+
mov edx, [rsp+14h]
117+
118+
mov rax, 29h
119+
syscall
120+
121+
mov [rsp+4], eax
122+
mov eax, [rsp+4]
123+
add rsp, 18h
124+
125+
pop rdi
126+
pop rsi
127+
ret
128+
wine_socket endp
129+
130+
wine_connect proc ; int wine_connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
131+
push rsi
132+
push rdi
133+
134+
sub rsp, 18h
135+
mov [rsp+14h], r8d
136+
mov [rsp+8], rdx
137+
mov [rsp+4], ecx
138+
139+
mov edi, [rsp+4]
140+
mov rsi, [rsp+8]
141+
mov edx, [rsp+14h]
142+
143+
mov rax, 2Ah
144+
syscall
145+
146+
mov [rsp], eax
147+
mov eax, [rsp]
148+
add rsp, 18h
149+
150+
pop rdi
151+
pop rsi
152+
ret
153+
wine_connect endp
154+
155+
wine_fcntl proc ; int wine_fcntl(int fd, int cmd, int arg);
156+
push rsi
157+
push rdi
158+
159+
sub rsp, 10h
160+
mov [rsp+20h-14h], r8d
161+
mov [rsp+8], edx
162+
mov [rsp+4], ecx
163+
164+
mov edi, [rsp+4]
165+
mov esi, [rsp+8]
166+
mov edx, [rsp+20h-14h]
167+
168+
mov rax, 48h
169+
syscall
170+
171+
mov [rsp], eax
172+
mov eax, [rsp]
173+
add rsp, 10h
174+
175+
pop rdi
176+
pop rsi
177+
ret
178+
wine_fcntl endp
179+
180+
END

0 commit comments

Comments
 (0)